Coverage Report

Created: 2026-08-31 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/svt-av1/Source/Lib/C_DEFAULT/variance.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
4
 * 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 https://www.aomedia.org/license/software-license. 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 https://www.aomedia.org/license/patent-license.
10
 */
11
12
#include <assert.h>
13
#include <stdlib.h>
14
#include <string.h>
15
16
#include "pcs.h"
17
#include "convolve.h"
18
#include "aom_dsp_rtcd.h"
19
#include "inter_prediction.h"
20
21
// Applies a 1-D 2-tap bilinear filter to the source block in either horizontal
22
// or vertical direction to produce the filtered output block. Used to implement
23
// the first-pass of 2-D separable filter.
24
//
25
// Produces int16_t output to retain precision for the next pass. Two filter
26
// taps should sum to FILTER_WEIGHT. pixel_step defines whether the filter is
27
// applied horizontally (pixel_step = 1) or vertically (pixel_step = stride).
28
// It defines the offset required to move from one input to the next.
29
static void aom_var_filter_block2d_bil_first_pass_c(const uint8_t* a, uint16_t* b, unsigned int src_pixels_per_line,
30
                                                    unsigned int pixel_step, unsigned int output_height,
31
0
                                                    unsigned int output_width, const uint8_t* filter) {
32
0
    unsigned int i, j;
33
34
0
    for (i = 0; i < output_height; ++i) {
35
0
        for (j = 0; j < output_width; ++j) {
36
0
            b[j] = ROUND_POWER_OF_TWO((int)a[0] * filter[0] + (int)a[pixel_step] * filter[1], FILTER_BITS);
37
38
0
            ++a;
39
0
        }
40
41
0
        a += src_pixels_per_line - output_width;
42
0
        b += output_width;
43
0
    }
44
0
}
45
46
// Applies a 1-D 2-tap bilinear filter to the source block in either horizontal
47
// or vertical direction to produce the filtered output block. Used to implement
48
// the second-pass of 2-D separable filter.
49
//
50
// Requires 16-bit input as produced by filter_block2d_bil_first_pass. Two
51
// filter taps should sum to FILTER_WEIGHT. pixel_step defines whether the
52
// filter is applied horizontally (pixel_step = 1) or vertically
53
// (pixel_step = stride). It defines the offset required to move from one input
54
// to the next. Output is 8-bit.
55
static void aom_var_filter_block2d_bil_second_pass_c(const uint16_t* a, uint8_t* b, unsigned int src_pixels_per_line,
56
                                                     unsigned int pixel_step, unsigned int output_height,
57
0
                                                     unsigned int output_width, const uint8_t* filter) {
58
0
    unsigned int i, j;
59
60
0
    for (i = 0; i < output_height; ++i) {
61
0
        for (j = 0; j < output_width; ++j) {
62
0
            b[j] = ROUND_POWER_OF_TWO((int)a[0] * filter[0] + (int)a[pixel_step] * filter[1], FILTER_BITS);
63
0
            ++a;
64
0
        }
65
66
0
        a += src_pixels_per_line - output_width;
67
0
        b += output_width;
68
0
    }
69
0
}
70
71
0
static INLINE const InterpFilterParams* av1_get_filter(int subpel_search) {
72
0
    assert(subpel_search >= USE_2_TAPS);
73
74
0
    switch (subpel_search) {
75
0
    case USE_2_TAPS:
76
0
        return &av1_interp_filter_params_list[BILINEAR];
77
0
    case USE_4_TAPS:
78
0
        return &av1_interp_4tap[EIGHTTAP_REGULAR];
79
0
    case USE_8_TAPS:
80
0
        return &av1_interp_filter_params_list[EIGHTTAP_REGULAR];
81
0
    default:
82
0
        assert(0);
83
0
        return NULL;
84
0
    }
85
0
}
86
87
// Get pred block from up-sampled reference.
88
void svt_aom_upsampled_pred_c(MacroBlockD* xd, const struct AV1Common* const cm, int mi_row, int mi_col,
89
                              uint8_t* comp_pred, int width, int height, int subpel_x_q3, int subpel_y_q3,
90
0
                              const uint8_t* ref, int ref_stride, int subpel_search) {
91
0
    (void)xd;
92
0
    (void)cm;
93
0
    (void)mi_row;
94
0
    (void)mi_col;
95
0
    const InterpFilterParams* filter = av1_get_filter(subpel_search);
96
0
    assert(filter != NULL);
97
0
    if (!subpel_x_q3 && !subpel_y_q3) {
98
0
        for (int i = 0; i < height; i++) {
99
0
            svt_memcpy(comp_pred, ref, width * sizeof(*comp_pred));
100
0
            comp_pred += width;
101
0
            ref += ref_stride;
102
0
        }
103
0
    } else if (!subpel_y_q3) {
104
0
        const int16_t* const kernel = av1_get_interp_filter_subpel_kernel(*filter, subpel_x_q3 << 1);
105
0
        svt_aom_convolve8_horiz_c(ref, ref_stride, comp_pred, width, kernel, 16, NULL, -1, width, height);
106
0
    } else if (!subpel_x_q3) {
107
0
        const int16_t* const kernel = av1_get_interp_filter_subpel_kernel(*filter, subpel_y_q3 << 1);
108
0
        svt_aom_convolve8_vert_c(ref, ref_stride, comp_pred, width, NULL, -1, kernel, 16, width, height);
109
0
    } else {
110
0
        DECLARE_ALIGNED(16, uint8_t, temp[((MAX_SB_SIZE * 2 + 16) + 16) * MAX_SB_SIZE]);
111
0
        const int16_t* const kernel_x            = av1_get_interp_filter_subpel_kernel(*filter, subpel_x_q3 << 1);
112
0
        const int16_t* const kernel_y            = av1_get_interp_filter_subpel_kernel(*filter, subpel_y_q3 << 1);
113
0
        const int            intermediate_height = (((height - 1) * 8 + subpel_y_q3) >> 3) + filter->taps;
114
0
        assert(intermediate_height <= (MAX_SB_SIZE * 2 + 16) + 16);
115
0
        svt_aom_convolve8_horiz_c(ref - ref_stride * ((filter->taps >> 1) - 1),
116
0
                                  ref_stride,
117
0
                                  temp,
118
0
                                  MAX_SB_SIZE,
119
0
                                  kernel_x,
120
0
                                  16,
121
0
                                  NULL,
122
0
                                  -1,
123
0
                                  width,
124
0
                                  intermediate_height);
125
0
        svt_aom_convolve8_vert_c(temp + MAX_SB_SIZE * ((filter->taps >> 1) - 1),
126
0
                                 MAX_SB_SIZE,
127
0
                                 comp_pred,
128
0
                                 width,
129
0
                                 NULL,
130
0
                                 -1,
131
0
                                 kernel_y,
132
0
                                 16,
133
0
                                 width,
134
0
                                 height);
135
0
    }
136
0
}
137
138
// functions are from deleted file, associated with this macro
139
// Moved from EbComputeVariance_C.c
140
static void variance_c(const uint8_t* a, int a_stride, const uint8_t* b, int b_stride, int w, int h, uint32_t* sse,
141
157k
                       int* sum) {
142
157k
    int i, j;
143
144
157k
    *sum = 0;
145
157k
    *sse = 0;
146
147
1.56M
    for (i = 0; i < h; ++i) {
148
21.9M
        for (j = 0; j < w; ++j) {
149
20.5M
            const int diff = a[j] - b[j];
150
20.5M
            *sum += diff;
151
20.5M
            *sse += diff * diff;
152
20.5M
        }
153
154
1.41M
        a += a_stride;
155
1.41M
        b += b_stride;
156
1.41M
    }
157
157k
}
158
159
// Moved from EbComputeVariance_C.c
160
// TODO: use or implement a simd version of this
161
uint32_t svt_aom_variance_highbd_c(const uint16_t* a, int a_stride, const uint16_t* b, int b_stride, int w, int h,
162
0
                                   uint32_t* sse) {
163
0
    int i, j;
164
165
0
    int sad = 0;
166
0
    *sse    = 0;
167
168
0
    for (i = 0; i < h; ++i) {
169
0
        for (j = 0; j < w; ++j) {
170
0
            const int diff = a[j] - b[j];
171
0
            sad += diff;
172
0
            *sse += diff * diff;
173
0
        }
174
175
0
        a += a_stride;
176
0
        b += b_stride;
177
0
    }
178
179
0
    return *sse - ((int64_t)sad * sad) / (w * h);
180
0
}
181
182
// Moved from EbComputeVariance_C.c
183
#define VAR(W, H)                                                                        \
184
    uint32_t svt_aom_variance##W##x##H##_c(                                              \
