Coverage Report

Created: 2026-10-17 06:56

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
2.48M
{
45
2.48M
    const ptrdiff_t y_stride = PXSTRIDE(stride[0]);
46
2.48M
    if (y_stride < 0)
47
0
        pixel_copy(dst[0] + y_stride, src[0] + 7 * y_stride, -2 * y_stride);
48
2.48M
    else
49
2.48M
        pixel_copy(dst[0], src[0] + 6 * y_stride, 2 * y_stride);
50
51
2.48M
    if (layout != DAV1D_PIXEL_LAYOUT_I400) {
52
2.07M
        const ptrdiff_t uv_stride = PXSTRIDE(stride[1]);
53
2.07M
        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
2.07M
        } else {
58
2.07M
            const int uv_off = layout == DAV1D_PIXEL_LAYOUT_I420 ? 2 : 6;
59
2.07M
            pixel_copy(dst[1], src[1] + uv_off * uv_stride, 2 * uv_stride);
60
2.07M
            pixel_copy(dst[2], src[2] + uv_off * uv_stride, 2 * uv_stride);
61
2.07M
        }
62
2.07M
    }
63
2.48M
}
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
5.48M
{
71
5.48M
    ptrdiff_t y_off = 0;
72
5.48M
    if (flag & BACKUP_2X8_Y) {
73
44.4M
        for (int y = 0; y < 8; y++, y_off += PXSTRIDE(src_stride[0]))
74
39.4M
            pixel_copy(dst[0][y], &src[0][y_off + x_off - 2], 2);
75
4.93M
    }
76
77
5.48M
    if (layout == DAV1D_PIXEL_LAYOUT_I400 || !(flag & BACKUP_2X8_UV))
78
777k
        return;
79
80
4.70M
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
81
4.70M
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
82
83
4.70M
    x_off >>= ss_hor;
84
4.70M
    y_off = 0;
85
33.8M
    for (int y = 0; y < (8 >> ss_ver); y++, y_off += PXSTRIDE(src_stride[1])) {
86
29.1M
        pixel_copy(dst[1][y], &src[1][y_off + x_off - 2], 2);
87
29.1M
        pixel_copy(dst[2][y], &src[2][y_off + x_off - 2], 2);
88
29.1M
    }
89
4.70M
}
90
91
5.64M
static int adjust_strength(const int strength, const unsigned var) {
92
5.64M
    if (!var) return 0;
93
2.64M
    const int i = var >> 6 ? imin(ulog2(var >> 6), 12) : 0;
94
2.64M
    return (strength * (4 + i) + 8) >> 4;
95
5.64M
}
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
631k
{
103
631k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
631k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
631k
    const Dav1dDSPContext *const dsp = f->dsp;
106
631k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
631k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
631k
    const int sbsz = 16;
109
631k
    const int sb64w = f->sb128w << 1;
110
631k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
631k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
631k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
631k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
631k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
631k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
631k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
631k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
631k
    const int have_tt = f->c->n_tc > 1;
119
631k
    const int sb128 = f->seq_hdr->sb128;
120
631k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
631k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
631k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
3.46M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
2.85M
        const int tf = tc->top_pre_cdef_toggle;
126
2.85M
        const int by_idx = (by & 30) >> 1;
127
2.85M
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
2.87M
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
2.48M
            edges & CDEF_HAVE_BOTTOM)
131
2.49M
        {
132
            // backup pre-filter data for next iteration
133
2.49M
            pixel *const cdef_top_bak[3] = {
134
2.49M
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
2.49M
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
2.49M
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
2.49M
            };
138
2.49M
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
2.49M
        }
140
141
2.85M
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
2.85M
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
2.85M
        edges &= ~CDEF_HAVE_LEFT;
144
2.85M
        edges |= CDEF_HAVE_RIGHT;
145
2.85M
        enum Backup2x8Flags prev_flag = 0;
146
9.76M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
6.93M
            const int sb128x = sbx >> 1;
