recon_tmpl.c 80.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*
 * Copyright © 2018, VideoLAN and dav1d authors
 * Copyright © 2018, Two Orioles, LLC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#include <string.h>
#include <stdio.h>

#include "common/attributes.h"
#include "common/bitdepth.h"
#include "common/dump.h"
#include "common/intops.h"
#include "common/mem.h"

#include "src/cdef_apply.h"
Ronald S. Bultje's avatar
Ronald S. Bultje committed
40
#include "src/ctx.h"
41 42 43 44 45 46 47 48 49 50 51 52
#include "src/ipred_prepare.h"
#include "src/lf_apply.h"
#include "src/lr_apply.h"
#include "src/recon.h"
#include "src/scan.h"
#include "src/tables.h"
#include "src/wedge.h"

static unsigned read_golomb(MsacContext *const msac) {
    int len = 0;
    unsigned val = 1;

53 54
    while (!msac_decode_bool_equi(msac) && len < 32) len++;
    while (len--) val = (val << 1) | msac_decode_bool_equi(msac);
55 56 57 58 59 60 61 62 63 64 65 66 67 68

    return val - 1;
}

static int decode_coefs(Dav1dTileContext *const t,
                        uint8_t *const a, uint8_t *const l,
                        const enum RectTxfmSize tx, const enum BlockSize bs,
                        const Av1Block *const b, const int intra,
                        const int plane, coef *cf,
                        enum TxfmType *const txtp, uint8_t *res_ctx)
{
    Dav1dTileState *const ts = t->ts;
    const int chroma = !!plane;
    const Dav1dFrameContext *const f = t->f;
69
    const TxfmInfo *const t_dim = &dav1d_txfm_dimensions[tx];
70 71 72 73 74
    const int dbg = DEBUG_BLOCK_INFO && plane && 0;

    if (dbg) printf("Start: r=%d\n", ts->msac.rng);

    // does this block have any non-zero coefficients
Ronald S. Bultje's avatar
Ronald S. Bultje committed
75
    const int sctx = get_coef_skip_ctx(t_dim, bs, a, l, chroma, f->cur.p.layout);
76 77 78 79 80 81 82
    const int all_skip =
        msac_decode_bool_adapt(&ts->msac, ts->cdf.coef.skip[t_dim->ctx][sctx]);
    if (dbg)
    printf("Post-non-zero[%d][%d][%d]: r=%d\n",
           t_dim->ctx, sctx, all_skip, ts->msac.rng);
    if (all_skip) {
        *res_ctx = 0x40;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
83
        *txtp = f->frame_hdr->segmentation.lossless[b->seg_id] ? WHT_WHT :
84 85 86 87 88 89 90
                                                                DCT_DCT;
        return -1;
    }

    // transform type (chroma: derived, luma: explicitly coded)
    if (chroma) {
        if (intra) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
91
            *txtp = get_uv_intra_txtp(b->uv_mode, tx, f->frame_hdr, b->seg_id);
92 93
        } else {
            const enum TxfmType y_txtp = *txtp;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
94
            *txtp = get_uv_inter_txtp(t_dim, y_txtp, f->frame_hdr, b->seg_id);
95 96 97
        }
    } else {
        const enum TxfmTypeSet set = get_ext_txtp_set(tx, !intra,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
98
                                                      f->frame_hdr, b->seg_id);
99
        const unsigned set_cnt = dav1d_tx_type_count[set];
100 101 102 103
        unsigned idx;
        if (set_cnt == 1) {
            idx = 0;
        } else {
104
            const int set_idx = dav1d_tx_type_set_index[!intra][set];
105 106
            const enum IntraPredMode y_mode_nofilt = intra ? b->y_mode == FILTER_PRED ?
                dav1d_filter_mode_to_y_mode[b->y_angle] : b->y_mode : 0;
107 108 109 110 111 112
            uint16_t *const txtp_cdf = intra ?
                       ts->cdf.m.txtp_intra[set_idx][t_dim->min][y_mode_nofilt] :
                       ts->cdf.m.txtp_inter[set_idx][t_dim->min];
            idx = msac_decode_symbol_adapt(&ts->msac, txtp_cdf, set_cnt);
            if (dbg)
            printf("Post-txtp[%d->%d][%d->%d][%d][%d->%d]: r=%d\n",
113
                   set, set_idx, tx, t_dim->min, intra ? (int)y_mode_nofilt : -1,
114
                   idx, dav1d_tx_types_per_set[set][idx], ts->msac.rng);
115
        }
116
        *txtp = dav1d_tx_types_per_set[set][idx];
117 118 119 120 121
    }

    // find end-of-block (eob)
    int eob_bin;
    const int tx2dszctx = imin(t_dim->lw, TX_32X32) + imin(t_dim->lh, TX_32X32);
122
    const enum TxClass tx_class = dav1d_tx_type_class[*txtp];
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    const int is_1d = tx_class != TX_CLASS_2D;
    switch (tx2dszctx) {
#define case_sz(sz, bin) \
    case sz: { \
        uint16_t *const eob_bin_cdf = ts->cdf.coef.eob_bin_##bin[chroma][is_1d]; \
        eob_bin = msac_decode_symbol_adapt(&ts->msac, eob_bin_cdf, 5 + sz); \
        break; \
    }
    case_sz(0,   16);
    case_sz(1,   32);
    case_sz(2,   64);
    case_sz(3,  128);
    case_sz(4,  256);
    case_sz(5,  512);
    case_sz(6, 1024);
#undef case_sz
    }
    if (dbg)
    printf("Post-eob_bin_%d[%d][%d][%d]: r=%d\n",
           16 << tx2dszctx, chroma, is_1d, eob_bin, ts->msac.rng);
    int eob;
    if (eob_bin > 1) {
        eob = 1 << (eob_bin - 1);
        uint16_t *const eob_hi_bit_cdf =
            ts->cdf.coef.eob_hi_bit[t_dim->ctx][chroma][eob_bin];
        const int eob_hi_bit = msac_decode_bool_adapt(&ts->msac, eob_hi_bit_cdf);
        if (dbg)
        printf("Post-eob_hi_bit[%d][%d][%d][%d]: r=%d\n",
               t_dim->ctx, chroma, eob_bin, eob_hi_bit, ts->msac.rng);
        unsigned mask = eob >> 1;
        if (eob_hi_bit) eob |= mask;
        for (mask >>= 1; mask; mask >>= 1) {
155
            const int eob_bit = msac_decode_bool_equi(&ts->msac);
156 157 158 159 160 161 162 163 164 165 166
            if (eob_bit) eob |= mask;
        }
        if (dbg)
        printf("Post-eob[%d]: r=%d\n", eob, ts->msac.rng);
    } else {
        eob = eob_bin;
    }

    // base tokens
    uint16_t (*const br_cdf)[5] =
        ts->cdf.coef.br_tok[imin(t_dim->ctx, 3)][chroma];
