Coverage Report

Created: 2026-08-31 06:22

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.31M
{
45
1.31M
    const ptrdiff_t y_stride = PXSTRIDE(stride[0]);
46
1.31M
    if (y_stride < 0)
47
0
        pixel_copy(dst[0] + y_stride, src[0] + 7 * y_stride, -2 * y_stride);
48
1.31M
    else
49
1.31M
        pixel_copy(dst[0], src[0] + 6 * y_stride, 2 * y_stride);
50
51
1.31M
    if (layout != DAV1D_PIXEL_LAYOUT_I400) {
52
1.05M
        const ptrdiff_t uv_stride = PXSTRIDE(stride[1]);
53
1.05M
        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.05M
        } else {
58
1.05M
            const int uv_off = layout == DAV1D_PIXEL_LAYOUT_I420 ? 2 : 6;
59
1.05M
            pixel_copy(dst[1], src[1] + uv_off * uv_stride, 2 * uv_stride);
60
1.05M
            pixel_copy(dst[2], src[2] + uv_off * uv_stride, 2 * uv_stride);
61
1.05M
        }
62
1.05M
    }
63
1.31M
}
64
65
static void backup2x8(pixel dst[3][8][2],
66
                      /*const*/ pixel *const src[3],
67
                      const ptrdiff_t src_stride[2], int x_off,
68
                      const enum Dav1dPixelLayout layout,
69
                      const enum Backup2x8Flags flag)
70
4.28M
{
71
4.28M
    ptrdiff_t y_off = 0;
72
4.28M
    if (flag & BACKUP_2X8_Y) {
73
35.8M
        for (int y = 0; y < 8; y++, y_off += PXSTRIDE(src_stride[0]))
74
31.8M
            pixel_copy(dst[0][y], &src[0][y_off + x_off - 2], 2);
75
3.98M
    }
76
77
4.28M
    if (layout == DAV1D_PIXEL_LAYOUT_I400 || !(flag & BACKUP_2X8_UV))
78
1.07M
        return;
79
80
3.21M
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
81
3.21M
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
82
83
3.21M
    x_off >>= ss_hor;
84
3.21M
    y_off = 0;
85
22.7M
    for (int y = 0; y < (8 >> ss_ver); y++, y_off += PXSTRIDE(src_stride[1])) {
86
19.4M
        pixel_copy(dst[1][y], &src[1][y_off + x_off - 2], 2);
87
19.4M
        pixel_copy(dst[2][y], &src[2][y_off + x_off - 2], 2);
88
19.4M
    }
89
3.21M
}
90
91
4.01M
static int adjust_strength(const int strength, const unsigned var) {
92
4.01M
    if (!var) return 0;
93
2.05M
    const int i = var >> 6 ? imin(ulog2(var >> 6), 12) : 0;
94
2.05M
    return (strength * (4 + i) + 8) >> 4;
95
4.01M
}
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
352k
{
103
352k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
352k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
352k
    const Dav1dDSPContext *const dsp = f->dsp;
106
352k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
352k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
352k
    const int sbsz = 16;
109
352k
    const int sb64w = f->sb128w << 1;
110
352k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
352k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
352k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
352k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
352k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
352k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
352k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
352k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
352k
    const int have_tt = f->c->n_tc > 1;
119
352k
    const int sb128 = f->seq_hdr->sb128;
120
352k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
352k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
352k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
1.89M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
1.54M
        const int tf = tc->top_pre_cdef_toggle;
126
1.54M
        const int by_idx = (by & 30) >> 1;
127
1.54M
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
1.54M
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
1.31M
            edges & CDEF_HAVE_BOTTOM)
131
1.31M
        {
132
            // backup pre-filter data for next iteration
133
1.31M
            pixel *const cdef_top_bak[3] = {
134
1.31M
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
1.31M
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
1.31M
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
1.31M
            };
138
1.31M
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
1.31M
        }
140
141
1.54M
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
1.54M
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
1.54M
        edges &= ~CDEF_HAVE_LEFT;
144
1.54M
        edges |= CDEF_HAVE_RIGHT;
145
1.54M
        enum Backup2x8Flags prev_flag = 0;
146
6.66M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
5.11M
            const int sb128x = sbx >> 1;