148
6.93M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
6.93M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
6.93M
            if (cdef_idx == -1 ||
151
3.79M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
1.30M
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
4.27M
            {
154
4.27M
                prev_flag = 0;
155
4.27M
                goto next_sb;
156
4.27M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
2.65M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
2.65M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
2.65M
                                                    noskip_row[0][0];
162
163
2.65M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
2.65M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
2.65M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
2.65M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
2.65M
            int y_sec_lvl = y_lvl & 3;
169
2.65M
            y_sec_lvl += y_sec_lvl == 3;
170
2.65M
            y_sec_lvl <<= bitdepth_min_8;
171
172
2.65M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
2.65M
            int uv_sec_lvl = uv_lvl & 3;
174
2.65M
            uv_sec_lvl += uv_sec_lvl == 3;
175
2.65M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
2.65M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
9.44M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
6.78M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
6.86M
            {
181
6.86M
                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.86M
                const uint32_t bx_mask = 3U << (bx & 30);
186
6.86M
                if (!(noskip_mask & bx_mask)) {
187
558k
                    prev_flag = 0;
188
558k
                    goto next_b;
189
558k
                }
190
6.30M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
6.30M
                prev_flag = flag;
192
6.30M
                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
78.3k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
78.3k
                }
197
6.30M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
5.39M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
5.39M
                }
201
202
6.30M
                int dir;
203
6.30M
                unsigned variance;
204
6.30M
                if (y_pri_lvl || uv_pri_lvl)
205
6.28M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
6.28M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
6.30M
                const pixel *top, *bot;
209
6.30M
                ptrdiff_t offset;
210
211
6.30M
                if (!have_tt) goto st_y;
212
6.30M
                if (sbrow_start && by == by_start) {
213
396k
                    if (resize) {
214
153k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
153k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
243k
                    } else {
217
243k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
243k
                        top = &f->lf.lr_lpf_line[0][offset];
219
243k
                    }
220
396k
                    bot = bptrs[0] + 8 * y_stride;
221
6.28M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
841k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
841k
                    if (resize) {
224
318k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
318k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
522k
                    } else {
227
522k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
522k
                        offset = line * y_stride + bx * 4;
229
522k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
522k
                    }
231
5.07M
                } else {
232
5.44M
            st_y:;
233
5.44M
                    offset = sby * 4 * y_stride;
234
5.44M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
5.44M
                    bot = bptrs[0] + 8 * y_stride;
236
5.44M
                }
237
6.68M
                if (y_pri_lvl) {
238
5.64M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
5.64M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
4.96M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
4.96M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
4.96M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
5.64M
                } else if (y_sec_lvl)
244
415k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
415k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
415k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
6.68M
                if (!uv_lvl) goto skip_uv;
249
5.74M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
5.74M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
16.1M
                for (int pl = 1; pl <= 2; pl++) {
253
10.2M
                    if (!have_tt) goto st_uv;
254
10.2M
                    if (sbrow_start && by == by_start) {
255
635k
                        if (resize) {
256
261k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
261k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
373k
                        } else {
259
373k
                            const int line = sby * (4 << sb128) - 4;
260
373k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
373k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
373k
                        }
263
635k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
9.69M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
1.30M
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
1.30M
                                                     (bx * 4 >> ss_hor);
267
1.30M
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
1.30M
                        if (resize) {
269
492k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
492k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
814k
                        } else {
272
814k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
814k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
814k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
814k
                        }
276
8.28M
                    } else {
277
8.49M
                st_uv:;
278
8.49M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
8.49M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
8.49M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
8.49M
                    }
282
10.4M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
10.4M
                                         lr_bak[bit][pl], top, bot,
284
10.4M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
10.4M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
10.4M
                }
287
288
6.23M
            skip_uv:
289
6.23M
                bit ^= 1;
290
291
6.78M
            next_b:
292
6.78M
                bptrs[0] += 8;
293
6.78M
                bptrs[1] += 8 >> ss_hor;
294
6.78M
                bptrs[2] += 8 >> ss_hor;
295
6.78M
            }
