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