Coverage Report

Created: 2026-12-01 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/decode.c
Line
Count
Source
1
/*
2
 * Copyright © 2018-2021, 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 <errno.h>
31
#include <limits.h>
32
#include <string.h>
33
#include <stdio.h>
34
#include <inttypes.h>
35
36
#include "dav1d/data.h"
37
38
#include "common/frame.h"
39
#include "common/intops.h"
40
41
#include "src/ctx.h"
42
#include "src/decode.h"
43
#include "src/dequant_tables.h"
44
#include "src/env.h"
45
#include "src/filmgrain.h"
46
#include "src/log.h"
47
#include "src/qm.h"
48
#include "src/recon.h"
49
#include "src/ref.h"
50
#include "src/tables.h"
51
#include "src/thread_task.h"
52
#include "src/warpmv.h"
53
54
static void init_quant_tables(const Dav1dSequenceHeader *const seq_hdr,
55
                              const Dav1dFrameHeader *const frame_hdr,
56
                              const int qidx, uint16_t (*dq)[3][2])
57
39.9k
{
58
191k
    for (int i = 0; i < (frame_hdr->segmentation.enabled ? 8 : 1); i++) {
59
151k
        const int yac = frame_hdr->segmentation.enabled ?
60
127k
            iclip_u8(qidx + frame_hdr->segmentation.seg_data.d[i].delta_q) : qidx;
61
151k
        const int ydc = iclip_u8(yac + frame_hdr->quant.ydc_delta);
62
151k
        const int uac = iclip_u8(yac + frame_hdr->quant.uac_delta);
63
151k
        const int udc = iclip_u8(yac + frame_hdr->quant.udc_delta);
64
151k
        const int vac = iclip_u8(yac + frame_hdr->quant.vac_delta);
65
151k
        const int vdc = iclip_u8(yac + frame_hdr->quant.vdc_delta);
66
67
151k
        dq[i][0][0] = dav1d_dq_tbl[seq_hdr->hbd][ydc][0];
68
151k
        dq[i][0][1] = dav1d_dq_tbl[seq_hdr->hbd][yac][1];
69
151k
        dq[i][1][0] = dav1d_dq_tbl[seq_hdr->hbd][udc][0];
70
151k
        dq[i][1][1] = dav1d_dq_tbl[seq_hdr->hbd][uac][1];
71
151k
        dq[i][2][0] = dav1d_dq_tbl[seq_hdr->hbd][vdc][0];
72
151k
        dq[i][2][1] = dav1d_dq_tbl[seq_hdr->hbd][vac][1];
73
151k
    }
74
39.9k
}
75
76
static int read_mv_component_diff(MsacContext *const msac,
77
                                  CdfMvComponent *const mv_comp,
78
                                  const int mv_prec)
79
1.26M
{
80
1.26M
    const int sign = dav1d_msac_decode_bool_adapt(msac, mv_comp->sign);
81
1.26M
    const int cl = dav1d_msac_decode_symbol_adapt16(msac, mv_comp->classes, 10);
82
1.26M
    int up, fp = 3, hp = 1;
83
84
1.26M
    if (!cl) {
85
278k
        up = dav1d_msac_decode_bool_adapt(msac, mv_comp->class0);
86
278k
        if (mv_prec >= 0) {  // !force_integer_mv
87
34.3k
            fp = dav1d_msac_decode_symbol_adapt4(msac, mv_comp->class0_fp[up], 3);
88
34.3k
            if (mv_prec > 0) // allow_high_precision_mv
89
17.4k
                hp = dav1d_msac_decode_bool_adapt(msac, mv_comp->class0_hp);
90
34.3k
        }
91
988k
    } else {
92
988k
        up = 1 << cl;
93
10.3M
        for (int n = 0; n < cl; n++)
94
9.39M
            up |= dav1d_msac_decode_bool_adapt(msac, mv_comp->classN[n]) << n;
95
988k
        if (mv_prec >= 0) {  // !force_integer_mv
96
11.8k
            fp = dav1d_msac_decode_symbol_adapt4(msac, mv_comp->classN_fp, 3);
97
11.8k
            if (mv_prec > 0) // allow_high_precision_mv
98
5.00k
                hp = dav1d_msac_decode_bool_adapt(msac, mv_comp->classN_hp);
99
11.8k
        }
100
988k
    }
101
102
1.26M
    const int diff = ((up << 3) | (fp << 1) | hp) + 1;
103
1.26M
    return apply_sign_bool(diff, sign);
104
1.26M
}
105
106
static void read_mv_residual(Dav1dTileState *const ts, mv *const ref_mv,
107
                             const int mv_prec)
108
714k
{
109
714k
    MsacContext *const msac = &ts->msac;
110
714k
    const enum MVJoint mv_joint =
111
714k
        dav1d_msac_decode_symbol_adapt4(msac, ts->cdf.mv.joint, N_MV_JOINTS - 1);
112
714k
    if (mv_joint & MV_JOINT_V)
113
635k
        ref_mv->y += read_mv_component_diff(msac, &ts->cdf.mv.comp[0], mv_prec);
114
714k
    if (mv_joint & MV_JOINT_H)
115
633k
        ref_mv->x += read_mv_component_diff(msac, &ts->cdf.mv.comp[1], mv_prec);
116
714k
}
117
118
static void read_tx_tree(Dav1dTaskContext *const t,
119
                         const enum RectTxfmSize from,
120
                         const int depth, uint16_t *const masks,
121
                         const int x_off, const int y_off)
122
156k
{
123
156k
    const Dav1dFrameContext *const f = t->f;
124
156k
    const int bx4 = t->bx & 31, by4 = t->by & 31;
125
156k
    const TxfmInfo *const t_dim = &dav1d_txfm_dimensions[from];
126
156k
    const int txw = t_dim->lw, txh = t_dim->lh;
127
156k
    int is_split;
128
129
156k
    if (depth < 2 && from > (int) TX_4X4) {
130
136k
        const int cat = 2 * (TX_64X64 - t_dim->max) - depth;
131
136k
        const int a = t->a->tx[bx4] < txw;
132
136k
        const int l = t->l.tx[by4] < txh;
133
134
136k
        is_split = dav1d_msac_decode_bool_adapt(&t->ts->msac,
135
136k
                       t->ts->cdf.m.txpart[cat][a + l]);
136
136k
        if (is_split)
137
38.1k
            masks[depth] |= 1 << (y_off * 4 + x_off);
138
136k
    } else {
139
19.9k
        is_split = 0;
140
19.9k
    }
141
142
156k
    if (is_split && t_dim->max > TX_8X8) {
143
26.9k
        const enum RectTxfmSize sub = t_dim->sub;
144
26.9k
        const TxfmInfo *const sub_t_dim = &dav1d_txfm_dimensions[sub];
145
26.9k
        const int txsw = sub_t_dim->w, txsh = sub_t_dim->h;
146
147
26.9k
        read_tx_tree(t, sub, depth + 1, masks, x_off * 2 + 0, y_off * 2 + 0);
148
26.9k
        t->bx += txsw;
149
26.9k
        if (txw >= txh && t->bx < f->bw)
150
20.0k
            read_tx_tree(t, sub, depth + 1, masks, x_off * 2 + 1, y_off * 2 + 0);
151
26.9k
        t->bx -= txsw;
152
26.9k
        t->by += txsh;
153
26.9k
        if (txh >= txw && t->by < f->bh) {
154
14.1k
            read_tx_tree(t, sub, depth + 1, masks, x_off * 2 + 0, y_off * 2 + 1);
155
14.1k
            t->bx += txsw;
156
14.1k
            if (txw >= txh && t->bx < f->bw)
157
7.40k
                read_tx_tree(t, sub, depth + 1, masks,
158
7.40k
                             x_off * 2 + 1, y_off * 2 + 1);
159
14.1k
            t->bx -= txsw;
160
14.1k
        }
161
26.9k
        t->by -= txsh;
162
129k
    } else {
163
129k
        dav1d_memset_pow2[t_dim->lw](&t->a->tx[bx4], is_split ? TX_4X4 : txw);
164
129k
        dav1d_memset_pow2[t_dim->lh](&t->l.tx[by4], is_split ? TX_4X4 : txh);
165
129k
    }
166
156k
}
167
168
792k
static int neg_deinterleave(int diff, int ref, int max) {
169
792k
    if (!ref) return diff;
170
482k
    if (ref >= (max - 1)) return max - diff - 1;
171
422k
    if (2 * ref < max) {
172
252k
        if (diff <= 2 * ref) {
173
212k
            if (diff & 1)
174
31.4k
                return ref + ((diff + 1) >> 1);
175
180k
            else
176
180k
                return ref - (diff >> 1);
177
212k
        }
178
40.4k
        return diff;
179
252k
    } else {
180
169k
        if (diff <= 2 * (max - ref - 1)) {
181
145k
            if (diff & 1)
182
22.4k
                return ref + ((diff + 1) >> 1);
183
122k
            else
184
122k
                return ref - (diff >> 1);
185
145k
        }
186
24.3k
        return max - (diff + 1);
187
169k
    }
188
422k
}
189
190
static void find_matching_ref(const Dav1dTaskContext *const t,
191
                              const enum EdgeFlags intra_edge_flags,
192
                              const int bw4, const int bh4,
193
                              const int w4, const int h4,
194
                              const int have_left, const int have_top,
195
                              const int ref, uint64_t masks[2])
196
63.2k
{
197
63.2k
    /*const*/ refmvs_block *const *r = &t->rt.r[(t->by & 31) + 5];
198
63.2k
    int count = 0;
199
63.2k
    int have_topleft = have_top && have_left;
200
63.2k
    int have_topright = imax(bw4, bh4) < 32 &&
201
62.2k
                        have_top && t->bx + bw4 < t->ts->tiling.col_end &&
202
30.5k
                        (intra_edge_flags & EDGE_I444_TOP_HAS_RIGHT);
203
204
140k
#define bs(rp) dav1d_block_dimensions[(rp)->bs]
205
141k
#define matches(rp) ((rp)->ref.ref[0] == ref + 1 && (rp)->ref.ref[1] == -1)
206
207
63.2k
    if (have_top) {
208
36.9k
        const refmvs_block *r2 = &r[-1][t->bx];
209
36.9k
        if (matches(r2)) {
210
27.2k
            masks[0] |= 1;
211
27.2k
            count = 1;
212
27.2k
        }
213
36.9k
        int aw4 = bs(r2)[0];
214
36.9k
        if (aw4 >= bw4) {
215
32.0k
            const int off = t->bx & (aw4 - 1);
216
32.0k
            if (off) have_topleft = 0;
217
32.0k
            if (aw4 - off > bw4) have_topright = 0;
218
32.0k
        } else {
219
4.89k
            unsigned mask = 1 << aw4;
220
11.0k
            for (int x = aw4; x < w4; x += aw4) {
221
6.19k
                r2 += aw4;
222
6.19k
                if (matches(r2)) {
223
4.18k
                    masks[0] |= mask;
224
4.18k
                    if (++count >= 8) return;
225
4.18k
                }
226
6.18k
                aw4 = bs(r2)[0];
227
6.18k
                mask <<= aw4;
228
6.18k
            }
229
4.89k
        }
230
36.9k
    }
231
63.2k
    if (have_left) {
232
57.0k
        /*const*/ refmvs_block *const *r2 = r;
233
57.0k
        if (matches(&r2[0][t->bx - 1])) {
234
42.5k
            masks[1] |= 1;
235
42.5k
            if (++count >= 8) return;
236
42.5k
        }
237
57.0k
        int lh4 = bs(&r2[0][t->bx - 1])[1];
238
57.0k
        if (lh4 >= bh4) {
239
48.8k
            if (t->by & (lh4 - 1)) have_topleft = 0;
240
48.8k
        } else {
241
8.22k
            unsigned mask = 1 << lh4;
242
18.8k
            for (int y = lh4; y < h4; y += lh4) {
243
10.6k
                r2 += lh4;
244
10.6k
                if (matches(&r2[0][t->bx - 1])) {
245
6.31k
                    masks[1] |= mask;
246
6.31k
                    if (++count >= 8) return;
247
6.31k
                }
248
10.6k
                lh4 = bs(&r2[0][t->bx - 1])[1];
249
10.6k
                mask <<= lh4;
250
10.6k
            }
251
8.22k
        }
252
57.0k
    }
253
63.2k
    if (have_topleft && matches(&r[-1][t->bx - 1])) {
254
12.7k
        masks[1] |= 1ULL << 32;
255
12.7k
        if (++count >= 8) return;
256
12.7k
    }
257
63.1k
    if (have_topright && matches(&r[-1][t->bx + bw4])) {
258
7.36k
        masks[0] |= 1ULL << 32;
259
7.36k
    }
260
63.1k
#undef matches
261
63.1k
}
262
263
static void derive_warpmv(const Dav1dTaskContext *const t,
264
                          const int bw4, const int bh4,
265
                          const uint64_t masks[2], const union mv mv,
266
                          Dav1dWarpedMotionParams *const wmp)
267
7.58k
{
268
7.58k
    int pts[8][2 /* in, out */][2 /* x, y */], np = 0;
269
7.58k
    /*const*/ refmvs_block *const *r = &t->rt.r[(t->by & 31) + 5];
270
271
11.7k
#define add_sample(dx, dy, sx, sy, rp) do { \
272
11.7k
    pts[np][0][0] = 16 * (2 * dx + sx * bs(rp)[0]) - 8; \
273
11.7k
    pts[np][0][1] = 16 * (2 * dy + sy * bs(rp)[1]) - 8; \
274
11.7k
    pts[np][1][0] = pts[np][0][0] + (rp)->mv.mv[0].x; \
275
11.7k
    pts[np][1][1] = pts[np][0][1] + (rp)->mv.mv[0].y; \
276
11.7k
    np++; \
277
11.7k
} while (0)
278
279
    // use masks[] to find the projectable motion vectors in the edges
280
7.58k
    if ((unsigned) masks[0] == 1 && !(masks[1] >> 32)) {
281
981
        const int off = t->bx & (bs(&r[-1][t->bx])[0] - 1);
282
981
        add_sample(-off, 0, 1, -1, &r[-1][t->bx]);
283
7.99k
    } else for (unsigned off = 0, xmask = (uint32_t) masks[0]; np < 8 && xmask;) { // top
284
1.38k
        const int tz = ctz(xmask);
285
1.38k
        off += tz;
286
1.38k
        xmask >>= tz;
287
1.38k
        add_sample(off, 0, 1, -1, &r[-1][t->bx + off]);
288
1.38k
        xmask &= ~1;
289
1.38k
    }
290
7.58k
    if (np < 8 && masks[1] == 1) {
291
5.66k
        const int off = t->by & (bs(&r[0][t->bx - 1])[1] - 1);
292
5.66k
        add_sample(0, -off, -1, 1, &r[-off][t->bx - 1]);
293
5.66k
    } else for (unsigned off = 0, ymask = (uint32_t) masks[1]; np < 8 && ymask;) { // left
294
1.75k
        const int tz = ctz(ymask);
295
1.75k
        off += tz;
296
1.75k
        ymask >>= tz;
297
1.75k
        add_sample(0, off, -1, 1, &r[off][t->bx - 1]);
298
1.75k
        ymask &= ~1;
299
1.75k
    }
300
7.58k
    if (np < 8 && masks[1] >> 32) // top/left
301
1.10k
        add_sample(0, 0, -1, -1, &r[-1][t->bx - 1]);
302
7.58k
    if (np < 8 && masks[0] >> 32) // top/right
303
807
        add_sample(bw4, 0, 1, -1, &r[-1][t->bx + bw4]);
304
7.58k
    assert(np > 0 && np <= 8);
305
7.58k
#undef bs
306
307
    // select according to motion vector difference against a threshold
308
7.58k
    int mvd[8], ret = 0;
309
7.58k
    const int thresh = 4 * iclip(imax(bw4, bh4), 4, 28);
310
19.2k
    for (int i = 0; i < np; i++) {
311
11.7k
        mvd[i] = abs(pts[i][1][0] - pts[i][0][0] - mv.x) +
312
11.7k
                 abs(pts[i][1][1] - pts[i][0][1] - mv.y);
313
11.7k
        if (mvd[i] > thresh)
314
4.53k
            mvd[i] = -1;
315
7.16k
        else
316
7.16k
            ret++;
317
11.7k
    }
318
7.58k
    if (!ret) {
319
2.95k
        ret = 1;
320
5.13k
    } else for (int i = 0, j = np - 1, k = 0; k < np - ret; k++, i++, j--) {
321
1.46k
        while (mvd[i] != -1) i++;
322
1.58k
        while (mvd[j] == -1) j--;
323
927
        assert(i != j);
324
927
        if (i > j) break;
325
        // replace the discarded samples;
326
503
        mvd[i] = mvd[j];
327
503
        memcpy(pts[i], pts[j], sizeof(*pts));
328
503
    }
329
330
7.58k
    if (!dav1d_find_affine_int(pts, ret, bw4, bh4, mv, wmp, t->bx, t->by) &&
331
6.65k
        !dav1d_get_shear_params(wmp))
332
5.55k
    {
333
5.55k
        wmp->type = DAV1D_WM_TYPE_AFFINE;
334
5.55k
    } else
335
2.03k
        wmp->type = DAV1D_WM_TYPE_IDENTITY;
336
7.58k
}
337
338
65.7k
static inline int findoddzero(const uint8_t *buf, int len) {
339
71.7k
    for (int n = 0; n < len; n++)
340
69.3k
        if (!buf[n * 2]) return 1;
341
2.45k
    return 0;
342
65.7k
}
343
344
// Resolve a decoded rank (color_idx) to a palette entry, given the ordered
345
// priority candidates p[0..prio_count-1] and the bitmask of values already
346
// used by those candidates. Ranks below prio_count select a priority
347
// candidate directly; higher ranks select the (rank - prio_count)th value
348
// (counting from 0) that is not present in mask, in ascending order.
349
static inline int resolve_pal_color(const int color_idx, const uint8_t *const p,
350
                                    const int prio_count, unsigned mask)
351
2.35M
{
352
2.35M
    if (color_idx < prio_count)
353
1.95M
        return p[color_idx];
354
403k
    unsigned rank = color_idx - prio_count + (mask & 1);
355
403k
    rank += ((mask >> 1) & 1) & (rank > 0);
356
    // Unset-bit positions minus their ranks, packed in 2-bit fields.
357
    // Mask bits 0 and 1 are cleared and compensated in rank.
358
    // At most three priority colors keep each offset within 0..3.
359
403k
    static const uint8_t free_offsets2[] = {
360
403k
        0x00, 0x00, 0x50, 0x15, 0x40, 0x15, 0xa0, 0x0a, 0x00, 0x15, 0x90, 0x0a,
361
403k
        0x80, 0x0a, 0xf0, 0x03, 0x00, 0x14, 0x50, 0x0a, 0x40, 0x0a, 0xe0, 0x03,
362
403k
        0x00, 0x0a, 0xd0, 0x03, 0xc0, 0x03, 0x00, 0x00, 0x00, 0x10, 0x50, 0x09,
363
403k
        0x40, 0x09, 0xa0, 0x03, 0x00, 0x09, 0x90, 0x03, 0x80, 0x03, 0x00, 0x00,
364
403k
        0x00, 0x08, 0x50, 0x03, 0x40, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
365
403k
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x05, 0x40, 0x05, 0xa0, 0x02,
366
403k
        0x00, 0x05, 0x90, 0x02, 0x80, 0x02, 0x00, 0x00, 0x00, 0x04, 0x50, 0x02,
367
403k
        0x40, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
368
403k
        0x00, 0x00, 0x50, 0x01, 0x40, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
369
403k
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
370
403k
    };
371
403k
    const unsigned byte_idx = 2 * (mask >> 2) + (rank >> 2);
372
403k
    assert(byte_idx < sizeof(free_offsets2));
373
403k
    return rank + ((free_offsets2[byte_idx] >> (2 * (rank & 3))) & 3);
374
403k
}
375
376
static void read_pal_indices(Dav1dTaskContext *const t,
377
                             uint8_t *const pal_idx,
378
                             const int pal_sz, const int pl,
379
                             const int w4, const int h4,
380
                             const int bw4, const int bh4)
381
18.4k
{
382
18.4k
    Dav1dTileState *const ts = t->ts;
383
18.4k
    const ptrdiff_t stride = bw4 * 4;
384
18.4k
    assert(pal_idx);
385
18.4k
    uint8_t *const pal_tmp = t->scratch.pal_idx_uv;
386
18.4k
    pal_tmp[0] = dav1d_msac_decode_uniform(&ts->msac, pal_sz);
387
18.4k
    uint16_t (*const color_map_cdf)[8] =
388
18.4k
        ts->cdf.m.color_map[pl][pal_sz - 2];
389
390
18.4k
    if (pal_sz == 2) {
391
        // Binary palette: ctx 1 is unreachable (needs 3 distinct neighbours),
392
        // and resolve always toggles the single priority value. Use a bool
393
        // decode instead of a symbol decode. ctx formula collapses to:
394
        // border -> 0, interior -> 2 + same_t_l + (same_t_l & same_t_tl)
395
119k
        for (int i = 1; i < 4 * (w4 + h4) - 1; i++) {
396
115k
            const int first = imin(i, w4 * 4 - 1);
397
115k
            const int last = imax(0, i - h4 * 4 + 1);
398
115k
            int have_top = i > first;
399
115k
            const uint8_t *pal_row = pal_tmp + first + (i - first) * stride;
400
1.05M
            for (int j = first; j >= last; have_top = 1, j--, pal_row += stride - 1) {
401
941k
                const int have_left = j > 0;
402
941k
                int ctx, p0;
403
941k
                if (!have_left) {
404
58.9k
                    ctx = 0;
405
58.9k
                    p0 = pal_row[-stride];
406
882k
                } else if (!have_top) {
407
56.2k
                    ctx = 0;
408
56.2k
                    p0 = pal_row[-1];
409
826k
                } else {
410
826k
                    const int l = pal_row[-1], tp = pal_row[-stride];
411
826k
                    const int tl = pal_row[-(stride + 1)];
412
826k
                    const int same_t_l = tp == l;
413
826k
                    const int same_t_tl = tp == tl;
414
826k
                    ctx = 2 + same_t_l + (same_t_l & same_t_tl);
415
826k
                    p0 = same_t_l ? tp : tl;
416
826k
                }
417
941k
                const int bit = dav1d_msac_decode_bool_adapt(
418
941k
                    &ts->msac, color_map_cdf[ctx]);
419
941k
                pal_tmp[(i - j) * stride + j] = bit ^ p0;
420
941k
            }
421
115k
        }
422
4.24k
        t->c->pal_dsp.pal_idx_finish(pal_idx, pal_tmp, bw4 * 4, bh4 * 4,
423
4.24k
                                     w4 * 4, h4 * 4);
424
4.24k
        return;
425
4.24k
    }
426
427
14.2k
    unsigned (*const symbol_adapt)(MsacContext *, uint16_t *, size_t) =
428
14.2k
        pal_sz <= 4 ? dav1d_msac_decode_symbol_adapt4 : dav1d_msac_decode_symbol_adapt8;
429
430
359k
    for (int i = 1; i < 4 * (w4 + h4) - 1; i++) {
431
        // top/left-to-bottom/right diagonals ("wave-front")
432
345k
        const int first = imin(i, w4 * 4 - 1);
433
345k
        const int last = imax(0, i - h4 * 4 + 1);
434
345k
        int have_top = i > first;
435
345k
        const uint8_t *pal_row = pal_tmp + first + (i - first) * stride;
436
2.70M
        for (int j = first; j >= last; have_top = 1, j--, pal_row += stride - 1) {
437
2.35M
            const int have_left = j > 0;
438
2.35M
            uint8_t p[3];
439
2.35M
            unsigned mask;
440
2.35M
            int ctx, prio_count;
441
2.35M
            if (!have_left) {
442
166k
                ctx = 0; prio_count = 1;
443
166k
                p[0] = pal_row[-stride];
444
166k
                mask = 1u << p[0];
445
2.19M
            } else if (!have_top) {
446
178k
                ctx = 0; prio_count = 1;
447
178k
                p[0] = pal_row[-1];
448
178k
                mask = 1u << p[0];
449
2.01M
            } else {
450
2.01M
                const int l = pal_row[-1], tp = pal_row[-stride];
451
2.01M
                const int tl = pal_row[-(stride + 1)];
452
2.01M
                const int same_t_l = tp == l;
453
2.01M
                const int same_t_tl = tp == tl;
454
2.01M
                const int same_l_tl = l == tl;
455
2.01M
                if (same_t_l & same_t_tl) {
456
894k
                    ctx = 4; prio_count = 1;
457
894k
                    p[0] = tp; mask = 1u << tp;
458
1.11M
                } else if (same_t_l) {
459
74.7k
                    ctx = 3; prio_count = 2;
460
74.7k
                    p[0] = tp; p[1] = tl;
461
74.7k
                    mask = (1u << tp) | (1u << tl);
462
1.04M
                } else if (same_t_tl | same_l_tl) {
463
818k
                    ctx = 2; prio_count = 2;
464
818k
                    p[0] = tl; p[1] = same_t_tl ? l : tp;
465
818k
                    mask = (1u << tl) | (1u << p[1]);
466
818k
                } else {
467
224k
                    ctx = 1; prio_count = 3;
468
224k
                    p[0] = imin(tp, l); p[1] = imax(tp, l); p[2] = tl;
469
224k
                    mask = (1u << tp) | (1u << l) | (1u << tl);
470
224k
                }
471
2.01M
            }
472
2.35M
            const int color_idx = symbol_adapt(&ts->msac, color_map_cdf[ctx], pal_sz - 1);
473
2.35M
            pal_tmp[(i - j) * stride + j] =
474
2.35M
                resolve_pal_color(color_idx, p, prio_count, mask);
475
2.35M
        }
476
345k
    }
477
478
14.2k
    t->c->pal_dsp.pal_idx_finish(pal_idx, pal_tmp, bw4 * 4, bh4 * 4,
479
14.2k
                                 w4 * 4, h4 * 4);
480
14.2k
}
481
482
static void read_vartx_tree(Dav1dTaskContext *const t,
483
                            Av1Block *const b, const enum BlockSize bs,
484
                            const int bx4, const int by4)
485
867k
{
486
867k
    const Dav1dFrameContext *const f = t->f;
487
867k
    const uint8_t *const b_dim = dav1d_block_dimensions[bs];
488
867k
    const int bw4 = b_dim[0], bh4 = b_dim[1];
489
490
    // var-tx tree coding
491
867k
    uint16_t tx_split[2] = { 0 };
492
867k
    b->max_ytx = dav1d_max_txfm_size_for_bs[bs][0];
493
867k
    if (!b->skip && (f->frame_hdr->segmentation.lossless[b->seg_id] ||
494
244k
                     b->max_ytx == TX_4X4))
495
26.4k
    {
496
26.4k
        b->max_ytx = b->uvtx = TX_4X4;
497
26.4k
        if (f->frame_hdr->txfm_mode == DAV1D_TX_SWITCHABLE) {
498
8.90k
            dav1d_memset_pow2[b_dim[2]](&t->a->tx[bx4], TX_4X4);
499
8.90k
            dav1d_memset_pow2[b_dim[3]](&t->l.tx[by4], TX_4X4);
500
8.90k
        }
501
840k
    } else if (f->frame_hdr->txfm_mode != DAV1D_TX_SWITCHABLE || b->skip) {
502
754k
        if (f->frame_hdr->txfm_mode == DAV1D_TX_SWITCHABLE) {
503
245k
            dav1d_memset_pow2[b_dim[2]](&t->a->tx[bx4], b_dim[2 + 0]);
504
245k
            dav1d_memset_pow2[b_dim[3]](&t->l.tx[by4], b_dim[2 + 1]);
505
245k
        }
506
754k
        b->uvtx = dav1d_max_txfm_size_for_bs[bs][f->cur.p.layout];
507
754k
    } else {
508
86.2k
        assert(bw4 <= 16 || bh4 <= 16 || b->max_ytx == TX_64X64);
509
86.2k
        int y, x, y_off, x_off;
510
86.2k
        const TxfmInfo *const ytx = &dav1d_txfm_dimensions[b->max_ytx];
511
172k
        for (y = 0, y_off = 0; y < bh4; y += ytx->h, y_off++) {
512
174k
            for (x = 0, x_off = 0; x < bw4; x += ytx->w, x_off++) {
513
87.5k
                read_tx_tree(t, b->max_ytx, 0, tx_split, x_off, y_off);
514
                // contexts are updated inside read_tx_tree()
515
87.5k
                t->bx += ytx->w;
516
87.5k
            }
517
86.5k
            t->bx -= x;
518
86.5k
            t->by += ytx->h;
519
86.5k
        }
520
86.2k
        t->by -= y;
521
86.2k
        if (DEBUG_BLOCK_INFO)
522
0
            printf("Post-vartxtree[%x/%x]: r=%d\n",
523
0
                   tx_split[0], tx_split[1], t->ts->msac.rng);
524
86.2k
        b->uvtx = dav1d_max_txfm_size_for_bs[bs][f->cur.p.layout];
525
86.2k
    }
526
867k
    assert(!(tx_split[0] & ~0x33));
527
867k
    b->tx_split0 = (uint8_t)tx_split[0];
528
867k
    b->tx_split1 = tx_split[1];
529
867k
}
530
531
static inline unsigned get_prev_frame_segid(const Dav1dFrameContext *const f,
532
                                            const int by, const int bx,
533
                                            const int w4, int h4,
534
                                            const uint8_t *ref_seg_map,
535
                                            const ptrdiff_t stride)
