Coverage Report

Created: 2026-07-16 06:31

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
58.7k
{
58
223k
    for (int i = 0; i < (frame_hdr->segmentation.enabled ? 8 : 1); i++) {
59
164k
        const int yac = frame_hdr->segmentation.enabled ?
60
120k
            iclip_u8(qidx + frame_hdr->segmentation.seg_data.d[i].delta_q) : qidx;
61
164k
        const int ydc = iclip_u8(yac + frame_hdr->quant.ydc_delta);
62
164k
        const int uac = iclip_u8(yac + frame_hdr->quant.uac_delta);
63
164k
        const int udc = iclip_u8(yac + frame_hdr->quant.udc_delta);
64
164k
        const int vac = iclip_u8(yac + frame_hdr->quant.vac_delta);
65
164k
        const int vdc = iclip_u8(yac + frame_hdr->quant.vdc_delta);
66
67
164k
        dq[i][0][0] = dav1d_dq_tbl[seq_hdr->hbd][ydc][0];
68
164k
        dq[i][0][1] = dav1d_dq_tbl[seq_hdr->hbd][yac][1];
69
164k
        dq[i][1][0] = dav1d_dq_tbl[seq_hdr->hbd][udc][0];
70
164k
        dq[i][1][1] = dav1d_dq_tbl[seq_hdr->hbd][uac][1];
71
164k
        dq[i][2][0] = dav1d_dq_tbl[seq_hdr->hbd][vdc][0];
72
164k
        dq[i][2][1] = dav1d_dq_tbl[seq_hdr->hbd][vac][1];
73
164k
    }
74
58.7k
}
75
76
static int read_mv_component_diff(MsacContext *const msac,
77
                                  CdfMvComponent *const mv_comp,
78
                                  const int mv_prec)
79
555k
{
80
555k
    const int sign = dav1d_msac_decode_bool_adapt(msac, mv_comp->sign);
81
555k
    const int cl = dav1d_msac_decode_symbol_adapt16(msac, mv_comp->classes, 10);
82
555k
    int up, fp = 3, hp = 1;
83
84
555k
    if (!cl) {
85
131k
        up = dav1d_msac_decode_bool_adapt(msac, mv_comp->class0);
86
131k
        if (mv_prec >= 0) {  // !force_integer_mv
87
74.3k
            fp = dav1d_msac_decode_symbol_adapt4(msac, mv_comp->class0_fp[up], 3);
88
74.3k
            if (mv_prec > 0) // allow_high_precision_mv
89
37.2k
                hp = dav1d_msac_decode_bool_adapt(msac, mv_comp->class0_hp);
90
74.3k
        }
91
423k
    } else {
92
423k
        up = 1 << cl;
93
4.46M
        for (int n = 0; n < cl; n++)
94
4.03M
            up |= dav1d_msac_decode_bool_adapt(msac, mv_comp->classN[n]) << n;
95
423k
        if (mv_prec >= 0) {  // !force_integer_mv
96
20.3k
            fp = dav1d_msac_decode_symbol_adapt4(msac, mv_comp->classN_fp, 3);
97
20.3k
            if (mv_prec > 0) // allow_high_precision_mv
98
10.1k
                hp = dav1d_msac_decode_bool_adapt(msac, mv_comp->classN_hp);
99
20.3k
        }
100
423k
    }
101
102
555k
    const int diff = ((up << 3) | (fp << 1) | hp) + 1;
103
104
555k
    return sign ? -diff : diff;
105
555k
}
106
107
static void read_mv_residual(Dav1dTileState *const ts, mv *const ref_mv,
108
                             const int mv_prec)
109
320k
{
110
320k
    MsacContext *const msac = &ts->msac;
111
320k
    const enum MVJoint mv_joint =
112
320k
        dav1d_msac_decode_symbol_adapt4(msac, ts->cdf.mv.joint, N_MV_JOINTS - 1);
113
320k
    if (mv_joint & MV_JOINT_V)
114
279k
        ref_mv->y += read_mv_component_diff(msac, &ts->cdf.mv.comp[0], mv_prec);
115
320k
    if (mv_joint & MV_JOINT_H)
116
276k
        ref_mv->x += read_mv_component_diff(msac, &ts->cdf.mv.comp[1], mv_prec);
117
320k
}
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
151k
{
124
151k
    const Dav1dFrameContext *const f = t->f;
125
151k
    const int bx4 = t->bx & 31, by4 = t->by & 31;
126
151k
    const TxfmInfo *const t_dim = &dav1d_txfm_dimensions[from];
127
151k
    const int txw = t_dim->lw, txh = t_dim->lh;
128
151k
    int is_split;
129
130
151k
    if (depth < 2 && from > (int) TX_4X4) {
131
128k
        const int cat = 2 * (TX_64X64 - t_dim->max) - depth;
132
128k
        const int a = t->a->tx[bx4] < txw;
133
128k
        const int l = t->l.tx[by4] < txh;
134
135
128k
        is_split = dav1d_msac_decode_bool_adapt(&t->ts->msac,
136
128k
                       t->ts->cdf.m.txpart[cat][a + l]);
137
128k
        if (is_split)
138
36.5k
            masks[depth] |= 1 << (y_off * 4 + x_off);
139
128k
    } else {
140
22.5k
        is_split = 0;
141
22.5k
    }
142
143
151k
    if (is_split && t_dim->max > TX_8X8) {
144
25.9k
        const enum RectTxfmSize sub = t_dim->sub;
145
25.9k
        const TxfmInfo *const sub_t_dim = &dav1d_txfm_dimensions[sub];
146
25.9k
        const int txsw = sub_t_dim->w, txsh = sub_t_dim->h;
147
148
25.9k
        read_tx_tree(t, sub, depth + 1, masks, x_off * 2 + 0, y_off * 2 + 0);
149
25.9k
        t->bx += txsw;
150
25.9k
        if (txw >= txh && t->bx < f->bw)
151
18.7k
            read_tx_tree(t, sub, depth + 1, masks, x_off * 2 + 1, y_off * 2 + 0);
152
25.9k
        t->bx -= txsw;
153
25.9k
        t->by += txsh;
154
25.9k
        if (txh >= txw && t->by < f->bh) {
155
16.6k
            read_tx_tree(t, sub, depth + 1, masks, x_off * 2 + 0, y_off * 2 + 1);
156
16.6k
            t->bx += txsw;
157
16.6k
            if (txw >= txh && t->bx < f->bw)
158
9.62k
                read_tx_tree(t, sub, depth + 1, masks,
159
9.62k
                             x_off * 2 + 1, y_off * 2 + 1);
160
16.6k
            t->bx -= txsw;
161
16.6k
        }
162
25.9k
        t->by -= txsh;
163
125k
    } else {
164
125k
        dav1d_memset_pow2[t_dim->lw](&t->a->tx[bx4], is_split ? TX_4X4 : txw);
165
125k
        dav1d_memset_pow2[t_dim->lh](&t->l.tx[by4], is_split ? TX_4X4 : txh);
166
125k
    }
167
151k
}
168
169
538k
static int neg_deinterleave(int diff, int ref, int max) {
170
538k
    if (!ref) return diff;
171
291k
    if (ref >= (max - 1)) return max - diff - 1;
172
241k
    if (2 * ref < max) {
173
145k
        if (diff <= 2 * ref) {
174
120k
            if (diff & 1)
175
15.9k
                return ref + ((diff + 1) >> 1);
176
104k
            else
177
104k
                return ref - (diff >> 1);
178
120k
        }
179
25.0k
        return diff;
180
145k
    } else {
181
96.4k
        if (diff <= 2 * (max - ref - 1)) {
182
81.3k
            if (diff & 1)
183
10.8k
                return ref + ((diff + 1) >> 1);
184
70.4k
            else
185
70.4k
                return ref - (diff >> 1);
186
81.3k
        }
187
15.0k
        return max - (diff + 1);
188
96.4k
    }
189
241k
}
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
55.8k
{
198
55.8k
    /*const*/ refmvs_block *const *r = &t->rt.r[(t->by & 31) + 5];
199
55.8k
    int count = 0;
200
55.8k
    int have_topleft = have_top && have_left;
201
55.8k
    int have_topright = imax(bw4, bh4) < 32 &&
202
55.6k
                        have_top && t->bx + bw4 < t->ts->tiling.col_end &&
203
33.3k
                        (intra_edge_flags & EDGE_I444_TOP_HAS_RIGHT);
204
205
138k
#define bs(rp) dav1d_block_dimensions[(rp)->bs]
206
144k
#define matches(rp) ((rp)->ref.ref[0] == ref + 1 && (rp)->ref.ref[1] == -1)
207
208
55.8k
    if (have_top) {
209
45.4k
        const refmvs_block *r2 = &r[-1][t->bx];
210
45.4k
        if (matches(r2)) {
211
28.9k
            masks[0] |= 1;
212
28.9k
            count = 1;
213
28.9k
        }
214
45.4k
        int aw4 = bs(r2)[0];
215
45.4k
        if (aw4 >= bw4) {
216
38.0k
            const int off = t->bx & (aw4 - 1);
217
38.0k
            if (off) have_topleft = 0;
218
38.0k
            if (aw4 - off > bw4) have_topright = 0;
219
38.0k
        } else {
220
7.37k
            unsigned mask = 1 << aw4;
221
16.4k
            for (int x = aw4; x < w4; x += aw4) {
222
9.10k
                r2 += aw4;
223
9.10k
                if (matches(r2)) {
224
5.00k
                    masks[0] |= mask;
225
5.00k
                    if (++count >= 8) return;
226
5.00k
                }
227
9.08k
                aw4 = bs(r2)[0];
228
9.08k
                mask <<= aw4;
229
9.08k
            }
230
7.37k
        }
231
45.4k
    }
232
55.8k
    if (have_left) {
233
44.2k
        /*const*/ refmvs_block *const *r2 = r;
234
44.2k
        if (matches(&r2[0][t->bx - 1])) {
235
28.1k
            masks[1] |= 1;
236
28.1k
            if (++count >= 8) return;
237
28.1k
        }
238
44.2k
        int lh4 = bs(&r2[0][t->bx - 1])[1];
239
44.2k
        if (lh4 >= bh4) {
240
37.4k
            if (t->by & (lh4 - 1)) have_topleft = 0;
241
37.4k
        } else {
242
6.70k
            unsigned mask = 1 << lh4;
243
15.3k
            for (int y = lh4; y < h4; y += lh4) {
244
8.69k
                r2 += lh4;
245
8.69k
                if (matches(&r2[0][t->bx - 1])) {
246
4.49k
                    masks[1] |= mask;
247
4.49k
                    if (++count >= 8) return;
248
4.49k
                }
249
8.66k
                lh4 = bs(&r2[0][t->bx - 1])[1];
250
8.66k
                mask <<= lh4;
251
8.66k
            }
252
6.70k
        }
253
44.2k
    }
254
55.8k
    if (have_topleft && matches(&r[-1][t->bx - 1])) {
255
11.4k
        masks[1] |= 1ULL << 32;
256
11.4k
        if (++count >= 8) return;
257
11.4k
    }
258
55.8k
    if (have_topright && matches(&r[-1][t->bx + bw4])) {
259
7.81k
        masks[0] |= 1ULL << 32;
260
7.81k
    }
261
55.8k
#undef matches
262
55.8k
}
263
264
static void derive_warpmv(const Dav1dTaskContext *const t,
265
                          const int bw4, const int bh4,
266
                          const uint64_t masks[2], const union mv mv,
267
                          Dav1dWarpedMotionParams *const wmp)
268
5.69k
{
269
5.69k
    int pts[8][2 /* in, out */][2 /* x, y */], np = 0;
270
5.69k
    /*const*/ refmvs_block *const *r = &t->rt.r[(t->by & 31) + 5];
271
272
13.2k
#define add_sample(dx, dy, sx, sy, rp) do { \
273
13.2k
    pts[np][0][0] = 16 * (2 * dx + sx * bs(rp)[0]) - 8; \
274
13.2k
    pts[np][0][1] = 16 * (2 * dy + sy * bs(rp)[1]) - 8; \
275
13.2k
    pts[np][1][0] = pts[np][0][0] + (rp)->mv.mv[0].x; \
276
13.2k
    pts[np][1][1] = pts[np][0][1] + (rp)->mv.mv[0].y; \
277
13.2k
    np++; \
278
13.2k
} while (0)
279
280
    // use masks[] to find the projectable motion vectors in the edges
281
5.69k
    if ((unsigned) masks[0] == 1 && !(masks[1] >> 32)) {
282
1.58k
        const int off = t->bx & (bs(&r[-1][t->bx])[0] - 1);
283
1.58k
        add_sample(-off, 0, 1, -1, &r[-1][t->bx]);
284
6.98k
    } else for (unsigned off = 0, xmask = (uint32_t) masks[0]; np < 8 && xmask;) { // top
285
2.87k
        const int tz = ctz(xmask);
286
2.87k
        off += tz;
287
2.87k
        xmask >>= tz;
288
2.87k
        add_sample(off, 0, 1, -1, &r[-1][t->bx + off]);
289
2.87k
        xmask &= ~1;
290
2.87k
    }
291
5.69k
    if (np < 8 && masks[1] == 1) {
292
2.52k
        const int off = t->by & (bs(&r[0][t->bx - 1])[1] - 1);
293
2.52k
        add_sample(0, -off, -1, 1, &r[-off][t->bx - 1]);
294
5.75k
    } else for (unsigned off = 0, ymask = (uint32_t) masks[1]; np < 8 && ymask;) { // left
295
2.58k
        const int tz = ctz(ymask);
296
2.58k
        off += tz;
297
2.58k
        ymask >>= tz;
298
2.58k
        add_sample(0, off, -1, 1, &r[off][t->bx - 1]);
299
2.58k
        ymask &= ~1;
300
2.58k
    }
301
5.69k
    if (np < 8 && masks[1] >> 32) // top/left
302
2.04k
        add_sample(0, 0, -1, -1, &r[-1][t->bx - 1]);
303
5.69k
    if (np < 8 && masks[0] >> 32) // top/right
304
1.67k
        add_sample(bw4, 0, 1, -1, &r[-1][t->bx + bw4]);
305
5.69k
    assert(np > 0 && np <= 8);
306
5.69k
#undef bs
307
308
    // select according to motion vector difference against a threshold
309
5.69k
    int mvd[8], ret = 0;
310
5.69k
    const int thresh = 4 * iclip(imax(bw4, bh4), 4, 28);
311
18.9k
    for (int i = 0; i < np; i++) {
312
13.2k
        mvd[i] = abs(pts[i][1][0] - pts[i][0][0] - mv.x) +
313
13.2k
                 abs(pts[i][1][1] - pts[i][0][1] - mv.y);
314
13.2k
        if (mvd[i] > thresh)
315
3.86k
            mvd[i] = -1;
316
9.43k
        else
317
9.43k
            ret++;
318
13.2k
    }
319
5.69k
    if (!ret) {
320
1.26k
        ret = 1;
321
5.16k
    } else for (int i = 0, j = np - 1, k = 0; k < np - ret; k++, i++, j--) {
322
2.31k
        while (mvd[i] != -1) i++;
323
2.48k
        while (mvd[j] == -1) j--;
324
1.41k
        assert(i != j);
325
1.41k
        if (i > j) break;
326
        // replace the discarded samples;
327
726
        mvd[i] = mvd[j];
328
726
        memcpy(pts[i], pts[j], sizeof(*pts));
329
726
    }
330
331
5.69k
    if (!dav1d_find_affine_int(pts, ret, bw4, bh4, mv, wmp, t->bx, t->by) &&
332
5.40k
        !dav1d_get_shear_params(wmp))
333
4.92k
    {
334
4.92k
        wmp->type = DAV1D_WM_TYPE_AFFINE;
335
4.92k
    } else
336
775
        wmp->type = DAV1D_WM_TYPE_IDENTITY;
337
5.69k
}
338
339
58.1k
static inline int findoddzero(const uint8_t *buf, int len) {
340
63.0k
    for (int n = 0; n < len; n++)
341
60.7k
        if (!buf[n * 2]) return 1;
342
2.26k
    return 0;
343
58.1k
}
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
557k
{
357
557k
    int have_top = i > first;
358
359
557k
    assert(pal_idx);
360
557k
    pal_idx += first + (i - first) * stride;
361
5.10M
    for (int j = first, n = 0; j >= last; have_top = 1, j--, n++, pal_idx += stride - 1) {
362
4.54M
        const int have_left = j > 0;
363
364
4.54M
        assert(have_left || have_top);
365
366
6.80M
#define add(v_in) do { \
367
6.80M
        const int v = v_in; \
368
6.80M
        assert((unsigned)v < 8U); \
369
6.80M
        order[n][o_idx++] = v; \
370
6.80M
        mask |= 1 << v; \
371
6.80M
    } while (0)
372
373
4.54M
        unsigned mask = 0;
374
4.54M
        int o_idx = 0;
375
4.54M
        if (!have_left) {
376
232k
            ctx[n] = 0;
377
232k
            add(pal_idx[-stride]);
378
4.31M
        } else if (!have_top) {
379
324k
            ctx[n] = 0;
380
324k
            add(pal_idx[-1]);
381
3.98M
        } else {
382
3.98M
            const int l = pal_idx[-1], t = pal_idx[-stride], tl = pal_idx[-(stride + 1)];
383
3.98M
            const int same_t_l = t == l;
384
3.98M
            const int same_t_tl = t == tl;
385
3.98M
            const int same_l_tl = l == tl;
386
3.98M
            const int same_all = same_t_l & same_t_tl & same_l_tl;
387
388
3.98M
            if (same_all) {
389
2.07M
                ctx[n] = 4;
390
2.07M
                add(t);
391
2.07M
            } else if (same_t_l) {
392
160k
                ctx[n] = 3;
393
160k
                add(t);
394
160k
                add(tl);
395
1.74M
            } else if (same_t_tl | same_l_tl) {
396
1.39M
                ctx[n] = 2;
397
1.39M
                add(tl);
398
1.39M
                add(same_t_tl ? l : t);
399
1.39M
            } else {
400
351k
                ctx[n] = 1;
401
351k
                add(imin(t, l));
402
351k
                add(imax(t, l));
403
351k
                add(tl);
404
351k
            }
405
3.98M
        }
406
40.7M
        for (unsigned m = 1, bit = 0; m < 0x100; m <<= 1, bit++)
407
36.2M
            if (!(mask & m))
408
29.4M
                order[n][o_idx++] = bit;
409
4.54M
        assert(o_idx == 8);
410
4.54M
#undef add
411
4.54M
    }
