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