Coverage Report

Created: 2026-06-15 06:25

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
772k
        const ptrdiff_t uv_stride = PXSTRIDE(stride[1]);
53
772k
        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
772k
        } else {
58
772k
            const int uv_off = layout == DAV1D_PIXEL_LAYOUT_I420 ? 2 : 6;
59
772k
            pixel_copy(dst[1], src[1] + uv_off * uv_stride, 2 * uv_stride);
60
772k
            pixel_copy(dst[2], src[2] + uv_off * uv_stride, 2 * uv_stride);
61
772k
        }
62
772k
    }
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.85M
{
71
4.85M
    ptrdiff_t y_off = 0;
72
4.85M
    if (flag & BACKUP_2X8_Y) {
73
40.9M
        for (int y = 0; y < 8; y++, y_off += PXSTRIDE(src_stride[0]))
74
36.4M
            pixel_copy(dst[0][y], &src[0][y_off + x_off - 2], 2);
75
4.55M
    }
76
77
4.85M
    if (layout == DAV1D_PIXEL_LAYOUT_I400 || !(flag & BACKUP_2X8_UV))
78
1.58M
        return;
79
80
3.27M
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
81
3.27M
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
82
83
3.27M
    x_off >>= ss_hor;
84
3.27M
    y_off = 0;
85
23.3M
    for (int y = 0; y < (8 >> ss_ver); y++, y_off += PXSTRIDE(src_stride[1])) {
86
20.0M
        pixel_copy(dst[1][y], &src[1][y_off + x_off - 2], 2);
87
20.0M
        pixel_copy(dst[2][y], &src[2][y_off + x_off - 2], 2);
88
20.0M
    }
89
3.27M
}
90
91
4.26M
static int adjust_strength(const int strength, const unsigned var) {
92
4.26M
    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.26M
}
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
257k
{
103
257k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
257k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
257k
    const Dav1dDSPContext *const dsp = f->dsp;
106
257k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
257k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
257k
    const int sbsz = 16;
109
257k
    const int sb64w = f->sb128w << 1;
110
257k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
257k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
257k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
257k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
257k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
257k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
257k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
257k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
257k
    const int have_tt = f->c->n_tc > 1;
119
257k
    const int sb128 = f->seq_hdr->sb128;
120
257k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
257k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
257k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
1.52M
    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.46M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
4.19M
            const int sb128x = sbx >> 1;
148
4.19M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
4.19M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
4.19M
            if (cdef_idx == -1 ||
151
2.16M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
914k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
2.83M
            {
154
2.83M
                prev_flag = 0;
155
2.83M
                goto next_sb;
156
2.83M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
1.36M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
1.36M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
1.36M
                                                    noskip_row[0][0];
162
163
1.36M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
1.36M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
1.36M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
1.36M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
1.36M
            int y_sec_lvl = y_lvl & 3;
169
1.36M
            y_sec_lvl += y_sec_lvl == 3;
170
1.36M
            y_sec_lvl <<= bitdepth_min_8;
171
172
1.36M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
1.36M
            int uv_sec_lvl = uv_lvl & 3;
174
1.36M
            uv_sec_lvl += uv_sec_lvl == 3;
175
1.36M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
1.36M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
6.90M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
5.53M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
5.58M
            {
181
5.58M
                if (bx + 2 >= f->bw) edges &= ~CDEF_HAVE_RIGHT;
182
183
                // check if this 8x8 block had any coded coefficients; if not,
184
                // go to the next block
185
5.58M
                const uint32_t bx_mask = 3U << (bx & 30);
186
5.58M
                if (!(noskip_mask & bx_mask)) {
187
358k
                    prev_flag = 0;
188
358k
                    goto next_b;
189
358k
                }
190
5.22M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
5.22M
                prev_flag = flag;
192
5.22M
                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
65.7k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
65.7k
                }
197
5.22M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
4.79M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
4.79M
                }
201
202
5.22M
                int dir;
203
5.22M
                unsigned variance;
204
5.22M
                if (y_pri_lvl || uv_pri_lvl)
205
4.99M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
4.99M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
5.22M
                const pixel *top, *bot;
209
5.22M
                ptrdiff_t offset;
210
211
5.22M
                if (!have_tt) goto st_y;
212
5.22M
                if (sbrow_start && by == by_start) {
213
326k
                    if (resize) {
214
156k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
156k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
170k
                    } else {
217
170k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
170k
                        top = &f->lf.lr_lpf_line[0][offset];
219
170k
                    }
220
326k
                    bot = bptrs[0] + 8 * y_stride;
221
5.05M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
648k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
648k
                    if (resize) {
224
309k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
309k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
339k
                    } else {
227
339k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
339k
                        offset = line * y_stride + bx * 4;
229
339k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
339k
                    }
231
4.25M
                } else {
232
4.40M
            st_y:;
233
4.40M
                    offset = sby * 4 * y_stride;
234
4.40M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
4.40M
                    bot = bptrs[0] + 8 * y_stride;
236
4.40M
                }
237
5.38M
                if (y_pri_lvl) {
238
4.26M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
4.26M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
3.50M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
3.50M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
3.50M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
4.26M
                } else if (y_sec_lvl)
244
791k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
791k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
791k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
5.38M
                if (!uv_lvl) goto skip_uv;
249
3.76M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
3.76M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
10.7M
                for (int pl = 1; pl <= 2; pl++) {
253
6.93M
                    if (!have_tt) goto st_uv;
254
6.93M
                    if (sbrow_start && by == by_start) {
255
404k
                        if (resize) {
256
163k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
163k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
241k
                        } else {
259
241k
                            const int line = sby * (4 << sb128) - 4;
260
241k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
241k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
241k
                        }
263
404k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
6.54M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
892k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
892k
                                                     (bx * 4 >> ss_hor);
267
892k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
892k
                        if (resize) {
269
368k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
368k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
523k
                        } else {
272
523k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
523k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
523k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
523k
                        }
276
5.63M
                    } else {
277
5.68M
                st_uv:;
278
5.68M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
5.68M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
5.68M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
5.68M
                    }
282
6.98M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
6.98M
                                         lr_bak[bit][pl], top, bot,
284
6.98M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
6.98M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
6.98M
                }
287
288
5.14M
            skip_uv:
289
5.14M
                bit ^= 1;
290
291
5.53M
            next_b:
292
5.53M
                bptrs[0] += 8;
293
5.53M
                bptrs[1] += 8 >> ss_hor;
294
5.53M
                bptrs[2] += 8 >> ss_hor;
295
5.53M
            }
296
297
4.19M
        next_sb:
298
4.19M
            iptrs[0] += sbsz * 4;
299
4.19M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
4.19M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
4.19M
        }
