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