Coverage Report

Created: 2026-05-16 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/recon_tmpl.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 <string.h>
31
#include <stdio.h>
32
33
#include "common/attributes.h"
34
#include "common/bitdepth.h"
35
#include "common/dump.h"
36
#include "common/frame.h"
37
#include "common/intops.h"
38
39
#include "src/cdef_apply.h"
40
#include "src/ctx.h"
41
#include "src/ipred_prepare.h"
42
#include "src/lf_apply.h"
43
#include "src/lr_apply.h"
44
#include "src/recon.h"
45
#include "src/scan.h"
46
#include "src/tables.h"
47
#include "src/wedge.h"
48
49
0
static inline unsigned read_golomb(MsacContext *const msac) {
50
0
    int len = 0;
51
0
    unsigned val = 1;
52
53
0
    while (!dav1d_msac_decode_bool_equi(msac) && len < 32) len++;
54
0
    while (len--) val = (val << 1) + dav1d_msac_decode_bool_equi(msac);
55
56
0
    return val - 1;
57
0
}
58
59
static inline unsigned get_skip_ctx(const TxfmInfo *const t_dim,
60
                                    const enum BlockSize bs,
61
                                    const uint8_t *const a,
62
                                    const uint8_t *const l,
63
                                    const int chroma,
64
                                    const enum Dav1dPixelLayout layout)
65
0
{
66
0
    const uint8_t *const b_dim = dav1d_block_dimensions[bs];
67
68
0
    if (chroma) {
69
0
        const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
70
0
        const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
71
0
        const int not_one_blk = b_dim[2] - (!!b_dim[2] && ss_hor) > t_dim->lw ||
72
0
                                b_dim[3] - (!!b_dim[3] && ss_ver) > t_dim->lh;
73
0
        unsigned ca, cl;
74
75
0
#define MERGE_CTX(dir, type, no_val) \
76
0
        c##dir = *(const type *) dir != no_val; \
77
0
        break
78
79
0
        switch (t_dim->lw) {
80
        /* For some reason the MSVC CRT _wassert() function is not flagged as
81
         * __declspec(noreturn), so when using those headers the compiler will
82
         * expect execution to continue after an assertion has been triggered
83
         * and will therefore complain about the use of uninitialized variables
84
         * when compiled in debug mode if we put the default case at the end. */
85
0
        default: assert(0); /* fall-through */
86
0
        case TX_4X4:   MERGE_CTX(a, uint8_t,  0x40);
87
0
        case TX_8X8:   MERGE_CTX(a, uint16_t, 0x4040);
88
0
        case TX_16X16: MERGE_CTX(a, uint32_t, 0x40404040U);
89
0
        case TX_32X32: MERGE_CTX(a, uint64_t, 0x4040404040404040ULL);
90
0
        }
91
0
        switch (t_dim->lh) {
92
0
        default: assert(0); /* fall-through */
93
0
        case TX_4X4:   MERGE_CTX(l, uint8_t,  0x40);
94
0
        case TX_8X8:   MERGE_CTX(l, uint16_t, 0x4040);
95
0
        case TX_16X16: MERGE_CTX(l, uint32_t, 0x40404040U);
96
0
        case TX_32X32: MERGE_CTX(l, uint64_t, 0x4040404040404040ULL);
97
0
        }
98
0
#undef MERGE_CTX
99
100
0
        return 7 + not_one_blk * 3 + ca + cl;
101
0
    } else if (b_dim[2] == t_dim->lw && b_dim[3] == t_dim->lh) {
102
0
        return 0;
103
0
    } else {
104
0
        unsigned la, ll;
105
106
0
#define MERGE_CTX(dir, type, tx) \
107
0
        if (tx == TX_64X64) { \
108
0
            uint64_t tmp = *(const uint64_t *) dir; \
109
0
            tmp |= *(const uint64_t *) &dir[8]; \
110
0
            l##dir = (unsigned) (tmp >> 32) | (unsigned) tmp; \
111
0
        } else \
112
0
            l##dir = *(const type *) dir; \
113
0
        if (tx == TX_32X32) l##dir |= *(const type *) &dir[sizeof(type)]; \
114
0
        if (tx >= TX_16X16) l##dir |= l##dir >> 16; \
115
0
        if (tx >= TX_8X8)   l##dir |= l##dir >> 8; \
116
0
        break
117
118
0
        switch (t_dim->lw) {
119
0
        default: assert(0); /* fall-through */
120
0
        case TX_4X4:   MERGE_CTX(a, uint8_t,  TX_4X4);
121
0
        case TX_8X8:   MERGE_CTX(a, uint16_t, TX_8X8);
122
0
        case TX_16X16: MERGE_CTX(a, uint32_t, TX_16X16);
123
0
        case TX_32X32: MERGE_CTX(a, uint32_t, TX_32X32);
124
0
        case TX_64X64: MERGE_CTX(a, uint32_t, TX_64X64);
125
0
        }
126
0
        switch (t_dim->lh) {
127
0
        default: assert(0); /* fall-through */
128
0
        case TX_4X4:   MERGE_CTX(l, uint8_t,  TX_4X4);
129
0
        case TX_8X8:   MERGE_CTX(l, uint16_t, TX_8X8);
130
0
        case TX_16X16: MERGE_CTX(l, uint32_t, TX_16X16);
131
0
        case TX_32X32: MERGE_CTX(l, uint32_t, TX_32X32);
132
0
        case TX_64X64: MERGE_CTX(l, uint32_t, TX_64X64);
133
0
        }
134
0
#undef MERGE_CTX
135
136
0
        return dav1d_skip_ctx[umin(la & 0x3F, 4)][umin(ll & 0x3F, 4)];
137
0
    }
138
0
}
139
140
static inline unsigned get_dc_sign_ctx(const int /*enum RectTxfmSize*/ tx,
141
                                       const uint8_t *const a,
142
                                       const uint8_t *const l)
143
0
{
144
0
    uint64_t mask = 0xC0C0C0C0C0C0C0C0ULL, mul = 0x0101010101010101ULL;
145
0
    int s;
146
147
0
#if ARCH_X86_64 && defined(__GNUC__)
148
    /* Coerce compilers into producing better code. For some reason
149
     * every x86-64 compiler is awful at handling 64-bit constants. */
150
0
    __asm__("" : "+r"(mask), "+r"(mul));
151
0
#endif
152
153
0
    switch(tx) {
154
0
    default: assert(0); /* fall-through */
155
0
    case TX_4X4: {
156
0
        int t = *(const uint8_t *) a >> 6;
157
0
        t    += *(const uint8_t *) l >> 6;
158
0
        s = t - 1 - 1;
159
0
        break;
160
0
    }
161
0
    case TX_8X8: {
162
0
        uint32_t t = *(const uint16_t *) a & (uint32_t) mask;
163
0
        t         += *(const uint16_t *) l & (uint32_t) mask;
164
0
        t *= 0x04040404U;
165
0
        s = (int) (t >> 24) - 2 - 2;
166
0
        break;
167
0
    }
168
0
    case TX_16X16: {
169
0
        uint32_t t = (*(const uint32_t *) a & (uint32_t) mask) >> 6;
170
0
        t         += (*(const uint32_t *) l & (uint32_t) mask) >> 6;
171
0
        t *= (uint32_t) mul;
172
0
        s = (int) (t >> 24) - 4 - 4;
173
0
        break;
174
0
    }
175
0
    case TX_32X32: {
176
0
        uint64_t t = (*(const uint64_t *) a & mask) >> 6;
177
0
        t         += (*(const uint64_t *) l & mask) >> 6;
178
0
        t *= mul;
179
0
        s = (int) (t >> 56) - 8 - 8;
180
0
        break;
181
0
    }
182
0
    case TX_64X64: {
183
0
        uint64_t t = (*(const uint64_t *) &a[0] & mask) >> 6;
184
0
        t         += (*(const uint64_t *) &a[8] & mask) >> 6;
185
0
        t         += (*(const uint64_t *) &l[0] & mask) >> 6;
186
0
        t         += (*(const uint64_t *) &l[8] & mask) >> 6;
187
0
        t *= mul;
188
0
        s = (int) (t >> 56) - 16 - 16;
189
0
        break;
190
0
    }
191
0
    case RTX_4X8: {
192
0
        uint32_t t = *(const uint8_t  *) a & (uint32_t) mask;
193
0
        t         += *(const uint16_t *) l & (uint32_t) mask;
194
0
        t *= 0x04040404U;
195
0
        s = (int) (t >> 24) - 1 - 2;
196
0
        break;
197
0
    }
198
0
    case RTX_8X4: {
199
0
        uint32_t t = *(const uint16_t *) a & (uint32_t) mask;
200
0
        t         += *(const uint8_t  *) l & (uint32_t) mask;
201
0
        t *= 0x04040404U;
202
0
        s = (int) (t >> 24) - 2 - 1;
203
0
        break;
204
0
    }
205
0
    case RTX_8X16: {
206
0
        uint32_t t = *(const uint16_t *) a & (uint32_t) mask;
207
0
        t         += *(const uint32_t *) l & (uint32_t) mask;
208
0
        t = (t >> 6) * (uint32_t) mul;
209
0
        s = (int) (t >> 24) - 2 - 4;
210
0
        break;
211
0
    }
212
0
    case RTX_16X8: {
213
0
        uint32_t t = *(const uint32_t *) a & (uint32_t) mask;
214
0
        t         += *(const uint16_t *) l & (uint32_t) mask;
215
0
        t = (t >> 6) * (uint32_t) mul;
216
0
        s = (int) (t >> 24) - 4 - 2;
217
0
        break;
218
0
    }
219
0
    case RTX_16X32: {
220
0
        uint64_t t = *(const uint32_t *) a & (uint32_t) mask;
221
0
        t         += *(const uint64_t *) l & mask;
222
0
        t = (t >> 6) * mul;
223
0
        s = (int) (t >> 56) - 4 - 8;
224
0
        break;
225
0
    }
226
0
    case RTX_32X16: {
227
0
        uint64_t t = *(const uint64_t *) a & mask;
228
0
        t         += *(const uint32_t *) l & (uint32_t) mask;
229
0
        t = (t >> 6) * mul;
230
0
        s = (int) (t >> 56) - 8 - 4;
231
0
        break;
232
0
    }
233
0
    case RTX_32X64: {
234
0
        uint64_t t = (*(const uint64_t *) &a[0] & mask) >> 6;
235
0
        t         += (*(const uint64_t *) &l[0] & mask) >> 6;
236
0
        t         += (*(const uint64_t *) &l[8] & mask) >> 6;
237
0
        t *= mul;
238
0
        s = (int) (t >> 56) - 8 - 16;
239
0
        break;
240
0
    }
241
0
    case RTX_64X32: {
242
0
        uint64_t t = (*(const uint64_t *) &a[0] & mask) >> 6;
243
0
        t         += (*(const uint64_t *) &a[8] & mask) >> 6;
244
0
        t         += (*(const uint64_t *) &l[0] & mask) >> 6;
245
0
        t *= mul;
246
0
        s = (int) (t >> 56) - 16 - 8;
247
0
        break;
248
0
    }
249
0
    case RTX_4X16: {
250
0
        uint32_t t = *(const uint8_t  *) a & (uint32_t) mask;
251
0
        t         += *(const uint32_t *) l & (uint32_t) mask;
252
0
        t = (t >> 6) * (uint32_t) mul;
253
0
        s = (int) (t >> 24) - 1 - 4;
254
0
        break;
255
0
    }
256
0
    case RTX_16X4: {
257
0
        uint32_t t = *(const uint32_t *) a & (uint32_t) mask;
258
0
        t         += *(const uint8_t  *) l & (uint32_t) mask;
259
0
        t = (t >> 6) * (uint32_t) mul;
260
0
        s = (int) (t >> 24) - 4 - 1;
261
0
        break;
262
0
    }
263
0
    case RTX_8X32: {
264
0
        uint64_t t = *(const uint16_t *) a & (uint32_t) mask;
265
0
        t         += *(const uint64_t *) l & mask;
266
0
        t = (t >> 6) * mul;
267
0
        s = (int) (t >> 56) - 2 - 8;
268
0
        break;
269
0
    }
270
0
    case RTX_32X8: {
271
0
        uint64_t t = *(const uint64_t *) a & mask;
272
0
        t         += *(const uint16_t *) l & (uint32_t) mask;
273
0
        t = (t >> 6) * mul;
274
0
        s = (int) (t >> 56) - 8 - 2;
275
0
        break;
276
0
    }
277
0
    case RTX_16X64: {
278
0
        uint64_t t = *(const uint32_t *) a & (uint32_t) mask;
279
0
        t         += *(const uint64_t *) &l[0] & mask;
280
0
        t = (t >> 6) + ((*(const uint64_t *) &l[8] & mask) >> 6);
281
0
        t *= mul;
282
0
        s = (int) (t >> 56) - 4 - 16;
283
0
        break;
284
0
    }
285
0
    case RTX_64X16: {
286
0
        uint64_t t = *(const uint64_t *) &a[0] & mask;
287
0
        t         += *(const uint32_t *) l & (uint32_t) mask;
288
0
        t = (t >> 6) + ((*(const uint64_t *) &a[8] & mask) >> 6);
289
0
        t *= mul;
290
0
        s = (int) (t >> 56) - 16 - 4;
291
0
        break;
292
0
    }
293
0
    }
294
295
0
    return (s != 0) + (s > 0);
296
0
}
297
298
static inline unsigned get_lo_ctx(const uint8_t *const levels,
299
                                  const enum TxClass tx_class,
300
                                  unsigned *const hi_mag,
301
                                  const uint8_t (*const ctx_offsets)[5],
302
                                  const unsigned x, const unsigned y,
303
                                  const ptrdiff_t stride)
304
0
{
305
0
    unsigned mag = levels[0 * stride + 1] + levels[1 * stride + 0];
306
0
    unsigned offset;
307
0
    if (tx_class == TX_CLASS_2D) {
308
0
        mag += levels[1 * stride + 1];
309
0
        *hi_mag = mag;
310
0
        mag += levels[0 * stride + 2] + levels[2 * stride + 0];
311
0
        offset = ctx_offsets[umin(y, 4)][umin(x, 4)];
312
0
    } else {
313
0
        mag += levels[0 * stride + 2];
314
0
        *hi_mag = mag;
315
0
        mag += levels[0 * stride + 3] + levels[0 * stride + 4];
316
0
        offset = 26 + (y > 1 ? 10 : y * 5);
317
0
    }
318
0
    return offset + (mag > 512 ? 4 : (mag + 64) >> 7);
319
0
}
320
321
static int decode_coefs(Dav1dTaskContext *const t,
322
                        uint8_t *const a, uint8_t *const l,
323
                        const enum RectTxfmSize tx, const enum BlockSize bs,
324
                        const Av1Block *const b, const int intra,
325
                        const int plane, coef *cf,
326
                        enum TxfmType *const txtp, uint8_t *res_ctx)