148
5.11M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
5.11M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
5.11M
            if (cdef_idx == -1 ||
151
3.26M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
1.52M
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
3.28M
            {
154
3.28M
                prev_flag = 0;
155
3.28M
                goto next_sb;
156
3.28M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
1.83M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
1.83M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
1.83M
                                                    noskip_row[0][0];
162
163
1.83M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
1.83M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
1.83M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
1.83M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
1.83M
            int y_sec_lvl = y_lvl & 3;
169
1.83M
            y_sec_lvl += y_sec_lvl == 3;
170
1.83M
            y_sec_lvl <<= bitdepth_min_8;
171
172
1.83M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
1.83M
            int uv_sec_lvl = uv_lvl & 3;
174
1.83M
            uv_sec_lvl += uv_sec_lvl == 3;
175
1.83M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
1.83M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
7.28M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
5.44M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
5.49M
            {
181
5.49M
                if (bx + 2 >= f->bw) edges &= ~CDEF_HAVE_RIGHT;
182
183
                // check if this 8x8 block had any coded coefficients; if not,
184
                // go to the next block
185
5.49M
                const uint32_t bx_mask = 3U << (bx & 30);
186
5.49M
                if (!(noskip_mask & bx_mask)) {
187
763k
                    prev_flag = 0;
188
763k
                    goto next_b;
189
763k
                }
190
4.73M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
4.73M
                prev_flag = flag;
192
4.73M
                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
89.0k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
89.0k
                }
197
4.73M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
4.19M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
4.19M
                }
201
202
4.73M
                int dir;
203
4.73M
                unsigned variance;
204
4.73M
                if (y_pri_lvl || uv_pri_lvl)
205
4.51M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
4.51M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
4.73M
                const pixel *top, *bot;
209
4.73M
                ptrdiff_t offset;
210
211
4.73M
                if (!have_tt) goto st_y;
212
4.73M
                if (sbrow_start && by == by_start) {
213
277k
                    if (resize) {
214
108k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
108k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
168k
                    } else {
217
168k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
168k
                        top = &f->lf.lr_lpf_line[0][offset];
219
168k
                    }
220
277k
                    bot = bptrs[0] + 8 * y_stride;
221
4.61M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
620k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
620k
                    if (resize) {
224
235k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
235k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
384k
                    } else {
227
384k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
384k
                        offset = line * y_stride + bx * 4;
229
384k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
384k
                    }
231
3.83M
                } else {
232
3.99M
            st_y:;
233
3.99M
                    offset = sby * 4 * y_stride;
234
3.99M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
3.99M
                    bot = bptrs[0] + 8 * y_stride;
236
3.99M
                }
237
4.89M
                if (y_pri_lvl) {
238
4.01M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
4.01M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
3.49M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
3.49M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
3.49M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
4.01M
                } else if (y_sec_lvl)
244
537k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
537k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
537k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
4.89M
                if (!uv_lvl) goto skip_uv;
249
3.79M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
3.79M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
10.8M
                for (int pl = 1; pl <= 2; pl++) {
253
6.99M
                    if (!have_tt) goto st_uv;
254
6.99M
                    if (sbrow_start && by == by_start) {
255
396k
                        if (resize) {
256
157k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
157k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
239k
                        } else {
259
239k
                            const int line = sby * (4 << sb128) - 4;
260
239k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
239k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
239k
                        }
263
396k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
6.63M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
922k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
922k
                                                     (bx * 4 >> ss_hor);
267
922k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
922k
                        if (resize) {
269
349k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
349k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
573k
                        } else {
272
573k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
573k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
573k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
573k
                        }
276
5.67M
                    } else {
277
5.75M
                st_uv:;
278
5.75M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
5.75M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
5.75M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
5.75M
                    }
282
7.07M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
7.07M
                                         lr_bak[bit][pl], top, bot,
284
7.07M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
7.07M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
7.07M
                }
287
288
4.68M
            skip_uv:
289
4.68M
                bit ^= 1;
290
291
5.44M
            next_b:
292
5.44M
                bptrs[0] += 8;
293
5.44M
                bptrs[1] += 8 >> ss_hor;
294
5.44M
                bptrs[2] += 8 >> ss_hor;
295
5.44M
            }
296
297
5.11M
        next_sb:
298
5.11M
            iptrs[0] += sbsz * 4;