412
557k
}
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
21.3k
{
420
21.3k
    Dav1dTileState *const ts = t->ts;
421
21.3k
    const ptrdiff_t stride = bw4 * 4;
422
21.3k
    assert(pal_idx);
423
21.3k
    uint8_t *const pal_tmp = t->scratch.pal_idx_uv;
424
21.3k
    pal_tmp[0] = dav1d_msac_decode_uniform(&ts->msac, pal_sz);
425
21.3k
    uint16_t (*const color_map_cdf)[8] =
426
21.3k
        ts->cdf.m.color_map[pl][pal_sz - 2];
427
21.3k
    uint8_t (*const order)[8] = t->scratch.pal_order;
428
21.3k
    uint8_t *const ctx = t->scratch.pal_ctx;
429
578k
    for (int i = 1; i < 4 * (w4 + h4) - 1; i++) {
430
        // top/left-to-bottom/right diagonals ("wave-front")
431
557k
        const int first = imin(i, w4 * 4 - 1);
432
557k
        const int last = imax(0, i - h4 * 4 + 1);
433
557k
        order_palette(pal_tmp, stride, i, first, last, order, ctx);
434
5.10M
        for (int j = first, m = 0; j >= last; j--, m++) {
435
4.55M
            const int color_idx = dav1d_msac_decode_symbol_adapt8(&ts->msac,
436
4.55M
                                      color_map_cdf[ctx[m]], pal_sz - 1);
437
4.55M
            pal_tmp[(i - j) * stride + j] = order[m][color_idx];
438
4.55M
        }
439
557k
    }
440
441
21.3k
    t->c->pal_dsp.pal_idx_finish(pal_idx, pal_tmp, bw4 * 4, bh4 * 4,
442
21.3k
                                 w4 * 4, h4 * 4);
443
21.3k
}
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
469k
{
449
469k
    const Dav1dFrameContext *const f = t->f;
450
469k
    const uint8_t *const b_dim = dav1d_block_dimensions[bs];
451
469k
    const int bw4 = b_dim[0], bh4 = b_dim[1];
452
453
    // var-tx tree coding
454
469k
    uint16_t tx_split[2] = { 0 };
455
469k
    b->max_ytx = dav1d_max_txfm_size_for_bs[bs][0];
456
469k
    if (!b->skip && (f->frame_hdr->segmentation.lossless[b->seg_id] ||
457
162k
                     b->max_ytx == TX_4X4))
458
17.5k
    {
459
17.5k
        b->max_ytx = b->uvtx = TX_4X4;
460
17.5k
        if (f->frame_hdr->txfm_mode == DAV1D_TX_SWITCHABLE) {
461
6.15k
            dav1d_memset_pow2[b_dim[2]](&t->a->tx[bx4], TX_4X4);
462
6.15k
            dav1d_memset_pow2[b_dim[3]](&t->l.tx[by4], TX_4X4);
463
6.15k
        }
464
451k
    } else if (f->frame_hdr->txfm_mode != DAV1D_TX_SWITCHABLE || b->skip) {
465
373k
        if (f->frame_hdr->txfm_mode == DAV1D_TX_SWITCHABLE) {
466
80.6k
            dav1d_memset_pow2[b_dim[2]](&t->a->tx[bx4], b_dim[2 + 0]);
467
80.6k
            dav1d_memset_pow2[b_dim[3]](&t->l.tx[by4], b_dim[2 + 1]);
468
80.6k
        }
469
373k
        b->uvtx = dav1d_max_txfm_size_for_bs[bs][f->cur.p.layout];
470
373k
    } else {
471
78.6k
        assert(bw4 <= 16 || bh4 <= 16 || b->max_ytx == TX_64X64);
472
78.6k
        int y, x, y_off, x_off;
473
78.6k
        const TxfmInfo *const ytx = &dav1d_txfm_dimensions[b->max_ytx];
474
157k
        for (y = 0, y_off = 0; y < bh4; y += ytx->h, y_off++) {
475
159k
            for (x = 0, x_off = 0; x < bw4; x += ytx->w, x_off++) {
476
80.2k
                read_tx_tree(t, b->max_ytx, 0, tx_split, x_off, y_off);
477
                // contexts are updated inside read_tx_tree()
478
80.2k
                t->bx += ytx->w;
479
80.2k
            }
480
79.2k
            t->bx -= x;
481
79.2k
            t->by += ytx->h;
482
79.2k
        }
483
78.6k
        t->by -= y;
484
78.6k
        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
78.6k
        b->uvtx = dav1d_max_txfm_size_for_bs[bs][f->cur.p.layout];
488
78.6k
    }
489
469k
    assert(!(tx_split[0] & ~0x33));
490
469k
    b->tx_split0 = (uint8_t)tx_split[0];
491
469k
    b->tx_split1 = tx_split[1];
492
469k
}
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
23.9k
{
500
23.9k
    assert(f->frame_hdr->primary_ref_frame != DAV1D_PRIMARY_REF_NONE);
501
502
23.9k
    unsigned seg_id = 8;
503
23.9k
    ref_seg_map += by * stride + bx;
504
60.7k
    do {
505
314k
        for (int x = 0; x < w4; x++)
506
253k
            seg_id = imin(seg_id, ref_seg_map[x]);
507
60.7k
        ref_seg_map += stride;
508
60.7k
    } while (--h4 > 0 && seg_id);
509
23.9k
    assert(seg_id < 8);
510
511
23.9k
    return seg_id;
512
23.9k
}
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
179k
{
520
179k
    const enum InterPredMode mode = b->inter_mode;
521
179k
    const refmvs_block ALIGN(tmpl, 16) = (refmvs_block) {
522
179k
        .ref.ref = { b->ref[0] + 1, b->interintra_type ? 0 : -1 },
523
179k
        .mv.mv[0] = b->mv[0],
524
179k
        .bs = bs,
525
179k
        .mf = (mode == GLOBALMV && imin(bw4, bh4) >= 2) | ((mode == NEWMV) * 2),
526
179k
    };
527
179k
    c->refmvs_dsp.splat_mv(&t->rt.r[(t->by & 31) + 5], &tmpl, t->bx, bw4, bh4);
528
179k
}
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
246k
{
536
246k
    const refmvs_block ALIGN(tmpl, 16) = (refmvs_block) {
537
246k
        .ref.ref = { 0, -1 },
538
246k
        .mv.mv[0] = b->mv[0],
539
246k
        .bs = bs,
540
246k
        .mf = 0,
541
246k
    };
542
246k
    c->refmvs_dsp.splat_mv(&t->rt.r[(t->by & 31) + 5], &tmpl, t->bx, bw4, bh4);
543
246k
}
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
43.4k
{
551
43.4k
    assert(bw4 >= 2 && bh4 >= 2);
552
43.4k
    const enum CompInterPredMode mode = b->inter_mode;
553
43.4k
    const refmvs_block ALIGN(tmpl, 16) = (refmvs_block) {
554
43.4k
        .ref.ref = { b->ref[0] + 1, b->ref[1] + 1 },
555
43.4k
        .mv.mv = { b->mv[0], b->mv[1] },
556
43.4k
        .bs = bs,
557
43.4k
        .mf = (mode == GLOBALMV_GLOBALMV) | !!((1 << mode) & (0xbc)) * 2,
558
43.4k
    };
559
43.4k
    c->refmvs_dsp.splat_mv(&t->rt.r[(t->by & 31) + 5], &tmpl, t->bx, bw4, bh4);
560
43.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
400k
{
567
400k
    const refmvs_block ALIGN(tmpl, 16) = (refmvs_block) {
568
400k
        .ref.ref = { 0, -1 },
569
400k
        .mv.mv[0].n = INVALID_MV,
570
400k
        .bs = bs,
571
400k
        .mf = 0,
572
400k
    };
573
400k
    c->refmvs_dsp.splat_mv(&t->rt.r[(t->by & 31) + 5], &tmpl, t->bx, bw4, bh4);
574
400k
}
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
545k
{
580
545k
    const int v_mul = 4 >> ss_ver;
581
545k
    if (!smp->scale) {
582
290k
        const int my = mvy >> (3 + ss_ver), dy = mvy & (15 >> !ss_ver);
583
290k
        *dst = imax(*dst, (by4 + bh4) * v_mul + my + 4 * !!dy);
584
290k
    } else {
585
255k
        int y = (by4 * v_mul << 4) + mvy * (1 << !ss_ver);
586
255k
        const int64_t tmp = (int64_t)(y) * smp->scale + (smp->scale - 0x4000) * 8;
587
255k
        y = apply_sign64((int)((llabs(tmp) + 128) >> 8), tmp) + 32;
588
255k
        const int bottom = ((y + (bh4 * v_mul - 1) * smp->step) >> 10) + 1 + 4;
589
255k
        *dst = imax(*dst, bottom);
590
255k
    }
591
545k
}
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
21.6k
{
598
21.6k
    const int h_mul = 4 >> ss_hor, v_mul = 4 >> ss_ver;
599
21.6k
    assert(!((b_dim[0] * h_mul) & 7) && !((b_dim[1] * v_mul) & 7));
600
21.6k
    const int32_t *const mat = wmp->matrix;
601
21.6k
    const int y = b_dim[1] * v_mul - 8; // lowest y
602
603
21.6k
    const int src_y = t->by * 4 + ((y + 4) << ss_ver);
604
21.6k
    const int64_t mat5_y = (int64_t) mat[5] * src_y + mat[1];
605
    // check left- and right-most blocks
606
53.4k
    for (int x = 0; x < b_dim[0] * h_mul; x += imax(8, b_dim[0] * h_mul - 8)) {
607
        // calculate transformation relative to center of 8x8 block in
608
        // luma pixel units
609
31.7k
        const int src_x = t->bx * 4 + ((x + 4) << ss_hor);
610
31.7k
        const int64_t mvy = ((int64_t) mat[4] * src_x + mat5_y) >> ss_ver;
611
31.7k
        const int dy = (int) (mvy >> 16) - 4;
612
31.7k
        *dst = imax(*dst, dy + 4 + 8);
613
31.7k
    }
614
21.6k
}
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
18.3k
{
620
18.3k
    affine_lowest_px(t, dst, b_dim, wmp, 0, 0);
621
18.3k
}
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.93k
{
627
6.93k
    const Dav1dFrameContext *const f = t->f;
628
6.93k
    assert(f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I400);
629
6.93k
    if (f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I444)
630
3.61k
        affine_lowest_px_luma(t, dst, b_dim, wmp);
631
3.32k
    else
632
3.32k
        affine_lowest_px(t, dst, b_dim, wmp, f->cur.p.layout & DAV1D_PIXEL_LAYOUT_I420, 1);
633
6.93k
}
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
52.1k
{
640
52.1k
    assert(!(t->bx & 1) && !(t->by & 1));
641
52.1k
    const Dav1dFrameContext *const f = t->f;
642
52.1k
    /*const*/ refmvs_block **r = &t->rt.r[(t->by & 31) + 5];
643
52.1k
    const int ss_ver = is_chroma && f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
644
52.1k
    const int ss_hor = is_chroma && f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
645
52.1k
    const int h_mul = 4 >> ss_hor, v_mul = 4 >> ss_ver;
646
647
52.1k
    if (t->by > t->ts->tiling.row_start &&
648
43.3k
        (!is_chroma || b_dim[0] * h_mul + b_dim[1] * v_mul >= 16))
649
33.9k
    {
650
71.8k
        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
37.9k
            const refmvs_block *const a_r = &r[-1][t->bx + x + 1];
653
37.9k
            const uint8_t *const a_b_dim = dav1d_block_dimensions[a_r->bs];
654
655
37.9k
            if (a_r->ref.ref[0] > 0) {
656
37.0k
                const int oh4 = imin(b_dim[1], 16) >> 1;
657
37.0k
                mc_lowest_px(&dst[a_r->ref.ref[0] - 1][is_chroma], t->by,
658
37.0k
                             (oh4 * 3 + 3) >> 2, a_r->mv.mv[0].y, ss_ver,
659
37.0k
                             &f->svc[a_r->ref.ref[0] - 1][1]);
660
37.0k
                i++;
661
37.0k
            }
662
37.9k
            x += imax(a_b_dim[0], 2);
663
37.9k
        }
664
33.9k
    }
665
666
52.1k
    if (t->bx > t->ts->tiling.col_start)
667
86.3k
        for (int i = 0, y = 0; y < h4 && i < imin(b_dim[3], 4); ) {
668
            // only odd blocks are considered for overlap handling, hence +1
669
45.0k
            const refmvs_block *const l_r = &r[y + 1][t->bx - 1];
670
45.0k
            const uint8_t *const l_b_dim = dav1d_block_dimensions[l_r->bs];
671
672
45.0k
            if (l_r->ref.ref[0] > 0) {
673
43.7k
                const int oh4 = iclip(l_b_dim[1], 2, b_dim[1]);
674
43.7k
                mc_lowest_px(&dst[l_r->ref.ref[0] - 1][is_chroma],
675
43.7k
                             t->by + y, oh4, l_r->mv.mv[0].y, ss_ver,
676
43.7k
                             &f->svc[l_r->ref.ref[0] - 1][1]);
677
43.7k
                i++;
678
43.7k
            }
679
45.0k
            y += imax(l_b_dim[1], 2);
680
45.0k
        }
681
52.1k
}
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.00M
                    const enum EdgeFlags intra_edge_flags) {
688
2.00M
    Dav1dTileState *const ts = t->ts;
689
2.00M
    const Dav1dFrameContext *const f = t->f;
690
2.00M
    Av1Block b_mem, *const b = t->frame_thread.pass ?
691
18.4E
        &f->frame_thread.b[t->by * f->b4_stride + t->bx] : &b_mem;
692
2.00M
    const uint8_t *const b_dim = dav1d_block_dimensions[bs];
693
2.00M
    const int bx4 = t->bx & 31, by4 = t->by & 31;
694
2.00M
    const int ss_ver = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
695
2.00M
    const int ss_hor = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
696
2.00M
    const int cbx4 = bx4 >> ss_hor, cby4 = by4 >> ss_ver;
697
2.00M
    const int bw4 = b_dim[0], bh4 = b_dim[1];
698
2.00M
    const int w4 = imin(bw4, f->bw - t->bx), h4 = imin(bh4, f->bh - t->by);
699
2.00M
    const int cbw4 = (bw4 + ss_hor) >> ss_hor, cbh4 = (bh4 + ss_ver) >> ss_ver;
700
2.00M
    const int have_left = t->bx > ts->tiling.col_start;
701
2.00M
    const int have_top = t->by > ts->tiling.row_start;
702
2.00M
    const int has_chroma = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I400 &&
703
1.74M
                           (bw4 > ss_hor || t->bx & 1) &&
704
1.68M
                           (bh4 > ss_ver || t->by & 1);
705
706
2.00M
    if (t->frame_thread.pass == 2) {
707
376k
        if (b->intra) {
708
281k
            f->bd_fn.recon_b_intra(t, bs, intra_edge_flags, b);
709
710
281k
            const enum IntraPredMode y_mode_nofilt =
711
281k
                b->y_mode == FILTER_PRED ? DC_PRED : b->y_mode;
712
281k
#define set_ctx(rep_macro) \
713
563k
            rep_macro(edge->mode, off, y_mode_nofilt); \
714
563k
            rep_macro(edge->intra, off, 1)
715
281k
            BlockContext *edge = t->a;
716
845k
            for (int i = 0, off = bx4; i < 2; i++, off = by4, edge = &t->l) {
717
563k
                case_set(b_dim[2 + i]);
718
563k
            }
719
281k
#undef set_ctx
720
281k
            if (IS_INTER_OR_SWITCH(f->frame_hdr)) {
721
4.11k
                refmvs_block *const r = &t->rt.r[(t->by & 31) + 5 + bh4 - 1][t->bx];
722
25.6k
                for (int x = 0; x < bw4; x++) {
723
21.5k
                    r[x].ref.ref[0] = 0;
724
21.5k
                    r[x].bs = bs;
725
21.5k
                }
726
4.11k
                refmvs_block *const *rr = &t->rt.r[(t->by & 31) + 5];
727
24.6k
                for (int y = 0; y < bh4 - 1; y++) {
728
20.5k
                    rr[y][t->bx + bw4 - 1].ref.ref[0] = 0;
729
20.5k
                    rr[y][t->bx + bw4 - 1].bs = bs;
730
20.5k
                }
731
4.11k
            }
732
733
281k
            if (has_chroma) {
734
191k
                uint8_t uv_mode = b->uv_mode;
735
191k
                dav1d_memset_pow2[ulog2(cbw4)](&t->a->uvmode[cbx4], uv_mode);
736
191k
                dav1d_memset_pow2[ulog2(cbh4)](&t->l.uvmode[cby4], uv_mode);
737
191k
            }
738
281k
        } else {
739
94.7k
            if (IS_INTER_OR_SWITCH(f->frame_hdr) /* not intrabc */ &&
740
86.9k
                b->comp_type == COMP_INTER_NONE && b->motion_mode == MM_WARP)
741
905
            {
742
905
                if (b->matrix[0] == INT16_MIN) {
743
101
                    t->warpmv.type = DAV1D_WM_TYPE_IDENTITY;
744
804
                } else {
745
804
                    t->warpmv.type = DAV1D_WM_TYPE_AFFINE;
746
804
                    t->warpmv.matrix[2] = b->matrix[0] + 0x10000;
747
804
                    t->warpmv.matrix[3] = b->matrix[1];
748
804
                    t->warpmv.matrix[4] = b->matrix[2];
749
804
                    t->warpmv.matrix[5] = b->matrix[3] + 0x10000;
750
804
                    dav1d_set_affine_mv2d(bw4, bh4, b->mv2d, &t->warpmv,
751
804
                                          t->bx, t->by);
752
804
                    dav1d_get_shear_params(&t->warpmv);
753
804
#define signabs(v) v < 0 ? '-' : ' ', abs(v)
754
804
                    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
804
#undef signabs
769
804
                }
770
905
            }
771
94.7k
            if (f->bd_fn.recon_b_inter(t, bs, b)) return -1;
772
773
94.7k
            const uint8_t *const filter = dav1d_filter_dir[b->filter2d];
774
94.7k
            BlockContext *edge = t->a;
775
284k
            for (int i = 0, off = bx4; i < 2; i++, off = by4, edge = &t->l) {
776
189k
#define set_ctx(rep_macro) \
777
189k
                rep_macro(edge->filter[0], off, filter[0]); \
778
189k
                rep_macro(edge->filter[1], off, filter[1]); \
779
189k
                rep_macro(edge->intra, off, 0)
780
189k
                case_set(b_dim[2 + i]);
781
189k
#undef set_ctx
782
189k
            }
783
784
94.7k
            if (IS_INTER_OR_SWITCH(f->frame_hdr)) {
785
86.9k
                refmvs_block *const r = &t->rt.r[(t->by & 31) + 5 + bh4 - 1][t->bx];
786
86.9k
                const int ref1 = b->ref[0] + 1;
787
86.9k
                const union mv mv1 = b->mv[0];
788
403k
                for (int x = 0; x < bw4; x++) {
789
316k
                    r[x].ref.ref[0] = ref1;
790
316k
                    r[x].mv.mv[0] = mv1;
791
316k
                    r[x].bs = bs;
792
316k
                }
793
86.9k
                refmvs_block *const *rr = &t->rt.r[(t->by & 31) + 5];
794
387k
                for (int y = 0; y < bh4 - 1; y++) {
795
300k
                    rr[y][t->bx + bw4 - 1].ref.ref[0] = ref1;
796
300k
                    rr[y][t->bx + bw4 - 1].mv.mv[0] = mv1;
797
300k
                    rr[y][t->bx + bw4 - 1].bs = bs;
798
300k
                }
799
86.9k
            }
800
801
94.7k
            if (has_chroma) {
802
67.7k
                dav1d_memset_pow2[ulog2(cbw4)](&t->a->uvmode[cbx4], DC_PRED);
803
67.7k
                dav1d_memset_pow2[ulog2(cbh4)](&t->l.uvmode[cby4], DC_PRED);
804
67.7k
            }
805
94.7k
        }
806
376k
        return 0;
807
376k
    }
808
809
1.62M
    const int cw4 = (w4 + ss_hor) >> ss_hor, ch4 = (h4 + ss_ver) >> ss_ver;
810
811
1.62M
    b->bl = bl;
812
1.62M
    b->bp = bp;
813
1.62M
    b->bs = bs;
814
815
1.62M
    const Dav1dSegmentationData *seg = NULL;
816
817
    // segment_id (if seg_feature for skip/ref/gmv is enabled)
818
1.62M
    int seg_pred = 0;
819
1.62M
    if (f->frame_hdr->segmentation.enabled) {
820
605k
        if (!f->frame_hdr->segmentation.update_map) {
821
40.3k
            if (f->prev_segmap) {
822
19.5k
                unsigned seg_id = get_prev_frame_segid(f, t->by, t->bx, w4, h4,
823
19.5k
                                                       f->prev_segmap,
824
19.5k
                                                       f->b4_stride);
825
19.5k
                if (seg_id >= 8) return -1;
826
19.5k
                b->seg_id = seg_id;
827
20.8k
            } else {
828
20.8k
                b->seg_id = 0;
829
20.8k
            }
830
40.3k
            seg = &f->frame_hdr->segmentation.seg_data.d[b->seg_id];
831
564k
        } else if (f->frame_hdr->segmentation.seg_data.preskip) {
832
507k
            if (f->frame_hdr->segmentation.temporal &&
833
7.81k
                (seg_pred = dav1d_msac_decode_bool_adapt(&ts->msac,
834
7.81k
                                ts->cdf.m.seg_pred[t->a->seg_pred[bx4] +
835
7.81k
                                t->l.seg_pred[by4]])))
836
3.92k
            {
837
                // temporal predicted seg_id
838
3.92k
                if (f->prev_segmap) {
839
3.00k
                    unsigned seg_id = get_prev_frame_segid(f, t->by, t->bx,
840
3.00k
                                                           w4, h4,
841
3.00k
                                                           f->prev_segmap,
842
3.00k
                                                           f->b4_stride);
843
3.00k
                    if (seg_id >= 8) return -1;
844
3.00k
                    b->seg_id = seg_id;
845
3.00k
                } else {
846
914
                    b->seg_id = 0;
847
914
                }
848
503k
            } else {
849
503k
                int seg_ctx;
850
503k
                const unsigned pred_seg_id =
851
503k
                    get_cur_frame_segid(t->by, t->bx, have_top, have_left,
852
503k
                                        &seg_ctx, f->cur_segmap, f->b4_stride);
853
503k
                const unsigned diff = dav1d_msac_decode_symbol_adapt8(&ts->msac,
854
503k
                                          ts->cdf.m.seg_id[seg_ctx],
855
503k
                                          DAV1D_MAX_SEGMENTS - 1);
856
503k
                const unsigned last_active_seg_id =
857
503k
                    f->frame_hdr->segmentation.seg_data.last_active_segid;
858
503k
                b->seg_id = neg_deinterleave(diff, pred_seg_id,
859
503k
                                             last_active_seg_id + 1);
860
503k
                if (b->seg_id > last_active_seg_id) b->seg_id = 0; // error?
861
503k
                if (b->seg_id >= DAV1D_MAX_SEGMENTS) b->seg_id = 0; // error?
862
503k
            }
863
864
507k
            if (DEBUG_BLOCK_INFO)
865
0
                printf("Post-segid[preskip;%d]: r=%d\n",
866
0
                       b->seg_id, ts->msac.rng);
867
868
507k
            seg = &f->frame_hdr->segmentation.seg_data.d[b->seg_id];
869
507k
        }
870
1.02M
    } else {
871
1.02M
        b->seg_id = 0;
872
1.02M
    }
873
874
    // skip_mode
875
1.62M
    if ((!seg || (!seg->globalmv && seg->ref == -1 && !seg->skip)) &&
876
1.22M
        f->frame_hdr->skip_mode_enabled && imin(bw4, bh4) > 1)
877
1.71k
    {
878
1.71k
        const int smctx = t->a->skip_mode[bx4] + t->l.skip_mode[by4];
879
1.71k
        b->skip_mode = dav1d_msac_decode_bool_adapt(&ts->msac,
880
1.71k
                           ts->cdf.m.skip_mode[smctx]);
881
1.71k
        if (DEBUG_BLOCK_INFO)
882
0
            printf("Post-skipmode[%d]: r=%d\n", b->skip_mode, ts->msac.rng);
883
1.62M
    } else {
884
1.62M
        b->skip_mode = 0;
885
1.62M
    }
886
887
    // skip
888
1.62M
    if (b->skip_mode || (seg && seg->skip)) {
889
325k
        b->skip = 1;
890
1.30M
    } else {
891
1.30M
        const int sctx = t->a->skip[bx4] + t->l.skip[by4];
892
1.30M
        b->skip = dav1d_msac_decode_bool_adapt(&ts->msac, ts->cdf.m.skip[sctx]);
893
1.30M
        if (DEBUG_BLOCK_INFO)
894
0
            printf("Post-skip[%d]: r=%d\n", b->skip, ts->msac.rng);
895
1.30M
    }
896
897
    // segment_id
898
1.62M
    if (f->frame_hdr->segmentation.enabled &&
899
605k
        f->frame_hdr->segmentation.update_map &&
900
565k
        !f->frame_hdr->segmentation.seg_data.preskip)
901
57.2k
    {
902
57.2k
        if (!b->skip && f->frame_hdr->segmentation.temporal &&
903
5.04k
            (seg_pred = dav1d_msac_decode_bool_adapt(&ts->msac,
904
5.04k
                            ts->cdf.m.seg_pred[t->a->seg_pred[bx4] +
905
5.04k
                            t->l.seg_pred[by4]])))
906
2.56k
        {
907
            // temporal predicted seg_id
908
2.56k
            if (f->prev_segmap) {
909
1.45k
                unsigned seg_id = get_prev_frame_segid(f, t->by, t->bx, w4, h4,
910
1.45k
                                                       f->prev_segmap,
911
1.45k
                                                       f->b4_stride);
912
1.45k
                if (seg_id >= 8) return -1;
913
1.45k
                b->seg_id = seg_id;
914
1.45k
            } else {
915
1.10k
                b->seg_id = 0;
916
1.10k
            }
917
54.7k
        } else {
918
54.7k
            int seg_ctx;
919
54.7k
            const unsigned pred_seg_id =
920
54.7k
                get_cur_frame_segid(t->by, t->bx, have_top, have_left,
921
54.7k
                                    &seg_ctx, f->cur_segmap, f->b4_stride);
922
54.7k
            if (b->skip) {
923
20.2k
                b->seg_id = pred_seg_id;
924
34.4k
            } else {
925
34.4k
                const unsigned diff = dav1d_msac_decode_symbol_adapt8(&ts->msac,
926
34.4k
                                          ts->cdf.m.seg_id[seg_ctx],
927
34.4k
                                          DAV1D_MAX_SEGMENTS - 1);
928
34.4k
                const unsigned last_active_seg_id =
929
34.4k
                    f->frame_hdr->segmentation.seg_data.last_active_segid;
930
34.4k
                b->seg_id = neg_deinterleave(diff, pred_seg_id,
931
34.4k
                                             last_active_seg_id + 1);
932
34.4k
                if (b->seg_id > last_active_seg_id) b->seg_id = 0; // error?
933
34.4k
            }
934
54.7k
            if (b->seg_id >= DAV1D_MAX_SEGMENTS) b->seg_id = 0; // error?
935
54.7k
        }
936
937
57.2k
        seg = &f->frame_hdr->segmentation.seg_data.d[b->seg_id];
938
939
57.2k
        if (DEBUG_BLOCK_INFO)
940
0
            printf("Post-segid[postskip;%d]: r=%d\n",
941
0
                   b->seg_id, ts->msac.rng);
942
57.2k
    }
943
944
    // cdef index
945
1.62M
    if (!b->skip) {
946
738k
        const int idx = f->seq_hdr->sb128 ? ((t->bx & 16) >> 4) +
947
427k
                                           ((t->by & 16) >> 3) : 0;
948
738k
        if (t->cur_sb_cdef_idx_ptr[idx] == -1) {
949
203k
            const int v = dav1d_msac_decode_bools(&ts->msac,
950
203k
                              f->frame_hdr->cdef.n_bits);
951
203k
            t->cur_sb_cdef_idx_ptr[idx] = v;
952
203k
            if (bw4 > 16) t->cur_sb_cdef_idx_ptr[idx + 1] = v;
953
203k
            if (bh4 > 16) t->cur_sb_cdef_idx_ptr[idx + 2] = v;
954
203k
            if (bw4 == 32 && bh4 == 32) t->cur_sb_cdef_idx_ptr[idx + 3] = v;
955
956
203k
            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
203k
        }
960
738k
    }
961
962
    // delta-q/lf
963
1.62M
    if (!((t->bx | t->by) & (31 >> !f->seq_hdr->sb128))) {
964
296k
        const int prev_qidx = ts->last_qidx;
965
296k
        const int have_delta_q = f->frame_hdr->delta.q.present &&
966
74.1k
            (bs != (f->seq_hdr->sb128 ? BS_128x128 : BS_64x64) || !b->skip);
967
968
296k
        uint32_t prev_delta_lf = ts->last_delta_lf.u32;
969
970
296k
        if (have_delta_q) {
971
64.5k
            int delta_q = dav1d_msac_decode_symbol_adapt4(&ts->msac,
972
64.5k
                                                          ts->cdf.m.delta_q, 3);
973
64.5k
            if (delta_q == 3) {
974
9.69k
                const int n_bits = 1 + dav1d_msac_decode_bools(&ts->msac, 3);
975
9.69k
                delta_q = dav1d_msac_decode_bools(&ts->msac, n_bits) +
976
9.69k
                          1 + (1 << n_bits);
977
9.69k
            }
978
64.5k
            if (delta_q) {
979
15.9k
                if (dav1d_msac_decode_bool_equi(&ts->msac)) delta_q = -delta_q;
980
15.9k
                delta_q *= 1 << f->frame_hdr->delta.q.res_log2;
981
15.9k
            }
982
64.5k
            ts->last_qidx = iclip(ts->last_qidx + delta_q, 1, 255);
983
64.5k
            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
64.5k
            if (f->frame_hdr->delta.lf.present) {
988
33.0k
                const int n_lfs = f->frame_hdr->delta.lf.multi ?
989
20.0k
                    f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I400 ? 4 : 2 : 1;
990
991
124k
                for (int i = 0; i < n_lfs; i++) {
992
91.6k
                    int delta_lf = dav1d_msac_decode_symbol_adapt4(&ts->msac,
993
91.6k
                        ts->cdf.m.delta_lf[i + f->frame_hdr->delta.lf.multi], 3);
994
91.6k
                    if (delta_lf == 3) {
995
15.1k
                        const int n_bits = 1 + dav1d_msac_decode_bools(&ts->msac, 3);
996
15.1k
                        delta_lf = dav1d_msac_decode_bools(&ts->msac, n_bits) +
997
15.1k
                                   1 + (1 << n_bits);
998
15.1k
                    }
999
91.6k
                    if (delta_lf) {
1000
24.4k
                        if (dav1d_msac_decode_bool_equi(&ts->msac))
1001
19.3k
                            delta_lf = -delta_lf;
1002
24.4k
                        delta_lf *= 1 << f->frame_hdr->delta.lf.res_log2;
1003
24.4k
                    }
1004
91.6k
                    ts->last_delta_lf.i8[i] =
1005
91.6k
                        iclip(ts->last_delta_lf.i8[i] + delta_lf, -63, 63);
1006
91.6k
                    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
91.6k
                }
1010
33.0k
            }
1011
64.5k
        }
1012
296k
        if (ts->last_qidx == f->frame_hdr->quant.yac) {
1013
            // assign frame-wide q values to this sb
1014
264k
            ts->dq = f->dq;
1015
264k
        } else if (ts->last_qidx != prev_qidx) {
1016
            // find sb-specific quant parameters
1017
6.57k
            init_quant_tables(f->seq_hdr, f->frame_hdr, ts->last_qidx, ts->dqmem);
1018
6.57k
            ts->dq = ts->dqmem;
1019
6.57k
        }
1020
296k
        if (!ts->last_delta_lf.u32) {
1021
            // assign frame-wide lf values to this sb
1022
274k
            ts->lflvl = f->lf.lvl;
1023
274k
        } else if (ts->last_delta_lf.u32 != prev_delta_lf) {
1024
            // find sb-specific lf lvl parameters
1025
8.01k
            ts->lflvl = ts->lflvlmem;
1026
8.01k
            dav1d_calc_lf_values(ts->lflvlmem, f->frame_hdr, ts->last_delta_lf.i8);
1027
8.01k
        }
1028
296k
    }
1029
1030
1.62M
    if (b->skip_mode) {
1031
627
        b->intra = 0;
1032
1.62M
    } else if (IS_INTER_OR_SWITCH(f->frame_hdr)) {
1033
239k
        if (seg && (seg->ref >= 0 || seg->globalmv)) {
1034
46.9k
            b->intra = !seg->ref;
1035
192k
        } else {
1036
192k
            const int ictx = get_intra_ctx(t->a, &t->l, by4, bx4,
1037
192k
                                           have_top, have_left);
1038
192k
            b->intra = !dav1d_msac_decode_bool_adapt(&ts->msac,
1039
192k
                            ts->cdf.m.intra[ictx]);
1040
192k
            if (DEBUG_BLOCK_INFO)
1041
0
                printf("Post-intra[%d]: r=%d\n", b->intra, ts->msac.rng);
1042
192k
        }
1043
1.38M
    } else if (f->frame_hdr->allow_intrabc) {
1044
629k
        b->intra = !dav1d_msac_decode_bool_adapt(&ts->msac, ts->cdf.m.intrabc);
1045
629k
        if (DEBUG_BLOCK_INFO)
1046
0
            printf("Post-intrabcflag[%d]: r=%d\n", b->intra, ts->msac.rng);
1047
760k
    } else {
1048
760k
        b->intra = 1;
1049
760k
    }
1050
1051
    // intra/inter-specific stuff
1052
1.62M
    if (b->intra) {
1053
1.15M
        uint16_t *const ymode_cdf = IS_INTER_OR_SWITCH(f->frame_hdr) ?
1054
17.3k
            ts->cdf.m.y_mode[dav1d_ymode_size_context[bs]] :
1055
1.15M
            ts->cdf.kfym[dav1d_intra_mode_context[t->a->mode[bx4]]]
1056
1.14M
                        [dav1d_intra_mode_context[t->l.mode[by4]]];
1057
1.15M
        b->y_mode = dav1d_msac_decode_symbol_adapt16(&ts->msac, ymode_cdf,
1058
1.15M
                                                     N_INTRA_PRED_MODES - 1);
1059
1.15M
        if (DEBUG_BLOCK_INFO)
1060
0
            printf("Post-ymode[%d]: r=%d\n", b->y_mode, ts->msac.rng);
1061
1062
        // angle delta
1063
1.15M
        if (b_dim[2] + b_dim[3] >= 2 && b->y_mode >= VERT_PRED &&
1064
491k
            b->y_mode <= VERT_LEFT_PRED)
1065
225k
        {
1066
225k
            uint16_t *const acdf = ts->cdf.m.angle_delta[b->y_mode - VERT_PRED];
1067
225k
            const int angle = dav1d_msac_decode_symbol_adapt8(&ts->msac, acdf, 6);
1068
225k
            b->y_angle = angle - 3;
1069
934k
        } else {
1070
934k
            b->y_angle = 0;
1071
934k
        }
1072
1073
1.15M
        if (has_chroma) {
1074
1.01M
            const int cfl_allowed = f->frame_hdr->segmentation.lossless[b->seg_id] ?
1075
952k
                cbw4 == 1 && cbh4 == 1 : !!(cfl_allowed_mask & (1 << bs));
1076
1.01M
            uint16_t *const uvmode_cdf = ts->cdf.m.uv_mode[cfl_allowed][b->y_mode];
1077
1.01M
            b->uv_mode = dav1d_msac_decode_symbol_adapt16(&ts->msac, uvmode_cdf,
1078
1.01M
                             N_UV_INTRA_PRED_MODES - 1 - !cfl_allowed);
1079
1.01M
            if (DEBUG_BLOCK_INFO)
1080
0
                printf("Post-uvmode[%d]: r=%d\n", b->uv_mode, ts->msac.rng);
1081
1082
1.01M
            b->uv_angle = 0;
1083
1.01M
            if (b->uv_mode == CFL_PRED) {
1084
308k
#define SIGN(a) (!!(a) + ((a) > 0))
1085
308k
                const int sign = dav1d_msac_decode_symbol_adapt8(&ts->msac,
1086
308k
                                     ts->cdf.m.cfl_sign, 7) + 1;
1087
308k
                const int sign_u = sign * 0x56 >> 8, sign_v = sign - sign_u * 3;
1088
308k
                assert(sign_u == sign / 3);
1089
308k
                if (sign_u) {
1090
298k
                    const int ctx = (sign_u == 2) * 3 + sign_v;
1091
298k
                    b->cfl_alpha[0] = dav1d_msac_decode_symbol_adapt16(&ts->msac,
1092
298k
                                          ts->cdf.m.cfl_alpha[ctx], 15) + 1;
1093
298k
                    if (sign_u == 1) b->cfl_alpha[0] = -b->cfl_alpha[0];
1094
298k
                } else {
1095
9.73k
                    b->cfl_alpha[0] = 0;
1096
9.73k
                }
1097
308k
                if (sign_v) {
1098
253k
                    const int ctx = (sign_v == 2) * 3 + sign_u;
1099
253k
                    b->cfl_alpha[1] = dav1d_msac_decode_symbol_adapt16(&ts->msac,
1100
253k
                                          ts->cdf.m.cfl_alpha[ctx], 15) + 1;
1101
253k
                    if (sign_v == 1) b->cfl_alpha[1] = -b->cfl_alpha[1];
1102
253k
                } else {
1103
55.1k
                    b->cfl_alpha[1] = 0;
1104
55.1k
                }
1105
308k
#undef SIGN
1106
308k
                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
709k
            } else if (b_dim[2] + b_dim[3] >= 2 && b->uv_mode >= VERT_PRED &&
1110
372k
                       b->uv_mode <= VERT_LEFT_PRED)
1111
169k
            {
1112
169k
                uint16_t *const acdf = ts->cdf.m.angle_delta[b->uv_mode - VERT_PRED];
1113
169k
                const int angle = dav1d_msac_decode_symbol_adapt8(&ts->msac, acdf, 6);
1114
169k
                b->uv_angle = angle - 3;
1115
169k
            }
1116
1.01M
        }
1117
1118
1.15M
        b->pal_sz[0] = b->pal_sz[1] = 0;
1119
1.15M
        if (f->frame_hdr->allow_screen_content_tools &&
1120
520k
            imax(bw4, bh4) <= 16 && bw4 + bh4 >= 4)
1121
407k
        {
1122
407k
            const int sz_ctx = b_dim[2] + b_dim[3] - 2;
1123
407k
            if (b->y_mode == DC_PRED) {
1124
188k
                const int pal_ctx = (t->a->pal_sz[bx4] > 0) + (t->l.pal_sz[by4] > 0);
1125
188k
                const int use_y_pal = dav1d_msac_decode_bool_adapt(&ts->msac,
1126
188k
                                          ts->cdf.m.pal_y[sz_ctx][pal_ctx]);
1127
188k
                if (DEBUG_BLOCK_INFO)
1128
0
                    printf("Post-y_pal[%d]: r=%d\n", use_y_pal, ts->msac.rng);
1129
188k
                if (use_y_pal)
1130
15.8k
                    f->bd_fn.read_pal_plane(t, b, 0, sz_ctx, bx4, by4);
1131
188k
            }
1132
1133
407k
            if (has_chroma && b->uv_mode == DC_PRED) {
1134
119k
                const int pal_ctx = b->pal_sz[0] > 0;
1135
119k
                const int use_uv_pal = dav1d_msac_decode_bool_adapt(&ts->msac,
1136
119k
                                           ts->cdf.m.pal_uv[pal_ctx]);
1137
119k
                if (DEBUG_BLOCK_INFO)
1138
0
                    printf("Post-uv_pal[%d]: r=%d\n", use_uv_pal, ts->msac.rng);
1139
119k
                if (use_uv_pal) // see aomedia bug 2183 for why we use luma coordinates
1140
5.52k
                    f->bd_fn.read_pal_uv(t, b, sz_ctx, bx4, by4);
1141
119k
            }
1142
407k
        }
1143
1144
1.15M
        if (b->y_mode == DC_PRED && !b->pal_sz[0] &&
1145
426k
            imax(b_dim[2], b_dim[3]) <= 3 && f->seq_hdr->filter_intra)
1146
168k
        {
1147
168k
            const int is_filter = dav1d_msac_decode_bool_adapt(&ts->msac,
1148
168k
                                      ts->cdf.m.use_filter_intra[bs]);
1149
168k
            if (is_filter) {
1150
111k
                b->y_mode = FILTER_PRED;
1151
111k
                b->y_angle = dav1d_msac_decode_symbol_adapt8(&ts->msac,
1152
111k
                                 ts->cdf.m.filter_intra, 4);
1153
111k
            }
1154
168k
            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
168k
        }
1158
1159
1.15M
        if (b->pal_sz[0]) {
1160
15.8k
            uint8_t *pal_idx;
1161
15.8k
            if (t->frame_thread.pass) {
1162
15.8k
                const int p = t->frame_thread.pass & 1;
1163
15.8k
                assert(ts->frame_thread[p].pal_idx);
1164
15.8k
                pal_idx = ts->frame_thread[p].pal_idx;
1165
15.8k
                ts->frame_thread[p].pal_idx += bw4 * bh4 * 8;
1166
15.8k
            } else
1167
0
                pal_idx = t->scratch.pal_idx_y;
1168
15.8k
            read_pal_indices(t, pal_idx, b->pal_sz[0], 0, w4, h4, bw4, bh4);
1169
15.8k
            if (DEBUG_BLOCK_INFO)
1170
0
                printf("Post-y-pal-indices: r=%d\n", ts->msac.rng);
1171
15.8k
        }
1172
1173
1.15M
        if (has_chroma && b->pal_sz[1]) {
1174
5.52k
            uint8_t *pal_idx;
1175
5.52k
            if (t->frame_thread.pass) {
1176
5.52k
                const int p = t->frame_thread.pass & 1;
1177
5.52k
                assert(ts->frame_thread[p].pal_idx);
1178
5.52k
                pal_idx = ts->frame_thread[p].pal_idx;
1179
5.52k
                ts->frame_thread[p].pal_idx += cbw4 * cbh4 * 8;
1180
5.52k
            } else
1181
0
                pal_idx = t->scratch.pal_idx_uv;
1182
5.52k
            read_pal_indices(t, pal_idx, b->pal_sz[1], 1, cw4, ch4, cbw4, cbh4);
1183
5.52k
            if (DEBUG_BLOCK_INFO)
1184
0
                printf("Post-uv-pal-indices: r=%d\n", ts->msac.rng);
1185
5.52k
        }
1186
1187
1.15M
        const TxfmInfo *t_dim;
1188
1.15M
        if (f->frame_hdr->segmentation.lossless[b->seg_id]) {
1189
73.4k
            b->tx = b->uvtx = (int) TX_4X4;
1190
73.4k
            t_dim = &dav1d_txfm_dimensions[TX_4X4];
1191
1.08M
        } else {
1192
1.08M
            b->tx = dav1d_max_txfm_size_for_bs[bs][0];
1193
1.08M
            b->uvtx = dav1d_max_txfm_size_for_bs[bs][f->cur.p.layout];
1194
1.08M
            t_dim = &dav1d_txfm_dimensions[b->tx];
1195
1.08M
            if (f->frame_hdr->txfm_mode == DAV1D_TX_SWITCHABLE && t_dim->max > TX_4X4) {
1196
270k
                const int tctx = get_tx_ctx(t->a, &t->l, t_dim, by4, bx4);
1197
270k
                uint16_t *const tx_cdf = ts->cdf.m.txsz[t_dim->max - 1][tctx];
1198
270k
                int depth = dav1d_msac_decode_symbol_adapt4(&ts->msac, tx_cdf,
1199
270k
                                imin(t_dim->max, 2));
1200
1201
511k
                while (depth--) {
1202
241k
                    b->tx = t_dim->sub;
1203
241k
                    t_dim = &dav1d_txfm_dimensions[b->tx];
1204
241k
                }
1205
270k
            }
1206
1.08M
            if (DEBUG_BLOCK_INFO)
1207
0
                printf("Post-tx[%d]: r=%d\n", b->tx, ts->msac.rng);
1208
1.08M
        }
1209
1210
        // reconstruction
1211
1.15M
        if (t->frame_thread.pass == 1) {
1212
1.15M
            f->bd_fn.read_coef_blocks(t, bs, b);
1213
1.15M
        } else {
1214
220
            f->bd_fn.recon_b_intra(t, bs, intra_edge_flags, b);
1215
220
        }
1216
1217
1.15M
        if (f->frame_hdr->loopfilter.level_y[0] ||
1218
652k
            f->frame_hdr->loopfilter.level_y[1])
1219
597k
        {
1220
597k
            dav1d_create_lf_mask_intra(t->lf_mask, f->lf.level, f->b4_stride,
1221
597k
                                       (const uint8_t (*)[8][2])
1222
597k
                                       &ts->lflvl[b->seg_id][0][0][0],
1223
597k
                                       t->bx, t->by, f->w4, f->h4, bs,
1224
597k
                                       b->tx, b->uvtx, f->cur.p.layout,
1225
597k
                                       &t->a->tx_lpf_y[bx4], &t->l.tx_lpf_y[by4],
1226
597k
                                       has_chroma ? &t->a->tx_lpf_uv[cbx4] : NULL,
1227
597k
                                       has_chroma ? &t->l.tx_lpf_uv[cby4] : NULL);
1228
597k
        }
1229
        // update contexts
1230
1.15M
        const enum IntraPredMode y_mode_nofilt =
1231
1.15M
            b->y_mode == FILTER_PRED ? DC_PRED : b->y_mode;
1232
1.15M
        BlockContext *edge = t->a;
1233
3.47M
        for (int i = 0, off = bx4; i < 2; i++, off = by4, edge = &t->l) {
1234
2.32M
            int t_lsz = ((uint8_t *) &t_dim->lw)[i]; // lw then lh
1235
2.32M
#define set_ctx(rep_macro) \
1236
2.32M
            rep_macro(edge->tx_intra, off, t_lsz); \
1237
2.32M
            rep_macro(edge->tx, off, t_lsz); \
1238
2.32M
            rep_macro(edge->mode, off, y_mode_nofilt); \
1239
2.32M
            rep_macro(edge->pal_sz, off, b->pal_sz[0]); \
1240
2.32M
            rep_macro(edge->seg_pred, off, seg_pred); \
1241
2.32M
            rep_macro(edge->skip_mode, off, 0); \
1242
2.32M
            rep_macro(edge->intra, off, 1); \
1243
2.32M
            rep_macro(edge->skip, off, b->skip); \
1244
            /* see aomedia bug 2183 for why we use luma coordinates here */ \
1245
2.32M
            rep_macro(t->pal_sz_uv[i], off, (has_chroma ? b->pal_sz[1] : 0)); \
1246
2.32M
            if (IS_INTER_OR_SWITCH(f->frame_hdr)) { \
1247
34.6k
                rep_macro(edge->comp_type, off, COMP_INTER_NONE); \
1248
34.6k
                rep_macro(edge->ref[0], off, ((uint8_t) -1)); \
1249
34.6k
                rep_macro(edge->ref[1], off, ((uint8_t) -1)); \
1250
34.6k
                rep_macro(edge->filter[0], off, DAV1D_N_SWITCHABLE_FILTERS); \
1251
34.6k
                rep_macro(edge->filter[1], off, DAV1D_N_SWITCHABLE_FILTERS); \
1252
34.6k
            }
1253
2.32M
            case_set(b_dim[2 + i]);
1254
2.32M
#undef set_ctx
1255
2.32M
        }
1256
1.15M
        if (b->pal_sz[0])
1257
15.8k
            f->bd_fn.copy_pal_block_y(t, bx4, by4, bw4, bh4);
1258
1.15M
        if (has_chroma) {
1259
1.01M
            uint8_t uv_mode = b->uv_mode;
1260
1.01M
            dav1d_memset_pow2[ulog2(cbw4)](&t->a->uvmode[cbx4], uv_mode);
1261
1.01M
            dav1d_memset_pow2[ulog2(cbh4)](&t->l.uvmode[cby4], uv_mode);
1262
1.01M
            if (b->pal_sz[1])
1263
5.52k
                f->bd_fn.copy_pal_block_uv(t, bx4, by4, bw4, bh4);
1264
1.01M
        }
1265
1.15M
        if (IS_INTER_OR_SWITCH(f->frame_hdr) || f->frame_hdr->allow_intrabc)
1266
400k
            splat_intraref(f->c, t, bs, bw4, bh4);
1267
1.15M
    } else if (IS_KEY_OR_INTRA(f->frame_hdr)) {
1268
        // intra block copy
1269
246k
        refmvs_candidate mvstack[8];
1270
246k
        int n_mvs, ctx;
1271
246k
        dav1d_refmvs_find(&t->rt, mvstack, &n_mvs, &ctx,
1272
246k
                          (union refmvs_refpair) { .ref = { 0, -1 }},
1273
246k
                          bs, intra_edge_flags, t->by, t->bx);
1274
1275
246k
        if (mvstack[0].mv.mv[0].n)
1276
227k
            b->mv[0] = mvstack[0].mv.mv[0];
1277
19.3k
        else if (mvstack[1].mv.mv[0].n)
1278
0
            b->mv[0] = mvstack[1].mv.mv[0];
1279
19.3k
        else {
1280
19.3k
            if (t->by - (16 << f->seq_hdr->sb128) < ts->tiling.row_start) {
1281
15.1k
                b->mv[0].y = 0;
1282
15.1k
                b->mv[0].x = -(512 << f->seq_hdr->sb128) - 2048;
1283
15.1k
            } else {
1284
4.17k
                b->mv[0].y = -(512 << f->seq_hdr->sb128);
1285
4.17k
                b->mv[0].x = 0;
1286
4.17k
            }
1287
19.3k
        }
1288
1289
246k
        const union mv ref = b->mv[0];
1290
246k
        read_mv_residual(ts, &b->mv[0], -1);
1291
1292
        // clip intrabc motion vector to decoded parts of current tile
1293
246k
        int border_left = ts->tiling.col_start * 4;
1294
246k
        int border_top  = ts->tiling.row_start * 4;
1295
246k
        if (has_chroma) {
1296
205k
            if (bw4 < 2 &&  ss_hor)
1297
12.6k
                border_left += 4;
1298
205k
            if (bh4 < 2 &&  ss_ver)
1299
10.8k
                border_top  += 4;
1300
205k
        }
1301
246k
        int src_left   = t->bx * 4 + (b->mv[0].x >> 3);
1302
246k
        int src_top    = t->by * 4 + (b->mv[0].y >> 3);
1303
246k
        int src_right  = src_left + bw4 * 4;
1304
246k
        int src_bottom = src_top  + bh4 * 4;
1305
246k
        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
246k
        if (src_left < border_left) {
1309
83.2k
            src_right += border_left - src_left;
1310
83.2k
            src_left  += border_left - src_left;
1311
163k
        } else if (src_right > border_right) {
1312
75.8k
            src_left  -= src_right - border_right;
1313
75.8k
            src_right -= src_right - border_right;
1314
75.8k
        }
1315
        // check against top tile boundary and adjust if necessary
1316
246k
        if (src_top < border_top) {
1317
199k
            src_bottom += border_top - src_top;
1318
199k
            src_top    += border_top - src_top;
1319
199k
        }
1320
1321
246k
        const int sbx = (t->bx >> (4 + f->seq_hdr->sb128)) << (6 + f->seq_hdr->sb128);
1322
246k
        const int sby = (t->by >> (4 + f->seq_hdr->sb128)) << (6 + f->seq_hdr->sb128);
1323
246k
        const int sb_size = 1 << (6 + f->seq_hdr->sb128);
1324
        // check for overlap with current superblock
1325
246k
        if (src_bottom > sby && src_right > sbx) {
1326
75.5k
            if (src_top - border_top >= src_bottom - sby) {
1327
                // if possible move src up into the previous suberblock row
1328
1.16k
                src_top    -= src_bottom - sby;
1329
1.16k
                src_bottom -= src_bottom - sby;
1330
74.3k
            } else if (src_left - border_left >= src_right - sbx) {
1331
                // if possible move src left into the previous suberblock
1332
73.8k
                src_left  -= src_right - sbx;
1333
73.8k
                src_right -= src_right - sbx;
1334
73.8k
            }
1335
75.5k
        }
1336
        // move src up if it is below current superblock row
1337
246k
        if (src_bottom > sby + sb_size) {
1338
2.59k
            src_top    -= src_bottom - (sby + sb_size);
1339
2.59k
            src_bottom -= src_bottom - (sby + sb_size);
1340
2.59k
        }
1341
        // error out if mv still overlaps with the current superblock
1342
246k
        if (src_bottom > sby && src_right > sbx)
1343
532
            return -1;
1344
1345
246k
        b->mv[0].x = (src_left - t->bx * 4) * 8;
1346
246k
        b->mv[0].y = (src_top  - t->by * 4) * 8;
1347
1348
246k
        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
246k
        read_vartx_tree(t, b, bs, bx4, by4);
1353
1354
        // reconstruction
1355
246k
        if (t->frame_thread.pass == 1) {
1356
246k
            f->bd_fn.read_coef_blocks(t, bs, b);
1357
246k
            b->filter2d = FILTER_2D_BILINEAR;
1358
18.4E
        } else {
1359
18.4E
            if (f->bd_fn.recon_b_inter(t, bs, b)) return -1;
1360
18.4E
        }
1361
1362
246k
        splat_intrabc_mv(f->c, t, bs, b, bw4, bh4);
1363
246k
        BlockContext *edge = t->a;
1364
739k
        for (int i = 0, off = bx4; i < 2; i++, off = by4, edge = &t->l) {
1365
492k
#define set_ctx(rep_macro) \
1366
492k
            rep_macro(edge->tx_intra, off, b_dim[2 + i]); \
1367
492k
            rep_macro(edge->mode, off, DC_PRED); \
1368
492k
            rep_macro(edge->pal_sz, off, 0); \
1369
            /* see aomedia bug 2183 for why this is outside if (has_chroma) */ \
1370
492k
            rep_macro(t->pal_sz_uv[i], off, 0); \
1371
492k
            rep_macro(edge->seg_pred, off, seg_pred); \
1372
492k
            rep_macro(edge->skip_mode, off, 0); \
1373
492k
            rep_macro(edge->intra, off, 0); \
1374
492k
            rep_macro(edge->skip, off, b->skip)
1375
492k
            case_set(b_dim[2 + i]);
1376
492k
#undef set_ctx
1377
492k
        }
1378
246k
        if (has_chroma) {
1379
205k
            dav1d_memset_pow2[ulog2(cbw4)](&t->a->uvmode[cbx4], DC_PRED);
1380
205k
            dav1d_memset_pow2[ulog2(cbh4)](&t->l.uvmode[cby4], DC_PRED);
1381
205k
        }
1382
246k
    } else {
1383
        // inter-specific mode/mv coding
1384
223k
        int is_comp, has_subpel_filter;
1385
1386
223k
        if (b->skip_mode) {
1387
627
            is_comp = 1;
1388
222k
        } else if ((!seg || (seg->ref == -1 && !seg->globalmv && !seg->skip)) &&
1389
175k
                   f->frame_hdr->switchable_comp_refs && imin(bw4, bh4) > 1)
1390
72.6k
        {
1391
72.6k
            const int ctx = get_comp_ctx(t->a, &t->l, by4, bx4,
1392
72.6k
                                         have_top, have_left);
1393
72.6k
            is_comp = dav1d_msac_decode_bool_adapt(&ts->msac,
1394
72.6k
                          ts->cdf.m.comp[ctx]);
1395
72.6k
            if (DEBUG_BLOCK_INFO)
1396
0
                printf("Post-compflag[%d]: r=%d\n", is_comp, ts->msac.rng);
1397
150k
        } else {
1398
150k
            is_comp = 0;
1399
150k
        }
1400
1401
223k
        if (b->skip_mode) {
1402
627
            b->ref[0] = f->frame_hdr->skip_mode_refs[0];
1403
627
            b->ref[1] = f->frame_hdr->skip_mode_refs[1];
1404
627
            b->comp_type = COMP_INTER_AVG;
1405
627
            b->inter_mode = NEARESTMV_NEARESTMV;
1406
627
            b->drl_idx = NEAREST_DRL;
1407
627
            has_subpel_filter = 0;
1408
1409
627
            refmvs_candidate mvstack[8];
1410
627
            int n_mvs, ctx;
1411
627
            dav1d_refmvs_find(&t->rt, mvstack, &n_mvs, &ctx,
1412
627
                              (union refmvs_refpair) { .ref = {
1413
627
                                    b->ref[0] + 1, b->ref[1] + 1 }},
1414
627
                              bs, intra_edge_flags, t->by, t->bx);
1415
1416
627
            b->mv[0] = mvstack[0].mv.mv[0];
1417
627
            b->mv[1] = mvstack[0].mv.mv[1];
1418
627
            fix_mv_precision(f->frame_hdr, &b->mv[0]);
1419
627
            fix_mv_precision(f->frame_hdr, &b->mv[1]);
1420
627
            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
222k
        } else if (is_comp) {
1425
42.8k
            const int dir_ctx = get_comp_dir_ctx(t->a, &t->l, by4, bx4,
1426
42.8k
                                                 have_top, have_left);
1427
42.8k
            if (dav1d_msac_decode_bool_adapt(&ts->msac,
1428
42.8k
                    ts->cdf.m.comp_dir[dir_ctx]))
1429
35.6k
            {
1430
                // bidir - first reference (fw)
1431
35.6k
                const int ctx1 = av1_get_fwd_ref_ctx(t->a, &t->l, by4, bx4,
1432
35.6k
                                                     have_top, have_left);
1433
35.6k
                if (dav1d_msac_decode_bool_adapt(&ts->msac,
1434
35.6k
                        ts->cdf.m.comp_fwd_ref[0][ctx1]))
1435
14.3k
                {
1436
14.3k
                    const int ctx2 = av1_get_fwd_ref_2_ctx(t->a, &t->l, by4, bx4,
1437
14.3k
                                                           have_top, have_left);
1438
14.3k
                    b->ref[0] = 2 + dav1d_msac_decode_bool_adapt(&ts->msac,
1439
14.3k
                                        ts->cdf.m.comp_fwd_ref[2][ctx2]);
1440
21.3k
                } else {
1441
21.3k
                    const int ctx2 = av1_get_fwd_ref_1_ctx(t->a, &t->l, by4, bx4,
1442
21.3k
                                                           have_top, have_left);
1443
21.3k
                    b->ref[0] = dav1d_msac_decode_bool_adapt(&ts->msac,
1444
21.3k
                                    ts->cdf.m.comp_fwd_ref[1][ctx2]);
1445
21.3k
                }
1446
1447
                // second reference (bw)
1448
35.6k
                const int ctx3 = av1_get_bwd_ref_ctx(t->a, &t->l, by4, bx4,
1449
35.6k
                                                     have_top, have_left);
1450
35.6k
                if (dav1d_msac_decode_bool_adapt(&ts->msac,
1451
35.6k
                        ts->cdf.m.comp_bwd_ref[0][ctx3]))
1452
20.2k
                {
1453
20.2k
                    b->ref[1] = 6;
1454
20.2k
                } else {
1455
15.4k
                    const int ctx4 = av1_get_bwd_ref_1_ctx(t->a, &t->l, by4, bx4,
1456
15.4k
                                                           have_top, have_left);
1457
15.4k
                    b->ref[1] = 4 + dav1d_msac_decode_bool_adapt(&ts->msac,
1458
15.4k
                                        ts->cdf.m.comp_bwd_ref[1][ctx4]);
1459
15.4k
                }
1460
35.6k
            } else {
1461
                // unidir
1462
7.23k
                const int uctx_p = av1_get_uni_p_ctx(t->a, &t->l, by4, bx4,
1463
7.23k
                                                     have_top, have_left);
1464
7.23k
                if (dav1d_msac_decode_bool_adapt(&ts->msac,
1465
7.23k
                        ts->cdf.m.comp_uni_ref[0][uctx_p]))
1466
2.50k
                {
1467
2.50k
                    b->ref[0] = 4;
1468
2.50k
                    b->ref[1] = 6;
1469
4.73k
                } else {
1470
4.73k
                    const int uctx_p1 = av1_get_uni_p1_ctx(t->a, &t->l, by4, bx4,
1471
4.73k
                                                           have_top, have_left);
1472
4.73k
                    b->ref[0] = 0;
1473
4.73k
                    b->ref[1] = 1 + dav1d_msac_decode_bool_adapt(&ts->msac,
1474
4.73k
                                        ts->cdf.m.comp_uni_ref[1][uctx_p1]);
1475
4.73k
                    if (b->ref[1] == 2) {
1476
2.95k
                        const int uctx_p2 = av1_get_uni_p2_ctx(t->a, &t->l, by4, bx4,
1477
2.95k
                                                               have_top, have_left);
1478
2.95k
                        b->ref[1] += dav1d_msac_decode_bool_adapt(&ts->msac,
1479
2.95k
                                         ts->cdf.m.comp_uni_ref[2][uctx_p2]);
1480
2.95k
                    }
1481
4.73k
                }
1482
7.23k
            }
1483
42.8k
            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
42.8k
            refmvs_candidate mvstack[8];
1488
42.8k
            int n_mvs, ctx;
1489
42.8k
            dav1d_refmvs_find(&t->rt, mvstack, &n_mvs, &ctx,
1490
42.8k
                              (union refmvs_refpair) { .ref = {
1491
42.8k
                                    b->ref[0] + 1, b->ref[1] + 1 }},
1492
42.8k
                              bs, intra_edge_flags, t->by, t->bx);
1493
1494
42.8k
            b->inter_mode = dav1d_msac_decode_symbol_adapt8(&ts->msac,
1495
42.8k
                                ts->cdf.m.comp_inter_mode[ctx],
1496
42.8k
                                N_COMP_INTER_PRED_MODES - 1);
1497
42.8k
            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
42.8k
            const uint8_t *const im = dav1d_comp_inter_pred_modes[b->inter_mode];
1502
42.8k
            b->drl_idx = NEAREST_DRL;
1503
42.8k
            if (b->inter_mode == NEWMV_NEWMV) {
1504
8.54k
                if (n_mvs > 1) { // NEARER, NEAR or NEARISH
1505
8.54k
                    const int drl_ctx_v1 = get_drl_context(mvstack, 0);
1506
8.54k
                    b->drl_idx += dav1d_msac_decode_bool_adapt(&ts->msac,
1507
8.54k
                                      ts->cdf.m.drl_bit[drl_ctx_v1]);
1508
8.54k
                    if (b->drl_idx == NEARER_DRL && n_mvs > 2) {
1509
1.30k
                        const int drl_ctx_v2 = get_drl_context(mvstack, 1);
1510
1.30k
                        b->drl_idx += dav1d_msac_decode_bool_adapt(&ts->msac,
1511
1.30k
                                          ts->cdf.m.drl_bit[drl_ctx_v2]);
1512
1.30k
                    }
1513
8.54k
                    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
8.54k
                }
1517
34.3k
            } else if (im[0] == NEARMV || im[1] == NEARMV) {
1518
10.0k
                b->drl_idx = NEARER_DRL;
1519
10.0k
                if (n_mvs > 2) { // NEAR or NEARISH
1520
1.19k
                    const int drl_ctx_v2 = get_drl_context(mvstack, 1);
1521
1.19k
                    b->drl_idx += dav1d_msac_decode_bool_adapt(&ts->msac,
1522
1.19k
                                      ts->cdf.m.drl_bit[drl_ctx_v2]);
1523
1.19k
                    if (b->drl_idx == NEAR_DRL && n_mvs > 3) {
1524
180
                        const int drl_ctx_v3 = get_drl_context(mvstack, 2);
1525
180
                        b->drl_idx += dav1d_msac_decode_bool_adapt(&ts->msac,
1526
180
                                          ts->cdf.m.drl_bit[drl_ctx_v3]);
1527
180
                    }
1528
1.19k
                    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.19k
                }
1532
10.0k
            }
1533
42.8k
            assert(b->drl_idx >= NEAREST_DRL && b->drl_idx <= NEARISH_DRL);
1534
1535
42.8k
#define assign_comp_mv(idx) \
1536
85.7k
            switch (im[idx]) { \
1537
17.3k
            case NEARMV: \
1538
51.6k
            case NEARESTMV: \
1539
51.6k
                b->mv[idx] = mvstack[b->drl_idx].mv.mv[idx]; \
1540
51.6k
                fix_mv_precision(f->frame_hdr, &b->mv[idx]); \
1541
51.6k
                break; \
1542
17.3k
            case GLOBALMV: \
1543
9.10k
                has_subpel_filter |= \
1544
9.10k
                    f->frame_hdr->gmv[b->ref[idx]].type == DAV1D_WM_TYPE_TRANSLATION; \
1545
9.10k
                b->mv[idx] = get_gmv_2d(&f->frame_hdr->gmv[b->ref[idx]], \
1546
9.10k
                                        t->bx, t->by, bw4, bh4, f->frame_hdr); \
1547
9.10k
                break; \
1548
25.0k
            case NEWMV: \
1549
25.0k
                b->mv[idx] = mvstack[b->drl_idx].mv.mv[idx]; \
1550
25.0k
                const int mv_prec = f->frame_hdr->hp - f->frame_hdr->force_integer_mv; \
1551
25.0k
                read_mv_residual(ts, &b->mv[idx], mv_prec); \
1552
25.0k
                break; \
1553
85.7k
            }
1554
42.8k
            has_subpel_filter = imin(bw4, bh4) == 1 ||
1555
42.8k
                                b->inter_mode != GLOBALMV_GLOBALMV;
1556
42.8k
            assign_comp_mv(0);
1557
42.8k
            assign_comp_mv(1);
1558
42.8k
#undef assign_comp_mv
1559
42.8k
            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
42.8k
            int is_segwedge = 0;
1566
42.8k
            if (f->seq_hdr->masked_compound) {
1567
31.0k
                const int mask_ctx = get_mask_comp_ctx(t->a, &t->l, by4, bx4);
1568
1569
31.0k
                is_segwedge = dav1d_msac_decode_bool_adapt(&ts->msac,
1570
31.0k
                                  ts->cdf.m.mask_comp[mask_ctx]);
1571
31.0k
                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
31.0k
            }
1575
1576
42.8k
            if (!is_segwedge) {
1577
33.4k
                if (f->seq_hdr->jnt_comp) {
1578
5.60k
                    const int jnt_ctx =
1579
5.60k
                        get_jnt_comp_ctx(f->seq_hdr->order_hint_n_bits,
1580
5.60k
                                         f->cur.frame_hdr->frame_offset,
1581
5.60k
                                         f->refp[b->ref[0]].p.frame_hdr->frame_offset,
1582
5.60k
                                         f->refp[b->ref[1]].p.frame_hdr->frame_offset,
1583
5.60k
                                         t->a, &t->l, by4, bx4);
1584
5.60k
                    b->comp_type = COMP_INTER_WEIGHTED_AVG +
1585
5.60k
                                   dav1d_msac_decode_bool_adapt(&ts->msac,
1586
5.60k
                                       ts->cdf.m.jnt_comp[jnt_ctx]);
1587
5.60k
                    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
27.8k
                } else {
1594
27.8k
                    b->comp_type = COMP_INTER_AVG;
1595
27.8k
                }
1596
33.4k
            } else {
1597
9.40k
                if (wedge_allowed_mask & (1 << bs)) {
1598
7.75k
                    const int ctx = dav1d_wedge_ctx_lut[bs];
1599
7.75k
                    b->comp_type = COMP_INTER_WEDGE -
1600
7.75k
                                   dav1d_msac_decode_bool_adapt(&ts->msac,
1601
7.75k
                                       ts->cdf.m.wedge_comp[ctx]);
1602
7.75k
                    if (b->comp_type == COMP_INTER_WEDGE)
1603
2.94k
                        b->wedge_idx = dav1d_msac_decode_symbol_adapt16(&ts->msac,
1604
2.94k
                                           ts->cdf.m.wedge_idx[ctx], 15);
1605
7.75k
                } else {
1606
1.64k
                    b->comp_type = COMP_INTER_SEG;
1607
1.64k
                }
1608
9.40k
                b->mask_sign = dav1d_msac_decode_bool_equi(&ts->msac);
1609
9.40k
                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
9.40k
            }
1614
179k
        } else {
1615
179k
            b->comp_type = COMP_INTER_NONE;
1616
1617
            // ref
1618
179k
            if (seg && seg->ref > 0) {
1619
26.2k
                b->ref[0] = seg->ref - 1;
1620
153k
            } else if (seg && (seg->globalmv || seg->skip)) {
1621
20.6k
                b->ref[0] = 0;
1622
132k
            } else {
1623
132k
                const int ctx1 = av1_get_ref_ctx(t->a, &t->l, by4, bx4,
1624
132k
                                                 have_top, have_left);
1625
132k
                if (dav1d_msac_decode_bool_adapt(&ts->msac,
1626
132k
                                                 ts->cdf.m.ref[0][ctx1]))
1627
68.1k
                {
1628
68.1k
                    const int ctx2 = av1_get_ref_2_ctx(t->a, &t->l, by4, bx4,
1629
68.1k
                                                       have_top, have_left);
1630
68.1k
                    if (dav1d_msac_decode_bool_adapt(&ts->msac,
1631
68.1k
                                                     ts->cdf.m.ref[1][ctx2]))
1632
45.4k
                    {
1633
45.4k
                        b->ref[0] = 6;
1634
45.4k
                    } else {
1635
22.6k
                        const int ctx3 = av1_get_ref_6_ctx(t->a, &t->l, by4, bx4,
1636
22.6k
                                                           have_top, have_left);
1637
22.6k
                        b->ref[0] = 4 + dav1d_msac_decode_bool_adapt(&ts->msac,
1638
22.6k
                                            ts->cdf.m.ref[5][ctx3]);
1639
22.6k
                    }
1640
68.1k
                } else {
1641
64.8k
                    const int ctx2 = av1_get_ref_3_ctx(t->a, &t->l, by4, bx4,
1642
64.8k
                                                       have_top, have_left);
1643
64.8k
                    if (dav1d_msac_decode_bool_adapt(&ts->msac,
1644
64.8k
                                                     ts->cdf.m.ref[2][ctx2]))
1645
22.4k
                    {
1646
22.4k
                        const int ctx3 = av1_get_ref_5_ctx(t->a, &t->l, by4, bx4,
1647
22.4k
                                                           have_top, have_left);
1648
22.4k
                        b->ref[0] = 2 + dav1d_msac_decode_bool_adapt(&ts->msac,
1649
22.4k
                                            ts->cdf.m.ref[4][ctx3]);
1650
42.3k
                    } else {
1651
42.3k
                        const int ctx3 = av1_get_ref_4_ctx(t->a, &t->l, by4, bx4,
1652
42.3k
                                                           have_top, have_left);
1653
42.3k
                        b->ref[0] = dav1d_msac_decode_bool_adapt(&ts->msac,
1654
42.3k
                                        ts->cdf.m.ref[3][ctx3]);
1655
42.3k
                    }
1656
64.8k
                }
1657
132k
                if (DEBUG_BLOCK_INFO)
1658
0
                    printf("Post-ref[%d]: r=%d\n", b->ref[0], ts->msac.rng);
1659
132k
            }
1660
179k
            b->ref[1] = -1;
1661
1662
179k
            refmvs_candidate mvstack[8];
1663
179k
            int n_mvs, ctx;
1664
179k
            dav1d_refmvs_find(&t->rt, mvstack, &n_mvs, &ctx,
1665
179k
                              (union refmvs_refpair) { .ref = { b->ref[0] + 1, -1 }},
1666
179k
                              bs, intra_edge_flags, t->by, t->bx);
1667
1668
            // mode parsing and mv derivation from ref_mvs
1669
179k
            if ((seg && (seg->skip || seg->globalmv)) ||
1670
133k
                dav1d_msac_decode_bool_adapt(&ts->msac,
1671
133k
                                             ts->cdf.m.newmv_mode[ctx & 7]))
1672
131k
            {
1673
131k
                if ((seg && (seg->skip || seg->globalmv)) ||
1674
85.7k
                    !dav1d_msac_decode_bool_adapt(&ts->msac,
1675
85.7k
                         ts->cdf.m.globalmv_mode[(ctx >> 3) & 1]))
1676
50.3k
                {
1677
50.3k
                    b->inter_mode = GLOBALMV;
1678
50.3k
                    b->mv[0] = get_gmv_2d(&f->frame_hdr->gmv[b->ref[0]],
1679
50.3k
                                          t->bx, t->by, bw4, bh4, f->frame_hdr);
1680
50.3k
                    has_subpel_filter = imin(bw4, bh4) == 1 ||
1681
30.6k
                        f->frame_hdr->gmv[b->ref[0]].type == DAV1D_WM_TYPE_TRANSLATION;
1682
80.9k
                } else {
1683
80.9k
                    has_subpel_filter = 1;
1684
80.9k
                    if (dav1d_msac_decode_bool_adapt(&ts->msac,
1685
80.9k
                            ts->cdf.m.refmv_mode[(ctx >> 4) & 15]))
1686
34.9k
                    { // NEAREST, NEARER, NEAR or NEARISH
1687
34.9k
                        b->inter_mode = NEARMV;
1688
34.9k
                        b->drl_idx = NEARER_DRL;
1689
34.9k
                        if (n_mvs > 2) { // NEARER, NEAR or NEARISH
1690
7.94k
                            const int drl_ctx_v2 = get_drl_context(mvstack, 1);
1691
7.94k
                            b->drl_idx += dav1d_msac_decode_bool_adapt(&ts->msac,
1692
7.94k
                                              ts->cdf.m.drl_bit[drl_ctx_v2]);
1693
7.94k
                            if (b->drl_idx == NEAR_DRL && n_mvs > 3) { // NEAR or NEARISH
1694
2.03k
                                const int drl_ctx_v3 =
1695
2.03k
                                    get_drl_context(mvstack, 2);
1696
2.03k
                                b->drl_idx += dav1d_msac_decode_bool_adapt(&ts->msac,
1697
2.03k
                                                  ts->cdf.m.drl_bit[drl_ctx_v3]);
1698
2.03k
                            }
1699
7.94k
                        }
1700
45.9k
                    } else {
1701
45.9k
                        b->inter_mode = NEARESTMV;
1702
45.9k
                        b->drl_idx = NEAREST_DRL;
1703
45.9k
                    }
1704
80.9k
                    assert(b->drl_idx >= NEAREST_DRL && b->drl_idx <= NEARISH_DRL);
1705
80.9k
                    b->mv[0] = mvstack[b->drl_idx].mv.mv[0];
1706
80.9k
                    if (b->drl_idx < NEAR_DRL)
1707
76.0k
                        fix_mv_precision(f->frame_hdr, &b->mv[0]);
1708
80.9k
                }
1709
1710
131k
                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
131k
            } else {
1715
48.5k
                has_subpel_filter = 1;
1716
48.5k
                b->inter_mode = NEWMV;
1717
48.5k
                b->drl_idx = NEAREST_DRL;
1718
48.5k
                if (n_mvs > 1) { // NEARER, NEAR or NEARISH
1719
28.4k
                    const int drl_ctx_v1 = get_drl_context(mvstack, 0);
1720
28.4k
                    b->drl_idx += dav1d_msac_decode_bool_adapt(&ts->msac,
1721
28.4k
                                      ts->cdf.m.drl_bit[drl_ctx_v1]);
1722
28.4k
                    if (b->drl_idx == NEARER_DRL && n_mvs > 2) { // NEAR or NEARISH
1723
4.74k
                        const int drl_ctx_v2 = get_drl_context(mvstack, 1);
1724
4.74k
                        b->drl_idx += dav1d_msac_decode_bool_adapt(&ts->msac,
1725
4.74k
                                          ts->cdf.m.drl_bit[drl_ctx_v2]);
1726
4.74k
                    }
1727
28.4k
                }
1728
48.5k
                assert(b->drl_idx >= NEAREST_DRL && b->drl_idx <= NEARISH_DRL);
1729
48.5k
                if (n_mvs > 1) {
1730
28.4k
                    b->mv[0] = mvstack[b->drl_idx].mv.mv[0];
1731
28.4k
                } else {
1732
20.1k
                    assert(!b->drl_idx);
1733
20.1k
                    b->mv[0] = mvstack[0].mv.mv[0];
1734
20.1k
                    fix_mv_precision(f->frame_hdr, &b->mv[0]);
1735
20.1k
                }
1736
48.5k
                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
48.5k
                const int mv_prec = f->frame_hdr->hp - f->frame_hdr->force_integer_mv;
1740
48.5k
                read_mv_residual(ts, &b->mv[0], mv_prec);
1741
48.5k
                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
48.5k
            }
1745
1746
            // interintra flags
1747
179k
            const int ii_sz_grp = dav1d_ymode_size_context[bs];
1748
179k
            if (f->seq_hdr->inter_intra &&
1749
137k
                interintra_allowed_mask & (1 << bs) &&
1750
59.4k
                dav1d_msac_decode_bool_adapt(&ts->msac,
1751
59.4k
                                             ts->cdf.m.interintra[ii_sz_grp]))
1752
11.0k
            {
1753
11.0k
                b->interintra_mode = dav1d_msac_decode_symbol_adapt4(&ts->msac,
1754
11.0k
                                         ts->cdf.m.interintra_mode[ii_sz_grp],
1755
11.0k
                                         N_INTER_INTRA_PRED_MODES - 1);
1756
11.0k
                const int wedge_ctx = dav1d_wedge_ctx_lut[bs];
1757
11.0k
                b->interintra_type = INTER_INTRA_BLEND +
1758
11.0k
                                     dav1d_msac_decode_bool_adapt(&ts->msac,
1759
11.0k
                                         ts->cdf.m.interintra_wedge[wedge_ctx]);
1760
11.0k
                if (b->interintra_type == INTER_INTRA_WEDGE)
1761
2.83k
                    b->wedge_idx = dav1d_msac_decode_symbol_adapt16(&ts->msac,
1762
2.83k
                                       ts->cdf.m.wedge_idx[wedge_ctx], 15);
1763
168k
            } else {
1764
168k
                b->interintra_type = INTER_INTRA_NONE;
1765
168k
            }
1766
179k
            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
179k
            if (f->frame_hdr->switchable_motion_mode &&
1776
136k
                b->interintra_type == INTER_INTRA_NONE && imin(bw4, bh4) >= 2 &&
1777
                // is not warped global motion
1778
64.1k
                !(!f->frame_hdr->force_integer_mv && b->inter_mode == GLOBALMV &&
1779
18.5k
                  f->frame_hdr->gmv[b->ref[0]].type > DAV1D_WM_TYPE_TRANSLATION) &&
1780
                // has overlappable neighbours
1781
60.5k
                ((have_left && findoddzero(&t->l.intra[by4 + 1], h4 >> 1)) ||
1782
17.2k
                 (have_top && findoddzero(&t->a->intra[bx4 + 1], w4 >> 1))))
1783
55.8k
            {
1784
                // reaching here means the block allows obmc - check warp by
1785
                // finding matching-ref blocks in top/left edges
1786
55.8k
                uint64_t mask[2] = { 0, 0 };
1787
55.8k
                find_matching_ref(t, intra_edge_flags, bw4, bh4, w4, h4,
1788
55.8k
                                  have_left, have_top, b->ref[0], mask);
1789
55.8k
                const int allow_warp = !f->svc[b->ref[0]][0].scale &&
1790
28.8k
                    !f->frame_hdr->force_integer_mv &&
1791
28.7k
                    f->frame_hdr->warp_motion && (mask[0] | mask[1]);
1792
1793
55.8k
                b->motion_mode = allow_warp ?
1794
15.9k
                    dav1d_msac_decode_symbol_adapt4(&ts->msac,
1795
15.9k
                        ts->cdf.m.motion_mode[bs], 2) :
1796
55.8k
                    dav1d_msac_decode_bool_adapt(&ts->msac, ts->cdf.m.obmc[bs]);
1797
55.8k
                if (b->motion_mode == MM_WARP) {
1798
5.69k
                    has_subpel_filter = 0;
1799
5.69k
                    derive_warpmv(t, bw4, bh4, mask, b->mv[0], &t->warpmv);
1800
5.69k
#define signabs(v) v < 0 ? '-' : ' ', abs(v)
1801
5.69k
                    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.69k
#undef signabs
1817
5.69k
                    if (t->frame_thread.pass) {
1818
5.69k
                        if (t->warpmv.type == DAV1D_WM_TYPE_AFFINE) {
1819
4.92k
                            b->matrix[0] = t->warpmv.matrix[2] - 0x10000;
1820
4.92k
                            b->matrix[1] = t->warpmv.matrix[3];
1821
4.92k
                            b->matrix[2] = t->warpmv.matrix[4];
1822
4.92k
                            b->matrix[3] = t->warpmv.matrix[5] - 0x10000;
1823
4.92k
                        } else {
1824
775
                            b->matrix[0] = INT16_MIN;
1825
775
                        }
1826
5.69k
                    }
1827
5.69k
                }
1828
1829
55.8k
                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
123k
            } else {
1834
123k
                b->motion_mode = MM_TRANSLATION;
1835
123k
            }
1836
179k
        }
1837
1838
        // subpel filter
1839
223k
        enum Dav1dFilterMode filter[2];
1840
223k
        if (f->frame_hdr->subpel_filter_mode == DAV1D_FILTER_SWITCHABLE) {
1841
129k
            if (has_subpel_filter) {
1842
107k
                const int comp = b->comp_type != COMP_INTER_NONE;
1843
107k
                const int ctx1 = get_filter_ctx(t->a, &t->l, comp, 0, b->ref[0],
1844
107k
                                                by4, bx4);
1845
107k
                filter[0] = dav1d_msac_decode_symbol_adapt4(&ts->msac,
1846
107k
                               ts->cdf.m.filter[0][ctx1],
1847
107k
                               DAV1D_N_SWITCHABLE_FILTERS - 1);
1848
107k
                if (f->seq_hdr->dual_filter) {
1849
67.9k
                    const int ctx2 = get_filter_ctx(t->a, &t->l, comp, 1,
1850
67.9k
                                                    b->ref[0], by4, bx4);
1851
67.9k
                    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
67.9k
                    filter[1] = dav1d_msac_decode_symbol_adapt4(&ts->msac,
1855
67.9k
                                    ts->cdf.m.filter[1][ctx2],
1856
67.9k
                                    DAV1D_N_SWITCHABLE_FILTERS - 1);
1857
67.9k
                    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
67.9k
                } else {
1861
39.3k
                    filter[1] = filter[0];
1862
39.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
39.3k
                }
1866
107k
            } else {
1867
21.7k
                filter[0] = filter[1] = DAV1D_FILTER_8TAP_REGULAR;
1868
21.7k
            }
1869
129k
        } else {
1870
94.2k
            filter[0] = filter[1] = f->frame_hdr->subpel_filter_mode;
1871
94.2k
        }
1872
223k
        b->filter2d = dav1d_filter_2d[filter[1]][filter[0]];
1873
1874
223k
        read_vartx_tree(t, b, bs, bx4, by4);
1875
1876
        // reconstruction
1877
223k
        if (t->frame_thread.pass == 1) {
1878
223k
            f->bd_fn.read_coef_blocks(t, bs, b);
1879
223k
        } else {
1880
309
            if (f->bd_fn.recon_b_inter(t, bs, b)) return -1;
1881
309
        }
1882
1883
223k
        if (f->frame_hdr->loopfilter.level_y[0] ||
1884
83.9k
            f->frame_hdr->loopfilter.level_y[1])
1885
162k
        {
1886
162k
            const int is_globalmv =
1887
162k
                b->inter_mode == (is_comp ? GLOBALMV_GLOBALMV : GLOBALMV);
1888
162k
            const uint8_t (*const lf_lvls)[8][2] = (const uint8_t (*)[8][2])
1889
162k
                &ts->lflvl[b->seg_id][0][b->ref[0] + 1][!is_globalmv];
1890
162k
            const uint16_t tx_split[2] = { b->tx_split0, b->tx_split1 };
1891
162k
            enum RectTxfmSize ytx = b->max_ytx, uvtx = b->uvtx;
1892
162k
            if (f->frame_hdr->segmentation.lossless[b->seg_id]) {
1893
738
                ytx  = (enum RectTxfmSize) TX_4X4;
1894
738
                uvtx = (enum RectTxfmSize) TX_4X4;
1895
738
            }
1896
162k
            dav1d_create_lf_mask_inter(t->lf_mask, f->lf.level, f->b4_stride, lf_lvls,
1897
162k
                                       t->bx, t->by, f->w4, f->h4, b->skip, bs,
1898
162k
                                       ytx, tx_split, uvtx, f->cur.p.layout,
1899
162k
                                       &t->a->tx_lpf_y[bx4], &t->l.tx_lpf_y[by4],
1900
162k
                                       has_chroma ? &t->a->tx_lpf_uv[cbx4] : NULL,
1901
162k
                                       has_chroma ? &t->l.tx_lpf_uv[cby4] : NULL);
1902
162k
        }
1903
1904
        // context updates
1905
223k
        if (is_comp)
1906
43.4k
            splat_tworef_mv(f->c, t, bs, b, bw4, bh4);
1907
179k
        else
1908
179k
            splat_oneref_mv(f->c, t, bs, b, bw4, bh4);
1909
223k
        BlockContext *edge = t->a;
1910
669k
        for (int i = 0, off = bx4; i < 2; i++, off = by4, edge = &t->l) {
1911
446k
#define set_ctx(rep_macro) \
1912
446k
            rep_macro(edge->seg_pred, off, seg_pred); \
1913
446k
            rep_macro(edge->skip_mode, off, b->skip_mode); \
1914
446k
            rep_macro(edge->intra, off, 0); \
1915
446k
            rep_macro(edge->skip, off, b->skip); \
1916
446k
            rep_macro(edge->pal_sz, off, 0); \
1917
            /* see aomedia bug 2183 for why this is outside if (has_chroma) */ \
1918
446k
            rep_macro(t->pal_sz_uv[i], off, 0); \
1919
446k
            rep_macro(edge->tx_intra, off, b_dim[2 + i]); \
1920
446k
            rep_macro(edge->comp_type, off, b->comp_type); \
1921
446k
            rep_macro(edge->filter[0], off, filter[0]); \
1922
446k
            rep_macro(edge->filter[1], off, filter[1]); \
1923
446k
            rep_macro(edge->mode, off, b->inter_mode); \
1924
446k
            rep_macro(edge->ref[0], off, b->ref[0]); \
1925
446k
            rep_macro(edge->ref[1], off, ((uint8_t) b->ref[1]))
1926
446k
            case_set(b_dim[2 + i]);
1927
446k
#undef set_ctx
1928
446k
        }
1929
223k
        if (has_chroma) {
1930
155k
            dav1d_memset_pow2[ulog2(cbw4)](&t->a->uvmode[cbx4], DC_PRED);
1931
155k
            dav1d_memset_pow2[ulog2(cbh4)](&t->l.uvmode[cby4], DC_PRED);
1932
155k
        }
1933
223k
    }
1934
1935
    // update contexts
1936
1.62M
    if (f->frame_hdr->segmentation.enabled &&
1937
605k
        f->frame_hdr->segmentation.update_map)
1938
565k
    {
1939
565k
        uint8_t *seg_ptr = &f->cur_segmap[t->by * f->b4_stride + t->bx];
1940
565k
#define set_ctx(rep_macro) \
1941
4.51M
        for (int y = 0; y < bh4; y++) { \
1942
3.94M
            rep_macro(seg_ptr, 0, b->seg_id); \
1943
3.94M
            seg_ptr += f->b4_stride; \
1944
3.94M
        }
1945
565k
        case_set(b_dim[2]);
1946
565k
#undef set_ctx
1947
565k
    }
1948
1.62M
    if (!b->skip) {
1949
738k
        uint16_t (*noskip_mask)[2] = &t->lf_mask->noskip_mask[by4 >> 1];
1950
738k
        const unsigned mask = (~0U >> (32 - bw4)) << (bx4 & 15);
1951
738k
        const int bx_idx = (bx4 & 16) >> 4;
1952
2.80M
        for (int y = 0; y < bh4; y += 2, noskip_mask++) {
1953
2.06M
            (*noskip_mask)[bx_idx] |= mask;
1954
2.06M
            if (bw4 == 32) // this should be mask >> 16, but it's 0xffffffff anyway
1955
389k
                (*noskip_mask)[1] |= mask;
1956
2.06M
        }
1957
738k
    }
1958
1959
1.62M
    if (t->frame_thread.pass == 1 && !b->intra && IS_INTER_OR_SWITCH(f->frame_hdr)) {
1960
223k
        const int sby = (t->by - ts->tiling.row_start) >> f->sb_shift;
1961
223k
        int (*const lowest_px)[2] = ts->lowest_pixel[sby];
1962
1963
        // keep track of motion vectors for each reference
1964
223k
        if (b->comp_type == COMP_INTER_NONE) {
1965
            // y
1966
179k
            if (imin(bw4, bh4) > 1 &&
1967
94.4k
                ((b->inter_mode == GLOBALMV && f->gmv_warp_allowed[b->ref[0]]) ||
1968
86.4k
                 (b->motion_mode == MM_WARP && t->warpmv.type > DAV1D_WM_TYPE_TRANSLATION)))
1969
12.8k
            {
1970
12.8k
                affine_lowest_px_luma(t, &lowest_px[b->ref[0]][0], b_dim,
1971
12.8k
                                      b->motion_mode == MM_WARP ? &t->warpmv :
1972
12.8k
                                      &f->frame_hdr->gmv[b->ref[0]]);
1973
166k
            } else {
1974
166k
                mc_lowest_px(&lowest_px[b->ref[0]][0], t->by, bh4, b->mv[0].y,
1975
166k
                             0, &f->svc[b->ref[0]][1]);
1976
166k
                if (b->motion_mode == MM_OBMC) {
1977
28.0k
                    obmc_lowest_px(t, lowest_px, 0, b_dim, bx4, by4, w4, h4);
1978
28.0k
                }
1979
166k
            }
1980
1981
            // uv
1982
179k
            if (has_chroma) {
1983
                // sub8x8 derivation
1984
122k
                int is_sub8x8 = bw4 == ss_hor || bh4 == ss_ver;
1985
122k
                refmvs_block *const *r;
1986
122k
                if (is_sub8x8) {
1987
22.1k
                    assert(ss_hor == 1);
1988
22.1k
                    r = &t->rt.r[(t->by & 31) + 5];
1989
22.1k
                    if (bw4 == 1) is_sub8x8 &= r[0][t->bx - 1].ref.ref[0] > 0;
1990
22.1k
                    if (bh4 == ss_ver) is_sub8x8 &= r[-1][t->bx].ref.ref[0] > 0;
1991
22.1k
                    if (bw4 == 1 && bh4 == ss_ver)
1992
5.93k
                        is_sub8x8 &= r[-1][t->bx - 1].ref.ref[0] > 0;
1993
22.1k
                }
1994
1995
                // chroma prediction
1996
122k
                if (is_sub8x8) {
1997
20.9k
                    assert(ss_hor == 1);
1998
20.9k
                    if (bw4 == 1 && bh4 == ss_ver) {
1999
5.04k
                        const refmvs_block *const rr = &r[-1][t->bx - 1];
2000
5.04k
                        mc_lowest_px(&lowest_px[rr->ref.ref[0] - 1][1],
2001
5.04k
                                     t->by - 1, bh4, rr->mv.mv[0].y, ss_ver,
2002
5.04k
                                     &f->svc[rr->ref.ref[0] - 1][1]);
2003
5.04k
                    }
2004
20.9k
                    if (bw4 == 1) {
2005
12.1k
                        const refmvs_block *const rr = &r[0][t->bx - 1];
2006
12.1k
                        mc_lowest_px(&lowest_px[rr->ref.ref[0] - 1][1],
2007
12.1k
                                     t->by, bh4, rr->mv.mv[0].y, ss_ver,
2008
12.1k
                                     &f->svc[rr->ref.ref[0] - 1][1]);
2009
12.1k
                    }
2010
20.9k
                    if (bh4 == ss_ver) {
2011
13.8k
                        const refmvs_block *const rr = &r[-1][t->bx];
2012
13.8k
                        mc_lowest_px(&lowest_px[rr->ref.ref[0] - 1][1],
2013
13.8k
                                     t->by - 1, bh4, rr->mv.mv[0].y, ss_ver,
2014
13.8k
                                     &f->svc[rr->ref.ref[0] - 1][1]);
2015
13.8k
                    }
2016
20.9k
                    mc_lowest_px(&lowest_px[b->ref[0]][1], t->by, bh4,
2017
20.9k
                                 b->mv[0].y, ss_ver, &f->svc[b->ref[0]][1]);
2018
101k
                } else {
2019
101k
                    if (imin(cbw4, cbh4) > 1 &&
2020
43.7k
                        ((b->inter_mode == GLOBALMV && f->gmv_warp_allowed[b->ref[0]]) ||
2021
40.6k
                         (b->motion_mode == MM_WARP && t->warpmv.type > DAV1D_WM_TYPE_TRANSLATION)))
2022
6.05k
                    {
2023
6.05k
                        affine_lowest_px_chroma(t, &lowest_px[b->ref[0]][1], b_dim,
2024
6.05k
                                                b->motion_mode == MM_WARP ? &t->warpmv :
2025
6.05k
                                                &f->frame_hdr->gmv[b->ref[0]]);
2026
95.1k
                    } else {
2027
95.1k
                        mc_lowest_px(&lowest_px[b->ref[0]][1],
2028
95.1k
                                     t->by & ~ss_ver, bh4 << (bh4 == ss_ver),
2029
95.1k
                                     b->mv[0].y, ss_ver, &f->svc[b->ref[0]][1]);
2030
95.1k
                        if (b->motion_mode == MM_OBMC) {
2031
24.1k
                            obmc_lowest_px(t, lowest_px, 1, b_dim, bx4, by4, w4, h4);
2032
24.1k
                        }
2033
95.1k
                    }
2034
101k
                }
2035
122k
            }
2036
179k
        } else {
2037
            // y
2038
130k
            for (int i = 0; i < 2; i++) {
2039
86.9k
                if (b->inter_mode == GLOBALMV_GLOBALMV && f->gmv_warp_allowed[b->ref[i]]) {
2040
1.87k
                    affine_lowest_px_luma(t, &lowest_px[b->ref[i]][0], b_dim,
2041
1.87k
                                          &f->frame_hdr->gmv[b->ref[i]]);
2042
85.1k
                } else {
2043
85.1k
                    mc_lowest_px(&lowest_px[b->ref[i]][0], t->by, bh4,
2044
85.1k
                                 b->mv[i].y, 0, &f->svc[b->ref[i]][1]);
2045
85.1k
                }
2046
86.9k
            }
2047
2048
            // uv
2049
100k
            if (has_chroma) for (int i = 0; i < 2; i++) {
2050
66.7k
                if (b->inter_mode == GLOBALMV_GLOBALMV &&
2051
7.00k
                    imin(cbw4, cbh4) > 1 && f->gmv_warp_allowed[b->ref[i]])
2052
888
                {
2053
888
                    affine_lowest_px_chroma(t, &lowest_px[b->ref[i]][1], b_dim,
2054
888
                                            &f->frame_hdr->gmv[b->ref[i]]);
2055
65.8k
                } else {
2056
65.8k
                    mc_lowest_px(&lowest_px[b->ref[i]][1], t->by, bh4,
2057
65.8k
                                 b->mv[i].y, ss_ver, &f->svc[b->ref[i]][1]);
2058
65.8k
                }
2059
66.7k
            }
2060
43.4k
        }
2061
223k
    }
2062
2063
1.62M
    return 0;
2064
1.62M
}
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
1.65M
{
2122
1.65M
    const Dav1dFrameContext *const f = t->f;
2123
1.65M
    Dav1dTileState *const ts = t->ts;
2124
1.65M
    const int hsz = 16 >> bl;
2125
1.65M
    const int have_h_split = f->bw > t->bx + hsz;
2126
1.65M
    const int have_v_split = f->bh > t->by + hsz;
2127
2128
1.65M
    if (!have_h_split && !have_v_split) {
2129
81.6k
        assert(bl < BL_8X8);
2130
81.6k
        return decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 0));