296
297
6.90M
        next_sb:
298
6.90M
            iptrs[0] += sbsz * 4;
299
6.90M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
6.90M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
6.90M
        }
302
303
2.83M
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
2.83M
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
2.83M
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
2.83M
        tc->top_pre_cdef_toggle ^= 1;
307
2.83M
    }
308
631k
}
dav1d_cdef_brow_8bpc
Line
Count
Source
102
332k
{
103
332k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
18.4E
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
332k
    const Dav1dDSPContext *const dsp = f->dsp;
106
332k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
332k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
332k
    const int sbsz = 16;
109
332k
    const int sb64w = f->sb128w << 1;
110
332k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
332k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
332k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
332k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
332k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
332k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
332k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
332k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
332k
    const int have_tt = f->c->n_tc > 1;
119
332k
    const int sb128 = f->seq_hdr->sb128;
120
332k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
332k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
332k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
1.85M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
1.52M
        const int tf = tc->top_pre_cdef_toggle;
126
1.52M
        const int by_idx = (by & 30) >> 1;
127
1.52M
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
1.52M
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
1.33M
            edges & CDEF_HAVE_BOTTOM)
131
1.33M
        {
132
            // backup pre-filter data for next iteration
133
1.33M
            pixel *const cdef_top_bak[3] = {
134
1.33M
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
1.33M
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
1.33M
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
1.33M
            };
138
1.33M
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
1.33M
        }
140
141
1.52M
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
1.52M
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
1.52M
        edges &= ~CDEF_HAVE_LEFT;
144
1.52M
        edges |= CDEF_HAVE_RIGHT;
145
1.52M
        enum Backup2x8Flags prev_flag = 0;
146
5.11M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
3.59M
            const int sb128x = sbx >> 1;
148
3.59M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
3.59M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
3.59M
            if (cdef_idx == -1 ||
151
1.93M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
593k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
2.15M
            {
154
2.15M
                prev_flag = 0;
155
2.15M
                goto next_sb;
156
2.15M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
1.43M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
1.43M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
1.43M
                                                    noskip_row[0][0];
162
163
1.43M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
1.43M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
1.43M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
1.43M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
1.43M
            int y_sec_lvl = y_lvl & 3;
169
1.43M
            y_sec_lvl += y_sec_lvl == 3;
170
1.43M
            y_sec_lvl <<= bitdepth_min_8;
171
172
1.43M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
1.43M
            int uv_sec_lvl = uv_lvl & 3;
174
1.43M
            uv_sec_lvl += uv_sec_lvl == 3;
175
1.43M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
1.43M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
4.67M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
3.23M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
3.28M
            {
181
3.28M
                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.28M
                const uint32_t bx_mask = 3U << (bx & 30);
186
3.28M
                if (!(noskip_mask & bx_mask)) {
187
320k
                    prev_flag = 0;
188
320k
                    goto next_b;
189
320k
                }
190
2.96M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
2.96M
                prev_flag = flag;
192
2.96M
                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.5k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
39.5k
                }
197
2.96M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.48M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.48M
                }
201
202
2.96M
                int dir;
203
2.96M
                unsigned variance;
204
2.96M
                if (y_pri_lvl || uv_pri_lvl)
205
2.95M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
2.95M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
2.96M
                const pixel *top, *bot;
209
2.96M
                ptrdiff_t offset;
210
211
2.96M
                if (!have_tt) goto st_y;
212
2.96M
                if (sbrow_start && by == by_start) {
213
201k
                    if (resize) {
214
59.5k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
59.5k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
142k
                    } else {
217
142k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
142k
                        top = &f->lf.lr_lpf_line[0][offset];
219
142k
                    }
220
201k
                    bot = bptrs[0] + 8 * y_stride;
221
2.99M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
404k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
404k
                    if (resize) {
224
132k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
132k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
272k
                    } else {
227
272k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
272k
                        offset = line * y_stride + bx * 4;
229
272k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
272k
                    }
231
2.36M
                } else {
232
2.58M
            st_y:;
233
2.58M
                    offset = sby * 4 * y_stride;
234
2.58M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
2.58M
                    bot = bptrs[0] + 8 * y_stride;
236
2.58M
                }
237
3.19M
                if (y_pri_lvl) {
238
2.63M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
2.63M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
2.29M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
2.29M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
2.29M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
2.63M
                } else if (y_sec_lvl)
244
228k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
228k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
228k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
3.19M
                if (!uv_lvl) goto skip_uv;
249
2.77M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
2.77M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
7.71M
                for (int pl = 1; pl <= 2; pl++) {
253
4.80M
                    if (!have_tt) goto st_uv;
254
4.80M
                    if (sbrow_start && by == by_start) {
255
316k
                        if (resize) {
256
101k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
101k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
215k
                        } else {
259
215k
                            const int line = sby * (4 << sb128) - 4;
260
215k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
215k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
215k
                        }
263
316k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
4.55M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
623k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
623k
                                                     (bx * 4 >> ss_hor);
267
623k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
623k
                        if (resize) {
269
194k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
194k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
429k
                        } else {
272
429k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
429k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
429k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
429k
                        }
276
3.86M
                    } else {
277
3.99M
                st_uv:;
278
3.99M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
3.99M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
3.99M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
3.99M
                    }
282
4.93M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
4.93M
                                         lr_bak[bit][pl], top, bot,
284
4.93M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
4.93M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
4.93M
                }
287
288
2.92M
            skip_uv:
289
2.92M
                bit ^= 1;
290
291
3.23M
            next_b:
292
3.23M
                bptrs[0] += 8;
293
3.23M
                bptrs[1] += 8 >> ss_hor;
294
3.23M
                bptrs[2] += 8 >> ss_hor;
295
3.23M
            }