327
0
{
328
0
    Dav1dTileState *const ts = t->ts;
329
0
    const int chroma = !!plane;
330
0
    const Dav1dFrameContext *const f = t->f;
331
0
    const int lossless = f->frame_hdr->segmentation.lossless[b->seg_id];
332
0
    const TxfmInfo *const t_dim = &dav1d_txfm_dimensions[tx];
333
0
    const int dbg = DEBUG_BLOCK_INFO && plane && 0;
334
335
0
    if (dbg)
336
0
        printf("Start: r=%d\n", ts->msac.rng);
337
338
    // does this block have any non-zero coefficients
339
0
    const int sctx = get_skip_ctx(t_dim, bs, a, l, chroma, f->cur.p.layout);
340
0
    const int all_skip = dav1d_msac_decode_bool_adapt(&ts->msac,
341
0
                             ts->cdf.coef.skip[t_dim->ctx][sctx]);
342
0
    if (dbg)
343
0
        printf("Post-non-zero[%d][%d][%d]: r=%d\n",
344
0
               t_dim->ctx, sctx, all_skip, ts->msac.rng);
345
0
    if (all_skip) {
346
0
        *res_ctx = 0x40;
347
0
        *txtp = lossless * WHT_WHT; /* lossless ? WHT_WHT : DCT_DCT */
348
0
        return -1;
349
0
    }
350
351
    // transform type (chroma: derived, luma: explicitly coded)
352
0
    if (lossless) {
353
0
        assert(t_dim->max == TX_4X4);
354
0
        *txtp = WHT_WHT;
355
0
    } else if (t_dim->max + intra >= TX_64X64) {
356
0
        *txtp = DCT_DCT;
357
0
    } else if (chroma) {
358
        // inferred from either the luma txtp (inter) or a LUT (intra)
359
0
        *txtp = intra ? dav1d_txtp_from_uvmode[b->uv_mode] :
360
0
                        get_uv_inter_txtp(t_dim, *txtp);
361
0
    } else if (!f->frame_hdr->segmentation.qidx[b->seg_id]) {
362
        // In libaom, lossless is checked by a literal qidx == 0, but not all
363
        // such blocks are actually lossless. The remainder gets an implicit
364
        // transform type (for luma)
365
0
        *txtp = DCT_DCT;
366
0
    } else {
367
0
        unsigned idx;
368
0
        if (intra) {
369
0
            const enum IntraPredMode y_mode_nofilt = b->y_mode == FILTER_PRED ?
370
0
                dav1d_filter_mode_to_y_mode[b->y_angle] : b->y_mode;
371
0
            if (f->frame_hdr->reduced_txtp_set || t_dim->min == TX_16X16) {
372
0
                idx = dav1d_msac_decode_symbol_adapt8(&ts->msac,
373
0
                          ts->cdf.m.txtp_intra2[t_dim->min][y_mode_nofilt], 4);
374
0
                *txtp = dav1d_tx_types_per_set[idx + 0];
375
0
            } else {
376
0
                idx = dav1d_msac_decode_symbol_adapt8(&ts->msac,
377
0
                          ts->cdf.m.txtp_intra1[t_dim->min][y_mode_nofilt], 6);
378
0
                *txtp = dav1d_tx_types_per_set[idx + 5];
379
0
            }
380
0
            if (dbg)
381
0
                printf("Post-txtp-intra[%d->%d][%d][%d->%d]: r=%d\n",
382
0
                       tx, t_dim->min, y_mode_nofilt, idx, *txtp, ts->msac.rng);
383
0
        } else {
384
0
            if (f->frame_hdr->reduced_txtp_set || t_dim->max == TX_32X32) {
385
0
                idx = dav1d_msac_decode_bool_adapt(&ts->msac,
386
0
                          ts->cdf.m.txtp_inter3[t_dim->min]);
387
0
                *txtp = (idx - 1) & IDTX; /* idx ? DCT_DCT : IDTX */
388
0
            } else if (t_dim->min == TX_16X16) {
389
0
                idx = dav1d_msac_decode_symbol_adapt16(&ts->msac,
390
0
                          ts->cdf.m.txtp_inter2, 11);
391
0
                *txtp = dav1d_tx_types_per_set[idx + 12];
392
0
            } else {
393
0
                idx = dav1d_msac_decode_symbol_adapt16(&ts->msac,
394
0
                          ts->cdf.m.txtp_inter1[t_dim->min], 15);
395
0
                *txtp = dav1d_tx_types_per_set[idx + 24];
396
0
            }
397
0
            if (dbg)
398
0
                printf("Post-txtp-inter[%d->%d][%d->%d]: r=%d\n",
399
0
                       tx, t_dim->min, idx, *txtp, ts->msac.rng);
400
0
        }
401
0
    }
402
403
    // find end-of-block (eob)
404
0
    int eob;
405
0
    const int slw = imin(t_dim->lw, TX_32X32), slh = imin(t_dim->lh, TX_32X32);
406
0
    const int tx2dszctx = slw + slh;
407
0
    const enum TxClass tx_class = dav1d_tx_type_class[*txtp];
408
0
    const int is_1d = tx_class != TX_CLASS_2D;
409
0
    switch (tx2dszctx) {
410
0
#define case_sz(sz, bin, ns, is_1d) \
411
0
    case sz: { \
412
0
        uint16_t *const eob_bin_cdf = ts->cdf.coef.eob_bin_##bin[chroma]is_1d; \
413
0
        eob = dav1d_msac_decode_symbol_adapt##ns(&ts->msac, eob_bin_cdf, 4 + sz); \
414
0
        break; \
415
0
    }
416
0
    case_sz(0,   16,  8, [is_1d]);
417
0
    case_sz(1,   32,  8, [is_1d]);
418
0
    case_sz(2,   64,  8, [is_1d]);
419
0
    case_sz(3,  128,  8, [is_1d]);
420
0
    case_sz(4,  256, 16, [is_1d]);
421
0
    case_sz(5,  512, 16,        );
422
0
    case_sz(6, 1024, 16,        );
423
0
#undef case_sz
424
0
    }
425
0
    if (dbg)
426
0
        printf("Post-eob_bin_%d[%d][%d][%d]: r=%d\n",
427
0
               16 << tx2dszctx, chroma, is_1d, eob, ts->msac.rng);
428
0
    if (eob > 1) {
429
0
        const int eob_bin = eob - 2;
430
0
        uint16_t *const eob_hi_bit_cdf =
431
0
            ts->cdf.coef.eob_hi_bit[t_dim->ctx][chroma][eob_bin];
432
0
        const int eob_hi_bit = dav1d_msac_decode_bool_adapt(&ts->msac, eob_hi_bit_cdf);
433
0
        if (dbg)
434
0
            printf("Post-eob_hi_bit[%d][%d][%d][%d]: r=%d\n",
435
0
                   t_dim->ctx, chroma, eob_bin, eob_hi_bit, ts->msac.rng);
436
0
        eob = ((eob_hi_bit | 2) << eob_bin) | dav1d_msac_decode_bools(&ts->msac, eob_bin);
437
0
        if (dbg)
438
0
            printf("Post-eob[%d]: r=%d\n", eob, ts->msac.rng);
439
0
    }
440
0
    assert(eob >= 0);
441
442
    // base tokens
443
0
    uint16_t (*const eob_cdf)[4] = ts->cdf.coef.eob_base_tok[t_dim->ctx][chroma];
444
0
    uint16_t (*const hi_cdf)[4] = ts->cdf.coef.br_tok[imin(t_dim->ctx, 3)][chroma];
445
0
    unsigned rc, dc_tok;
446
447
0
    if (eob) {
448
0
        uint16_t (*const lo_cdf)[4] = ts->cdf.coef.base_tok[t_dim->ctx][chroma];
449
0
        uint8_t *const levels = t->scratch.levels; // bits 0-5: tok, 6-7: lo_tok
450
451
        /* eob */
452
0
        unsigned ctx = 1 + (eob > 2 << tx2dszctx) + (eob > 4 << tx2dszctx);
453
0
        int eob_tok = dav1d_msac_decode_symbol_adapt4(&ts->msac, eob_cdf[ctx], 2);
454
0
        int tok = eob_tok + 1;
455
0
        int level_tok = tok * 0x41;
456
0
        unsigned mag;
457
458
0
#define DECODE_COEFS_CLASS(tx_class) \
459
0
        unsigned x, y; \
460
0
        uint8_t *level; \
461
0
        if (tx_class == TX_CLASS_2D) \
462
0
            rc = scan[eob], x = rc >> shift, y = rc & mask; \
463
0
        else if (tx_class == TX_CLASS_H) \
464
            /* Transposing reduces the stride and padding requirements */ \
465
0
            x = eob & mask, y = eob >> shift, rc = eob; \
466
0
        else /* tx_class == TX_CLASS_V */ \
467
0
            x = eob & mask, y = eob >> shift, rc = (x << shift2) | y; \
468
0
        if (dbg) \
469
0
            printf("Post-lo_tok[%d][%d][%d][%d=%d=%d]: r=%d\n", \
470
0
                   t_dim->ctx, chroma, ctx, eob, rc, tok, ts->msac.rng); \
471
0
        if (eob_tok == 2) { \
472
0
            ctx = (tx_class == TX_CLASS_2D ? (x | y) > 1 : y != 0) ? 14 : 7; \
473
0
            tok = dav1d_msac_decode_hi_tok(&ts->msac, hi_cdf[ctx]); \
474
0
            level_tok = tok + (3 << 6); \
475
0
            if (dbg) \
476
0
                printf("Post-hi_tok[%d][%d][%d][%d=%d=%d]: r=%d\n", \
477
0
                       imin(t_dim->ctx, 3), chroma, ctx, eob, rc, tok, \
478
0
                       ts->msac.rng); \
479
0
        } \
480
0
        cf[rc] = tok << 11; \
481
0
        if (tx_class == TX_CLASS_2D) \
482
0
            level = levels + rc; \
483
0
        else \
484
0
            level = levels + x * stride + y; \
485
0
        *level = (uint8_t) level_tok; \
486
0
        for (int i = eob - 1; i > 0; i--) { /* ac */ \
487
0
            unsigned rc_i; \
488
0
            if (tx_class == TX_CLASS_2D) \
489
0
                rc_i = scan[i], x = rc_i >> shift, y = rc_i & mask; \
490
0
            else if (tx_class == TX_CLASS_H) \
491
0
                x = i & mask, y = i >> shift, rc_i = i; \
492
0
            else /* tx_class == TX_CLASS_V */ \
493
0
                x = i & mask, y = i >> shift, rc_i = (x << shift2) | y; \
494
0
            assert(x < 32 && y < 32); \
495
0
            if (tx_class == TX_CLASS_2D) \
496
0
                level = levels + rc_i; \
497
0
            else \
498
0
                level = levels + x * stride + y; \
499
0
            ctx = get_lo_ctx(level, tx_class, &mag, lo_ctx_offsets, x, y, stride); \
500
0
            if (tx_class == TX_CLASS_2D) \
501
0
                y |= x; \
502
0
            tok = dav1d_msac_decode_symbol_adapt4(&ts->msac, lo_cdf[ctx], 3); \
503
0
            if (dbg) \
504
0
                printf("Post-lo_tok[%d][%d][%d][%d=%d=%d]: r=%d\n", \
505
0
                       t_dim->ctx, chroma, ctx, i, rc_i, tok, ts->msac.rng); \
506
0
            if (tok == 3) { \
507
0
                mag &= 63; \
508
0
                ctx = (y > (tx_class == TX_CLASS_2D) ? 14 : 7) + \
509
0
                      (mag > 12 ? 6 : (mag + 1) >> 1); \
510
0
                tok = dav1d_msac_decode_hi_tok(&ts->msac, hi_cdf[ctx]); \
511
0
                if (dbg) \
512
0
                    printf("Post-hi_tok[%d][%d][%d][%d=%d=%d]: r=%d\n", \
513
0
                           imin(t_dim->ctx, 3), chroma, ctx, i, rc_i, tok, \
514
0
                           ts->msac.rng); \
515
0
                *level = (uint8_t) (tok + (3 << 6)); \
516
0
                cf[rc_i] = (tok << 11) | rc; \
517
0
                rc = rc_i; \
518
0
            } else { \
519
                /* 0x1 for tok, 0x7ff as bitmask for rc, 0x41 for level_tok */ \
520
0
                tok *= 0x17ff41; \
521
0
                *level = (uint8_t) tok; \
522
                /* tok ? (tok << 11) | rc : 0 */ \
523
0
                tok = (tok >> 9) & (rc + ~0x7ffu); \
524
0
                if (tok) rc = rc_i; \
525
0
                cf[rc_i] = tok; \
526
0
            } \
527
0
        } \
528
        /* dc */ \
529
0
        ctx = (tx_class == TX_CLASS_2D) ? 0 : \
530
0
            get_lo_ctx(levels, tx_class, &mag, lo_ctx_offsets, 0, 0, stride); \
531
0
        dc_tok = dav1d_msac_decode_symbol_adapt4(&ts->msac, lo_cdf[ctx], 3); \
532
0
        if (dbg) \
533
0
            printf("Post-dc_lo_tok[%d][%d][%d][%d]: r=%d\n", \
534
0
                   t_dim->ctx, chroma, ctx, dc_tok, ts->msac.rng); \
535
0
        if (dc_tok == 3) { \
536
0
            if (tx_class == TX_CLASS_2D) \
537
0
                mag = levels[0 * stride + 1] + levels[1 * stride + 0] + \
538
0
                      levels[1 * stride + 1]; \
539
0
            mag &= 63; \
540
0
            ctx = mag > 12 ? 6 : (mag + 1) >> 1; \
541
0
            dc_tok = dav1d_msac_decode_hi_tok(&ts->msac, hi_cdf[ctx]); \
542
0
            if (dbg) \
543
0
                printf("Post-dc_hi_tok[%d][%d][0][%d]: r=%d\n", \
544
0
                       imin(t_dim->ctx, 3), chroma, dc_tok, ts->msac.rng); \
545
0
        } \
546
0
        break
547
548
0
        const uint16_t *scan;
549
0
        switch (tx_class) {
550
0
        case TX_CLASS_2D: {
551
0
            const unsigned nonsquare_tx = tx >= RTX_4X8;
552
0
            const uint8_t (*const lo_ctx_offsets)[5] =
553
0
                dav1d_lo_ctx_offsets[nonsquare_tx + (tx & nonsquare_tx)];
554
0
            scan = dav1d_scans[tx];
555
0
            const ptrdiff_t stride = 4 << slh;
556
0
            const unsigned shift = slh + 2, shift2 = 0;
557
0
            const unsigned mask = (4 << slh) - 1;
558
0
            memset(levels, 0, stride * ((4 << slw) + 2));
559
0
            DECODE_COEFS_CLASS(TX_CLASS_2D);
560
0
        }
561
0
        case TX_CLASS_H: {
562
0
            const uint8_t (*const lo_ctx_offsets)[5] = NULL;
563
0
            const ptrdiff_t stride = 16;
564
0
            const unsigned shift = slh + 2, shift2 = 0;
565
0
            const unsigned mask = (4 << slh) - 1;
566
0
            memset(levels, 0, stride * ((4 << slh) + 2));
567
0
            DECODE_COEFS_CLASS(TX_CLASS_H);
568
0
        }
569
0
        case TX_CLASS_V: {
570
0
            const uint8_t (*const lo_ctx_offsets)[5] = NULL;
571
0
            const ptrdiff_t stride = 16;
572
0
            const unsigned shift = slw + 2, shift2 = slh + 2;
573
0
            const unsigned mask = (4 << slw) - 1;
574
0
            memset(levels, 0, stride * ((4 << slw) + 2));
575
0
            DECODE_COEFS_CLASS(TX_CLASS_V);
576
0
        }
577
0
#undef DECODE_COEFS_CLASS
578
0
        default: assert(0);
579
0
        }
580
0
    } else { // dc-only
581
0
        int tok_br = dav1d_msac_decode_symbol_adapt4(&ts->msac, eob_cdf[0], 2);
582
0
        dc_tok = 1 + tok_br;
583
0
        if (dbg)
584
0
            printf("Post-dc_lo_tok[%d][%d][%d][%d]: r=%d\n",
585
0
                   t_dim->ctx, chroma, 0, dc_tok, ts->msac.rng);
586
0
        if (tok_br == 2) {
587
0
            dc_tok = dav1d_msac_decode_hi_tok(&ts->msac, hi_cdf[0]);
588
0
            if (dbg)
589
0
                printf("Post-dc_hi_tok[%d][%d][0][%d]: r=%d\n",
590
0
                       imin(t_dim->ctx, 3), chroma, dc_tok, ts->msac.rng);
591
0
        }
592
0
        rc = 0;
593
0
    }
594
595
    // residual and sign
596
0
    const uint16_t *const dq_tbl = ts->dq[b->seg_id][plane];
597
0
    const uint8_t *const qm_tbl = *txtp < IDTX ? f->qm[tx][plane] : NULL;
598
0
    const int dq_shift = imax(0, t_dim->ctx - 2);
599
0
    const int cf_max = ~(~127U << (BITDEPTH == 8 ? 8 : f->cur.p.bpc));
600
0
    unsigned cul_level, dc_sign_level;
601
602
0
    if (!dc_tok) {
603
0
        cul_level = 0;
604
0
        dc_sign_level = 1 << 6;
605
0
        if (qm_tbl) goto ac_qm;
606
0
        goto ac_noqm;
607
0
    }
608
609
0
    const int dc_sign_ctx = get_dc_sign_ctx(tx, a, l);
610
0
    uint16_t *const dc_sign_cdf = ts->cdf.coef.dc_sign[chroma][dc_sign_ctx];
611
0
    const int dc_sign = dav1d_msac_decode_bool_adapt(&ts->msac, dc_sign_cdf);
612
0
    if (dbg)
613
0
        printf("Post-dc_sign[%d][%d][%d]: r=%d\n",
614
0
               chroma, dc_sign_ctx, dc_sign, ts->msac.rng);
615
616
0
    int dc_dq = dq_tbl[0];
617
0
    dc_sign_level = (dc_sign - 1) & (2 << 6);
618
619
0
    if (qm_tbl) {
620
0
        dc_dq = (dc_dq * qm_tbl[0] + 16) >> 5;
621
622
0
        if (dc_tok == 15) {
623
0
            dc_tok = read_golomb(&ts->msac) + 15;
624
0
            if (dbg)
625
0
                printf("Post-dc_residual[%d->%d]: r=%d\n",
626
0
                       dc_tok - 15, dc_tok, ts->msac.rng);
627
628
0
            dc_tok &= 0xfffff;
629
0
            dc_dq = (dc_dq * dc_tok) & 0xffffff;
630
0
        } else {
631
0
            dc_dq *= dc_tok;
632
0
            assert(dc_dq <= 0xffffff);
633
0
        }
634
0
        cul_level = dc_tok;
635
0
        dc_dq >>= dq_shift;
636
0
        dc_dq = umin(dc_dq, cf_max + dc_sign);
637
0
        cf[0] = (coef) (dc_sign ? -dc_dq : dc_dq);
638
639
0
        if (rc) ac_qm: {
640
0
            const unsigned ac_dq = dq_tbl[1];
641
0
            do {
642
0
                const int sign = dav1d_msac_decode_bool_equi(&ts->msac);
643
0
                if (dbg)
644
0
                    printf("Post-sign[%d=%d]: r=%d\n", rc, sign, ts->msac.rng);
645
0
                const unsigned rc_tok = cf[rc];
646
0
                unsigned tok, dq = (ac_dq * qm_tbl[rc] + 16) >> 5;
647
0
                int dq_sat;
648
649
0
                if (rc_tok >= (15 << 11)) {
650
0
                    tok = read_golomb(&ts->msac) + 15;
651
0
                    if (dbg)
652
0
                        printf("Post-residual[%d=%d->%d]: r=%d\n",
653
0
                               rc, tok - 15, tok, ts->msac.rng);
654
655
0
                    tok &= 0xfffff;
656
0
                    dq = (dq * tok) & 0xffffff;
657
0
                } else {
658
0
                    tok = rc_tok >> 11;
659
0
                    dq *= tok;
660
0
                    assert(dq <= 0xffffff);
661
0
                }
662
0
                cul_level += tok;
663
0
                dq >>= dq_shift;
664
0
                dq_sat = umin(dq, cf_max + sign);
665
0
                cf[rc] = (coef) (sign ? -dq_sat : dq_sat);
666
667
0
                rc = rc_tok & 0x3ff;
668
0
            } while (rc);
669
0
        }
670
0
    } else {
671
        // non-qmatrix is the common case and allows for additional optimizations
672
0
        if (dc_tok == 15) {
673
0
            dc_tok = read_golomb(&ts->msac) + 15;
674
0
            if (dbg)
675
0
                printf("Post-dc_residual[%d->%d]: r=%d\n",
676
0
                       dc_tok - 15, dc_tok, ts->msac.rng);
677
678
0
            dc_tok &= 0xfffff;
679
0
            dc_dq = ((dc_dq * dc_tok) & 0xffffff) >> dq_shift;
680
0
            dc_dq = umin(dc_dq, cf_max + dc_sign);
681
0
        } else {
682
0
            dc_dq = ((dc_dq * dc_tok) >> dq_shift);
683
0
            assert(dc_dq <= cf_max);
684
0
        }
685
0
        cul_level = dc_tok;
686
0
        cf[0] = (coef) (dc_sign ? -dc_dq : dc_dq);
687
688
0
        if (rc) ac_noqm: {
689
0
            const unsigned ac_dq = dq_tbl[1];
690
0
            do {
691
0
                const int sign = dav1d_msac_decode_bool_equi(&ts->msac);
692
0
                if (dbg)
693
0
                    printf("Post-sign[%d=%d]: r=%d\n", rc, sign, ts->msac.rng);
694
0
                const unsigned rc_tok = cf[rc];
695
0
                unsigned tok;
696
0
                int dq;
697
698
                // residual
699
0
                if (rc_tok >= (15 << 11)) {
700
0
                    tok = read_golomb(&ts->msac) + 15;
701
0
                    if (dbg)
702
0
                        printf("Post-residual[%d=%d->%d]: r=%d\n",
703
0
                               rc, tok - 15, tok, ts->msac.rng);
704
705
                    // coefficient parsing, see 5.11.39
706
0
                    tok &= 0xfffff;
707
708
                    // dequant, see 7.12.3
709
0
                    dq = ((ac_dq * tok) & 0xffffff) >> dq_shift;
710
0
                    dq = umin(dq, cf_max + sign);
711
0
                } else {
712
                    // cannot exceed cf_max, so we can avoid the clipping
713
0
                    tok = rc_tok >> 11;
714
0
                    dq = ((ac_dq * tok) >> dq_shift);
715
0
                    assert(dq <= cf_max);
716
0
                }
717
0
                cul_level += tok;
718
0
                cf[rc] = (coef) (sign ? -dq : dq);
719
720
0
                rc = rc_tok & 0x3ff; // next non-zero rc, zero if eob
721
0
            } while (rc);
722
0
        }
723
0
    }
724
725
    // context
726
0
    *res_ctx = umin(cul_level, 63) | dc_sign_level;
727
728
0
    return eob;
729
0
}
730
731
static void read_coef_tree(Dav1dTaskContext *const t,
732
                           const enum BlockSize bs, const Av1Block *const b,
733
                           const enum RectTxfmSize ytx, const int depth,
734
                           const uint16_t *const tx_split,
735
                           const int x_off, const int y_off, pixel *dst)
