Coverage Report

Created: 2026-05-30 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/cdef_tmpl.c
Line
Count
Source
1
/*
2
 * Copyright © 2018, VideoLAN and dav1d authors
3
 * Copyright © 2018, Two Orioles, LLC
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *    list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *    this list of conditions and the following disclaimer in the documentation
14
 *    and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27
28
#include "config.h"
29
30
#include <stdlib.h>
31
32
#include "common/intops.h"
33
34
#include "src/cdef.h"
35
#include "src/tables.h"
36
37
static inline int constrain(const int diff, const int threshold,
38
                            const int shift)
39
531M
{
40
531M
    const int adiff = abs(diff);
41
531M
    return apply_sign(imin(adiff, imax(0, threshold - (adiff >> shift))), diff);
42
531M
}
43
44
static inline void fill(int16_t *tmp, const ptrdiff_t stride,
45
                        const int w, const int h)
46
931k
{
47
    /* Use a value that's a large positive number when interpreted as unsigned,
48
     * and a large negative number when interpreted as signed. */
49
7.96M
    for (int y = 0; y < h; y++) {
50
27.2M
        for (int x = 0; x < w; x++)
51
20.2M
            tmp[x] = INT16_MIN;
52
7.03M
        tmp += stride;
53
7.03M
    }
54
931k
}
55
56
static void padding(int16_t *tmp, const ptrdiff_t tmp_stride,
57
                    const pixel *src, const ptrdiff_t src_stride,
58
                    const pixel (*left)[2],
59
                    const pixel *top, const pixel *bottom,
60
                    const int w, const int h, const enum CdefEdgeFlags edges)
61
1.53M
{
62
    // fill extended input buffer
63
1.53M
    int x_start = -2, x_end = w + 2, y_start = -2, y_end = h + 2;
64
1.53M
    if (!(edges & CDEF_HAVE_TOP)) {
65
175k
        fill(tmp - 2 - 2 * tmp_stride, tmp_stride, w + 4, 2);
66
175k
        y_start = 0;
67
175k
    }
68
1.53M
    if (!(edges & CDEF_HAVE_BOTTOM)) {
69
173k
        fill(tmp + h * tmp_stride - 2, tmp_stride, w + 4, 2);
70
173k
        y_end -= 2;
71
173k
    }
72
1.53M
    if (!(edges & CDEF_HAVE_LEFT)) {
73
291k
        fill(tmp + y_start * tmp_stride - 2, tmp_stride, 2, y_end - y_start);
74
291k
        x_start = 0;
75
291k
    }
76
1.53M
    if (!(edges & CDEF_HAVE_RIGHT)) {
77
291k
        fill(tmp + y_start * tmp_stride + w, tmp_stride, 2, y_end - y_start);
78
291k
        x_end -= 2;
79
291k
    }
80
81
4.25M
    for (int y = y_start; y < 0; y++) {
82
31.2M
        for (int x = x_start; x < x_end; x++)
83
28.5M
            tmp[x + y * tmp_stride] = top[x];
84
2.71M
        top += PXSTRIDE(src_stride);
85
2.71M
    }
86
12.6M
    for (int y = 0; y < h; y++)
87
29.3M
        for (int x = x_start; x < 0; x++)
88
18.1M
            tmp[x + y * tmp_stride] = left[y][2 + x];
89
12.6M
    for (int y = 0; y < h; y++) {
90
18.4E
        for (int x = (y < h) ? 0 : x_start; x < x_end; x++)
91
101M
            tmp[x] = src[x];
92
11.0M
        src += PXSTRIDE(src_stride);
93
11.0M
        tmp += tmp_stride;
94
11.0M
    }
95
4.25M
    for (int y = h; y < y_end; y++) {
96
31.1M
        for (int x = x_start; x < x_end; x++)
97
28.4M
            tmp[x] = bottom[x];
98
2.71M
        bottom += PXSTRIDE(src_stride);
99
2.71M
        tmp += tmp_stride;
100
2.71M
    }
101
102
1.53M
}
103
104
static NOINLINE void
105
cdef_filter_block_c(pixel *dst, const ptrdiff_t dst_stride,
106
                    const pixel (*left)[2],
107
                    const pixel *const top, const pixel *const bottom,
108
                    const int pri_strength, const int sec_strength,
109
                    const int dir, const int damping, const int w, int h,
110
                    const enum CdefEdgeFlags edges HIGHBD_DECL_SUFFIX)
