Coverage Report

Created: 2026-10-17 06:56

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