John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1 | /* |
John Koleszar | c2140b8 | 2010-09-09 08:16:39 -0400 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 3 | * |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 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 |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 7 | * in the file PATENTS. All contributing project authors may |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 8 | * be found in the AUTHORS file in the root of the source tree. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 9 | */ |
| 10 | |
Yaowu Xu | eeaf8e6 | 2016-02-22 16:05:19 -0800 | [diff] [blame] | 11 | #include <assert.h> |
Jim Bankoski | ee87e20 | 2015-04-21 10:05:37 -0700 | [diff] [blame] | 12 | #include <math.h> |
Alex Converse | c7b7011 | 2015-08-06 12:53:59 -0700 | [diff] [blame] | 13 | #include "./vpx_dsp_rtcd.h" |
| 14 | #include "vpx_dsp/ssim.h" |
Johann | 1d7ccd5 | 2015-05-11 19:09:22 -0700 | [diff] [blame] | 15 | #include "vpx_ports/mem.h" |
Alex Converse | a8a08ce | 2015-08-10 11:28:04 -0700 | [diff] [blame] | 16 | #include "vpx_ports/system_state.h" |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 17 | |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 18 | void vpx_ssim_parms_16x16_c(const uint8_t *s, int sp, const uint8_t *r, int rp, |
| 19 | uint32_t *sum_s, uint32_t *sum_r, |
Alex Converse | c65e79d | 2015-08-06 13:46:28 -0700 | [diff] [blame] | 20 | uint32_t *sum_sq_s, uint32_t *sum_sq_r, |
| 21 | uint32_t *sum_sxr) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 22 | int i, j; |
| 23 | for (i = 0; i < 16; i++, s += sp, r += rp) { |
| 24 | for (j = 0; j < 16; j++) { |
| 25 | *sum_s += s[j]; |
| 26 | *sum_r += r[j]; |
| 27 | *sum_sq_s += s[j] * s[j]; |
| 28 | *sum_sq_r += r[j] * r[j]; |
| 29 | *sum_sxr += s[j] * r[j]; |
| 30 | } |
| 31 | } |
Jim Bankoski | 3f6f728 | 2011-03-08 09:05:18 -0500 | [diff] [blame] | 32 | } |
Alex Converse | 26f4f2d | 2015-08-07 10:48:32 -0700 | [diff] [blame] | 33 | void vpx_ssim_parms_8x8_c(const uint8_t *s, int sp, const uint8_t *r, int rp, |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 34 | uint32_t *sum_s, uint32_t *sum_r, uint32_t *sum_sq_s, |
| 35 | uint32_t *sum_sq_r, uint32_t *sum_sxr) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 36 | int i, j; |
| 37 | for (i = 0; i < 8; i++, s += sp, r += rp) { |
| 38 | for (j = 0; j < 8; j++) { |
| 39 | *sum_s += s[j]; |
| 40 | *sum_r += r[j]; |
| 41 | *sum_sq_s += s[j] * s[j]; |
| 42 | *sum_sq_r += r[j] * r[j]; |
| 43 | *sum_sxr += s[j] * r[j]; |
| 44 | } |
| 45 | } |
Jim Bankoski | 3f6f728 | 2011-03-08 09:05:18 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Deb Mukherjee | 4109372 | 2014-09-19 16:51:59 -0700 | [diff] [blame] | 48 | #if CONFIG_VP9_HIGHBITDEPTH |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 49 | void vpx_highbd_ssim_parms_8x8_c(const uint16_t *s, int sp, const uint16_t *r, |
| 50 | int rp, uint32_t *sum_s, uint32_t *sum_r, |
Deb Mukherjee | 4109372 | 2014-09-19 16:51:59 -0700 | [diff] [blame] | 51 | uint32_t *sum_sq_s, uint32_t *sum_sq_r, |
| 52 | uint32_t *sum_sxr) { |
| 53 | int i, j; |
| 54 | for (i = 0; i < 8; i++, s += sp, r += rp) { |
| 55 | for (j = 0; j < 8; j++) { |
| 56 | *sum_s += s[j]; |
| 57 | *sum_r += r[j]; |
| 58 | *sum_sq_s += s[j] * s[j]; |
| 59 | *sum_sq_r += r[j] * r[j]; |
| 60 | *sum_sxr += s[j] * r[j]; |
| 61 | } |
| 62 | } |
| 63 | } |
Deb Mukherjee | 4109372 | 2014-09-19 16:51:59 -0700 | [diff] [blame] | 64 | #endif // CONFIG_VP9_HIGHBITDEPTH |
| 65 | |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 66 | static const int64_t cc1 = 26634; // (64^2*(.01*255)^2 |
| 67 | static const int64_t cc2 = 239708; // (64^2*(.03*255)^2 |
| 68 | static const int64_t cc1_10 = 428658; // (64^2*(.01*1023)^2 |
| 69 | static const int64_t cc2_10 = 3857925; // (64^2*(.03*1023)^2 |
| 70 | static const int64_t cc1_12 = 6868593; // (64^2*(.01*4095)^2 |
Yaowu Xu | eeaf8e6 | 2016-02-22 16:05:19 -0800 | [diff] [blame] | 71 | static const int64_t cc2_12 = 61817334; // (64^2*(.03*4095)^2 |
Jim Bankoski | 3f6f728 | 2011-03-08 09:05:18 -0500 | [diff] [blame] | 72 | |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 73 | static double similarity(uint32_t sum_s, uint32_t sum_r, uint32_t sum_sq_s, |
| 74 | uint32_t sum_sq_r, uint32_t sum_sxr, int count, |
Yaowu Xu | eeaf8e6 | 2016-02-22 16:05:19 -0800 | [diff] [blame] | 75 | uint32_t bd) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 76 | int64_t ssim_n, ssim_d; |
| 77 | int64_t c1, c2; |
Yaowu Xu | eeaf8e6 | 2016-02-22 16:05:19 -0800 | [diff] [blame] | 78 | if (bd == 8) { |
| 79 | // scale the constants by number of pixels |
| 80 | c1 = (cc1 * count * count) >> 12; |
| 81 | c2 = (cc2 * count * count) >> 12; |
| 82 | } else if (bd == 10) { |
| 83 | c1 = (cc1_10 * count * count) >> 12; |
| 84 | c2 = (cc2_10 * count * count) >> 12; |
| 85 | } else if (bd == 12) { |
| 86 | c1 = (cc1_12 * count * count) >> 12; |
| 87 | c2 = (cc2_12 * count * count) >> 12; |
hui su | 4aeabf1 | 2016-02-26 13:26:54 -0800 | [diff] [blame] | 88 | } else { |
| 89 | c1 = c2 = 0; |
| 90 | assert(0); |
Yaowu Xu | eeaf8e6 | 2016-02-22 16:05:19 -0800 | [diff] [blame] | 91 | } |
Jim Bankoski | d4cdb68 | 2011-03-28 16:39:05 -0700 | [diff] [blame] | 92 | |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 93 | ssim_n = (2 * sum_s * sum_r + c1) * |
| 94 | ((int64_t)2 * count * sum_sxr - (int64_t)2 * sum_s * sum_r + c2); |
Jim Bankoski | d4cdb68 | 2011-03-28 16:39:05 -0700 | [diff] [blame] | 95 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 96 | ssim_d = (sum_s * sum_s + sum_r * sum_r + c1) * |
| 97 | ((int64_t)count * sum_sq_s - (int64_t)sum_s * sum_s + |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 98 | (int64_t)count * sum_sq_r - (int64_t)sum_r * sum_r + c2); |
Jim Bankoski | 3f6f728 | 2011-03-08 09:05:18 -0500 | [diff] [blame] | 99 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 100 | return ssim_n * 1.0 / ssim_d; |
Jim Bankoski | 3f6f728 | 2011-03-08 09:05:18 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Alex Converse | 26f4f2d | 2015-08-07 10:48:32 -0700 | [diff] [blame] | 103 | static double ssim_8x8(const uint8_t *s, int sp, const uint8_t *r, int rp) { |
Alex Converse | c65e79d | 2015-08-06 13:46:28 -0700 | [diff] [blame] | 104 | uint32_t sum_s = 0, sum_r = 0, sum_sq_s = 0, sum_sq_r = 0, sum_sxr = 0; |
Alex Converse | c7b7011 | 2015-08-06 12:53:59 -0700 | [diff] [blame] | 105 | vpx_ssim_parms_8x8(s, sp, r, rp, &sum_s, &sum_r, &sum_sq_s, &sum_sq_r, |
Jim Bankoski | 118b2fe | 2012-10-21 20:47:57 -0700 | [diff] [blame] | 106 | &sum_sxr); |
Yaowu Xu | eeaf8e6 | 2016-02-22 16:05:19 -0800 | [diff] [blame] | 107 | return similarity(sum_s, sum_r, sum_sq_s, sum_sq_r, sum_sxr, 64, 8); |
Jim Bankoski | 3f6f728 | 2011-03-08 09:05:18 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Deb Mukherjee | 4109372 | 2014-09-19 16:51:59 -0700 | [diff] [blame] | 110 | #if CONFIG_VP9_HIGHBITDEPTH |
Alex Converse | 26f4f2d | 2015-08-07 10:48:32 -0700 | [diff] [blame] | 111 | static double highbd_ssim_8x8(const uint16_t *s, int sp, const uint16_t *r, |
Yaowu Xu | eeaf8e6 | 2016-02-22 16:05:19 -0800 | [diff] [blame] | 112 | int rp, uint32_t bd, uint32_t shift) { |
Deb Mukherjee | 4109372 | 2014-09-19 16:51:59 -0700 | [diff] [blame] | 113 | uint32_t sum_s = 0, sum_r = 0, sum_sq_s = 0, sum_sq_r = 0, sum_sxr = 0; |
Alex Converse | c7b7011 | 2015-08-06 12:53:59 -0700 | [diff] [blame] | 114 | vpx_highbd_ssim_parms_8x8(s, sp, r, rp, &sum_s, &sum_r, &sum_sq_s, &sum_sq_r, |
Deb Mukherjee | 4109372 | 2014-09-19 16:51:59 -0700 | [diff] [blame] | 115 | &sum_sxr); |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 116 | return similarity(sum_s >> shift, sum_r >> shift, sum_sq_s >> (2 * shift), |
| 117 | sum_sq_r >> (2 * shift), sum_sxr >> (2 * shift), 64, bd); |
Deb Mukherjee | 4109372 | 2014-09-19 16:51:59 -0700 | [diff] [blame] | 118 | } |
| 119 | #endif // CONFIG_VP9_HIGHBITDEPTH |
| 120 | |
Yaowu Xu | 6c565fa | 2011-05-02 15:27:14 -0700 | [diff] [blame] | 121 | // We are using a 8x8 moving window with starting location of each 8x8 window |
| 122 | // on the 4x4 pixel grid. Such arrangement allows the windows to overlap |
| 123 | // block boundaries to penalize blocking artifacts. |
Alex Converse | 26f4f2d | 2015-08-07 10:48:32 -0700 | [diff] [blame] | 124 | static double vpx_ssim2(const uint8_t *img1, const uint8_t *img2, |
| 125 | int stride_img1, int stride_img2, int width, |
| 126 | int height) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 127 | int i, j; |
| 128 | int samples = 0; |
| 129 | double ssim_total = 0; |
Jim Bankoski | 3f6f728 | 2011-03-08 09:05:18 -0500 | [diff] [blame] | 130 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 131 | // sample point start with each 4x4 location |
Ronald S. Bultje | 44f349d | 2013-06-10 11:36:04 -0700 | [diff] [blame] | 132 | for (i = 0; i <= height - 8; |
| 133 | i += 4, img1 += stride_img1 * 4, img2 += stride_img2 * 4) { |
| 134 | for (j = 0; j <= width - 8; j += 4) { |
Jim Bankoski | 118b2fe | 2012-10-21 20:47:57 -0700 | [diff] [blame] | 135 | double v = ssim_8x8(img1 + j, stride_img1, img2 + j, stride_img2); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 136 | ssim_total += v; |
| 137 | samples++; |
Jim Bankoski | 3f6f728 | 2011-03-08 09:05:18 -0500 | [diff] [blame] | 138 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 139 | } |
| 140 | ssim_total /= samples; |
| 141 | return ssim_total; |
Jim Bankoski | 3f6f728 | 2011-03-08 09:05:18 -0500 | [diff] [blame] | 142 | } |
Deb Mukherjee | 4109372 | 2014-09-19 16:51:59 -0700 | [diff] [blame] | 143 | |
| 144 | #if CONFIG_VP9_HIGHBITDEPTH |
Alex Converse | 26f4f2d | 2015-08-07 10:48:32 -0700 | [diff] [blame] | 145 | static double vpx_highbd_ssim2(const uint8_t *img1, const uint8_t *img2, |
| 146 | int stride_img1, int stride_img2, int width, |
Yaowu Xu | eeaf8e6 | 2016-02-22 16:05:19 -0800 | [diff] [blame] | 147 | int height, uint32_t bd, uint32_t shift) { |
Deb Mukherjee | 4109372 | 2014-09-19 16:51:59 -0700 | [diff] [blame] | 148 | int i, j; |
| 149 | int samples = 0; |
| 150 | double ssim_total = 0; |
| 151 | |
Deb Mukherjee | a160d72 | 2014-09-30 21:56:33 -0700 | [diff] [blame] | 152 | // sample point start with each 4x4 location |
| 153 | for (i = 0; i <= height - 8; |
| 154 | i += 4, img1 += stride_img1 * 4, img2 += stride_img2 * 4) { |
| 155 | for (j = 0; j <= width - 8; j += 4) { |
| 156 | double v = highbd_ssim_8x8(CONVERT_TO_SHORTPTR(img1 + j), stride_img1, |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 157 | CONVERT_TO_SHORTPTR(img2 + j), stride_img2, bd, |
| 158 | shift); |
Deb Mukherjee | a160d72 | 2014-09-30 21:56:33 -0700 | [diff] [blame] | 159 | ssim_total += v; |
| 160 | samples++; |
Deb Mukherjee | 4109372 | 2014-09-19 16:51:59 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | ssim_total /= samples; |
| 164 | return ssim_total; |
| 165 | } |
| 166 | #endif // CONFIG_VP9_HIGHBITDEPTH |
| 167 | |
Alex Converse | 26f4f2d | 2015-08-07 10:48:32 -0700 | [diff] [blame] | 168 | double vpx_calc_ssim(const YV12_BUFFER_CONFIG *source, |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 169 | const YV12_BUFFER_CONFIG *dest, double *weight) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 170 | double a, b, c; |
| 171 | double ssimv; |
Jim Bankoski | 3dc3822 | 2011-03-08 15:23:40 -0500 | [diff] [blame] | 172 | |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 173 | a = vpx_ssim2(source->y_buffer, dest->y_buffer, source->y_stride, |
| 174 | dest->y_stride, source->y_crop_width, source->y_crop_height); |
Jim Bankoski | 3f6f728 | 2011-03-08 09:05:18 -0500 | [diff] [blame] | 175 | |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 176 | b = vpx_ssim2(source->u_buffer, dest->u_buffer, source->uv_stride, |
| 177 | dest->uv_stride, source->uv_crop_width, source->uv_crop_height); |
Jim Bankoski | 3f6f728 | 2011-03-08 09:05:18 -0500 | [diff] [blame] | 178 | |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 179 | c = vpx_ssim2(source->v_buffer, dest->v_buffer, source->uv_stride, |
| 180 | dest->uv_stride, source->uv_crop_width, source->uv_crop_height); |
Jim Bankoski | 3f6f728 | 2011-03-08 09:05:18 -0500 | [diff] [blame] | 181 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 182 | ssimv = a * .8 + .1 * (b + c); |
Jim Bankoski | 3f6f728 | 2011-03-08 09:05:18 -0500 | [diff] [blame] | 183 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 184 | *weight = 1; |
Jim Bankoski | 3f6f728 | 2011-03-08 09:05:18 -0500 | [diff] [blame] | 185 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 186 | return ssimv; |
Yaowu Xu | 6c565fa | 2011-05-02 15:27:14 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Jim Bankoski | ee87e20 | 2015-04-21 10:05:37 -0700 | [diff] [blame] | 189 | // traditional ssim as per: http://en.wikipedia.org/wiki/Structural_similarity |
| 190 | // |
| 191 | // Re working out the math -> |
| 192 | // |
| 193 | // ssim(x,y) = (2*mean(x)*mean(y) + c1)*(2*cov(x,y)+c2) / |
| 194 | // ((mean(x)^2+mean(y)^2+c1)*(var(x)+var(y)+c2)) |
| 195 | // |
| 196 | // mean(x) = sum(x) / n |
| 197 | // |
| 198 | // cov(x,y) = (n*sum(xi*yi)-sum(x)*sum(y))/(n*n) |
| 199 | // |
| 200 | // var(x) = (n*sum(xi*xi)-sum(xi)*sum(xi))/(n*n) |
| 201 | // |
| 202 | // ssim(x,y) = |
| 203 | // (2*sum(x)*sum(y)/(n*n) + c1)*(2*(n*sum(xi*yi)-sum(x)*sum(y))/(n*n)+c2) / |
| 204 | // (((sum(x)*sum(x)+sum(y)*sum(y))/(n*n) +c1) * |
| 205 | // ((n*sum(xi*xi) - sum(xi)*sum(xi))/(n*n)+ |
| 206 | // (n*sum(yi*yi) - sum(yi)*sum(yi))/(n*n)+c2))) |
| 207 | // |
| 208 | // factoring out n*n |
| 209 | // |
| 210 | // ssim(x,y) = |
| 211 | // (2*sum(x)*sum(y) + n*n*c1)*(2*(n*sum(xi*yi)-sum(x)*sum(y))+n*n*c2) / |
| 212 | // (((sum(x)*sum(x)+sum(y)*sum(y)) + n*n*c1) * |
| 213 | // (n*sum(xi*xi)-sum(xi)*sum(xi)+n*sum(yi*yi)-sum(yi)*sum(yi)+n*n*c2)) |
| 214 | // |
| 215 | // Replace c1 with n*n * c1 for the final step that leads to this code: |
| 216 | // The final step scales by 12 bits so we don't lose precision in the constants. |
| 217 | |
Alex Converse | 26f4f2d | 2015-08-07 10:48:32 -0700 | [diff] [blame] | 218 | static double ssimv_similarity(const Ssimv *sv, int64_t n) { |
Jim Bankoski | ee87e20 | 2015-04-21 10:05:37 -0700 | [diff] [blame] | 219 | // Scale the constants by number of pixels. |
| 220 | const int64_t c1 = (cc1 * n * n) >> 12; |
| 221 | const int64_t c2 = (cc2 * n * n) >> 12; |
| 222 | |
| 223 | const double l = 1.0 * (2 * sv->sum_s * sv->sum_r + c1) / |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 224 | (sv->sum_s * sv->sum_s + sv->sum_r * sv->sum_r + c1); |
Jim Bankoski | ee87e20 | 2015-04-21 10:05:37 -0700 | [diff] [blame] | 225 | |
| 226 | // Since these variables are unsigned sums, convert to double so |
| 227 | // math is done in double arithmetic. |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 228 | const double v = (2.0 * n * sv->sum_sxr - 2 * sv->sum_s * sv->sum_r + c2) / |
| 229 | (n * sv->sum_sq_s - sv->sum_s * sv->sum_s + |
| 230 | n * sv->sum_sq_r - sv->sum_r * sv->sum_r + c2); |
Jim Bankoski | ee87e20 | 2015-04-21 10:05:37 -0700 | [diff] [blame] | 231 | |
| 232 | return l * v; |
| 233 | } |
| 234 | |
| 235 | // The first term of the ssim metric is a luminance factor. |
| 236 | // |
| 237 | // (2*mean(x)*mean(y) + c1)/ (mean(x)^2+mean(y)^2+c1) |
| 238 | // |
| 239 | // This luminance factor is super sensitive to the dark side of luminance |
| 240 | // values and completely insensitive on the white side. check out 2 sets |
| 241 | // (1,3) and (250,252) the term gives ( 2*1*3/(1+9) = .60 |
| 242 | // 2*250*252/ (250^2+252^2) => .99999997 |
| 243 | // |
| 244 | // As a result in this tweaked version of the calculation in which the |
| 245 | // luminance is taken as percentage off from peak possible. |
| 246 | // |
| 247 | // 255 * 255 - (sum_s - sum_r) / count * (sum_s - sum_r) / count |
| 248 | // |
Alex Converse | 26f4f2d | 2015-08-07 10:48:32 -0700 | [diff] [blame] | 249 | static double ssimv_similarity2(const Ssimv *sv, int64_t n) { |
Jim Bankoski | ee87e20 | 2015-04-21 10:05:37 -0700 | [diff] [blame] | 250 | // Scale the constants by number of pixels. |
| 251 | const int64_t c1 = (cc1 * n * n) >> 12; |
| 252 | const int64_t c2 = (cc2 * n * n) >> 12; |
| 253 | |
| 254 | const double mean_diff = (1.0 * sv->sum_s - sv->sum_r) / n; |
| 255 | const double l = (255 * 255 - mean_diff * mean_diff + c1) / (255 * 255 + c1); |
| 256 | |
| 257 | // Since these variables are unsigned, sums convert to double so |
| 258 | // math is done in double arithmetic. |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 259 | const double v = (2.0 * n * sv->sum_sxr - 2 * sv->sum_s * sv->sum_r + c2) / |
| 260 | (n * sv->sum_sq_s - sv->sum_s * sv->sum_s + |
| 261 | n * sv->sum_sq_r - sv->sum_r * sv->sum_r + c2); |
Jim Bankoski | ee87e20 | 2015-04-21 10:05:37 -0700 | [diff] [blame] | 262 | |
| 263 | return l * v; |
| 264 | } |
Alex Converse | 26f4f2d | 2015-08-07 10:48:32 -0700 | [diff] [blame] | 265 | static void ssimv_parms(uint8_t *img1, int img1_pitch, uint8_t *img2, |
| 266 | int img2_pitch, Ssimv *sv) { |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 267 | vpx_ssim_parms_8x8(img1, img1_pitch, img2, img2_pitch, &sv->sum_s, &sv->sum_r, |
| 268 | &sv->sum_sq_s, &sv->sum_sq_r, &sv->sum_sxr); |
Jim Bankoski | ee87e20 | 2015-04-21 10:05:37 -0700 | [diff] [blame] | 269 | } |
| 270 | |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 271 | double vpx_get_ssim_metrics(uint8_t *img1, int img1_pitch, uint8_t *img2, |
| 272 | int img2_pitch, int width, int height, Ssimv *sv2, |
| 273 | Metrics *m, int do_inconsistency) { |
Jim Bankoski | ee87e20 | 2015-04-21 10:05:37 -0700 | [diff] [blame] | 274 | double dssim_total = 0; |
| 275 | double ssim_total = 0; |
| 276 | double ssim2_total = 0; |
| 277 | double inconsistency_total = 0; |
| 278 | int i, j; |
| 279 | int c = 0; |
| 280 | double norm; |
| 281 | double old_ssim_total = 0; |
Alex Converse | c7b7011 | 2015-08-06 12:53:59 -0700 | [diff] [blame] | 282 | vpx_clear_system_state(); |
Jim Bankoski | ee87e20 | 2015-04-21 10:05:37 -0700 | [diff] [blame] | 283 | // We can sample points as frequently as we like start with 1 per 4x4. |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 284 | for (i = 0; i < height; |
| 285 | i += 4, img1 += img1_pitch * 4, img2 += img2_pitch * 4) { |
Jim Bankoski | ee87e20 | 2015-04-21 10:05:37 -0700 | [diff] [blame] | 286 | for (j = 0; j < width; j += 4, ++c) { |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 287 | Ssimv sv = { 0 }; |
Jim Bankoski | ee87e20 | 2015-04-21 10:05:37 -0700 | [diff] [blame] | 288 | double ssim; |
| 289 | double ssim2; |
| 290 | double dssim; |
| 291 | uint32_t var_new; |
| 292 | uint32_t var_old; |
| 293 | uint32_t mean_new; |
| 294 | uint32_t mean_old; |
| 295 | double ssim_new; |
| 296 | double ssim_old; |
| 297 | |
| 298 | // Not sure there's a great way to handle the edge pixels |
| 299 | // in ssim when using a window. Seems biased against edge pixels |
| 300 | // however you handle this. This uses only samples that are |
| 301 | // fully in the frame. |
| 302 | if (j + 8 <= width && i + 8 <= height) { |
| 303 | ssimv_parms(img1 + j, img1_pitch, img2 + j, img2_pitch, &sv); |
| 304 | } |
| 305 | |
| 306 | ssim = ssimv_similarity(&sv, 64); |
| 307 | ssim2 = ssimv_similarity2(&sv, 64); |
| 308 | |
| 309 | sv.ssim = ssim2; |
| 310 | |
| 311 | // dssim is calculated to use as an actual error metric and |
| 312 | // is scaled up to the same range as sum square error. |
| 313 | // Since we are subsampling every 16th point maybe this should be |
| 314 | // *16 ? |
| 315 | dssim = 255 * 255 * (1 - ssim2) / 2; |
| 316 | |
| 317 | // Here I introduce a new error metric: consistency-weighted |
| 318 | // SSIM-inconsistency. This metric isolates frames where the |
| 319 | // SSIM 'suddenly' changes, e.g. if one frame in every 8 is much |
| 320 | // sharper or blurrier than the others. Higher values indicate a |
| 321 | // temporally inconsistent SSIM. There are two ideas at work: |
| 322 | // |
| 323 | // 1) 'SSIM-inconsistency': the total inconsistency value |
| 324 | // reflects how much SSIM values are changing between this |
| 325 | // source / reference frame pair and the previous pair. |
| 326 | // |
| 327 | // 2) 'consistency-weighted': weights de-emphasize areas in the |
| 328 | // frame where the scene content has changed. Changes in scene |
| 329 | // content are detected via changes in local variance and local |
| 330 | // mean. |
| 331 | // |
| 332 | // Thus the overall measure reflects how inconsistent the SSIM |
| 333 | // values are, over consistent regions of the frame. |
| 334 | // |
| 335 | // The metric has three terms: |
| 336 | // |
| 337 | // term 1 -> uses change in scene Variance to weight error score |
| 338 | // 2 * var(Fi)*var(Fi-1) / (var(Fi)^2+var(Fi-1)^2) |
| 339 | // larger changes from one frame to the next mean we care |
| 340 | // less about consistency. |
| 341 | // |
| 342 | // term 2 -> uses change in local scene luminance to weight error |
| 343 | // 2 * avg(Fi)*avg(Fi-1) / (avg(Fi)^2+avg(Fi-1)^2) |
| 344 | // larger changes from one frame to the next mean we care |
| 345 | // less about consistency. |
| 346 | // |
| 347 | // term3 -> measures inconsistency in ssim scores between frames |
| 348 | // 1 - ( 2 * ssim(Fi)*ssim(Fi-1)/(ssim(Fi)^2+sssim(Fi-1)^2). |
| 349 | // |
| 350 | // This term compares the ssim score for the same location in 2 |
| 351 | // subsequent frames. |
| 352 | var_new = sv.sum_sq_s - sv.sum_s * sv.sum_s / 64; |
| 353 | var_old = sv2[c].sum_sq_s - sv2[c].sum_s * sv2[c].sum_s / 64; |
| 354 | mean_new = sv.sum_s; |
| 355 | mean_old = sv2[c].sum_s; |
| 356 | ssim_new = sv.ssim; |
| 357 | ssim_old = sv2[c].ssim; |
| 358 | |
| 359 | if (do_inconsistency) { |
| 360 | // We do the metric once for every 4x4 block in the image. Since |
| 361 | // we are scaling the error to SSE for use in a psnr calculation |
| 362 | // 1.0 = 4x4x255x255 the worst error we can possibly have. |
| 363 | static const double kScaling = 4. * 4 * 255 * 255; |
| 364 | |
| 365 | // The constants have to be non 0 to avoid potential divide by 0 |
| 366 | // issues other than that they affect kind of a weighting between |
| 367 | // the terms. No testing of what the right terms should be has been |
| 368 | // done. |
| 369 | static const double c1 = 1, c2 = 1, c3 = 1; |
| 370 | |
| 371 | // This measures how much consistent variance is in two consecutive |
| 372 | // source frames. 1.0 means they have exactly the same variance. |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 373 | const double variance_term = |
| 374 | (2.0 * var_old * var_new + c1) / |
Jim Bankoski | ee87e20 | 2015-04-21 10:05:37 -0700 | [diff] [blame] | 375 | (1.0 * var_old * var_old + 1.0 * var_new * var_new + c1); |
| 376 | |
| 377 | // This measures how consistent the local mean are between two |
| 378 | // consecutive frames. 1.0 means they have exactly the same mean. |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 379 | const double mean_term = |
| 380 | (2.0 * mean_old * mean_new + c2) / |
Jim Bankoski | ee87e20 | 2015-04-21 10:05:37 -0700 | [diff] [blame] | 381 | (1.0 * mean_old * mean_old + 1.0 * mean_new * mean_new + c2); |
| 382 | |
| 383 | // This measures how consistent the ssims of two |
| 384 | // consecutive frames is. 1.0 means they are exactly the same. |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 385 | double ssim_term = |
| 386 | pow((2.0 * ssim_old * ssim_new + c3) / |
| 387 | (ssim_old * ssim_old + ssim_new * ssim_new + c3), |
| 388 | 5); |
Jim Bankoski | ee87e20 | 2015-04-21 10:05:37 -0700 | [diff] [blame] | 389 | |
| 390 | double this_inconsistency; |
| 391 | |
| 392 | // Floating point math sometimes makes this > 1 by a tiny bit. |
| 393 | // We want the metric to scale between 0 and 1.0 so we can convert |
| 394 | // it to an snr scaled value. |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 395 | if (ssim_term > 1) ssim_term = 1; |
Jim Bankoski | ee87e20 | 2015-04-21 10:05:37 -0700 | [diff] [blame] | 396 | |
| 397 | // This converts the consistency metric to an inconsistency metric |
| 398 | // ( so we can scale it like psnr to something like sum square error. |
| 399 | // The reason for the variance and mean terms is the assumption that |
| 400 | // if there are big changes in the source we shouldn't penalize |
| 401 | // inconsistency in ssim scores a bit less as it will be less visible |
| 402 | // to the user. |
| 403 | this_inconsistency = (1 - ssim_term) * variance_term * mean_term; |
| 404 | |
| 405 | this_inconsistency *= kScaling; |
| 406 | inconsistency_total += this_inconsistency; |
| 407 | } |
| 408 | sv2[c] = sv; |
| 409 | ssim_total += ssim; |
| 410 | ssim2_total += ssim2; |
| 411 | dssim_total += dssim; |
| 412 | |
| 413 | old_ssim_total += ssim_old; |
| 414 | } |
| 415 | old_ssim_total += 0; |
| 416 | } |
| 417 | |
| 418 | norm = 1. / (width / 4) / (height / 4); |
| 419 | ssim_total *= norm; |
| 420 | ssim2_total *= norm; |
| 421 | m->ssim2 = ssim2_total; |
| 422 | m->ssim = ssim_total; |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 423 | if (old_ssim_total == 0) inconsistency_total = 0; |
Jim Bankoski | ee87e20 | 2015-04-21 10:05:37 -0700 | [diff] [blame] | 424 | |
| 425 | m->ssimc = inconsistency_total; |
| 426 | |
| 427 | m->dssim = dssim_total; |
| 428 | return inconsistency_total; |
| 429 | } |
| 430 | |
Deb Mukherjee | 4109372 | 2014-09-19 16:51:59 -0700 | [diff] [blame] | 431 | #if CONFIG_VP9_HIGHBITDEPTH |
Alex Converse | 26f4f2d | 2015-08-07 10:48:32 -0700 | [diff] [blame] | 432 | double vpx_highbd_calc_ssim(const YV12_BUFFER_CONFIG *source, |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 433 | const YV12_BUFFER_CONFIG *dest, double *weight, |
| 434 | uint32_t bd, uint32_t in_bd) { |
Deb Mukherjee | 4109372 | 2014-09-19 16:51:59 -0700 | [diff] [blame] | 435 | double a, b, c; |
| 436 | double ssimv; |
Yaowu Xu | eeaf8e6 | 2016-02-22 16:05:19 -0800 | [diff] [blame] | 437 | uint32_t shift = 0; |
| 438 | |
| 439 | assert(bd >= in_bd); |
| 440 | shift = bd - in_bd; |
Deb Mukherjee | 4109372 | 2014-09-19 16:51:59 -0700 | [diff] [blame] | 441 | |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 442 | a = vpx_highbd_ssim2(source->y_buffer, dest->y_buffer, source->y_stride, |
| 443 | dest->y_stride, source->y_crop_width, |
| 444 | source->y_crop_height, in_bd, shift); |
Deb Mukherjee | 4109372 | 2014-09-19 16:51:59 -0700 | [diff] [blame] | 445 | |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 446 | b = vpx_highbd_ssim2(source->u_buffer, dest->u_buffer, source->uv_stride, |
| 447 | dest->uv_stride, source->uv_crop_width, |
| 448 | source->uv_crop_height, in_bd, shift); |
Deb Mukherjee | 4109372 | 2014-09-19 16:51:59 -0700 | [diff] [blame] | 449 | |
clang-format | 1214cee | 2016-08-08 22:59:08 -0700 | [diff] [blame] | 450 | c = vpx_highbd_ssim2(source->v_buffer, dest->v_buffer, source->uv_stride, |
| 451 | dest->uv_stride, source->uv_crop_width, |
| 452 | source->uv_crop_height, in_bd, shift); |
Deb Mukherjee | 4109372 | 2014-09-19 16:51:59 -0700 | [diff] [blame] | 453 | |
| 454 | ssimv = a * .8 + .1 * (b + c); |
| 455 | |
| 456 | *weight = 1; |
| 457 | |
| 458 | return ssimv; |
| 459 | } |
| 460 | |
Deb Mukherjee | 4109372 | 2014-09-19 16:51:59 -0700 | [diff] [blame] | 461 | #endif // CONFIG_VP9_HIGHBITDEPTH |