Coverage Report

Created: 2026-09-13 06:34

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.65M
{
45
1.65M
    const ptrdiff_t y_stride = PXSTRIDE(stride[0]);
46
1.65M
    if (y_stride < 0)
47
0
        pixel_copy(dst[0] + y_stride, src[0] + 7 * y_stride, -2 * y_stride);
48
1.65M
    else
49
1.65M
        pixel_copy(dst[0], src[0] + 6 * y_stride, 2 * y_stride);
50
51
1.65M
    if (layout != DAV1D_PIXEL_LAYOUT_I400) {
52
1.30M
        const ptrdiff_t uv_stride = PXSTRIDE(stride[1]);
53
1.30M
        if (uv_stride < 0) {
54
0
            const int uv_off = layout == DAV1D_PIXEL_LAYOUT_I420 ? 3 : 7;
55
0
            pixel_copy(dst[1] + uv_stride, src[1] + uv_off * uv_stride, -2 * uv_stride);
56
0
            pixel_copy(dst[2] + uv_stride, src[2] + uv_off * uv_stride, -2 * uv_stride);
57
1.30M
        } else {
58
1.30M
            const int uv_off = layout == DAV1D_PIXEL_LAYOUT_I420 ? 2 : 6;
59
1.30M
            pixel_copy(dst[1], src[1] + uv_off * uv_stride, 2 * uv_stride);
60
1.30M
            pixel_copy(dst[2], src[2] + uv_off * uv_stride, 2 * uv_stride);
61
1.30M
        }
62
1.30M
    }
63
1.65M
}
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.55M
{
71
5.55M
    ptrdiff_t y_off = 0;
72
5.55M
    if (flag & BACKUP_2X8_Y) {
73
44.4M
        for (int y = 0; y < 8; y++, y_off += PXSTRIDE(src_stride[0]))
74
39.5M
            pixel_copy(dst[0][y], &src[0][y_off + x_off - 2], 2);
75
4.95M
    }
76
77
5.55M
    if (layout == DAV1D_PIXEL_LAYOUT_I400 || !(flag & BACKUP_2X8_UV))
78
782k
        return;
79
80
4.77M
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
81
4.77M
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
82
83
4.77M
    x_off >>= ss_hor;
84
4.77M
    y_off = 0;
85
31.7M
    for (int y = 0; y < (8 >> ss_ver); y++, y_off += PXSTRIDE(src_stride[1])) {
86
27.0M
        pixel_copy(dst[1][y], &src[1][y_off + x_off - 2], 2);
87
27.0M
        pixel_copy(dst[2][y], &src[2][y_off + x_off - 2], 2);
88
27.0M
    }
89
4.77M
}
90
91
5.17M
static int adjust_strength(const int strength, const unsigned var) {
92
5.17M
    if (!var) return 0;
93
2.37M
    const int i = var >> 6 ? imin(ulog2(var >> 6), 12) : 0;
94
2.37M
    return (strength * (4 + i) + 8) >> 4;
95
5.17M
}
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
426k
{
103
426k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
426k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
426k
    const Dav1dDSPContext *const dsp = f->dsp;
106
426k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
426k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
426k
    const int sbsz = 16;
109
426k
    const int sb64w = f->sb128w << 1;
110
426k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
426k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
426k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
426k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
426k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
426k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
426k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
426k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
426k
    const int have_tt = f->c->n_tc > 1;
119
426k
    const int sb128 = f->seq_hdr->sb128;
120
426k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
426k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
426k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
2.35M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
1.93M
        const int tf = tc->top_pre_cdef_toggle;
126
1.93M
        const int by_idx = (by & 30) >> 1;
127
1.93M
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
1.93M
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
1.65M
            edges & CDEF_HAVE_BOTTOM)
131
1.65M
        {
132
            // backup pre-filter data for next iteration
133
1.65M
            pixel *const cdef_top_bak[3] = {
134
1.65M
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
1.65M
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
1.65M
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
1.65M
            };
138
1.65M
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
1.65M
        }
140
141
1.93M
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
1.93M
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
1.93M
        edges &= ~CDEF_HAVE_LEFT;
144
1.93M
        edges |= CDEF_HAVE_RIGHT;
145
1.93M
        enum Backup2x8Flags prev_flag = 0;
146
7.07M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
5.14M
            const int sb128x = sbx >> 1;
