decode.c 125 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright © 2018, VideoLAN and dav1d authors
 * Copyright © 2018, Two Orioles, LLC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#include <errno.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <inttypes.h>

#include "dav1d/data.h"

#include "common/intops.h"
#include "common/mem.h"

#include "src/decode.h"
#include "src/dequant_tables.h"
#include "src/env.h"
#include "src/qm.h"
#include "src/recon.h"
#include "src/ref.h"
#include "src/tables.h"
#include "src/thread_task.h"
#include "src/warpmv.h"

static void init_quant_tables(const Av1SequenceHeader *const seq_hdr,
                              const Av1FrameHeader *const frame_hdr,
                              const int qidx, uint16_t (*dq)[3][2])
{
    for (int i = 0; i < (frame_hdr->segmentation.enabled ? 8 : 1); i++) {
        const int yac = frame_hdr->segmentation.enabled ?
            iclip_u8(qidx + frame_hdr->segmentation.seg_data.d[i].delta_q) : qidx;
        const int ydc = iclip_u8(yac + frame_hdr->quant.ydc_delta);
        const int uac = iclip_u8(yac + frame_hdr->quant.uac_delta);
        const int udc = iclip_u8(yac + frame_hdr->quant.udc_delta);
        const int vac = iclip_u8(yac + frame_hdr->quant.vac_delta);
        const int vdc = iclip_u8(yac + frame_hdr->quant.vdc_delta);

        dq[i][0][0] = dav1d_dq_tbl[seq_hdr->bpc > 8][ydc][0];
        dq[i][0][1] = dav1d_dq_tbl[seq_hdr->bpc > 8][yac][1];
        dq[i][1][0] = dav1d_dq_tbl[seq_hdr->bpc > 8][udc][0];
        dq[i][1][1] = dav1d_dq_tbl[seq_hdr->bpc > 8][uac][1];
        dq[i][2][0] = dav1d_dq_tbl[seq_hdr->bpc > 8][vdc][0];
        dq[i][2][1] = dav1d_dq_tbl[seq_hdr->bpc > 8][vac][1];
    }
}

static int read_mv_component_diff(Dav1dTileContext *const t,
                                  CdfMvComponent *const mv_comp,
                                  const int have_fp)
{
    Dav1dTileState *const ts = t->ts;
    const Dav1dFrameContext *const f = t->f;
    const int have_hp = f->frame_hdr.hp;
    const int sign = msac_decode_bool_adapt(&ts->msac, mv_comp->sign);
    const int cl = msac_decode_symbol_adapt(&ts->msac, mv_comp->classes, 11);
    int up, fp, hp;

    if (!cl) {
        up = msac_decode_bool_adapt(&ts->msac, mv_comp->class0);
        if (have_fp) {
            fp = msac_decode_symbol_adapt(&ts->msac, mv_comp->class0_fp[up], 4);
            hp = have_hp ? msac_decode_bool_adapt(&ts->msac, mv_comp->class0_hp) : 1;
        } else {
            fp = 3;
            hp = 1;
        }
    } else {
        up = 1 << cl;
        for (int n = 0; n < cl; n++)
            up |= msac_decode_bool_adapt(&ts->msac, mv_comp->classN[n]) << n;
        if (have_fp) {
            fp = msac_decode_symbol_adapt(&ts->msac, mv_comp->classN_fp, 4);
            hp = have_hp ? msac_decode_bool_adapt(&ts->msac, mv_comp->classN_hp) : 1;
        } else {
            fp = 3;
            hp = 1;
        }
    }

    const int diff = ((up << 3) | (fp << 1) | hp) + 1;

    return sign ? -diff : diff;
}

static void read_mv_residual(Dav1dTileContext *const t, mv *const ref_mv,
                             CdfMvContext *const mv_cdf, const int have_fp)
{
    switch (msac_decode_symbol_adapt(&t->ts->msac, t->ts->cdf.mv.joint, N_MV_JOINTS)) {
    case MV_JOINT_HV:
        ref_mv->y += read_mv_component_diff(t, &mv_cdf->comp[0], have_fp);
        ref_mv->x += read_mv_component_diff(t, &mv_cdf->comp[1], have_fp);
        break;
    case MV_JOINT_H:
        ref_mv->x += read_mv_component_diff(t, &mv_cdf->comp[1], have_fp);
        break;
    case MV_JOINT_V:
        ref_mv->y += read_mv_component_diff(t, &mv_cdf->comp[0], have_fp);
        break;
    default:
        break;
    }
}

static void read_tx_tree(Dav1dTileContext *const t,
                         const enum RectTxfmSize from,
                         const int depth, uint16_t *const masks,
                         const int x_off, const int y_off)
{
    const Dav1dFrameContext *const f = t->f;
    const int bx4 = t->bx & 31, by4 = t->by & 31;
137
    const TxfmInfo *const t_dim = &dav1d_txfm_dimensions[from];
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
    const int txw = t_dim->lw, txh = t_dim->lh;
    int is_split;

    if (depth < 2 && from > (int) TX_4X4) {
        const int cat = 2 * (TX_64X64 - t_dim->max) - depth;
        const int a = t->a->tx[bx4] < txw;
        const int l = t->l.tx[by4] < txh;

        is_split = msac_decode_bool_adapt(&t->ts->msac, t->ts->cdf.m.txpart[cat][a + l]);
        if (is_split)
            masks[depth] |= 1 << (y_off * 4 + x_off);
    } else {
        is_split = 0;
    }

    if (is_split && t_dim->max > TX_8X8) {
        const enum RectTxfmSize sub = t_dim->sub;
155
        const TxfmInfo *const sub_t_dim = &dav1d_txfm_dimensions[sub];
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
        const int txsw = sub_t_dim->w, txsh = sub_t_dim->h;

        read_tx_tree(t, sub, depth + 1, masks, x_off * 2 + 0, y_off * 2 + 0);
        t->bx += txsw;
        if (txw >= txh && t->bx < f->bw)
            read_tx_tree(t, sub, depth + 1, masks, x_off * 2 + 1, y_off * 2 + 0);
        t->bx -= txsw;
        t->by += txsh;
        if (txh >= txw && t->by < f->bh) {
            read_tx_tree(t, sub, depth + 1, masks, x_off * 2 + 0, y_off * 2 + 1);
            t->bx += txsw;
            if (txw >= txh && t->bx < f->bw)
                read_tx_tree(t, sub, depth + 1, masks,
                             x_off * 2 + 1, y_off * 2 + 1);
            t->bx -= txsw;
        }
        t->by -= txsh;
    } else {
        memset(&t->a->tx[bx4], is_split ? TX_4X4 : txw, t_dim->w);
        memset(&t->l.tx[by4], is_split ? TX_4X4 : txh, t_dim->h);
    }
}

179
static int neg_deinterleave(int diff, int ref, int max) {
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
    if (!ref) return diff;
    if (ref >= (max - 1)) return max - diff - 1;
    if (2 * ref < max) {
        if (diff <= 2 * ref) {
            if (diff & 1)
                return ref + ((diff + 1) >> 1);
            else
                return ref - (diff >> 1);
        }
        return diff;
    } else {
        if (diff <= 2 * (max - ref - 1)) {
            if (diff & 1)
                return ref + ((diff + 1) >> 1);
            else
                return ref - (diff >> 1);
        }
        return max - (diff + 1);
    }
}

static void find_matching_ref(const Dav1dTileContext *const t,
                              const enum EdgeFlags intra_edge_flags,
                              const int bw4, const int bh4,
                              const int w4, const int h4,
                              const int have_left, const int have_top,
                              const int ref, uint64_t masks[2])
{
    const Dav1dFrameContext *const f = t->f;
    const ptrdiff_t b4_stride = f->b4_stride;
    const refmvs *const r = &f->mvs[t->by * b4_stride + t->bx];
    int count = 0;
    int have_topleft = have_top && have_left;
    int have_topright = imax(bw4, bh4) < 32 &&
                        have_top && t->bx + bw4 < t->ts->tiling.col_end &&
                        (intra_edge_flags & EDGE_I444_TOP_HAS_RIGHT);

217
#define bs(rp) dav1d_block_dimensions[sbtype_to_bs[(rp)->sb_type]]
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#define matches(rp) ((rp)->ref[0] == ref + 1 && (rp)->ref[1] == -1)

    if (have_top) {
        const refmvs *r2 = &r[-b4_stride];
        if (matches(r2)) {
            masks[0] |= 1;
            count = 1;
        }
        int aw4 = bs(r2)[0];
        if (aw4 >= bw4) {
            const int off = t->bx & (aw4 - 1);
            if (off) have_topleft = 0;
            if (aw4 - off > bw4) have_topright = 0;
        } else {
            unsigned mask = 1 << aw4;
            for (int x = aw4; x < w4; x += aw4) {
                r2 += aw4;
                if (matches(r2)) {
                    masks[0] |= mask;
                    if (++count >= 8) return;
                }
                aw4 = bs(r2)[0];
                mask <<= aw4;
            }
        }
    }
    if (have_left) {
        const refmvs *r2 = &r[-1];
        if (matches(r2)) {
            masks[1] |= 1;
            if (++count >= 8) return;
        }
        int lh4 = bs(r2)[1];
        if (lh4 >= bh4) {
            if (t->by & (lh4 - 1)) have_topleft = 0;
        } else {
            unsigned mask = 1 << lh4;
            for (int y = lh4; y < h4; y += lh4) {
                r2 += lh4 * b4_stride;
                if (matches(r2)) {
                    masks[1] |= mask;
                    if (++count >= 8) return;
                }
                lh4 = bs(r2)[1];
                mask <<= lh4;
            }
        }
    }
    if (have_topleft && matches(&r[-(1 + b4_stride)])) {
        masks[1] |= 1ULL << 32;
        if (++count >= 8) return;
    }
    if (have_topright && matches(&r[bw4 - b4_stride])) {
        masks[0] |= 1ULL << 32;
    }
#undef matches
}

static void derive_warpmv(const Dav1dTileContext *const t,
                          const int bw4, const int bh4,
                          const uint64_t masks[2], const struct mv mv,
                          WarpedMotionParams *const wmp)
{
    int pts[8][2 /* in, out */][2 /* x, y */], np = 0;
    const Dav1dFrameContext *const f = t->f;
    const ptrdiff_t b4_stride = f->b4_stride;
    const refmvs *const r = &f->mvs[t->by * b4_stride + t->bx];

#define add_sample(dx, dy, sx, sy, rp) do { \
    pts[np][0][0] = 16 * (2 * dx + sx * bs(rp)[0]) - 8; \
    pts[np][0][1] = 16 * (2 * dy + sy * bs(rp)[1]) - 8; \
    pts[np][1][0] = pts[np][0][0] + (rp)->mv[0].x; \
    pts[np][1][1] = pts[np][0][1] + (rp)->mv[0].y; \
    np++; \
} while (0)

    // use masks[] to find the projectable motion vectors in the edges
    if ((unsigned) masks[0] == 1 && !(masks[1] >> 32)) {
        const int off = t->bx & (bs(&r[-b4_stride])[0] - 1);
        add_sample(-off, 0, 1, -1, &r[-b4_stride]);
    } else for (unsigned off = 0, xmask = masks[0]; np < 8 && xmask;) { // top
299
        const int tz = ctz(xmask);
300
301
302
303
304
305
306
307
308
        off += tz;
        add_sample(off, 0, 1, -1, &r[off - b4_stride]);
        xmask >>= tz + 1;
        off += 1;
    }
    if (np < 8 && masks[1] == 1) {
        const int off = t->by & (bs(&r[-1])[1] - 1);
        add_sample(0, -off, -1, 1, &r[-1 - off * b4_stride]);
    } else for (unsigned off = 0, ymask = masks[1]; np < 8 && ymask;) { // left
309
        const int tz = ctz(ymask);
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
        off += tz;
        add_sample(0, off, -1, 1, &r[off * b4_stride - 1]);
        ymask >>= tz + 1;
        off += 1;
    }
    if (np < 8 && masks[1] >> 32) // top/left
        add_sample(0, 0, -1, -1, &r[-(1 + b4_stride)]);
    if (np < 8 && masks[0] >> 32) // top/right
        add_sample(bw4, 0, 1, -1, &r[bw4 - b4_stride]);
    assert(np > 0 && np <= 8);
