Coverage Report

Created: 2026-06-10 07:00

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.11M
{
45
1.11M
    const ptrdiff_t y_stride = PXSTRIDE(stride[0]);
46
1.11M
    if (y_stride < 0)
47
0
        pixel_copy(dst[0] + y_stride, src[0] + 7 * y_stride, -2 * y_stride);
48
1.11M
    else
49
1.11M
        pixel_copy(dst[0], src[0] + 6 * y_stride, 2 * y_stride);
50
51
1.11M
    if (layout != DAV1D_PIXEL_LAYOUT_I400) {
52
777k
        const ptrdiff_t uv_stride = PXSTRIDE(stride[1]);
53
777k
        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
777k
        } else {
58
777k
            const int uv_off = layout == DAV1D_PIXEL_LAYOUT_I420 ? 2 : 6;
59
777k
            pixel_copy(dst[1], src[1] + uv_off * uv_stride, 2 * uv_stride);
60
777k
            pixel_copy(dst[2], src[2] + uv_off * uv_stride, 2 * uv_stride);
61
777k
        }
62
777k
    }
63
1.11M
}
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.79M
{
71
4.79M
    ptrdiff_t y_off = 0;
72
4.79M
    if (flag & BACKUP_2X8_Y) {
73
40.6M
        for (int y = 0; y < 8; y++, y_off += PXSTRIDE(src_stride[0]))
74
36.0M
            pixel_copy(dst[0][y], &src[0][y_off + x_off - 2], 2);
75
4.51M
    }
76
77
4.79M
    if (layout == DAV1D_PIXEL_LAYOUT_I400 || !(flag & BACKUP_2X8_UV))
78
1.61M
        return;
79
80
3.18M
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
81
3.18M
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
82
83
3.18M
    x_off >>= ss_hor;
84
3.18M
    y_off = 0;
85
22.9M
    for (int y = 0; y < (8 >> ss_ver); y++, y_off += PXSTRIDE(src_stride[1])) {
86
19.8M
        pixel_copy(dst[1][y], &src[1][y_off + x_off - 2], 2);
87
19.8M
        pixel_copy(dst[2][y], &src[2][y_off + x_off - 2], 2);
88
19.8M
    }
89
3.18M
}
90
91
4.29M
static int adjust_strength(const int strength, const unsigned var) {
92
4.29M
    if (!var) return 0;
93
1.81M
    const int i = var >> 6 ? imin(ulog2(var >> 6), 12) : 0;
94
1.81M
    return (strength * (4 + i) + 8) >> 4;
95
4.29M
}
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
259k
{
103
259k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
259k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
259k
    const Dav1dDSPContext *const dsp = f->dsp;
106
259k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
259k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
259k
    const int sbsz = 16;
109
259k
    const int sb64w = f->sb128w << 1;
110
259k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
259k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
259k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
259k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
259k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
259k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
259k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
259k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
259k
    const int have_tt = f->c->n_tc > 1;
119
259k
    const int sb128 = f->seq_hdr->sb128;
120
259k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
259k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
259k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
1.53M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
1.27M
        const int tf = tc->top_pre_cdef_toggle;
126
1.27M
        const int by_idx = (by & 30) >> 1;
127
1.27M
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
1.27M
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
1.11M
            edges & CDEF_HAVE_BOTTOM)
131
1.11M
        {
132
            // backup pre-filter data for next iteration
133
1.11M
            pixel *const cdef_top_bak[3] = {
134
1.11M
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
1.11M
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
1.11M
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
1.11M
            };
138
1.11M
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
1.11M
        }
140
141
1.27M
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
1.27M
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
1.27M
        edges &= ~CDEF_HAVE_LEFT;
144
1.27M
        edges |= CDEF_HAVE_RIGHT;
145
1.27M
        enum Backup2x8Flags prev_flag = 0;
146
5.48M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
4.20M
            const int sb128x = sbx >> 1;