111
1.53M
{
112
1.53M
    const ptrdiff_t tmp_stride = 12;
113
1.53M
    assert((w == 4 || w == 8) && (h == 4 || h == 8));
114
1.53M
    int16_t tmp_buf[144]; // 12*12 is the maximum value of tmp_stride * (h + 4)
115
1.53M
    int16_t *tmp = tmp_buf + 2 * tmp_stride + 2;
116
117
1.53M
    padding(tmp, tmp_stride, dst, dst_stride, left, top, bottom, w, h, edges);
118
119
1.53M
    if (pri_strength) {
120
1.09M
        const int bitdepth_min_8 = bitdepth_from_max(bitdepth_max) - 8;
121
1.09M
        const int pri_tap = 4 - ((pri_strength >> bitdepth_min_8) & 1);
122
1.09M
        const int pri_shift = imax(0, damping - ulog2(pri_strength));
123
1.09M
        if (sec_strength) {
124
639k
            const int sec_shift = damping - ulog2(sec_strength);
125
4.32M
            do {
126
32.2M
                for (int x = 0; x < w; x++) {
127
27.8M
                    const int px = dst[x];
128
27.8M
                    int sum = 0;
129
27.8M
                    int max = px, min = px;
130
27.8M
                    int pri_tap_k = pri_tap;
131
79.9M
                    for (int k = 0; k < 2; k++) {
132
52.0M
                        const int off1 = dav1d_cdef_directions[dir + 2][k]; // dir
133
52.0M
                        const int p0 = tmp[x + off1];
134
52.0M
                        const int p1 = tmp[x - off1];
135
52.0M
                        sum += pri_tap_k * constrain(p0 - px, pri_strength, pri_shift);
136
52.0M
                        sum += pri_tap_k * constrain(p1 - px, pri_strength, pri_shift);
137
                        // if pri_tap_k == 4 then it becomes 2 else it remains 3
138
52.0M
                        pri_tap_k = (pri_tap_k & 3) | 2;
139
52.0M
                        min = umin(p0, min);
140
52.0M
                        max = imax(p0, max);
141
52.0M
                        min = umin(p1, min);
142
52.0M
                        max = imax(p1, max);
143
52.0M
                        const int off2 = dav1d_cdef_directions[dir + 4][k]; // dir + 2
144
52.0M
                        const int off3 = dav1d_cdef_directions[dir + 0][k]; // dir - 2
145
52.0M
                        const int s0 = tmp[x + off2];
146
52.0M
                        const int s1 = tmp[x - off2];
147
52.0M
                        const int s2 = tmp[x + off3];
148
52.0M
                        const int s3 = tmp[x - off3];
149
                        // sec_tap starts at 2 and becomes 1
150
52.0M
                        const int sec_tap = 2 - k;
151
52.0M
                        sum += sec_tap * constrain(s0 - px, sec_strength, sec_shift);
152
52.0M
                        sum += sec_tap * constrain(s1 - px, sec_strength, sec_shift);
153
52.0M
                        sum += sec_tap * constrain(s2 - px, sec_strength, sec_shift);
154
52.0M
                        sum += sec_tap * constrain(s3 - px, sec_strength, sec_shift);
155
52.0M
                        min = umin(s0, min);
156
52.0M
                        max = imax(s0, max);
157
52.0M
                        min = umin(s1, min);
158
52.0M
                        max = imax(s1, max);
159
52.0M
                        min = umin(s2, min);
160
52.0M
                        max = imax(s2, max);
161
52.0M
                        min = umin(s3, min);
162
52.0M
                        max = imax(s3, max);
163
52.0M
                    }
164
27.8M
                    dst[x] = iclip(px + ((sum - (sum < 0) + 8) >> 4), min, max);
165
27.8M
                }
166
4.32M
                dst += PXSTRIDE(dst_stride);
167
4.32M
                tmp += tmp_stride;
168
4.32M
            } while (--h);
169
639k
        } else { // pri_strength only
170
3.24M
            do {
171
26.3M
                for (int x = 0; x < w; x++) {
172
23.0M
                    const int px = dst[x];
173
23.0M
                    int sum = 0;
174
23.0M
                    int pri_tap_k = pri_tap;
175
68.3M
                    for (int k = 0; k < 2; k++) {
176
45.2M
                        const int off = dav1d_cdef_directions[dir + 2][k]; // dir
177
45.2M
                        const int p0 = tmp[x + off];
178
45.2M
                        const int p1 = tmp[x - off];
179
45.2M
                        sum += pri_tap_k * constrain(p0 - px, pri_strength, pri_shift);
180
45.2M
                        sum += pri_tap_k * constrain(p1 - px, pri_strength, pri_shift);
181
45.2M
                        pri_tap_k = (pri_tap_k & 3) | 2;
182
45.2M
                    }
183
23.0M
                    dst[x] = px + ((sum - (sum < 0) + 8) >> 4);
184
23.0M
                }
185
3.24M
                dst += PXSTRIDE(dst_stride);
186
3.24M
                tmp += tmp_stride;
187
3.24M
            } while (--h);
188
452k
        }
189
1.09M
    } else { // sec_strength only
190
446k
        assert(sec_strength);
191
446k
        const int sec_shift = damping - ulog2(sec_strength);
192
3.15M
        do {
193
23.9M
            for (int x = 0; x < w; x++) {
194
20.7M
                const int px = dst[x];
195
20.7M
                int sum = 0;
196
61.2M
                for (int k = 0; k < 2; k++) {
197
40.4M
                    const int off1 = dav1d_cdef_directions[dir + 4][k]; // dir + 2
198
40.4M
                    const int off2 = dav1d_cdef_directions[dir + 0][k]; // dir - 2
199
40.4M
                    const int s0 = tmp[x + off1];
200
40.4M
                    const int s1 = tmp[x - off1];
201
40.4M
                    const int s2 = tmp[x + off2];
202
40.4M
                    const int s3 = tmp[x - off2];
203
40.4M
                    const int sec_tap = 2 - k;
204
40.4M
                    sum += sec_tap * constrain(s0 - px, sec_strength, sec_shift);
205
40.4M
                    sum += sec_tap * constrain(s1 - px, sec_strength, sec_shift);
206
40.4M
                    sum += sec_tap * constrain(s2 - px, sec_strength, sec_shift);
207
40.4M
                    sum += sec_tap * constrain(s3 - px, sec_strength, sec_shift);
208
40.4M
                }
209
20.7M
                dst[x] = px + ((sum - (sum < 0) + 8) >> 4);
210
20.7M
            }
211
3.15M
            dst += PXSTRIDE(dst_stride);
212
3.15M
            tmp += tmp_stride;
213
3.15M
        } while (--h);
214
446k
    }
215
1.53M
}
216
217
#define cdef_fn(w, h) \
218
static void cdef_filter_block_##w##x##h##_c(pixel *const dst, \
219
                                            const ptrdiff_t stride, \