736
0
{
737
0
    const Dav1dFrameContext *const f = t->f;
738
0
    Dav1dTileState *const ts = t->ts;
739
0
    const Dav1dDSPContext *const dsp = f->dsp;
740
0
    const TxfmInfo *const t_dim = &dav1d_txfm_dimensions[ytx];
741
0
    const int txw = t_dim->w, txh = t_dim->h;
742
743
    /* y_off can be larger than 3 since lossless blocks use TX_4X4 but can't
744
     * be splitted. Aviods an undefined left shift. */
745
0
    if (depth < 2 && tx_split[depth] &&
746
0
        tx_split[depth] & (1 << (y_off * 4 + x_off)))
747
0
    {
748
0
        const enum RectTxfmSize sub = t_dim->sub;
749
0
        const TxfmInfo *const sub_t_dim = &dav1d_txfm_dimensions[sub];
750
0
        const int txsw = sub_t_dim->w, txsh = sub_t_dim->h;
751
752
0
        read_coef_tree(t, bs, b, sub, depth + 1, tx_split,
753
0
                       x_off * 2 + 0, y_off * 2 + 0, dst);
754
0
        t->bx += txsw;
755
0
        if (txw >= txh && t->bx < f->bw)
756
0
            read_coef_tree(t, bs, b, sub, depth + 1, tx_split, x_off * 2 + 1,
757
0
                           y_off * 2 + 0, dst ? &dst[4 * txsw] : NULL);
758
0
        t->bx -= txsw;
759
0
        t->by += txsh;
760
0
        if (txh >= txw && t->by < f->bh) {
761
0
            if (dst)
762
0
                dst += 4 * txsh * PXSTRIDE(f->cur.stride[0]);
763
0
            read_coef_tree(t, bs, b, sub, depth + 1, tx_split,
764
0
                           x_off * 2 + 0, y_off * 2 + 1, dst);
765
0
            t->bx += txsw;
766
0
            if (txw >= txh && t->bx < f->bw)
767
0
                read_coef_tree(t, bs, b, sub, depth + 1, tx_split, x_off * 2 + 1,
768
0
                               y_off * 2 + 1, dst ? &dst[4 * txsw] : NULL);
769
0
            t->bx -= txsw;
770
0
        }
771
0
        t->by -= txsh;
772
0
    } else {
773
0
        const int bx4 = t->bx & 31, by4 = t->by & 31;
774
0
        enum TxfmType txtp;
775
0
        uint8_t cf_ctx;
776
0
        int eob;
777
0
        coef *cf;
778
779
0
        if (t->frame_thread.pass) {
780
0
            const int p = t->frame_thread.pass & 1;
781
0
            assert(ts->frame_thread[p].cf);
782
0
            cf = ts->frame_thread[p].cf;
783
0
            ts->frame_thread[p].cf += imin(t_dim->w, 8) * imin(t_dim->h, 8) * 16;
784
0
        } else {
785
0
            cf = bitfn(t->cf);
786
0
        }
787
0
        if (t->frame_thread.pass != 2) {
788
0
            eob = decode_coefs(t, &t->a->lcoef[bx4], &t->l.lcoef[by4],
789
0
                               ytx, bs, b, 0, 0, cf, &txtp, &cf_ctx);
790
0
            if (DEBUG_BLOCK_INFO)
791
0
                printf("Post-y-cf-blk[tx=%d,txtp=%d,eob=%d]: r=%d\n",
792
0
                       ytx, txtp, eob, ts->msac.rng);
793
0
            dav1d_memset_likely_pow2(&t->a->lcoef[bx4], cf_ctx, imin(txw, f->bw - t->bx));
794
0
            dav1d_memset_likely_pow2(&t->l.lcoef[by4], cf_ctx, imin(txh, f->bh - t->by));
795
0
#define set_ctx(rep_macro) \
796
0
            for (int y = 0; y < txh; y++) { \
797
0
                rep_macro(txtp_map, 0, txtp); \
798
0
                txtp_map += 32; \
799
0
            }
800
0
            uint8_t *txtp_map = &t->scratch.txtp_map[by4 * 32 + bx4];
801
0
            case_set_upto16(t_dim->lw);
802
0
#undef set_ctx
803
0
            if (t->frame_thread.pass == 1)
804
0
                *ts->frame_thread[1].cbi++ = eob * (1 << 5) + txtp;
805
0
        } else {
806
0
            const int cbi = *ts->frame_thread[0].cbi++;
807
0
            eob  = cbi >> 5;
808
0
            txtp = cbi & 0x1f;
809
0
        }
810
0
        if (!(t->frame_thread.pass & 1)) {
811
0
            assert(dst);
812
0
            if (eob >= 0) {
813
0
                if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS)
814
0
                    coef_dump(cf, imin(t_dim->h, 8) * 4, imin(t_dim->w, 8) * 4, 3, "dq");
815
0
                dsp->itx.itxfm_add[ytx][txtp](dst, f->cur.stride[0], cf, eob
816
0
                                              HIGHBD_CALL_SUFFIX);
817
0
                if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS)
818
0
                    hex_dump(dst, f->cur.stride[0], t_dim->w * 4, t_dim->h * 4, "recon");
819
0
            }
820
0
        }
821
0
    }
822
0
}
823
824
void bytefn(dav1d_read_coef_blocks)(Dav1dTaskContext *const t,
825
                                    const enum BlockSize bs, const Av1Block *const b)
826
0
{
827
0
    const Dav1dFrameContext *const f = t->f;
828
0
    const int ss_ver = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
829
0
    const int ss_hor = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
830
0
    const int bx4 = t->bx & 31, by4 = t->by & 31;
831
0
    const int cbx4 = bx4 >> ss_hor, cby4 = by4 >> ss_ver;
832
0
    const uint8_t *const b_dim = dav1d_block_dimensions[bs];
833
0
    const int bw4 = b_dim[0], bh4 = b_dim[1];
834
0
    const int cbw4 = (bw4 + ss_hor) >> ss_hor, cbh4 = (bh4 + ss_ver) >> ss_ver;
835
0
    const int has_chroma = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I400 &&
836
0
                           (bw4 > ss_hor || t->bx & 1) &&
837
0
                           (bh4 > ss_ver || t->by & 1);
838
839
0
    if (b->skip) {
840
0
        BlockContext *const a = t->a;
841
0
        dav1d_memset_pow2[b_dim[2]](&a->lcoef[bx4], 0x40);
842
0
        dav1d_memset_pow2[b_dim[3]](&t->l.lcoef[by4], 0x40);
843
0
        if (has_chroma) {
844
0
            dav1d_memset_pow2_fn memset_cw = dav1d_memset_pow2[ulog2(cbw4)];
845
0
            dav1d_memset_pow2_fn memset_ch = dav1d_memset_pow2[ulog2(cbh4)];
846
0
            memset_cw(&a->ccoef[0][cbx4], 0x40);
847
0
            memset_cw(&a->ccoef[1][cbx4], 0x40);
848
0
            memset_ch(&t->l.ccoef[0][cby4], 0x40);
849
0
            memset_ch(&t->l.ccoef[1][cby4], 0x40);
850
0
        }
851
0
        return;
852
0
    }
853
854
0
    Dav1dTileState *const ts = t->ts;
855
0
    const int w4 = imin(bw4, f->bw - t->bx), h4 = imin(bh4, f->bh - t->by);
856
0
    const int cw4 = (w4 + ss_hor) >> ss_hor, ch4 = (h4 + ss_ver) >> ss_ver;
857
0
    assert(t->frame_thread.pass == 1);
858
0
    assert(!b->skip);
859
0
    const TxfmInfo *const uv_t_dim = &dav1d_txfm_dimensions[b->uvtx];
860
0
    const TxfmInfo *const t_dim = &dav1d_txfm_dimensions[b->intra ? b->tx : b->max_ytx];
861
0
    const uint16_t tx_split[2] = { b->tx_split0, b->tx_split1 };
862
863
0
    for (int init_y = 0; init_y < h4; init_y += 16) {
864
0
        const int sub_h4 = imin(h4, 16 + init_y);
865
0
        for (int init_x = 0; init_x < w4; init_x += 16) {
866
0
            const int sub_w4 = imin(w4, init_x + 16);
867
0
            int y_off = !!init_y, y, x;
868
0
            for (y = init_y, t->by += init_y; y < sub_h4;
869
0
                 y += t_dim->h, t->by += t_dim->h, y_off++)
870
0
            {
871
0
                int x_off = !!init_x;
872
0
                for (x = init_x, t->bx += init_x; x < sub_w4;
873
0
                     x += t_dim->w, t->bx += t_dim->w, x_off++)
874
0
                {
875
0
                    if (!b->intra) {
876
0
                        read_coef_tree(t, bs, b, b->max_ytx, 0, tx_split,
877
0
                                       x_off, y_off, NULL);
878
0
                    } else {
879
0
                        uint8_t cf_ctx = 0x40;
880
0
                        enum TxfmType txtp;
881
0
                        const int eob =
882
0
                            decode_coefs(t, &t->a->lcoef[bx4 + x],
883
0
                                         &t->l.lcoef[by4 + y], b->tx, bs, b, 1,
884
0
                                         0, ts->frame_thread[1].cf, &txtp, &cf_ctx);
885
0
                        if (DEBUG_BLOCK_INFO)
886
0
                            printf("Post-y-cf-blk[tx=%d,txtp=%d,eob=%d]: r=%d\n",
887
0
                                   b->tx, txtp, eob, ts->msac.rng);
888
0
                        *ts->frame_thread[1].cbi++ = eob * (1 << 5) + txtp;
889
0
                        ts->frame_thread[1].cf += imin(t_dim->w, 8) * imin(t_dim->h, 8) * 16;
890
0
                        dav1d_memset_likely_pow2(&t->a->lcoef[bx4 + x], cf_ctx, imin(t_dim->w, f->bw - t->bx));
891
0
                        dav1d_memset_likely_pow2(&t->l.lcoef[by4 + y], cf_ctx, imin(t_dim->h, f->bh - t->by));
892
0
                    }
893
0
                }
894
0
                t->bx -= x;
895
0
            }
896
0
            t->by -= y;
897
898
0
            if (!has_chroma) continue;
899
900
0
            const int sub_ch4 = imin(ch4, (init_y + 16) >> ss_ver);
901
0
            const int sub_cw4 = imin(cw4, (init_x + 16) >> ss_hor);
902
0
            for (int pl = 0; pl < 2; pl++) {
903
0
                for (y = init_y >> ss_ver, t->by += init_y; y < sub_ch4;
904
0
                     y += uv_t_dim->h, t->by += uv_t_dim->h << ss_ver)
905
0
                {
906
0
                    for (x = init_x >> ss_hor, t->bx += init_x; x < sub_cw4;
907
0
                         x += uv_t_dim->w, t->bx += uv_t_dim->w << ss_hor)
908
0
                    {
909
0
                        uint8_t cf_ctx = 0x40;
910
0
                        enum TxfmType txtp;
911
0
                        if (!b->intra)
912
0
                            txtp = t->scratch.txtp_map[(by4 + (y << ss_ver)) * 32 +
913
0
                                                        bx4 + (x << ss_hor)];
914
0
                        const int eob =
915
0
                            decode_coefs(t, &t->a->ccoef[pl][cbx4 + x],
916
0
                                         &t->l.ccoef[pl][cby4 + y], b->uvtx, bs,
917
0
                                         b, b->intra, 1 + pl, ts->frame_thread[1].cf,
918
0
                                         &txtp, &cf_ctx);
919
0
                        if (DEBUG_BLOCK_INFO)
920
0
                            printf("Post-uv-cf-blk[pl=%d,tx=%d,"
921
0
                                   "txtp=%d,eob=%d]: r=%d\n",
922
0
                                   pl, b->uvtx, txtp, eob, ts->msac.rng);
923
0
                        *ts->frame_thread[1].cbi++ = eob * (1 << 5) + txtp;
924
0
                        ts->frame_thread[1].cf += uv_t_dim->w * uv_t_dim->h * 16;
925
0
                        int ctw = imin(uv_t_dim->w, (f->bw - t->bx + ss_hor) >> ss_hor);
926
0
                        int cth = imin(uv_t_dim->h, (f->bh - t->by + ss_ver) >> ss_ver);
927
0
                        dav1d_memset_likely_pow2(&t->a->ccoef[pl][cbx4 + x], cf_ctx, ctw);
928
0
                        dav1d_memset_likely_pow2(&t->l.ccoef[pl][cby4 + y], cf_ctx, cth);
929
0
                    }
930
0
                    t->bx -= x << ss_hor;
931
0
                }
932
0
                t->by -= y << ss_ver;
933
0
            }
934
0
        }
935
0
    }
936
0
}
Unexecuted instantiation: dav1d_read_coef_blocks_8bpc
Unexecuted instantiation: dav1d_read_coef_blocks_16bpc
937
938
static int mc(Dav1dTaskContext *const t,
939
              pixel *const dst8, int16_t *const dst16, const ptrdiff_t dst_stride,
940
              const int bw4, const int bh4,
941
              const int bx, const int by, const int pl,
942
              const mv mv, const Dav1dThreadPicture *const refp, const int refidx,
943
              const enum Filter2d filter_2d)
