Coverage Report

Created: 2026-08-13 07:23

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
794k
{
45
794k
    const ptrdiff_t y_stride = PXSTRIDE(stride[0]);
46
794k
    if (y_stride < 0)
47
0
        pixel_copy(dst[0] + y_stride, src[0] + 7 * y_stride, -2 * y_stride);
48
794k
    else
49
794k
        pixel_copy(dst[0], src[0] + 6 * y_stride, 2 * y_stride);
50
51
794k
    if (layout != DAV1D_PIXEL_LAYOUT_I400) {
52
562k
        const ptrdiff_t uv_stride = PXSTRIDE(stride[1]);
53
562k
        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
562k
        } else {
58
562k
            const int uv_off = layout == DAV1D_PIXEL_LAYOUT_I420 ? 2 : 6;
59
562k
            pixel_copy(dst[1], src[1] + uv_off * uv_stride, 2 * uv_stride);
60
562k
            pixel_copy(dst[2], src[2] + uv_off * uv_stride, 2 * uv_stride);
61
562k
        }
62
562k
    }
63
794k
}
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.08M
{
71
3.08M
    ptrdiff_t y_off = 0;
72
3.08M
    if (flag & BACKUP_2X8_Y) {
73
26.1M
        for (int y = 0; y < 8; y++, y_off += PXSTRIDE(src_stride[0]))
74
23.2M
            pixel_copy(dst[0][y], &src[0][y_off + x_off - 2], 2);
75
2.90M
    }
76
77
3.08M
    if (layout == DAV1D_PIXEL_LAYOUT_I400 || !(flag & BACKUP_2X8_UV))
78
836k
        return;
79
80
2.24M
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
81
2.24M
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
82
83
2.24M
    x_off >>= ss_hor;
84
2.24M
    y_off = 0;
85
15.3M
    for (int y = 0; y < (8 >> ss_ver); y++, y_off += PXSTRIDE(src_stride[1])) {
86
13.1M
        pixel_copy(dst[1][y], &src[1][y_off + x_off - 2], 2);
87
13.1M
        pixel_copy(dst[2][y], &src[2][y_off + x_off - 2], 2);
88
13.1M
    }
89
2.24M
}
90
91
3.09M
static int adjust_strength(const int strength, const unsigned var) {
92
3.09M
    if (!var) return 0;
93
1.55M
    const int i = var >> 6 ? imin(ulog2(var >> 6), 12) : 0;
94
1.55M
    return (strength * (4 + i) + 8) >> 4;
95
3.09M
}
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
220k
{
103
220k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
220k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
220k
    const Dav1dDSPContext *const dsp = f->dsp;
106
220k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
220k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
220k
    const int sbsz = 16;
109
220k
    const int sb64w = f->sb128w << 1;
110
220k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
220k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
220k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
220k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
220k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
220k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
220k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
220k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
220k
    const int have_tt = f->c->n_tc > 1;
119
220k
    const int sb128 = f->seq_hdr->sb128;
120
220k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
220k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
220k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
1.16M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
949k
        const int tf = tc->top_pre_cdef_toggle;
126
949k
        const int by_idx = (by & 30) >> 1;
127
949k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
949k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
794k
            edges & CDEF_HAVE_BOTTOM)
131
794k
        {
132
            // backup pre-filter data for next iteration
133
794k
            pixel *const cdef_top_bak[3] = {
134
794k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
794k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
794k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
794k
            };
138
794k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
794k
        }
140
141
949k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
949k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
949k
        edges &= ~CDEF_HAVE_LEFT;
144
949k
        edges |= CDEF_HAVE_RIGHT;
145
949k
        enum Backup2x8Flags prev_flag = 0;
146
4.00M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
3.05M
            const int sb128x = sbx >> 1;
