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