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