Coverage Report

Created: 2026-10-28 06:18

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
2.15M
{
45
2.15M
    const ptrdiff_t y_stride = PXSTRIDE(stride[0]);
46
2.15M
    if (y_stride < 0)
47
0
        pixel_copy(dst[0] + y_stride, src[0] + 7 * y_stride, -2 * y_stride);
48
2.15M
    else
49
2.15M
        pixel_copy(dst[0], src[0] + 6 * y_stride, 2 * y_stride);
50
51
2.15M
    if (layout != DAV1D_PIXEL_LAYOUT_I400) {
52
1.55M
        const ptrdiff_t uv_stride = PXSTRIDE(stride[1]);
53
1.55M
        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.55M
        } else {
58
1.55M
            const int uv_off = layout == DAV1D_PIXEL_LAYOUT_I420 ? 2 : 6;
59
1.55M
            pixel_copy(dst[1], src[1] + uv_off * uv_stride, 2 * uv_stride);
60
1.55M
            pixel_copy(dst[2], src[2] + uv_off * uv_stride, 2 * uv_stride);
61
1.55M
        }
62
1.55M
    }
63
2.15M
}
64
65
static void backup2x8(pixel dst[3][8][2],
66
                      /*const*/ pixel *const src[3],
67
                      const ptrdiff_t src_stride[2], ptrdiff_t x_off,
68
                      const enum Dav1dPixelLayout layout,
69
                      const enum Backup2x8Flags flag)
70
6.24M
{
71
6.24M
    ptrdiff_t y_off = 0;
72
6.24M
    if (flag & BACKUP_2X8_Y) {
73
6.01M
        const ptrdiff_t y_stride = PXSTRIDE(src_stride[0]);
74
6.01M
        const pixel *src0 = &src[0][x_off - 2];
75
54.1M
        for (int y = 0; y < 8; y++, y_off += y_stride)
76
48.0M
            pixel_copy(dst[0][y], &src0[y_off], 2);
77
6.01M
    }
78
79
6.24M
    if (layout == DAV1D_PIXEL_LAYOUT_I400 || !(flag & BACKUP_2X8_UV))
80
1.38M
        return;
81
82
4.86M
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
83
4.86M
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
84
85
4.86M
    x_off >>= ss_hor;
86
4.86M
    y_off = 0;
87
4.86M
    const ptrdiff_t uv_stride = PXSTRIDE(src_stride[1]);
88
4.86M
    const pixel *src1 = &src[1][x_off - 2], *const src2 = &src[2][x_off - 2];
89
30.5M
    for (int y = 0; y < (8 >> ss_ver); y++, y_off += uv_stride) {
90
25.6M
        pixel_copy(dst[1][y], &src1[y_off], 2);
91
25.6M
        pixel_copy(dst[2][y], &src2[y_off], 2);
92
25.6M
    }
93
4.86M
}
94
95
6.17M
static int adjust_strength(const int strength, const unsigned var) {
96
6.17M
    if (!var) return 0;
97
2.93M
    const int i = var >> 6 ? imin(ulog2(var >> 6), 12) : 0;
98
2.93M
    return (strength * (4 + i) + 8) >> 4;
99
6.17M
}
100
101
void bytefn(dav1d_cdef_brow)(Dav1dTaskContext *const tc,
102
                             pixel *const p[3],
103
                             const Av1Filter *const lflvl,
104
                             const int by_start, const int by_end,
105
                             const int sbrow_start, const int sby)
106
472k
{
107
472k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
108
472k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
109
472k
    const Dav1dDSPContext *const dsp = f->dsp;
110
472k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
111
472k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
112
472k
    const int sbsz = 16;
113
472k
    const int sb64w = f->sb128w << 1;
114
472k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
115
472k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
116
472k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
117
472k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
118
472k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
119
472k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
120
472k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
121
472k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
122
472k
    const int have_tt = f->c->n_tc > 1;
123
472k
    const int sb128 = f->seq_hdr->sb128;
124
472k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
125
472k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
126
472k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
127
128
2.87M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
129
2.41M
        const int tf = tc->top_pre_cdef_toggle;
130
2.41M
        const int by_idx = (by & 30) >> 1;
131
2.41M
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
132
133
2.41M
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
134
2.15M
            edges & CDEF_HAVE_BOTTOM)