148
4.20M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
4.20M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
4.20M
            if (cdef_idx == -1 ||
151
2.16M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
916k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
2.85M
            {
154
2.85M
                prev_flag = 0;
155
2.85M
                goto next_sb;
156
2.85M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
1.35M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
1.35M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
1.35M
                                                    noskip_row[0][0];
162
163
1.35M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
1.35M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
1.35M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
1.35M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
1.35M
            int y_sec_lvl = y_lvl & 3;
169
1.35M
            y_sec_lvl += y_sec_lvl == 3;
170
1.35M
            y_sec_lvl <<= bitdepth_min_8;
171
172
1.35M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
1.35M
            int uv_sec_lvl = uv_lvl & 3;
174
1.35M
            uv_sec_lvl += uv_sec_lvl == 3;
175
1.35M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
1.35M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
6.77M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
5.41M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
5.50M
            {
181
5.50M
                if (bx + 2 >= f->bw) edges &= ~CDEF_HAVE_RIGHT;
182
183
                // check if this 8x8 block had any coded coefficients; if not,
184
                // go to the next block
185
5.50M
                const uint32_t bx_mask = 3U << (bx & 30);
186
5.50M
                if (!(noskip_mask & bx_mask)) {
187
358k
                    prev_flag = 0;
188
358k
                    goto next_b;
189
358k
                }
190
5.14M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
5.14M
                prev_flag = flag;
192
5.14M
                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
66.0k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
66.0k
                }
197
5.14M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
4.73M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
4.73M
                }
201
202
5.14M
                int dir;
203
5.14M
                unsigned variance;
204
5.14M
                if (y_pri_lvl || uv_pri_lvl)
205
4.98M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
4.98M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
5.14M
                const pixel *top, *bot;
209
5.14M
                ptrdiff_t offset;
210
211
5.14M
                if (!have_tt) goto st_y;
212
5.14M
                if (sbrow_start && by == by_start) {
213
327k
                    if (resize) {
214
155k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
155k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
172k
                    } else {
217
172k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
172k
                        top = &f->lf.lr_lpf_line[0][offset];
219
172k
                    }
220
327k
                    bot = bptrs[0] + 8 * y_stride;
221
5.07M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
649k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
649k
                    if (resize) {
224
308k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
308k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
340k
                    } else {
227
340k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
340k
                        offset = line * y_stride + bx * 4;
229
340k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
340k
                    }
231
4.17M
                } else {
232
4.42M
            st_y:;
233
4.42M
                    offset = sby * 4 * y_stride;
234
4.42M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
4.42M
                    bot = bptrs[0] + 8 * y_stride;
236
4.42M
                }
237
5.40M
                if (y_pri_lvl) {
238
4.29M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
4.29M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
3.53M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
3.53M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
3.53M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
4.29M
                } else if (y_sec_lvl)
244
790k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
790k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
790k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
5.40M
                if (!uv_lvl) goto skip_uv;
249
3.83M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
3.83M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
10.6M
                for (int pl = 1; pl <= 2; pl++) {
253
6.72M
                    if (!have_tt) goto st_uv;
254
6.72M
                    if (sbrow_start && by == by_start) {
255
392k
                        if (resize) {
256
160k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
160k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
232k
                        } else {
259
232k
                            const int line = sby * (4 << sb128) - 4;
260
232k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
232k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
232k
                        }
263
392k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
6.36M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
882k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
882k
                                                     (bx * 4 >> ss_hor);
267
882k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
882k
                        if (resize) {
269
367k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
367k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
514k
                        } else {
272
514k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
514k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
514k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
514k
                        }
276
5.44M
                    } else {
277
5.52M
                st_uv:;
278
5.52M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
5.52M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
5.52M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
5.52M
                    }
282
6.80M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
6.80M
                                         lr_bak[bit][pl], top, bot,
284
6.80M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
6.80M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
6.80M
                }
287
288
5.04M
            skip_uv:
289
5.04M
                bit ^= 1;
290
291
5.41M
            next_b:
292
5.41M
                bptrs[0] += 8;
293
5.41M
                bptrs[1] += 8 >> ss_hor;
294
5.41M
                bptrs[2] += 8 >> ss_hor;
295
5.41M
            }
296
297
4.20M
        next_sb:
298
4.20M
            iptrs[0] += sbsz * 4;
299
4.20M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
4.20M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
4.20M
        }
302
303
1.27M
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
1.27M
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
1.27M
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
1.27M
        tc->top_pre_cdef_toggle ^= 1;
307
1.27M
    }
308
259k
}
dav1d_cdef_brow_8bpc
Line
Count
Source
102
118k
{
103
118k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
18.4E
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
118k
    const Dav1dDSPContext *const dsp = f->dsp;
106
118k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
118k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
118k
    const int sbsz = 16;
109
118k
    const int sb64w = f->sb128w << 1;
110
118k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
118k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
118k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
118k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
118k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
118k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
118k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
118k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
118k
    const int have_tt = f->c->n_tc > 1;
119
118k
    const int sb128 = f->seq_hdr->sb128;
120
118k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
118k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
118k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
693k
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
576k
        const int tf = tc->top_pre_cdef_toggle;
126
576k
        const int by_idx = (by & 30) >> 1;
127
576k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
576k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
500k
            edges & CDEF_HAVE_BOTTOM)
131
500k
        {
132
            // backup pre-filter data for next iteration
133
500k
            pixel *const cdef_top_bak[3] = {
134
500k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
500k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
500k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
500k
            };
138
500k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
500k
        }
140
141
576k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
576k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
576k
        edges &= ~CDEF_HAVE_LEFT;
144
576k
        edges |= CDEF_HAVE_RIGHT;
145
576k
        enum Backup2x8Flags prev_flag = 0;