536
37.7k
{
537
37.7k
    assert(f->frame_hdr->primary_ref_frame != DAV1D_PRIMARY_REF_NONE);
538
539
37.7k
    unsigned seg_id = 8;
540
37.7k
    ref_seg_map += by * stride + bx;
541
45.1k
    do {
542
213k
        for (int x = 0; x < w4; x++)
543
168k
            seg_id = imin(seg_id, ref_seg_map[x]);
544
45.1k
        ref_seg_map += stride;
545
45.1k
    } while (--h4 > 0 && seg_id);
546
37.7k
    assert(seg_id < 8);
547
548
37.7k
    return seg_id;
549
37.7k
}
550
551
static inline void splat_oneref_mv(const Dav1dContext *const c,
552
                                   Dav1dTaskContext *const t,
553
                                   const enum BlockSize bs,
554
                                   const Av1Block *const b,
555
                                   const int bw4, const int bh4)
556
185k
{
557
185k
    const enum InterPredMode mode = b->inter_mode;
558
185k
    const refmvs_block ALIGN(tmpl, 16) = (refmvs_block) {
559
185k
        .ref.ref = { b->ref[0] + 1, b->interintra_type ? 0 : -1 },
560
185k
        .mv.mv[0] = b->mv[0],
561
185k
        .bs = bs,
562
185k
        .mf = (mode == GLOBALMV && imin(bw4, bh4) >= 2) | ((mode == NEWMV) * 2),
563
185k
    };
564
185k
    c->refmvs_dsp.splat_mv(&t->rt.r[(t->by & 31) + 5], &tmpl, t->bx, bw4, bh4);
565
185k
}
566
567
static inline void splat_intrabc_mv(const Dav1dContext *const c,
568
                                    Dav1dTaskContext *const t,
569
                                    const enum BlockSize bs,
570
                                    const Av1Block *const b,
571
                                    const int bw4, const int bh4)
572
646k
{
573
646k
    const refmvs_block ALIGN(tmpl, 16) = (refmvs_block) {
574
646k
        .ref.ref = { 0, -1 },
575
646k
        .mv.mv[0] = b->mv[0],
576
646k
        .bs = bs,
577
646k
        .mf = 0,
578
646k
    };
579
646k
    c->refmvs_dsp.splat_mv(&t->rt.r[(t->by & 31) + 5], &tmpl, t->bx, bw4, bh4);
580
646k
}
581
582
static inline void splat_tworef_mv(const Dav1dContext *const c,
583
                                   Dav1dTaskContext *const t,
584
                                   const enum BlockSize bs,
585
                                   const Av1Block *const b,
586
                                   const int bw4, const int bh4)
587
35.6k
{
588
35.6k
    assert(bw4 >= 2 && bh4 >= 2);
589
35.6k
    const enum CompInterPredMode mode = b->inter_mode;
590
35.6k
    const refmvs_block ALIGN(tmpl, 16) = (refmvs_block) {
591
35.6k
        .ref.ref = { b->ref[0] + 1, b->ref[1] + 1 },
592
35.6k
        .mv.mv = { b->mv[0], b->mv[1] },
593
35.6k
        .bs = bs,
594
35.6k
        .mf = (mode == GLOBALMV_GLOBALMV) | !!((1 << mode) & (0xbc)) * 2,
595
35.6k
    };
596
35.6k
    c->refmvs_dsp.splat_mv(&t->rt.r[(t->by & 31) + 5], &tmpl, t->bx, bw4, bh4);
597
35.6k
}
598
599
static inline void splat_intraref(const Dav1dContext *const c,
600
                                  Dav1dTaskContext *const t,
601
                                  const enum BlockSize bs,
602
                                  const int bw4, const int bh4)
603
367k
{
604
367k
    const refmvs_block ALIGN(tmpl, 16) = (refmvs_block) {
605
367k
        .ref.ref = { 0, -1 },
606
367k
        .mv.mv[0].n = INVALID_MV,
607
367k
        .bs = bs,
608
367k
        .mf = 0,
609
367k
    };
610
367k
    c->refmvs_dsp.splat_mv(&t->rt.r[(t->by & 31) + 5], &tmpl, t->bx, bw4, bh4);
611
367k
}
612
613
static void mc_lowest_px(int *const dst, const int by4, const int bh4,
614
                         const int mvy, const int ss_ver,
615
                         const struct ScalableMotionParams *const smp)
616
425k
{
617
425k
    const int v_mul = 4 >> ss_ver;
618
425k
    if (!smp->scale) {
619
349k
        const int my = mvy >> (3 + ss_ver), dy = mvy & (15 >> !ss_ver);
620
349k
        *dst = imax(*dst, (by4 + bh4) * v_mul + my + 4 * !!dy);
621
349k
    } else {
622
76.1k
        int y = (by4 * v_mul << 4) + mvy * (1 << !ss_ver);
623
76.1k
        const int64_t tmp = (int64_t)(y) * smp->scale + (smp->scale - 0x4000) * 8;
624
76.1k
        y = apply_sign64((int)((llabs(tmp) + 128) >> 8), tmp) + 32;
625
76.1k
        const int bottom = ((y + (bh4 * v_mul - 1) * smp->step) >> 10) + 1 + 4;
626
76.1k
        *dst = imax(*dst, bottom);
627
76.1k
    }
628
425k
}
629
630
static ALWAYS_INLINE void affine_lowest_px(Dav1dTaskContext *const t, int *const dst,
631
                                           const uint8_t *const b_dim,
632
                                           const Dav1dWarpedMotionParams *const wmp,
633
                                           const int ss_ver, const int ss_hor)
634
16.0k
{
635
16.0k
    const int h_mul = 4 >> ss_hor, v_mul = 4 >> ss_ver;
636
16.0k
    assert(!((b_dim[0] * h_mul) & 7) && !((b_dim[1] * v_mul) & 7));
637
16.0k
    const int32_t *const mat = wmp->matrix;
638
16.0k
    const int y = b_dim[1] * v_mul - 8; // lowest y
639
640
16.0k
    const int src_y = t->by * 4 + ((y + 4) << ss_ver);
641
16.0k
    const int64_t mat5_y = (int64_t) mat[5] * src_y + mat[1];
642
    // check left- and right-most blocks
643
41.5k
    for (int x = 0; x < b_dim[0] * h_mul; x += imax(8, b_dim[0] * h_mul - 8)) {
644
        // calculate transformation relative to center of 8x8 block in
645
        // luma pixel units
646
25.4k
        const int src_x = t->bx * 4 + ((x + 4) << ss_hor);
647
25.4k
        const int64_t mvy = ((int64_t) mat[4] * src_x + mat5_y) >> ss_ver;
648
25.4k
        const int dy = (int) (mvy >> 16) - 4;
649
25.4k
        *dst = imax(*dst, dy + 4 + 8);
650
25.4k
    }
651
16.0k
}
652
653
static NOINLINE void affine_lowest_px_luma(Dav1dTaskContext *const t, int *const dst,
654
                                           const uint8_t *const b_dim,
655
                                           const Dav1dWarpedMotionParams *const wmp)
656
12.7k
{
657
12.7k
    affine_lowest_px(t, dst, b_dim, wmp, 0, 0);
658
12.7k
}
659
660
static NOINLINE void affine_lowest_px_chroma(Dav1dTaskContext *const t, int *const dst,
661
                                             const uint8_t *const b_dim,
662
                                             const Dav1dWarpedMotionParams *const wmp)
663
3.98k
{
664
3.98k
    const Dav1dFrameContext *const f = t->f;
665
3.98k
    assert(f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I400);
666
3.98k
    if (f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I444)
667
623
        affine_lowest_px_luma(t, dst, b_dim, wmp);
668
3.36k
    else
669
3.36k
        affine_lowest_px(t, dst, b_dim, wmp, f->cur.p.layout & DAV1D_PIXEL_LAYOUT_I420, 1);
670
3.98k
}
671
672
static void obmc_lowest_px(Dav1dTaskContext *const t,
673
                           int (*const dst)[2], const int is_chroma,
674
                           const uint8_t *const b_dim,
675
                           const int bx4, const int by4, const int w4, const int h4)
