Coverage Report

Created: 2026-05-30 06:10

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
856k
{
45
856k
    const ptrdiff_t y_stride = PXSTRIDE(stride[0]);
46
856k
    if (y_stride < 0)
47
0
        pixel_copy(dst[0] + y_stride, src[0] + 7 * y_stride, -2 * y_stride);
48
856k
    else
49
856k
        pixel_copy(dst[0], src[0] + 6 * y_stride, 2 * y_stride);
50
51
856k
    if (layout != DAV1D_PIXEL_LAYOUT_I400) {
52
656k
        const ptrdiff_t uv_stride = PXSTRIDE(stride[1]);
53
656k
        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
656k
        } else {
58
656k
            const int uv_off = layout == DAV1D_PIXEL_LAYOUT_I420 ? 2 : 6;
59
656k
            pixel_copy(dst[1], src[1] + uv_off * uv_stride, 2 * uv_stride);
60
656k
            pixel_copy(dst[2], src[2] + uv_off * uv_stride, 2 * uv_stride);
61
656k
        }
62
656k
    }
63
856k
}
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
3.96M
{
71
3.96M
    ptrdiff_t y_off = 0;
72
3.96M
    if (flag & BACKUP_2X8_Y) {
73
34.2M
        for (int y = 0; y < 8; y++, y_off += PXSTRIDE(src_stride[0]))
74
30.4M
            pixel_copy(dst[0][y], &src[0][y_off + x_off - 2], 2);
75
3.81M
    }
76
77
3.96M
    if (layout == DAV1D_PIXEL_LAYOUT_I400 || !(flag & BACKUP_2X8_UV))
78
1.04M
        return;
79
80
2.92M
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
81
2.92M
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
82
83
2.92M
    x_off >>= ss_hor;
84
2.92M
    y_off = 0;
85
20.1M
    for (int y = 0; y < (8 >> ss_ver); y++, y_off += PXSTRIDE(src_stride[1])) {
86
17.2M
        pixel_copy(dst[1][y], &src[1][y_off + x_off - 2], 2);
87
17.2M
        pixel_copy(dst[2][y], &src[2][y_off + x_off - 2], 2);
88
17.2M
    }
89
2.92M
}
90
91
3.53M
static int adjust_strength(const int strength, const unsigned var) {
92
3.53M
    if (!var) return 0;
93
1.69M
    const int i = var >> 6 ? imin(ulog2(var >> 6), 12) : 0;
94
1.69M
    return (strength * (4 + i) + 8) >> 4;
95
3.53M
}
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
194k
{
103
194k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
194k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
194k
    const Dav1dDSPContext *const dsp = f->dsp;
106
194k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
194k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
194k
    const int sbsz = 16;
109
194k
    const int sb64w = f->sb128w << 1;
110
194k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
194k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
194k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
194k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
194k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
194k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
194k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
194k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
194k
    const int have_tt = f->c->n_tc > 1;
119
194k
    const int sb128 = f->seq_hdr->sb128;
120
194k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
194k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
194k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
1.17M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
980k
        const int tf = tc->top_pre_cdef_toggle;
126
980k
        const int by_idx = (by & 30) >> 1;
127
980k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
980k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
856k
            edges & CDEF_HAVE_BOTTOM)
131
856k
        {
132
            // backup pre-filter data for next iteration
133
856k
            pixel *const cdef_top_bak[3] = {
134
856k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
856k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
856k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
856k
            };
138
856k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
856k
        }
140
141
980k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
980k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
980k
        edges &= ~CDEF_HAVE_LEFT;
144
980k
        edges |= CDEF_HAVE_RIGHT;
145
980k
        enum Backup2x8Flags prev_flag = 0;
146
4.02M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
3.04M
            const int sb128x = sbx >> 1;
