Coverage Report

Created: 2026-09-01 06:57

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.37M
{
45
1.37M
    const ptrdiff_t y_stride = PXSTRIDE(stride[0]);
46
1.37M
    if (y_stride < 0)
47
0
        pixel_copy(dst[0] + y_stride, src[0] + 7 * y_stride, -2 * y_stride);
48
1.37M
    else
49
1.37M
        pixel_copy(dst[0], src[0] + 6 * y_stride, 2 * y_stride);
50
51
1.37M
    if (layout != DAV1D_PIXEL_LAYOUT_I400) {
52
1.12M
        const ptrdiff_t uv_stride = PXSTRIDE(stride[1]);
53
1.12M
        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.12M
        } else {
58
1.12M
            const int uv_off = layout == DAV1D_PIXEL_LAYOUT_I420 ? 2 : 6;
59
1.12M
            pixel_copy(dst[1], src[1] + uv_off * uv_stride, 2 * uv_stride);
60
1.12M
            pixel_copy(dst[2], src[2] + uv_off * uv_stride, 2 * uv_stride);
61
1.12M
        }
62
1.12M
    }
63
1.37M
}
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.90M
{
71
4.90M
    ptrdiff_t y_off = 0;
72
4.90M
    if (flag & BACKUP_2X8_Y) {
73
41.4M
        for (int y = 0; y < 8; y++, y_off += PXSTRIDE(src_stride[0]))
74
36.8M
            pixel_copy(dst[0][y], &src[0][y_off + x_off - 2], 2);
75
4.60M
    }
76
77
4.90M
    if (layout == DAV1D_PIXEL_LAYOUT_I400 || !(flag & BACKUP_2X8_UV))
78
1.35M
        return;
79
80
3.54M
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
81
3.54M
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
82
83
3.54M
    x_off >>= ss_hor;
84
3.54M
    y_off = 0;
85
24.5M
    for (int y = 0; y < (8 >> ss_ver); y++, y_off += PXSTRIDE(src_stride[1])) {
86
21.0M
        pixel_copy(dst[1][y], &src[1][y_off + x_off - 2], 2);
87
21.0M
        pixel_copy(dst[2][y], &src[2][y_off + x_off - 2], 2);
88
21.0M
    }
89
3.54M
}
90
91
4.67M
static int adjust_strength(const int strength, const unsigned var) {
92
4.67M
    if (!var) return 0;
93
2.25M
    const int i = var >> 6 ? imin(ulog2(var >> 6), 12) : 0;
94
2.25M
    return (strength * (4 + i) + 8) >> 4;
95
4.67M
}
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
367k
{
103
367k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
367k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
367k
    const Dav1dDSPContext *const dsp = f->dsp;
106
367k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
367k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
367k
    const int sbsz = 16;
109
367k
    const int sb64w = f->sb128w << 1;
110
367k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
367k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
367k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
367k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
367k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
367k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
367k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
367k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
367k
    const int have_tt = f->c->n_tc > 1;
119
367k
    const int sb128 = f->seq_hdr->sb128;
120
367k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
367k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
367k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
1.97M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
1.61M
        const int tf = tc->top_pre_cdef_toggle;
126
1.61M
        const int by_idx = (by & 30) >> 1;
127
1.61M
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
1.61M
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
1.37M
            edges & CDEF_HAVE_BOTTOM)
131
1.37M
        {
132
            // backup pre-filter data for next iteration
133
1.37M
            pixel *const cdef_top_bak[3] = {
134
1.37M
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
1.37M
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
1.37M
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
1.37M
            };
138
1.37M
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
1.37M
        }
140
141
1.61M
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
1.61M
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
1.61M
        edges &= ~CDEF_HAVE_LEFT;
144
1.61M
        edges |= CDEF_HAVE_RIGHT;
145
1.61M
        enum Backup2x8Flags prev_flag = 0;
146
6.97M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
5.36M
            const int sb128x = sbx >> 1;
