Coverage Report

Created: 2026-12-01 06:52

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
                         const enum Backup2x8Flags flag)
45
1.43M
{
46
1.43M
    if (flag & BACKUP_2X8_Y) {
47
1.33M
        const ptrdiff_t y_stride = PXSTRIDE(stride[0]);
48
1.33M
        if (y_stride < 0)
49
0
            pixel_copy(dst[0] + y_stride, src[0] + 7 * y_stride, -2 * y_stride);
50
1.33M
        else
51
1.33M
            pixel_copy(dst[0], src[0] + 6 * y_stride, 2 * y_stride);
52
1.33M
    }
53
54
1.43M
    if (flag & BACKUP_2X8_UV) {
55
1.00M
        assert(layout != DAV1D_PIXEL_LAYOUT_I400);
56
1.00M
        const ptrdiff_t uv_stride = PXSTRIDE(stride[1]);
57
1.00M
        if (uv_stride < 0) {
58
0
            const int uv_off = layout == DAV1D_PIXEL_LAYOUT_I420 ? 3 : 7;
59
0
            pixel_copy(dst[1] + uv_stride, src[1] + uv_off * uv_stride, -2 * uv_stride);
60
0
            pixel_copy(dst[2] + uv_stride, src[2] + uv_off * uv_stride, -2 * uv_stride);
61
1.00M
        } else {
62
1.00M
            const int uv_off = layout == DAV1D_PIXEL_LAYOUT_I420 ? 2 : 6;
63
1.00M
            pixel_copy(dst[1], src[1] + uv_off * uv_stride, 2 * uv_stride);
64
1.00M
            pixel_copy(dst[2], src[2] + uv_off * uv_stride, 2 * uv_stride);
65
1.00M
        }
66
1.00M
    }
67
1.43M
}
68
69
static void backup2x8(pixel dst[3][8][2],
70
                      /*const*/ pixel *const src[3],
71
                      const ptrdiff_t src_stride[2], ptrdiff_t x_off,
72
                      const enum Dav1dPixelLayout layout,
73
                      const enum Backup2x8Flags flag)
74
7.03M
{
75
7.03M
    ptrdiff_t y_off = 0;
76
7.03M
    if (flag & BACKUP_2X8_Y) {
77
6.54M
        const ptrdiff_t y_stride = PXSTRIDE(src_stride[0]);
78
6.54M
        const pixel *src0 = &src[0][x_off - 2];
79
58.9M
        for (int y = 0; y < 8; y++, y_off += y_stride)
80
52.3M
            pixel_copy(dst[0][y], &src0[y_off], 2);
81
6.54M
    }
82
83
7.03M
    if (!(flag & BACKUP_2X8_UV))
84
1.32M
        return;
85
5.70M
    assert(layout != DAV1D_PIXEL_LAYOUT_I400);
86
87
5.70M
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
88
5.70M
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
89
90
5.70M
    x_off >>= ss_hor;
91
5.70M
    y_off = 0;
92
5.70M
    const ptrdiff_t uv_stride = PXSTRIDE(src_stride[1]);
93
5.70M
    const pixel *src1 = &src[1][x_off - 2], *const src2 = &src[2][x_off - 2];
94
35.7M
    for (int y = 0; y < (8 >> ss_ver); y++, y_off += uv_stride) {
95
30.0M
        pixel_copy(dst[1][y], &src1[y_off], 2);
96
30.0M
        pixel_copy(dst[2][y], &src2[y_off], 2);
97
30.0M
    }
98
5.70M
}
99
100
6.77M
static int adjust_strength(const int strength, const unsigned var) {
101
6.77M
    if (!var) return 0;
102
3.02M
    const int i = var >> 6 ? imin(ulog2(var >> 6), 12) : 0;
103
3.02M
    return (strength * (4 + i) + 8) >> 4;
104
6.77M
}
105
106
void bytefn(dav1d_cdef_brow)(Dav1dTaskContext *const tc,
107
                             pixel *const p[3],
108
                             const Av1Filter *const lflvl,
109
                             const int by_start, const int by_end,
110
                             const int sbrow_start, const int sby)