944
0
{
945
0
    assert((dst8 != NULL) ^ (dst16 != NULL));
946
0
    const Dav1dFrameContext *const f = t->f;
947
0
    const int ss_ver = !!pl && f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
948
0
    const int ss_hor = !!pl && f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
949
0
    const int h_mul = 4 >> ss_hor, v_mul = 4 >> ss_ver;
950
0
    const int mvx = mv.x, mvy = mv.y;
951
0
    const int mx = mvx & (15 >> !ss_hor), my = mvy & (15 >> !ss_ver);
952
0
    ptrdiff_t ref_stride = refp->p.stride[!!pl];
953
0
    const pixel *ref;
954
955
0
    if (refp->p.p.w == f->cur.p.w && refp->p.p.h == f->cur.p.h) {
956
0
        const int dx = bx * h_mul + (mvx >> (3 + ss_hor));
957
0
        const int dy = by * v_mul + (mvy >> (3 + ss_ver));
958
0
        int w, h;
959
960
0
        if (refp->p.data[0] != f->cur.data[0]) { // i.e. not for intrabc
961
0
            w = (f->cur.p.w + ss_hor) >> ss_hor;
962
0
            h = (f->cur.p.h + ss_ver) >> ss_ver;
963
0
        } else {
964
0
            w = f->bw * 4 >> ss_hor;
965
0
            h = f->bh * 4 >> ss_ver;
966
0
        }
967
0
        if (dx < !!mx * 3 || dy < !!my * 3 ||
968
0
            dx + bw4 * h_mul + !!mx * 4 > w ||
969
0
            dy + bh4 * v_mul + !!my * 4 > h)
970
0
        {
971
0
            pixel *const emu_edge_buf = bitfn(t->scratch.emu_edge);
972
0
            f->dsp->mc.emu_edge(bw4 * h_mul + !!mx * 7, bh4 * v_mul + !!my * 7,
973
0
                                w, h, dx - !!mx * 3, dy - !!my * 3,
974
0
                                emu_edge_buf, 192 * sizeof(pixel),
975
0
                                refp->p.data[pl], ref_stride);
976
0
            ref = &emu_edge_buf[192 * !!my * 3 + !!mx * 3];
977
0
            ref_stride = 192 * sizeof(pixel);
978
0
        } else {
979
0
            ref = ((pixel *) refp->p.data[pl]) + PXSTRIDE(ref_stride) * dy + dx;
980
0
        }
981
982
0
        if (dst8 != NULL) {
983
0
            f->dsp->mc.mc[filter_2d](dst8, dst_stride, ref, ref_stride, bw4 * h_mul,
984
0
                                     bh4 * v_mul, mx << !ss_hor, my << !ss_ver
985
0
                                     HIGHBD_CALL_SUFFIX);
986
0
        } else {
987
0
            f->dsp->mc.mct[filter_2d](dst16, ref, ref_stride, bw4 * h_mul,
988
0
                                      bh4 * v_mul, mx << !ss_hor, my << !ss_ver
989
0
                                      HIGHBD_CALL_SUFFIX);
990
0
        }
991
0
    } else {
992
0
        assert(refp != &f->sr_cur);
993
994
0
        const int orig_pos_y = (by * v_mul << 4) + mvy * (1 << !ss_ver);
995
0
        const int orig_pos_x = (bx * h_mul << 4) + mvx * (1 << !ss_hor);
996
0
#define scale_mv(res, val, scale) do { \
997
0
            const int64_t tmp = (int64_t)(val) * scale + (scale - 0x4000) * 8; \
998
0
            res = apply_sign64((int) ((llabs(tmp) + 128) >> 8), tmp) + 32;     \
999
0
        } while (0)
1000
0
        int pos_y, pos_x;
1001
0
        scale_mv(pos_x, orig_pos_x, f->svc[refidx][0].scale);
1002
0
        scale_mv(pos_y, orig_pos_y, f->svc[refidx][1].scale);
1003
0
#undef scale_mv
1004
0
        const int left = pos_x >> 10;
1005
0
        const int top = pos_y >> 10;
1006
0
        const int right =
1007
0
            ((pos_x + (bw4 * h_mul - 1) * f->svc[refidx][0].step) >> 10) + 1;
1008
0
        const int bottom =
1009
0
            ((pos_y + (bh4 * v_mul - 1) * f->svc[refidx][1].step) >> 10) + 1;
1010
1011
0
        if (DEBUG_BLOCK_INFO)
1012
0
            printf("Off %dx%d [%d,%d,%d], size %dx%d [%d,%d]\n",
1013
0
                   left, top, orig_pos_x, f->svc[refidx][0].scale, refidx,
1014
0
                   right-left, bottom-top,
1015
0
                   f->svc[refidx][0].step, f->svc[refidx][1].step);
1016
1017
0
        const int w = (refp->p.p.w + ss_hor) >> ss_hor;
1018
0
        const int h = (refp->p.p.h + ss_ver) >> ss_ver;
1019
0
        if (left < 3 || top < 3 || right + 4 > w || bottom + 4 > h) {
1020
0
            pixel *const emu_edge_buf = bitfn(t->scratch.emu_edge);
1021
0
            f->dsp->mc.emu_edge(right - left + 7, bottom - top + 7,
1022
0
                                w, h, left - 3, top - 3,
1023
0
                                emu_edge_buf, 320 * sizeof(pixel),
1024
0
                                refp->p.data[pl], ref_stride);
1025
0
            ref = &emu_edge_buf[320 * 3 + 3];
1026
0
            ref_stride = 320 * sizeof(pixel);
1027
0
            if (DEBUG_BLOCK_INFO) printf("Emu\n");
1028
0
        } else {
1029
0
            ref = ((pixel *) refp->p.data[pl]) + PXSTRIDE(ref_stride) * top + left;
1030
0
        }
1031
1032
0
        if (dst8 != NULL) {
1033
0
            f->dsp->mc.mc_scaled[filter_2d](dst8, dst_stride, ref, ref_stride,
1034
0
                                            bw4 * h_mul, bh4 * v_mul,
1035
0
                                            pos_x & 0x3ff, pos_y & 0x3ff,
1036
0
                                            f->svc[refidx][0].step,
1037
0
                                            f->svc[refidx][1].step
1038
0
                                            HIGHBD_CALL_SUFFIX);
1039
0
        } else {
1040
0
            f->dsp->mc.mct_scaled[filter_2d](dst16, ref, ref_stride,
1041
0
                                             bw4 * h_mul, bh4 * v_mul,
1042
0
                                             pos_x & 0x3ff, pos_y & 0x3ff,
1043
0
                                             f->svc[refidx][0].step,
1044
0
                                             f->svc[refidx][1].step
1045
0
                                             HIGHBD_CALL_SUFFIX);
1046
0
        }
1047
0
    }
1048
1049
0
    return 0;
1050
0
}
1051
1052
static int obmc(Dav1dTaskContext *const t,
1053
                pixel *const dst, const ptrdiff_t dst_stride,
1054
                const uint8_t *const b_dim, const int pl,
1055
                const int bx4, const int by4, const int w4, const int h4)
1056
0
{
1057
0
    assert(!(t->bx & 1) && !(t->by & 1));
1058
0
    const Dav1dFrameContext *const f = t->f;
1059
0
    /*const*/ refmvs_block **r = &t->rt.r[(t->by & 31) + 5];
1060
0
    pixel *const lap = bitfn(t->scratch.lap);
1061
0
    const int ss_ver = !!pl && f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
1062
0
    const int ss_hor = !!pl && f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
1063
0
    const int h_mul = 4 >> ss_hor, v_mul = 4 >> ss_ver;
1064
0
    int res;
1065
1066
0
    if (t->by > t->ts->tiling.row_start &&
1067
0
        (!pl || b_dim[0] * h_mul + b_dim[1] * v_mul >= 16))
1068
0
    {
1069
0
        for (int i = 0, x = 0; x < w4 && i < imin(b_dim[2], 4); ) {
1070
            // only odd blocks are considered for overlap handling, hence +1
1071
0
            const refmvs_block *const a_r = &r[-1][t->bx + x + 1];
1072
0
            const uint8_t *const a_b_dim = dav1d_block_dimensions[a_r->bs];
1073
0
            const int step4 = iclip(a_b_dim[0], 2, 16);
1074
1075
0
            if (a_r->ref.ref[0] > 0) {
1076
0
                const int ow4 = imin(step4, b_dim[0]);
1077
0
                const int oh4 = imin(b_dim[1], 16) >> 1;
1078
0
                res = mc(t, lap, NULL, ow4 * h_mul * sizeof(pixel), ow4, (oh4 * 3 + 3) >> 2,
1079
0
                         t->bx + x, t->by, pl, a_r->mv.mv[0],
1080
0
                         &f->refp[a_r->ref.ref[0] - 1], a_r->ref.ref[0] - 1,
1081
0
                         dav1d_filter_2d[t->a->filter[1][bx4 + x + 1]][t->a->filter[0][bx4 + x + 1]]);
1082
0
                if (res) return res;
1083
0
                f->dsp->mc.blend_h(&dst[x * h_mul], dst_stride, lap,
1084
0
                                   h_mul * ow4, v_mul * oh4);
1085
0
                i++;
1086
0
            }
1087
0
            x += step4;
1088
0
        }
1089
0
    }
1090
1091
0
    if (t->bx > t->ts->tiling.col_start)
1092
0
        for (int i = 0, y = 0; y < h4 && i < imin(b_dim[3], 4); ) {
1093
            // only odd blocks are considered for overlap handling, hence +1
1094
0
            const refmvs_block *const l_r = &r[y + 1][t->bx - 1];
1095
0
            const uint8_t *const l_b_dim = dav1d_block_dimensions[l_r->bs];
1096
0
            const int step4 = iclip(l_b_dim[1], 2, 16);
1097
1098
0
            if (l_r->ref.ref[0] > 0) {
1099
0
                const int ow4 = imin(b_dim[0], 16) >> 1;
1100
0
                const int oh4 = imin(step4, b_dim[1]);
1101
0
                res = mc(t, lap, NULL, h_mul * ow4 * sizeof(pixel), ow4, oh4,
1102
0
                         t->bx, t->by + y, pl, l_r->mv.mv[0],
1103
0
                         &f->refp[l_r->ref.ref[0] - 1], l_r->ref.ref[0] - 1,
1104
0
                         dav1d_filter_2d[t->l.filter[1][by4 + y + 1]][t->l.filter[0][by4 + y + 1]]);
1105
0
                if (res) return res;
1106
0
                f->dsp->mc.blend_v(&dst[y * v_mul * PXSTRIDE(dst_stride)],
1107
0
                                   dst_stride, lap, h_mul * ow4, v_mul * oh4);
1108
0
                i++;
1109
0
            }
1110
0
            y += step4;
1111
0
        }
1112
0
    return 0;
1113
0
}
1114
1115
static int warp_affine(Dav1dTaskContext *const t,
1116
                       pixel *dst8, int16_t *dst16, const ptrdiff_t dstride,
1117
                       const uint8_t *const b_dim, const int pl,
1118
                       const Dav1dThreadPicture *const refp,
1119
                       const Dav1dWarpedMotionParams *const wmp)
1120
0
{
1121
0
    assert((dst8 != NULL) ^ (dst16 != NULL));
1122
0
    const Dav1dFrameContext *const f = t->f;
1123
0
    const Dav1dDSPContext *const dsp = f->dsp;
1124
0
    const int ss_ver = !!pl && f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
1125
0
    const int ss_hor = !!pl && f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
1126
0
    const int h_mul = 4 >> ss_hor, v_mul = 4 >> ss_ver;
1127
0
    assert(!((b_dim[0] * h_mul) & 7) && !((b_dim[1] * v_mul) & 7));
1128
0
    const int32_t *const mat = wmp->matrix;
1129
0
    const int width = (refp->p.p.w + ss_hor) >> ss_hor;
1130
0
    const int height = (refp->p.p.h + ss_ver) >> ss_ver;
1131
1132
0
    for (int y = 0; y < b_dim[1] * v_mul; y += 8) {
1133
0
        const int src_y = t->by * 4 + ((y + 4) << ss_ver);
1134
0
        const int64_t mat3_y = (int64_t) mat[3] * src_y + mat[0];
1135
0
        const int64_t mat5_y = (int64_t) mat[5] * src_y + mat[1];
1136
0
        for (int x = 0; x < b_dim[0] * h_mul; x += 8) {
1137
            // calculate transformation relative to center of 8x8 block in
1138
            // luma pixel units
1139
0
            const int src_x = t->bx * 4 + ((x + 4) << ss_hor);
1140
0
            const int64_t mvx = ((int64_t) mat[2] * src_x + mat3_y) >> ss_hor;
1141
0
            const int64_t mvy = ((int64_t) mat[4] * src_x + mat5_y) >> ss_ver;
1142
1143
0
            const int dx = (int) (mvx >> 16) - 4;
1144
0
            const int mx = (((int) mvx & 0xffff) - wmp->u.p.alpha * 4 -
1145
0
                                                   wmp->u.p.beta  * 7) & ~0x3f;
1146
0
            const int dy = (int) (mvy >> 16) - 4;
1147
0
            const int my = (((int) mvy & 0xffff) - wmp->u.p.gamma * 4 -
1148
0
                                                   wmp->u.p.delta * 4) & ~0x3f;
1149
1150
0
            const pixel *ref_ptr;
1151
0
            ptrdiff_t ref_stride = refp->p.stride[!!pl];
1152
1153
0
            if (dx < 3 || dx + 8 + 4 > width || dy < 3 || dy + 8 + 4 > height) {
1154
0
                pixel *const emu_edge_buf = bitfn(t->scratch.emu_edge);
1155
0
                f->dsp->mc.emu_edge(15, 15, width, height, dx - 3, dy - 3,
1156
0
                                    emu_edge_buf, 32 * sizeof(pixel),
1157
0
                                    refp->p.data[pl], ref_stride);
1158
0
                ref_ptr = &emu_edge_buf[32 * 3 + 3];
1159
0
                ref_stride = 32 * sizeof(pixel);
1160
0
            } else {
1161
0
                ref_ptr = ((pixel *) refp->p.data[pl]) + PXSTRIDE(ref_stride) * dy + dx;
1162
0
            }
1163
0
            if (dst16 != NULL)
1164
0
                dsp->mc.warp8x8t(&dst16[x], dstride, ref_ptr, ref_stride,
1165
0
                                 wmp->u.abcd, mx, my HIGHBD_CALL_SUFFIX);
1166
0
            else
1167
0
                dsp->mc.warp8x8(&dst8[x], dstride, ref_ptr, ref_stride,
1168
0
                                wmp->u.abcd, mx, my HIGHBD_CALL_SUFFIX);
1169
0
        }
1170
0
        if (dst8) dst8  += 8 * PXSTRIDE(dstride);
1171
0
        else      dst16 += 8 * dstride;
1172
0
    }
1173
0
    return 0;
1174
0
}
1175
1176
void bytefn(dav1d_recon_b_intra)(Dav1dTaskContext *const t, const enum BlockSize bs,
1177
                                 const enum EdgeFlags intra_edge_flags,
1178
                                 const Av1Block *const b)