148
5.14M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
5.14M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
5.14M
            if (cdef_idx == -1 ||
151
2.78M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
661k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
2.87M
            {
154
2.87M
                prev_flag = 0;
155
2.87M
                goto next_sb;
156
2.87M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
2.26M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
2.26M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
2.26M
                                                    noskip_row[0][0];
162
163
2.26M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
2.26M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
2.26M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
2.26M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
2.26M
            int y_sec_lvl = y_lvl & 3;
169
2.26M
            y_sec_lvl += y_sec_lvl == 3;
170
2.26M
            y_sec_lvl <<= bitdepth_min_8;
171
172
2.26M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
2.26M
            int uv_sec_lvl = uv_lvl & 3;
174
2.26M
            uv_sec_lvl += uv_sec_lvl == 3;
175
2.26M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
2.26M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
9.05M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
6.78M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
6.81M
            {
181
6.81M
                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.81M
                const uint32_t bx_mask = 3U << (bx & 30);
186
6.81M
                if (!(noskip_mask & bx_mask)) {
187
629k
                    prev_flag = 0;
188
629k
                    goto next_b;
189
629k
                }
190
6.18M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
6.18M
                prev_flag = flag;
192
6.18M
                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
79.2k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
79.2k
                }
197
6.18M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
5.48M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
5.48M
                }
201
202
6.18M
                int dir;
203
6.18M
                unsigned variance;
204
6.18M
                if (y_pri_lvl || uv_pri_lvl)
205
5.99M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
5.99M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
6.18M
                const pixel *top, *bot;
209
6.18M
                ptrdiff_t offset;
210
211
6.18M
                if (!have_tt) goto st_y;
212
6.18M
                if (sbrow_start && by == by_start) {
213
411k
                    if (resize) {
214
111k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
111k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
299k
                    } else {
217
299k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
299k
                        top = &f->lf.lr_lpf_line[0][offset];
219
299k
                    }
220
411k
                    bot = bptrs[0] + 8 * y_stride;
221
5.94M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
813k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
813k
                    if (resize) {
224
268k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
268k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
545k
                    } else {
227
545k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
545k
                        offset = line * y_stride + bx * 4;
229
545k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
545k
                    }
231
4.95M
                } else {
232
5.12M
            st_y:;
233
5.12M
                    offset = sby * 4 * y_stride;
234
5.12M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
5.12M
                    bot = bptrs[0] + 8 * y_stride;
236
5.12M
                }
237
6.35M
                if (y_pri_lvl) {
238
5.17M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
5.17M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
4.30M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
4.30M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
4.30M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
5.17M
                } else if (y_sec_lvl)
244
517k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
517k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
517k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
6.35M
                if (!uv_lvl) goto skip_uv;
249
5.48M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
5.48M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
15.9M
                for (int pl = 1; pl <= 2; pl++) {
253
10.3M
                    if (!have_tt) goto st_uv;
254
10.3M
                    if (sbrow_start && by == by_start) {
255
684k
                        if (resize) {
256
198k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
198k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
486k
                        } else {
259
486k
                            const int line = sby * (4 << sb128) - 4;
260
486k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
486k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
486k
                        }
263
684k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
9.74M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
1.32M
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
1.32M
                                                     (bx * 4 >> ss_hor);
267
1.32M
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
1.32M
                        if (resize) {
269
424k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
424k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
897k
                        } else {
272
897k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
897k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
897k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
897k
                        }
276
8.37M
                    } else {
277
8.48M
                st_uv:;
278
8.48M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
8.48M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
8.48M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
8.48M
                    }
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.15M
            skip_uv:
289
6.15M
                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
5.13M
        next_sb:
298
5.13M
            iptrs[0] += sbsz * 4;
299
5.13M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
5.13M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
5.13M
        }
302
303
1.93M
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
1.93M
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
1.93M
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
1.93M
        tc->top_pre_cdef_toggle ^= 1;
307
1.93M
    }
308
426k
}
dav1d_cdef_brow_8bpc
Line
Count
Source
102
233k
{
103
233k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
18.4E
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
233k
    const Dav1dDSPContext *const dsp = f->dsp;
106
233k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
233k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
233k
    const int sbsz = 16;
109
233k
    const int sb64w = f->sb128w << 1;
110
233k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
233k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
233k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
233k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
233k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
233k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
233k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
233k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
233k
    const int have_tt = f->c->n_tc > 1;
119
233k
    const int sb128 = f->seq_hdr->sb128;
120
233k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
233k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
233k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
1.27M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
1.04M
        const int tf = tc->top_pre_cdef_toggle;
126
1.04M
        const int by_idx = (by & 30) >> 1;
127
1.04M
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
1.04M
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
892k
            edges & CDEF_HAVE_BOTTOM)
131
892k
        {
132
            // backup pre-filter data for next iteration
133
892k
            pixel *const cdef_top_bak[3] = {
134
892k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
892k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
892k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
892k
            };
138
892k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
892k
        }
140
141
1.04M
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
1.04M
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
1.04M
        edges &= ~CDEF_HAVE_LEFT;
144
1.04M
        edges |= CDEF_HAVE_RIGHT;
145
1.04M
        enum Backup2x8Flags prev_flag = 0;
146
3.78M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
2.74M
            const int sb128x = sbx >> 1;
