Coverage Report

Created: 2026-10-17 06:56

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