148
3.05M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
3.05M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
3.05M
            if (cdef_idx == -1 ||
151
1.74M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
718k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.96M
            {
154
1.96M
                prev_flag = 0;
155
1.96M
                goto next_sb;
156
1.96M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
1.08M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
1.08M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
1.08M
                                                    noskip_row[0][0];
162
163
1.08M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
1.08M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
1.08M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
1.08M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
1.08M
            int y_sec_lvl = y_lvl & 3;
169
1.08M
            y_sec_lvl += y_sec_lvl == 3;
170
1.08M
            y_sec_lvl <<= bitdepth_min_8;
171
172
1.08M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
1.08M
            int uv_sec_lvl = uv_lvl & 3;
174
1.08M
            uv_sec_lvl += uv_sec_lvl == 3;
175
1.08M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
1.08M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
4.93M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
3.85M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
3.87M
            {
181
3.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
3.87M
                const uint32_t bx_mask = 3U << (bx & 30);
186
3.87M
                if (!(noskip_mask & bx_mask)) {
187
493k
                    prev_flag = 0;
188
493k
                    goto next_b;
189
493k
                }
190
3.37M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
3.37M
                prev_flag = flag;
192
3.37M
                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
63.1k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
63.1k
                }
197
3.37M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
3.01M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
3.01M
                }
201
202
3.37M
                int dir;
203
3.37M
                unsigned variance;
204
3.37M
                if (y_pri_lvl || uv_pri_lvl)
205
3.33M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
3.33M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
3.37M
                const pixel *top, *bot;
209
3.37M
                ptrdiff_t offset;
210
211
3.37M
                if (!have_tt) goto st_y;
212
3.37M
                if (sbrow_start && by == by_start) {
213
213k
                    if (resize) {
214
89.3k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
89.3k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
124k
                    } else {
217
124k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
124k
                        top = &f->lf.lr_lpf_line[0][offset];
219
124k
                    }
220
213k
                    bot = bptrs[0] + 8 * y_stride;
221
3.24M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
444k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
444k
                    if (resize) {
224
187k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
187k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
256k
                    } else {
227
256k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
256k
                        offset = line * y_stride + bx * 4;
229
256k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
256k
                    }
231
2.72M
                } else {
232
2.79M
            st_y:;
233
2.79M
                    offset = sby * 4 * y_stride;
234
2.79M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
2.79M
                    bot = bptrs[0] + 8 * y_stride;
236
2.79M
                }
237
3.45M
                if (y_pri_lvl) {
238
3.09M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
3.09M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
2.49M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
2.49M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
2.49M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
3.09M
                } else if (y_sec_lvl)
244
162k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
162k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
162k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
3.45M
                if (!uv_lvl) goto skip_uv;
249
2.55M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
2.55M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
7.43M
                for (int pl = 1; pl <= 2; pl++) {
253
4.83M
                    if (!have_tt) goto st_uv;
254
4.83M
                    if (sbrow_start && by == by_start) {
255
303k
                        if (resize) {
256
110k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
110k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
192k
                        } else {
259
192k
                            const int line = sby * (4 << sb128) - 4;
260
192k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
192k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
192k
                        }
263
303k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
4.55M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
644k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
644k
                                                     (bx * 4 >> ss_hor);
267
644k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
644k
                        if (resize) {
269
243k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
243k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
400k
                        } else {
272
400k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
400k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
400k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
400k
                        }
276
3.89M
                    } else {
277
3.93M
                st_uv:;
278
3.93M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
3.93M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
3.93M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
3.93M
                    }
282
4.88M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
4.88M
                                         lr_bak[bit][pl], top, bot,
284
4.88M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
4.88M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
4.88M
                }
287
288
3.36M
            skip_uv:
289
3.36M
                bit ^= 1;
290
291
3.85M
            next_b:
292
3.85M
                bptrs[0] += 8;
293
3.85M
                bptrs[1] += 8 >> ss_hor;
294
3.85M
                bptrs[2] += 8 >> ss_hor;
295
3.85M
            }
296
297
3.05M
        next_sb:
298
3.05M
            iptrs[0] += sbsz * 4;
299
3.05M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
3.05M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
3.05M
        }
302
303
947k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
947k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
947k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
947k
        tc->top_pre_cdef_toggle ^= 1;
307
947k
    }
308
220k
}
dav1d_cdef_brow_8bpc
Line
Count
Source
102
112k
{
103
112k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
18.4E
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
112k
    const Dav1dDSPContext *const dsp = f->dsp;
106
112k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
112k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
112k
    const int sbsz = 16;
109
112k
    const int sb64w = f->sb128w << 1;
110
112k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
112k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
112k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
112k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
112k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
112k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
112k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
112k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
112k
    const int have_tt = f->c->n_tc > 1;
119
112k
    const int sb128 = f->seq_hdr->sb128;
120
112k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
112k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
112k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
617k
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
505k
        const int tf = tc->top_pre_cdef_toggle;
126
505k
        const int by_idx = (by & 30) >> 1;
127
505k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
505k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
429k
            edges & CDEF_HAVE_BOTTOM)
131
429k
        {
132
            // backup pre-filter data for next iteration
133
429k
            pixel *const cdef_top_bak[3] = {
134
429k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
429k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
429k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
429k
            };
138
429k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
429k
        }
140
141
505k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
505k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
505k
        edges &= ~CDEF_HAVE_LEFT;
144
505k
        edges |= CDEF_HAVE_RIGHT;
145
505k
        enum Backup2x8Flags prev_flag = 0;
146
2.20M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
1.70M
            const int sb128x = sbx >> 1;
148
1.70M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
1.70M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
1.70M
            if (cdef_idx == -1 ||
151
1.01M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
449k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.10M
            {
154
1.10M
                prev_flag = 0;
155
1.10M
                goto next_sb;
156
1.10M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
593k
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
593k
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
593k
                                                    noskip_row[0][0];
162
163
593k
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
593k
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
593k
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
593k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
593k
            int y_sec_lvl = y_lvl & 3;
169
593k
            y_sec_lvl += y_sec_lvl == 3;
170
593k
            y_sec_lvl <<= bitdepth_min_8;
171
172
593k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
593k
            int uv_sec_lvl = uv_lvl & 3;
174
593k
            uv_sec_lvl += uv_sec_lvl == 3;
175
593k
            uv_sec_lvl <<= bitdepth_min_8;
176
177
593k
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
2.68M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
2.09M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
2.10M
            {
181
2.10M
                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.10M
                const uint32_t bx_mask = 3U << (bx & 30);
186
2.10M
                if (!(noskip_mask & bx_mask)) {
187
374k
                    prev_flag = 0;
188
374k
                    goto next_b;
189
374k
                }
190
1.73M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
1.73M
                prev_flag = flag;
192
1.73M
                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
39.0k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
39.0k
                }
197
1.73M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
1.54M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
1.54M
                }
201
202
1.73M
                int dir;
203
1.73M
                unsigned variance;
204
1.73M
                if (y_pri_lvl || uv_pri_lvl)
205
1.72M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
1.72M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
1.73M
                const pixel *top, *bot;
209
1.73M
                ptrdiff_t offset;
210
211
1.73M
                if (!have_tt) goto st_y;
212
1.73M
                if (sbrow_start && by == by_start) {
213
119k
                    if (resize) {
214
30.1k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
30.1k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
89.7k
                    } else {
217
89.7k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
89.7k
                        top = &f->lf.lr_lpf_line[0][offset];
219
89.7k
                    }
220
119k
                    bot = bptrs[0] + 8 * y_stride;
221
1.66M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
226k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
226k
                    if (resize) {
224
69.1k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
69.1k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
157k
                    } else {
227
157k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
157k
                        offset = line * y_stride + bx * 4;
229
157k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
157k
                    }
231
1.38M
                } else {
232
1.44M
            st_y:;
233
1.44M
                    offset = sby * 4 * y_stride;
234
1.44M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
1.44M
                    bot = bptrs[0] + 8 * y_stride;
236
1.44M
                }
237
1.78M
                if (y_pri_lvl) {
238
1.63M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
1.63M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
1.20M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
1.20M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
1.20M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
1.63M
                } else if (y_sec_lvl)
244
51.4k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
51.4k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
51.4k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
1.78M
                if (!uv_lvl) goto skip_uv;
249
1.28M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
1.28M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
3.70M
                for (int pl = 1; pl <= 2; pl++) {
253
2.38M
                    if (!have_tt) goto st_uv;
254
2.38M
                    if (sbrow_start && by == by_start) {
255
160k
                        if (resize) {
256
26.0k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
26.0k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
134k
                        } else {
259
134k
                            const int line = sby * (4 << sb128) - 4;
260
134k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
134k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
134k
                        }
263
160k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
2.23M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
326k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
326k
                                                     (bx * 4 >> ss_hor);
267
326k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
326k
                        if (resize) {
269
74.7k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
74.7k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
251k
                        } else {
272
251k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
251k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
251k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
251k
                        }
276
1.90M
                    } else {
277
1.93M
                st_uv:;
278
1.93M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
1.93M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
1.93M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
1.93M
                    }
282
2.41M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
2.41M
                                         lr_bak[bit][pl], top, bot,
284
2.41M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
2.41M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
2.41M
                }
287
288
1.72M
            skip_uv:
289
1.72M
                bit ^= 1;
290
291
2.09M
            next_b:
292
2.09M
                bptrs[0] += 8;
293
2.09M
                bptrs[1] += 8 >> ss_hor;
294
2.09M
                bptrs[2] += 8 >> ss_hor;
295
2.09M
            }
