blob: 966067662659961d0cadc261489f37b66ea56c9b [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 Xuf883b422016-08-30 14:01:10 -070034void aom_ssim_parms_8x8_c(const uint8_t *s, int sp, const uint8_t *r, int rp,
Yaowu Xuc27fc142016-08-22 16:08:15 -070035 uint32_t *sum_s, uint32_t *sum_r, uint32_t *sum_sq_s,
36 uint32_t *sum_sq_r, uint32_t *sum_sxr) {
37 int i, j;
38 for (i = 0; i < 8; i++, s += sp, r += rp) {
39 for (j = 0; j < 8; j++) {
40 *sum_s += s[j];
41 *sum_r += r[j];
42 *sum_sq_s += s[j] * s[j];
43 *sum_sq_r += r[j] * r[j];
44 *sum_sxr += s[j] * r[j];
45 }
46 }
47}
48
Yaowu Xuf883b422016-08-30 14:01:10 -070049#if CONFIG_AOM_HIGHBITDEPTH
50void 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 Xuf883b422016-08-30 14:01:10 -070065#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070066
67static const int64_t cc1 = 26634; // (64^2*(.01*255)^2
68static const int64_t cc2 = 239708; // (64^2*(.03*255)^2
69static const int64_t cc1_10 = 428658; // (64^2*(.01*1023)^2
70static const int64_t cc2_10 = 3857925; // (64^2*(.03*1023)^2
71static const int64_t cc1_12 = 6868593; // (64^2*(.01*4095)^2
72static const int64_t cc2_12 = 61817334; // (64^2*(.03*4095)^2
73
74static double similarity(uint32_t sum_s, uint32_t sum_r, uint32_t sum_sq_s,
75 uint32_t sum_sq_r, uint32_t sum_sxr, int count,
76 uint32_t bd) {
77 int64_t ssim_n, ssim_d;
78 int64_t c1, c2;
79 if (bd == 8) {
80 // scale the constants by number of pixels
81 c1 = (cc1 * count * count) >> 12;
82 c2 = (cc2 * count * count) >> 12;
83 } else if (bd == 10) {
84 c1 = (cc1_10 * count * count) >> 12;
85 c2 = (cc2_10 * count * count) >> 12;
86 } else if (bd == 12) {
87 c1 = (cc1_12 * count * count) >> 12;
88 c2 = (cc2_12 * count * count) >> 12;
89 } else {
90 c1 = c2 = 0;
91 assert(0);
92 }
93
94 ssim_n = (2 * sum_s * sum_r + c1) *
95 ((int64_t)2 * count * sum_sxr - (int64_t)2 * sum_s * sum_r + c2);
96
97 ssim_d = (sum_s * sum_s + sum_r * sum_r + c1) *
98 ((int64_t)count * sum_sq_s - (int64_t)sum_s * sum_s +
99 (int64_t)count * sum_sq_r - (int64_t)sum_r * sum_r + c2);
100
101 return ssim_n * 1.0 / ssim_d;
102}
103
104static double ssim_8x8(const uint8_t *s, int sp, const uint8_t *r, int rp) {
105 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 -0700106 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 -0700107 &sum_sxr);
108 return similarity(sum_s, sum_r, sum_sq_s, sum_sq_r, sum_sxr, 64, 8);
109}
110
Yaowu Xuf883b422016-08-30 14:01:10 -0700111#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700112static 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;
Yaowu Xuf883b422016-08-30 14:01:10 -0700115 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 -0700116 &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}
Yaowu Xuf883b422016-08-30 14:01:10 -0700120#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700121
122// We are using a 8x8 moving window with starting location of each 8x8 window
123// on the 4x4 pixel grid. Such arrangement allows the windows to overlap
124// block boundaries to penalize blocking artifacts.
Yaowu Xuf883b422016-08-30 14:01:10 -0700125static double aom_ssim2(const uint8_t *img1, const uint8_t *img2,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700126 int stride_img1, int stride_img2, int width,
127 int height) {
128 int i, j;
129 int samples = 0;
130 double ssim_total = 0;
131
132 // sample point start with each 4x4 location
133 for (i = 0; i <= height - 8;
134 i += 4, img1 += stride_img1 * 4, img2 += stride_img2 * 4) {
135 for (j = 0; j <= width - 8; j += 4) {
136 double v = ssim_8x8(img1 + j, stride_img1, img2 + j, stride_img2);
137 ssim_total += v;
138 samples++;
139 }
140 }
141 ssim_total /= samples;
142 return ssim_total;
143}
144
Yaowu Xuf883b422016-08-30 14:01:10 -0700145#if CONFIG_AOM_HIGHBITDEPTH
146static double aom_highbd_ssim2(const uint8_t *img1, const uint8_t *img2,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700147 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}
Yaowu Xuf883b422016-08-30 14:01:10 -0700167#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700168
Yaowu Xuf883b422016-08-30 14:01:10 -0700169double aom_calc_ssim(const YV12_BUFFER_CONFIG *source,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700170 const YV12_BUFFER_CONFIG *dest, double *weight) {
171 double a, b, c;
172 double ssimv;
173
Yaowu Xuf883b422016-08-30 14:01:10 -0700174 a = aom_ssim2(source->y_buffer, dest->y_buffer, source->y_stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700175 dest->y_stride, source->y_crop_width, source->y_crop_height);
176
Yaowu Xuf883b422016-08-30 14:01:10 -0700177 b = aom_ssim2(source->u_buffer, dest->u_buffer, source->uv_stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700178 dest->uv_stride, source->uv_crop_width, source->uv_crop_height);
179
Yaowu Xuf883b422016-08-30 14:01:10 -0700180 c = aom_ssim2(source->v_buffer, dest->v_buffer, source->uv_stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700181 dest->uv_stride, source->uv_crop_width, source->uv_crop_height);
182
183 ssimv = a * .8 + .1 * (b + c);
184
185 *weight = 1;
186
187 return ssimv;
188}
189
190// traditional ssim as per: http://en.wikipedia.org/wiki/Structural_similarity
191//
192// Re working out the math ->
193//
194// ssim(x,y) = (2*mean(x)*mean(y) + c1)*(2*cov(x,y)+c2) /
195// ((mean(x)^2+mean(y)^2+c1)*(var(x)+var(y)+c2))
196//
197// mean(x) = sum(x) / n
198//
199// cov(x,y) = (n*sum(xi*yi)-sum(x)*sum(y))/(n*n)
200//
201// var(x) = (n*sum(xi*xi)-sum(xi)*sum(xi))/(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*n)+
207// (n*sum(yi*yi) - sum(yi)*sum(yi))/(n*n)+c2)))
208//
209// factoring out n*n
210//
211// ssim(x,y) =
212// (2*sum(x)*sum(y) + n*n*c1)*(2*(n*sum(xi*yi)-sum(x)*sum(y))+n*n*c2) /
213// (((sum(x)*sum(x)+sum(y)*sum(y)) + n*n*c1) *
214// (n*sum(xi*xi)-sum(xi)*sum(xi)+n*sum(yi*yi)-sum(yi)*sum(yi)+n*n*c2))
215//
216// Replace c1 with n*n * c1 for the final step that leads to this code:
217// The final step scales by 12 bits so we don't lose precision in the constants.
218
219static double ssimv_similarity(const Ssimv *sv, int64_t n) {
220 // Scale the constants by number of pixels.
221 const int64_t c1 = (cc1 * n * n) >> 12;
222 const int64_t c2 = (cc2 * n * n) >> 12;
223
224 const double l = 1.0 * (2 * sv->sum_s * sv->sum_r + c1) /
225 (sv->sum_s * sv->sum_s + sv->sum_r * sv->sum_r + c1);
226
227 // Since these variables are unsigned sums, convert to double so
228 // math is done in double arithmetic.
229 const double v = (2.0 * n * sv->sum_sxr - 2 * sv->sum_s * sv->sum_r + c2) /
230 (n * sv->sum_sq_s - sv->sum_s * sv->sum_s +
231 n * sv->sum_sq_r - sv->sum_r * sv->sum_r + c2);
232
233 return l * v;
234}
235
236// The first term of the ssim metric is a luminance factor.
237//
238// (2*mean(x)*mean(y) + c1)/ (mean(x)^2+mean(y)^2+c1)
239//
240// This luminance factor is super sensitive to the dark side of luminance
241// values and completely insensitive on the white side. check out 2 sets
242// (1,3) and (250,252) the term gives ( 2*1*3/(1+9) = .60
243// 2*250*252/ (250^2+252^2) => .99999997
244//
245// As a result in this tweaked version of the calculation in which the
246// luminance is taken as percentage off from peak possible.
247//
248// 255 * 255 - (sum_s - sum_r) / count * (sum_s - sum_r) / count
249//
250static double ssimv_similarity2(const Ssimv *sv, int64_t n) {
251 // Scale the constants by number of pixels.
252 const int64_t c1 = (cc1 * n * n) >> 12;
253 const int64_t c2 = (cc2 * n * n) >> 12;
254
255 const double mean_diff = (1.0 * sv->sum_s - sv->sum_r) / n;
256 const double l = (255 * 255 - mean_diff * mean_diff + c1) / (255 * 255 + c1);
257
258 // Since these variables are unsigned, sums convert to double so
259 // math is done in double arithmetic.
260 const double v = (2.0 * n * sv->sum_sxr - 2 * sv->sum_s * sv->sum_r + c2) /
261 (n * sv->sum_sq_s - sv->sum_s * sv->sum_s +
262 n * sv->sum_sq_r - sv->sum_r * sv->sum_r + c2);
263
264 return l * v;
265}
266static void ssimv_parms(uint8_t *img1, int img1_pitch, uint8_t *img2,
267 int img2_pitch, Ssimv *sv) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700268 aom_ssim_parms_8x8(img1, img1_pitch, img2, img2_pitch, &sv->sum_s, &sv->sum_r,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700269 &sv->sum_sq_s, &sv->sum_sq_r, &sv->sum_sxr);
270}
271
Yaowu Xuf883b422016-08-30 14:01:10 -0700272double aom_get_ssim_metrics(uint8_t *img1, int img1_pitch, uint8_t *img2,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700273 int img2_pitch, int width, int height, Ssimv *sv2,
274 Metrics *m, int do_inconsistency) {
275 double dssim_total = 0;
276 double ssim_total = 0;
277 double ssim2_total = 0;
278 double inconsistency_total = 0;
279 int i, j;
280 int c = 0;
281 double norm;
282 double old_ssim_total = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700283 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700284 // We can sample points as frequently as we like start with 1 per 4x4.
285 for (i = 0; i < height;
286 i += 4, img1 += img1_pitch * 4, img2 += img2_pitch * 4) {
287 for (j = 0; j < width; j += 4, ++c) {
288 Ssimv sv = { 0 };
289 double ssim;
290 double ssim2;
291 double dssim;
292 uint32_t var_new;
293 uint32_t var_old;
294 uint32_t mean_new;
295 uint32_t mean_old;
296 double ssim_new;
297 double ssim_old;
298
299 // Not sure there's a great way to handle the edge pixels
300 // in ssim when using a window. Seems biased against edge pixels
301 // however you handle this. This uses only samples that are
302 // fully in the frame.
303 if (j + 8 <= width && i + 8 <= height) {
304 ssimv_parms(img1 + j, img1_pitch, img2 + j, img2_pitch, &sv);
305 }
306
307 ssim = ssimv_similarity(&sv, 64);
308 ssim2 = ssimv_similarity2(&sv, 64);
309
310 sv.ssim = ssim2;
311
312 // dssim is calculated to use as an actual error metric and
313 // is scaled up to the same range as sum square error.
314 // Since we are subsampling every 16th point maybe this should be
315 // *16 ?
316 dssim = 255 * 255 * (1 - ssim2) / 2;
317
318 // Here I introduce a new error metric: consistency-weighted
319 // SSIM-inconsistency. This metric isolates frames where the
320 // SSIM 'suddenly' changes, e.g. if one frame in every 8 is much
321 // sharper or blurrier than the others. Higher values indicate a
322 // temporally inconsistent SSIM. There are two ideas at work:
323 //
324 // 1) 'SSIM-inconsistency': the total inconsistency value
325 // reflects how much SSIM values are changing between this
326 // source / reference frame pair and the previous pair.
327 //
328 // 2) 'consistency-weighted': weights de-emphasize areas in the
329 // frame where the scene content has changed. Changes in scene
330 // content are detected via changes in local variance and local
331 // mean.
332 //
333 // Thus the overall measure reflects how inconsistent the SSIM
334 // values are, over consistent regions of the frame.
335 //
336 // The metric has three terms:
337 //
338 // term 1 -> uses change in scene Variance to weight error score
339 // 2 * var(Fi)*var(Fi-1) / (var(Fi)^2+var(Fi-1)^2)
340 // larger changes from one frame to the next mean we care
341 // less about consistency.
342 //
343 // term 2 -> uses change in local scene luminance to weight error
344 // 2 * avg(Fi)*avg(Fi-1) / (avg(Fi)^2+avg(Fi-1)^2)
345 // larger changes from one frame to the next mean we care
346 // less about consistency.
347 //
348 // term3 -> measures inconsistency in ssim scores between frames
349 // 1 - ( 2 * ssim(Fi)*ssim(Fi-1)/(ssim(Fi)^2+sssim(Fi-1)^2).
350 //
351 // This term compares the ssim score for the same location in 2
352 // subsequent frames.
353 var_new = sv.sum_sq_s - sv.sum_s * sv.sum_s / 64;
354 var_old = sv2[c].sum_sq_s - sv2[c].sum_s * sv2[c].sum_s / 64;
355 mean_new = sv.sum_s;
356 mean_old = sv2[c].sum_s;
357 ssim_new = sv.ssim;
358 ssim_old = sv2[c].ssim;
359
360 if (do_inconsistency) {
361 // We do the metric once for every 4x4 block in the image. Since
362 // we are scaling the error to SSE for use in a psnr calculation
363 // 1.0 = 4x4x255x255 the worst error we can possibly have.
364 static const double kScaling = 4. * 4 * 255 * 255;
365
366 // The constants have to be non 0 to avoid potential divide by 0
367 // issues other than that they affect kind of a weighting between
368 // the terms. No testing of what the right terms should be has been
369 // done.
370 static const double c1 = 1, c2 = 1, c3 = 1;
371
372 // This measures how much consistent variance is in two consecutive
373 // source frames. 1.0 means they have exactly the same variance.
374 const double variance_term =
375 (2.0 * var_old * var_new + c1) /
376 (1.0 * var_old * var_old + 1.0 * var_new * var_new + c1);
377
378 // This measures how consistent the local mean are between two
379 // consecutive frames. 1.0 means they have exactly the same mean.
380 const double mean_term =
381 (2.0 * mean_old * mean_new + c2) /
382 (1.0 * mean_old * mean_old + 1.0 * mean_new * mean_new + c2);
383
384 // This measures how consistent the ssims of two
385 // consecutive frames is. 1.0 means they are exactly the same.
386 double ssim_term =
387 pow((2.0 * ssim_old * ssim_new + c3) /
388 (ssim_old * ssim_old + ssim_new * ssim_new + c3),
389 5);
390
391 double this_inconsistency;
392
393 // Floating point math sometimes makes this > 1 by a tiny bit.
394 // We want the metric to scale between 0 and 1.0 so we can convert
395 // it to an snr scaled value.
396 if (ssim_term > 1) ssim_term = 1;
397
398 // This converts the consistency metric to an inconsistency metric
399 // ( so we can scale it like psnr to something like sum square error.
400 // The reason for the variance and mean terms is the assumption that
401 // if there are big changes in the source we shouldn't penalize
402 // inconsistency in ssim scores a bit less as it will be less visible
403 // to the user.
404 this_inconsistency = (1 - ssim_term) * variance_term * mean_term;
405
406 this_inconsistency *= kScaling;
407 inconsistency_total += this_inconsistency;
408 }
409 sv2[c] = sv;
410 ssim_total += ssim;
411 ssim2_total += ssim2;
412 dssim_total += dssim;
413
414 old_ssim_total += ssim_old;
415 }
416 old_ssim_total += 0;
417 }
418
419 norm = 1. / (width / 4) / (height / 4);
420 ssim_total *= norm;
421 ssim2_total *= norm;
422 m->ssim2 = ssim2_total;
423 m->ssim = ssim_total;
424 if (old_ssim_total == 0) inconsistency_total = 0;
425
426 m->ssimc = inconsistency_total;
427
428 m->dssim = dssim_total;
429 return inconsistency_total;
430}
431
Yaowu Xuf883b422016-08-30 14:01:10 -0700432#if CONFIG_AOM_HIGHBITDEPTH
433double aom_highbd_calc_ssim(const YV12_BUFFER_CONFIG *source,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700434 const YV12_BUFFER_CONFIG *dest, double *weight,
435 uint32_t bd, uint32_t in_bd) {
436 double a, b, c;
437 double ssimv;
438 uint32_t shift = 0;
439
440 assert(bd >= in_bd);
441 shift = bd - in_bd;
442
Yaowu Xuf883b422016-08-30 14:01:10 -0700443 a = aom_highbd_ssim2(source->y_buffer, dest->y_buffer, source->y_stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700444 dest->y_stride, source->y_crop_width,
445 source->y_crop_height, in_bd, shift);
446
Yaowu Xuf883b422016-08-30 14:01:10 -0700447 b = aom_highbd_ssim2(source->u_buffer, dest->u_buffer, source->uv_stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700448 dest->uv_stride, source->uv_crop_width,
449 source->uv_crop_height, in_bd, shift);
450
Yaowu Xuf883b422016-08-30 14:01:10 -0700451 c = aom_highbd_ssim2(source->v_buffer, dest->v_buffer, source->uv_stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700452 dest->uv_stride, source->uv_crop_width,
453 source->uv_crop_height, in_bd, shift);
454
455 ssimv = a * .8 + .1 * (b + c);
456
457 *weight = 1;
458
459 return ssimv;
460}
461
Yaowu Xuf883b422016-08-30 14:01:10 -0700462#endif // CONFIG_AOM_HIGHBITDEPTH