2131
81.6k
    }
2132
2133
1.57M
    uint16_t *pc;
2134
1.57M
    enum BlockPartition bp;
2135
1.57M
    int ctx, bx8, by8;
2136
1.57M
    if (t->frame_thread.pass != 2) {
2137
1.22M
        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.22M
        bx8 = (t->bx & 31) >> 1;
2141
1.22M
        by8 = (t->by & 31) >> 1;
2142
1.22M
        ctx = get_partition_ctx(t->a, &t->l, bl, by8, bx8);
2143
1.22M
        pc = ts->cdf.m.partition[bl][ctx];
2144
1.22M
    }
2145
2146
1.57M
    if (have_h_split && have_v_split) {
2147
964k
        if (t->frame_thread.pass == 2) {
2148
184k
            const Av1Block *const b = &f->frame_thread.b[t->by * f->b4_stride + t->bx];
2149
184k
            bp = b->bl == bl ? b->bp : PARTITION_SPLIT;
2150
780k
        } else {
2151
780k
            bp = dav1d_msac_decode_symbol_adapt16(&ts->msac, pc,
2152
780k
                                                  dav1d_partition_type_count[bl]);
2153
780k
            if (f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I422 &&
2154
5.37k
                (bp == PARTITION_V || bp == PARTITION_V4 ||
2155
5.19k
                 bp == PARTITION_T_LEFT_SPLIT || bp == PARTITION_T_RIGHT_SPLIT))
2156
210
            {
2157
210
                return 1;
2158
210
            }
2159
779k
            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
779k
        }
2164
964k
        const uint8_t *const b = dav1d_block_sizes[bl][bp];
2165
2166
964k
        switch (bp) {
2167
416k
        case PARTITION_NONE:
2168
416k
            if (decode_b(t, bl, b[0], PARTITION_NONE, node->o))
2169
31
                return -1;
2170
416k
            break;
2171
416k
        case PARTITION_H:
2172
99.4k
            if (decode_b(t, bl, b[0], PARTITION_H, node->h[0]))
2173
54
                return -1;
2174
99.3k
            t->by += hsz;
2175
99.3k
            if (decode_b(t, bl, b[0], PARTITION_H, node->h[1]))
2176
37
                return -1;
2177
99.3k
            t->by -= hsz;
2178
99.3k
            break;
2179
71.1k
        case PARTITION_V:
2180
71.1k
            if (decode_b(t, bl, b[0], PARTITION_V, node->v[0]))
2181
17
                return -1;
2182
71.1k
            t->bx += hsz;
2183
71.1k
            if (decode_b(t, bl, b[0], PARTITION_V, node->v[1]))
2184
23
                return -1;
2185
71.1k
            t->bx -= hsz;
2186
71.1k
            break;
2187
224k
        case PARTITION_SPLIT:
2188
224k
            if (bl == BL_8X8) {
2189
104k
                const EdgeTip *const tip = (const EdgeTip *) node;
2190
104k
                assert(hsz == 1);
2191
104k
                if (decode_b(t, bl, BS_4x4, PARTITION_SPLIT, EDGE_ALL_TR_AND_BL))
2192
23
                    return -1;
2193
104k
                const enum Filter2d tl_filter = t->tl_4x4_filter;
2194
104k
                t->bx++;
2195
104k
                if (decode_b(t, bl, BS_4x4, PARTITION_SPLIT, tip->split[0]))
2196
19
                    return -1;
2197
104k
                t->bx--;
2198
104k
                t->by++;
2199
104k
                if (decode_b(t, bl, BS_4x4, PARTITION_SPLIT, tip->split[1]))
2200
9
                    return -1;
2201
104k
                t->bx++;
2202
104k
                t->tl_4x4_filter = tl_filter;
2203
104k
                if (decode_b(t, bl, BS_4x4, PARTITION_SPLIT, tip->split[2]))
2204
13
                    return -1;
2205
104k
                t->bx--;
2206
104k
                t->by--;
2207
104k
#if ARCH_X86_64
2208
104k
                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
104k
                    const int p = t->frame_thread.pass & 1;
2213
104k
                    ts->frame_thread[p].cf =
2214
104k
                        (void*)(((uintptr_t)ts->frame_thread[p].cf + 63) & ~63);
2215
104k
                }
2216
104k
#endif
2217
119k
            } else {
2218
119k
                if (decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 0)))