185
157k
        const uint8_t* a, int a_stride, const uint8_t* b, int b_stride, uint32_t* sse) { \
186
157k
        int sum;                                                                         \
187
157k
        variance_c(a, a_stride, b, b_stride, W, H, sse, &sum);                           \
188
157k
        return *sse - (uint32_t)(((int64_t)sum * sum) / (W * H));                        \
189
157k
    }
Unexecuted instantiation: svt_aom_variance128x128_c
Unexecuted instantiation: svt_aom_variance128x64_c
Unexecuted instantiation: svt_aom_variance64x128_c
svt_aom_variance64x64_c
Line
Count
Source
185
2.46k
        const uint8_t* a, int a_stride, const uint8_t* b, int b_stride, uint32_t* sse) { \
186
2.46k
        int sum;                                                                         \
187
2.46k
        variance_c(a, a_stride, b, b_stride, W, H, sse, &sum);                           \
188
2.46k
        return *sse - (uint32_t)(((int64_t)sum * sum) / (W * H));                        \
189
2.46k
    }
Unexecuted instantiation: svt_aom_variance64x32_c
Unexecuted instantiation: svt_aom_variance32x64_c
svt_aom_variance32x32_c
Line
Count
Source
185
675
        const uint8_t* a, int a_stride, const uint8_t* b, int b_stride, uint32_t* sse) { \
186
675
        int sum;                                                                         \
187
675
        variance_c(a, a_stride, b, b_stride, W, H, sse, &sum);                           \
188
675
        return *sse - (uint32_t)(((int64_t)sum * sum) / (W * H));                        \
189
675
    }