167
    const int16_t *const scan = dav1d_scans[tx][tx_class];
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    uint8_t levels[36 * 36];
    ptrdiff_t stride = 4 * (imin(t_dim->h, 8) + 1);
    memset(levels, 0, stride * 4 * (imin(t_dim->w, 8) + 1));
    const int shift = 2 + imin(t_dim->lh, 3), mask = 4 * imin(t_dim->h, 8) - 1;
    unsigned cul_level = 0;
    for (int i = eob, is_last = 1; i >= 0; i--, is_last = 0) {
        const int rc = scan[i], x = rc >> shift, y = rc & mask;

        // lo tok
        const int ctx = get_coef_nz_ctx(levels, i, rc, is_last, tx, tx_class);
        uint16_t *const lo_cdf = is_last ?
            ts->cdf.coef.eob_base_tok[t_dim->ctx][chroma][ctx] :
            ts->cdf.coef.base_tok[t_dim->ctx][chroma][ctx];
        int tok = msac_decode_symbol_adapt(&ts->msac, lo_cdf,
                                           4 - is_last) + is_last;
        if (dbg)
        printf("Post-lo_tok[%d][%d][%d][%d=%d=%d]: r=%d\n",
               t_dim->ctx, chroma, ctx, i, rc, tok, ts->msac.rng);
        if (!tok) continue;

        // hi tok
        if (tok == 3) {
            const int br_ctx = get_br_ctx(levels, rc, tx, tx_class);
            do {
                const int tok_br =
                    msac_decode_symbol_adapt(&ts->msac, br_cdf[br_ctx], 4);
                if (dbg)
                printf("Post-hi_tok[%d][%d][%d][%d=%d=%d->%d]: r=%d\n",
                       imin(t_dim->ctx, 3), chroma, br_ctx,
                       i, rc, tok_br, tok, ts->msac.rng);
                tok += tok_br;
                if (tok_br < 3) break;
            } while (tok < 15);
        }

        levels[x * stride + y] = cf[rc] = tok;
    }

    // residual and sign
    int dc_sign = 1;
208
    const int lossless = f->frame_hdr->segmentation.lossless[b->seg_id];
209
    const uint16_t *const dq_tbl = ts->dq[b->seg_id][plane];
210
    const uint8_t *const qm_tbl = f->qm[lossless || is_1d || *txtp == IDTX][tx][plane];
211
    const int dq_shift = imax(0, t_dim->ctx - 2);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
212 213 214
    const int bitdepth = BITDEPTH == 8 ? 8 : f->cur.p.bpc;
    const int cf_min = -(1 << (7 + bitdepth));
    const int cf_max = (1 << (7 + bitdepth)) - 1;
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    for (int i = 0; i <= eob; i++) {
        const int rc = scan[i];
        int tok = cf[rc];
        if (!tok) continue;
        int dq;

        // sign
        int sign;
        if (i == 0) {
            const int dc_sign_ctx = get_dc_sign_ctx(t_dim, a, l);
            uint16_t *const dc_sign_cdf =
                ts->cdf.coef.dc_sign[chroma][dc_sign_ctx];
            sign = msac_decode_bool_adapt(&ts->msac, dc_sign_cdf);
            if (dbg)
            printf("Post-dc_sign[%d][%d][%d]: r=%d\n",
                   chroma, dc_sign_ctx, sign, ts->msac.rng);
            dc_sign = sign ? 0 : 2;
            dq = (dq_tbl[0] * qm_tbl[0] + 16) >> 5;
        } else {
234
            sign = msac_decode_bool_equi(&ts->msac);
235 236 237 238 239 240 241 242 243 244 245 246 247
            if (dbg)
            printf("Post-sign[%d=%d=%d]: r=%d\n", i, rc, sign, ts->msac.rng);
            dq = (dq_tbl[1] * qm_tbl[rc] + 16) >> 5;
        }

        // residual
        if (tok == 15) {
            tok += read_golomb(&ts->msac);
            if (dbg)
            printf("Post-residual[%d=%d=%d->%d]: r=%d\n",
                   i, rc, tok - 15, tok, ts->msac.rng);
        }

248 249 250 251
        // coefficient parsing, see 5.11.39
        tok &= 0xfffff;

        // dequant, see 7.12.3
252
        cul_level += tok;
253
        tok = (((int64_t)dq * tok) & 0xffffff) >> dq_shift;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
254
        cf[rc] = iclip(sign ? -tok : tok, cf_min, cf_max);
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    }

    // context
    *res_ctx = imin(cul_level, 63) | (dc_sign << 6);

    return eob;
}

