Coverage Report

Created: 2024-07-27 06:27

/src/libwebp/src/dsp/ssim.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2017 Google Inc. All Rights Reserved.
2
//
3
// Use of this source code is governed by a BSD-style license
4
// that can be found in the COPYING file in the root of the source
5
// tree. An additional intellectual property rights grant can be found
6
// in the file PATENTS. All contributing project authors may
7
// be found in the AUTHORS file in the root of the source tree.
8
// -----------------------------------------------------------------------------
9
//
10
// distortion calculation
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
14
#include <assert.h>
15
#include <stdlib.h>  // for abs()
16
17
#include "src/dsp/dsp.h"
18
19
#if !defined(WEBP_REDUCE_SIZE)
20
21
//------------------------------------------------------------------------------
22
// SSIM / PSNR
23
24
// hat-shaped filter. Sum of coefficients is equal to 16.
25
static const uint32_t kWeight[2 * VP8_SSIM_KERNEL + 1] = {
26
  1, 2, 3, 4, 3, 2, 1
27
};
28
static const uint32_t kWeightSum = 16 * 16;   // sum{kWeight}^2
29
30
static WEBP_INLINE double SSIMCalculation(
31
0
    const VP8DistoStats* const stats, uint32_t N  /*num samples*/) {
32
0
  const uint32_t w2 =  N * N;
33
0
  const uint32_t C1 = 20 * w2;
34
0
  const uint32_t C2 = 60 * w2;
35
0
  const uint32_t C3 = 8 * 8 * w2;   // 'dark' limit ~= 6
36
0
  const uint64_t xmxm = (uint64_t)stats->xm * stats->xm;
37
0
  const uint64_t ymym = (uint64_t)stats->ym * stats->ym;
38
0
  if (xmxm + ymym >= C3) {
39
0
    const int64_t xmym = (int64_t)stats->xm * stats->ym;
40
0
    const int64_t sxy = (int64_t)stats->xym * N - xmym;    // can be negative
41
0
    const uint64_t sxx = (uint64_t)stats->xxm * N - xmxm;
42
0
    const uint64_t syy = (uint64_t)stats->yym * N - ymym;
43
    // we descale by 8 to prevent overflow during the fnum/fden multiply.
44
0
    const uint64_t num_S = (2 * (uint64_t)(sxy < 0 ? 0 : sxy) + C2) >> 8;
45
0
    const uint64_t den_S = (sxx + syy + C2) >> 8;
46
0
    const uint64_t fnum = (2 * xmym + C1) * num_S;
47
0
    const uint64_t fden = (xmxm + ymym + C1) * den_S;
48
0
    const double r = (double)fnum / fden;
49
0
    assert(r >= 0. && r <= 1.0);
50
0
    return r;
51
0
  }
52
0
  return 1.;   // area is too dark to contribute meaningfully
53
0
}
54
55
0
double VP8SSIMFromStats(const VP8DistoStats* const stats) {
56
0
  return SSIMCalculation(stats, kWeightSum);
57
0
}
58
59
0
double VP8SSIMFromStatsClipped(const VP8DistoStats* const stats) {
60
0
  return SSIMCalculation(stats, stats->w);
61
0
}
62
63
static double SSIMGetClipped_C(const uint8_t* src1, int stride1,
64
                               const uint8_t* src2, int stride2,
65
0
                               int xo, int yo, int W, int H) {
66
0
  VP8DistoStats stats = { 0, 0, 0, 0, 0, 0 };
67
0
  const int ymin = (yo - VP8_SSIM_KERNEL < 0) ? 0 : yo - VP8_SSIM_KERNEL;
68
0
  const int ymax = (yo + VP8_SSIM_KERNEL > H - 1) ? H - 1
69
0
                                                  : yo + VP8_SSIM_KERNEL;
70
0
  const int xmin = (xo - VP8_SSIM_KERNEL < 0) ? 0 : xo - VP8_SSIM_KERNEL;
71
0
  const int xmax = (xo + VP8_SSIM_KERNEL > W - 1) ? W - 1
72
0
                                                  : xo + VP8_SSIM_KERNEL;
73
0
  int x, y;
74
0
  src1 += ymin * stride1;
75
0
  src2 += ymin * stride2;
76
0
  for (y = ymin; y <= ymax; ++y, src1 += stride1, src2 += stride2) {
77
0
    for (x = xmin; x <= xmax; ++x) {
78
0
      const uint32_t w = kWeight[VP8_SSIM_KERNEL + x - xo]
79
0
                       * kWeight[VP8_SSIM_KERNEL + y - yo];
80
0
      const uint32_t s1 = src1[x];
81
0
      const uint32_t s2 = src2[x];
82
0
      stats.w   += w;
83
0
      stats.xm  += w * s1;
84
0
      stats.ym  += w * s2;
85
0
      stats.xxm += w * s1 * s1;
86
0
      stats.xym += w * s1 * s2;
87
0
      stats.yym += w * s2 * s2;
88
0
    }
89
0
  }
90
0
  return VP8SSIMFromStatsClipped(&stats);
91
0
}
92
93
static double SSIMGet_C(const uint8_t* src1, int stride1,
94
0
                        const uint8_t* src2, int stride2) {
95
0
  VP8DistoStats stats = { 0, 0, 0, 0, 0, 0 };
96
0
  int x, y;
97
0
  for (y = 0; y <= 2 * VP8_SSIM_KERNEL; ++y, src1 += stride1, src2 += stride2) {
98
0
    for (x = 0; x <= 2 * VP8_SSIM_KERNEL; ++x) {
99
0
      const uint32_t w = kWeight[x] * kWeight[y];
100
0
      const uint32_t s1 = src1[x];
101
0
      const uint32_t s2 = src2[x];
102
0
      stats.xm  += w * s1;
103
0
      stats.ym  += w * s2;
104
0
      stats.xxm += w * s1 * s1;
105
0
      stats.xym += w * s1 * s2;
106
0
      stats.yym += w * s2 * s2;
107
0
    }
108
0
  }
109
0
  return VP8SSIMFromStats(&stats);
110
0
}
111
112
#endif  // !defined(WEBP_REDUCE_SIZE)
113
114
//------------------------------------------------------------------------------
115
116
#if !defined(WEBP_DISABLE_STATS)
117
static uint32_t AccumulateSSE_C(const uint8_t* src1,
118
0
                                const uint8_t* src2, int len) {
119
0
  int i;
120
0
  uint32_t sse2 = 0;
121
0
  assert(len <= 65535);  // to ensure that accumulation fits within uint32_t
122
0
  for (i = 0; i < len; ++i) {
123
0
    const int32_t diff = src1[i] - src2[i];
124
0
    sse2 += diff * diff;
125
0
  }
126
0
  return sse2;
127
0
}
128
#endif
129
130
//------------------------------------------------------------------------------
131
132
#if !defined(WEBP_REDUCE_SIZE)
133
VP8SSIMGetFunc VP8SSIMGet;
134
VP8SSIMGetClippedFunc VP8SSIMGetClipped;
135
#endif
136
#if !defined(WEBP_DISABLE_STATS)
137
VP8AccumulateSSEFunc VP8AccumulateSSE;
138
#endif
139
140
extern VP8CPUInfo VP8GetCPUInfo;
141
extern void VP8SSIMDspInitSSE2(void);
142
143
0
WEBP_DSP_INIT_FUNC(VP8SSIMDspInit) {
144
0
#if !defined(WEBP_REDUCE_SIZE)
145
0
  VP8SSIMGetClipped = SSIMGetClipped_C;
146
0
  VP8SSIMGet = SSIMGet_C;
147
0
#endif
148
149
0
#if !defined(WEBP_DISABLE_STATS)
150
0
  VP8AccumulateSSE = AccumulateSSE_C;
151
0
#endif
152
153
0
  if (VP8GetCPUInfo != NULL) {
154
0
#if defined(WEBP_HAVE_SSE2)
155
0
    if (VP8GetCPUInfo(kSSE2)) {
156
0
      VP8SSIMDspInitSSE2();
157
0
    }
158
0
#endif
159
0
  }
160
0
}