676
44.1k
{
677
44.1k
    assert(!(t->bx & 1) && !(t->by & 1));
678
44.1k
    const Dav1dFrameContext *const f = t->f;
679
44.1k
    /*const*/ refmvs_block **r = &t->rt.r[(t->by & 31) + 5];
680
44.1k
    const int ss_ver = is_chroma && f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
681
44.1k
    const int ss_hor = is_chroma && f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
682
44.1k
    const int h_mul = 4 >> ss_hor, v_mul = 4 >> ss_ver;
683
684
44.1k
    if (t->by > t->ts->tiling.row_start &&
685
30.1k
        (!is_chroma || b_dim[0] * h_mul + b_dim[1] * v_mul >= 16))
686
26.0k
    {
687
54.9k
        for (int i = 0, x = 0; x < w4 && i < imin(b_dim[2], 4); ) {
688
            // only odd blocks are considered for overlap handling, hence +1
689
28.8k
            const refmvs_block *const a_r = &r[-1][t->bx + x + 1];
690
28.8k
            const uint8_t *const a_b_dim = dav1d_block_dimensions[a_r->bs];
691
692
28.8k
            if (a_r->ref.ref[0] > 0) {
693
28.2k
                const int oh4 = imin(b_dim[1], 16) >> 1;
694
28.2k
                mc_lowest_px(&dst[a_r->ref.ref[0] - 1][is_chroma], t->by,
695
28.2k
                             (oh4 * 3 + 3) >> 2, a_r->mv.mv[0].y, ss_ver,
696
28.2k
                             &f->svc[a_r->ref.ref[0] - 1][1]);
697
28.2k
                i++;
698
28.2k
            }
699
28.8k
            x += imax(a_b_dim[0], 2);
700
28.8k
        }
701
26.0k
    }
702
703
44.1k
    if (t->bx > t->ts->tiling.col_start)
704
82.4k
        for (int i = 0, y = 0; y < h4 && i < imin(b_dim[3], 4); ) {
705
            // only odd blocks are considered for overlap handling, hence +1
706
42.9k
            const refmvs_block *const l_r = &r[y + 1][t->bx - 1];
707
42.9k
            const uint8_t *const l_b_dim = dav1d_block_dimensions[l_r->bs];
708
709
42.9k
            if (l_r->ref.ref[0] > 0) {
710
42.0k
                const int oh4 = iclip(l_b_dim[1], 2, b_dim[1]);
711
42.0k
                mc_lowest_px(&dst[l_r->ref.ref[0] - 1][is_chroma],
712
42.0k
                             t->by + y, oh4, l_r->mv.mv[0].y, ss_ver,
713
42.0k
                             &f->svc[l_r->ref.ref[0] - 1][1]);
714
42.0k
                i++;
715
42.0k
            }
716
42.9k
            y += imax(l_b_dim[1], 2);
717
42.9k
        }
718
44.1k
}
719
720
static int decode_b(Dav1dTaskContext *const t,
721
                    const enum BlockLevel bl,
722
                    const enum BlockSize bs,
723
                    const enum BlockPartition bp,
724
2.90M
                    const enum EdgeFlags intra_edge_flags) {
725
2.90M
    Dav1dTileState *const ts = t->ts;
726
2.90M
    const Dav1dFrameContext *const f = t->f;
727
2.90M
    Av1Block b_mem, *const b = t->frame_thread.pass ?
728
18.4E
        &f->frame_thread.b[t->by * f->b4_stride + t->bx] : &b_mem;
729
2.90M
    const uint8_t *const b_dim = dav1d_block_dimensions[bs];
730
2.90M
    const int bx4 = t->bx & 31, by4 = t->by & 31;
731
2.90M
    const int ss_ver = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
732
2.90M
    const int ss_hor = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
733
2.90M
    const int cbx4 = bx4 >> ss_hor, cby4 = by4 >> ss_ver;
734
2.90M
    const int bw4 = b_dim[0], bh4 = b_dim[1];
735
2.90M
    const int w4 = imin(bw4, f->bw - t->bx), h4 = imin(bh4, f->bh - t->by);
736
2.90M
    const int cbw4 = (bw4 + ss_hor) >> ss_hor, cbh4 = (bh4 + ss_ver) >> ss_ver;
737
2.90M
    const int have_left = t->bx > ts->tiling.col_start;
738
2.90M
    const int have_top = t->by > ts->tiling.row_start;
739
2.90M
    const int has_chroma = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I400 &&
740
2.01M
                           (bw4 > ss_hor || t->bx & 1) &&
741
1.89M
                           (bh4 > ss_ver || t->by & 1);
742
743
2.90M
    if (t->frame_thread.pass == 2) {
744
911k
        if (b->intra) {
745
670k
            f->bd_fn.recon_b_intra(t, bs, intra_edge_flags, b);
746
747
670k
            const enum IntraPredMode y_mode_nofilt =
748
670k
                b->y_mode == FILTER_PRED ? DC_PRED : b->y_mode;
749
670k
#define set_ctx(rep_macro) \
750
1.34M
            rep_macro(edge->mode, off, y_mode_nofilt); \
751
1.34M
            rep_macro(edge->intra, off, 1)
752
670k
            BlockContext *edge = t->a;
753
2.01M
            for (int i = 0, off = bx4; i < 2; i++, off = by4, edge = &t->l) {
754
1.34M
                case_set(b_dim[2 + i]);
755
1.34M
            }
756
670k
#undef set_ctx
757
670k
            if (IS_INTER_OR_SWITCH(f->frame_hdr)) {
758
3.33k
                refmvs_block *const r = &t->rt.r[(t->by & 31) + 5 + bh4 - 1][t->bx];
759
20.5k
                for (int x = 0; x < bw4; x++) {
760
17.1k
                    r[x].ref.ref[0] = 0;
761
17.1k
                    r[x].bs = bs;
762
17.1k
                }
763
3.33k
                refmvs_block *const *rr = &t->rt.r[(t->by & 31) + 5];
764
3.33k
                const int bx = t->bx + bw4 - 1;
765
16.7k
                for (int y = 0; y < bh4 - 1; y++) {
766
13.4k
                    refmvs_block *d = &rr[y][bx];
767
13.4k
                    d->ref.ref[0] = 0;
768
13.4k
                    d->bs = bs;
769
13.4k
                }
770
3.33k
            }
771
772
670k
            if (has_chroma) {
773
377k
                uint8_t uv_mode = b->uv_mode;
774
377k
                dav1d_memset_pow2[ulog2(cbw4)](&t->a->uvmode[cbx4], uv_mode);
775
377k
                dav1d_memset_pow2[ulog2(cbh4)](&t->l.uvmode[cby4], uv_mode);
776
377k
            }
777
670k
        } else {
778
241k
            if (IS_INTER_OR_SWITCH(f->frame_hdr) /* not intrabc */ &&
779
58.9k
                b->comp_type == COMP_INTER_NONE && b->motion_mode == MM_WARP)
780
855
            {
781
855
                if (b->matrix[0] == INT16_MIN) {
782
133
                    t->warpmv.type = DAV1D_WM_TYPE_IDENTITY;
783
722
                } else {
784
722
                    t->warpmv.type = DAV1D_WM_TYPE_AFFINE;
785
722
                    t->warpmv.matrix[2] = b->matrix[0] + 0x10000;
786
722
                    t->warpmv.matrix[3] = b->matrix[1];
787
722
                    t->warpmv.matrix[4] = b->matrix[2];
788
722
                    t->warpmv.matrix[5] = b->matrix[3] + 0x10000;
789
722
                    dav1d_set_affine_mv2d(bw4, bh4, b->mv2d, &t->warpmv,
790
722
                                          t->bx, t->by);
791
722
                    dav1d_get_shear_params(&t->warpmv);
792
722
#define signabs(v) v < 0 ? '-' : ' ', abs(v)
793
722
                    if (DEBUG_BLOCK_INFO)
794
0
                        printf("[ %c%x %c%x %c%x\n  %c%x %c%x %c%x ]\n"
795
0
                               "alpha=%c%x, beta=%c%x, gamma=%c%x, delta=%c%x, mv=y:%d,x:%d\n",
796
0
                               signabs(t->warpmv.matrix[0]),
797
0
                               signabs(t->warpmv.matrix[1]),
798
0
                               signabs(t->warpmv.matrix[2]),
799
0
                               signabs(t->warpmv.matrix[3]),
800
0
                               signabs(t->warpmv.matrix[4]),
801
0
                               signabs(t->warpmv.matrix[5]),
802
0
                               signabs(t->warpmv.u.p.alpha),
803
0
                               signabs(t->warpmv.u.p.beta),
804
0
                               signabs(t->warpmv.u.p.gamma),
805
0
                               signabs(t->warpmv.u.p.delta),
806
0
                               b->mv2d.y, b->mv2d.x);
807
722
#undef signabs
808
722
                }
809
855
            }
810
241k
            if (f->bd_fn.recon_b_inter(t, bs, b)) return -1;
811
812
241k
            const uint8_t *const filter = dav1d_filter_dir[b->filter2d];
813
241k
            BlockContext *edge = t->a;
814
723k
            for (int i = 0, off = bx4; i < 2; i++, off = by4, edge = &t->l) {
815
482k
#define set_ctx(rep_macro) \
816
482k
                rep_macro(edge->filter[0], off, filter[0]); \
817
482k
                rep_macro(edge->filter[1], off, filter[1]); \
818
482k
                rep_macro(edge->intra, off, 0)
819
482k
                case_set(b_dim[2 + i]);
820
482k
#undef set_ctx
821
482k
            }
822
823
241k
            if (IS_INTER_OR_SWITCH(f->frame_hdr)) {
824
58.9k
                refmvs_block *const r = &t->rt.r[(t->by & 31) + 5 + bh4 - 1][t->bx];
825
58.9k
                const int ref1 = b->ref[0] + 1;
826
58.9k
                const union mv mv1 = b->mv[0];
827
331k
                for (int x = 0; x < bw4; x++) {
828
272k
                    r[x].ref.ref[0] = ref1;
829
272k
                    r[x].mv.mv[0] = mv1;
830
272k
                    r[x].bs = bs;
831
272k
                }
832
58.9k
                refmvs_block *const *rr = &t->rt.r[(t->by & 31) + 5];
833
58.9k
                const int bx = t->bx + bw4 - 1;
834
297k
                for (int y = 0; y < bh4 - 1; y++) {
835
238k
                    refmvs_block* d = &rr[y][bx];
836
238k
                    d->ref.ref[0] = ref1;
837
238k
                    d->mv.mv[0] = mv1;
838
238k
                    d->bs = bs;
839
238k
                }
840
58.9k
            }
841
842
241k
            if (has_chroma) {
843
137k
                dav1d_memset_pow2[ulog2(cbw4)](&t->a->uvmode[cbx4], DC_PRED);
844
137k
                dav1d_memset_pow2[ulog2(cbh4)](&t->l.uvmode[cby4], DC_PRED);
845
137k
            }
846
241k
        }
847
911k
        return 0;
848
911k
    }
849
850
1.99M
    const int cw4 = (w4 + ss_hor) >> ss_hor, ch4 = (h4 + ss_ver) >> ss_ver;
851
852
1.99M
    b->bl = bl;
853
1.99M
    b->bp = bp;
854
1.99M
    b->bs = bs;
855
856
1.99M
    const Dav1dSegmentationData *seg = NULL;
857
858
    // segment_id (if seg_feature for skip/ref/gmv is enabled)
859
1.99M
    int seg_pred = 0;
860
1.99M
    if (f->frame_hdr->segmentation.enabled) {
861
897k
        if (!f->frame_hdr->segmentation.update_map) {
862
46.4k
            if (f->prev_segmap) {
863
31.8k
                unsigned seg_id = get_prev_frame_segid(f, t->by, t->bx, w4, h4,
864
31.8k
                                                       f->prev_segmap,
865
31.8k
                                                       f->b4_stride);
866
31.8k
                if (seg_id >= 8) return -1;
867
31.8k
                b->seg_id = seg_id;
868
31.8k
            } else {
869
14.6k
                b->seg_id = 0;
870
14.6k
            }
871
46.4k
            seg = &f->frame_hdr->segmentation.seg_data.d[b->seg_id];
872
850k
        } else if (f->frame_hdr->segmentation.seg_data.preskip) {
873
743k
            if (f->frame_hdr->segmentation.temporal &&
874
10.1k
                (seg_pred = dav1d_msac_decode_bool_adapt(&ts->msac,
875
10.1k
                                ts->cdf.m.seg_pred[t->a->seg_pred[bx4] +
876
10.1k
                                t->l.seg_pred[by4]])))
877
6.46k
            {
878
                // temporal predicted seg_id
879
6.46k
                if (f->prev_segmap) {
880
5.25k
                    unsigned seg_id = get_prev_frame_segid(f, t->by, t->bx,
881
5.25k
                                                           w4, h4,
882
5.25k
                                                           f->prev_segmap,
883
5.25k
                                                           f->b4_stride);
884
5.25k
                    if (seg_id >= 8) return -1;
885
5.25k
                    b->seg_id = seg_id;
886
5.25k
                } else {
887
1.21k
                    b->seg_id = 0;
888
1.21k
                }
889
737k
            } else {
890
737k
                int seg_ctx;
891
737k
                const unsigned pred_seg_id =
892
737k
                    get_cur_frame_segid(t->by, t->bx, have_top, have_left,
893
737k
                                        &seg_ctx, f->cur_segmap, f->b4_stride);
894
737k
                const unsigned diff = dav1d_msac_decode_symbol_adapt8(&ts->msac,
895
737k
                                          ts->cdf.m.seg_id[seg_ctx],
896
737k
                                          DAV1D_MAX_SEGMENTS - 1);
897
737k
                const unsigned last_active_seg_id =
898
737k
                    f->frame_hdr->segmentation.seg_data.last_active_segid;
899
737k
                b->seg_id = neg_deinterleave(diff, pred_seg_id,
900
737k
                                             last_active_seg_id + 1);
901
737k
                if (b->seg_id > last_active_seg_id) b->seg_id = 0; // error?
902
737k
                if (b->seg_id >= DAV1D_MAX_SEGMENTS) b->seg_id = 0; // error?
903
737k
            }
904
905
743k
            if (DEBUG_BLOCK_INFO)
906
0
                printf("Post-segid[preskip;%d]: r=%d\n",
907
0
                       b->seg_id, ts->msac.rng);
908
909
743k
            seg = &f->frame_hdr->segmentation.seg_data.d[b->seg_id];
910
743k
        }
911
1.09M
    } else {
912
1.09M
        b->seg_id = 0;
913
1.09M
    }
914
915
    // skip_mode
916
1.99M
    if ((!seg || (!seg->globalmv && seg->ref == -1 && !seg->skip)) &&
917
1.34M
        f->frame_hdr->skip_mode_enabled && imin(bw4, bh4) > 1)
918
2.75k
    {
919
2.75k
        const int smctx = t->a->skip_mode[bx4] + t->l.skip_mode[by4];
920
2.75k
        b->skip_mode = dav1d_msac_decode_bool_adapt(&ts->msac,
921
2.75k
                           ts->cdf.m.skip_mode[smctx]);
922
2.75k
        if (DEBUG_BLOCK_INFO)
923
0
            printf("Post-skipmode[%d]: r=%d\n", b->skip_mode, ts->msac.rng);
924
1.99M
    } else {
925
1.99M
        b->skip_mode = 0;
926
1.99M
    }
927
928
    // skip
929
1.99M
    if (b->skip_mode || (seg && seg->skip)) {
930
333k
        b->skip = 1;
931
1.66M
    } else {
932
1.66M
        const int sctx = t->a->skip[bx4] + t->l.skip[by4];
933
1.66M
        b->skip = dav1d_msac_decode_bool_adapt(&ts->msac, ts->cdf.m.skip[sctx]);
934
1.66M
        if (DEBUG_BLOCK_INFO)
935
0
            printf("Post-skip[%d]: r=%d\n", b->skip, ts->msac.rng);
936
1.66M
    }
937
938
    // segment_id
939
1.99M
    if (f->frame_hdr->segmentation.enabled &&
940
898k
        f->frame_hdr->segmentation.update_map &&
941
851k
        !f->frame_hdr->segmentation.seg_data.preskip)
942
107k
    {
943
107k
        if (!b->skip && f->frame_hdr->segmentation.temporal &&
944
8.44k
            (seg_pred = dav1d_msac_decode_bool_adapt(&ts->msac,
945
8.44k
                            ts->cdf.m.seg_pred[t->a->seg_pred[bx4] +
946
8.44k
                            t->l.seg_pred[by4]])))
947
3.70k
        {
948
            // temporal predicted seg_id
949
3.70k
            if (f->prev_segmap) {
950
662
                unsigned seg_id = get_prev_frame_segid(f, t->by, t->bx, w4, h4,
951
662
                                                       f->prev_segmap,
952
662
                                                       f->b4_stride);
953
662
                if (seg_id >= 8) return -1;
954
662
                b->seg_id = seg_id;
955
3.04k
            } else {
956
3.04k
                b->seg_id = 0;
957
3.04k
            }
958
103k
        } else {
959
103k
            int seg_ctx;
960
103k
            const unsigned pred_seg_id =
961
103k
                get_cur_frame_segid(t->by, t->bx, have_top, have_left,
962
103k
                                    &seg_ctx, f->cur_segmap, f->b4_stride);
963
103k
            if (b->skip) {
964
49.4k
                b->seg_id = pred_seg_id;
965
54.4k
            } else {
966
54.4k
                const unsigned diff = dav1d_msac_decode_symbol_adapt8(&ts->msac,
967
54.4k
                                          ts->cdf.m.seg_id[seg_ctx],
968
54.4k
                                          DAV1D_MAX_SEGMENTS - 1);
969
54.4k
                const unsigned last_active_seg_id =
970
54.4k
                    f->frame_hdr->segmentation.seg_data.last_active_segid;
971
54.4k
                b->seg_id = neg_deinterleave(diff, pred_seg_id,
972
54.4k
                                             last_active_seg_id + 1);
973
54.4k
                if (b->seg_id > last_active_seg_id) b->seg_id = 0; // error?
974
54.4k
            }
975
103k
            if (b->seg_id >= DAV1D_MAX_SEGMENTS) b->seg_id = 0; // error?
976
103k
        }
977
978
107k
        seg = &f->frame_hdr->segmentation.seg_data.d[b->seg_id];
979
980
107k
        if (DEBUG_BLOCK_INFO)
981
0
            printf("Post-segid[postskip;%d]: r=%d\n",
982
0
                   b->seg_id, ts->msac.rng);
983
107k
    }
984
985
    // cdef index
986
1.99M
    if (!b->skip) {
987
825k
        const int idx = f->seq_hdr->sb128 ? ((t->bx & 16) >> 4) +
988
721k
                                           ((t->by & 16) >> 3) : 0;
989
825k
        if (t->cur_sb_cdef_idx_ptr[idx] == -1) {
990
197k
            const int v = dav1d_msac_decode_bools(&ts->msac,
991
197k
                              f->frame_hdr->cdef.n_bits);
992
197k
            t->cur_sb_cdef_idx_ptr[idx] = v;
993
197k
            if (bw4 > 16) t->cur_sb_cdef_idx_ptr[idx + 1] = v;
994
197k
            if (bh4 > 16) t->cur_sb_cdef_idx_ptr[idx + 2] = v;
995
197k
            if (bw4 == 32 && bh4 == 32) t->cur_sb_cdef_idx_ptr[idx + 3] = v;
996
997
197k
            if (DEBUG_BLOCK_INFO)
998
0
                printf("Post-cdef_idx[%d]: r=%d\n",
999
0
                        *t->cur_sb_cdef_idx_ptr, ts->msac.rng);
1000
197k
        }
1001
825k
    }
1002
1003
    // delta-q/lf
1004
1.99M
    if (!((t->bx | t->by) & (31 >> !f->seq_hdr->sb128))) {
1005
302k
        const int prev_qidx = ts->last_qidx;
1006
302k
        const int have_delta_q = f->frame_hdr->delta.q.present &&
1007
84.6k
            (bs != (f->seq_hdr->sb128 ? BS_128x128 : BS_64x64) || !b->skip);
1008
1009
302k
        uint32_t prev_delta_lf = ts->last_delta_lf.u32;
1010
1011
302k
        if (have_delta_q) {
1012
82.6k
            int delta_q = dav1d_msac_decode_symbol_adapt4(&ts->msac,
1013
82.6k
                                                          ts->cdf.m.delta_q, 3);
1014
82.6k
            if (delta_q == 3) {
1015
9.19k
                const int n_bits = 1 + dav1d_msac_decode_bools(&ts->msac, 3);
1016
9.19k
                delta_q = dav1d_msac_decode_bools(&ts->msac, n_bits) +
1017
9.19k
                          1 + (1 << n_bits);
1018
9.19k
            }
1019
82.6k
            if (delta_q) {
1020
18.0k
                const int sign = dav1d_msac_decode_bool_equi(&ts->msac);
1021
18.0k
                delta_q = apply_sign_bool(delta_q, sign);
1022
18.0k
                delta_q *= 1 << f->frame_hdr->delta.q.res_log2;
1023
18.0k
            }
1024
82.6k
            ts->last_qidx = iclip(ts->last_qidx + delta_q, 1, 255);
1025
82.6k
            if (have_delta_q && DEBUG_BLOCK_INFO)
1026
0
                printf("Post-delta_q[%d->%d]: r=%d\n",
1027
0
                       delta_q, ts->last_qidx, ts->msac.rng);
1028
1029
82.6k
            if (f->frame_hdr->delta.lf.present) {
1030
36.2k
                const int n_lfs = f->frame_hdr->delta.lf.multi ?
1031
25.3k
                    f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I400 ? 4 : 2 : 1;
1032
1033
133k
                for (int i = 0; i < n_lfs; i++) {
1034
97.1k
                    int delta_lf = dav1d_msac_decode_symbol_adapt4(&ts->msac,
1035
97.1k
                        ts->cdf.m.delta_lf[i + f->frame_hdr->delta.lf.multi], 3);
1036
97.1k
                    if (delta_lf == 3) {
1037
10.0k
                        const int n_bits = 1 + dav1d_msac_decode_bools(&ts->msac, 3);
1038
10.0k
                        delta_lf = dav1d_msac_decode_bools(&ts->msac, n_bits) +
1039
10.0k
                                   1 + (1 << n_bits);
1040
10.0k
                    }
1041
97.1k
                    if (delta_lf) {
1042
20.9k
                        const int sign = dav1d_msac_decode_bool_equi(&ts->msac);
1043
20.9k
                        delta_lf = apply_sign_bool(delta_lf, sign);
1044
20.9k
                        delta_lf *= 1 << f->frame_hdr->delta.lf.res_log2;
1045
20.9k
                    }
1046
97.1k
                    ts->last_delta_lf.i8[i] =
1047
97.1k
                        iclip(ts->last_delta_lf.i8[i] + delta_lf, -63, 63);
1048
97.1k
                    if (have_delta_q && DEBUG_BLOCK_INFO)
1049
0
                        printf("Post-delta_lf[%d:%d]: r=%d\n", i, delta_lf,
1050
0
                               ts->msac.rng);
1051
97.1k
                }
1052
36.2k
            }
1053
82.6k
        }
1054
302k
        if (ts->last_qidx == f->frame_hdr->quant.yac) {
1055
            // assign frame-wide q values to this sb
1056
250k
            ts->dq = f->dq;
1057
250k
        } else if (ts->last_qidx != prev_qidx) {
1058
            // find sb-specific quant parameters
1059
9.09k
            init_quant_tables(f->seq_hdr, f->frame_hdr, ts->last_qidx, ts->dqmem);
1060
9.09k
            ts->dq = ts->dqmem;
1061
9.09k
        }
1062
302k
        if (!ts->last_delta_lf.u32) {
1063
            // assign frame-wide lf values to this sb
1064
274k
            ts->lflvl = f->lf.lvl;
1065
274k
        } else if (ts->last_delta_lf.u32 != prev_delta_lf) {
1066
            // find sb-specific lf lvl parameters
1067
9.57k
            ts->lflvl = ts->lflvlmem;
1068
9.57k
            dav1d_calc_lf_values(ts->lflvlmem, f->frame_hdr, ts->last_delta_lf.i8);
1069
9.57k
        }
1070
302k
    }
1071
1072
1.99M
    if (b->skip_mode) {
1073
1.27k
        b->intra = 0;
1074
1.99M
    } else if (IS_INTER_OR_SWITCH(f->frame_hdr)) {
1075
239k
        if (seg && (seg->ref >= 0 || seg->globalmv)) {
1076
60.7k
            b->intra = !seg->ref;
1077
178k
        } else {
1078
178k
            const int ictx = get_intra_ctx(t->a, &t->l, by4, bx4,
1079
178k
                                           have_top, have_left);
1080
178k
            b->intra = !dav1d_msac_decode_bool_adapt(&ts->msac,
1081
178k
                            ts->cdf.m.intra[ictx]);
1082
178k
            if (DEBUG_BLOCK_INFO)
1083
0
                printf("Post-intra[%d]: r=%d\n", b->intra, ts->msac.rng);
1084
178k
        }
1085
1.75M
    } else if (f->frame_hdr->allow_intrabc) {
1086
993k
        b->intra = !dav1d_msac_decode_bool_adapt(&ts->msac, ts->cdf.m.intrabc);
1087
993k
        if (DEBUG_BLOCK_INFO)
1088
0
            printf("Post-intrabcflag[%d]: r=%d\n", b->intra, ts->msac.rng);
1089
993k
    } else {
1090
762k
        b->intra = 1;
1091
762k
    }
1092
1093
    // intra/inter-specific stuff
1094
1.99M
    if (b->intra) {
1095
1.13M
        uint16_t *const ymode_cdf = IS_INTER_OR_SWITCH(f->frame_hdr) ?
1096
19.5k
            ts->cdf.m.y_mode[dav1d_ymode_size_context[bs]] :
1097
1.13M
            ts->cdf.kfym[dav1d_intra_mode_context[t->a->mode[bx4]]]
1098
1.11M
                        [dav1d_intra_mode_context[t->l.mode[by4]]];
1099
1.13M
        b->y_mode = dav1d_msac_decode_symbol_adapt16(&ts->msac, ymode_cdf,
1100
1.13M
                                                     N_INTRA_PRED_MODES - 1);
1101
1.13M
        if (DEBUG_BLOCK_INFO)
1102
0
            printf("Post-ymode[%d]: r=%d\n", b->y_mode, ts->msac.rng);
1103
1104
        // angle delta
1105
1.13M
        if (b_dim[2] + b_dim[3] >= 2 && b->y_mode >= VERT_PRED &&
1106
386k
            b->y_mode <= VERT_LEFT_PRED)
1107
199k
        {
1108
199k
            uint16_t *const acdf = ts->cdf.m.angle_delta[b->y_mode - VERT_PRED];
1109
199k
            const int angle = dav1d_msac_decode_symbol_adapt8(&ts->msac, acdf, 6);
1110
199k
            b->y_angle = angle - 3;
1111
935k
        } else {
1112
935k
            b->y_angle = 0;
1113
935k
        }
1114
1115
1.13M
        if (has_chroma) {
1116
728k
            const int cfl_allowed = f->frame_hdr->segmentation.lossless[b->seg_id] ?
1117
701k
                cbw4 == 1 && cbh4 == 1 : !!(cfl_allowed_mask & (1 << bs));
1118
728k
            uint16_t *const uvmode_cdf = ts->cdf.m.uv_mode[cfl_allowed][b->y_mode];
1119
728k
            b->uv_mode = dav1d_msac_decode_symbol_adapt16(&ts->msac, uvmode_cdf,
1120
728k
                             N_UV_INTRA_PRED_MODES - 1 - !cfl_allowed);
1121
728k
            if (DEBUG_BLOCK_INFO)
1122
0
                printf("Post-uvmode[%d]: r=%d\n", b->uv_mode, ts->msac.rng);
1123
1124
728k
            b->uv_angle = 0;
1125
728k
            if (b->uv_mode == CFL_PRED) {
1126
233k
                const int sign = dav1d_msac_decode_symbol_adapt8(&ts->msac,
1127
233k
                                     ts->cdf.m.cfl_sign, 7) + 1;
1128
233k
                const int sign_u = sign * 0x56 >> 8, sign_v = sign - sign_u * 3;
1129
233k
                assert(sign_u == sign / 3);
1130
233k
                if (sign_u) {
1131
224k
                    const int ctx = (sign_u == 2) * 3 + sign_v;
1132
224k
                    const int alpha = dav1d_msac_decode_symbol_adapt16(&ts->msac,
1133
224k
                                          ts->cdf.m.cfl_alpha[ctx], 15) + 1;
1134
224k
                    b->cfl_alpha[0] = apply_sign_bool(alpha, sign_u & 1);
1135
224k
                } else {
1136
8.35k
                    b->cfl_alpha[0] = 0;
1137
8.35k
                }
1138
233k
                if (sign_v) {
1139
188k
                    const int ctx = (sign_v == 2) * 3 + sign_u;
1140
188k
                    const int alpha = dav1d_msac_decode_symbol_adapt16(&ts->msac,
1141
188k
                                          ts->cdf.m.cfl_alpha[ctx], 15) + 1;
1142
188k
                    b->cfl_alpha[1] = apply_sign_bool(alpha, sign_v & 1);
1143
188k
                } else {
1144
44.2k
                    b->cfl_alpha[1] = 0;
1145
44.2k
                }
1146
233k
                if (DEBUG_BLOCK_INFO)
1147
0
                    printf("Post-uvalphas[%d/%d]: r=%d\n",
1148
0
                           b->cfl_alpha[0], b->cfl_alpha[1], ts->msac.rng);
1149
495k
            } else if (b_dim[2] + b_dim[3] >= 2 && b->uv_mode >= VERT_PRED &&
1150
237k
                       b->uv_mode <= VERT_LEFT_PRED)
1151
120k
            {
1152
120k
                uint16_t *const acdf = ts->cdf.m.angle_delta[b->uv_mode - VERT_PRED];
1153
120k
                const int angle = dav1d_msac_decode_symbol_adapt8(&ts->msac, acdf, 6);
1154
120k
                b->uv_angle = angle - 3;
1155
120k
            }
1156
728k
        }
1157
1158
1.13M
        b->pal_sz[0] = b->pal_sz[1] = 0;
1159
1.13M
        if (f->frame_hdr->allow_screen_content_tools &&
1160
632k
            imax(bw4, bh4) <= 16 && bw4 + bh4 >= 4)
1161
435k
        {
1162
435k
            const int sz_ctx = b_dim[2] + b_dim[3] - 2;
1163
435k
            if (b->y_mode == DC_PRED) {
1164
209k
                const int pal_ctx = (t->a->pal_sz[bx4] > 0) + (t->l.pal_sz[by4] > 0);
1165
209k
                const int use_y_pal = dav1d_msac_decode_bool_adapt(&ts->msac,
1166
209k
                                          ts->cdf.m.pal_y[sz_ctx][pal_ctx]);
1167
209k
                if (DEBUG_BLOCK_INFO)
1168
0
                    printf("Post-y_pal[%d]: r=%d\n", use_y_pal, ts->msac.rng);
1169
209k
                if (use_y_pal)
1170
13.8k
                    f->bd_fn.read_pal_plane(t, b, 0, sz_ctx, bx4, by4);
1171
209k
            }
1172
1173
435k
            if (has_chroma && b->uv_mode == DC_PRED) {
1174
99.7k
                const int pal_ctx = b->pal_sz[0] > 0;
1175
99.7k
                const int use_uv_pal = dav1d_msac_decode_bool_adapt(&ts->msac,
1176
99.7k
                                           ts->cdf.m.pal_uv[pal_ctx]);
1177
99.7k
                if (DEBUG_BLOCK_INFO)
1178
0
                    printf("Post-uv_pal[%d]: r=%d\n", use_uv_pal, ts->msac.rng);
1179
99.7k
                if (use_uv_pal) // see aomedia bug 2183 for why we use luma coordinates
1180
4.62k
                    f->bd_fn.read_pal_uv(t, b, sz_ctx, bx4, by4);
1181
99.7k
            }
1182
435k
        }
1183
1184
1.13M
        if (b->y_mode == DC_PRED && !b->pal_sz[0] &&
1185
408k
            imax(b_dim[2], b_dim[3]) <= 3 && f->seq_hdr->filter_intra)
1186
172k
        {
1187
172k
            const int is_filter = dav1d_msac_decode_bool_adapt(&ts->msac,
1188
172k
                                      ts->cdf.m.use_filter_intra[bs]);
1189
172k
            if (is_filter) {
1190
117k
                b->y_mode = FILTER_PRED;
1191
117k
                b->y_angle = dav1d_msac_decode_symbol_adapt8(&ts->msac,
1192
117k
                                 ts->cdf.m.filter_intra, 4);
1193
117k
            }
1194
172k
            if (DEBUG_BLOCK_INFO)
1195
0
                printf("Post-filterintramode[%d/%d]: r=%d\n",
1196
0
                       b->y_mode, b->y_angle, ts->msac.rng);
1197
172k
        }
1198
1199
1.13M
        if (b->pal_sz[0]) {
1200
13.8k
            uint8_t *pal_idx;
1201
13.8k
            if (t->frame_thread.pass) {
1202
13.8k
                const int p = t->frame_thread.pass & 1;
1203
13.8k
                assert(ts->frame_thread[p].pal_idx);
1204
13.8k
                pal_idx = ts->frame_thread[p].pal_idx;
1205
13.8k
                ts->frame_thread[p].pal_idx += bw4 * bh4 * 8;
1206
13.8k
            } else
1207
0
                pal_idx = t->scratch.pal_idx_y;
1208
13.8k
            read_pal_indices(t, pal_idx, b->pal_sz[0], 0, w4, h4, bw4, bh4);
1209
13.8k
            if (DEBUG_BLOCK_INFO)
1210
0
                printf("Post-y-pal-indices: r=%d\n", ts->msac.rng);
1211
13.8k
        }
1212
1213
1.13M
        if (has_chroma && b->pal_sz[1]) {
1214
4.62k
            uint8_t *pal_idx;
1215
4.62k
            if (t->frame_thread.pass) {
1216
4.62k
                const int p = t->frame_thread.pass & 1;
1217
4.62k
                assert(ts->frame_thread[p].pal_idx);
1218
4.62k
                pal_idx = ts->frame_thread[p].pal_idx;
1219
4.62k
                ts->frame_thread[p].pal_idx += cbw4 * cbh4 * 8;
1220
4.62k
            } else
1221
0
                pal_idx = t->scratch.pal_idx_uv;
1222
4.62k
            read_pal_indices(t, pal_idx, b->pal_sz[1], 1, cw4, ch4, cbw4, cbh4);
1223
4.62k
            if (DEBUG_BLOCK_INFO)
1224
0
                printf("Post-uv-pal-indices: r=%d\n", ts->msac.rng);
1225
4.62k
        }
1226
1227
1.13M
        const TxfmInfo *t_dim;
1228
1.13M
        if (f->frame_hdr->segmentation.lossless[b->seg_id]) {
1229
36.9k
            b->tx = b->uvtx = (int) TX_4X4;
1230
36.9k
            t_dim = &dav1d_txfm_dimensions[TX_4X4];
1231
1.09M
        } else {
1232
1.09M
            b->tx = dav1d_max_txfm_size_for_bs[bs][0];
1233
1.09M
            b->uvtx = dav1d_max_txfm_size_for_bs[bs][f->cur.p.layout];
1234
1.09M
            t_dim = &dav1d_txfm_dimensions[b->tx];
1235
1.09M
            if (f->frame_hdr->txfm_mode == DAV1D_TX_SWITCHABLE && t_dim->max > TX_4X4) {
1236
267k
                const int tctx = get_tx_ctx(t->a, &t->l, t_dim, by4, bx4);
1237
267k
                uint16_t *const tx_cdf = ts->cdf.m.txsz[t_dim->max - 1][tctx];
1238
267k
                int depth = dav1d_msac_decode_symbol_adapt4(&ts->msac, tx_cdf,
1239
267k
                                imin(t_dim->max, 2));
1240
1241
466k
                while (depth--) {
1242
199k
                    b->tx = t_dim->sub;
1243
199k
                    t_dim = &dav1d_txfm_dimensions[b->tx];
1244
199k
                }
1245
267k
            }
1246
1.09M
            if (DEBUG_BLOCK_INFO)
1247
0
                printf("Post-tx[%d]: r=%d\n", b->tx, ts->msac.rng);
1248
1.09M
        }
1249
1250
        // reconstruction
1251
1.13M
        if (t->frame_thread.pass == 1) {
1252
1.13M
            f->bd_fn.read_coef_blocks(t, bs, b);
1253
18.4E
        } else {
1254
18.4E
            f->bd_fn.recon_b_intra(t, bs, intra_edge_flags, b);
1255
18.4E
        }
1256
1257
1.13M
        if (f->frame_hdr->loopfilter.level_y[0] ||
1258
578k
            f->frame_hdr->loopfilter.level_y[1])
1259
629k
        {
1260
629k
            dav1d_create_lf_mask_intra(&t->c->lf_mask_dsp, t->lf_mask, f->lf.level,
1261
629k
                                       f->b4_stride,
1262
629k
                                       (const uint8_t (*)[8][2])
1263
629k
                                       &ts->lflvl[b->seg_id][0][0][0],
1264
629k
                                       t->bx, t->by, f->w4, f->h4, bs,
1265
629k
                                       b->tx, b->uvtx, f->cur.p.layout,
1266
629k
                                       &t->a->tx_lpf_y[bx4], &t->l.tx_lpf_y[by4],
1267
629k
                                       has_chroma ? &t->a->tx_lpf_uv[cbx4] : NULL,
1268
629k
                                       has_chroma ? &t->l.tx_lpf_uv[cby4] : NULL);
1269
629k
        }
1270
        // update contexts
1271
1.13M
        const enum IntraPredMode y_mode_nofilt =
1272
1.13M
            b->y_mode == FILTER_PRED ? DC_PRED : b->y_mode;
1273
1.13M
        BlockContext *edge = t->a;
1274
3.40M
        for (int i = 0, off = bx4; i < 2; i++, off = by4, edge = &t->l) {
1275
2.26M
            int t_lsz = ((uint8_t *) &t_dim->lw)[i]; // lw then lh
1276
2.26M
#define set_ctx(rep_macro) \
1277
2.27M
            rep_macro(edge->tx_intra, off, t_lsz); \
1278
2.27M
            rep_macro(edge->tx, off, t_lsz); \
1279
2.27M
            rep_macro(edge->mode, off, y_mode_nofilt); \
1280
2.27M
            rep_macro(edge->pal_sz, off, b->pal_sz[0]); \
1281
2.27M
            rep_macro(edge->seg_pred, off, seg_pred); \
1282
2.27M
            rep_macro(edge->skip_mode, off, 0); \
1283
2.27M
            rep_macro(edge->intra, off, 1); \
1284
2.27M
            rep_macro(edge->skip, off, b->skip); \
1285
            /* see aomedia bug 2183 for why we use luma coordinates here */ \
1286
2.27M
            rep_macro(t->pal_sz_uv[i], off, (has_chroma ? b->pal_sz[1] : 0)); \
1287
2.27M
            if (IS_INTER_OR_SWITCH(f->frame_hdr)) { \
1288
39.0k
                rep_macro(edge->comp_type, off, COMP_INTER_NONE); \
1289
39.0k
                rep_macro(edge->ref[0], off, ((uint8_t) -1)); \
1290
39.0k
                rep_macro(edge->ref[1], off, ((uint8_t) -1)); \
1291
39.0k
                rep_macro(edge->filter[0], off, DAV1D_N_SWITCHABLE_FILTERS); \
1292
39.0k
                rep_macro(edge->filter[1], off, DAV1D_N_SWITCHABLE_FILTERS); \
1293
39.0k
            }
1294
2.26M
            case_set(b_dim[2 + i]);
1295
2.26M
#undef set_ctx
1296
2.26M
        }
1297
1.13M
        if (b->pal_sz[0])
1298
13.8k
            f->bd_fn.copy_pal_block_y(t, bx4, by4, bw4, bh4);
1299
1.13M
        if (has_chroma) {
1300
728k
            uint8_t uv_mode = b->uv_mode;
1301
728k
            dav1d_memset_pow2[ulog2(cbw4)](&t->a->uvmode[cbx4], uv_mode);
1302
728k
            dav1d_memset_pow2[ulog2(cbh4)](&t->l.uvmode[cby4], uv_mode);
1303
728k
            if (b->pal_sz[1])
1304
4.62k
                f->bd_fn.copy_pal_block_uv(t, bx4, by4, bw4, bh4);
1305
728k
        }
1306
1.13M
        if (IS_INTER_OR_SWITCH(f->frame_hdr) || f->frame_hdr->allow_intrabc)
1307
367k
            splat_intraref(f->c, t, bs, bw4, bh4);
1308
1.13M
    } else if (IS_KEY_OR_INTRA(f->frame_hdr)) {
1309
        // intra block copy
1310
646k
        refmvs_candidate mvstack[8];
1311
646k
        int n_mvs, ctx;
1312
646k
        dav1d_refmvs_find(&t->rt, mvstack, &n_mvs, &ctx,
1313
646k
                          (union refmvs_refpair) { .ref = { 0, -1 }},
1314
646k
                          bs, intra_edge_flags, t->by, t->bx);
1315
1316
646k
        if (mvstack[0].mv.mv[0].n)
1317
622k
            b->mv[0] = mvstack[0].mv.mv[0];
1318
23.1k
        else if (mvstack[1].mv.mv[0].n)
1319
0
            b->mv[0] = mvstack[1].mv.mv[0];
1320
23.1k
        else {
1321
23.1k
            if (t->by - (16 << f->seq_hdr->sb128) < ts->tiling.row_start) {
1322
15.5k
                b->mv[0].y = 0;
1323
15.5k
                b->mv[0].x = -(512 << f->seq_hdr->sb128) - 2048;
1324
15.5k
            } else {
1325
7.59k
                b->mv[0].y = -(512 << f->seq_hdr->sb128);
1326
7.59k
                b->mv[0].x = 0;
1327
7.59k
            }
1328
23.1k
        }
1329
1330
646k
        const union mv ref = b->mv[0];
1331
646k
        read_mv_residual(ts, &b->mv[0], -1);
1332
1333
        // clip intrabc motion vector to decoded parts of current tile
1334
646k
        int border_left = ts->tiling.col_start * 4;
1335
646k
        int border_top  = ts->tiling.row_start * 4;
1336
646k
        if (has_chroma) {
1337
507k
            if (bw4 < 2 &&  ss_hor)
1338
20.3k
                border_left += 4;
1339
507k
            if (bh4 < 2 &&  ss_ver)
1340
18.2k
                border_top  += 4;
1341
507k
        }
1342
646k
        int src_left   = t->bx * 4 + (b->mv[0].x >> 3);
1343
646k
        int src_top    = t->by * 4 + (b->mv[0].y >> 3);
1344
646k
        int src_right  = src_left + bw4 * 4;
1345
646k
        int src_bottom = src_top  + bh4 * 4;
1346
646k
        const int border_right = ((ts->tiling.col_end + (bw4 - 1)) & ~(bw4 - 1)) * 4;
1347
1348
        // check against left or right tile boundary and adjust if necessary
1349
646k
        if (src_left < border_left) {
1350
213k
            src_right += border_left - src_left;
1351
213k
            src_left  += border_left - src_left;
1352
432k
        } else if (src_right > border_right) {
1353
176k
            src_left  -= src_right - border_right;
1354
176k
            src_right -= src_right - border_right;
1355
176k
        }
1356
        // check against top tile boundary and adjust if necessary
1357
646k
        if (src_top < border_top) {
1358
453k
            src_bottom += border_top - src_top;
1359
453k
            src_top    += border_top - src_top;
1360
453k
        }
1361
1362
646k
        const int sbx = (t->bx >> (4 + f->seq_hdr->sb128)) << (6 + f->seq_hdr->sb128);
1363
646k
        const int sby = (t->by >> (4 + f->seq_hdr->sb128)) << (6 + f->seq_hdr->sb128);
1364
646k
        const int sb_size = 1 << (6 + f->seq_hdr->sb128);
1365
        // check for overlap with current superblock
1366
646k
        if (src_bottom > sby && src_right > sbx) {
1367
180k
            if (src_top - border_top >= src_bottom - sby) {
1368
                // if possible move src up into the previous suberblock row
1369
12.5k
                src_top    -= src_bottom - sby;
1370
12.5k
                src_bottom -= src_bottom - sby;
1371
167k
            } else if (src_left - border_left >= src_right - sbx) {
1372
                // if possible move src left into the previous suberblock
1373
167k
                src_left  -= src_right - sbx;
1374
167k
                src_right -= src_right - sbx;
1375
167k
            }
1376
180k
        }
1377
        // move src up if it is below current superblock row
1378
646k
        if (src_bottom > sby + sb_size) {
1379
5.63k
            src_top    -= src_bottom - (sby + sb_size);
1380
5.63k
            src_bottom -= src_bottom - (sby + sb_size);
1381
5.63k
        }
1382
        // error out if mv still overlaps with the current superblock
1383
646k
        if (src_bottom > sby && src_right > sbx)
1384
244
            return -1;
1385
1386
645k
        b->mv[0].x = (src_left - t->bx * 4) * 8;
1387
645k
        b->mv[0].y = (src_top  - t->by * 4) * 8;
1388
1389
645k
        if (DEBUG_BLOCK_INFO)
1390
0
            printf("Post-dmv[%d/%d,ref=%d/%d|%d/%d]: r=%d\n",
1391
0
                   b->mv[0].y, b->mv[0].x, ref.y, ref.x,
1392
0
                   mvstack[0].mv.mv[0].y, mvstack[0].mv.mv[0].x, ts->msac.rng);
1393
645k
        read_vartx_tree(t, b, bs, bx4, by4);
1394
1395
        // reconstruction
1396
645k
        if (t->frame_thread.pass == 1) {
1397
645k
            f->bd_fn.read_coef_blocks(t, bs, b);
1398
645k
            b->filter2d = FILTER_2D_BILINEAR;
1399
18.4E
        } else {
1400
18.4E
            if (f->bd_fn.recon_b_inter(t, bs, b)) return -1;
1401
18.4E
        }
1402
1403
645k
        splat_intrabc_mv(f->c, t, bs, b, bw4, bh4);
1404
645k
        BlockContext *edge = t->a;
1405
1.93M
        for (int i = 0, off = bx4; i < 2; i++, off = by4, edge = &t->l) {
1406
1.29M
#define set_ctx(rep_macro) \
1407
1.29M
            rep_macro(edge->tx_intra, off, b_dim[2 + i]); \
1408
1.29M
            rep_macro(edge->mode, off, DC_PRED); \
1409
1.29M
            rep_macro(edge->pal_sz, off, 0); \
1410
            /* see aomedia bug 2183 for why this is outside if (has_chroma) */ \
1411
1.29M
            rep_macro(t->pal_sz_uv[i], off, 0); \
1412
1.29M
            rep_macro(edge->seg_pred, off, seg_pred); \
1413
1.29M
            rep_macro(edge->skip_mode, off, 0); \
1414
1.29M
            rep_macro(edge->intra, off, 0); \
1415
1.29M
            rep_macro(edge->skip, off, b->skip)
1416
1.29M
            case_set(b_dim[2 + i]);
1417
1.29M
#undef set_ctx
1418
1.29M
        }
1419
645k
        if (has_chroma) {
1420
507k
            dav1d_memset_pow2[ulog2(cbw4)](&t->a->uvmode[cbx4], DC_PRED);
1421
507k
            dav1d_memset_pow2[ulog2(cbh4)](&t->l.uvmode[cby4], DC_PRED);
1422
507k
        }
1423
645k
    } else {
1424
        // inter-specific mode/mv coding
1425
215k
        int is_comp, has_subpel_filter;
1426
1427
215k
        if (b->skip_mode) {
1428
1.27k
            is_comp = 1;
1429
214k
        } else if ((!seg || (seg->ref == -1 && !seg->globalmv && !seg->skip)) &&
1430
158k
                   f->frame_hdr->switchable_comp_refs && imin(bw4, bh4) > 1)
1431
60.8k
        {
1432
60.8k
            const int ctx = get_comp_ctx(t->a, &t->l, by4, bx4,
1433
60.8k
                                         have_top, have_left);
1434
60.8k
            is_comp = dav1d_msac_decode_bool_adapt(&ts->msac,
1435
60.8k
                          ts->cdf.m.comp[ctx]);
1436
60.8k
            if (DEBUG_BLOCK_INFO)
1437
0
                printf("Post-compflag[%d]: r=%d\n", is_comp, ts->msac.rng);
1438
153k
        } else {
1439
153k
            is_comp = 0;
1440
153k
        }
1441
1442
215k
        if (b->skip_mode) {
1443
1.27k
            b->ref[0] = f->frame_hdr->skip_mode_refs[0];
1444
1.27k
            b->ref[1] = f->frame_hdr->skip_mode_refs[1];
1445
1.27k
            b->comp_type = COMP_INTER_AVG;
1446
1.27k
            b->inter_mode = NEARESTMV_NEARESTMV;
1447
1.27k
            b->drl_idx = NEAREST_DRL;
1448
1.27k
            has_subpel_filter = 0;
1449
1450
1.27k
            refmvs_candidate mvstack[8];
1451
1.27k
            int n_mvs, ctx;
1452
1.27k
            dav1d_refmvs_find(&t->rt, mvstack, &n_mvs, &ctx,
1453
1.27k
                              (union refmvs_refpair) { .ref = {
1454
1.27k
                                    b->ref[0] + 1, b->ref[1] + 1 }},
1455
1.27k
                              bs, intra_edge_flags, t->by, t->bx);
1456
1457
1.27k
            b->mv[0] = mvstack[0].mv.mv[0];
1458
1.27k
            b->mv[1] = mvstack[0].mv.mv[1];
1459
1.27k
            fix_mv_precision(f->frame_hdr, &b->mv[0]);
1460
1.27k
            fix_mv_precision(f->frame_hdr, &b->mv[1]);
1461
1.27k
            if (DEBUG_BLOCK_INFO)
1462
0
                printf("Post-skipmodeblock[mv=1:y=%d,x=%d,2:y=%d,x=%d,refs=%d+%d\n",
1463
0
                       b->mv[0].y, b->mv[0].x, b->mv[1].y, b->mv[1].x,
1464
0
                       b->ref[0], b->ref[1]);
1465
214k
        } else if (is_comp) {
1466
34.3k
            const int dir_ctx = get_comp_dir_ctx(t->a, &t->l, by4, bx4,
1467
34.3k
                                                 have_top, have_left);
1468
34.3k
            if (dav1d_msac_decode_bool_adapt(&ts->msac,
1469
34.3k
                    ts->cdf.m.comp_dir[dir_ctx]))
1470
29.2k
            {
1471
                // bidir - first reference (fw)
1472
29.2k
                const int ctx1 = av1_get_fwd_ref_ctx(t->a, &t->l, by4, bx4,
1473
29.2k
                                                     have_top, have_left);
1474
29.2k
                if (dav1d_msac_decode_bool_adapt(&ts->msac,
1475
29.2k
                        ts->cdf.m.comp_fwd_ref[0][ctx1]))
1476
14.7k
                {
1477
14.7k
                    const int ctx2 = av1_get_fwd_ref_2_ctx(t->a, &t->l, by4, bx4,
1478
14.7k
                                                           have_top, have_left);
1479
14.7k
                    b->ref[0] = 2 + dav1d_msac_decode_bool_adapt(&ts->msac,
1480
14.7k
                                        ts->cdf.m.comp_fwd_ref[2][ctx2]);
1481
14.7k
                } else {
1482
14.5k
                    const int ctx2 = av1_get_fwd_ref_1_ctx(t->a, &t->l, by4, bx4,
1483
14.5k
                                                           have_top, have_left);
1484
14.5k
                    b->ref[0] = dav1d_msac_decode_bool_adapt(&ts->msac,
1485
14.5k
                                    ts->cdf.m.comp_fwd_ref[1][ctx2]);
1486
14.5k
                }
1487
1488
                // second reference (bw)
1489
29.2k
                const int ctx3 = av1_get_bwd_ref_ctx(t->a, &t->l, by4, bx4,
1490
29.2k
                                                     have_top, have_left);
1491
29.2k
                if (dav1d_msac_decode_bool_adapt(&ts->msac,
1492
29.2k
                        ts->cdf.m.comp_bwd_ref[0][ctx3]))
1493
17.9k
                {
1494
17.9k
                    b->ref[1] = 6;
1495
17.9k
                } else {
1496
11.3k
                    const int ctx4 = av1_get_bwd_ref_1_ctx(t->a, &t->l, by4, bx4,
1497
11.3k
                                                           have_top, have_left);
1498
11.3k
                    b->ref[1] = 4 + dav1d_msac_decode_bool_adapt(&ts->msac,
1499
11.3k
                                        ts->cdf.m.comp_bwd_ref[1][ctx4]);
1500
11.3k
                }
1501
29.2k
            } else {
1502
                // unidir
1503
5.10k
                const int uctx_p = av1_get_uni_p_ctx(t->a, &t->l, by4, bx4,
1504
5.10k
                                                     have_top, have_left);
1505
5.10k
                if (dav1d_msac_decode_bool_adapt(&ts->msac,
1506
5.10k
                        ts->cdf.m.comp_uni_ref[0][uctx_p]))
1507
1.54k
                {
1508
1.54k
                    b->ref[0] = 4;
1509
1.54k
                    b->ref[1] = 6;
1510
3.55k
                } else {
1511
3.55k
                    const int uctx_p1 = av1_get_uni_p1_ctx(t->a, &t->l, by4, bx4,
1512
3.55k
                                                           have_top, have_left);
1513
3.55k
                    b->ref[0] = 0;
1514
3.55k
                    b->ref[1] = 1 + dav1d_msac_decode_bool_adapt(&ts->msac,
1515
3.55k
                                        ts->cdf.m.comp_uni_ref[1][uctx_p1]);
1516
3.55k
                    if (b->ref[1] == 2) {
1517
2.32k
                        const int uctx_p2 = av1_get_uni_p2_ctx(t->a, &t->l, by4, bx4,
1518
2.32k
                                                               have_top, have_left);
1519
2.32k
                        b->ref[1] += dav1d_msac_decode_bool_adapt(&ts->msac,
1520
2.32k
                                         ts->cdf.m.comp_uni_ref[2][uctx_p2]);
1521
2.32k
                    }
1522
3.55k
                }
1523
5.10k
            }
1524
34.3k
            if (DEBUG_BLOCK_INFO)
1525
0
                printf("Post-refs[%d/%d]: r=%d\n",
1526
0
                       b->ref[0], b->ref[1], ts->msac.rng);
1527
1528
34.3k
            refmvs_candidate mvstack[8];
1529
34.3k
            int n_mvs, ctx;
1530
34.3k
            dav1d_refmvs_find(&t->rt, mvstack, &n_mvs, &ctx,
1531
34.3k
                              (union refmvs_refpair) { .ref = {
1532
34.3k
                                    b->ref[0] + 1, b->ref[1] + 1 }},
1533
34.3k
                              bs, intra_edge_flags, t->by, t->bx);
1534
1535
34.3k
            b->inter_mode = dav1d_msac_decode_symbol_adapt8(&ts->msac,
1536
34.3k
                                ts->cdf.m.comp_inter_mode[ctx],
1537
34.3k
                                N_COMP_INTER_PRED_MODES - 1);
1538
34.3k
            if (DEBUG_BLOCK_INFO)
1539
0
                printf("Post-compintermode[%d,ctx=%d,n_mvs=%d]: r=%d\n",
1540
0
                       b->inter_mode, ctx, n_mvs, ts->msac.rng);
1541
1542
34.3k
            const uint8_t *const im = dav1d_comp_inter_pred_modes[b->inter_mode];
1543
34.3k
            b->drl_idx = NEAREST_DRL;
1544
34.3k
            if (b->inter_mode == NEWMV_NEWMV) {
1545
11.0k
                if (n_mvs > 1) { // NEARER, NEAR or NEARISH
1546
11.0k
                    const int drl_ctx_v1 = get_drl_context(mvstack, 0);
1547
11.0k
                    b->drl_idx += dav1d_msac_decode_bool_adapt(&ts->msac,
1548
11.0k
                                      ts->cdf.m.drl_bit[drl_ctx_v1]);
1549
11.0k
                    if (b->drl_idx == NEARER_DRL && n_mvs > 2) {
1550
2.11k
                        const int drl_ctx_v2 = get_drl_context(mvstack, 1);
1551
2.11k
                        b->drl_idx += dav1d_msac_decode_bool_adapt(&ts->msac,
1552
2.11k
                                          ts->cdf.m.drl_bit[drl_ctx_v2]);
1553
2.11k
                    }
1554
11.0k
                    if (DEBUG_BLOCK_INFO)
1555
0
                        printf("Post-drlidx[%d,n_mvs=%d]: r=%d\n",
1556
0
                               b->drl_idx, n_mvs, ts->msac.rng);
1557
11.0k
                }
1558
23.3k
            } else if (im[0] == NEARMV || im[1] == NEARMV) {
1559
6.71k
                b->drl_idx = NEARER_DRL;
1560
6.71k
                if (n_mvs > 2) { // NEAR or NEARISH
1561
490
                    const int drl_ctx_v2 = get_drl_context(mvstack, 1);
1562
490
                    b->drl_idx += dav1d_msac_decode_bool_adapt(&ts->msac,
1563
490
                                      ts->cdf.m.drl_bit[drl_ctx_v2]);
1564
490
                    if (b->drl_idx == NEAR_DRL && n_mvs > 3) {
1565
84
                        const int drl_ctx_v3 = get_drl_context(mvstack, 2);
1566
84
                        b->drl_idx += dav1d_msac_decode_bool_adapt(&ts->msac,
1567
84
                                          ts->cdf.m.drl_bit[drl_ctx_v3]);
1568
84
                    }
1569
490
                    if (DEBUG_BLOCK_INFO)
1570
0
                        printf("Post-drlidx[%d,n_mvs=%d]: r=%d\n",
1571
0
                               b->drl_idx, n_mvs, ts->msac.rng);
1572
490
                }
1573
6.71k
            }
1574
34.3k
            assert(b->drl_idx >= NEAREST_DRL && b->drl_idx <= NEARISH_DRL);
1575
1576
34.3k
#define assign_comp_mv(idx) \
1577
68.7k
            switch (im[idx]) { \
1578
11.5k
            case NEARMV: \
1579
34.4k
            case NEARESTMV: \
1580
34.4k
                b->mv[idx] = mvstack[b->drl_idx].mv.mv[idx]; \
1581
34.4k
                fix_mv_precision(f->frame_hdr, &b->mv[idx]); \
1582
34.4k
                break; \
1583
11.5k
            case GLOBALMV: \
1584
7.01k
                has_subpel_filter |= \
1585
7.01k
                    f->frame_hdr->gmv[b->ref[idx]].type == DAV1D_WM_TYPE_TRANSLATION; \
1586
7.01k
                b->mv[idx] = get_gmv_2d(&f->frame_hdr->gmv[b->ref[idx]], \
1587
7.01k
                                        t->bx, t->by, bw4, bh4, f->frame_hdr); \
1588
7.01k
                break; \
1589
27.3k
            case NEWMV: \
1590
27.3k
                b->mv[idx] = mvstack[b->drl_idx].mv.mv[idx]; \
1591
27.3k
                const int mv_prec = f->frame_hdr->hp - f->frame_hdr->force_integer_mv; \
1592
27.3k
                read_mv_residual(ts, &b->mv[idx], mv_prec); \
1593
27.3k
                break; \
1594
68.7k
            }
1595
34.3k
            has_subpel_filter = imin(bw4, bh4) == 1 ||
1596
34.3k
                                b->inter_mode != GLOBALMV_GLOBALMV;
1597
34.3k
            assign_comp_mv(0);
1598
34.3k
            assign_comp_mv(1);
1599
34.3k
#undef assign_comp_mv
1600
34.3k
            if (DEBUG_BLOCK_INFO)
1601
0
                printf("Post-residual_mv[1:y=%d,x=%d,2:y=%d,x=%d]: r=%d\n",
1602
0
                       b->mv[0].y, b->mv[0].x, b->mv[1].y, b->mv[1].x,
1603
0
                       ts->msac.rng);
1604
1605
            // jnt_comp vs. seg vs. wedge
1606
34.3k
            int is_segwedge = 0;
1607
34.3k
            if (f->seq_hdr->masked_compound) {
1608
23.2k
                const int mask_ctx = get_mask_comp_ctx(t->a, &t->l, by4, bx4);
1609
1610
23.2k
                is_segwedge = dav1d_msac_decode_bool_adapt(&ts->msac,
1611
23.2k
                                  ts->cdf.m.mask_comp[mask_ctx]);
1612
23.2k
                if (DEBUG_BLOCK_INFO)
1613
0
                    printf("Post-segwedge_vs_jntavg[%d,ctx=%d]: r=%d\n",
1614
0
                           is_segwedge, mask_ctx, ts->msac.rng);
1615
23.2k
            }
1616
1617
34.3k
            if (!is_segwedge) {
1618
25.6k
                if (f->seq_hdr->jnt_comp) {
1619
5.49k
                    const int jnt_ctx =
1620
5.49k
                        get_jnt_comp_ctx(f->seq_hdr->order_hint_n_bits,
1621
5.49k
                                         f->cur.frame_hdr->frame_offset,
1622
5.49k
                                         f->refp[b->ref[0]].p.frame_hdr->frame_offset,
1623
5.49k
                                         f->refp[b->ref[1]].p.frame_hdr->frame_offset,
1624
5.49k
                                         t->a, &t->l, by4, bx4);
1625
5.49k
                    b->comp_type = COMP_INTER_WEIGHTED_AVG +
1626
5.49k
                                   dav1d_msac_decode_bool_adapt(&ts->msac,
1627
5.49k
                                       ts->cdf.m.jnt_comp[jnt_ctx]);
1628
5.49k
                    if (DEBUG_BLOCK_INFO)
1629
0
                        printf("Post-jnt_comp[%d,ctx=%d[ac:%d,ar:%d,lc:%d,lr:%d]]: r=%d\n",
1630
0
                               b->comp_type == COMP_INTER_AVG,
1631
0
                               jnt_ctx, t->a->comp_type[bx4], t->a->ref[0][bx4],
1632
0
                               t->l.comp_type[by4], t->l.ref[0][by4],
1633
0
                               ts->msac.rng);
1634
20.1k
                } else {
1635
20.1k
                    b->comp_type = COMP_INTER_AVG;
1636
20.1k
                }
1637
25.6k
            } else {
1638
8.72k
                if (wedge_allowed_mask & (1 << bs)) {
1639
5.63k
                    const int ctx = dav1d_wedge_ctx_lut[bs];
1640
5.63k
                    b->comp_type = COMP_INTER_WEDGE -
1641
5.63k
                                   dav1d_msac_decode_bool_adapt(&ts->msac,
1642
5.63k
                                       ts->cdf.m.wedge_comp[ctx]);
1643
5.63k
                    if (b->comp_type == COMP_INTER_WEDGE)
1644
1.60k
                        b->wedge_idx = dav1d_msac_decode_symbol_adapt16(&ts->msac,
1645
1.60k
                                           ts->cdf.m.wedge_idx[ctx], 15);
1646
5.63k
                } else {
1647
3.08k
                    b->comp_type = COMP_INTER_SEG;
1648
3.08k
                }
1649
8.72k
                b->mask_sign = dav1d_msac_decode_bool_equi(&ts->msac);
1650
8.72k
                if (DEBUG_BLOCK_INFO)
1651
0
                    printf("Post-seg/wedge[%d,wedge_idx=%d,sign=%d]: r=%d\n",
1652
0
                           b->comp_type == COMP_INTER_WEDGE,
1653
0
                           b->wedge_idx, b->mask_sign, ts->msac.rng);
1654
8.72k
            }
1655
179k
        } else {
1656
179k
            b->comp_type = COMP_INTER_NONE;
1657
1658
            // ref
1659
179k
            if (seg && seg->ref > 0) {
1660
42.0k
                b->ref[0] = seg->ref - 1;
1661
137k
            } else if (seg && (seg->globalmv || seg->skip)) {
1662
19.0k
                b->ref[0] = 0;
1663
118k
            } else {
1664
118k
                const int ctx1 = av1_get_ref_ctx(t->a, &t->l, by4, bx4,
1665
118k
                                                 have_top, have_left);
1666
118k
                if (dav1d_msac_decode_bool_adapt(&ts->msac,
1667
118k
                                                 ts->cdf.m.ref[0][ctx1]))
1668
65.3k
                {
1669
65.3k
                    const int ctx2 = av1_get_ref_2_ctx(t->a, &t->l, by4, bx4,
1670
65.3k
                                                       have_top, have_left);
1671
65.3k
                    if (dav1d_msac_decode_bool_adapt(&ts->msac,
1672
65.3k
                                                     ts->cdf.m.ref[1][ctx2]))
1673
46.2k
                    {
1674
46.2k
                        b->ref[0] = 6;
1675
46.2k
                    } else {
1676
19.1k
                        const int ctx3 = av1_get_ref_6_ctx(t->a, &t->l, by4, bx4,
1677
19.1k
                                                           have_top, have_left);
1678
19.1k
                        b->ref[0] = 4 + dav1d_msac_decode_bool_adapt(&ts->msac,
1679
19.1k
                                            ts->cdf.m.ref[5][ctx3]);
1680
19.1k
                    }
1681
65.3k
                } else {
1682
53.4k
                    const int ctx2 = av1_get_ref_3_ctx(t->a, &t->l, by4, bx4,
1683
53.4k
                                                       have_top, have_left);
1684
53.4k
                    if (dav1d_msac_decode_bool_adapt(&ts->msac,
1685
53.4k
                                                     ts->cdf.m.ref[2][ctx2]))
1686
17.4k
                    {
1687
17.4k
                        const int ctx3 = av1_get_ref_5_ctx(t->a, &t->l, by4, bx4,
1688
17.4k
                                                           have_top, have_left);
1689
17.4k
                        b->ref[0] = 2 + dav1d_msac_decode_bool_adapt(&ts->msac,
1690
17.4k
                                            ts->cdf.m.ref[4][ctx3]);
1691
36.0k
                    } else {
1692
36.0k
                        const int ctx3 = av1_get_ref_4_ctx(t->a, &t->l, by4, bx4,
1693
36.0k
                                                           have_top, have_left);
1694
36.0k
                        b->ref[0] = dav1d_msac_decode_bool_adapt(&ts->msac,
1695
36.0k
                                        ts->cdf.m.ref[3][ctx3]);
1696
36.0k
                    }
1697
53.4k
                }
1698
118k
                if (DEBUG_BLOCK_INFO)
1699
0
                    printf("Post-ref[%d]: r=%d\n", b->ref[0], ts->msac.rng);
1700
118k
            }
1701
179k
            b->ref[1] = -1;
1702
1703
179k
            refmvs_candidate mvstack[8];
1704
179k
            int n_mvs, ctx;
1705
179k
            dav1d_refmvs_find(&t->rt, mvstack, &n_mvs, &ctx,
1706
179k
                              (union refmvs_refpair) { .ref = { b->ref[0] + 1, -1 }},
1707
179k
                              bs, intra_edge_flags, t->by, t->bx);
1708
1709
            // mode parsing and mv derivation from ref_mvs
1710
179k
            if ((seg && (seg->skip || seg->globalmv)) ||
1711
125k
                dav1d_msac_decode_bool_adapt(&ts->msac,
1712
125k
                                             ts->cdf.m.newmv_mode[ctx & 7]))
1713
144k
            {
1714
144k
                if ((seg && (seg->skip || seg->globalmv)) ||
1715
84.8k
                    !dav1d_msac_decode_bool_adapt(&ts->msac,
1716
84.8k
                         ts->cdf.m.globalmv_mode[(ctx >> 3) & 1]))
1717
64.4k
                {
1718
64.4k
                    b->inter_mode = GLOBALMV;
1719
64.4k
                    b->mv[0] = get_gmv_2d(&f->frame_hdr->gmv[b->ref[0]],
1720
64.4k
                                          t->bx, t->by, bw4, bh4, f->frame_hdr);
1721
64.4k
                    has_subpel_filter = imin(bw4, bh4) == 1 ||
1722
41.3k
                        f->frame_hdr->gmv[b->ref[0]].type == DAV1D_WM_TYPE_TRANSLATION;
1723
80.3k
                } else {
1724
80.3k
                    has_subpel_filter = 1;
1725
80.3k
                    if (dav1d_msac_decode_bool_adapt(&ts->msac,
1726
80.3k
                            ts->cdf.m.refmv_mode[(ctx >> 4) & 15]))
1727
40.7k
                    { // NEAREST, NEARER, NEAR or NEARISH
1728
40.7k
                        b->inter_mode = NEARMV;
1729
40.7k
                        b->drl_idx = NEARER_DRL;
1730
40.7k
                        if (n_mvs > 2) { // NEARER, NEAR or NEARISH
1731
8.61k
                            const int drl_ctx_v2 = get_drl_context(mvstack, 1);
1732
8.61k
                            b->drl_idx += dav1d_msac_decode_bool_adapt(&ts->msac,
1733
8.61k
                                              ts->cdf.m.drl_bit[drl_ctx_v2]);
1734
8.61k
                            if (b->drl_idx == NEAR_DRL && n_mvs > 3) { // NEAR or NEARISH
1735
2.76k
                                const int drl_ctx_v3 =
1736
2.76k
                                    get_drl_context(mvstack, 2);
1737
2.76k
                                b->drl_idx += dav1d_msac_decode_bool_adapt(&ts->msac,
1738
2.76k
                                                  ts->cdf.m.drl_bit[drl_ctx_v3]);
1739
2.76k
                            }
1740
8.61k
                        }
1741
40.7k
                    } else {
1742
39.6k
                        b->inter_mode = NEARESTMV;
1743
39.6k
                        b->drl_idx = NEAREST_DRL;
1744
39.6k
                    }
1745
80.3k
                    assert(b->drl_idx >= NEAREST_DRL && b->drl_idx <= NEARISH_DRL);
1746
80.3k
                    b->mv[0] = mvstack[b->drl_idx].mv.mv[0];
1747
80.3k
                    if (b->drl_idx < NEAR_DRL)
1748
74.0k
                        fix_mv_precision(f->frame_hdr, &b->mv[0]);
1749
80.3k
                }
1750
1751
144k
                if (DEBUG_BLOCK_INFO)
1752
0
                    printf("Post-intermode[%d,drl=%d,mv=y:%d,x:%d,n_mvs=%d]: r=%d\n",
1753
0
                           b->inter_mode, b->drl_idx, b->mv[0].y, b->mv[0].x, n_mvs,
1754
0
                           ts->msac.rng);
1755
144k
            } else {
1756
35.0k
                has_subpel_filter = 1;
1757
35.0k
                b->inter_mode = NEWMV;
1758
35.0k
                b->drl_idx = NEAREST_DRL;
1759
35.0k
                if (n_mvs > 1) { // NEARER, NEAR or NEARISH
1760
22.9k
                    const int drl_ctx_v1 = get_drl_context(mvstack, 0);
1761
22.9k
                    b->drl_idx += dav1d_msac_decode_bool_adapt(&ts->msac,
1762
22.9k
                                      ts->cdf.m.drl_bit[drl_ctx_v1]);
1763
22.9k
                    if (b->drl_idx == NEARER_DRL && n_mvs > 2) { // NEAR or NEARISH
1764
3.75k
                        const int drl_ctx_v2 = get_drl_context(mvstack, 1);
1765
3.75k
                        b->drl_idx += dav1d_msac_decode_bool_adapt(&ts->msac,
1766
3.75k
                                          ts->cdf.m.drl_bit[drl_ctx_v2]);
1767
3.75k
                    }
1768
22.9k
                }
1769
35.0k
                assert(b->drl_idx >= NEAREST_DRL && b->drl_idx <= NEARISH_DRL);
1770
35.0k
                if (n_mvs > 1) {
1771
22.9k
                    b->mv[0] = mvstack[b->drl_idx].mv.mv[0];
1772
22.9k
                } else {
1773
12.1k
                    assert(!b->drl_idx);
1774
12.1k
                    b->mv[0] = mvstack[0].mv.mv[0];
1775
12.1k
                    fix_mv_precision(f->frame_hdr, &b->mv[0]);
1776
12.1k
                }
1777
35.0k
                if (DEBUG_BLOCK_INFO)
1778
0
                    printf("Post-intermode[%d,drl=%d]: r=%d\n",
1779
0
                           b->inter_mode, b->drl_idx, ts->msac.rng);
1780
35.0k
                const int mv_prec = f->frame_hdr->hp - f->frame_hdr->force_integer_mv;
1781
35.0k
                read_mv_residual(ts, &b->mv[0], mv_prec);
1782
35.0k
                if (DEBUG_BLOCK_INFO)
1783
0
                    printf("Post-residualmv[mv=y:%d,x:%d]: r=%d\n",
1784
0
                           b->mv[0].y, b->mv[0].x, ts->msac.rng);
1785
35.0k
            }
1786
1787
            // interintra flags
1788
179k
            const int ii_sz_grp = dav1d_ymode_size_context[bs];
1789
179k
            if (f->seq_hdr->inter_intra &&
1790
134k
                interintra_allowed_mask & (1 << bs) &&
1791
53.3k
                dav1d_msac_decode_bool_adapt(&ts->msac,
1792
53.3k
                                             ts->cdf.m.interintra[ii_sz_grp]))
1793
9.18k
            {
1794
9.18k
                b->interintra_mode = dav1d_msac_decode_symbol_adapt4(&ts->msac,
1795
9.18k
                                         ts->cdf.m.interintra_mode[ii_sz_grp],
1796
9.18k
                                         N_INTER_INTRA_PRED_MODES - 1);
1797
9.18k
                const int wedge_ctx = dav1d_wedge_ctx_lut[bs];
1798
9.18k
                b->interintra_type = INTER_INTRA_BLEND +
1799
9.18k
                                     dav1d_msac_decode_bool_adapt(&ts->msac,
1800
9.18k
                                         ts->cdf.m.interintra_wedge[wedge_ctx]);
1801
9.18k
                if (b->interintra_type == INTER_INTRA_WEDGE)
1802
2.29k
                    b->wedge_idx = dav1d_msac_decode_symbol_adapt16(&ts->msac,
1803
2.29k
                                       ts->cdf.m.wedge_idx[wedge_ctx], 15);
1804
170k
            } else {
1805
170k
                b->interintra_type = INTER_INTRA_NONE;
1806
170k
            }
1807
179k
            if (DEBUG_BLOCK_INFO && f->seq_hdr->inter_intra &&
1808
0
                interintra_allowed_mask & (1 << bs))
1809
0
            {
1810
0
                printf("Post-interintra[t=%d,m=%d,w=%d]: r=%d\n",
1811
0
                       b->interintra_type, b->interintra_mode,
1812
0
                       b->wedge_idx, ts->msac.rng);
1813
0
            }
1814
1815
            // motion variation
1816
179k
            if (f->frame_hdr->switchable_motion_mode &&
1817
125k
                b->interintra_type == INTER_INTRA_NONE && imin(bw4, bh4) >= 2 &&
1818
                // is not warped global motion
1819
72.3k
                !(!f->frame_hdr->force_integer_mv && b->inter_mode == GLOBALMV &&
1820
11.0k
                  f->frame_hdr->gmv[b->ref[0]].type > DAV1D_WM_TYPE_TRANSLATION) &&
1821
                // has overlappable neighbours
1822
67.7k
                ((have_left && findoddzero(&t->l.intra[by4 + 1], h4 >> 1)) ||
1823
11.3k
                 (have_top && findoddzero(&t->a->intra[bx4 + 1], w4 >> 1))))
1824
63.2k
            {
1825
                // reaching here means the block allows obmc - check warp by
1826
                // finding matching-ref blocks in top/left edges
1827
63.2k
                uint64_t mask[2] = { 0, 0 };
1828
63.2k
                find_matching_ref(t, intra_edge_flags, bw4, bh4, w4, h4,
1829
63.2k
                                  have_left, have_top, b->ref[0], mask);
1830
63.2k
                const int allow_warp = !f->svc[b->ref[0]][0].scale &&
1831
54.9k
                    !f->frame_hdr->force_integer_mv &&
1832
31.6k
                    f->frame_hdr->warp_motion && (mask[0] | mask[1]);
1833
1834
63.2k
                b->motion_mode = allow_warp ?
1835
17.0k
                    dav1d_msac_decode_symbol_adapt4(&ts->msac,
1836
17.0k
                        ts->cdf.m.motion_mode[bs], 2) :
1837
63.2k
                    dav1d_msac_decode_bool_adapt(&ts->msac, ts->cdf.m.obmc[bs]);
1838
63.2k
                if (b->motion_mode == MM_WARP) {
1839
7.58k
                    has_subpel_filter = 0;
1840
7.58k
                    derive_warpmv(t, bw4, bh4, mask, b->mv[0], &t->warpmv);
1841
7.58k
#define signabs(v) v < 0 ? '-' : ' ', abs(v)
1842
7.58k
                    if (DEBUG_BLOCK_INFO)
1843
0
                        printf("[ %c%x %c%x %c%x\n  %c%x %c%x %c%x ]\n"
1844
0
                               "alpha=%c%x, beta=%c%x, gamma=%c%x, delta=%c%x, "
1845
0
                               "mv=y:%d,x:%d\n",
1846
0
                               signabs(t->warpmv.matrix[0]),
1847
0
                               signabs(t->warpmv.matrix[1]),
1848
0
                               signabs(t->warpmv.matrix[2]),
1849
0
                               signabs(t->warpmv.matrix[3]),
1850
0
                               signabs(t->warpmv.matrix[4]),
1851
0
                               signabs(t->warpmv.matrix[5]),
1852
0
                               signabs(t->warpmv.u.p.alpha),
1853
0
                               signabs(t->warpmv.u.p.beta),
1854
0
                               signabs(t->warpmv.u.p.gamma),
1855
0
                               signabs(t->warpmv.u.p.delta),
1856
0
                               b->mv[0].y, b->mv[0].x);
1857
7.58k
#undef signabs
1858
7.58k
                    if (t->frame_thread.pass) {
1859
7.58k
                        if (t->warpmv.type == DAV1D_WM_TYPE_AFFINE) {
1860
5.55k
                            b->matrix[0] = t->warpmv.matrix[2] - 0x10000;
1861
5.55k
                            b->matrix[1] = t->warpmv.matrix[3];
1862
5.55k
                            b->matrix[2] = t->warpmv.matrix[4];
1863
5.55k
                            b->matrix[3] = t->warpmv.matrix[5] - 0x10000;
1864
5.55k
                        } else {
1865
2.03k
                            b->matrix[0] = INT16_MIN;
1866
2.03k
                        }
1867
7.58k
                    }
1868
7.58k
                }
1869
1870
63.2k
                if (DEBUG_BLOCK_INFO)
1871
0
                    printf("Post-motionmode[%d]: r=%d [mask: 0x%" PRIx64 "/0x%"
1872
0
                           PRIx64 "]\n", b->motion_mode, ts->msac.rng, mask[0],
1873
0
                            mask[1]);
1874
116k
            } else {
1875
116k
                b->motion_mode = MM_TRANSLATION;
1876
116k
            }
1877
179k
        }
1878
1879
        // subpel filter
1880
215k
        enum Dav1dFilterMode filter[2];
1881
215k
        if (f->frame_hdr->subpel_filter_mode == DAV1D_FILTER_SWITCHABLE) {
1882
105k
            if (has_subpel_filter) {
1883
82.4k
                const int comp = b->comp_type != COMP_INTER_NONE;
1884
82.4k
                const int ctx1 = get_filter_ctx(t->a, &t->l, comp, 0, b->ref[0],
1885
82.4k
                                                by4, bx4);
1886
82.4k
                filter[0] = dav1d_msac_decode_symbol_adapt4(&ts->msac,
1887
82.4k
                               ts->cdf.m.filter[0][ctx1],
1888
82.4k
                               DAV1D_N_SWITCHABLE_FILTERS - 1);
1889
82.4k
                if (f->seq_hdr->dual_filter) {
1890
41.7k
                    const int ctx2 = get_filter_ctx(t->a, &t->l, comp, 1,
1891
41.7k
                                                    b->ref[0], by4, bx4);
1892
41.7k
                    if (DEBUG_BLOCK_INFO)
1893
0
                        printf("Post-subpel_filter1[%d,ctx=%d]: r=%d\n",
1894
0
                               filter[0], ctx1, ts->msac.rng);
1895
41.7k
                    filter[1] = dav1d_msac_decode_symbol_adapt4(&ts->msac,
1896
41.7k
                                    ts->cdf.m.filter[1][ctx2],
1897
41.7k
                                    DAV1D_N_SWITCHABLE_FILTERS - 1);
1898
41.7k
                    if (DEBUG_BLOCK_INFO)
1899
0
                        printf("Post-subpel_filter2[%d,ctx=%d]: r=%d\n",
1900
0
                               filter[1], ctx2, ts->msac.rng);
1901
41.7k
                } else {
1902
40.6k
                    filter[1] = filter[0];
1903
40.6k
                    if (DEBUG_BLOCK_INFO)
1904
0
                        printf("Post-subpel_filter[%d,ctx=%d]: r=%d\n",
1905
0
                               filter[0], ctx1, ts->msac.rng);
1906
40.6k
                }
1907
82.4k
            } else {
1908
23.0k
                filter[0] = filter[1] = DAV1D_FILTER_8TAP_REGULAR;
1909
23.0k
            }
1910
110k
        } else {
1911
110k
            filter[0] = filter[1] = f->frame_hdr->subpel_filter_mode;
1912
110k
        }
1913
215k
        b->filter2d = dav1d_filter_2d[filter[1]][filter[0]];
1914
1915
215k
        read_vartx_tree(t, b, bs, bx4, by4);
1916
1917
        // reconstruction
1918
221k
        if (t->frame_thread.pass == 1) {
1919
221k
            f->bd_fn.read_coef_blocks(t, bs, b);
1920
18.4E
        } else {
1921
18.4E
            if (f->bd_fn.recon_b_inter(t, bs, b)) return -1;
1922
18.4E
        }
1923
1924
215k
        if (f->frame_hdr->loopfilter.level_y[0] ||
1925
52.6k
            f->frame_hdr->loopfilter.level_y[1])
1926
187k
        {
1927
187k
            const int is_globalmv =
1928
187k
                b->inter_mode == (is_comp ? GLOBALMV_GLOBALMV : GLOBALMV);
1929
187k
            const uint8_t (*const lf_lvls)[8][2] = (const uint8_t (*)[8][2])
1930
187k
                &ts->lflvl[b->seg_id][0][b->ref[0] + 1][!is_globalmv];
1931
187k
            const uint16_t tx_split[2] = { b->tx_split0, b->tx_split1 };
1932
187k
            enum RectTxfmSize ytx = b->max_ytx, uvtx = b->uvtx;
1933
187k
            if (f->frame_hdr->segmentation.lossless[b->seg_id]) {
1934
18.8k
                ytx  = (enum RectTxfmSize) TX_4X4;
1935
18.8k
                uvtx = (enum RectTxfmSize) TX_4X4;
1936
18.8k
            }
1937
187k
            dav1d_create_lf_mask_inter(t, f->lf.level, f->b4_stride, lf_lvls, f->w4,
1938
187k
                                       f->h4, b->skip, bs, ytx, tx_split, uvtx,
1939
187k
                                       f->cur.p.layout, &t->a->tx_lpf_y[bx4],
1940
187k
                                       &t->l.tx_lpf_y[by4],
1941
187k
                                       has_chroma ? &t->a->tx_lpf_uv[cbx4] : NULL,
1942
187k
                                       has_chroma ? &t->l.tx_lpf_uv[cby4] : NULL);
1943
187k
        }
1944
1945
        // context updates
1946
215k
        if (is_comp)
1947
35.6k
            splat_tworef_mv(f->c, t, bs, b, bw4, bh4);
1948
179k
        else
1949
179k
            splat_oneref_mv(f->c, t, bs, b, bw4, bh4);
1950
215k
        BlockContext *edge = t->a;
1951
658k
        for (int i = 0, off = bx4; i < 2; i++, off = by4, edge = &t->l) {
1952
442k
#define set_ctx(rep_macro) \
1953
442k
            rep_macro(edge->seg_pred, off, seg_pred); \
1954
442k
            rep_macro(edge->skip_mode, off, b->skip_mode); \
1955
442k
            rep_macro(edge->intra, off, 0); \
1956
442k
            rep_macro(edge->skip, off, b->skip); \
1957
442k
            rep_macro(edge->pal_sz, off, 0); \
1958
            /* see aomedia bug 2183 for why this is outside if (has_chroma) */ \
1959
442k
            rep_macro(t->pal_sz_uv[i], off, 0); \
1960
442k
            rep_macro(edge->tx_intra, off, b_dim[2 + i]); \
1961
442k
            rep_macro(edge->comp_type, off, b->comp_type); \
1962
442k
            rep_macro(edge->filter[0], off, filter[0]); \
1963
442k
            rep_macro(edge->filter[1], off, filter[1]); \
1964
442k
            rep_macro(edge->mode, off, b->inter_mode); \
1965
442k
            rep_macro(edge->ref[0], off, b->ref[0]); \
1966
442k
            rep_macro(edge->ref[1], off, ((uint8_t) b->ref[1]))
1967
442k
            case_set(b_dim[2 + i]);
1968
442k
#undef set_ctx
1969
442k
        }
1970
215k
        if (has_chroma) {
1971
83.4k
            dav1d_memset_pow2[ulog2(cbw4)](&t->a->uvmode[cbx4], DC_PRED);
1972
83.4k
            dav1d_memset_pow2[ulog2(cbh4)](&t->l.uvmode[cby4], DC_PRED);
1973
83.4k
        }
1974
215k
    }
1975
1976
    // update contexts
1977
1.99M
    if (f->frame_hdr->segmentation.enabled &&
1978
898k
        f->frame_hdr->segmentation.update_map)
1979
851k
    {
1980
851k
        uint8_t *seg_ptr = &f->cur_segmap[t->by * f->b4_stride + t->bx];
1981
851k
#define set_ctx(rep_macro) \
1982
4.34M
        for (int y = 0; y < bh4; y++) { \
1983
3.49M
            rep_macro(seg_ptr, 0, b->seg_id); \
1984
3.49M
            seg_ptr += f->b4_stride; \
1985
3.49M
        }
1986
851k
        case_set(b_dim[2]);
1987
851k
#undef set_ctx
1988
851k
    }
1989
1.99M
    if (!b->skip) {
1990
825k
        uint16_t (*noskip_mask)[2] = &t->lf_mask->noskip_mask[by4 >> 1];
1991
825k
        const unsigned mask = (~0U >> (32 - bw4)) << (bx4 & 15);
1992
825k
        const int bx_idx = (bx4 & 16) >> 4;
1993
2.62M
        for (int y = 0; y < bh4; y += 2, noskip_mask++) {
1994
1.80M
            (*noskip_mask)[bx_idx] |= mask;
1995
1.80M
            if (bw4 == 32) // this should be mask >> 16, but it's 0xffffffff anyway
1996
115k
                (*noskip_mask)[1] |= mask;
1997
1.80M
        }
1998
825k
    }
1999
2000
2.00M
    if (t->frame_thread.pass == 1 && !b->intra && IS_INTER_OR_SWITCH(f->frame_hdr)) {
2001
221k
        const int sby = (t->by - ts->tiling.row_start) >> f->sb_shift;
2002
221k
        int (*const lowest_px)[2] = ts->lowest_pixel[sby];
2003
2004
        // keep track of motion vectors for each reference
2005
221k
        if (b->comp_type == COMP_INTER_NONE) {
2006
            // y
2007
185k
            if (imin(bw4, bh4) > 1 &&
2008
108k
                ((b->inter_mode == GLOBALMV && f->gmv_warp_allowed[b->ref[0]]) ||
2009
102k
                 (b->motion_mode == MM_WARP && t->warpmv.type > DAV1D_WM_TYPE_TRANSLATION)))
2010
11.2k
            {
2011
11.2k
                affine_lowest_px_luma(t, &lowest_px[b->ref[0]][0], b_dim,
2012
11.2k
                                      b->motion_mode == MM_WARP ? &t->warpmv :
2013
11.2k
                                      &f->frame_hdr->gmv[b->ref[0]]);
2014
174k
            } else {
2015
174k
                mc_lowest_px(&lowest_px[b->ref[0]][0], t->by, bh4, b->mv[0].y,
2016
174k
                             0, &f->svc[b->ref[0]][1]);
2017
174k
                if (b->motion_mode == MM_OBMC) {
2018
30.9k
                    obmc_lowest_px(t, lowest_px, 0, b_dim, bx4, by4, w4, h4);
2019
30.9k
                }
2020
174k
            }
2021
2022
            // uv
2023
185k
            if (has_chroma) {
2024
                // sub8x8 derivation
2025
68.7k
                int is_sub8x8 = bw4 == ss_hor || bh4 == ss_ver;
2026
68.7k
                refmvs_block *const *r;
2027
68.7k
                if (is_sub8x8) {
2028
11.1k
                    assert(ss_hor == 1);
2029
11.1k
                    r = &t->rt.r[(t->by & 31) + 5];
2030
11.1k
                    if (bw4 == 1) is_sub8x8 &= r[0][t->bx - 1].ref.ref[0] > 0;
2031
11.1k
                    if (bh4 == ss_ver) is_sub8x8 &= r[-1][t->bx].ref.ref[0] > 0;
2032
11.1k
                    if (bw4 == 1 && bh4 == ss_ver)
2033
2.61k
                        is_sub8x8 &= r[-1][t->bx - 1].ref.ref[0] > 0;
2034
11.1k
                }
2035
2036
                // chroma prediction
2037
68.7k
                if (is_sub8x8) {
2038
10.8k
                    assert(ss_hor == 1);
2039
10.8k
                    if (bw4 == 1 && bh4 == ss_ver) {
2040
2.52k
                        const refmvs_block *const rr = &r[-1][t->bx - 1];
2041
2.52k
                        mc_lowest_px(&lowest_px[rr->ref.ref[0] - 1][1],
2042
2.52k
                                     t->by - 1, bh4, rr->mv.mv[0].y, ss_ver,
2043
2.52k
                                     &f->svc[rr->ref.ref[0] - 1][1]);
2044
2.52k
                    }
2045
10.8k
                    if (bw4 == 1) {
2046
6.63k
                        const refmvs_block *const rr = &r[0][t->bx - 1];
2047
6.63k
                        mc_lowest_px(&lowest_px[rr->ref.ref[0] - 1][1],
2048
6.63k
                                     t->by, bh4, rr->mv.mv[0].y, ss_ver,
2049
6.63k
                                     &f->svc[rr->ref.ref[0] - 1][1]);
2050
6.63k
                    }
2051
10.8k
                    if (bh4 == ss_ver) {
2052
6.78k
                        const refmvs_block *const rr = &r[-1][t->bx];
2053
6.78k
                        mc_lowest_px(&lowest_px[rr->ref.ref[0] - 1][1],
2054
6.78k
                                     t->by - 1, bh4, rr->mv.mv[0].y, ss_ver,
2055
6.78k
                                     &f->svc[rr->ref.ref[0] - 1][1]);
2056
6.78k
                    }
2057
10.8k
                    mc_lowest_px(&lowest_px[b->ref[0]][1], t->by, bh4,
2058
10.8k
                                 b->mv[0].y, ss_ver, &f->svc[b->ref[0]][1]);
2059
57.8k
                } else {
2060
57.8k
                    if (imin(cbw4, cbh4) > 1 &&
2061
28.5k
                        ((b->inter_mode == GLOBALMV && f->gmv_warp_allowed[b->ref[0]]) ||
2062
25.9k
                         (b->motion_mode == MM_WARP && t->warpmv.type > DAV1D_WM_TYPE_TRANSLATION)))
2063
3.68k
                    {
2064
3.68k
                        affine_lowest_px_chroma(t, &lowest_px[b->ref[0]][1], b_dim,
2065
3.68k
                                                b->motion_mode == MM_WARP ? &t->warpmv :
2066
3.68k
                                                &f->frame_hdr->gmv[b->ref[0]]);
2067
54.1k
                    } else {
2068
54.1k
                        mc_lowest_px(&lowest_px[b->ref[0]][1],
2069
54.1k
                                     t->by & ~ss_ver, bh4 << (bh4 == ss_ver),
2070
54.1k
                                     b->mv[0].y, ss_ver, &f->svc[b->ref[0]][1]);
2071
54.1k
                        if (b->motion_mode == MM_OBMC) {
2072
13.1k
                            obmc_lowest_px(t, lowest_px, 1, b_dim, bx4, by4, w4, h4);
2073
13.1k
                        }
2074
54.1k
                    }
2075
57.8k
                }
2076
68.7k
            }
2077
185k
        } else {
2078
            // y
2079
106k
            for (int i = 0; i < 2; i++) {
2080
71.3k
                if (b->inter_mode == GLOBALMV_GLOBALMV && f->gmv_warp_allowed[b->ref[i]]) {
2081
821
                    affine_lowest_px_luma(t, &lowest_px[b->ref[i]][0], b_dim,
2082
821
                                          &f->frame_hdr->gmv[b->ref[i]]);
2083
70.5k
                } else {
2084
70.5k
                    mc_lowest_px(&lowest_px[b->ref[i]][0], t->by, bh4,
2085
70.5k
                                 b->mv[i].y, 0, &f->svc[b->ref[i]][1]);
2086
70.5k
                }
2087
71.3k
            }
2088
2089
            // uv
2090
44.3k
            if (has_chroma) for (int i = 0; i < 2; i++) {
2091
29.5k
                if (b->inter_mode == GLOBALMV_GLOBALMV &&
2092
3.02k
                    imin(cbw4, cbh4) > 1 && f->gmv_warp_allowed[b->ref[i]])
2093
306
                {
2094
306
                    affine_lowest_px_chroma(t, &lowest_px[b->ref[i]][1], b_dim,
2095
306
                                            &f->frame_hdr->gmv[b->ref[i]]);
2096
29.2k
                } else {
2097
29.2k
                    mc_lowest_px(&lowest_px[b->ref[i]][1], t->by, bh4,
2098
29.2k
                                 b->mv[i].y, ss_ver, &f->svc[b->ref[i]][1]);
2099
29.2k
                }
2100
29.5k
            }
2101
35.6k
        }
2102
221k
    }
2103
2104
1.99M
    return 0;
2105
1.99M
}
2106
2107
#if __has_feature(memory_sanitizer)
2108
2109
#include <sanitizer/msan_interface.h>
2110
2111
static int checked_decode_b(Dav1dTaskContext *const t,
2112
                            const enum BlockLevel bl,
2113
                            const enum BlockSize bs,
2114
                            const enum BlockPartition bp,
2115
                            const enum EdgeFlags intra_edge_flags)