static void read_coef_tree(Dav1dTileContext *const t,
                           const enum BlockSize bs, const Av1Block *const b,
                           const enum RectTxfmSize ytx, const int depth,
                           const uint16_t *const tx_split,
                           const int x_off, const int y_off, pixel *dst)
{
    const Dav1dFrameContext *const f = t->f;
    Dav1dTileState *const ts = t->ts;
    const Dav1dDSPContext *const dsp = f->dsp;
272
    const TxfmInfo *const t_dim = &dav1d_txfm_dimensions[ytx];
273 274
    const int txw = t_dim->w, txh = t_dim->h;

275 276 277 278 279
    /* y_off can be larger than 3 since lossless blocks use TX_4X4 but can't
     * be splitted. Aviods an undefined left shift. */
    if (depth < 2 && tx_split[depth] &&
        tx_split[depth] & (1 << (y_off * 4 + x_off)))
    {
280
        const enum RectTxfmSize sub = t_dim->sub;
281
        const TxfmInfo *const sub_t_dim = &dav1d_txfm_dimensions[sub];
282 283 284 285 286 287 288 289 290 291 292 293
        const int txsw = sub_t_dim->w, txsh = sub_t_dim->h;

        read_coef_tree(t, bs, b, sub, depth + 1, tx_split,
                       x_off * 2 + 0, y_off * 2 + 0, dst);
        t->bx += txsw;
        if (txw >= txh && t->bx < f->bw)
            read_coef_tree(t, bs, b, sub, depth + 1, tx_split, x_off * 2 + 1,
                           y_off * 2 + 0, dst ? &dst[4 * txsw] : NULL);
        t->bx -= txsw;
        t->by += txsh;
        if (txh >= txw && t->by < f->bh) {
            if (dst)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
294
                dst += 4 * txsh * PXSTRIDE(f->cur.stride[0]);
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
            read_coef_tree(t, bs, b, sub, depth + 1, tx_split,
                           x_off * 2 + 0, y_off * 2 + 1, dst);
            t->bx += txsw;
            if (txw >= txh && t->bx < f->bw)
                read_coef_tree(t, bs, b, sub, depth + 1, tx_split, x_off * 2 + 1,
                               y_off * 2 + 1, dst ? &dst[4 * txsw] : NULL);
            t->bx -= txsw;
        }
        t->by -= txsh;
    } else {
        const int bx4 = t->bx & 31, by4 = t->by & 31;
        enum TxfmType txtp;
        uint8_t cf_ctx;
        int eob;
        coef *cf;
        struct CodedBlockInfo *cbi;

        if (f->frame_thread.pass) {
            cf = ts->frame_thread.cf;
            ts->frame_thread.cf += imin(t_dim->w, 8) * imin(t_dim->h, 8) * 16;
            cbi = &f->frame_thread.cbi[t->by * f->b4_stride + t->bx];
        } else {
            cf = t->cf;
        }
        if (f->frame_thread.pass != 2) {
            eob = decode_coefs(t, &t->a->lcoef[bx4], &t->l.lcoef[by4],
                               ytx, bs, b, 0, 0, cf, &txtp, &cf_ctx);
            if (DEBUG_BLOCK_INFO)
                printf("Post-y-cf-blk[tx=%d,txtp=%d,eob=%d]: r=%d\n",
                       ytx, txtp, eob, ts->msac.rng);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
#define set_ctx(type, dir, diridx, off, mul, rep_macro) \
            rep_macro(type, t->dir lcoef, off, mul * cf_ctx)
#define default_memset(dir, diridx, off, sz) \
            memset(&t->dir lcoef[off], cf_ctx, sz)
            case_set_upto16_with_default(imin(txh, f->bh - t->by), l., 1, by4);
            case_set_upto16_with_default(imin(txw, f->bw - t->bx), a->, 0, bx4);
#undef default_memset
#undef set_ctx
#define set_ctx(type, dir, diridx, off, mul, rep_macro) \
            for (int y = 0; y < txh; y++) { \
                rep_macro(type, txtp_map, 0, mul * txtp); \
                txtp_map += 32; \
            }
            uint8_t *txtp_map = &t->txtp_map[by4 * 32 + bx4];
            case_set_upto16(txw,,,);
#undef set_ctx
341 342 343 344 345 346 347 348 349 350 351 352 353
            if (f->frame_thread.pass == 1) {
                cbi->eob[0] = eob;
                cbi->txtp[0] = txtp;
            }
        } else {
            eob = cbi->eob[0];
            txtp = cbi->txtp[0];
        }
        if (!(f->frame_thread.pass & 1)) {
            assert(dst);
            if (eob >= 0) {
                if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS)
                    coef_dump(cf, imin(t_dim->h, 8) * 4, imin(t_dim->w, 8) * 4, 3, "dq");
Ronald S. Bultje's avatar
Ronald S. Bultje committed
354 355
                dsp->itx.itxfm_add[ytx][txtp](dst, f->cur.stride[0], cf, eob
                                              HIGHBD_CALL_SUFFIX);
356
                if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
357
                    hex_dump(dst, f->cur.stride[0], t_dim->w * 4, t_dim->h * 4, "recon");
358 359 360 361 362
            }
        }
    }
}

363 364
void bytefn(dav1d_read_coef_blocks)(Dav1dTileContext *const t,
                                    const enum BlockSize bs, const Av1Block *const b)
365 366
{
    const Dav1dFrameContext *const f = t->f;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
367 368
    const int ss_ver = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
    const int ss_hor = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
369 370
    const int bx4 = t->bx & 31, by4 = t->by & 31;
    const int cbx4 = bx4 >> ss_hor, cby4 = by4 >> ss_ver;
371
    const uint8_t *const b_dim = dav1d_block_dimensions[bs];
372
    const int bw4 = b_dim[0], bh4 = b_dim[1];
373
    const int cbw4 = (bw4 + ss_hor) >> ss_hor, cbh4 = (bh4 + ss_ver) >> ss_ver;
374
    const int has_chroma = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I400 &&
375 376 377 378
                           (bw4 > ss_hor || t->bx & 1) &&
                           (bh4 > ss_ver || t->by & 1);

    if (b->skip) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
379 380 381 382 383 384 385 386 387 388 389 390
#define set_ctx(type, dir, diridx, off, mul, rep_macro) \
        rep_macro(type, t->dir lcoef, off, mul * 0x40)
        case_set(bh4, l., 1, by4);
        case_set(bw4, a->, 0, bx4);
#undef set_ctx
        if (has_chroma) {
#define set_ctx(type, dir, diridx, off, mul, rep_macro) \
            rep_macro(type, t->dir ccoef[0], off, mul * 0x40); \
            rep_macro(type, t->dir ccoef[1], off, mul * 0x40)
            case_set(cbh4, l., 1, cby4);
            case_set(cbw4, a->, 0, cbx4);
#undef set_ctx
391 392 393 394 395 396 397 398 399
        }
        return;
    }

    Dav1dTileState *const ts = t->ts;
    const int w4 = imin(bw4, f->bw - t->bx), h4 = imin(bh4, f->bh - t->by);
    const int cw4 = (w4 + ss_hor) >> ss_hor, ch4 = (h4 + ss_ver) >> ss_ver;
    assert(f->frame_thread.pass == 1);
    assert(!b->skip);