111
328k
{
112
328k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
113
328k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
114
328k
    const Dav1dDSPContext *const dsp = f->dsp;
115
328k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
116
328k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
117
328k
    const int sbsz = 16;
118
328k
    const int sb64w = f->sb128w << 1;
119
328k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
120
328k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
121
328k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
122
328k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
123
328k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
124
328k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
125
328k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
126
328k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
127
328k
    assert(f->lf.cdef_planes);
128
328k
    assert(BACKUP_2X8_Y == 1 && BACKUP_2X8_UV == 2);
129
328k
    const enum Backup2x8Flags frame_backup_flag = f->lf.cdef_planes;
130
328k
    const int have_tt = f->c->n_tc > 1;
131
328k
    const int sb128 = f->seq_hdr->sb128;
132
328k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
133
328k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
134
328k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
135
136
1.93M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
137
1.60M
        const int tf = tc->top_pre_cdef_toggle;
138
1.60M
        const int by_idx = (by & 30) >> 1;
139
1.60M
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
140
141
1.63M
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
142
1.43M
            edges & CDEF_HAVE_BOTTOM)
143
1.43M
        {
144
            // backup pre-filter data for next iteration
145
1.43M
            pixel *const cdef_top_bak[3] = {
146
1.43M
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
147
1.43M
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
148
1.43M
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
149
1.43M
            };
150
1.43M
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout, frame_backup_flag);
151
1.43M
        }
152
153
1.60M
        const pixel *top_base[3], *bot_base[3];
154
1.60M
        pixel *const *const lpf = resize ? f->lf.cdef_lpf_line : f->lf.lr_lpf_line;
155
1.62M
        if (have_tt && sbrow_start && by == by_start) {
156
136k
            const int line = resize ? (sby - 1) * 4 : sby * (4 << sb128) - 4;
157
547k
            for (int pl = 0; pl < 3; pl++)
158
410k
                top_base[pl] = &lpf[pl][line * (pl ? uv_stride : y_stride)];
159
1.47M
        } else {
160
1.47M
            top_base[0] = &f->lf.cdef_line[tf][0][have_tt * sby * 4 * y_stride];
161
1.47M
            top_base[1] = &f->lf.cdef_line[tf][1][have_tt * sby * 8 * uv_stride];
162
1.47M
            top_base[2] = &f->lf.cdef_line[tf][2][have_tt * sby * 8 * uv_stride];
163
1.47M
        }
164
1.62M
        if (have_tt && !sbrow_start && by + 2 >= by_end) {
165
191k
            const int line = resize ? sby * 4 + 2 : sby * (4 << sb128) + 4 * sb128 + 2;
166
767k
            for (int pl = 0; pl < 3; pl++)
167
575k
                bot_base[pl] = &lpf[pl][line * (pl ? uv_stride : y_stride)];
168
1.41M
        } else {
169
1.41M
            bot_base[0] = ptrs[0] + 8 * y_stride;
170
1.41M
            bot_base[1] = ptrs[1] + (8 >> ss_ver) * uv_stride;
171
1.41M
            bot_base[2] = ptrs[2] + (8 >> ss_ver) * uv_stride;
172
1.41M
        }
173
174
1.60M
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
175
1.60M
        enum Backup2x8Flags prev_flag = 0;