1179
0
{
1180
0
    Dav1dTileState *const ts = t->ts;
1181
0
    const Dav1dFrameContext *const f = t->f;
1182
0
    const Dav1dDSPContext *const dsp = f->dsp;
1183
0
    const int bx4 = t->bx & 31, by4 = t->by & 31;
1184
0
    const int ss_ver = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
1185
0
    const int ss_hor = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
1186
0
    const int cbx4 = bx4 >> ss_hor, cby4 = by4 >> ss_ver;
1187
0
    const uint8_t *const b_dim = dav1d_block_dimensions[bs];
1188
0
    const int bw4 = b_dim[0], bh4 = b_dim[1];
1189
0
    const int w4 = imin(bw4, f->bw - t->bx), h4 = imin(bh4, f->bh - t->by);
1190
0
    const int cw4 = (w4 + ss_hor) >> ss_hor, ch4 = (h4 + ss_ver) >> ss_ver;
1191
0
    const int has_chroma = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I400 &&
1192
0
                           (bw4 > ss_hor || t->bx & 1) &&
1193
0
                           (bh4 > ss_ver || t->by & 1);
1194
0
    const TxfmInfo *const t_dim = &dav1d_txfm_dimensions[b->tx];
1195
0
    const TxfmInfo *const uv_t_dim = &dav1d_txfm_dimensions[b->uvtx];
1196
1197
    // coefficient coding
1198
0
    pixel *const edge = bitfn(t->scratch.edge) + 128;
1199
0
    const int cbw4 = (bw4 + ss_hor) >> ss_hor, cbh4 = (bh4 + ss_ver) >> ss_ver;
1200
1201
0
    const int intra_edge_filter_flag = f->seq_hdr->intra_edge_filter << 10;
1202
1203
0
    for (int init_y = 0; init_y < h4; init_y += 16) {
1204
0
        const int sub_h4 = imin(h4, 16 + init_y);
1205
0
        const int sub_ch4 = imin(ch4, (init_y + 16) >> ss_ver);
1206
0
        for (int init_x = 0; init_x < w4; init_x += 16) {
1207
0
            if (b->pal_sz[0]) {
1208
0
                pixel *dst = ((pixel *) f->cur.data[0]) +
1209
0
                             4 * (t->by * PXSTRIDE(f->cur.stride[0]) + t->bx);
1210
0
                const uint8_t *pal_idx;
1211
0
                if (t->frame_thread.pass) {
1212
0
                    const int p = t->frame_thread.pass & 1;
1213
0
                    assert(ts->frame_thread[p].pal_idx);
1214
0
                    pal_idx = ts->frame_thread[p].pal_idx;
1215
0
                    ts->frame_thread[p].pal_idx += bw4 * bh4 * 8;
1216
0
                } else {
1217
0
                    pal_idx = t->scratch.pal_idx_y;
1218
0
                }
1219
0
                const pixel *const pal = t->frame_thread.pass ?
1220
0
                    f->frame_thread.pal[((t->by >> 1) + (t->bx & 1)) * (f->b4_stride >> 1) +
1221
0
                                        ((t->bx >> 1) + (t->by & 1))][0] :
1222
0
                    bytefn(t->scratch.pal)[0];
1223
0
                f->dsp->ipred.pal_pred(dst, f->cur.stride[0], pal,
1224
0
                                       pal_idx, bw4 * 4, bh4 * 4);
1225
0
                if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS)
1226
0
                    hex_dump(dst, PXSTRIDE(f->cur.stride[0]),
1227
0
                             bw4 * 4, bh4 * 4, "y-pal-pred");
1228
0
            }
1229
1230
0
            const int intra_flags = (sm_flag(t->a, bx4) |
1231
0
                                     sm_flag(&t->l, by4) |
1232
0
                                     intra_edge_filter_flag);
1233
0
            const int sb_has_tr = init_x + 16 < w4 ? 1 : init_y ? 0 :
1234
0
                              intra_edge_flags & EDGE_I444_TOP_HAS_RIGHT;
1235
0
            const int sb_has_bl = init_x ? 0 : init_y + 16 < h4 ? 1 :
1236
0
                              intra_edge_flags & EDGE_I444_LEFT_HAS_BOTTOM;
1237
0
            int y, x;
1238
0
            const int sub_w4 = imin(w4, init_x + 16);
1239
0
            for (y = init_y, t->by += init_y; y < sub_h4;
1240
0
                 y += t_dim->h, t->by += t_dim->h)
1241
0
            {
1242
0
                pixel *dst = ((pixel *) f->cur.data[0]) +
1243
0
                               4 * (t->by * PXSTRIDE(f->cur.stride[0]) +
1244
0
                                    t->bx + init_x);
1245
0
                for (x = init_x, t->bx += init_x; x < sub_w4;
1246
0
                     x += t_dim->w, t->bx += t_dim->w)
1247
0
                {
1248
0
                    if (b->pal_sz[0]) goto skip_y_pred;
1249
1250
0
                    int angle = b->y_angle;
1251
0
                    const enum EdgeFlags edge_flags =
1252
0
                        (((y > init_y || !sb_has_tr) && (x + t_dim->w >= sub_w4)) ?
1253
0
                             0 : EDGE_I444_TOP_HAS_RIGHT) |
1254
0
                        ((x > init_x || (!sb_has_bl && y + t_dim->h >= sub_h4)) ?
1255
0
                             0 : EDGE_I444_LEFT_HAS_BOTTOM);
1256
0
                    const pixel *top_sb_edge = NULL;
1257
0
                    if (!(t->by & (f->sb_step - 1))) {
1258
0
                        top_sb_edge = f->ipred_edge[0];
1259
0
                        const int sby = t->by >> f->sb_shift;
1260
0
                        top_sb_edge += f->sb128w * 128 * (sby - 1);
1261
0
                    }
1262
0
                    const enum IntraPredMode m =
1263
0
                        bytefn(dav1d_prepare_intra_edges)(t->bx,
1264
0
                                                          t->bx > ts->tiling.col_start,
1265
0
                                                          t->by,
1266
0
                                                          t->by > ts->tiling.row_start,
1267
0
                                                          ts->tiling.col_end,
1268
0
                                                          ts->tiling.row_end,
1269
0
                                                          edge_flags, dst,
1270
0
                                                          f->cur.stride[0], top_sb_edge,
1271
0
                                                          b->y_mode, &angle,
1272
0
                                                          t_dim->w, t_dim->h,
1273
0
                                                          f->seq_hdr->intra_edge_filter,
1274
0
                                                          edge HIGHBD_CALL_SUFFIX);
1275
0
                    dsp->ipred.intra_pred[m](dst, f->cur.stride[0], edge,
1276
0
                                             t_dim->w * 4, t_dim->h * 4,
1277
0
                                             angle | intra_flags,
1278
0
                                             4 * f->bw - 4 * t->bx,
1279
0
                                             4 * f->bh - 4 * t->by
1280
0
                                             HIGHBD_CALL_SUFFIX);
1281
1282
0
                    if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS) {
1283
0
                        hex_dump(edge - t_dim->h * 4, t_dim->h * 4,
1284
0
                                 t_dim->h * 4, 2, "l");
1285
0
                        hex_dump(edge, 0, 1, 1, "tl");
1286
0
                        hex_dump(edge + 1, t_dim->w * 4,
1287
0
                                 t_dim->w * 4, 2, "t");
1288
0
                        hex_dump(dst, f->cur.stride[0],
1289
0
                                 t_dim->w * 4, t_dim->h * 4, "y-intra-pred");
1290
0
                    }
1291
1292
0
                skip_y_pred: {}
1293
0
                    if (!b->skip) {
1294
0
                        coef *cf;
1295
0
                        int eob;
1296
0
                        enum TxfmType txtp;
1297
0
                        if (t->frame_thread.pass) {
1298
0
                            const int p = t->frame_thread.pass & 1;
1299
0
                            const int cbi = *ts->frame_thread[p].cbi++;
1300
0
                            cf = ts->frame_thread[p].cf;
1301
0
                            ts->frame_thread[p].cf += imin(t_dim->w, 8) * imin(t_dim->h, 8) * 16;
1302
0
                            eob  = cbi >> 5;
1303
0
                            txtp = cbi & 0x1f;
1304
0
                        } else {
1305
0
                            uint8_t cf_ctx;
1306
0
                            cf = bitfn(t->cf);
1307
0
                            eob = decode_coefs(t, &t->a->lcoef[bx4 + x],
1308
0
                                               &t->l.lcoef[by4 + y], b->tx, bs,
1309
0
                                               b, 1, 0, cf, &txtp, &cf_ctx);
1310
0
                            if (DEBUG_BLOCK_INFO)
1311
0
                                printf("Post-y-cf-blk[tx=%d,txtp=%d,eob=%d]: r=%d\n",
1312
0
                                       b->tx, txtp, eob, ts->msac.rng);
1313
0
                            dav1d_memset_likely_pow2(&t->a->lcoef[bx4 + x], cf_ctx, imin(t_dim->w, f->bw - t->bx));
1314
0
                            dav1d_memset_likely_pow2(&t->l.lcoef[by4 + y], cf_ctx, imin(t_dim->h, f->bh - t->by));
1315
0
                        }
1316
0
                        if (eob >= 0) {
1317
0
                            if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS)
1318
0
                                coef_dump(cf, imin(t_dim->h, 8) * 4,
1319
0
                                          imin(t_dim->w, 8) * 4, 3, "dq");
1320
0
                            dsp->itx.itxfm_add[b->tx]
1321
0
                                              [txtp](dst,
1322
0
                                                     f->cur.stride[0],
1323
0
                                                     cf, eob HIGHBD_CALL_SUFFIX);
1324
0
                            if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS)
1325
0
                                hex_dump(dst, f->cur.stride[0],
1326
0
                                         t_dim->w * 4, t_dim->h * 4, "recon");
1327
0
                        }
1328
0
                    } else if (!t->frame_thread.pass) {
1329
0
                        dav1d_memset_pow2[t_dim->lw](&t->a->lcoef[bx4 + x], 0x40);
1330
0
                        dav1d_memset_pow2[t_dim->lh](&t->l.lcoef[by4 + y], 0x40);
1331
0
                    }
1332
0
                    dst += 4 * t_dim->w;
1333
0
                }
1334
0
                t->bx -= x;
1335
0
            }
1336
0
            t->by -= y;
1337
1338
0
            if (!has_chroma) continue;
1339
1340
0
            const ptrdiff_t stride = f->cur.stride[1];
1341
1342
0
            if (b->uv_mode == CFL_PRED) {
1343
0
                assert(!init_x && !init_y);
1344
1345
0
                int16_t *const ac = t->scratch.ac;
1346
0
                pixel *y_src = ((pixel *) f->cur.data[0]) + 4 * (t->bx & ~ss_hor) +
1347
0
                                 4 * (t->by & ~ss_ver) * PXSTRIDE(f->cur.stride[0]);
1348
0
                const ptrdiff_t uv_off = 4 * ((t->bx >> ss_hor) +
1349
0
                                              (t->by >> ss_ver) * PXSTRIDE(stride));
1350
0
                pixel *const uv_dst[2] = { ((pixel *) f->cur.data[1]) + uv_off,
1351
0
                                           ((pixel *) f->cur.data[2]) + uv_off };
1352
1353
0
                const int furthest_r =
1354
0
                    ((cw4 << ss_hor) + t_dim->w - 1) & ~(t_dim->w - 1);
1355
0
                const int furthest_b =
1356
0
                    ((ch4 << ss_ver) + t_dim->h - 1) & ~(t_dim->h - 1);
1357
0
                dsp->ipred.cfl_ac[f->cur.p.layout - 1](ac, y_src, f->cur.stride[0],
1358
0
                                                         cbw4 - (furthest_r >> ss_hor),
1359
0
                                                         cbh4 - (furthest_b >> ss_ver),
1360
0
                                                         cbw4 * 4, cbh4 * 4);
1361
0
                for (int pl = 0; pl < 2; pl++) {
1362
0
                    if (!b->cfl_alpha[pl]) continue;
1363
0
                    int angle = 0;
1364
0
                    const pixel *top_sb_edge = NULL;
1365
0
                    if (!((t->by & ~ss_ver) & (f->sb_step - 1))) {
1366
0
                        top_sb_edge = f->ipred_edge[pl + 1];
1367
0
                        const int sby = t->by >> f->sb_shift;
1368
0
                        top_sb_edge += f->sb128w * 128 * (sby - 1);
1369
0
                    }
1370
0
                    const int xpos = t->bx >> ss_hor, ypos = t->by >> ss_ver;
1371
0
                    const int xstart = ts->tiling.col_start >> ss_hor;
1372
0
                    const int ystart = ts->tiling.row_start >> ss_ver;
1373
0
                    const enum IntraPredMode m =
1374
0
                        bytefn(dav1d_prepare_intra_edges)(xpos, xpos > xstart,
1375
0
                                                          ypos, ypos > ystart,
1376
0
                                                          ts->tiling.col_end >> ss_hor,
1377
0
                                                          ts->tiling.row_end >> ss_ver,
1378
0
                                                          0, uv_dst[pl], stride,
1379
0
                                                          top_sb_edge, DC_PRED, &angle,
1380
0
                                                          uv_t_dim->w, uv_t_dim->h, 0,
1381
0
                                                          edge HIGHBD_CALL_SUFFIX);
1382
0
                    dsp->ipred.cfl_pred[m](uv_dst[pl], stride, edge,
1383
0
                                           uv_t_dim->w * 4,
1384
0
                                           uv_t_dim->h * 4,
1385
0
                                           ac, b->cfl_alpha[pl]
1386
0
                                           HIGHBD_CALL_SUFFIX);
1387
0
                }
1388
0
                if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS) {
1389
0
                    ac_dump(ac, 4*cbw4, 4*cbh4, "ac");
1390
0
                    hex_dump(uv_dst[0], stride, cbw4 * 4, cbh4 * 4, "u-cfl-pred");
1391
0
                    hex_dump(uv_dst[1], stride, cbw4 * 4, cbh4 * 4, "v-cfl-pred");
1392
0
                }
1393
0
            } else if (b->pal_sz[1]) {
1394
0
                const ptrdiff_t uv_dstoff = 4 * ((t->bx >> ss_hor) +
1395
0
                                              (t->by >> ss_ver) * PXSTRIDE(f->cur.stride[1]));
1396
0
                const pixel (*pal)[8];
1397
0
                const uint8_t *pal_idx;
1398
0
                if (t->frame_thread.pass) {
1399
0
                    const int p = t->frame_thread.pass & 1;
1400
0
                    assert(ts->frame_thread[p].pal_idx);
1401
0
                    pal = f->frame_thread.pal[((t->by >> 1) + (t->bx & 1)) * (f->b4_stride >> 1) +
1402
0
                                              ((t->bx >> 1) + (t->by & 1))];
1403
0
                    pal_idx = ts->frame_thread[p].pal_idx;
1404
0
                    ts->frame_thread[p].pal_idx += cbw4 * cbh4 * 8;
1405
0
                } else {
1406
0
                    pal = bytefn(t->scratch.pal);
1407
0
                    pal_idx = t->scratch.pal_idx_uv;
1408
0
                }
1409
1410
0
                f->dsp->ipred.pal_pred(((pixel *) f->cur.data[1]) + uv_dstoff,
1411
0
                                       f->cur.stride[1], pal[1],
1412
0
                                       pal_idx, cbw4 * 4, cbh4 * 4);
1413
0
                f->dsp->ipred.pal_pred(((pixel *) f->cur.data[2]) + uv_dstoff,
1414
0
                                       f->cur.stride[1], pal[2],
1415
0
                                       pal_idx, cbw4 * 4, cbh4 * 4);
1416
0
                if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS) {
1417
0
                    hex_dump(((pixel *) f->cur.data[1]) + uv_dstoff,
1418
0
                             PXSTRIDE(f->cur.stride[1]),
1419
0
                             cbw4 * 4, cbh4 * 4, "u-pal-pred");
1420
0
                    hex_dump(((pixel *) f->cur.data[2]) + uv_dstoff,
1421
0
                             PXSTRIDE(f->cur.stride[1]),
1422
0
                             cbw4 * 4, cbh4 * 4, "v-pal-pred");
1423
0
                }
1424
0
            }
1425
1426
0
            const int sm_uv_fl = sm_uv_flag(t->a, cbx4) |
1427
0
                                 sm_uv_flag(&t->l, cby4);
1428
0
            const int uv_sb_has_tr =
1429
0
                ((init_x + 16) >> ss_hor) < cw4 ? 1 : init_y ? 0 :
1430
0
                intra_edge_flags & (EDGE_I420_TOP_HAS_RIGHT >> (f->cur.p.layout - 1));
1431
0
            const int uv_sb_has_bl =
1432
0
                init_x ? 0 : ((init_y + 16) >> ss_ver) < ch4 ? 1 :
1433
0
                intra_edge_flags & (EDGE_I420_LEFT_HAS_BOTTOM >> (f->cur.p.layout - 1));
1434
0
            const int sub_cw4 = imin(cw4, (init_x + 16) >> ss_hor);
1435
0
            for (int pl = 0; pl < 2; pl++) {
1436
0
                for (y = init_y >> ss_ver, t->by += init_y; y < sub_ch4;
1437
0
                     y += uv_t_dim->h, t->by += uv_t_dim->h << ss_ver)
1438
0
                {
1439
0
                    pixel *dst = ((pixel *) f->cur.data[1 + pl]) +
1440
0
                                   4 * ((t->by >> ss_ver) * PXSTRIDE(stride) +
1441
0
                                        ((t->bx + init_x) >> ss_hor));
1442
0
                    for (x = init_x >> ss_hor, t->bx += init_x; x < sub_cw4;
1443
0
                         x += uv_t_dim->w, t->bx += uv_t_dim->w << ss_hor)
1444
0
                    {
1445
0
                        if ((b->uv_mode == CFL_PRED && b->cfl_alpha[pl]) ||
1446
0
                            b->pal_sz[1])
1447
0
                        {
1448
0
                            goto skip_uv_pred;
1449
0
                        }
1450
1451
0
                        int angle = b->uv_angle;
1452
                        // this probably looks weird because we're using
1453
                        // luma flags in a chroma loop, but that's because
1454
                        // prepare_intra_edges() expects luma flags as input
1455
0
                        const enum EdgeFlags edge_flags =
1456
0
                            (((y > (init_y >> ss_ver) || !uv_sb_has_tr) &&
1457
0
                              (x + uv_t_dim->w >= sub_cw4)) ?
1458
0
                                 0 : EDGE_I444_TOP_HAS_RIGHT) |
1459
0
                            ((x > (init_x >> ss_hor) ||
1460
0
                              (!uv_sb_has_bl && y + uv_t_dim->h >= sub_ch4)) ?
1461
0
                                 0 : EDGE_I444_LEFT_HAS_BOTTOM);
1462
0
                        const pixel *top_sb_edge = NULL;
1463
0
                        if (!((t->by & ~ss_ver) & (f->sb_step - 1))) {
1464
0
                            top_sb_edge = f->ipred_edge[1 + pl];
1465
0
                            const int sby = t->by >> f->sb_shift;
1466
0
                            top_sb_edge += f->sb128w * 128 * (sby - 1);
1467
0
                        }
1468
0
                        const enum IntraPredMode uv_mode =
1469
0
                             b->uv_mode == CFL_PRED ? DC_PRED : b->uv_mode;
1470
0
                        const int xpos = t->bx >> ss_hor, ypos = t->by >> ss_ver;
1471
0
                        const int xstart = ts->tiling.col_start >> ss_hor;
1472
0
                        const int ystart = ts->tiling.row_start >> ss_ver;
1473
0
                        const enum IntraPredMode m =
1474
0
                            bytefn(dav1d_prepare_intra_edges)(xpos, xpos > xstart,
1475
0
                                                              ypos, ypos > ystart,
1476
0
                                                              ts->tiling.col_end >> ss_hor,
1477
0
                                                              ts->tiling.row_end >> ss_ver,
1478
0
                                                              edge_flags, dst, stride,
1479
0
                                                              top_sb_edge, uv_mode,
1480
0
                                                              &angle, uv_t_dim->w,
1481
0
                                                              uv_t_dim->h,
1482
0
                                                              f->seq_hdr->intra_edge_filter,
1483
0
                                                              edge HIGHBD_CALL_SUFFIX);
1484
0
                        angle |= intra_edge_filter_flag;
1485
0
                        dsp->ipred.intra_pred[m](dst, stride, edge,
1486
0
                                                 uv_t_dim->w * 4,
1487
0
                                                 uv_t_dim->h * 4,
1488
0
                                                 angle | sm_uv_fl,
1489
0
                                                 (4 * f->bw + ss_hor -
1490
0
                                                  4 * (t->bx & ~ss_hor)) >> ss_hor,
1491
0
                                                 (4 * f->bh + ss_ver -
1492
0
                                                  4 * (t->by & ~ss_ver)) >> ss_ver
1493
0
                                                 HIGHBD_CALL_SUFFIX);
1494
0
                        if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS) {
1495
0
                            hex_dump(edge - uv_t_dim->h * 4, uv_t_dim->h * 4,
1496
0
                                     uv_t_dim->h * 4, 2, "l");
1497
0
                            hex_dump(edge, 0, 1, 1, "tl");
1498
0
                            hex_dump(edge + 1, uv_t_dim->w * 4,
1499
0
                                     uv_t_dim->w * 4, 2, "t");
1500
0
                            hex_dump(dst, stride, uv_t_dim->w * 4,
1501
0
                                     uv_t_dim->h * 4, pl ? "v-intra-pred" : "u-intra-pred");
1502
0
                        }
1503
1504
0
                    skip_uv_pred: {}
1505
0
                        if (!b->skip) {
1506
0
                            enum TxfmType txtp;
1507
0
                            int eob;
1508
0
                            coef *cf;
1509
0
                            if (t->frame_thread.pass) {
1510
0
                                const int p = t->frame_thread.pass & 1;
1511
0
                                const int cbi = *ts->frame_thread[p].cbi++;
1512
0
                                cf = ts->frame_thread[p].cf;
1513
0
                                ts->frame_thread[p].cf += uv_t_dim->w * uv_t_dim->h * 16;
1514
0
                                eob  = cbi >> 5;
1515
0
                                txtp = cbi & 0x1f;
1516
0
                            } else {
1517
0
                                uint8_t cf_ctx;
1518
0
                                cf = bitfn(t->cf);
1519
0
                                eob = decode_coefs(t, &t->a->ccoef[pl][cbx4 + x],
1520
0
                                                   &t->l.ccoef[pl][cby4 + y],
1521
0
                                                   b->uvtx, bs, b, 1, 1 + pl, cf,
1522
0
                                                   &txtp, &cf_ctx);
1523
0
                                if (DEBUG_BLOCK_INFO)
1524
0
                                    printf("Post-uv-cf-blk[pl=%d,tx=%d,"
1525
0
                                           "txtp=%d,eob=%d]: r=%d [x=%d,cbx4=%d]\n",
1526
0
                                           pl, b->uvtx, txtp, eob, ts->msac.rng, x, cbx4);
1527
0
                                int ctw = imin(uv_t_dim->w, (f->bw - t->bx + ss_hor) >> ss_hor);
1528
0
                                int cth = imin(uv_t_dim->h, (f->bh - t->by + ss_ver) >> ss_ver);
1529
0
                                dav1d_memset_likely_pow2(&t->a->ccoef[pl][cbx4 + x], cf_ctx, ctw);
1530
0
                                dav1d_memset_likely_pow2(&t->l.ccoef[pl][cby4 + y], cf_ctx, cth);
1531
0
                            }
1532
0
                            if (eob >= 0) {
1533
0
                                if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS)
1534
0
                                    coef_dump(cf, uv_t_dim->h * 4,
1535
0
                                              uv_t_dim->w * 4, 3, "dq");
1536
0
                                dsp->itx.itxfm_add[b->uvtx]
1537
0
                                                  [txtp](dst, stride,
1538
0
                                                         cf, eob HIGHBD_CALL_SUFFIX);
1539
0
                                if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS)
1540
0
                                    hex_dump(dst, stride, uv_t_dim->w * 4,
1541
0
                                             uv_t_dim->h * 4, "recon");
1542
0
                            }
1543
0
                        } else if (!t->frame_thread.pass) {
1544
0
                            dav1d_memset_pow2[uv_t_dim->lw](&t->a->ccoef[pl][cbx4 + x], 0x40);
1545
0
                            dav1d_memset_pow2[uv_t_dim->lh](&t->l.ccoef[pl][cby4 + y], 0x40);
1546
0
                        }