400 401
    const TxfmInfo *const uv_t_dim = &dav1d_txfm_dimensions[b->uvtx];
    const TxfmInfo *const t_dim = &dav1d_txfm_dimensions[b->intra ? b->tx : b->max_ytx];
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

    for (int init_y = 0; init_y < h4; init_y += 16) {
        for (int init_x = 0; init_x < w4; init_x += 16) {
            const int sub_h4 = imin(h4, 16 + init_y);
            const int sub_w4 = imin(w4, init_x + 16);
            int y_off = !!init_y, y, x;
            for (y = init_y, t->by += init_y; y < sub_h4;
                 y += t_dim->h, t->by += t_dim->h, y_off++)
            {
                struct CodedBlockInfo *const cbi =
                    &f->frame_thread.cbi[t->by * f->b4_stride];
                int x_off = !!init_x;
                for (x = init_x, t->bx += init_x; x < sub_w4;
                     x += t_dim->w, t->bx += t_dim->w, x_off++)
                {
                    if (!b->intra) {
                        read_coef_tree(t, bs, b, b->max_ytx, 0, b->tx_split,
                                       x_off, y_off, NULL);
                    } else {
                        uint8_t cf_ctx = 0x40;
                        enum TxfmType txtp;
                        const int eob = cbi[t->bx].eob[0] =
                            decode_coefs(t, &t->a->lcoef[bx4 + x],
                                         &t->l.lcoef[by4 + y], b->tx, bs, b, 1,
                                         0, ts->frame_thread.cf, &txtp, &cf_ctx);
                        if (DEBUG_BLOCK_INFO)
                            printf("Post-y-cf-blk[tx=%d,txtp=%d,eob=%d]: r=%d\n",
                                   b->tx, txtp, eob, ts->msac.rng);
                        cbi[t->bx].txtp[0] = txtp;
                        ts->frame_thread.cf += imin(t_dim->w, 8) * imin(t_dim->h, 8) * 16;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
432 433 434 435 436 437 438 439 440 441
#define set_ctx(type, dir, diridx, off, mul, rep_macro) \
                        rep_macro(type, t->dir lcoef, off, mul * cf_ctx)
#define default_memset(dir, diridx, off, sz) \
                        memset(&t->dir lcoef[off], cf_ctx, sz)
                        case_set_upto16_with_default(imin(t_dim->h, f->bh - t->by),
                                                     l., 1, by4 + y);
                        case_set_upto16_with_default(imin(t_dim->w, f->bw - t->bx),
                                                     a->, 0, bx4 + x);
#undef default_memset
#undef set_ctx
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
                    }
                }
                t->bx -= x;
            }
            t->by -= y;

            if (!has_chroma) continue;

            const int sub_ch4 = imin(ch4, (init_y + 16) >> ss_ver);
            const int sub_cw4 = imin(cw4, (init_x + 16) >> ss_hor);
            for (int pl = 0; pl < 2; pl++) {
                for (y = init_y >> ss_ver, t->by += init_y; y < sub_ch4;
                     y += uv_t_dim->h, t->by += uv_t_dim->h << ss_ver)
                {
                    struct CodedBlockInfo *const cbi =
                        &f->frame_thread.cbi[t->by * f->b4_stride];
                    for (x = init_x >> ss_hor, t->bx += init_x; x < sub_cw4;
                         x += uv_t_dim->w, t->bx += uv_t_dim->w << ss_hor)
                    {
                        uint8_t cf_ctx = 0x40;
                        enum TxfmType txtp;
                        if (!b->intra)
                            txtp = t->txtp_map[(by4 + (y << ss_ver)) * 32 +
                                                bx4 + (x << ss_hor)];
                        const int eob = cbi[t->bx].eob[1 + pl] =
                            decode_coefs(t, &t->a->ccoef[pl][cbx4 + x],
                                         &t->l.ccoef[pl][cby4 + y], b->uvtx, bs,
                                         b, b->intra, 1 + pl, ts->frame_thread.cf,
                                         &txtp, &cf_ctx);
                        if (DEBUG_BLOCK_INFO)
                            printf("Post-uv-cf-blk[pl=%d,tx=%d,"
                                   "txtp=%d,eob=%d]: r=%d\n",
                                   pl, b->uvtx, txtp, eob, ts->msac.rng);
                        cbi[t->bx].txtp[1 + pl] = txtp;
                        ts->frame_thread.cf += uv_t_dim->w * uv_t_dim->h * 16;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
477 478 479 480 481 482 483 484 485 486 487 488
#define set_ctx(type, dir, diridx, off, mul, rep_macro) \
                        rep_macro(type, t->dir ccoef[pl], off, mul * cf_ctx)
#define default_memset(dir, diridx, off, sz) \
                        memset(&t->dir ccoef[pl][off], cf_ctx, sz)
                        case_set_upto16_with_default( \
                                 imin(uv_t_dim->h, (f->bh - t->by + ss_ver) >> ss_ver),
                                 l., 1, cby4 + y);
                        case_set_upto16_with_default( \
                                 imin(uv_t_dim->w, (f->bw - t->bx + ss_hor) >> ss_hor),
                                 a->, 0, cbx4 + x);
#undef default_memset
#undef set_ctx
489 490 491 492 493 494 495 496 497
                    }
                    t->bx -= x << ss_hor;
                }
                t->by -= y << ss_ver;
            }
        }
    }
}

498
static int mc(Dav1dTileContext *const t,
499
              pixel *const dst8, int16_t *const dst16, const ptrdiff_t dst_stride,
500 501
              const int bw4, const int bh4,
              const int bx, const int by, const int pl,
502
              const mv mv, const Dav1dThreadPicture *const refp, const int refidx,
503
              const enum Filter2d filter_2d)
504 505 506
{
    assert((dst8 != NULL) ^ (dst16 != NULL));
    const Dav1dFrameContext *const f = t->f;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
507 508
    const int ss_ver = !!pl && f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
    const int ss_hor = !!pl && f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
509 510 511 512 513 514
    const int h_mul = 4 >> ss_hor, v_mul = 4 >> ss_ver;
    const int mvx = mv.x, mvy = mv.y;
    const int mx = mvx & (15 >> !ss_hor), my = mvy & (15 >> !ss_ver);
    ptrdiff_t ref_stride = refp->p.stride[!!pl];
    const pixel *ref;

Ronald S. Bultje's avatar
Ronald S. Bultje committed
515
    if (refp->p.p.w == f->cur.p.w && refp->p.p.h == f->cur.p.h) {
516 517 518 519
        const int dx = bx * h_mul + (mvx >> (3 + ss_hor));
        const int dy = by * v_mul + (mvy >> (3 + ss_ver));
        int w, h;

Ronald S. Bultje's avatar
Ronald S. Bultje committed
520
        if (refp->p.data[0] != f->cur.data[0]) { // i.e. not for intrabc
521 522 523 524 525
            if (dav1d_thread_picture_wait(refp, dy + bh4 * v_mul + !!my * 4,
                                          PLANE_TYPE_Y + !!pl))
            {
                return -1;
            }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
526 527
            w = (f->cur.p.w + ss_hor) >> ss_hor;
            h = (f->cur.p.h + ss_ver) >> ss_ver;
528 529 530 531 532 533 534
        } else {
            w = f->bw * 4 >> ss_hor;
            h = f->bh * 4 >> ss_ver;
        }
        if (dx < !!mx * 3 || dy < !!my * 3 ||
            dx + bw4 * h_mul + !!mx * 4 > w ||
            dy + bh4 * v_mul + !!my * 4 > h)
535
        {
536 537 538 539 540 541 542 543
            f->dsp->mc.emu_edge(bw4 * h_mul + !!mx * 7, bh4 * v_mul + !!my * 7,
                                w, h, dx - !!mx * 3, dy - !!my * 3,
                                t->emu_edge, 192 * sizeof(pixel),
                                refp->p.data[pl], ref_stride);
            ref = &t->emu_edge[192 * !!my * 3 + !!mx * 3];
            ref_stride = 192 * sizeof(pixel);
        } else {
            ref = ((pixel *) refp->p.data[pl]) + PXSTRIDE(ref_stride) * dy + dx;
544
        }
545

546 547
        if (dst8 != NULL) {
            f->dsp->mc.mc[filter_2d](dst8, dst_stride, ref, ref_stride, bw4 * h_mul,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
548 549
                                     bh4 * v_mul, mx << !ss_hor, my << !ss_ver
                                     HIGHBD_CALL_SUFFIX);
550 551
        } else {
            f->dsp->mc.mct[filter_2d](dst16, ref, ref_stride, bw4 * h_mul,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
552 553
                                      bh4 * v_mul, mx << !ss_hor, my << !ss_ver
                                      HIGHBD_CALL_SUFFIX);
554
        }
555
    } else {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
556
        assert(refp != &f->sr_cur);
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

        int orig_pos_y = (by * v_mul << 4) + mvy * (1 << !ss_ver);
        int orig_pos_x = (bx * h_mul << 4) + mvx * (1 << !ss_hor);
#define scale_mv(res, val, scale) do { \
            const int64_t tmp = (int64_t)(val) * scale + (scale - 0x4000) * 8; \
            res = (int)apply_sign64((llabs(tmp) + 128) >> 8, tmp) + 32; \
        } while (0)
        int pos_y, pos_x;
        scale_mv(pos_x, orig_pos_x, f->svc[refidx][0].scale);
        scale_mv(pos_y, orig_pos_y, f->svc[refidx][1].scale);
#undef scale_mv
        const int left = pos_x >> 10;
        const int top = pos_y >> 10;
        const int right =
            ((pos_x + (bw4 * h_mul - 1) * f->svc[refidx][0].step) >> 10) + 1;
        const int bottom =
            ((pos_y + (bh4 * v_mul - 1) * f->svc[refidx][1].step) >> 10) + 1;

575
        if (dav1d_thread_picture_wait(refp, bottom + 4, PLANE_TYPE_Y + !!pl))
576
            return -1;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
577 578 579 580 581
        if (DEBUG_BLOCK_INFO)
            printf("Off %dx%d [%d,%d,%d], size %dx%d [%d,%d]\n",
                   left, top, orig_pos_x, f->svc[refidx][0].scale, refidx,
                   right-left, bottom-top,
                   f->svc[refidx][0].step, f->svc[refidx][1].step);
582 583 584 585 586 587 588 589 590 591

        const int w = (refp->p.p.w + ss_hor) >> ss_hor;
        const int h = (refp->p.p.h + ss_ver) >> ss_ver;
        if (left < 3 || top < 3 || right + 4 > w || bottom + 4 > h) {
            f->dsp->mc.emu_edge(right - left + 7, bottom - top + 7,
                                w, h, left - 3, top - 3,
                                t->emu_edge, 320 * sizeof(pixel),
                                refp->p.data[pl], ref_stride);
            ref = &t->emu_edge[320 * 3 + 3];
            ref_stride = 320 * sizeof(pixel);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
592
            if (DEBUG_BLOCK_INFO) printf("Emu\n");
593 594 595 596 597 598 599 600 601
        } else {
            ref = ((pixel *) refp->p.data[pl]) + PXSTRIDE(ref_stride) * top + left;
        }

        if (dst8 != NULL) {
            f->dsp->mc.mc_scaled[filter_2d](dst8, dst_stride, ref, ref_stride,
                                            bw4 * h_mul, bh4 * v_mul,
                                            pos_x & 0x3ff, pos_y & 0x3ff,
                                            f->svc[refidx][0].step,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
602 603
                                            f->svc[refidx][1].step
                                            HIGHBD_CALL_SUFFIX);
604 605 606 607 608
        } else {
            f->dsp->mc.mct_scaled[filter_2d](dst16, ref, ref_stride,
                                             bw4 * h_mul, bh4 * v_mul,
                                             pos_x & 0x3ff, pos_y & 0x3ff,
                                             f->svc[refidx][0].step,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
609 610
                                             f->svc[refidx][1].step
                                             HIGHBD_CALL_SUFFIX);
611
        }
612
    }
