Coverage Report

Created: 2026-08-13 07:20

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