2116
{
2117
    const Dav1dFrameContext *const f = t->f;
2118
    const int err = decode_b(t, bl, bs, bp, intra_edge_flags);
2119
2120
    if (err == 0 && !(t->frame_thread.pass & 1)) {
2121
        const int ss_ver = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
2122
        const int ss_hor = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
2123
        const uint8_t *const b_dim = dav1d_block_dimensions[bs];
2124
        const int bw4 = b_dim[0], bh4 = b_dim[1];
2125
        const int w4 = imin(bw4, f->bw - t->bx), h4 = imin(bh4, f->bh - t->by);
2126
        const int has_chroma = f->seq_hdr->layout != DAV1D_PIXEL_LAYOUT_I400 &&
2127
                               (bw4 > ss_hor || t->bx & 1) &&
2128
                               (bh4 > ss_ver || t->by & 1);
2129
2130
        for (int p = 0; p < 1 + 2 * has_chroma; p++) {
2131
            const int ss_ver = p && f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
2132
            const int ss_hor = p && f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
2133
            const ptrdiff_t stride = f->cur.stride[!!p];
2134
            const int bx = t->bx & ~ss_hor;
2135
            const int by = t->by & ~ss_ver;
2136
            const int width  = w4 << (2 - ss_hor + (bw4 == ss_hor));
2137
            const int height = h4 << (2 - ss_ver + (bh4 == ss_ver));
2138
2139
            const uint8_t *data = f->cur.data[p] + (by << (2 - ss_ver)) * stride +
2140
                                  (bx << (2 - ss_hor + !!f->seq_hdr->hbd));
2141
2142
            for (int y = 0; y < height; data += stride, y++) {
2143
                const size_t line_sz = width << !!f->seq_hdr->hbd;
2144
                if (__msan_test_shadow(data, line_sz) != -1) {
2145
                    fprintf(stderr, "B[%d](%d, %d) w4:%d, h4:%d, row:%d\n",
2146
                            p, bx, by, w4, h4, y);
2147
                    __msan_check_mem_is_initialized(data, line_sz);
2148
                }
2149
            }
2150
        }
2151
    }
2152
2153
    return err;
2154
}
2155
2156
#define decode_b checked_decode_b
2157
2158
#endif /* defined(__has_feature) */
2159
2160
static int decode_sb(Dav1dTaskContext *const t, const enum BlockLevel bl,
2161
                     const EdgeNode *const node)
