Coverage Report

Created: 2026-09-02 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/cdef_apply_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 <string.h>
31
32
#include "common/intops.h"
33
34
#include "src/cdef_apply.h"
35
36
enum Backup2x8Flags {
37
    BACKUP_2X8_Y = 1 << 0,
38
    BACKUP_2X8_UV = 1 << 1,
39
};
40
41
static void backup2lines(pixel *const dst[3], /*const*/ pixel *const src[3],
42
                         const ptrdiff_t stride[2],
43
                         const enum Dav1dPixelLayout layout)
44
1.45M
{
45
1.45M
    const ptrdiff_t y_stride = PXSTRIDE(stride[0]);
46
1.45M
    if (y_stride < 0)
47
0
        pixel_copy(dst[0] + y_stride, src[0] + 7 * y_stride, -2 * y_stride);
48
1.45M
    else
49
1.45M
        pixel_copy(dst[0], src[0] + 6 * y_stride, 2 * y_stride);
50
51
1.45M
    if (layout != DAV1D_PIXEL_LAYOUT_I400) {
52
1.18M
        const ptrdiff_t uv_stride = PXSTRIDE(stride[1]);
53
1.18M
        if (uv_stride < 0) {
54
0
            const int uv_off = layout == DAV1D_PIXEL_LAYOUT_I420 ? 3 : 7;
55
0
            pixel_copy(dst[1] + uv_stride, src[1] + uv_off * uv_stride, -2 * uv_stride);
56
0
            pixel_copy(dst[2] + uv_stride, src[2] + uv_off * uv_stride, -2 * uv_stride);
57
1.18M
        } else {
58
1.18M
            const int uv_off = layout == DAV1D_PIXEL_LAYOUT_I420 ? 2 : 6;
59
1.18M
            pixel_copy(dst[1], src[1] + uv_off * uv_stride, 2 * uv_stride);
60
1.18M
            pixel_copy(dst[2], src[2] + uv_off * uv_stride, 2 * uv_stride);
61
1.18M
        }
62
1.18M
    }
63
1.45M
}
64
65
static void backup2x8(pixel dst[3][8][2],
66
                      /*const*/ pixel *const src[3],
67
                      const ptrdiff_t src_stride[2], int x_off,
68
                      const enum Dav1dPixelLayout layout,
69
                      const enum Backup2x8Flags flag)
70
4.83M
{
71
4.83M
    ptrdiff_t y_off = 0;
72
4.83M
    if (flag & BACKUP_2X8_Y) {
73
40.2M
        for (int y = 0; y < 8; y++, y_off += PXSTRIDE(src_stride[0]))
74
35.7M
            pixel_copy(dst[0][y], &src[0][y_off + x_off - 2], 2);
75
4.47M
    }
76
77
4.83M
    if (layout == DAV1D_PIXEL_LAYOUT_I400 || !(flag & BACKUP_2X8_UV))
78
1.08M
        return;
79
80
3.75M
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
81
3.75M
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
82
83
3.75M
    x_off >>= ss_hor;
84
3.75M
    y_off = 0;
85
25.7M
    for (int y = 0; y < (8 >> ss_ver); y++, y_off += PXSTRIDE(src_stride[1])) {
86
22.0M
        pixel_copy(dst[1][y], &src[1][y_off + x_off - 2], 2);
87
22.0M
        pixel_copy(dst[2][y], &src[2][y_off + x_off - 2], 2);
88
22.0M
    }
89
3.75M
}
90
91
4.59M
static int adjust_strength(const int strength, const unsigned var) {
92
4.59M
    if (!var) return 0;
93
2.19M
    const int i = var >> 6 ? imin(ulog2(var >> 6), 12) : 0;
94
2.19M
    return (strength * (4 + i) + 8) >> 4;
95
4.59M
}
96
97
void bytefn(dav1d_cdef_brow)(Dav1dTaskContext *const tc,
98
                             pixel *const p[3],
99
                             const Av1Filter *const lflvl,
100
                             const int by_start, const int by_end,
101
                             const int sbrow_start, const int sby)
102
383k
{
103
383k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
383k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
383k
    const Dav1dDSPContext *const dsp = f->dsp;
106
383k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
383k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
383k
    const int sbsz = 16;
109
383k
    const int sb64w = f->sb128w << 1;
110
383k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
383k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
383k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
383k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
383k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
383k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
383k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
383k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
383k
    const int have_tt = f->c->n_tc > 1;
119
383k
    const int sb128 = f->seq_hdr->sb128;
120
383k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
383k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
383k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
2.08M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
1.70M
        const int tf = tc->top_pre_cdef_toggle;
126
1.70M
        const int by_idx = (by & 30) >> 1;
127
1.70M
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
1.70M
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
1.45M
            edges & CDEF_HAVE_BOTTOM)
