Coverage Report

Created: 2026-09-14 06:44

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.54M
{
45
1.54M
    const ptrdiff_t y_stride = PXSTRIDE(stride[0]);
46
1.54M
    if (y_stride < 0)
47
0
        pixel_copy(dst[0] + y_stride, src[0] + 7 * y_stride, -2 * y_stride);
48
1.54M
    else
49
1.54M
        pixel_copy(dst[0], src[0] + 6 * y_stride, 2 * y_stride);
50
51
1.54M
    if (layout != DAV1D_PIXEL_LAYOUT_I400) {
52
1.23M
        const ptrdiff_t uv_stride = PXSTRIDE(stride[1]);
53
1.23M
        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.23M
        } else {
58
1.23M
            const int uv_off = layout == DAV1D_PIXEL_LAYOUT_I420 ? 2 : 6;
59
1.23M
            pixel_copy(dst[1], src[1] + uv_off * uv_stride, 2 * uv_stride);
60
1.23M
            pixel_copy(dst[2], src[2] + uv_off * uv_stride, 2 * uv_stride);
61
1.23M
        }
62
1.23M
    }
63
1.54M
}
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.00M
{
71
5.00M
    ptrdiff_t y_off = 0;
72
5.00M
    if (flag & BACKUP_2X8_Y) {
73
39.7M
        for (int y = 0; y < 8; y++, y_off += PXSTRIDE(src_stride[0]))
74
35.3M
            pixel_copy(dst[0][y], &src[0][y_off + x_off - 2], 2);
75
4.42M
    }
76
77
5.00M
    if (layout == DAV1D_PIXEL_LAYOUT_I400 || !(flag & BACKUP_2X8_UV))
78
725k
        return;
79
80
4.27M
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
81
4.27M
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
82
83
4.27M
    x_off >>= ss_hor;
84
4.27M
    y_off = 0;
85
29.4M
    for (int y = 0; y < (8 >> ss_ver); y++, y_off += PXSTRIDE(src_stride[1])) {
86
25.2M
        pixel_copy(dst[1][y], &src[1][y_off + x_off - 2], 2);
87
25.2M
        pixel_copy(dst[2][y], &src[2][y_off + x_off - 2], 2);
88
25.2M
    }
89
4.27M
}
90
91
4.86M
static int adjust_strength(const int strength, const unsigned var) {
92
4.86M
    if (!var) return 0;
93
2.32M
    const int i = var >> 6 ? imin(ulog2(var >> 6), 12) : 0;
94
2.32M
    return (strength * (4 + i) + 8) >> 4;
95
4.86M
}
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
402k
{
103
402k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
402k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
402k
    const Dav1dDSPContext *const dsp = f->dsp;
106
402k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
402k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
402k
    const int sbsz = 16;
109
402k
    const int sb64w = f->sb128w << 1;
110
402k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
402k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
402k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
402k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
402k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
402k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
402k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
402k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
402k
    const int have_tt = f->c->n_tc > 1;
119
402k
    const int sb128 = f->seq_hdr->sb128;
120
402k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
402k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
402k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
2.20M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
1.81M
        const int tf = tc->top_pre_cdef_toggle;
126
1.81M
        const int by_idx = (by & 30) >> 1;
127
1.81M
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
1.81M
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
1.54M
            edges & CDEF_HAVE_BOTTOM)
131
1.54M
        {
132
            // backup pre-filter data for next iteration
133
1.54M
            pixel *const cdef_top_bak[3] = {
134
1.54M
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
1.54M
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
1.54M
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
1.54M
            };
138
1.54M
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
1.54M
        }
140
141
1.81M
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
1.81M
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
1.81M
        edges &= ~CDEF_HAVE_LEFT;
144
1.81M
        edges |= CDEF_HAVE_RIGHT;
145
1.81M
        enum Backup2x8Flags prev_flag = 0;
146
6.63M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
4.82M
            const int sb128x = sbx >> 1;