148
5.36M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
5.36M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
5.36M
            if (cdef_idx == -1 ||
151
3.46M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
1.59M
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
3.38M
            {
154
3.38M
                prev_flag = 0;
155
3.38M
                goto next_sb;
156
3.38M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
1.97M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
1.97M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
1.97M
                                                    noskip_row[0][0];
162
163
1.97M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
1.97M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
1.97M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
1.97M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
1.97M
            int y_sec_lvl = y_lvl & 3;
169
1.97M
            y_sec_lvl += y_sec_lvl == 3;
170
1.97M
            y_sec_lvl <<= bitdepth_min_8;
171
172
1.97M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
1.97M
            int uv_sec_lvl = uv_lvl & 3;
174
1.97M
            uv_sec_lvl += uv_sec_lvl == 3;
175
1.97M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
1.97M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
7.98M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
6.00M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
6.04M
            {
181
6.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
6.04M
                const uint32_t bx_mask = 3U << (bx & 30);
186
6.04M
                if (!(noskip_mask & bx_mask)) {
187
637k
                    prev_flag = 0;
188
637k
                    goto next_b;
189
637k
                }
190
5.40M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
5.40M
                prev_flag = flag;
192
5.40M
                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
82.9k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
82.9k
                }
197
5.40M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
4.81M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
4.81M
                }
201
202
5.40M
                int dir;
203
5.40M
                unsigned variance;
204
5.40M
                if (y_pri_lvl || uv_pri_lvl)
205
5.20M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
5.20M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
5.40M
                const pixel *top, *bot;
209
5.40M
                ptrdiff_t offset;
210
211
5.40M
                if (!have_tt) goto st_y;
212
5.40M
                if (sbrow_start && by == by_start) {
213
342k
                    if (resize) {
214
118k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
118k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
223k
                    } else {
217
223k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
223k
                        top = &f->lf.lr_lpf_line[0][offset];
219
223k
                    }
220
342k
                    bot = bptrs[0] + 8 * y_stride;
221
5.18M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
701k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
701k
                    if (resize) {
224
251k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
251k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
450k
                    } else {
227
450k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
450k
                        offset = line * y_stride + bx * 4;
229
450k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
450k
                    }
231
4.36M
                } else {
232
4.49M
            st_y:;
233
4.49M
                    offset = sby * 4 * y_stride;
234
4.49M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
4.49M
                    bot = bptrs[0] + 8 * y_stride;
236
4.49M
                }
237
5.53M
                if (y_pri_lvl) {
238
4.67M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
4.67M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
3.89M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
3.89M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
3.89M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
4.67M
                } else if (y_sec_lvl)
244
511k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
511k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
511k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
5.53M
                if (!uv_lvl) goto skip_uv;
249
4.15M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
4.15M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
12.0M
                for (int pl = 1; pl <= 2; pl++) {
253
7.86M
                    if (!have_tt) goto st_uv;
254
7.86M
                    if (sbrow_start && by == by_start) {
255
470k
                        if (resize) {
256
170k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
170k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
299k
                        } else {
259
299k
                            const int line = sby * (4 << sb128) - 4;
260
299k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
299k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
299k
                        }
263
470k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
7.41M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
1.01M
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
1.01M
                                                     (bx * 4 >> ss_hor);
267
1.01M
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
1.01M
                        if (resize) {
269
361k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
361k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
649k
                        } else {
272
649k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
649k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
649k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
649k
                        }
276
6.38M
                    } else {
277
6.43M
                st_uv:;
278
6.43M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
6.43M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
6.43M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
6.43M
                    }
282
7.92M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
7.92M
                                         lr_bak[bit][pl], top, bot,
284
7.92M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
7.92M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
7.92M
                }
287
288
5.37M
            skip_uv:
289
5.37M
                bit ^= 1;
290
291
6.00M
            next_b:
292
6.00M
                bptrs[0] += 8;
293
6.00M
                bptrs[1] += 8 >> ss_hor;
294
6.00M
                bptrs[2] += 8 >> ss_hor;
295
6.00M
            }
296
297
5.36M
        next_sb:
298
5.36M
            iptrs[0] += sbsz * 4;
299
5.36M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
5.36M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
5.36M
        }
302
303
1.61M
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
1.61M
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
1.61M
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
1.61M
        tc->top_pre_cdef_toggle ^= 1;
307
1.61M
    }
308
367k
}
dav1d_cdef_brow_8bpc
Line
Count
Source
102
209k
{
103
209k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
18.4E
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
209k
    const Dav1dDSPContext *const dsp = f->dsp;
106
209k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
209k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
209k
    const int sbsz = 16;
109
209k
    const int sb64w = f->sb128w << 1;
110
209k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
209k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
209k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
209k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
209k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
209k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
209k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
209k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
209k
    const int have_tt = f->c->n_tc > 1;
119
209k
    const int sb128 = f->seq_hdr->sb128;
120
209k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
209k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
209k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
1.15M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
943k
        const int tf = tc->top_pre_cdef_toggle;
126
943k
        const int by_idx = (by & 30) >> 1;
127
943k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
943k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
813k
            edges & CDEF_HAVE_BOTTOM)
131
813k
        {
132
            // backup pre-filter data for next iteration
133
813k
            pixel *const cdef_top_bak[3] = {
134
813k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
813k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
813k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
813k
            };
138
813k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
813k
        }
140
141
943k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
943k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
943k
        edges &= ~CDEF_HAVE_LEFT;
144
943k
        edges |= CDEF_HAVE_RIGHT;
145
943k
        enum Backup2x8Flags prev_flag = 0;
