• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  *
10  *  This code was originally written by: Nathan E. Egge, at the Daala
11  *  project.
12  */
13 #include <assert.h>
14 #include <math.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "./vpx_config.h"
18 #include "./vpx_dsp_rtcd.h"
19 #include "vpx_dsp/ssim.h"
20 #include "vpx_ports/system_state.h"
21 
22 typedef struct fs_level fs_level;
23 typedef struct fs_ctx fs_ctx;
24 
25 #define SSIM_C1 (255 * 255 * 0.01 * 0.01)
26 #define SSIM_C2 (255 * 255 * 0.03 * 0.03)
27 #if CONFIG_VP9_HIGHBITDEPTH
28 #define SSIM_C1_10 (1023 * 1023 * 0.01 * 0.01)
29 #define SSIM_C1_12 (4095 * 4095 * 0.01 * 0.01)
30 #define SSIM_C2_10 (1023 * 1023 * 0.03 * 0.03)
31 #define SSIM_C2_12 (4095 * 4095 * 0.03 * 0.03)
32 #endif
33 #define FS_MINI(_a, _b) ((_a) < (_b) ? (_a) : (_b))
34 #define FS_MAXI(_a, _b) ((_a) > (_b) ? (_a) : (_b))
35 
36 struct fs_level {
37   uint32_t *im1;
38   uint32_t *im2;
39   double *ssim;
40   int w;
41   int h;
42 };
43 
44 struct fs_ctx {
45   fs_level *level;
46   int nlevels;
47   unsigned *col_buf;
48 };
49 
fs_ctx_init(fs_ctx * _ctx,int _w,int _h,int _nlevels)50 static int fs_ctx_init(fs_ctx *_ctx, int _w, int _h, int _nlevels) {
51   unsigned char *data;
52   size_t data_size;
53   int lw;
54   int lh;
55   int l;
56   lw = (_w + 1) >> 1;
57   lh = (_h + 1) >> 1;
58   data_size =
59       _nlevels * sizeof(fs_level) + 2 * (lw + 8) * 8 * sizeof(*_ctx->col_buf);
60   for (l = 0; l < _nlevels; l++) {
61     size_t im_size;
62     size_t level_size;
63     im_size = lw * (size_t)lh;
64     level_size = 2 * im_size * sizeof(*_ctx->level[l].im1);
65     level_size += sizeof(*_ctx->level[l].ssim) - 1;
66     level_size /= sizeof(*_ctx->level[l].ssim);
67     level_size += im_size;
68     level_size *= sizeof(*_ctx->level[l].ssim);
69     data_size += level_size;
70     lw = (lw + 1) >> 1;
71     lh = (lh + 1) >> 1;
72   }
73   data = (unsigned char *)malloc(data_size);
74   if (!data) return -1;
75   _ctx->level = (fs_level *)data;
76   _ctx->nlevels = _nlevels;
77   data += _nlevels * sizeof(*_ctx->level);
78   lw = (_w + 1) >> 1;
79   lh = (_h + 1) >> 1;
80   for (l = 0; l < _nlevels; l++) {
81     size_t im_size;
82     size_t level_size;
83     _ctx->level[l].w = lw;
84     _ctx->level[l].h = lh;
85     im_size = lw * (size_t)lh;
86     level_size = 2 * im_size * sizeof(*_ctx->level[l].im1);
87     level_size += sizeof(*_ctx->level[l].ssim) - 1;
88     level_size /= sizeof(*_ctx->level[l].ssim);
89     level_size *= sizeof(*_ctx->level[l].ssim);
90     _ctx->level[l].im1 = (uint32_t *)data;
91     _ctx->level[l].im2 = _ctx->level[l].im1 + im_size;
92     data += level_size;
93     _ctx->level[l].ssim = (double *)data;
94     data += im_size * sizeof(*_ctx->level[l].ssim);
95     lw = (lw + 1) >> 1;
96     lh = (lh + 1) >> 1;
97   }
98   _ctx->col_buf = (unsigned *)data;
99   return 0;
100 }
101 
fs_ctx_clear(fs_ctx * _ctx)102 static void fs_ctx_clear(fs_ctx *_ctx) { free(_ctx->level); }
103 
fs_downsample_level(fs_ctx * _ctx,int _l)104 static void fs_downsample_level(fs_ctx *_ctx, int _l) {
105   const uint32_t *src1;
106   const uint32_t *src2;
107   uint32_t *dst1;
108   uint32_t *dst2;
109   int w2;
110   int h2;
111   int w;
112   int h;
113   int i;
114   int j;
115   w = _ctx->level[_l].w;
116   h = _ctx->level[_l].h;
117   dst1 = _ctx->level[_l].im1;
118   dst2 = _ctx->level[_l].im2;
119   w2 = _ctx->level[_l - 1].w;
120   h2 = _ctx->level[_l - 1].h;
121   src1 = _ctx->level[_l - 1].im1;
122   src2 = _ctx->level[_l - 1].im2;
123   for (j = 0; j < h; j++) {
124     int j0offs;
125     int j1offs;
126     j0offs = 2 * j * w2;
127     j1offs = FS_MINI(2 * j + 1, h2) * w2;
128     for (i = 0; i < w; i++) {
129       int i0;
130       int i1;
131       i0 = 2 * i;
132       i1 = FS_MINI(i0 + 1, w2);
133       dst1[j * w + i] =
134           (uint32_t)((int64_t)src1[j0offs + i0] + src1[j0offs + i1] +
135                      src1[j1offs + i0] + src1[j1offs + i1]);
136       dst2[j * w + i] =
137           (uint32_t)((int64_t)src2[j0offs + i0] + src2[j0offs + i1] +
138                      src2[j1offs + i0] + src2[j1offs + i1]);
139     }
140   }
141 }
142 
fs_downsample_level0(fs_ctx * _ctx,const uint8_t * _src1,int _s1ystride,const uint8_t * _src2,int _s2ystride,int _w,int _h,uint32_t bd,uint32_t shift)143 static void fs_downsample_level0(fs_ctx *_ctx, const uint8_t *_src1,
144                                  int _s1ystride, const uint8_t *_src2,
145                                  int _s2ystride, int _w, int _h, uint32_t bd,
146                                  uint32_t shift) {
147   uint32_t *dst1;
148   uint32_t *dst2;
149   int w;
150   int h;
151   int i;
152   int j;
153   w = _ctx->level[0].w;
154   h = _ctx->level[0].h;
155   dst1 = _ctx->level[0].im1;
156   dst2 = _ctx->level[0].im2;
157   for (j = 0; j < h; j++) {
158     int j0;
159     int j1;
160     j0 = 2 * j;
161     j1 = FS_MINI(j0 + 1, _h);
162     for (i = 0; i < w; i++) {
163       int i0;
164       int i1;
165       i0 = 2 * i;
166       i1 = FS_MINI(i0 + 1, _w);
167       if (bd == 8 && shift == 0) {
168         dst1[j * w + i] =
169             _src1[j0 * _s1ystride + i0] + _src1[j0 * _s1ystride + i1] +
170             _src1[j1 * _s1ystride + i0] + _src1[j1 * _s1ystride + i1];
171         dst2[j * w + i] =
172             _src2[j0 * _s2ystride + i0] + _src2[j0 * _s2ystride + i1] +
173             _src2[j1 * _s2ystride + i0] + _src2[j1 * _s2ystride + i1];
174       } else {
175         uint16_t *src1s = CONVERT_TO_SHORTPTR(_src1);
176         uint16_t *src2s = CONVERT_TO_SHORTPTR(_src2);
177         dst1[j * w + i] = (src1s[j0 * _s1ystride + i0] >> shift) +
178                           (src1s[j0 * _s1ystride + i1] >> shift) +
179                           (src1s[j1 * _s1ystride + i0] >> shift) +
180                           (src1s[j1 * _s1ystride + i1] >> shift);
181         dst2[j * w + i] = (src2s[j0 * _s2ystride + i0] >> shift) +
182                           (src2s[j0 * _s2ystride + i1] >> shift) +
183                           (src2s[j1 * _s2ystride + i0] >> shift) +
184                           (src2s[j1 * _s2ystride + i1] >> shift);
185       }
186     }
187   }
188 }
189 
fs_apply_luminance(fs_ctx * _ctx,int _l,int bit_depth)190 static void fs_apply_luminance(fs_ctx *_ctx, int _l, int bit_depth) {
191   unsigned *col_sums_x;
192   unsigned *col_sums_y;
193   uint32_t *im1;
194   uint32_t *im2;
195   double *ssim;
196   double c1;
197   int w;
198   int h;
199   int j0offs;
200   int j1offs;
201   int i;
202   int j;
203   double ssim_c1 = SSIM_C1;
204 #if CONFIG_VP9_HIGHBITDEPTH
205   if (bit_depth == 10) ssim_c1 = SSIM_C1_10;
206   if (bit_depth == 12) ssim_c1 = SSIM_C1_12;
207 #else
208   assert(bit_depth == 8);
209   (void)bit_depth;
210 #endif
211   w = _ctx->level[_l].w;
212   h = _ctx->level[_l].h;
213   col_sums_x = _ctx->col_buf;
214   col_sums_y = col_sums_x + w;
215   im1 = _ctx->level[_l].im1;
216   im2 = _ctx->level[_l].im2;
217   for (i = 0; i < w; i++) col_sums_x[i] = 5 * im1[i];
218   for (i = 0; i < w; i++) col_sums_y[i] = 5 * im2[i];
219   for (j = 1; j < 4; j++) {
220     j1offs = FS_MINI(j, h - 1) * w;
221     for (i = 0; i < w; i++) col_sums_x[i] += im1[j1offs + i];
222     for (i = 0; i < w; i++) col_sums_y[i] += im2[j1offs + i];
223   }
224   ssim = _ctx->level[_l].ssim;
225   c1 = (double)(ssim_c1 * 4096 * (1 << 4 * _l));
226   for (j = 0; j < h; j++) {
227     int64_t mux;
228     int64_t muy;
229     int i0;
230     int i1;
231     mux = (int64_t)5 * col_sums_x[0];
232     muy = (int64_t)5 * col_sums_y[0];
233     for (i = 1; i < 4; i++) {
234       i1 = FS_MINI(i, w - 1);
235       mux += col_sums_x[i1];
236       muy += col_sums_y[i1];
237     }
238     for (i = 0; i < w; i++) {
239       ssim[j * w + i] *= (2 * mux * (double)muy + c1) /
240                          (mux * (double)mux + muy * (double)muy + c1);
241       if (i + 1 < w) {
242         i0 = FS_MAXI(0, i - 4);
243         i1 = FS_MINI(i + 4, w - 1);
244         mux += (int)col_sums_x[i1] - (int)col_sums_x[i0];
245         muy += (int)col_sums_x[i1] - (int)col_sums_x[i0];
246       }
247     }
248     if (j + 1 < h) {
249       j0offs = FS_MAXI(0, j - 4) * w;
250       for (i = 0; i < w; i++) col_sums_x[i] -= im1[j0offs + i];
251       for (i = 0; i < w; i++) col_sums_y[i] -= im2[j0offs + i];
252       j1offs = FS_MINI(j + 4, h - 1) * w;
253       for (i = 0; i < w; i++)
254         col_sums_x[i] = (uint32_t)((int64_t)col_sums_x[i] + im1[j1offs + i]);
255       for (i = 0; i < w; i++)
256         col_sums_y[i] = (uint32_t)((int64_t)col_sums_y[i] + im2[j1offs + i]);
257     }
258   }
259 }
260 
261 #define FS_COL_SET(_col, _joffs, _ioffs)                       \
262   do {                                                         \
263     unsigned gx;                                               \
264     unsigned gy;                                               \
265     gx = gx_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \
266     gy = gy_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \
267     col_sums_gx2[(_col)] = gx * (double)gx;                    \
268     col_sums_gy2[(_col)] = gy * (double)gy;                    \
269     col_sums_gxgy[(_col)] = gx * (double)gy;                   \
270   } while (0)
271 
272 #define FS_COL_ADD(_col, _joffs, _ioffs)                       \
273   do {                                                         \
274     unsigned gx;                                               \
275     unsigned gy;                                               \
276     gx = gx_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \
277     gy = gy_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \
278     col_sums_gx2[(_col)] += gx * (double)gx;                   \
279     col_sums_gy2[(_col)] += gy * (double)gy;                   \
280     col_sums_gxgy[(_col)] += gx * (double)gy;                  \
281   } while (0)
282 
283 #define FS_COL_SUB(_col, _joffs, _ioffs)                       \
284   do {                                                         \
285     unsigned gx;                                               \
286     unsigned gy;                                               \
287     gx = gx_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \
288     gy = gy_buf[((j + (_joffs)) & 7) * stride + i + (_ioffs)]; \
289     col_sums_gx2[(_col)] -= gx * (double)gx;                   \
290     col_sums_gy2[(_col)] -= gy * (double)gy;                   \
291     col_sums_gxgy[(_col)] -= gx * (double)gy;                  \
292   } while (0)
293 
294 #define FS_COL_COPY(_col1, _col2)                    \
295   do {                                               \
296     col_sums_gx2[(_col1)] = col_sums_gx2[(_col2)];   \
297     col_sums_gy2[(_col1)] = col_sums_gy2[(_col2)];   \
298     col_sums_gxgy[(_col1)] = col_sums_gxgy[(_col2)]; \
299   } while (0)
300 
301 #define FS_COL_HALVE(_col1, _col2)                         \
302   do {                                                     \
303     col_sums_gx2[(_col1)] = col_sums_gx2[(_col2)] * 0.5;   \
304     col_sums_gy2[(_col1)] = col_sums_gy2[(_col2)] * 0.5;   \
305     col_sums_gxgy[(_col1)] = col_sums_gxgy[(_col2)] * 0.5; \
306   } while (0)
307 
308 #define FS_COL_DOUBLE(_col1, _col2)                      \
309   do {                                                   \
310     col_sums_gx2[(_col1)] = col_sums_gx2[(_col2)] * 2;   \
311     col_sums_gy2[(_col1)] = col_sums_gy2[(_col2)] * 2;   \
312     col_sums_gxgy[(_col1)] = col_sums_gxgy[(_col2)] * 2; \
313   } while (0)
314 
fs_calc_structure(fs_ctx * _ctx,int _l,int bit_depth)315 static void fs_calc_structure(fs_ctx *_ctx, int _l, int bit_depth) {
316   uint32_t *im1;
317   uint32_t *im2;
318   unsigned *gx_buf;
319   unsigned *gy_buf;
320   double *ssim;
321   double col_sums_gx2[8];
322   double col_sums_gy2[8];
323   double col_sums_gxgy[8];
324   double c2;
325   int stride;
326   int w;
327   int h;
328   int i;
329   int j;
330   double ssim_c2 = SSIM_C2;
331 #if CONFIG_VP9_HIGHBITDEPTH
332   if (bit_depth == 10) ssim_c2 = SSIM_C2_10;
333   if (bit_depth == 12) ssim_c2 = SSIM_C2_12;
334 #else
335   assert(bit_depth == 8);
336   (void)bit_depth;
337 #endif
338 
339   w = _ctx->level[_l].w;
340   h = _ctx->level[_l].h;
341   im1 = _ctx->level[_l].im1;
342   im2 = _ctx->level[_l].im2;
343   ssim = _ctx->level[_l].ssim;
344   gx_buf = _ctx->col_buf;
345   stride = w + 8;
346   gy_buf = gx_buf + 8 * stride;
347   memset(gx_buf, 0, 2 * 8 * stride * sizeof(*gx_buf));
348   c2 = ssim_c2 * (1 << 4 * _l) * 16 * 104;
349   for (j = 0; j < h + 4; j++) {
350     if (j < h - 1) {
351       for (i = 0; i < w - 1; i++) {
352         int64_t g1;
353         int64_t g2;
354         int64_t gx;
355         int64_t gy;
356         g1 = labs((int64_t)im1[(j + 1) * w + i + 1] - (int64_t)im1[j * w + i]);
357         g2 = labs((int64_t)im1[(j + 1) * w + i] - (int64_t)im1[j * w + i + 1]);
358         gx = 4 * FS_MAXI(g1, g2) + FS_MINI(g1, g2);
359         g1 = labs((int64_t)im2[(j + 1) * w + i + 1] - (int64_t)im2[j * w + i]);
360         g2 = labs((int64_t)im2[(j + 1) * w + i] - (int64_t)im2[j * w + i + 1]);
361         gy = ((int64_t)4 * FS_MAXI(g1, g2) + FS_MINI(g1, g2));
362         gx_buf[(j & 7) * stride + i + 4] = (uint32_t)gx;
363         gy_buf[(j & 7) * stride + i + 4] = (uint32_t)gy;
364       }
365     } else {
366       memset(gx_buf + (j & 7) * stride, 0, stride * sizeof(*gx_buf));
367       memset(gy_buf + (j & 7) * stride, 0, stride * sizeof(*gy_buf));
368     }
369     if (j >= 4) {
370       int k;
371       col_sums_gx2[3] = col_sums_gx2[2] = col_sums_gx2[1] = col_sums_gx2[0] = 0;
372       col_sums_gy2[3] = col_sums_gy2[2] = col_sums_gy2[1] = col_sums_gy2[0] = 0;
373       col_sums_gxgy[3] = col_sums_gxgy[2] = col_sums_gxgy[1] =
374           col_sums_gxgy[0] = 0;
375       for (i = 4; i < 8; i++) {
376         FS_COL_SET(i, -1, 0);
377         FS_COL_ADD(i, 0, 0);
378         for (k = 1; k < 8 - i; k++) {
379           FS_COL_DOUBLE(i, i);
380           FS_COL_ADD(i, -k - 1, 0);
381           FS_COL_ADD(i, k, 0);
382         }
383       }
384       for (i = 0; i < w; i++) {
385         double mugx2;
386         double mugy2;
387         double mugxgy;
388         mugx2 = col_sums_gx2[0];
389         for (k = 1; k < 8; k++) mugx2 += col_sums_gx2[k];
390         mugy2 = col_sums_gy2[0];
391         for (k = 1; k < 8; k++) mugy2 += col_sums_gy2[k];
392         mugxgy = col_sums_gxgy[0];
393         for (k = 1; k < 8; k++) mugxgy += col_sums_gxgy[k];
394         ssim[(j - 4) * w + i] = (2 * mugxgy + c2) / (mugx2 + mugy2 + c2);
395         if (i + 1 < w) {
396           FS_COL_SET(0, -1, 1);
397           FS_COL_ADD(0, 0, 1);
398           FS_COL_SUB(2, -3, 2);
399           FS_COL_SUB(2, 2, 2);
400           FS_COL_HALVE(1, 2);
401           FS_COL_SUB(3, -4, 3);
402           FS_COL_SUB(3, 3, 3);
403           FS_COL_HALVE(2, 3);
404           FS_COL_COPY(3, 4);
405           FS_COL_DOUBLE(4, 5);
406           FS_COL_ADD(4, -4, 5);
407           FS_COL_ADD(4, 3, 5);
408           FS_COL_DOUBLE(5, 6);
409           FS_COL_ADD(5, -3, 6);
410           FS_COL_ADD(5, 2, 6);
411           FS_COL_DOUBLE(6, 7);
412           FS_COL_ADD(6, -2, 7);
413           FS_COL_ADD(6, 1, 7);
414           FS_COL_SET(7, -1, 8);
415           FS_COL_ADD(7, 0, 8);
416         }
417       }
418     }
419   }
420 }
421 
422 #define FS_NLEVELS (4)
423 
424 /*These weights were derived from the default weights found in Wang's original
425  Matlab implementation: {0.0448, 0.2856, 0.2363, 0.1333}.
426  We drop the finest scale and renormalize the rest to sum to 1.*/
427 
428 static const double FS_WEIGHTS[FS_NLEVELS] = {
429   0.2989654541015625, 0.3141326904296875, 0.2473602294921875, 0.1395416259765625
430 };
431 
fs_average(fs_ctx * _ctx,int _l)432 static double fs_average(fs_ctx *_ctx, int _l) {
433   double *ssim;
434   double ret;
435   int w;
436   int h;
437   int i;
438   int j;
439   w = _ctx->level[_l].w;
440   h = _ctx->level[_l].h;
441   ssim = _ctx->level[_l].ssim;
442   ret = 0;
443   for (j = 0; j < h; j++)
444     for (i = 0; i < w; i++) ret += ssim[j * w + i];
445   return pow(ret / (w * h), FS_WEIGHTS[_l]);
446 }
447 
convert_ssim_db(double _ssim,double _weight)448 static double convert_ssim_db(double _ssim, double _weight) {
449   assert(_weight >= _ssim);
450   if ((_weight - _ssim) < 1e-10) return MAX_SSIM_DB;
451   return 10 * (log10(_weight) - log10(_weight - _ssim));
452 }
453 
calc_ssim(const uint8_t * _src,int _systride,const uint8_t * _dst,int _dystride,int _w,int _h,uint32_t _bd,uint32_t _shift)454 static double calc_ssim(const uint8_t *_src, int _systride, const uint8_t *_dst,
455                         int _dystride, int _w, int _h, uint32_t _bd,
456                         uint32_t _shift) {
457   fs_ctx ctx;
458   double ret;
459   int l;
460   ret = 1;
461   if (fs_ctx_init(&ctx, _w, _h, FS_NLEVELS)) return 99.0;
462   fs_downsample_level0(&ctx, _src, _systride, _dst, _dystride, _w, _h, _bd,
463                        _shift);
464   for (l = 0; l < FS_NLEVELS - 1; l++) {
465     fs_calc_structure(&ctx, l, _bd);
466     ret *= fs_average(&ctx, l);
467     fs_downsample_level(&ctx, l + 1);
468   }
469   fs_calc_structure(&ctx, l, _bd);
470   fs_apply_luminance(&ctx, l, _bd);
471   ret *= fs_average(&ctx, l);
472   fs_ctx_clear(&ctx);
473   return ret;
474 }
475 
vpx_calc_fastssim(const YV12_BUFFER_CONFIG * source,const YV12_BUFFER_CONFIG * dest,double * ssim_y,double * ssim_u,double * ssim_v,uint32_t bd,uint32_t in_bd)476 double vpx_calc_fastssim(const YV12_BUFFER_CONFIG *source,
477                          const YV12_BUFFER_CONFIG *dest, double *ssim_y,
478                          double *ssim_u, double *ssim_v, uint32_t bd,
479                          uint32_t in_bd) {
480   double ssimv;
481   uint32_t bd_shift = 0;
482   vpx_clear_system_state();
483   assert(bd >= in_bd);
484   bd_shift = bd - in_bd;
485 
486   *ssim_y = calc_ssim(source->y_buffer, source->y_stride, dest->y_buffer,
487                       dest->y_stride, source->y_crop_width,
488                       source->y_crop_height, in_bd, bd_shift);
489   *ssim_u = calc_ssim(source->u_buffer, source->uv_stride, dest->u_buffer,
490                       dest->uv_stride, source->uv_crop_width,
491                       source->uv_crop_height, in_bd, bd_shift);
492   *ssim_v = calc_ssim(source->v_buffer, source->uv_stride, dest->v_buffer,
493                       dest->uv_stride, source->uv_crop_width,
494                       source->uv_crop_height, in_bd, bd_shift);
495 
496   ssimv = (*ssim_y) * .8 + .1 * ((*ssim_u) + (*ssim_v));
497   return convert_ssim_db(ssimv, 1.0);
498 }
499