1547
0
                        dst += uv_t_dim->w * 4;
1548
0
                    }
1549
0
                    t->bx -= x << ss_hor;
1550
0
                }
1551
0
                t->by -= y << ss_ver;
1552
0
            }
1553
0
        }
1554
0
    }
1555
0
}
Unexecuted instantiation: dav1d_recon_b_intra_8bpc
Unexecuted instantiation: dav1d_recon_b_intra_16bpc
1556
1557
int bytefn(dav1d_recon_b_inter)(Dav1dTaskContext *const t, const enum BlockSize bs,
1558
                                const Av1Block *const b)
1559
0
{
1560
0
    Dav1dTileState *const ts = t->ts;
1561
0
    const Dav1dFrameContext *const f = t->f;
1562
0
    const Dav1dDSPContext *const dsp = f->dsp;
1563
0
    const int bx4 = t->bx & 31, by4 = t->by & 31;
1564
0
    const int ss_ver = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
1565
0
    const int ss_hor = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
1566
0
    const int cbx4 = bx4 >> ss_hor, cby4 = by4 >> ss_ver;
1567
0
    const uint8_t *const b_dim = dav1d_block_dimensions[bs];
1568
0
    const int bw4 = b_dim[0], bh4 = b_dim[1];
1569
0
    const int w4 = imin(bw4, f->bw - t->bx), h4 = imin(bh4, f->bh - t->by);
1570
0
    const int has_chroma = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I400 &&
1571
0
                           (bw4 > ss_hor || t->bx & 1) &&
1572
0
                           (bh4 > ss_ver || t->by & 1);
1573
0
    const int chr_layout_idx = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I400 ? 0 :
1574
0
                               DAV1D_PIXEL_LAYOUT_I444 - f->cur.p.layout;
1575
0
    int res;
1576
1577
    // prediction
1578
0
    const int cbh4 = (bh4 + ss_ver) >> ss_ver, cbw4 = (bw4 + ss_hor) >> ss_hor;
1579
0
    pixel *dst = ((pixel *) f->cur.data[0]) +
1580
0
        4 * (t->by * PXSTRIDE(f->cur.stride[0]) + t->bx);
1581
0
    const ptrdiff_t uvdstoff =
1582
0
        4 * ((t->bx >> ss_hor) + (t->by >> ss_ver) * PXSTRIDE(f->cur.stride[1]));
1583
0
    if (IS_KEY_OR_INTRA(f->frame_hdr)) {
1584
        // intrabc
1585
0
        assert(!f->frame_hdr->super_res.enabled);
1586
0
        res = mc(t, dst, NULL, f->cur.stride[0], bw4, bh4, t->bx, t->by, 0,
1587
0
                 b->mv[0], &f->sr_cur, 0 /* unused */, FILTER_2D_BILINEAR);
1588
0
        if (res) return res;
1589
0
        if (has_chroma) for (int pl = 1; pl < 3; pl++) {
1590
0
            res = mc(t, ((pixel *)f->cur.data[pl]) + uvdstoff, NULL, f->cur.stride[1],
1591
0
                     bw4 << (bw4 == ss_hor), bh4 << (bh4 == ss_ver),
1592
0
                     t->bx & ~ss_hor, t->by & ~ss_ver, pl, b->mv[0],
1593
0
                     &f->sr_cur, 0 /* unused */, FILTER_2D_BILINEAR);
1594
0
            if (res) return res;
1595
0
        }
1596
0
    } else if (b->comp_type == COMP_INTER_NONE) {
1597
0
        const Dav1dThreadPicture *const refp = &f->refp[b->ref[0]];
1598
0
        const enum Filter2d filter_2d = b->filter2d;
1599
1600
0
        if (imin(bw4, bh4) > 1 &&
1601
0
            ((b->inter_mode == GLOBALMV && f->gmv_warp_allowed[b->ref[0]]) ||
1602
0
             (b->motion_mode == MM_WARP && t->warpmv.type > DAV1D_WM_TYPE_TRANSLATION)))
1603
0
        {
1604
0
            res = warp_affine(t, dst, NULL, f->cur.stride[0], b_dim, 0, refp,
1605
0
                              b->motion_mode == MM_WARP ? &t->warpmv :
1606
0
                                  &f->frame_hdr->gmv[b->ref[0]]);
1607
0
            if (res) return res;
1608
0
        } else {
1609
0
            res = mc(t, dst, NULL, f->cur.stride[0],
1610
0
                     bw4, bh4, t->bx, t->by, 0, b->mv[0], refp, b->ref[0], filter_2d);
1611
0
            if (res) return res;
1612
0
            if (b->motion_mode == MM_OBMC) {
1613
0
                res = obmc(t, dst, f->cur.stride[0], b_dim, 0, bx4, by4, w4, h4);
1614
0
                if (res) return res;
1615
0
            }
1616
0
        }
1617
0
        if (b->interintra_type) {
1618
0
            pixel *const tl_edge = bitfn(t->scratch.edge) + 32;
1619
0
            enum IntraPredMode m = b->interintra_mode == II_SMOOTH_PRED ?
1620
0
                                   SMOOTH_PRED : b->interintra_mode;
1621
0
            pixel *const tmp = bitfn(t->scratch.interintra);
1622
0
            int angle = 0;
1623
0
            const pixel *top_sb_edge = NULL;
1624
0
            if (!(t->by & (f->sb_step - 1))) {
1625
0
                top_sb_edge = f->ipred_edge[0];
1626
0
                const int sby = t->by >> f->sb_shift;
1627
0
                top_sb_edge += f->sb128w * 128 * (sby - 1);
1628
0
            }
1629
0
            m = bytefn(dav1d_prepare_intra_edges)(t->bx, t->bx > ts->tiling.col_start,
1630
0
                                                  t->by, t->by > ts->tiling.row_start,
1631
0
                                                  ts->tiling.col_end, ts->tiling.row_end,
1632
0
                                                  0, dst, f->cur.stride[0], top_sb_edge,
1633
0
                                                  m, &angle, bw4, bh4, 0, tl_edge
1634
0
                                                  HIGHBD_CALL_SUFFIX);
1635
0
            dsp->ipred.intra_pred[m](tmp, 4 * bw4 * sizeof(pixel),
1636
0
                                     tl_edge, bw4 * 4, bh4 * 4, 0, 0, 0
1637
0
                                     HIGHBD_CALL_SUFFIX);
1638
0
            dsp->mc.blend(dst, f->cur.stride[0], tmp,
1639
0
                          bw4 * 4, bh4 * 4, II_MASK(0, bs, b));
1640
0
        }
1641
1642
0
        if (!has_chroma) goto skip_inter_chroma_pred;
1643
1644
        // sub8x8 derivation
1645
0
        int is_sub8x8 = bw4 == ss_hor || bh4 == ss_ver;
1646
0
        refmvs_block *const *r;
1647
0
        if (is_sub8x8) {
1648
0
            assert(ss_hor == 1);
1649
0
            r = &t->rt.r[(t->by & 31) + 5];
1650
0
            if (bw4 == 1) is_sub8x8 &= r[0][t->bx - 1].ref.ref[0] > 0;
1651
0
            if (bh4 == ss_ver) is_sub8x8 &= r[-1][t->bx].ref.ref[0] > 0;
1652
0
            if (bw4 == 1 && bh4 == ss_ver)
1653
0
                is_sub8x8 &= r[-1][t->bx - 1].ref.ref[0] > 0;
1654
0
        }
1655
1656
        // chroma prediction
1657
0
        if (is_sub8x8) {
1658
0
            assert(ss_hor == 1);
1659
0
            ptrdiff_t h_off = 0, v_off = 0;
1660
0
            if (bw4 == 1 && bh4 == ss_ver) {
1661
0
                for (int pl = 0; pl < 2; pl++) {
1662
0
                    res = mc(t, ((pixel *) f->cur.data[1 + pl]) + uvdstoff,
1663
0
                             NULL, f->cur.stride[1],
1664
0
                             bw4, bh4, t->bx - 1, t->by - 1, 1 + pl,
1665
0
                             r[-1][t->bx - 1].mv.mv[0],
1666
0
                             &f->refp[r[-1][t->bx - 1].ref.ref[0] - 1],
1667
0
                             r[-1][t->bx - 1].ref.ref[0] - 1,
1668
0
                             t->frame_thread.pass != 2 ? t->tl_4x4_filter :
1669
0
                                 f->frame_thread.b[((t->by - 1) * f->b4_stride) + t->bx - 1].filter2d);
1670
0
                    if (res) return res;
1671
0
                }
1672
0
                v_off = 2 * PXSTRIDE(f->cur.stride[1]);
1673
0
                h_off = 2;
1674
0
            }
1675
0
            if (bw4 == 1) {
1676
0
                const enum Filter2d left_filter_2d =
1677
0
                    dav1d_filter_2d[t->l.filter[1][by4]][t->l.filter[0][by4]];
1678
0
                for (int pl = 0; pl < 2; pl++) {
1679
0
                    res = mc(t, ((pixel *) f->cur.data[1 + pl]) + uvdstoff + v_off, NULL,
1680
0
                             f->cur.stride[1], bw4, bh4, t->bx - 1,
1681
0
                             t->by, 1 + pl, r[0][t->bx - 1].mv.mv[0],
1682
0
                             &f->refp[r[0][t->bx - 1].ref.ref[0] - 1],
1683
0
                             r[0][t->bx - 1].ref.ref[0] - 1,
1684
0
                             t->frame_thread.pass != 2 ? left_filter_2d :
1685
0
                                 f->frame_thread.b[(t->by * f->b4_stride) + t->bx - 1].filter2d);
1686
0
                    if (res) return res;
1687
0
                }
1688
0
                h_off = 2;
1689
0
            }
1690
0
            if (bh4 == ss_ver) {
1691
0
                const enum Filter2d top_filter_2d =
1692
0
                    dav1d_filter_2d[t->a->filter[1][bx4]][t->a->filter[0][bx4]];
1693
0
                for (int pl = 0; pl < 2; pl++) {
1694
0
                    res = mc(t, ((pixel *) f->cur.data[1 + pl]) + uvdstoff + h_off, NULL,
1695
0
                             f->cur.stride[1], bw4, bh4, t->bx, t->by - 1,
1696
0
                             1 + pl, r[-1][t->bx].mv.mv[0],
1697
0
                             &f->refp[r[-1][t->bx].ref.ref[0] - 1],
1698
0
                             r[-1][t->bx].ref.ref[0] - 1,
1699
0
                             t->frame_thread.pass != 2 ? top_filter_2d :
1700
0
                                 f->frame_thread.b[((t->by - 1) * f->b4_stride) + t->bx].filter2d);
1701
0
                    if (res) return res;
1702
0
                }
1703
0
                v_off = 2 * PXSTRIDE(f->cur.stride[1]);
1704
0
            }
1705
0
            for (int pl = 0; pl < 2; pl++) {
1706
0
                res = mc(t, ((pixel *) f->cur.data[1 + pl]) + uvdstoff + h_off + v_off, NULL, f->cur.stride[1],
1707
0
                         bw4, bh4, t->bx, t->by, 1 + pl, b->mv[0],
1708
0
                         refp, b->ref[0], filter_2d);
1709
0
                if (res) return res;
1710
0
            }
1711
0
        } else {
1712
0
            if (imin(cbw4, cbh4) > 1 &&
1713
0
                ((b->inter_mode == GLOBALMV && f->gmv_warp_allowed[b->ref[0]]) ||
1714
0
                 (b->motion_mode == MM_WARP && t->warpmv.type > DAV1D_WM_TYPE_TRANSLATION)))
1715
0
            {
1716
0
                for (int pl = 0; pl < 2; pl++) {
1717
0
                    res = warp_affine(t, ((pixel *) f->cur.data[1 + pl]) + uvdstoff, NULL,
1718
0
                                      f->cur.stride[1], b_dim, 1 + pl, refp,
1719
0
                                      b->motion_mode == MM_WARP ? &t->warpmv :
1720
0
                                          &f->frame_hdr->gmv[b->ref[0]]);
1721
0
                    if (res) return res;
1722
0
                }
1723
0
            } else {
1724
0
                for (int pl = 0; pl < 2; pl++) {
1725
0
                    res = mc(t, ((pixel *) f->cur.data[1 + pl]) + uvdstoff,
1726
0
                             NULL, f->cur.stride[1],
1727
0
                             bw4 << (bw4 == ss_hor), bh4 << (bh4 == ss_ver),
1728
0
                             t->bx & ~ss_hor, t->by & ~ss_ver,
1729
0
                             1 + pl, b->mv[0], refp, b->ref[0], filter_2d);
1730
0
                    if (res) return res;
1731
0
                    if (b->motion_mode == MM_OBMC) {
1732
0
                        res = obmc(t, ((pixel *) f->cur.data[1 + pl]) + uvdstoff,
1733
0
                                   f->cur.stride[1], b_dim, 1 + pl, bx4, by4, w4, h4);
1734
0
                        if (res) return res;
1735
0
                    }
1736
0
                }
1737
0
            }
1738
0
            if (b->interintra_type) {
1739
                // FIXME for 8x32 with 4:2:2 subsampling, this probably does
1740
                // the wrong thing since it will select 4x16, not 4x32, as a
1741
                // transform size...
1742
0
                const uint8_t *const ii_mask = II_MASK(chr_layout_idx, bs, b);
1743
1744
0
                for (int pl = 0; pl < 2; pl++) {
1745
0
                    pixel *const tmp = bitfn(t->scratch.interintra);
1746
0
                    pixel *const tl_edge = bitfn(t->scratch.edge) + 32;
1747
0
                    enum IntraPredMode m =
1748
0
                        b->interintra_mode == II_SMOOTH_PRED ?
1749
0
                        SMOOTH_PRED : b->interintra_mode;
1750
0
                    int angle = 0;
1751
0
                    pixel *const uvdst = ((pixel *) f->cur.data[1 + pl]) + uvdstoff;
1752
0
                    const pixel *top_sb_edge = NULL;
1753
0
                    if (!(t->by & (f->sb_step - 1))) {
1754
0
                        top_sb_edge = f->ipred_edge[pl + 1];
1755
0
                        const int sby = t->by >> f->sb_shift;
1756
0
                        top_sb_edge += f->sb128w * 128 * (sby - 1);
1757
0
                    }
1758
0
                    m = bytefn(dav1d_prepare_intra_edges)(t->bx >> ss_hor,
1759
0
                                                          (t->bx >> ss_hor) >
1760
0
                                                              (ts->tiling.col_start >> ss_hor),
1761
0
                                                          t->by >> ss_ver,
1762
0
                                                          (t->by >> ss_ver) >
1763
0
                                                              (ts->tiling.row_start >> ss_ver),
1764
0
                                                          ts->tiling.col_end >> ss_hor,
1765
0
                                                          ts->tiling.row_end >> ss_ver,
1766
0
                                                          0, uvdst, f->cur.stride[1],
1767
0
                                                          top_sb_edge, m,
1768
0
                                                          &angle, cbw4, cbh4, 0, tl_edge
1769
0
                                                          HIGHBD_CALL_SUFFIX);
1770
0
                    dsp->ipred.intra_pred[m](tmp, cbw4 * 4 * sizeof(pixel),
1771
0
                                             tl_edge, cbw4 * 4, cbh4 * 4, 0, 0, 0
1772
0
                                             HIGHBD_CALL_SUFFIX);
1773
0
                    dsp->mc.blend(uvdst, f->cur.stride[1], tmp,
1774
0
                                  cbw4 * 4, cbh4 * 4, ii_mask);
1775
0
                }
1776
0
            }
1777
0
        }
1778
1779
0
    skip_inter_chroma_pred: {}
1780
0
        t->tl_4x4_filter = filter_2d;
1781
0
    } else {
1782
0
        const enum Filter2d filter_2d = b->filter2d;
1783
        // Maximum super block size is 128x128
1784
0
        int16_t (*tmp)[128 * 128] = t->scratch.compinter;
1785
0
        int jnt_weight;
1786
0
        uint8_t *const seg_mask = t->scratch.seg_mask;
1787
0
        const uint8_t *mask;
1788
1789
0
        for (int i = 0; i < 2; i++) {
1790
0
            const Dav1dThreadPicture *const refp = &f->refp[b->ref[i]];
1791
1792
0
            if (b->inter_mode == GLOBALMV_GLOBALMV && f->gmv_warp_allowed[b->ref[i]]) {
1793
0
                res = warp_affine(t, NULL, tmp[i], bw4 * 4, b_dim, 0, refp,
1794
0
                                  &f->frame_hdr->gmv[b->ref[i]]);
1795
0
                if (res) return res;
1796
0
            } else {
1797
0
                res = mc(t, NULL, tmp[i], 0, bw4, bh4, t->bx, t->by, 0,
1798
0
                         b->mv[i], refp, b->ref[i], filter_2d);
1799
0
                if (res) return res;
1800
0
            }
1801
0
        }
1802
0
        switch (b->comp_type) {
1803
0
        case COMP_INTER_AVG:
1804
0
            dsp->mc.avg(dst, f->cur.stride[0], tmp[0], tmp[1],
1805
0
                        bw4 * 4, bh4 * 4 HIGHBD_CALL_SUFFIX);
1806
0
            break;
1807
0
        case COMP_INTER_WEIGHTED_AVG:
1808
0
            jnt_weight = f->jnt_weights[b->ref[0]][b->ref[1]];
1809
0
            dsp->mc.w_avg(dst, f->cur.stride[0], tmp[0], tmp[1],
1810
0
                          bw4 * 4, bh4 * 4, jnt_weight HIGHBD_CALL_SUFFIX);
1811
0
            break;
1812
0
        case COMP_INTER_SEG:
1813
0
            dsp->mc.w_mask[chr_layout_idx](dst, f->cur.stride[0],
1814
0
                                           tmp[b->mask_sign], tmp[!b->mask_sign],
1815
0
                                           bw4 * 4, bh4 * 4, seg_mask,
1816
0
                                           b->mask_sign HIGHBD_CALL_SUFFIX);
1817
0
            mask = seg_mask;
1818
0
            break;
1819
0
        case COMP_INTER_WEDGE:
1820
0
            mask = WEDGE_MASK(0, bs, 0, b->wedge_idx);
1821
0
            dsp->mc.mask(dst, f->cur.stride[0],
1822
0
                         tmp[b->mask_sign], tmp[!b->mask_sign],
1823
0
                         bw4 * 4, bh4 * 4, mask HIGHBD_CALL_SUFFIX);
1824
0
            if (has_chroma)
1825
0
                mask = WEDGE_MASK(chr_layout_idx, bs, b->mask_sign, b->wedge_idx);
1826
0
            break;
1827
0
        }
1828
1829
        // chroma
1830
0
        if (has_chroma) for (int pl = 0; pl < 2; pl++) {
1831
0
            for (int i = 0; i < 2; i++) {
1832
0
                const Dav1dThreadPicture *const refp = &f->refp[b->ref[i]];
1833
0
                if (b->inter_mode == GLOBALMV_GLOBALMV &&
1834
0
                    imin(cbw4, cbh4) > 1 && f->gmv_warp_allowed[b->ref[i]])
1835
0
                {
1836
0
                    res = warp_affine(t, NULL, tmp[i], bw4 * 4 >> ss_hor,
1837
0
                                      b_dim, 1 + pl,
1838
0
                                      refp, &f->frame_hdr->gmv[b->ref[i]]);
1839
0
                    if (res) return res;
1840
0
                } else {
1841
0
                    res = mc(t, NULL, tmp[i], 0, bw4, bh4, t->bx, t->by,
1842
0
                             1 + pl, b->mv[i], refp, b->ref[i], filter_2d);
1843
0
                    if (res) return res;
1844
0
                }
1845
0
            }
1846
0
            pixel *const uvdst = ((pixel *) f->cur.data[1 + pl]) + uvdstoff;
1847
0
            switch (b->comp_type) {
1848
0
            case COMP_INTER_AVG:
1849
0
                dsp->mc.avg(uvdst, f->cur.stride[1], tmp[0], tmp[1],
1850
0
                            bw4 * 4 >> ss_hor, bh4 * 4 >> ss_ver
1851
0
                            HIGHBD_CALL_SUFFIX);
1852
0
                break;
1853
0
            case COMP_INTER_WEIGHTED_AVG:
1854
0
                dsp->mc.w_avg(uvdst, f->cur.stride[1], tmp[0], tmp[1],
1855
0
                              bw4 * 4 >> ss_hor, bh4 * 4 >> ss_ver, jnt_weight
1856
0
                              HIGHBD_CALL_SUFFIX);
1857
0
                break;
1858
0
            case COMP_INTER_WEDGE:
1859
0
            case COMP_INTER_SEG:
1860
0
                dsp->mc.mask(uvdst, f->cur.stride[1],
1861
0
                             tmp[b->mask_sign], tmp[!b->mask_sign],
1862
0
                             bw4 * 4 >> ss_hor, bh4 * 4 >> ss_ver, mask
1863
0
                             HIGHBD_CALL_SUFFIX);
1864
0
                break;
1865
0
            }
1866
0
        }
1867
0
    }
1868
1869
0
    if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS) {
1870
0
        hex_dump(dst, f->cur.stride[0], b_dim[0] * 4, b_dim[1] * 4, "y-pred");
1871
0
        if (has_chroma) {
1872
0
            hex_dump(&((pixel *) f->cur.data[1])[uvdstoff], f->cur.stride[1],
1873
0
                     cbw4 * 4, cbh4 * 4, "u-pred");
1874
0
            hex_dump(&((pixel *) f->cur.data[2])[uvdstoff], f->cur.stride[1],
1875
0
                     cbw4 * 4, cbh4 * 4, "v-pred");
1876
0
        }
1877
0
    }