2219
112
                    return 1;
2220
119k
                t->bx += hsz;
2221
119k
                if (decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 1)))
2222
366
                    return 1;
2223
119k
                t->bx -= hsz;
2224
119k
                t->by += hsz;
2225
119k
                if (decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 2)))
2226
216
                    return 1;
2227
118k
                t->bx += hsz;
2228
118k
                if (decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 3)))
2229
216
                    return 1;
2230
118k
                t->bx -= hsz;
2231
118k
                t->by -= hsz;
2232
118k
            }
2233
223k
            break;
2234
223k
        case PARTITION_T_TOP_SPLIT: {
2235
13.4k
            if (decode_b(t, bl, b[0], PARTITION_T_TOP_SPLIT, EDGE_ALL_TR_AND_BL))
2236
4
                return -1;
2237
13.4k
            t->bx += hsz;
2238
13.4k
            if (decode_b(t, bl, b[0], PARTITION_T_TOP_SPLIT, node->v[1]))
2239
9
                return -1;
2240
13.4k
            t->bx -= hsz;
2241
13.4k
            t->by += hsz;
2242
13.4k
            if (decode_b(t, bl, b[1], PARTITION_T_TOP_SPLIT, node->h[1]))
2243
7
                return -1;
2244
13.4k
            t->by -= hsz;
2245
13.4k
            break;
2246
13.4k
        }