148
4.82M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
4.82M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
4.82M
            if (cdef_idx == -1 ||
151
2.65M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
649k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
2.69M
            {
154
2.69M
                prev_flag = 0;
155
2.69M
                goto next_sb;
156
2.69M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
2.13M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
2.13M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
2.13M
                                                    noskip_row[0][0];
162
163
2.13M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
2.13M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
2.13M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
2.13M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
2.13M
            int y_sec_lvl = y_lvl & 3;
169
2.13M
            y_sec_lvl += y_sec_lvl == 3;
170
2.13M
            y_sec_lvl <<= bitdepth_min_8;
171
172
2.13M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
2.13M
            int uv_sec_lvl = uv_lvl & 3;
174
2.13M
            uv_sec_lvl += uv_sec_lvl == 3;
175
2.13M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
2.13M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
8.22M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
6.08M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
6.16M
            {
181
6.16M
                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.16M
                const uint32_t bx_mask = 3U << (bx & 30);
186
6.16M
                if (!(noskip_mask & bx_mask)) {
187
589k
                    prev_flag = 0;
188
589k
                    goto next_b;
189
589k
                }
190
5.57M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
5.57M
                prev_flag = flag;
192
5.57M
                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
73.4k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
73.4k
                }
197
5.57M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
4.92M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
4.92M
                }
201
202
5.57M
                int dir;
203
5.57M
                unsigned variance;
204
5.57M
                if (y_pri_lvl || uv_pri_lvl)
205
5.52M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
5.52M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
5.57M
                const pixel *top, *bot;
209
5.57M
                ptrdiff_t offset;
210
211
5.57M
                if (!have_tt) goto st_y;
212
5.57M
                if (sbrow_start && by == by_start) {
213
387k
                    if (resize) {
214
114k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
114k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
273k
                    } else {
217
273k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
273k
                        top = &f->lf.lr_lpf_line[0][offset];
219
273k
                    }
220
387k
                    bot = bptrs[0] + 8 * y_stride;
221
5.61M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
769k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
769k
                    if (resize) {
224
265k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
265k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
504k
                    } else {
227
504k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
504k
                        offset = line * y_stride + bx * 4;
229
504k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
504k
                    }
231
4.42M
                } else {
232
4.85M
            st_y:;
233
4.85M
                    offset = sby * 4 * y_stride;
234
4.85M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
4.85M
                    bot = bptrs[0] + 8 * y_stride;
236
4.85M
                }
237
6.00M
                if (y_pri_lvl) {
238
4.86M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
4.86M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
4.03M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
4.03M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
4.03M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
4.86M
                } else if (y_sec_lvl)
244
498k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
498k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
498k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
6.00M
                if (!uv_lvl) goto skip_uv;
249
5.21M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
5.21M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
14.4M
                for (int pl = 1; pl <= 2; pl++) {
253
9.04M
                    if (!have_tt) goto st_uv;
254
9.04M
                    if (sbrow_start && by == by_start) {
255
595k
                        if (resize) {
256
202k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
202k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
392k
                        } else {
259
392k
                            const int line = sby * (4 << sb128) - 4;
260
392k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
392k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
392k
                        }
263
595k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
8.55M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
1.19M
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
1.19M
                                                     (bx * 4 >> ss_hor);
267
1.19M
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
1.19M
                        if (resize) {
269
421k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
421k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
778k
                        } else {
272
778k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
778k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
778k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
778k
                        }
276
7.24M
                    } else {
277
7.48M
                st_uv:;
278
7.48M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
7.48M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
7.48M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
7.48M
                    }
282
9.27M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
9.27M
                                         lr_bak[bit][pl], top, bot,
284
9.27M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
9.27M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
9.27M
                }
287
288
5.50M
            skip_uv:
289
5.50M
                bit ^= 1;
290
291
6.08M
            next_b:
292
6.08M
                bptrs[0] += 8;
293
6.08M
                bptrs[1] += 8 >> ss_hor;
294
6.08M
                bptrs[2] += 8 >> ss_hor;
295
6.08M
            }