296
297
1.70M
        next_sb:
298
1.70M
            iptrs[0] += sbsz * 4;
299
1.70M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
1.70M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
1.70M
        }
302
303
504k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
504k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
504k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
504k
        tc->top_pre_cdef_toggle ^= 1;
307
504k
    }
308
112k
}
dav1d_cdef_brow_16bpc
Line
Count
Source
102
107k
{
103
107k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
107k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
107k
    const Dav1dDSPContext *const dsp = f->dsp;
106
107k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
107k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
107k
    const int sbsz = 16;
109
107k
    const int sb64w = f->sb128w << 1;
110
107k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
107k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
107k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
107k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
107k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
107k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
107k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
107k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
107k
    const int have_tt = f->c->n_tc > 1;
119
107k
    const int sb128 = f->seq_hdr->sb128;
120
107k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
107k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
107k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
550k
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
443k
        const int tf = tc->top_pre_cdef_toggle;
126
443k
        const int by_idx = (by & 30) >> 1;
127
443k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
443k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
365k
            edges & CDEF_HAVE_BOTTOM)
131
365k
        {
132
            // backup pre-filter data for next iteration
133
365k
            pixel *const cdef_top_bak[3] = {
134
365k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
365k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
365k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
365k
            };
138
365k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
365k
        }
140
141
443k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
443k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
443k
        edges &= ~CDEF_HAVE_LEFT;
144
443k
        edges |= CDEF_HAVE_RIGHT;
145
443k
        enum Backup2x8Flags prev_flag = 0;
146
1.79M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
1.34M
            const int sb128x = sbx >> 1;