302
303
1.26M
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
1.26M
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
1.26M
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
1.26M
        tc->top_pre_cdef_toggle ^= 1;
307
1.26M
    }
308
257k
}
dav1d_cdef_brow_8bpc
Line
Count
Source
102
115k
{
103
115k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
115k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
115k
    const Dav1dDSPContext *const dsp = f->dsp;
106
115k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
115k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
115k
    const int sbsz = 16;
109
115k
    const int sb64w = f->sb128w << 1;
110
115k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
115k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
115k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
115k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
115k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
115k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
115k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
115k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
115k
    const int have_tt = f->c->n_tc > 1;
119
115k
    const int sb128 = f->seq_hdr->sb128;
120
115k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
115k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
115k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
675k
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
562k
        const int tf = tc->top_pre_cdef_toggle;
126
562k
        const int by_idx = (by & 30) >> 1;
127
562k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
562k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
488k
            edges & CDEF_HAVE_BOTTOM)
131
488k
        {
132
            // backup pre-filter data for next iteration
133
488k
            pixel *const cdef_top_bak[3] = {
134
488k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
488k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
488k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
488k
            };
138
488k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
488k
        }
140
141
562k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
562k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
562k
        edges &= ~CDEF_HAVE_LEFT;
144
562k
        edges |= CDEF_HAVE_RIGHT;
145
562k
        enum Backup2x8Flags prev_flag = 0;
146
2.46M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
1.90M
            const int sb128x = sbx >> 1;