#undef bs

    // select according to motion vector difference against a threshold
    int mvd[8], ret = 0;
    const int thresh = 4 * iclip(imax(bw4, bh4), 4, 28);
    for (int i = 0; i < np; i++) {
        mvd[i] = labs(pts[i][1][0] - pts[i][0][0] - mv.x) +
                 labs(pts[i][1][1] - pts[i][0][1] - mv.y);
        if (mvd[i] > thresh)
            mvd[i] = -1;
        else
            ret++;
    }
    if (!ret) {
        ret = 1;
    } else for (int i = 0, j = np - 1, k = 0; k < np - ret; k++, i++, j--) {
        while (mvd[i] != -1) i++;
        while (mvd[j] == -1) j--;
        assert(i != j);
        if (i > j) break;
        // replace the discarded samples;
        mvd[i] = mvd[j];
        memcpy(pts[i], pts[j], sizeof(*pts));
    }

345
346
    if (!dav1d_find_affine_int(pts, ret, bw4, bh4, mv, wmp, t->bx, t->by) &&
        !dav1d_get_shear_params(wmp))
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
    {
        wmp->type = WM_TYPE_AFFINE;
    } else
        wmp->type = WM_TYPE_IDENTITY;
}

static inline int findoddzero(const uint8_t *buf, int len) {
    for (int n = 0; n < len; n++)
        if (!buf[n * 2]) return 1;
    return 0;
}

static void read_pal_plane(Dav1dTileContext *const t, Av1Block *const b,
                           const int pl, const int sz_ctx,
                           const int bx4, const int by4)
{
    Dav1dTileState *const ts = t->ts;
    const Dav1dFrameContext *const f = t->f;
    const int pal_sz = b->pal_sz[pl] = 2 + msac_decode_symbol_adapt(&ts->msac,
                                                 ts->cdf.m.pal_sz[pl][sz_ctx], 7);
    uint16_t cache[16], used_cache[8];
    int l_cache = pl ? t->pal_sz_uv[1][by4] : t->l.pal_sz[by4];
    int n_cache = 0;
    // don't reuse above palette outside SB64 boundaries
    int a_cache = by4 & 15 ? pl ? t->pal_sz_uv[0][bx4] : t->a->pal_sz[bx4] : 0;
    const uint16_t *l = t->al_pal[1][by4][pl], *a = t->al_pal[0][bx4][pl];

    // fill/sort cache
    while (l_cache && a_cache) {
        if (*l < *a) {
            if (!n_cache || cache[n_cache - 1] != *l)
                cache[n_cache++] = *l;
            l++;
            l_cache--;
        } else {
            if (*a == *l) {
                l++;
                l_cache--;
            }
            if (!n_cache || cache[n_cache - 1] != *a)
                cache[n_cache++] = *a;
            a++;
            a_cache--;
        }
    }
    if (l_cache) {
        do {
            if (!n_cache || cache[n_cache - 1] != *l)
                cache[n_cache++] = *l;
            l++;
        } while (--l_cache > 0);
    } else if (a_cache) {
        do {
            if (!n_cache || cache[n_cache - 1] != *a)
                cache[n_cache++] = *a;
            a++;
        } while (--a_cache > 0);
    }

    // find reused cache entries
    int i = 0;
    for (int n = 0; n < n_cache && i < pal_sz; n++)
        if (msac_decode_bool(&ts->msac, 128 << 7))
            used_cache[i++] = cache[n];
    const int n_used_cache = i;

    // parse new entries
    uint16_t *const pal = f->frame_thread.pass ?
        f->frame_thread.pal[((t->by >> 1) + (t->bx & 1)) * (f->b4_stride >> 1) +
                            ((t->bx >> 1) + (t->by & 1))][pl] : t->pal[pl];
    if (i < pal_sz) {
        int prev = pal[i++] = msac_decode_bools(&ts->msac, f->cur.p.p.bpc);

        if (i < pal_sz) {
            int bits = f->cur.p.p.bpc - 3 + msac_decode_bools(&ts->msac, 2);
            const int max = (1 << f->cur.p.p.bpc) - 1;

            do {
                const int delta = msac_decode_bools(&ts->msac, bits);
                prev = pal[i++] = imin(prev + delta + !pl, max);
                if (prev + !pl >= max) {
                    for (; i < pal_sz; i++)
                        pal[i] = pal[i - 1];
                    break;
                }
                bits = imin(bits, 1 + ulog2(max - prev - !pl));
            } while (i < pal_sz);
        }

        // merge cache+new entries
        int n = 0, m = n_used_cache;
        for (i = 0; i < pal_sz; i++) {
            if (n < n_used_cache && (m >= pal_sz || used_cache[n] <= pal[m])) {
                pal[i] = used_cache[n++];
            } else {
                assert(m < pal_sz);
                pal[i] = pal[m++];
            }
        }
    } else {
        memcpy(pal, used_cache, n_used_cache * sizeof(*used_cache));
    }

    if (DEBUG_BLOCK_INFO) {
        printf("Post-pal[pl=%d,sz=%d,cache_size=%d,used_cache=%d]: r=%d, cache=",
452
               pl, pal_sz, n_cache, n_used_cache, ts->msac.rng);
453
454
455
        for (int n = 0; n < n_cache; n++)
            printf("%c%02x", n ? ' ' : '[', cache[n]);
        printf("%s, pal=", n_cache ? "]" : "[]");
456
        for (int n = 0; n < pal_sz; n++)
457
458
459
460
461
462
            printf("%c%02x", n ? ' ' : '[', pal[n]);
        printf("]\n");
    }
}

static void read_pal_uv(Dav1dTileContext *const t, Av1Block *const b,
463
                        const int sz_ctx, const int bx4, const int by4)
464
{
465
    read_pal_plane(t, b, 1, sz_ctx, bx4, by4);
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515

    // V pal coding
    Dav1dTileState *const ts = t->ts;
    const Dav1dFrameContext *const f = t->f;
    uint16_t *const pal = f->frame_thread.pass ?
        f->frame_thread.pal[((t->by >> 1) + (t->bx & 1)) * (f->b4_stride >> 1) +
                            ((t->bx >> 1) + (t->by & 1))][2] : t->pal[2];
    if (msac_decode_bool(&ts->msac, 128 << 7)) {
        const int bits = f->cur.p.p.bpc - 4 + msac_decode_bools(&ts->msac, 2);
        int prev = pal[0] = msac_decode_bools(&ts->msac, f->cur.p.p.bpc);
        const int max = (1 << f->cur.p.p.bpc) - 1;
        for (int i = 1; i < b->pal_sz[1]; i++) {
            int delta = msac_decode_bools(&ts->msac, bits);
            if (delta && msac_decode_bool(&ts->msac, 128 << 7)) delta = -delta;
            prev = pal[i] = (prev + delta) & max;
        }
    } else {
        for (int i = 0; i < b->pal_sz[1]; i++)
            pal[i] = msac_decode_bools(&ts->msac, f->cur.p.p.bpc);
    }
    if (DEBUG_BLOCK_INFO) {
        printf("Post-pal[pl=2]: r=%d ", ts->msac.rng);
        for (int n = 0; n < b->pal_sz[1]; n++)
            printf("%c%02x", n ? ' ' : '[', pal[n]);
        printf("]\n");
    }
}

// meant to be SIMD'able, so that theoretical complexity of this function
// times block size goes from w4*h4 to w4+h4-1
// a and b are previous two lines containing (a) top/left entries or (b)
// top/left entries, with a[0] being either the first top or first left entry,
// depending on top_offset being 1 or 0, and b being the first top/left entry
// for whichever has one. left_offset indicates whether the (len-1)th entry
// has a left neighbour.
// output is order[] and ctx for each member of this diagonal.
static void order_palette(const uint8_t *pal_idx, const ptrdiff_t stride,
                          const int i, const int first, const int last,
                          uint8_t (*const order)[8], uint8_t *const ctx)
{
    int have_top = i > first;

    pal_idx += first + (i - first) * stride;
    for (int j = first, n = 0; j >= last; have_top = 1, j--, n++, pal_idx += stride - 1) {
        const int have_left = j > 0;

        assert(have_left || have_top);

#define add(v_in) do { \
        const int v = v_in; \
516
        assert((unsigned)v < 8U); \
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
        order[n][o_idx++] = v; \
        mask |= 1 << v; \
    } while (0)

        unsigned mask = 0;
        int o_idx = 0;
        if (!have_left) {
            ctx[n] = 0;
            add(pal_idx[-stride]);
        } else if (!have_top) {
            ctx[n] = 0;
            add(pal_idx[-1]);
        } else {
            const int l = pal_idx[-1], t = pal_idx[-stride], tl = pal_idx[-(stride + 1)];
            const int same_t_l = t == l;
            const int same_t_tl = t == tl;
            const int same_l_tl = l == tl;
            const int same_all = same_t_l & same_t_tl & same_l_tl;

            if (same_all) {
                ctx[n] = 4;
                add(t);
            } else if (same_t_l) {
                ctx[n] = 3;
                add(t);
                add(tl);
            } else if (same_t_tl | same_l_tl) {
                ctx[n] = 2;
                add(tl);
                add(same_t_tl ? l : t);
            } else {
                ctx[n] = 1;
                add(imin(t, l));
                add(imax(t, l));
                add(tl);
            }
        }
        for (unsigned m = 1, bit = 0; m < 0x100; m <<= 1, bit++)
            if (!(mask & m))
                order[n][o_idx++] = bit;
        assert(o_idx == 8);
#undef add
    }
}