148
1.34M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
1.34M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
1.34M
            if (cdef_idx == -1 ||
151
727k
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
269k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
860k
            {
154
860k
                prev_flag = 0;
155
860k
                goto next_sb;
156
860k
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
489k
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
489k
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
489k
                                                    noskip_row[0][0];
162
163
489k
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
489k
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
489k
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
489k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
489k
            int y_sec_lvl = y_lvl & 3;
169
489k
            y_sec_lvl += y_sec_lvl == 3;
170
489k
            y_sec_lvl <<= bitdepth_min_8;
171
172
489k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
489k
            int uv_sec_lvl = uv_lvl & 3;
174
489k
            uv_sec_lvl += uv_sec_lvl == 3;
175
489k
            uv_sec_lvl <<= bitdepth_min_8;
176
177
489k
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
2.25M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
1.76M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
1.76M
            {
181
1.76M
                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
1.76M
                const uint32_t bx_mask = 3U << (bx & 30);
186
1.76M
                if (!(noskip_mask & bx_mask)) {
187
118k
                    prev_flag = 0;
188
118k
                    goto next_b;
189
118k
                }
190
1.64M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
1.64M
                prev_flag = flag;
192
1.64M
                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
24.0k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
24.0k
                }
197
1.64M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
1.47M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
1.47M
                }
201
202
1.64M
                int dir;
203
1.64M
                unsigned variance;
204
1.64M
                if (y_pri_lvl || uv_pri_lvl)
205
1.60M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
1.60M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
1.64M
                const pixel *top, *bot;
209
1.64M
                ptrdiff_t offset;
210
211
1.64M
                if (!have_tt) goto st_y;
212
1.64M
                if (sbrow_start && by == by_start) {
213
93.7k
                    if (resize) {
214
59.2k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
59.2k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
59.2k
                    } else {
217
34.4k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
34.4k
                        top = &f->lf.lr_lpf_line[0][offset];
219
34.4k
                    }
220
93.7k
                    bot = bptrs[0] + 8 * y_stride;
221
1.57M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
217k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
217k
                    if (resize) {
224
118k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
118k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
118k
                    } else {
227
99.4k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
99.4k
                        offset = line * y_stride + bx * 4;
229
99.4k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
99.4k
                    }
231
1.33M
                } else {
232
1.35M
            st_y:;
233
1.35M
                    offset = sby * 4 * y_stride;
234
1.35M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
1.35M
                    bot = bptrs[0] + 8 * y_stride;
236
1.35M
                }
237
1.66M
                if (y_pri_lvl) {
238
1.45M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
1.45M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
1.29M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
1.29M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
1.29M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
1.45M
                } else if (y_sec_lvl)
244
110k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
110k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
110k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
1.66M
                if (!uv_lvl) goto skip_uv;
249
1.26M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
1.26M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
3.72M
                for (int pl = 1; pl <= 2; pl++) {
253
2.45M
                    if (!have_tt) goto st_uv;
254
2.45M
                    if (sbrow_start && by == by_start) {
255
142k
                        if (resize) {
256
84.3k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
84.3k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
84.3k
                        } else {
259
58.5k
                            const int line = sby * (4 << sb128) - 4;
260
58.5k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
58.5k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
58.5k
                        }
263
142k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
2.31M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
317k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
317k
                                                     (bx * 4 >> ss_hor);
267
317k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
317k
                        if (resize) {
269
169k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
169k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
169k
                        } else {
272
148k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
148k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
148k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
148k
                        }
276
1.98M
                    } else {
277
2.00M
                st_uv:;
278
2.00M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
2.00M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
2.00M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
2.00M
                    }
282
2.46M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
2.46M
                                         lr_bak[bit][pl], top, bot,
284
2.46M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
2.46M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
2.46M
                }
287
288
1.64M
            skip_uv:
289
1.64M
                bit ^= 1;
290
291
1.76M
            next_b:
292
1.76M
                bptrs[0] += 8;
293
1.76M
                bptrs[1] += 8 >> ss_hor;
294
1.76M
                bptrs[2] += 8 >> ss_hor;
295
1.76M
            }
296
297
1.34M
        next_sb:
298
1.34M
            iptrs[0] += sbsz * 4;
299
1.34M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
1.34M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
1.34M
        }
302
303
443k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
443k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
443k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
443k
        tc->top_pre_cdef_toggle ^= 1;
307
443k
    }
308
107k
}