Coverage Report

Created: 2025-06-22 07:10

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