Coverage Report

Created: 2026-07-16 06:32

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.04M
{
45
1.04M
    const ptrdiff_t y_stride = PXSTRIDE(stride[0]);
46
1.04M
    if (y_stride < 0)
47
0
        pixel_copy(dst[0] + y_stride, src[0] + 7 * y_stride, -2 * y_stride);
48
1.04M
    else
49
1.04M
        pixel_copy(dst[0], src[0] + 6 * y_stride, 2 * y_stride);
50
51
1.04M
    if (layout != DAV1D_PIXEL_LAYOUT_I400) {
52
729k
        const ptrdiff_t uv_stride = PXSTRIDE(stride[1]);
53
729k
        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
729k
        } else {
58
729k
            const int uv_off = layout == DAV1D_PIXEL_LAYOUT_I420 ? 2 : 6;
59
729k
            pixel_copy(dst[1], src[1] + uv_off * uv_stride, 2 * uv_stride);
60
729k
            pixel_copy(dst[2], src[2] + uv_off * uv_stride, 2 * uv_stride);
61
729k
        }
62
729k
    }
63
1.04M
}
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.83M
{
71
4.83M
    ptrdiff_t y_off = 0;
72
4.83M
    if (flag & BACKUP_2X8_Y) {
73
40.2M
        for (int y = 0; y < 8; y++, y_off += PXSTRIDE(src_stride[0]))
74
35.7M
            pixel_copy(dst[0][y], &src[0][y_off + x_off - 2], 2);
75
4.47M
    }
76
77
4.83M
    if (layout == DAV1D_PIXEL_LAYOUT_I400 || !(flag & BACKUP_2X8_UV))
78
1.22M
        return;
79
80
3.61M
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
81
3.61M
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
82
83
3.61M
    x_off >>= ss_hor;
84
3.61M
    y_off = 0;
85
25.5M
    for (int y = 0; y < (8 >> ss_ver); y++, y_off += PXSTRIDE(src_stride[1])) {
86
21.9M
        pixel_copy(dst[1][y], &src[1][y_off + x_off - 2], 2);
87
21.9M
        pixel_copy(dst[2][y], &src[2][y_off + x_off - 2], 2);
88
21.9M
    }
89
3.61M
}
90
91
4.24M
static int adjust_strength(const int strength, const unsigned var) {
92
4.24M
    if (!var) return 0;
93
1.93M
    const int i = var >> 6 ? imin(ulog2(var >> 6), 12) : 0;
94
1.93M
    return (strength * (4 + i) + 8) >> 4;
95
4.24M
}
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
245k
{
103
245k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
245k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
245k
    const Dav1dDSPContext *const dsp = f->dsp;
106
245k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
245k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
245k
    const int sbsz = 16;
109
245k
    const int sb64w = f->sb128w << 1;
110
245k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
245k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
245k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
245k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
245k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
245k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
245k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
245k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
245k
    const int have_tt = f->c->n_tc > 1;
119
245k
    const int sb128 = f->seq_hdr->sb128;
120
245k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
245k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
245k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
1.44M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
1.20M
        const int tf = tc->top_pre_cdef_toggle;
126
1.20M
        const int by_idx = (by & 30) >> 1;
127
1.20M
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
1.20M
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
1.04M
            edges & CDEF_HAVE_BOTTOM)
131
1.04M
        {
132
            // backup pre-filter data for next iteration
133
1.04M
            pixel *const cdef_top_bak[3] = {
134
1.04M
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
1.04M
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
1.04M
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
1.04M
            };
138
1.04M
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
1.04M
        }
140
141
1.20M
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
1.20M
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
1.20M
        edges &= ~CDEF_HAVE_LEFT;
144
1.20M
        edges |= CDEF_HAVE_RIGHT;
145
1.20M
        enum Backup2x8Flags prev_flag = 0;
146
5.19M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
3.99M
            const int sb128x = sbx >> 1;