146
2.50M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
1.93M
            const int sb128x = sbx >> 1;
148
1.93M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
1.93M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
1.93M
            if (cdef_idx == -1 ||
151
952k
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
341k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.27M
            {
154
1.27M
                prev_flag = 0;
155
1.27M
                goto next_sb;
156
1.27M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
659k
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
659k
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
659k
                                                    noskip_row[0][0];
162
163
659k
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
659k
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
659k
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
659k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
659k
            int y_sec_lvl = y_lvl & 3;
169
659k
            y_sec_lvl += y_sec_lvl == 3;
170
659k
            y_sec_lvl <<= bitdepth_min_8;
171
172
659k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
659k
            int uv_sec_lvl = uv_lvl & 3;
174
659k
            uv_sec_lvl += uv_sec_lvl == 3;
175
659k
            uv_sec_lvl <<= bitdepth_min_8;
176
177
659k
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
3.21M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
2.55M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
2.59M
            {
181
2.59M
                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.59M
                const uint32_t bx_mask = 3U << (bx & 30);
186
2.59M
                if (!(noskip_mask & bx_mask)) {
187
178k
                    prev_flag = 0;
188
178k
                    goto next_b;
189
178k
                }
190
2.41M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
2.41M
                prev_flag = flag;
192
2.41M
                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
27.3k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
27.3k
                }
197
2.41M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.18M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.18M
                }
201
202
2.41M
                int dir;
203
2.41M
                unsigned variance;
204
2.41M
                if (y_pri_lvl || uv_pri_lvl)
205
2.17M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
2.17M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
2.41M
                const pixel *top, *bot;
209
2.41M
                ptrdiff_t offset;
210
211
2.41M
                if (!have_tt) goto st_y;
212
2.41M
                if (sbrow_start && by == by_start) {
213
155k
                    if (resize) {
214
51.5k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
51.5k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
104k
                    } else {
217
104k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
104k
                        top = &f->lf.lr_lpf_line[0][offset];
219
104k
                    }
220
155k
                    bot = bptrs[0] + 8 * y_stride;
221
2.36M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
317k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
317k
                    if (resize) {
224
100k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
100k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
216k
                    } else {
227
216k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
216k
                        offset = line * y_stride + bx * 4;
229
216k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
216k
                    }
231
1.93M
                } else {
232
2.04M
            st_y:;
233
2.04M
                    offset = sby * 4 * y_stride;
234
2.04M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
2.04M
                    bot = bptrs[0] + 8 * y_stride;
236
2.04M
                }
237
2.52M
                if (y_pri_lvl) {
238
1.68M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
1.68M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
1.22M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
1.22M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
1.22M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
1.68M
                } else if (y_sec_lvl)
244
614k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
614k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
614k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
2.52M
                if (!uv_lvl) goto skip_uv;
249
1.73M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
1.73M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
4.81M
                for (int pl = 1; pl <= 2; pl++) {
253
3.02M
                    if (!have_tt) goto st_uv;
254
3.02M
                    if (sbrow_start && by == by_start) {
255
174k
                        if (resize) {
256
48.6k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
48.6k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
125k
                        } else {
259
125k
                            const int line = sby * (4 << sb128) - 4;
260
125k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
125k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
125k
                        }
263
174k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
2.87M
                    } 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
114k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
114k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
322k
                        } else {
272
322k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
322k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
322k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
322k
                        }
276
2.41M
                    } else {
277
2.46M
                st_uv:;
278
2.46M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
2.46M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
2.46M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
2.46M
                    }
282
3.07M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
3.07M
                                         lr_bak[bit][pl], top, bot,
284
3.07M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
3.07M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
3.07M
                }
287
288
2.36M
            skip_uv:
289
2.36M
                bit ^= 1;
290
291
2.55M
            next_b:
292
2.55M
                bptrs[0] += 8;
293
2.55M
                bptrs[1] += 8 >> ss_hor;
294
2.55M
                bptrs[2] += 8 >> ss_hor;
295
2.55M
            }
296
297
1.93M
        next_sb:
298
1.93M
            iptrs[0] += sbsz * 4;
299
1.93M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
1.93M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
1.93M
        }
302
303
574k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
574k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
574k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
574k
        tc->top_pre_cdef_toggle ^= 1;
307
574k
    }
308
118k
}
dav1d_cdef_brow_16bpc
Line
Count
Source
102
140k
{
103
140k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
140k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
140k
    const Dav1dDSPContext *const dsp = f->dsp;
106
140k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
140k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
140k
    const int sbsz = 16;
109
140k
    const int sb64w = f->sb128w << 1;
110
140k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
140k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
140k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
140k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
140k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
140k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
140k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
140k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
140k
    const int have_tt = f->c->n_tc > 1;
119
140k
    const int sb128 = f->seq_hdr->sb128;
120
140k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
140k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
140k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
836k
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
701k
        const int tf = tc->top_pre_cdef_toggle;
126
701k
        const int by_idx = (by & 30) >> 1;
127
701k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
703k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
618k
            edges & CDEF_HAVE_BOTTOM)