static void read_pal_indices(Dav1dTileContext *const t,
                             uint8_t *const pal_idx,
                             const Av1Block *const b, const int pl,
                             const int w4, const int h4,
                             const int bw4, const int bh4)
{
    Dav1dTileState *const ts = t->ts;
    const ptrdiff_t stride = bw4 * 4;
    pal_idx[0] = msac_decode_uniform(&ts->msac, b->pal_sz[pl]);
    uint16_t (*const color_map_cdf)[8 + 1] =
        ts->cdf.m.color_map[pl][b->pal_sz[pl] - 2];
    for (int i = 1; i < 4 * (w4 + h4) - 1; i++) {
        // top/left-to-bottom/right diagonals ("wave-front")
        uint8_t order[64][8], ctx[64];
        const int first = imin(i, w4 * 4 - 1);
        const int last = imax(0, i - h4 * 4 + 1);
        order_palette(pal_idx, stride, i, first, last, order, ctx);
        for (int j = first, m = 0; j >= last; j--, m++) {
            const int color_idx =
                msac_decode_symbol_adapt(&ts->msac, color_map_cdf[ctx[m]],
                                         b->pal_sz[pl]);
            pal_idx[(i - j) * stride + j] = order[m][color_idx];
        }
    }
    // fill invisible edges
    if (bw4 > w4)
        for (int y = 0; y < 4 * h4; y++)
            memset(&pal_idx[y * stride + 4 * w4],
                   pal_idx[y * stride + 4 * w4 - 1], 4 * (bw4 - w4));
    if (h4 < bh4) {
        const uint8_t *const src = &pal_idx[stride * (4 * h4 - 1)];
        for (int y = h4 * 4; y < bh4 * 4; y++)
            memcpy(&pal_idx[y * stride], src, bw4 * 4);
    }
}

static void read_vartx_tree(Dav1dTileContext *const t,
                            Av1Block *const b, const enum BlockSize bs,
                            const int bx4, const int by4)
{
    const Dav1dFrameContext *const f = t->f;
603
    const uint8_t *const b_dim = dav1d_block_dimensions[bs];
604
605
606
607
    const int bw4 = b_dim[0], bh4 = b_dim[1];

    // var-tx tree coding
    b->tx_split[0] = b->tx_split[1] = 0;
608
    b->max_ytx = dav1d_max_txfm_size_for_bs[bs][0];
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
    if (f->frame_hdr.segmentation.lossless[b->seg_id] ||
        b->max_ytx == TX_4X4)
    {
        b->max_ytx = b->uvtx = TX_4X4;
        if (f->frame_hdr.txfm_mode == TX_SWITCHABLE) {
            memset(&t->a->tx[bx4], TX_4X4, bw4);
            memset(&t->l.tx[by4], TX_4X4, bh4);
        }
    } else if (f->frame_hdr.txfm_mode != TX_SWITCHABLE || b->skip) {
        if (f->frame_hdr.txfm_mode == TX_SWITCHABLE) {
            memset(&t->a->tx[bx4], b_dim[2], bw4);
            memset(&t->l.tx[by4], b_dim[3], bh4);
        } else {
            assert(f->frame_hdr.txfm_mode == TX_LARGEST);
        }
624
        b->uvtx = dav1d_max_txfm_size_for_bs[bs][f->cur.p.p.layout];
625
626
627
    } else {
        assert(imin(bw4, bh4) <= 16 || b->max_ytx == TX_64X64);
        int y, x, y_off, x_off;
628
        const TxfmInfo *const ytx = &dav1d_txfm_dimensions[b->max_ytx];
629
630
631
632
633
634
635
636
637
638
639
640
641
        for (y = 0, y_off = 0; y < bh4; y += ytx->h, y_off++) {
            for (x = 0, x_off = 0; x < bw4; x += ytx->w, x_off++) {
                read_tx_tree(t, b->max_ytx, 0, b->tx_split, x_off, y_off);
                // contexts are updated inside read_tx_tree()
                t->bx += ytx->w;
            }
            t->bx -= x;
            t->by += ytx->h;
        }
        t->by -= y;
        if (DEBUG_BLOCK_INFO)
            printf("Post-vartxtree[%x/%x]: r=%d\n",
                   b->tx_split[0], b->tx_split[1], t->ts->msac.rng);
642
        b->uvtx = dav1d_max_txfm_size_for_bs[bs][f->cur.p.p.layout];
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
    }
}

static inline unsigned get_prev_frame_segid(const Dav1dFrameContext *const f,
                                            const int by, const int bx,
                                            const int w4, int h4,
                                            const uint8_t *ref_seg_map,
                                            const ptrdiff_t stride)
{
    unsigned seg_id = 8;

    assert(f->frame_hdr.primary_ref_frame != PRIMARY_REF_NONE);
    dav1d_thread_picture_wait(&f->refp[f->frame_hdr.primary_ref_frame],
                              (by + h4) * 4, PLANE_TYPE_BLOCK);

    ref_seg_map += by * stride + bx;
    do {
        for (int x = 0; x < w4; x++)
            seg_id = imin(seg_id, ref_seg_map[x]);
        ref_seg_map += stride;
    } while (--h4 > 0);
    assert(seg_id < 8);

    return seg_id;
}

