xref: /aosp_15_r20/external/libvpx/vpx_dsp/psnr.c (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1 /*
2  *  Copyright (c) 2016 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <math.h>
12 #include <assert.h>
13 #include "./vpx_dsp_rtcd.h"
14 #include "vpx_dsp/psnr.h"
15 #include "vpx_scale/yv12config.h"
16 
vpx_sse_to_psnr(double samples,double peak,double sse)17 double vpx_sse_to_psnr(double samples, double peak, double sse) {
18   if (sse > 0.0) {
19     const double psnr = 10.0 * log10(samples * peak * peak / sse);
20     return psnr > MAX_PSNR ? MAX_PSNR : psnr;
21   } else {
22     return MAX_PSNR;
23   }
24 }
25 
26 /* TODO(yaowu): The block_variance calls the unoptimized versions of variance()
27  * and highbd_8_variance(). It should not.
28  */
encoder_sse(const uint8_t * a,int a_stride,const uint8_t * b,int b_stride,int w,int h)29 static int64_t encoder_sse(const uint8_t *a, int a_stride, const uint8_t *b,
30                            int b_stride, int w, int h) {
31   int i, j;
32   int64_t sse = 0;
33 
34   for (i = 0; i < h; i++) {
35     for (j = 0; j < w; j++) {
36       const int diff = a[j] - b[j];
37       sse += diff * diff;
38     }
39 
40     a += a_stride;
41     b += b_stride;
42   }
43 
44   return sse;
45 }
46 
47 #if CONFIG_VP9_HIGHBITDEPTH
encoder_highbd_sse(const uint8_t * a8,int a_stride,const uint8_t * b8,int b_stride,int w,int h)48 static int64_t encoder_highbd_sse(const uint8_t *a8, int a_stride,
49                                   const uint8_t *b8, int b_stride, int w,
50                                   int h) {
51   int i, j;
52   int64_t sse = 0;
53 
54   const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
55   const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
56 
57   for (i = 0; i < h; i++) {
58     for (j = 0; j < w; j++) {
59       const int diff = a[j] - b[j];
60       sse += diff * diff;
61     }
62     a += a_stride;
63     b += b_stride;
64   }
65 
66   return sse;
67 }
68 #endif  // CONFIG_VP9_HIGHBITDEPTH
69 
get_sse(const uint8_t * a,int a_stride,const uint8_t * b,int b_stride,int width,int height)70 static int64_t get_sse(const uint8_t *a, int a_stride, const uint8_t *b,
71                        int b_stride, int width, int height) {
72   const int dw = width % 16;
73   const int dh = height % 16;
74   int64_t total_sse = 0;
75   int x, y;
76 
77   if (dw > 0) {
78     total_sse += encoder_sse(&a[width - dw], a_stride, &b[width - dw], b_stride,
79                              dw, height);
80   }
81 
82   if (dh > 0) {
83     total_sse +=
84         encoder_sse(&a[(height - dh) * a_stride], a_stride,
85                     &b[(height - dh) * b_stride], b_stride, width - dw, dh);
86   }
87 
88   for (y = 0; y < height / 16; ++y) {
89     const uint8_t *pa = a;
90     const uint8_t *pb = b;
91     for (x = 0; x < width / 16; ++x) {
92       total_sse += vpx_sse(pa, a_stride, pb, b_stride, 16, 16);
93 
94       pa += 16;
95       pb += 16;
96     }
97 
98     a += 16 * a_stride;
99     b += 16 * b_stride;
100   }
101 
102   return total_sse;
103 }
104 
105 #if CONFIG_VP9_HIGHBITDEPTH
highbd_get_sse_shift(const uint8_t * a8,int a_stride,const uint8_t * b8,int b_stride,int width,int height,unsigned int input_shift)106 static int64_t highbd_get_sse_shift(const uint8_t *a8, int a_stride,
107                                     const uint8_t *b8, int b_stride, int width,
108                                     int height, unsigned int input_shift) {
109   const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
110   const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
111   int64_t total_sse = 0;
112   int x, y;
113   for (y = 0; y < height; ++y) {
114     for (x = 0; x < width; ++x) {
115       int64_t diff;
116       diff = (a[x] >> input_shift) - (b[x] >> input_shift);
117       total_sse += diff * diff;
118     }
119     a += a_stride;
120     b += b_stride;
121   }
122   return total_sse;
123 }
124 
highbd_get_sse(const uint8_t * a,int a_stride,const uint8_t * b,int b_stride,int width,int height)125 static int64_t highbd_get_sse(const uint8_t *a, int a_stride, const uint8_t *b,
126                               int b_stride, int width, int height) {
127   int64_t total_sse = 0;
128   int x, y;
129   const int dw = width % 16;
130   const int dh = height % 16;
131   if (dw > 0) {
132     total_sse += encoder_highbd_sse(&a[width - dw], a_stride, &b[width - dw],
133                                     b_stride, dw, height);
134   }
135   if (dh > 0) {
136     total_sse += encoder_highbd_sse(&a[(height - dh) * a_stride], a_stride,
137                                     &b[(height - dh) * b_stride], b_stride,
138                                     width - dw, dh);
139   }
140   for (y = 0; y < height / 16; ++y) {
141     const uint8_t *pa = a;
142     const uint8_t *pb = b;
143     for (x = 0; x < width / 16; ++x) {
144       total_sse += vpx_highbd_sse(pa, a_stride, pb, b_stride, 16, 16);
145       pa += 16;
146       pb += 16;
147     }
148     a += 16 * a_stride;
149     b += 16 * b_stride;
150   }
151   return total_sse;
152 }
153 #endif  // CONFIG_VP9_HIGHBITDEPTH
154 
vpx_get_y_sse(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b)155 int64_t vpx_get_y_sse(const YV12_BUFFER_CONFIG *a,
156                       const YV12_BUFFER_CONFIG *b) {
157   assert(a->y_crop_width == b->y_crop_width);
158   assert(a->y_crop_height == b->y_crop_height);
159 
160   return get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
161                  a->y_crop_width, a->y_crop_height);
162 }
163 
164 #if CONFIG_VP9_HIGHBITDEPTH
vpx_highbd_get_y_sse(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b)165 int64_t vpx_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
166                              const YV12_BUFFER_CONFIG *b) {
167   assert(a->y_crop_width == b->y_crop_width);
168   assert(a->y_crop_height == b->y_crop_height);
169   assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
170   assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
171 
172   return highbd_get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
173                         a->y_crop_width, a->y_crop_height);
174 }
175 #endif  // CONFIG_VP9_HIGHBITDEPTH
176 
177 #if CONFIG_VP9_HIGHBITDEPTH
vpx_calc_highbd_psnr(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b,PSNR_STATS * psnr,uint32_t bit_depth,uint32_t in_bit_depth)178 void vpx_calc_highbd_psnr(const YV12_BUFFER_CONFIG *a,
179                           const YV12_BUFFER_CONFIG *b, PSNR_STATS *psnr,
180                           uint32_t bit_depth, uint32_t in_bit_depth) {
181   const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
182   const int heights[3] = { a->y_crop_height, a->uv_crop_height,
183                            a->uv_crop_height };
184   const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer };
185   const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
186   const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer };
187   const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
188   int i;
189   uint64_t total_sse = 0;
190   uint32_t total_samples = 0;
191   const double peak = (double)((1 << in_bit_depth) - 1);
192   const unsigned int input_shift = bit_depth - in_bit_depth;
193 
194   for (i = 0; i < 3; ++i) {
195     const int w = widths[i];
196     const int h = heights[i];
197     const uint32_t samples = w * h;
198     uint64_t sse;
199     if (a->flags & YV12_FLAG_HIGHBITDEPTH) {
200       if (input_shift) {
201         sse = highbd_get_sse_shift(a_planes[i], a_strides[i], b_planes[i],
202                                    b_strides[i], w, h, input_shift);
203       } else {
204         sse = highbd_get_sse(a_planes[i], a_strides[i], b_planes[i],
205                              b_strides[i], w, h);
206       }
207     } else {
208       sse = get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h);
209     }
210     psnr->sse[1 + i] = sse;
211     psnr->samples[1 + i] = samples;
212     psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
213 
214     total_sse += sse;
215     total_samples += samples;
216   }
217 
218   psnr->sse[0] = total_sse;
219   psnr->samples[0] = total_samples;
220   psnr->psnr[0] =
221       vpx_sse_to_psnr((double)total_samples, peak, (double)total_sse);
222 }
223 
224 #endif  // !CONFIG_VP9_HIGHBITDEPTH
225 
vpx_calc_psnr(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b,PSNR_STATS * psnr)226 void vpx_calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
227                    PSNR_STATS *psnr) {
228   static const double peak = 255.0;
229   const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
230   const int heights[3] = { a->y_crop_height, a->uv_crop_height,
231                            a->uv_crop_height };
232   const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer };
233   const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
234   const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer };
235   const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
236   int i;
237   uint64_t total_sse = 0;
238   uint32_t total_samples = 0;
239 
240   for (i = 0; i < 3; ++i) {
241     const int w = widths[i];
242     const int h = heights[i];
243     const uint32_t samples = w * h;
244     const uint64_t sse =
245         get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h);
246     psnr->sse[1 + i] = sse;
247     psnr->samples[1 + i] = samples;
248     psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
249 
250     total_sse += sse;
251     total_samples += samples;
252   }
253 
254   psnr->sse[0] = total_sse;
255   psnr->samples[0] = total_samples;
256   psnr->psnr[0] =
257       vpx_sse_to_psnr((double)total_samples, peak, (double)total_sse);
258 }
259