176
6.04M
        for (int sbx = 0; sbx < sb64w; sbx++) {
177
4.43M
            const int sb128x = sbx >> 1;
178
4.43M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
179
4.43M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
180
4.43M
            unsigned row_mask = lflvl[sb128x].noskip_mask[by_idx][sbx & 1];
181
4.43M
            const int y_lvl = cdef_idx == -1 ? 0 : f->frame_hdr->cdef.y_strength[cdef_idx];
182
4.43M
            const int uv_lvl = cdef_idx == -1 ? 0 : f->frame_hdr->cdef.uv_strength[cdef_idx];
183
4.43M
            if (!row_mask || !(y_lvl | uv_lvl)) {
184
2.92M
                prev_flag = 0;
185
2.92M
                continue;
186
2.92M
            }
187
188
1.51M
            const enum Backup2x8Flags flag = (!!y_lvl) | ((!!uv_lvl) << 1);
189
190
1.51M
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
191
1.51M
            int y_sec_lvl = y_lvl & 3;
192
1.51M
            y_sec_lvl += y_sec_lvl == 3;
193
1.51M
            y_sec_lvl <<= bitdepth_min_8;
194
195
1.51M
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
196
1.51M
            int uv_sec_lvl = uv_lvl & 3;
197
1.51M
            uv_sec_lvl += uv_sec_lvl == 3;
198
1.51M
            uv_sec_lvl <<= bitdepth_min_8;
199
200
1.51M
            const int bx_base = sbx * sbsz, bx_limit = imin(sbsz, f->bw - bx_base);
201
9.24M
            while (row_mask) {
202
8.26M
                const int bx_off = ctz(row_mask) & ~1;
203
8.26M
                row_mask &= ~(3U << bx_off);
204
8.26M
                if (bx_off >= bx_limit) break;
205
206
7.73M
                const int bx = bx_base + bx_off;
207
7.73M
                const int have_right = bx + 2 < f->bw;
208
7.73M
                const enum CdefEdgeFlags b_edges = edges |
209
7.73M
                    (bx > 0 ? CDEF_HAVE_LEFT : 0) | (have_right ? CDEF_HAVE_RIGHT : 0);
210
211
7.73M
                const ptrdiff_t x_y = (ptrdiff_t) bx * 4, x_uv = x_y >> ss_hor;
212
7.73M
                pixel *const bptrs[3] = { ptrs[0] + x_y, ptrs[1] + x_uv, ptrs[2] + x_uv };
213
214
7.73M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
215
7.73M
                if (do_left && bx > 0)
216
98.4k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
217
7.73M
                if (have_right &&
218
7.02M
                    (bx_off < 14 ? ((row_mask >> (bx_off + 2)) & 3)
219
7.02M
                                 : (lflvl[(sbx + 1) >> 1].noskip_mask[by_idx][(sbx + 1) & 1] & 3)))
220
6.93M
                {
221
6.93M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
222
6.93M
                    prev_flag = flag;
223
6.93M
                } else {
224
797k
                    prev_flag = 0;
225
797k
                }
226
227
7.73M
                int dir;
228
7.73M
                unsigned variance;
229
7.73M
                if (y_pri_lvl || uv_pri_lvl)
230
7.42M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
231
7.42M
                                        &variance HIGHBD_CALL_SUFFIX);
232
233
7.73M
                if (y_lvl) {
234
7.35M
                    const int adj_y_pri_lvl = y_pri_lvl ? adjust_strength(y_pri_lvl, variance) : 0;
235
7.35M
                    if (adj_y_pri_lvl || y_sec_lvl)
236
6.63M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
237
6.63M
                                        top_base[0] + x_y, bot_base[0] + x_y,
238
6.63M
                                        adj_y_pri_lvl, y_sec_lvl,
239
6.63M
                                        y_pri_lvl ? dir : 0, damping,
240
6.63M
                                        b_edges HIGHBD_CALL_SUFFIX);
241
7.35M
                }
242
7.73M
                if (uv_lvl) {
243
6.21M
                    assert(layout != DAV1D_PIXEL_LAYOUT_I400);
244
6.21M
                    const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
245
18.3M
                    for (int pl = 1; pl <= 2; pl++)
246
12.1M
                        dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1], lr_bak[bit][pl],
247
12.1M
                                             top_base[pl] + x_uv, bot_base[pl] + x_uv,
248
12.1M
                                             uv_pri_lvl, uv_sec_lvl, uvdir,
