blob: 4414393f6521ab9de7ec107c89c9ab866df1e640 [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
Yaowu Xuf883b422016-08-30 14:01:10 -070051#if CONFIG_AOM_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}
Yaowu Xuf883b422016-08-30 14:01:10 -070083#endif // CONFIG_AOM_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
Yaowu Xuf883b422016-08-30 14:01:10 -0700125#if CONFIG_AOM_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}
Yaowu Xuf883b422016-08-30 14:01:10 -0700178#endif // CONFIG_AOM_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
Yaowu Xuf883b422016-08-30 14:01:10 -0700231#if CONFIG_AOM_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}
Yaowu Xuf883b422016-08-30 14:01:10 -0700290#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700291
Yaowu Xuf883b422016-08-30 14:01:10 -0700292#if CONFIG_AOM_HIGHBITDEPTH
293void aom_calc_highbd_psnr(const YV12_BUFFER_CONFIG *a,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700294 const YV12_BUFFER_CONFIG *b, PSNR_STATS *psnr,
295 uint32_t bit_depth, uint32_t in_bit_depth) {
296 const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
297 const int heights[3] = { a->y_crop_height, a->uv_crop_height,
298 a->uv_crop_height };
299 const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer };
300 const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
301 const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer };
302 const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
303 int i;
304 uint64_t total_sse = 0;
305 uint32_t total_samples = 0;
306 const double peak = (double)((1 << in_bit_depth) - 1);
307 const unsigned int input_shift = bit_depth - in_bit_depth;
308
309 for (i = 0; i < 3; ++i) {
310 const int w = widths[i];
311 const int h = heights[i];
312 const uint32_t samples = w * h;
313 uint64_t sse;
314 if (a->flags & YV12_FLAG_HIGHBITDEPTH) {
315 if (input_shift) {
316 sse = highbd_get_sse_shift(a_planes[i], a_strides[i], b_planes[i],
317 b_strides[i], w, h, input_shift);
318 } else {
319 sse = highbd_get_sse(a_planes[i], a_strides[i], b_planes[i],
320 b_strides[i], w, h);
321 }
322 } else {
323 sse = get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h);
324 }
325 psnr->sse[1 + i] = sse;
326 psnr->samples[1 + i] = samples;
Yaowu Xuf883b422016-08-30 14:01:10 -0700327 psnr->psnr[1 + i] = aom_sse_to_psnr(samples, peak, (double)sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700328
329 total_sse += sse;
330 total_samples += samples;
331 }
332
333 psnr->sse[0] = total_sse;
334 psnr->samples[0] = total_samples;
335 psnr->psnr[0] =
Yaowu Xuf883b422016-08-30 14:01:10 -0700336 aom_sse_to_psnr((double)total_samples, peak, (double)total_sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700337}
338
Yaowu Xuf883b422016-08-30 14:01:10 -0700339#endif // !CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700340
Yaowu Xuf883b422016-08-30 14:01:10 -0700341void aom_calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700342 PSNR_STATS *psnr) {
343 static const double peak = 255.0;
344 const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
345 const int heights[3] = { a->y_crop_height, a->uv_crop_height,
346 a->uv_crop_height };
347 const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer };
348 const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
349 const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer };
350 const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
351 int i;
352 uint64_t total_sse = 0;
353 uint32_t total_samples = 0;
354
355 for (i = 0; i < 3; ++i) {
356 const int w = widths[i];
357 const int h = heights[i];
358 const uint32_t samples = w * h;
359 const uint64_t sse =
360 get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h);
361 psnr->sse[1 + i] = sse;
362 psnr->samples[1 + i] = samples;
Yaowu Xuf883b422016-08-30 14:01:10 -0700363 psnr->psnr[1 + i] = aom_sse_to_psnr(samples, peak, (double)sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700364
365 total_sse += sse;
366 total_samples += samples;
367 }
368
369 psnr->sse[0] = total_sse;
370 psnr->samples[0] = total_samples;
371 psnr->psnr[0] =
Yaowu Xuf883b422016-08-30 14:01:10 -0700372 aom_sse_to_psnr((double)total_samples, peak, (double)total_sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700373}