/src/libvpx/vpx_dsp/psnr.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
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 | | |
17 | 0 | double vpx_sse_to_psnr(double samples, double peak, double sse) { |
18 | 0 | if (sse > 0.0) { |
19 | 0 | const double psnr = 10.0 * log10(samples * peak * peak / sse); |
20 | 0 | return psnr > MAX_PSNR ? MAX_PSNR : psnr; |
21 | 0 | } else { |
22 | 0 | return MAX_PSNR; |
23 | 0 | } |
24 | 0 | } |
25 | | |
26 | | /* TODO(yaowu): The block_variance calls the unoptimized versions of variance() |
27 | | * and highbd_8_variance(). It should not. |
28 | | */ |
29 | | static int64_t encoder_sse(const uint8_t *a, int a_stride, const uint8_t *b, |
30 | 99.2k | int b_stride, int w, int h) { |
31 | 99.2k | int i, j; |
32 | 99.2k | int64_t sse = 0; |
33 | | |
34 | 5.17M | for (i = 0; i < h; i++) { |
35 | 80.5M | for (j = 0; j < w; j++) { |
36 | 75.4M | const int diff = a[j] - b[j]; |
37 | 75.4M | sse += diff * diff; |
38 | 75.4M | } |
39 | | |
40 | 5.07M | a += a_stride; |
41 | 5.07M | b += b_stride; |
42 | 5.07M | } |
43 | | |
44 | 99.2k | return sse; |
45 | 99.2k | } |
46 | | |
47 | | #if CONFIG_VP9_HIGHBITDEPTH |
48 | | static int64_t encoder_highbd_sse(const uint8_t *a8, int a_stride, |
49 | | const uint8_t *b8, int b_stride, int w, |
50 | 0 | int h) { |
51 | 0 | int i, j; |
52 | 0 | int64_t sse = 0; |
53 | |
|
54 | 0 | const uint16_t *a = CONVERT_TO_SHORTPTR(a8); |
55 | 0 | const uint16_t *b = CONVERT_TO_SHORTPTR(b8); |
56 | |
|
57 | 0 | for (i = 0; i < h; i++) { |
58 | 0 | for (j = 0; j < w; j++) { |
59 | 0 | const int diff = a[j] - b[j]; |
60 | 0 | sse += diff * diff; |
61 | 0 | } |
62 | 0 | a += a_stride; |
63 | 0 | b += b_stride; |
64 | 0 | } |
65 | |
|
66 | 0 | return sse; |
67 | 0 | } |
68 | | #endif // CONFIG_VP9_HIGHBITDEPTH |
69 | | |
70 | | static int64_t get_sse(const uint8_t *a, int a_stride, const uint8_t *b, |
71 | 235k | int b_stride, int width, int height) { |
72 | 235k | const int dw = width % 16; |
73 | 235k | const int dh = height % 16; |
74 | 235k | int64_t total_sse = 0; |
75 | 235k | int x, y; |
76 | | |
77 | 235k | if (dw > 0) { |
78 | 46.9k | total_sse += encoder_sse(&a[width - dw], a_stride, &b[width - dw], b_stride, |
79 | 46.9k | dw, height); |
80 | 46.9k | } |
81 | | |
82 | 235k | if (dh > 0) { |
83 | 52.2k | total_sse += |
84 | 52.2k | encoder_sse(&a[(height - dh) * a_stride], a_stride, |
85 | 52.2k | &b[(height - dh) * b_stride], b_stride, width - dw, dh); |
86 | 52.2k | } |
87 | | |
88 | 1.28M | for (y = 0; y < height / 16; ++y) { |
89 | 1.05M | const uint8_t *pa = a; |
90 | 1.05M | const uint8_t *pb = b; |
91 | 5.82M | for (x = 0; x < width / 16; ++x) { |
92 | 4.77M | total_sse += vpx_sse(pa, a_stride, pb, b_stride, 16, 16); |
93 | | |
94 | 4.77M | pa += 16; |
95 | 4.77M | pb += 16; |
96 | 4.77M | } |
97 | | |
98 | 1.05M | a += 16 * a_stride; |
99 | 1.05M | b += 16 * b_stride; |
100 | 1.05M | } |
101 | | |
102 | 235k | return total_sse; |
103 | 235k | } |
104 | | |
105 | | #if CONFIG_VP9_HIGHBITDEPTH |
106 | | static int64_t highbd_get_sse_shift(const uint8_t *a8, int a_stride, |
107 | | const uint8_t *b8, int b_stride, int width, |
108 | 0 | int height, unsigned int input_shift) { |
109 | 0 | const uint16_t *a = CONVERT_TO_SHORTPTR(a8); |
110 | 0 | const uint16_t *b = CONVERT_TO_SHORTPTR(b8); |
111 | 0 | int64_t total_sse = 0; |
112 | 0 | int x, y; |
113 | 0 | for (y = 0; y < height; ++y) { |
114 | 0 | for (x = 0; x < width; ++x) { |
115 | 0 | int64_t diff; |
116 | 0 | diff = (a[x] >> input_shift) - (b[x] >> input_shift); |
117 | 0 | total_sse += diff * diff; |
118 | 0 | } |
119 | 0 | a += a_stride; |
120 | 0 | b += b_stride; |
121 | 0 | } |
122 | 0 | return total_sse; |
123 | 0 | } |
124 | | |
125 | | static int64_t highbd_get_sse(const uint8_t *a, int a_stride, const uint8_t *b, |
126 | 0 | int b_stride, int width, int height) { |
127 | 0 | int64_t total_sse = 0; |
128 | 0 | int x, y; |
129 | 0 | const int dw = width % 16; |
130 | 0 | const int dh = height % 16; |
131 | 0 | if (dw > 0) { |
132 | 0 | total_sse += encoder_highbd_sse(&a[width - dw], a_stride, &b[width - dw], |
133 | 0 | b_stride, dw, height); |
134 | 0 | } |
135 | 0 | if (dh > 0) { |
136 | 0 | total_sse += encoder_highbd_sse(&a[(height - dh) * a_stride], a_stride, |
137 | 0 | &b[(height - dh) * b_stride], b_stride, |
138 | 0 | width - dw, dh); |
139 | 0 | } |
140 | 0 | for (y = 0; y < height / 16; ++y) { |
141 | 0 | const uint8_t *pa = a; |
142 | 0 | const uint8_t *pb = b; |
143 | 0 | for (x = 0; x < width / 16; ++x) { |
144 | 0 | total_sse += vpx_highbd_sse(pa, a_stride, pb, b_stride, 16, 16); |
145 | 0 | pa += 16; |
146 | 0 | pb += 16; |
147 | 0 | } |
148 | 0 | a += 16 * a_stride; |
149 | 0 | b += 16 * b_stride; |
150 | 0 | } |
151 | 0 | return total_sse; |
152 | 0 | } |
153 | | #endif // CONFIG_VP9_HIGHBITDEPTH |
154 | | |
155 | | int64_t vpx_get_y_sse(const YV12_BUFFER_CONFIG *a, |
156 | 235k | const YV12_BUFFER_CONFIG *b) { |
157 | 235k | assert(a->y_crop_width == b->y_crop_width); |
158 | 235k | assert(a->y_crop_height == b->y_crop_height); |
159 | | |
160 | 235k | return get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride, |
161 | 235k | a->y_crop_width, a->y_crop_height); |
162 | 235k | } |
163 | | |
164 | | #if CONFIG_VP9_HIGHBITDEPTH |
165 | | int64_t vpx_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a, |
166 | 0 | const YV12_BUFFER_CONFIG *b) { |
167 | 0 | assert(a->y_crop_width == b->y_crop_width); |
168 | 0 | assert(a->y_crop_height == b->y_crop_height); |
169 | 0 | assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0); |
170 | 0 | assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0); |
171 | |
|
172 | 0 | return highbd_get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride, |
173 | 0 | a->y_crop_width, a->y_crop_height); |
174 | 0 | } |
175 | | #endif // CONFIG_VP9_HIGHBITDEPTH |
176 | | |
177 | | #if CONFIG_VP9_HIGHBITDEPTH |
178 | | void vpx_calc_highbd_psnr(const YV12_BUFFER_CONFIG *a, |
179 | | const YV12_BUFFER_CONFIG *b, PSNR_STATS *psnr, |
180 | 0 | uint32_t bit_depth, uint32_t in_bit_depth) { |
181 | 0 | const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width }; |
182 | 0 | const int heights[3] = { a->y_crop_height, a->uv_crop_height, |
183 | 0 | a->uv_crop_height }; |
184 | 0 | const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer }; |
185 | 0 | const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride }; |
186 | 0 | const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer }; |
187 | 0 | const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride }; |
188 | 0 | int i; |
189 | 0 | uint64_t total_sse = 0; |
190 | 0 | uint32_t total_samples = 0; |
191 | 0 | const double peak = (double)((1 << in_bit_depth) - 1); |
192 | 0 | const unsigned int input_shift = bit_depth - in_bit_depth; |
193 | |
|
194 | 0 | for (i = 0; i < 3; ++i) { |
195 | 0 | const int w = widths[i]; |
196 | 0 | const int h = heights[i]; |
197 | 0 | const uint32_t samples = w * h; |
198 | 0 | uint64_t sse; |
199 | 0 | if (a->flags & YV12_FLAG_HIGHBITDEPTH) { |
200 | 0 | if (input_shift) { |
201 | 0 | sse = highbd_get_sse_shift(a_planes[i], a_strides[i], b_planes[i], |
202 | 0 | b_strides[i], w, h, input_shift); |
203 | 0 | } else { |
204 | 0 | sse = highbd_get_sse(a_planes[i], a_strides[i], b_planes[i], |
205 | 0 | b_strides[i], w, h); |
206 | 0 | } |
207 | 0 | } else { |
208 | 0 | sse = get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h); |
209 | 0 | } |
210 | 0 | psnr->sse[1 + i] = sse; |
211 | 0 | psnr->samples[1 + i] = samples; |
212 | 0 | psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse); |
213 | |
|
214 | 0 | total_sse += sse; |
215 | 0 | total_samples += samples; |
216 | 0 | } |
217 | |
|
218 | 0 | psnr->sse[0] = total_sse; |
219 | 0 | psnr->samples[0] = total_samples; |
220 | 0 | psnr->psnr[0] = |
221 | 0 | vpx_sse_to_psnr((double)total_samples, peak, (double)total_sse); |
222 | 0 | } |
223 | | |
224 | | #endif // !CONFIG_VP9_HIGHBITDEPTH |
225 | | |
226 | | void vpx_calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b, |
227 | 0 | PSNR_STATS *psnr) { |
228 | 0 | static const double peak = 255.0; |
229 | 0 | const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width }; |
230 | 0 | const int heights[3] = { a->y_crop_height, a->uv_crop_height, |
231 | 0 | a->uv_crop_height }; |
232 | 0 | const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer }; |
233 | 0 | const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride }; |
234 | 0 | const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer }; |
235 | 0 | const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride }; |
236 | 0 | int i; |
237 | 0 | uint64_t total_sse = 0; |
238 | 0 | uint32_t total_samples = 0; |
239 | |
|
240 | 0 | for (i = 0; i < 3; ++i) { |
241 | 0 | const int w = widths[i]; |
242 | 0 | const int h = heights[i]; |
243 | 0 | const uint32_t samples = w * h; |
244 | 0 | const uint64_t sse = |
245 | 0 | get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h); |
246 | 0 | psnr->sse[1 + i] = sse; |
247 | 0 | psnr->samples[1 + i] = samples; |
248 | 0 | psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse); |
249 | |
|
250 | 0 | total_sse += sse; |
251 | 0 | total_samples += samples; |
252 | 0 | } |
253 | |
|
254 | 0 | psnr->sse[0] = total_sse; |
255 | 0 | psnr->samples[0] = total_samples; |
256 | 0 | psnr->psnr[0] = |
257 | 0 | vpx_sse_to_psnr((double)total_samples, peak, (double)total_sse); |
258 | 0 | } |