249
12.1M
                                             damping - 1, b_edges HIGHBD_CALL_SUFFIX);
250
6.21M
                }
251
252
7.73M
                bit ^= 1;
253
7.73M
            }
254
1.51M
        }
255
256
1.60M
        ptrs[0] += 8 * y_stride;
257
1.60M
        ptrs[1] += (8 >> ss_ver) * uv_stride;
258
1.60M
        ptrs[2] += (8 >> ss_ver) * uv_stride;
259
1.60M
        tc->top_pre_cdef_toggle ^= 1;
260
1.60M
    }
261
328k
}
dav1d_cdef_brow_8bpc
Line
Count
Source
111
149k
{
112
149k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
113
18.4E
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
114
149k
    const Dav1dDSPContext *const dsp = f->dsp;
115
149k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
116
149k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
117
149k
    const int sbsz = 16;
118
149k
    const int sb64w = f->sb128w << 1;
119
149k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
120
149k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
121
149k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
122
149k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
123
149k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
124
149k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
125
149k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
126
149k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
127
149k
    assert(f->lf.cdef_planes);
128
149k
    assert(BACKUP_2X8_Y == 1 && BACKUP_2X8_UV == 2);
129
149k
    const enum Backup2x8Flags frame_backup_flag = f->lf.cdef_planes;
130
149k
    const int have_tt = f->c->n_tc > 1;
131
149k
    const int sb128 = f->seq_hdr->sb128;
132
149k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
133
149k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
134
149k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
135
136
884k
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
137
735k
        const int tf = tc->top_pre_cdef_toggle;
138
735k
        const int by_idx = (by & 30) >> 1;
139
735k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
140
141
743k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
142
656k
            edges & CDEF_HAVE_BOTTOM)
143
656k
        {
144
            // backup pre-filter data for next iteration
145
656k
            pixel *const cdef_top_bak[3] = {
146
656k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
147
656k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
148
656k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
149
656k
            };
150
656k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout, frame_backup_flag);
151
656k
        }
152
153
735k
        const pixel *top_base[3], *bot_base[3];
154
735k
        pixel *const *const lpf = resize ? f->lf.cdef_lpf_line : f->lf.lr_lpf_line;
155
741k
        if (have_tt && sbrow_start && by == by_start) {
156
62.9k
            const int line = resize ? (sby - 1) * 4 : sby * (4 << sb128) - 4;
157
251k
            for (int pl = 0; pl < 3; pl++)
158
188k
                top_base[pl] = &lpf[pl][line * (pl ? uv_stride : y_stride)];
159
672k
        } else {
160
672k
            top_base[0] = &f->lf.cdef_line[tf][0][have_tt * sby * 4 * y_stride];
161
672k
            top_base[1] = &f->lf.cdef_line[tf][1][have_tt * sby * 8 * uv_stride];
162
672k
            top_base[2] = &f->lf.cdef_line[tf][2][have_tt * sby * 8 * uv_stride];
163
672k
        }
164
741k
        if (have_tt && !sbrow_start && by + 2 >= by_end) {
165
86.8k
            const int line = resize ? sby * 4 + 2 : sby * (4 << sb128) + 4 * sb128 + 2;
166
347k
            for (int pl = 0; pl < 3; pl++)
167
260k
                bot_base[pl] = &lpf[pl][line * (pl ? uv_stride : y_stride)];
168
648k
        } else {
169
648k
            bot_base[0] = ptrs[0] + 8 * y_stride;
170
648k
            bot_base[1] = ptrs[1] + (8 >> ss_ver) * uv_stride;
171
648k
            bot_base[2] = ptrs[2] + (8 >> ss_ver) * uv_stride;
172
648k
        }
173
174
735k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
175
735k
        enum Backup2x8Flags prev_flag = 0;
