blob: d543f12d1b0cd3685a678373e828f9574577df95 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * 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 */
Yaowu Xuc27fc142016-08-22 16:08:15 -070011
Yaowu Xuc27fc142016-08-22 16:08:15 -070012#include <assert.h>
Yaowu Xu9c01aa12016-09-01 14:32:49 -070013#include <math.h>
14
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "./aom_dsp_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070016#include "aom_dsp/psnr.h"
17#include "aom_scale/yv12config.h"
18
Yaowu Xuf883b422016-08-30 14:01:10 -070019double aom_sse_to_psnr(double samples, double peak, double sse) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070020 if (sse > 0.0) {
21 const double psnr = 10.0 * log10(samples * peak * peak / sse);
22 return psnr > MAX_PSNR ? MAX_PSNR : psnr;
23 } else {
24 return MAX_PSNR;
25 }
26}
27
28/* TODO(yaowu): The block_variance calls the unoptimized versions of variance()
29* and highbd_8_variance(). It should not.
30*/
31static void encoder_variance(const uint8_t *a, int a_stride, const uint8_t *b,
32 int b_stride, int w, int h, unsigned int *sse,
33 int *sum) {
34 int i, j;
35
36 *sum = 0;
37 *sse = 0;
38
39 for (i = 0; i < h; i++) {
40 for (j = 0; j < w; j++) {
41 const int diff = a[j] - b[j];
42 *sum += diff;
43 *sse += diff * diff;
44 }
45
46 a += a_stride;
47 b += b_stride;
48 }
49}
50
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020051#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070052static void encoder_highbd_variance64(const uint8_t *a8, int a_stride,
53 const uint8_t *b8, int b_stride, int w,
54 int h, uint64_t *sse, int64_t *sum) {
55 int i, j;
56
57 uint16_t *a = CONVERT_TO_SHORTPTR(a8);
58 uint16_t *b = CONVERT_TO_SHORTPTR(b8);
59 *sum = 0;
60 *sse = 0;
61
62 for (i = 0; i < h; i++) {
63 for (j = 0; j < w; j++) {
64 const int diff = a[j] - b[j];
65 *sum += diff;
66 *sse += diff * diff;
67 }
68 a += a_stride;
69 b += b_stride;
70 }
71}
72
73static void encoder_highbd_8_variance(const uint8_t *a8, int a_stride,
74 const uint8_t *b8, int b_stride, int w,
75 int h, unsigned int *sse, int *sum) {
76 uint64_t sse_long = 0;
77 int64_t sum_long = 0;
78 encoder_highbd_variance64(a8, a_stride, b8, b_stride, w, h, &sse_long,
79 &sum_long);
80 *sse = (unsigned int)sse_long;
81 *sum = (int)sum_long;
82}
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020083#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070084
85static int64_t get_sse(const uint8_t *a, int a_stride, const uint8_t *b,
86 int b_stride, int width, int height) {
87 const int dw = width % 16;
88 const int dh = height % 16;
89 int64_t total_sse = 0;
90 unsigned int sse = 0;
91 int sum = 0;
92 int x, y;
93
94 if (dw > 0) {
95 encoder_variance(&a[width - dw], a_stride, &b[width - dw], b_stride, dw,
96 height, &sse, &sum);
97 total_sse += sse;
98 }
99
100 if (dh > 0) {
101 encoder_variance(&a[(height - dh) * a_stride], a_stride,
102 &b[(height - dh) * b_stride], b_stride, width - dw, dh,
103 &sse, &sum);
104 total_sse += sse;
105 }
106
107 for (y = 0; y < height / 16; ++y) {
108 const uint8_t *pa = a;
109 const uint8_t *pb = b;
110 for (x = 0; x < width / 16; ++x) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700111 aom_mse16x16(pa, a_stride, pb, b_stride, &sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700112 total_sse += sse;
113
114 pa += 16;
115 pb += 16;
116 }
117
118 a += 16 * a_stride;
119 b += 16 * b_stride;
120 }
121
122 return total_sse;
123}
124
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200125#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700126static int64_t highbd_get_sse_shift(const uint8_t *a8, int a_stride,
127 const uint8_t *b8, int b_stride, int width,
128 int height, unsigned int input_shift) {
129 const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
130 const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
131 int64_t total_sse = 0;
132 int x, y;
133 for (y = 0; y < height; ++y) {
134 for (x = 0; x < width; ++x) {
135 int64_t diff;
136 diff = (a[x] >> input_shift) - (b[x] >> input_shift);
137 total_sse += diff * diff;
138 }
139 a += a_stride;
140 b += b_stride;
141 }
142 return total_sse;
143}
144
145static int64_t highbd_get_sse(const uint8_t *a, int a_stride, const uint8_t *b,
146 int b_stride, int width, int height) {
147 int64_t total_sse = 0;
148 int x, y;
149 const int dw = width % 16;
150 const int dh = height % 16;
151 unsigned int sse = 0;
152 int sum = 0;
153 if (dw > 0) {
154 encoder_highbd_8_variance(&a[width - dw], a_stride, &b[width - dw],
155 b_stride, dw, height, &sse, &sum);
156 total_sse += sse;
157 }
158 if (dh > 0) {
159 encoder_highbd_8_variance(&a[(height - dh) * a_stride], a_stride,
160 &b[(height - dh) * b_stride], b_stride,
161 width - dw, dh, &sse, &sum);
162 total_sse += sse;
163 }
164 for (y = 0; y < height / 16; ++y) {
165 const uint8_t *pa = a;
166 const uint8_t *pb = b;
167 for (x = 0; x < width / 16; ++x) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700168 aom_highbd_8_mse16x16(pa, a_stride, pb, b_stride, &sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700169 total_sse += sse;
170 pa += 16;
171 pb += 16;
172 }
173 a += 16 * a_stride;
174 b += 16 * b_stride;
175 }
176 return total_sse;
177}
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200178#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700179
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700180int64_t aom_get_y_sse_part(const YV12_BUFFER_CONFIG *a,
clang-formatbda8d612016-09-19 15:55:46 -0700181 const YV12_BUFFER_CONFIG *b, int hstart, int width,
182 int vstart, int height) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700183 return get_sse(a->y_buffer + vstart * a->y_stride + hstart, a->y_stride,
184 b->y_buffer + vstart * b->y_stride + hstart, b->y_stride,
185 width, height);
186}
187
Yaowu Xuf883b422016-08-30 14:01:10 -0700188int64_t aom_get_y_sse(const YV12_BUFFER_CONFIG *a,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700189 const YV12_BUFFER_CONFIG *b) {
190 assert(a->y_crop_width == b->y_crop_width);
191 assert(a->y_crop_height == b->y_crop_height);
192
193 return get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
194 a->y_crop_width, a->y_crop_height);
195}
196
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800197int64_t aom_get_u_sse_part(const YV12_BUFFER_CONFIG *a,
198 const YV12_BUFFER_CONFIG *b, int hstart, int width,
199 int vstart, int height) {
200 return get_sse(a->u_buffer + vstart * a->uv_stride + hstart, a->uv_stride,
201 b->u_buffer + vstart * b->uv_stride + hstart, b->uv_stride,
202 width, height);
203}
204
Yaowu Xuf883b422016-08-30 14:01:10 -0700205int64_t aom_get_u_sse(const YV12_BUFFER_CONFIG *a,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700206 const YV12_BUFFER_CONFIG *b) {
207 assert(a->uv_crop_width == b->uv_crop_width);
208 assert(a->uv_crop_height == b->uv_crop_height);
209
210 return get_sse(a->u_buffer, a->uv_stride, b->u_buffer, b->uv_stride,
211 a->uv_crop_width, a->uv_crop_height);
212}
213
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800214int64_t aom_get_v_sse_part(const YV12_BUFFER_CONFIG *a,
215 const YV12_BUFFER_CONFIG *b, int hstart, int width,
216 int vstart, int height) {
217 return get_sse(a->v_buffer + vstart * a->uv_stride + hstart, a->uv_stride,
218 b->v_buffer + vstart * b->uv_stride + hstart, b->uv_stride,
219 width, height);
220}
221
Yaowu Xuf883b422016-08-30 14:01:10 -0700222int64_t aom_get_v_sse(const YV12_BUFFER_CONFIG *a,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700223 const YV12_BUFFER_CONFIG *b) {
224 assert(a->uv_crop_width == b->uv_crop_width);
225 assert(a->uv_crop_height == b->uv_crop_height);
226
227 return get_sse(a->v_buffer, a->uv_stride, b->v_buffer, b->uv_stride,
228 a->uv_crop_width, a->uv_crop_height);
229}
230
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200231#if CONFIG_HIGHBITDEPTH
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700232int64_t aom_highbd_get_y_sse_part(const YV12_BUFFER_CONFIG *a,
clang-formatbda8d612016-09-19 15:55:46 -0700233 const YV12_BUFFER_CONFIG *b, int hstart,
234 int width, int vstart, int height) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700235 return highbd_get_sse(
236 a->y_buffer + vstart * a->y_stride + hstart, a->y_stride,
clang-formatbda8d612016-09-19 15:55:46 -0700237 b->y_buffer + vstart * b->y_stride + hstart, b->y_stride, width, height);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700238}
239
Yaowu Xuf883b422016-08-30 14:01:10 -0700240int64_t aom_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700241 const YV12_BUFFER_CONFIG *b) {
242 assert(a->y_crop_width == b->y_crop_width);
243 assert(a->y_crop_height == b->y_crop_height);
244 assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
245 assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
246
247 return highbd_get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
248 a->y_crop_width, a->y_crop_height);
249}
250
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800251int64_t aom_highbd_get_u_sse_part(const YV12_BUFFER_CONFIG *a,
252 const YV12_BUFFER_CONFIG *b, int hstart,
253 int width, int vstart, int height) {
254 return highbd_get_sse(a->u_buffer + vstart * a->uv_stride + hstart,
255 a->uv_stride,
256 b->u_buffer + vstart * b->uv_stride + hstart,
257 b->uv_stride, width, height);
258}
259
Yaowu Xuf883b422016-08-30 14:01:10 -0700260int64_t aom_highbd_get_u_sse(const YV12_BUFFER_CONFIG *a,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700261 const YV12_BUFFER_CONFIG *b) {
262 assert(a->uv_crop_width == b->uv_crop_width);
263 assert(a->uv_crop_height == b->uv_crop_height);
264 assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
265 assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
266
267 return highbd_get_sse(a->u_buffer, a->uv_stride, b->u_buffer, b->uv_stride,
268 a->uv_crop_width, a->uv_crop_height);
269}
270
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800271int64_t aom_highbd_get_v_sse_part(const YV12_BUFFER_CONFIG *a,
272 const YV12_BUFFER_CONFIG *b, int hstart,
273 int width, int vstart, int height) {
274 return highbd_get_sse(a->v_buffer + vstart * a->uv_stride + hstart,
275 a->uv_stride,
276 b->v_buffer + vstart * b->uv_stride + hstart,
277 b->uv_stride, width, height);
278}
279
Yaowu Xuf883b422016-08-30 14:01:10 -0700280int64_t aom_highbd_get_v_sse(const YV12_BUFFER_CONFIG *a,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700281 const YV12_BUFFER_CONFIG *b) {
282 assert(a->uv_crop_width == b->uv_crop_width);
283 assert(a->uv_crop_height == b->uv_crop_height);
284 assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
285 assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
286
287 return highbd_get_sse(a->v_buffer, a->uv_stride, b->v_buffer, b->uv_stride,
288 a->uv_crop_width, a->uv_crop_height);
289}
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200290#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700291
Cheng Chen9efbaf92017-07-27 14:09:59 -0700292int64_t aom_get_sse_plane(const YV12_BUFFER_CONFIG *a,
293 const YV12_BUFFER_CONFIG *b, int plane, int highbd) {
294#if CONFIG_HIGHBITDEPTH
295 if (highbd) {
296 switch (plane) {
297 case 0: return aom_highbd_get_y_sse(a, b);
298 case 1: return aom_highbd_get_u_sse(a, b);
299 case 2: return aom_highbd_get_v_sse(a, b);
300 default: assert(plane >= 0 && plane <= 2); return 0;
301 }
302 }
303#endif
304 (void)highbd;
305 switch (plane) {
306 case 0: return aom_get_y_sse(a, b);
307 case 1: return aom_get_u_sse(a, b);
308 case 2: return aom_get_v_sse(a, b);
309 default: assert(plane >= 0 && plane <= 2); return 0;
310 }
311}
312
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200313#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700314void aom_calc_highbd_psnr(const YV12_BUFFER_CONFIG *a,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700315 const YV12_BUFFER_CONFIG *b, PSNR_STATS *psnr,
316 uint32_t bit_depth, uint32_t in_bit_depth) {
317 const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
318 const int heights[3] = { a->y_crop_height, a->uv_crop_height,
319 a->uv_crop_height };
Yaowu Xuc27fc142016-08-22 16:08:15 -0700320 const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
Yaowu Xuc27fc142016-08-22 16:08:15 -0700321 const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
322 int i;
323 uint64_t total_sse = 0;
324 uint32_t total_samples = 0;
325 const double peak = (double)((1 << in_bit_depth) - 1);
326 const unsigned int input_shift = bit_depth - in_bit_depth;
327
328 for (i = 0; i < 3; ++i) {
329 const int w = widths[i];
330 const int h = heights[i];
331 const uint32_t samples = w * h;
332 uint64_t sse;
333 if (a->flags & YV12_FLAG_HIGHBITDEPTH) {
334 if (input_shift) {
Rupert Swarbrick82529d22017-09-20 13:36:39 +0100335 sse = highbd_get_sse_shift(a->buffers[i], a_strides[i], b->buffers[i],
Yaowu Xuc27fc142016-08-22 16:08:15 -0700336 b_strides[i], w, h, input_shift);
337 } else {
Rupert Swarbrick82529d22017-09-20 13:36:39 +0100338 sse = highbd_get_sse(a->buffers[i], a_strides[i], b->buffers[i],
Yaowu Xuc27fc142016-08-22 16:08:15 -0700339 b_strides[i], w, h);
340 }
341 } else {
Rupert Swarbrick82529d22017-09-20 13:36:39 +0100342 sse = get_sse(a->buffers[i], a_strides[i], b->buffers[i], b_strides[i], w,
343 h);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700344 }
345 psnr->sse[1 + i] = sse;
346 psnr->samples[1 + i] = samples;
Yaowu Xuf883b422016-08-30 14:01:10 -0700347 psnr->psnr[1 + i] = aom_sse_to_psnr(samples, peak, (double)sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700348
349 total_sse += sse;
350 total_samples += samples;
351 }
352
353 psnr->sse[0] = total_sse;
354 psnr->samples[0] = total_samples;
355 psnr->psnr[0] =
Yaowu Xuf883b422016-08-30 14:01:10 -0700356 aom_sse_to_psnr((double)total_samples, peak, (double)total_sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700357}
358
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200359#endif // !CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700360
Yaowu Xuf883b422016-08-30 14:01:10 -0700361void aom_calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700362 PSNR_STATS *psnr) {
363 static const double peak = 255.0;
364 const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
365 const int heights[3] = { a->y_crop_height, a->uv_crop_height,
366 a->uv_crop_height };
Yaowu Xuc27fc142016-08-22 16:08:15 -0700367 const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
Yaowu Xuc27fc142016-08-22 16:08:15 -0700368 const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
369 int i;
370 uint64_t total_sse = 0;
371 uint32_t total_samples = 0;
372
373 for (i = 0; i < 3; ++i) {
374 const int w = widths[i];
375 const int h = heights[i];
376 const uint32_t samples = w * h;
377 const uint64_t sse =
Rupert Swarbrick82529d22017-09-20 13:36:39 +0100378 get_sse(a->buffers[i], a_strides[i], b->buffers[i], b_strides[i], w, h);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700379 psnr->sse[1 + i] = sse;
380 psnr->samples[1 + i] = samples;
Yaowu Xuf883b422016-08-30 14:01:10 -0700381 psnr->psnr[1 + i] = aom_sse_to_psnr(samples, peak, (double)sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700382
383 total_sse += sse;
384 total_samples += samples;
385 }
386
387 psnr->sse[0] = total_sse;
388 psnr->samples[0] = total_samples;
389 psnr->psnr[0] =
Yaowu Xuf883b422016-08-30 14:01:10 -0700390 aom_sse_to_psnr((double)total_samples, peak, (double)total_sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700391}