148
1.90M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
1.90M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
1.90M
            if (cdef_idx == -1 ||
151
936k
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
329k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.24M
            {
154
1.24M
                prev_flag = 0;
155
1.24M
                goto next_sb;
156
1.24M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
656k
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
656k
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
656k
                                                    noskip_row[0][0];
162
163
656k
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
656k
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
656k
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
656k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
656k
            int y_sec_lvl = y_lvl & 3;
169
656k
            y_sec_lvl += y_sec_lvl == 3;
170
656k
            y_sec_lvl <<= bitdepth_min_8;
171
172
656k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
656k
            int uv_sec_lvl = uv_lvl & 3;
174
656k
            uv_sec_lvl += uv_sec_lvl == 3;
175
656k
            uv_sec_lvl <<= bitdepth_min_8;
176
177
656k
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
3.22M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
2.57M
                 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.2k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
27.2k
                }
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.13M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
2.13M
                                        &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
154k
                    if (resize) {
214
52.3k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
52.3k
                        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
154k
                    bot = bptrs[0] + 8 * y_stride;
221
2.32M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
313k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
313k
                    if (resize) {
224
99.4k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
99.4k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
214k
                    } else {
227
214k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
214k
                        offset = line * y_stride + bx * 4;
229
214k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
214k
                    }
231
1.94M
                } else {
232
2.01M
            st_y:;
233
2.01M
                    offset = sby * 4 * y_stride;
234
2.01M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
2.01M
                    bot = bptrs[0] + 8 * y_stride;
236
2.01M
                }
237
2.47M
                if (y_pri_lvl) {
238
1.64M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
1.64M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
1.19M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
1.19M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
1.19M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
1.64M
                } else if (y_sec_lvl)
244
610k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
610k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
610k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
2.47M
                if (!uv_lvl) goto skip_uv;
249
1.68M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
1.68M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
4.83M
                for (int pl = 1; pl <= 2; pl++) {
253
3.11M
                    if (!have_tt) goto st_uv;
254
3.11M
                    if (sbrow_start && by == by_start) {
255
181k
                        if (resize) {
256
50.6k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
50.6k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
130k
                        } else {
259
130k
                            const int line = sby * (4 << sb128) - 4;
260
130k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
130k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
130k
                        }
263
181k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
2.95M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
439k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
439k
                                                     (bx * 4 >> ss_hor);
267
439k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
439k
                        if (resize) {
269
112k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
112k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
326k
                        } else {
272
326k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
326k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
326k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
326k
                        }
276
2.49M
                    } else {
277
2.53M
                st_uv:;
278
2.53M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
2.53M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
2.53M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
2.53M
                    }
282
3.15M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
3.15M
                                         lr_bak[bit][pl], top, bot,
284
3.15M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
3.15M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
3.15M
                }
287
288
2.39M
            skip_uv:
289
2.39M
                bit ^= 1;
290
291
2.57M
            next_b:
292
2.57M
                bptrs[0] += 8;
293
2.57M
                bptrs[1] += 8 >> ss_hor;
294
2.57M
                bptrs[2] += 8 >> ss_hor;
295
2.57M
            }
296
297
1.90M
        next_sb:
298
1.90M
            iptrs[0] += sbsz * 4;
299
1.90M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
1.90M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
1.90M
        }
302
303
560k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
560k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
560k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
560k
        tc->top_pre_cdef_toggle ^= 1;
307
560k
    }
308
115k
}
dav1d_cdef_brow_16bpc
Line
Count
Source
102
142k
{
103
142k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
142k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
142k
    const Dav1dDSPContext *const dsp = f->dsp;
106
142k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
142k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
142k
    const int sbsz = 16;
109
142k
    const int sb64w = f->sb128w << 1;
110
142k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
142k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
142k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
142k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
142k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
142k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
142k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
142k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
142k
    const int have_tt = f->c->n_tc > 1;
119
142k
    const int sb128 = f->seq_hdr->sb128;
120
142k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
142k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
142k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
845k
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
707k
        const int tf = tc->top_pre_cdef_toggle;
126
707k
        const int by_idx = (by & 30) >> 1;
127
707k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
708k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
623k
            edges & CDEF_HAVE_BOTTOM)
131
623k
        {
132
            // backup pre-filter data for next iteration
133
623k
            pixel *const cdef_top_bak[3] = {
134
623k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
623k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
623k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
623k
            };
138
623k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
623k
        }
140
141
707k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
707k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
707k
        edges &= ~CDEF_HAVE_LEFT;
144
707k
        edges |= CDEF_HAVE_RIGHT;
145
707k
        enum Backup2x8Flags prev_flag = 0;