296
297
4.82M
        next_sb:
298
4.82M
            iptrs[0] += sbsz * 4;
299
4.82M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
4.82M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
4.82M
        }
302
303
1.80M
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
1.80M
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
1.80M
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
1.80M
        tc->top_pre_cdef_toggle ^= 1;
307
1.80M
    }
308
402k
}
dav1d_cdef_brow_8bpc
Line
Count
Source
102
221k
{
103
221k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
221k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
221k
    const Dav1dDSPContext *const dsp = f->dsp;
106
221k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
221k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
221k
    const int sbsz = 16;
109
221k
    const int sb64w = f->sb128w << 1;
110
221k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
221k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
221k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
221k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
221k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
221k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
221k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
221k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
221k
    const int have_tt = f->c->n_tc > 1;
119
221k
    const int sb128 = f->seq_hdr->sb128;
120
221k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
221k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
221k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
1.20M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
989k
        const int tf = tc->top_pre_cdef_toggle;
126
989k
        const int by_idx = (by & 30) >> 1;
127
989k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
989k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
849k
            edges & CDEF_HAVE_BOTTOM)
131
849k
        {
132
            // backup pre-filter data for next iteration
133
849k
            pixel *const cdef_top_bak[3] = {
134
849k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
849k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
849k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
849k
            };
138
849k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
849k
        }
140
141
989k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
989k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
989k
        edges &= ~CDEF_HAVE_LEFT;
144
989k
        edges |= CDEF_HAVE_RIGHT;
145
989k
        enum Backup2x8Flags prev_flag = 0;
146
3.58M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
2.59M
            const int sb128x = sbx >> 1;
148
2.59M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
2.59M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
2.59M
            if (cdef_idx == -1 ||
151
1.49M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
356k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.37M
            {
154
1.37M
                prev_flag = 0;
155
1.37M
                goto next_sb;
156
1.37M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
1.22M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
1.22M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
1.22M
                                                    noskip_row[0][0];
162
163
1.22M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
1.22M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
1.22M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
1.22M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
1.22M
            int y_sec_lvl = y_lvl & 3;
169
1.22M
            y_sec_lvl += y_sec_lvl == 3;
170
1.22M
            y_sec_lvl <<= bitdepth_min_8;
171
172
1.22M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
1.22M
            int uv_sec_lvl = uv_lvl & 3;
174
1.22M
            uv_sec_lvl += uv_sec_lvl == 3;
175
1.22M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
1.22M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
4.44M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
3.22M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
3.28M
            {
181
3.28M
                if (bx + 2 >= f->bw) edges &= ~CDEF_HAVE_RIGHT;
182
183
                // check if this 8x8 block had any coded coefficients; if not,
184
                // go to the next block
185
3.28M
                const uint32_t bx_mask = 3U << (bx & 30);
186
3.28M
                if (!(noskip_mask & bx_mask)) {
187
382k
                    prev_flag = 0;
188
382k
                    goto next_b;
189
382k
                }
190
2.90M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
2.90M
                prev_flag = flag;
192
2.90M
                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
42.4k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
42.4k
                }
197
2.90M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.59M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.59M
                }
201
202
2.90M
                int dir;
203
2.90M
                unsigned variance;
204
2.90M
                if (y_pri_lvl || uv_pri_lvl)
205
2.94M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
2.94M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
2.90M
                const pixel *top, *bot;
209
2.90M
                ptrdiff_t offset;
210
211
2.90M
                if (!have_tt) goto st_y;
212
2.90M
                if (sbrow_start && by == by_start) {
213
245k
                    if (resize) {
214
47.1k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
47.1k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
198k
                    } else {
217
198k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
198k
                        top = &f->lf.lr_lpf_line[0][offset];
219
198k
                    }
220
245k
                    bot = bptrs[0] + 8 * y_stride;
221
2.98M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
428k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
428k
                    if (resize) {
224
109k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
109k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
319k
                    } else {
227
319k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
319k
                        offset = line * y_stride + bx * 4;
229
319k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
319k
                    }
231
2.22M
                } else {
232
2.55M
            st_y:;
233
2.55M
                    offset = sby * 4 * y_stride;
234
2.55M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
2.55M
                    bot = bptrs[0] + 8 * y_stride;
236
2.55M
                }
237
3.23M
                if (y_pri_lvl) {
238
2.63M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
2.63M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
2.09M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
2.09M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
2.09M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
2.63M
                } else if (y_sec_lvl)
244
203k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
203k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
203k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
3.23M
                if (!uv_lvl) goto skip_uv;
249
2.86M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
2.86M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
7.73M
                for (int pl = 1; pl <= 2; pl++) {
253
4.69M
                    if (!have_tt) goto st_uv;
254
4.69M
                    if (sbrow_start && by == by_start) {
255
364k
                        if (resize) {
256
85.0k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
85.0k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
279k
                        } else {
259
279k
                            const int line = sby * (4 << sb128) - 4;
260
279k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
279k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
279k
                        }
263
364k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
4.41M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
666k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
666k
                                                     (bx * 4 >> ss_hor);
267
666k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
666k
                        if (resize) {
269
163k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
163k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
502k
                        } else {
272
502k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
502k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
502k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
502k
                        }
276
3.66M
                    } else {
277
3.84M
                st_uv:;
278
3.84M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
3.84M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
3.84M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
3.84M
                    }
282
4.87M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
4.87M
                                         lr_bak[bit][pl], top, bot,
284
4.87M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
4.87M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
4.87M
                }
287
288
3.03M
            skip_uv:
289
2.85M
                bit ^= 1;
290
291
3.22M
            next_b:
292
3.22M
                bptrs[0] += 8;
293
3.22M
                bptrs[1] += 8 >> ss_hor;
294
3.22M
                bptrs[2] += 8 >> ss_hor;
295
3.22M
            }