131
1.45M
        {
132
            // backup pre-filter data for next iteration
133
1.45M
            pixel *const cdef_top_bak[3] = {
134
1.45M
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
1.45M
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
1.45M
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
1.45M
            };
138
1.45M
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
1.45M
        }
140
141
1.70M
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
1.70M
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
1.70M
        edges &= ~CDEF_HAVE_LEFT;
144
1.70M
        edges |= CDEF_HAVE_RIGHT;
145
1.70M
        enum Backup2x8Flags prev_flag = 0;
146
7.33M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
5.63M
            const int sb128x = sbx >> 1;
148
5.63M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
5.63M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
5.63M
            if (cdef_idx == -1 ||
151
3.63M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
1.74M
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
3.62M
            {
154
3.62M
                prev_flag = 0;
155
3.62M
                goto next_sb;
156
3.62M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
2.00M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
2.00M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
2.00M
                                                    noskip_row[0][0];
162
163
2.00M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
2.00M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
2.00M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
2.00M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
2.00M
            int y_sec_lvl = y_lvl & 3;
169
2.00M
            y_sec_lvl += y_sec_lvl == 3;
170
2.00M
            y_sec_lvl <<= bitdepth_min_8;
171
172
2.00M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
2.00M
            int uv_sec_lvl = uv_lvl & 3;
174
2.00M
            uv_sec_lvl += uv_sec_lvl == 3;
175
2.00M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
2.00M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
7.91M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
5.91M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
5.98M
            {
181
5.98M
                if (bx + 2 >= f->bw) edges &= ~CDEF_HAVE_RIGHT;
182
183
                // check if this 8x8 block had any coded coefficients; if not,
184
                // go to the next block
185
5.98M
                const uint32_t bx_mask = 3U << (bx & 30);
186
5.98M
                if (!(noskip_mask & bx_mask)) {
187
640k
                    prev_flag = 0;
188
640k
                    goto next_b;
189
640k
                }
190
5.34M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
5.34M
                prev_flag = flag;
192
5.34M
                if (do_left && edges & CDEF_HAVE_LEFT) {
193
                    // we didn't backup the prefilter data because it wasn't
194
                    // there, so do it here instead
195
83.5k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
83.5k
                }
197
5.34M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
4.75M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
4.75M
                }
201
202
5.34M
                int dir;
203
5.34M
                unsigned variance;
204
5.34M
                if (y_pri_lvl || uv_pri_lvl)
205
5.11M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
5.11M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
5.34M
                const pixel *top, *bot;
209
5.34M
                ptrdiff_t offset;
210
211
5.34M
                if (!have_tt) goto st_y;
212
5.34M
                if (sbrow_start && by == by_start) {
213
340k
                    if (resize) {
214
115k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
115k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
224k
                    } else {
217
224k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
224k
                        top = &f->lf.lr_lpf_line[0][offset];
219
224k
                    }
220
340k
                    bot = bptrs[0] + 8 * y_stride;
221
5.22M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
713k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
713k
                    if (resize) {
224
243k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
243k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
469k
                    } else {
227
469k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
469k
                        offset = line * y_stride + bx * 4;
229
469k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
469k
                    }
231
4.29M
                } else {
232
4.52M
            st_y:;
233
4.52M
                    offset = sby * 4 * y_stride;
234
4.52M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
4.52M
                    bot = bptrs[0] + 8 * y_stride;
236
4.52M
                }
237
5.57M
                if (y_pri_lvl) {
238
4.59M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
4.59M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
3.76M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
3.76M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
3.76M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
4.59M
                } else if (y_sec_lvl)
244
550k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
550k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
550k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
5.57M
                if (!uv_lvl) goto skip_uv;
249
4.46M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
4.46M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
12.6M
                for (int pl = 1; pl <= 2; pl++) {
253
8.07M
                    if (!have_tt) goto st_uv;
254
8.07M
                    if (sbrow_start && by == by_start) {
255
494k
                        if (resize) {
256
174k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
174k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
320k
                        } else {
259
320k
                            const int line = sby * (4 << sb128) - 4;
260
320k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
320k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
320k
                        }
263
494k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
7.64M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
1.08M
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
1.08M
                                                     (bx * 4 >> ss_hor);
267
1.08M
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
1.08M
                        if (resize) {
269
364k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
364k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
715k
                        } else {
272
715k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
715k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
715k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
715k
                        }
276
6.50M
                    } else {
277
6.63M
                st_uv:;
278
6.63M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
6.63M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
6.63M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
6.63M
                    }
282
8.20M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
8.20M
                                         lr_bak[bit][pl], top, bot,
284
8.20M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
8.20M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
8.20M
                }
287
288
5.26M
            skip_uv:
289
5.26M
                bit ^= 1;
290
291
5.91M
            next_b:
292
5.91M
                bptrs[0] += 8;
293
5.91M
                bptrs[1] += 8 >> ss_hor;
294
5.91M
                bptrs[2] += 8 >> ss_hor;
295
5.91M
            }
296
297
5.63M
        next_sb:
298
5.63M
            iptrs[0] += sbsz * 4;
299
5.63M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
5.63M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
5.63M
        }
