Coverage Report

Created: 2026-07-30 06:27

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.10M
{
45
1.10M
    const ptrdiff_t y_stride = PXSTRIDE(stride[0]);
46
1.10M
    if (y_stride < 0)
47
0
        pixel_copy(dst[0] + y_stride, src[0] + 7 * y_stride, -2 * y_stride);
48
1.10M
    else
49
1.10M
        pixel_copy(dst[0], src[0] + 6 * y_stride, 2 * y_stride);
50
51
1.10M
    if (layout != DAV1D_PIXEL_LAYOUT_I400) {
52
799k
        const ptrdiff_t uv_stride = PXSTRIDE(stride[1]);
53
799k
        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
799k
        } else {
58
799k
            const int uv_off = layout == DAV1D_PIXEL_LAYOUT_I420 ? 2 : 6;
59
799k
            pixel_copy(dst[1], src[1] + uv_off * uv_stride, 2 * uv_stride);
60
799k
            pixel_copy(dst[2], src[2] + uv_off * uv_stride, 2 * uv_stride);
61
799k
        }
62
799k
    }
63
1.10M
}
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.79M
{
71
5.79M
    ptrdiff_t y_off = 0;
72
5.79M
    if (flag & BACKUP_2X8_Y) {
73
49.6M
        for (int y = 0; y < 8; y++, y_off += PXSTRIDE(src_stride[0]))
74
44.1M
            pixel_copy(dst[0][y], &src[0][y_off + x_off - 2], 2);
75
5.53M
    }
76
77
5.79M
    if (layout == DAV1D_PIXEL_LAYOUT_I400 || !(flag & BACKUP_2X8_UV))
78
1.41M
        return;
79
80
4.38M
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
81
4.38M
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
82
83
4.38M
    x_off >>= ss_hor;
84
4.38M
    y_off = 0;
85
27.5M
    for (int y = 0; y < (8 >> ss_ver); y++, y_off += PXSTRIDE(src_stride[1])) {
86
23.1M
        pixel_copy(dst[1][y], &src[1][y_off + x_off - 2], 2);
87
23.1M
        pixel_copy(dst[2][y], &src[2][y_off + x_off - 2], 2);
88
23.1M
    }
89
4.38M
}
90
91
5.47M
static int adjust_strength(const int strength, const unsigned var) {
92
5.47M
    if (!var) return 0;
93
2.82M
    const int i = var >> 6 ? imin(ulog2(var >> 6), 12) : 0;
94
2.82M
    return (strength * (4 + i) + 8) >> 4;
95
5.47M
}
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
281k
{
103
281k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
281k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
281k
    const Dav1dDSPContext *const dsp = f->dsp;
106
281k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
281k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
281k
    const int sbsz = 16;
109
281k
    const int sb64w = f->sb128w << 1;
110
281k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
281k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
281k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
281k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
281k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
281k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
281k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
281k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
281k
    const int have_tt = f->c->n_tc > 1;
119
281k
    const int sb128 = f->seq_hdr->sb128;
120
281k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
281k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
281k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
1.57M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
1.29M
        const int tf = tc->top_pre_cdef_toggle;
126
1.29M
        const int by_idx = (by & 30) >> 1;
127
1.29M
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
1.29M
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
1.10M
            edges & CDEF_HAVE_BOTTOM)
131
1.10M
        {
132
            // backup pre-filter data for next iteration
133
1.10M
            pixel *const cdef_top_bak[3] = {
134
1.10M
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
1.10M
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
1.10M
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
1.10M
            };
138
1.10M
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
1.10M
        }
140
141
1.29M
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
1.29M
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
1.29M
        edges &= ~CDEF_HAVE_LEFT;
144
1.29M
        edges |= CDEF_HAVE_RIGHT;
145
1.29M
        enum Backup2x8Flags prev_flag = 0;
146
5.76M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
4.46M
            const int sb128x = sbx >> 1;