299
5.11M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
5.11M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
5.11M
        }
302
303
1.54M
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
1.54M
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
1.54M
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
1.54M
        tc->top_pre_cdef_toggle ^= 1;
307
1.54M
    }
308
352k
}
dav1d_cdef_brow_8bpc
Line
Count
Source
102
203k
{
103
203k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
203k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
203k
    const Dav1dDSPContext *const dsp = f->dsp;
106
203k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
203k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
203k
    const int sbsz = 16;
109
203k
    const int sb64w = f->sb128w << 1;
110
203k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
203k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
203k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
203k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
203k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
203k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
203k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
203k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
203k
    const int have_tt = f->c->n_tc > 1;
119
203k
    const int sb128 = f->seq_hdr->sb128;
120
203k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
203k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
203k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
1.11M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
910k
        const int tf = tc->top_pre_cdef_toggle;
126
910k
        const int by_idx = (by & 30) >> 1;
127
910k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
910k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
784k
            edges & CDEF_HAVE_BOTTOM)
131
784k
        {
132
            // backup pre-filter data for next iteration
133
784k
            pixel *const cdef_top_bak[3] = {
134
784k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
784k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
784k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
784k
            };
138
784k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
784k
        }
140
141
910k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
910k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
910k
        edges &= ~CDEF_HAVE_LEFT;
144
910k
        edges |= CDEF_HAVE_RIGHT;
145
910k
        enum Backup2x8Flags prev_flag = 0;
146
3.81M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
2.90M
            const int sb128x = sbx >> 1;
148
2.90M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
2.90M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
2.90M
            if (cdef_idx == -1 ||
151
1.84M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
889k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.89M
            {
154
1.89M
                prev_flag = 0;
155
1.89M
                goto next_sb;
156
1.89M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
1.01M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
1.01M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
1.01M
                                                    noskip_row[0][0];
162
163
1.01M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
1.01M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
1.01M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
1.01M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
1.01M
            int y_sec_lvl = y_lvl & 3;
169
1.01M
            y_sec_lvl += y_sec_lvl == 3;
170
1.01M
            y_sec_lvl <<= bitdepth_min_8;
171
172
1.01M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
1.01M
            int uv_sec_lvl = uv_lvl & 3;
174
1.01M
            uv_sec_lvl += uv_sec_lvl == 3;
175
1.01M
            uv_sec_lvl <<= bitdepth_min_8;
176
177
1.01M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
3.52M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
2.50M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
2.54M
            {
181
2.54M
                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.54M
                const uint32_t bx_mask = 3U << (bx & 30);
186
2.54M
                if (!(noskip_mask & bx_mask)) {
187
487k
                    prev_flag = 0;
188
487k
                    goto next_b;
189
487k
                }
190
2.05M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
191
2.05M
                prev_flag = flag;
192
2.05M
                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
46.0k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
46.0k
                }
197
2.05M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
1.79M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
1.79M
                }
201
202
2.05M
                int dir;
203
2.05M
                unsigned variance;
204
2.05M
                if (y_pri_lvl || uv_pri_lvl)
205
1.93M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
1.93M
                                        &variance HIGHBD_CALL_SUFFIX);
207
208
2.05M
                const pixel *top, *bot;
209
2.05M
                ptrdiff_t offset;
210
211
2.05M
                if (!have_tt) goto st_y;
212
2.05M
                if (sbrow_start && by == by_start) {
213
134k
                    if (resize) {
214
21.1k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
21.1k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
113k
                    } else {
217
113k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
113k
                        top = &f->lf.lr_lpf_line[0][offset];
219
113k
                    }
220
134k
                    bot = bptrs[0] + 8 * y_stride;
221
2.02M
                } else if (!sbrow_start && by + 2 >= by_end) {
222
279k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
223
279k
                    if (resize) {
224
64.1k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
64.1k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
215k
                    } else {
227
215k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
215k
                        offset = line * y_stride + bx * 4;
229
215k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
215k
                    }
231
1.63M
                } else {
232
1.74M
            st_y:;
233
1.74M
                    offset = sby * 4 * y_stride;
234
1.74M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
1.74M
                    bot = bptrs[0] + 8 * y_stride;
236
1.74M
                }