148
3.04M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
3.04M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
3.04M
            if (cdef_idx == -1 ||
151
1.64M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
662k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
2.00M
            {
154
2.00M
                prev_flag = 0;
155
2.00M
                goto next_sb;
156
2.00M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
1.03M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
1.03M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
1.03M
                                                    noskip_row[0][0];
162
163
1.03M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
1.03M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
1.03M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
1.03M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
1.03M
            int y_sec_lvl = y_lvl & 3;
169
1.03M
            y_sec_lvl += y_sec_lvl == 3;
170
1.03M
            y_sec_lvl <<= bitdepth_min_8;
171
172
1.03M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
1.03M
            int uv_sec_lvl = uv_lvl & 3;
174
1.03M
            uv_sec_lvl += uv_sec_lvl == 3;
175
1.03M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
1.03M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
5.61M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
4.57M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
4.58M
            {
181
4.58M
                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
4.58M
                const uint32_t bx_mask = 3U << (bx & 30);
186
4.58M
                if (!(noskip_mask & bx_mask)) {
187
340k
                    prev_flag = 0;
188
340k
                    goto next_b;
189
340k
                }
190
4.24M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
4.24M
                prev_flag = flag;
192
4.24M
                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
52.3k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
52.3k
                }
197
4.24M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
3.91M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
3.91M
                }
201
202
4.24M
                int dir;
203
4.24M
                unsigned variance;
204
4.24M
                if (y_pri_lvl || uv_pri_lvl)
205
4.04M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
4.04M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
4.24M
                const pixel *top, *bot;
209
4.24M
                ptrdiff_t offset;
210
211
4.24M
                if (!have_tt) goto st_y;
212
4.24M
                if (sbrow_start && by == by_start) {
213
276k
                    if (resize) {
214
98.2k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
98.2k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
177k
                    } else {
217
177k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
177k
                        top = &f->lf.lr_lpf_line[0][offset];
219
177k
                    }
220
276k
                    bot = bptrs[0] + 8 * y_stride;
221
4.04M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
562k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
562k
                    if (resize) {
224
230k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
230k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
331k
                    } else {
227
331k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
331k
                        offset = line * y_stride + bx * 4;
229
331k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
331k
                    }
231
3.40M
                } else {
232
3.48M
            st_y:;
233
3.48M
                    offset = sby * 4 * y_stride;
234
3.48M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
3.48M
                    bot = bptrs[0] + 8 * y_stride;
236
3.48M
                }
237
4.32M
                if (y_pri_lvl) {
238
3.53M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
3.53M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
2.57M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
2.57M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
2.57M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
3.53M
                } else if (y_sec_lvl)
244
604k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
604k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
604k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
4.32M
                if (!uv_lvl) goto skip_uv;
249
3.17M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
3.17M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
9.27M
                for (int pl = 1; pl <= 2; pl++) {
253
6.05M
                    if (!have_tt) goto st_uv;
254
6.05M
                    if (sbrow_start && by == by_start) {
255
385k
                        if (resize) {
256
100k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
100k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
284k
                        } else {
259
284k
                            const int line = sby * (4 << sb128) - 4;
260
284k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
284k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
284k
                        }
263
385k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
5.68M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
851k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
851k
                                                     (bx * 4 >> ss_hor);
267
851k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
851k
                        if (resize) {
269
302k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
302k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
549k
                        } else {
272
549k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
549k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
549k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
549k
                        }
276
4.81M
                    } else {
277
4.86M
                st_uv:;
278
4.86M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
4.86M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
4.86M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
4.86M
                    }
282
6.10M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
6.10M
                                         lr_bak[bit][pl], top, bot,
284
6.10M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
6.10M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
6.10M
                }
287
288
4.23M
            skip_uv:
289
4.23M
                bit ^= 1;
290
291
4.57M
            next_b:
292
4.57M
                bptrs[0] += 8;
293
4.57M
                bptrs[1] += 8 >> ss_hor;
294
4.57M
                bptrs[2] += 8 >> ss_hor;
295
4.57M
            }