148
4.46M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
4.46M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
4.46M
            if (cdef_idx == -1 ||
151
2.52M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
1.01M
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
2.85M
            {
154
2.85M
                prev_flag = 0;
155
2.85M
                goto next_sb;
156
2.85M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
1.61M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
1.61M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
1.61M
                                                    noskip_row[0][0];
162
163
1.61M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
1.61M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
1.61M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
1.61M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
1.61M
            int y_sec_lvl = y_lvl & 3;
169
1.61M
            y_sec_lvl += y_sec_lvl == 3;
170
1.61M
            y_sec_lvl <<= bitdepth_min_8;
171
172
1.61M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
1.61M
            int uv_sec_lvl = uv_lvl & 3;
174
1.61M
            uv_sec_lvl += uv_sec_lvl == 3;
175
1.61M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
1.61M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
8.66M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
7.05M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
7.10M
            {
181
7.10M
                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
7.10M
                const uint32_t bx_mask = 3U << (bx & 30);
186
7.10M
                if (!(noskip_mask & bx_mask)) {
187
967k
                    prev_flag = 0;
188
967k
                    goto next_b;
189
967k
                }
190
6.14M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
6.14M
                prev_flag = flag;
192
6.14M
                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
141k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
141k
                }
197
6.14M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
5.65M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
5.65M
                }
201
202
6.14M
                int dir;
203
6.14M
                unsigned variance;
204
6.14M
                if (y_pri_lvl || uv_pri_lvl)
205
6.03M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
6.03M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
6.14M
                const pixel *top, *bot;
209
6.14M
                ptrdiff_t offset;
210
211
6.14M
                if (!have_tt) goto st_y;
212
6.14M
                if (sbrow_start && by == by_start) {
213
379k
                    if (resize) {
214
149k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
149k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
229k
                    } else {
217
229k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
229k
                        top = &f->lf.lr_lpf_line[0][offset];
219
229k
                    }
220
379k
                    bot = bptrs[0] + 8 * y_stride;
221
5.93M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
739k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
739k
                    if (resize) {
224
306k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
306k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
433k
                    } else {
227
433k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
433k
                        offset = line * y_stride + bx * 4;
229
433k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
433k
                    }
231
5.02M
                } else {
232
5.20M
            st_y:;
233
5.20M
                    offset = sby * 4 * y_stride;
234
5.20M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
5.20M
                    bot = bptrs[0] + 8 * y_stride;
236
5.20M
                }
237
6.32M
                if (y_pri_lvl) {
238
5.47M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
5.47M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
4.32M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
4.32M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
4.32M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
5.47M
                } else if (y_sec_lvl)
244
522k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
522k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
522k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
6.32M
                if (!uv_lvl) goto skip_uv;
249
4.82M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
4.82M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
13.9M
                for (int pl = 1; pl <= 2; pl++) {
253
8.97M
                    if (!have_tt) goto st_uv;
254
8.97M
                    if (sbrow_start && by == by_start) {
255
572k
                        if (resize) {
256
186k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
186k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
385k
                        } else {
259
385k
                            const int line = sby * (4 << sb128) - 4;
260
385k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
385k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
385k
                        }
263
572k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
8.45M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
1.09M
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
1.09M
                                                     (bx * 4 >> ss_hor);
267
1.09M
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
1.09M
                        if (resize) {
269
393k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
393k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
703k
                        } else {
272
703k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
703k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
703k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
703k
                        }
276
7.30M
                    } else {
277
7.42M
                st_uv:;
278
7.42M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
7.42M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
7.42M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
7.42M
                    }
282
9.09M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
9.09M
                                         lr_bak[bit][pl], top, bot,
284
9.09M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
9.09M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
9.09M
                }
287
288
6.09M
            skip_uv:
289
6.09M
                bit ^= 1;
290
291
7.05M
            next_b:
292
7.05M
                bptrs[0] += 8;
293
7.05M
                bptrs[1] += 8 >> ss_hor;
294
7.05M
                bptrs[2] += 8 >> ss_hor;
295
7.05M
            }
296
297
4.46M
        next_sb:
298
4.46M
            iptrs[0] += sbsz * 4;
299
4.46M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
4.46M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
4.46M
        }
302
303
1.29M
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
1.29M
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
1.29M
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
1.29M
        tc->top_pre_cdef_toggle ^= 1;
307
1.29M
    }
308
281k
}
dav1d_cdef_brow_8bpc
Line
Count
Source
102
138k
{
103
138k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
18.4E
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
138k
    const Dav1dDSPContext *const dsp = f->dsp;
106
138k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
138k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
138k
    const int sbsz = 16;
109
138k
    const int sb64w = f->sb128w << 1;
110
138k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
138k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
138k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
138k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
138k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
138k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
138k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
138k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
138k
    const int have_tt = f->c->n_tc > 1;
119
138k
    const int sb128 = f->seq_hdr->sb128;
120
138k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
138k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
138k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
794k
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
656k
        const int tf = tc->top_pre_cdef_toggle;
126
656k
        const int by_idx = (by & 30) >> 1;
127
656k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
656k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
561k
            edges & CDEF_HAVE_BOTTOM)
131
561k
        {
132
            // backup pre-filter data for next iteration
133
561k
            pixel *const cdef_top_bak[3] = {
134
561k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
561k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
561k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
561k
            };
138
561k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
561k
        }
140
141
656k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
656k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
656k
        edges &= ~CDEF_HAVE_LEFT;
144
656k
        edges |= CDEF_HAVE_RIGHT;
145
656k
        enum Backup2x8Flags prev_flag = 0;