Unexecuted instantiation: svt_aom_variance32x16_c
Unexecuted instantiation: svt_aom_variance16x32_c
svt_aom_variance16x16_c
Line
Count
Source
185
999
        const uint8_t* a, int a_stride, const uint8_t* b, int b_stride, uint32_t* sse) { \
186
999
        int sum;                                                                         \
187
999
        variance_c(a, a_stride, b, b_stride, W, H, sse, &sum);                           \
188
999
        return *sse - (uint32_t)(((int64_t)sum * sum) / (W * H));                        \
189
999
    }
Unexecuted instantiation: svt_aom_variance16x8_c
Unexecuted instantiation: svt_aom_variance8x16_c
svt_aom_variance8x8_c
Line
Count
Source
185
153k
        const uint8_t* a, int a_stride, const uint8_t* b, int b_stride, uint32_t* sse) { \
186
153k
        int sum;                                                                         \
187
153k
        variance_c(a, a_stride, b, b_stride, W, H, sse, &sum);                           \
188
153k
        return *sse - (uint32_t)(((int64_t)sum * sum) / (W * H));                        \
189
153k
    }
Unexecuted instantiation: svt_aom_variance8x4_c
Unexecuted instantiation: svt_aom_variance4x8_c
Unexecuted instantiation: svt_aom_variance4x4_c
Unexecuted instantiation: svt_aom_variance4x16_c
Unexecuted instantiation: svt_aom_variance16x4_c
Unexecuted instantiation: svt_aom_variance8x32_c
Unexecuted instantiation: svt_aom_variance32x8_c
Unexecuted instantiation: svt_aom_variance16x64_c
Unexecuted instantiation: svt_aom_variance64x16_c
190
191
#define SUBPIX_VAR(W, H)                                                                                           \
192
    uint32_t svt_aom_sub_pixel_variance##W##x##H##_c(                                                              \