613

614
    return 0;
615 616
}

617 618 619 620
static int obmc(Dav1dTileContext *const t,
                pixel *const dst, const ptrdiff_t dst_stride,
                const uint8_t *const b_dim, const int pl,
                const int bx4, const int by4, const int w4, const int h4)
621 622 623 624 625
{
    assert(!(t->bx & 1) && !(t->by & 1));
    const Dav1dFrameContext *const f = t->f;
    const refmvs *const r = &f->mvs[t->by * f->b4_stride + t->bx];
    pixel *const lap = t->scratch.lap;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
626 627
    const int ss_ver = !!pl && f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
    const int ss_hor = !!pl && f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
628
    const int h_mul = 4 >> ss_hor, v_mul = 4 >> ss_ver;
629
    int res;
630 631 632 633 634 635 636 637

    if (t->by > t->ts->tiling.row_start &&
        (!pl || b_dim[0] * h_mul + b_dim[1] * v_mul >= 16))
    {
        for (int i = 0, x = 0; x < w4 && i < imin(b_dim[2], 4); ) {
            // only odd blocks are considered for overlap handling, hence +1
            const refmvs *const a_r = &r[x - f->b4_stride + 1];
            const uint8_t *const a_b_dim =
638
                dav1d_block_dimensions[sbtype_to_bs[a_r->sb_type]];
639 640

            if (a_r->ref[0] > 0) {
641 642
                const int ow4 = iclip(a_b_dim[0], 2, b_dim[0]);
                const int oh4 = imin(b_dim[1], 16) >> 1;
643 644
                res = mc(t, lap, NULL, ow4 * h_mul * sizeof(pixel), ow4, oh4,
                         t->bx + x, t->by, pl, a_r->mv[0],
645
                         &f->refp[a_r->ref[0] - 1], a_r->ref[0] - 1,
646 647
                         dav1d_filter_2d[t->a->filter[1][bx4 + x + 1]][t->a->filter[0][bx4 + x + 1]]);
                if (res) return res;
Henrik Gramner's avatar
Henrik Gramner committed
648 649
                f->dsp->mc.blend_h(&dst[x * h_mul], dst_stride, lap,
                                   h_mul * ow4, v_mul * oh4);
650 651 652 653 654 655 656 657 658 659 660
                i++;
            }
            x += imax(a_b_dim[0], 2);
        }
    }

    if (t->bx > t->ts->tiling.col_start)
        for (int i = 0, y = 0; y < h4 && i < imin(b_dim[3], 4); ) {
            // only odd blocks are considered for overlap handling, hence +1
            const refmvs *const l_r = &r[(y + 1) * f->b4_stride - 1];
            const uint8_t *const l_b_dim =
661
                dav1d_block_dimensions[sbtype_to_bs[l_r->sb_type]];
662 663

            if (l_r->ref[0] > 0) {
664 665
                const int ow4 = imin(b_dim[0], 16) >> 1;
                const int oh4 = iclip(l_b_dim[1], 2, b_dim[1]);
666 667
                res = mc(t, lap, NULL, h_mul * ow4 * sizeof(pixel), ow4, oh4,
                         t->bx, t->by + y, pl, l_r->mv[0],
668
                         &f->refp[l_r->ref[0] - 1], l_r->ref[0] - 1,
669 670
                         dav1d_filter_2d[t->l.filter[1][by4 + y + 1]][t->l.filter[0][by4 + y + 1]]);
                if (res) return res;
Henrik Gramner's avatar
Henrik Gramner committed
671 672
                f->dsp->mc.blend_v(&dst[y * v_mul * PXSTRIDE(dst_stride)],
                                   dst_stride, lap, h_mul * ow4, v_mul * oh4);
673 674 675 676
                i++;
            }
            y += imax(l_b_dim[1], 2);
        }
677
    return 0;
678 679
}

680
static int warp_affine(Dav1dTileContext *const t,
681
                       pixel *dst8, int16_t *dst16, const ptrdiff_t dstride,
682 683
                       const uint8_t *const b_dim, const int pl,
                       const Dav1dThreadPicture *const refp,
684
                       const Dav1dWarpedMotionParams *const wmp)
