Coverage Report

Created: 2026-07-16 06:28

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
0
{
40
0
    const int adiff = abs(diff);
41
0
    return apply_sign(imin(adiff, imax(0, threshold - (adiff >> shift))), diff);
42
0
}
43
44
static inline void fill(int16_t *tmp, const ptrdiff_t stride,
45
                        const int w, const int h)
46
0
{
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
0
    for (int y = 0; y < h; y++) {
50
0
        for (int x = 0; x < w; x++)
51
0
            tmp[x] = INT16_MIN;
52
0
        tmp += stride;
53
0
    }
54
0
}
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
0
{
62
    // fill extended input buffer
63
0
    int x_start = -2, x_end = w + 2, y_start = -2, y_end = h + 2;
64
0
    if (!(edges & CDEF_HAVE_TOP)) {
65
0
        fill(tmp - 2 - 2 * tmp_stride, tmp_stride, w + 4, 2);
66
0
        y_start = 0;
67
0
    }
68
0
    if (!(edges & CDEF_HAVE_BOTTOM)) {
69
0
        fill(tmp + h * tmp_stride - 2, tmp_stride, w + 4, 2);
70
0
        y_end -= 2;
71
0
    }
72
0
    if (!(edges & CDEF_HAVE_LEFT)) {
73
0
        fill(tmp + y_start * tmp_stride - 2, tmp_stride, 2, y_end - y_start);
74
0
        x_start = 0;
75
0
    }
76
0
    if (!(edges & CDEF_HAVE_RIGHT)) {
77
0
        fill(tmp + y_start * tmp_stride + w, tmp_stride, 2, y_end - y_start);
78
0
        x_end -= 2;
79
0
    }
80
81
0
    for (int y = y_start; y < 0; y++) {
82
0
        for (int x = x_start; x < x_end; x++)
83
0
            tmp[x + y * tmp_stride] = top[x];
84
0
        top += PXSTRIDE(src_stride);
85
0
    }
86
0
    for (int y = 0; y < h; y++)
87
0
        for (int x = x_start; x < 0; x++)
88
0
            tmp[x + y * tmp_stride] = left[y][2 + x];
89
0
    for (int y = 0; y < h; y++) {
90
0
        for (int x = (y < h) ? 0 : x_start; x < x_end; x++)
91
0
            tmp[x] = src[x];
92
0
        src += PXSTRIDE(src_stride);
93
0
        tmp += tmp_stride;
94
0
    }
95
0
    for (int y = h; y < y_end; y++) {
96
0
        for (int x = x_start; x < x_end; x++)
97
0
            tmp[x] = bottom[x];
98
0
        bottom += PXSTRIDE(src_stride);
99
0
        tmp += tmp_stride;
100
0
    }
101
102
0
}
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
0
{
112
0
    const ptrdiff_t tmp_stride = 12;
113
0
    assert((w == 4 || w == 8) && (h == 4 || h == 8));
114
0
    int16_t tmp_buf[144]; // 12*12 is the maximum value of tmp_stride * (h + 4)
115
0
    int16_t *tmp = tmp_buf + 2 * tmp_stride + 2;
116
0
    const int8_t (*const cdef_dirs)[2] = &dav1d_cdef_directions[dir];
117
118
0
    padding(tmp, tmp_stride, dst, dst_stride, left, top, bottom, w, h, edges);
119
120
0
    if (pri_strength) {
121
0
        const int bitdepth_min_8 = bitdepth_from_max(bitdepth_max) - 8;
122
0
        const int pri_tap = 4 - ((pri_strength >> bitdepth_min_8) & 1);
123
0
        const int pri_shift = imax(0, damping - ulog2(pri_strength));
124
0
        if (sec_strength) {
125
0
            const int sec_shift = damping - ulog2(sec_strength);
126
0
            do {
127
0
                for (int x = 0; x < w; x++) {
128
0
                    const int px = dst[x];
129
0
                    int sum = 0;
130
0
                    int max = px, min = px;
131
0
                    int pri_tap_k = pri_tap;
132
0
                    for (int k = 0; k < 2; k++) {
133
0
                        const int off1 = cdef_dirs[2][k]; // dir
134
0
                        const int p0 = tmp[x + off1];
135
0
                        const int p1 = tmp[x - off1];
136
0
                        sum += pri_tap_k * constrain(p0 - px, pri_strength, pri_shift);
137
0
                        sum += pri_tap_k * constrain(p1 - px, pri_strength, pri_shift);
138
                        // if pri_tap_k == 4 then it becomes 2 else it remains 3
139
0
                        pri_tap_k = (pri_tap_k & 3) | 2;
140
0
                        min = umin(p0, min);
141
0
                        max = imax(p0, max);
142
0
                        min = umin(p1, min);
143
0
                        max = imax(p1, max);
144
0
                        const int off2 = cdef_dirs[4][k]; // dir + 2
145
0
                        const int off3 = cdef_dirs[0][k]; // dir - 2
146
0
                        const int s0 = tmp[x + off2];
147
0
                        const int s1 = tmp[x - off2];
148
0
                        const int s2 = tmp[x + off3];
149
0
                        const int s3 = tmp[x - off3];
150
                        // sec_tap starts at 2 and becomes 1
151
0
                        const int sec_tap = 2 - k;
152
0
                        sum += sec_tap * constrain(s0 - px, sec_strength, sec_shift);
153
0
                        sum += sec_tap * constrain(s1 - px, sec_strength, sec_shift);
154
0
                        sum += sec_tap * constrain(s2 - px, sec_strength, sec_shift);
155
0
                        sum += sec_tap * constrain(s3 - px, sec_strength, sec_shift);
156
0
                        min = umin(s0, min);
157
0
                        max = imax(s0, max);
158
0
                        min = umin(s1, min);
159
0
                        max = imax(s1, max);
160
0
                        min = umin(s2, min);
161
0
                        max = imax(s2, max);
162
0
                        min = umin(s3, min);
163
0
                        max = imax(s3, max);
164
0
                    }
165
0
                    dst[x] = iclip(px + ((sum - (sum < 0) + 8) >> 4), min, max);
166
0
                }
167
0
                dst += PXSTRIDE(dst_stride);
168
0
                tmp += tmp_stride;
169
0
            } while (--h);
170
0
        } else { // pri_strength only
171
0
            do {
172
0
                for (int x = 0; x < w; x++) {
173
0
                    const int px = dst[x];
174
0
                    int sum = 0;
175
0
                    int pri_tap_k = pri_tap;
176
0
                    for (int k = 0; k < 2; k++) {
177
0
                        const int off = cdef_dirs[2][k]; // dir
178
0
                        const int p0 = tmp[x + off];
179
0
                        const int p1 = tmp[x - off];
180
0
                        sum += pri_tap_k * constrain(p0 - px, pri_strength, pri_shift);
181
0
                        sum += pri_tap_k * constrain(p1 - px, pri_strength, pri_shift);
182
0
                        pri_tap_k = (pri_tap_k & 3) | 2;
183
0
                    }
184
0
                    dst[x] = px + ((sum - (sum < 0) + 8) >> 4);
185
0
                }
186
0
                dst += PXSTRIDE(dst_stride);
187
0
                tmp += tmp_stride;
188
0
            } while (--h);
189
0
        }
190
0
    } else { // sec_strength only
191
0
        assert(sec_strength);
192
0
        const int sec_shift = damping - ulog2(sec_strength);
193
0
        do {
194
0
            for (int x = 0; x < w; x++) {
195
0
                const int px = dst[x];
196
0
                int sum = 0;
197
0
                for (int k = 0; k < 2; k++) {
198
0
                    const int off1 = cdef_dirs[4][k]; // dir + 2
199
0
                    const int off2 = cdef_dirs[0][k]; // dir - 2
200
0
                    const int s0 = tmp[x + off1];
201
0
                    const int s1 = tmp[x - off1];
202
0
                    const int s2 = tmp[x + off2];
203
0
                    const int s3 = tmp[x - off2];
204
0
                    const int sec_tap = 2 - k;
205
0
                    sum += sec_tap * constrain(s0 - px, sec_strength, sec_shift);
206
0
                    sum += sec_tap * constrain(s1 - px, sec_strength, sec_shift);
207
0
                    sum += sec_tap * constrain(s2 - px, sec_strength, sec_shift);
208
0
                    sum += sec_tap * constrain(s3 - px, sec_strength, sec_shift);
209
0
                }
210
0
                dst[x] = px + ((sum - (sum < 0) + 8) >> 4);
211
0
            }
212
0
            dst += PXSTRIDE(dst_stride);
213
0
            tmp += tmp_stride;
214
0
        } while (--h);
215
0
    }
216
0
}
217
218
#define cdef_fn(w, h) \
219
static void cdef_filter_block_##w##x##h##_c(pixel *const dst, \
220
                                            const ptrdiff_t stride, \