2247
15.3k
        case PARTITION_T_BOTTOM_SPLIT: {
2248
15.3k
            if (decode_b(t, bl, b[0], PARTITION_T_BOTTOM_SPLIT, node->h[0]))
2249
8
                return -1;
2250
15.3k
            t->by += hsz;
2251
15.3k
            if (decode_b(t, bl, b[1], PARTITION_T_BOTTOM_SPLIT, node->v[0]))
2252
6
                return -1;
2253
15.3k
            t->bx += hsz;
2254
15.3k
            if (decode_b(t, bl, b[1], PARTITION_T_BOTTOM_SPLIT, 0))
2255
13
                return -1;
2256
15.3k
            t->bx -= hsz;
2257
15.3k
            t->by -= hsz;
2258
15.3k
            break;
2259
15.3k
        }
2260
10.3k
        case PARTITION_T_LEFT_SPLIT: {
2261
10.3k
            if (decode_b(t, bl, b[0], PARTITION_T_LEFT_SPLIT, EDGE_ALL_TR_AND_BL))
2262
15
                return -1;
2263
10.3k
            t->by += hsz;
2264
10.3k
            if (decode_b(t, bl, b[0], PARTITION_T_LEFT_SPLIT, node->h[1]))
2265
38
                return -1;
2266
10.2k
            t->by -= hsz;
2267
10.2k
            t->bx += hsz;
2268
10.2k
            if (decode_b(t, bl, b[1], PARTITION_T_LEFT_SPLIT, node->v[1]))
2269
3
                return -1;
2270
10.2k
            t->bx -= hsz;
2271
10.2k
            break;
2272
10.2k
        }
2273
23.2k
        case PARTITION_T_RIGHT_SPLIT: {
2274
23.2k
            if (decode_b(t, bl, b[0], PARTITION_T_RIGHT_SPLIT, node->v[0]))
2275
3
                return -1;
2276
23.2k
            t->bx += hsz;
2277
23.2k
            if (decode_b(t, bl, b[1], PARTITION_T_RIGHT_SPLIT, node->h[0]))
2278
12
                return -1;
2279
23.2k
            t->by += hsz;
2280
23.2k
            if (decode_b(t, bl, b[1], PARTITION_T_RIGHT_SPLIT, 0))
2281
3
                return -1;
2282
23.2k
            t->by -= hsz;
2283
23.2k
            t->bx -= hsz;
2284
23.2k
            break;
2285
23.2k
        }
2286
33.4k
        case PARTITION_H4: {
2287
33.4k
            const EdgeBranch *const branch = (const EdgeBranch *) node;
2288
33.4k
            if (decode_b(t, bl, b[0], PARTITION_H4, node->h[0]))
2289
6
                return -1;
2290
33.4k
            t->by += hsz >> 1;
2291
33.4k
            if (decode_b(t, bl, b[0], PARTITION_H4, branch->h4))
2292
15
                return -1;
2293
33.3k
            t->by += hsz >> 1;
2294
33.3k
            if (decode_b(t, bl, b[0], PARTITION_H4, EDGE_ALL_LEFT_HAS_BOTTOM))
2295
17
                return -1;
2296
33.3k
            t->by += hsz >> 1;
2297
33.3k
            if (t->by < f->bh)
2298
32.5k
                if (decode_b(t, bl, b[0], PARTITION_H4, node->h[1]))
2299
14
                    return -1;
2300
33.3k
            t->by -= hsz * 3 >> 1;
2301
33.3k
            break;
2302
33.3k
        }
2303
57.6k
        case PARTITION_V4: {
2304
57.6k
            const EdgeBranch *const branch = (const EdgeBranch *) node;
2305
57.6k
            if (decode_b(t, bl, b[0], PARTITION_V4, node->v[0]))
2306
32
                return -1;
2307
57.5k
            t->bx += hsz >> 1;
2308
57.5k
            if (decode_b(t, bl, b[0], PARTITION_V4, branch->v4))
2309
13
                return -1;
2310
57.5k
            t->bx += hsz >> 1;
2311
57.5k
            if (decode_b(t, bl, b[0], PARTITION_V4, EDGE_ALL_TOP_HAS_RIGHT))
2312
20
                return -1;
2313
57.5k
            t->bx += hsz >> 1;
2314
57.5k
            if (t->bx < f->bw)
2315
54.1k
                if (decode_b(t, bl, b[0], PARTITION_V4, node->v[1]))
2316
27
                    return -1;
2317
57.5k
            t->bx -= hsz * 3 >> 1;
2318
57.5k
            break;
2319
57.5k
        }
2320
0
        default: assert(0);
2321
964k
        }
2322
964k
    } else if (have_h_split) {
2323
368k
        unsigned is_split;
2324
368k
        if (t->frame_thread.pass == 2) {
2325
60.4k
            const Av1Block *const b = &f->frame_thread.b[t->by * f->b4_stride + t->bx];
2326
60.4k
            is_split = b->bl != bl;
2327
308k
        } else {
2328
308k
            is_split = dav1d_msac_decode_bool(&ts->msac,
2329
308k
                           gather_top_partition_prob(pc, bl));
2330
308k
            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
308k
        }
2335
2336
368k
        assert(bl < BL_8X8);
2337
368k
        if (is_split) {
2338
216k
            bp = PARTITION_SPLIT;
2339
216k
            if (decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 0))) return 1;
2340
216k
            t->bx += hsz;
2341
216k
            if (decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 1))) return 1;
2342
216k
            t->bx -= hsz;
2343
216k
        } else {
2344
151k
            bp = PARTITION_H;
2345
151k
            if (decode_b(t, bl, dav1d_block_sizes[bl][PARTITION_H][0],
2346
151k
                         PARTITION_H, node->h[0]))
2347
21
                return -1;
2348
151k
        }
2349
368k
    } else {
2350
237k
        assert(have_v_split);
2351
237k
        unsigned is_split;
2352
237k
        if (t->frame_thread.pass == 2) {
2353
100k
            const Av1Block *const b = &f->frame_thread.b[t->by * f->b4_stride + t->bx];
2354
100k
            is_split = b->bl != bl;
2355
137k
        } else {
2356
137k
            is_split = dav1d_msac_decode_bool(&ts->msac,
2357
137k
                           gather_left_partition_prob(pc, bl));
2358
137k
            if (f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I422 && !is_split)
2359
28
                return 1;
2360
137k
            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
137k
        }
2365
2366
237k
        assert(bl < BL_8X8);
2367
237k
        if (is_split) {
2368
105k
            bp = PARTITION_SPLIT;
2369
105k
            if (decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 0))) return 1;
2370
105k
            t->by += hsz;
2371
105k
            if (decode_sb(t, bl + 1, INTRA_EDGE_SPLIT(node, 2))) return 1;