static void decode_b(Dav1dTileContext *const t,
                     const enum BlockLevel bl,
                     const enum BlockSize bs,
                     const enum BlockPartition bp,
                     const enum EdgeFlags intra_edge_flags)
{
    Dav1dTileState *const ts = t->ts;
    const Dav1dFrameContext *const f = t->f;
    Av1Block b_mem, *const b = f->frame_thread.pass ?
        &f->frame_thread.b[t->by * f->b4_stride + t->bx] : &b_mem;
679
    const uint8_t *const b_dim = dav1d_block_dimensions[bs];
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
    const int bx4 = t->bx & 31, by4 = t->by & 31;
    const int ss_ver = f->cur.p.p.layout == DAV1D_PIXEL_LAYOUT_I420;
    const int ss_hor = f->cur.p.p.layout != DAV1D_PIXEL_LAYOUT_I444;
    const int cbx4 = bx4 >> ss_hor, cby4 = by4 >> ss_ver;
    const int bw4 = b_dim[0], bh4 = b_dim[1];
    const int w4 = imin(bw4, f->bw - t->bx), h4 = imin(bh4, f->bh - t->by);
    const int cbw4 = (bw4 + ss_hor) >> ss_hor, cbh4 = (bh4 + ss_ver) >> ss_ver;
    const int have_left = t->bx > ts->tiling.col_start;
    const int have_top = t->by > ts->tiling.row_start;
    const int has_chroma = f->seq_hdr.layout != DAV1D_PIXEL_LAYOUT_I400 &&
                           (bw4 > ss_hor || t->bx & 1) &&
                           (bh4 > ss_ver || t->by & 1);

    if (f->frame_thread.pass == 2) {
        if (b->intra) {
            f->bd_fn.recon_b_intra(t, bs, intra_edge_flags, b);

            if (has_chroma) {
                memset(&t->l.uvmode[cby4], b->uv_mode, cbh4);
                memset(&t->a->uvmode[cbx4], b->uv_mode, cbw4);
            }
            const enum IntraPredMode y_mode_nofilt =
                b->y_mode == FILTER_PRED ? DC_PRED : b->y_mode;
            memset(&t->l.mode[by4], y_mode_nofilt, bh4);
            memset(&t->a->mode[bx4], y_mode_nofilt, bw4);
        } else {
            if (b->comp_type == COMP_INTER_NONE && b->motion_mode == MM_WARP) {
                uint64_t mask[2] = { 0, 0 };
                find_matching_ref(t, intra_edge_flags, bw4, bh4, w4, h4,
                                  have_left, have_top, b->ref[0], mask);
                derive_warpmv(t, bw4, bh4, mask, b->mv[0], &t->warpmv);
            }
            f->bd_fn.recon_b_inter(t, bs, b);

714
            const uint8_t *const filter = dav1d_filter_dir[b->filter2d];
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
            memset(&t->l.filter[0][by4], filter[0], bh4);
            memset(&t->a->filter[0][bx4], filter[0], bw4);
            memset(&t->l.filter[1][by4], filter[1], bh4);
            memset(&t->a->filter[1][bx4], filter[1], bw4);
            if (has_chroma) {
                memset(&t->l.uvmode[cby4], DC_PRED, cbh4);
                memset(&t->a->uvmode[cbx4], DC_PRED, cbw4);
            }
        }
        memset(&t->l.intra[by4], b->intra, bh4);
        memset(&t->a->intra[bx4], b->intra, bw4);
        return;
    }

    const int cw4 = (w4 + ss_hor) >> ss_hor, ch4 = (h4 + ss_ver) >> ss_ver;

    b->bl = bl;
    b->bp = bp;
    b->bs = bs;

    // skip_mode
    if (f->frame_hdr.skip_mode_enabled && imin(bw4, bh4) > 1) {
        const int smctx = t->a->skip_mode[bx4] + t->l.skip_mode[by4];
        b->skip_mode = msac_decode_bool_adapt(&ts->msac,
                                              ts->cdf.m.skip_mode[smctx]);
        if (DEBUG_BLOCK_INFO)
            printf("Post-skipmode[%d]: r=%d\n", b->skip_mode, ts->msac.rng);
    } else {
        b->skip_mode = 0;
    }

    // segment_id (if seg_feature for skip/ref/gmv is enabled)
    int seg_pred = 0;
    if (f->frame_hdr.segmentation.enabled) {
        if (!f->frame_hdr.segmentation.update_map) {
            b->seg_id = f->prev_segmap ?
                        get_prev_frame_segid(f, t->by, t->bx, w4, h4,
                                             f->prev_segmap, f->b4_stride) : 0;
        } else if (f->frame_hdr.segmentation.seg_data.preskip) {
            if (f->frame_hdr.segmentation.temporal &&
                (seg_pred = msac_decode_bool_adapt(&ts->msac,
                                       ts->cdf.m.seg_pred[t->a->seg_pred[bx4] +
                                                          t->l.seg_pred[by4]])))
            {
                // temporal predicted seg_id
                b->seg_id = f->prev_segmap ?
                            get_prev_frame_segid(f, t->by, t->bx, w4, h4,
                                                 f->prev_segmap, f->b4_stride) : 0;
            } else {
                int seg_ctx;
                const unsigned pred_seg_id =
                    get_cur_frame_segid(t->by, t->bx, have_top, have_left,
                                        &seg_ctx, f->cur_segmap, f->b4_stride);
                const unsigned diff = msac_decode_symbol_adapt(&ts->msac,
                                                   ts->cdf.m.seg_id[seg_ctx],
                                                   NUM_SEGMENTS);
                const unsigned last_active_seg_id =
                    f->frame_hdr.segmentation.seg_data.last_active_segid;
773
774
                b->seg_id = neg_deinterleave(diff, pred_seg_id,
                                             last_active_seg_id + 1);
775
                if (b->seg_id > last_active_seg_id) b->seg_id = 0; // error?
776
                if (b->seg_id >= NUM_SEGMENTS) b->seg_id = 0; // error?
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
            }

            if (DEBUG_BLOCK_INFO)
                printf("Post-segid[preskip;%d]: r=%d\n",
                       b->seg_id, ts->msac.rng);
        }
    } else {
        b->seg_id = 0;
    }

    // skip
    const int sctx = t->a->skip[bx4] + t->l.skip[by4];
    b->skip = b->skip_mode ? 1 :
              msac_decode_bool_adapt(&ts->msac, ts->cdf.m.skip[sctx]);
    if (DEBUG_BLOCK_INFO)
        printf("Post-skip[%d]: r=%d\n", b->skip, ts->msac.rng);

    // segment_id
    if (f->frame_hdr.segmentation.enabled &&
        f->frame_hdr.segmentation.update_map &&
        !f->frame_hdr.segmentation.seg_data.preskip)
    {
        if (!b->skip && f->frame_hdr.segmentation.temporal &&
            (seg_pred = msac_decode_bool_adapt(&ts->msac,
                                   ts->cdf.m.seg_pred[t->a->seg_pred[bx4] +
                                                      t->l.seg_pred[by4]])))
        {
            // temporal predicted seg_id
            b->seg_id = f->prev_segmap ?
                        get_prev_frame_segid(f, t->by, t->bx, w4, h4,
                                             f->prev_segmap, f->b4_stride) : 0;
        } else {
            int seg_ctx;
            const unsigned pred_seg_id =
                get_cur_frame_segid(t->by, t->bx, have_top, have_left,
                                    &seg_ctx, f->cur_segmap, f->b4_stride);
            if (b->skip) {
                b->seg_id = pred_seg_id;
            } else {
                const unsigned diff = msac_decode_symbol_adapt(&ts->msac,
                                                   ts->cdf.m.seg_id[seg_ctx],
                                                   NUM_SEGMENTS);
                const unsigned last_active_seg_id =
                    f->frame_hdr.segmentation.seg_data.last_active_segid;
821
822
                b->seg_id = neg_deinterleave(diff, pred_seg_id,
                                             last_active_seg_id + 1);
823
824
                if (b->seg_id > last_active_seg_id) b->seg_id = 0; // error?
            }
825
            if (b->seg_id >= NUM_SEGMENTS) b->seg_id = 0; // error?
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
        }

        if (DEBUG_BLOCK_INFO)
            printf("Post-segid[postskip;%d]: r=%d\n",
                   b->seg_id, ts->msac.rng);
    }

    // cdef index
    if (!b->skip) {
        const int idx = f->seq_hdr.sb128 ? ((t->bx & 16) >> 4) +
                                           ((t->by & 16) >> 3) : 0;
        if (t->cur_sb_cdef_idx_ptr[idx] == -1) {
            const int v = msac_decode_bools(&ts->msac, f->frame_hdr.cdef.n_bits);
            t->cur_sb_cdef_idx_ptr[idx] = v;
            if (bw4 > 16) t->cur_sb_cdef_idx_ptr[idx + 1] = v;
            if (bh4 > 16) t->cur_sb_cdef_idx_ptr[idx + 2] = v;
            if (bw4 == 32 && bh4 == 32) t->cur_sb_cdef_idx_ptr[idx + 3] = v;

            if (DEBUG_BLOCK_INFO)
                printf("Post-cdef_idx[%d]: r=%d\n",
                        *t->cur_sb_cdef_idx_ptr, ts->msac.rng);
        }
    }

    // delta-q/lf
    if (!(t->bx & (31 >> !f->seq_hdr.sb128)) &&
        !(t->by & (31 >> !f->seq_hdr.sb128)))
    {
        const int prev_qidx = ts->last_qidx;
855
        const int have_delta_q = f->frame_hdr.delta.q.present &&
856
            (bs != (f->seq_hdr.sb128 ? BS_128x128 : BS_64x64) || !b->skip);
857
858
859
860

        int8_t prev_delta_lf[4];
        memcpy(prev_delta_lf, ts->last_delta_lf, 4);

861
862
863
864
865
866
867
868
        if (have_delta_q) {
            int delta_q = msac_decode_symbol_adapt(&ts->msac, ts->cdf.m.delta_q, 4);
            if (delta_q == 3) {
                const int n_bits = 1 + msac_decode_bools(&ts->msac, 3);
                delta_q = msac_decode_bools(&ts->msac, n_bits) + 1 + (1 << n_bits);
            }
            if (delta_q) {
                if (msac_decode_bool(&ts->msac, 128 << 7)) delta_q = -delta_q;
869
                delta_q *= 1 << f->frame_hdr.delta.q.res_log2;
870
871
872
873
874
            }
            ts->last_qidx = iclip(ts->last_qidx + delta_q, 1, 255);
            if (have_delta_q && DEBUG_BLOCK_INFO)
                printf("Post-delta_q[%d->%d]: r=%d\n",
                       delta_q, ts->last_qidx, ts->msac.rng);
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900

            if (f->frame_hdr.delta.lf.present) {
                const int n_lfs = f->frame_hdr.delta.lf.multi ?
                    f->seq_hdr.layout != DAV1D_PIXEL_LAYOUT_I400 ? 4 : 2 : 1;

                for (int i = 0; i < n_lfs; i++) {
                    int delta_lf =
                        msac_decode_symbol_adapt(&ts->msac,
                        ts->cdf.m.delta_lf[i + f->frame_hdr.delta.lf.multi], 4);
                    if (delta_lf == 3) {
                        const int n_bits = 1 + msac_decode_bools(&ts->msac, 3);
                        delta_lf = msac_decode_bools(&ts->msac, n_bits) +
                                   1 + (1 << n_bits);
                    }
                    if (delta_lf) {
                        if (msac_decode_bool(&ts->msac, 128 << 7))
                            delta_lf = -delta_lf;
                        delta_lf *= 1 << f->frame_hdr.delta.lf.res_log2;
                    }
                    ts->last_delta_lf[i] =
                        iclip(ts->last_delta_lf[i] + delta_lf, -63, 63);
                    if (have_delta_q && DEBUG_BLOCK_INFO)
                        printf("Post-delta_lf[%d:%d]: r=%d\n", i, delta_lf,
                               ts->msac.rng);
                }
            }
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
        }
        if (ts->last_qidx == f->frame_hdr.quant.yac) {
            // assign frame-wide q values to this sb
            ts->dq = f->dq;
        } else if (ts->last_qidx != prev_qidx) {
            // find sb-specific quant parameters
            init_quant_tables(&f->seq_hdr, &f->frame_hdr, ts->last_qidx, ts->dqmem);
            ts->dq = ts->dqmem;
        }
        if (!memcmp(ts->last_delta_lf, (int8_t[4]) { 0, 0, 0, 0 }, 4)) {
            // assign frame-wide lf values to this sb
            ts->lflvl = f->lf.lvl;
        } else if (memcmp(ts->last_delta_lf, prev_delta_lf, 4)) {
            // find sb-specific lf lvl parameters
            dav1d_calc_lf_values(ts->lflvlmem, &f->frame_hdr, ts->last_delta_lf);
            ts->lflvl = ts->lflvlmem;
        }
    }

    if (b->skip_mode) {
        b->intra = 0;
    } else if (f->frame_hdr.frame_type & 1) {
        const int ictx = get_intra_ctx(t->a, &t->l, by4, bx4,
                                       have_top, have_left);
        b->intra = !msac_decode_bool_adapt(&ts->msac, ts->cdf.m.intra[ictx]);
        if (DEBUG_BLOCK_INFO)
            printf("Post-intra[%d]: r=%d\n", b->intra, ts->msac.rng);
    } else if (f->frame_hdr.allow_intrabc) {
        b->intra = !msac_decode_bool_adapt(&ts->msac, ts->cdf.m.intrabc);
        if (DEBUG_BLOCK_INFO)
            printf("Post-intrabcflag[%d]: r=%d\n", b->intra, ts->msac.rng);
    } else {
        b->intra = 1;
    }

    // intra/inter-specific stuff
    if (b->intra) {
        uint16_t *const ymode_cdf = f->frame_hdr.frame_type & 1 ?
939
940
941
            ts->cdf.m.y_mode[dav1d_ymode_size_context[bs]] :
            ts->cdf.kfym[dav1d_intra_mode_context[t->a->mode[bx4]]]
                        [dav1d_intra_mode_context[t->l.mode[by4]]];
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
        b->y_mode = msac_decode_symbol_adapt(&ts->msac, ymode_cdf,
                                              N_INTRA_PRED_MODES);
        if (DEBUG_BLOCK_INFO)
            printf("Post-ymode[%d]: r=%d\n", b->y_mode, ts->msac.rng);

        // angle delta
        if (b_dim[2] + b_dim[3] >= 2 && b->y_mode >= VERT_PRED &&
            b->y_mode <= VERT_LEFT_PRED)
        {
            uint16_t *const acdf = ts->cdf.m.angle_delta[b->y_mode - VERT_PRED];
            const int angle = msac_decode_symbol_adapt(&ts->msac, acdf, 7);
            b->y_angle = angle - 3;
        } else {
            b->y_angle = 0;
        }

        if (has_chroma) {
959
960
            const int cfl_allowed = f->frame_hdr.segmentation.lossless[b->seg_id] ?
                cbw4 == 1 && cbh4 == 1 : !!(cfl_allowed_mask & (1 << bs));
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
            uint16_t *const uvmode_cdf = ts->cdf.m.uv_mode[cfl_allowed][b->y_mode];
            b->uv_mode = msac_decode_symbol_adapt(&ts->msac, uvmode_cdf,
                                         N_UV_INTRA_PRED_MODES - !cfl_allowed);
            if (DEBUG_BLOCK_INFO)
                printf("Post-uvmode[%d]: r=%d\n", b->uv_mode, ts->msac.rng);

            if (b->uv_mode == CFL_PRED) {
#define SIGN(a) (!!(a) + ((a) > 0))
                const int sign =
                    msac_decode_symbol_adapt(&ts->msac, ts->cdf.m.cfl_sign, 8) + 1;
                const int sign_u = sign * 0x56 >> 8, sign_v = sign - sign_u * 3;
                assert(sign_u == sign / 3);
                if (sign_u) {
                    const int ctx = (sign_u == 2) * 3 + sign_v;
                    b->cfl_alpha[0] = msac_decode_symbol_adapt(&ts->msac,
                                            ts->cdf.m.cfl_alpha[ctx], 16) + 1;
                    if (sign_u == 1) b->cfl_alpha[0] = -b->cfl_alpha[0];
                } else {
                    b->cfl_alpha[0] = 0;
                }
                if (sign_v) {
                    const int ctx = (sign_v == 2) * 3 + sign_u;
                    b->cfl_alpha[1] = msac_decode_symbol_adapt(&ts->msac,
                                            ts->cdf.m.cfl_alpha[ctx], 16) + 1;
                    if (sign_v == 1) b->cfl_alpha[1] = -b->cfl_alpha[1];
                } else {
                    b->cfl_alpha[1] = 0;
                }
#undef SIGN
                if (DEBUG_BLOCK_INFO)
                    printf("Post-uvalphas[%d/%d]: r=%d\n",
                           b->cfl_alpha[0], b->cfl_alpha[1], ts->msac.rng);
            } else if (b_dim[2] + b_dim[3] >= 2 && b->uv_mode >= VERT_PRED &&
                       b->uv_mode <= VERT_LEFT_PRED)
            {
                uint16_t *const acdf = ts->cdf.m.angle_delta[b->uv_mode - VERT_PRED];
                const int angle = msac_decode_symbol_adapt(&ts->msac, acdf, 7);
                b->uv_angle = angle - 3;
            } else {
                b->uv_angle = 0;
            }
        }

        b->pal_sz[0] = b->pal_sz[1] = 0;
        if (f->frame_hdr.allow_screen_content_tools &&
            imax(bw4, bh4) <= 16 && bw4 + bh4 >= 4)
        {
            const int sz_ctx = b_dim[2] + b_dim[3] - 2;
            if (b->y_mode == DC_PRED) {
                const int pal_ctx = (t->a->pal_sz[bx4] > 0) + (t->l.pal_sz[by4] > 0);
                const int use_y_pal =
                    msac_decode_bool_adapt(&ts->msac, ts->cdf.m.pal_y[sz_ctx][pal_ctx]);
                if (DEBUG_BLOCK_INFO)
                    printf("Post-y_pal[%d]: r=%d\n", use_y_pal, ts->msac.rng);
                if (use_y_pal)
                    read_pal_plane(t, b, 0, sz_ctx, bx4, by4);
            }

            if (has_chroma && b->uv_mode == DC_PRED) {
                const int pal_ctx = b->pal_sz[0] > 0;
                const int use_uv_pal =
                    msac_decode_bool_adapt(&ts->msac, ts->cdf.m.pal_uv[pal_ctx]);
                if (DEBUG_BLOCK_INFO)
                    printf("Post-uv_pal[%d]: r=%d\n", use_uv_pal, ts->msac.rng);
1025
1026
                if (use_uv_pal) // see aomedia bug 2183 for why we use luma coordinates
                    read_pal_uv(t, b, sz_ctx, bx4, by4);
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
            }
        }

        if (b->y_mode == DC_PRED && !b->pal_sz[0] &&
            imax(b_dim[2], b_dim[3]) <= 3 && f->seq_hdr.filter_intra)
        {
            const int is_filter = msac_decode_bool_adapt(&ts->msac,
                                            ts->cdf.m.use_filter_intra[bs]);
            if (is_filter) {
                b->y_mode = FILTER_PRED;
                b->y_angle = msac_decode_symbol_adapt(&ts->msac,
                                                  ts->cdf.m.filter_intra, 5);
            }
            if (DEBUG_BLOCK_INFO)
                printf("Post-filterintramode[%d/%d]: r=%d\n",
                       b->y_mode, b->y_angle, ts->msac.rng);
        }

        if (b->pal_sz[0]) {
            uint8_t *pal_idx;
            if (f->frame_thread.pass) {
                pal_idx = ts->frame_thread.pal_idx;
                ts->frame_thread.pal_idx += bw4 * bh4 * 16;
            } else
                pal_idx = t->scratch.pal_idx;
            read_pal_indices(t, pal_idx, b, 0, w4, h4, bw4, bh4);
            if (DEBUG_BLOCK_INFO)
                printf("Post-y-pal-indices: r=%d\n", ts->msac.rng);
        }

        if (has_chroma && b->pal_sz[1]) {
            uint8_t *pal_idx;
            if (f->frame_thread.pass) {
                pal_idx = ts->frame_thread.pal_idx;
                ts->frame_thread.pal_idx += cbw4 * cbh4 * 16;
            } else
                pal_idx = &t->scratch.pal_idx[bw4 * bh4 * 16];
            read_pal_indices(t, pal_idx, b, 1, cw4, ch4, cbw4, cbh4);
            if (DEBUG_BLOCK_INFO)
                printf("Post-uv-pal-indices: r=%d\n", ts->msac.rng);
        }

        const TxfmInfo *t_dim;
        if (f->frame_hdr.segmentation.lossless[b->seg_id]) {
            b->tx = b->uvtx = (int) TX_4X4;
1072
            t_dim = &dav1d_txfm_dimensions[TX_4X4];
1073
        } else {
1074
1075
1076
            b->tx = dav1d_max_txfm_size_for_bs[bs][0];
            b->uvtx = dav1d_max_txfm_size_for_bs[bs][f->cur.p.p.layout];
            t_dim = &dav1d_txfm_dimensions[b->tx];
1077
            if (f->frame_hdr.txfm_mode == TX_SWITCHABLE && t_dim->max > TX_4X4) {
1078
                const int tctx = get_tx_ctx(t->a, &t->l, t_dim, by4, bx4);
1079
1080
1081
1082
1083
1084
                uint16_t *const tx_cdf = ts->cdf.m.txsz[t_dim->max - 1][tctx];
                int depth = msac_decode_symbol_adapt(&ts->msac, tx_cdf,
                                                     imin(t_dim->max + 1, 3));

                while (depth--) {
                    b->tx = t_dim->sub;
1085
                    t_dim = &dav1d_txfm_dimensions[b->tx];
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
                }
            }
            if (DEBUG_BLOCK_INFO)
                printf("Post-tx[%d]: r=%d\n", b->tx, ts->msac.rng);
        }

        // reconstruction
        if (f->frame_thread.pass == 1) {
            f->bd_fn.read_coef_blocks(t, bs, b);
        } else {
            f->bd_fn.recon_b_intra(t, bs, intra_edge_flags, b);
        }

        dav1d_create_lf_mask_intra(t->lf_mask, f->lf.level, f->b4_stride,
                                   &f->frame_hdr, (const uint8_t (*)[8][2])
                                   &ts->lflvl[b->seg_id][0][0][0],
1102
1103
                                   t->bx, t->by, (f->cur.p.p.w + 3) >> 2,
                                   (f->cur.p.p.h + 3) >> 2, bs,
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
                                   b->tx, b->uvtx, f->cur.p.p.layout,
                                   &t->a->tx_lpf_y[bx4], &t->l.tx_lpf_y[by4],
                                   has_chroma ? &t->a->tx_lpf_uv[cbx4] : NULL,
                                   has_chroma ? &t->l.tx_lpf_uv[cby4] : NULL);

        // update contexts
        memset(&t->a->tx_intra[bx4], t_dim->lw, bw4);
        memset(&t->l.tx_intra[by4], t_dim->lh, bh4);
        const enum IntraPredMode y_mode_nofilt =
            b->y_mode == FILTER_PRED ? DC_PRED : b->y_mode;
        memset(&t->l.mode[by4], y_mode_nofilt, bh4);
        memset(&t->a->mode[bx4], y_mode_nofilt, bw4);
        memset(&t->l.pal_sz[by4], b->pal_sz[0], bh4);
        memset(&t->a->pal_sz[bx4], b->pal_sz[0], bw4);
        if (b->pal_sz[0]) {
            uint16_t *const pal = f->frame_thread.pass ?
                f->frame_thread.pal[((t->by >> 1) + (t->bx & 1)) * (f->b4_stride >> 1) +
                                    ((t->bx >> 1) + (t->by & 1))][0] : t->pal[0];
            for (int x = 0; x < bw4; x++)
                memcpy(t->al_pal[0][bx4 + x][0], pal, 16);
            for (int y = 0; y < bh4; y++)
                memcpy(t->al_pal[1][by4 + y][0], pal, 16);
        }
        if (has_chroma) {
            memset(&t->l.uvmode[cby4], b->uv_mode, cbh4);
            memset(&t->a->uvmode[cbx4], b->uv_mode, cbw4);
1130
1131
1132
            // see aomedia bug 2183 for why we use luma coordinates here
            memset(&t->pal_sz_uv[1][by4], b->pal_sz[1], bh4);
            memset(&t->pal_sz_uv[0][bx4], b->pal_sz[1], bw4);
1133
1134
1135
1136
            if (b->pal_sz[1]) for (int pl = 1; pl < 3; pl++) {
                uint16_t *const pal = f->frame_thread.pass ?
                    f->frame_thread.pal[((t->by >> 1) + (t->bx & 1)) * (f->b4_stride >> 1) +
                                        ((t->bx >> 1) + (t->by & 1))][pl] : t->pal[pl];
1137
1138
1139
1140
1141
                // see aomedia bug 2183 for why we use luma coordinates here
                for (int x = 0; x < bw4; x++)
                    memcpy(t->al_pal[0][bx4 + x][pl], pal, 16);
                for (int y = 0; y < bh4; y++)
                    memcpy(t->al_pal[1][by4 + y][pl], pal, 16);
1142
            }
1143
1144
1145
        } else { // see aomedia bug 2183 for why we reset this
            memset(&t->pal_sz_uv[1][by4], 0, bh4);
            memset(&t->pal_sz_uv[0][bx4], 0, bw4);
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
        }
        if ((f->frame_hdr.frame_type & 1) || f->frame_hdr.allow_intrabc) {
            memset(&t->a->tx[bx4], t_dim->lw, bw4);
            memset(&t->l.tx[by4], t_dim->lh, bh4);
            splat_intraref(f->mvs, f->b4_stride, t->by, t->bx, bs,
                           y_mode_nofilt);
        }
        if (f->frame_hdr.frame_type & 1) {
            memset(&t->l.comp_type[by4], COMP_INTER_NONE, bh4);
            memset(&t->a->comp_type[bx4], COMP_INTER_NONE, bw4);
            memset(&t->l.ref[0][by4], -1, bh4);
            memset(&t->a->ref[0][bx4], -1, bw4);
            memset(&t->l.ref[1][by4], -1, bh4);
            memset(&t->a->ref[1][bx4], -1, bw4);
            memset(&t->l.filter[0][by4], N_SWITCHABLE_FILTERS, bh4);
            memset(&t->a->filter[0][bx4], N_SWITCHABLE_FILTERS, bw4);
            memset(&t->l.filter[1][by4], N_SWITCHABLE_FILTERS, bh4);
            memset(&t->a->filter[1][bx4], N_SWITCHABLE_FILTERS, bw4);
        }
    } else if (!(f->frame_hdr.frame_type & 1)) {
        // intra block copy
        candidate_mv mvstack[8];
        int n_mvs;
        mv mvlist[2][2];
        av1_find_ref_mvs(mvstack, &n_mvs, mvlist, NULL,
                         (int[2]) { -1, -1 }, f->bw, f->bh,
                         bs, bp, t->by, t->bx, ts->tiling.col_start,
                         ts->tiling.col_end, ts->tiling.row_start,
                         ts->tiling.row_end, f->libaom_cm);

        if (mvlist[0][0].y | mvlist[0][0].x)
            b->mv[0] = mvlist[0][0];
        else if (mvlist[0][1].y | mvlist[0][1].x)
            b->mv[0] = mvlist[0][1];
        else {
            if (t->by - (16 << f->seq_hdr.sb128) < ts->tiling.row_start) {
                b->mv[0].y = 0;
                b->mv[0].x = -(512 << f->seq_hdr.sb128) - 2048;
            } else {
                b->mv[0].y = -(512 << f->seq_hdr.sb128);
                b->mv[0].x = 0;
            }
        }

        const struct mv ref = b->mv[0];
        read_mv_residual(t, &b->mv[0], &ts->cdf.dmv, 0);
        if (DEBUG_BLOCK_INFO)
            printf("Post-dmv[%d/%d,ref=%d/%d|%d/%d]: r=%d\n",
                   b->mv[0].y, b->mv[0].x, ref.y, ref.x,
                   mvlist[0][0].y, mvlist[0][0].x, ts->msac.rng);
1196
1197
        if (b->mv[0].x >= 0 && b->mv[0].y >= 0)
            b->mv[0] = ref;
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
        read_vartx_tree(t, b, bs, bx4, by4);

        // reconstruction
        if (f->frame_thread.pass == 1) {
            f->bd_fn.read_coef_blocks(t, bs, b);
        } else {
            f->bd_fn.recon_b_inter(t, bs, b);
        }

        splat_intrabc_mv(f->mvs, f->b4_stride, t->by, t->bx, bs, b->mv[0]);

        memset(&t->a->tx_intra[bx4], b_dim[2], bw4);
        memset(&t->l.tx_intra[by4], b_dim[3], bh4);
        memset(&t->l.mode[by4], DC_PRED, bh4);
        memset(&t->a->mode[bx4], DC_PRED, bw4);
        memset(&t->l.pal_sz[by4], 0, bh4);
        memset(&t->a->pal_sz[bx4], 0, bw4);
1215
1216
1217
        // see aomedia bug 2183 for why this is outside if (has_chroma)
        memset(&t->pal_sz_uv[1][by4], 0, bh4);
        memset(&t->pal_sz_uv[0][bx4], 0, bw4);
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
        if (has_chroma) {
            memset(&t->l.uvmode[cby4], DC_PRED, cbh4);
            memset(&t->a->uvmode[cbx4], DC_PRED, cbw4);
        }
    } else {
        // inter-specific mode/mv coding
        int is_comp, has_subpel_filter;

        if (b->skip_mode) {
            is_comp = 1;
        } else if (f->frame_hdr.switchable_comp_refs && imin(bw4, bh4) > 1) {
            const int ctx = get_comp_ctx(t->a, &t->l, by4, bx4,
                                         have_top, have_left);
            is_comp = msac_decode_bool_adapt(&ts->msac, ts->cdf.m.comp[ctx]);
            if (DEBUG_BLOCK_INFO)
                printf("Post-compflag[%d]: r=%d\n", is_comp, ts->msac.rng);
        } else {
            is_comp = 0;
        }

        if (b->skip_mode) {
            b->ref[0] = f->frame_hdr.skip_mode_refs[0];
            b->ref[1] = f->frame_hdr.skip_mode_refs[1];
            b->comp_type = COMP_INTER_AVG;
            b->inter_mode = NEARESTMV_NEARESTMV;
            b->drl_idx = 0;
            has_subpel_filter = 0;

            candidate_mv mvstack[8];
            int n_mvs, ctx;
            mv mvlist[2][2];
            av1_find_ref_mvs(mvstack, &n_mvs, mvlist, &ctx,
                             (int[2]) { b->ref[0], b->ref[1] }, f->bw, f->bh,
                             bs, bp, t->by, t->bx, ts->tiling.col_start,
                             ts->tiling.col_end, ts->tiling.row_start,
                             ts->tiling.row_end, f->libaom_cm);

            b->mv[0] = mvstack[0].this_mv;
            b->mv[1] = mvstack[0].comp_mv;
            if (!f->frame_hdr.hp) {
                unset_hp_bit(&b->mv[0]);
                unset_hp_bit(&b->mv[1]);
            }
            if (DEBUG_BLOCK_INFO)
                printf("Post-skipmodeblock[mv=1:y=%d,x=%d,2:y=%d,x=%d,refs=%d+%d\n",
                       b->mv[0].y, b->mv[0].x, b->mv[1].y, b->mv[1].x,
                       b->ref[0], b->ref[1]);
        } else if (is_comp) {
            const int dir_ctx = get_comp_dir_ctx(t->a, &t->l, by4, bx4,
                                                 have_top, have_left);
            if (msac_decode_bool_adapt(&ts->msac, ts->cdf.m.comp_dir[dir_ctx])) {
                // bidir - first reference (fw)
                const int ctx1 = av1_get_fwd_ref_ctx(t->a, &t->l, by4, bx4,
                                                     have_top, have_left);
                if (msac_decode_bool_adapt(&ts->msac,
                                           ts->cdf.m.comp_fwd_ref[0][ctx1]))
                {
                    const int ctx2 = av1_get_fwd_ref_2_ctx(t->a, &t->l, by4, bx4,
                                                           have_top, have_left);
                    b->ref[0] = 2 + msac_decode_bool_adapt(&ts->msac,
                                            ts->cdf.m.comp_fwd_ref[2][ctx2]);
                } else {
                    const int ctx2 = av1_get_fwd_ref_1_ctx(t->a, &t->l, by4, bx4,
                                                           have_top, have_left);
                    b->ref[0] = msac_decode_bool_adapt(&ts->msac,
                                            ts->cdf.m.comp_fwd_ref[1][ctx2]);
                }

                // second reference (bw)
                const int ctx3 = av1_get_bwd_ref_ctx(t->a, &t->l, by4, bx4,
                                                     have_top, have_left);
                if (msac_decode_bool_adapt(&ts->msac,
                                           ts->cdf.m.comp_bwd_ref[0][ctx3]))
                {
                    b->ref[1] = 6;
                } else {
                    const int ctx4 = av1_get_bwd_ref_1_ctx(t->a, &t->l, by4, bx4,
                                                           have_top, have_left);
                    b->ref[1] = 4 + msac_decode_bool_adapt(&ts->msac,
                                           ts->cdf.m.comp_bwd_ref[1][ctx4]);
                }
            } else {
                // unidir
                const int uctx_p = av1_get_uni_p_ctx(t->a, &t->l, by4, bx4,
                                                     have_top, have_left);
                if (msac_decode_bool_adapt(&ts->msac,
                                           ts->cdf.m.comp_uni_ref[0][uctx_p]))
                {
                    b->ref[0] = 4;
                    b->ref[1] = 6;
                } else {
                    const int uctx_p1 = av1_get_uni_p1_ctx(t->a, &t->l, by4, bx4,
                                                           have_top, have_left);
                    b->ref[0] = 0;
                    b->ref[1] = 1 + msac_decode_bool_adapt(&ts->msac,
                                           ts->cdf.m.comp_uni_ref[1][uctx_p1]);
                    if (b->ref[1] == 2) {
                        const int uctx_p2 = av1_get_uni_p2_ctx(t->a, &t->l, by4, bx4,
                                                               have_top, have_left);
                        b->ref[1] += msac_decode_bool_adapt(&ts->msac,
                                           ts->cdf.m.comp_uni_ref[2][uctx_p2]);
                    }
                }
            }
            if (DEBUG_BLOCK_INFO)
                printf("Post-refs[%d/%d]: r=%d\n",
                       b->ref[0], b->ref[1], ts->msac.rng);

            candidate_mv mvstack[8];
            int n_mvs, ctx;
            mv mvlist[2][2];
            av1_find_ref_mvs(mvstack, &n_mvs, mvlist, &ctx,
                             (int[2]) { b->ref[0], b->ref[1] }, f->bw, f->bh,
                             bs, bp, t->by, t->bx, ts->tiling.col_start,
                             ts->tiling.col_end, ts->tiling.row_start,
                             ts->tiling.row_end, f->libaom_cm);

            b->inter_mode = msac_decode_symbol_adapt(&ts->msac,
                                             ts->cdf.m.comp_inter_mode[ctx],
                                             N_COMP_INTER_PRED_MODES);
            if (DEBUG_BLOCK_INFO)
                printf("Post-compintermode[%d,ctx=%d,n_mvs=%d]: r=%d\n",
                       b->inter_mode, ctx, n_mvs, ts->msac.rng);

1342
            const uint8_t *const im = dav1d_comp_inter_pred_modes[b->inter_mode];
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
            b->drl_idx = 0;
            if (b->inter_mode == NEWMV_NEWMV) {
                if (n_mvs > 1) {
                    const int drl_ctx_v1 = get_drl_context(mvstack, 0);
                    b->drl_idx += msac_decode_bool_adapt(&ts->msac,
                                             ts->cdf.m.drl_bit[drl_ctx_v1]);
                    if (b->drl_idx == 1 && n_mvs > 2) {
                        const int drl_ctx_v2 = get_drl_context(mvstack, 1);
                        b->drl_idx += msac_decode_bool_adapt(&ts->msac,
                                             ts->cdf.m.drl_bit[drl_ctx_v2]);
                    }
                    if (DEBUG_BLOCK_INFO)
                        printf("Post-drlidx[%d,n_mvs=%d]: r=%d\n",
                               b->drl_idx, n_mvs, ts->msac.rng);
                }
            } else if (im[0] == NEARMV || im[1] == NEARMV) {
                b->drl_idx = 1;
                if (n_mvs > 2) {
                    const int drl_ctx_v2 = get_drl_context(mvstack, 1);
                    b->drl_idx += msac_decode_bool_adapt(&ts->msac,
                                             ts->cdf.m.drl_bit[drl_ctx_v2]);
                    if (b->drl_idx == 2 && n_mvs > 3) {
                        const int drl_ctx_v3 = get_drl_context(mvstack, 2);
                        b->drl_idx += msac_decode_bool_adapt(&ts->msac,
                                             ts->cdf.m.drl_bit[drl_ctx_v3]);
                    }
                    if (DEBUG_BLOCK_INFO)
                        printf("Post-drlidx[%d,n_mvs=%d]: r=%d\n",
                               b->drl_idx, n_mvs, ts->msac.rng);
                }
            }

#define assign_comp_mv(idx, pfx) \
            switch (im[idx]) { \
            case NEARMV: \
            case NEARESTMV: \
                b->mv[idx] = mvstack[b->drl_idx].pfx##_mv; \
                if (!f->frame_hdr.hp) unset_hp_bit(&b->mv[idx]); \
                break; \
            case GLOBALMV: \
                has_subpel_filter |= \
                    f->frame_hdr.gmv[b->ref[idx]].type == WM_TYPE_TRANSLATION; \
                b->mv[idx] = get_gmv_2d(&f->frame_hdr.gmv[b->ref[idx]], \
                                        t->bx, t->by, bw4, bh4, &f->frame_hdr); \
                break; \
            case NEWMV: \
                b->mv[idx] = mvstack[b->drl_idx].pfx##_mv; \
                read_mv_residual(t, &b->mv[idx], &ts->cdf.mv, \
                                 !f->frame_hdr.force_integer_mv); \
                break; \
            }
            has_subpel_filter = imin(bw4, bh4) == 1 ||
                                b->inter_mode != GLOBALMV_GLOBALMV;
            assign_comp_mv(0, this);
            assign_comp_mv(1, comp);
#undef assign_comp_mv
            if (DEBUG_BLOCK_INFO)
                printf("Post-residual_mv[1:y=%d,x=%d,2:y=%d,x=%d]: r=%d\n",
                       b->mv[0].y, b->mv[0].x, b->mv[1].y, b->mv[1].x,
                       ts->msac.rng);

            // jnt_comp vs. seg vs. wedge
            int is_segwedge = 0;
            if (f->seq_hdr.masked_compound) {
                const int mask_ctx = get_mask_comp_ctx(t->a, &t->l, by4, bx4);

                is_segwedge = msac_decode_bool_adapt(&ts->msac,
                                                 ts->cdf.m.mask_comp[mask_ctx]);
                if (DEBUG_BLOCK_INFO)
                    printf("Post-segwedge_vs_jntavg[%d,ctx=%d]: r=%d\n",
                           is_segwedge, mask_ctx, ts->msac.rng);
            }

            if (!is_segwedge) {
                if (f->seq_hdr.jnt_comp) {
                    const int jnt_ctx =
                        get_jnt_comp_ctx(f->seq_hdr.order_hint_n_bits,
                                         f->cur.p.poc, f->refp[b->ref[0]].p.poc,
                                         f->refp[b->ref[1]].p.poc, t->a, &t->l,
                                         by4, bx4);
                    b->comp_type = COMP_INTER_WEIGHTED_AVG +
                        msac_decode_bool_adapt(&ts->msac,
                                               ts->cdf.m.jnt_comp[jnt_ctx]);
                    if (DEBUG_BLOCK_INFO)
                        printf("Post-jnt_comp[%d,ctx=%d[ac:%d,ar:%d,lc:%d,lr:%d]]: r=%d\n",
                               b->comp_type == COMP_INTER_AVG,
                               jnt_ctx, t->a->comp_type[bx4], t->a->ref[0][bx4],
                               t->l.comp_type[by4], t->l.ref[0][by4],
                               ts->msac.rng);
                } else {
                    b->comp_type = COMP_INTER_AVG;
                }
            } else {
                if (wedge_allowed_mask & (1 << bs)) {
1437
                    const int ctx = dav1d_wedge_ctx_lut[bs];
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
                    b->comp_type = COMP_INTER_WEDGE -
                        msac_decode_bool_adapt(&ts->msac,
                                               ts->cdf.m.wedge_comp[ctx]);
                    if (b->comp_type == COMP_INTER_WEDGE)
                        b->wedge_idx = msac_decode_symbol_adapt(&ts->msac,
                                                ts->cdf.m.wedge_idx[ctx], 16);
                } else {
                    b->comp_type = COMP_INTER_SEG;
                }
                b->mask_sign = msac_decode_bool(&ts->msac, 128 << 7);
                if (DEBUG_BLOCK_INFO)
                    printf("Post-seg/wedge[%d,wedge_idx=%d,sign=%d]: r=%d\n",
                           b->comp_type == COMP_INTER_WEDGE,
                           b->wedge_idx, b->mask_sign, ts->msac.rng);
            }
        } else {
            b->comp_type = COMP_INTER_NONE;

            // ref
            const int ctx1 = av1_get_ref_ctx(t->a, &t->l, by4, bx4,
                                             have_top, have_left);
            if (msac_decode_bool_adapt(&ts->msac, ts->cdf.m.ref[0][ctx1])) {
                const int ctx2 = av1_get_ref_2_ctx(t->a, &t->l, by4, bx4,
                                                   have_top, have_left);
                if (msac_decode_bool_adapt(&ts->msac, ts->cdf.m.ref[1][ctx2])) {
                    b->ref[0] = 6;
                } else {
                    const int ctx3 = av1_get_ref_6_ctx(t->a, &t->l, by4, bx4,
                                                       have_top, have_left);
                    b->ref[0] = 4 + msac_decode_bool_adapt(&ts->msac,
                                                       ts->cdf.m.ref[5][ctx3]);
                }
            } else {
                const int ctx2 = av1_get_ref_3_ctx(t->a, &t->l, by4, bx4,
                                                   have_top, have_left);
                if (msac_decode_bool_adapt(&ts->msac, ts->cdf.m.ref[2][ctx2])) {
                    const int ctx3 = av1_get_ref_5_ctx(t->a, &t->l, by4, bx4,
                                                       have_top, have_left);
                    b->ref[0] = 2 + msac_decode_bool_adapt(&ts->msac,
                                                       ts->cdf.m.ref[4][ctx3]);
                } else {
                    const int ctx3 = av1_get_ref_4_ctx(t->a, &t->l, by4, bx4,
                                                       have_top, have_left);
                    b->ref[0] = msac_decode_bool_adapt(&ts->msac,
                                                       ts->cdf.m.ref[3][ctx3]);
                }
            }
            b->ref[1] = -1;
            if (DEBUG_BLOCK_INFO)
                printf("Post-ref[%d]: r=%d\n", b->ref[0], ts->msac.rng);

            candidate_mv mvstack[8];
            int n_mvs, ctx;
            mv mvlist[2][2];
            av1_find_ref_mvs(mvstack, &n_mvs, mvlist, &ctx,
                             (int[2]) { b->ref[0], -1 }, f->bw, f->bh, bs, bp,
                             t->by, t->bx, ts->tiling.col_start,
                             ts->tiling.col_end, ts->tiling.row_start,
                             ts->tiling.row_end, f->libaom_cm);

            // mode parsing and mv derivation from ref_mvs
            if (msac_decode_bool_adapt(&ts->msac, ts->cdf.m.newmv_mode[ctx & 7])) {
                if (!msac_decode_bool_adapt(&ts->msac,
                                        ts->cdf.m.globalmv_mode[(ctx >> 3) & 1]))
                {
                    b->inter_mode = GLOBALMV;
                    b->mv[0] = get_gmv_2d(&f->frame_hdr.gmv[b->ref[0]],
                                          t->bx, t->by, bw4, bh4, &f->frame_hdr);
                    has_subpel_filter = imin(bw4, bh4) == 1 ||
                        f->frame_hdr.gmv[b->ref[0]].type == WM_TYPE_TRANSLATION;
                } else {
                    has_subpel_filter = 1;
                    if (msac_decode_bool_adapt(&ts->msac,
                                       ts->cdf.m.refmv_mode[(ctx >> 4) & 15]))
                    {
                        b->inter_mode = NEARMV;
                        b->drl_idx = 1;
                        if (n_mvs > 2) {
                            const int drl_ctx_v2 = get_drl_context(mvstack, 1);
                            b->drl_idx += msac_decode_bool_adapt(&ts->msac,
                                                 ts->cdf.m.drl_bit[drl_ctx_v2]);
                            if (b->drl_idx == 2 && n_mvs > 3) {
                                const int drl_ctx_v3 =
                                    get_drl_context(mvstack, 2);
                                b->drl_idx += msac_decode_bool_adapt(&ts->msac,
                                                 ts->cdf.m.drl_bit[drl_ctx_v3]);
                            }
                        }
                    } else {
                        b->inter_mode = NEARESTMV;
                        b->drl_idx = 0;
                    }
                    if (b->drl_idx >= 2) {
                        b->mv[0] = mvstack[b->drl_idx].this_mv;
                    } else {
                        b->mv[0] = mvlist[0][b->drl_idx];
                        if (!f->frame_hdr.hp) unset_hp_bit(&b->mv[0]);
                    }
                }

                if (DEBUG_BLOCK_INFO)
                    printf("Post-intermode[%d,drl=%d,mv=y:%d,x:%d,n_mvs=%d]: r=%d\n",
                           b->inter_mode, b->drl_idx, b->mv[0].y, b->mv[0].x, n_mvs,
                           ts->msac.rng);
            } else {
                has_subpel_filter = 1;
                b->inter_mode = NEWMV;
                b->drl_idx = 0;
                if (n_mvs > 1) {
                    const int drl_ctx_v1 = get_drl_context(mvstack, 0);
                    b->drl_idx += msac_decode_bool_adapt(&ts->msac,
                                                 ts->cdf.m.drl_bit[drl_ctx_v1]);
                    if (b->drl_idx == 1 && n_mvs > 2) {
                        const int drl_ctx_v2 = get_drl_context(mvstack, 1);
                        b->drl_idx += msac_decode_bool_adapt(&ts->msac,
                                                 ts->cdf.m.drl_bit[drl_ctx_v2]);
                    }
                }
                if (n_mvs > 1) {
                    b->mv[0] = mvstack[b->drl_idx].this_mv;
                } else {
                    b->mv[0] = mvlist[0][0];
                    if (!f->frame_hdr.hp) unset_hp_bit(&b->mv[0]);
                }
                if (DEBUG_BLOCK_INFO)
                    printf("Post-intermode[%d,drl=%d]: r=%d\n",
                           b->inter_mode, b->drl_idx, ts->msac.rng);
                read_mv_residual(t, &b->mv[0], &ts->cdf.mv,
                                 !f->frame_hdr.force_integer_mv);
                if (DEBUG_BLOCK_INFO)
                    printf("Post-residualmv[mv=y:%d,x:%d]: r=%d\n",
                           b->mv[0].y, b->mv[0].x, ts->msac.rng);
            }

            // interintra flags
1573
            const int ii_sz_grp = dav1d_ymode_size_context[bs];
1574
1575
1576
1577
1578
1579
1580
            if (f->seq_hdr.inter_intra &&
                interintra_allowed_mask & (1 << bs) &&
                msac_decode_bool_adapt(&ts->msac, ts->cdf.m.interintra[ii_sz_grp]))
            {
                b->interintra_mode = msac_decode_symbol_adapt(&ts->msac,
                                          ts->cdf.m.interintra_mode[ii_sz_grp],
                                          N_INTER_INTRA_PRED_MODES);
1581
                const int wedge_ctx = dav1d_wedge_ctx_lut[bs];
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
                b->interintra_type = INTER_INTRA_BLEND +
                    msac_decode_bool_adapt(&ts->msac,
                                           ts->cdf.m.interintra_wedge[wedge_ctx]);
                if (b->interintra_type == INTER_INTRA_WEDGE)
                    b->wedge_idx = msac_decode_symbol_adapt(&ts->msac,
                                            ts->cdf.m.wedge_idx[wedge_ctx], 16);
            } else {
                b->interintra_type = INTER_INTRA_NONE;
            }
            if (DEBUG_BLOCK_INFO && f->seq_hdr.inter_intra &&
                interintra_allowed_mask & (1 << bs))
            {
                printf("Post-interintra[t=%d,m=%d,w=%d]: r=%d\n",
                       b->interintra_type, b->interintra_mode,
                       b->wedge_idx, ts->msac.rng);
            }

            // motion variation
            if (f->frame_hdr.switchable_motion_mode &&
                b->interintra_type == INTER_INTRA_NONE && imin(bw4, bh4) >= 2 &&
                // is not warped global motion
                !(!f->frame_hdr.force_integer_mv && b->inter_mode == GLOBALMV &&
                  f->frame_hdr.gmv[b->ref[0]].type > WM_TYPE_TRANSLATION) &&
                // has overlappable neighbours
                ((have_left && findoddzero(&t->l.intra[by4 + 1], h4 >> 1)) ||
                 (have_top && findoddzero(&t->a->intra[bx4 + 1], w4 >> 1))))
            {
                // reaching here means the block allows obmc - check warp by
                // finding matching-ref blocks in top/left edges
                uint64_t mask[2] = { 0, 0 };
                find_matching_ref(t, intra_edge_flags, bw4, bh4, w4, h4,
                                  have_left, have_top, b->ref[0], mask);
                const int allow_warp = !f->frame_hdr.force_integer_mv &&
                    f->frame_hdr.warp_motion && (mask[0] | mask[1]);

                b->motion_mode = allow_warp ?
                    msac_decode_symbol_adapt(&ts->msac, ts->cdf.m.motion_mode[bs], 3) :
                    msac_decode_bool_adapt(&ts->msac, ts->cdf.m.obmc[bs]);
                if (b->motion_mode == MM_WARP) {
                    has_subpel_filter = 0;
                    derive_warpmv(t, bw4, bh4, mask, b->mv[0], &t->warpmv);
#define signabs(v) v < 0 ? '-' : ' ', abs(v)
                    if (DEBUG_BLOCK_INFO)
                        printf("[ %c%x %c%x %c%x\n  %c%x %c%x %c%x ]\n"
                               "alpha=%c%x, beta=%c%x, gamma=%c%x, delta=%c%x\n",
                               signabs(t->warpmv.matrix[0]),
                               signabs(t->warpmv.matrix[1]),
                               signabs(t->warpmv.matrix[2]),
                               signabs(t->warpmv.matrix[3]),
                               signabs(t->warpmv.matrix[4]),
                               signabs(t->warpmv.matrix[5]),
                               signabs(t->warpmv.alpha),
                               signabs(t->warpmv.beta),
                               signabs(t->warpmv.gamma),
                               signabs(t->warpmv.delta));
#undef signabs
                }

                if (DEBUG_BLOCK_INFO)
                    printf("Post-motionmode[%d]: r=%d [mask: 0x%" PRIu64 "x/0x%"
                           PRIu64 "x]\n", b->motion_mode, ts->msac.rng, mask[0],
                            mask[1]);
            } else {
                b->motion_mode = MM_TRANSLATION;
            }
        }

        // subpel filter
        enum FilterMode filter[2];
        if (f->frame_hdr.subpel_filter_mode == FILTER_SWITCHABLE) {
            if (has_subpel_filter) {
                const int comp = b->comp_type != COMP_INTER_NONE;
                const int ctx1 = get_filter_ctx(t->a, &t->l, comp, 0, b->ref[0],
                                                by4, bx4);
                filter[0] = msac_decode_symbol_adapt(&ts->msac,
                    ts->cdf.m.filter[0][ctx1], N_SWITCHABLE_FILTERS);
                if (f->seq_hdr.dual_filter) {
                    const int ctx2 = get_filter_ctx(t->a, &t->l, comp, 1,
                                                    b->ref[0], by4, bx4);
                    if (DEBUG_BLOCK_INFO)
                        printf("Post-subpel_filter1[%d,ctx=%d]: r=%d\n",
                               filter[0], ctx1, ts->msac.rng);
                    filter[1] = msac_decode_symbol_adapt(&ts->msac,
                        ts->cdf.m.filter[1][ctx2], N_SWITCHABLE_FILTERS);
                    if (DEBUG_BLOCK_INFO)
                        printf("Post-subpel_filter2[%d,ctx=%d]: r=%d\n",
                               filter[1], ctx2, ts->msac.rng);
                } else {
                    filter[1] = filter[0];
                    if (DEBUG_BLOCK_INFO)
                        printf("Post-subpel_filter[%d,ctx=%d]: r=%d\n",
                               filter[0], ctx1, ts->msac.rng);
                }
            } else {
                filter[0] = filter[1] = FILTER_8TAP_REGULAR;
            }
        } else {
            filter[0] = filter[1] = f->frame_hdr.subpel_filter_mode;
        }
1681
        b->filter2d = dav1d_filter_2d[filter[1]][filter[0]];
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697

        read_vartx_tree(t, b, bs, bx4, by4);

        // reconstruction
        if (f->frame_thread.pass == 1) {
            f->bd_fn.read_coef_blocks(t, bs, b);
        } else {
            f->bd_fn.recon_b_inter(t, bs, b);
        }

        const int is_globalmv =
            b->inter_mode == (is_comp ? GLOBALMV_GLOBALMV : GLOBALMV);
        const uint8_t (*const lf_lvls)[8][2] = (const uint8_t (*)[8][2])
            &ts->lflvl[b->seg_id][0][b->ref[0] + 1][!is_globalmv];
        dav1d_create_lf_mask_inter(t->lf_mask, f->lf.level, f->b4_stride,
                                   &f->frame_hdr, lf_lvls, t->bx, t->by,
1698
1699
1700
                                   (f->cur.p.p.w + 3) >> 2,
                                   (f->cur.p.p.h + 3) >> 2,
                                   b->skip, bs, b->tx_split,
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
                                   b->uvtx, f->cur.p.p.layout,
                                   &t->a->tx_lpf_y[bx4], &t->l.tx_lpf_y[by4],
                                   has_chroma ? &t->a->tx_lpf_uv[cbx4] : NULL,
                                   has_chroma ? &t->l.tx_lpf_uv[cby4] : NULL);

        // context updates
        if (is_comp) {
            splat_tworef_mv(f->mvs, f->b4_stride, t->by, t->bx, bs,
                            b->inter_mode, b->ref[0], b->ref[1],
                            b->mv[0], b->mv[1]);
        } else {
            splat_oneref_mv(f->mvs, f->b4_stride, t->by, t->bx, bs,
                            b->inter_mode, b->ref[0], b->mv[0],
                            b->interintra_type);
        }
        memset(&t->l.pal_sz[by4], 0, bh4);
        memset(&t->a->pal_sz[bx4], 0, bw4);
1718
1719
1720
        // see aomedia bug 2183 for why this is outside if (has_chroma)
        memset(&t->pal_sz_uv[1][by4], 0, bh4);
        memset(&t->pal_sz_uv[0][bx4], 0, bw4);
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
        if (has_chroma) {
            memset(&t->l.uvmode[cby4], DC_PRED, cbh4);
            memset(&t->a->uvmode[cbx4], DC_PRED, cbw4);
        }
        memset(&t->a->tx_intra[bx4], b_dim[2], bw4);
        memset(&t->l.tx_intra[by4], b_dim[3], bh4);
        memset(&t->l.comp_type[by4], b->comp_type, bh4);
        memset(&t->a->comp_type[bx4], b->comp_type, bw4);
        memset(&t->l.filter[0][by4], filter[0], bh4);
        memset(&t->a->filter[0][bx4], filter[0], bw4);
        memset(&t->l.filter[1][by4], filter[1], bh4);
        memset(&t->a->filter[1][bx4], filter[1], bw4);
        memset(&t->l.mode[by4], b->inter_mode, bh4);
        memset(&t->a->mode[bx4], b->inter_mode, bw4);
        memset(&t->l.ref[0][by4], b->ref[0], bh4);
        memset(&t->a->ref[0][bx4], b->ref[0], bw4);
        memset(&t->l.ref[1][by4], b->ref[1], bh4);
        memset(&t->a->ref[1][bx4], b->ref[1], bw4);
    }

    // update contexts
    if (f->frame_hdr.segmentation.enabled &&
        f->frame_hdr.segmentation.update_map)
    {
        uint8_t *seg_ptr = &f->cur_segmap[t->by * f->b4_stride + t->bx];
        for (int y = 0; y < bh4; y++) {
            memset(seg_ptr, b->seg_id, bw4);
            seg_ptr += f->b4_stride;
        }
    }
    memset(&t->l.seg_pred[by4], seg_pred, bh4);
    memset(&t->a->seg_pred[bx4], seg_pred, bw4);
    memset(&t->l.skip_mode[by4], b->skip_mode, bh4);
    memset(&t->a->skip_mode[bx4], b->skip_mode, bw4);
    memset(&t->l.intra[by4], b->intra, bh4);
    memset(&t->a->intra[bx4], b->intra, bw4);
    memset(&t->l.skip[by4], b->skip, bh4);
    memset(&t->a->skip[bx4], b->skip, bw4);
    if (!b->skip) {
        uint32_t *noskip_mask = &t->lf_mask->noskip_mask[by4];
        const unsigned mask = ((1ULL << bw4) - 1) << bx4;
        for (int y = 0; y < bh4; y++)
            *noskip_mask++ |= mask;
    }
}

