blob: 47afd4388abfba8d05e9295c1ee6ffd9d6219688 [file] [log] [blame]
Yaowu Xu75385012016-02-17 08:42:56 -08001/*
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
Yaowu Xu75385012016-02-17 08:42:56 -080017double 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*/
clang-format1214cee2016-08-08 22:59:08 -070029static 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) {
Yaowu Xu75385012016-02-17 08:42:56 -080032 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_VP9_HIGHBITDEPTH
clang-format1214cee2016-08-08 22:59:08 -070050static 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) {
Yaowu Xu75385012016-02-17 08:42:56 -080053 int i, j;
54
55 uint16_t *a = CONVERT_TO_SHORTPTR(a8);
56 uint16_t *b = CONVERT_TO_SHORTPTR(b8);
57 *sum = 0;
58 *sse = 0;
59
60 for (i = 0; i < h; i++) {
61 for (j = 0; j < w; j++) {
62 const int diff = a[j] - b[j];
63 *sum += diff;
64 *sse += diff * diff;
65 }
66 a += a_stride;
67 b += b_stride;
68 }
69}
70
clang-format1214cee2016-08-08 22:59:08 -070071static void encoder_highbd_8_variance(const uint8_t *a8, int a_stride,
72 const uint8_t *b8, int b_stride, int w,
73 int h, unsigned int *sse, int *sum) {
Yaowu Xu75385012016-02-17 08:42:56 -080074 uint64_t sse_long = 0;
Johann2967bf32016-06-22 16:08:10 -070075 int64_t sum_long = 0;
clang-format1214cee2016-08-08 22:59:08 -070076 encoder_highbd_variance64(a8, a_stride, b8, b_stride, w, h, &sse_long,
77 &sum_long);
Yaowu Xu75385012016-02-17 08:42:56 -080078 *sse = (unsigned int)sse_long;
79 *sum = (int)sum_long;
80}
81#endif // CONFIG_VP9_HIGHBITDEPTH
82
clang-format1214cee2016-08-08 22:59:08 -070083static int64_t get_sse(const uint8_t *a, int a_stride, const uint8_t *b,
84 int b_stride, int width, int height) {
Yaowu Xu75385012016-02-17 08:42:56 -080085 const int dw = width % 16;
86 const int dh = height % 16;
87 int64_t total_sse = 0;
88 unsigned int sse = 0;
89 int sum = 0;
90 int x, y;
91
92 if (dw > 0) {
clang-format1214cee2016-08-08 22:59:08 -070093 encoder_variance(&a[width - dw], a_stride, &b[width - dw], b_stride, dw,
94 height, &sse, &sum);
Yaowu Xu75385012016-02-17 08:42:56 -080095 total_sse += sse;
96 }
97
98 if (dh > 0) {
99 encoder_variance(&a[(height - dh) * a_stride], a_stride,
clang-format1214cee2016-08-08 22:59:08 -0700100 &b[(height - dh) * b_stride], b_stride, width - dw, dh,
101 &sse, &sum);
Yaowu Xu75385012016-02-17 08:42:56 -0800102 total_sse += sse;
103 }
104
105 for (y = 0; y < height / 16; ++y) {
106 const uint8_t *pa = a;
107 const uint8_t *pb = b;
108 for (x = 0; x < width / 16; ++x) {
109 vpx_mse16x16(pa, a_stride, pb, b_stride, &sse);
110 total_sse += sse;
111
112 pa += 16;
113 pb += 16;
114 }
115
116 a += 16 * a_stride;
117 b += 16 * b_stride;
118 }
119
120 return total_sse;
121}
122
123#if CONFIG_VP9_HIGHBITDEPTH
Yaowu Xu38cfc452016-02-22 15:24:25 -0800124static int64_t highbd_get_sse_shift(const uint8_t *a8, int a_stride,
clang-format1214cee2016-08-08 22:59:08 -0700125 const uint8_t *b8, int b_stride, int width,
126 int height, unsigned int input_shift) {
Yaowu Xu75385012016-02-17 08:42:56 -0800127 const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
128 const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
129 int64_t total_sse = 0;
130 int x, y;
131 for (y = 0; y < height; ++y) {
132 for (x = 0; x < width; ++x) {
133 int64_t diff;
134 diff = (a[x] >> input_shift) - (b[x] >> input_shift);
135 total_sse += diff * diff;
136 }
137 a += a_stride;
138 b += b_stride;
139 }
140 return total_sse;
141}
142
clang-format1214cee2016-08-08 22:59:08 -0700143static int64_t highbd_get_sse(const uint8_t *a, int a_stride, const uint8_t *b,
144 int b_stride, int width, int height) {
Yaowu Xu75385012016-02-17 08:42:56 -0800145 int64_t total_sse = 0;
146 int x, y;
147 const int dw = width % 16;
148 const int dh = height % 16;
149 unsigned int sse = 0;
150 int sum = 0;
151 if (dw > 0) {
clang-format1214cee2016-08-08 22:59:08 -0700152 encoder_highbd_8_variance(&a[width - dw], a_stride, &b[width - dw],
153 b_stride, dw, height, &sse, &sum);
Yaowu Xu75385012016-02-17 08:42:56 -0800154 total_sse += sse;
155 }
156 if (dh > 0) {
157 encoder_highbd_8_variance(&a[(height - dh) * a_stride], a_stride,
clang-format1214cee2016-08-08 22:59:08 -0700158 &b[(height - dh) * b_stride], b_stride,
159 width - dw, dh, &sse, &sum);
Yaowu Xu75385012016-02-17 08:42:56 -0800160 total_sse += sse;
161 }
162 for (y = 0; y < height / 16; ++y) {
163 const uint8_t *pa = a;
164 const uint8_t *pb = b;
165 for (x = 0; x < width / 16; ++x) {
166 vpx_highbd_8_mse16x16(pa, a_stride, pb, b_stride, &sse);
167 total_sse += sse;
168 pa += 16;
169 pb += 16;
170 }
171 a += 16 * a_stride;
172 b += 16 * b_stride;
173 }
174 return total_sse;
175}
176#endif // CONFIG_VP9_HIGHBITDEPTH
177
Yaowu Xu75385012016-02-17 08:42:56 -0800178int64_t vpx_get_y_sse(const YV12_BUFFER_CONFIG *a,
clang-format1214cee2016-08-08 22:59:08 -0700179 const YV12_BUFFER_CONFIG *b) {
Yaowu Xu75385012016-02-17 08:42:56 -0800180 assert(a->y_crop_width == b->y_crop_width);
181 assert(a->y_crop_height == b->y_crop_height);
182
183 return get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
clang-format1214cee2016-08-08 22:59:08 -0700184 a->y_crop_width, a->y_crop_height);
Yaowu Xu75385012016-02-17 08:42:56 -0800185}
186
187#if CONFIG_VP9_HIGHBITDEPTH
188int64_t vpx_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
clang-format1214cee2016-08-08 22:59:08 -0700189 const YV12_BUFFER_CONFIG *b) {
Yaowu Xu75385012016-02-17 08:42:56 -0800190 assert(a->y_crop_width == b->y_crop_width);
191 assert(a->y_crop_height == b->y_crop_height);
192 assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
193 assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
194
195 return highbd_get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
clang-format1214cee2016-08-08 22:59:08 -0700196 a->y_crop_width, a->y_crop_height);
Yaowu Xu75385012016-02-17 08:42:56 -0800197}
198#endif // CONFIG_VP9_HIGHBITDEPTH
199
200#if CONFIG_VP9_HIGHBITDEPTH
Yaowu Xu38cfc452016-02-22 15:24:25 -0800201void vpx_calc_highbd_psnr(const YV12_BUFFER_CONFIG *a,
clang-format1214cee2016-08-08 22:59:08 -0700202 const YV12_BUFFER_CONFIG *b, PSNR_STATS *psnr,
203 uint32_t bit_depth, uint32_t in_bit_depth) {
204 const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
205 const int heights[3] = { a->y_crop_height, a->uv_crop_height,
206 a->uv_crop_height };
Yaowu Xu75385012016-02-17 08:42:56 -0800207 const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer };
208 const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
209 const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer };
210 const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
211 int i;
212 uint64_t total_sse = 0;
213 uint32_t total_samples = 0;
214 const double peak = (double)((1 << in_bit_depth) - 1);
215 const unsigned int input_shift = bit_depth - in_bit_depth;
216
217 for (i = 0; i < 3; ++i) {
218 const int w = widths[i];
219 const int h = heights[i];
220 const uint32_t samples = w * h;
221 uint64_t sse;
222 if (a->flags & YV12_FLAG_HIGHBITDEPTH) {
223 if (input_shift) {
clang-format1214cee2016-08-08 22:59:08 -0700224 sse = highbd_get_sse_shift(a_planes[i], a_strides[i], b_planes[i],
225 b_strides[i], w, h, input_shift);
Yaowu Xu75385012016-02-17 08:42:56 -0800226 } else {
clang-format1214cee2016-08-08 22:59:08 -0700227 sse = highbd_get_sse(a_planes[i], a_strides[i], b_planes[i],
228 b_strides[i], w, h);
Yaowu Xu75385012016-02-17 08:42:56 -0800229 }
230 } else {
clang-format1214cee2016-08-08 22:59:08 -0700231 sse = get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h);
Yaowu Xu75385012016-02-17 08:42:56 -0800232 }
233 psnr->sse[1 + i] = sse;
234 psnr->samples[1 + i] = samples;
235 psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
236
237 total_sse += sse;
238 total_samples += samples;
239 }
240
241 psnr->sse[0] = total_sse;
242 psnr->samples[0] = total_samples;
clang-format1214cee2016-08-08 22:59:08 -0700243 psnr->psnr[0] =
244 vpx_sse_to_psnr((double)total_samples, peak, (double)total_sse);
Yaowu Xu75385012016-02-17 08:42:56 -0800245}
246
Yaowu Xu9fb593d2016-02-17 12:38:54 -0800247#endif // !CONFIG_VP9_HIGHBITDEPTH
Yaowu Xu75385012016-02-17 08:42:56 -0800248
Yaowu Xu38cfc452016-02-22 15:24:25 -0800249void vpx_calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
250 PSNR_STATS *psnr) {
Yaowu Xu75385012016-02-17 08:42:56 -0800251 static const double peak = 255.0;
clang-format1214cee2016-08-08 22:59:08 -0700252 const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
253 const int heights[3] = { a->y_crop_height, a->uv_crop_height,
254 a->uv_crop_height };
Yaowu Xu75385012016-02-17 08:42:56 -0800255 const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer };
256 const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
257 const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer };
258 const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
259 int i;
260 uint64_t total_sse = 0;
261 uint32_t total_samples = 0;
262
263 for (i = 0; i < 3; ++i) {
264 const int w = widths[i];
265 const int h = heights[i];
266 const uint32_t samples = w * h;
clang-format1214cee2016-08-08 22:59:08 -0700267 const uint64_t sse =
268 get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h);
Yaowu Xu75385012016-02-17 08:42:56 -0800269 psnr->sse[1 + i] = sse;
270 psnr->samples[1 + i] = samples;
271 psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
272
273 total_sse += sse;
274 total_samples += samples;
275 }
276
277 psnr->sse[0] = total_sse;
278 psnr->samples[0] = total_samples;
clang-format1214cee2016-08-08 22:59:08 -0700279 psnr->psnr[0] =
280 vpx_sse_to_psnr((double)total_samples, peak, (double)total_sse);
Yaowu Xu75385012016-02-17 08:42:56 -0800281}