2372
105k
            t->by -= hsz;
2373
131k
        } else {
2374
131k
            bp = PARTITION_V;
2375
131k
            if (decode_b(t, bl, dav1d_block_sizes[bl][PARTITION_V][0],
2376
131k
                         PARTITION_V, node->v[0]))
2377
20
                return -1;
2378
131k
        }
2379
237k
    }
2380
2381
1.56M
    if (t->frame_thread.pass != 2 && (bp != PARTITION_SPLIT || bl == BL_8X8)) {
2382
866k
#define set_ctx(rep_macro) \
2383
866k
        rep_macro(t->a->partition, bx8, dav1d_al_part_ctx[0][bl][bp]); \
2384
866k
        rep_macro(t->l.partition, by8, dav1d_al_part_ctx[1][bl][bp])
2385
866k
        case_set_upto16(ulog2(hsz));
2386
866k
#undef set_ctx
2387
866k
    }
2388
2389
1.56M
    return 0;
2390
1.56M
}
2391
2392
526k
static void reset_context(BlockContext *const ctx, const int keyframe, const int pass) {
2393
526k
    memset(ctx->intra, keyframe, sizeof(ctx->intra));
2394
526k
    memset(ctx->uvmode, DC_PRED, sizeof(ctx->uvmode));
2395
526k
    if (keyframe)
2396
466k
        memset(ctx->mode, DC_PRED, sizeof(ctx->mode));
2397
2398
526k
    if (pass == 2) return;
2399
2400
277k
    memset(ctx->partition, 0, sizeof(ctx->partition));
2401
277k
    memset(ctx->skip, 0, sizeof(ctx->skip));
2402
277k
    memset(ctx->skip_mode, 0, sizeof(ctx->skip_mode));
2403
277k
    memset(ctx->tx_lpf_y, 2, sizeof(ctx->tx_lpf_y));
2404
277k
    memset(ctx->tx_lpf_uv, 1, sizeof(ctx->tx_lpf_uv));
2405
277k
    memset(ctx->tx_intra, -1, sizeof(ctx->tx_intra));
2406
277k
    memset(ctx->tx, TX_64X64, sizeof(ctx->tx));
2407
277k
    if (!keyframe) {
2408
32.5k
        memset(ctx->ref, -1, sizeof(ctx->ref));
2409
32.5k
        memset(ctx->comp_type, 0, sizeof(ctx->comp_type));
2410
32.5k
        memset(ctx->mode, NEARESTMV, sizeof(ctx->mode));
2411
32.5k
    }
2412
277k
    memset(ctx->lcoef, 0x40, sizeof(ctx->lcoef));
2413
277k
    memset(ctx->ccoef, 0x40, sizeof(ctx->ccoef));
2414
277k
    memset(ctx->filter, DAV1D_N_SWITCHABLE_FILTERS, sizeof(ctx->filter));
2415
277k
    memset(ctx->seg_pred, 0, sizeof(ctx->seg_pred));
2416
277k
    memset(ctx->pal_sz, 0, sizeof(ctx->pal_sz));
2417
277k
}
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
57.8k
{
2433
57.8k
    const int col_sb_start = f->frame_hdr->tiling.col_start_sb[tile_col];
2434
57.8k
    const int col_sb128_start = col_sb_start >> !f->seq_hdr->sb128;
2435
57.8k
    const int col_sb_end = f->frame_hdr->tiling.col_start_sb[tile_col + 1];
2436
57.8k
    const int row_sb_start = f->frame_hdr->tiling.row_start_sb[tile_row];
2437
57.8k
    const int row_sb_end = f->frame_hdr->tiling.row_start_sb[tile_row + 1];
2438
57.8k
    const int sb_shift = f->sb_shift;
2439
2440
57.8k
    const uint8_t *const size_mul = ss_size_mul[f->cur.p.layout];
2441
173k
    for (int p = 0; p < 2; p++) {
2442
115k
        ts->frame_thread[p].pal_idx = f->frame_thread.pal_idx ?
2443
35.4k
            &f->frame_thread.pal_idx[(size_t)tile_start_off * size_mul[1] / 8] :
2444
115k
            NULL;
2445
115k
        ts->frame_thread[p].cbi = f->frame_thread.cbi ?
2446
115k
            &f->frame_thread.cbi[(size_t)tile_start_off * size_mul[0] / 64] :
2447
115k
            NULL;
2448
115k
        ts->frame_thread[p].cf = f->frame_thread.cf ?
2449
115k
            (uint8_t*)f->frame_thread.cf +
2450
115k
                (((size_t)tile_start_off * size_mul[0]) >> !f->seq_hdr->hbd) :
2451
115k
            NULL;
2452
115k
    }
2453
2454
57.8k
    dav1d_cdf_thread_copy(&ts->cdf, &f->in_cdf);
2455
57.8k
    ts->last_qidx = f->frame_hdr->quant.yac;
2456
57.8k
    ts->last_delta_lf.u32 = 0;
2457
2458
57.8k
    dav1d_msac_init(&ts->msac, data, sz, f->frame_hdr->disable_cdf_update);
2459
2460
57.8k
    ts->tiling.row = tile_row;
2461
57.8k
    ts->tiling.col = tile_col;
2462
57.8k
    ts->tiling.col_start = col_sb_start << sb_shift;
2463
57.8k
    ts->tiling.col_end = imin(col_sb_end << sb_shift, f->bw);
2464
57.8k
    ts->tiling.row_start = row_sb_start << sb_shift;
2465
57.8k
    ts->tiling.row_end = imin(row_sb_end << sb_shift, f->bh);
2466
2467
    // Reference Restoration Unit (used for exp coding)
2468
57.8k
    int sb_idx, unit_idx;
2469
57.8k
    if (f->frame_hdr->width[0] != f->frame_hdr->width[1]) {
2470
        // vertical components only
2471
10.7k
        sb_idx = (ts->tiling.row_start >> 5) * f->sr_sb128w;
2472
10.7k
        unit_idx = (ts->tiling.row_start & 16) >> 3;
2473
47.0k
    } else {
2474
47.0k
        sb_idx = (ts->tiling.row_start >> 5) * f->sb128w + col_sb128_start;
2475
47.0k
        unit_idx = ((ts->tiling.row_start & 16) >> 3) +
2476
47.0k
                   ((ts->tiling.col_start & 16) >> 4);
2477
47.0k
    }
2478
231k
    for (int p = 0; p < 3; p++) {
2479
173k
        if (!((f->lf.restore_planes >> p) & 1U))
2480
142k
            continue;
2481
2482
30.8k
        if (f->frame_hdr->width[0] != f->frame_hdr->width[1]) {
2483
11.5k
            const int ss_hor = p && f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
2484
11.5k
            const int d = f->frame_hdr->super_res.width_scale_denominator;
2485
11.5k
            const int unit_size_log2 = f->frame_hdr->restoration.unit_size[!!p];
2486
11.5k
            const int rnd = (8 << unit_size_log2) - 1, shift = unit_size_log2 + 3;
2487
11.5k
            const int x = ((4 * ts->tiling.col_start * d >> ss_hor) + rnd) >> shift;
2488
11.5k
            const int px_x = x << (unit_size_log2 + ss_hor);
2489
11.5k
            const int u_idx = unit_idx + ((px_x & 64) >> 6);
2490
11.5k
            const int sb128x = px_x >> 7;
2491
11.5k
            if (sb128x >= f->sr_sb128w) continue;
2492
11.2k
            ts->lr_ref[p] = &f->lf.lr_mask[sb_idx + sb128x].lr[p][u_idx];
2493
19.2k
        } else {
2494
19.2k
            ts->lr_ref[p] = &f->lf.lr_mask[sb_idx].lr[p][unit_idx];
2495
19.2k
        }
2496
2497
30.5k
        ts->lr_ref[p]->filter_v[0] = 3;
2498
30.5k
        ts->lr_ref[p]->filter_v[1] = -7;
2499
30.5k
        ts->lr_ref[p]->filter_v[2] = 15;
2500
30.5k
        ts->lr_ref[p]->filter_h[0] = 3;
2501
30.5k
        ts->lr_ref[p]->filter_h[1] = -7;
2502
30.5k
        ts->lr_ref[p]->filter_h[2] = 15;
2503
30.5k
        ts->lr_ref[p]->sgr_weights[0] = -32;
2504
30.5k
        ts->lr_ref[p]->sgr_weights[1] = 31;
2505
30.5k
    }
2506
2507
57.8k
    if (f->c->n_tc > 1) {
2508
173k
        for (int p = 0; p < 2; p++)
2509
115k
            atomic_init(&ts->progress[p], row_sb_start);
2510
57.8k
    }
2511
57.8k
}
2512
2513
static void read_restoration_info(Dav1dTaskContext *const t,
2514
                                  Av1RestorationUnit *const lr, const int p,
2515
                                  const enum Dav1dRestorationType frame_type)
2516
119k
{
2517
119k
    const Dav1dFrameContext *const f = t->f;
2518
119k
    Dav1dTileState *const ts = t->ts;
2519
119k
    const Av1RestorationUnit *const lr_ref = ts->lr_ref[p];
2520
2521
119k
    if (frame_type == DAV1D_RESTORATION_SWITCHABLE) {
2522
36.8k
        const int filter = dav1d_msac_decode_symbol_adapt4(&ts->msac,
2523
36.8k
                               ts->cdf.m.restore_switchable, 2);
2524
36.8k
        lr->type = filter + !!filter; /* NONE/WIENER/SGRPROJ */
2525
82.2k
    } else {
2526
82.2k
        const unsigned type =
2527
82.2k
            dav1d_msac_decode_bool_adapt(&ts->msac,
2528
82.2k
                frame_type == DAV1D_RESTORATION_WIENER ?
2529
41.7k
                ts->cdf.m.restore_wiener : ts->cdf.m.restore_sgrproj);
2530
82.2k
        lr->type = type ? frame_type : DAV1D_RESTORATION_NONE;
2531
82.2k
    }
2532
2533
119k
    if (lr->type == DAV1D_RESTORATION_WIENER) {
2534
32.6k
        lr->filter_v[0] = p ? 0 :
2535
32.6k
            dav1d_msac_decode_subexp(&ts->msac,
2536
13.5k
                lr_ref->filter_v[0] + 5, 16, 1) - 5;
2537
32.6k
        lr->filter_v[1] =
2538
32.6k
            dav1d_msac_decode_subexp(&ts->msac,
2539
32.6k
                lr_ref->filter_v[1] + 23, 32, 2) - 23;
2540
32.6k
        lr->filter_v[2] =
2541
32.6k
            dav1d_msac_decode_subexp(&ts->msac,
2542
32.6k
                lr_ref->filter_v[2] + 17, 64, 3) - 17;
2543
2544
32.6k
        lr->filter_h[0] = p ? 0 :
2545
32.6k
            dav1d_msac_decode_subexp(&ts->msac,
2546
13.5k
                lr_ref->filter_h[0] + 5, 16, 1) - 5;
2547
32.6k
        lr->filter_h[1] =
2548
32.6k
            dav1d_msac_decode_subexp(&ts->msac,
2549
32.6k
                lr_ref->filter_h[1] + 23, 32, 2) - 23;
2550
32.6k
        lr->filter_h[2] =
2551
32.6k
            dav1d_msac_decode_subexp(&ts->msac,
2552
32.6k
                lr_ref->filter_h[2] + 17, 64, 3) - 17;
2553
32.6k
        memcpy(lr->sgr_weights, lr_ref->sgr_weights, sizeof(lr->sgr_weights));
2554
32.6k
        ts->lr_ref[p] = lr;
2555
32.6k
        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
86.4k
    } else if (lr->type == DAV1D_RESTORATION_SGRPROJ) {
2561
35.9k
        const unsigned idx = dav1d_msac_decode_bools(&ts->msac, 4);
2562
35.9k
        const uint16_t *const sgr_params = dav1d_sgr_params[idx];
2563
35.9k
        lr->type += idx;
2564
35.9k
        lr->sgr_weights[0] = sgr_params[0] ? dav1d_msac_decode_subexp(&ts->msac,
2565
30.3k
            lr_ref->sgr_weights[0] + 96, 128, 4) - 96 : 0;
2566
35.9k
        lr->sgr_weights[1] = sgr_params[1] ? dav1d_msac_decode_subexp(&ts->msac,
2567
19.4k
            lr_ref->sgr_weights[1] + 32, 128, 4) - 32 : 95;
2568
35.9k
        memcpy(lr->filter_v, lr_ref->filter_v, sizeof(lr->filter_v));
2569
35.9k
        memcpy(lr->filter_h, lr_ref->filter_h, sizeof(lr->filter_h));
2570
35.9k
        ts->lr_ref[p] = lr;
2571
35.9k
        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
35.9k
    }
2576
119k
}
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
241k
int dav1d_decode_tile_sbrow(Dav1dTaskContext *const t) {
2598
241k
    const Dav1dFrameContext *const f = t->f;
2599
241k
    const enum BlockLevel root_bl = f->seq_hdr->sb128 ? BL_128X128 : BL_64X64;
2600
241k
    Dav1dTileState *const ts = t->ts;
2601
241k
    const Dav1dContext *const c = f->c;
2602
241k
    const int sb_step = f->sb_step;
2603
241k
    const int tile_row = ts->tiling.row, tile_col = ts->tiling.col;
2604
241k
    const int col_sb_start = f->frame_hdr->tiling.col_start_sb[tile_col];
2605
241k
    const int col_sb128_start = col_sb_start >> !f->seq_hdr->sb128;
2606
2607
241k
    if (IS_INTER_OR_SWITCH(f->frame_hdr) || f->frame_hdr->allow_intrabc) {
2608
73.6k
        dav1d_refmvs_tile_sbrow_init(&t->rt, &f->rf, ts->tiling.col_start,
2609
73.6k
                                     ts->tiling.col_end, ts->tiling.row_start,
2610
73.6k
                                     ts->tiling.row_end, t->by >> f->sb_shift,
2611
73.6k
                                     ts->tiling.row, t->frame_thread.pass);
2612
73.6k
    }
2613
2614
241k
    if (IS_INTER_OR_SWITCH(f->frame_hdr) && c->n_fc > 1) {
2615
30.8k
        const int sby = (t->by - ts->tiling.row_start) >> f->sb_shift;
2616
30.8k
        int (*const lowest_px)[2] = ts->lowest_pixel[sby];
2617
246k
        for (int n = 0; n < 7; n++)
2618
646k
            for (int m = 0; m < 2; m++)
2619
431k
                lowest_px[n][m] = INT_MIN;
2620
30.8k
    }
2621
2622
241k
    reset_context(&t->l, IS_KEY_OR_INTRA(f->frame_hdr), t->frame_thread.pass);
2623
241k
    if (t->frame_thread.pass == 2) {
2624
107k
        const int off_2pass = c->n_tc > 1 ? f->sb128w * f->frame_hdr->tiling.rows : 0;
2625
107k
        for (t->bx = ts->tiling.col_start,
2626
107k
             t->a = f->a + off_2pass + col_sb128_start + tile_row * f->sb128w;
2627
261k
             t->bx < ts->tiling.col_end; t->bx += sb_step)
2628
154k
        {
2629
154k
            if (atomic_load_explicit(c->flush, memory_order_acquire))
2630
327
                return 1;
2631
153k
            if (decode_sb(t, root_bl, dav1d_intra_edge_tree[root_bl]))
2632
0
                return 1;
2633
153k
            if (t->bx & 16 || f->seq_hdr->sb128)
2634
76.4k
                t->a++;
2635
153k
        }
2636
106k
        f->bd_fn.backup_ipred_edge(t);
2637
106k
        return 0;
2638
107k
    }
2639
2640
134k
    if (f->c->n_tc > 1 && f->frame_hdr->use_ref_frame_mvs) {
2641
2.85k
        f->c->refmvs_dsp.load_tmvs(&f->rf, ts->tiling.row,
2642
2.85k
                                   ts->tiling.col_start >> 1, ts->tiling.col_end >> 1,
2643
2.85k
                                   t->by >> 1, (t->by + sb_step) >> 1);
2644
2.85k
    }
2645
134k
    memset(t->pal_sz_uv[1], 0, sizeof(*t->pal_sz_uv));
2646
134k
    const int sb128y = t->by >> 5;
2647
134k
    for (t->bx = ts->tiling.col_start, t->a = f->a + col_sb128_start + tile_row * f->sb128w,
2648
134k
         t->lf_mask = f->lf.mask + sb128y * f->sb128w + col_sb128_start;
2649
429k
         t->bx < ts->tiling.col_end; t->bx += sb_step)
2650
296k
    {
2651
296k
        if (atomic_load_explicit(c->flush, memory_order_acquire))
2652
315
            return 1;
2653
296k
        if (root_bl == BL_128X128) {
2654
99.8k
            t->cur_sb_cdef_idx_ptr = t->lf_mask->cdef_idx;
2655
99.8k
            t->cur_sb_cdef_idx_ptr[0] = -1;
2656
99.8k
            t->cur_sb_cdef_idx_ptr[1] = -1;
2657
99.8k
            t->cur_sb_cdef_idx_ptr[2] = -1;
2658
99.8k
            t->cur_sb_cdef_idx_ptr[3] = -1;
2659
196k
        } else {
2660
196k
            t->cur_sb_cdef_idx_ptr =
2661
196k
                &t->lf_mask->cdef_idx[((t->bx & 16) >> 4) +
2662
196k
                                      ((t->by & 16) >> 3)];
2663
196k
            t->cur_sb_cdef_idx_ptr[0] = -1;
2664
196k
        }
2665
        // Restoration filter
2666
1.18M
        for (int p = 0; p < 3; p++) {
2667
888k
            if (!((f->lf.restore_planes >> p) & 1U))
2668
727k
                continue;
2669
2670
160k
            const int ss_ver = p && f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
2671
160k
            const int ss_hor = p && f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
2672
160k
            const int unit_size_log2 = f->frame_hdr->restoration.unit_size[!!p];
2673
160k
            const int y = t->by * 4 >> ss_ver;
2674
160k
            const int h = (f->cur.p.h + ss_ver) >> ss_ver;
2675
2676
160k
            const int unit_size = 1 << unit_size_log2;
2677
160k
            const unsigned mask = unit_size - 1;
2678
160k
            if (y & mask) continue;
2679
134k
            const int half_unit = unit_size >> 1;
2680
            // Round half up at frame boundaries, if there's more than one
2681
            // restoration unit
2682
134k
            if (y && y + half_unit > h) continue;
2683
2684
126k
            const enum Dav1dRestorationType frame_type = f->frame_hdr->restoration.type[p];
2685
2686
126k
            if (f->frame_hdr->width[0] != f->frame_hdr->width[1]) {
2687
33.2k
                const int w = (f->sr_cur.p.p.w + ss_hor) >> ss_hor;
2688
33.2k
                const int n_units = imax(1, (w + half_unit) >> unit_size_log2);
2689
2690
33.2k
                const int d = f->frame_hdr->super_res.width_scale_denominator;
2691
33.2k
                const int rnd = unit_size * 8 - 1, shift = unit_size_log2 + 3;
2692
33.2k
                const int x0 = ((4 *  t->bx            * d >> ss_hor) + rnd) >> shift;
2693
33.2k
                const int x1 = ((4 * (t->bx + sb_step) * d >> ss_hor) + rnd) >> shift;
2694
2695
68.9k
                for (int x = x0; x < imin(x1, n_units); x++) {
2696
35.6k
                    const int px_x = x << (unit_size_log2 + ss_hor);
2697
35.6k
                    const int sb_idx = (t->by >> 5) * f->sr_sb128w + (px_x >> 7);
2698
35.6k
                    const int unit_idx = ((t->by & 16) >> 3) + ((px_x & 64) >> 6);
2699
35.6k
                    Av1RestorationUnit *const lr = &f->lf.lr_mask[sb_idx].lr[p][unit_idx];
2700
2701
35.6k
                    read_restoration_info(t, lr, p, frame_type);
2702
35.6k
                }
2703
93.4k
            } else {
2704
93.4k
                const int x = 4 * t->bx >> ss_hor;
2705
93.4k
                if (x & mask) continue;
2706
84.4k
                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
84.4k
                if (x && x + half_unit > w) continue;
2710
83.3k
                const int sb_idx = (t->by >> 5) * f->sr_sb128w + (t->bx >> 5);
2711
83.3k
                const int unit_idx = ((t->by & 16) >> 3) + ((t->bx & 16) >> 4);
2712
83.3k
                Av1RestorationUnit *const lr = &f->lf.lr_mask[sb_idx].lr[p][unit_idx];
2713
2714
83.3k
                read_restoration_info(t, lr, p, frame_type);
2715
83.3k
            }
2716
126k
        }
2717
296k
        if (decode_sb(t, root_bl, dav1d_intra_edge_tree[root_bl]))
2718
770
            return 1;
2719
295k
        if (t->bx & 16 || f->seq_hdr->sb128) {
2720
160k
            t->a++;
2721
160k
            t->lf_mask++;
2722
160k
        }
2723
295k
    }
2724
2725
133k
    if (f->seq_hdr->ref_frame_mvs && f->c->n_tc > 1 && IS_INTER_OR_SWITCH(f->frame_hdr)) {
2726
9.13k
        dav1d_refmvs_save_tmvs(&f->c->refmvs_dsp, &t->rt,
2727
9.13k
                               ts->tiling.col_start >> 1, ts->tiling.col_end >> 1,
2728
9.13k
                               t->by >> 1, (t->by + sb_step) >> 1);
2729
9.13k
    }
2730
2731
    // backup pre-loopfilter pixels for intra prediction of the next sbrow
2732
133k
    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
133k
    int align_h = (f->bh + 31) & ~31;
2738
133k
    memcpy(&f->lf.tx_lpf_right_edge[0][align_h * tile_col + t->by],
2739
133k
           &t->l.tx_lpf_y[t->by & 16], sb_step);
2740
133k
    const int ss_ver = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
2741
133k
    align_h >>= ss_ver;
2742
133k
    memcpy(&f->lf.tx_lpf_right_edge[1][align_h * tile_col + (t->by >> ss_ver)],
2743
133k
           &t->l.tx_lpf_uv[(t->by & 16) >> ss_ver], sb_step >> ss_ver);
2744
2745
    // error out on symbol decoder overread
2746
133k
    if (ts->msac.cnt <= -15) return 1;
2747
2748
120k
    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
133k
}
2752
2753
52.2k
int dav1d_decode_frame_init(Dav1dFrameContext *const f) {
2754
52.2k
    const Dav1dContext *const c = f->c;
2755
52.2k
    int retval = DAV1D_ERR(ENOMEM);
2756
2757
52.2k
    if (f->sbh > f->lf.start_of_tile_row_sz) {
2758
51.5k
        dav1d_free(f->lf.start_of_tile_row);
2759
51.5k
        f->lf.start_of_tile_row = dav1d_malloc(ALLOC_TILE, f->sbh * sizeof(uint8_t));
2760
51.5k
        if (!f->lf.start_of_tile_row) {
2761
0
            f->lf.start_of_tile_row_sz = 0;
2762
0
            goto error;
2763
0
        }
2764
51.5k
        f->lf.start_of_tile_row_sz = f->sbh;
2765
51.5k
    }
2766
52.2k
    int sby = 0;
2767
111k
    for (int tile_row = 0; tile_row < f->frame_hdr->tiling.rows; tile_row++) {
2768
58.8k
        f->lf.start_of_tile_row[sby++] = tile_row;
2769
265k
        while (sby < f->frame_hdr->tiling.row_start_sb[tile_row + 1])
2770
207k
            f->lf.start_of_tile_row[sby++] = 0;
2771
58.8k
    }
2772
2773
52.2k
    const int n_ts = f->frame_hdr->tiling.cols * f->frame_hdr->tiling.rows;
2774
52.2k
    if (n_ts != f->n_ts) {
2775
51.5k
        if (c->n_fc > 1) {
2776
51.5k
            dav1d_free(f->frame_thread.tile_start_off);
2777
51.5k
            f->frame_thread.tile_start_off =
2778
51.5k
                dav1d_malloc(ALLOC_TILE, sizeof(*f->frame_thread.tile_start_off) * n_ts);
2779
51.5k
            if (!f->frame_thread.tile_start_off) {
2780
0
                f->n_ts = 0;
2781
0
                goto error;
2782
0
            }
2783
51.5k
        }
2784
51.5k
        dav1d_free_aligned(f->ts);
2785
51.5k
        f->ts = dav1d_alloc_aligned(ALLOC_TILE, sizeof(*f->ts) * n_ts, 32);
2786
51.5k
        if (!f->ts) goto error;
2787
51.5k
        f->n_ts = n_ts;
2788
51.5k
    }
2789
2790
52.2k
    const int a_sz = f->sb128w * f->frame_hdr->tiling.rows * (1 + (c->n_fc > 1 && c->n_tc > 1));
2791
52.2k
    if (a_sz != f->a_sz) {
2792
51.5k
        dav1d_free(f->a);
2793
51.5k
        f->a = dav1d_malloc(ALLOC_TILE, sizeof(*f->a) * a_sz);
2794
51.5k
        if (!f->a) {
2795
0
            f->a_sz = 0;
2796
0
            goto error;
2797
0
        }
2798
51.5k
        f->a_sz = a_sz;
2799
51.5k
    }
2800
2801
52.2k
    const int num_sb128 = f->sb128w * f->sb128h;
2802
52.2k
    const uint8_t *const size_mul = ss_size_mul[f->cur.p.layout];
2803
52.2k
    const int hbd = !!f->seq_hdr->hbd;
2804
52.2k
    if (c->n_fc > 1) {
2805
52.2k
        const unsigned sb_step4 = f->sb_step * 4;
2806
52.2k
        int tile_idx = 0;
2807
111k
        for (int tile_row = 0; tile_row < f->frame_hdr->tiling.rows; tile_row++) {
2808
58.8k
            const unsigned row_off = f->frame_hdr->tiling.row_start_sb[tile_row] *
2809
58.8k
                                     sb_step4 * f->sb128w * 128;
2810
58.8k
            const unsigned b_diff = (f->frame_hdr->tiling.row_start_sb[tile_row + 1] -
2811
58.8k
                                     f->frame_hdr->tiling.row_start_sb[tile_row]) * sb_step4;
2812
140k
            for (int tile_col = 0; tile_col < f->frame_hdr->tiling.cols; tile_col++) {
2813
81.4k
                f->frame_thread.tile_start_off[tile_idx++] = row_off + b_diff *
2814
81.4k
                    f->frame_hdr->tiling.col_start_sb[tile_col] * sb_step4;
2815
81.4k
            }
2816
58.8k
        }
2817
2818
52.2k
        const int lowest_pixel_mem_sz = f->frame_hdr->tiling.cols * f->sbh;
2819
52.2k
        if (lowest_pixel_mem_sz != f->tile_thread.lowest_pixel_mem_sz) {
2820
51.5k
            dav1d_free(f->tile_thread.lowest_pixel_mem);
2821
51.5k
            f->tile_thread.lowest_pixel_mem =
2822
51.5k
                dav1d_malloc(ALLOC_TILE, lowest_pixel_mem_sz *
2823
51.5k
                             sizeof(*f->tile_thread.lowest_pixel_mem));
2824
51.5k
            if (!f->tile_thread.lowest_pixel_mem) {
2825
0
                f->tile_thread.lowest_pixel_mem_sz = 0;
2826
0
                goto error;
2827
0
            }
2828
51.5k
            f->tile_thread.lowest_pixel_mem_sz = lowest_pixel_mem_sz;
2829
51.5k
        }
2830
52.2k
        int (*lowest_pixel_ptr)[7][2] = f->tile_thread.lowest_pixel_mem;
2831
111k
        for (int tile_row = 0, tile_row_base = 0; tile_row < f->frame_hdr->tiling.rows;
2832
58.8k
             tile_row++, tile_row_base += f->frame_hdr->tiling.cols)
2833
58.8k
        {
2834
58.8k
            const int tile_row_sb_h = f->frame_hdr->tiling.row_start_sb[tile_row + 1] -
2835
58.8k
                                      f->frame_hdr->tiling.row_start_sb[tile_row];
2836
140k
            for (int tile_col = 0; tile_col < f->frame_hdr->tiling.cols; tile_col++) {
2837
81.4k
                f->ts[tile_row_base + tile_col].lowest_pixel = lowest_pixel_ptr;
2838
81.4k
                lowest_pixel_ptr += tile_row_sb_h;
2839
81.4k
            }
2840
58.8k
        }
2841
2842
52.2k
        const int cbi_sz = num_sb128 * size_mul[0];
2843
52.2k
        if (cbi_sz != f->frame_thread.cbi_sz) {
2844
51.5k
            dav1d_free_aligned(f->frame_thread.cbi);
2845
51.5k
            f->frame_thread.cbi =
2846
51.5k
                dav1d_alloc_aligned(ALLOC_BLOCK, sizeof(*f->frame_thread.cbi) *
2847
51.5k
                                    cbi_sz * 32 * 32 / 4, 64);
2848
51.5k
            if (!f->frame_thread.cbi) {
2849
0
                f->frame_thread.cbi_sz = 0;
2850
0
                goto error;
2851
0
            }
2852
51.5k
            f->frame_thread.cbi_sz = cbi_sz;
2853
51.5k
        }
2854
2855
52.2k
        const int cf_sz = (num_sb128 * size_mul[0]) << hbd;
2856
52.2k
        if (cf_sz != f->frame_thread.cf_sz) {
2857
51.5k
            dav1d_free_aligned(f->frame_thread.cf);
2858
51.5k
            f->frame_thread.cf =
2859
51.5k
                dav1d_alloc_aligned(ALLOC_COEF, (size_t)cf_sz * 128 * 128 / 2, 64);
2860
51.5k
            if (!f->frame_thread.cf) {
2861
0
                f->frame_thread.cf_sz = 0;
2862
0
                goto error;
2863
0
            }
2864
51.5k
            memset(f->frame_thread.cf, 0, (size_t)cf_sz * 128 * 128 / 2);
2865
51.5k
            f->frame_thread.cf_sz = cf_sz;
2866
51.5k
        }
2867
2868
52.2k
        if (f->frame_hdr->allow_screen_content_tools) {
2869
15.1k
            const int pal_sz = num_sb128 << hbd;
2870
15.1k
            if (pal_sz != f->frame_thread.pal_sz) {
2871
14.9k
                dav1d_free_aligned(f->frame_thread.pal);
2872
14.9k
                f->frame_thread.pal =
2873
14.9k
                    dav1d_alloc_aligned(ALLOC_PAL, sizeof(*f->frame_thread.pal) *
2874
14.9k
                                        pal_sz * 16 * 16, 64);
2875
14.9k
                if (!f->frame_thread.pal) {
2876
0
                    f->frame_thread.pal_sz = 0;
2877
0
                    goto error;
2878
0
                }
2879
14.9k
                f->frame_thread.pal_sz = pal_sz;
2880
14.9k
            }
2881
2882
15.1k
            const int pal_idx_sz = num_sb128 * size_mul[1];
2883
15.1k
            if (pal_idx_sz != f->frame_thread.pal_idx_sz) {
2884
14.9k
                dav1d_free_aligned(f->frame_thread.pal_idx);
2885
14.9k
                f->frame_thread.pal_idx =
2886
14.9k
                    dav1d_alloc_aligned(ALLOC_PAL, sizeof(*f->frame_thread.pal_idx) *
2887
14.9k
                                        pal_idx_sz * 128 * 128 / 8, 64);
2888
14.9k
                if (!f->frame_thread.pal_idx) {
2889
0
                    f->frame_thread.pal_idx_sz = 0;
2890
0
                    goto error;
2891
0
                }
2892
14.9k
                f->frame_thread.pal_idx_sz = pal_idx_sz;
2893
14.9k
            }
2894
37.0k
        } else if (f->frame_thread.pal) {
2895
11
            dav1d_freep_aligned(&f->frame_thread.pal);
2896
11
            dav1d_freep_aligned(&f->frame_thread.pal_idx);
2897
11
            f->frame_thread.pal_sz = f->frame_thread.pal_idx_sz = 0;
2898
11
        }
2899
52.2k
    }
2900
2901
    // update allocation of block contexts for above
2902
52.2k
    ptrdiff_t y_stride = f->cur.stride[0], uv_stride = f->cur.stride[1];
2903
52.2k
    const int has_resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
2904
52.2k
    const int need_cdef_lpf_copy = c->n_tc > 1 && has_resize;
2905
52.2k
    if (y_stride * f->sbh * 4 != f->lf.cdef_buf_plane_sz[0] ||
2906
663
        uv_stride * f->sbh * 8 != f->lf.cdef_buf_plane_sz[1] ||
2907
663
        need_cdef_lpf_copy != f->lf.need_cdef_lpf_copy ||
2908
663
        f->sbh != f->lf.cdef_buf_sbh)
2909
51.5k
    {
2910
51.5k
        dav1d_free_aligned(f->lf.cdef_line_buf);
2911
51.5k
        size_t alloc_sz = 64;
2912
51.5k
        alloc_sz += (size_t)llabs(y_stride) * 4 * f->sbh << need_cdef_lpf_copy;
2913
51.5k
        alloc_sz += (size_t)llabs(uv_stride) * 8 * f->sbh << need_cdef_lpf_copy;
2914
51.5k
        uint8_t *ptr = f->lf.cdef_line_buf = dav1d_alloc_aligned(ALLOC_CDEF, alloc_sz, 32);
2915
51.5k
        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
51.5k
        ptr += 32;
2921
51.5k
        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
51.5k
        } else {
2925
51.5k
            f->lf.cdef_line[0][0] = ptr + y_stride * 0;
2926
51.5k
            f->lf.cdef_line[1][0] = ptr + y_stride * 2;
2927
51.5k
        }
2928
51.5k
        ptr += llabs(y_stride) * f->sbh * 4;
2929
51.5k
        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
51.5k
        } else {
2935
51.5k
            f->lf.cdef_line[0][1] = ptr + uv_stride * 0;
2936
51.5k
            f->lf.cdef_line[0][2] = ptr + uv_stride * 2;
2937
51.5k
            f->lf.cdef_line[1][1] = ptr + uv_stride * 4;
2938
51.5k
            f->lf.cdef_line[1][2] = ptr + uv_stride * 6;
2939
51.5k
        }