2162
2.42M
{
2163
2.42M
    const Dav1dFrameContext *const f = t->f;
2164
2.42M
    Dav1dTileState *const ts = t->ts;
2165
2.42M
    const int hsz = 16 >> bl;
2166
2.42M
    const int have_h_split = f->bw > t->bx + hsz;
2167
2.42M
    const int have_v_split = f->bh > t->by + hsz;
2168
2169
2.42M
    if (!have_h_split && !have_v_split) {
2170
74.0k
        assert(bl < BL_8X8);
2171
74.0k
        return decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 0));
2172
74.0k
    }
2173
2174
2.34M
    uint16_t *pc;
2175
2.34M
    enum BlockPartition bp;
2176
2.34M
    int ctx, bx8, by8;
2177
2.34M
    if (t->frame_thread.pass != 2) {
2178
1.56M
        if (0 && bl == BL_64X64)
2179
0
            printf("poc=%d,y=%d,x=%d,bl=%d,r=%d\n",
2180
0
                   f->frame_hdr->frame_offset, t->by, t->bx, bl, ts->msac.rng);
2181
1.56M
        bx8 = (t->bx & 31) >> 1;
2182
1.56M
        by8 = (t->by & 31) >> 1;
2183
1.56M
        ctx = get_partition_ctx(t->a, &t->l, bl, by8, bx8);
2184
1.56M
        pc = ts->cdf.m.partition[bl][ctx];
2185
1.56M
    }
2186
2187
2.34M
    if (have_h_split && have_v_split) {
2188
1.22M
        if (t->frame_thread.pass == 2) {
2189
425k
            const Av1Block *const b = &f->frame_thread.b[t->by * f->b4_stride + t->bx];
2190
425k
            bp = b->bl == bl ? b->bp : PARTITION_SPLIT;
2191
803k
        } else {
2192
803k
            bp = dav1d_msac_decode_symbol_adapt16(&ts->msac, pc,
2193
803k
                                                  dav1d_partition_type_count[bl]);
2194
803k
            if (f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I422 &&
2195
4.12k
                (bp == PARTITION_V || bp == PARTITION_V4 ||
2196
4.05k
                 bp == PARTITION_T_LEFT_SPLIT || bp == PARTITION_T_RIGHT_SPLIT))
2197
96
            {
2198
96
                return 1;
2199
96
            }
2200
803k
            if (DEBUG_BLOCK_INFO)
2201
0
                printf("poc=%d,y=%d,x=%d,bl=%d,ctx=%d,bp=%d: r=%d\n",
2202
0
                       f->frame_hdr->frame_offset, t->by, t->bx, bl, ctx, bp,
2203
0
                       ts->msac.rng);
2204
803k
        }
2205
1.22M
        const uint8_t *const b = dav1d_block_sizes[bl][bp];
2206
2207
1.22M
        switch (bp) {
2208
429k
        case PARTITION_NONE:
2209
429k
            if (decode_b(t, bl, b[0], PARTITION_NONE, node->o))
2210
20
                return -1;
2211
428k
            break;
2212
428k
        case PARTITION_H:
2213
153k
            if (decode_b(t, bl, b[0], PARTITION_H, node->h[0]))
2214
15
                return -1;
2215
153k
            t->by += hsz;
2216
153k
            if (decode_b(t, bl, b[0], PARTITION_H, node->h[1]))
2217
14
                return -1;
2218
153k
            t->by -= hsz;
2219
153k
            break;
2220
106k
        case PARTITION_V:
2221
106k
            if (decode_b(t, bl, b[0], PARTITION_V, node->v[0]))
2222
13
                return -1;
2223
106k
            t->bx += hsz;
2224
106k
            if (decode_b(t, bl, b[0], PARTITION_V, node->v[1]))
2225
13
                return -1;
2226
106k
            t->bx -= hsz;
2227
106k
            break;
2228
387k
        case PARTITION_SPLIT:
2229
387k
            if (bl == BL_8X8) {
2230
228k
                const EdgeTip *const tip = (const EdgeTip *) node;
2231
228k
                assert(hsz == 1);
2232
228k
                if (decode_b(t, bl, BS_4x4, PARTITION_SPLIT, EDGE_ALL_TR_AND_BL))
2233
18
                    return -1;
2234
228k
                const enum Filter2d tl_filter = t->tl_4x4_filter;
2235
228k
                t->bx++;
2236
228k
                if (decode_b(t, bl, BS_4x4, PARTITION_SPLIT, tip->split[0]))
2237
8
                    return -1;
2238
228k
                t->bx--;
2239
228k
                t->by++;
2240
228k
                if (decode_b(t, bl, BS_4x4, PARTITION_SPLIT, tip->split[1]))
2241
14
                    return -1;
2242
228k
                t->bx++;
2243
228k
                t->tl_4x4_filter = tl_filter;
2244
228k
                if (decode_b(t, bl, BS_4x4, PARTITION_SPLIT, tip->split[2]))
2245
13
                    return -1;
2246
228k
                t->bx--;
2247
228k
                t->by--;
2248
228k
#if ARCH_X86_64
2249
228k
                if (t->frame_thread.pass) {
2250
                    /* In 8-bit mode with 2-pass decoding the coefficient buffer
2251
                     * can end up misaligned due to skips here. Work around
2252
                     * the issue by explicitly realigning the buffer. */
2253
228k
                    const int p = t->frame_thread.pass & 1;
2254
228k
                    ts->frame_thread[p].cf =
2255
228k
                        (void*)(((uintptr_t)ts->frame_thread[p].cf + 63) & ~63);
2256
228k
                }
2257
228k
#endif
2258
228k
            } else {
2259
158k
                if (decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 0)))
2260
100
                    return 1;
2261
158k
                t->bx += hsz;
2262
158k
                if (decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 1)))
2263
78
                    return 1;
2264
158k
                t->bx -= hsz;
2265
158k
                t->by += hsz;
2266
158k
                if (decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 2)))
2267
49
                    return 1;
2268
158k
                t->bx += hsz;
2269
158k
                if (decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 3)))
2270
53
                    return 1;
2271
158k
                t->bx -= hsz;
2272
158k
                t->by -= hsz;
2273
158k
            }
2274
387k
            break;
2275
387k
        case PARTITION_T_TOP_SPLIT: {
2276
16.2k
            if (decode_b(t, bl, b[0], PARTITION_T_TOP_SPLIT, EDGE_ALL_TR_AND_BL))
2277
3
                return -1;
2278
16.2k
            t->bx += hsz;
2279
16.2k
            if (decode_b(t, bl, b[0], PARTITION_T_TOP_SPLIT, node->v[1]))
2280
3
                return -1;
2281
16.2k
            t->bx -= hsz;
2282
16.2k
            t->by += hsz;
2283
16.2k
            if (decode_b(t, bl, b[1], PARTITION_T_TOP_SPLIT, node->h[1]))
2284
3
                return -1;
2285
16.2k
            t->by -= hsz;
2286
16.2k
            break;
2287
16.2k
        }
2288
18.7k
        case PARTITION_T_BOTTOM_SPLIT: {
2289
18.7k
            if (decode_b(t, bl, b[0], PARTITION_T_BOTTOM_SPLIT, node->h[0]))
2290
3
                return -1;
2291
18.7k
            t->by += hsz;
2292
18.7k
            if (decode_b(t, bl, b[1], PARTITION_T_BOTTOM_SPLIT, node->v[0]))
2293
3
                return -1;
2294
18.7k
            t->bx += hsz;
2295
18.7k
            if (decode_b(t, bl, b[1], PARTITION_T_BOTTOM_SPLIT, 0))
2296
5
                return -1;
2297
18.7k
            t->bx -= hsz;
2298
18.7k
            t->by -= hsz;
2299
18.7k
            break;
2300
18.7k
        }
2301
13.3k
        case PARTITION_T_LEFT_SPLIT: {
2302
13.3k
            if (decode_b(t, bl, b[0], PARTITION_T_LEFT_SPLIT, EDGE_ALL_TR_AND_BL))
2303
3
                return -1;
2304
13.3k
            t->by += hsz;
2305
13.3k
            if (decode_b(t, bl, b[0], PARTITION_T_LEFT_SPLIT, node->h[1]))
2306
3
                return -1;
2307
13.3k
            t->by -= hsz;
2308
13.3k
            t->bx += hsz;
2309
13.3k
            if (decode_b(t, bl, b[1], PARTITION_T_LEFT_SPLIT, node->v[1]))
2310
3
                return -1;
2311
13.3k
            t->bx -= hsz;
2312
13.3k
            break;
2313
13.3k
        }
2314
14.4k
        case PARTITION_T_RIGHT_SPLIT: {
2315
14.4k
            if (decode_b(t, bl, b[0], PARTITION_T_RIGHT_SPLIT, node->v[0]))
2316
3
                return -1;
2317
14.4k
            t->bx += hsz;
2318
14.4k
            if (decode_b(t, bl, b[1], PARTITION_T_RIGHT_SPLIT, node->h[0]))
2319
5
                return -1;
2320
14.4k
            t->by += hsz;
2321
14.4k
            if (decode_b(t, bl, b[1], PARTITION_T_RIGHT_SPLIT, 0))
2322
3
                return -1;
2323
14.4k
            t->by -= hsz;
2324
14.4k
            t->bx -= hsz;
2325
14.4k
            break;
2326
14.4k
        }
2327
43.7k
        case PARTITION_H4: {
2328
43.7k
            const EdgeBranch *const branch = (const EdgeBranch *) node;
2329
43.7k
            if (decode_b(t, bl, b[0], PARTITION_H4, node->h[0]))
2330
4
                return -1;
2331
43.7k
            t->by += hsz >> 1;
2332
43.7k
            if (decode_b(t, bl, b[0], PARTITION_H4, branch->h4))
2333
5
                return -1;
2334
43.7k
            t->by += hsz >> 1;
2335
43.7k
            if (decode_b(t, bl, b[0], PARTITION_H4, EDGE_ALL_LEFT_HAS_BOTTOM))
2336
3
                return -1;
2337
43.7k
            t->by += hsz >> 1;
2338
43.7k
            if (t->by < f->bh)
2339
42.1k
                if (decode_b(t, bl, b[0], PARTITION_H4, node->h[1]))
2340
4
                    return -1;
2341
43.7k
            t->by -= hsz * 3 >> 1;
2342
43.7k
            break;
2343
43.7k
        }
2344
46.2k
        case PARTITION_V4: {
2345
46.2k
            const EdgeBranch *const branch = (const EdgeBranch *) node;
2346
46.2k
            if (decode_b(t, bl, b[0], PARTITION_V4, node->v[0]))
2347
13
                return -1;
2348
46.2k
            t->bx += hsz >> 1;
2349
46.2k
            if (decode_b(t, bl, b[0], PARTITION_V4, branch->v4))
2350
10
                return -1;
2351
46.1k
            t->bx += hsz >> 1;
2352
46.1k
            if (decode_b(t, bl, b[0], PARTITION_V4, EDGE_ALL_TOP_HAS_RIGHT))
2353
3
                return -1;
2354
46.1k
            t->bx += hsz >> 1;
2355
46.1k
            if (t->bx < f->bw)
2356
44.6k
                if (decode_b(t, bl, b[0], PARTITION_V4, node->v[1]))
2357
3
                    return -1;
2358
46.1k
            t->bx -= hsz * 3 >> 1;
2359
46.1k
            break;
2360
46.1k
        }
2361
0
        default: assert(0);
2362
1.22M
        }
2363
1.22M
    } else if (have_h_split) {
2364
541k
        unsigned is_split;
2365
541k
        if (t->frame_thread.pass == 2) {
2366
100k
            const Av1Block *const b = &f->frame_thread.b[t->by * f->b4_stride + t->bx];
2367
100k
            is_split = b->bl != bl;
2368
441k
        } else {
2369
441k
            is_split = dav1d_msac_decode_bool(&ts->msac,
2370
441k
                           gather_top_partition_prob(pc, bl));
2371
441k
            if (DEBUG_BLOCK_INFO)
2372
0
                printf("poc=%d,y=%d,x=%d,bl=%d,ctx=%d,bp=%d: r=%d\n",
2373
0
                       f->frame_hdr->frame_offset, t->by, t->bx, bl, ctx,
2374
0
                       is_split ? PARTITION_SPLIT : PARTITION_H, ts->msac.rng);
2375
441k
        }
2376
2377
541k
        assert(bl < BL_8X8);
2378
541k
        if (is_split) {
2379
333k
            bp = PARTITION_SPLIT;
2380
333k
            if (decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 0))) return 1;
2381
333k
            t->bx += hsz;
2382
333k
            if (decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 1))) return 1;
2383
333k
            t->bx -= hsz;
2384
333k
        } else {
2385
208k
            bp = PARTITION_H;
2386
208k
            if (decode_b(t, bl, dav1d_block_sizes[bl][PARTITION_H][0],
2387
208k
                         PARTITION_H, node->h[0]))
2388
14
                return -1;
2389
208k
        }
2390
576k
    } else {
2391
576k
        assert(have_v_split);
2392
576k
        unsigned is_split;
2393
576k
        if (t->frame_thread.pass == 2) {
2394
261k
            const Av1Block *const b = &f->frame_thread.b[t->by * f->b4_stride + t->bx];
2395
261k
            is_split = b->bl != bl;
2396
314k
        } else {
2397
314k
            is_split = dav1d_msac_decode_bool(&ts->msac,
2398
314k
                           gather_left_partition_prob(pc, bl));
2399
314k
            if (f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I422 && !is_split)
2400
29
                return 1;
2401
314k
            if (DEBUG_BLOCK_INFO)
2402
0
                printf("poc=%d,y=%d,x=%d,bl=%d,ctx=%d,bp=%d: r=%d\n",
2403
0
                       f->frame_hdr->frame_offset, t->by, t->bx, bl, ctx,
2404
0
                       is_split ? PARTITION_SPLIT : PARTITION_V, ts->msac.rng);
2405
314k
        }
2406
2407
575k
        assert(bl < BL_8X8);
2408
575k
        if (is_split) {
2409
280k
            bp = PARTITION_SPLIT;
2410
280k
            if (decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 0))) return 1;
2411
280k
            t->by += hsz;
2412
280k
            if (decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 2))) return 1;
2413
280k
            t->by -= hsz;
2414
295k
        } else {
2415
295k
            bp = PARTITION_V;
2416
295k
            if (decode_b(t, bl, dav1d_block_sizes[bl][PARTITION_V][0],
2417
295k
                         PARTITION_V, node->v[0]))
2418
17
                return -1;
2419
295k
        }
2420
575k
    }