148
3.99M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
3.99M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
3.99M
            if (cdef_idx == -1 ||
151
2.06M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
804k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
2.65M
            {
154
2.65M
                prev_flag = 0;
155
2.65M
                goto next_sb;
156
2.65M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
1.34M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
1.34M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
1.34M
                                                    noskip_row[0][0];
162
163
1.34M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
1.34M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
1.34M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
1.34M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
1.34M
            int y_sec_lvl = y_lvl & 3;
169
1.34M
            y_sec_lvl += y_sec_lvl == 3;
170
1.34M
            y_sec_lvl <<= bitdepth_min_8;
171
172
1.34M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
1.34M
            int uv_sec_lvl = uv_lvl & 3;
174
1.34M
            uv_sec_lvl += uv_sec_lvl == 3;
175
1.34M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
1.34M
            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.56M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
5.59M
            {
181
5.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
5.59M
                const uint32_t bx_mask = 3U << (bx & 30);
186
5.59M
                if (!(noskip_mask & bx_mask)) {
187
377k
                    prev_flag = 0;
188
377k
                    goto next_b;
189
377k
                }
190
5.21M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
5.21M
                prev_flag = flag;
192
5.21M
                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.9k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
66.9k
                }
197
5.21M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
4.76M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
4.76M
                }
201
202
5.21M
                int dir;
203
5.21M
                unsigned variance;
204
5.21M
                if (y_pri_lvl || uv_pri_lvl)
205
5.06M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
5.06M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
5.21M
                const pixel *top, *bot;
209
5.21M
                ptrdiff_t offset;
210
211
5.21M
                if (!have_tt) goto st_y;
212
5.21M
                if (sbrow_start && by == by_start) {
213
321k
                    if (resize) {
214
162k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
162k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
162k
                    } else {
217
159k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
159k
                        top = &f->lf.lr_lpf_line[0][offset];
219
159k
                    }
220
321k
                    bot = bptrs[0] + 8 * y_stride;
221
5.00M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
658k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
658k
                    if (resize) {
224
316k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
316k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
342k
                    } else {
227
342k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
342k
                        offset = line * y_stride + bx * 4;
229
342k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
342k
                    }
231
4.23M
                } else {
232
4.35M
            st_y:;
233
4.35M
                    offset = sby * 4 * y_stride;
234
4.35M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
4.35M
                    bot = bptrs[0] + 8 * y_stride;
236
4.35M
                }
237
5.33M
                if (y_pri_lvl) {
238
4.24M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
4.24M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
3.32M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
3.32M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
3.32M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
4.24M
                } else if (y_sec_lvl)
244
701k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
701k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
701k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
5.33M
                if (!uv_lvl) goto skip_uv;
249
4.03M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
4.03M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
11.6M
                for (int pl = 1; pl <= 2; pl++) {
253
7.61M
                    if (!have_tt) goto st_uv;
254
7.61M
                    if (sbrow_start && by == by_start) {
255
457k
                        if (resize) {
256
207k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
207k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
249k
                        } else {
259
249k
                            const int line = sby * (4 << sb128) - 4;
260
249k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
249k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
249k
                        }
263
457k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
7.17M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
978k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
978k
                                                     (bx * 4 >> ss_hor);
267
978k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
978k
                        if (resize) {
269
419k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
419k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
559k
                        } else {
272
559k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
559k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
559k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
559k
                        }
276
6.17M
                    } else {
277
6.22M
                st_uv:;
278
6.22M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
6.22M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
6.22M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
6.22M
                    }
282
7.66M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
7.66M
                                         lr_bak[bit][pl], top, bot,
284
7.66M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
7.66M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
7.66M
                }
287
288
5.16M
            skip_uv:
289
5.16M
                bit ^= 1;
290
291
5.56M
            next_b:
292
5.56M
                bptrs[0] += 8;
293
5.56M
                bptrs[1] += 8 >> ss_hor;
294
5.56M
                bptrs[2] += 8 >> ss_hor;
295
5.56M
            }
296
297
3.98M
        next_sb:
298
3.98M
            iptrs[0] += sbsz * 4;
299
3.98M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
3.98M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
3.98M
        }
302
303
1.20M
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
1.20M
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
1.20M
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
1.20M
        tc->top_pre_cdef_toggle ^= 1;
307
1.20M
    }
308
245k
}
dav1d_cdef_brow_8bpc
Line
Count
Source
102
123k
{
103
123k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
18.4E
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
123k
    const Dav1dDSPContext *const dsp = f->dsp;
106
123k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
123k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
123k
    const int sbsz = 16;
109
123k
    const int sb64w = f->sb128w << 1;
110
123k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
123k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
123k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
123k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
123k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
123k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
123k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
123k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
123k
    const int have_tt = f->c->n_tc > 1;
119
123k
    const int sb128 = f->seq_hdr->sb128;
120
123k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
123k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
123k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
717k
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
595k
        const int tf = tc->top_pre_cdef_toggle;
126
595k
        const int by_idx = (by & 30) >> 1;
127
595k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
595k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
513k
            edges & CDEF_HAVE_BOTTOM)
131
513k
        {
132
            // backup pre-filter data for next iteration
133
513k
            pixel *const cdef_top_bak[3] = {
134
513k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
513k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
513k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
513k
            };
138
513k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
513k
        }
140
141
595k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
595k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
595k
        edges &= ~CDEF_HAVE_LEFT;
144
595k
        edges |= CDEF_HAVE_RIGHT;
145
595k
        enum Backup2x8Flags prev_flag = 0;
146
2.55M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
1.96M
            const int sb128x = sbx >> 1;