302
303
1.69M
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
1.69M
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
1.69M
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
1.69M
        tc->top_pre_cdef_toggle ^= 1;
307
1.69M
    }
308
383k
}
dav1d_cdef_brow_8bpc
Line
Count
Source
102
219k
{
103
219k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
18.4E
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
219k
    const Dav1dDSPContext *const dsp = f->dsp;
106
219k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
219k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
219k
    const int sbsz = 16;
109
219k
    const int sb64w = f->sb128w << 1;
110
219k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
219k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
219k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
219k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
219k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
219k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
219k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
219k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
219k
    const int have_tt = f->c->n_tc > 1;
119
219k
    const int sb128 = f->seq_hdr->sb128;
120
219k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
219k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
219k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
1.21M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
1.00M
        const int tf = tc->top_pre_cdef_toggle;
126
1.00M
        const int by_idx = (by & 30) >> 1;
127
1.00M
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
1.00M
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
866k
            edges & CDEF_HAVE_BOTTOM)
131
866k
        {
132
            // backup pre-filter data for next iteration
133
866k
            pixel *const cdef_top_bak[3] = {
134
866k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
866k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
866k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
866k
            };
138
866k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
866k
        }
140
141
1.00M
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
1.00M
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
1.00M
        edges &= ~CDEF_HAVE_LEFT;
144
1.00M
        edges |= CDEF_HAVE_RIGHT;
145
1.00M
        enum Backup2x8Flags prev_flag = 0;
146
4.17M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
3.17M
            const int sb128x = sbx >> 1;