2421
2422
2.34M
    if (t->frame_thread.pass != 2 && (bp != PARTITION_SPLIT || bl == BL_8X8)) {
2423
1.02M
#define set_ctx(rep_macro) \
2424
1.02M
        rep_macro(t->a->partition, bx8, dav1d_al_part_ctx[0][bl][bp]); \
2425
1.02M
        rep_macro(t->l.partition, by8, dav1d_al_part_ctx[1][bl][bp])
2426
1.02M
        case_set_upto16(ulog2(hsz));
2427
1.02M
#undef set_ctx
2428
1.02M
    }
2429
2430
2.34M
    return 0;
2431
2.34M
}
2432
2433
486k
static void reset_context(BlockContext *const ctx, const int keyframe, const int pass) {
2434
486k
    memset(ctx->intra, keyframe, sizeof(ctx->intra));
2435
486k
    memset(ctx->uvmode, DC_PRED, sizeof(ctx->uvmode));
2436
486k
    if (keyframe)
2437
419k
        memset(ctx->mode, DC_PRED, sizeof(ctx->mode));
2438
2439
486k
    if (pass == 2) return;
2440
2441
253k
    memset(ctx->partition, 0, sizeof(ctx->partition));
2442
253k
    memset(ctx->skip, 0, sizeof(ctx->skip));
2443
253k
    memset(ctx->skip_mode, 0, sizeof(ctx->skip_mode));
2444
253k
    memset(ctx->tx_lpf_y, 2, sizeof(ctx->tx_lpf_y));
2445
253k
    memset(ctx->tx_lpf_uv, 1, sizeof(ctx->tx_lpf_uv));
2446
253k
    memset(ctx->tx_intra, -1, sizeof(ctx->tx_intra));
2447
253k
    memset(ctx->tx, TX_64X64, sizeof(ctx->tx));
2448
253k
    if (!keyframe) {
2449
34.5k
        memset(ctx->ref, -1, sizeof(ctx->ref));
2450
34.5k
        memset(ctx->comp_type, 0, sizeof(ctx->comp_type));
2451
34.5k
        memset(ctx->mode, NEARESTMV, sizeof(ctx->mode));
2452
34.5k
    }
2453
253k
    memset(ctx->lcoef, 0x40, sizeof(ctx->lcoef));
2454
253k
    memset(ctx->ccoef, 0x40, sizeof(ctx->ccoef));
2455
253k
    memset(ctx->filter, DAV1D_N_SWITCHABLE_FILTERS, sizeof(ctx->filter));
2456
253k
    memset(ctx->seg_pred, 0, sizeof(ctx->seg_pred));
2457
253k
    memset(ctx->pal_sz, 0, sizeof(ctx->pal_sz));
2458
253k
}
2459
2460
// { Y+U+V, Y+U } * 4
2461
static const uint8_t ss_size_mul[4][2] = {
2462
    [DAV1D_PIXEL_LAYOUT_I400] = {  4, 4 },
2463
    [DAV1D_PIXEL_LAYOUT_I420] = {  6, 5 },
2464
    [DAV1D_PIXEL_LAYOUT_I422] = {  8, 6 },
2465
    [DAV1D_PIXEL_LAYOUT_I444] = { 12, 8 },
2466
};
2467
2468
static void setup_tile(Dav1dTileState *const ts,
2469
                       const Dav1dFrameContext *const f,
2470
                       const uint8_t *const data, const size_t sz,
2471
                       const int tile_row, const int tile_col,
2472
                       const unsigned tile_start_off)
2473
34.5k
{
2474
34.5k
    const int col_sb_start = f->frame_hdr->tiling.col_start_sb[tile_col];
2475
34.5k
    const int col_sb128_start = col_sb_start >> !f->seq_hdr->sb128;
2476
34.5k
    const int col_sb_end = f->frame_hdr->tiling.col_start_sb[tile_col + 1];
2477
34.5k
    const int row_sb_start = f->frame_hdr->tiling.row_start_sb[tile_row];
2478
34.5k
    const int row_sb_end = f->frame_hdr->tiling.row_start_sb[tile_row + 1];
2479
34.5k
    const int sb_shift = f->sb_shift;
2480
2481
34.5k
    const uint8_t *const size_mul = ss_size_mul[f->cur.p.layout];
2482
103k
    for (int p = 0; p < 2; p++) {
2483
69.0k
        ts->frame_thread[p].pal_idx = f->frame_thread.pal_idx ?
2484
41.4k
            &f->frame_thread.pal_idx[(size_t)tile_start_off * size_mul[1] / 8] :
2485
69.0k
            NULL;
2486
69.0k
        ts->frame_thread[p].cbi = f->frame_thread.cbi ?
2487
69.0k
            &f->frame_thread.cbi[(size_t)tile_start_off * size_mul[0] / 64] :
2488
69.0k
            NULL;
2489
69.0k
        ts->frame_thread[p].cf = f->frame_thread.cf ?
2490
69.0k
            (uint8_t*)f->frame_thread.cf +
2491
69.0k
                (((size_t)tile_start_off * size_mul[0]) >> !f->seq_hdr->hbd) :
2492
69.0k
            NULL;
2493
69.0k
    }
2494
2495
34.5k
    dav1d_cdf_thread_copy(&ts->cdf, &f->in_cdf);
2496
34.5k
    ts->last_qidx = f->frame_hdr->quant.yac;
2497
34.5k
    ts->last_delta_lf.u32 = 0;
2498
2499
34.5k
    dav1d_msac_init(&ts->msac, data, sz, f->frame_hdr->disable_cdf_update);
2500
2501
34.5k
    ts->tiling.row = tile_row;
2502
34.5k
    ts->tiling.col = tile_col;
2503
34.5k
    ts->tiling.col_start = col_sb_start << sb_shift;
2504
34.5k
    ts->tiling.col_end = imin(col_sb_end << sb_shift, f->bw);
2505
34.5k
    ts->tiling.row_start = row_sb_start << sb_shift;
2506
34.5k
    ts->tiling.row_end = imin(row_sb_end << sb_shift, f->bh);
2507
2508
    // Reference Restoration Unit (used for exp coding)
2509
34.5k
    int sb_idx, unit_idx;
2510
34.5k
    if (f->frame_hdr->width[0] != f->frame_hdr->width[1]) {
2511
        // vertical components only
2512
6.13k
        sb_idx = (ts->tiling.row_start >> 5) * f->sr_sb128w;
2513
6.13k
        unit_idx = (ts->tiling.row_start & 16) >> 3;
2514
28.4k
    } else {
2515
28.4k
        sb_idx = (ts->tiling.row_start >> 5) * f->sb128w + col_sb128_start;
2516
28.4k
        unit_idx = ((ts->tiling.row_start & 16) >> 3) +
2517
28.4k
                   ((ts->tiling.col_start & 16) >> 4);
2518
28.4k
    }
2519
138k
    for (int p = 0; p < 3; p++) {
2520
103k
        if (!((f->lf.restore_planes >> p) & 1U))
2521
81.3k
            continue;
2522
2523
22.2k
        if (f->frame_hdr->width[0] != f->frame_hdr->width[1]) {
2524
5.77k
            const int ss_hor = p && f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
2525
5.77k
            const int d = f->frame_hdr->super_res.width_scale_denominator;
2526
5.77k
            const int unit_size_log2 = f->frame_hdr->restoration.unit_size[!!p];
2527
5.77k
            const int rnd = (8 << unit_size_log2) - 1, shift = unit_size_log2 + 3;
2528
5.77k
            const int x = ((4 * ts->tiling.col_start * d >> ss_hor) + rnd) >> shift;
2529
5.77k
            const int px_x = x << (unit_size_log2 + ss_hor);
2530
5.77k
            const int u_idx = unit_idx + ((px_x & 64) >> 6);
2531
5.77k
            const int sb128x = px_x >> 7;
2532
5.77k
            if (sb128x >= f->sr_sb128w) continue;
2533
5.67k
            ts->lr_ref[p] = &f->lf.lr_mask[sb_idx + sb128x].lr[p][u_idx];
2534
16.4k
        } else {
2535
16.4k
            ts->lr_ref[p] = &f->lf.lr_mask[sb_idx].lr[p][unit_idx];
2536
16.4k
        }
2537
2538
22.1k
        ts->lr_ref[p]->filter_v[0] = 3;
2539
22.1k
        ts->lr_ref[p]->filter_v[1] = -7;
2540
22.1k
        ts->lr_ref[p]->filter_v[2] = 15;
2541
22.1k
        ts->lr_ref[p]->filter_h[0] = 3;
2542
22.1k
        ts->lr_ref[p]->filter_h[1] = -7;
2543
22.1k
        ts->lr_ref[p]->filter_h[2] = 15;
2544
22.1k
        ts->lr_ref[p]->sgr_weights[0] = -32;
2545
22.1k
        ts->lr_ref[p]->sgr_weights[1] = 31;
2546
22.1k
    }
2547
2548
34.5k
    if (f->c->n_tc > 1) {
2549
103k
        for (int p = 0; p < 2; p++)
2550
69.0k
            atomic_init(&ts->progress[p], row_sb_start);
2551
34.5k
    }
2552
34.5k
}
2553
2554
static void read_restoration_info(Dav1dTaskContext *const t,
2555
                                  Av1RestorationUnit *const lr, const int p,
2556
                                  const enum Dav1dRestorationType frame_type)