148
1.96M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
1.96M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
1.96M
            if (cdef_idx == -1 ||
151
1.02M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
426k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.31M
            {
154
1.31M
                prev_flag = 0;
155
1.31M
                goto next_sb;
156
1.31M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
650k
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
650k
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
650k
                                                    noskip_row[0][0];
162
163
650k
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
650k
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
650k
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
650k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
650k
            int y_sec_lvl = y_lvl & 3;
169
650k
            y_sec_lvl += y_sec_lvl == 3;
170
650k
            y_sec_lvl <<= bitdepth_min_8;
171
172
650k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
650k
            int uv_sec_lvl = uv_lvl & 3;
174
650k
            uv_sec_lvl += uv_sec_lvl == 3;
175
650k
            uv_sec_lvl <<= bitdepth_min_8;
176
177
650k
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
3.19M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
2.54M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
2.55M
            {
181
2.55M
                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.55M
                const uint32_t bx_mask = 3U << (bx & 30);
186
2.55M
                if (!(noskip_mask & bx_mask)) {
187
181k
                    prev_flag = 0;
188
181k
                    goto next_b;
189
181k
                }
190
2.37M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
2.37M
                prev_flag = flag;
192
2.37M
                if (do_left && edges & CDEF_HAVE_LEFT) {
193
                    // we didn't backup the prefilter data because it wasn't
194
                    // there, so do it here instead
195
28.3k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
28.3k
                }
197
2.37M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.14M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.14M
                }
201
202
2.37M
                int dir;
203
2.37M
                unsigned variance;
204
2.37M
                if (y_pri_lvl || uv_pri_lvl)
205
2.22M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
2.22M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
2.37M
                const pixel *top, *bot;
209
2.37M
                ptrdiff_t offset;
210
211
2.37M
                if (!have_tt) goto st_y;
212
2.37M
                if (sbrow_start && by == by_start) {
213
148k
                    if (resize) {
214
56.8k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
56.8k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
91.6k
                    } else {
217
91.6k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
91.6k
                        top = &f->lf.lr_lpf_line[0][offset];
219
91.6k
                    }
220
148k
                    bot = bptrs[0] + 8 * y_stride;
221
2.28M
                } 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
109k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
109k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
207k
                    } else {
227
207k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
207k
                        offset = line * y_stride + bx * 4;
229
207k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
207k
                    }
231
1.90M
                } else {
232
1.96M
            st_y:;
233
1.96M
                    offset = sby * 4 * y_stride;
234
1.96M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
1.96M
                    bot = bptrs[0] + 8 * y_stride;
236
1.96M
                }
237
2.43M
                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.15M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
1.15M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
1.15M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
1.68M
                } else if (y_sec_lvl)
244
477k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
477k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
477k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
2.43M
                if (!uv_lvl) goto skip_uv;
249
1.77M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
1.77M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
5.14M
                for (int pl = 1; pl <= 2; pl++) {
253
3.33M
                    if (!have_tt) goto st_uv;
254
3.33M
                    if (sbrow_start && by == by_start) {
255
198k
                        if (resize) {
256
65.6k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
65.6k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
133k
                        } else {
259
133k
                            const int line = sby * (4 << sb128) - 4;
260
133k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
133k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
133k
                        }
263
198k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
3.15M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
470k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
470k
                                                     (bx * 4 >> ss_hor);
267
470k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
470k
                        if (resize) {
269
132k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
132k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
337k
                        } else {
272
337k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
337k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
337k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
337k
                        }
276
2.67M
                    } else {
277
2.69M
                st_uv:;
278
2.69M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
2.69M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
2.69M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
2.69M
                    }
282
3.36M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
3.36M
                                         lr_bak[bit][pl], top, bot,
284
3.36M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
3.36M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
3.36M
                }
287
288
2.36M
            skip_uv:
289
2.36M
                bit ^= 1;
290
291
2.54M
            next_b:
292
2.54M
                bptrs[0] += 8;
293
2.54M
                bptrs[1] += 8 >> ss_hor;
294
2.54M
                bptrs[2] += 8 >> ss_hor;
295
2.54M
            }
296
297
1.96M
        next_sb:
298
1.96M
            iptrs[0] += sbsz * 4;
299
1.96M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
1.96M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
1.96M
        }
302
303
593k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
593k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
593k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
593k
        tc->top_pre_cdef_toggle ^= 1;
307
593k
    }
308
123k
}
dav1d_cdef_brow_16bpc
Line
Count
Source
102
122k
{
103
122k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
122k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
122k
    const Dav1dDSPContext *const dsp = f->dsp;
106
122k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
122k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
122k
    const int sbsz = 16;
109
122k
    const int sb64w = f->sb128w << 1;
110
122k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
122k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
122k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
122k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
122k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
122k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
122k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
122k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
122k
    const int have_tt = f->c->n_tc > 1;
119
122k
    const int sb128 = f->seq_hdr->sb128;
120
122k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
122k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
122k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
730k
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
610k
        const int tf = tc->top_pre_cdef_toggle;
126
610k
        const int by_idx = (by & 30) >> 1;
127
610k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
611k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
532k
            edges & CDEF_HAVE_BOTTOM)