2940
2941
51.5k
        if (need_cdef_lpf_copy) {
2942
9.18k
            ptr += llabs(uv_stride) * f->sbh * 8;
2943
9.18k
            if (y_stride < 0)
2944
0
                f->lf.cdef_lpf_line[0] = ptr - y_stride * (f->sbh * 4 - 1);
2945
9.18k
            else
2946
9.18k
                f->lf.cdef_lpf_line[0] = ptr;
2947
9.18k
            ptr += llabs(y_stride) * f->sbh * 4;
2948
9.18k
            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
9.18k
            } else {
2952
9.18k
                f->lf.cdef_lpf_line[1] = ptr;
2953
9.18k
                f->lf.cdef_lpf_line[2] = ptr + uv_stride * f->sbh * 4;
2954
9.18k
            }
2955
9.18k
        }
2956
2957
51.5k
        f->lf.cdef_buf_plane_sz[0] = (int) y_stride * f->sbh * 4;
2958
51.5k
        f->lf.cdef_buf_plane_sz[1] = (int) uv_stride * f->sbh * 8;
2959
51.5k
        f->lf.need_cdef_lpf_copy = need_cdef_lpf_copy;
2960
51.5k
        f->lf.cdef_buf_sbh = f->sbh;
2961
51.5k
    }
2962
2963
52.2k
    const int sb128 = f->seq_hdr->sb128;
2964
52.2k
    const int num_lines = c->n_tc > 1 ? f->sbh * 4 << sb128 : 12;
2965
52.2k
    y_stride = f->sr_cur.p.stride[0], uv_stride = f->sr_cur.p.stride[1];
2966
52.2k
    if (y_stride * num_lines != f->lf.lr_buf_plane_sz[0] ||
2967
671
        uv_stride * num_lines * 2 != f->lf.lr_buf_plane_sz[1])
2968
51.5k
    {
2969
51.5k
        dav1d_free_aligned(f->lf.lr_line_buf);
2970
        // lr simd may overread the input, so slightly over-allocate the lpf buffer
2971
51.5k
        size_t alloc_sz = 128;
2972
51.5k
        alloc_sz += (size_t)llabs(y_stride) * num_lines;
2973
51.5k
        alloc_sz += (size_t)llabs(uv_stride) * num_lines * 2;
2974
51.5k
        uint8_t *ptr = f->lf.lr_line_buf = dav1d_alloc_aligned(ALLOC_LR, alloc_sz, 64);
2975
51.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
51.5k
        ptr += 64;
2981
51.5k
        if (y_stride < 0)
2982
0
            f->lf.lr_lpf_line[0] = ptr - y_stride * (num_lines - 1);
2983
51.5k
        else
2984
51.5k
            f->lf.lr_lpf_line[0] = ptr;
2985
51.5k
        ptr += llabs(y_stride) * num_lines;
2986
51.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
51.5k
        } else {
2990
51.5k
            f->lf.lr_lpf_line[1] = ptr;
2991
51.5k
            f->lf.lr_lpf_line[2] = ptr + uv_stride * num_lines;
2992
51.5k
        }
2993
2994
51.5k
        f->lf.lr_buf_plane_sz[0] = (int) y_stride * num_lines;
2995
51.5k
        f->lf.lr_buf_plane_sz[1] = (int) uv_stride * num_lines * 2;
2996
51.5k
    }
2997
2998
    // update allocation for loopfilter masks
2999
52.2k
    if (num_sb128 != f->lf.mask_sz) {
3000
51.5k
        dav1d_free(f->lf.mask);
3001
51.5k
        dav1d_free(f->lf.level);
3002
51.5k
        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
51.5k
        f->lf.level = dav1d_malloc(ALLOC_LF, sizeof(*f->lf.level) * num_sb128 * 32 * 32 + 3);
3006
51.5k
        if (!f->lf.mask || !f->lf.level) {
3007
0
            f->lf.mask_sz = 0;
3008
0
            goto error;
3009
0
        }
3010
51.5k
        if (c->n_fc > 1) {
3011
51.5k
            dav1d_free(f->frame_thread.b);
3012
51.5k
            f->frame_thread.b = dav1d_malloc(ALLOC_BLOCK, sizeof(*f->frame_thread.b) *
3013
51.5k
                                             num_sb128 * 32 * 32);
3014
51.5k
            if (!f->frame_thread.b) {
3015
0
                f->lf.mask_sz = 0;
3016
0
                goto error;
3017
0
            }
3018
51.5k
        }
3019
51.5k
        f->lf.mask_sz = num_sb128;
3020
51.5k
    }
3021
3022
52.2k
    f->sr_sb128w = (f->sr_cur.p.p.w + 127) >> 7;
3023
52.2k
    const int lr_mask_sz = f->sr_sb128w * f->sb128h;
3024
52.2k
    if (lr_mask_sz != f->lf.lr_mask_sz) {
3025
51.5k
        dav1d_free(f->lf.lr_mask);
3026
51.5k
        f->lf.lr_mask = dav1d_malloc(ALLOC_LR, sizeof(*f->lf.lr_mask) * lr_mask_sz);
3027
51.5k
        if (!f->lf.lr_mask) {
3028
0
            f->lf.lr_mask_sz = 0;
3029
0
            goto error;
3030
0
        }
3031
51.5k
        f->lf.lr_mask_sz = lr_mask_sz;
3032
51.5k
    }
3033
52.2k
    f->lf.restore_planes =
3034
52.2k
        ((f->frame_hdr->restoration.type[0] != DAV1D_RESTORATION_NONE) << 0) +
3035
52.2k
        ((f->frame_hdr->restoration.type[1] != DAV1D_RESTORATION_NONE) << 1) +
3036
52.2k
        ((f->frame_hdr->restoration.type[2] != DAV1D_RESTORATION_NONE) << 2);
3037
52.2k
    if (f->frame_hdr->loopfilter.sharpness != f->lf.last_sharpness) {
3038
51.6k
        dav1d_calc_eih(&f->lf.lim_lut, f->frame_hdr->loopfilter.sharpness);
3039
51.6k
        f->lf.last_sharpness = f->frame_hdr->loopfilter.sharpness;
3040
51.6k
    }
3041
52.2k
    dav1d_calc_lf_values(f->lf.lvl, f->frame_hdr, (int8_t[4]) { 0, 0, 0, 0 });
3042
52.2k
    memset(f->lf.mask, 0, sizeof(*f->lf.mask) * num_sb128);
3043
3044
52.2k
    const int ipred_edge_sz = f->sbh * f->sb128w << hbd;
3045
52.2k
    if (ipred_edge_sz != f->ipred_edge_sz) {
3046
51.5k
        dav1d_free_aligned(f->ipred_edge[0]);
3047
51.5k
        uint8_t *ptr = f->ipred_edge[0] =
3048
51.5k
            dav1d_alloc_aligned(ALLOC_IPRED, ipred_edge_sz * 128 * 3, 64);
3049
51.5k
        if (!ptr) {
3050
0
            f->ipred_edge_sz = 0;
3051
0
            goto error;
3052
0
        }
3053
51.5k
        f->ipred_edge[1] = ptr + ipred_edge_sz * 128 * 1;
3054
51.5k
        f->ipred_edge[2] = ptr + ipred_edge_sz * 128 * 2;
3055
51.5k
        f->ipred_edge_sz = ipred_edge_sz;
3056
51.5k
    }
3057
3058
52.2k
    const int re_sz = f->sb128h * f->frame_hdr->tiling.cols;
3059
52.2k
    if (re_sz != f->lf.re_sz) {
3060
51.5k
        dav1d_free(f->lf.tx_lpf_right_edge[0]);
3061
51.5k
        f->lf.tx_lpf_right_edge[0] = dav1d_malloc(ALLOC_LF, re_sz * 32 * 2);
3062
51.5k
        if (!f->lf.tx_lpf_right_edge[0]) {
3063
0
            f->lf.re_sz = 0;
3064
0
            goto error;
3065
0
        }
3066
51.5k
        f->lf.tx_lpf_right_edge[1] = f->lf.tx_lpf_right_edge[0] + re_sz * 32;
3067
51.5k
        f->lf.re_sz = re_sz;
3068
51.5k
    }
3069
3070
    // init ref mvs
3071
52.2k
    if (IS_INTER_OR_SWITCH(f->frame_hdr) || f->frame_hdr->allow_intrabc) {
3072
18.9k
        const int ret =
3073
18.9k
            dav1d_refmvs_init_frame(&f->rf, f->seq_hdr, f->frame_hdr,
3074
18.9k
                                    f->refpoc, f->mvs, f->refrefpoc, f->ref_mvs,
3075
18.9k
                                    f->c->n_tc, f->c->n_fc);
3076
18.9k
        if (ret < 0) goto error;
3077
18.9k
    }
3078
3079
    // setup dequant tables
3080
52.2k
    init_quant_tables(f->seq_hdr, f->frame_hdr, f->frame_hdr->quant.yac, f->dq);
3081
52.2k
    if (f->frame_hdr->quant.qm)
3082
436k
        for (int i = 0; i < N_RECT_TX_SIZES; i++) {
3083
414k
            f->qm[i][0] = dav1d_qm_tbl[f->frame_hdr->quant.qm_y][0][i];
3084
414k
            f->qm[i][1] = dav1d_qm_tbl[f->frame_hdr->quant.qm_u][1][i];
3085
414k
            f->qm[i][2] = dav1d_qm_tbl[f->frame_hdr->quant.qm_v][1][i];
3086
414k
        }
3087
30.3k
    else
3088
30.3k
        memset(f->qm, 0, sizeof(f->qm));
3089
3090
    // setup jnt_comp weights
3091
52.2k
    if (f->frame_hdr->switchable_comp_refs) {
3092
60.2k
        for (int i = 0; i < 7; i++) {
3093
52.7k
            const unsigned ref0poc = f->refp[i].p.frame_hdr->frame_offset;
3094
3095
210k
            for (int j = i + 1; j < 7; j++) {
3096
158k
                const unsigned ref1poc = f->refp[j].p.frame_hdr->frame_offset;
3097
3098
158k
                const unsigned d1 =
3099
158k
                    imin(abs(get_poc_diff(f->seq_hdr->order_hint_n_bits, ref0poc,
3100
158k
                                          f->cur.frame_hdr->frame_offset)), 31);
3101
158k
                const unsigned d0 =
3102
158k
                    imin(abs(get_poc_diff(f->seq_hdr->order_hint_n_bits, ref1poc,
3103
158k
                                          f->cur.frame_hdr->frame_offset)), 31);
3104
158k
                const int order = d0 <= d1;
3105
3106
158k
                static const uint8_t quant_dist_weight[3][2] = {
3107
158k
                    { 2, 3 }, { 2, 5 }, { 2, 7 }
3108
158k
                };
3109
158k
                static const uint8_t quant_dist_lookup_table[4][2] = {
3110
158k
                    { 9, 7 }, { 11, 5 }, { 12, 4 }, { 13, 3 }
3111
158k
                };
3112
3113
158k
                int k;
3114
422k
                for (k = 0; k < 3; k++) {
3115
336k
                    const int c0 = quant_dist_weight[k][order];
3116
336k
                    const int c1 = quant_dist_weight[k][!order];
3117
336k
                    const int d0_c0 = d0 * c0;
3118
336k
                    const int d1_c1 = d1 * c1;
3119
336k
                    if ((d0 > d1 && d0_c0 < d1_c1) || (d0 <= d1 && d0_c0 > d1_c1)) break;
3120
336k
                }
3121
3122
158k
                f->jnt_weights[i][j] = quant_dist_lookup_table[k][order];
3123
158k
            }
3124
52.7k
        }
3125
7.53k
    }
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
52.2k
    const int has_chroma = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I400;
3133
52.2k
    f->lf.p[0] = f->cur.data[0];
3134
52.2k
    f->lf.p[1] = f->cur.data[has_chroma ? 1 : 0];
3135
52.2k
    f->lf.p[2] = f->cur.data[has_chroma ? 2 : 0];
3136
52.2k
    f->lf.sr_p[0] = f->sr_cur.p.data[0];
3137
52.2k
    f->lf.sr_p[1] = f->sr_cur.p.data[has_chroma ? 1 : 0];
3138
52.2k
    f->lf.sr_p[2] = f->sr_cur.p.data[has_chroma ? 2 : 0];
3139
3140
52.2k
    retval = 0;
3141
52.2k
error:
3142
52.2k
    return retval;