220
                                            const pixel (*left)[2], \
221
                                            const pixel *const top, \
222
                                            const pixel *const bottom, \
223
                                            const int pri_strength, \
224
                                            const int sec_strength, \
225
                                            const int dir, \
226
                                            const int damping, \
227
                                            const enum CdefEdgeFlags edges \
228
1.53M
                                            HIGHBD_DECL_SUFFIX) \
229
1.53M
{ \
230
1.53M
    cdef_filter_block_c(dst, stride, left, top, bottom, \
231
1.53M
                        pri_strength, sec_strength, dir, damping, w, h, edges HIGHBD_TAIL_SUFFIX); \
232
1.53M
}
cdef_tmpl.c:cdef_filter_block_8x8_c
Line
Count
Source
228
1.23M
                                            HIGHBD_DECL_SUFFIX) \
229
1.23M
{ \
230
1.23M
    cdef_filter_block_c(dst, stride, left, top, bottom, \
231
1.23M
                        pri_strength, sec_strength, dir, damping, w, h, edges HIGHBD_TAIL_SUFFIX); \
232
1.23M
}
cdef_tmpl.c:cdef_filter_block_4x8_c
Line
Count
Source
228
31.4k
                                            HIGHBD_DECL_SUFFIX) \
229
31.4k
{ \
230
31.4k
    cdef_filter_block_c(dst, stride, left, top, bottom, \
231
31.4k
                        pri_strength, sec_strength, dir, damping, w, h, edges HIGHBD_TAIL_SUFFIX); \
232
31.4k
}
cdef_tmpl.c:cdef_filter_block_4x4_c
Line
Count
Source
228
265k
                                            HIGHBD_DECL_SUFFIX) \
