Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 2 | * 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 Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12 | #include <assert.h> |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 13 | #include <math.h> |
| 14 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 15 | #include "./aom_dsp_rtcd.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 16 | #include "aom_dsp/psnr.h" |
| 17 | #include "aom_scale/yv12config.h" |
| 18 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 19 | double aom_sse_to_psnr(double samples, double peak, double sse) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 20 | 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 | */ |
| 31 | static 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 51 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 52 | static 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 | |
| 73 | static 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 83 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 84 | |
| 85 | static 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 111 | aom_mse16x16(pa, a_stride, pb, b_stride, &sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 112 | 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 125 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 126 | static 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 | |
| 145 | static 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 168 | aom_highbd_8_mse16x16(pa, a_stride, pb, b_stride, &sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 169 | 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 178 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 179 | |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 180 | int64_t aom_get_y_sse_part(const YV12_BUFFER_CONFIG *a, |
clang-format | bda8d61 | 2016-09-19 15:55:46 -0700 | [diff] [blame] | 181 | const YV12_BUFFER_CONFIG *b, int hstart, int width, |
| 182 | int vstart, int height) { |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 183 | 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 188 | int64_t aom_get_y_sse(const YV12_BUFFER_CONFIG *a, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 189 | 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 197 | int64_t aom_get_u_sse(const YV12_BUFFER_CONFIG *a, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 198 | 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 206 | int64_t aom_get_v_sse(const YV12_BUFFER_CONFIG *a, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 207 | 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 215 | #if CONFIG_AOM_HIGHBITDEPTH |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 216 | int64_t aom_highbd_get_y_sse_part(const YV12_BUFFER_CONFIG *a, |
clang-format | bda8d61 | 2016-09-19 15:55:46 -0700 | [diff] [blame] | 217 | const YV12_BUFFER_CONFIG *b, int hstart, |
| 218 | int width, int vstart, int height) { |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 219 | return highbd_get_sse( |
| 220 | a->y_buffer + vstart * a->y_stride + hstart, a->y_stride, |
clang-format | bda8d61 | 2016-09-19 15:55:46 -0700 | [diff] [blame] | 221 | b->y_buffer + vstart * b->y_stride + hstart, b->y_stride, width, height); |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 224 | int64_t aom_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 225 | 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 235 | int64_t aom_highbd_get_u_sse(const YV12_BUFFER_CONFIG *a, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 236 | 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 246 | int64_t aom_highbd_get_v_sse(const YV12_BUFFER_CONFIG *a, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 247 | 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 256 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 257 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 258 | #if CONFIG_AOM_HIGHBITDEPTH |
| 259 | void aom_calc_highbd_psnr(const YV12_BUFFER_CONFIG *a, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 260 | 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 293 | psnr->psnr[1 + i] = aom_sse_to_psnr(samples, peak, (double)sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 294 | |
| 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 302 | aom_sse_to_psnr((double)total_samples, peak, (double)total_sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 305 | #endif // !CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 306 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 307 | void aom_calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 308 | 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 329 | psnr->psnr[1 + i] = aom_sse_to_psnr(samples, peak, (double)sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 330 | |
| 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 338 | aom_sse_to_psnr((double)total_samples, peak, (double)total_sse); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 339 | } |