135
2.15M
        {
136
            // backup pre-filter data for next iteration
137
2.15M
            pixel *const cdef_top_bak[3] = {
138
2.15M
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
139
2.15M
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
140
2.15M
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
141
2.15M
            };
142
2.15M
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
143
2.15M
        }
144
145
2.41M
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
146
2.41M
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
147
2.41M
        edges &= ~CDEF_HAVE_LEFT;
148
2.41M
        edges |= CDEF_HAVE_RIGHT;
149
2.41M
        enum Backup2x8Flags prev_flag = 0;
150
8.86M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
151
6.45M
            const int sb128x = sbx >> 1;
152
6.45M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
153
6.45M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
154
6.45M
            if (cdef_idx == -1 ||
155
3.16M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
156
985k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
157
4.16M
            {
158
4.16M
                prev_flag = 0;
159
4.16M
                goto next_sb;
160
4.16M
            }
161
162
            // Create a complete 32-bit mask for the sb row ahead of time.
163
2.28M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
164
2.28M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
165
2.28M
                                                    noskip_row[0][0];
166
167
2.28M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
168
2.28M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
169
2.28M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
170
171
2.28M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
172
2.28M
            int y_sec_lvl = y_lvl & 3;
173
2.28M
            y_sec_lvl += y_sec_lvl == 3;
174
2.28M
            y_sec_lvl <<= bitdepth_min_8;
175
176
2.28M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
177
2.28M
            int uv_sec_lvl = uv_lvl & 3;
178
2.28M
            uv_sec_lvl += uv_sec_lvl == 3;
179
2.28M
            uv_sec_lvl <<= bitdepth_min_8;
180
181
2.28M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
182
9.96M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
183
7.68M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
184
7.72M
            {
185
7.72M
                if (bx + 2 >= f->bw) edges &= ~CDEF_HAVE_RIGHT;
186
187
                // check if this 8x8 block had any coded coefficients; if not,
188
                // go to the next block
189
7.72M
                const uint32_t bx_mask = 3U << (bx & 30);
190
7.72M
                if (!(noskip_mask & bx_mask)) {
191
998k
                    prev_flag = 0;
192
998k
                    goto next_b;
193
998k
                }
194
6.72M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
195
6.72M
                prev_flag = flag;
196
6.72M
                if (do_left && edges & CDEF_HAVE_LEFT) {
197
                    // we didn't backup the prefilter data because it wasn't
198
                    // there, so do it here instead
199
111k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
200
111k
                }
201
6.72M
                if (edges & CDEF_HAVE_RIGHT) {
202
                    // backup pre-filter data for next iteration
203
6.13M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
204
6.13M
                }
205
206
6.72M
                int dir;
207
6.72M
                unsigned variance;
208
6.72M
                if (y_pri_lvl || uv_pri_lvl)
209
6.52M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
210
6.52M
                                        &variance HIGHBD_CALL_SUFFIX);
211
212
6.72M
                const pixel *top, *bot;
213
6.72M
                ptrdiff_t offset;
214
215
6.72M
                if (!have_tt) goto st_y;
216
6.72M
                if (sbrow_start && by == by_start) {
217
491k
                    if (resize) {
218
131k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
219
131k
                        top = &f->lf.cdef_lpf_line[0][offset];
220
359k
                    } else {
221
359k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
222
359k
                        top = &f->lf.lr_lpf_line[0][offset];
223
359k
                    }
224
491k
                    bot = bptrs[0] + 8 * y_stride;
225
6.42M
                } else if (!sbrow_start && by + 2 >= by_end) {
226
920k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
227
920k
                    if (resize) {
228
293k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
229
293k
                        bot = &f->lf.cdef_lpf_line[0][offset];
230
627k
                    } else {
231
627k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
232
627k
                        offset = line * y_stride + bx * 4;
233
627k
                        bot = &f->lf.lr_lpf_line[0][offset];
234
627k
                    }
235
5.31M
                } else {
236
5.50M
            st_y:;
237
5.50M
                    offset = sby * 4 * y_stride;
238
5.50M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
239
5.50M
                    bot = bptrs[0] + 8 * y_stride;
240
5.50M
                }
241
6.91M
                if (y_pri_lvl) {
242
6.17M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
243
6.17M
                    if (adj_y_pri_lvl || y_sec_lvl)
244
5.45M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
5.45M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
246
5.45M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
247
6.17M
                } else if (y_sec_lvl)
248
462k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
249
462k
                                    top, bot, 0, y_sec_lvl, 0, damping,
250
462k
                                    edges HIGHBD_CALL_SUFFIX);