237
2.16M
                if (y_pri_lvl) {
238
1.64M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
1.64M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
1.37M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
1.37M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
1.37M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
1.64M
                } else if (y_sec_lvl)
244
301k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
301k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
301k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
2.16M
                if (!uv_lvl) goto skip_uv;
249
1.78M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
1.78M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
5.02M
                for (int pl = 1; pl <= 2; pl++) {
253
3.18M
                    if (!have_tt) goto st_uv;
254
3.18M
                    if (sbrow_start && by == by_start) {
255
189k
                        if (resize) {
256
37.1k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
37.1k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
152k
                        } else {
259
152k
                            const int line = sby * (4 << sb128) - 4;
260
152k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
152k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
152k
                        }
263
189k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
3.01M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
421k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
421k
                                                     (bx * 4 >> ss_hor);
267
421k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
421k
                        if (resize) {
269
97.3k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
97.3k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
324k
                        } else {
272
324k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
324k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
324k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
324k
                        }
276
2.57M
                    } else {
277
2.62M
                st_uv:;
278
2.62M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
2.62M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
2.62M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
2.62M
                    }
282
3.23M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
3.23M
                                         lr_bak[bit][pl], top, bot,
284
3.23M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
3.23M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
3.23M
                }
287
288
2.01M
            skip_uv:
289
2.01M
                bit ^= 1;
290
291
2.50M
            next_b:
292
2.50M
                bptrs[0] += 8;
293
2.50M
                bptrs[1] += 8 >> ss_hor;
294
2.50M
                bptrs[2] += 8 >> ss_hor;
295
2.50M
            }
296
297
2.90M
        next_sb:
298
2.90M
            iptrs[0] += sbsz * 4;
299
2.90M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
2.90M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
2.90M
        }
302
303
909k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
909k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
909k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
909k
        tc->top_pre_cdef_toggle ^= 1;
307
909k
    }
308
203k
}
dav1d_cdef_brow_16bpc
Line
Count
Source
102
148k
{
103
148k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
104
148k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
105
148k
    const Dav1dDSPContext *const dsp = f->dsp;
106
148k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
107
148k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
108
148k
    const int sbsz = 16;
109
148k
    const int sb64w = f->sb128w << 1;
110
148k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
111
148k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
112
148k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
113
148k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
114
148k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
115
148k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
116
148k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
117
148k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
118
148k
    const int have_tt = f->c->n_tc > 1;
119
148k
    const int sb128 = f->seq_hdr->sb128;
120
148k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
121
148k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
122
148k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
123
124
782k
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
125
633k
        const int tf = tc->top_pre_cdef_toggle;
126
633k
        const int by_idx = (by & 30) >> 1;
127
633k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
128
129
633k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
130
528k
            edges & CDEF_HAVE_BOTTOM)
131
528k
        {
132
            // backup pre-filter data for next iteration
133
528k
            pixel *const cdef_top_bak[3] = {
134
528k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
135
528k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
136
528k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
137
528k
            };
138
528k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
139
528k
        }
140
141
633k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
142
633k
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
143
633k
        edges &= ~CDEF_HAVE_LEFT;
144
633k
        edges |= CDEF_HAVE_RIGHT;
145
633k
        enum Backup2x8Flags prev_flag = 0;
146
2.84M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
147
2.21M
            const int sb128x = sbx >> 1;
148
2.21M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
149
2.21M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
150
2.21M
            if (cdef_idx == -1 ||
151
1.41M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
152
637k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
153
1.38M
            {
154
1.38M
                prev_flag = 0;
155
1.38M
                goto next_sb;
156
1.38M
            }
157
158
            // Create a complete 32-bit mask for the sb row ahead of time.
159
821k
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
160
821k
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
161
821k
                                                    noskip_row[0][0];
162
163
821k
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
164
821k
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
165
821k
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
166
167
821k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
168
821k
            int y_sec_lvl = y_lvl & 3;
169
821k
            y_sec_lvl += y_sec_lvl == 3;
170
821k
            y_sec_lvl <<= bitdepth_min_8;
171
172
821k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
173
821k
            int uv_sec_lvl = uv_lvl & 3;
174
821k
            uv_sec_lvl += uv_sec_lvl == 3;
175
821k
            uv_sec_lvl <<= bitdepth_min_8;
176
177
821k
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
178
3.76M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
179
2.93M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
180
2.95M
            {
181
2.95M
                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.95M
                const uint32_t bx_mask = 3U << (bx & 30);
186
2.95M
                if (!(noskip_mask & bx_mask)) {
187
275k
                    prev_flag = 0;
188
275k
                    goto next_b;
189
275k
                }
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
43.0k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
196
43.0k
                }
197
2.67M
                if (edges & CDEF_HAVE_RIGHT) {
198
                    // backup pre-filter data for next iteration
199
2.39M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
200
2.39M
                }
201
202
2.67M
                int dir;
203
2.67M
                unsigned variance;
204
2.67M
                if (y_pri_lvl || uv_pri_lvl)
205
2.57M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
206
2.57M
                                        &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
87.3k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
215
87.3k
                        top = &f->lf.cdef_lpf_line[0][offset];
216
87.3k
                    } else {
217
55.0k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
218
55.0k
                        top = &f->lf.lr_lpf_line[0][offset];
219
55.0k
                    }