2557
86.0k
{
2558
86.0k
    const Dav1dFrameContext *const f = t->f;
2559
86.0k
    Dav1dTileState *const ts = t->ts;
2560
86.0k
    const Av1RestorationUnit *const lr_ref = ts->lr_ref[p];
2561
2562
86.0k
    if (frame_type == DAV1D_RESTORATION_SWITCHABLE) {
2563
27.8k
        const int filter = dav1d_msac_decode_symbol_adapt4(&ts->msac,
2564
27.8k
                               ts->cdf.m.restore_switchable, 2);
2565
27.8k
        lr->type = filter + !!filter; /* NONE/WIENER/SGRPROJ */
2566
58.1k
    } else {
2567
58.1k
        const unsigned type =
2568
58.1k
            dav1d_msac_decode_bool_adapt(&ts->msac,
2569
58.1k
                frame_type == DAV1D_RESTORATION_WIENER ?
2570
30.9k
                ts->cdf.m.restore_wiener : ts->cdf.m.restore_sgrproj);
2571
58.1k
        lr->type = type ? frame_type : DAV1D_RESTORATION_NONE;
2572
58.1k
    }
2573
2574
86.0k
    if (lr->type == DAV1D_RESTORATION_WIENER) {
2575
23.9k
        lr->filter_v[0] = p ? 0 :
2576
23.9k
            dav1d_msac_decode_subexp(&ts->msac,
2577
10.6k
                lr_ref->filter_v[0] + 5, 16, 1) - 5;
2578
23.9k
        lr->filter_v[1] =
2579
23.9k
            dav1d_msac_decode_subexp(&ts->msac,
2580
23.9k
                lr_ref->filter_v[1] + 23, 32, 2) - 23;
2581
23.9k
        lr->filter_v[2] =
2582
23.9k
            dav1d_msac_decode_subexp(&ts->msac,
2583
23.9k
                lr_ref->filter_v[2] + 17, 64, 3) - 17;
2584
2585
23.9k
        lr->filter_h[0] = p ? 0 :
2586
23.9k
            dav1d_msac_decode_subexp(&ts->msac,
2587
10.6k
                lr_ref->filter_h[0] + 5, 16, 1) - 5;
2588
23.9k
        lr->filter_h[1] =
2589
23.9k
            dav1d_msac_decode_subexp(&ts->msac,
2590
23.9k
                lr_ref->filter_h[1] + 23, 32, 2) - 23;
2591
23.9k
        lr->filter_h[2] =
2592
23.9k
            dav1d_msac_decode_subexp(&ts->msac,
2593
23.9k
                lr_ref->filter_h[2] + 17, 64, 3) - 17;
2594
23.9k
        memcpy(lr->sgr_weights, lr_ref->sgr_weights, sizeof(lr->sgr_weights));
2595
23.9k
        ts->lr_ref[p] = lr;
2596
23.9k
        if (DEBUG_BLOCK_INFO)
2597
0
            printf("Post-lr_wiener[pl=%d,v[%d,%d,%d],h[%d,%d,%d]]: r=%d\n",
2598
0
                   p, lr->filter_v[0], lr->filter_v[1],
2599
0
                   lr->filter_v[2], lr->filter_h[0],
2600
0
                   lr->filter_h[1], lr->filter_h[2], ts->msac.rng);
2601
62.0k
    } else if (lr->type == DAV1D_RESTORATION_SGRPROJ) {
2602
25.6k
        const unsigned idx = dav1d_msac_decode_bools(&ts->msac, 4);
2603
25.6k
        const uint16_t *const sgr_params = dav1d_sgr_params[idx];
2604
25.6k
        lr->type += idx;
2605
25.6k
        lr->sgr_weights[0] = sgr_params[0] ? dav1d_msac_decode_subexp(&ts->msac,
2606
20.6k
            lr_ref->sgr_weights[0] + 96, 128, 4) - 96 : 0;
2607
25.6k
        lr->sgr_weights[1] = sgr_params[1] ? dav1d_msac_decode_subexp(&ts->msac,
2608
16.7k
            lr_ref->sgr_weights[1] + 32, 128, 4) - 32 : 95;
2609
25.6k
        memcpy(lr->filter_v, lr_ref->filter_v, sizeof(lr->filter_v));
2610
25.6k
        memcpy(lr->filter_h, lr_ref->filter_h, sizeof(lr->filter_h));
2611
25.6k
        ts->lr_ref[p] = lr;
2612
25.6k
        if (DEBUG_BLOCK_INFO)
2613
0
            printf("Post-lr_sgrproj[pl=%d,idx=%d,w[%d,%d]]: r=%d\n",
2614
0
                   p, idx, lr->sgr_weights[0],
2615
0
                   lr->sgr_weights[1], ts->msac.rng);
2616
25.6k
    }
2617
86.0k
}
2618
2619
// modeled after the equivalent function in aomdec:decodeframe.c
2620
0
static int check_trailing_bits_after_symbol_coder(const MsacContext *const msac) {
2621
    // check marker bit (single 1), followed by zeroes
2622
0
    const int n_bits = -(msac->cnt + 14);
2623
0
    assert(n_bits <= 0); // this assumes we errored out when cnt <= -15 in caller
2624
0
    const int n_bytes = (n_bits + 7) >> 3;
2625
0
    const uint8_t *p = &msac->buf_pos[n_bytes];
2626
0
    const int pattern = 128 >> ((n_bits - 1) & 7);
2627
0
    if ((p[-1] & (2 * pattern - 1)) != pattern)
2628
0
        return 1;
2629
2630
    // check remainder zero bytes
2631
0
    for (; p < msac->buf_end; p++)
2632
0
        if (*p)
2633
0
            return 1;
2634
2635
0
    return 0;
2636
0
}
2637
2638
258k
int dav1d_decode_tile_sbrow(Dav1dTaskContext *const t) {
2639
258k
    const Dav1dFrameContext *const f = t->f;
2640
258k
    const enum BlockLevel root_bl = f->seq_hdr->sb128 ? BL_128X128 : BL_64X64;
2641
258k
    Dav1dTileState *const ts = t->ts;
2642
258k
    const Dav1dContext *const c = f->c;
2643
258k
    const int sb_step = f->sb_step;
2644
258k
    const int tile_row = ts->tiling.row, tile_col = ts->tiling.col;
2645
258k
    const int col_sb_start = f->frame_hdr->tiling.col_start_sb[tile_col];
2646
258k
    const int col_sb128_start = col_sb_start >> !f->seq_hdr->sb128;
2647
2648
258k
    if (IS_INTER_OR_SWITCH(f->frame_hdr) || f->frame_hdr->allow_intrabc) {
2649
87.2k
        dav1d_refmvs_tile_sbrow_init(&t->rt, &f->rf, ts->tiling.col_start,
2650
87.2k
                                     ts->tiling.col_end, ts->tiling.row_start,
2651
87.2k
                                     ts->tiling.row_end, t->by >> f->sb_shift,
2652
87.2k
                                     ts->tiling.row, t->frame_thread.pass);
2653
87.2k
    }
2654
2655
258k
    if (IS_INTER_OR_SWITCH(f->frame_hdr) && c->n_fc > 1) {
2656
24.8k
        const int sby = (t->by - ts->tiling.row_start) >> f->sb_shift;
2657
24.8k
        int (*const lowest_px)[2] = ts->lowest_pixel[sby];
2658
198k
        for (int n = 0; n < 7; n++)
2659
521k
            for (int m = 0; m < 2; m++)
2660
347k
                lowest_px[n][m] = INT_MIN;
2661
24.8k
    }
2662
2663
258k
    reset_context(&t->l, IS_KEY_OR_INTRA(f->frame_hdr), t->frame_thread.pass);
2664
258k
    if (t->frame_thread.pass == 2) {
2665
118k
        const int off_2pass = c->n_tc > 1 ? f->sb128w * f->frame_hdr->tiling.rows : 0;
2666
118k
        for (t->bx = ts->tiling.col_start,
2667
118k
             t->a = f->a + off_2pass + col_sb128_start + tile_row * f->sb128w;
2668
301k
             t->bx < ts->tiling.col_end; t->bx += sb_step)
2669
183k
        {
2670
183k
            if (atomic_load_explicit(c->flush, memory_order_relaxed))
2671
2
                return 1;
2672
183k
            if (decode_sb(t, root_bl, dav1d_intra_edge_tree[root_bl]))
2673
0
                return 1;
2674
183k
            if (t->bx & 16 || f->seq_hdr->sb128)
2675
66.0k
                t->a++;
2676
183k
        }
2677
118k
        f->bd_fn.backup_ipred_edge(t);
2678
118k
        return 0;
2679
118k
    }
2680
2681
140k
    if (f->c->n_tc > 1 && f->frame_hdr->use_ref_frame_mvs) {
2682
3.03k
        f->c->refmvs_dsp.load_tmvs(&f->rf, ts->tiling.row,
2683
3.03k
                                   ts->tiling.col_start >> 1, ts->tiling.col_end >> 1,
2684
3.03k
                                   t->by >> 1, (t->by + sb_step) >> 1);
2685
3.03k
    }
2686
140k
    memset(t->pal_sz_uv[1], 0, sizeof(*t->pal_sz_uv));
2687
140k
    const int sb128y = t->by >> 5;
2688
140k
    for (t->bx = ts->tiling.col_start, t->a = f->a + col_sb128_start + tile_row * f->sb128w,
2689
140k
         t->lf_mask = f->lf.mask + sb128y * f->sb128w + col_sb128_start;
2690
442k
         t->bx < ts->tiling.col_end; t->bx += sb_step)
2691
302k
    {
2692
302k
        if (atomic_load_explicit(c->flush, memory_order_relaxed))
2693
23
            return 1;
2694
302k
        if (root_bl == BL_128X128) {
2695
48.9k
            t->cur_sb_cdef_idx_ptr = t->lf_mask->cdef_idx;
2696
48.9k
            t->cur_sb_cdef_idx_ptr[0] = -1;
2697
48.9k
            t->cur_sb_cdef_idx_ptr[1] = -1;
2698
48.9k
            t->cur_sb_cdef_idx_ptr[2] = -1;
2699
48.9k
            t->cur_sb_cdef_idx_ptr[3] = -1;
2700
253k
        } else {
2701
253k
            t->cur_sb_cdef_idx_ptr =
2702
253k
                &t->lf_mask->cdef_idx[((t->bx & 16) >> 4) +
2703
253k
                                      ((t->by & 16) >> 3)];
2704
253k
            t->cur_sb_cdef_idx_ptr[0] = -1;
2705
253k
        }
2706
        // Restoration filter
2707
1.21M
        for (int p = 0; p < 3; p++) {
2708
907k
            if (!((f->lf.restore_planes >> p) & 1U))
2709
788k
                continue;
2710
2711
119k
            const int ss_ver = p && f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
2712
119k
            const int ss_hor = p && f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
2713
119k
            const int unit_size_log2 = f->frame_hdr->restoration.unit_size[!!p];
2714
119k
            const int y = t->by * 4 >> ss_ver;
2715
119k
            const int h = (f->cur.p.h + ss_ver) >> ss_ver;
2716
2717
119k
            const int unit_size = 1 << unit_size_log2;
2718
119k
            const unsigned mask = unit_size - 1;
2719
119k
            if (y & mask) continue;
2720
95.5k
            const int half_unit = unit_size >> 1;
2721
            // Round half up at frame boundaries, if there's more than one
2722
            // restoration unit
2723
95.5k
            if (y && y + half_unit > h) continue;
2724
2725
93.1k
            const enum Dav1dRestorationType frame_type = f->frame_hdr->restoration.type[p];
2726
2727
93.1k
            if (f->frame_hdr->width[0] != f->frame_hdr->width[1]) {
2728
20.1k
                const int w = (f->sr_cur.p.p.w + ss_hor) >> ss_hor;
2729
20.1k
                const int n_units = imax(1, (w + half_unit) >> unit_size_log2);
2730
2731
20.1k
                const int d = f->frame_hdr->super_res.width_scale_denominator;
2732
20.1k
                const int rnd = unit_size * 8 - 1, shift = unit_size_log2 + 3;
2733
20.1k
                const int x0 = ((4 *  t->bx            * d >> ss_hor) + rnd) >> shift;
2734
20.1k
                const int x1 = ((4 * (t->bx + sb_step) * d >> ss_hor) + rnd) >> shift;
2735
2736
46.2k
                for (int x = x0; x < imin(x1, n_units); x++) {
2737
26.1k
                    const int px_x = x << (unit_size_log2 + ss_hor);
2738
26.1k
                    const int sb_idx = (t->by >> 5) * f->sr_sb128w + (px_x >> 7);
2739
26.1k
                    const int unit_idx = ((t->by & 16) >> 3) + ((px_x & 64) >> 6);
2740
26.1k
                    Av1RestorationUnit *const lr = &f->lf.lr_mask[sb_idx].lr[p][unit_idx];
2741
2742
26.1k
                    read_restoration_info(t, lr, p, frame_type);
2743
26.1k
                }
2744
72.9k
            } else {
2745
72.9k
                const int x = 4 * t->bx >> ss_hor;
2746
72.9k
                if (x & mask) continue;
2747
61.0k
                const int w = (f->cur.p.w + ss_hor) >> ss_hor;
2748
                // Round half up at frame boundaries, if there's more than one
2749
                // restoration unit
2750
61.0k
                if (x && x + half_unit > w) continue;
2751
59.8k
                const int sb_idx = (t->by >> 5) * f->sr_sb128w + (t->bx >> 5);
2752
59.8k
                const int unit_idx = ((t->by & 16) >> 3) + ((t->bx & 16) >> 4);
2753
59.8k
                Av1RestorationUnit *const lr = &f->lf.lr_mask[sb_idx].lr[p][unit_idx];
2754
2755
59.8k
                read_restoration_info(t, lr, p, frame_type);
2756
59.8k
            }
2757
93.1k
        }
2758
302k
        if (decode_sb(t, root_bl, dav1d_intra_edge_tree[root_bl]))
2759
369
            return 1;
2760
302k
        if (t->bx & 16 || f->seq_hdr->sb128) {
2761
123k
            t->a++;
2762
123k
            t->lf_mask++;
2763
123k
        }
2764
302k
    }
2765
2766
139k
    if (f->seq_hdr->ref_frame_mvs && f->c->n_tc > 1 && IS_INTER_OR_SWITCH(f->frame_hdr)) {
2767
7.88k
        dav1d_refmvs_save_tmvs(&f->c->refmvs_dsp, &t->rt,
2768
7.88k
                               ts->tiling.col_start >> 1, ts->tiling.col_end >> 1,
2769
7.88k
                               t->by >> 1, (t->by + sb_step) >> 1);
2770
7.88k
    }
2771
2772
    // backup pre-loopfilter pixels for intra prediction of the next sbrow
2773
139k
    if (t->frame_thread.pass != 1)
2774
0
        f->bd_fn.backup_ipred_edge(t);
2775
2776
    // backup t->a/l.tx_lpf_y/uv at tile boundaries to use them to "fix"
2777
    // up the initial value in neighbour tiles when running the loopfilter
2778
139k
    int align_h = (f->bh + 31) & ~31;
2779
139k
    memcpy(&f->lf.tx_lpf_right_edge[0][align_h * tile_col + t->by],
2780
139k
           &t->l.tx_lpf_y[t->by & 16], sb_step);
2781
139k
    const int ss_ver = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
2782
139k
    align_h >>= ss_ver;
2783
139k
    memcpy(&f->lf.tx_lpf_right_edge[1][align_h * tile_col + (t->by >> ss_ver)],
2784
139k
           &t->l.tx_lpf_uv[(t->by & 16) >> ss_ver], sb_step >> ss_ver);
2785
2786
    // error out on symbol decoder overread
2787
139k
    if (ts->msac.cnt <= -15) return 1;
2788
2789
134k
    return c->strict_std_compliance &&
2790
0
           (t->by >> f->sb_shift) + 1 >= f->frame_hdr->tiling.row_start_sb[tile_row + 1] &&
2791
0
           check_trailing_bits_after_symbol_coder(&ts->msac);
2792
139k
}
2793
2794
30.8k
int dav1d_decode_frame_init(Dav1dFrameContext *const f) {
2795
30.8k
    const Dav1dContext *const c = f->c;
2796
30.8k
    int retval = DAV1D_ERR(ENOMEM);
2797
2798
30.8k
    if (f->sbh > f->lf.start_of_tile_row_sz) {
2799
30.4k
        dav1d_free(f->lf.start_of_tile_row);
2800
30.4k
        f->lf.start_of_tile_row = dav1d_malloc(ALLOC_TILE, f->sbh * sizeof(uint8_t));
2801
30.4k
        if (!f->lf.start_of_tile_row) {
2802
0
            f->lf.start_of_tile_row_sz = 0;
2803
0
            goto error;
2804
0
        }
2805
30.4k
        f->lf.start_of_tile_row_sz = f->sbh;
2806
30.4k
    }
2807
30.8k
    int sby = 0;
2808
64.7k
    for (int tile_row = 0; tile_row < f->frame_hdr->tiling.rows; tile_row++) {
2809
33.8k
        f->lf.start_of_tile_row[sby++] = tile_row;
2810
208k
        while (sby < f->frame_hdr->tiling.row_start_sb[tile_row + 1])
2811
174k
            f->lf.start_of_tile_row[sby++] = 0;
2812
33.8k
    }
2813
2814
30.8k
    const int n_ts = f->frame_hdr->tiling.cols * f->frame_hdr->tiling.rows;
2815
30.8k
    if (n_ts != f->n_ts) {
2816
30.4k
        if (c->n_fc > 1) {
2817
30.4k
            dav1d_free(f->frame_thread.tile_start_off);
2818
30.4k
            f->frame_thread.tile_start_off =
2819
30.4k
                dav1d_malloc(ALLOC_TILE, sizeof(*f->frame_thread.tile_start_off) * n_ts);
2820
30.4k
            if (!f->frame_thread.tile_start_off) {
2821
0
                f->n_ts = 0;
2822
0
                goto error;
2823
0
            }
2824
30.4k
        }
2825
30.4k
        dav1d_free_aligned(f->ts);
2826
30.4k
        f->ts = dav1d_alloc_aligned(ALLOC_TILE, sizeof(*f->ts) * n_ts, 32);
2827
30.4k
        if (!f->ts) {
2828
0
            f->n_ts = 0;
2829
0
            goto error;
2830
0
        }
2831
30.4k
        f->n_ts = n_ts;
2832
30.4k
    }
2833
2834
30.8k
    const int a_sz = f->sb128w * f->frame_hdr->tiling.rows * (1 + (c->n_fc > 1 && c->n_tc > 1));
2835
30.8k
    if (a_sz != f->a_sz) {
2836
30.4k
        dav1d_free_aligned(f->a);
2837
30.4k
        f->a = dav1d_alloc_aligned(ALLOC_TILE, sizeof(*f->a) * a_sz, 16);
2838
30.4k
        if (!f->a) {
2839
0
            f->a_sz = 0;
2840
0
            goto error;
2841
0
        }
2842
30.4k
        f->a_sz = a_sz;
2843
30.4k
    }
2844
2845
30.8k
    const int num_sb128 = f->sb128w * f->sb128h;
2846
30.8k
    const uint8_t *const size_mul = ss_size_mul[f->cur.p.layout];
2847
30.8k
    const int hbd = !!f->seq_hdr->hbd;
2848
30.8k
    if (c->n_fc > 1) {
2849
30.8k
        const unsigned sb_step4 = f->sb_step * 4;
2850
30.8k
        int tile_idx = 0;
2851
64.7k
        for (int tile_row = 0; tile_row < f->frame_hdr->tiling.rows; tile_row++) {
2852
33.8k
            const unsigned row_off = f->frame_hdr->tiling.row_start_sb[tile_row] *
2853
33.8k
                                     sb_step4 * f->sb128w * 128;
2854
33.8k
            const unsigned b_diff = (f->frame_hdr->tiling.row_start_sb[tile_row + 1] -
2855
33.8k
                                     f->frame_hdr->tiling.row_start_sb[tile_row]) * sb_step4;
2856
71.8k
            for (int tile_col = 0; tile_col < f->frame_hdr->tiling.cols; tile_col++) {
2857
37.9k
                f->frame_thread.tile_start_off[tile_idx++] = row_off + b_diff *
2858
37.9k
                    f->frame_hdr->tiling.col_start_sb[tile_col] * sb_step4;
2859
37.9k
            }
2860
33.8k
        }
2861
2862
30.8k
        const int lowest_pixel_mem_sz = f->frame_hdr->tiling.cols * f->sbh;
2863
30.8k
        if (lowest_pixel_mem_sz != f->tile_thread.lowest_pixel_mem_sz) {
2864
30.4k
            dav1d_free(f->tile_thread.lowest_pixel_mem);
2865
30.4k
            f->tile_thread.lowest_pixel_mem =
2866
30.4k
                dav1d_malloc(ALLOC_TILE, lowest_pixel_mem_sz *
2867
30.4k
                             sizeof(*f->tile_thread.lowest_pixel_mem));
2868
30.4k
            if (!f->tile_thread.lowest_pixel_mem) {
2869
0
                f->tile_thread.lowest_pixel_mem_sz = 0;
2870
0
                goto error;
2871
0
            }
2872
30.4k
            f->tile_thread.lowest_pixel_mem_sz = lowest_pixel_mem_sz;
2873
30.4k
        }
2874
30.8k
        int (*lowest_pixel_ptr)[7][2] = f->tile_thread.lowest_pixel_mem;
2875
64.7k
        for (int tile_row = 0, tile_row_base = 0; tile_row < f->frame_hdr->tiling.rows;
2876
33.8k
             tile_row++, tile_row_base += f->frame_hdr->tiling.cols)
2877
33.8k
        {
2878
33.8k
            const int tile_row_sb_h = f->frame_hdr->tiling.row_start_sb[tile_row + 1] -
2879
33.8k
                                      f->frame_hdr->tiling.row_start_sb[tile_row];
2880
71.8k
            for (int tile_col = 0; tile_col < f->frame_hdr->tiling.cols; tile_col++) {
2881
37.9k
                f->ts[tile_row_base + tile_col].lowest_pixel = lowest_pixel_ptr;
2882
37.9k
                lowest_pixel_ptr += tile_row_sb_h;
2883
37.9k
            }
2884
33.8k
        }
2885
2886
30.8k
        const int cbi_sz = num_sb128 * size_mul[0];
2887
30.8k
        if (cbi_sz != f->frame_thread.cbi_sz) {
2888
30.4k
            dav1d_free_aligned(f->frame_thread.cbi);
2889
30.4k
            f->frame_thread.cbi =
2890
30.4k
                dav1d_alloc_aligned(ALLOC_BLOCK, sizeof(*f->frame_thread.cbi) *
2891
30.4k
                                    cbi_sz * 32 * 32 / 4, 64);
2892
30.4k
            if (!f->frame_thread.cbi) {
2893
0
                f->frame_thread.cbi_sz = 0;
2894
0
                goto error;
2895
0
            }
2896
30.4k
            f->frame_thread.cbi_sz = cbi_sz;
2897
30.4k
        }
2898
2899
30.8k
        const int cf_sz = (num_sb128 * size_mul[0]) << hbd;
2900
30.8k
        if (cf_sz != f->frame_thread.cf_sz) {
2901
30.4k
            dav1d_free_aligned(f->frame_thread.cf);
2902
30.4k
            f->frame_thread.cf =
2903
30.4k
                dav1d_alloc_aligned(ALLOC_COEF, (size_t)cf_sz * 128 * 128 / 2, 64);
2904
30.4k
            if (!f->frame_thread.cf) {
2905
0
                f->frame_thread.cf_sz = 0;
2906
0
                goto error;
2907
0
            }
2908
30.4k
            memset(f->frame_thread.cf, 0, (size_t)cf_sz * 128 * 128 / 2);
2909
30.4k
            f->frame_thread.cf_sz = cf_sz;
2910
30.4k
        }
2911
2912
30.8k
        if (f->frame_hdr->allow_screen_content_tools) {
2913
19.2k
            const int pal_sz = num_sb128 << hbd;
2914
19.2k
            if (pal_sz != f->frame_thread.pal_sz) {
2915
19.1k
                dav1d_free_aligned(f->frame_thread.pal);
2916
19.1k
                f->frame_thread.pal =
2917
19.1k
                    dav1d_alloc_aligned(ALLOC_PAL, sizeof(*f->frame_thread.pal) *
2918
19.1k
                                        pal_sz * 16 * 16, 64);
2919
19.1k
                if (!f->frame_thread.pal) {
2920
0
                    f->frame_thread.pal_sz = 0;
2921
0
                    goto error;
2922
0
                }
2923
19.1k
                f->frame_thread.pal_sz = pal_sz;
2924
19.1k
            }
2925
2926
19.2k
            const int pal_idx_sz = num_sb128 * size_mul[1];
2927
19.2k
            if (pal_idx_sz != f->frame_thread.pal_idx_sz) {
2928
19.1k
                dav1d_free_aligned(f->frame_thread.pal_idx);
2929
19.1k
                f->frame_thread.pal_idx =
2930
19.1k
                    dav1d_alloc_aligned(ALLOC_PAL, sizeof(*f->frame_thread.pal_idx) *
2931
19.1k
                                        pal_idx_sz * 128 * 128 / 8, 64);
2932
19.1k
                if (!f->frame_thread.pal_idx) {
2933
0
                    f->frame_thread.pal_idx_sz = 0;
2934
0
                    goto error;
2935
0
                }
2936
19.1k
                f->frame_thread.pal_idx_sz = pal_idx_sz;
2937
19.1k
            }
2938
19.2k
        } else if (f->frame_thread.pal) {
2939
0
            dav1d_freep_aligned(&f->frame_thread.pal);
2940
0
            dav1d_freep_aligned(&f->frame_thread.pal_idx);
2941
0
            f->frame_thread.pal_sz = f->frame_thread.pal_idx_sz = 0;
2942
0
        }
2943
30.8k
    }
2944
2945
    // update allocation of block contexts for above
2946
30.8k
    ptrdiff_t y_stride = f->cur.stride[0], uv_stride = f->cur.stride[1];
2947
30.8k
    const int has_resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
2948
30.8k
    const int need_cdef_lpf_copy = c->n_tc > 1 && has_resize;
2949
30.8k
    if (y_stride * f->sbh * 4 != f->lf.cdef_buf_plane_sz[0] ||
2950
417
        uv_stride * f->sbh * 8 != f->lf.cdef_buf_plane_sz[1] ||
2951
413
        need_cdef_lpf_copy != f->lf.need_cdef_lpf_copy ||
2952
409
        f->sbh != f->lf.cdef_buf_sbh)
2953
30.4k
    {
2954
30.4k
        dav1d_free_aligned(f->lf.cdef_line_buf);
2955
30.4k
        size_t alloc_sz = 64;
2956
30.4k
        alloc_sz += (size_t)llabs(y_stride) * 4 * f->sbh << need_cdef_lpf_copy;
2957
30.4k
        alloc_sz += (size_t)llabs(uv_stride) * 8 * f->sbh << need_cdef_lpf_copy;
2958
30.4k
        uint8_t *ptr = f->lf.cdef_line_buf = dav1d_alloc_aligned(ALLOC_CDEF, alloc_sz, 32);
2959
30.4k
        if (!ptr) {
2960
0
            f->lf.cdef_buf_plane_sz[0] = f->lf.cdef_buf_plane_sz[1] = 0;
2961
0
            goto error;
2962
0
        }
2963
2964
30.4k
        ptr += 32;
2965
30.4k
        if (y_stride < 0) {
2966
0
            f->lf.cdef_line[0][0] = ptr - y_stride * (f->sbh * 4 - 1);
2967
0
            f->lf.cdef_line[1][0] = ptr - y_stride * (f->sbh * 4 - 3);
2968
30.4k
        } else {
2969
30.4k
            f->lf.cdef_line[0][0] = ptr + y_stride * 0;
2970
30.4k
            f->lf.cdef_line[1][0] = ptr + y_stride * 2;
2971
30.4k
        }
2972
30.4k
        ptr += llabs(y_stride) * f->sbh * 4;
2973
30.4k
        if (uv_stride < 0) {
2974
0
            f->lf.cdef_line[0][1] = ptr - uv_stride * (f->sbh * 8 - 1);
2975
0
            f->lf.cdef_line[0][2] = ptr - uv_stride * (f->sbh * 8 - 3);
2976
0
            f->lf.cdef_line[1][1] = ptr - uv_stride * (f->sbh * 8 - 5);
2977
0
            f->lf.cdef_line[1][2] = ptr - uv_stride * (f->sbh * 8 - 7);
2978
30.4k
        } else {
2979
30.4k
            f->lf.cdef_line[0][1] = ptr + uv_stride * 0;
2980
30.4k
            f->lf.cdef_line[0][2] = ptr + uv_stride * 2;
2981
30.4k
            f->lf.cdef_line[1][1] = ptr + uv_stride * 4;
2982
30.4k
            f->lf.cdef_line[1][2] = ptr + uv_stride * 6;
2983
30.4k
        }
2984
2985
30.4k
        if (need_cdef_lpf_copy) {
2986
5.62k
            ptr += llabs(uv_stride) * f->sbh * 8;
2987
5.62k
            if (y_stride < 0)
2988
0
                f->lf.cdef_lpf_line[0] = ptr - y_stride * (f->sbh * 4 - 1);
2989
5.62k
            else
2990
5.62k
                f->lf.cdef_lpf_line[0] = ptr;
2991
5.62k
            ptr += llabs(y_stride) * f->sbh * 4;
2992
5.62k
            if (uv_stride < 0) {
2993
0
                f->lf.cdef_lpf_line[1] = ptr - uv_stride * (f->sbh * 4 - 1);
2994
0
                f->lf.cdef_lpf_line[2] = ptr - uv_stride * (f->sbh * 8 - 1);
2995
5.62k
            } else {
2996
5.62k
                f->lf.cdef_lpf_line[1] = ptr;
2997
5.62k
                f->lf.cdef_lpf_line[2] = ptr + uv_stride * f->sbh * 4;
2998
5.62k
            }
2999
5.62k
        }
3000
3001
30.4k
        f->lf.cdef_buf_plane_sz[0] = (int) y_stride * f->sbh * 4;
3002
30.4k
        f->lf.cdef_buf_plane_sz[1] = (int) uv_stride * f->sbh * 8;
3003
30.4k
        f->lf.need_cdef_lpf_copy = need_cdef_lpf_copy;
3004
30.4k
        f->lf.cdef_buf_sbh = f->sbh;
3005
30.4k
    }
3006
3007
30.8k
    const int sb128 = f->seq_hdr->sb128;
3008
30.8k
    const int num_lines = c->n_tc > 1 ? f->sbh * 4 << sb128 : 12;
3009
30.8k
    y_stride = f->sr_cur.p.stride[0], uv_stride = f->sr_cur.p.stride[1];
3010
30.8k
    if (y_stride * num_lines != f->lf.lr_buf_plane_sz[0] ||
3011
417
        uv_stride * num_lines * 2 != f->lf.lr_buf_plane_sz[1])
3012
30.4k
    {
3013
30.4k
        dav1d_free_aligned(f->lf.lr_line_buf);
3014
        // lr simd may overread the input, so slightly over-allocate the lpf buffer
3015
30.4k
        size_t alloc_sz = 128;
3016
30.4k
        alloc_sz += (size_t)llabs(y_stride) * num_lines;
3017
30.4k
        alloc_sz += (size_t)llabs(uv_stride) * num_lines * 2;
3018
30.4k
        uint8_t *ptr = f->lf.lr_line_buf = dav1d_alloc_aligned(ALLOC_LR, alloc_sz, 64);
3019
30.4k
        if (!ptr) {
3020
0
            f->lf.lr_buf_plane_sz[0] = f->lf.lr_buf_plane_sz[1] = 0;
3021
0
            goto error;
3022
0
        }
3023
3024
30.4k
        ptr += 64;
3025
30.4k
        if (y_stride < 0)
3026
0
            f->lf.lr_lpf_line[0] = ptr - y_stride * (num_lines - 1);
3027
30.4k
        else
3028
30.4k
            f->lf.lr_lpf_line[0] = ptr;
3029
30.4k
        ptr += llabs(y_stride) * num_lines;
3030
30.4k
        if (uv_stride < 0) {
3031
0
            f->lf.lr_lpf_line[1] = ptr - uv_stride * (num_lines * 1 - 1);
3032
0
            f->lf.lr_lpf_line[2] = ptr - uv_stride * (num_lines * 2 - 1);
3033
30.4k
        } else {
3034
30.4k
            f->lf.lr_lpf_line[1] = ptr;
3035
30.4k
            f->lf.lr_lpf_line[2] = ptr + uv_stride * num_lines;
3036
30.4k
        }
3037
3038
30.4k
        f->lf.lr_buf_plane_sz[0] = (int) y_stride * num_lines;
3039
30.4k
        f->lf.lr_buf_plane_sz[1] = (int) uv_stride * num_lines * 2;
3040
30.4k
    }
3041
3042
    // update allocation for loopfilter masks
3043
30.8k
    if (num_sb128 != f->lf.mask_sz) {
3044
30.4k
        dav1d_free(f->lf.mask);
3045
30.4k
        dav1d_free_aligned(f->lf.level[0]);
3046
30.4k
        f->lf.mask = dav1d_malloc(ALLOC_LF, sizeof(*f->lf.mask) * num_sb128);
3047
        // over-allocate by 3 bytes since some of the SIMD implementations
3048
        // index these from the level type and can thus over-read by up to 3
3049
30.4k
        size_t level_bytes = sizeof(*f->lf.level[0]) * num_sb128 * 32 * 32;
3050
30.4k
        f->lf.level[0] = dav1d_alloc_aligned(ALLOC_LF, 2 * level_bytes + 3, 64);
3051
30.4k
        if (!f->lf.mask || !f->lf.level[0]) {
3052
0
            f->lf.mask_sz = 0;
3053
0
            goto error;
3054
0
        }
3055
30.4k
        f->lf.level[1] = (void*)((uint8_t*)f->lf.level[0] + level_bytes);
3056
30.4k
        if (c->n_fc > 1) {
3057
30.4k
            dav1d_free(f->frame_thread.b);
3058
30.4k
            f->frame_thread.b = dav1d_malloc(ALLOC_BLOCK, sizeof(*f->frame_thread.b) *
3059
30.4k
                                             num_sb128 * 32 * 32);
3060
30.4k
            if (!f->frame_thread.b) {
3061
0
                f->lf.mask_sz = 0;
3062
0
                goto error;
3063
0
            }
3064
30.4k
        }
3065
30.4k
        f->lf.mask_sz = num_sb128;
3066
30.4k
    }
3067
3068
30.8k
    f->sr_sb128w = (f->sr_cur.p.p.w + 127) >> 7;
3069
30.8k
    const int lr_mask_sz = f->sr_sb128w * f->sb128h;
3070
30.8k
    if (lr_mask_sz != f->lf.lr_mask_sz) {
3071
30.4k
        dav1d_free(f->lf.lr_mask);
3072
30.4k
        f->lf.lr_mask = dav1d_malloc(ALLOC_LR, sizeof(*f->lf.lr_mask) * lr_mask_sz);
3073
30.4k
        if (!f->lf.lr_mask) {
3074
0
            f->lf.lr_mask_sz = 0;
3075
0
            goto error;
3076
0
        }
3077
30.4k
        f->lf.lr_mask_sz = lr_mask_sz;
3078
30.4k
    }
3079
30.8k
    int cdef_planes = 0;
3080
30.8k
    if (c->inloop_filters & DAV1D_INLOOPFILTER_CDEF) {
3081
77.0k
        for (int i = 0; i < (1 << f->frame_hdr->cdef.n_bits); i++) {
3082
46.2k
            cdef_planes |= (!!f->frame_hdr->cdef.y_strength[i]) |
3083
46.2k
                           ((!!f->frame_hdr->cdef.uv_strength[i]) << 1);
3084
46.2k
        }
3085
30.8k
    }
3086
30.8k
    f->lf.cdef_planes = cdef_planes;
3087
30.8k
    f->lf.restore_planes =
3088
30.8k
        ((f->frame_hdr->restoration.type[0] != DAV1D_RESTORATION_NONE) << 0) +
3089
30.8k
        ((f->frame_hdr->restoration.type[1] != DAV1D_RESTORATION_NONE) << 1) +
3090
30.8k
        ((f->frame_hdr->restoration.type[2] != DAV1D_RESTORATION_NONE) << 2);
3091
30.8k
    if (f->frame_hdr->loopfilter.sharpness != f->lf.last_sharpness) {
3092
30.7k
        dav1d_calc_eih(&f->lf.lim_lut, f->frame_hdr->loopfilter.sharpness);
3093
30.7k
        f->lf.last_sharpness = f->frame_hdr->loopfilter.sharpness;
3094
30.7k
    }
3095
30.8k
    dav1d_calc_lf_values(f->lf.lvl, f->frame_hdr, (int8_t[4]) { 0, 0, 0, 0 });
3096
30.8k
    memset(f->lf.mask, 0, sizeof(*f->lf.mask) * num_sb128);
3097
3098
30.8k
    const int ipred_edge_sz = f->sbh * f->sb128w << hbd;
3099
30.8k
    if (ipred_edge_sz != f->ipred_edge_sz) {
3100
30.4k
        dav1d_free_aligned(f->ipred_edge[0]);
3101
30.4k
        uint8_t *ptr = f->ipred_edge[0] =
3102
30.4k
            dav1d_alloc_aligned(ALLOC_IPRED, ipred_edge_sz * 128 * 3, 64);
3103
30.4k
        if (!ptr) {
3104
0
            f->ipred_edge_sz = 0;
3105
0
            goto error;
3106
0
        }
3107
30.4k
        f->ipred_edge[1] = ptr + ipred_edge_sz * 128 * 1;
3108
30.4k
        f->ipred_edge[2] = ptr + ipred_edge_sz * 128 * 2;
3109
30.4k
        f->ipred_edge_sz = ipred_edge_sz;
3110
30.4k
    }
3111
3112
30.8k
    const int re_sz = f->sb128h * f->frame_hdr->tiling.cols;
3113
30.8k
    if (re_sz != f->lf.re_sz) {
3114
30.4k
        dav1d_free(f->lf.tx_lpf_right_edge[0]);
3115
30.4k
        f->lf.tx_lpf_right_edge[0] = dav1d_malloc(ALLOC_LF, re_sz * 32 * 2);
3116
30.4k
        if (!f->lf.tx_lpf_right_edge[0]) {
3117
0
            f->lf.re_sz = 0;
3118
0
            goto error;
3119
0
        }
3120
30.4k
        f->lf.tx_lpf_right_edge[1] = f->lf.tx_lpf_right_edge[0] + re_sz * 32;
3121
30.4k
        f->lf.re_sz = re_sz;
3122
30.4k
    }
3123
3124
    // init ref mvs
3125
30.8k
    if (IS_INTER_OR_SWITCH(f->frame_hdr) || f->frame_hdr->allow_intrabc) {
3126
15.0k
        const int ret =
3127
15.0k
            dav1d_refmvs_init_frame(&f->rf, f->seq_hdr, f->frame_hdr,
3128
15.0k
                                    f->refpoc, f->mvs, f->refrefpoc, f->ref_mvs,
3129
15.0k
                                    f->c->n_tc, f->c->n_fc);
3130
15.0k
        if (ret < 0) goto error;
3131
15.0k
    }
3132
3133
    // setup dequant tables
3134
30.8k
    init_quant_tables(f->seq_hdr, f->frame_hdr, f->frame_hdr->quant.yac, f->dq);
3135
30.8k
    if (f->frame_hdr->quant.qm)
3136
267k
        for (int i = 0; i < N_RECT_TX_SIZES; i++) {
3137
254k
            f->qm[i][0] = dav1d_qm_tbl[f->frame_hdr->quant.qm_y][0][i];
3138
254k
            f->qm[i][1] = dav1d_qm_tbl[f->frame_hdr->quant.qm_u][1][i];
3139
254k
            f->qm[i][2] = dav1d_qm_tbl[f->frame_hdr->quant.qm_v][1][i];
3140
254k
        }
3141
17.4k
    else
3142
17.4k
        memset(f->qm, 0, sizeof(f->qm));
3143
3144
    // setup jnt_comp weights
3145
30.8k
    if (f->frame_hdr->switchable_comp_refs) {
3146
45.2k
        for (int i = 0; i < 7; i++) {
3147
39.5k
            const unsigned ref0poc = f->refp[i].p.frame_hdr->frame_offset;
3148
3149
157k
            for (int j = i + 1; j < 7; j++) {
3150
118k
                const unsigned ref1poc = f->refp[j].p.frame_hdr->frame_offset;
3151
3152
118k
                const unsigned d1 =
3153
118k
                    imin(abs(get_poc_diff(f->seq_hdr->order_hint_n_bits, ref0poc,
3154
118k
                                          f->cur.frame_hdr->frame_offset)), 31);
3155
118k
                const unsigned d0 =
3156
118k
                    imin(abs(get_poc_diff(f->seq_hdr->order_hint_n_bits, ref1poc,
3157
118k
                                          f->cur.frame_hdr->frame_offset)), 31);
3158
118k
                const int order = d0 <= d1;
3159
3160
118k
                static const uint8_t quant_dist_weight[3][2] = {
3161
118k
                    { 2, 3 }, { 2, 5 }, { 2, 7 }
3162
118k
                };
3163
118k
                static const uint8_t quant_dist_lookup_table[4][2] = {
3164
118k
                    { 9, 7 }, { 11, 5 }, { 12, 4 }, { 13, 3 }
3165
118k
                };
3166
3167
118k
                int k;
3168
279k
                for (k = 0; k < 3; k++) {
3169
228k
                    const int c0 = quant_dist_weight[k][order];
3170
228k
                    const int c1 = quant_dist_weight[k][!order];
3171
228k
                    const int d0_c0 = d0 * c0;
3172
228k
                    const int d1_c1 = d1 * c1;
3173
228k
                    if ((d0 > d1 && d0_c0 < d1_c1) || (d0 <= d1 && d0_c0 > d1_c1)) break;
3174
228k
                }
3175
3176
118k
                f->jnt_weights[i][j] = quant_dist_lookup_table[k][order];
3177
118k
            }
3178
39.5k
        }
3179
5.65k
    }
3180
3181
    /* Init loopfilter pointers. Increasing NULL pointers is technically UB,
3182
     * so just point the chroma pointers in 4:0:0 to the luma plane here to
3183
     * avoid having additional in-loop branches in various places. We never
3184
     * dereference those pointers so it doesn't really matter what they
3185
     * point at, as long as the pointers are valid. */
3186
30.8k
    const int has_chroma = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I400;
3187
30.8k
    f->lf.p[0] = f->cur.data[0];
3188
30.8k
    f->lf.p[1] = f->cur.data[has_chroma ? 1 : 0];
3189
30.8k
    f->lf.p[2] = f->cur.data[has_chroma ? 2 : 0];
3190
30.8k
    f->lf.sr_p[0] = f->sr_cur.p.data[0];
3191
30.8k
    f->lf.sr_p[1] = f->sr_cur.p.data[has_chroma ? 1 : 0];
3192
30.8k
    f->lf.sr_p[2] = f->sr_cur.p.data[has_chroma ? 2 : 0];
3193
3194
30.8k
    retval = 0;
3195
30.8k
error:
3196
30.8k
    return retval;
3197
30.8k
}
3198
3199
30.1k
int dav1d_decode_frame_init_cdf(Dav1dFrameContext *const f) {
3200
30.1k
    const Dav1dContext *const c = f->c;
3201
30.1k
    int retval = DAV1D_ERR(EINVAL);
3202
3203
30.1k
    if (f->frame_hdr->refresh_context)
3204
8.95k
        dav1d_cdf_thread_copy(f->out_cdf.data.cdf, &f->in_cdf);
3205
3206
    // parse individual tiles per tile group
3207
30.1k
    int tile_row = 0, tile_col = 0;
3208
60.0k
    for (int i = 0; i < f->n_tile_data; i++) {
3209
30.1k
        const uint8_t *data = f->tile[i].data.data;
3210
30.1k
        size_t size = f->tile[i].data.sz;
3211
3212
64.7k
        for (int j = f->tile[i].start; j <= f->tile[i].end; j++) {
3213
34.8k
            size_t tile_sz;
3214
34.8k
            if (j == f->tile[i].end) {
3215
29.8k
                tile_sz = size;
3216
29.8k
            } else {
3217
4.99k
                if (f->frame_hdr->tiling.n_bytes > size) goto error;
3218
4.94k
                tile_sz = 0;
3219
10.8k
                for (unsigned k = 0; k < f->frame_hdr->tiling.n_bytes; k++)
3220
5.92k
                    tile_sz |= (unsigned)*data++ << (k * 8);
3221
4.94k
                tile_sz++;
3222
4.94k
                size -= f->frame_hdr->tiling.n_bytes;
3223
4.94k
                if (tile_sz > size) goto error;
3224
4.94k
            }
3225
3226
34.5k
            setup_tile(&f->ts[j], f, data, tile_sz, tile_row, tile_col++,
3227
18.4E
                       c->n_fc > 1 ? f->frame_thread.tile_start_off[j] : 0);
3228
3229
34.5k
            if (tile_col == f->frame_hdr->tiling.cols) {
3230
31.9k
                tile_col = 0;
3231
31.9k
                tile_row++;
3232
31.9k
            }
3233
34.5k
            data += tile_sz;
3234
34.5k
            size -= tile_sz;
3235
34.5k
        }
3236
30.1k
    }
3237
3238
29.8k
    if (c->n_tc > 1) {
3239
29.8k
        const int uses_2pass = c->n_fc > 1;
3240
257k
        for (int n = 0; n < f->sb128w * f->frame_hdr->tiling.rows * (1 + uses_2pass); n++)
3241
227k
            reset_context(&f->a[n], IS_KEY_OR_INTRA(f->frame_hdr),
3242
18.4E
                          uses_2pass ? 1 + (n >= f->sb128w * f->frame_hdr->tiling.rows) : 0);
3243
29.8k
    }
3244
3245
29.8k
    retval = 0;
3246
30.1k
error:
3247
30.1k
    return retval;
3248
29.8k
}
3249
3250
0
int dav1d_decode_frame_main(Dav1dFrameContext *const f) {
3251
0
    const Dav1dContext *const c = f->c;
3252
0
    int retval = DAV1D_ERR(EINVAL);
3253
3254
0
    assert(f->c->n_tc == 1);
3255
3256
0
    Dav1dTaskContext *const t = &c->tc[f - c->fc];
3257
0
    t->f = f;
3258
0
    t->frame_thread.pass = 0;
3259
3260
0
    for (int n = 0; n < f->sb128w * f->frame_hdr->tiling.rows; n++)
3261
0
        reset_context(&f->a[n], IS_KEY_OR_INTRA(f->frame_hdr), 0);
3262
3263
    // no threading - we explicitly interleave tile/sbrow decoding
3264
    // and post-filtering, so that the full process runs in-line
3265
0
    for (int tile_row = 0; tile_row < f->frame_hdr->tiling.rows; tile_row++) {
3266
0
        const int sbh_end =
3267
0
            imin(f->frame_hdr->tiling.row_start_sb[tile_row + 1], f->sbh);
3268
0
        for (int sby = f->frame_hdr->tiling.row_start_sb[tile_row];
3269
0
             sby < sbh_end; sby++)
3270
0
        {
3271
0
            t->by = sby << (4 + f->seq_hdr->sb128);
3272
0
            const int by_end = (t->by + f->sb_step) >> 1;
3273
0
            if (f->frame_hdr->use_ref_frame_mvs) {
3274
0
                f->c->refmvs_dsp.load_tmvs(&f->rf, tile_row,
3275
0
                                           0, f->bw >> 1, t->by >> 1, by_end);
3276
0
            }
3277
0
            for (int tile_col = 0; tile_col < f->frame_hdr->tiling.cols; tile_col++) {
3278
0
                t->ts = &f->ts[tile_row * f->frame_hdr->tiling.cols + tile_col];
3279
0
                if (dav1d_decode_tile_sbrow(t)) goto error;
3280
0
            }
3281
0
            if (IS_INTER_OR_SWITCH(f->frame_hdr)) {
3282
0
                dav1d_refmvs_save_tmvs(&f->c->refmvs_dsp, &t->rt,
3283
0
                                       0, f->bw >> 1, t->by >> 1, by_end);
3284
0
            }
3285
3286
            // loopfilter + cdef + restoration
3287
0
            f->bd_fn.filter_sbrow(f, sby);
3288
0
        }
3289
0
    }
3290
3291
0
    retval = 0;
3292
0
error:
3293
0
    return retval;
3294
0
}
3295
3296
158k
void dav1d_decode_frame_exit(Dav1dFrameContext *const f, int retval) {
3297
158k
    const Dav1dContext *const c = f->c;
3298
3299
158k
    if (f->sr_cur.p.data[0])
3300
158k
        atomic_init(&f->task_thread.error, 0);
3301
3302
158k
    if (c->n_fc > 1 && retval && f->frame_thread.cf) {
3303
36.4k
        memset(f->frame_thread.cf, 0,
3304
36.4k
               (size_t)f->frame_thread.cf_sz * 128 * 128 / 2);
3305
36.4k
    }
3306
1.26M
    for (int i = 0; i < 7; i++) {
3307
1.10M
        if (f->refp[i].p.frame_hdr) {
3308
71.0k
            if (!retval && c->n_fc > 1 && c->strict_std_compliance &&
3309
71.0k
                atomic_load_explicit(&f->refp[i].progress[1], memory_order_relaxed) == FRAME_ERROR)
3310
0
            {
3311
0
                retval = DAV1D_ERR(EINVAL);
3312
0
                atomic_store_explicit(&f->task_thread.error, 1, memory_order_relaxed);
3313
0
                atomic_store_explicit(&f->sr_cur.progress[1], FRAME_ERROR, memory_order_relaxed);
3314
0
            }
3315
71.0k
            dav1d_thread_picture_unref(&f->refp[i]);
3316
71.0k
        }
3317
1.10M
        dav1d_ref_dec(&f->ref_mvs_ref[i]);
3318
1.10M
    }
3319
3320
158k
    dav1d_picture_unref_internal(&f->cur);
3321
158k
    dav1d_thread_picture_unref(&f->sr_cur);
3322
158k
    dav1d_cdf_thread_unref(&f->in_cdf);
3323
158k
    if (f->frame_hdr && f->frame_hdr->refresh_context) {
3324
18.8k
        if (f->out_cdf.progress)
3325
18.8k
            atomic_store_explicit(f->out_cdf.progress, retval == 0 ? 1 : TILE_ERROR,
3326
9.80k
                                  memory_order_release);
3327
18.8k
        dav1d_cdf_thread_unref(&f->out_cdf);
3328
18.8k
    }
3329
158k
    dav1d_ref_dec(&f->cur_segmap_ref);
3330
158k
    dav1d_ref_dec(&f->prev_segmap_ref);
3331
158k
    dav1d_ref_dec(&f->mvs_ref);
3332
158k
    dav1d_ref_dec(&f->seq_hdr_ref);
3333
158k
    dav1d_ref_dec(&f->frame_hdr_ref);
3334
3335
190k
    for (int i = 0; i < f->n_tile_data; i++)
3336
31.7k
        dav1d_data_unref_internal(&f->tile[i].data);
3337
158k
    f->task_thread.retval = retval;
3338
158k
}
3339
3340
0
int dav1d_decode_frame(Dav1dFrameContext *const f) {
3341
0
    assert(f->c->n_fc == 1);
3342
    // if n_tc > 1 (but n_fc == 1), we could run init/exit in the task
3343
    // threads also. Not sure it makes a measurable difference.
3344
0
    int res = dav1d_decode_frame_init(f);
3345
0
    if (!res) res = dav1d_decode_frame_init_cdf(f);
3346
    // wait until all threads have completed
3347
0
    if (!res) {
3348
0
        if (f->c->n_tc > 1) {
3349
0
            res = dav1d_task_create_tile_sbrow(f, 0, 1);
3350
0
            pthread_mutex_lock(&f->task_thread.ttd->lock);
3351
0
            pthread_cond_signal(&f->task_thread.ttd->cond);
3352
0
            if (!res) {
3353
0
                while (!f->task_thread.done[0] ||
3354
0
                       f->task_thread.task_counter > 0)
3355
0
                {
3356
0
                    pthread_cond_wait(&f->task_thread.cond,
3357
0
                                      &f->task_thread.ttd->lock);
3358
0
                }
3359
0
            }
3360
0
            pthread_mutex_unlock(&f->task_thread.ttd->lock);
3361
0
            if (!res) res = f->task_thread.retval;
3362
0
        } else {
3363
0
            res = dav1d_decode_frame_main(f);
3364
0
            if (!res && f->frame_hdr->refresh_context) {
3365
0
                dav1d_cdf_thread_update(f->frame_hdr, f->out_cdf.data.cdf,
3366
0
                                        &f->ts[f->frame_hdr->tiling.update].cdf);
3367
0
            }
3368
0
        }
3369
0
    }
3370
0
    dav1d_decode_frame_exit(f, res);
3371
0
    res = f->task_thread.retval;
3372
0
    f->n_tile_data = 0;
3373
0
    return res;
3374
0
}
3375
3376
11.6k
static int get_upscale_x0(const int in_w, const int out_w, const int step) {
3377
11.6k
    const int err = out_w * step - (in_w << 14);
3378
11.6k
    const int x0 = (-((out_w - in_w) << 13) + (out_w >> 1)) / out_w + 128 - (err / 2);
3379
11.6k
    return x0 & 0x3fff;
3380
11.6k
}
3381
3382
31.8k
int dav1d_submit_frame(Dav1dContext *const c) {
3383
31.8k
    Dav1dFrameContext *f;
3384
31.8k
    int res = -1;
3385
3386
    // wait for c->out_delayed[next] and move into c->out if visible
3387
31.8k
    Dav1dThreadPicture *out_delayed;
3388
31.8k
    if (c->n_fc > 1) {
3389
31.8k
        pthread_mutex_lock(&c->task_thread.lock);
3390
31.8k
        const unsigned next = c->frame_thread.next++;
3391
31.8k
        if (c->frame_thread.next == c->n_fc)
3392
523
            c->frame_thread.next = 0;
3393
3394
31.8k
        f = &c->fc[next];
3395
32.2k
        while (f->n_tile_data > 0)
3396
359
            pthread_cond_wait(&f->task_thread.cond,
3397
359
                              &c->task_thread.lock);
3398
31.8k
        out_delayed = &c->frame_thread.out_delayed[next];
3399
31.8k
        if (out_delayed->p.data[0] ||
3400
31.8k
            atomic_load_explicit(&f->task_thread.error, memory_order_relaxed))
3401
433
        {
3402
433
            unsigned first = atomic_load_explicit(&c->task_thread.first, memory_order_relaxed);
3403
433
            if (first + 1U < c->n_fc)
3404
433
                atomic_fetch_add_explicit(&c->task_thread.first, 1U, memory_order_relaxed);
3405
0
            else
3406
433
                atomic_store_explicit(&c->task_thread.first, 0, memory_order_relaxed);
3407
433
            atomic_compare_exchange_strong_explicit(&c->task_thread.reset_task_cur,
3408
433
                                                    &first, UINT_MAX,
3409
433
                                                    memory_order_relaxed,
3410
433
                                                    memory_order_relaxed);
3411
433
            if (c->task_thread.cur && c->task_thread.cur < c->n_fc)
3412
242
                c->task_thread.cur--;
3413
433
        }
3414
31.8k
        const int error = f->task_thread.retval;
3415
31.8k
        if (error) {
3416
0
            f->task_thread.retval = 0;
3417
0
            c->cached_error = error;
3418
0
            dav1d_data_props_copy(&c->cached_error_props, &out_delayed->p.m);
3419
0
            dav1d_thread_picture_unref(out_delayed);
3420
31.8k
        } else if (out_delayed->p.data[0]) {
3421
433
            const unsigned progress = atomic_load_explicit(&out_delayed->progress[1],
3422
433
                                                           memory_order_relaxed);
3423
433
            if ((out_delayed->visible || c->output_invisible_frames) &&
3424
422
                progress != FRAME_ERROR)
3425
422
            {
3426
422
                dav1d_thread_picture_ref(&c->out, out_delayed);
3427
422
                c->event_flags |= dav1d_picture_get_event_flags(out_delayed);
3428
422
            }
3429
433
            dav1d_thread_picture_unref(out_delayed);
3430
433
        }
3431
31.8k
    } else {
3432
0
        f = c->fc;
3433
0
    }
3434
3435
31.8k
    f->seq_hdr = c->seq_hdr;
3436
31.8k
    f->seq_hdr_ref = c->seq_hdr_ref;
3437
31.8k
    dav1d_ref_inc(f->seq_hdr_ref);
3438
31.8k
    f->frame_hdr = c->frame_hdr;
3439
31.8k
    f->frame_hdr_ref = c->frame_hdr_ref;
3440
31.8k
    c->frame_hdr = NULL;
3441
31.8k
    c->frame_hdr_ref = NULL;
3442
31.8k
    f->dsp = &c->dsp[f->seq_hdr->hbd];
3443
3444
31.8k
    const int bpc = 8 + 2 * f->seq_hdr->hbd;
3445
3446
31.8k
    if (!f->dsp->ipred.intra_pred[DC_PRED]) {
3447
20.4k
        Dav1dDSPContext *const dsp = &c->dsp[f->seq_hdr->hbd];
3448
3449
20.4k
        switch (bpc) {
3450
0
#define assign_bitdepth_case(bd) \
3451
20.4k
            dav1d_cdef_dsp_init_##bd##bpc(&dsp->cdef); \
3452
20.4k
            dav1d_intra_pred_dsp_init_##bd##bpc(&dsp->ipred); \
3453
20.4k
            dav1d_itx_dsp_init_##bd##bpc(&dsp->itx, bpc); \
3454
20.4k
            dav1d_loop_filter_dsp_init_##bd##bpc(&dsp->lf); \
3455
20.4k
            dav1d_loop_restoration_dsp_init_##bd##bpc(&dsp->lr, bpc); \
3456
20.4k
            dav1d_mc_dsp_init_##bd##bpc(&dsp->mc); \
3457
20.4k
            dav1d_film_grain_dsp_init_##bd##bpc(&dsp->fg); \
3458
20.4k
            break
3459
0
#if CONFIG_8BPC
3460
9.31k
        case 8:
3461
9.31k
            assign_bitdepth_case(8);
3462
0
#endif
3463
0
#if CONFIG_16BPC
3464
8.54k
        case 10:
3465
11.1k
        case 12:
3466
11.1k
            assign_bitdepth_case(16);
3467
0
#endif
3468
0
#undef assign_bitdepth_case
3469
0
        default:
3470
0
            dav1d_log(c, "Compiled without support for %d-bit decoding\n",
3471
0
                    8 + 2 * f->seq_hdr->hbd);
3472
0
            res = DAV1D_ERR(ENOPROTOOPT);
3473
0
            goto error;
3474
20.4k
        }
3475
20.4k
    }
3476
3477
31.8k
#define assign_bitdepth_case(bd) \
3478
31.8k
        f->bd_fn.recon_b_inter = dav1d_recon_b_inter_##bd##bpc; \
3479
31.8k
        f->bd_fn.recon_b_intra = dav1d_recon_b_intra_##bd##bpc; \
3480
31.8k
        f->bd_fn.filter_sbrow = dav1d_filter_sbrow_##bd##bpc; \
3481
31.8k
        f->bd_fn.filter_sbrow_deblock_cols = dav1d_filter_sbrow_deblock_cols_##bd##bpc; \
3482
31.8k
        f->bd_fn.filter_sbrow_deblock_rows = dav1d_filter_sbrow_deblock_rows_##bd##bpc; \
3483
31.8k
        f->bd_fn.filter_sbrow_cdef = dav1d_filter_sbrow_cdef_##bd##bpc; \
3484
31.8k
        f->bd_fn.filter_sbrow_resize = dav1d_filter_sbrow_resize_##bd##bpc; \
3485
31.8k
        f->bd_fn.filter_sbrow_lr = dav1d_filter_sbrow_lr_##bd##bpc; \
3486
31.8k
        f->bd_fn.backup_ipred_edge = dav1d_backup_ipred_edge_##bd##bpc; \
3487
31.8k
        f->bd_fn.read_coef_blocks = dav1d_read_coef_blocks_##bd##bpc; \
3488
31.8k
        f->bd_fn.copy_pal_block_y = dav1d_copy_pal_block_y_##bd##bpc; \
3489
31.8k
        f->bd_fn.copy_pal_block_uv = dav1d_copy_pal_block_uv_##bd##bpc; \
3490
31.8k
        f->bd_fn.read_pal_plane = dav1d_read_pal_plane_##bd##bpc; \
3491
31.8k
        f->bd_fn.read_pal_uv = dav1d_read_pal_uv_##bd##bpc
3492
31.8k
    if (!f->seq_hdr->hbd) {
3493
16.4k
#if CONFIG_8BPC
3494
16.4k
        assign_bitdepth_case(8);
3495
16.4k
#endif
3496
16.4k
    } else {
3497
15.4k
#if CONFIG_16BPC
3498
15.4k
        assign_bitdepth_case(16);
3499
15.4k
#endif
3500
15.4k
    }
3501
31.8k
#undef assign_bitdepth_case
3502
3503
31.8k
    int ref_coded_width[7];
3504
31.8k
    if (IS_INTER_OR_SWITCH(f->frame_hdr)) {
3505
10.2k
        if (f->frame_hdr->primary_ref_frame != DAV1D_PRIMARY_REF_NONE) {
3506
9.15k
            const int pri_ref = f->frame_hdr->refidx[f->frame_hdr->primary_ref_frame];
3507
9.15k
            if (!c->refs[pri_ref].p.p.data[0]) {
3508
5
                res = DAV1D_ERR(EINVAL);
3509
5
                goto error;
3510
5
            }
3511
9.15k
        }
3512
81.3k
        for (int i = 0; i < 7; i++) {
3513
71.1k
            const int refidx = f->frame_hdr->refidx[i];
3514
71.1k
            if (!c->refs[refidx].p.p.data[0] ||
3515
71.1k
                f->frame_hdr->width[0] * 2 < c->refs[refidx].p.p.p.w ||
3516
71.1k
                f->frame_hdr->height * 2 < c->refs[refidx].p.p.p.h ||
3517
71.1k
                f->frame_hdr->width[0] > c->refs[refidx].p.p.p.w * 16 ||
3518
71.0k
                f->frame_hdr->height > c->refs[refidx].p.p.p.h * 16 ||
3519
71.0k
                f->seq_hdr->layout != c->refs[refidx].p.p.p.layout ||
3520
71.0k
                bpc != c->refs[refidx].p.p.p.bpc)
3521
62
            {
3522
133
                for (int j = 0; j < i; j++)
3523
71
                    dav1d_thread_picture_unref(&f->refp[j]);
3524
62
                res = DAV1D_ERR(EINVAL);
3525
62
                goto error;
3526
62
            }
3527
71.0k
            dav1d_thread_picture_ref(&f->refp[i], &c->refs[refidx].p);
3528
71.0k
            ref_coded_width[i] = c->refs[refidx].p.p.frame_hdr->width[0];
3529
71.0k
            if (f->frame_hdr->width[0] != c->refs[refidx].p.p.p.w ||
3530
50.3k
                f->frame_hdr->height != c->refs[refidx].p.p.p.h)
3531
21.8k
            {
3532
21.8k
#define scale_fac(ref_sz, this_sz) \
3533
55.3k
    ((((ref_sz) << 14) + ((this_sz) >> 1)) / (this_sz))
3534
21.8k
                f->svc[i][0].scale = scale_fac(c->refs[refidx].p.p.p.w,
3535
21.8k
                                               f->frame_hdr->width[0]);
3536
21.8k
                f->svc[i][1].scale = scale_fac(c->refs[refidx].p.p.p.h,
3537
21.8k
                                               f->frame_hdr->height);
3538
21.8k
                f->svc[i][0].step = (f->svc[i][0].scale + 8) >> 4;
3539
21.8k
                f->svc[i][1].step = (f->svc[i][1].scale + 8) >> 4;
3540
49.2k
            } else {
3541
49.2k
                f->svc[i][0].scale = f->svc[i][1].scale = 0;
3542
49.2k
            }
3543
71.0k
            f->gmv_warp_allowed[i] = f->frame_hdr->gmv[i].type > DAV1D_WM_TYPE_TRANSLATION &&
3544
16.1k
                                     !f->frame_hdr->force_integer_mv &&
3545
10.5k
                                     !dav1d_get_shear_params(&f->frame_hdr->gmv[i]) &&
3546
10.1k
                                     !f->svc[i][0].scale;
3547
71.0k
        }
3548
10.2k
    }
3549
3550
    // setup entropy
3551
31.7k
    if (f->frame_hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
3552
22.6k
        dav1d_cdf_thread_init_static(&f->in_cdf, f->frame_hdr->quant.yac);
3553
22.6k
    } else {
3554
9.10k
        const int pri_ref = f->frame_hdr->refidx[f->frame_hdr->primary_ref_frame];
3555
9.10k
        dav1d_cdf_thread_ref(&f->in_cdf, &c->cdf[pri_ref]);
3556
9.10k
    }
3557
31.7k
    if (f->frame_hdr->refresh_context) {
3558
9.80k
        res = dav1d_cdf_thread_alloc(c, &f->out_cdf, c->n_fc > 1);
3559
9.80k
        if (res < 0) goto error;
3560
9.80k
    }
3561
3562
    // FIXME qsort so tiles are in order (for frame threading)
3563
31.7k
    if (f->n_tile_data_alloc < c->n_tile_data) {
3564
31.3k
        dav1d_free(f->tile);
3565
31.3k
        assert(c->n_tile_data < INT_MAX / (int)sizeof(*f->tile));
3566
31.3k
        f->tile = dav1d_malloc(ALLOC_TILE, c->n_tile_data * sizeof(*f->tile));
3567
31.3k
        if (!f->tile) {
3568
0
            f->n_tile_data_alloc = f->n_tile_data = 0;
3569
0
            res = DAV1D_ERR(ENOMEM);
3570
0
            goto error;
3571
0
        }
3572
31.3k
        f->n_tile_data_alloc = c->n_tile_data;
3573
31.3k
    }
3574
31.7k
    memcpy(f->tile, c->tile, c->n_tile_data * sizeof(*f->tile));
3575
31.7k
    memset(c->tile, 0, c->n_tile_data * sizeof(*c->tile));
3576
31.7k
    f->n_tile_data = c->n_tile_data;
3577
31.7k
    c->n_tile_data = 0;
3578
3579
    // allocate frame
3580
31.7k
    res = dav1d_thread_picture_alloc(c, f, bpc);
3581
31.7k
    if (res < 0) goto error;
3582
3583
31.7k
    if (f->frame_hdr->width[0] != f->frame_hdr->width[1]) {
3584
5.82k
        res = dav1d_picture_alloc_copy(c, &f->cur, f->frame_hdr->width[0], &f->sr_cur.p);
3585
5.82k
        if (res < 0) goto error;
3586
25.9k
    } else {
3587
25.9k
        dav1d_picture_ref(&f->cur, &f->sr_cur.p);
3588
25.9k
    }
3589
3590
31.7k
    if (f->frame_hdr->width[0] != f->frame_hdr->width[1]) {
3591
5.82k
        f->resize_step[0] = scale_fac(f->cur.p.w, f->sr_cur.p.p.w);
3592
5.82k
        const int ss_hor = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
3593
5.82k
        const int in_cw = (f->cur.p.w + ss_hor) >> ss_hor;
3594
5.82k
        const int out_cw = (f->sr_cur.p.p.w + ss_hor) >> ss_hor;
3595
5.82k
        f->resize_step[1] = scale_fac(in_cw, out_cw);
3596
5.82k
#undef scale_fac
3597
5.82k
        f->resize_start[0] = get_upscale_x0(f->cur.p.w, f->sr_cur.p.p.w, f->resize_step[0]);
3598
5.82k
        f->resize_start[1] = get_upscale_x0(in_cw, out_cw, f->resize_step[1]);
3599
5.82k
    }
3600
3601
    // move f->cur into output queue
3602
31.7k
    if (c->n_fc == 1) {
3603
0
        if (f->frame_hdr->show_frame || c->output_invisible_frames) {
3604
0
            dav1d_thread_picture_ref(&c->out, &f->sr_cur);
3605
0
            c->event_flags |= dav1d_picture_get_event_flags(&f->sr_cur);
3606
0
        }
3607
31.7k
    } else {
3608
31.7k
        dav1d_thread_picture_ref(out_delayed, &f->sr_cur);
3609
31.7k
    }
3610
3611
31.7k
    f->w4 = (f->frame_hdr->width[0] + 3) >> 2;
3612
31.7k
    f->h4 = (f->frame_hdr->height + 3) >> 2;
3613
31.7k
    f->bw = ((f->frame_hdr->width[0] + 7) >> 3) << 1;
3614
31.7k
    f->bh = ((f->frame_hdr->height + 7) >> 3) << 1;
3615
31.7k
    f->sb128w = (f->bw + 31) >> 5;
3616
31.7k
    f->sb128h = (f->bh + 31) >> 5;
3617
31.7k
    f->sb_shift = 4 + f->seq_hdr->sb128;
3618
31.7k
    f->sb_step = 16 << f->seq_hdr->sb128;
3619
31.7k
    f->sbh = (f->bh + f->sb_step - 1) >> f->sb_shift;
3620
31.7k
    f->b4_stride = (f->bw + 31) & ~31;
3621
31.7k
    f->bitdepth_max = (1 << f->cur.p.bpc) - 1;
3622
31.7k
    atomic_init(&f->task_thread.error, 0);
3623
31.7k
    const int uses_2pass = c->n_fc > 1;
3624
31.7k
    const int cols = f->frame_hdr->tiling.cols;
3625
31.7k
    const int rows = f->frame_hdr->tiling.rows;
3626
31.7k
    f->task_thread.task_counter = (cols * rows + f->sbh) << uses_2pass;
3627
3628
    // ref_mvs
3629
31.7k
    if (IS_INTER_OR_SWITCH(f->frame_hdr) || f->frame_hdr->allow_intrabc) {
3630
15.6k
        f->mvs_ref = dav1d_ref_create_using_pool(c->refmvs_pool,
3631
15.6k
            sizeof(*f->mvs) * f->sb128h * 16 * (f->b4_stride >> 1));
3632
15.6k
        if (!f->mvs_ref) {
3633
0
            res = DAV1D_ERR(ENOMEM);
3634
0
            goto error;
3635
0
        }
3636
15.6k
        f->mvs = f->mvs_ref->data;
3637
15.6k
        if (!f->frame_hdr->allow_intrabc) {
3638
81.1k
            for (int i = 0; i < 7; i++)
3639
71.0k
                f->refpoc[i] = f->refp[i].p.frame_hdr->frame_offset;
3640
10.1k
        } else {
3641
5.51k
            memset(f->refpoc, 0, sizeof(f->refpoc));
3642
5.51k
        }
3643
15.6k
        if (f->frame_hdr->use_ref_frame_mvs) {
3644
18.6k
            for (int i = 0; i < 7; i++) {
3645
16.3k
                const int refidx = f->frame_hdr->refidx[i];
3646
16.3k
                const int ref_w = ((ref_coded_width[i] + 7) >> 3) << 1;
3647
16.3k
                const int ref_h = ((f->refp[i].p.p.h + 7) >> 3) << 1;
3648
16.3k
                if (c->refs[refidx].refmvs != NULL &&
3649
6.01k
                    ref_w == f->bw && ref_h == f->bh)
3650
5.54k
                {
3651
5.54k
                    f->ref_mvs_ref[i] = c->refs[refidx].refmvs;
3652
5.54k
                    dav1d_ref_inc(f->ref_mvs_ref[i]);
3653
5.54k
                    f->ref_mvs[i] = c->refs[refidx].refmvs->data;
3654
10.7k
                } else {
3655
10.7k
                    f->ref_mvs[i] = NULL;
3656
10.7k
                    f->ref_mvs_ref[i] = NULL;
3657
10.7k
                }
3658
16.3k
                memcpy(f->refrefpoc[i], c->refs[refidx].refpoc,
3659
16.3k
                       sizeof(*f->refrefpoc));
3660
16.3k
            }
3661
13.3k
        } else {
3662
13.3k
            memset(f->ref_mvs_ref, 0, sizeof(f->ref_mvs_ref));
3663
13.3k
        }
3664
16.1k
    } else {
3665
16.1k
        f->mvs_ref = NULL;
3666
16.1k
        memset(f->ref_mvs_ref, 0, sizeof(f->ref_mvs_ref));
3667
16.1k
    }
3668
3669
    // segmap
3670
31.7k
    if (f->frame_hdr->segmentation.enabled) {
3671
        // By default, the previous segmentation map is not initialised.
3672
11.3k
        f->prev_segmap_ref = NULL;
3673
11.3k
        f->prev_segmap = NULL;
3674
3675
        // We might need a previous frame's segmentation map. This
3676
        // happens if there is either no update or a temporal update.
3677
11.3k
        if (f->frame_hdr->segmentation.temporal || !f->frame_hdr->segmentation.update_map) {
3678
3.39k
            const int pri_ref = f->frame_hdr->primary_ref_frame;
3679
3.39k
            assert(pri_ref != DAV1D_PRIMARY_REF_NONE);
3680
3.39k
            const int ref_w = ((ref_coded_width[pri_ref] + 7) >> 3) << 1;
3681
3.39k
            const int ref_h = ((f->refp[pri_ref].p.p.h + 7) >> 3) << 1;
3682
3.39k
            if (ref_w == f->bw && ref_h == f->bh) {
3683
2.15k
                f->prev_segmap_ref = c->refs[f->frame_hdr->refidx[pri_ref]].segmap;
3684
2.15k
                if (f->prev_segmap_ref) {
3685
1.35k
                    dav1d_ref_inc(f->prev_segmap_ref);
3686
1.35k
                    f->prev_segmap = f->prev_segmap_ref->data;
3687
1.35k
                }
3688
2.15k
            }
3689
3.39k
        }
3690
3691
11.3k
        if (f->frame_hdr->segmentation.update_map) {
3692
            // We're updating an existing map, but need somewhere to
3693
            // put the new values. Allocate them here (the data
3694
            // actually gets set elsewhere)
3695
8.44k
            f->cur_segmap_ref = dav1d_ref_create_using_pool(c->segmap_pool,
3696
8.44k
                sizeof(*f->cur_segmap) * f->b4_stride * 32 * f->sb128h);
3697
8.44k
            if (!f->cur_segmap_ref) {
3698
0
                dav1d_ref_dec(&f->prev_segmap_ref);
3699
0
                res = DAV1D_ERR(ENOMEM);
3700
0
                goto error;
3701
0
            }
3702
8.44k
            f->cur_segmap = f->cur_segmap_ref->data;
3703
8.44k
        } else if (f->prev_segmap_ref) {
3704
            // We're not updating an existing map, and we have a valid
3705
            // reference. Use that.
3706
1.09k
            f->cur_segmap_ref = f->prev_segmap_ref;
3707
1.09k
            dav1d_ref_inc(f->cur_segmap_ref);
3708
1.09k
            f->cur_segmap = f->prev_segmap_ref->data;
3709
1.77k
        } else {
3710
            // We need to make a new map. Allocate one here and zero it out.
3711
1.77k
            const size_t segmap_size = sizeof(*f->cur_segmap) * f->b4_stride * 32 * f->sb128h;
3712
1.77k
            f->cur_segmap_ref = dav1d_ref_create_using_pool(c->segmap_pool, segmap_size);
3713
1.77k
            if (!f->cur_segmap_ref) {
3714
0
                res = DAV1D_ERR(ENOMEM);
3715
0
                goto error;
3716
0
            }
3717
1.77k
            f->cur_segmap = f->cur_segmap_ref->data;
3718
1.77k
            memset(f->cur_segmap, 0, segmap_size);
3719
1.77k
        }
3720
20.4k
    } else {
3721
20.4k
        f->cur_segmap = NULL;
3722
20.4k
        f->cur_segmap_ref = NULL;
3723
20.4k
        f->prev_segmap_ref = NULL;
3724
20.4k
    }
3725
3726
    // update references etc.
3727
31.7k
    const unsigned refresh_frame_flags = f->frame_hdr->refresh_frame_flags;
3728
285k
    for (int i = 0; i < 8; i++) {
3729
254k
        if (refresh_frame_flags & (1 << i)) {
3730
203k
            if (c->refs[i].p.p.frame_hdr)
3731
45.1k
                dav1d_thread_picture_unref(&c->refs[i].p);
3732
203k
            dav1d_thread_picture_ref(&c->refs[i].p, &f->sr_cur);
3733
3734
203k
            dav1d_cdf_thread_unref(&c->cdf[i]);
3735
203k
            if (f->frame_hdr->refresh_context) {
3736
49.7k
                dav1d_cdf_thread_ref(&c->cdf[i], &f->out_cdf);
3737
154k
            } else {
3738
154k
                dav1d_cdf_thread_ref(&c->cdf[i], &f->in_cdf);
3739
154k
            }
3740
3741
203k
            dav1d_ref_dec(&c->refs[i].segmap);
3742
203k
            c->refs[i].segmap = f->cur_segmap_ref;
3743
203k
            if (f->cur_segmap_ref)
3744
68.5k
                dav1d_ref_inc(f->cur_segmap_ref);
3745
203k
            dav1d_ref_dec(&c->refs[i].refmvs);
3746
203k
            if (!f->frame_hdr->allow_intrabc) {
3747
161k
                c->refs[i].refmvs = f->mvs_ref;
3748
161k
                if (f->mvs_ref)
3749
38.9k
                    dav1d_ref_inc(f->mvs_ref);
3750
161k
            }
3751
203k
            memcpy(c->refs[i].refpoc, f->refpoc, sizeof(f->refpoc));
3752
203k
        }
3753
254k
    }
3754
3755
31.7k
    if (c->n_fc == 1) {
3756
0
        if ((res = dav1d_decode_frame(f)) < 0) {
3757
0
            dav1d_thread_picture_unref(&c->out);
3758
0
            for (int i = 0; i < 8; i++) {
3759
0
                if (refresh_frame_flags & (1 << i)) {
3760
0
                    if (c->refs[i].p.p.frame_hdr)
3761
0
                        dav1d_thread_picture_unref(&c->refs[i].p);
3762
0
                    dav1d_cdf_thread_unref(&c->cdf[i]);
3763
0
                    dav1d_ref_dec(&c->refs[i].segmap);
3764
0
                    dav1d_ref_dec(&c->refs[i].refmvs);
3765
0
                }
3766
0
            }
3767
0
            goto error;
3768
0
        }
3769
31.7k
    } else {
3770
31.7k
        dav1d_task_frame_init(f);
3771
31.7k
        pthread_mutex_unlock(&c->task_thread.lock);
3772
31.7k
    }
3773
3774
31.7k
    return 0;
3775
67
error:
3776
67
    atomic_init(&f->task_thread.error, 1);
3777
67
    dav1d_cdf_thread_unref(&f->in_cdf);
3778
67
    if (f->frame_hdr->refresh_context)
3779
41
        dav1d_cdf_thread_unref(&f->out_cdf);
3780
536
    for (int i = 0; i < 7; i++) {
3781
469
        if (f->refp[i].p.frame_hdr)
3782
0
            dav1d_thread_picture_unref(&f->refp[i]);
3783
469
        dav1d_ref_dec(&f->ref_mvs_ref[i]);
3784
469
    }
3785
67
    if (c->n_fc == 1)
3786
0
        dav1d_thread_picture_unref(&c->out);
3787
67
    else
3788
67
        dav1d_thread_picture_unref(out_delayed);
3789
67
    dav1d_picture_unref_internal(&f->cur);
3790
67
    dav1d_thread_picture_unref(&f->sr_cur);
3791
67
    dav1d_ref_dec(&f->mvs_ref);
3792
67
    dav1d_ref_dec(&f->seq_hdr_ref);
3793
67
    dav1d_ref_dec(&f->frame_hdr_ref);
3794
67
    dav1d_data_props_copy(&c->cached_error_props, &c->in.m);
3795
3796
67
    for (int i = 0; i < f->n_tile_data; i++)
3797
0
        dav1d_data_unref_internal(&f->tile[i].data);
3798
67
    f->n_tile_data = 0;
3799
3800
67
    if (c->n_fc > 1)
3801
67
        pthread_mutex_unlock(&c->task_thread.lock);
3802
3803
67
    return res;
3804
31.7k
}