1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include <assert.h>
13 #include <math.h>
14
15 #include "config/aom_dsp_rtcd.h"
16
17 #include "aom_dsp/psnr.h"
18 #include "aom_scale/yv12config.h"
19
aom_sse_to_psnr(double samples,double peak,double sse)20 double aom_sse_to_psnr(double samples, double peak, double sse) {
21 if (sse > 0.0) {
22 const double psnr = 10.0 * log10(samples * peak * peak / sse);
23 return psnr > MAX_PSNR ? MAX_PSNR : psnr;
24 } else {
25 return MAX_PSNR;
26 }
27 }
28
encoder_variance(const uint8_t * a,int a_stride,const uint8_t * b,int b_stride,int w,int h,unsigned int * sse,int * sum)29 static void encoder_variance(const uint8_t *a, int a_stride, const uint8_t *b,
30 int b_stride, int w, int h, unsigned int *sse,
31 int *sum) {
32 int i, j;
33
34 *sum = 0;
35 *sse = 0;
36
37 for (i = 0; i < h; i++) {
38 for (j = 0; j < w; j++) {
39 const int diff = a[j] - b[j];
40 *sum += diff;
41 *sse += diff * diff;
42 }
43
44 a += a_stride;
45 b += b_stride;
46 }
47 }
48
49 #if CONFIG_AV1_HIGHBITDEPTH
encoder_highbd_variance64(const uint8_t * a8,int a_stride,const uint8_t * b8,int b_stride,int w,int h,uint64_t * sse,int64_t * sum)50 static void encoder_highbd_variance64(const uint8_t *a8, int a_stride,
51 const uint8_t *b8, int b_stride, int w,
52 int h, uint64_t *sse, int64_t *sum) {
53 const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
54 const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
55 int64_t tsum = 0;
56 uint64_t tsse = 0;
57 for (int i = 0; i < h; ++i) {
58 int32_t lsum = 0;
59 for (int j = 0; j < w; ++j) {
60 const int diff = a[j] - b[j];
61 lsum += diff;
62 tsse += (uint32_t)(diff * diff);
63 }
64 tsum += lsum;
65 a += a_stride;
66 b += b_stride;
67 }
68 *sum = tsum;
69 *sse = tsse;
70 }
71
encoder_highbd_8_variance(const uint8_t * a8,int a_stride,const uint8_t * b8,int b_stride,int w,int h,unsigned int * sse,int * sum)72 static void encoder_highbd_8_variance(const uint8_t *a8, int a_stride,
73 const uint8_t *b8, int b_stride, int w,
74 int h, unsigned int *sse, int *sum) {
75 uint64_t sse_long = 0;
76 int64_t sum_long = 0;
77 encoder_highbd_variance64(a8, a_stride, b8, b_stride, w, h, &sse_long,
78 &sum_long);
79 *sse = (unsigned int)sse_long;
80 *sum = (int)sum_long;
81 }
82 #endif // CONFIG_AV1_HIGHBITDEPTH
83
get_sse(const uint8_t * a,int a_stride,const uint8_t * b,int b_stride,int width,int height)84 static int64_t get_sse(const uint8_t *a, int a_stride, const uint8_t *b,
85 int b_stride, int width, int height) {
86 const int dw = width % 16;
87 const int dh = height % 16;
88 int64_t total_sse = 0;
89 unsigned int sse = 0;
90 int sum = 0;
91 int x, y;
92
93 if (dw > 0) {
94 encoder_variance(&a[width - dw], a_stride, &b[width - dw], b_stride, dw,
95 height, &sse, &sum);
96 total_sse += sse;
97 }
98
99 if (dh > 0) {
100 encoder_variance(&a[(height - dh) * a_stride], a_stride,
101 &b[(height - dh) * b_stride], b_stride, width - dw, dh,
102 &sse, &sum);
103 total_sse += sse;
104 }
105
106 for (y = 0; y < height / 16; ++y) {
107 const uint8_t *pa = a;
108 const uint8_t *pb = b;
109 for (x = 0; x < width / 16; ++x) {
110 aom_mse16x16(pa, a_stride, pb, b_stride, &sse);
111 total_sse += sse;
112
113 pa += 16;
114 pb += 16;
115 }
116
117 a += 16 * a_stride;
118 b += 16 * b_stride;
119 }
120
121 return total_sse;
122 }
123
124 #if CONFIG_AV1_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)125 static int64_t highbd_get_sse_shift(const uint8_t *a8, int a_stride,
126 const uint8_t *b8, int b_stride, int width,
127 int height, unsigned int input_shift) {
128 const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
129 const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
130 int64_t total_sse = 0;
131 int x, y;
132 for (y = 0; y < height; ++y) {
133 for (x = 0; x < width; ++x) {
134 int64_t diff;
135 diff = (a[x] >> input_shift) - (b[x] >> input_shift);
136 total_sse += diff * diff;
137 }
138 a += a_stride;
139 b += b_stride;
140 }
141 return total_sse;
142 }
143
highbd_get_sse(const uint8_t * a,int a_stride,const uint8_t * b,int b_stride,int width,int height)144 static int64_t highbd_get_sse(const uint8_t *a, int a_stride, const uint8_t *b,
145 int b_stride, int width, int height) {
146 int64_t total_sse = 0;
147 int x, y;
148 const int dw = width % 16;
149 const int dh = height % 16;
150 unsigned int sse = 0;
151 int sum = 0;
152 if (dw > 0) {
153 encoder_highbd_8_variance(&a[width - dw], a_stride, &b[width - dw],
154 b_stride, dw, height, &sse, &sum);
155 total_sse += sse;
156 }
157 if (dh > 0) {
158 encoder_highbd_8_variance(&a[(height - dh) * a_stride], a_stride,
159 &b[(height - dh) * b_stride], b_stride,
160 width - dw, dh, &sse, &sum);
161 total_sse += sse;
162 }
163 for (y = 0; y < height / 16; ++y) {
164 const uint8_t *pa = a;
165 const uint8_t *pb = b;
166 for (x = 0; x < width / 16; ++x) {
167 aom_highbd_8_mse16x16(pa, a_stride, pb, b_stride, &sse);
168 total_sse += sse;
169 pa += 16;
170 pb += 16;
171 }
172 a += 16 * a_stride;
173 b += 16 * b_stride;
174 }
175 return total_sse;
176 }
177 #endif // CONFIG_AV1_HIGHBITDEPTH
178
aom_get_y_var(const YV12_BUFFER_CONFIG * a,int hstart,int width,int vstart,int height)179 uint64_t aom_get_y_var(const YV12_BUFFER_CONFIG *a, int hstart, int width,
180 int vstart, int height) {
181 return aom_var_2d_u8(a->y_buffer + vstart * a->y_stride + hstart, a->y_stride,
182 width, height) /
183 (width * height);
184 }
185
aom_get_u_var(const YV12_BUFFER_CONFIG * a,int hstart,int width,int vstart,int height)186 uint64_t aom_get_u_var(const YV12_BUFFER_CONFIG *a, int hstart, int width,
187 int vstart, int height) {
188 return aom_var_2d_u8(a->u_buffer + vstart * a->uv_stride + hstart,
189 a->uv_stride, width, height) /
190 (width * height);
191 }
192
aom_get_v_var(const YV12_BUFFER_CONFIG * a,int hstart,int width,int vstart,int height)193 uint64_t aom_get_v_var(const YV12_BUFFER_CONFIG *a, int hstart, int width,
194 int vstart, int height) {
195 return aom_var_2d_u8(a->v_buffer + vstart * a->uv_stride + hstart,
196 a->uv_stride, width, height) /
197 (width * height);
198 }
199
aom_get_y_sse_part(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b,int hstart,int width,int vstart,int height)200 int64_t aom_get_y_sse_part(const YV12_BUFFER_CONFIG *a,
201 const YV12_BUFFER_CONFIG *b, int hstart, int width,
202 int vstart, int height) {
203 return get_sse(a->y_buffer + vstart * a->y_stride + hstart, a->y_stride,
204 b->y_buffer + vstart * b->y_stride + hstart, b->y_stride,
205 width, height);
206 }
207
aom_get_y_sse(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b)208 int64_t aom_get_y_sse(const YV12_BUFFER_CONFIG *a,
209 const YV12_BUFFER_CONFIG *b) {
210 assert(a->y_crop_width == b->y_crop_width);
211 assert(a->y_crop_height == b->y_crop_height);
212
213 return get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
214 a->y_crop_width, a->y_crop_height);
215 }
216
aom_get_u_sse_part(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b,int hstart,int width,int vstart,int height)217 int64_t aom_get_u_sse_part(const YV12_BUFFER_CONFIG *a,
218 const YV12_BUFFER_CONFIG *b, int hstart, int width,
219 int vstart, int height) {
220 return get_sse(a->u_buffer + vstart * a->uv_stride + hstart, a->uv_stride,
221 b->u_buffer + vstart * b->uv_stride + hstart, b->uv_stride,
222 width, height);
223 }
224
aom_get_u_sse(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b)225 int64_t aom_get_u_sse(const YV12_BUFFER_CONFIG *a,
226 const YV12_BUFFER_CONFIG *b) {
227 assert(a->uv_crop_width == b->uv_crop_width);
228 assert(a->uv_crop_height == b->uv_crop_height);
229
230 return get_sse(a->u_buffer, a->uv_stride, b->u_buffer, b->uv_stride,
231 a->uv_crop_width, a->uv_crop_height);
232 }
233
aom_get_v_sse_part(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b,int hstart,int width,int vstart,int height)234 int64_t aom_get_v_sse_part(const YV12_BUFFER_CONFIG *a,
235 const YV12_BUFFER_CONFIG *b, int hstart, int width,
236 int vstart, int height) {
237 return get_sse(a->v_buffer + vstart * a->uv_stride + hstart, a->uv_stride,
238 b->v_buffer + vstart * b->uv_stride + hstart, b->uv_stride,
239 width, height);
240 }
241
aom_get_v_sse(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b)242 int64_t aom_get_v_sse(const YV12_BUFFER_CONFIG *a,
243 const YV12_BUFFER_CONFIG *b) {
244 assert(a->uv_crop_width == b->uv_crop_width);
245 assert(a->uv_crop_height == b->uv_crop_height);
246
247 return get_sse(a->v_buffer, a->uv_stride, b->v_buffer, b->uv_stride,
248 a->uv_crop_width, a->uv_crop_height);
249 }
250
251 #if CONFIG_AV1_HIGHBITDEPTH
aom_highbd_get_y_var(const YV12_BUFFER_CONFIG * a,int hstart,int width,int vstart,int height)252 uint64_t aom_highbd_get_y_var(const YV12_BUFFER_CONFIG *a, int hstart,
253 int width, int vstart, int height) {
254 return aom_var_2d_u16(a->y_buffer + vstart * a->y_stride + hstart,
255 a->y_stride, width, height) /
256 (width * height);
257 }
258
aom_highbd_get_u_var(const YV12_BUFFER_CONFIG * a,int hstart,int width,int vstart,int height)259 uint64_t aom_highbd_get_u_var(const YV12_BUFFER_CONFIG *a, int hstart,
260 int width, int vstart, int height) {
261 return aom_var_2d_u16(a->u_buffer + vstart * a->uv_stride + hstart,
262 a->uv_stride, width, height) /
263 (width * height);
264 }
265
aom_highbd_get_v_var(const YV12_BUFFER_CONFIG * a,int hstart,int width,int vstart,int height)266 uint64_t aom_highbd_get_v_var(const YV12_BUFFER_CONFIG *a, int hstart,
267 int width, int vstart, int height) {
268 return aom_var_2d_u16(a->v_buffer + vstart * a->uv_stride + hstart,
269 a->uv_stride, width, height) /
270 (width * height);
271 }
272
aom_highbd_get_y_sse_part(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b,int hstart,int width,int vstart,int height)273 int64_t aom_highbd_get_y_sse_part(const YV12_BUFFER_CONFIG *a,
274 const YV12_BUFFER_CONFIG *b, int hstart,
275 int width, int vstart, int height) {
276 return highbd_get_sse(
277 a->y_buffer + vstart * a->y_stride + hstart, a->y_stride,
278 b->y_buffer + vstart * b->y_stride + hstart, b->y_stride, width, height);
279 }
280
aom_highbd_get_y_sse(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b)281 int64_t aom_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
282 const YV12_BUFFER_CONFIG *b) {
283 assert(a->y_crop_width == b->y_crop_width);
284 assert(a->y_crop_height == b->y_crop_height);
285 assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
286 assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
287
288 return highbd_get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
289 a->y_crop_width, a->y_crop_height);
290 }
291
aom_highbd_get_u_sse_part(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b,int hstart,int width,int vstart,int height)292 int64_t aom_highbd_get_u_sse_part(const YV12_BUFFER_CONFIG *a,
293 const YV12_BUFFER_CONFIG *b, int hstart,
294 int width, int vstart, int height) {
295 return highbd_get_sse(a->u_buffer + vstart * a->uv_stride + hstart,
296 a->uv_stride,
297 b->u_buffer + vstart * b->uv_stride + hstart,
298 b->uv_stride, width, height);
299 }
300
aom_highbd_get_u_sse(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b)301 int64_t aom_highbd_get_u_sse(const YV12_BUFFER_CONFIG *a,
302 const YV12_BUFFER_CONFIG *b) {
303 assert(a->uv_crop_width == b->uv_crop_width);
304 assert(a->uv_crop_height == b->uv_crop_height);
305 assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
306 assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
307
308 return highbd_get_sse(a->u_buffer, a->uv_stride, b->u_buffer, b->uv_stride,
309 a->uv_crop_width, a->uv_crop_height);
310 }
311
aom_highbd_get_v_sse_part(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b,int hstart,int width,int vstart,int height)312 int64_t aom_highbd_get_v_sse_part(const YV12_BUFFER_CONFIG *a,
313 const YV12_BUFFER_CONFIG *b, int hstart,
314 int width, int vstart, int height) {
315 return highbd_get_sse(a->v_buffer + vstart * a->uv_stride + hstart,
316 a->uv_stride,
317 b->v_buffer + vstart * b->uv_stride + hstart,
318 b->uv_stride, width, height);
319 }
320
aom_highbd_get_v_sse(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b)321 int64_t aom_highbd_get_v_sse(const YV12_BUFFER_CONFIG *a,
322 const YV12_BUFFER_CONFIG *b) {
323 assert(a->uv_crop_width == b->uv_crop_width);
324 assert(a->uv_crop_height == b->uv_crop_height);
325 assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
326 assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
327
328 return highbd_get_sse(a->v_buffer, a->uv_stride, b->v_buffer, b->uv_stride,
329 a->uv_crop_width, a->uv_crop_height);
330 }
331 #endif // CONFIG_AV1_HIGHBITDEPTH
332
aom_get_sse_plane(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b,int plane,int highbd)333 int64_t aom_get_sse_plane(const YV12_BUFFER_CONFIG *a,
334 const YV12_BUFFER_CONFIG *b, int plane, int highbd) {
335 #if CONFIG_AV1_HIGHBITDEPTH
336 if (highbd) {
337 switch (plane) {
338 case 0: return aom_highbd_get_y_sse(a, b);
339 case 1: return aom_highbd_get_u_sse(a, b);
340 case 2: return aom_highbd_get_v_sse(a, b);
341 default: assert(plane >= 0 && plane <= 2); return 0;
342 }
343 } else {
344 switch (plane) {
345 case 0: return aom_get_y_sse(a, b);
346 case 1: return aom_get_u_sse(a, b);
347 case 2: return aom_get_v_sse(a, b);
348 default: assert(plane >= 0 && plane <= 2); return 0;
349 }
350 }
351 #else
352 (void)highbd;
353 switch (plane) {
354 case 0: return aom_get_y_sse(a, b);
355 case 1: return aom_get_u_sse(a, b);
356 case 2: return aom_get_v_sse(a, b);
357 default: assert(plane >= 0 && plane <= 2); return 0;
358 }
359 #endif
360 }
361
362 #if CONFIG_AV1_HIGHBITDEPTH
aom_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)363 void aom_calc_highbd_psnr(const YV12_BUFFER_CONFIG *a,
364 const YV12_BUFFER_CONFIG *b, PSNR_STATS *psnr,
365 uint32_t bit_depth, uint32_t in_bit_depth) {
366 const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
367 const int heights[3] = { a->y_crop_height, a->uv_crop_height,
368 a->uv_crop_height };
369 const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
370 const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
371 int i;
372 uint64_t total_sse = 0;
373 uint32_t total_samples = 0;
374 const double peak = (double)((1 << in_bit_depth) - 1);
375 const unsigned int input_shift = bit_depth - in_bit_depth;
376
377 for (i = 0; i < 3; ++i) {
378 const int w = widths[i];
379 const int h = heights[i];
380 const uint32_t samples = w * h;
381 uint64_t sse;
382 if (a->flags & YV12_FLAG_HIGHBITDEPTH) {
383 if (input_shift) {
384 sse = highbd_get_sse_shift(a->buffers[i], a_strides[i], b->buffers[i],
385 b_strides[i], w, h, input_shift);
386 } else {
387 sse = highbd_get_sse(a->buffers[i], a_strides[i], b->buffers[i],
388 b_strides[i], w, h);
389 }
390 } else {
391 sse = get_sse(a->buffers[i], a_strides[i], b->buffers[i], b_strides[i], w,
392 h);
393 }
394 psnr->sse[1 + i] = sse;
395 psnr->samples[1 + i] = samples;
396 psnr->psnr[1 + i] = aom_sse_to_psnr(samples, peak, (double)sse);
397
398 total_sse += sse;
399 total_samples += samples;
400 }
401
402 psnr->sse[0] = total_sse;
403 psnr->samples[0] = total_samples;
404 psnr->psnr[0] =
405 aom_sse_to_psnr((double)total_samples, peak, (double)total_sse);
406 }
407 #endif
408
aom_calc_psnr(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b,PSNR_STATS * psnr)409 void aom_calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
410 PSNR_STATS *psnr) {
411 static const double peak = 255.0;
412 const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
413 const int heights[3] = { a->y_crop_height, a->uv_crop_height,
414 a->uv_crop_height };
415 const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
416 const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
417 int i;
418 uint64_t total_sse = 0;
419 uint32_t total_samples = 0;
420
421 for (i = 0; i < 3; ++i) {
422 const int w = widths[i];
423 const int h = heights[i];
424 const uint32_t samples = w * h;
425 const uint64_t sse =
426 get_sse(a->buffers[i], a_strides[i], b->buffers[i], b_strides[i], w, h);
427 psnr->sse[1 + i] = sse;
428 psnr->samples[1 + i] = samples;
429 psnr->psnr[1 + i] = aom_sse_to_psnr(samples, peak, (double)sse);
430
431 total_sse += sse;
432 total_samples += samples;
433 }
434
435 psnr->sse[0] = total_sse;
436 psnr->samples[0] = total_samples;
437 psnr->psnr[0] =
438 aom_sse_to_psnr((double)total_samples, peak, (double)total_sse);
439 }
440