131
532k
        {
132
            // backup pre-filter data for next iteration
133
532k
            pixel *const cdef_top_bak[3] = {
134
532k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
532k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
532k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
532k
            };
138
532k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
532k
        }
140
141
610k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
610k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
610k
        edges &= ~CDEF_HAVE_LEFT;
144
610k
        edges |= CDEF_HAVE_RIGHT;
145
610k
        enum Backup2x8Flags prev_flag = 0;
146
2.63M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
2.03M
            const int sb128x = sbx >> 1;
148
2.03M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
2.03M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
2.03M
            if (cdef_idx == -1 ||
151
1.03M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
378k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.33M
            {
154
1.33M
                prev_flag = 0;
155
1.33M
                goto next_sb;
156
1.33M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
692k
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
692k
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
692k
                                                    noskip_row[0][0];
162
163
692k
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
692k
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
692k
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
692k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
692k
            int y_sec_lvl = y_lvl & 3;
169
692k
            y_sec_lvl += y_sec_lvl == 3;
170
692k
            y_sec_lvl <<= bitdepth_min_8;
171
172
692k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
692k
            int uv_sec_lvl = uv_lvl & 3;
174
692k
            uv_sec_lvl += uv_sec_lvl == 3;
175
692k
            uv_sec_lvl <<= bitdepth_min_8;
176
177
692k
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
3.71M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
3.01M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
3.03M
            {
181
3.03M
                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.03M
                const uint32_t bx_mask = 3U << (bx & 30);
186
3.03M
                if (!(noskip_mask & bx_mask)) {
187
195k
                    prev_flag = 0;
188
195k
                    goto next_b;
189
195k
                }
190
2.84M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
2.84M
                prev_flag = flag;
192
2.84M
                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.84M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.61M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.61M
                }
201
202
2.84M
                int dir;
203
2.84M
                unsigned variance;
204
2.84M
                if (y_pri_lvl || uv_pri_lvl)
205
2.84M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
2.84M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
2.84M
                const pixel *top, *bot;
209
2.84M
                ptrdiff_t offset;
210
211
2.84M
                if (!have_tt) goto st_y;
212
2.84M
                if (sbrow_start && by == by_start) {
213
173k
                    if (resize) {
214
105k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
105k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
105k
                    } else {
217
67.8k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
67.8k
                        top = &f->lf.lr_lpf_line[0][offset];
219
67.8k
                    }
220
173k
                    bot = bptrs[0] + 8 * y_stride;
221
2.72M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
341k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
341k
                    if (resize) {
224
206k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
206k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
206k
                    } else {
227
134k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
134k
                        offset = line * y_stride + bx * 4;
229
134k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
134k
                    }
231
2.32M
                } 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.90M
                if (y_pri_lvl) {
238
2.55M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
2.55M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
2.16M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
2.16M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
2.16M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
2.55M
                } else if (y_sec_lvl)
244
224k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
224k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
224k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
2.90M
                if (!uv_lvl) goto skip_uv;
249
2.25M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
2.25M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
6.55M
                for (int pl = 1; pl <= 2; pl++) {
253
4.27M
                    if (!have_tt) goto st_uv;
254
4.27M
                    if (sbrow_start && by == by_start) {
255
258k
                        if (resize) {
256
142k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
142k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
142k
                        } else {
259
116k
                            const int line = sby * (4 << sb128) - 4;
260
116k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
116k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
116k
                        }
263
258k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
4.02M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
507k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
507k
                                                     (bx * 4 >> ss_hor);
267
507k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
507k
                        if (resize) {
269
286k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
286k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
286k
                        } else {
272
221k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
221k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
221k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
221k
                        }
276
3.50M
                    } else {
277
3.53M
                st_uv:;
278
3.53M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
3.53M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
3.53M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
3.53M
                    }
282
4.29M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
4.29M
                                         lr_bak[bit][pl], top, bot,
284
4.29M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
4.29M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
4.29M
                }
287
288
2.80M
            skip_uv:
289
2.80M
                bit ^= 1;
290
291
3.01M
            next_b:
292
3.01M
                bptrs[0] += 8;
293
3.01M
                bptrs[1] += 8 >> ss_hor;
294
3.01M
                bptrs[2] += 8 >> ss_hor;
295
3.01M
            }
296
297
2.02M
        next_sb:
298
2.02M
            iptrs[0] += sbsz * 4;
299
2.02M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
2.02M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
2.02M
        }
302
303
607k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
607k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
607k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
607k
        tc->top_pre_cdef_toggle ^= 1;
307
607k
    }
308
122k
}