146
3.99M
        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.98M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
947k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.95M
            {
154
1.95M
                prev_flag = 0;
155
1.95M
                goto next_sb;
156
1.95M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
1.09M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
1.09M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
1.09M
                                                    noskip_row[0][0];
162
163
1.09M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
1.09M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
1.09M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
1.09M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
1.09M
            int y_sec_lvl = y_lvl & 3;
169
1.09M
            y_sec_lvl += y_sec_lvl == 3;
170
1.09M
            y_sec_lvl <<= bitdepth_min_8;
171
172
1.09M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
1.09M
            int uv_sec_lvl = uv_lvl & 3;
174
1.09M
            uv_sec_lvl += uv_sec_lvl == 3;
175
1.09M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
1.09M
            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
2.89M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
2.92M
            {
181
2.92M
                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.92M
                const uint32_t bx_mask = 3U << (bx & 30);
186
2.92M
                if (!(noskip_mask & bx_mask)) {
187
373k
                    prev_flag = 0;
188
373k
                    goto next_b;
189
373k
                }
190
2.55M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
2.55M
                prev_flag = flag;
192
2.55M
                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
40.3k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
40.3k
                }
197
2.55M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.26M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.26M
                }
201
202
2.55M
                int dir;
203
2.55M
                unsigned variance;
204
2.55M
                if (y_pri_lvl || uv_pri_lvl)
205
2.44M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
2.44M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
2.55M
                const pixel *top, *bot;
209
2.55M
                ptrdiff_t offset;
210
211
2.55M
                if (!have_tt) goto st_y;
212
2.55M
                if (sbrow_start && by == by_start) {
213
190k
                    if (resize) {
214
24.0k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
24.0k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
166k
                    } else {
217
166k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
166k
                        top = &f->lf.lr_lpf_line[0][offset];
219
166k
                    }
220
190k
                    bot = bptrs[0] + 8 * y_stride;
221
2.44M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
342k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
342k
                    if (resize) {
224
68.0k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
68.0k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
274k
                    } else {
227
274k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
274k
                        offset = line * y_stride + bx * 4;
229
274k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
274k
                    }
231
2.01M
                } else {
232
2.11M
            st_y:;
233
2.11M
                    offset = sby * 4 * y_stride;
234
2.11M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
2.11M
                    bot = bptrs[0] + 8 * y_stride;
236
2.11M
                }
237
2.64M
                if (y_pri_lvl) {
238
2.16M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
2.16M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
1.64M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
1.64M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
1.64M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
2.16M
                } else if (y_sec_lvl)
244
272k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
272k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
272k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
2.64M
                if (!uv_lvl) goto skip_uv;
249
2.05M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
2.05M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
5.90M
                for (int pl = 1; pl <= 2; pl++) {
253
3.80M
                    if (!have_tt) goto st_uv;
254
3.80M
                    if (sbrow_start && by == by_start) {
255
247k
                        if (resize) {
256
42.4k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
42.4k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
205k
                        } else {
259
205k
                            const int line = sby * (4 << sb128) - 4;
260
205k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
205k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
205k
                        }
263
247k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
3.57M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
490k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
490k
                                                     (bx * 4 >> ss_hor);
267
490k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
490k
                        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
389k
                        } else {
272
389k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
389k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
389k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
389k
                        }
276
3.06M
                    } else {
277
3.10M
                st_uv:;
278
3.10M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
3.10M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
3.10M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
3.10M
                    }
282
3.84M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
3.84M
                                         lr_bak[bit][pl], top, bot,
284
3.84M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
3.84M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
3.84M
                }
287
288
2.52M
            skip_uv:
289
2.52M
                bit ^= 1;
290
291
2.89M
            next_b:
292
2.89M
                bptrs[0] += 8;
293
2.89M
                bptrs[1] += 8 >> ss_hor;
294
2.89M
                bptrs[2] += 8 >> ss_hor;
295
2.89M
            }
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
941k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
941k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
941k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
941k
        tc->top_pre_cdef_toggle ^= 1;
307
941k
    }
308
209k
}
dav1d_cdef_brow_16bpc
Line
Count
Source
102
158k
{
103
158k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
158k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
158k
    const Dav1dDSPContext *const dsp = f->dsp;
106
158k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
158k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
158k
    const int sbsz = 16;
109
158k
    const int sb64w = f->sb128w << 1;
110
158k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
158k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
158k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
158k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
158k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
158k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
158k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
158k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
158k
    const int have_tt = f->c->n_tc > 1;
119
158k
    const int sb128 = f->seq_hdr->sb128;
120
158k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
158k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
158k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
829k
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
671k
        const int tf = tc->top_pre_cdef_toggle;
126
671k
        const int by_idx = (by & 30) >> 1;
127
671k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
671k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
559k
            edges & CDEF_HAVE_BOTTOM)
