Coverage Report

Created: 2025-11-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vpx_dsp/psnr.c
Line
Count
Source
1
/*
2
 *  Copyright (c) 2016 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
11
#include <math.h>
12
#include <assert.h>
13
#include "./vpx_dsp_rtcd.h"
14
#include "vpx_dsp/psnr.h"
15
#include "vpx_scale/yv12config.h"
16
17
0
double vpx_sse_to_psnr(double samples, double peak, double sse) {
18
0
  if (sse > 0.0) {
19
0
    const double psnr = 10.0 * log10(samples * peak * peak / sse);
20
0
    return psnr > MAX_PSNR ? MAX_PSNR : psnr;
21
0
  } else {
22
0
    return MAX_PSNR;
23
0
  }
24
0
}
25
26
/* TODO(yaowu): The block_variance calls the unoptimized versions of variance()
27
 * and highbd_8_variance(). It should not.
28
 */
29
static int64_t encoder_sse(const uint8_t *a, int a_stride, const uint8_t *b,
30
234k
                           int b_stride, int w, int h) {
31
234k
  int i, j;
32
234k
  int64_t sse = 0;
33
34
8.75M
  for (i = 0; i < h; i++) {
35
161M
    for (j = 0; j < w; j++) {
36
153M
      const int diff = a[j] - b[j];
37
153M
      sse += diff * diff;
38
153M
    }
39
40
8.52M
    a += a_stride;
41
8.52M
    b += b_stride;
42
8.52M
  }
43
44
234k
  return sse;
45
234k
}
46
47
#if CONFIG_VP9_HIGHBITDEPTH
48
static int64_t encoder_highbd_sse(const uint8_t *a8, int a_stride,
49
                                  const uint8_t *b8, int b_stride, int w,
50
0
                                  int h) {
51
0
  int i, j;
52
0
  int64_t sse = 0;
53
54
0
  const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
55
0
  const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
56
57
0
  for (i = 0; i < h; i++) {
58
0
    for (j = 0; j < w; j++) {
59
0
      const int diff = a[j] - b[j];
60
0
      sse += diff * diff;
61
0
    }
62
0
    a += a_stride;
63
0
    b += b_stride;
64
0
  }
65
66
0
  return sse;
67
0
}
68
#endif  // CONFIG_VP9_HIGHBITDEPTH
69
70
static int64_t get_sse(const uint8_t *a, int a_stride, const uint8_t *b,
71
309k
                       int b_stride, int width, int height) {
72
309k
  const int dw = width % 16;
73
309k
  const int dh = height % 16;
74
309k
  int64_t total_sse = 0;
75
309k
  int x, y;
76
77
309k
  if (dw > 0) {
78
112k
    total_sse += encoder_sse(&a[width - dw], a_stride, &b[width - dw], b_stride,
79
112k
                             dw, height);
80
112k
  }
81
82
309k
  if (dh > 0) {
83
122k
    total_sse +=
84
122k
        encoder_sse(&a[(height - dh) * a_stride], a_stride,
85
122k
                    &b[(height - dh) * b_stride], b_stride, width - dw, dh);
86
122k
  }
87
88
1.60M
  for (y = 0; y < height / 16; ++y) {
89
1.29M
    const uint8_t *pa = a;
90
1.29M
    const uint8_t *pb = b;
91
7.89M
    for (x = 0; x < width / 16; ++x) {
92
6.60M
      total_sse += vpx_sse(pa, a_stride, pb, b_stride, 16, 16);
93
94
6.60M
      pa += 16;
95
6.60M
      pb += 16;
96
6.60M
    }
97
98
1.29M
    a += 16 * a_stride;
99
1.29M
    b += 16 * b_stride;
100
1.29M
  }
101
102
309k
  return total_sse;
103
309k
}
104
105
#if CONFIG_VP9_HIGHBITDEPTH
106
static int64_t highbd_get_sse_shift(const uint8_t *a8, int a_stride,
107
                                    const uint8_t *b8, int b_stride, int width,
108
0
                                    int height, unsigned int input_shift) {
109
0
  const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
110
0
  const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
111
0
  int64_t total_sse = 0;
112
0
  int x, y;
113
0
  for (y = 0; y < height; ++y) {
114
0
    for (x = 0; x < width; ++x) {
115
0
      int64_t diff;
116
0
      diff = (a[x] >> input_shift) - (b[x] >> input_shift);
117
0
      total_sse += diff * diff;
118
0
    }
119
0
    a += a_stride;
120
0
    b += b_stride;
121
0
  }
122
0
  return total_sse;
123
0
}
124
125
static int64_t highbd_get_sse(const uint8_t *a, int a_stride, const uint8_t *b,
126
0
                              int b_stride, int width, int height) {
127
0
  int64_t total_sse = 0;
128
0
  int x, y;
129
0
  const int dw = width % 16;
130
0
  const int dh = height % 16;
131
0
  if (dw > 0) {
132
0
    total_sse += encoder_highbd_sse(&a[width - dw], a_stride, &b[width - dw],
133
0
                                    b_stride, dw, height);
134
0
  }
135
0
  if (dh > 0) {
136
0
    total_sse += encoder_highbd_sse(&a[(height - dh) * a_stride], a_stride,
137
0
                                    &b[(height - dh) * b_stride], b_stride,
138
0
                                    width - dw, dh);
139
0
  }
140
0
  for (y = 0; y < height / 16; ++y) {
141
0
    const uint8_t *pa = a;
142
0
    const uint8_t *pb = b;
143
0
    for (x = 0; x < width / 16; ++x) {
144
0
      total_sse += vpx_highbd_sse(pa, a_stride, pb, b_stride, 16, 16);
145
0
      pa += 16;
146
0
      pb += 16;
147
0
    }
148
0
    a += 16 * a_stride;
149
0
    b += 16 * b_stride;
150
0
  }
151
0
  return total_sse;
152
0
}
153
#endif  // CONFIG_VP9_HIGHBITDEPTH
154
155
int64_t vpx_get_y_sse(const YV12_BUFFER_CONFIG *a,
156
309k
                      const YV12_BUFFER_CONFIG *b) {
157
309k
  assert(a->y_crop_width == b->y_crop_width);
158
309k
  assert(a->y_crop_height == b->y_crop_height);
159
160
309k
  return get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
161
309k
                 a->y_crop_width, a->y_crop_height);
