1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
/*
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 2 Clause License and
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
* was not distributed with this source code in the LICENSE file, you can
* obtain it at www.aomedia.org/license/software. If the Alliance for Open
* Media Patent License 1.0 was not distributed with this source code in the
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/
#include <assert.h>
#include <math.h>
#include "config/aom_dsp_rtcd.h"
#include "aom_dsp/psnr.h"
#include "aom_scale/yv12config.h"
double aom_sse_to_psnr(double samples, double peak, double sse) {
if (sse > 0.0) {
const double psnr = 10.0 * log10(samples * peak * peak / sse);
return psnr > MAX_PSNR ? MAX_PSNR : psnr;
} else {
return MAX_PSNR;
}
}
static int64_t encoder_sse(const uint8_t *a, int a_stride, const uint8_t *b,
int b_stride, int w, int h) {
int i, j;
int64_t sse = 0;
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
const int diff = a[j] - b[j];
sse += diff * diff;
}
a += a_stride;
b += b_stride;
}
return sse;
}
#if CONFIG_AV1_HIGHBITDEPTH
static int64_t encoder_highbd_sse(const uint8_t *a8, int a_stride,
const uint8_t *b8, int b_stride, int w,
int h) {
const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
int64_t sse = 0;
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
const int diff = a[j] - b[j];
sse += diff * diff;
}
a += a_stride;
b += b_stride;
}
return sse;
}
#endif // CONFIG_AV1_HIGHBITDEPTH
static int64_t get_sse(const uint8_t *a, int a_stride, const uint8_t *b,
int b_stride, int width, int height) {
const int dw = width % 16;
const int dh = height % 16;
int64_t total_sse = 0;
int x, y;
if (dw > 0) {
total_sse += encoder_sse(&a[width - dw], a_stride, &b[width - dw], b_stride,
dw, height);
}
if (dh > 0) {
total_sse +=
encoder_sse(&a[(height - dh) * a_stride], a_stride,
&b[(height - dh) * b_stride], b_stride, width - dw, dh);
}
for (y = 0; y < height / 16; ++y) {
const uint8_t *pa = a;
const uint8_t *pb = b;
for (x = 0; x < width / 16; ++x) {
total_sse += aom_sse(pa, a_stride, pb, b_stride, 16, 16);
pa += 16;
pb += 16;
}
a += 16 * a_stride;
b += 16 * b_stride;
}
return total_sse;
}
#if CONFIG_AV1_HIGHBITDEPTH
static int64_t highbd_get_sse_shift(const uint8_t *a8, int a_stride,
const uint8_t *b8, int b_stride, int width,
int height, unsigned int input_shift) {
const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
int64_t total_sse = 0;
int x, y;
for (y = 0; y < height; ++y) {
for (x = 0; x < width; ++x) {
int64_t diff;
diff = (a[x] >> input_shift) - (b[x] >> input_shift);
total_sse += diff * diff;
}
a += a_stride;
b += b_stride;
}
return total_sse;
}
static int64_t highbd_get_sse(const uint8_t *a, int a_stride, const uint8_t *b,
int b_stride, int width, int height) {
int64_t total_sse = 0;
int x, y;
const int dw = width % 16;
const int dh = height % 16;
if (dw > 0) {
total_sse += encoder_highbd_sse(&a[width - dw], a_stride, &b[width - dw],
b_stride, dw, height);
}
if (dh > 0) {
total_sse += encoder_highbd_sse(&a[(height - dh) * a_stride], a_stride,
&b[(height - dh) * b_stride], b_stride,
width - dw, dh);
}
for (y = 0; y < height / 16; ++y) {
const uint8_t *pa = a;
const uint8_t *pb = b;
for (x = 0; x < width / 16; ++x) {
total_sse += aom_highbd_sse(pa, a_stride, pb, b_stride, 16, 16);
pa += 16;
pb += 16;
}
a += 16 * a_stride;
b += 16 * b_stride;
}
return total_sse;
}
#endif // CONFIG_AV1_HIGHBITDEPTH
uint64_t aom_get_y_var(const YV12_BUFFER_CONFIG *a, int hstart, int width,
int vstart, int height) {
return aom_var_2d_u8(a->y_buffer + vstart * a->y_stride + hstart, a->y_stride,
width, height) /
(width * height);
}
uint64_t aom_get_u_var(const YV12_BUFFER_CONFIG *a, int hstart, int width,
int vstart, int height) {
return aom_var_2d_u8(a->u_buffer + vstart * a->uv_stride + hstart,
a->uv_stride, width, height) /
(width * height);
}
uint64_t aom_get_v_var(const YV12_BUFFER_CONFIG *a, int hstart, int width,
int vstart, int height) {
return aom_var_2d_u8(a->v_buffer + vstart * a->uv_stride + hstart,
a->uv_stride, width, height) /
(width * height);
}
int64_t aom_get_y_sse_part(const YV12_BUFFER_CONFIG *a,
const YV12_BUFFER_CONFIG *b, int hstart, int width,
int vstart, int height) {
return get_sse(a->y_buffer + vstart * a->y_stride + hstart, a->y_stride,
b->y_buffer + vstart * b->y_stride + hstart, b->y_stride,
width, height);
}
int64_t aom_get_y_sse(const YV12_BUFFER_CONFIG *a,
const YV12_BUFFER_CONFIG *b) {
assert(a->y_crop_width == b->y_crop_width);
assert(a->y_crop_height == b->y_crop_height);
return get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
a->y_crop_width, a->y_crop_height);
}
int64_t aom_get_u_sse_part(const YV12_BUFFER_CONFIG *a,
const YV12_BUFFER_CONFIG *b, int hstart, int width,
int vstart, int height) {
return get_sse(a->u_buffer + vstart * a->uv_stride + hstart, a->uv_stride,
b->u_buffer + vstart * b->uv_stride + hstart, b->uv_stride,
width, height);
}
int64_t aom_get_u_sse(const YV12_BUFFER_CONFIG *a,
const YV12_BUFFER_CONFIG *b) {
assert(a->uv_crop_width == b->uv_crop_width);
assert(a->uv_crop_height == b->uv_crop_height);
return get_sse(a->u_buffer, a->uv_stride, b->u_buffer, b->uv_stride,
a->uv_crop_width, a->uv_crop_height);
}
int64_t aom_get_v_sse_part(const YV12_BUFFER_CONFIG *a,
const YV12_BUFFER_CONFIG *b, int hstart, int width,
int vstart, int height) {
return get_sse(a->v_buffer + vstart * a->uv_stride + hstart, a->uv_stride,
b->v_buffer + vstart * b->uv_stride + hstart, b->uv_stride,
width, height);
}
int64_t aom_get_v_sse(const YV12_BUFFER_CONFIG *a,
const YV12_BUFFER_CONFIG *b) {
assert(a->uv_crop_width == b->uv_crop_width);
assert(a->uv_crop_height == b->uv_crop_height);
return get_sse(a->v_buffer, a->uv_stride, b->v_buffer, b->uv_stride,
a->uv_crop_width, a->uv_crop_height);
}
#if CONFIG_AV1_HIGHBITDEPTH
uint64_t aom_highbd_get_y_var(const YV12_BUFFER_CONFIG *a, int hstart,
int width, int vstart, int height) {
return aom_var_2d_u16(a->y_buffer + vstart * a->y_stride + hstart,
a->y_stride, width, height) /
(width * height);
}
uint64_t aom_highbd_get_u_var(const YV12_BUFFER_CONFIG *a, int hstart,
int width, int vstart, int height) {
return aom_var_2d_u16(a->u_buffer + vstart * a->uv_stride + hstart,
a->uv_stride, width, height) /
(width * height);
}
uint64_t aom_highbd_get_v_var(const YV12_BUFFER_CONFIG *a, int hstart,
int width, int vstart, int height) {
return aom_var_2d_u16(a->v_buffer + vstart * a->uv_stride + hstart,
a->uv_stride, width, height) /
(width * height);
}
int64_t aom_highbd_get_y_sse_part(const YV12_BUFFER_CONFIG *a,
const YV12_BUFFER_CONFIG *b, int hstart,
int width, int vstart, int height) {
return highbd_get_sse(
a->y_buffer + vstart * a->y_stride + hstart, a->y_stride,
b->y_buffer + vstart * b->y_stride + hstart, b->y_stride, width, height);
}
int64_t aom_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
const YV12_BUFFER_CONFIG *b) {
assert(a->y_crop_width == b->y_crop_width);
assert(a->y_crop_height == b->y_crop_height);
assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
return highbd_get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
a->y_crop_width, a->y_crop_height);
}
int64_t aom_highbd_get_u_sse_part(const YV12_BUFFER_CONFIG *a,
const YV12_BUFFER_CONFIG *b, int hstart,
int width, int vstart, int height) {
return highbd_get_sse(a->u_buffer + vstart * a->uv_stride + hstart,
a->uv_stride,
b->u_buffer + vstart * b->uv_stride + hstart,
b->uv_stride, width, height);
}
int64_t aom_highbd_get_u_sse(const YV12_BUFFER_CONFIG *a,
const YV12_BUFFER_CONFIG *b) {
assert(a->uv_crop_width == b->uv_crop_width);
assert(a->uv_crop_height == b->uv_crop_height);
assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
return highbd_get_sse(a->u_buffer, a->uv_stride, b->u_buffer, b->uv_stride,
a->uv_crop_width, a->uv_crop_height);
}
int64_t aom_highbd_get_v_sse_part(const YV12_BUFFER_CONFIG *a,
const YV12_BUFFER_CONFIG *b, int hstart,
int width, int vstart, int height) {
return highbd_get_sse(a->v_buffer + vstart * a->uv_stride + hstart,
a->uv_stride,
b->v_buffer + vstart * b->uv_stride + hstart,
b->uv_stride, width, height);
}
int64_t aom_highbd_get_v_sse(const YV12_BUFFER_CONFIG *a,
const YV12_BUFFER_CONFIG *b) {
assert(a->uv_crop_width == b->uv_crop_width);
assert(a->uv_crop_height == b->uv_crop_height);
assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
return highbd_get_sse(a->v_buffer, a->uv_stride, b->v_buffer, b->uv_stride,
a->uv_crop_width, a->uv_crop_height);
}
#endif // CONFIG_AV1_HIGHBITDEPTH
int64_t aom_get_sse_plane(const YV12_BUFFER_CONFIG *a,
const YV12_BUFFER_CONFIG *b, int plane, int highbd) {
#if CONFIG_AV1_HIGHBITDEPTH
if (highbd) {
switch (plane) {
case 0: return aom_highbd_get_y_sse(a, b);
case 1: return aom_highbd_get_u_sse(a, b);
case 2: return aom_highbd_get_v_sse(a, b);
default: assert(plane >= 0 && plane <= 2); return 0;
}
} else {
switch (plane) {
case 0: return aom_get_y_sse(a, b);
case 1: return aom_get_u_sse(a, b);
case 2: return aom_get_v_sse(a, b);
default: assert(plane >= 0 && plane <= 2); return 0;
}
}
#else
(void)highbd;
switch (plane) {
case 0: return aom_get_y_sse(a, b);
case 1: return aom_get_u_sse(a, b);
case 2: return aom_get_v_sse(a, b);
default: assert(plane >= 0 && plane <= 2); return 0;
}
#endif
}
#if CONFIG_AV1_HIGHBITDEPTH
void aom_calc_highbd_psnr(const YV12_BUFFER_CONFIG *a,
const YV12_BUFFER_CONFIG *b, PSNR_STATS *psnr,
uint32_t bit_depth, uint32_t in_bit_depth) {
assert(a->y_crop_width == b->y_crop_width);
assert(a->y_crop_height == b->y_crop_height);
assert(a->uv_crop_width == b->uv_crop_width);
assert(a->uv_crop_height == b->uv_crop_height);
const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
const int heights[3] = { a->y_crop_height, a->uv_crop_height,
a->uv_crop_height };
const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
int i;
uint64_t total_sse = 0;
uint32_t total_samples = 0;
#if CONFIG_LIBVMAF_PSNR_PEAK
double peak = (double)(255 << (in_bit_depth - 8));
#else
double peak = (double)((1 << in_bit_depth) - 1);
#endif // CONFIG_LIBVMAF_PSNR_PEAK
const unsigned int input_shift = bit_depth - in_bit_depth;
for (i = 0; i < 3; ++i) {
const int w = widths[i];
const int h = heights[i];
const uint32_t samples = w * h;
uint64_t sse;
if (a->flags & YV12_FLAG_HIGHBITDEPTH) {
if (input_shift) {
sse = highbd_get_sse_shift(a->buffers[i], a_strides[i], b->buffers[i],
b_strides[i], w, h, input_shift);
} else {
sse = highbd_get_sse(a->buffers[i], a_strides[i], b->buffers[i],
b_strides[i], w, h);
}
} else {
sse = get_sse(a->buffers[i], a_strides[i], b->buffers[i], b_strides[i], w,
h);
}
psnr->sse[1 + i] = sse;
psnr->samples[1 + i] = samples;
psnr->psnr[1 + i] = aom_sse_to_psnr(samples, peak, (double)sse);
total_sse += sse;
total_samples += samples;
}
psnr->sse[0] = total_sse;
psnr->samples[0] = total_samples;
psnr->psnr[0] =
aom_sse_to_psnr((double)total_samples, peak, (double)total_sse);
// Compute PSNR based on stream bit depth
if ((a->flags & YV12_FLAG_HIGHBITDEPTH) && (in_bit_depth < bit_depth)) {
#if CONFIG_LIBVMAF_PSNR_PEAK
peak = (double)(255 << (bit_depth - 8));
#else
peak = (double)((1 << bit_depth) - 1);
#endif // CONFIG_LIBVMAF_PSNR_PEAK
total_sse = 0;
total_samples = 0;
for (i = 0; i < 3; ++i) {
const int w = widths[i];
const int h = heights[i];
const uint32_t samples = w * h;
uint64_t sse;
sse = highbd_get_sse(a->buffers[i], a_strides[i], b->buffers[i],
b_strides[i], w, h);
psnr->sse_hbd[1 + i] = sse;
psnr->samples_hbd[1 + i] = samples;
psnr->psnr_hbd[1 + i] = aom_sse_to_psnr(samples, peak, (double)sse);
total_sse += sse;
total_samples += samples;
}
psnr->sse_hbd[0] = total_sse;
psnr->samples_hbd[0] = total_samples;
psnr->psnr_hbd[0] =
aom_sse_to_psnr((double)total_samples, peak, (double)total_sse);
}
}
#endif
void aom_calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
PSNR_STATS *psnr) {
assert(a->y_crop_width == b->y_crop_width);
assert(a->y_crop_height == b->y_crop_height);
assert(a->uv_crop_width == b->uv_crop_width);
assert(a->uv_crop_height == b->uv_crop_height);
static const double peak = 255.0;
const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
const int heights[3] = { a->y_crop_height, a->uv_crop_height,
a->uv_crop_height };
const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
int i;
uint64_t total_sse = 0;
uint32_t total_samples = 0;
for (i = 0; i < 3; ++i) {
const int w = widths[i];
const int h = heights[i];
const uint32_t samples = w * h;
const uint64_t sse =
get_sse(a->buffers[i], a_strides[i], b->buffers[i], b_strides[i], w, h);
psnr->sse[1 + i] = sse;
psnr->samples[1 + i] = samples;
psnr->psnr[1 + i] = aom_sse_to_psnr(samples, peak, (double)sse);
total_sse += sse;
total_samples += samples;
}
psnr->sse[0] = total_sse;
psnr->samples[0] = total_samples;
psnr->psnr[0] =
aom_sse_to_psnr((double)total_samples, peak, (double)total_sse);
}
|