229
265k
{ \
230
265k
    cdef_filter_block_c(dst, stride, left, top, bottom, \
231
265k
                        pri_strength, sec_strength, dir, damping, w, h, edges HIGHBD_TAIL_SUFFIX); \
232
265k
}
233
234
cdef_fn(4, 4);
235
cdef_fn(4, 8);
236
cdef_fn(8, 8);
237
238
static int cdef_find_dir_c(const pixel *img, const ptrdiff_t stride,
239
                           unsigned *const var HIGHBD_DECL_SUFFIX)
240
1.15M
{
241
1.15M
    const int bitdepth_min_8 = bitdepth_from_max(bitdepth_max) - 8;
242
1.15M
    int partial_sum_hv[2][8] = { { 0 } };
243
1.15M
    int partial_sum_diag[2][15] = { { 0 } };
244
1.15M
    int partial_sum_alt[4][11] = { { 0 } };
245
246
10.3M
    for (int y = 0; y < 8; y++) {
247
82.9M
        for (int x = 0; x < 8; x++) {
248
73.7M
            const int px = (img[x] >> bitdepth_min_8) - 128;
249
250
73.7M
            partial_sum_diag[0][     y       +  x      ] += px;
251
73.7M
            partial_sum_alt [0][     y       + (x >> 1)] += px;
252
73.7M
            partial_sum_hv  [0][     y                 ] += px;
253
73.7M
            partial_sum_alt [1][3 +  y       - (x >> 1)] += px;
254
73.7M
            partial_sum_diag[1][7 +  y       -  x      ] += px;
255
73.7M
            partial_sum_alt [2][3 - (y >> 1) +  x      ] += px;
256
73.7M
            partial_sum_hv  [1][                x      ] += px;
257
73.7M
            partial_sum_alt [3][    (y >> 1) +  x      ] += px;
258
73.7M
        }
259
9.22M
        img += PXSTRIDE(stride);
260
9.22M
    }
261
262
1.15M
    unsigned cost[8] = { 0 };
263
10.4M
    for (int n = 0; n < 8; n++) {
264
9.27M
        cost[2] += partial_sum_hv[0][n] * partial_sum_hv[0][n];
265
9.27M
        cost[6] += partial_sum_hv[1][n] * partial_sum_hv[1][n];
266
9.27M
    }
267
1.15M
    cost[2] *= 105;
268
1.15M
    cost[6] *= 105;
269
270
1.15M
    static const uint16_t div_table[7] = { 840, 420, 280, 210, 168, 140, 120 };
271
9.27M
    for (int n = 0; n < 7; n++) {
272
8.11M
        const int d = div_table[n];
273
8.11M
        cost[0] += (partial_sum_diag[0][n]      * partial_sum_diag[0][n] +
274
8.11M
                    partial_sum_diag[0][14 - n] * partial_sum_diag[0][14 - n]) * d;
275
8.11M
        cost[4] += (partial_sum_diag[1][n]      * partial_sum_diag[1][n] +
276
8.11M
                    partial_sum_diag[1][14 - n] * partial_sum_diag[1][14 - n]) * d;
277
8.11M
    }
278
1.15M
    cost[0] += partial_sum_diag[0][7] * partial_sum_diag[0][7] * 105;
279
1.15M
    cost[4] += partial_sum_diag[1][7] * partial_sum_diag[1][7] * 105;
280
281
5.79M
    for (int n = 0; n < 4; n++) {
282
4.63M
        unsigned *const cost_ptr = &cost[n * 2 + 1];
283
27.7M
        for (int m = 0; m < 5; m++)
284
23.1M
            *cost_ptr += partial_sum_alt[n][3 + m] * partial_sum_alt[n][3 + m];
285
4.63M
        *cost_ptr *= 105;
286
18.5M
        for (int m = 0; m < 3; m++) {
287
13.8M
            const int d = div_table[2 * m + 1];
288
13.8M
            *cost_ptr += (partial_sum_alt[n][m]      * partial_sum_alt[n][m] +
289
13.8M
                          partial_sum_alt[n][10 - m] * partial_sum_alt[n][10 - m]) * d;
290
13.8M
        }
291
4.63M
    }
292
293
1.15M
    int best_dir = 0;
294
1.15M
    unsigned best_cost = cost[0];
295
9.27M
    for (int n = 1; n < 8; n++) {
296
8.11M
        if (cost[n] > best_cost) {
297
722k
            best_cost = cost[n];
298
722k
            best_dir = n;
299
722k
        }
300
8.11M
    }
301
302
1.15M
    *var = (best_cost - (cost[best_dir ^ 4])) >> 10;
303
1.15M
    return best_dir;
304
1.15M
}
305
306
#if HAVE_ASM
307
#if ARCH_AARCH64 || ARCH_ARM
308
#include "src/arm/cdef.h"
309
#elif ARCH_PPC64LE
310
#include "src/ppc/cdef.h"
311
#elif ARCH_RISCV
312
#include "src/riscv/cdef.h"
313
#elif ARCH_X86
314
#include "src/x86/cdef.h"
315
#elif ARCH_LOONGARCH64
316
#include "src/loongarch/cdef.h"
317
#endif
318
#endif
319
320
38.3k
COLD void bitfn(dav1d_cdef_dsp_init)(Dav1dCdefDSPContext *const c) {
321
38.3k
    c->dir = cdef_find_dir_c;
322
38.3k
    c->fb[0] = cdef_filter_block_8x8_c;
323
38.3k
    c->fb[1] = cdef_filter_block_4x8_c;
324
38.3k
    c->fb[2] = cdef_filter_block_4x4_c;
325
326
#if HAVE_ASM
327
#if ARCH_AARCH64 || ARCH_ARM
328
    cdef_dsp_init_arm(c);
329
#elif ARCH_PPC64LE
330
    cdef_dsp_init_ppc(c);
331
#elif ARCH_RISCV
332
    cdef_dsp_init_riscv(c);
333
#elif ARCH_X86
334
    cdef_dsp_init_x86(c);
335
#elif ARCH_LOONGARCH64
336
    cdef_dsp_init_loongarch(c);
337
#endif
338
#endif
339
38.3k
}
dav1d_cdef_dsp_init_8bpc
Line
Count
Source
320
17.2k
COLD void bitfn(dav1d_cdef_dsp_init)(Dav1dCdefDSPContext *const c) {
321
17.2k
    c->dir = cdef_find_dir_c;
322
17.2k
    c->fb[0] = cdef_filter_block_8x8_c;
323
17.2k
    c->fb[1] = cdef_filter_block_4x8_c;
324
17.2k
    c->fb[2] = cdef_filter_block_4x4_c;
325
326
#if HAVE_ASM
327
#if ARCH_AARCH64 || ARCH_ARM
328
    cdef_dsp_init_arm(c);
329
#elif ARCH_PPC64LE
330
    cdef_dsp_init_ppc(c);
331
#elif ARCH_RISCV
332
    cdef_dsp_init_riscv(c);
333
#elif ARCH_X86
334
    cdef_dsp_init_x86(c);
335
#elif ARCH_LOONGARCH64
336
    cdef_dsp_init_loongarch(c);
337
#endif
338
#endif
339
17.2k
}
dav1d_cdef_dsp_init_16bpc
Line
Count
Source
320
21.1k
COLD void bitfn(dav1d_cdef_dsp_init)(Dav1dCdefDSPContext *const c) {
321
21.1k
    c->dir = cdef_find_dir_c;
322
21.1k
    c->fb[0] = cdef_filter_block_8x8_c;
323
21.1k
    c->fb[1] = cdef_filter_block_4x8_c;
324
21.1k
    c->fb[2] = cdef_filter_block_4x4_c;
325
326
#if HAVE_ASM
327
#if ARCH_AARCH64 || ARCH_ARM
328
    cdef_dsp_init_arm(c);
329
#elif ARCH_PPC64LE
330
    cdef_dsp_init_ppc(c);
331
#elif ARCH_RISCV
332
    cdef_dsp_init_riscv(c);
333
#elif ARCH_X86
334
    cdef_dsp_init_x86(c);
335
#elif ARCH_LOONGARCH64
336
    cdef_dsp_init_loongarch(c);
337
#endif
338
#endif
339
21.1k
}