685 686 687 688
{
    assert((dst8 != NULL) ^ (dst16 != NULL));
    const Dav1dFrameContext *const f = t->f;
    const Dav1dDSPContext *const dsp = f->dsp;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
689 690
    const int ss_ver = !!pl && f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
    const int ss_hor = !!pl && f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
691 692 693
    const int h_mul = 4 >> ss_hor, v_mul = 4 >> ss_ver;
    assert(!((b_dim[0] * h_mul) & 7) && !((b_dim[1] * v_mul) & 7));
    const int32_t *const mat = wmp->matrix;
694 695
    const int width = (refp->p.p.w + ss_hor) >> ss_hor;
    const int height = (refp->p.p.h + ss_ver) >> ss_ver;
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715

    for (int y = 0; y < b_dim[1] * v_mul; y += 8) {
        for (int x = 0; x < b_dim[0] * h_mul; x += 8) {
            // calculate transformation relative to center of 8x8 block in
            // luma pixel units
            const int src_x = t->bx * 4 + ((x + 4) << ss_hor);
            const int src_y = t->by * 4 + ((y + 4) << ss_ver);
            const int mvx = (mat[2] * src_x + mat[3] * src_y + mat[0]) >> ss_hor;
            const int mvy = (mat[4] * src_x + mat[5] * src_y + mat[1]) >> ss_ver;

            const int dx = (mvx >> 16) - 4;
            const int mx = ((mvx & 0xffff) - wmp->alpha * 4 -
                                             wmp->beta  * 7) & ~0x3f;
            const int dy = (mvy >> 16) - 4;
            const int my = ((mvy & 0xffff) - wmp->gamma * 4 -
                                             wmp->delta * 4) & ~0x3f;

            const pixel *ref_ptr;
            ptrdiff_t ref_stride = refp->p.stride[!!pl];

716 717 718 719 720
            if (dav1d_thread_picture_wait(refp, dy + 4 + 8,
                                          PLANE_TYPE_Y + !!pl))
            {
                return -1;
            }
721
            if (dx < 3 || dx + 8 + 4 > width || dy < 3 || dy + 8 + 4 > height) {
722
                f->dsp->mc.emu_edge(15, 15, width, height, dx - 3, dy - 3,
723
                                    t->emu_edge, 192 * sizeof(pixel),
724
                                    refp->p.data[pl], ref_stride);
725 726
                ref_ptr = &t->emu_edge[192 * 3 + 3];
                ref_stride = 192 * sizeof(pixel);
727 728 729 730 731
            } else {
                ref_ptr = ((pixel *) refp->p.data[pl]) + PXSTRIDE(ref_stride) * dy + dx;
            }
            if (dst16 != NULL)
                dsp->mc.warp8x8t(&dst16[x], dstride, ref_ptr, ref_stride,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
732
                                 wmp->abcd, mx, my HIGHBD_CALL_SUFFIX);
733 734
            else
                dsp->mc.warp8x8(&dst8[x], dstride, ref_ptr, ref_stride,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
735
                                wmp->abcd, mx, my HIGHBD_CALL_SUFFIX);
736 737 738 739
        }
        if (dst8) dst8  += 8 * PXSTRIDE(dstride);
        else      dst16 += 8 * dstride;
    }
740
    return 0;
741 742
}

743 744 745
void bytefn(dav1d_recon_b_intra)(Dav1dTileContext *const t, const enum BlockSize bs,
                                 const enum EdgeFlags intra_edge_flags,
                                 const Av1Block *const b)