193
0
        const uint8_t* a, int a_stride, int xoffset, int yoffset, const uint8_t* b, int b_stride, uint32_t* sse) { \
194
0
        uint16_t fdata3[(H + 1) * W];                                                                              \
195
0
        uint8_t  temp2[H * W];                                                                                     \
196
0
                                                                                                                   \
197
0
        aom_var_filter_block2d_bil_first_pass_c(a, fdata3, a_stride, 1, H + 1, W, bilinear_filters_2t[xoffset]);   \
198
0
        aom_var_filter_block2d_bil_second_pass_c(fdata3, temp2, W, W, H, W, bilinear_filters_2t[yoffset]);         \
199
0
                                                                                                                   \
200
0
        return svt_aom_variance##W##x##H##_c(temp2, W, b, b_stride, sse);                                          \
201
0
    }
Unexecuted instantiation: svt_aom_sub_pixel_variance128x128_c
Unexecuted instantiation: svt_aom_sub_pixel_variance128x64_c
Unexecuted instantiation: svt_aom_sub_pixel_variance64x128_c
Unexecuted instantiation: svt_aom_sub_pixel_variance64x64_c
Unexecuted instantiation: svt_aom_sub_pixel_variance64x32_c
Unexecuted instantiation: svt_aom_sub_pixel_variance32x64_c
Unexecuted instantiation: svt_aom_sub_pixel_variance32x32_c
Unexecuted instantiation: svt_aom_sub_pixel_variance32x16_c
Unexecuted instantiation: svt_aom_sub_pixel_variance16x32_c
Unexecuted instantiation: svt_aom_sub_pixel_variance16x16_c
Unexecuted instantiation: svt_aom_sub_pixel_variance16x8_c
Unexecuted instantiation: svt_aom_sub_pixel_variance8x16_c
Unexecuted instantiation: svt_aom_sub_pixel_variance8x8_c
Unexecuted instantiation: svt_aom_sub_pixel_variance8x4_c
Unexecuted instantiation: svt_aom_sub_pixel_variance4x8_c
Unexecuted instantiation: svt_aom_sub_pixel_variance4x4_c
Unexecuted instantiation: svt_aom_sub_pixel_variance4x16_c
Unexecuted instantiation: svt_aom_sub_pixel_variance16x4_c
Unexecuted instantiation: svt_aom_sub_pixel_variance8x32_c
Unexecuted instantiation: svt_aom_sub_pixel_variance32x8_c
Unexecuted instantiation: svt_aom_sub_pixel_variance16x64_c
Unexecuted instantiation: svt_aom_sub_pixel_variance64x16_c
202
203
/* All the variance are available in the same sizes. */
204
#define VARIANCES(W, H) \
205
    VAR(W, H)           \
206
    SUBPIX_VAR(W, H)