1878
1879
0
    const int cw4 = (w4 + ss_hor) >> ss_hor, ch4 = (h4 + ss_ver) >> ss_ver;
1880
1881
0
    if (b->skip) {
1882
        // reset coef contexts
1883
0
        BlockContext *const a = t->a;
1884
0
        dav1d_memset_pow2[b_dim[2]](&a->lcoef[bx4], 0x40);
1885
0
        dav1d_memset_pow2[b_dim[3]](&t->l.lcoef[by4], 0x40);
1886
0
        if (has_chroma) {
1887
0
            dav1d_memset_pow2_fn memset_cw = dav1d_memset_pow2[ulog2(cbw4)];
1888
0
            dav1d_memset_pow2_fn memset_ch = dav1d_memset_pow2[ulog2(cbh4)];
1889
0
            memset_cw(&a->ccoef[0][cbx4], 0x40);
1890
0
            memset_cw(&a->ccoef[1][cbx4], 0x40);
1891
0
            memset_ch(&t->l.ccoef[0][cby4], 0x40);
1892
0
            memset_ch(&t->l.ccoef[1][cby4], 0x40);
1893
0
        }
1894
0
        return 0;
1895
0
    }
1896
1897
0
    const TxfmInfo *const uvtx = &dav1d_txfm_dimensions[b->uvtx];
1898
0
    const TxfmInfo *const ytx = &dav1d_txfm_dimensions[b->max_ytx];
1899
0
    const uint16_t tx_split[2] = { b->tx_split0, b->tx_split1 };
1900
1901
0
    for (int init_y = 0; init_y < bh4; init_y += 16) {
1902
0
        for (int init_x = 0; init_x < bw4; init_x += 16) {
1903
            // coefficient coding & inverse transforms
1904
0
            int y_off = !!init_y, y;
1905
0
            dst += PXSTRIDE(f->cur.stride[0]) * 4 * init_y;
1906
0
            for (y = init_y, t->by += init_y; y < imin(h4, init_y + 16);
1907
0
                 y += ytx->h, y_off++)
1908
0
            {
1909
0
                int x, x_off = !!init_x;
1910
0
                for (x = init_x, t->bx += init_x; x < imin(w4, init_x + 16);
1911
0
                     x += ytx->w, x_off++)
1912
0
                {
1913
0
                    read_coef_tree(t, bs, b, b->max_ytx, 0, tx_split,
1914
0
                                   x_off, y_off, &dst[x * 4]);
1915
0
                    t->bx += ytx->w;
1916
0
                }
1917
0
                dst += PXSTRIDE(f->cur.stride[0]) * 4 * ytx->h;
1918
0
                t->bx -= x;
1919
0
                t->by += ytx->h;
1920
0
            }
1921
0
            dst -= PXSTRIDE(f->cur.stride[0]) * 4 * y;
1922
0
            t->by -= y;
1923
1924
            // chroma coefs and inverse transform
1925
0
            if (has_chroma) for (int pl = 0; pl < 2; pl++) {
1926
0
                pixel *uvdst = ((pixel *) f->cur.data[1 + pl]) + uvdstoff +
1927
0
                    (PXSTRIDE(f->cur.stride[1]) * init_y * 4 >> ss_ver);
1928
0
                for (y = init_y >> ss_ver, t->by += init_y;
1929
0
                     y < imin(ch4, (init_y + 16) >> ss_ver); y += uvtx->h)
1930
0
                {
1931
0
                    int x;
1932
0
                    for (x = init_x >> ss_hor, t->bx += init_x;
1933
0
                         x < imin(cw4, (init_x + 16) >> ss_hor); x += uvtx->w)
1934
0
                    {
1935
0
                        coef *cf;
1936
0
                        int eob;
1937
0
                        enum TxfmType txtp;
1938
0
                        if (t->frame_thread.pass) {
1939
0
                            const int p = t->frame_thread.pass & 1;
1940
0
                            const int cbi = *ts->frame_thread[p].cbi++;
1941
0
                            cf = ts->frame_thread[p].cf;
1942
0
                            ts->frame_thread[p].cf += uvtx->w * uvtx->h * 16;
1943
0
                            eob  = cbi >> 5;
1944
0
                            txtp = cbi & 0x1f;
1945
0
                        } else {
1946
0
                            uint8_t cf_ctx;
1947
0
                            cf = bitfn(t->cf);
1948
0
                            txtp = t->scratch.txtp_map[(by4 + (y << ss_ver)) * 32 +
1949
0
                                                        bx4 + (x << ss_hor)];
1950
0
                            eob = decode_coefs(t, &t->a->ccoef[pl][cbx4 + x],
1951
0
                                               &t->l.ccoef[pl][cby4 + y],
1952
0
                                               b->uvtx, bs, b, 0, 1 + pl,
1953
0
                                               cf, &txtp, &cf_ctx);
1954
0
                            if (DEBUG_BLOCK_INFO)
1955
0
                                printf("Post-uv-cf-blk[pl=%d,tx=%d,"
1956
0
                                       "txtp=%d,eob=%d]: r=%d\n",
1957
0
                                       pl, b->uvtx, txtp, eob, ts->msac.rng);
1958
0
                            int ctw = imin(uvtx->w, (f->bw - t->bx + ss_hor) >> ss_hor);
1959
0
                            int cth = imin(uvtx->h, (f->bh - t->by + ss_ver) >> ss_ver);
1960
0
                            dav1d_memset_likely_pow2(&t->a->ccoef[pl][cbx4 + x], cf_ctx, ctw);
1961
0
                            dav1d_memset_likely_pow2(&t->l.ccoef[pl][cby4 + y], cf_ctx, cth);
1962
0
                        }
1963
0
                        if (eob >= 0) {
1964
0
                            if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS)
1965
0
                                coef_dump(cf, uvtx->h * 4, uvtx->w * 4, 3, "dq");
1966
0
                            dsp->itx.itxfm_add[b->uvtx]
1967
0
                                              [txtp](&uvdst[4 * x],
1968
0
                                                     f->cur.stride[1],
1969
0
                                                     cf, eob HIGHBD_CALL_SUFFIX);
1970
0
                            if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS)
1971
0
                                hex_dump(&uvdst[4 * x], f->cur.stride[1],
1972
0
                                         uvtx->w * 4, uvtx->h * 4, "recon");
1973
0
                        }
1974
0
                        t->bx += uvtx->w << ss_hor;
1975
0
                    }
1976
0
                    uvdst += PXSTRIDE(f->cur.stride[1]) * 4 * uvtx->h;
1977
0
                    t->bx -= x << ss_hor;
1978
0
                    t->by += uvtx->h << ss_ver;
1979
0
                }
1980
0
                t->by -= y << ss_ver;
1981
0
            }
1982
0
        }
1983
0
    }
1984
0
    return 0;