296
297
2.59M
        next_sb:
298
2.59M
            iptrs[0] += sbsz * 4;
299
2.59M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
2.59M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
2.59M
        }
302
303
986k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
986k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
986k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
986k
        tc->top_pre_cdef_toggle ^= 1;
307
986k
    }
308
221k
}
dav1d_cdef_brow_16bpc
Line
Count
Source
102
180k
{
103
180k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
180k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
180k
    const Dav1dDSPContext *const dsp = f->dsp;
106
180k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
180k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
180k
    const int sbsz = 16;
109
180k
    const int sb64w = f->sb128w << 1;
110
180k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
180k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
180k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
180k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
180k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
180k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
180k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
180k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
180k
    const int have_tt = f->c->n_tc > 1;
119
180k
    const int sb128 = f->seq_hdr->sb128;
120
180k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
180k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
180k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
1.00M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
821k
        const int tf = tc->top_pre_cdef_toggle;
126
821k
        const int by_idx = (by & 30) >> 1;
127
821k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
822k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
698k
            edges & CDEF_HAVE_BOTTOM)
131
698k
        {
132
            // backup pre-filter data for next iteration
133
698k
            pixel *const cdef_top_bak[3] = {
134
698k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
698k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
698k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
698k
            };
138
698k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
698k
        }
140
141
821k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
821k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
821k
        edges &= ~CDEF_HAVE_LEFT;
144
821k
        edges |= CDEF_HAVE_RIGHT;
145
821k
        enum Backup2x8Flags prev_flag = 0;
146
3.05M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
2.23M
            const int sb128x = sbx >> 1;