146
3.04M
        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.43M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
616k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.53M
            {
154
1.53M
                prev_flag = 0;
155
1.53M
                goto next_sb;
156
1.53M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
860k
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
860k
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
860k
                                                    noskip_row[0][0];
162
163
860k
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
860k
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
860k
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
860k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
860k
            int y_sec_lvl = y_lvl & 3;
169
860k
            y_sec_lvl += y_sec_lvl == 3;
170
860k
            y_sec_lvl <<= bitdepth_min_8;
171
172
860k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
860k
            int uv_sec_lvl = uv_lvl & 3;
174
860k
            uv_sec_lvl += uv_sec_lvl == 3;
175
860k
            uv_sec_lvl <<= bitdepth_min_8;
176
177
860k
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
4.79M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
3.93M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
3.97M
            {
181
3.97M
                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.97M
                const uint32_t bx_mask = 3U << (bx & 30);
186
3.97M
                if (!(noskip_mask & bx_mask)) {
187
652k
                    prev_flag = 0;
188
652k
                    goto next_b;
189
652k
                }
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
84.4k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
84.4k
                }
197
3.32M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
3.06M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
3.06M
                }
201
202
3.32M
                int dir;
203
3.32M
                unsigned variance;
204
3.32M
                if (y_pri_lvl || uv_pri_lvl)
205
3.28M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
3.28M
                                        &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
215k
                    if (resize) {
214
47.6k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
47.6k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
167k
                    } else {
217
167k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
167k
                        top = &f->lf.lr_lpf_line[0][offset];
219
167k
                    }
220
215k
                    bot = bptrs[0] + 8 * y_stride;
221
3.23M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
395k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
395k
                    if (resize) {
224
109k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
109k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
285k
                    } else {
227
285k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
285k
                        offset = line * y_stride + bx * 4;
229
285k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
285k
                    }
231
2.71M
                } else {
232
2.84M
            st_y:;
233
2.84M
                    offset = sby * 4 * y_stride;
234
2.84M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
2.84M
                    bot = bptrs[0] + 8 * y_stride;
236
2.84M
                }
237
3.45M
                if (y_pri_lvl) {
238
2.92M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
2.92M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
2.04M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
2.04M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
2.04M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
2.92M
                } else if (y_sec_lvl)
244
342k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
342k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
342k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
3.45M
                if (!uv_lvl) goto skip_uv;
249
2.75M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
2.75M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
7.85M
                for (int pl = 1; pl <= 2; pl++) {
253
5.02M
                    if (!have_tt) goto st_uv;
254
5.02M
                    if (sbrow_start && by == by_start) {
255
330k
                        if (resize) {
256
46.6k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
46.6k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
283k
                        } else {
259
283k
                            const int line = sby * (4 << sb128) - 4;
260
283k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
283k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
283k
                        }
263
330k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
4.72M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
618k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
618k
                                                     (bx * 4 >> ss_hor);
267
618k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
618k
                        if (resize) {
269
125k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
125k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
492k
                        } else {
272
492k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
492k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
492k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
492k
                        }
276
4.07M
                    } else {
277
4.15M
                st_uv:;
278
4.15M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
4.15M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
4.15M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
4.15M
                    }
282
5.10M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
5.10M
                                         lr_bak[bit][pl], top, bot,
284
5.10M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
5.10M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
5.10M
                }
287
288
3.28M
            skip_uv:
289
3.28M
                bit ^= 1;
290
291
3.93M
            next_b:
292
3.93M
                bptrs[0] += 8;
293
3.93M
                bptrs[1] += 8 >> ss_hor;
294
3.93M
                bptrs[2] += 8 >> ss_hor;
295
3.93M
            }
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
655k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
655k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
655k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
655k
        tc->top_pre_cdef_toggle ^= 1;
307
655k
    }
308
138k
}
dav1d_cdef_brow_16bpc
Line
Count
Source
102
142k
{
103
142k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
142k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
142k
    const Dav1dDSPContext *const dsp = f->dsp;
106
142k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
142k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
142k
    const int sbsz = 16;
109
142k
    const int sb64w = f->sb128w << 1;
110
142k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
142k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
142k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
142k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
142k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
142k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
142k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
142k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
142k
    const int have_tt = f->c->n_tc > 1;
119
142k
    const int sb128 = f->seq_hdr->sb128;
120
142k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
142k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
142k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
779k
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
638k
        const int tf = tc->top_pre_cdef_toggle;
126
638k
        const int by_idx = (by & 30) >> 1;
127
638k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
639k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
544k
            edges & CDEF_HAVE_BOTTOM)
131
544k
        {
132
            // backup pre-filter data for next iteration
133
544k
            pixel *const cdef_top_bak[3] = {
134
544k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
544k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
544k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
544k
            };
138
544k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
544k
        }