1985
0
}
Unexecuted instantiation: dav1d_recon_b_inter_8bpc
Unexecuted instantiation: dav1d_recon_b_inter_16bpc
1986
1987
0
void bytefn(dav1d_filter_sbrow_deblock_cols)(Dav1dFrameContext *const f, const int sby) {
1988
0
    if (!(f->c->inloop_filters & DAV1D_INLOOPFILTER_DEBLOCK) ||
1989
0
        (!f->frame_hdr->loopfilter.level_y[0] && !f->frame_hdr->loopfilter.level_y[1]))
1990
0
    {
1991
0
        return;
1992
0
    }
1993
0
    const int y = sby * f->sb_step * 4;
1994
0
    const int ss_ver = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
1995
0
    pixel *const p[3] = {
1996
0
        f->lf.p[0] + y * PXSTRIDE(f->cur.stride[0]),
1997
0
        f->lf.p[1] + (y * PXSTRIDE(f->cur.stride[1]) >> ss_ver),
1998
0
        f->lf.p[2] + (y * PXSTRIDE(f->cur.stride[1]) >> ss_ver)
1999
0
    };
2000
0
    Av1Filter *mask = f->lf.mask + (sby >> !f->seq_hdr->sb128) * f->sb128w;
2001
0
    bytefn(dav1d_loopfilter_sbrow_cols)(f, p, mask, sby,
2002
0
                                        f->lf.start_of_tile_row[sby]);
2003
0
}
Unexecuted instantiation: dav1d_filter_sbrow_deblock_cols_8bpc
Unexecuted instantiation: dav1d_filter_sbrow_deblock_cols_16bpc
2004
2005
0
void bytefn(dav1d_filter_sbrow_deblock_rows)(Dav1dFrameContext *const f, const int sby) {
2006
0
    const int y = sby * f->sb_step * 4;
2007
0
    const int ss_ver = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
2008
0
    pixel *const p[3] = {
2009
0
        f->lf.p[0] + y * PXSTRIDE(f->cur.stride[0]),
2010
0
        f->lf.p[1] + (y * PXSTRIDE(f->cur.stride[1]) >> ss_ver),
2011
0
        f->lf.p[2] + (y * PXSTRIDE(f->cur.stride[1]) >> ss_ver)
2012
0
    };
2013
0
    Av1Filter *mask = f->lf.mask + (sby >> !f->seq_hdr->sb128) * f->sb128w;
2014
0
    if (f->c->inloop_filters & DAV1D_INLOOPFILTER_DEBLOCK &&
2015
0
        (f->frame_hdr->loopfilter.level_y[0] || f->frame_hdr->loopfilter.level_y[1]))
2016
0
    {
2017
0
        bytefn(dav1d_loopfilter_sbrow_rows)(f, p, mask, sby);
2018
0
    }
2019
0
    if (f->seq_hdr->cdef || f->lf.restore_planes) {
2020
        // Store loop filtered pixels required by CDEF / LR
2021
0
        bytefn(dav1d_copy_lpf)(f, p, sby);
2022
0
    }
2023
0
}
Unexecuted instantiation: dav1d_filter_sbrow_deblock_rows_8bpc
Unexecuted instantiation: dav1d_filter_sbrow_deblock_rows_16bpc
2024
2025
0
void bytefn(dav1d_filter_sbrow_cdef)(Dav1dTaskContext *const tc, const int sby) {
2026
0
    const Dav1dFrameContext *const f = tc->f;
2027
0
    if (!(f->c->inloop_filters & DAV1D_INLOOPFILTER_CDEF)) return;
2028
0
    const int sbsz = f->sb_step;
2029
0
    const int y = sby * sbsz * 4;
2030
0
    const int ss_ver = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
2031
0
    pixel *const p[3] = {
2032
0
        f->lf.p[0] + y * PXSTRIDE(f->cur.stride[0]),
2033
0
        f->lf.p[1] + (y * PXSTRIDE(f->cur.stride[1]) >> ss_ver),
2034
0
        f->lf.p[2] + (y * PXSTRIDE(f->cur.stride[1]) >> ss_ver)
2035
0
    };
2036
0
    Av1Filter *prev_mask = f->lf.mask + ((sby - 1) >> !f->seq_hdr->sb128) * f->sb128w;
2037
0
    Av1Filter *mask = f->lf.mask + (sby >> !f->seq_hdr->sb128) * f->sb128w;
2038
0
    const int start = sby * sbsz;
2039
0
    if (sby) {
2040
0
        const int ss_ver = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
2041
0
        pixel *p_up[3] = {
2042
0
            p[0] - 8 * PXSTRIDE(f->cur.stride[0]),
2043
0
            p[1] - (8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver),
2044
0
            p[2] - (8 * PXSTRIDE(f->cur.stride[1]) >> ss_ver),
2045
0
        };
2046
0
        bytefn(dav1d_cdef_brow)(tc, p_up, prev_mask, start - 2, start, 1, sby);
2047
0
    }
2048
0
    const int n_blks = sbsz - 2 * (sby + 1 < f->sbh);
2049
0
    const int end = imin(start + n_blks, f->bh);
2050
0
    bytefn(dav1d_cdef_brow)(tc, p, mask, start, end, 0, sby);
2051
0
}
Unexecuted instantiation: dav1d_filter_sbrow_cdef_8bpc
Unexecuted instantiation: dav1d_filter_sbrow_cdef_16bpc
2052
2053
0
void bytefn(dav1d_filter_sbrow_resize)(Dav1dFrameContext *const f, const int sby) {
2054
0
    const int sbsz = f->sb_step;
2055
0
    const int y = sby * sbsz * 4;
2056
0
    const int ss_ver = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
2057
0
    const pixel *const p[3] = {
2058
0
        f->lf.p[0] + y * PXSTRIDE(f->cur.stride[0]),
2059
0
        f->lf.p[1] + (y * PXSTRIDE(f->cur.stride[1]) >> ss_ver),
2060
0
        f->lf.p[2] + (y * PXSTRIDE(f->cur.stride[1]) >> ss_ver)
2061
0
    };
2062
0
    pixel *const sr_p[3] = {
2063
0
        f->lf.sr_p[0] + y * PXSTRIDE(f->sr_cur.p.stride[0]),
2064
0
        f->lf.sr_p[1] + (y * PXSTRIDE(f->sr_cur.p.stride[1]) >> ss_ver),
2065
0
        f->lf.sr_p[2] + (y * PXSTRIDE(f->sr_cur.p.stride[1]) >> ss_ver)
2066
0
    };
2067
0
    const int has_chroma = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I400;
2068
0
    for (int pl = 0; pl < 1 + 2 * has_chroma; pl++) {
2069
0
        const int ss_ver = pl && f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
2070
0
        const int h_start = 8 * !!sby >> ss_ver;
2071
0
        const ptrdiff_t dst_stride = f->sr_cur.p.stride[!!pl];
2072
0
        pixel *dst = sr_p[pl] - h_start * PXSTRIDE(dst_stride);
2073
0
        const ptrdiff_t src_stride = f->cur.stride[!!pl];
2074
0
        const pixel *src = p[pl] - h_start * PXSTRIDE(src_stride);
2075
0
        const int h_end = 4 * (sbsz - 2 * (sby + 1 < f->sbh)) >> ss_ver;
2076
0
        const int ss_hor = pl && f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
2077
0
        const int dst_w = (f->sr_cur.p.p.w + ss_hor) >> ss_hor;
2078
0
        const int src_w = (4 * f->bw + ss_hor) >> ss_hor;
2079
0
        const int img_h = (f->cur.p.h - sbsz * 4 * sby + ss_ver) >> ss_ver;
2080
2081
0
        f->dsp->mc.resize(dst, dst_stride, src, src_stride, dst_w,
2082
0
                          imin(img_h, h_end) + h_start, src_w,
2083
0
                          f->resize_step[!!pl], f->resize_start[!!pl]
2084
0
                          HIGHBD_CALL_SUFFIX);
2085
0
    }
2086
0
}
Unexecuted instantiation: dav1d_filter_sbrow_resize_8bpc
Unexecuted instantiation: dav1d_filter_sbrow_resize_16bpc
2087
2088
0
void bytefn(dav1d_filter_sbrow_lr)(Dav1dFrameContext *const f, const int sby) {
2089
0
    if (!(f->c->inloop_filters & DAV1D_INLOOPFILTER_RESTORATION)) return;
2090
0
    const int y = sby * f->sb_step * 4;
2091
0
    const int ss_ver = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
2092
0
    pixel *const sr_p[3] = {
2093
0
        f->lf.sr_p[0] + y * PXSTRIDE(f->sr_cur.p.stride[0]),
2094
0
        f->lf.sr_p[1] + (y * PXSTRIDE(f->sr_cur.p.stride[1]) >> ss_ver),
2095
0
        f->lf.sr_p[2] + (y * PXSTRIDE(f->sr_cur.p.stride[1]) >> ss_ver)
2096
0
    };
2097
0
    bytefn(dav1d_lr_sbrow)(f, sr_p, sby);
2098
0
}
Unexecuted instantiation: dav1d_filter_sbrow_lr_8bpc
Unexecuted instantiation: dav1d_filter_sbrow_lr_16bpc
2099
2100
0
void bytefn(dav1d_filter_sbrow)(Dav1dFrameContext *const f, const int sby) {
2101
0
    bytefn(dav1d_filter_sbrow_deblock_cols)(f, sby);
2102
0
    bytefn(dav1d_filter_sbrow_deblock_rows)(f, sby);
2103
0
    if (f->seq_hdr->cdef)
2104
0
        bytefn(dav1d_filter_sbrow_cdef)(f->c->tc, sby);
2105
0
    if (f->frame_hdr->width[0] != f->frame_hdr->width[1])
2106
0
        bytefn(dav1d_filter_sbrow_resize)(f, sby);
2107
0
    if (f->lf.restore_planes)
2108
0
        bytefn(dav1d_filter_sbrow_lr)(f, sby);
2109
0
}
Unexecuted instantiation: dav1d_filter_sbrow_8bpc
Unexecuted instantiation: dav1d_filter_sbrow_16bpc
2110
2111
0
void bytefn(dav1d_backup_ipred_edge)(Dav1dTaskContext *const t) {
2112
0
    const Dav1dFrameContext *const f = t->f;
2113
0
    Dav1dTileState *const ts = t->ts;
2114
0
    const int sby = t->by >> f->sb_shift;
2115
0
    const int sby_off = f->sb128w * 128 * sby;
2116
0
    const int x_off = ts->tiling.col_start;
2117
2118
0
    const pixel *const y =
2119
0
        ((const pixel *) f->cur.data[0]) + x_off * 4 +
2120
0
                    ((t->by + f->sb_step) * 4 - 1) * PXSTRIDE(f->cur.stride[0]);
2121
0
    pixel_copy(&f->ipred_edge[0][sby_off + x_off * 4], y,
2122
0
               4 * (ts->tiling.col_end - x_off));
2123
2124
0
    if (f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I400) {
2125
0
        const int ss_ver = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
2126
0
        const int ss_hor = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
2127
2128
0
        const ptrdiff_t uv_off = (x_off * 4 >> ss_hor) +
2129
0
            (((t->by + f->sb_step) * 4 >> ss_ver) - 1) * PXSTRIDE(f->cur.stride[1]);
2130
0
        for (int pl = 1; pl <= 2; pl++)
2131
0
            pixel_copy(&f->ipred_edge[pl][sby_off + (x_off * 4 >> ss_hor)],
2132
0
                       &((const pixel *) f->cur.data[pl])[uv_off],
2133
0
                       4 * (ts->tiling.col_end - x_off) >> ss_hor);
2134
0
    }
2135
0
}
Unexecuted instantiation: dav1d_backup_ipred_edge_8bpc
Unexecuted instantiation: dav1d_backup_ipred_edge_16bpc
2136
2137
void bytefn(dav1d_copy_pal_block_y)(Dav1dTaskContext *const t,
2138
                                    const int bx4, const int by4,
2139
                                    const int bw4, const int bh4)
2140
2141
0
{
2142
0
    const Dav1dFrameContext *const f = t->f;
2143
0
    pixel *const pal = t->frame_thread.pass ?
2144
0
        f->frame_thread.pal[((t->by >> 1) + (t->bx & 1)) * (f->b4_stride >> 1) +
2145
0
                            ((t->bx >> 1) + (t->by & 1))][0] :
2146
0
        bytefn(t->scratch.pal)[0];
2147
0
    for (int x = 0; x < bw4; x++)
2148
0
        memcpy(bytefn(t->al_pal)[0][bx4 + x][0], pal, 8 * sizeof(pixel));
2149
0
    for (int y = 0; y < bh4; y++)
2150
0
        memcpy(bytefn(t->al_pal)[1][by4 + y][0], pal, 8 * sizeof(pixel));
2151
0
}
Unexecuted instantiation: dav1d_copy_pal_block_y_8bpc
Unexecuted instantiation: dav1d_copy_pal_block_y_16bpc
2152
2153
void bytefn(dav1d_copy_pal_block_uv)(Dav1dTaskContext *const t,
2154
                                     const int bx4, const int by4,
2155
                                     const int bw4, const int bh4)
2156
2157
0
{
2158
0
    const Dav1dFrameContext *const f = t->f;
2159
0
    const pixel (*const pal)[8] = t->frame_thread.pass ?
2160
0
        f->frame_thread.pal[((t->by >> 1) + (t->bx & 1)) * (f->b4_stride >> 1) +
2161
0
                            ((t->bx >> 1) + (t->by & 1))] :
2162
0
        bytefn(t->scratch.pal);
2163
    // see aomedia bug 2183 for why we use luma coordinates here
2164
0
    for (int pl = 1; pl <= 2; pl++) {
2165
0
        for (int x = 0; x < bw4; x++)
2166
0
            memcpy(bytefn(t->al_pal)[0][bx4 + x][pl], pal[pl], 8 * sizeof(pixel));
2167
0
        for (int y = 0; y < bh4; y++)
2168
0
            memcpy(bytefn(t->al_pal)[1][by4 + y][pl], pal[pl], 8 * sizeof(pixel));
2169
0
    }
2170
0
}
Unexecuted instantiation: dav1d_copy_pal_block_uv_8bpc
Unexecuted instantiation: dav1d_copy_pal_block_uv_16bpc
2171
2172
void bytefn(dav1d_read_pal_plane)(Dav1dTaskContext *const t, Av1Block *const b,
2173
                                  const int pl, const int sz_ctx,
2174
                                  const int bx4, const int by4)
2175
0
{
2176
0
    Dav1dTileState *const ts = t->ts;
2177
0
    const Dav1dFrameContext *const f = t->f;
2178
0
    const int pal_sz = b->pal_sz[pl] = dav1d_msac_decode_symbol_adapt8(&ts->msac,
2179
0
                                           ts->cdf.m.pal_sz[pl][sz_ctx], 6) + 2;
2180
0
    pixel cache[16], used_cache[8];
2181
0
    int l_cache = pl ? t->pal_sz_uv[1][by4] : t->l.pal_sz[by4];
2182
0
    int n_cache = 0;
2183
    // don't reuse above palette outside SB64 boundaries
2184
0
    int a_cache = by4 & 15 ? pl ? t->pal_sz_uv[0][bx4] : t->a->pal_sz[bx4] : 0;
2185
0
    const pixel *l = bytefn(t->al_pal)[1][by4][pl];
2186
0
    const pixel *a = bytefn(t->al_pal)[0][bx4][pl];
2187
2188
    // fill/sort cache
2189
0
    while (l_cache && a_cache) {
2190
0
        if (*l < *a) {
2191
0
            if (!n_cache || cache[n_cache - 1] != *l)
2192
0
                cache[n_cache++] = *l;
2193
0
            l++;
2194
0
            l_cache--;
2195
0
        } else {
2196
0
            if (*a == *l) {
2197
0
                l++;
2198
0
                l_cache--;
2199
0
            }
2200
0
            if (!n_cache || cache[n_cache - 1] != *a)
2201
0
                cache[n_cache++] = *a;
2202
0
            a++;
2203
0
            a_cache--;
2204
0
        }
2205
0
    }
2206
0
    if (l_cache) {
2207
0
        do {
2208
0
            if (!n_cache || cache[n_cache - 1] != *l)
2209
0
                cache[n_cache++] = *l;
2210
0
            l++;
2211
0
        } while (--l_cache > 0);
2212
0
    } else if (a_cache) {
2213
0
        do {
2214
0
            if (!n_cache || cache[n_cache - 1] != *a)
2215
0
                cache[n_cache++] = *a;
2216
0
            a++;
2217
0
        } while (--a_cache > 0);
2218
0
    }
2219
2220
    // find reused cache entries
2221
0
    int i = 0;
2222
0
    for (int n = 0; n < n_cache && i < pal_sz; n++)
2223
0
        if (dav1d_msac_decode_bool_equi(&ts->msac))
2224
0
            used_cache[i++] = cache[n];
2225
0
    const int n_used_cache = i;
2226
2227
    // parse new entries
2228
0
    pixel *const pal = t->frame_thread.pass ?
2229
0
        f->frame_thread.pal[((t->by >> 1) + (t->bx & 1)) * (f->b4_stride >> 1) +
2230
0
                            ((t->bx >> 1) + (t->by & 1))][pl] :
2231
0
        bytefn(t->scratch.pal)[pl];
2232
0
    if (i < pal_sz) {
2233
0
        const int bpc = BITDEPTH == 8 ? 8 : f->cur.p.bpc;
2234
0
        int prev = pal[i++] = dav1d_msac_decode_bools(&ts->msac, bpc);
2235
2236
0
        if (i < pal_sz) {
2237
0
            int bits = bpc - 3 + dav1d_msac_decode_bools(&ts->msac, 2);
2238
0
            const int max = (1 << bpc) - 1;
2239
2240
0
            do {
2241
0
                const int delta = dav1d_msac_decode_bools(&ts->msac, bits);
2242
0
                prev = pal[i++] = imin(prev + delta + !pl, max);
2243
0
                if (prev + !pl >= max) {
2244
0
                    for (; i < pal_sz; i++)
2245
0
                        pal[i] = max;
2246
0
                    break;
2247
0
                }
2248
0
                bits = imin(bits, 1 + ulog2(max - prev - !pl));
2249
0
            } while (i < pal_sz);
2250
0
        }
2251
2252
        // merge cache+new entries
2253
0
        int n = 0, m = n_used_cache;
2254
0
        for (i = 0; i < pal_sz; i++) {
2255
0
            if (n < n_used_cache && (m >= pal_sz || used_cache[n] <= pal[m])) {
2256
0
                pal[i] = used_cache[n++];
2257
0
            } else {
2258
0
                assert(m < pal_sz);
2259
0
                pal[i] = pal[m++];
2260
0
            }
2261
0
        }
2262
0
    } else {
2263
0
        memcpy(pal, used_cache, n_used_cache * sizeof(*used_cache));
2264
0
    }
2265
2266
0
    if (DEBUG_BLOCK_INFO) {
2267
0
        printf("Post-pal[pl=%d,sz=%d,cache_size=%d,used_cache=%d]: r=%d, cache=",
2268
0
               pl, pal_sz, n_cache, n_used_cache, ts->msac.rng);
2269
0
        for (int n = 0; n < n_cache; n++)
2270
0
            printf("%c%02x", n ? ' ' : '[', cache[n]);
2271
0
        printf("%s, pal=", n_cache ? "]" : "[]");
2272
0
        for (int n = 0; n < pal_sz; n++)
2273
0
            printf("%c%02x", n ? ' ' : '[', pal[n]);
2274
0
        printf("]\n");
2275
0
    }
2276
0
}
Unexecuted instantiation: dav1d_read_pal_plane_8bpc
Unexecuted instantiation: dav1d_read_pal_plane_16bpc
2277
2278
void bytefn(dav1d_read_pal_uv)(Dav1dTaskContext *const t, Av1Block *const b,
2279
                               const int sz_ctx, const int bx4, const int by4)
2280
0
{
2281
0
    bytefn(dav1d_read_pal_plane)(t, b, 1, sz_ctx, bx4, by4);
2282
2283
    // V pal coding
2284
0
    Dav1dTileState *const ts = t->ts;
2285
0
    const Dav1dFrameContext *const f = t->f;
2286
0
    pixel *const pal = t->frame_thread.pass ?
2287
0
        f->frame_thread.pal[((t->by >> 1) + (t->bx & 1)) * (f->b4_stride >> 1) +
2288
0
                            ((t->bx >> 1) + (t->by & 1))][2] :
2289
0
        bytefn(t->scratch.pal)[2];
2290
0
    const int bpc = BITDEPTH == 8 ? 8 : f->cur.p.bpc;
2291
0
    if (dav1d_msac_decode_bool_equi(&ts->msac)) {
2292
0
        const int bits = bpc - 4 + dav1d_msac_decode_bools(&ts->msac, 2);
2293
0
        int prev = pal[0] = dav1d_msac_decode_bools(&ts->msac, bpc);
2294
0
        const int max = (1 << bpc) - 1;
2295
0
        for (int i = 1; i < b->pal_sz[1]; i++) {
2296
0
            int delta = dav1d_msac_decode_bools(&ts->msac, bits);
2297
0
            if (delta && dav1d_msac_decode_bool_equi(&ts->msac)) delta = -delta;
2298
0
            prev = pal[i] = (prev + delta) & max;
2299
0
        }
2300
0
    } else {
2301
0
        for (int i = 0; i < b->pal_sz[1]; i++)
2302
0
            pal[i] = dav1d_msac_decode_bools(&ts->msac, bpc);
2303
0
    }
2304
0
    if (DEBUG_BLOCK_INFO) {
2305
0
        printf("Post-pal[pl=2]: r=%d ", ts->msac.rng);
2306
0
        for (int n = 0; n < b->pal_sz[1]; n++)
2307
0
            printf("%c%02x", n ? ' ' : '[', pal[n]);
2308
0
        printf("]\n");
2309
0
    }
2310
0
}
Unexecuted instantiation: dav1d_read_pal_uv_8bpc
Unexecuted instantiation: dav1d_read_pal_uv_16bpc