176
2.79M
        for (int sbx = 0; sbx < sb64w; sbx++) {
177
2.05M
            const int sb128x = sbx >> 1;
178
2.05M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
179
2.05M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
180
2.05M
            unsigned row_mask = lflvl[sb128x].noskip_mask[by_idx][sbx & 1];
181
2.05M
            const int y_lvl = cdef_idx == -1 ? 0 : f->frame_hdr->cdef.y_strength[cdef_idx];
182
2.05M
            const int uv_lvl = cdef_idx == -1 ? 0 : f->frame_hdr->cdef.uv_strength[cdef_idx];
183
2.05M
            if (!row_mask || !(y_lvl | uv_lvl)) {
184
1.37M
                prev_flag = 0;
185
1.37M
                continue;
186
1.37M
            }
187
188
684k
            const enum Backup2x8Flags flag = (!!y_lvl) | ((!!uv_lvl) << 1);
189
190
684k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
191
684k
            int y_sec_lvl = y_lvl & 3;
192
684k
            y_sec_lvl += y_sec_lvl == 3;
193
684k
            y_sec_lvl <<= bitdepth_min_8;
194
195
684k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
196
684k
            int uv_sec_lvl = uv_lvl & 3;
197
684k
            uv_sec_lvl += uv_sec_lvl == 3;
198
684k
            uv_sec_lvl <<= bitdepth_min_8;
199
200
684k
            const int bx_base = sbx * sbsz, bx_limit = imin(sbsz, f->bw - bx_base);
201
4.28M
            while (row_mask) {
202
3.82M
                const int bx_off = ctz(row_mask) & ~1;
203
3.82M
                row_mask &= ~(3U << bx_off);
204
3.82M
                if (bx_off >= bx_limit) break;
205
206
3.59M
                const int bx = bx_base + bx_off;
207
3.59M
                const int have_right = bx + 2 < f->bw;
208
3.59M
                const enum CdefEdgeFlags b_edges = edges |
209
3.59M
                    (bx > 0 ? CDEF_HAVE_LEFT : 0) | (have_right ? CDEF_HAVE_RIGHT : 0);
210
211
3.59M
                const ptrdiff_t x_y = (ptrdiff_t) bx * 4, x_uv = x_y >> ss_hor;
212
3.59M
                pixel *const bptrs[3] = { ptrs[0] + x_y, ptrs[1] + x_uv, ptrs[2] + x_uv };
213
214
3.59M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
215
3.59M
                if (do_left && bx > 0)
216
48.7k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
217
3.59M
                if (have_right &&
218
3.29M
                    (bx_off < 14 ? ((row_mask >> (bx_off + 2)) & 3)
219
3.29M
                                 : (lflvl[(sbx + 1) >> 1].noskip_mask[by_idx][(sbx + 1) & 1] & 3)))
220
3.25M
                {
221
3.25M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
222
3.25M
                    prev_flag = flag;
223
3.25M
                } else {
224
341k
                    prev_flag = 0;
225
341k
                }
226
227
3.59M
                int dir;
228
3.59M
                unsigned variance;
229
3.59M
                if (y_pri_lvl || uv_pri_lvl)
230
3.45M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
231
3.45M
                                        &variance HIGHBD_CALL_SUFFIX);
232
233
3.59M
                if (y_lvl) {
234
3.50M
                    const int adj_y_pri_lvl = y_pri_lvl ? adjust_strength(y_pri_lvl, variance) : 0;
235
3.50M
                    if (adj_y_pri_lvl || y_sec_lvl)
236
3.14M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
237
3.14M
                                        top_base[0] + x_y, bot_base[0] + x_y,
238
3.14M
                                        adj_y_pri_lvl, y_sec_lvl,
239
3.14M
                                        y_pri_lvl ? dir : 0, damping,
240
3.14M
                                        b_edges HIGHBD_CALL_SUFFIX);
241
3.50M
                }
242
3.59M
                if (uv_lvl) {
243
2.91M
                    assert(layout != DAV1D_PIXEL_LAYOUT_I400);
244
2.91M
                    const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
245
8.56M
                    for (int pl = 1; pl <= 2; pl++)
246
5.64M
                        dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1], lr_bak[bit][pl],