207
VARIANCES(128, 128)
208
VARIANCES(128, 64)
209
VARIANCES(64, 128)
210
VARIANCES(64, 64)
211
VARIANCES(64, 32)
212
VARIANCES(32, 64)
213
VARIANCES(32, 32)
214
VARIANCES(32, 16)
215
VARIANCES(16, 32)
216
VARIANCES(16, 16)
217
VARIANCES(16, 8)
218
VARIANCES(8, 16)
219
VARIANCES(8, 8)
220
VARIANCES(8, 4)
221
VARIANCES(4, 8)
222
VARIANCES(4, 4)
223
VARIANCES(4, 16)
224
VARIANCES(16, 4)
225
VARIANCES(8, 32)
226
VARIANCES(32, 8)
227
VARIANCES(16, 64)
228
VARIANCES(64, 16)
229
230
static INLINE void obmc_variance(const uint8_t* pre, int pre_stride, const int32_t* wsrc, const int32_t* mask, int w,
231
                                 int h, unsigned int* sse, int* sum) {
232
    int i, j;
233
234
    *sse = 0;
235
    *sum = 0;
236
237
    for (i = 0; i < h; i++) {
238
        for (j = 0; j < w; j++) {
239
            int diff = ROUND_POWER_OF_TWO_SIGNED(wsrc[j] - pre[j] * mask[j], 12);
240
            *sum += diff;
241
            *sse += diff * diff;
242
        }
243
244
        pre += pre_stride;
245
        wsrc += w;
246
        mask += w;
247
    }
248
}
249
250
#define OBMC_VAR(W, H)                                                                                     \
251
    unsigned int svt_aom_obmc_variance##W##x##H##_c(                                                       \
252
0
        const uint8_t* pre, int pre_stride, const int32_t* wsrc, const int32_t* mask, unsigned int* sse) { \
253
0
        int sum;                                                                                           \
254
0
        obmc_variance(pre, pre_stride, wsrc, mask, W, H, sse, &sum);                                       \
255
0
        return *sse - (unsigned int)(((int64_t)sum * sum) / (W * H));                                      \
256
0
    }
Unexecuted instantiation: svt_aom_obmc_variance4x4_c
Unexecuted instantiation: svt_aom_obmc_variance4x8_c
Unexecuted instantiation: svt_aom_obmc_variance8x4_c
Unexecuted instantiation: svt_aom_obmc_variance8x8_c
Unexecuted instantiation: svt_aom_obmc_variance8x16_c
Unexecuted instantiation: svt_aom_obmc_variance16x8_c
Unexecuted instantiation: svt_aom_obmc_variance16x16_c
Unexecuted instantiation: svt_aom_obmc_variance16x32_c
Unexecuted instantiation: svt_aom_obmc_variance32x16_c
Unexecuted instantiation: svt_aom_obmc_variance32x32_c
Unexecuted instantiation: svt_aom_obmc_variance32x64_c
Unexecuted instantiation: svt_aom_obmc_variance64x32_c
Unexecuted instantiation: svt_aom_obmc_variance64x64_c
Unexecuted instantiation: svt_aom_obmc_variance64x128_c
Unexecuted instantiation: svt_aom_obmc_variance128x64_c
Unexecuted instantiation: svt_aom_obmc_variance128x128_c
Unexecuted instantiation: svt_aom_obmc_variance4x16_c
Unexecuted instantiation: svt_aom_obmc_variance16x4_c
Unexecuted instantiation: svt_aom_obmc_variance8x32_c
Unexecuted instantiation: svt_aom_obmc_variance32x8_c
Unexecuted instantiation: svt_aom_obmc_variance16x64_c
Unexecuted instantiation: svt_aom_obmc_variance64x16_c
257
258
#define OBMC_SUBPIX_VAR(W, H)                                                                                        \
259
    unsigned int svt_aom_obmc_sub_pixel_variance##W##x##H##_c(const uint8_t* pre,                                    \
260
                                                              int            pre_stride,                             \
261
                                                              int            xoffset,                                \
262
                                                              int            yoffset,                                \
263
                                                              const int32_t* wsrc,                                   \
264
                                                              const int32_t* mask,                                   \