140
141
638k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
638k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
638k
        edges &= ~CDEF_HAVE_LEFT;
144
638k
        edges |= CDEF_HAVE_RIGHT;
145
638k
        enum Backup2x8Flags prev_flag = 0;
146
2.71M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
2.07M
            const int sb128x = sbx >> 1;
148
2.07M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
2.07M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
2.07M
            if (cdef_idx == -1 ||
151
1.09M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
398k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.32M
            {
154
1.32M
                prev_flag = 0;
155
1.32M
                goto next_sb;
156
1.32M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
750k
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
750k
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
750k
                                                    noskip_row[0][0];
162
163
750k
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
750k
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
750k
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
750k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
750k
            int y_sec_lvl = y_lvl & 3;
169
750k
            y_sec_lvl += y_sec_lvl == 3;
170
750k
            y_sec_lvl <<= bitdepth_min_8;
171
172
750k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
750k
            int uv_sec_lvl = uv_lvl & 3;
174
750k
            uv_sec_lvl += uv_sec_lvl == 3;
175
750k
            uv_sec_lvl <<= bitdepth_min_8;
176
177
750k
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
3.87M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
3.12M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
3.13M
            {
181
3.13M
                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.13M
                const uint32_t bx_mask = 3U << (bx & 30);
186
3.13M
                if (!(noskip_mask & bx_mask)) {
187
314k
                    prev_flag = 0;
188
314k
                    goto next_b;
189
314k
                }
190
2.81M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
2.81M
                prev_flag = flag;
192
2.81M
                if (do_left && edges & CDEF_HAVE_LEFT) {
193
                    // we didn't backup the prefilter data because it wasn't
194
                    // there, so do it here instead
195
57.5k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
57.5k
                }
197
2.81M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.58M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.58M
                }
201
202
2.81M
                int dir;
203
2.81M
                unsigned variance;
204
2.81M
                if (y_pri_lvl || uv_pri_lvl)
205
2.75M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
2.75M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
2.81M
                const pixel *top, *bot;
209
2.81M
                ptrdiff_t offset;
210
211
2.81M
                if (!have_tt) goto st_y;
212
2.81M
                if (sbrow_start && by == by_start) {
213
164k
                    if (resize) {
214
102k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
102k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
102k
                    } else {
217
62.3k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
62.3k
                        top = &f->lf.lr_lpf_line[0][offset];
219
62.3k
                    }
220
164k
                    bot = bptrs[0] + 8 * y_stride;
221
2.69M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
344k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
344k
                    if (resize) {
224
197k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
197k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
197k
                    } else {
227
147k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
147k
                        offset = line * y_stride + bx * 4;
229
147k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
147k
                    }
231
2.31M
                } else {
232
2.35M
            st_y:;
233
2.35M
                    offset = sby * 4 * y_stride;
234
2.35M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
2.35M
                    bot = bptrs[0] + 8 * y_stride;
236
2.35M
                }
237
2.86M
                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.28M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
2.28M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
2.28M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
2.55M
                } else if (y_sec_lvl)
244
180k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
180k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
180k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
2.86M
                if (!uv_lvl) goto skip_uv;
249
2.07M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
2.07M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
6.06M
                for (int pl = 1; pl <= 2; pl++) {
253
3.95M
                    if (!have_tt) goto st_uv;
254
3.95M
                    if (sbrow_start && by == by_start) {
255
242k
                        if (resize) {
256
139k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
139k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
139k
                        } else {
259
102k
                            const int line = sby * (4 << sb128) - 4;
260
102k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
102k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
102k
                        }
263
242k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
3.72M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
479k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
479k
                                                     (bx * 4 >> ss_hor);
267
479k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
479k
                        if (resize) {
269
268k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
268k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
268k
                        } else {
272
211k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
211k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
211k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
211k
                        }
276
3.23M
                    } else {
277
3.26M
                st_uv:;
278
3.26M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
3.26M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
3.26M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
3.26M
                    }
282
3.99M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
3.99M
                                         lr_bak[bit][pl], top, bot,
284
3.99M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
3.99M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
3.99M
                }
287
288
2.80M
            skip_uv:
289
2.80M
                bit ^= 1;
290
291
3.12M
            next_b:
292
3.12M
                bptrs[0] += 8;
293
3.12M
                bptrs[1] += 8 >> ss_hor;
294
3.12M
                bptrs[2] += 8 >> ss_hor;
295
3.12M
            }
296
297
2.07M
        next_sb:
298
2.07M
            iptrs[0] += sbsz * 4;
299
2.07M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
2.07M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
2.07M
        }
302
303
637k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
637k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
637k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
637k
        tc->top_pre_cdef_toggle ^= 1;
307
637k
    }
308
142k
}