148
2.74M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
2.74M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
2.74M
            if (cdef_idx == -1 ||
151
1.59M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
359k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.42M
            {
154
1.42M
                prev_flag = 0;
155
1.42M
                goto next_sb;
156
1.42M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
1.31M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
1.31M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
1.31M
                                                    noskip_row[0][0];
162
163
1.31M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
1.31M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
1.31M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
1.31M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
1.31M
            int y_sec_lvl = y_lvl & 3;
169
1.31M
            y_sec_lvl += y_sec_lvl == 3;
170
1.31M
            y_sec_lvl <<= bitdepth_min_8;
171
172
1.31M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
1.31M
            int uv_sec_lvl = uv_lvl & 3;
174
1.31M
            uv_sec_lvl += uv_sec_lvl == 3;
175
1.31M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
1.31M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
5.02M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
3.71M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
3.73M
            {
181
3.73M
                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.73M
                const uint32_t bx_mask = 3U << (bx & 30);
186
3.73M
                if (!(noskip_mask & bx_mask)) {
187
414k
                    prev_flag = 0;
188
414k
                    goto next_b;
189
414k
                }
190
3.32M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
3.32M
                prev_flag = flag;
192
3.32M
                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
45.6k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
45.6k
                }
197
3.32M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.98M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.98M
                }
201
202
3.32M
                int dir;
203
3.32M
                unsigned variance;
204
3.32M
                if (y_pri_lvl || uv_pri_lvl)
205
3.25M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
3.25M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
3.32M
                const pixel *top, *bot;
209
3.32M
                ptrdiff_t offset;
210
211
3.32M
                if (!have_tt) goto st_y;
212
3.32M
                if (sbrow_start && by == by_start) {
213
262k
                    if (resize) {
214
43.6k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
43.6k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
218k
                    } else {
217
218k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
218k
                        top = &f->lf.lr_lpf_line[0][offset];
219
218k
                    }
220
262k
                    bot = bptrs[0] + 8 * y_stride;
221
3.18M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
454k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
454k
                    if (resize) {
224
108k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
108k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
345k
                    } else {
227
345k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
345k
                        offset = line * y_stride + bx * 4;
229
345k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
345k
                    }
231
2.60M
                } else {
232
2.73M
            st_y:;
233
2.73M
                    offset = sby * 4 * y_stride;
234
2.73M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
2.73M
                    bot = bptrs[0] + 8 * y_stride;
236
2.73M
                }
237
3.45M
                if (y_pri_lvl) {
238
2.87M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
2.87M
                    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.87M
                } else if (y_sec_lvl)
244
188k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
188k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
188k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
3.45M
                if (!uv_lvl) goto skip_uv;
249
3.04M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
3.04M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
8.77M
                for (int pl = 1; pl <= 2; pl++) {
253
5.64M
                    if (!have_tt) goto st_uv;
254
5.64M
                    if (sbrow_start && by == by_start) {
255
435k
                        if (resize) {
256
80.4k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
80.4k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
355k
                        } else {
259
355k
                            const int line = sby * (4 << sb128) - 4;
260
355k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
355k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
355k
                        }
263
435k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
5.24M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
749k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
749k
                                                     (bx * 4 >> ss_hor);
267
749k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
749k
                        if (resize) {
269
162k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
162k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
586k
                        } else {
272
586k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
586k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
586k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
586k
                        }
276
4.46M
                    } else {
277
4.54M
                st_uv:;
278
4.54M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
4.54M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
4.54M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
4.54M
                    }
282
5.73M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
5.73M
                                         lr_bak[bit][pl], top, bot,
284
5.73M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
5.73M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
5.73M
                }
287
288
3.30M
            skip_uv:
289
3.30M
                bit ^= 1;
290
291
3.71M
            next_b:
292
3.71M
                bptrs[0] += 8;
293
3.71M
                bptrs[1] += 8 >> ss_hor;
294
3.71M
                bptrs[2] += 8 >> ss_hor;
295
3.71M
            }
296
297
2.74M
        next_sb:
298
2.74M
            iptrs[0] += sbsz * 4;
299
2.74M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
2.74M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
2.74M
        }
302
303
1.03M
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
1.03M
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
1.03M
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
1.03M
        tc->top_pre_cdef_toggle ^= 1;
307
1.03M
    }
308
233k
}
dav1d_cdef_brow_16bpc
Line
Count
Source
102
193k
{
103
193k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
193k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
193k
    const Dav1dDSPContext *const dsp = f->dsp;
106
193k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
193k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
193k
    const int sbsz = 16;
109
193k
    const int sb64w = f->sb128w << 1;
110
193k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
193k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
193k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
193k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
193k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
193k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
193k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
193k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
193k
    const int have_tt = f->c->n_tc > 1;
119
193k
    const int sb128 = f->seq_hdr->sb128;
120
193k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
193k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
193k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
1.08M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
892k
        const int tf = tc->top_pre_cdef_toggle;
126
892k
        const int by_idx = (by & 30) >> 1;
127
892k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
892k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
759k
            edges & CDEF_HAVE_BOTTOM)