162
309k
}
163
164
#if CONFIG_VP9_HIGHBITDEPTH
165
int64_t vpx_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
166
0
                             const YV12_BUFFER_CONFIG *b) {
167
0
  assert(a->y_crop_width == b->y_crop_width);
168
0
  assert(a->y_crop_height == b->y_crop_height);
169
0
  assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
170
0
  assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
171
172
0
  return highbd_get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
173
0
                        a->y_crop_width, a->y_crop_height);
174
0
}
175
#endif  // CONFIG_VP9_HIGHBITDEPTH
176
177
#if CONFIG_VP9_HIGHBITDEPTH
178
void vpx_calc_highbd_psnr(const YV12_BUFFER_CONFIG *a,
179
                          const YV12_BUFFER_CONFIG *b, PSNR_STATS *psnr,
180
                          uint32_t bit_depth, uint32_t in_bit_depth,
181
0
                          int spatial_layer_id) {
182
0
  const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
183
0
  const int heights[3] = { a->y_crop_height, a->uv_crop_height,
184
0
                           a->uv_crop_height };
185
0
  const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer };
186
0
  const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
187
0
  const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer };
188
0
  const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
189
0
  int i;
190
0
  uint64_t total_sse = 0;
191
0
  uint32_t total_samples = 0;
192
0
  const double peak = (double)((1 << in_bit_depth) - 1);
193
0
  const unsigned int input_shift = bit_depth - in_bit_depth;
194
195
0
  for (i = 0; i < 3; ++i) {
196
0
    const int w = widths[i];
197
0
    const int h = heights[i];
198
0
    const uint32_t samples = w * h;
199
0
    uint64_t sse;
200
0
    if (a->flags & YV12_FLAG_HIGHBITDEPTH) {
201
0
      if (input_shift) {
202
0
        sse = highbd_get_sse_shift(a_planes[i], a_strides[i], b_planes[i],
203
0
                                   b_strides[i], w, h, input_shift);
204
0
      } else {
205
0
        sse = highbd_get_sse(a_planes[i], a_strides[i], b_planes[i],
206
0
                             b_strides[i], w, h);
207
0
      }
208
0
    } else {
209
0
      sse = get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h);
210
0
    }
211
0
    psnr->sse[1 + i] = sse;
212
0
    psnr->samples[1 + i] = samples;
213
0
    psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
214
215
0
    total_sse += sse;
216
0
    total_samples += samples;
217
0
  }
218
219
0
  psnr->sse[0] = total_sse;
220
0
  psnr->samples[0] = total_samples;
221
0
  psnr->psnr[0] =
222
0
      vpx_sse_to_psnr((double)total_samples, peak, (double)total_sse);
223
0
  psnr->spatial_layer_id = spatial_layer_id;
224
0
}
225
226
#endif  // !CONFIG_VP9_HIGHBITDEPTH
227
228
void vpx_calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
229
0
                   PSNR_STATS *psnr, int spatial_layer_id) {
230
0
  static const double peak = 255.0;
231
0
  const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
232
0
  const int heights[3] = { a->y_crop_height, a->uv_crop_height,
233
0
                           a->uv_crop_height };
234
0
  const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer };
235
0
  const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
236
0
  const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer };
237
0
  const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
238
0
  int i;
239
0
  uint64_t total_sse = 0;
240
0
  uint32_t total_samples = 0;
241
242
0
  for (i = 0; i < 3; ++i) {
243
0
    const int w = widths[i];
244
0
    const int h = heights[i];
245
0
    const uint32_t samples = w * h;
246
0
    const uint64_t sse =
247
0
        get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h);
248
0
    psnr->sse[1 + i] = sse;
249
0
    psnr->samples[1 + i] = samples;
250
0
    psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
251
252
0
    total_sse += sse;
253
0
    total_samples += samples;
254
0
  }
255
256
0
  psnr->sse[0] = total_sse;
257
0
  psnr->samples[0] = total_samples;
258
0
  psnr->psnr[0] =
259
0
      vpx_sse_to_psnr((double)total_samples, peak, (double)total_sse);
260
0
  psnr->spatial_layer_id = spatial_layer_id;
261
0
}