148
3.17M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
3.17M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
3.17M
            if (cdef_idx == -1 ||
151
2.04M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
997k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
2.06M
            {
154
2.06M
                prev_flag = 0;
155
2.06M
                goto next_sb;
156
2.06M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
1.10M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
1.10M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
1.10M
                                                    noskip_row[0][0];
162
163
1.10M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
1.10M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
1.10M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
1.10M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
1.10M
            int y_sec_lvl = y_lvl & 3;
169
1.10M
            y_sec_lvl += y_sec_lvl == 3;
170
1.10M
            y_sec_lvl <<= bitdepth_min_8;
171
172
1.10M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
1.10M
            int uv_sec_lvl = uv_lvl & 3;
174
1.10M
            uv_sec_lvl += uv_sec_lvl == 3;
175
1.10M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
1.10M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
3.92M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
2.81M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
2.87M
            {
181
2.87M
                if (bx + 2 >= f->bw) edges &= ~CDEF_HAVE_RIGHT;
182
183
                // check if this 8x8 block had any coded coefficients; if not,
184
                // go to the next block
185
2.87M
                const uint32_t bx_mask = 3U << (bx & 30);
186
2.87M
                if (!(noskip_mask & bx_mask)) {
187
357k
                    prev_flag = 0;
188
357k
                    goto next_b;
189
357k
                }
190
2.51M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
2.51M
                prev_flag = flag;
192
2.51M
                if (do_left && edges & CDEF_HAVE_LEFT) {
193
                    // we didn't backup the prefilter data because it wasn't
194
                    // there, so do it here instead
195
37.7k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
37.7k
                }
197
2.51M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.22M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.22M
                }
201
202
2.51M
                int dir;
203
2.51M
                unsigned variance;
204
2.51M
                if (y_pri_lvl || uv_pri_lvl)
205
2.37M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
2.37M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
2.51M
                const pixel *top, *bot;
209
2.51M
                ptrdiff_t offset;
210
211
2.51M
                if (!have_tt) goto st_y;
212
2.51M
                if (sbrow_start && by == by_start) {
213
185k
                    if (resize) {
214
23.3k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
23.3k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
162k
                    } else {
217
162k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
162k
                        top = &f->lf.lr_lpf_line[0][offset];
219
162k
                    }
220
185k
                    bot = bptrs[0] + 8 * y_stride;
221
2.47M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
343k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
343k
                    if (resize) {
224
67.1k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
67.1k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
276k
                    } else {
227
276k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
276k
                        offset = line * y_stride + bx * 4;
229
276k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
276k
                    }
231
1.98M
                } else {
232
2.14M
            st_y:;
233
2.14M
                    offset = sby * 4 * y_stride;
234
2.14M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
2.14M
                    bot = bptrs[0] + 8 * y_stride;
236
2.14M
                }
237
2.67M
                if (y_pri_lvl) {
238
2.10M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
2.10M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
1.56M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
1.56M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
1.56M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
2.10M
                } else if (y_sec_lvl)
244
287k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
287k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
287k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
2.67M
                if (!uv_lvl) goto skip_uv;
249
2.23M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
2.23M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
6.21M
                for (int pl = 1; pl <= 2; pl++) {
253
3.88M
                    if (!have_tt) goto st_uv;
254
3.88M
                    if (sbrow_start && by == by_start) {
255
260k
                        if (resize) {
256
40.9k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
40.9k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
219k
                        } else {
259
219k
                            const int line = sby * (4 << sb128) - 4;
260
219k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
219k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
219k
                        }
263
260k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
3.66M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
523k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
523k
                                                     (bx * 4 >> ss_hor);
267
523k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
523k
                        if (resize) {
269
101k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
101k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
422k
                        } else {
272
422k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
422k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
422k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
422k
                        }
276
3.09M
                    } else {
277
3.19M
                st_uv:;
278
3.19M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
3.19M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
3.19M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
3.19M
                    }
282
3.97M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
3.97M
                                         lr_bak[bit][pl], top, bot,
284
3.97M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
3.97M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
3.97M
                }
287
288
2.44M
            skip_uv:
289
2.44M
                bit ^= 1;
290
291
2.81M
            next_b:
292
2.81M
                bptrs[0] += 8;
293
2.81M
                bptrs[1] += 8 >> ss_hor;
294
2.81M
                bptrs[2] += 8 >> ss_hor;
295
2.81M
            }
296
297
3.17M
        next_sb:
298
3.17M
            iptrs[0] += sbsz * 4;
299
3.17M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
3.17M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
3.17M
        }
302
303
999k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
999k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
999k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
999k
        tc->top_pre_cdef_toggle ^= 1;
307
999k
    }
308
219k
}
dav1d_cdef_brow_16bpc
Line
Count
Source
102
164k
{
103
164k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
164k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
164k
    const Dav1dDSPContext *const dsp = f->dsp;
106
164k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
164k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
164k
    const int sbsz = 16;
109
164k
    const int sb64w = f->sb128w << 1;
110
164k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
164k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
164k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
164k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
164k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
164k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
164k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
164k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
164k
    const int have_tt = f->c->n_tc > 1;
119
164k
    const int sb128 = f->seq_hdr->sb128;
120
164k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
164k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
164k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
863k
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
699k
        const int tf = tc->top_pre_cdef_toggle;
126
699k
        const int by_idx = (by & 30) >> 1;
127
699k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
700k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
585k
            edges & CDEF_HAVE_BOTTOM)
131
585k
        {
132
            // backup pre-filter data for next iteration
133
585k
            pixel *const cdef_top_bak[3] = {
134
585k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
585k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
585k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
585k
            };
138
585k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
585k
        }
140
141
699k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
699k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
699k
        edges &= ~CDEF_HAVE_LEFT;
144
699k
        edges |= CDEF_HAVE_RIGHT;
145
699k
        enum Backup2x8Flags prev_flag = 0;
146
3.15M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
2.45M
            const int sb128x = sbx >> 1;