3143
52.2k
}
3144
3145
51.3k
int dav1d_decode_frame_init_cdf(Dav1dFrameContext *const f) {
3146
51.3k
    const Dav1dContext *const c = f->c;
3147
51.3k
    int retval = DAV1D_ERR(EINVAL);
3148
3149
51.3k
    if (f->frame_hdr->refresh_context)
3150
8.50k
        dav1d_cdf_thread_copy(f->out_cdf.data.cdf, &f->in_cdf);
3151
3152
    // parse individual tiles per tile group
3153
51.3k
    int tile_row = 0, tile_col = 0;
3154
51.3k
    f->task_thread.update_set = 0;
3155
101k
    for (int i = 0; i < f->n_tile_data; i++) {
3156
51.3k
        const uint8_t *data = f->tile[i].data.data;
3157
51.3k
        size_t size = f->tile[i].data.sz;
3158
3159
109k
        for (int j = f->tile[i].start; j <= f->tile[i].end; j++) {
3160
58.6k
            size_t tile_sz;
3161
58.6k
            if (j == f->tile[i].end) {
3162
50.4k
                tile_sz = size;
3163
50.4k
            } else {
3164
8.20k
                if (f->frame_hdr->tiling.n_bytes > size) goto error;
3165
8.12k
                tile_sz = 0;
3166
19.3k
                for (unsigned k = 0; k < f->frame_hdr->tiling.n_bytes; k++)
3167
11.1k
                    tile_sz |= (unsigned)*data++ << (k * 8);
3168
8.12k
                tile_sz++;
3169
8.12k
                size -= f->frame_hdr->tiling.n_bytes;
3170
8.12k
                if (tile_sz > size) goto error;
3171
8.12k
            }
3172
3173
57.8k
            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
57.8k
            if (tile_col == f->frame_hdr->tiling.cols) {
3177
53.5k
                tile_col = 0;
3178
53.5k
                tile_row++;
3179
53.5k
            }
3180
57.8k
            if (j == f->frame_hdr->tiling.update && f->frame_hdr->refresh_context)
3181
8.41k
                f->task_thread.update_set = 1;
3182
57.8k
            data += tile_sz;
3183
57.8k
            size -= tile_sz;
3184
57.8k
        }
3185
51.3k
    }
3186
3187
50.4k
    if (c->n_tc > 1) {
3188
50.4k
        const int uses_2pass = c->n_fc > 1;
3189
335k
        for (int n = 0; n < f->sb128w * f->frame_hdr->tiling.rows * (1 + uses_2pass); n++)
3190
285k
            reset_context(&f->a[n], IS_KEY_OR_INTRA(f->frame_hdr),
3191
285k
                          uses_2pass ? 1 + (n >= f->sb128w * f->frame_hdr->tiling.rows) : 0);
3192
50.4k
    }
3193
3194
50.4k
    retval = 0;
3195
51.2k
error:
3196
51.2k
    return retval;
3197
50.4k
}
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
276k
void dav1d_decode_frame_exit(Dav1dFrameContext *const f, int retval) {
3246
276k
    const Dav1dContext *const c = f->c;
3247
3248
276k
    if (f->sr_cur.p.data[0])
3249
276k
        atomic_init(&f->task_thread.error, 0);
3250
3251
276k
    if (c->n_fc > 1 && retval && f->frame_thread.cf) {
3252
65.1k
        memset(f->frame_thread.cf, 0,
3253
65.1k
               (size_t)f->frame_thread.cf_sz * 128 * 128 / 2);
3254
65.1k
    }
3255
2.21M
    for (int i = 0; i < 7; i++) {
3256
1.93M
        if (f->refp[i].p.frame_hdr) {
3257
93.5k
            if (!retval && c->n_fc > 1 && c->strict_std_compliance &&
3258
93.5k
                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
93.5k
            dav1d_thread_picture_unref(&f->refp[i]);
3265
93.5k
        }
3266
1.93M
        dav1d_ref_dec(&f->ref_mvs_ref[i]);
3267
1.93M
    }
3268
3269
276k
    dav1d_picture_unref_internal(&f->cur);
3270
276k
    dav1d_thread_picture_unref(&f->sr_cur);
3271
276k
    dav1d_cdf_thread_unref(&f->in_cdf);
3272
276k
    if (f->frame_hdr && f->frame_hdr->refresh_context) {
3273
16.3k
        if (f->out_cdf.progress)
3274
16.3k
            atomic_store(f->out_cdf.progress, retval == 0 ? 1 : TILE_ERROR);
3275
16.3k
        dav1d_cdf_thread_unref(&f->out_cdf);
3276
16.3k
    }
3277
276k
    dav1d_ref_dec(&f->cur_segmap_ref);
3278
276k
    dav1d_ref_dec(&f->prev_segmap_ref);
3279
276k
    dav1d_ref_dec(&f->mvs_ref);
3280
276k
    dav1d_ref_dec(&f->seq_hdr_ref);
3281
276k
    dav1d_ref_dec(&f->frame_hdr_ref);
3282
3283
330k
    for (int i = 0; i < f->n_tile_data; i++)
3284
53.4k
        dav1d_data_unref_internal(&f->tile[i].data);
3285
276k
    f->task_thread.retval = retval;
3286
276k
}
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
18.6k
static int get_upscale_x0(const int in_w, const int out_w, const int step) {
3325
18.6k
    const int err = out_w * step - (in_w << 14);
3326
18.6k
    const int x0 = (-((out_w - in_w) << 13) + (out_w >> 1)) / out_w + 128 - (err / 2);
3327
18.6k
    return x0 & 0x3fff;
3328
18.6k
}
3329
3330
53.4k
int dav1d_submit_frame(Dav1dContext *const c) {
3331
53.4k
    Dav1dFrameContext *f;
3332
53.4k
    int res = -1;
3333
3334
    // wait for c->out_delayed[next] and move into c->out if visible
3335
53.4k
    Dav1dThreadPicture *out_delayed;
3336
53.4k
    if (c->n_fc > 1) {
3337
53.4k
        pthread_mutex_lock(&c->task_thread.lock);
3338
53.4k
        const unsigned next = c->frame_thread.next++;
3339
53.4k
        if (c->frame_thread.next == c->n_fc)
3340
279
            c->frame_thread.next = 0;
3341
3342
53.4k
        f = &c->fc[next];
3343
53.7k
        while (f->n_tile_data > 0)
3344
244
            pthread_cond_wait(&f->task_thread.cond,
3345
244
                              &c->task_thread.lock);
3346
53.4k
        out_delayed = &c->frame_thread.out_delayed[next];
3347
53.4k
        if (out_delayed->p.data[0] || atomic_load(&f->task_thread.error)) {
3348
684
            unsigned first = atomic_load(&c->task_thread.first);
3349
684
            if (first + 1U < c->n_fc)
3350
684
                atomic_fetch_add(&c->task_thread.first, 1U);
3351
30
            else
3352
684
                atomic_store(&c->task_thread.first, 0);
3353
684
            atomic_compare_exchange_strong(&c->task_thread.reset_task_cur,
3354
684
                                           &first, UINT_MAX);
3355
684
            if (c->task_thread.cur && c->task_thread.cur < c->n_fc)
3356
264
                c->task_thread.cur--;
3357
684
        }
3358
53.4k
        const int error = f->task_thread.retval;
3359
53.4k
        if (error) {
3360
0
            f->task_thread.retval = 0;
3361
0
            c->cached_error = error;
3362
0
            dav1d_data_props_copy(&c->cached_error_props, &out_delayed->p.m);
3363
0
            dav1d_thread_picture_unref(out_delayed);
3364
53.4k
        } else if (out_delayed->p.data[0]) {
3365
684
            const unsigned progress = atomic_load_explicit(&out_delayed->progress[1],
3366
684
                                                           memory_order_relaxed);
3367
684
            if ((out_delayed->visible || c->output_invisible_frames) &&
3368
675
                progress != FRAME_ERROR)
3369
675
            {
3370
675
                dav1d_thread_picture_ref(&c->out, out_delayed);
3371
675
                c->event_flags |= dav1d_picture_get_event_flags(out_delayed);
3372
675
            }
3373
684
            dav1d_thread_picture_unref(out_delayed);
3374
684
        }
3375
53.4k
    } else {
3376
0
        f = c->fc;
3377
0
    }
3378
3379
53.4k
    f->seq_hdr = c->seq_hdr;
3380
53.4k
    f->seq_hdr_ref = c->seq_hdr_ref;
3381
53.4k
    dav1d_ref_inc(f->seq_hdr_ref);
3382
53.4k
    f->frame_hdr = c->frame_hdr;
3383
53.4k
    f->frame_hdr_ref = c->frame_hdr_ref;
3384
53.4k
    c->frame_hdr = NULL;
3385
53.4k
    c->frame_hdr_ref = NULL;
3386
53.4k
    f->dsp = &c->dsp[f->seq_hdr->hbd];
3387
3388
53.4k
    const int bpc = 8 + 2 * f->seq_hdr->hbd;
3389
3390
53.4k
    if (!f->dsp->ipred.intra_pred[DC_PRED]) {
3391
36.5k
        Dav1dDSPContext *const dsp = &c->dsp[f->seq_hdr->hbd];
3392
3393
36.5k
        switch (bpc) {
3394
0
#define assign_bitdepth_case(bd) \
3395
36.5k
            dav1d_cdef_dsp_init_##bd##bpc(&dsp->cdef); \
3396
36.5k
            dav1d_intra_pred_dsp_init_##bd##bpc(&dsp->ipred); \
3397
36.5k
            dav1d_itx_dsp_init_##bd##bpc(&dsp->itx, bpc); \
3398
36.5k
            dav1d_loop_filter_dsp_init_##bd##bpc(&dsp->lf); \
3399
36.5k
            dav1d_loop_restoration_dsp_init_##bd##bpc(&dsp->lr, bpc); \
3400
36.5k
            dav1d_mc_dsp_init_##bd##bpc(&dsp->mc); \
3401
36.5k
            dav1d_film_grain_dsp_init_##bd##bpc(&dsp->fg); \
3402
36.5k
            break
3403
0
#if CONFIG_8BPC
3404
15.9k
        case 8:
3405
15.9k
            assign_bitdepth_case(8);
3406
0
#endif
3407
0
#if CONFIG_16BPC
3408
17.5k
        case 10:
3409
20.6k
        case 12:
3410
20.6k
            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
36.5k
        }
3419
36.5k
    }
3420
3421
53.4k
#define assign_bitdepth_case(bd) \
3422
53.4k
        f->bd_fn.recon_b_inter = dav1d_recon_b_inter_##bd##bpc; \
3423
53.4k
        f->bd_fn.recon_b_intra = dav1d_recon_b_intra_##bd##bpc; \
3424
53.4k
        f->bd_fn.filter_sbrow = dav1d_filter_sbrow_##bd##bpc; \
3425
53.4k
        f->bd_fn.filter_sbrow_deblock_cols = dav1d_filter_sbrow_deblock_cols_##bd##bpc; \
3426
53.4k
        f->bd_fn.filter_sbrow_deblock_rows = dav1d_filter_sbrow_deblock_rows_##bd##bpc; \
3427
53.4k
        f->bd_fn.filter_sbrow_cdef = dav1d_filter_sbrow_cdef_##bd##bpc; \
3428
53.4k
        f->bd_fn.filter_sbrow_resize = dav1d_filter_sbrow_resize_##bd##bpc; \
3429
53.4k
        f->bd_fn.filter_sbrow_lr = dav1d_filter_sbrow_lr_##bd##bpc; \
3430
53.4k
        f->bd_fn.backup_ipred_edge = dav1d_backup_ipred_edge_##bd##bpc; \
3431
53.4k
        f->bd_fn.read_coef_blocks = dav1d_read_coef_blocks_##bd##bpc; \
3432
53.4k
        f->bd_fn.copy_pal_block_y = dav1d_copy_pal_block_y_##bd##bpc; \
3433
53.4k
        f->bd_fn.copy_pal_block_uv = dav1d_copy_pal_block_uv_##bd##bpc; \
3434
53.4k
        f->bd_fn.read_pal_plane = dav1d_read_pal_plane_##bd##bpc; \
3435
53.4k
        f->bd_fn.read_pal_uv = dav1d_read_pal_uv_##bd##bpc
3436
53.4k
    if (!f->seq_hdr->hbd) {
3437
24.7k
#if CONFIG_8BPC
3438
24.7k
        assign_bitdepth_case(8);
3439
24.7k
#endif
3440
28.7k
    } else {
3441
28.7k
#if CONFIG_16BPC
3442
28.7k
        assign_bitdepth_case(16);
3443
28.7k
#endif
3444
28.7k
    }
3445
53.4k
#undef assign_bitdepth_case
3446
3447
53.4k
    int ref_coded_width[7];
3448
53.4k
    if (IS_INTER_OR_SWITCH(f->frame_hdr)) {
3449
13.4k
        if (f->frame_hdr->primary_ref_frame != DAV1D_PRIMARY_REF_NONE) {
3450
9.16k
            const int pri_ref = f->frame_hdr->refidx[f->frame_hdr->primary_ref_frame];
3451
9.16k
            if (!c->refs[pri_ref].p.p.data[0]) {
3452
3
                res = DAV1D_ERR(EINVAL);
3453
3
                goto error;
3454
3
            }
3455
9.16k
        }
3456
106k
        for (int i = 0; i < 7; i++) {
3457
93.6k
            const int refidx = f->frame_hdr->refidx[i];
3458
93.6k
            if (!c->refs[refidx].p.p.data[0] ||
3459
93.6k
                f->frame_hdr->width[0] * 2 < c->refs[refidx].p.p.p.w ||
3460
93.5k
                f->frame_hdr->height * 2 < c->refs[refidx].p.p.p.h ||
3461
93.5k
                f->frame_hdr->width[0] > c->refs[refidx].p.p.p.w * 16 ||
3462
93.5k
                f->frame_hdr->height > c->refs[refidx].p.p.p.h * 16 ||
3463
93.5k
                f->seq_hdr->layout != c->refs[refidx].p.p.p.layout ||
3464
93.5k
                bpc != c->refs[refidx].p.p.p.bpc)
3465
72
            {
3466
123
                for (int j = 0; j < i; j++)
3467
51
                    dav1d_thread_picture_unref(&f->refp[j]);
3468
72
                res = DAV1D_ERR(EINVAL);
3469
72
                goto error;
3470
72
            }
3471
93.5k
            dav1d_thread_picture_ref(&f->refp[i], &c->refs[refidx].p);
3472
93.5k
            ref_coded_width[i] = c->refs[refidx].p.p.frame_hdr->width[0];
3473
93.5k
            if (f->frame_hdr->width[0] != c->refs[refidx].p.p.p.w ||
3474
54.3k
                f->frame_hdr->height != c->refs[refidx].p.p.p.h)
3475
44.6k
            {
3476
44.6k
#define scale_fac(ref_sz, this_sz) \
3477
107k
    ((((ref_sz) << 14) + ((this_sz) >> 1)) / (this_sz))
3478
44.6k
                f->svc[i][0].scale = scale_fac(c->refs[refidx].p.p.p.w,
3479
44.6k
                                               f->frame_hdr->width[0]);
3480
44.6k
                f->svc[i][1].scale = scale_fac(c->refs[refidx].p.p.p.h,
3481
44.6k
                                               f->frame_hdr->height);
3482
44.6k
                f->svc[i][0].step = (f->svc[i][0].scale + 8) >> 4;
3483
44.6k
                f->svc[i][1].step = (f->svc[i][1].scale + 8) >> 4;
3484
48.9k
            } else {
3485
48.9k
                f->svc[i][0].scale = f->svc[i][1].scale = 0;
3486
48.9k
            }
3487
93.5k
            f->gmv_warp_allowed[i] = f->frame_hdr->gmv[i].type > DAV1D_WM_TYPE_TRANSLATION &&
3488
24.6k
                                     !f->frame_hdr->force_integer_mv &&
3489
22.7k
                                     !dav1d_get_shear_params(&f->frame_hdr->gmv[i]) &&
3490
21.7k
                                     !f->svc[i][0].scale;
3491
93.5k
        }
3492
13.4k
    }
3493
3494
    // setup entropy
3495
53.4k
    if (f->frame_hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
3496
44.2k
        dav1d_cdf_thread_init_static(&f->in_cdf, f->frame_hdr->quant.yac);
3497
44.2k
    } else {
3498
9.13k
        const int pri_ref = f->frame_hdr->refidx[f->frame_hdr->primary_ref_frame];
3499
9.13k
        dav1d_cdf_thread_ref(&f->in_cdf, &c->cdf[pri_ref]);
3500
9.13k
    }
3501
53.4k
    if (f->frame_hdr->refresh_context) {
3502
9.30k
        res = dav1d_cdf_thread_alloc(c, &f->out_cdf, c->n_fc > 1);
3503
9.30k
        if (res < 0) goto error;
3504
9.30k
    }
3505
3506
    // FIXME qsort so tiles are in order (for frame threading)
3507
53.4k
    if (f->n_tile_data_alloc < c->n_tile_data) {
3508
52.7k
        dav1d_free(f->tile);
3509
52.7k
        assert(c->n_tile_data < INT_MAX / (int)sizeof(*f->tile));
3510
52.7k
        f->tile = dav1d_malloc(ALLOC_TILE, c->n_tile_data * sizeof(*f->tile));
3511
52.7k
        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
52.7k
        f->n_tile_data_alloc = c->n_tile_data;
3517
52.7k
    }
3518
53.4k
    memcpy(f->tile, c->tile, c->n_tile_data * sizeof(*f->tile));
3519
53.4k
    memset(c->tile, 0, c->n_tile_data * sizeof(*c->tile));
3520
53.4k
    f->n_tile_data = c->n_tile_data;
3521
53.4k
    c->n_tile_data = 0;
3522
3523
    // allocate frame
3524
53.4k
    res = dav1d_thread_picture_alloc(c, f, bpc);
3525
53.4k
    if (res < 0) goto error;
3526
3527
53.4k
    if (f->frame_hdr->width[0] != f->frame_hdr->width[1]) {
3528
9.30k
        res = dav1d_picture_alloc_copy(c, &f->cur, f->frame_hdr->width[0], &f->sr_cur.p);
3529
9.30k
        if (res < 0) goto error;
3530
44.1k
    } else {
3531
44.1k
        dav1d_picture_ref(&f->cur, &f->sr_cur.p);
3532
44.1k
    }
3533
3534
53.4k
    if (f->frame_hdr->width[0] != f->frame_hdr->width[1]) {
3535
9.30k
        f->resize_step[0] = scale_fac(f->cur.p.w, f->sr_cur.p.p.w);
3536
9.30k
        const int ss_hor = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
3537
9.30k
        const int in_cw = (f->cur.p.w + ss_hor) >> ss_hor;
3538
9.30k
        const int out_cw = (f->sr_cur.p.p.w + ss_hor) >> ss_hor;
3539
9.30k
        f->resize_step[1] = scale_fac(in_cw, out_cw);
3540
9.30k
#undef scale_fac
3541
9.30k
        f->resize_start[0] = get_upscale_x0(f->cur.p.w, f->sr_cur.p.p.w, f->resize_step[0]);
3542
9.30k
        f->resize_start[1] = get_upscale_x0(in_cw, out_cw, f->resize_step[1]);
3543
9.30k
    }
3544
3545
    // move f->cur into output queue
3546
53.4k
    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
53.4k
    } else {
3552
53.4k
        dav1d_thread_picture_ref(out_delayed, &f->sr_cur);
3553
53.4k
    }
3554
3555
53.4k
    f->w4 = (f->frame_hdr->width[0] + 3) >> 2;
3556
53.4k
    f->h4 = (f->frame_hdr->height + 3) >> 2;
3557
53.4k
    f->bw = ((f->frame_hdr->width[0] + 7) >> 3) << 1;
3558
53.4k
    f->bh = ((f->frame_hdr->height + 7) >> 3) << 1;
3559
53.4k
    f->sb128w = (f->bw + 31) >> 5;
3560
53.4k
    f->sb128h = (f->bh + 31) >> 5;
3561
53.4k
    f->sb_shift = 4 + f->seq_hdr->sb128;
3562
53.4k
    f->sb_step = 16 << f->seq_hdr->sb128;
3563
53.4k
    f->sbh = (f->bh + f->sb_step - 1) >> f->sb_shift;
3564
53.4k
    f->b4_stride = (f->bw + 31) & ~31;
3565
53.4k
    f->bitdepth_max = (1 << f->cur.p.bpc) - 1;
3566
53.4k
    atomic_init(&f->task_thread.error, 0);
3567
53.4k
    const int uses_2pass = c->n_fc > 1;
3568
53.4k
    const int cols = f->frame_hdr->tiling.cols;
3569
53.4k
    const int rows = f->frame_hdr->tiling.rows;
3570
53.4k
    atomic_store(&f->task_thread.task_counter,
3571
53.4k
                 (cols * rows + f->sbh) << uses_2pass);
3572
3573
    // ref_mvs
3574
53.4k
    if (IS_INTER_OR_SWITCH(f->frame_hdr) || f->frame_hdr->allow_intrabc) {
3575
19.7k
        f->mvs_ref = dav1d_ref_create_using_pool(c->refmvs_pool,
3576
19.7k
            sizeof(*f->mvs) * f->sb128h * 16 * (f->b4_stride >> 1));
3577
19.7k
        if (!f->mvs_ref) {
3578
0
            res = DAV1D_ERR(ENOMEM);
3579
0
            goto error;
3580
0
        }
3581
19.7k
        f->mvs = f->mvs_ref->data;
3582
19.7k
        if (!f->frame_hdr->allow_intrabc) {
3583
106k
            for (int i = 0; i < 7; i++)
3584
93.5k
                f->refpoc[i] = f->refp[i].p.frame_hdr->frame_offset;
3585
13.3k
        } else {
3586
6.38k
            memset(f->refpoc, 0, sizeof(f->refpoc));
3587
6.38k
        }
3588
19.7k
        if (f->frame_hdr->use_ref_frame_mvs) {
3589
20.9k
            for (int i = 0; i < 7; i++) {
3590
18.3k
                const int refidx = f->frame_hdr->refidx[i];
3591
18.3k
                const int ref_w = ((ref_coded_width[i] + 7) >> 3) << 1;
3592
18.3k
                const int ref_h = ((f->refp[i].p.p.h + 7) >> 3) << 1;
3593
18.3k
                if (c->refs[refidx].refmvs != NULL &&
3594
6.20k
                    ref_w == f->bw && ref_h == f->bh)
3595
5.88k
                {
3596
5.88k
                    f->ref_mvs_ref[i] = c->refs[refidx].refmvs;
3597
5.88k
                    dav1d_ref_inc(f->ref_mvs_ref[i]);
3598
5.88k
                    f->ref_mvs[i] = c->refs[refidx].refmvs->data;
3599
12.4k
                } else {
3600
12.4k
                    f->ref_mvs[i] = NULL;
3601
12.4k
                    f->ref_mvs_ref[i] = NULL;
3602
12.4k
                }
3603
18.3k
                memcpy(f->refrefpoc[i], c->refs[refidx].refpoc,
3604
18.3k
                       sizeof(*f->refrefpoc));
3605
18.3k
            }
3606
17.1k
        } else {
3607
17.1k
            memset(f->ref_mvs_ref, 0, sizeof(f->ref_mvs_ref));
3608
17.1k
        }
3609
33.6k
    } else {
3610
33.6k
        f->mvs_ref = NULL;
3611
33.6k
        memset(f->ref_mvs_ref, 0, sizeof(f->ref_mvs_ref));
3612
33.6k
    }
3613
3614
    // segmap
3615
53.4k
    if (f->frame_hdr->segmentation.enabled) {
3616
        // By default, the previous segmentation map is not initialised.
3617
12.6k
        f->prev_segmap_ref = NULL;
3618
12.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
12.6k
        if (f->frame_hdr->segmentation.temporal || !f->frame_hdr->segmentation.update_map) {
3623
2.85k
            const int pri_ref = f->frame_hdr->primary_ref_frame;
3624
2.85k
            assert(pri_ref != DAV1D_PRIMARY_REF_NONE);
3625
2.85k
            const int ref_w = ((ref_coded_width[pri_ref] + 7) >> 3) << 1;
3626
2.85k
            const int ref_h = ((f->refp[pri_ref].p.p.h + 7) >> 3) << 1;
3627
2.85k
            if (ref_w == f->bw && ref_h == f->bh) {
3628
2.25k
                f->prev_segmap_ref = c->refs[f->frame_hdr->refidx[pri_ref]].segmap;
3629
2.25k
                if (f->prev_segmap_ref) {
3630
732
                    dav1d_ref_inc(f->prev_segmap_ref);
3631
732
                    f->prev_segmap = f->prev_segmap_ref->data;
3632
732
                }
3633
2.25k
            }
3634
2.85k
        }
3635
3636
12.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
10.6k
            f->cur_segmap_ref = dav1d_ref_create_using_pool(c->segmap_pool,
3641
10.6k
                sizeof(*f->cur_segmap) * f->b4_stride * 32 * f->sb128h);
3642
10.6k
            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
10.6k
            f->cur_segmap = f->cur_segmap_ref->data;
3648
10.6k
        } else if (f->prev_segmap_ref) {
3649
            // We're not updating an existing map, and we have a valid
3650
            // reference. Use that.
3651
423
            f->cur_segmap_ref = f->prev_segmap_ref;
3652
423
            dav1d_ref_inc(f->cur_segmap_ref);
3653
423
            f->cur_segmap = f->prev_segmap_ref->data;
3654
1.56k
        } else {
3655
            // We need to make a new map. Allocate one here and zero it out.
3656
1.56k
            const size_t segmap_size = sizeof(*f->cur_segmap) * f->b4_stride * 32 * f->sb128h;
3657
1.56k
            f->cur_segmap_ref = dav1d_ref_create_using_pool(c->segmap_pool, segmap_size);
3658
1.56k
            if (!f->cur_segmap_ref) {
3659
0
                res = DAV1D_ERR(ENOMEM);
3660
0
                goto error;
3661
0
            }
3662
1.56k
            f->cur_segmap = f->cur_segmap_ref->data;
3663
1.56k
            memset(f->cur_segmap, 0, segmap_size);
3664
1.56k
        }
3665
40.7k
    } else {
3666
40.7k
        f->cur_segmap = NULL;
3667
40.7k
        f->cur_segmap_ref = NULL;
3668
40.7k
        f->prev_segmap_ref = NULL;
3669
40.7k
    }
3670
3671
    // update references etc.
3672
53.4k
    const unsigned refresh_frame_flags = f->frame_hdr->refresh_frame_flags;
3673
480k
    for (int i = 0; i < 8; i++) {
3674
427k
        if (refresh_frame_flags & (1 << i)) {
3675
379k
            if (c->refs[i].p.p.frame_hdr)
3676
86.9k
                dav1d_thread_picture_unref(&c->refs[i].p);
3677
379k
            dav1d_thread_picture_ref(&c->refs[i].p, &f->sr_cur);
3678
3679
379k
            dav1d_cdf_thread_unref(&c->cdf[i]);
3680
379k
            if (f->frame_hdr->refresh_context) {
3681
49.6k
                dav1d_cdf_thread_ref(&c->cdf[i], &f->out_cdf);
3682
329k
            } else {
3683
329k
                dav1d_cdf_thread_ref(&c->cdf[i], &f->in_cdf);
3684
329k
            }
3685
3686
379k
            dav1d_ref_dec(&c->refs[i].segmap);
3687
379k
            c->refs[i].segmap = f->cur_segmap_ref;
3688
379k
            if (f->cur_segmap_ref)
3689
83.6k
                dav1d_ref_inc(f->cur_segmap_ref);
3690
379k
            dav1d_ref_dec(&c->refs[i].refmvs);
3691
379k
            if (!f->frame_hdr->allow_intrabc) {
3692
328k
                c->refs[i].refmvs = f->mvs_ref;
3693
328k
                if (f->mvs_ref)
3694
63.0k
                    dav1d_ref_inc(f->mvs_ref);
3695
328k
            }
3696
379k
            memcpy(c->refs[i].refpoc, f->refpoc, sizeof(f->refpoc));
3697
379k
        }
3698
427k
    }
3699
3700
53.4k
    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
53.4k
    } else {
3715
53.4k
        dav1d_task_frame_init(f);
3716
53.4k
        pthread_mutex_unlock(&c->task_thread.lock);
3717
53.4k
    }
3718
3719
53.4k
    return 0;
3720
75
error:
3721
75
    atomic_init(&f->task_thread.error, 1);
3722
75
    dav1d_cdf_thread_unref(&f->in_cdf);
3723
75
    if (f->frame_hdr->refresh_context)
3724
39
        dav1d_cdf_thread_unref(&f->out_cdf);
3725
600
    for (int i = 0; i < 7; i++) {
3726
525
        if (f->refp[i].p.frame_hdr)
3727
0
            dav1d_thread_picture_unref(&f->refp[i]);
3728
525
        dav1d_ref_dec(&f->ref_mvs_ref[i]);
3729
525
    }
3730
75
    if (c->n_fc == 1)
3731
0
        dav1d_thread_picture_unref(&c->out);
3732
75
    else
3733
75
        dav1d_thread_picture_unref(out_delayed);
3734
75
    dav1d_picture_unref_internal(&f->cur);
3735
75
    dav1d_thread_picture_unref(&f->sr_cur);
3736
75
    dav1d_ref_dec(&f->mvs_ref);
3737
75
    dav1d_ref_dec(&f->seq_hdr_ref);
3738
75
    dav1d_ref_dec(&f->frame_hdr_ref);
3739
75
    dav1d_data_props_copy(&c->cached_error_props, &c->in.m);
3740
3741
75
    for (int i = 0; i < f->n_tile_data; i++)
3742
0
        dav1d_data_unref_internal(&f->tile[i].data);
3743
75
    f->n_tile_data = 0;
3744
3745
75
    if (c->n_fc > 1)
3746
75
        pthread_mutex_unlock(&c->task_thread.lock);
3747
3748
75
    return res;
3749
53.4k
}