296
297
3.04M
        next_sb:
298
3.04M
            iptrs[0] += sbsz * 4;
299
3.04M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
3.04M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
3.04M
        }
302
303
979k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
979k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
979k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
979k
        tc->top_pre_cdef_toggle ^= 1;
307
979k
    }
308
194k
}
dav1d_cdef_brow_8bpc
Line
Count
Source
102
87.9k
{
103
87.9k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
87.9k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
87.9k
    const Dav1dDSPContext *const dsp = f->dsp;
106
87.9k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
87.9k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
87.9k
    const int sbsz = 16;
109
87.9k
    const int sb64w = f->sb128w << 1;
110
87.9k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
87.9k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
87.9k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
87.9k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
87.9k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
87.9k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
87.9k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
87.9k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
87.9k
    const int have_tt = f->c->n_tc > 1;
119
87.9k
    const int sb128 = f->seq_hdr->sb128;
120
87.9k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
87.9k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
87.9k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
549k
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
462k
        const int tf = tc->top_pre_cdef_toggle;
126
462k
        const int by_idx = (by & 30) >> 1;
127
462k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
462k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
401k
            edges & CDEF_HAVE_BOTTOM)
131
401k
        {
132
            // backup pre-filter data for next iteration
133
401k
            pixel *const cdef_top_bak[3] = {
134
401k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
401k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
401k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
401k
            };
138
401k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
401k
        }
140
141
462k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
462k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
462k
        edges &= ~CDEF_HAVE_LEFT;
144
462k
        edges |= CDEF_HAVE_RIGHT;
145
462k
        enum Backup2x8Flags prev_flag = 0;
146
1.93M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
1.46M
            const int sb128x = sbx >> 1;
148
1.46M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
1.46M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
1.46M
            if (cdef_idx == -1 ||
151
784k
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
223k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
878k
            {
154
878k
                prev_flag = 0;
155
878k
                goto next_sb;
156
878k
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
591k
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
591k
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
591k
                                                    noskip_row[0][0];
162
163
591k
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
591k
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
591k
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
591k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
591k
            int y_sec_lvl = y_lvl & 3;
169
591k
            y_sec_lvl += y_sec_lvl == 3;
170
591k
            y_sec_lvl <<= bitdepth_min_8;
171
172
591k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
591k
            int uv_sec_lvl = uv_lvl & 3;
174
591k
            uv_sec_lvl += uv_sec_lvl == 3;
175
591k
            uv_sec_lvl <<= bitdepth_min_8;
176
177
591k
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
3.12M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
2.53M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
2.54M
            {
181
2.54M
                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.54M
                const uint32_t bx_mask = 3U << (bx & 30);
186
2.54M
                if (!(noskip_mask & bx_mask)) {
187
186k
                    prev_flag = 0;
188
186k
                    goto next_b;
189
186k
                }
190
2.35M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
2.35M
                prev_flag = flag;
192
2.35M
                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
25.6k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
25.6k
                }
197
2.35M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.14M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.14M
                }
201
202
2.35M
                int dir;
203
2.35M
                unsigned variance;
204
2.35M
                if (y_pri_lvl || uv_pri_lvl)
205
2.18M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
2.18M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
2.35M
                const pixel *top, *bot;
209
2.35M
                ptrdiff_t offset;
210
211
2.35M
                if (!have_tt) goto st_y;
212
2.35M
                if (sbrow_start && by == by_start) {
213
149k
                    if (resize) {
214
54.9k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
54.9k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
94.7k
                    } else {
217
94.7k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
94.7k
                        top = &f->lf.lr_lpf_line[0][offset];
219
94.7k
                    }
220
149k
                    bot = bptrs[0] + 8 * y_stride;
221
2.25M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
295k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
295k
                    if (resize) {
224
101k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
101k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
194k
                    } else {
227
194k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
194k
                        offset = line * y_stride + bx * 4;
229
194k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
194k
                    }
231
1.91M
                } else {
232
1.95M
            st_y:;
233
1.95M
                    offset = sby * 4 * y_stride;
234
1.95M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
1.95M
                    bot = bptrs[0] + 8 * y_stride;
236
1.95M
                }
237
2.40M
                if (y_pri_lvl) {
238
2.02M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
2.02M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
1.24M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
1.24M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
1.24M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
2.02M
                } else if (y_sec_lvl)
244
277k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
277k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
277k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
2.40M
                if (!uv_lvl) goto skip_uv;
249
1.48M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
1.48M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
4.32M
                for (int pl = 1; pl <= 2; pl++) {
253
2.81M
                    if (!have_tt) goto st_uv;
254
2.81M
                    if (sbrow_start && by == by_start) {
255
172k
                        if (resize) {
256
26.3k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
26.3k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
145k
                        } else {
259
145k
                            const int line = sby * (4 << sb128) - 4;
260
145k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
145k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
145k
                        }
263
172k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
2.65M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
413k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
413k
                                                     (bx * 4 >> ss_hor);
267
413k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
413k
                        if (resize) {
269
92.5k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
92.5k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
321k
                        } else {
272
321k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
321k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
321k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
321k
                        }
276
2.23M
                    } else {
277
2.25M
                st_uv:;
278
2.25M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
2.25M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
2.25M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
2.25M
                    }
282
2.84M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
2.84M
                                         lr_bak[bit][pl], top, bot,
284
2.84M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
2.84M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
2.84M
                }
287
288
2.35M
            skip_uv:
289
2.35M
                bit ^= 1;
290
291
2.53M
            next_b:
292
2.53M
                bptrs[0] += 8;
293
2.53M
                bptrs[1] += 8 >> ss_hor;
294
2.53M
                bptrs[2] += 8 >> ss_hor;
295
2.53M
            }