148
2.23M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
2.23M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
2.23M
            if (cdef_idx == -1 ||
151
1.15M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
293k
                 !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
916k
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
916k
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
916k
                                                    noskip_row[0][0];
162
163
916k
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
916k
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
916k
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
916k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
916k
            int y_sec_lvl = y_lvl & 3;
169
916k
            y_sec_lvl += y_sec_lvl == 3;
170
916k
            y_sec_lvl <<= bitdepth_min_8;
171
172
916k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
916k
            int uv_sec_lvl = uv_lvl & 3;
174
916k
            uv_sec_lvl += uv_sec_lvl == 3;
175
916k
            uv_sec_lvl <<= bitdepth_min_8;
176
177
916k
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
3.77M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
2.85M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
2.88M
            {
181
2.88M
                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.88M
                const uint32_t bx_mask = 3U << (bx & 30);
186
2.88M
                if (!(noskip_mask & bx_mask)) {
187
206k
                    prev_flag = 0;
188
206k
                    goto next_b;
189
206k
                }
190
2.67M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
2.67M
                prev_flag = flag;
192
2.67M
                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
30.9k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
30.9k
                }
197
2.67M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.33M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.33M
                }
201
202
2.67M
                int dir;
203
2.67M
                unsigned variance;
204
2.67M
                if (y_pri_lvl || uv_pri_lvl)
205
2.58M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
2.58M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
2.67M
                const pixel *top, *bot;
209
2.67M
                ptrdiff_t offset;
210
211
2.67M
                if (!have_tt) goto st_y;
212
2.67M
                if (sbrow_start && by == by_start) {
213
142k
                    if (resize) {
214
66.9k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
66.9k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
75.2k
                    } else {
217
75.2k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
75.2k
                        top = &f->lf.lr_lpf_line[0][offset];
219
75.2k
                    }
220
142k
                    bot = bptrs[0] + 8 * y_stride;
221
2.63M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
340k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
340k
                    if (resize) {
224
155k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
155k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
184k
                    } else {
227
184k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
184k
                        offset = line * y_stride + bx * 4;
229
184k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
184k
                    }
231
2.19M
                } else {
232
2.29M
            st_y:;
233
2.29M
                    offset = sby * 4 * y_stride;
234
2.29M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
2.29M
                    bot = bptrs[0] + 8 * y_stride;
236
2.29M
                }
237
2.77M
                if (y_pri_lvl) {
238
2.23M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
2.23M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
1.93M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
1.93M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
1.93M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
2.23M
                } else if (y_sec_lvl)
244
294k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
294k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
294k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
2.77M
                if (!uv_lvl) goto skip_uv;
249
2.35M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
2.35M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
6.75M
                for (int pl = 1; pl <= 2; pl++) {
253
4.34M
                    if (!have_tt) goto st_uv;
254
4.34M
                    if (sbrow_start && by == by_start) {
255
230k
                        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
117k
                        } else {
259
113k
                            const int line = sby * (4 << sb128) - 4;
260
113k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
113k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
113k
                        }
263
230k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
4.14M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
533k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
533k
                                                     (bx * 4 >> ss_hor);
267
533k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
533k
                        if (resize) {
269
257k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
257k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
276k
                        } else {
272
276k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
276k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
276k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
276k
                        }
276
3.58M
                    } else {
277
3.63M
                st_uv:;
278
3.63M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
3.63M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
3.63M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
3.63M
                    }
282
4.40M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
4.40M
                                         lr_bak[bit][pl], top, bot,
284
4.40M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
4.40M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
4.40M
                }
287
288
2.65M
            skip_uv:
289
2.65M
                bit ^= 1;
290
291
2.85M
            next_b:
292
2.85M
                bptrs[0] += 8;
293
2.85M
                bptrs[1] += 8 >> ss_hor;
294
2.85M
                bptrs[2] += 8 >> ss_hor;
295
2.85M
            }
296
297
2.22M
        next_sb:
298
2.22M
            iptrs[0] += sbsz * 4;
299
2.22M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
2.22M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
2.22M
        }
302
303
819k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
819k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
819k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
819k
        tc->top_pre_cdef_toggle ^= 1;
307
819k
    }
308
180k
}