251
252
6.91M
                if (!uv_lvl) goto skip_uv;
253
5.43M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
254
255
5.43M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
256
15.7M
                for (int pl = 1; pl <= 2; pl++) {
257
10.1M
                    if (!have_tt) goto st_uv;
258
10.1M
                    if (sbrow_start && by == by_start) {
259
741k
                        if (resize) {
260
188k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
261
188k
                            top = &f->lf.cdef_lpf_line[pl][offset];
262
552k
                        } else {
263
552k
                            const int line = sby * (4 << sb128) - 4;
264
552k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
265
552k
                            top = &f->lf.lr_lpf_line[pl][offset];
266
552k
                        }
267
741k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
268
9.48M
                    } else if (!sbrow_start && by + 2 >= by_end) {
269
1.40M
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
270
1.40M
                                                     (bx * 4 >> ss_hor);
271
1.40M
                        top = &f->lf.cdef_line[tf][pl][top_offset];
272
1.40M
                        if (resize) {
273
437k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
274
437k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
275
962k
                        } else {
276
962k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
277
962k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
278
962k
                            bot = &f->lf.lr_lpf_line[pl][offset];
279
962k
                        }
280
8.04M
                    } else {
281
8.16M
                st_uv:;
282
8.16M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
283
8.16M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
284
8.16M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
285
8.16M
                    }
286
10.3M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
287
10.3M
                                         lr_bak[bit][pl], top, bot,
288
10.3M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
289
10.3M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
290
10.3M
                }
291
292
6.69M
            skip_uv:
293
6.69M
                bit ^= 1;
294
295
7.68M
            next_b:
296
7.68M
                bptrs[0] += 8;
297
7.68M
                bptrs[1] += 8 >> ss_hor;
298
7.68M
                bptrs[2] += 8 >> ss_hor;
299
7.68M
            }
300
301
6.44M
        next_sb:
302
6.44M
            iptrs[0] += sbsz * 4;
303
6.44M
            iptrs[1] += sbsz * 4 >> ss_hor;
304
6.44M
            iptrs[2] += sbsz * 4 >> ss_hor;
305
6.44M
        }
306
307
2.40M
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
308
2.40M
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
309
2.40M
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
310
2.40M
        tc->top_pre_cdef_toggle ^= 1;
311
2.40M
    }
312
472k
}
dav1d_cdef_brow_8bpc
Line
Count
Source
106
254k
{
107
254k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
108
254k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
109
254k
    const Dav1dDSPContext *const dsp = f->dsp;
110
254k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
111
254k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
112
254k
    const int sbsz = 16;
113
254k
    const int sb64w = f->sb128w << 1;
114
254k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
115
254k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
116
254k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
117
254k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
118
254k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
119
254k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
120
254k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
121
254k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
122
254k
    const int have_tt = f->c->n_tc > 1;
123
254k
    const int sb128 = f->seq_hdr->sb128;
124
254k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
125
254k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
126
254k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
127
128
1.56M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
129
1.31M
        const int tf = tc->top_pre_cdef_toggle;
130
1.31M
        const int by_idx = (by & 30) >> 1;
131
1.31M
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
132
133
1.31M
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
134
1.18M
            edges & CDEF_HAVE_BOTTOM)
135
1.18M
        {
136
            // backup pre-filter data for next iteration
137
1.18M
            pixel *const cdef_top_bak[3] = {
138
1.18M
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
139
1.18M
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
140
1.18M
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
141
1.18M
            };
142
1.18M
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
143
1.18M
        }
144
145
1.31M
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
146
1.31M
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
147
1.31M
        edges &= ~CDEF_HAVE_LEFT;
148
1.31M
        edges |= CDEF_HAVE_RIGHT;
149
1.31M
        enum Backup2x8Flags prev_flag = 0;
