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 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 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. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10 | * |
| 11 | * This code was originally written by: Nathan E. Egge, at the Daala |
| 12 | * project. |
| 13 | */ |
| 14 | #include <assert.h> |
| 15 | #include <math.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 18 | #include "./aom_config.h" |
| 19 | #include "./aom_dsp_rtcd.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 20 | #include "aom_dsp/ssim.h" |
| 21 | #include "aom_ports/system_state.h" |
| 22 | |
| 23 | typedef struct fs_level fs_level; |
| 24 | typedef struct fs_ctx fs_ctx; |
| 25 | |
| 26 | #define SSIM_C1 (255 * 255 * 0.01 * 0.01) |
| 27 | #define SSIM_C2 (255 * 255 * 0.03 * 0.03) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 28 | #define SSIM_C1_10 (1023 * 1023 * 0.01 * 0.01) |
| 29 | #define SSIM_C1_12 (4095 * 4095 * 0.01 * 0.01) |
| 30 | #define SSIM_C2_10 (1023 * 1023 * 0.03 * 0.03) |
| 31 | #define SSIM_C2_12 (4095 * 4095 * 0.03 * 0.03) |
Yaowu Xu | d3e7c68 | 2017-12-21 14:08:25 -0800 | [diff] [blame] | 32 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 33 | #define FS_MINI(_a, _b) ((_a) < (_b) ? (_a) : (_b)) |
| 34 | #define FS_MAXI(_a, _b) ((_a) > (_b) ? (_a) : (_b)) |
| 35 | |
| 36 | struct fs_level { |
| 37 | uint32_t *im1; |
| 38 | uint32_t *im2; |
| 39 | double *ssim; |
| 40 | int w; |
| 41 | int h; |
| 42 | }; |
| 43 | |
| 44 | struct fs_ctx { |
| 45 | fs_level *level; |
| 46 | int nlevels; |
| 47 | unsigned *col_buf; |
| 48 | }; |
| 49 | |
| 50 | static void fs_ctx_init(fs_ctx *_ctx, int _w, int _h, int _nlevels) { |
| 51 | unsigned char *data; |
| 52 | size_t data_size; |
| 53 | int lw; |
| 54 | int lh; |
| 55 | int l; |
| 56 | lw = (_w + 1) >> 1; |
| 57 | lh = (_h + 1) >> 1; |
| 58 | data_size = |
| 59 | _nlevels * sizeof(fs_level) + 2 * (lw + 8) * 8 * sizeof(*_ctx->col_buf); |
| 60 | for (l = 0; l < _nlevels; l++) { |
| 61 | size_t im_size; |
| 62 | size_t level_size; |
| 63 | im_size = lw * (size_t)lh; |
| 64 | level_size = 2 * im_size * sizeof(*_ctx->level[l].im1); |
| 65 | level_size += sizeof(*_ctx->level[l].ssim) - 1; |
| 66 | level_size /= sizeof(*_ctx->level[l].ssim); |
| 67 | level_size += im_size; |
| 68 | level_size *= sizeof(*_ctx->level[l].ssim); |
| 69 | data_size += level_size; |
| 70 | lw = (lw + 1) >> 1; |
| 71 | lh = (lh + 1) >> 1; |
| 72 | } |
| 73 | data = (unsigned char *)malloc(data_size); |
| 74 | _ctx->level = (fs_level *)data; |
| 75 | _ctx->nlevels = _nlevels; |
| 76 | data += _nlevels * sizeof(*_ctx->level); |
| 77 | lw = (_w + 1) >> 1; |
| 78 | lh = (_h + 1) >> 1; |
| 79 | for (l = 0; l < _nlevels; l++) { |
| 80 | size_t im_size; |
| 81 | size_t level_size; |
| 82 | _ctx->level[l].w = lw; |
| 83 | _ctx->level[l].h = lh; |
| 84 | im_size = lw * (size_t)lh; |
| 85 | level_size = 2 * im_size * sizeof(*_ctx->level[l].im1); |
| 86 | level_size += sizeof(*_ctx->level[l].ssim) - 1; |
| 87 | level_size /= sizeof(*_ctx->level[l].ssim); |
| 88 | level_size *= sizeof(*_ctx->level[l].ssim); |
| 89 | _ctx->level[l].im1 = (uint32_t *)data; |
| 90 | _ctx->level[l].im2 = _ctx->level[l].im1 + im_size; |
| 91 | data += level_size; |
| 92 | _ctx->level[l].ssim = (double *)data; |
| 93 | data += im_size * sizeof(*_ctx->level[l].ssim); |
| 94 | lw = (lw + 1) >> 1; |
| 95 | lh = (lh + 1) >> 1; |
| 96 | } |
| 97 | _ctx->col_buf = (unsigned *)data; |
| 98 | } |
| 99 | |
| 100 | static void fs_ctx_clear(fs_ctx *_ctx) { free(_ctx->level); } |
| 101 | |
| 102 | static void fs_downsample_level(fs_ctx *_ctx, int _l) { |
| 103 | const uint32_t *src1; |
| 104 | const uint32_t *src2; |
| 105 | uint32_t *dst1; |
| 106 | uint32_t *dst2; |
| 107 | int w2; |
| 108 | int h2; |
| 109 | int w; |
| 110 | int h; |
| 111 | int i; |
| 112 | int j; |
| 113 | w = _ctx->level[_l].w; |
| 114 | h = _ctx->level[_l].h; |
| 115 | dst1 = _ctx->level[_l].im1; |
| 116 | dst2 = _ctx->level[_l].im2; |
| 117 | w2 = _ctx->level[_l - 1].w; |
| 118 | h2 = _ctx->level[_l - 1].h; |
| 119 | src1 = _ctx->level[_l - 1].im1; |
| 120 | src2 = _ctx->level[_l - 1].im2; |
| 121 | for (j = 0; j < h; j++) { |
| 122 | int j0offs; |
| 123 | int j1offs; |
| 124 | j0offs = 2 * j * w2; |
| 125 | j1offs = FS_MINI(2 * j + 1, h2) * w2; |
| 126 | for (i = 0; i < w; i++) { |
| 127 | int i0; |
| 128 | int i1; |
| 129 | i0 = 2 * i; |
| 130 | i1 = FS_MINI(i0 + 1, w2); |
| 131 | dst1[j * w + i] = src1[j0offs + i0] + src1[j0offs + i1] + |
| 132 | src1[j1offs + i0] + src1[j1offs + i1]; |
| 133 | dst2[j * w + i] = src2[j0offs + i0] + src2[j0offs + i1] + |
| 134 | src2[j1offs + i0] + src2[j1offs + i1]; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | static void fs_downsample_level0(fs_ctx *_ctx, const uint8_t *_src1, |
| 140 | int _s1ystride, const uint8_t *_src2, |
| 141 | int _s2ystride, int _w, int _h, uint32_t bd, |
| 142 | uint32_t shift) { |
| 143 | uint32_t *dst1; |
| 144 | uint32_t *dst2; |
| 145 | int w; |
| 146 | int h; |
| 147 | int i; |
| 148 | int j; |
| 149 | w = _ctx->level[0].w; |
| 150 | h = _ctx->level[0].h; |
| 151 | dst1 = _ctx->level[0].im1; |
| 152 | dst2 = _ctx->level[0].im2; |
| 153 | for (j = 0; j < h; j++) { |
| 154 | int j0; |
| 155 | int j1; |
| 156 | j0 = 2 * j; |
| 157 | j1 = FS_MINI(j0 + 1, _h); |
| 158 | for (i = 0; i < w; i++) { |
| 159 | int i0; |
| 160 | int i1; |
| 161 | i0 = 2 * i; |
| 162 | i1 = FS_MINI(i0 + 1, _w); |
| 163 | if (bd == 8 && shift == 0) { |
| 164 | dst1[j * w + i] = |
| 165 | _src1[j0 * _s1ystride + i0] + _src1[j0 * _s1ystride + i1] + |
| 166 | _src1[j1 * _s1ystride + i0] + _src1[j1 * _s1ystride + i1]; |
| 167 | dst2[j * w + i] = |
| 168 | _src2[j0 * _s2ystride + i0] + _src2[j0 * _s2ystride + i1] + |
| 169 | _src2[j1 * _s2ystride + i0] + _src2[j1 * _s2ystride + i1]; |
| 170 | } else { |
| 171 | uint16_t *src1s = CONVERT_TO_SHORTPTR(_src1); |
| 172 | uint16_t *src2s = CONVERT_TO_SHORTPTR(_src2); |
| 173 | dst1[j * w + i] = (src1s[j0 * _s1ystride + i0] >> shift) + |
| 174 | (src1s[j0 * _s1ystride + i1] >> shift) + |
| 175 | (src1s[j1 * _s1ystride + i0] >> shift) + |
| 176 | (src1s[j1 * _s1ystride + i1] >> shift); |
| 177 | dst2[j * w + i] = (src2s[j0 * _s2ystride + i0] >> shift) + |
| 178 | (src2s[j0 * _s2ystride + i1] >> shift) + |
| 179 | (src2s[j1 * _s2ystride + i0] >> shift) + |
| 180 | (src2s[j1 * _s2ystride + i1] >> shift); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | static void fs_apply_luminance(fs_ctx *_ctx, int _l, int bit_depth) { |
| 187 | unsigned *col_sums_x; |
| 188 | unsigned *col_sums_y; |
| 189 | uint32_t *im1; |
| 190 | uint32_t *im2; |
| 191 | double *ssim; |
| 192 | double c1; |
| 193 | int w; |
| 194 | int h; |
| 195 | int j0offs; |
| 196 | int j1offs; |
| 197 | int i; |
| 198 | int j; |
| 199 | double ssim_c1 = SSIM_C1; |
Yaowu Xu | d3e7c68 | 2017-12-21 14:08:25 -0800 | [diff] [blame] | 200 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 201 | if (bit_depth == 10) ssim_c1 = SSIM_C1_10; |
| 202 | if (bit_depth == 12) ssim_c1 = SSIM_C1_12; |
Yaowu Xu | d3e7c68 | 2017-12-21 14:08:25 -0800 | [diff] [blame] | 203 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 204 | w = _ctx->level[_l].w; |
| 205 | h = _ctx->level[_l].h; |
| 206 | col_sums_x = _ctx->col_buf; |
| 207 | col_sums_y = col_sums_x + w; |
| 208 | im1 = _ctx->level[_l].im1; |
| 209 | im2 = _ctx->level[_l].im2; |
| 210 | for (i = 0; i < w; i++) col_sums_x[i] = 5 * im1[i]; |
| 211 | for (i = 0; i < w; i++) col_sums_y[i] = 5 * im2[i]; |
| 212 | for (j = 1; j < 4; j++) { |
| 213 | j1offs = FS_MINI(j, h - 1) * w; |
| 214 | for (i = 0; i < w; i++) col_sums_x[i] += im1[j1offs + i]; |
| 215 | for (i = 0; i < w; i++) col_sums_y[i] += im2[j1offs + i]; |
| 216 | } |
| 217 | ssim = _ctx->level[_l].ssim; |
| 218 | c1 = (double)(ssim_c1 * 4096 * (1 << 4 * _l)); |
| 219 | for (j = 0; j < h; j++) { |
| 220 | unsigned mux; |
| 221 | unsigned muy; |
| 222 | int i0; |
| 223 | int i1; |
| 224 | mux = 5 * col_sums_x[0]; |
| 225 | muy = 5 * col_sums_y[0]; |
| 226 | for (i = 1; i < 4; i++) { |
| 227 | i1 = FS_MINI(i, w - 1); |
| 228 | mux += col_sums_x[i1]; |
| 229 | muy += col_sums_y[i1]; |
| 230 | } |
| 231 | for (i = 0; i < w; i++) { |
| 232 | ssim[j * w + i] *= (2 * mux * (double)muy + c1) / |
| 233 | (mux * (double)mux + muy * (double)muy + c1); |
| 234 | if (i + 1 < w) { |
| 235 | i0 = FS_MAXI(0, i - 4); |
| 236 | i1 = FS_MINI(i + 4, w - 1); |
| 237 | mux += col_sums_x[i1] - col_sums_x[i0]; |
| 238 | muy += col_sums_x[i1] - col_sums_x[i0]; |
| 239 | } |
| 240 | } |
| 241 | if (j + 1 < h) { |
| 242 | j0offs = FS_MAXI(0, j - 4) * w; |
| 243 | for (i = 0; i < w; i++) col_sums_x[i] -= im1[j0offs + i]; |
| 244 | for (i = 0; i < w; i++) col_sums_y[i] -= im2[j0offs + i]; |
| 245 | j1offs = FS_MINI(j + 4, h - 1) * w; |
| 246 | for (i = 0; i < w; i++) col_sums_x[i] += im1[j1offs + i]; |
| 247 | for (i = 0; i < w; i++) col_sums_y[i] += im2[j1offs + i]; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | #define FS_COL_SET(_col, _joffs, _ioffs) \ |
| 253 | do { \ |
| 254 | unsigned gx; \ |
| 255 | unsigned gy; \ |
| 256 | gx = gx_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \ |
| 257 | gy = gy_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \ |
| 258 | col_sums_gx2[(_col)] = gx * (double)gx; \ |
| 259 | col_sums_gy2[(_col)] = gy * (double)gy; \ |
| 260 | col_sums_gxgy[(_col)] = gx * (double)gy; \ |
| 261 | } while (0) |
| 262 | |
| 263 | #define FS_COL_ADD(_col, _joffs, _ioffs) \ |
| 264 | do { \ |
| 265 | unsigned gx; \ |
| 266 | unsigned gy; \ |
| 267 | gx = gx_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \ |
| 268 | gy = gy_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \ |
| 269 | col_sums_gx2[(_col)] += gx * (double)gx; \ |
| 270 | col_sums_gy2[(_col)] += gy * (double)gy; \ |
| 271 | col_sums_gxgy[(_col)] += gx * (double)gy; \ |
| 272 | } while (0) |
| 273 | |
| 274 | #define FS_COL_SUB(_col, _joffs, _ioffs) \ |
| 275 | do { \ |
| 276 | unsigned gx; \ |
| 277 | unsigned gy; \ |
| 278 | gx = gx_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \ |
| 279 | gy = gy_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \ |
| 280 | col_sums_gx2[(_col)] -= gx * (double)gx; \ |
| 281 | col_sums_gy2[(_col)] -= gy * (double)gy; \ |
| 282 | col_sums_gxgy[(_col)] -= gx * (double)gy; \ |
| 283 | } while (0) |
| 284 | |
| 285 | #define FS_COL_COPY(_col1, _col2) \ |
| 286 | do { \ |
| 287 | col_sums_gx2[(_col1)] = col_sums_gx2[(_col2)]; \ |
| 288 | col_sums_gy2[(_col1)] = col_sums_gy2[(_col2)]; \ |
| 289 | col_sums_gxgy[(_col1)] = col_sums_gxgy[(_col2)]; \ |
| 290 | } while (0) |
| 291 | |
| 292 | #define FS_COL_HALVE(_col1, _col2) \ |
| 293 | do { \ |
| 294 | col_sums_gx2[(_col1)] = col_sums_gx2[(_col2)] * 0.5; \ |
| 295 | col_sums_gy2[(_col1)] = col_sums_gy2[(_col2)] * 0.5; \ |
| 296 | col_sums_gxgy[(_col1)] = col_sums_gxgy[(_col2)] * 0.5; \ |
| 297 | } while (0) |
| 298 | |
| 299 | #define FS_COL_DOUBLE(_col1, _col2) \ |
| 300 | do { \ |
| 301 | col_sums_gx2[(_col1)] = col_sums_gx2[(_col2)] * 2; \ |
| 302 | col_sums_gy2[(_col1)] = col_sums_gy2[(_col2)] * 2; \ |
| 303 | col_sums_gxgy[(_col1)] = col_sums_gxgy[(_col2)] * 2; \ |
| 304 | } while (0) |
| 305 | |
| 306 | static void fs_calc_structure(fs_ctx *_ctx, int _l, int bit_depth) { |
| 307 | uint32_t *im1; |
| 308 | uint32_t *im2; |
| 309 | unsigned *gx_buf; |
| 310 | unsigned *gy_buf; |
| 311 | double *ssim; |
| 312 | double col_sums_gx2[8]; |
| 313 | double col_sums_gy2[8]; |
| 314 | double col_sums_gxgy[8]; |
| 315 | double c2; |
| 316 | int stride; |
| 317 | int w; |
| 318 | int h; |
| 319 | int i; |
| 320 | int j; |
| 321 | double ssim_c2 = SSIM_C2; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 322 | if (bit_depth == 10) ssim_c2 = SSIM_C2_10; |
| 323 | if (bit_depth == 12) ssim_c2 = SSIM_C2_12; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 324 | |
| 325 | w = _ctx->level[_l].w; |
| 326 | h = _ctx->level[_l].h; |
| 327 | im1 = _ctx->level[_l].im1; |
| 328 | im2 = _ctx->level[_l].im2; |
| 329 | ssim = _ctx->level[_l].ssim; |
| 330 | gx_buf = _ctx->col_buf; |
| 331 | stride = w + 8; |
| 332 | gy_buf = gx_buf + 8 * stride; |
| 333 | memset(gx_buf, 0, 2 * 8 * stride * sizeof(*gx_buf)); |
| 334 | c2 = ssim_c2 * (1 << 4 * _l) * 16 * 104; |
| 335 | for (j = 0; j < h + 4; j++) { |
| 336 | if (j < h - 1) { |
| 337 | for (i = 0; i < w - 1; i++) { |
| 338 | unsigned g1; |
| 339 | unsigned g2; |
| 340 | unsigned gx; |
| 341 | unsigned gy; |
| 342 | g1 = abs((int)im1[(j + 1) * w + i + 1] - (int)im1[j * w + i]); |
| 343 | g2 = abs((int)im1[(j + 1) * w + i] - (int)im1[j * w + i + 1]); |
| 344 | gx = 4 * FS_MAXI(g1, g2) + FS_MINI(g1, g2); |
| 345 | g1 = abs((int)im2[(j + 1) * w + i + 1] - (int)im2[j * w + i]); |
| 346 | g2 = abs((int)im2[(j + 1) * w + i] - (int)im2[j * w + i + 1]); |
| 347 | gy = 4 * FS_MAXI(g1, g2) + FS_MINI(g1, g2); |
| 348 | gx_buf[(j & 7) * stride + i + 4] = gx; |
| 349 | gy_buf[(j & 7) * stride + i + 4] = gy; |
| 350 | } |
| 351 | } else { |
| 352 | memset(gx_buf + (j & 7) * stride, 0, stride * sizeof(*gx_buf)); |
| 353 | memset(gy_buf + (j & 7) * stride, 0, stride * sizeof(*gy_buf)); |
| 354 | } |
| 355 | if (j >= 4) { |
| 356 | int k; |
| 357 | col_sums_gx2[3] = col_sums_gx2[2] = col_sums_gx2[1] = col_sums_gx2[0] = 0; |
| 358 | col_sums_gy2[3] = col_sums_gy2[2] = col_sums_gy2[1] = col_sums_gy2[0] = 0; |
| 359 | col_sums_gxgy[3] = col_sums_gxgy[2] = col_sums_gxgy[1] = |
| 360 | col_sums_gxgy[0] = 0; |
| 361 | for (i = 4; i < 8; i++) { |
| 362 | FS_COL_SET(i, -1, 0); |
| 363 | FS_COL_ADD(i, 0, 0); |
| 364 | for (k = 1; k < 8 - i; k++) { |
| 365 | FS_COL_DOUBLE(i, i); |
| 366 | FS_COL_ADD(i, -k - 1, 0); |
| 367 | FS_COL_ADD(i, k, 0); |
| 368 | } |
| 369 | } |
| 370 | for (i = 0; i < w; i++) { |
| 371 | double mugx2; |
| 372 | double mugy2; |
| 373 | double mugxgy; |
| 374 | mugx2 = col_sums_gx2[0]; |
| 375 | for (k = 1; k < 8; k++) mugx2 += col_sums_gx2[k]; |
| 376 | mugy2 = col_sums_gy2[0]; |
| 377 | for (k = 1; k < 8; k++) mugy2 += col_sums_gy2[k]; |
| 378 | mugxgy = col_sums_gxgy[0]; |
| 379 | for (k = 1; k < 8; k++) mugxgy += col_sums_gxgy[k]; |
| 380 | ssim[(j - 4) * w + i] = (2 * mugxgy + c2) / (mugx2 + mugy2 + c2); |
| 381 | if (i + 1 < w) { |
| 382 | FS_COL_SET(0, -1, 1); |
| 383 | FS_COL_ADD(0, 0, 1); |
| 384 | FS_COL_SUB(2, -3, 2); |
| 385 | FS_COL_SUB(2, 2, 2); |
| 386 | FS_COL_HALVE(1, 2); |
| 387 | FS_COL_SUB(3, -4, 3); |
| 388 | FS_COL_SUB(3, 3, 3); |
| 389 | FS_COL_HALVE(2, 3); |
| 390 | FS_COL_COPY(3, 4); |
| 391 | FS_COL_DOUBLE(4, 5); |
| 392 | FS_COL_ADD(4, -4, 5); |
| 393 | FS_COL_ADD(4, 3, 5); |
| 394 | FS_COL_DOUBLE(5, 6); |
| 395 | FS_COL_ADD(5, -3, 6); |
| 396 | FS_COL_ADD(5, 2, 6); |
| 397 | FS_COL_DOUBLE(6, 7); |
| 398 | FS_COL_ADD(6, -2, 7); |
| 399 | FS_COL_ADD(6, 1, 7); |
| 400 | FS_COL_SET(7, -1, 8); |
| 401 | FS_COL_ADD(7, 0, 8); |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | #define FS_NLEVELS (4) |
| 409 | |
| 410 | /*These weights were derived from the default weights found in Wang's original |
| 411 | Matlab implementation: {0.0448, 0.2856, 0.2363, 0.1333}. |
| 412 | We drop the finest scale and renormalize the rest to sum to 1.*/ |
| 413 | |
| 414 | static const double FS_WEIGHTS[FS_NLEVELS] = { |
| 415 | 0.2989654541015625, 0.3141326904296875, 0.2473602294921875, 0.1395416259765625 |
| 416 | }; |
| 417 | |
| 418 | static double fs_average(fs_ctx *_ctx, int _l) { |
| 419 | double *ssim; |
| 420 | double ret; |
| 421 | int w; |
| 422 | int h; |
| 423 | int i; |
| 424 | int j; |
| 425 | w = _ctx->level[_l].w; |
| 426 | h = _ctx->level[_l].h; |
| 427 | ssim = _ctx->level[_l].ssim; |
| 428 | ret = 0; |
| 429 | for (j = 0; j < h; j++) |
| 430 | for (i = 0; i < w; i++) ret += ssim[j * w + i]; |
| 431 | return pow(ret / (w * h), FS_WEIGHTS[_l]); |
| 432 | } |
| 433 | |
| 434 | static double convert_ssim_db(double _ssim, double _weight) { |
| 435 | assert(_weight >= _ssim); |
| 436 | if ((_weight - _ssim) < 1e-10) return MAX_SSIM_DB; |
| 437 | return 10 * (log10(_weight) - log10(_weight - _ssim)); |
| 438 | } |
| 439 | |
| 440 | static double calc_ssim(const uint8_t *_src, int _systride, const uint8_t *_dst, |
| 441 | int _dystride, int _w, int _h, uint32_t _bd, |
| 442 | uint32_t _shift) { |
| 443 | fs_ctx ctx; |
| 444 | double ret; |
| 445 | int l; |
| 446 | ret = 1; |
| 447 | fs_ctx_init(&ctx, _w, _h, FS_NLEVELS); |
| 448 | fs_downsample_level0(&ctx, _src, _systride, _dst, _dystride, _w, _h, _bd, |
| 449 | _shift); |
| 450 | for (l = 0; l < FS_NLEVELS - 1; l++) { |
| 451 | fs_calc_structure(&ctx, l, _bd); |
| 452 | ret *= fs_average(&ctx, l); |
| 453 | fs_downsample_level(&ctx, l + 1); |
| 454 | } |
| 455 | fs_calc_structure(&ctx, l, _bd); |
| 456 | fs_apply_luminance(&ctx, l, _bd); |
| 457 | ret *= fs_average(&ctx, l); |
| 458 | fs_ctx_clear(&ctx); |
| 459 | return ret; |
| 460 | } |
| 461 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 462 | double aom_calc_fastssim(const YV12_BUFFER_CONFIG *source, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 463 | const YV12_BUFFER_CONFIG *dest, double *ssim_y, |
| 464 | double *ssim_u, double *ssim_v, uint32_t bd, |
| 465 | uint32_t in_bd) { |
| 466 | double ssimv; |
| 467 | uint32_t bd_shift = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 468 | aom_clear_system_state(); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 469 | assert(bd >= in_bd); |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 470 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 471 | bd_shift = bd - in_bd; |
| 472 | |
| 473 | *ssim_y = calc_ssim(source->y_buffer, source->y_stride, dest->y_buffer, |
| 474 | dest->y_stride, source->y_crop_width, |
| 475 | source->y_crop_height, in_bd, bd_shift); |
| 476 | *ssim_u = calc_ssim(source->u_buffer, source->uv_stride, dest->u_buffer, |
| 477 | dest->uv_stride, source->uv_crop_width, |
| 478 | source->uv_crop_height, in_bd, bd_shift); |
| 479 | *ssim_v = calc_ssim(source->v_buffer, source->uv_stride, dest->v_buffer, |
| 480 | dest->uv_stride, source->uv_crop_width, |
| 481 | source->uv_crop_height, in_bd, bd_shift); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 482 | ssimv = (*ssim_y) * .8 + .1 * ((*ssim_u) + (*ssim_v)); |
| 483 | return convert_ssim_db(ssimv, 1.0); |
| 484 | } |