131
618k
        {
132
            // backup pre-filter data for next iteration
133
618k
            pixel *const cdef_top_bak[3] = {
134
618k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
618k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
618k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
618k
            };
138
618k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
618k
        }
140
141
701k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
701k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
701k
        edges &= ~CDEF_HAVE_LEFT;
144
701k
        edges |= CDEF_HAVE_RIGHT;
145
701k
        enum Backup2x8Flags prev_flag = 0;
146
2.97M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
2.27M
            const int sb128x = sbx >> 1;
148
2.27M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
2.27M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
2.27M
            if (cdef_idx == -1 ||
151
1.21M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
574k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.57M
            {
154
1.57M
                prev_flag = 0;
155
1.57M
                goto next_sb;
156
1.57M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
698k
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
698k
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
698k
                                                    noskip_row[0][0];
162
163
698k
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
698k
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
698k
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
698k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
698k
            int y_sec_lvl = y_lvl & 3;
169
698k
            y_sec_lvl += y_sec_lvl == 3;
170
698k
            y_sec_lvl <<= bitdepth_min_8;
171
172
698k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
698k
            int uv_sec_lvl = uv_lvl & 3;
174
698k
            uv_sec_lvl += uv_sec_lvl == 3;
175
698k
            uv_sec_lvl <<= bitdepth_min_8;
176
177
698k
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
3.56M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
2.86M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
2.91M
            {
181
2.91M
                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.91M
                const uint32_t bx_mask = 3U << (bx & 30);
186
2.91M
                if (!(noskip_mask & bx_mask)) {
187
179k
                    prev_flag = 0;
188
179k
                    goto next_b;
189
179k
                }
190
2.73M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
2.73M
                prev_flag = flag;
192
2.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
38.6k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
38.6k
                }
197
2.73M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.54M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.54M
                }
201
202
2.73M
                int dir;
203
2.73M
                unsigned variance;
204
2.73M
                if (y_pri_lvl || uv_pri_lvl)
205
2.81M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
2.81M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
2.73M
                const pixel *top, *bot;
209
2.73M
                ptrdiff_t offset;
210
211
2.73M
                if (!have_tt) goto st_y;
212
2.73M
                if (sbrow_start && by == by_start) {
213
171k
                    if (resize) {
214
103k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
103k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
103k
                    } else {
217
68.2k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
68.2k
                        top = &f->lf.lr_lpf_line[0][offset];
219
68.2k
                    }
220
171k
                    bot = bptrs[0] + 8 * y_stride;
221
2.71M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
332k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
332k
                    if (resize) {
224
208k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
208k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
208k
                    } else {
227
124k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
124k
                        offset = line * y_stride + bx * 4;
229
124k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
124k
                    }
231
2.23M
                } 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.88M
                if (y_pri_lvl) {
238
2.60M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
2.60M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
2.30M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
2.30M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
2.30M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
2.60M
                } else if (y_sec_lvl)
244
176k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
176k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
176k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
2.88M
                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
5.82M
                for (int pl = 1; pl <= 2; pl++) {
253
3.69M
                    if (!have_tt) goto st_uv;
254
3.69M
                    if (sbrow_start && by == by_start) {
255
218k
                        if (resize) {
256
111k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
111k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
111k
                        } else {
259
106k
                            const int line = sby * (4 << sb128) - 4;
260
106k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
106k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
106k
                        }
263
218k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
3.49M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
445k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
445k
                                                     (bx * 4 >> ss_hor);
267
445k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
445k
                        if (resize) {
269
253k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
253k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
253k
                        } else {
272
191k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
191k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
191k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
191k
                        }
276
3.02M
                    } else {
277
3.06M
                st_uv:;
278
3.06M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
3.06M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
3.06M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
3.06M
                    }
282
3.72M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
3.72M
                                         lr_bak[bit][pl], top, bot,
284
3.72M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
3.72M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
3.72M
                }
287
288
2.67M
            skip_uv:
289
2.67M
                bit ^= 1;
290
291
2.86M
            next_b:
292
2.86M
                bptrs[0] += 8;
293
2.86M
                bptrs[1] += 8 >> ss_hor;
294
2.86M
                bptrs[2] += 8 >> ss_hor;
295
2.86M
            }
296
297
2.26M
        next_sb:
298
2.26M
            iptrs[0] += sbsz * 4;
299
2.26M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
2.26M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
2.26M
        }
302
303
696k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
696k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
696k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
696k
        tc->top_pre_cdef_toggle ^= 1;
307
696k
    }
308
140k
}