296
297
1.46M
        next_sb:
298
1.46M
            iptrs[0] += sbsz * 4;
299
1.46M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
1.46M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
1.46M
        }
302
303
461k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
461k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
461k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
461k
        tc->top_pre_cdef_toggle ^= 1;
307
461k
    }
308
87.9k
}
dav1d_cdef_brow_16bpc
Line
Count
Source
102
106k
{
103
106k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
106k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
106k
    const Dav1dDSPContext *const dsp = f->dsp;
106
106k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
106k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
106k
    const int sbsz = 16;
109
106k
    const int sb64w = f->sb128w << 1;
110
106k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
106k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
106k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
106k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
106k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
106k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
106k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
106k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
106k
    const int have_tt = f->c->n_tc > 1;
119
106k
    const int sb128 = f->seq_hdr->sb128;
120
106k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
106k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
106k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
623k
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
518k
        const int tf = tc->top_pre_cdef_toggle;
126
518k
        const int by_idx = (by & 30) >> 1;
127
518k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
518k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
454k
            edges & CDEF_HAVE_BOTTOM)
131
454k
        {
132
            // backup pre-filter data for next iteration
133
454k
            pixel *const cdef_top_bak[3] = {
134
454k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
454k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
454k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
454k
            };
138
454k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
454k
        }
140
141
518k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
518k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
518k
        edges &= ~CDEF_HAVE_LEFT;
144
518k
        edges |= CDEF_HAVE_RIGHT;
145
518k
        enum Backup2x8Flags prev_flag = 0;
146
2.09M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
1.57M
            const int sb128x = sbx >> 1;