148
2.45M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
2.45M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
2.45M
            if (cdef_idx == -1 ||
151
1.59M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
744k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.55M
            {
154
1.55M
                prev_flag = 0;
155
1.55M
                goto next_sb;
156
1.55M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
898k
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
898k
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
898k
                                                    noskip_row[0][0];
162
163
898k
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
898k
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
898k
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
898k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
898k
            int y_sec_lvl = y_lvl & 3;
169
898k
            y_sec_lvl += y_sec_lvl == 3;
170
898k
            y_sec_lvl <<= bitdepth_min_8;
171
172
898k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
898k
            int uv_sec_lvl = uv_lvl & 3;
174
898k
            uv_sec_lvl += uv_sec_lvl == 3;
175
898k
            uv_sec_lvl <<= bitdepth_min_8;
176
177
898k
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
3.99M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
3.09M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
3.11M
            {
181
3.11M
                if (bx + 2 >= f->bw) edges &= ~CDEF_HAVE_RIGHT;
182
183
                // check if this 8x8 block had any coded coefficients; if not,
184
                // go to the next block
185
3.11M
                const uint32_t bx_mask = 3U << (bx & 30);
186
3.11M
                if (!(noskip_mask & bx_mask)) {
187
283k
                    prev_flag = 0;
188
283k
                    goto next_b;
189
283k
                }
190
2.83M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
2.83M
                prev_flag = flag;
192
2.83M
                if (do_left && edges & CDEF_HAVE_LEFT) {
193
                    // we didn't backup the prefilter data because it wasn't
194
                    // there, so do it here instead
195
45.8k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
45.8k
                }
197
2.83M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.52M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.52M
                }
201
202
2.83M
                int dir;
203
2.83M
                unsigned variance;
204
2.83M
                if (y_pri_lvl || uv_pri_lvl)
205
2.73M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
2.73M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
2.83M
                const pixel *top, *bot;
209
2.83M
                ptrdiff_t offset;
210
211
2.83M
                if (!have_tt) goto st_y;
212
2.83M
                if (sbrow_start && by == by_start) {
213
154k
                    if (resize) {
214
91.9k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
91.9k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
91.9k
                    } else {
217
62.8k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
62.8k
                        top = &f->lf.lr_lpf_line[0][offset];
219
62.8k
                    }
220
154k
                    bot = bptrs[0] + 8 * y_stride;
221
2.74M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
369k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
369k
                    if (resize) {
224
176k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
176k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
192k
                    } else {
227
192k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
192k
                        offset = line * y_stride + bx * 4;
229
192k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
192k
                    }
231
2.30M
                } else {
232
2.37M
            st_y:;
233
2.37M
                    offset = sby * 4 * y_stride;
234
2.37M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
2.37M
                    bot = bptrs[0] + 8 * y_stride;
236
2.37M
                }
237
2.90M
                if (y_pri_lvl) {
238
2.49M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
2.49M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
2.19M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
2.19M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
2.19M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
2.49M
                } else if (y_sec_lvl)
244
262k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
262k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
262k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
2.90M
                if (!uv_lvl) goto skip_uv;
249
2.22M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
2.22M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
6.45M
                for (int pl = 1; pl <= 2; pl++) {
253
4.19M
                    if (!have_tt) goto st_uv;
254
4.19M
                    if (sbrow_start && by == by_start) {
255
234k
                        if (resize) {
256
133k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
133k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
133k
                        } else {
259
101k
                            const int line = sby * (4 << sb128) - 4;
260
101k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
101k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
101k
                        }
263
234k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
3.97M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
556k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
556k
                                                     (bx * 4 >> ss_hor);
267
556k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
556k
                        if (resize) {
269
263k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
263k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
293k
                        } else {
272
293k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
293k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
293k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
293k
                        }
276
3.40M
                    } else {
277
3.43M
                st_uv:;
278
3.43M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
3.43M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
3.43M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
3.43M
                    }
282
4.23M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
4.23M
                                         lr_bak[bit][pl], top, bot,
284
4.23M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
4.23M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
4.23M
                }
287
288
2.81M
            skip_uv:
289
2.81M
                bit ^= 1;
290
291
3.09M
            next_b:
292
3.09M
                bptrs[0] += 8;
293
3.09M
                bptrs[1] += 8 >> ss_hor;
294
3.09M
                bptrs[2] += 8 >> ss_hor;
295
3.09M
            }
296
297
2.45M
        next_sb:
298
2.45M
            iptrs[0] += sbsz * 4;
299
2.45M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
2.45M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
2.45M
        }
302
303
699k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
699k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
699k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
699k
        tc->top_pre_cdef_toggle ^= 1;
307
699k
    }
308
164k
}