150
4.65M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
151
3.33M
            const int sb128x = sbx >> 1;
152
3.33M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
153
3.33M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
154
3.33M
            if (cdef_idx == -1 ||
155
1.59M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
156
465k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
157
2.17M
            {
158
2.17M
                prev_flag = 0;
159
2.17M
                goto next_sb;
160
2.17M
            }
161
162
            // Create a complete 32-bit mask for the sb row ahead of time.
163
1.15M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
164
1.15M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
165
1.15M
                                                    noskip_row[0][0];
166
167
1.15M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
168
1.15M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
169
1.15M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
170
171
1.15M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
172
1.15M
            int y_sec_lvl = y_lvl & 3;
173
1.15M
            y_sec_lvl += y_sec_lvl == 3;
174
1.15M
            y_sec_lvl <<= bitdepth_min_8;
175
176
1.15M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
177
1.15M
            int uv_sec_lvl = uv_lvl & 3;
178
1.15M
            uv_sec_lvl += uv_sec_lvl == 3;
179
1.15M
            uv_sec_lvl <<= bitdepth_min_8;
180
181
1.15M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
182
4.88M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
183
3.72M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
184
3.75M
            {
185
3.75M
                if (bx + 2 >= f->bw) edges &= ~CDEF_HAVE_RIGHT;
186
187
                // check if this 8x8 block had any coded coefficients; if not,
188
                // go to the next block
189
3.75M
                const uint32_t bx_mask = 3U << (bx & 30);
190
3.75M
                if (!(noskip_mask & bx_mask)) {
191
527k
                    prev_flag = 0;
192
527k
                    goto next_b;
193
527k
                }
194
3.22M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
195
3.22M
                prev_flag = flag;
196
3.22M
                if (do_left && edges & CDEF_HAVE_LEFT) {
197
                    // we didn't backup the prefilter data because it wasn't
198
                    // there, so do it here instead
199
61.7k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
200
61.7k
                }
201
3.22M
                if (edges & CDEF_HAVE_RIGHT) {
202
                    // backup pre-filter data for next iteration
203
2.92M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
204
2.92M
                }
205
206
3.22M
                int dir;
207
3.22M
                unsigned variance;
208
3.22M
                if (y_pri_lvl || uv_pri_lvl)
209
3.10M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
210
3.10M
                                        &variance HIGHBD_CALL_SUFFIX);
211
212
3.22M
                const pixel *top, *bot;
213
3.22M
                ptrdiff_t offset;
214
215
3.22M
                if (!have_tt) goto st_y;
216
3.22M
                if (sbrow_start && by == by_start) {
217
272k
                    if (resize) {
218
40.2k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
219
40.2k
                        top = &f->lf.cdef_lpf_line[0][offset];
220
232k
                    } else {
221
232k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
222
232k
                        top = &f->lf.lr_lpf_line[0][offset];
223
232k
                    }
224
272k
                    bot = bptrs[0] + 8 * y_stride;
225
3.07M
                } else if (!sbrow_start && by + 2 >= by_end) {
226
477k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
227
477k
                    if (resize) {
228
102k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
229
102k
                        bot = &f->lf.cdef_lpf_line[0][offset];
230
375k
                    } else {
231
375k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
232
375k
                        offset = line * y_stride + bx * 4;
233
375k
                        bot = &f->lf.lr_lpf_line[0][offset];
234
375k
                    }
235
2.47M
                } else {
236
2.59M
            st_y:;
237
2.59M
                    offset = sby * 4 * y_stride;
238
2.59M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
239
2.59M
                    bot = bptrs[0] + 8 * y_stride;
240
2.59M
                }
241
3.34M
                if (y_pri_lvl) {
242
2.98M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
243
2.98M
                    if (adj_y_pri_lvl || y_sec_lvl)
244
2.61M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
2.61M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
246
2.61M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
247
2.98M
                } else if (y_sec_lvl)
248
258k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
249
258k
                                    top, bot, 0, y_sec_lvl, 0, damping,
250
258k
                                    edges HIGHBD_CALL_SUFFIX);
251
252
3.34M
                if (!uv_lvl) goto skip_uv;