221
                                            const pixel (*left)[2], \
222
                                            const pixel *const top, \
223
                                            const pixel *const bottom, \
224
                                            const int pri_strength, \
225
                                            const int sec_strength, \
226
                                            const int dir, \
227
                                            const int damping, \
228
                                            const enum CdefEdgeFlags edges \
229
0
                                            HIGHBD_DECL_SUFFIX) \
230
0
{ \
231
0
    cdef_filter_block_c(dst, stride, left, top, bottom, \
232
0
                        pri_strength, sec_strength, dir, damping, w, h, edges HIGHBD_TAIL_SUFFIX); \
233
0
}
Unexecuted instantiation: cdef_tmpl.c:cdef_filter_block_8x8_c
Unexecuted instantiation: cdef_tmpl.c:cdef_filter_block_4x8_c
Unexecuted instantiation: cdef_tmpl.c:cdef_filter_block_4x4_c
234
235
cdef_fn(4, 4);
236
cdef_fn(4, 8);
237
cdef_fn(8, 8);
238
239
static int cdef_find_dir_c(const pixel *img, const ptrdiff_t stride,
240
                           unsigned *const var HIGHBD_DECL_SUFFIX)
241
0
{
242
0
    const int bitdepth_min_8 = bitdepth_from_max(bitdepth_max) - 8;
243
0
    int partial_sum_hv[2][8] = { { 0 } };
244
0
    int partial_sum_diag[2][15] = { { 0 } };
245
0
    int partial_sum_alt[4][11] = { { 0 } };
246
247
0
    for (int y = 0; y < 8; y++) {
248
0
        for (int x = 0; x < 8; x++) {
249
0
            const int px = (img[x] >> bitdepth_min_8) - 128;
250
251
0
            partial_sum_diag[0][     y       +  x      ] += px;
252
0
            partial_sum_alt [0][     y       + (x >> 1)] += px;
253
0
            partial_sum_hv  [0][     y                 ] += px;
254
0
            partial_sum_alt [1][3 +  y       - (x >> 1)] += px;
255
0
            partial_sum_diag[1][7 +  y       -  x      ] += px;
256
0
            partial_sum_alt [2][3 - (y >> 1) +  x      ] += px;
257
0
            partial_sum_hv  [1][                x      ] += px;
258
0
            partial_sum_alt [3][    (y >> 1) +  x      ] += px;
259
0
        }
260
0
        img += PXSTRIDE(stride);
261
0
    }
262
263
0
    unsigned cost[8] = { 0 };
264
0
    for (int n = 0; n < 8; n++) {
265
0
        cost[2] += partial_sum_hv[0][n] * partial_sum_hv[0][n];
266
0
        cost[6] += partial_sum_hv[1][n] * partial_sum_hv[1][n];
267
0
    }
268
0
    cost[2] *= 105;
269
0
    cost[6] *= 105;
270
271
0
    static const uint16_t div_table[7] = { 840, 420, 280, 210, 168, 140, 120 };
272
0
    for (int n = 0; n < 7; n++) {
273
0
        const int d = div_table[n];
274
0
        cost[0] += (partial_sum_diag[0][n]      * partial_sum_diag[0][n] +
275
0
                    partial_sum_diag[0][14 - n] * partial_sum_diag[0][14 - n]) * d;
276
0
        cost[4] += (partial_sum_diag[1][n]      * partial_sum_diag[1][n] +
277
0
                    partial_sum_diag[1][14 - n] * partial_sum_diag[1][14 - n]) * d;
278
0
    }
279
0
    cost[0] += partial_sum_diag[0][7] * partial_sum_diag[0][7] * 105;
280
0
    cost[4] += partial_sum_diag[1][7] * partial_sum_diag[1][7] * 105;
281
282
0
    for (int n = 0; n < 4; n++) {
283
0
        unsigned *const cost_ptr = &cost[n * 2 + 1];
284
0
        for (int m = 0; m < 5; m++)
285
0
            *cost_ptr += partial_sum_alt[n][3 + m] * partial_sum_alt[n][3 + m];
286
0
        *cost_ptr *= 105;
287
0
        for (int m = 0; m < 3; m++) {
288
0
            const int d = div_table[2 * m + 1];
289
0
            *cost_ptr += (partial_sum_alt[n][m]      * partial_sum_alt[n][m] +
290
0
                          partial_sum_alt[n][10 - m] * partial_sum_alt[n][10 - m]) * d;
291
0
        }
292
0
    }
293
294
0
    int best_dir = 0;
295
0
    unsigned best_cost = cost[0];
296
0
    for (int n = 1; n < 8; n++) {
297
0
        if (cost[n] > best_cost) {
298
0
            best_cost = cost[n];
299
0
            best_dir = n;
300
0
        }
301
0
    }
302
303
0
    *var = (best_cost - (cost[best_dir ^ 4])) >> 10;
304
0
    return best_dir;
305
0
}
306
307
#if HAVE_ASM
308
#if ARCH_AARCH64 || ARCH_ARM
309
#include "src/arm/cdef.h"
310
#elif ARCH_PPC64LE
311
#include "src/ppc/cdef.h"
312
#elif ARCH_RISCV
313
#include "src/riscv/cdef.h"
314
#elif ARCH_X86
315
#include "src/x86/cdef.h"
316
#elif ARCH_LOONGARCH64
317
#include "src/loongarch/cdef.h"
318
#endif
319
#endif
320
321
0
COLD void bitfn(dav1d_cdef_dsp_init)(Dav1dCdefDSPContext *const c) {
322
0
    c->dir = cdef_find_dir_c;
323
0
    c->fb[0] = cdef_filter_block_8x8_c;
324
0
    c->fb[1] = cdef_filter_block_4x8_c;
325
0
    c->fb[2] = cdef_filter_block_4x4_c;
326
327
#if HAVE_ASM
328
#if ARCH_AARCH64 || ARCH_ARM
329
    cdef_dsp_init_arm(c);
330
#elif ARCH_PPC64LE
331
    cdef_dsp_init_ppc(c);
332
#elif ARCH_RISCV
333
    cdef_dsp_init_riscv(c);
334
#elif ARCH_X86
335
    cdef_dsp_init_x86(c);
336
#elif ARCH_LOONGARCH64
337
    cdef_dsp_init_loongarch(c);
338
#endif
339
#endif
340
0
}
Unexecuted instantiation: dav1d_cdef_dsp_init_8bpc
Unexecuted instantiation: dav1d_cdef_dsp_init_16bpc