746 747 748 749 750
{
    Dav1dTileState *const ts = t->ts;
    const Dav1dFrameContext *const f = t->f;
    const Dav1dDSPContext *const dsp = f->dsp;
    const int bx4 = t->bx & 31, by4 = t->by & 31;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
751 752
    const int ss_ver = f->cur.p.layout == DAV1D_PIXEL_LAYOUT_I420;
    const int ss_hor = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I444;
753
    const int cbx4 = bx4 >> ss_hor, cby4 = by4 >> ss_ver;
754
    const uint8_t *const b_dim = dav1d_block_dimensions[bs];
755 756 757
    const int bw4 = b_dim[0], bh4 = b_dim[1];
    const int w4 = imin(bw4, f->bw - t->bx), h4 = imin(bh4, f->bh - t->by);
    const int cw4 = (w4 + ss_hor) >> ss_hor, ch4 = (h4 + ss_ver) >> ss_ver;
758
    const int has_chroma = f->cur.p.layout != DAV1D_PIXEL_LAYOUT_I400 &&
759 760
                           (bw4 > ss_hor || t->bx & 1) &&
                           (bh4 > ss_ver || t->by & 1);
761 762
    const TxfmInfo *const t_dim = &dav1d_txfm_dimensions[b->tx];
    const TxfmInfo *const uv_t_dim = &dav1d_txfm_dimensions[b->uvtx];
763 764

    // coefficient coding
765 766
    ALIGN_STK_32(pixel, edge_buf, 257,);
    pixel *const edge = edge_buf + 128;
767 768
    const int cbw4 = (bw4 + ss_hor) >> ss_hor, cbh4 = (bh4 + ss_ver) >> ss_ver;

769
    const int intra_edge_filter_flag = f->seq_hdr->intra_edge_filter << 10;
770

771 772 773
    for (int init_y = 0; init_y < h4; init_y += 16) {
        for (int init_x = 0; init_x < w4; init_x += 16) {
            if (b->pal_sz[0]) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
774 775
                pixel *dst = ((pixel *) f->cur.data[0]) +
                             4 * (t->by * PXSTRIDE(f->cur.stride[0]) + t->bx);
776 777 778 779 780 781 782 783 784 785
                const uint8_t *pal_idx;
                if (f->frame_thread.pass) {
                    pal_idx = ts->frame_thread.pal_idx;
                    ts->frame_thread.pal_idx += bw4 * bh4 * 16;
                } else {
                    pal_idx = t->scratch.pal_idx;
                }
                const uint16_t *const pal = f->frame_thread.pass ?
                    f->frame_thread.pal[((t->by >> 1) + (t->bx & 1)) * (f->b4_stride >> 1) +
                                        ((t->bx >> 1) + (t->by & 1))][0] : t->pal[0];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
786
                f->dsp->ipred.pal_pred(dst, f->cur.stride[0], pal,
787 788
                                       pal_idx, bw4 * 4, bh4 * 4);
                if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
789
                    hex_dump(dst, PXSTRIDE(f->cur.stride[0]),
790 791 792
                             bw4 * 4, bh4 * 4, "y-pal-pred");
            }

793 794 795
            const int intra_flags = (sm_flag(t->a, bx4) |
                                     sm_flag(&t->l, by4) |
                                     intra_edge_filter_flag);
796 797 798 799 800 801 802 803 804 805
            const int sb_has_tr = init_x + 16 < w4 ? 1 : init_y ? 0 :
                              intra_edge_flags & EDGE_I444_TOP_HAS_RIGHT;
            const int sb_has_bl = init_x ? 0 : init_y + 16 < h4 ? 1 :
                              intra_edge_flags & EDGE_I444_LEFT_HAS_BOTTOM;
            int y, x;
            const int sub_h4 = imin(h4, 16 + init_y);
            const int sub_w4 = imin(w4, init_x + 16);
            for (y = init_y, t->by += init_y; y < sub_h4;
                 y += t_dim->h, t->by += t_dim->h)
            {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
806 807
                pixel *dst = ((pixel *) f->cur.data[0]) +
                               4 * (t->by * PXSTRIDE(f->cur.stride[0]) +
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
                                    t->bx + init_x);
                for (x = init_x, t->bx += init_x; x < sub_w4;
                     x += t_dim->w, t->bx += t_dim->w)
                {
                    if (b->pal_sz[0]) goto skip_y_pred;

                    int angle = b->y_angle;
                    const enum EdgeFlags edge_flags =
                        (((y > init_y || !sb_has_tr) && (x + t_dim->w >= sub_w4)) ?
                             0 : EDGE_I444_TOP_HAS_RIGHT) |
                        ((x > init_x || (!sb_has_bl && y + t_dim->h >= sub_h4)) ?
                             0 : EDGE_I444_LEFT_HAS_BOTTOM);
                    const pixel *top_sb_edge = NULL;
                    if (!(t->by & (f->sb_step - 1))) {
                        top_sb_edge = f->ipred_edge[0];
                        const int sby = t->by >> f->sb_shift;
                        top_sb_edge += f->sb128w * 128 * (sby - 1);
                    }
                    const enum IntraPredMode m =
827 828 829 830 831 832 833
                        bytefn(dav1d_prepare_intra_edges)(t->bx,
                                                          t->bx > ts->tiling.col_start,
                                                          t->by,
                                                          t->by > ts->tiling.row_start,
                                                          ts->tiling.col_end,
                                                          ts->tiling.row_end,
                                                          edge_flags, dst,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
834
                                                          f->cur.stride[0], top_sb_edge,
835
                                                          b->y_mode, &angle,
836 837 838
                                                          t_dim->w, t_dim->h,
                                                          f->seq_hdr->intra_edge_filter,
                                                          edge HIGHBD_CALL_SUFFIX);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
839
                    dsp->ipred.intra_pred[m](dst, f->cur.stride[0], edge,
840
                                             t_dim->w * 4, t_dim->h * 4,
841
                                             angle | intra_flags,
842
                                             4 * f->bw - 4 * t->bx,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
843 844
                                             4 * f->bh - 4 * t->by
                                             HIGHBD_CALL_SUFFIX);
845 846 847 848 849 850 851

                    if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS) {
                        hex_dump(edge - t_dim->h * 4, t_dim->h * 4,
                                 t_dim->h * 4, 2, "l");
                        hex_dump(edge, 0, 1, 1, "tl");
                        hex_dump(edge + 1, t_dim->w * 4,
                                 t_dim->w * 4, 2, "t");
Ronald S. Bultje's avatar
Ronald S. Bultje committed
852
                        hex_dump(dst, f->cur.stride[0],
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
                                 t_dim->w * 4, t_dim->h * 4, "y-intra-pred");
                    }

                skip_y_pred: {}
                    if (!b->skip) {
                        coef *cf;
                        int eob;
                        enum TxfmType txtp;
                        if (f->frame_thread.pass) {
                            cf = ts->frame_thread.cf;
                            ts->frame_thread.cf += imin(t_dim->w, 8) * imin(t_dim->h, 8) * 16;
                            const struct CodedBlockInfo *const cbi =
                                &f->frame_thread.cbi[t->by * f->b4_stride + t->bx];
                            eob = cbi->eob[0];
                            txtp = cbi->txtp[0];
                        } else {
                            uint8_t cf_ctx;
                            cf = t->cf;
                            eob = decode_coefs(t, &t->a->lcoef[bx4 + x],
                                               &t->l.lcoef[by4 + y], b->tx, bs,
                                               b, 1, 0, cf, &txtp, &cf_ctx);
                            if (DEBUG_BLOCK_INFO)
                                printf("Post-y-cf-blk[tx=%d,txtp=%d,eob=%d]: r=%d\n",
                                       b->tx, txtp, eob, ts->msac.rng);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
877 878 879 880 881 882 883 884 885 886
#define set_ctx(type, dir, diridx, off, mul, rep_macro) \
                            rep_macro(type, t->dir lcoef, off, mul * cf_ctx)
#define default_memset(dir, diridx, off, sz) \
                            memset(&t->dir lcoef[off], cf_ctx, sz)
                            case_set_upto16_with_default(imin(t_dim->h, f->bh - t->by), \
                                                         l., 1, by4 + y);
                            case_set_upto16_with_default(imin(t_dim->w, f->bw - t->bx), \
                                                         a->, 0, bx4 + x);
#undef default_memset
#undef set_ctx
887 888 889 890 891 892 893
                        }
                        if (eob >= 0) {
                            if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS)
                                coef_dump(cf, imin(t_dim->h, 8) * 4,
                                          imin(t_dim->w, 8) * 4, 3, "dq");
                            dsp->itx.itxfm_add[b->tx]
                                              [txtp](dst,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
894
                                                     f->cur.stride[0],
Ronald S. Bultje's avatar
Ronald S. Bultje committed
895
                                                     cf, eob HIGHBD_CALL_SUFFIX);
896
                            if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
897
                                hex_dump(dst, f->cur.stride[0],
898 899 900
                                         t_dim->w * 4, t_dim->h * 4, "recon");
                        }
                    } else if (!f->frame_thread.pass) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
901 902 903 904 905
#define set_ctx(type, dir, diridx, off, mul, rep_macro) \
                        rep_macro(type, t->dir lcoef, off, mul * 0x40)
                        case_set_upto16(t_dim->h, l., 1, by4 + y);
                        case_set_upto16(t_dim->w, a->, 0, bx4 + x);
#undef set_ctx
906 907 908 909 910 911 912 913 914
                    }
                    dst += 4 * t_dim->w;
                }
                t->bx -= x;
            }
            t->by -= y;

            if (!has_chroma) continue;

Ronald S. Bultje's avatar
Ronald S. Bultje committed
915
            const ptrdiff_t stride = f->cur.stride[1];