253
2.52M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
254
255
2.52M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
256
7.20M
                for (int pl = 1; pl <= 2; pl++) {
257
4.61M
                    if (!have_tt) goto st_uv;
258
4.61M
                    if (sbrow_start && by == by_start) {
259
388k
                        if (resize) {
260
39.7k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
261
39.7k
                            top = &f->lf.cdef_lpf_line[pl][offset];
262
348k
                        } else {
263
348k
                            const int line = sby * (4 << sb128) - 4;
264
348k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
265
348k
                            top = &f->lf.lr_lpf_line[pl][offset];
266
348k
                        }
267
388k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
268
4.25M
                    } else if (!sbrow_start && by + 2 >= by_end) {
269
679k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
270
679k
                                                     (bx * 4 >> ss_hor);
271
679k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
272
679k
                        if (resize) {
273
118k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
274
118k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
275
561k
                        } else {
276
561k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
277
561k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
278
561k
                            bot = &f->lf.lr_lpf_line[pl][offset];
279
561k
                        }
280
3.54M
                    } else {
281
3.61M
                st_uv:;
282
3.61M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
283
3.61M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
284
3.61M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
285
3.61M
                    }
286
4.68M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
287
4.68M
                                         lr_bak[bit][pl], top, bot,
288
4.68M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
289
4.68M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
290
4.68M
                }
291
292
3.20M
            skip_uv:
293
3.20M
                bit ^= 1;
294
295
3.72M
            next_b:
296
3.72M
                bptrs[0] += 8;
297
3.72M
                bptrs[1] += 8 >> ss_hor;
298
3.72M
                bptrs[2] += 8 >> ss_hor;
299
3.72M
            }
300
301
3.33M
        next_sb:
302
3.33M
            iptrs[0] += sbsz * 4;
303
3.33M
            iptrs[1] += sbsz * 4 >> ss_hor;
304
3.33M
            iptrs[2] += sbsz * 4 >> ss_hor;
305
3.33M
        }
306
307
1.31M
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
308
1.31M
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
309
1.31M
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
310
1.31M
        tc->top_pre_cdef_toggle ^= 1;
311
1.31M
    }
312
254k
}
dav1d_cdef_brow_16bpc
Line
Count
Source
106
217k
{
107
217k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
108
217k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
109
217k
    const Dav1dDSPContext *const dsp = f->dsp;
110
217k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
111
217k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
112
217k
    const int sbsz = 16;
113
217k
    const int sb64w = f->sb128w << 1;
114
217k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
115
217k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
116
217k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
117
217k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
118
217k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
119
217k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
120
217k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
121
217k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
122
217k
    const int have_tt = f->c->n_tc > 1;
123
217k
    const int sb128 = f->seq_hdr->sb128;
124
217k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
125
217k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
126
217k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
127
128
1.31M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
129
1.09M
        const int tf = tc->top_pre_cdef_toggle;
130
1.09M
        const int by_idx = (by & 30) >> 1;
131
1.09M
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
132
133
1.09M
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
134
974k
            edges & CDEF_HAVE_BOTTOM)
135
974k
        {
136
            // backup pre-filter data for next iteration
137
974k
            pixel *const cdef_top_bak[3] = {
138
974k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
139
974k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
140
974k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
141
974k
            };
142
974k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout);
143
974k
        }
144
145
1.09M
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
146
1.09M
        pixel *iptrs[3] = { ptrs[0], ptrs[1], ptrs[2] };
147
1.09M
        edges &= ~CDEF_HAVE_LEFT;
148
1.09M
        edges |= CDEF_HAVE_RIGHT;
149
1.09M
        enum Backup2x8Flags prev_flag = 0;