296
297
3.58M
        next_sb:
298
3.58M
            iptrs[0] += sbsz * 4;
299
3.58M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
3.58M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
3.58M
        }
302
303
1.51M
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
1.51M
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
1.51M
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
1.51M
        tc->top_pre_cdef_toggle ^= 1;
307
1.51M
    }
308
332k
}
dav1d_cdef_brow_16bpc
Line
Count
Source
102
298k
{
103
298k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
298k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
298k
    const Dav1dDSPContext *const dsp = f->dsp;
106
298k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
298k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
298k
    const int sbsz = 16;
109
298k
    const int sb64w = f->sb128w << 1;
110
298k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
298k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
298k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
298k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
298k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
298k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
298k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
298k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
298k
    const int have_tt = f->c->n_tc > 1;
119
298k
    const int sb128 = f->seq_hdr->sb128;
120
298k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
298k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
298k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
1.61M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
1.33M
        const int tf = tc->top_pre_cdef_toggle;
126
1.33M
        const int by_idx = (by & 30) >> 1;
127
1.33M
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
1.34M
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
1.15M
            edges & CDEF_HAVE_BOTTOM)
131
1.15M
        {
132
            // backup pre-filter data for next iteration
133
1.15M
            pixel *const cdef_top_bak[3] = {
134
1.15M
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
1.15M
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
1.15M
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
1.15M
            };
138
1.15M
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
1.15M
        }
140
141
1.33M
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
1.33M
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
1.33M
        edges &= ~CDEF_HAVE_LEFT;
144
1.33M
        edges |= CDEF_HAVE_RIGHT;
145
1.33M
        enum Backup2x8Flags prev_flag = 0;
146
4.65M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
3.33M
            const int sb128x = sbx >> 1;