146
2.99M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
2.29M
            const int sb128x = sbx >> 1;
148
2.29M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
2.29M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
2.29M
            if (cdef_idx == -1 ||
151
1.22M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
584k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.58M
            {
154
1.58M
                prev_flag = 0;
155
1.58M
                goto next_sb;
156
1.58M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
707k
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
707k
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
707k
                                                    noskip_row[0][0];
162
163
707k
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
707k
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
707k
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
707k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
707k
            int y_sec_lvl = y_lvl & 3;
169
707k
            y_sec_lvl += y_sec_lvl == 3;
170
707k
            y_sec_lvl <<= bitdepth_min_8;
171
172
707k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
707k
            int uv_sec_lvl = uv_lvl & 3;
174
707k
            uv_sec_lvl += uv_sec_lvl == 3;
175
707k
            uv_sec_lvl <<= bitdepth_min_8;
176
177
707k
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
3.67M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
2.96M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
2.99M
            {
181
2.99M
                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.99M
                const uint32_t bx_mask = 3U << (bx & 30);
186
2.99M
                if (!(noskip_mask & bx_mask)) {
187
179k
                    prev_flag = 0;
188
179k
                    goto next_b;
189
179k
                }
190
2.81M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
2.81M
                prev_flag = flag;
192
2.81M
                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.5k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
38.5k
                }
197
2.81M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.60M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.60M
                }
201
202
2.81M
                int dir;
203
2.81M
                unsigned variance;
204
2.81M
                if (y_pri_lvl || uv_pri_lvl)
205
2.85M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
2.85M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
2.81M
                const pixel *top, *bot;
209
2.81M
                ptrdiff_t offset;
210
211
2.81M
                if (!have_tt) goto st_y;
212
2.81M
                if (sbrow_start && by == by_start) {
213
172k
                    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.3k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
68.3k
                        top = &f->lf.lr_lpf_line[0][offset];
219
68.3k
                    }
220
172k
                    bot = bptrs[0] + 8 * y_stride;
221
2.72M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
334k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
334k
                    if (resize) {
224
209k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
209k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
209k
                    } else {
227
125k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
125k
                        offset = line * y_stride + bx * 4;
229
125k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
125k
                    }
231
2.31M
                } else {
232
2.39M
            st_y:;
233
2.39M
                    offset = sby * 4 * y_stride;
234
2.39M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
2.39M
                    bot = bptrs[0] + 8 * y_stride;
236
2.39M
                }
237
2.90M
                if (y_pri_lvl) {
238
2.61M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
2.61M
                    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.61M
                } else if (y_sec_lvl)
244
181k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
181k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
181k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
2.90M
                if (!uv_lvl) goto skip_uv;
249
2.07M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
2.07M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
5.90M
                for (int pl = 1; pl <= 2; pl++) {
253
3.81M
                    if (!have_tt) goto st_uv;
254
3.81M
                    if (sbrow_start && by == by_start) {
255
223k
                        if (resize) {
256
112k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
112k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
112k
                        } else {
259
110k
                            const int line = sby * (4 << sb128) - 4;
260
110k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
110k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
110k
                        }
263
223k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
3.59M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
452k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
452k
                                                     (bx * 4 >> ss_hor);
267
452k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
452k
                        if (resize) {
269
256k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
256k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
256k
                        } else {
272
196k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
196k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
196k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
196k
                        }
276
3.13M
                    } else {
277
3.15M
                st_uv:;
278
3.15M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
3.15M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
3.15M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
3.15M
                    }
282
3.82M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
3.82M
                                         lr_bak[bit][pl], top, bot,
284
3.82M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
3.82M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
3.82M
                }
287
288
2.75M
            skip_uv:
289
2.75M
                bit ^= 1;
290
291
2.96M
            next_b:
292
2.96M
                bptrs[0] += 8;
293
2.96M
                bptrs[1] += 8 >> ss_hor;
294
2.96M
                bptrs[2] += 8 >> ss_hor;
295
2.96M
            }
296
297
2.28M
        next_sb:
298
2.28M
            iptrs[0] += sbsz * 4;
299
2.28M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
2.28M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
2.28M
        }
302
303
703k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
703k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
703k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
703k
        tc->top_pre_cdef_toggle ^= 1;
307
703k
    }
308
142k
}