265
0
                                                              unsigned int*  sse) {                                   \
266
0
        uint16_t fdata3[(H + 1) * W];                                                                                \
267
0
        uint8_t  temp2[H * W];                                                                                       \
268
0
                                                                                                                     \
269
0
        aom_var_filter_block2d_bil_first_pass_c(pre, fdata3, pre_stride, 1, H + 1, W, bilinear_filters_2t[xoffset]); \
270
0
        aom_var_filter_block2d_bil_second_pass_c(fdata3, temp2, W, W, H, W, bilinear_filters_2t[yoffset]);           \
271
0
                                                                                                                     \
272
0
        return svt_aom_obmc_variance##W##x##H##_c(temp2, W, wsrc, mask, sse);                                        \
273
0
    }
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance4x4_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance4x8_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance8x4_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance8x8_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance8x16_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance16x8_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance16x16_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance16x32_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance32x16_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance32x32_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance32x64_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance64x32_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance64x64_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance64x128_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance128x64_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance128x128_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance4x16_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance16x4_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance8x32_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance32x8_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance16x64_c
Unexecuted instantiation: svt_aom_obmc_sub_pixel_variance64x16_c
274
275
OBMC_VAR(4, 4)
276
OBMC_SUBPIX_VAR(4, 4)
277
278
OBMC_VAR(4, 8)
279
OBMC_SUBPIX_VAR(4, 8)
280
281
OBMC_VAR(8, 4)
282
OBMC_SUBPIX_VAR(8, 4)
283
284
OBMC_VAR(8, 8)
285
OBMC_SUBPIX_VAR(8, 8)
286
287
OBMC_VAR(8, 16)
288
OBMC_SUBPIX_VAR(8, 16)
289
290
OBMC_VAR(16, 8)
291
OBMC_SUBPIX_VAR(16, 8)
292
293
OBMC_VAR(16, 16)
294
OBMC_SUBPIX_VAR(16, 16)
295
296
OBMC_VAR(16, 32)
297
OBMC_SUBPIX_VAR(16, 32)
298
299
OBMC_VAR(32, 16)
300
OBMC_SUBPIX_VAR(32, 16)
301
302
OBMC_VAR(32, 32)
303
OBMC_SUBPIX_VAR(32, 32)
304
305
OBMC_VAR(32, 64)
306
OBMC_SUBPIX_VAR(32, 64)
307
308
OBMC_VAR(64, 32)
309
OBMC_SUBPIX_VAR(64, 32)
310
311
OBMC_VAR(64, 64)
312
OBMC_SUBPIX_VAR(64, 64)
313
314
OBMC_VAR(64, 128)
315
OBMC_SUBPIX_VAR(64, 128)
316
317
OBMC_VAR(128, 64)
318
OBMC_SUBPIX_VAR(128, 64)
319
320
OBMC_VAR(128, 128)
321
OBMC_SUBPIX_VAR(128, 128)
322
323
OBMC_VAR(4, 16)
324
OBMC_SUBPIX_VAR(4, 16)
325
OBMC_VAR(16, 4)
326
OBMC_SUBPIX_VAR(16, 4)
327
OBMC_VAR(8, 32)
328
OBMC_SUBPIX_VAR(8, 32)
329
OBMC_VAR(32, 8)
330
OBMC_SUBPIX_VAR(32, 8)
331
OBMC_VAR(16, 64)
332
OBMC_SUBPIX_VAR(16, 64)
333
OBMC_VAR(64, 16)
334
OBMC_SUBPIX_VAR(64, 16)
335
336
uint32_t svt_aom_highbd_mse16x16_c(const uint8_t* src_ptr, int32_t source_stride, const uint8_t* ref_ptr,
337
0
                                   int32_t recon_stride) {
338
0
    const uint16_t* a    = CONVERT_TO_SHORTPTR(src_ptr);
339
0
    const uint16_t* b    = CONVERT_TO_SHORTPTR(ref_ptr);
340
0
    uint64_t        tsse = 0;
341
342
0
    for (int i = 0; i < 16; ++i) {
343
0
        for (int j = 0; j < 16; ++j) {
344
0
            const int diff = a[j] - b[j];
345
0
            tsse += (uint32_t)(diff * diff);
346
0
        }
347
0
        a += source_stride;
348
0
        b += recon_stride;
349
0
    }
350
0
    return (uint32_t)tsse;
351
0
}