916 917 918 919 920

            if (b->uv_mode == CFL_PRED) {
                assert(!init_x && !init_y);

                int16_t *const ac = t->scratch.ac;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
921 922
                pixel *y_src = ((pixel *) f->cur.data[0]) + 4 * (t->bx & ~ss_hor) +
                                 4 * (t->by & ~ss_ver) * PXSTRIDE(f->cur.stride[0]);
923 924
                const ptrdiff_t uv_off = 4 * ((t->bx >> ss_hor) +
                                              (t->by >> ss_ver) * PXSTRIDE(stride));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
925 926
                pixel *const uv_dst[2] = { ((pixel *) f->cur.data[1]) + uv_off,
                                           ((pixel *) f->cur.data[2]) + uv_off };
927

928 929 930 931
                const int furthest_r =
                    ((cw4 << ss_hor) + t_dim->w - 1) & ~(t_dim->w - 1);
                const int furthest_b =
                    ((ch4 << ss_ver) + t_dim->h - 1) & ~(t_dim->h - 1);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
932
                dsp->ipred.cfl_ac[f->cur.p.layout - 1](ac, y_src, f->cur.stride[0],
933 934 935
                                                         cbw4 - (furthest_r >> ss_hor),
                                                         cbh4 - (furthest_b >> ss_ver),
                                                         cbw4 * 4, cbh4 * 4);
936
                for (int pl = 0; pl < 2; pl++) {
937
                    if (!b->cfl_alpha[pl]) continue;
938 939 940 941 942 943 944
                    int angle = 0;
                    const pixel *top_sb_edge = NULL;
                    if (!((t->by & ~ss_ver) & (f->sb_step - 1))) {
                        top_sb_edge = f->ipred_edge[pl + 1];
                        const int sby = t->by >> f->sb_shift;
                        top_sb_edge += f->sb128w * 128 * (sby - 1);
                    }
945 946 947
                    const int xpos = t->bx >> ss_hor, ypos = t->by >> ss_ver;
                    const int xstart = ts->tiling.col_start >> ss_hor;
                    const int ystart = ts->tiling.row_start >> ss_ver;
948
                    const enum IntraPredMode m =
949 950
                        bytefn(dav1d_prepare_intra_edges)(xpos, xpos > xstart,
                                                          ypos, ypos > ystart,
951 952 953 954
                                                          ts->tiling.col_end >> ss_hor,
                                                          ts->tiling.row_end >> ss_ver,
                                                          0, uv_dst[pl], stride,
                                                          top_sb_edge, DC_PRED, &angle,
955 956
                                                          uv_t_dim->w, uv_t_dim->h, 0,
                                                          edge HIGHBD_CALL_SUFFIX);
957 958 959
                    dsp->ipred.cfl_pred[m](uv_dst[pl], stride, edge,
                                           uv_t_dim->w * 4,
                                           uv_t_dim->h * 4,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
960 961
                                           ac, b->cfl_alpha[pl]
                                           HIGHBD_CALL_SUFFIX);
962
                }
963
                if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS) {
964
                    ac_dump(ac, 4*cbw4, 4*cbh4, "ac");
965 966 967 968 969
                    hex_dump(uv_dst[0], stride, cbw4 * 4, cbh4 * 4, "u-cfl-pred");
                    hex_dump(uv_dst[1], stride, cbw4 * 4, cbh4 * 4, "v-cfl-pred");
                }
            } else if (b->pal_sz[1]) {
                ptrdiff_t uv_dstoff = 4 * ((t->bx >> ss_hor) +
Ronald S. Bultje's avatar
Ronald S. Bultje committed
970
                                           (t->by >> ss_ver) * PXSTRIDE(f->cur.stride[1]));
971 972 973 974 975 976 977 978 979 980
                const uint8_t *pal_idx;
                if (f->frame_thread.pass) {
                    pal_idx = ts->frame_thread.pal_idx;
                    ts->frame_thread.pal_idx += cbw4 * cbh4 * 16;
                } else {
                    pal_idx = &t->scratch.pal_idx[bw4 * bh4 * 16];
                }
                const uint16_t *const pal_u = f->frame_thread.pass ?
                    f->frame_thread.pal[((t->by >> 1) + (t->bx & 1)) * (f->b4_stride >> 1) +
                                        ((t->bx >> 1) + (t->by & 1))][1] : t->pal[1];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
981 982
                f->dsp->ipred.pal_pred(((pixel *) f->cur.data[1]) + uv_dstoff,
                                       f->cur.stride[1], pal_u,
983 984 985 986
                                       pal_idx, cbw4 * 4, cbh4 * 4);
                const uint16_t *const pal_v = f->frame_thread.pass ?
                    f->frame_thread.pal[((t->by >> 1) + (t->bx & 1)) * (f->b4_stride >> 1) +
                                        ((t->bx >> 1) + (t->by & 1))][2] : t->pal[2];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
987 988
                f->dsp->ipred.pal_pred(((pixel *) f->cur.data[2]) + uv_dstoff,
                                       f->cur.stride[1], pal_v,
989 990
                                       pal_idx, cbw4 * 4, cbh4 * 4);
                if (DEBUG_BLOCK_INFO && DEBUG_B_PIXELS) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
991 992
                    hex_dump(((pixel *) f->cur.data[1]) + uv_dstoff,
                             PXSTRIDE(f->cur.stride[1]),
993
                             cbw4 * 4, cbh4 * 4, "u-pal-pred");
Ronald S. Bultje's avatar
Ronald S. Bultje committed
994 995
                    hex_dump(((pixel *) f->cur.data[2]) + uv_dstoff,
                             PXSTRIDE(f->cur.stride[1]),
996 997 998 999 1000 1001 1002 1003
                             cbw4 * 4, cbh4 * 4, "v-pal-pred");
                }
            }

            const int sm_uv_fl = sm_uv_flag(t->a, cbx4) |
                                 sm_uv_flag(&t->l, cby4);
            const int uv_sb_has_tr =
                ((init_x + 16) >> ss_hor) < cw4 ? 1 : init_y ? 0 :
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1004
                intra_edge_flags & (EDGE_I420_TOP_HAS_RIGHT >> (f->cur.p.layout - 1));
1005 1006
            const int uv_sb_has_bl =
                init_x ? 0 : ((init_y + 16) >> ss_ver) < ch4 ? 1 :
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1007
                intra_edge_flags & (EDGE_I420_LEFT_HAS_BOTTOM >> (f->cur.p.layout - 1));
1008 1009 1010 1011 1012 1013
            const int sub_ch4 = imin(ch4, (init_y + 16) >> ss_ver);
            const int sub_cw4 = imin(cw4, (init_x + 16) >> ss_hor);
            for (int pl = 0; pl < 2; pl++) {
                for (y = init_y >> ss_ver, t->by += init_y; y < sub_ch4;
                     y += uv_t_dim->h, t->by += uv_t_dim->h << ss_ver)
                {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1014
                    pixel *dst = ((pixel *) f->cur.data[1 + pl]) +
1015 1016 1017 1018 1019
                                   4 * ((t->by >> ss_ver) * PXSTRIDE(stride) +
                                        ((t->bx + init_x) >> ss_hor));
                    for (x = init_x >> ss_hor, t->bx += init_x; x < sub_cw4;
                         x += uv_t_dim->w, t->bx += uv_t_dim->w << ss_hor)
                    {
1020 1021 1022
                        if ((b->uv_mode == CFL_PRED && b->cfl_alpha[pl]) ||
                            b->pal_sz[1])
                        {
1023
                            goto skip_uv_pred;
1024
                        }
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036