blob: 93899ba24ab9466d766f43467b16bfb4bdf838db [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
Yaowu Xuf883b422016-08-30 14:01:10 -0700197int64_t aom_get_u_sse(const YV12_BUFFER_CONFIG *a,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700198 const YV12_BUFFER_CONFIG *b) {
199 assert(a->uv_crop_width == b->uv_crop_width);
200 assert(a->uv_crop_height == b->uv_crop_height);
201
202 return get_sse(a->u_buffer, a->uv_stride, b->u_buffer, b->uv_stride,
203 a->uv_crop_width, a->uv_crop_height);
204}
205
Yaowu Xuf883b422016-08-30 14:01:10 -0700206int64_t aom_get_v_sse(const YV12_BUFFER_CONFIG *a,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700207 const YV12_BUFFER_CONFIG *b) {
208 assert(a->uv_crop_width == b->uv_crop_width);
209 assert(a->uv_crop_height == b->uv_crop_height);
210
211 return get_sse(a->v_buffer, a->uv_stride, b->v_buffer, b->uv_stride,
212 a->uv_crop_width, a->uv_crop_height);
213}
214
Yaowu Xuf883b422016-08-30 14:01:10 -0700215#if CONFIG_AOM_HIGHBITDEPTH
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700216int64_t aom_highbd_get_y_sse_part(const YV12_BUFFER_CONFIG *a,
clang-formatbda8d612016-09-19 15:55:46 -0700217 const YV12_BUFFER_CONFIG *b, int hstart,
218 int width, int vstart, int height) {
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700219 return highbd_get_sse(
220 a->y_buffer + vstart * a->y_stride + hstart, a->y_stride,
clang-formatbda8d612016-09-19 15:55:46 -0700221 b->y_buffer + vstart * b->y_stride + hstart, b->y_stride, width, height);
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -0700222}
223
Yaowu Xuf883b422016-08-30 14:01:10 -0700224int64_t aom_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700225 const YV12_BUFFER_CONFIG *b) {
226 assert(a->y_crop_width == b->y_crop_width);
227 assert(a->y_crop_height == b->y_crop_height);
228 assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
229 assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
230
231 return highbd_get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
232 a->y_crop_width, a->y_crop_height);
233}
234
Yaowu Xuf883b422016-08-30 14:01:10 -0700235int64_t aom_highbd_get_u_sse(const YV12_BUFFER_CONFIG *a,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700236 const YV12_BUFFER_CONFIG *b) {
237 assert(a->uv_crop_width == b->uv_crop_width);
238 assert(a->uv_crop_height == b->uv_crop_height);
239 assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
240 assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
241
242 return highbd_get_sse(a->u_buffer, a->uv_stride, b->u_buffer, b->uv_stride,
243 a->uv_crop_width, a->uv_crop_height);
244}
245
Yaowu Xuf883b422016-08-30 14:01:10 -0700246int64_t aom_highbd_get_v_sse(const YV12_BUFFER_CONFIG *a,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700247 const YV12_BUFFER_CONFIG *b) {
248 assert(a->uv_crop_width == b->uv_crop_width);
249 assert(a->uv_crop_height == b->uv_crop_height);
250 assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
251 assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
252
253 return highbd_get_sse(a->v_buffer, a->uv_stride, b->v_buffer, b->uv_stride,
254 a->uv_crop_width, a->uv_crop_height);
255}
Yaowu Xuf883b422016-08-30 14:01:10 -0700256#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700257
Yaowu Xuf883b422016-08-30 14:01:10 -0700258#if CONFIG_AOM_HIGHBITDEPTH
259void aom_calc_highbd_psnr(const YV12_BUFFER_CONFIG *a,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700260 const YV12_BUFFER_CONFIG *b, PSNR_STATS *psnr,
261 uint32_t bit_depth, uint32_t in_bit_depth) {
262 const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
263 const int heights[3] = { a->y_crop_height, a->uv_crop_height,
264 a->uv_crop_height };
265 const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer };
266 const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
267 const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer };
268 const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
269 int i;
270 uint64_t total_sse = 0;
271 uint32_t total_samples = 0;
272 const double peak = (double)((1 << in_bit_depth) - 1);
273 const unsigned int input_shift = bit_depth - in_bit_depth;
274
275 for (i = 0; i < 3; ++i) {
276 const int w = widths[i];
277 const int h = heights[i];
278 const uint32_t samples = w * h;
279 uint64_t sse;
280 if (a->flags & YV12_FLAG_HIGHBITDEPTH) {
281 if (input_shift) {
282 sse = highbd_get_sse_shift(a_planes[i], a_strides[i], b_planes[i],
283 b_strides[i], w, h, input_shift);
284 } else {
285 sse = highbd_get_sse(a_planes[i], a_strides[i], b_planes[i],
286 b_strides[i], w, h);
287 }
288 } else {
289 sse = get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h);
290 }
291 psnr->sse[1 + i] = sse;
292 psnr->samples[1 + i] = samples;
Yaowu Xuf883b422016-08-30 14:01:10 -0700293 psnr->psnr[1 + i] = aom_sse_to_psnr(samples, peak, (double)sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700294
295 total_sse += sse;
296 total_samples += samples;
297 }
298
299 psnr->sse[0] = total_sse;
300 psnr->samples[0] = total_samples;
301 psnr->psnr[0] =
Yaowu Xuf883b422016-08-30 14:01:10 -0700302 aom_sse_to_psnr((double)total_samples, peak, (double)total_sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700303}
304
Yaowu Xuf883b422016-08-30 14:01:10 -0700305#endif // !CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700306
Yaowu Xuf883b422016-08-30 14:01:10 -0700307void aom_calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700308 PSNR_STATS *psnr) {
309 static const double peak = 255.0;
310 const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
311 const int heights[3] = { a->y_crop_height, a->uv_crop_height,
312 a->uv_crop_height };
313 const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer };
314 const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
315 const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer };
316 const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
317 int i;
318 uint64_t total_sse = 0;
319 uint32_t total_samples = 0;
320
321 for (i = 0; i < 3; ++i) {
322 const int w = widths[i];
323 const int h = heights[i];
324 const uint32_t samples = w * h;
325 const uint64_t sse =
326 get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h);
327 psnr->sse[1 + i] = sse;
328 psnr->samples[1 + i] = samples;
Yaowu Xuf883b422016-08-30 14:01:10 -0700329 psnr->psnr[1 + i] = aom_sse_to_psnr(samples, peak, (double)sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700330
331 total_sse += sse;
332 total_samples += samples;
333 }
334
335 psnr->sse[0] = total_sse;
336 psnr->samples[0] = total_samples;
337 psnr->psnr[0] =
Yaowu Xuf883b422016-08-30 14:01:10 -0700338 aom_sse_to_psnr((double)total_samples, peak, (double)total_sse);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700339}