static int decode_sb(Dav1dTileContext *const t, const enum BlockLevel bl,
                     const EdgeNode *const node)
{
    const Dav1dFrameContext *const f = t->f;
    const int hsz = 16 >> bl;
    const int have_h_split = f->bw > t->bx + hsz;
    const int have_v_split = f->bh > t->by + hsz;

    if (!have_h_split && !have_v_split) {
        assert(bl < BL_8X8);
        return decode_sb(t, bl + 1, ((const EdgeBranch *) node)->split[0]);
    }

    uint16_t *pc;
    enum BlockPartition bp;
    int ctx, bx8, by8;
    if (f->frame_thread.pass != 2) {
        if (0 && bl == BL_64X64)
            printf("poc=%d,y=%d,x=%d,bl=%d,r=%d\n",
                   f->frame_hdr.frame_offset, t->by, t->bx, bl, t->ts->msac.rng);
        bx8 = (t->bx & 31) >> 1;
        by8 = (t->by & 31) >> 1;
        ctx = get_partition_ctx(t->a, &t->l, bl, by8, bx8);
        pc = t->ts->cdf.m.partition[bl][ctx];
    }

    if (have_h_split && have_v_split) {
        if (f->frame_thread.pass == 2) {
            const Av1Block *const b = &f->frame_thread.b[t->by * f->b4_stride + t->bx];
            bp = b->bl == bl ? b->bp : PARTITION_SPLIT;
        } else {
            const unsigned n_part = bl == BL_8X8 ? N_SUB8X8_PARTITIONS :
                bl == BL_128X128 ? N_PARTITIONS - 2 : N_PARTITIONS;
            bp = msac_decode_symbol_adapt(&t->ts->msac, pc, n_part);
            if (f->cur.p.p.layout == DAV1D_PIXEL_LAYOUT_I422 &&
                (bp == PARTITION_V || bp == PARTITION_V4 ||
                 bp == PARTITION_T_LEFT_SPLIT || bp == PARTITION_T_RIGHT_SPLIT))
            {
                return 1;
            }
            if (DEBUG_BLOCK_INFO)
                printf("poc=%d,y=%d,x=%d,bl=%d,ctx=%d,bp=%d: r=%d\n",
                       f->frame_hdr.frame_offset, t->by, t->bx, bl, ctx, bp,
                       t->ts->msac.rng);
        }
1812
        const uint8_t *const b = dav1d_block_sizes[bl][bp];
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955

        switch (bp) {
        case PARTITION_NONE:
            decode_b(t, bl, b[0], PARTITION_NONE, node->o);
            break;
        case PARTITION_H:
            decode_b(t, bl, b[0], PARTITION_H, node->h[0]);
            t->by += hsz;
            decode_b(t, bl, b[0], PARTITION_H, node->h[1]);
            t->by -= hsz;
            break;
        case PARTITION_V:
            decode_b(t, bl, b[0], PARTITION_V, node->v[0]);
            t->bx += hsz;
            decode_b(t, bl, b[0], PARTITION_V, node->v[1]);
            t->bx -= hsz;
            break;
        case PARTITION_SPLIT:
            if (bl == BL_8X8) {
                const EdgeTip *const tip = (const EdgeTip *) node;
                assert(hsz == 1);
                decode_b(t, bl, BS_4x4, PARTITION_SPLIT, tip->split[0]);
                const enum Filter2d tl_filter = t->tl_4x4_filter;
                t->bx++;
                decode_b(t, bl, BS_4x4, PARTITION_SPLIT, tip->split[1]);
                t->bx--;
                t->by++;
                decode_b(t, bl, BS_4x4, PARTITION_SPLIT, tip->split[2]);
                t->bx++;
                t->tl_4x4_filter = tl_filter;
                decode_b(t, bl, BS_4x4