Coverage Report

Created: 2026-05-30 06:08

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