148
1.57M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
1.57M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
1.57M
            if (cdef_idx == -1 ||
151
857k
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
439k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.12M
            {
154
1.12M
                prev_flag = 0;
155
1.12M
                goto next_sb;
156
1.12M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
447k
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
447k
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
447k
                                                    noskip_row[0][0];
162
163
447k
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
447k
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
447k
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
447k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
447k
            int y_sec_lvl = y_lvl & 3;
169
447k
            y_sec_lvl += y_sec_lvl == 3;
170
447k
            y_sec_lvl <<= bitdepth_min_8;
171
172
447k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
447k
            int uv_sec_lvl = uv_lvl & 3;
174
447k
            uv_sec_lvl += uv_sec_lvl == 3;
175
447k
            uv_sec_lvl <<= bitdepth_min_8;
176
177
447k
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
2.48M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
2.03M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
2.04M
            {
181
2.04M
                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.04M
                const uint32_t bx_mask = 3U << (bx & 30);
186
2.04M
                if (!(noskip_mask & bx_mask)) {
187
154k
                    prev_flag = 0;
188
154k
                    goto next_b;
189
154k
                }
190
1.88M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
1.88M
                prev_flag = flag;
192
1.88M
                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
26.7k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
26.7k
                }
197
1.88M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
1.76M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
1.76M
                }
201
202
1.88M
                int dir;
203
1.88M
                unsigned variance;
204
1.88M
                if (y_pri_lvl || uv_pri_lvl)
205
1.86M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
1.86M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
1.88M
                const pixel *top, *bot;
209
1.88M
                ptrdiff_t offset;
210
211
1.88M
                if (!have_tt) goto st_y;
212
1.88M
                if (sbrow_start && by == by_start) {
213
126k
                    if (resize) {
214
43.3k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
43.3k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
82.9k
                    } else {
217
82.9k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
82.9k
                        top = &f->lf.lr_lpf_line[0][offset];
219
82.9k
                    }
220
126k
                    bot = bptrs[0] + 8 * y_stride;
221
1.79M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
266k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
266k
                    if (resize) {
224
128k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
128k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
137k
                    } else {
227
137k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
137k
                        offset = line * y_stride + bx * 4;
229
137k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
137k
                    }
231
1.49M
                } else {
232
1.52M
            st_y:;
233
1.52M
                    offset = sby * 4 * y_stride;
234
1.52M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
1.52M
                    bot = bptrs[0] + 8 * y_stride;
236
1.52M
                }
237
1.91M
                if (y_pri_lvl) {
238
1.51M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
1.51M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
1.32M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
1.32M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
1.32M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
1.51M
                } else if (y_sec_lvl)
244
327k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
327k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
327k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
1.91M
                if (!uv_lvl) goto skip_uv;
249
1.68M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
1.68M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
4.94M
                for (int pl = 1; pl <= 2; pl++) {
253
3.23M
                    if (!have_tt) goto st_uv;
254
3.23M
                    if (sbrow_start && by == by_start) {
255
213k
                        if (resize) {
256
74.3k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
74.3k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
139k
                        } else {
259
139k
                            const int line = sby * (4 << sb128) - 4;
260
139k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
139k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
139k
                        }
263
213k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
3.03M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
437k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
437k
                                                     (bx * 4 >> ss_hor);
267
437k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
437k
                        if (resize) {
269
209k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
209k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
228k
                        } else {
272
228k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
228k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
228k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
228k
                        }
276
2.58M
                    } else {
277
2.61M
                st_uv:;
278
2.61M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
2.61M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
2.61M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
2.61M
                    }
282
3.26M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
3.26M
                                         lr_bak[bit][pl], top, bot,
284
3.26M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
3.26M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
3.26M
                }
287
288
1.88M
            skip_uv:
289
1.88M
                bit ^= 1;
290
291
2.03M
            next_b:
292
2.03M
                bptrs[0] += 8;
293
2.03M
                bptrs[1] += 8 >> ss_hor;
294
2.03M
                bptrs[2] += 8 >> ss_hor;
295
2.03M
            }
296
297
1.57M
        next_sb:
298
1.57M
            iptrs[0] += sbsz * 4;
299
1.57M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
1.57M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
1.57M
        }
302
303
517k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
517k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
517k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
517k
        tc->top_pre_cdef_toggle ^= 1;
307
517k
    }
308
106k
}