131
759k
        {
132
            // backup pre-filter data for next iteration
133
759k
            pixel *const cdef_top_bak[3] = {
134
759k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
759k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
759k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
759k
            };
138
759k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
759k
        }
140
141
892k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
892k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
892k
        edges &= ~CDEF_HAVE_LEFT;
144
892k
        edges |= CDEF_HAVE_RIGHT;
145
892k
        enum Backup2x8Flags prev_flag = 0;
146
3.29M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
2.39M
            const int sb128x = sbx >> 1;
148
2.39M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
2.39M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
2.39M
            if (cdef_idx == -1 ||
151
1.19M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
302k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.44M
            {
154
1.44M
                prev_flag = 0;
155
1.44M
                goto next_sb;
156
1.44M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
955k
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
955k
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
955k
                                                    noskip_row[0][0];
162
163
955k
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
955k
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
955k
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
955k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
955k
            int y_sec_lvl = y_lvl & 3;
169
955k
            y_sec_lvl += y_sec_lvl == 3;
170
955k
            y_sec_lvl <<= bitdepth_min_8;
171
172
955k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
955k
            int uv_sec_lvl = uv_lvl & 3;
174
955k
            uv_sec_lvl += uv_sec_lvl == 3;
175
955k
            uv_sec_lvl <<= bitdepth_min_8;
176
177
955k
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
4.02M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
3.06M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
3.07M
            {
181
3.07M
                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.07M
                const uint32_t bx_mask = 3U << (bx & 30);
186
3.07M
                if (!(noskip_mask & bx_mask)) {
187
214k
                    prev_flag = 0;
188
214k
                    goto next_b;
189
214k
                }
190
2.86M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
2.86M
                prev_flag = flag;
192
2.86M
                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
33.5k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
33.5k
                }
197
2.86M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.49M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.49M
                }
201
202
2.86M
                int dir;
203
2.86M
                unsigned variance;
204
2.86M
                if (y_pri_lvl || uv_pri_lvl)
205
2.73M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
2.73M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
2.86M
                const pixel *top, *bot;
209
2.86M
                ptrdiff_t offset;
210
211
2.86M
                if (!have_tt) goto st_y;
212
2.86M
                if (sbrow_start && by == by_start) {
213
148k
                    if (resize) {
214
67.7k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
67.7k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
81.2k
                    } else {
217
81.2k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
81.2k
                        top = &f->lf.lr_lpf_line[0][offset];
219
81.2k
                    }
220
148k
                    bot = bptrs[0] + 8 * y_stride;
221
2.75M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
359k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
359k
                    if (resize) {
224
159k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
159k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
200k
                    } else {
227
200k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
200k
                        offset = line * y_stride + bx * 4;
229
200k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
200k
                    }
231
2.35M
                } 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.30M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
2.30M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
1.99M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
1.99M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
1.99M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
2.30M
                } else if (y_sec_lvl)
244
329k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
329k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
329k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
2.90M
                if (!uv_lvl) goto skip_uv;
249
2.44M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
2.44M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
7.19M
                for (int pl = 1; pl <= 2; pl++) {
253
4.73M
                    if (!have_tt) goto st_uv;
254
4.73M
                    if (sbrow_start && by == by_start) {
255
248k
                        if (resize) {
256
117k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
117k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
131k
                        } else {
259
131k
                            const int line = sby * (4 << sb128) - 4;
260
131k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
131k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
131k
                        }
263
248k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
4.49M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
572k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
572k
                                                     (bx * 4 >> ss_hor);
267
572k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
572k
                        if (resize) {
269
261k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
261k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
310k
                        } else {
272
310k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
310k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
310k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
310k
                        }
276
3.91M
                    } else {
277
3.93M
                st_uv:;
278
3.93M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
3.93M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
3.93M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
3.93M
                    }
282
4.75M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
4.75M
                                         lr_bak[bit][pl], top, bot,
284
4.75M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
4.75M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
4.75M
                }
287
288
2.85M
            skip_uv:
289
2.85M
                bit ^= 1;
290
291
3.06M
            next_b:
292
3.06M
                bptrs[0] += 8;
293
3.06M
                bptrs[1] += 8 >> ss_hor;
294
3.06M
                bptrs[2] += 8 >> ss_hor;
295
3.06M
            }
296
297
2.39M
        next_sb:
298
2.39M
            iptrs[0] += sbsz * 4;
299
2.39M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
2.39M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
2.39M
        }
302
303
891k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
891k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
891k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
891k
        tc->top_pre_cdef_toggle ^= 1;
307
891k
    }
308
193k
}