247
5.64M
                                             top_base[pl] + x_uv, bot_base[pl] + x_uv,
248
5.64M
                                             uv_pri_lvl, uv_sec_lvl, uvdir,
249
5.64M
                                             damping - 1, b_edges HIGHBD_CALL_SUFFIX);
250
2.91M
                }
251
252
3.59M
                bit ^= 1;
253
3.59M
            }
254
684k
        }
255
256
735k
        ptrs[0] += 8 * y_stride;
257
735k
        ptrs[1] += (8 >> ss_ver) * uv_stride;
258
735k
        ptrs[2] += (8 >> ss_ver) * uv_stride;
259
735k
        tc->top_pre_cdef_toggle ^= 1;
260
735k
    }
261
149k
}
dav1d_cdef_brow_16bpc
Line
Count
Source
111
178k
{
112
178k
    Dav1dFrameContext *const f = (Dav1dFrameContext *)tc->f;
113
178k
    const int bitdepth_min_8 = BITDEPTH == 8 ? 0 : f->cur.p.bpc - 8;
114
178k
    const Dav1dDSPContext *const dsp = f->dsp;
115
178k
    enum CdefEdgeFlags edges = CDEF_HAVE_BOTTOM | (by_start > 0 ? CDEF_HAVE_TOP : 0);
116
178k
    pixel *ptrs[3] = { p[0], p[1], p[2] };
117
178k
    const int sbsz = 16;
118
178k
    const int sb64w = f->sb128w << 1;
119
178k
    const int damping = f->frame_hdr->cdef.damping + bitdepth_min_8;
120
178k
    const enum Dav1dPixelLayout layout = f->cur.p.layout;
121
178k
    const int uv_idx = DAV1D_PIXEL_LAYOUT_I444 - layout;
122
178k
    const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
123
178k
    const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
124
178k
    static const uint8_t uv_dirs[2][8] = { { 0, 1, 2, 3, 4, 5, 6, 7 },
125
178k
                                           { 7, 0, 2, 4, 5, 6, 6, 6 } };
126
178k
    const uint8_t *uv_dir = uv_dirs[layout == DAV1D_PIXEL_LAYOUT_I422];
127
178k
    assert(f->lf.cdef_planes);
128
178k
    assert(BACKUP_2X8_Y == 1 && BACKUP_2X8_UV == 2);
129
178k
    const enum Backup2x8Flags frame_backup_flag = f->lf.cdef_planes;
130
178k
    const int have_tt = f->c->n_tc > 1;
131
178k
    const int sb128 = f->seq_hdr->sb128;
132
178k
    const int resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
133
178k
    const ptrdiff_t y_stride = PXSTRIDE(f->cur.stride[0]);
134
178k
    const ptrdiff_t uv_stride = PXSTRIDE(f->cur.stride[1]);
135
136
1.05M
    for (int bit = 0, by = by_start; by < by_end; by += 2, edges |= CDEF_HAVE_TOP) {
137
874k
        const int tf = tc->top_pre_cdef_toggle;
138
874k
        const int by_idx = (by & 30) >> 1;
139
874k
        if (by + 2 >= f->bh) edges &= ~CDEF_HAVE_BOTTOM;
140
141
888k
        if ((!have_tt || sbrow_start || by + 2 < by_end) &&
142
783k
            edges & CDEF_HAVE_BOTTOM)
143
783k
        {
144
            // backup pre-filter data for next iteration
145
783k
            pixel *const cdef_top_bak[3] = {
146
783k
                f->lf.cdef_line[!tf][0] + have_tt * sby * 4 * y_stride,
147
783k
                f->lf.cdef_line[!tf][1] + have_tt * sby * 8 * uv_stride,
148
783k
                f->lf.cdef_line[!tf][2] + have_tt * sby * 8 * uv_stride
149
783k
            };
150
783k
            backup2lines(cdef_top_bak, ptrs, f->cur.stride, layout, frame_backup_flag);
151
783k
        }
152
153
874k
        const pixel *top_base[3], *bot_base[3];
154
874k
        pixel *const *const lpf = resize ? f->lf.cdef_lpf_line : f->lf.lr_lpf_line;
155
887k
        if (have_tt && sbrow_start && by == by_start) {
156
74.0k
            const int line = resize ? (sby - 1) * 4 : sby * (4 << sb128) - 4;
157
296k
            for (int pl = 0; pl < 3; pl++)
158
222k
                top_base[pl] = &lpf[pl][line * (pl ? uv_stride : y_stride)];
159
800k
        } else {
160
800k
            top_base[0] = &f->lf.cdef_line[tf][0][have_tt * sby * 4 * y_stride];
161
800k
            top_base[1] = &f->lf.cdef_line[tf][1][have_tt * sby * 8 * uv_stride];
162
800k
            top_base[2] = &f->lf.cdef_line[tf][2][have_tt * sby * 8 * uv_stride];
163
800k
        }
164
887k
        if (have_tt && !sbrow_start && by + 2 >= by_end) {
165
104k
            const int line = resize ? sby * 4 + 2 : sby * (4 << sb128) + 4 * sb128 + 2;
166
419k
            for (int pl = 0; pl < 3; pl++)
167
314k
                bot_base[pl] = &lpf[pl][line * (pl ? uv_stride : y_stride)];
168
769k
        } else {
169
769k
            bot_base[0] = ptrs[0] + 8 * y_stride;
170
769k
            bot_base[1] = ptrs[1] + (8 >> ss_ver) * uv_stride;
171
769k
            bot_base[2] = ptrs[2] + (8 >> ss_ver) * uv_stride;
172
769k
        }
173
174
874k
        ALIGN_STK_16(pixel, lr_bak, 2 /* idx */, [3 /* plane */][8 /* y */][2 /* x */]);
175
874k
        enum Backup2x8Flags prev_flag = 0;
176
3.25M
        for (int sbx = 0; sbx < sb64w; sbx++) {
177
2.37M
            const int sb128x = sbx >> 1;
178
2.37M
            const int sb64_idx = ((by & sbsz) >> 3) + (sbx & 1);
179
2.37M
            const int cdef_idx = lflvl[sb128x].cdef_idx[sb64_idx];
180
2.37M
            unsigned row_mask = lflvl[sb128x].noskip_mask[by_idx][sbx & 1];
181
2.37M
            const int y_lvl = cdef_idx == -1 ? 0 : f->frame_hdr->cdef.y_strength[cdef_idx];
182
2.37M
            const int uv_lvl = cdef_idx == -1 ? 0 : f->frame_hdr->cdef.uv_strength[cdef_idx];
183
2.37M
            if (!row_mask || !(y_lvl | uv_lvl)) {
184
1.55M
                prev_flag = 0;
185
1.55M
                continue;
186
1.55M
            }
187
188
825k
            const enum Backup2x8Flags flag = (!!y_lvl) | ((!!uv_lvl) << 1);
189
190
825k
            const int y_pri_lvl = (y_lvl >> 2) << bitdepth_min_8;
191
825k
            int y_sec_lvl = y_lvl & 3;
192
825k
            y_sec_lvl += y_sec_lvl == 3;
193
825k
            y_sec_lvl <<= bitdepth_min_8;
194
195
825k
            const int uv_pri_lvl = (uv_lvl >> 2) << bitdepth_min_8;
196
825k
            int uv_sec_lvl = uv_lvl & 3;
197
825k
            uv_sec_lvl += uv_sec_lvl == 3;
198
825k
            uv_sec_lvl <<= bitdepth_min_8;
199
200
825k
            const int bx_base = sbx * sbsz, bx_limit = imin(sbsz, f->bw - bx_base);
201
4.96M
            while (row_mask) {
202
4.44M
                const int bx_off = ctz(row_mask) & ~1;
203
4.44M
                row_mask &= ~(3U << bx_off);
204
4.44M
                if (bx_off >= bx_limit) break;
205
206
4.13M
                const int bx = bx_base + bx_off;
207
4.13M
                const int have_right = bx + 2 < f->bw;
208
4.13M
                const enum CdefEdgeFlags b_edges = edges |
209
4.13M
                    (bx > 0 ? CDEF_HAVE_LEFT : 0) | (have_right ? CDEF_HAVE_RIGHT : 0);
210
211
4.13M
                const ptrdiff_t x_y = (ptrdiff_t) bx * 4, x_uv = x_y >> ss_hor;
212
4.13M
                pixel *const bptrs[3] = { ptrs[0] + x_y, ptrs[1] + x_uv, ptrs[2] + x_uv };
213
214
4.13M
                const enum Backup2x8Flags do_left = (prev_flag ^ flag) & flag;
215
4.13M
                if (do_left && bx > 0)
216
49.6k
                    backup2x8(lr_bak[bit], bptrs, f->cur.stride, 0, layout, do_left);
217
4.13M
                if (have_right &&
218
3.72M
                    (bx_off < 14 ? ((row_mask >> (bx_off + 2)) & 3)
219
3.72M
                                 : (lflvl[(sbx + 1) >> 1].noskip_mask[by_idx][(sbx + 1) & 1] & 3)))
220
3.68M
                {
221
3.68M
                    backup2x8(lr_bak[!bit], bptrs, f->cur.stride, 8, layout, flag);
222
3.68M
                    prev_flag = flag;
223
3.68M
                } else {
224
455k
                    prev_flag = 0;
225
455k
                }
226
227
4.13M
                int dir;
228
4.13M
                unsigned variance;
229
4.13M
                if (y_pri_lvl || uv_pri_lvl)
230
3.96M
                    dir = dsp->cdef.dir(bptrs[0], f->cur.stride[0],
231
3.96M
                                        &variance HIGHBD_CALL_SUFFIX);
232
233
4.13M
                if (y_lvl) {
234
3.85M
                    const int adj_y_pri_lvl = y_pri_lvl ? adjust_strength(y_pri_lvl, variance) : 0;
235
3.85M
                    if (adj_y_pri_lvl || y_sec_lvl)
236
3.48M
                        dsp->cdef.fb[0](bptrs[0], f->cur.stride[0], lr_bak[bit][0],
237
3.48M
                                        top_base[0] + x_y, bot_base[0] + x_y,
238
3.48M
                                        adj_y_pri_lvl, y_sec_lvl,
239
3.48M
                                        y_pri_lvl ? dir : 0, damping,
240
3.48M
                                        b_edges HIGHBD_CALL_SUFFIX);
241
3.85M
                }
242
4.13M
                if (uv_lvl) {
243
3.29M
                    assert(layout != DAV1D_PIXEL_LAYOUT_I400);
244
3.29M
                    const int uvdir = uv_pri_lvl ? uv_dir[dir] : 0;
245
9.77M
                    for (int pl = 1; pl <= 2; pl++)
246
6.48M
                        dsp->cdef.fb[uv_idx](bptrs[pl], f->cur.stride[1], lr_bak[bit][pl],
247
6.48M
                                             top_base[pl] + x_uv, bot_base[pl] + x_uv,
248
6.48M
                                             uv_pri_lvl, uv_sec_lvl, uvdir,
249
6.48M
                                             damping - 1, b_edges HIGHBD_CALL_SUFFIX);
250
3.29M
                }
251
252
4.13M
                bit ^= 1;
253
4.13M
            }
254
825k
        }
255
256
874k
        ptrs[0] += 8 * y_stride;
257
874k
        ptrs[1] += (8 >> ss_ver) * uv_stride;
258
874k
        ptrs[2] += (8 >> ss_ver) * uv_stride;
259
874k
        tc->top_pre_cdef_toggle ^= 1;
260
874k
    }
261
178k
}