150
4.20M
        for (int sbx = 0; sbx < sb64w; sbx++, edges |= CDEF_HAVE_LEFT) {
151
3.11M
            const int sb128x = sbx >> 1;
152
3.11M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
153
3.11M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
154
3.11M
            if (cdef_idx == -1 ||
155
1.57M
                (!f->frame_hdr->cdef.y_strength[cdef_idx] &&
156
520k
                 !f->frame_hdr->cdef.uv_strength[cdef_idx]))
157
1.99M
            {
158
1.99M
                prev_flag = 0;
159
1.99M
                goto next_sb;
160
1.99M
            }
161
162
            // Create a complete 32-bit mask for the sb row ahead of time.
163
1.12M
            const uint16_t (*noskip_row)[2] = &lflvl[sb128x].noskip_mask[by_idx];
164
1.12M
            const unsigned noskip_mask = (unsigned) noskip_row[0][1] << 16 |
165
1.12M
                                                    noskip_row[0][0];
166
167
1.12M
            const int y_lvl = f->frame_hdr->cdef.y_strength[cdef_idx];
168
1.12M
            const int uv_lvl = f->frame_hdr->cdef.uv_strength[cdef_idx];
169
1.12M
            const enum Backup2x8Flags flag = !!y_lvl + (!!uv_lvl << 1);
170
171
1.12M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
172
1.12M
            int y_sec_lvl = y_lvl & 3;
173
1.12M
            y_sec_lvl += y_sec_lvl == 3;
174
1.12M
            y_sec_lvl <<= bitdepth_min_8;
175
176
1.12M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
177
1.12M
            int uv_sec_lvl = uv_lvl & 3;
178
1.12M
            uv_sec_lvl += uv_sec_lvl == 3;
179
1.12M
            uv_sec_lvl <<= bitdepth_min_8;
180
181
1.12M
            pixel *bptrs[3] = { iptrs[0], iptrs[1], iptrs[2] };
182
5.08M
            for (int bx = sbx * sbsz; bx < imin((sbx + 1) * sbsz, f->bw);
183
3.95M
                 bx += 2, edges |= CDEF_HAVE_LEFT)
184
3.97M
            {
185
3.97M
                if (bx + 2 >= f->bw) edges &= ~CDEF_HAVE_RIGHT;
186
187
                // check if this 8x8 block had any coded coefficients; if not,
188
                // go to the next block
189
3.97M
                const uint32_t bx_mask = 3U << (bx & 30);
190
3.97M
                if (!(noskip_mask & bx_mask)) {
191
470k
                    prev_flag = 0;
192
470k
                    goto next_b;
193
470k
                }
194
3.50M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
195
3.50M
                prev_flag = flag;
196
3.50M
                if (do_left && edges & CDEF_HAVE_LEFT) {
197
                    // we didn't backup the prefilter data because it wasn't
198
                    // there, so do it here instead
199
49.4k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
200
49.4k
                }
201
3.50M
                if (edges & CDEF_HAVE_RIGHT) {
202
                    // backup pre-filter data for next iteration
203
3.21M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
204
3.21M
                }
205
206
3.50M
                int dir;
207
3.50M
                unsigned variance;
208
3.50M
                if (y_pri_lvl || uv_pri_lvl)
209
3.41M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
210
3.41M
                                        &variance HIGHBD_CALL_SUFFIX);
211
212
3.50M
                const pixel *top, *bot;
213
3.50M
                ptrdiff_t offset;
214
215
3.50M
                if (!have_tt) goto st_y;
216
3.50M
                if (sbrow_start && by == by_start) {
217
218k
                    if (resize) {
218
91.1k
                        offset = (sby - 1) * 4 * y_stride + bx * 4;
219
91.1k
                        top = &f->lf.cdef_lpf_line[0][offset];
220
127k
                    } else {
221
127k
                        offset = (sby * (4 << sb128) - 4) * y_stride + bx * 4;
222
127k
                        top = &f->lf.lr_lpf_line[0][offset];
223
127k
                    }
224
218k
                    bot = bptrs[0] + 8 * y_stride;
225
3.34M
                } else if (!sbrow_start && by + 2 >= by_end) {
226
442k
                    top = &f->lf.cdef_line[tf][0][sby * 4 * y_stride + bx * 4];
227
442k
                    if (resize) {
228
191k
                        offset = (sby * 4 + 2) * y_stride + bx * 4;
229
191k
                        bot = &f->lf.cdef_lpf_line[0][offset];
230
251k
                    } else {
231
251k
                        const int line = sby * (4 << sb128) + 4 * sb128 + 2;
232
251k
                        offset = line * y_stride + bx * 4;
233
251k
                        bot = &f->lf.lr_lpf_line[0][offset];
234
251k
                    }
235
2.84M
                } else {
236
2.90M
            st_y:;
237
2.90M
                    offset = sby * 4 * y_stride;
238
2.90M
                    top = &f->lf.cdef_line[tf][0][have_tt * offset + bx * 4];
239
2.90M
                    bot = bptrs[0] + 8 * y_stride;
240
2.90M
                }
241
3.56M
                if (y_pri_lvl) {
242
3.18M
                    const int adj_y_pri_lvl = adjust_strength(y_pri_lvl, variance);
243
3.18M
                    if (adj_y_pri_lvl || y_sec_lvl)
244
2.83M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
245
2.83M
                                        top, bot, adj_y_pri_lvl, y_sec_lvl,
246
2.83M
                                        dir, damping, edges HIGHBD_CALL_SUFFIX);
247
3.18M
                } else if (y_sec_lvl)