220
142k
                    bot = bptrs[0] + 8 * y_stride;
221
2.58M
                } 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
171k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
225
171k
                        bot = &f->lf.cdef_lpf_line[0][offset];
226
171k
                    } else {
227
169k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
228
169k
                        offset = line * y_stride + bx * 4;
229
169k
                        bot = &f->lf.lr_lpf_line[0][offset];
230
169k
                    }
231
2.19M
                } else {
232
2.24M
            st_y:;
233
2.24M
                    offset = sby * 4 * y_stride;
234
2.24M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
235
2.24M
                    bot = bptrs[0] + 8 * y_stride;
236
2.24M
                }
237
2.73M
                if (y_pri_lvl) {
238
2.36M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
239
2.36M
                    if (adj_y_pri_lvl || y_sec_lvl)
240
2.11M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
241
2.11M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
242
2.11M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
243
2.36M
                } else if (y_sec_lvl)
244
236k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
236k
                                    top, bot, 0, y_sec_lvl, 0, damping,
246
236k
                                    edges HIGHBD_CALL_SUFFIX);
247
248
2.73M
                if (!uv_lvl) goto skip_uv;
249
2.00M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
250
251
2.00M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
252
5.84M
                for (int pl = 1; pl <= 2; pl++) {
253
3.80M
                    if (!have_tt) goto st_uv;
254
3.80M
                    if (sbrow_start && by == by_start) {
255
207k
                        if (resize) {
256
120k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
257
120k
                            top = &f->lf.cdef_lpf_line[pl][offset];
258
120k
                        } else {
259
87.0k
                            const int line = sby * (4 << sb128) - 4;
260
87.0k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
261
87.0k
                            top = &f->lf.lr_lpf_line[pl][offset];
262
87.0k
                        }
263
207k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
264
3.61M
                    } else if (!sbrow_start && by + 2 >= by_end) {
265
500k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
266
500k
                                                     (bx * 4 >> ss_hor);
267
500k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
268
500k
                        if (resize) {
269
251k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
270
251k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
271
251k
                        } else {
272
249k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
273
249k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
274
249k
                            bot = &f->lf.lr_lpf_line[pl][offset];
275
249k
                        }
276
3.10M
                    } else {
277
3.12M
                st_uv:;
278
3.12M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
279
3.12M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
280
3.12M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
281
3.12M
                    }
282
3.83M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
283
3.83M
                                         lr_bak[bit][pl], top, bot,
284
3.83M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
285
3.83M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
286
3.83M
                }
287
288
2.66M
            skip_uv:
289
2.66M
                bit ^= 1;
290
291
2.93M
            next_b:
292
2.93M
                bptrs[0] += 8;
293
2.93M
                bptrs[1] += 8 >> ss_hor;
294
2.93M
                bptrs[2] += 8 >> ss_hor;
295
2.93M
            }
296
297
2.21M
        next_sb:
298
2.21M
            iptrs[0] += sbsz * 4;
299
2.21M
            iptrs[1] += sbsz * 4 >> ss_hor;
300
2.21M
            iptrs[2] += sbsz * 4 >> ss_hor;
301
2.21M
        }
302
303
633k
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
304
633k
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
305
633k
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
306
633k
        tc->top_pre_cdef_toggle ^= 1;
307
633k
    }
308
148k
}