131
559k
        {
132
            // backup pre-filter data for next iteration
133
559k
            pixel *const cdef_top_bak[3] = {
134
559k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
559k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
559k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
559k
            };
138
559k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
559k
        }
140
141
671k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
671k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
671k
        edges &= ~CDEF_HAVE_LEFT;
144
671k
        edges |= CDEF_HAVE_RIGHT;
145
671k
        enum Backup2x8Flags prev_flag = 0;
146
2.98M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
2.31M
            const int sb128x = sbx >> 1;
148
2.31M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
2.31M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
2.31M
            if (cdef_idx == -1 ||
151
1.48M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
643k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.42M
            {
154
1.42M
                prev_flag = 0;
155
1.42M
                goto next_sb;
156
1.42M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
883k
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
883k
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
883k
                                                    noskip_row[0][0];
162
163
883k
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
883k
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
883k
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
883k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
883k
            int y_sec_lvl = y_lvl & 3;
169
883k
            y_sec_lvl += y_sec_lvl == 3;
170
883k
            y_sec_lvl <<= bitdepth_min_8;
171
172
883k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
883k
            int uv_sec_lvl = uv_lvl & 3;
174
883k
            uv_sec_lvl += uv_sec_lvl == 3;
175
883k
            uv_sec_lvl <<= bitdepth_min_8;
176
177
883k
            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.11M
                 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
263k
                    prev_flag = 0;
188
263k
                    goto next_b;
189
263k
                }
190
2.85M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
2.85M
                prev_flag = flag;
192
2.85M
                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
42.6k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
42.6k
                }
197
2.85M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.55M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.55M
                }
201
202
2.85M
                int dir;
203
2.85M
                unsigned variance;
204
2.85M
                if (y_pri_lvl || uv_pri_lvl)
205
2.76M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
2.76M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
2.85M
                const pixel *top, *bot;
209
2.85M
                ptrdiff_t offset;
210
211
2.85M
                if (!have_tt) goto st_y;
212
2.85M
                if (sbrow_start && by == by_start) {
213
152k
                    if (resize) {
214
94.9k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
94.9k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
94.9k
                    } else {
217
57.1k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
57.1k
                        top = &f->lf.lr_lpf_line[0][offset];
219
57.1k
                    }
220
152k
                    bot = bptrs[0] + 8 * y_stride;
221
2.73M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
358k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
358k
                    if (resize) {
224
183k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
183k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
183k
                    } else {
227
175k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
175k
                        offset = line * y_stride + bx * 4;
229
175k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
175k
                    }
231
2.34M
                } else {
232
2.38M
            st_y:;
233
2.38M
                    offset = sby * 4 * y_stride;
234
2.38M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
2.38M
                    bot = bptrs[0] + 8 * y_stride;
236
2.38M
                }
237
2.89M
                if (y_pri_lvl) {
238
2.51M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
2.51M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
2.25M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
2.25M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
2.25M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
2.51M
                } else if (y_sec_lvl)
244
239k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
239k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
239k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
2.89M
                if (!uv_lvl) goto skip_uv;
249
2.09M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
2.09M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
6.16M
                for (int pl = 1; pl <= 2; pl++) {
253
4.05M
                    if (!have_tt) goto st_uv;
254
4.05M
                    if (sbrow_start && by == by_start) {
255
222k
                        if (resize) {
256
128k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
128k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
128k
                        } else {
259
94.4k
                            const int line = sby * (4 << sb128) - 4;
260
94.4k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
94.4k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
94.4k
                        }
263
222k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
3.84M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
520k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
520k
                                                     (bx * 4 >> ss_hor);
267
520k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
520k
                        if (resize) {
269
260k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
260k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
260k
                        } else {
272
259k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
259k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
259k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
259k
                        }
276
3.31M
                    } else {
277
3.33M
                st_uv:;
278
3.33M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
3.33M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
3.33M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
3.33M
                    }
282
4.07M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
4.07M
                                         lr_bak[bit][pl], top, bot,
284
4.07M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
4.07M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
4.07M
                }
287
288
2.84M
            skip_uv:
289
2.84M
                bit ^= 1;
290
291
3.11M
            next_b:
292
3.11M
                bptrs[0] += 8;
293
3.11M
                bptrs[1] += 8 >> ss_hor;
294
3.11M
                bptrs[2] += 8 >> ss_hor;
295
3.11M
            }
296
297
2.31M
        next_sb:
298
2.31M
            iptrs[0] += sbsz * 4;
299
2.31M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
2.31M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
2.31M
        }
302
303
671k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
671k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
671k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
671k
        tc->top_pre_cdef_toggle ^= 1;
307
671k
    }
308
158k
}