248
203k
                    dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
249
203k
                                    top, bot, 0, y_sec_lvl, 0, damping,
250
203k
                                    edges HIGHBD_CALL_SUFFIX);
251
252
3.56M
                if (!uv_lvl) goto skip_uv;
253
2.91M
                assert(layout != DAV1D_PIXEL_LAYOUT_I400);
254
255
2.91M
                const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
256
8.53M
                for (int pl = 1; pl <= 2; pl++) {
257
5.56M
                    if (!have_tt) goto st_uv;
258
5.56M
                    if (sbrow_start && by == by_start) {
259
352k
                        if (resize) {
260
148k
                            offset = (sby - 1) * 4 * uv_stride + (bx * 4 >> ss_hor);
261
148k
                            top = &f->lf.cdef_lpf_line[pl][offset];
262
204k
                        } else {
263
204k
                            const int line = sby * (4 << sb128) - 4;
264
204k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
265
204k
                            top = &f->lf.lr_lpf_line[pl][offset];
266
204k
                        }
267
352k
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
268
5.23M
                    } else if (!sbrow_start && by + 2 >= by_end) {
269
720k
                        const ptrdiff_t top_offset = sby * 8 * uv_stride +
270
720k
                                                     (bx * 4 >> ss_hor);
271
720k
                        top = &f->lf.cdef_line[tf][pl][top_offset];
272
720k
                        if (resize) {
273
319k
                            offset = (sby * 4 + 2) * uv_stride + (bx * 4 >> ss_hor);
274
319k
                            bot = &f->lf.cdef_lpf_line[pl][offset];
275
400k
                        } else {
276
400k
                            const int line = sby * (4 << sb128) + 4 * sb128 + 2;
277
400k
                            offset = line * uv_stride + (bx * 4 >> ss_hor);
278
400k
                            bot = &f->lf.lr_lpf_line[pl][offset];
279
400k
                        }
280
4.49M
                    } else {
281
4.54M
                st_uv:;
282
4.54M
                        const ptrdiff_t offset = sby * 8 * uv_stride;
283
4.54M
                        top = &f->lf.cdef_line[tf][pl][have_tt * offset + (bx * 4 >> ss_hor)];
284
4.54M
                        bot = bptrs[pl] + (8 >> ss_ver) * uv_stride;
285
4.54M
                    }
286
5.61M
                    dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1],
287
5.61M
                                         lr_bak[bit][pl], top, bot,
288
5.61M
                                         uv_pri_lvl, uv_sec_lvl, uvdir,
289
5.61M
                                         damping - 1, edges HIGHBD_CALL_SUFFIX);
290
5.61M
                }
291
292
3.48M
            skip_uv:
293
3.48M
                bit ^= 1;
294
295
3.95M
            next_b:
296
3.95M
                bptrs[0] += 8;
297
3.95M
                bptrs[1] += 8 >> ss_hor;
298
3.95M
                bptrs[2] += 8 >> ss_hor;
299
3.95M
            }
300
301
3.11M
        next_sb:
302
3.11M
            iptrs[0] += sbsz * 4;
303
3.11M
            iptrs[1] += sbsz * 4 >> ss_hor;
304
3.11M
            iptrs[2] += sbsz * 4 >> ss_hor;
305
3.11M
        }
306
307
1.09M
        ptrs[0] += 8 * PXSTRIDE(f->cur.stride[0]);
308
1.09M
        ptrs[1] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
309
1.09M
        ptrs[2] += 8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver;
310
1.09M
        tc->top_pre_cdef_toggle ^= 1;
311
1.09M
    }
312
217k
}