148
3.33M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
3.33M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
3.33M
            if (cdef_idx == -1 ||
151
1.85M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
711k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
2.11M
            {
154
2.11M
                prev_flag = 0;
155
2.11M
                goto next_sb;
156
2.11M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
1.22M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
1.22M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
1.22M
                                                    noskip_row[0][0];
162
163
1.22M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
1.22M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
1.22M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
1.22M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
1.22M
            int y_sec_lvl = y_lvl & 3;
169
1.22M
            y_sec_lvl += y_sec_lvl == 3;
170
1.22M
            y_sec_lvl <<= bitdepth_min_8;
171
172
1.22M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
1.22M
            int uv_sec_lvl = uv_lvl & 3;
174
1.22M
            uv_sec_lvl += uv_sec_lvl == 3;
175
1.22M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
1.22M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
4.76M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
3.54M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
3.57M
            {
181
3.57M
                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.57M
                const uint32_t bx_mask = 3U << (bx & 30);
186
3.57M
                if (!(noskip_mask & bx_mask)) {
187
238k
                    prev_flag = 0;
188
238k
                    goto next_b;
189
238k
                }
190
3.34M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
3.34M
                prev_flag = flag;
192
3.34M
                if (do_left && edges & CDEF_HAVE_LEFT) {
193
                    // we didn't backup the prefilter data because it wasn't
194
                    // there, so do it here instead
195
38.7k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
38.7k
                }
197
3.34M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.91M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.91M
                }
201
202
3.34M
                int dir;
203
3.34M
                unsigned variance;
204
3.34M
                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.34M
                const pixel *top, *bot;
209
3.34M
                ptrdiff_t offset;
210
211
3.34M
                if (!have_tt) goto st_y;
212
3.34M
                if (sbrow_start && by == by_start) {
213
194k
                    if (resize) {
214
93.5k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
93.5k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
101k
                    } else {
217
101k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
101k
                        top = &f->lf.lr_lpf_line[0][offset];
219
101k
                    }
220
194k
                    bot = bptrs[0] + 8 * y_stride;
221
3.29M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
436k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
436k
                    if (resize) {
224
186k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
186k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
250k
                    } else {
227
250k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
250k
                        offset = line * y_stride + bx * 4;
229
250k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
250k
                    }
231
2.70M
                } else {
232
2.85M
            st_y:;
233
2.85M
                    offset = sby * 4 * y_stride;
234
2.85M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
2.85M
                    bot = bptrs[0] + 8 * y_stride;
236
2.85M
                }
237
3.48M
                if (y_pri_lvl) {
238
3.01M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
3.01M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
2.67M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
2.67M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
2.67M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
3.01M
                } else if (y_sec_lvl)
244
186k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
186k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
186k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
3.48M
                if (!uv_lvl) goto skip_uv;
249
2.96M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
2.96M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
8.47M
                for (int pl = 1; pl <= 2; pl++) {
253
5.41M
                    if (!have_tt) goto st_uv;
254
5.41M
                    if (sbrow_start && by == by_start) {
255
318k
                        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
160k
                        } else {
259
158k
                            const int line = sby * (4 << sb128) - 4;
260
158k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
158k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
158k
                        }
263
318k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
5.14M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
682k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
682k
                                                     (bx * 4 >> ss_hor);
267
682k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
682k
                        if (resize) {
269
298k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
298k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
384k
                        } else {
272
384k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
384k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
384k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
384k
                        }
276
4.41M
                    } else {
277
4.50M
                st_uv:;
278
4.50M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
4.50M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
4.50M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
4.50M
                    }
282
5.50M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
5.50M
                                         lr_bak[bit][pl], top, bot,
284
5.50M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
5.50M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
5.50M
                }
287
288
3.31M
            skip_uv:
289
3.31M
                bit ^= 1;
290
291
3.54M
            next_b:
292
3.54M
                bptrs[0] += 8;
293
3.54M
                bptrs[1] += 8 >> ss_hor;
294
3.54M
                bptrs[2] += 8 >> ss_hor;
295
3.54M
            }
296
297
3.32M
        next_sb:
298
3.32M
            iptrs[0] += sbsz * 4;
299
3.32M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
3.32M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
3.32M
        }
302
303
1.31M
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
1.31M
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
1.31M
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
1.31M
        tc->top_pre_cdef_toggle ^= 1;
307
1.31M
    }
308
298k
}