tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

decodeframe.c (213759B)


      1 /*
      2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
      3 *
      4 * This source code is subject to the terms of the BSD 2 Clause License and
      5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
      6 * was not distributed with this source code in the LICENSE file, you can
      7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
      8 * Media Patent License 1.0 was not distributed with this source code in the
      9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
     10 */
     11 
     12 #include <assert.h>
     13 #include <stdbool.h>
     14 #include <stddef.h>
     15 
     16 #include "config/aom_config.h"
     17 #include "config/aom_scale_rtcd.h"
     18 
     19 #include "aom/aom_codec.h"
     20 #include "aom/aom_image.h"
     21 #include "aom/internal/aom_codec_internal.h"
     22 #include "aom_dsp/aom_dsp_common.h"
     23 #include "aom_dsp/binary_codes_reader.h"
     24 #include "aom_dsp/bitreader.h"
     25 #include "aom_dsp/bitreader_buffer.h"
     26 #include "aom_dsp/txfm_common.h"
     27 #include "aom_mem/aom_mem.h"
     28 #include "aom_ports/aom_timer.h"
     29 #include "aom_ports/mem.h"
     30 #include "aom_ports/mem_ops.h"
     31 #include "aom_scale/yv12config.h"
     32 #include "aom_util/aom_pthread.h"
     33 #include "aom_util/aom_thread.h"
     34 
     35 #if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
     36 #include "aom_util/debug_util.h"
     37 #endif  // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
     38 
     39 #include "av1/common/alloccommon.h"
     40 #include "av1/common/av1_common_int.h"
     41 #include "av1/common/blockd.h"
     42 #include "av1/common/cdef.h"
     43 #include "av1/common/cfl.h"
     44 #include "av1/common/common_data.h"
     45 #include "av1/common/common.h"
     46 #include "av1/common/entropy.h"
     47 #include "av1/common/entropymode.h"
     48 #include "av1/common/entropymv.h"
     49 #include "av1/common/enums.h"
     50 #include "av1/common/frame_buffers.h"
     51 #include "av1/common/idct.h"
     52 #include "av1/common/mv.h"
     53 #include "av1/common/mvref_common.h"
     54 #include "av1/common/obmc.h"
     55 #include "av1/common/pred_common.h"
     56 #include "av1/common/quant_common.h"
     57 #include "av1/common/reconinter.h"
     58 #include "av1/common/reconintra.h"
     59 #include "av1/common/resize.h"
     60 #include "av1/common/restoration.h"
     61 #include "av1/common/scale.h"
     62 #include "av1/common/seg_common.h"
     63 #include "av1/common/thread_common.h"
     64 #include "av1/common/tile_common.h"
     65 #include "av1/common/warped_motion.h"
     66 
     67 #include "av1/decoder/decodeframe.h"
     68 #include "av1/decoder/decodemv.h"
     69 #include "av1/decoder/decoder.h"
     70 #include "av1/decoder/decodetxb.h"
     71 #include "av1/decoder/detokenize.h"
     72 #if CONFIG_INSPECTION
     73 #include "av1/decoder/inspection.h"
     74 #endif
     75 
     76 #define ACCT_STR __func__
     77 
     78 #define AOM_MIN_THREADS_PER_TILE 1
     79 #define AOM_MAX_THREADS_PER_TILE 2
     80 
     81 // This is needed by ext_tile related unit tests.
     82 #define EXT_TILE_DEBUG 1
     83 #define MC_TEMP_BUF_PELS                           \
     84  (((MAX_SB_SIZE) * 2 + (AOM_INTERP_EXTEND) * 2) * \
     85   ((MAX_SB_SIZE) * 2 + (AOM_INTERP_EXTEND) * 2))
     86 
     87 // Checks that the remaining bits start with a 1 and ends with 0s.
     88 // It consumes an additional byte, if already byte aligned before the check.
     89 int av1_check_trailing_bits(AV1Decoder *pbi, struct aom_read_bit_buffer *rb) {
     90  // bit_offset is set to 0 (mod 8) when the reader is already byte aligned
     91  int bits_before_alignment = 8 - rb->bit_offset % 8;
     92  int trailing = aom_rb_read_literal(rb, bits_before_alignment);
     93  if (trailing != (1 << (bits_before_alignment - 1))) {
     94    pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
     95    return -1;
     96  }
     97  return 0;
     98 }
     99 
    100 // Use only_chroma = 1 to only set the chroma planes
    101 static inline void set_planes_to_neutral_grey(
    102    const SequenceHeader *const seq_params, const YV12_BUFFER_CONFIG *const buf,
    103    int only_chroma) {
    104  if (seq_params->use_highbitdepth) {
    105    const int val = 1 << (seq_params->bit_depth - 1);
    106    for (int plane = only_chroma; plane < MAX_MB_PLANE; plane++) {
    107      const int is_uv = plane > 0;
    108      uint16_t *const base = CONVERT_TO_SHORTPTR(buf->buffers[plane]);
    109      // Set the first row to neutral grey. Then copy the first row to all
    110      // subsequent rows.
    111      if (buf->crop_heights[is_uv] > 0) {
    112        aom_memset16(base, val, buf->crop_widths[is_uv]);
    113        for (int row_idx = 1; row_idx < buf->crop_heights[is_uv]; row_idx++) {
    114          memcpy(&base[row_idx * buf->strides[is_uv]], base,
    115                 sizeof(*base) * buf->crop_widths[is_uv]);
    116        }
    117      }
    118    }
    119  } else {
    120    for (int plane = only_chroma; plane < MAX_MB_PLANE; plane++) {
    121      const int is_uv = plane > 0;
    122      for (int row_idx = 0; row_idx < buf->crop_heights[is_uv]; row_idx++) {
    123        memset(&buf->buffers[plane][row_idx * buf->strides[is_uv]], 1 << 7,
    124               buf->crop_widths[is_uv]);
    125      }
    126    }
    127  }
    128 }
    129 
    130 static inline void loop_restoration_read_sb_coeffs(const AV1_COMMON *const cm,
    131                                                   MACROBLOCKD *xd,
    132                                                   aom_reader *const r,
    133                                                   int plane, int runit_idx);
    134 
    135 static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
    136  return len != 0 && len <= (size_t)(end - start);
    137 }
    138 
    139 static TX_MODE read_tx_mode(struct aom_read_bit_buffer *rb,
    140                            int coded_lossless) {
    141  if (coded_lossless) return ONLY_4X4;
    142  return aom_rb_read_bit(rb) ? TX_MODE_SELECT : TX_MODE_LARGEST;
    143 }
    144 
    145 static REFERENCE_MODE read_frame_reference_mode(
    146    const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
    147  if (frame_is_intra_only(cm)) {
    148    return SINGLE_REFERENCE;
    149  } else {
    150    return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
    151  }
    152 }
    153 
    154 static inline void inverse_transform_block(DecoderCodingBlock *dcb, int plane,
    155                                           const TX_TYPE tx_type,
    156                                           const TX_SIZE tx_size, uint8_t *dst,
    157                                           int stride, int reduced_tx_set) {
    158  tran_low_t *const dqcoeff = dcb->dqcoeff_block[plane] + dcb->cb_offset[plane];
    159  eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
    160  uint16_t scan_line = eob_data->max_scan_line;
    161  uint16_t eob = eob_data->eob;
    162  av1_inverse_transform_block(&dcb->xd, dqcoeff, plane, tx_type, tx_size, dst,
    163                              stride, eob, reduced_tx_set);
    164  memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
    165 }
    166 
    167 static inline void read_coeffs_tx_intra_block(
    168    const AV1_COMMON *const cm, DecoderCodingBlock *dcb, aom_reader *const r,
    169    const int plane, const int row, const int col, const TX_SIZE tx_size) {
    170  MB_MODE_INFO *mbmi = dcb->xd.mi[0];
    171  if (!mbmi->skip_txfm) {
    172 #if TXCOEFF_TIMER
    173    struct aom_usec_timer timer;
    174    aom_usec_timer_start(&timer);
    175 #endif
    176    av1_read_coeffs_txb(cm, dcb, r, plane, row, col, tx_size);
    177 #if TXCOEFF_TIMER
    178    aom_usec_timer_mark(&timer);
    179    const int64_t elapsed_time = aom_usec_timer_elapsed(&timer);
    180    cm->txcoeff_timer += elapsed_time;
    181    ++cm->txb_count;
    182 #endif
    183  }
    184 }
    185 
    186 static inline void decode_block_void(const AV1_COMMON *const cm,
    187                                     DecoderCodingBlock *dcb,
    188                                     aom_reader *const r, const int plane,
    189                                     const int row, const int col,
    190                                     const TX_SIZE tx_size) {
    191  (void)cm;
    192  (void)dcb;
    193  (void)r;
    194  (void)plane;
    195  (void)row;
    196  (void)col;
    197  (void)tx_size;
    198 }
    199 
    200 static inline void predict_inter_block_void(AV1_COMMON *const cm,
    201                                            DecoderCodingBlock *dcb,
    202                                            BLOCK_SIZE bsize) {
    203  (void)cm;
    204  (void)dcb;
    205  (void)bsize;
    206 }
    207 
    208 static inline void cfl_store_inter_block_void(AV1_COMMON *const cm,
    209                                              MACROBLOCKD *const xd) {
    210  (void)cm;
    211  (void)xd;
    212 }
    213 
    214 static inline void predict_and_reconstruct_intra_block(
    215    const AV1_COMMON *const cm, DecoderCodingBlock *dcb, aom_reader *const r,
    216    const int plane, const int row, const int col, const TX_SIZE tx_size) {
    217  (void)r;
    218  MACROBLOCKD *const xd = &dcb->xd;
    219  MB_MODE_INFO *mbmi = xd->mi[0];
    220  PLANE_TYPE plane_type = get_plane_type(plane);
    221 
    222  av1_predict_intra_block_facade(cm, xd, plane, col, row, tx_size);
    223 
    224  if (!mbmi->skip_txfm) {
    225    eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
    226    if (eob_data->eob) {
    227      const bool reduced_tx_set_used = cm->features.reduced_tx_set_used;
    228      // tx_type was read out in av1_read_coeffs_txb.
    229      const TX_TYPE tx_type = av1_get_tx_type(xd, plane_type, row, col, tx_size,
    230                                              reduced_tx_set_used);
    231      struct macroblockd_plane *const pd = &xd->plane[plane];
    232      uint8_t *dst = &pd->dst.buf[(row * pd->dst.stride + col) << MI_SIZE_LOG2];
    233      inverse_transform_block(dcb, plane, tx_type, tx_size, dst, pd->dst.stride,
    234                              reduced_tx_set_used);
    235    }
    236  }
    237  if (plane == AOM_PLANE_Y && store_cfl_required(cm, xd)) {
    238    cfl_store_tx(xd, row, col, tx_size, mbmi->bsize);
    239  }
    240 }
    241 
    242 static inline void inverse_transform_inter_block(
    243    const AV1_COMMON *const cm, DecoderCodingBlock *dcb, aom_reader *const r,
    244    const int plane, const int blk_row, const int blk_col,
    245    const TX_SIZE tx_size) {
    246  (void)r;
    247  MACROBLOCKD *const xd = &dcb->xd;
    248  PLANE_TYPE plane_type = get_plane_type(plane);
    249  const struct macroblockd_plane *const pd = &xd->plane[plane];
    250  const bool reduced_tx_set_used = cm->features.reduced_tx_set_used;
    251  // tx_type was read out in av1_read_coeffs_txb.
    252  const TX_TYPE tx_type = av1_get_tx_type(xd, plane_type, blk_row, blk_col,
    253                                          tx_size, reduced_tx_set_used);
    254 
    255  uint8_t *dst =
    256      &pd->dst.buf[(blk_row * pd->dst.stride + blk_col) << MI_SIZE_LOG2];
    257  inverse_transform_block(dcb, plane, tx_type, tx_size, dst, pd->dst.stride,
    258                          reduced_tx_set_used);
    259 #if CONFIG_MISMATCH_DEBUG
    260  int pixel_c, pixel_r;
    261  BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
    262  int blk_w = block_size_wide[bsize];
    263  int blk_h = block_size_high[bsize];
    264  const int mi_row = -xd->mb_to_top_edge >> (3 + MI_SIZE_LOG2);
    265  const int mi_col = -xd->mb_to_left_edge >> (3 + MI_SIZE_LOG2);
    266  mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, blk_col, blk_row,
    267                  pd->subsampling_x, pd->subsampling_y);
    268  mismatch_check_block_tx(dst, pd->dst.stride, cm->current_frame.order_hint,
    269                          plane, pixel_c, pixel_r, blk_w, blk_h,
    270                          xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
    271 #endif
    272 }
    273 
    274 static inline void set_cb_buffer_offsets(DecoderCodingBlock *dcb,
    275                                         TX_SIZE tx_size, int plane) {
    276  dcb->cb_offset[plane] += tx_size_wide[tx_size] * tx_size_high[tx_size];
    277  dcb->txb_offset[plane] =
    278      dcb->cb_offset[plane] / (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
    279 }
    280 
    281 static inline void decode_reconstruct_tx(AV1_COMMON *cm, ThreadData *const td,
    282                                         aom_reader *r,
    283                                         MB_MODE_INFO *const mbmi, int plane,
    284                                         BLOCK_SIZE plane_bsize, int blk_row,
    285                                         int blk_col, int block,
    286                                         TX_SIZE tx_size, int *eob_total) {
    287  DecoderCodingBlock *const dcb = &td->dcb;
    288  MACROBLOCKD *const xd = &dcb->xd;
    289  const struct macroblockd_plane *const pd = &xd->plane[plane];
    290  const TX_SIZE plane_tx_size =
    291      plane ? av1_get_max_uv_txsize(mbmi->bsize, pd->subsampling_x,
    292                                    pd->subsampling_y)
    293            : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row,
    294                                                         blk_col)];
    295  // Scale to match transform block unit.
    296  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
    297  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
    298 
    299  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
    300 
    301  if (tx_size == plane_tx_size || plane) {
    302    td->read_coeffs_tx_inter_block_visit(cm, dcb, r, plane, blk_row, blk_col,
    303                                         tx_size);
    304 
    305    td->inverse_tx_inter_block_visit(cm, dcb, r, plane, blk_row, blk_col,
    306                                     tx_size);
    307    eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
    308    *eob_total += eob_data->eob;
    309    set_cb_buffer_offsets(dcb, tx_size, plane);
    310  } else {
    311    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
    312    assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
    313    assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
    314    const int bsw = tx_size_wide_unit[sub_txs];
    315    const int bsh = tx_size_high_unit[sub_txs];
    316    const int sub_step = bsw * bsh;
    317    const int row_end =
    318        AOMMIN(tx_size_high_unit[tx_size], max_blocks_high - blk_row);
    319    const int col_end =
    320        AOMMIN(tx_size_wide_unit[tx_size], max_blocks_wide - blk_col);
    321 
    322    assert(bsw > 0 && bsh > 0);
    323 
    324    for (int row = 0; row < row_end; row += bsh) {
    325      const int offsetr = blk_row + row;
    326      for (int col = 0; col < col_end; col += bsw) {
    327        const int offsetc = blk_col + col;
    328 
    329        decode_reconstruct_tx(cm, td, r, mbmi, plane, plane_bsize, offsetr,
    330                              offsetc, block, sub_txs, eob_total);
    331        block += sub_step;
    332      }
    333    }
    334  }
    335 }
    336 
    337 static inline void set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
    338                               BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
    339                               int bh, int x_mis, int y_mis) {
    340  const int num_planes = av1_num_planes(cm);
    341  const CommonModeInfoParams *const mi_params = &cm->mi_params;
    342  const TileInfo *const tile = &xd->tile;
    343 
    344  set_mi_offsets(mi_params, xd, mi_row, mi_col);
    345  xd->mi[0]->bsize = bsize;
    346 #if CONFIG_RD_DEBUG
    347  xd->mi[0]->mi_row = mi_row;
    348  xd->mi[0]->mi_col = mi_col;
    349 #endif
    350 
    351  assert(x_mis && y_mis);
    352  for (int x = 1; x < x_mis; ++x) xd->mi[x] = xd->mi[0];
    353  int idx = mi_params->mi_stride;
    354  for (int y = 1; y < y_mis; ++y) {
    355    memcpy(&xd->mi[idx], &xd->mi[0], x_mis * sizeof(xd->mi[0]));
    356    idx += mi_params->mi_stride;
    357  }
    358 
    359  set_plane_n4(xd, bw, bh, num_planes);
    360  set_entropy_context(xd, mi_row, mi_col, num_planes);
    361 
    362  // Distance of Mb to the various image edges. These are specified to 8th pel
    363  // as they are always compared to values that are in 1/8th pel units
    364  set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, mi_params->mi_rows,
    365                 mi_params->mi_cols);
    366 
    367  av1_setup_dst_planes(xd->plane, bsize, &cm->cur_frame->buf, mi_row, mi_col, 0,
    368                       num_planes);
    369 }
    370 
    371 static inline void decode_mbmi_block(AV1Decoder *const pbi,
    372                                     DecoderCodingBlock *dcb, int mi_row,
    373                                     int mi_col, aom_reader *r,
    374                                     PARTITION_TYPE partition,
    375                                     BLOCK_SIZE bsize) {
    376  AV1_COMMON *const cm = &pbi->common;
    377  const SequenceHeader *const seq_params = cm->seq_params;
    378  const int bw = mi_size_wide[bsize];
    379  const int bh = mi_size_high[bsize];
    380  const int x_mis = AOMMIN(bw, cm->mi_params.mi_cols - mi_col);
    381  const int y_mis = AOMMIN(bh, cm->mi_params.mi_rows - mi_row);
    382  MACROBLOCKD *const xd = &dcb->xd;
    383 
    384 #if CONFIG_ACCOUNTING
    385  aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
    386 #endif
    387  set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
    388  xd->mi[0]->partition = partition;
    389  av1_read_mode_info(pbi, dcb, r, x_mis, y_mis);
    390  if (bsize >= BLOCK_8X8 &&
    391      (seq_params->subsampling_x || seq_params->subsampling_y)) {
    392    const BLOCK_SIZE uv_subsize =
    393        av1_ss_size_lookup[bsize][seq_params->subsampling_x]
    394                          [seq_params->subsampling_y];
    395    if (uv_subsize == BLOCK_INVALID)
    396      aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
    397                         "Invalid block size.");
    398  }
    399 }
    400 
    401 typedef struct PadBlock {
    402  int x0;
    403  int x1;
    404  int y0;
    405  int y1;
    406 } PadBlock;
    407 
    408 #if CONFIG_AV1_HIGHBITDEPTH
    409 static inline void highbd_build_mc_border(const uint8_t *src8, int src_stride,
    410                                          uint8_t *dst8, int dst_stride, int x,
    411                                          int y, int b_w, int b_h, int w,
    412                                          int h) {
    413  // Get a pointer to the start of the real data for this row.
    414  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
    415  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
    416  const uint16_t *ref_row = src - x - y * src_stride;
    417 
    418  if (y >= h)
    419    ref_row += (h - 1) * src_stride;
    420  else if (y > 0)
    421    ref_row += y * src_stride;
    422 
    423  do {
    424    int right = 0, copy;
    425    int left = x < 0 ? -x : 0;
    426 
    427    if (left > b_w) left = b_w;
    428 
    429    if (x + b_w > w) right = x + b_w - w;
    430 
    431    if (right > b_w) right = b_w;
    432 
    433    copy = b_w - left - right;
    434 
    435    if (left) aom_memset16(dst, ref_row[0], left);
    436 
    437    if (copy) memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t));
    438 
    439    if (right) aom_memset16(dst + left + copy, ref_row[w - 1], right);
    440 
    441    dst += dst_stride;
    442    ++y;
    443 
    444    if (y > 0 && y < h) ref_row += src_stride;
    445  } while (--b_h);
    446 }
    447 #endif  // CONFIG_AV1_HIGHBITDEPTH
    448 
    449 static inline void build_mc_border(const uint8_t *src, int src_stride,
    450                                   uint8_t *dst, int dst_stride, int x, int y,
    451                                   int b_w, int b_h, int w, int h) {
    452  // Get a pointer to the start of the real data for this row.
    453  const uint8_t *ref_row = src - x - y * src_stride;
    454 
    455  if (y >= h)
    456    ref_row += (h - 1) * src_stride;
    457  else if (y > 0)
    458    ref_row += y * src_stride;
    459 
    460  do {
    461    int right = 0, copy;
    462    int left = x < 0 ? -x : 0;
    463 
    464    if (left > b_w) left = b_w;
    465 
    466    if (x + b_w > w) right = x + b_w - w;
    467 
    468    if (right > b_w) right = b_w;
    469 
    470    copy = b_w - left - right;
    471 
    472    if (left) memset(dst, ref_row[0], left);
    473 
    474    if (copy) memcpy(dst + left, ref_row + x + left, copy);
    475 
    476    if (right) memset(dst + left + copy, ref_row[w - 1], right);
    477 
    478    dst += dst_stride;
    479    ++y;
    480 
    481    if (y > 0 && y < h) ref_row += src_stride;
    482  } while (--b_h);
    483 }
    484 
    485 static inline int update_extend_mc_border_params(
    486    const struct scale_factors *const sf, struct buf_2d *const pre_buf,
    487    MV32 scaled_mv, PadBlock *block, int subpel_x_mv, int subpel_y_mv,
    488    int do_warp, int is_intrabc, int *x_pad, int *y_pad) {
    489  const int is_scaled = av1_is_scaled(sf);
    490  // Get reference width and height.
    491  int frame_width = pre_buf->width;
    492  int frame_height = pre_buf->height;
    493 
    494  // Do border extension if there is motion or
    495  // width/height is not a multiple of 8 pixels.
    496  if ((!is_intrabc) && (!do_warp) &&
    497      (is_scaled || scaled_mv.col || scaled_mv.row || (frame_width & 0x7) ||
    498       (frame_height & 0x7))) {
    499    if (subpel_x_mv || (sf->x_step_q4 != SUBPEL_SHIFTS)) {
    500      block->x0 -= AOM_INTERP_EXTEND - 1;
    501      block->x1 += AOM_INTERP_EXTEND;
    502      *x_pad = 1;
    503    }
    504 
    505    if (subpel_y_mv || (sf->y_step_q4 != SUBPEL_SHIFTS)) {
    506      block->y0 -= AOM_INTERP_EXTEND - 1;
    507      block->y1 += AOM_INTERP_EXTEND;
    508      *y_pad = 1;
    509    }
    510 
    511    // Skip border extension if block is inside the frame.
    512    if (block->x0 < 0 || block->x1 > frame_width - 1 || block->y0 < 0 ||
    513        block->y1 > frame_height - 1) {
    514      return 1;
    515    }
    516  }
    517  return 0;
    518 }
    519 
    520 static inline void extend_mc_border(const struct scale_factors *const sf,
    521                                    struct buf_2d *const pre_buf,
    522                                    MV32 scaled_mv, PadBlock block,
    523                                    int subpel_x_mv, int subpel_y_mv,
    524                                    int do_warp, int is_intrabc, int highbd,
    525                                    uint8_t *mc_buf, uint8_t **pre,
    526                                    int *src_stride) {
    527  int x_pad = 0, y_pad = 0;
    528  if (update_extend_mc_border_params(sf, pre_buf, scaled_mv, &block,
    529                                     subpel_x_mv, subpel_y_mv, do_warp,
    530                                     is_intrabc, &x_pad, &y_pad)) {
    531    // Get reference block pointer.
    532    const uint8_t *const buf_ptr =
    533        pre_buf->buf0 + block.y0 * pre_buf->stride + block.x0;
    534    int buf_stride = pre_buf->stride;
    535    const int b_w = block.x1 - block.x0;
    536    const int b_h = block.y1 - block.y0;
    537 
    538 #if CONFIG_AV1_HIGHBITDEPTH
    539    // Extend the border.
    540    if (highbd) {
    541      highbd_build_mc_border(buf_ptr, buf_stride, mc_buf, b_w, block.x0,
    542                             block.y0, b_w, b_h, pre_buf->width,
    543                             pre_buf->height);
    544    } else {
    545      build_mc_border(buf_ptr, buf_stride, mc_buf, b_w, block.x0, block.y0, b_w,
    546                      b_h, pre_buf->width, pre_buf->height);
    547    }
    548 #else
    549    (void)highbd;
    550    build_mc_border(buf_ptr, buf_stride, mc_buf, b_w, block.x0, block.y0, b_w,
    551                    b_h, pre_buf->width, pre_buf->height);
    552 #endif
    553    *src_stride = b_w;
    554    *pre = mc_buf + y_pad * (AOM_INTERP_EXTEND - 1) * b_w +
    555           x_pad * (AOM_INTERP_EXTEND - 1);
    556  }
    557 }
    558 
    559 static inline void dec_calc_subpel_params(
    560    const MV *const src_mv, InterPredParams *const inter_pred_params,
    561    const MACROBLOCKD *const xd, int mi_x, int mi_y, uint8_t **pre,
    562    SubpelParams *subpel_params, int *src_stride, PadBlock *block,
    563    MV32 *scaled_mv, int *subpel_x_mv, int *subpel_y_mv) {
    564  const struct scale_factors *sf = inter_pred_params->scale_factors;
    565  struct buf_2d *pre_buf = &inter_pred_params->ref_frame_buf;
    566  const int bw = inter_pred_params->block_width;
    567  const int bh = inter_pred_params->block_height;
    568  const int is_scaled = av1_is_scaled(sf);
    569  if (is_scaled) {
    570    int ssx = inter_pred_params->subsampling_x;
    571    int ssy = inter_pred_params->subsampling_y;
    572    int orig_pos_y = inter_pred_params->pix_row << SUBPEL_BITS;
    573    orig_pos_y += src_mv->row * (1 << (1 - ssy));
    574    int orig_pos_x = inter_pred_params->pix_col << SUBPEL_BITS;
    575    orig_pos_x += src_mv->col * (1 << (1 - ssx));
    576    int pos_y = av1_scaled_y(orig_pos_y, sf);
    577    int pos_x = av1_scaled_x(orig_pos_x, sf);
    578    pos_x += SCALE_EXTRA_OFF;
    579    pos_y += SCALE_EXTRA_OFF;
    580 
    581    const int top = -AOM_LEFT_TOP_MARGIN_SCALED(ssy);
    582    const int left = -AOM_LEFT_TOP_MARGIN_SCALED(ssx);
    583    const int bottom = (pre_buf->height + AOM_INTERP_EXTEND)
    584                       << SCALE_SUBPEL_BITS;
    585    const int right = (pre_buf->width + AOM_INTERP_EXTEND) << SCALE_SUBPEL_BITS;
    586    pos_y = clamp(pos_y, top, bottom);
    587    pos_x = clamp(pos_x, left, right);
    588 
    589    subpel_params->subpel_x = pos_x & SCALE_SUBPEL_MASK;
    590    subpel_params->subpel_y = pos_y & SCALE_SUBPEL_MASK;
    591    subpel_params->xs = sf->x_step_q4;
    592    subpel_params->ys = sf->y_step_q4;
    593 
    594    // Get reference block top left coordinate.
    595    block->x0 = pos_x >> SCALE_SUBPEL_BITS;
    596    block->y0 = pos_y >> SCALE_SUBPEL_BITS;
    597 
    598    // Get reference block bottom right coordinate.
    599    block->x1 =
    600        ((pos_x + (bw - 1) * subpel_params->xs) >> SCALE_SUBPEL_BITS) + 1;
    601    block->y1 =
    602        ((pos_y + (bh - 1) * subpel_params->ys) >> SCALE_SUBPEL_BITS) + 1;
    603 
    604    MV temp_mv;
    605    temp_mv = clamp_mv_to_umv_border_sb(xd, src_mv, bw, bh,
    606                                        inter_pred_params->subsampling_x,
    607                                        inter_pred_params->subsampling_y);
    608    *scaled_mv = av1_scale_mv(&temp_mv, mi_x, mi_y, sf);
    609    scaled_mv->row += SCALE_EXTRA_OFF;
    610    scaled_mv->col += SCALE_EXTRA_OFF;
    611 
    612    *subpel_x_mv = scaled_mv->col & SCALE_SUBPEL_MASK;
    613    *subpel_y_mv = scaled_mv->row & SCALE_SUBPEL_MASK;
    614  } else {
    615    // Get block position in current frame.
    616    int pos_x = inter_pred_params->pix_col << SUBPEL_BITS;
    617    int pos_y = inter_pred_params->pix_row << SUBPEL_BITS;
    618 
    619    const MV mv_q4 = clamp_mv_to_umv_border_sb(
    620        xd, src_mv, bw, bh, inter_pred_params->subsampling_x,
    621        inter_pred_params->subsampling_y);
    622    subpel_params->xs = subpel_params->ys = SCALE_SUBPEL_SHIFTS;
    623    subpel_params->subpel_x = (mv_q4.col & SUBPEL_MASK) << SCALE_EXTRA_BITS;
    624    subpel_params->subpel_y = (mv_q4.row & SUBPEL_MASK) << SCALE_EXTRA_BITS;
    625 
    626    // Get reference block top left coordinate.
    627    pos_x += mv_q4.col;
    628    pos_y += mv_q4.row;
    629    block->x0 = pos_x >> SUBPEL_BITS;
    630    block->y0 = pos_y >> SUBPEL_BITS;
    631 
    632    // Get reference block bottom right coordinate.
    633    block->x1 = (pos_x >> SUBPEL_BITS) + (bw - 1) + 1;
    634    block->y1 = (pos_y >> SUBPEL_BITS) + (bh - 1) + 1;
    635 
    636    scaled_mv->row = mv_q4.row;
    637    scaled_mv->col = mv_q4.col;
    638    *subpel_x_mv = scaled_mv->col & SUBPEL_MASK;
    639    *subpel_y_mv = scaled_mv->row & SUBPEL_MASK;
    640  }
    641  *pre = pre_buf->buf0 + block->y0 * pre_buf->stride + block->x0;
    642  *src_stride = pre_buf->stride;
    643 }
    644 
    645 static inline void dec_calc_subpel_params_and_extend(
    646    const MV *const src_mv, InterPredParams *const inter_pred_params,
    647    MACROBLOCKD *const xd, int mi_x, int mi_y, int ref, uint8_t **mc_buf,
    648    uint8_t **pre, SubpelParams *subpel_params, int *src_stride) {
    649  PadBlock block;
    650  MV32 scaled_mv;
    651  int subpel_x_mv, subpel_y_mv;
    652  dec_calc_subpel_params(src_mv, inter_pred_params, xd, mi_x, mi_y, pre,
    653                         subpel_params, src_stride, &block, &scaled_mv,
    654                         &subpel_x_mv, &subpel_y_mv);
    655  extend_mc_border(
    656      inter_pred_params->scale_factors, &inter_pred_params->ref_frame_buf,
    657      scaled_mv, block, subpel_x_mv, subpel_y_mv,
    658      inter_pred_params->mode == WARP_PRED, inter_pred_params->is_intrabc,
    659      inter_pred_params->use_hbd_buf, mc_buf[ref], pre, src_stride);
    660 }
    661 
    662 #define IS_DEC 1
    663 #include "av1/common/reconinter_template.inc"
    664 #undef IS_DEC
    665 
    666 static void dec_build_inter_predictors(const AV1_COMMON *cm,
    667                                       DecoderCodingBlock *dcb, int plane,
    668                                       const MB_MODE_INFO *mi,
    669                                       int build_for_obmc, int bw, int bh,
    670                                       int mi_x, int mi_y) {
    671  build_inter_predictors(cm, &dcb->xd, plane, mi, build_for_obmc, bw, bh, mi_x,
    672                         mi_y, dcb->mc_buf);
    673 }
    674 
    675 static inline void dec_build_inter_predictor(const AV1_COMMON *cm,
    676                                             DecoderCodingBlock *dcb,
    677                                             int mi_row, int mi_col,
    678                                             BLOCK_SIZE bsize) {
    679  MACROBLOCKD *const xd = &dcb->xd;
    680  const int num_planes = av1_num_planes(cm);
    681  for (int plane = 0; plane < num_planes; ++plane) {
    682    if (plane && !xd->is_chroma_ref) break;
    683    const int mi_x = mi_col * MI_SIZE;
    684    const int mi_y = mi_row * MI_SIZE;
    685    dec_build_inter_predictors(cm, dcb, plane, xd->mi[0], 0,
    686                               xd->plane[plane].width, xd->plane[plane].height,
    687                               mi_x, mi_y);
    688    if (is_interintra_pred(xd->mi[0])) {
    689      BUFFER_SET ctx = { { xd->plane[0].dst.buf, xd->plane[1].dst.buf,
    690                           xd->plane[2].dst.buf },
    691                         { xd->plane[0].dst.stride, xd->plane[1].dst.stride,
    692                           xd->plane[2].dst.stride } };
    693      av1_build_interintra_predictor(cm, xd, xd->plane[plane].dst.buf,
    694                                     xd->plane[plane].dst.stride, &ctx, plane,
    695                                     bsize);
    696    }
    697  }
    698 }
    699 
    700 static inline void dec_build_prediction_by_above_pred(
    701    MACROBLOCKD *const xd, int rel_mi_row, int rel_mi_col, uint8_t op_mi_size,
    702    int dir, MB_MODE_INFO *above_mbmi, void *fun_ctxt, const int num_planes) {
    703  struct build_prediction_ctxt *ctxt = (struct build_prediction_ctxt *)fun_ctxt;
    704  const int above_mi_col = xd->mi_col + rel_mi_col;
    705  int mi_x, mi_y;
    706  MB_MODE_INFO backup_mbmi = *above_mbmi;
    707 
    708  (void)rel_mi_row;
    709  (void)dir;
    710 
    711  av1_setup_build_prediction_by_above_pred(xd, rel_mi_col, op_mi_size,
    712                                           &backup_mbmi, ctxt, num_planes);
    713  mi_x = above_mi_col << MI_SIZE_LOG2;
    714  mi_y = xd->mi_row << MI_SIZE_LOG2;
    715 
    716  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
    717 
    718  for (int j = 0; j < num_planes; ++j) {
    719    const struct macroblockd_plane *pd = &xd->plane[j];
    720    int bw = (op_mi_size * MI_SIZE) >> pd->subsampling_x;
    721    int bh = clamp(block_size_high[bsize] >> (pd->subsampling_y + 1), 4,
    722                   block_size_high[BLOCK_64X64] >> (pd->subsampling_y + 1));
    723 
    724    if (av1_skip_u4x4_pred_in_obmc(bsize, pd, 0)) continue;
    725    dec_build_inter_predictors(ctxt->cm, (DecoderCodingBlock *)ctxt->dcb, j,
    726                               &backup_mbmi, 1, bw, bh, mi_x, mi_y);
    727  }
    728 }
    729 
    730 static inline void dec_build_prediction_by_above_preds(
    731    const AV1_COMMON *cm, DecoderCodingBlock *dcb,
    732    uint8_t *tmp_buf[MAX_MB_PLANE], int tmp_width[MAX_MB_PLANE],
    733    int tmp_height[MAX_MB_PLANE], int tmp_stride[MAX_MB_PLANE]) {
    734  MACROBLOCKD *const xd = &dcb->xd;
    735  if (!xd->up_available) return;
    736 
    737  // Adjust mb_to_bottom_edge to have the correct value for the OBMC
    738  // prediction block. This is half the height of the original block,
    739  // except for 128-wide blocks, where we only use a height of 32.
    740  const int this_height = xd->height * MI_SIZE;
    741  const int pred_height = AOMMIN(this_height / 2, 32);
    742  xd->mb_to_bottom_edge += GET_MV_SUBPEL(this_height - pred_height);
    743  struct build_prediction_ctxt ctxt = {
    744    cm, tmp_buf, tmp_width, tmp_height, tmp_stride, xd->mb_to_right_edge, dcb
    745  };
    746  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
    747  foreach_overlappable_nb_above(cm, xd,
    748                                max_neighbor_obmc[mi_size_wide_log2[bsize]],
    749                                dec_build_prediction_by_above_pred, &ctxt);
    750 
    751  xd->mb_to_left_edge = -GET_MV_SUBPEL(xd->mi_col * MI_SIZE);
    752  xd->mb_to_right_edge = ctxt.mb_to_far_edge;
    753  xd->mb_to_bottom_edge -= GET_MV_SUBPEL(this_height - pred_height);
    754 }
    755 
    756 static inline void dec_build_prediction_by_left_pred(
    757    MACROBLOCKD *const xd, int rel_mi_row, int rel_mi_col, uint8_t op_mi_size,
    758    int dir, MB_MODE_INFO *left_mbmi, void *fun_ctxt, const int num_planes) {
    759  struct build_prediction_ctxt *ctxt = (struct build_prediction_ctxt *)fun_ctxt;
    760  const int left_mi_row = xd->mi_row + rel_mi_row;
    761  int mi_x, mi_y;
    762  MB_MODE_INFO backup_mbmi = *left_mbmi;
    763 
    764  (void)rel_mi_col;
    765  (void)dir;
    766 
    767  av1_setup_build_prediction_by_left_pred(xd, rel_mi_row, op_mi_size,
    768                                          &backup_mbmi, ctxt, num_planes);
    769  mi_x = xd->mi_col << MI_SIZE_LOG2;
    770  mi_y = left_mi_row << MI_SIZE_LOG2;
    771  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
    772 
    773  for (int j = 0; j < num_planes; ++j) {
    774    const struct macroblockd_plane *pd = &xd->plane[j];
    775    int bw = clamp(block_size_wide[bsize] >> (pd->subsampling_x + 1), 4,
    776                   block_size_wide[BLOCK_64X64] >> (pd->subsampling_x + 1));
    777    int bh = (op_mi_size << MI_SIZE_LOG2) >> pd->subsampling_y;
    778 
    779    if (av1_skip_u4x4_pred_in_obmc(bsize, pd, 1)) continue;
    780    dec_build_inter_predictors(ctxt->cm, (DecoderCodingBlock *)ctxt->dcb, j,
    781                               &backup_mbmi, 1, bw, bh, mi_x, mi_y);
    782  }
    783 }
    784 
    785 static inline void dec_build_prediction_by_left_preds(
    786    const AV1_COMMON *cm, DecoderCodingBlock *dcb,
    787    uint8_t *tmp_buf[MAX_MB_PLANE], int tmp_width[MAX_MB_PLANE],
    788    int tmp_height[MAX_MB_PLANE], int tmp_stride[MAX_MB_PLANE]) {
    789  MACROBLOCKD *const xd = &dcb->xd;
    790  if (!xd->left_available) return;
    791 
    792  // Adjust mb_to_right_edge to have the correct value for the OBMC
    793  // prediction block. This is half the width of the original block,
    794  // except for 128-wide blocks, where we only use a width of 32.
    795  const int this_width = xd->width * MI_SIZE;
    796  const int pred_width = AOMMIN(this_width / 2, 32);
    797  xd->mb_to_right_edge += GET_MV_SUBPEL(this_width - pred_width);
    798 
    799  struct build_prediction_ctxt ctxt = {
    800    cm, tmp_buf, tmp_width, tmp_height, tmp_stride, xd->mb_to_bottom_edge, dcb
    801  };
    802  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
    803  foreach_overlappable_nb_left(cm, xd,
    804                               max_neighbor_obmc[mi_size_high_log2[bsize]],
    805                               dec_build_prediction_by_left_pred, &ctxt);
    806 
    807  xd->mb_to_top_edge = -GET_MV_SUBPEL(xd->mi_row * MI_SIZE);
    808  xd->mb_to_right_edge -= GET_MV_SUBPEL(this_width - pred_width);
    809  xd->mb_to_bottom_edge = ctxt.mb_to_far_edge;
    810 }
    811 
    812 static inline void dec_build_obmc_inter_predictors_sb(const AV1_COMMON *cm,
    813                                                      DecoderCodingBlock *dcb) {
    814  const int num_planes = av1_num_planes(cm);
    815  uint8_t *dst_buf1[MAX_MB_PLANE], *dst_buf2[MAX_MB_PLANE];
    816  int dst_stride1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
    817  int dst_stride2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
    818  int dst_width1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
    819  int dst_width2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
    820  int dst_height1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
    821  int dst_height2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
    822 
    823  MACROBLOCKD *const xd = &dcb->xd;
    824  av1_setup_obmc_dst_bufs(xd, dst_buf1, dst_buf2);
    825 
    826  dec_build_prediction_by_above_preds(cm, dcb, dst_buf1, dst_width1,
    827                                      dst_height1, dst_stride1);
    828  dec_build_prediction_by_left_preds(cm, dcb, dst_buf2, dst_width2, dst_height2,
    829                                     dst_stride2);
    830  const int mi_row = xd->mi_row;
    831  const int mi_col = xd->mi_col;
    832  av1_setup_dst_planes(xd->plane, xd->mi[0]->bsize, &cm->cur_frame->buf, mi_row,
    833                       mi_col, 0, num_planes);
    834  av1_build_obmc_inter_prediction(cm, xd, dst_buf1, dst_stride1, dst_buf2,
    835                                  dst_stride2);
    836 }
    837 
    838 static inline void cfl_store_inter_block(AV1_COMMON *const cm,
    839                                         MACROBLOCKD *const xd) {
    840  MB_MODE_INFO *mbmi = xd->mi[0];
    841  if (store_cfl_required(cm, xd)) {
    842    cfl_store_block(xd, mbmi->bsize, mbmi->tx_size);
    843  }
    844 }
    845 
    846 static inline void predict_inter_block(AV1_COMMON *const cm,
    847                                       DecoderCodingBlock *dcb,
    848                                       BLOCK_SIZE bsize) {
    849  MACROBLOCKD *const xd = &dcb->xd;
    850  MB_MODE_INFO *mbmi = xd->mi[0];
    851  const int num_planes = av1_num_planes(cm);
    852  const int mi_row = xd->mi_row;
    853  const int mi_col = xd->mi_col;
    854  for (int ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
    855    const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
    856    if (frame < LAST_FRAME) {
    857      assert(is_intrabc_block(mbmi));
    858      assert(frame == INTRA_FRAME);
    859      assert(ref == 0);
    860    } else {
    861      const RefCntBuffer *ref_buf = get_ref_frame_buf(cm, frame);
    862      const struct scale_factors *ref_scale_factors =
    863          get_ref_scale_factors_const(cm, frame);
    864 
    865      xd->block_ref_scale_factors[ref] = ref_scale_factors;
    866      av1_setup_pre_planes(xd, ref, &ref_buf->buf, mi_row, mi_col,
    867                           ref_scale_factors, num_planes);
    868    }
    869  }
    870 
    871  dec_build_inter_predictor(cm, dcb, mi_row, mi_col, bsize);
    872  if (mbmi->motion_mode == OBMC_CAUSAL) {
    873    dec_build_obmc_inter_predictors_sb(cm, dcb);
    874  }
    875 #if CONFIG_MISMATCH_DEBUG
    876  for (int plane = 0; plane < num_planes; ++plane) {
    877    const struct macroblockd_plane *pd = &xd->plane[plane];
    878    int pixel_c, pixel_r;
    879    mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, 0, 0, pd->subsampling_x,
    880                    pd->subsampling_y);
    881    if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
    882                             pd->subsampling_y))
    883      continue;
    884    mismatch_check_block_pre(pd->dst.buf, pd->dst.stride,
    885                             cm->current_frame.order_hint, plane, pixel_c,
    886                             pixel_r, pd->width, pd->height,
    887                             xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
    888  }
    889 #endif
    890 }
    891 
    892 static inline void set_color_index_map_offset(MACROBLOCKD *const xd, int plane,
    893                                              aom_reader *r) {
    894  (void)r;
    895  Av1ColorMapParam params;
    896  const MB_MODE_INFO *const mbmi = xd->mi[0];
    897  av1_get_block_dimensions(mbmi->bsize, plane, xd, &params.plane_width,
    898                           &params.plane_height, NULL, NULL);
    899  xd->color_index_map_offset[plane] += params.plane_width * params.plane_height;
    900 }
    901 
    902 static inline void decode_token_recon_block(AV1Decoder *const pbi,
    903                                            ThreadData *const td, aom_reader *r,
    904                                            BLOCK_SIZE bsize) {
    905  AV1_COMMON *const cm = &pbi->common;
    906  DecoderCodingBlock *const dcb = &td->dcb;
    907  MACROBLOCKD *const xd = &dcb->xd;
    908  const int num_planes = av1_num_planes(cm);
    909  MB_MODE_INFO *mbmi = xd->mi[0];
    910 
    911  if (!is_inter_block(mbmi)) {
    912    int row, col;
    913    assert(bsize == get_plane_block_size(bsize, xd->plane[0].subsampling_x,
    914                                         xd->plane[0].subsampling_y));
    915    const int max_blocks_wide = max_block_wide(xd, bsize, 0);
    916    const int max_blocks_high = max_block_high(xd, bsize, 0);
    917    const BLOCK_SIZE max_unit_bsize = BLOCK_64X64;
    918    int mu_blocks_wide = mi_size_wide[max_unit_bsize];
    919    int mu_blocks_high = mi_size_high[max_unit_bsize];
    920    mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
    921    mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
    922 
    923    for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
    924      for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
    925        for (int plane = 0; plane < num_planes; ++plane) {
    926          if (plane && !xd->is_chroma_ref) break;
    927          const struct macroblockd_plane *const pd = &xd->plane[plane];
    928          const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
    929          const int stepr = tx_size_high_unit[tx_size];
    930          const int stepc = tx_size_wide_unit[tx_size];
    931 
    932          const int unit_height = ROUND_POWER_OF_TWO(
    933              AOMMIN(mu_blocks_high + row, max_blocks_high), pd->subsampling_y);
    934          const int unit_width = ROUND_POWER_OF_TWO(
    935              AOMMIN(mu_blocks_wide + col, max_blocks_wide), pd->subsampling_x);
    936 
    937          for (int blk_row = row >> pd->subsampling_y; blk_row < unit_height;
    938               blk_row += stepr) {
    939            for (int blk_col = col >> pd->subsampling_x; blk_col < unit_width;
    940                 blk_col += stepc) {
    941              td->read_coeffs_tx_intra_block_visit(cm, dcb, r, plane, blk_row,
    942                                                   blk_col, tx_size);
    943              td->predict_and_recon_intra_block_visit(
    944                  cm, dcb, r, plane, blk_row, blk_col, tx_size);
    945              set_cb_buffer_offsets(dcb, tx_size, plane);
    946            }
    947          }
    948        }
    949      }
    950    }
    951  } else {
    952    td->predict_inter_block_visit(cm, dcb, bsize);
    953    // Reconstruction
    954    if (!mbmi->skip_txfm) {
    955      int eobtotal = 0;
    956 
    957      const int max_blocks_wide = max_block_wide(xd, bsize, 0);
    958      const int max_blocks_high = max_block_high(xd, bsize, 0);
    959      int row, col;
    960 
    961      const BLOCK_SIZE max_unit_bsize = BLOCK_64X64;
    962      assert(max_unit_bsize ==
    963             get_plane_block_size(BLOCK_64X64, xd->plane[0].subsampling_x,
    964                                  xd->plane[0].subsampling_y));
    965      int mu_blocks_wide = mi_size_wide[max_unit_bsize];
    966      int mu_blocks_high = mi_size_high[max_unit_bsize];
    967 
    968      mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
    969      mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
    970 
    971      for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
    972        for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
    973          for (int plane = 0; plane < num_planes; ++plane) {
    974            if (plane && !xd->is_chroma_ref) break;
    975            const struct macroblockd_plane *const pd = &xd->plane[plane];
    976            const int ss_x = pd->subsampling_x;
    977            const int ss_y = pd->subsampling_y;
    978            const BLOCK_SIZE plane_bsize =
    979                get_plane_block_size(bsize, ss_x, ss_y);
    980            const TX_SIZE max_tx_size =
    981                get_vartx_max_txsize(xd, plane_bsize, plane);
    982            const int bh_var_tx = tx_size_high_unit[max_tx_size];
    983            const int bw_var_tx = tx_size_wide_unit[max_tx_size];
    984            int block = 0;
    985            int step =
    986                tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
    987            int blk_row, blk_col;
    988            const int unit_height = ROUND_POWER_OF_TWO(
    989                AOMMIN(mu_blocks_high + row, max_blocks_high), ss_y);
    990            const int unit_width = ROUND_POWER_OF_TWO(
    991                AOMMIN(mu_blocks_wide + col, max_blocks_wide), ss_x);
    992 
    993            for (blk_row = row >> ss_y; blk_row < unit_height;
    994                 blk_row += bh_var_tx) {
    995              for (blk_col = col >> ss_x; blk_col < unit_width;
    996                   blk_col += bw_var_tx) {
    997                decode_reconstruct_tx(cm, td, r, mbmi, plane, plane_bsize,
    998                                      blk_row, blk_col, block, max_tx_size,
    999                                      &eobtotal);
   1000                block += step;
   1001              }
   1002            }
   1003          }
   1004        }
   1005      }
   1006    }
   1007    td->cfl_store_inter_block_visit(cm, xd);
   1008  }
   1009 
   1010  av1_visit_palette(pbi, xd, r, set_color_index_map_offset);
   1011 }
   1012 
   1013 static inline void set_inter_tx_size(MB_MODE_INFO *mbmi, int stride_log2,
   1014                                     int tx_w_log2, int tx_h_log2, int min_txs,
   1015                                     int split_size, int txs, int blk_row,
   1016                                     int blk_col) {
   1017  for (int idy = 0; idy < tx_size_high_unit[split_size];
   1018       idy += tx_size_high_unit[min_txs]) {
   1019    for (int idx = 0; idx < tx_size_wide_unit[split_size];
   1020         idx += tx_size_wide_unit[min_txs]) {
   1021      const int index = (((blk_row + idy) >> tx_h_log2) << stride_log2) +
   1022                        ((blk_col + idx) >> tx_w_log2);
   1023      mbmi->inter_tx_size[index] = txs;
   1024    }
   1025  }
   1026 }
   1027 
   1028 static inline void read_tx_size_vartx(MACROBLOCKD *xd, MB_MODE_INFO *mbmi,
   1029                                      TX_SIZE tx_size, int depth, int blk_row,
   1030                                      int blk_col, aom_reader *r) {
   1031  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
   1032  int is_split = 0;
   1033  const BLOCK_SIZE bsize = mbmi->bsize;
   1034  const int max_blocks_high = max_block_high(xd, bsize, 0);
   1035  const int max_blocks_wide = max_block_wide(xd, bsize, 0);
   1036  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
   1037  assert(tx_size > TX_4X4);
   1038  TX_SIZE txs = max_txsize_rect_lookup[bsize];
   1039  for (int level = 0; level < MAX_VARTX_DEPTH - 1; ++level)
   1040    txs = sub_tx_size_map[txs];
   1041  const int tx_w_log2 = tx_size_wide_log2[txs] - MI_SIZE_LOG2;
   1042  const int tx_h_log2 = tx_size_high_log2[txs] - MI_SIZE_LOG2;
   1043  const int bw_log2 = mi_size_wide_log2[bsize];
   1044  const int stride_log2 = bw_log2 - tx_w_log2;
   1045 
   1046  if (depth == MAX_VARTX_DEPTH) {
   1047    set_inter_tx_size(mbmi, stride_log2, tx_w_log2, tx_h_log2, txs, tx_size,
   1048                      tx_size, blk_row, blk_col);
   1049    mbmi->tx_size = tx_size;
   1050    txfm_partition_update(xd->above_txfm_context + blk_col,
   1051                          xd->left_txfm_context + blk_row, tx_size, tx_size);
   1052    return;
   1053  }
   1054 
   1055  const int ctx = txfm_partition_context(xd->above_txfm_context + blk_col,
   1056                                         xd->left_txfm_context + blk_row,
   1057                                         mbmi->bsize, tx_size);
   1058  is_split = aom_read_symbol(r, ec_ctx->txfm_partition_cdf[ctx], 2, ACCT_STR);
   1059 
   1060  if (is_split) {
   1061    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
   1062    const int bsw = tx_size_wide_unit[sub_txs];
   1063    const int bsh = tx_size_high_unit[sub_txs];
   1064 
   1065    if (sub_txs == TX_4X4) {
   1066      set_inter_tx_size(mbmi, stride_log2, tx_w_log2, tx_h_log2, txs, tx_size,
   1067                        sub_txs, blk_row, blk_col);
   1068      mbmi->tx_size = sub_txs;
   1069      txfm_partition_update(xd->above_txfm_context + blk_col,
   1070                            xd->left_txfm_context + blk_row, sub_txs, tx_size);
   1071      return;
   1072    }
   1073 
   1074    assert(bsw > 0 && bsh > 0);
   1075    for (int row = 0; row < tx_size_high_unit[tx_size]; row += bsh) {
   1076      for (int col = 0; col < tx_size_wide_unit[tx_size]; col += bsw) {
   1077        int offsetr = blk_row + row;
   1078        int offsetc = blk_col + col;
   1079        read_tx_size_vartx(xd, mbmi, sub_txs, depth + 1, offsetr, offsetc, r);
   1080      }
   1081    }
   1082  } else {
   1083    set_inter_tx_size(mbmi, stride_log2, tx_w_log2, tx_h_log2, txs, tx_size,
   1084                      tx_size, blk_row, blk_col);
   1085    mbmi->tx_size = tx_size;
   1086    txfm_partition_update(xd->above_txfm_context + blk_col,
   1087                          xd->left_txfm_context + blk_row, tx_size, tx_size);
   1088  }
   1089 }
   1090 
   1091 static TX_SIZE read_selected_tx_size(const MACROBLOCKD *const xd,
   1092                                     aom_reader *r) {
   1093  // TODO(debargha): Clean up the logic here. This function should only
   1094  // be called for intra.
   1095  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
   1096  const int32_t tx_size_cat = bsize_to_tx_size_cat(bsize);
   1097  const int max_depths = bsize_to_max_depth(bsize);
   1098  const int ctx = get_tx_size_context(xd);
   1099  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
   1100  const int depth = aom_read_symbol(r, ec_ctx->tx_size_cdf[tx_size_cat][ctx],
   1101                                    max_depths + 1, ACCT_STR);
   1102  assert(depth >= 0 && depth <= max_depths);
   1103  const TX_SIZE tx_size = depth_to_tx_size(depth, bsize);
   1104  return tx_size;
   1105 }
   1106 
   1107 static TX_SIZE read_tx_size(const MACROBLOCKD *const xd, TX_MODE tx_mode,
   1108                            int is_inter, int allow_select_inter,
   1109                            aom_reader *r) {
   1110  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
   1111  if (xd->lossless[xd->mi[0]->segment_id]) return TX_4X4;
   1112 
   1113  if (block_signals_txsize(bsize)) {
   1114    if ((!is_inter || allow_select_inter) && tx_mode == TX_MODE_SELECT) {
   1115      const TX_SIZE coded_tx_size = read_selected_tx_size(xd, r);
   1116      return coded_tx_size;
   1117    } else {
   1118      return tx_size_from_tx_mode(bsize, tx_mode);
   1119    }
   1120  } else {
   1121    assert(IMPLIES(tx_mode == ONLY_4X4, bsize == BLOCK_4X4));
   1122    return max_txsize_rect_lookup[bsize];
   1123  }
   1124 }
   1125 
   1126 static inline void parse_decode_block(AV1Decoder *const pbi,
   1127                                      ThreadData *const td, int mi_row,
   1128                                      int mi_col, aom_reader *r,
   1129                                      PARTITION_TYPE partition,
   1130                                      BLOCK_SIZE bsize) {
   1131  DecoderCodingBlock *const dcb = &td->dcb;
   1132  MACROBLOCKD *const xd = &dcb->xd;
   1133  decode_mbmi_block(pbi, dcb, mi_row, mi_col, r, partition, bsize);
   1134 
   1135  av1_visit_palette(pbi, xd, r, av1_decode_palette_tokens);
   1136 
   1137  AV1_COMMON *cm = &pbi->common;
   1138  const int num_planes = av1_num_planes(cm);
   1139  MB_MODE_INFO *mbmi = xd->mi[0];
   1140  int inter_block_tx = is_inter_block(mbmi) || is_intrabc_block(mbmi);
   1141  if (cm->features.tx_mode == TX_MODE_SELECT && block_signals_txsize(bsize) &&
   1142      !mbmi->skip_txfm && inter_block_tx && !xd->lossless[mbmi->segment_id]) {
   1143    const TX_SIZE max_tx_size = max_txsize_rect_lookup[bsize];
   1144    const int bh = tx_size_high_unit[max_tx_size];
   1145    const int bw = tx_size_wide_unit[max_tx_size];
   1146    const int width = mi_size_wide[bsize];
   1147    const int height = mi_size_high[bsize];
   1148 
   1149    for (int idy = 0; idy < height; idy += bh)
   1150      for (int idx = 0; idx < width; idx += bw)
   1151        read_tx_size_vartx(xd, mbmi, max_tx_size, 0, idy, idx, r);
   1152  } else {
   1153    mbmi->tx_size = read_tx_size(xd, cm->features.tx_mode, inter_block_tx,
   1154                                 !mbmi->skip_txfm, r);
   1155    if (inter_block_tx)
   1156      memset(mbmi->inter_tx_size, mbmi->tx_size, sizeof(mbmi->inter_tx_size));
   1157    set_txfm_ctxs(mbmi->tx_size, xd->width, xd->height,
   1158                  mbmi->skip_txfm && is_inter_block(mbmi), xd);
   1159  }
   1160 
   1161  if (cm->delta_q_info.delta_q_present_flag) {
   1162    for (int i = 0; i < MAX_SEGMENTS; i++) {
   1163      const int current_qindex =
   1164          av1_get_qindex(&cm->seg, i, xd->current_base_qindex);
   1165      const CommonQuantParams *const quant_params = &cm->quant_params;
   1166      for (int j = 0; j < num_planes; ++j) {
   1167        const int dc_delta_q = j == 0 ? quant_params->y_dc_delta_q
   1168                                      : (j == 1 ? quant_params->u_dc_delta_q
   1169                                                : quant_params->v_dc_delta_q);
   1170        const int ac_delta_q = j == 0 ? 0
   1171                                      : (j == 1 ? quant_params->u_ac_delta_q
   1172                                                : quant_params->v_ac_delta_q);
   1173        xd->plane[j].seg_dequant_QTX[i][0] = av1_dc_quant_QTX(
   1174            current_qindex, dc_delta_q, cm->seq_params->bit_depth);
   1175        xd->plane[j].seg_dequant_QTX[i][1] = av1_ac_quant_QTX(
   1176            current_qindex, ac_delta_q, cm->seq_params->bit_depth);
   1177      }
   1178    }
   1179  }
   1180  if (mbmi->skip_txfm) av1_reset_entropy_context(xd, bsize, num_planes);
   1181 
   1182  decode_token_recon_block(pbi, td, r, bsize);
   1183 }
   1184 
   1185 static inline void set_offsets_for_pred_and_recon(AV1Decoder *const pbi,
   1186                                                  ThreadData *const td,
   1187                                                  int mi_row, int mi_col,
   1188                                                  BLOCK_SIZE bsize) {
   1189  AV1_COMMON *const cm = &pbi->common;
   1190  const CommonModeInfoParams *const mi_params = &cm->mi_params;
   1191  DecoderCodingBlock *const dcb = &td->dcb;
   1192  MACROBLOCKD *const xd = &dcb->xd;
   1193  const int bw = mi_size_wide[bsize];
   1194  const int bh = mi_size_high[bsize];
   1195  const int num_planes = av1_num_planes(cm);
   1196 
   1197  const int offset = mi_row * mi_params->mi_stride + mi_col;
   1198  const TileInfo *const tile = &xd->tile;
   1199 
   1200  xd->mi = mi_params->mi_grid_base + offset;
   1201  xd->tx_type_map =
   1202      &mi_params->tx_type_map[mi_row * mi_params->mi_stride + mi_col];
   1203  xd->tx_type_map_stride = mi_params->mi_stride;
   1204 
   1205  set_plane_n4(xd, bw, bh, num_planes);
   1206 
   1207  // Distance of Mb to the various image edges. These are specified to 8th pel
   1208  // as they are always compared to values that are in 1/8th pel units
   1209  set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, mi_params->mi_rows,
   1210                 mi_params->mi_cols);
   1211 
   1212  av1_setup_dst_planes(xd->plane, bsize, &cm->cur_frame->buf, mi_row, mi_col, 0,
   1213                       num_planes);
   1214 }
   1215 
   1216 static inline void decode_block(AV1Decoder *const pbi, ThreadData *const td,
   1217                                int mi_row, int mi_col, aom_reader *r,
   1218                                PARTITION_TYPE partition, BLOCK_SIZE bsize) {
   1219  (void)partition;
   1220  set_offsets_for_pred_and_recon(pbi, td, mi_row, mi_col, bsize);
   1221  decode_token_recon_block(pbi, td, r, bsize);
   1222 }
   1223 
   1224 static PARTITION_TYPE read_partition(MACROBLOCKD *xd, int mi_row, int mi_col,
   1225                                     aom_reader *r, int has_rows, int has_cols,
   1226                                     BLOCK_SIZE bsize) {
   1227  const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
   1228  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
   1229 
   1230  if (!has_rows && !has_cols) return PARTITION_SPLIT;
   1231 
   1232  assert(ctx >= 0);
   1233  aom_cdf_prob *partition_cdf = ec_ctx->partition_cdf[ctx];
   1234  if (has_rows && has_cols) {
   1235    return (PARTITION_TYPE)aom_read_symbol(
   1236        r, partition_cdf, partition_cdf_length(bsize), ACCT_STR);
   1237  } else if (!has_rows && has_cols) {
   1238    assert(bsize > BLOCK_8X8);
   1239    aom_cdf_prob cdf[2];
   1240    partition_gather_vert_alike(cdf, partition_cdf, bsize);
   1241    assert(cdf[1] == AOM_ICDF(CDF_PROB_TOP));
   1242    return aom_read_cdf(r, cdf, 2, ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
   1243  } else {
   1244    assert(has_rows && !has_cols);
   1245    assert(bsize > BLOCK_8X8);
   1246    aom_cdf_prob cdf[2];
   1247    partition_gather_horz_alike(cdf, partition_cdf, bsize);
   1248    assert(cdf[1] == AOM_ICDF(CDF_PROB_TOP));
   1249    return aom_read_cdf(r, cdf, 2, ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
   1250  }
   1251 }
   1252 
   1253 // TODO(slavarnway): eliminate bsize and subsize in future commits
   1254 static inline void decode_partition(AV1Decoder *const pbi, ThreadData *const td,
   1255                                    int mi_row, int mi_col, aom_reader *reader,
   1256                                    BLOCK_SIZE bsize, int parse_decode_flag) {
   1257  assert(bsize < BLOCK_SIZES_ALL);
   1258  AV1_COMMON *const cm = &pbi->common;
   1259  DecoderCodingBlock *const dcb = &td->dcb;
   1260  MACROBLOCKD *const xd = &dcb->xd;
   1261  const int bw = mi_size_wide[bsize];
   1262  const int hbs = bw >> 1;
   1263  PARTITION_TYPE partition;
   1264  BLOCK_SIZE subsize;
   1265  const int quarter_step = bw / 4;
   1266  BLOCK_SIZE bsize2 = get_partition_subsize(bsize, PARTITION_SPLIT);
   1267  const int has_rows = (mi_row + hbs) < cm->mi_params.mi_rows;
   1268  const int has_cols = (mi_col + hbs) < cm->mi_params.mi_cols;
   1269 
   1270  if (mi_row >= cm->mi_params.mi_rows || mi_col >= cm->mi_params.mi_cols)
   1271    return;
   1272 
   1273  // parse_decode_flag takes the following values :
   1274  // 01 - do parse only
   1275  // 10 - do decode only
   1276  // 11 - do parse and decode
   1277  static const block_visitor_fn_t block_visit[4] = { NULL, parse_decode_block,
   1278                                                     decode_block,
   1279                                                     parse_decode_block };
   1280 
   1281  if (parse_decode_flag & 1) {
   1282    const int num_planes = av1_num_planes(cm);
   1283    for (int plane = 0; plane < num_planes; ++plane) {
   1284      int rcol0, rcol1, rrow0, rrow1;
   1285 
   1286      // Skip some unnecessary work if loop restoration is disabled
   1287      if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
   1288 
   1289      if (av1_loop_restoration_corners_in_sb(cm, plane, mi_row, mi_col, bsize,
   1290                                             &rcol0, &rcol1, &rrow0, &rrow1)) {
   1291        const int rstride = cm->rst_info[plane].horz_units;
   1292        for (int rrow = rrow0; rrow < rrow1; ++rrow) {
   1293          for (int rcol = rcol0; rcol < rcol1; ++rcol) {
   1294            const int runit_idx = rcol + rrow * rstride;
   1295            loop_restoration_read_sb_coeffs(cm, xd, reader, plane, runit_idx);
   1296          }
   1297        }
   1298      }
   1299    }
   1300 
   1301    partition = (bsize < BLOCK_8X8) ? PARTITION_NONE
   1302                                    : read_partition(xd, mi_row, mi_col, reader,
   1303                                                     has_rows, has_cols, bsize);
   1304  } else {
   1305    partition = get_partition(cm, mi_row, mi_col, bsize);
   1306  }
   1307  subsize = get_partition_subsize(bsize, partition);
   1308  if (subsize == BLOCK_INVALID) {
   1309    // When an internal error occurs ensure that xd->mi_row is set appropriately
   1310    // w.r.t. current tile, which is used to signal processing of current row is
   1311    // done.
   1312    xd->mi_row = mi_row;
   1313    aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
   1314                       "Partition is invalid for block size %dx%d",
   1315                       block_size_wide[bsize], block_size_high[bsize]);
   1316  }
   1317  // Check the bitstream is conformant: if there is subsampling on the
   1318  // chroma planes, subsize must subsample to a valid block size.
   1319  const struct macroblockd_plane *const pd_u = &xd->plane[1];
   1320  if (get_plane_block_size(subsize, pd_u->subsampling_x, pd_u->subsampling_y) ==
   1321      BLOCK_INVALID) {
   1322    // When an internal error occurs ensure that xd->mi_row is set appropriately
   1323    // w.r.t. current tile, which is used to signal processing of current row is
   1324    // done.
   1325    xd->mi_row = mi_row;
   1326    aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
   1327                       "Block size %dx%d invalid with this subsampling mode",
   1328                       block_size_wide[subsize], block_size_high[subsize]);
   1329  }
   1330 
   1331 #define DEC_BLOCK_STX_ARG
   1332 #define DEC_BLOCK_EPT_ARG partition,
   1333 #define DEC_BLOCK(db_r, db_c, db_subsize)                                  \
   1334  block_visit[parse_decode_flag](pbi, td, DEC_BLOCK_STX_ARG(db_r), (db_c), \
   1335                                 reader, DEC_BLOCK_EPT_ARG(db_subsize))
   1336 #define DEC_PARTITION(db_r, db_c, db_subsize)                        \
   1337  decode_partition(pbi, td, DEC_BLOCK_STX_ARG(db_r), (db_c), reader, \
   1338                   (db_subsize), parse_decode_flag)
   1339 
   1340  switch (partition) {
   1341    case PARTITION_NONE: DEC_BLOCK(mi_row, mi_col, subsize); break;
   1342    case PARTITION_HORZ:
   1343      DEC_BLOCK(mi_row, mi_col, subsize);
   1344      if (has_rows) DEC_BLOCK(mi_row + hbs, mi_col, subsize);
   1345      break;
   1346    case PARTITION_VERT:
   1347      DEC_BLOCK(mi_row, mi_col, subsize);
   1348      if (has_cols) DEC_BLOCK(mi_row, mi_col + hbs, subsize);
   1349      break;
   1350    case PARTITION_SPLIT:
   1351      DEC_PARTITION(mi_row, mi_col, subsize);
   1352      DEC_PARTITION(mi_row, mi_col + hbs, subsize);
   1353      DEC_PARTITION(mi_row + hbs, mi_col, subsize);
   1354      DEC_PARTITION(mi_row + hbs, mi_col + hbs, subsize);
   1355      break;
   1356    case PARTITION_HORZ_A:
   1357      DEC_BLOCK(mi_row, mi_col, bsize2);
   1358      DEC_BLOCK(mi_row, mi_col + hbs, bsize2);
   1359      DEC_BLOCK(mi_row + hbs, mi_col, subsize);
   1360      break;
   1361    case PARTITION_HORZ_B:
   1362      DEC_BLOCK(mi_row, mi_col, subsize);
   1363      DEC_BLOCK(mi_row + hbs, mi_col, bsize2);
   1364      DEC_BLOCK(mi_row + hbs, mi_col + hbs, bsize2);
   1365      break;
   1366    case PARTITION_VERT_A:
   1367      DEC_BLOCK(mi_row, mi_col, bsize2);
   1368      DEC_BLOCK(mi_row + hbs, mi_col, bsize2);
   1369      DEC_BLOCK(mi_row, mi_col + hbs, subsize);
   1370      break;
   1371    case PARTITION_VERT_B:
   1372      DEC_BLOCK(mi_row, mi_col, subsize);
   1373      DEC_BLOCK(mi_row, mi_col + hbs, bsize2);
   1374      DEC_BLOCK(mi_row + hbs, mi_col + hbs, bsize2);
   1375      break;
   1376    case PARTITION_HORZ_4:
   1377      for (int i = 0; i < 4; ++i) {
   1378        int this_mi_row = mi_row + i * quarter_step;
   1379        if (i > 0 && this_mi_row >= cm->mi_params.mi_rows) break;
   1380        DEC_BLOCK(this_mi_row, mi_col, subsize);
   1381      }
   1382      break;
   1383    case PARTITION_VERT_4:
   1384      for (int i = 0; i < 4; ++i) {
   1385        int this_mi_col = mi_col + i * quarter_step;
   1386        if (i > 0 && this_mi_col >= cm->mi_params.mi_cols) break;
   1387        DEC_BLOCK(mi_row, this_mi_col, subsize);
   1388      }
   1389      break;
   1390    default: assert(0 && "Invalid partition type");
   1391  }
   1392 
   1393 #undef DEC_PARTITION
   1394 #undef DEC_BLOCK
   1395 #undef DEC_BLOCK_EPT_ARG
   1396 #undef DEC_BLOCK_STX_ARG
   1397 
   1398  if (parse_decode_flag & 1)
   1399    update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition);
   1400 }
   1401 
   1402 static inline void setup_bool_decoder(
   1403    MACROBLOCKD *const xd, const uint8_t *data, const uint8_t *data_end,
   1404    const size_t read_size, struct aom_internal_error_info *error_info,
   1405    aom_reader *r, uint8_t allow_update_cdf) {
   1406  // Validate the calculated partition length. If the buffer
   1407  // described by the partition can't be fully read, then restrict
   1408  // it to the portion that can be (for EC mode) or throw an error.
   1409  if (!read_is_valid(data, read_size, data_end)) {
   1410    // When internal error occurs ensure that xd->mi_row is set appropriately
   1411    // w.r.t. current tile, which is used to signal processing of current row is
   1412    // done in row-mt decoding.
   1413    xd->mi_row = xd->tile.mi_row_start;
   1414 
   1415    aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
   1416                       "Truncated packet or corrupt tile length");
   1417  }
   1418  if (aom_reader_init(r, data, read_size)) {
   1419    // When internal error occurs ensure that xd->mi_row is set appropriately
   1420    // w.r.t. current tile, which is used to signal processing of current row is
   1421    // done in row-mt decoding.
   1422    xd->mi_row = xd->tile.mi_row_start;
   1423 
   1424    aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
   1425                       "Failed to allocate bool decoder %d", 1);
   1426  }
   1427 
   1428  r->allow_update_cdf = allow_update_cdf;
   1429 }
   1430 
   1431 static inline void setup_segmentation(AV1_COMMON *const cm,
   1432                                      struct aom_read_bit_buffer *rb) {
   1433  struct segmentation *const seg = &cm->seg;
   1434 
   1435  seg->update_map = 0;
   1436  seg->update_data = 0;
   1437  seg->temporal_update = 0;
   1438 
   1439  seg->enabled = aom_rb_read_bit(rb);
   1440  if (!seg->enabled) {
   1441    if (cm->cur_frame->seg_map) {
   1442      memset(cm->cur_frame->seg_map, 0,
   1443             (cm->cur_frame->mi_rows * cm->cur_frame->mi_cols));
   1444    }
   1445 
   1446    memset(seg, 0, sizeof(*seg));
   1447    segfeatures_copy(&cm->cur_frame->seg, seg);
   1448    return;
   1449  }
   1450  if (cm->seg.enabled && cm->prev_frame &&
   1451      (cm->mi_params.mi_rows == cm->prev_frame->mi_rows) &&
   1452      (cm->mi_params.mi_cols == cm->prev_frame->mi_cols)) {
   1453    cm->last_frame_seg_map = cm->prev_frame->seg_map;
   1454  } else {
   1455    cm->last_frame_seg_map = NULL;
   1456  }
   1457  // Read update flags
   1458  if (cm->features.primary_ref_frame == PRIMARY_REF_NONE) {
   1459    // These frames can't use previous frames, so must signal map + features
   1460    seg->update_map = 1;
   1461    seg->temporal_update = 0;
   1462    seg->update_data = 1;
   1463  } else {
   1464    seg->update_map = aom_rb_read_bit(rb);
   1465    if (seg->update_map) {
   1466      seg->temporal_update = aom_rb_read_bit(rb);
   1467    } else {
   1468      seg->temporal_update = 0;
   1469    }
   1470    seg->update_data = aom_rb_read_bit(rb);
   1471  }
   1472 
   1473  // Segmentation data update
   1474  if (seg->update_data) {
   1475    av1_clearall_segfeatures(seg);
   1476 
   1477    for (int i = 0; i < MAX_SEGMENTS; i++) {
   1478      for (int j = 0; j < SEG_LVL_MAX; j++) {
   1479        int data = 0;
   1480        const int feature_enabled = aom_rb_read_bit(rb);
   1481        if (feature_enabled) {
   1482          av1_enable_segfeature(seg, i, j);
   1483 
   1484          const int data_max = av1_seg_feature_data_max(j);
   1485          const int data_min = -data_max;
   1486          const int ubits = get_unsigned_bits(data_max);
   1487 
   1488          if (av1_is_segfeature_signed(j)) {
   1489            data = aom_rb_read_inv_signed_literal(rb, ubits);
   1490          } else {
   1491            data = aom_rb_read_literal(rb, ubits);
   1492          }
   1493 
   1494          data = clamp(data, data_min, data_max);
   1495        }
   1496        av1_set_segdata(seg, i, j, data);
   1497      }
   1498    }
   1499    av1_calculate_segdata(seg);
   1500  } else if (cm->prev_frame) {
   1501    segfeatures_copy(seg, &cm->prev_frame->seg);
   1502  }
   1503  segfeatures_copy(&cm->cur_frame->seg, seg);
   1504 }
   1505 
   1506 static inline void decode_restoration_mode(AV1_COMMON *cm,
   1507                                           struct aom_read_bit_buffer *rb) {
   1508  assert(!cm->features.all_lossless);
   1509  const int num_planes = av1_num_planes(cm);
   1510  if (cm->features.allow_intrabc) return;
   1511  int all_none = 1, chroma_none = 1;
   1512  for (int p = 0; p < num_planes; ++p) {
   1513    RestorationInfo *rsi = &cm->rst_info[p];
   1514    if (aom_rb_read_bit(rb)) {
   1515      rsi->frame_restoration_type =
   1516          aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
   1517    } else {
   1518      rsi->frame_restoration_type =
   1519          aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
   1520    }
   1521    if (rsi->frame_restoration_type != RESTORE_NONE) {
   1522      all_none = 0;
   1523      chroma_none &= p == 0;
   1524    }
   1525  }
   1526  if (!all_none) {
   1527    assert(cm->seq_params->sb_size == BLOCK_64X64 ||
   1528           cm->seq_params->sb_size == BLOCK_128X128);
   1529    const int sb_size = cm->seq_params->sb_size == BLOCK_128X128 ? 128 : 64;
   1530 
   1531    for (int p = 0; p < num_planes; ++p)
   1532      cm->rst_info[p].restoration_unit_size = sb_size;
   1533 
   1534    RestorationInfo *rsi = &cm->rst_info[0];
   1535 
   1536    if (sb_size == 64) {
   1537      rsi->restoration_unit_size <<= aom_rb_read_bit(rb);
   1538    }
   1539    if (rsi->restoration_unit_size > 64) {
   1540      rsi->restoration_unit_size <<= aom_rb_read_bit(rb);
   1541    }
   1542  } else {
   1543    const int size = RESTORATION_UNITSIZE_MAX;
   1544    for (int p = 0; p < num_planes; ++p)
   1545      cm->rst_info[p].restoration_unit_size = size;
   1546  }
   1547 
   1548  if (num_planes > 1) {
   1549    int s =
   1550        AOMMIN(cm->seq_params->subsampling_x, cm->seq_params->subsampling_y);
   1551    if (s && !chroma_none) {
   1552      cm->rst_info[1].restoration_unit_size =
   1553          cm->rst_info[0].restoration_unit_size >> (aom_rb_read_bit(rb) * s);
   1554    } else {
   1555      cm->rst_info[1].restoration_unit_size =
   1556          cm->rst_info[0].restoration_unit_size;
   1557    }
   1558    cm->rst_info[2].restoration_unit_size =
   1559        cm->rst_info[1].restoration_unit_size;
   1560  }
   1561 }
   1562 
   1563 static inline void read_wiener_filter(int wiener_win, WienerInfo *wiener_info,
   1564                                      WienerInfo *ref_wiener_info,
   1565                                      aom_reader *rb) {
   1566  memset(wiener_info->vfilter, 0, sizeof(wiener_info->vfilter));
   1567  memset(wiener_info->hfilter, 0, sizeof(wiener_info->hfilter));
   1568 
   1569  if (wiener_win == WIENER_WIN)
   1570    wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
   1571        aom_read_primitive_refsubexpfin(
   1572            rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
   1573            WIENER_FILT_TAP0_SUBEXP_K,
   1574            ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV, ACCT_STR) +
   1575        WIENER_FILT_TAP0_MINV;
   1576  else
   1577    wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] = 0;
   1578  wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
   1579      aom_read_primitive_refsubexpfin(
   1580          rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
   1581          WIENER_FILT_TAP1_SUBEXP_K,
   1582          ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV, ACCT_STR) +
   1583      WIENER_FILT_TAP1_MINV;
   1584  wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
   1585      aom_read_primitive_refsubexpfin(
   1586          rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
   1587          WIENER_FILT_TAP2_SUBEXP_K,
   1588          ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV, ACCT_STR) +
   1589      WIENER_FILT_TAP2_MINV;
   1590  // The central element has an implicit +WIENER_FILT_STEP
   1591  wiener_info->vfilter[WIENER_HALFWIN] =
   1592      -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
   1593            wiener_info->vfilter[2]);
   1594 
   1595  if (wiener_win == WIENER_WIN)
   1596    wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
   1597        aom_read_primitive_refsubexpfin(
   1598            rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
   1599            WIENER_FILT_TAP0_SUBEXP_K,
   1600            ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV, ACCT_STR) +
   1601        WIENER_FILT_TAP0_MINV;
   1602  else
   1603    wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] = 0;
   1604  wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
   1605      aom_read_primitive_refsubexpfin(
   1606          rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
   1607          WIENER_FILT_TAP1_SUBEXP_K,
   1608          ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV, ACCT_STR) +
   1609      WIENER_FILT_TAP1_MINV;
   1610  wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
   1611      aom_read_primitive_refsubexpfin(
   1612          rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
   1613          WIENER_FILT_TAP2_SUBEXP_K,
   1614          ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV, ACCT_STR) +
   1615      WIENER_FILT_TAP2_MINV;
   1616  // The central element has an implicit +WIENER_FILT_STEP
   1617  wiener_info->hfilter[WIENER_HALFWIN] =
   1618      -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
   1619            wiener_info->hfilter[2]);
   1620  *ref_wiener_info = *wiener_info;
   1621 }
   1622 
   1623 static inline void read_sgrproj_filter(SgrprojInfo *sgrproj_info,
   1624                                       SgrprojInfo *ref_sgrproj_info,
   1625                                       aom_reader *rb) {
   1626  sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
   1627  const sgr_params_type *params = &av1_sgr_params[sgrproj_info->ep];
   1628 
   1629  if (params->r[0] == 0) {
   1630    sgrproj_info->xqd[0] = 0;
   1631    sgrproj_info->xqd[1] =
   1632        aom_read_primitive_refsubexpfin(
   1633            rb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
   1634            ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1, ACCT_STR) +
   1635        SGRPROJ_PRJ_MIN1;
   1636  } else if (params->r[1] == 0) {
   1637    sgrproj_info->xqd[0] =
   1638        aom_read_primitive_refsubexpfin(
   1639            rb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
   1640            ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0, ACCT_STR) +
   1641        SGRPROJ_PRJ_MIN0;
   1642    sgrproj_info->xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - sgrproj_info->xqd[0],
   1643                                 SGRPROJ_PRJ_MIN1, SGRPROJ_PRJ_MAX1);
   1644  } else {
   1645    sgrproj_info->xqd[0] =
   1646        aom_read_primitive_refsubexpfin(
   1647            rb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
   1648            ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0, ACCT_STR) +
   1649        SGRPROJ_PRJ_MIN0;
   1650    sgrproj_info->xqd[1] =
   1651        aom_read_primitive_refsubexpfin(
   1652            rb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
   1653            ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1, ACCT_STR) +
   1654        SGRPROJ_PRJ_MIN1;
   1655  }
   1656 
   1657  *ref_sgrproj_info = *sgrproj_info;
   1658 }
   1659 
   1660 static inline void loop_restoration_read_sb_coeffs(const AV1_COMMON *const cm,
   1661                                                   MACROBLOCKD *xd,
   1662                                                   aom_reader *const r,
   1663                                                   int plane, int runit_idx) {
   1664  const RestorationInfo *rsi = &cm->rst_info[plane];
   1665  RestorationUnitInfo *rui = &rsi->unit_info[runit_idx];
   1666  assert(rsi->frame_restoration_type != RESTORE_NONE);
   1667 
   1668  assert(!cm->features.all_lossless);
   1669 
   1670  const int wiener_win = (plane > 0) ? WIENER_WIN_CHROMA : WIENER_WIN;
   1671  WienerInfo *wiener_info = xd->wiener_info + plane;
   1672  SgrprojInfo *sgrproj_info = xd->sgrproj_info + plane;
   1673 
   1674  if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
   1675    rui->restoration_type =
   1676        aom_read_symbol(r, xd->tile_ctx->switchable_restore_cdf,
   1677                        RESTORE_SWITCHABLE_TYPES, ACCT_STR);
   1678    switch (rui->restoration_type) {
   1679      case RESTORE_WIENER:
   1680        read_wiener_filter(wiener_win, &rui->wiener_info, wiener_info, r);
   1681        break;
   1682      case RESTORE_SGRPROJ:
   1683        read_sgrproj_filter(&rui->sgrproj_info, sgrproj_info, r);
   1684        break;
   1685      default: assert(rui->restoration_type == RESTORE_NONE); break;
   1686    }
   1687  } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
   1688    if (aom_read_symbol(r, xd->tile_ctx->wiener_restore_cdf, 2, ACCT_STR)) {
   1689      rui->restoration_type = RESTORE_WIENER;
   1690      read_wiener_filter(wiener_win, &rui->wiener_info, wiener_info, r);
   1691    } else {
   1692      rui->restoration_type = RESTORE_NONE;
   1693    }
   1694  } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
   1695    if (aom_read_symbol(r, xd->tile_ctx->sgrproj_restore_cdf, 2, ACCT_STR)) {
   1696      rui->restoration_type = RESTORE_SGRPROJ;
   1697      read_sgrproj_filter(&rui->sgrproj_info, sgrproj_info, r);
   1698    } else {
   1699      rui->restoration_type = RESTORE_NONE;
   1700    }
   1701  }
   1702 }
   1703 
   1704 static inline void setup_loopfilter(AV1_COMMON *cm,
   1705                                    struct aom_read_bit_buffer *rb) {
   1706  const int num_planes = av1_num_planes(cm);
   1707  struct loopfilter *lf = &cm->lf;
   1708 
   1709  if (cm->features.allow_intrabc || cm->features.coded_lossless) {
   1710    // write default deltas to frame buffer
   1711    av1_set_default_ref_deltas(cm->cur_frame->ref_deltas);
   1712    av1_set_default_mode_deltas(cm->cur_frame->mode_deltas);
   1713    return;
   1714  }
   1715  assert(!cm->features.coded_lossless);
   1716  if (cm->prev_frame) {
   1717    // write deltas to frame buffer
   1718    memcpy(lf->ref_deltas, cm->prev_frame->ref_deltas, REF_FRAMES);
   1719    memcpy(lf->mode_deltas, cm->prev_frame->mode_deltas, MAX_MODE_LF_DELTAS);
   1720  } else {
   1721    av1_set_default_ref_deltas(lf->ref_deltas);
   1722    av1_set_default_mode_deltas(lf->mode_deltas);
   1723  }
   1724  lf->filter_level[0] = aom_rb_read_literal(rb, 6);
   1725  lf->filter_level[1] = aom_rb_read_literal(rb, 6);
   1726  if (num_planes > 1) {
   1727    if (lf->filter_level[0] || lf->filter_level[1]) {
   1728      lf->filter_level_u = aom_rb_read_literal(rb, 6);
   1729      lf->filter_level_v = aom_rb_read_literal(rb, 6);
   1730    }
   1731  }
   1732  lf->sharpness_level = aom_rb_read_literal(rb, 3);
   1733 
   1734  // Read in loop filter deltas applied at the MB level based on mode or ref
   1735  // frame.
   1736  lf->mode_ref_delta_update = 0;
   1737 
   1738  lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
   1739  if (lf->mode_ref_delta_enabled) {
   1740    lf->mode_ref_delta_update = aom_rb_read_bit(rb);
   1741    if (lf->mode_ref_delta_update) {
   1742      for (int i = 0; i < REF_FRAMES; i++)
   1743        if (aom_rb_read_bit(rb))
   1744          lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
   1745 
   1746      for (int i = 0; i < MAX_MODE_LF_DELTAS; i++)
   1747        if (aom_rb_read_bit(rb))
   1748          lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
   1749    }
   1750  }
   1751 
   1752  // write deltas to frame buffer
   1753  memcpy(cm->cur_frame->ref_deltas, lf->ref_deltas, REF_FRAMES);
   1754  memcpy(cm->cur_frame->mode_deltas, lf->mode_deltas, MAX_MODE_LF_DELTAS);
   1755 }
   1756 
   1757 static inline void setup_cdef(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
   1758  const int num_planes = av1_num_planes(cm);
   1759  CdefInfo *const cdef_info = &cm->cdef_info;
   1760 
   1761  if (cm->features.allow_intrabc) return;
   1762  cdef_info->cdef_damping = aom_rb_read_literal(rb, 2) + 3;
   1763  cdef_info->cdef_bits = aom_rb_read_literal(rb, 2);
   1764  cdef_info->nb_cdef_strengths = 1 << cdef_info->cdef_bits;
   1765  for (int i = 0; i < cdef_info->nb_cdef_strengths; i++) {
   1766    cdef_info->cdef_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
   1767    cdef_info->cdef_uv_strengths[i] =
   1768        num_planes > 1 ? aom_rb_read_literal(rb, CDEF_STRENGTH_BITS) : 0;
   1769  }
   1770 }
   1771 
   1772 static inline int read_delta_q(struct aom_read_bit_buffer *rb) {
   1773  return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
   1774 }
   1775 
   1776 static inline void setup_quantization(CommonQuantParams *quant_params,
   1777                                      int num_planes, bool separate_uv_delta_q,
   1778                                      struct aom_read_bit_buffer *rb) {
   1779  quant_params->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
   1780  quant_params->y_dc_delta_q = read_delta_q(rb);
   1781  if (num_planes > 1) {
   1782    int diff_uv_delta = 0;
   1783    if (separate_uv_delta_q) diff_uv_delta = aom_rb_read_bit(rb);
   1784    quant_params->u_dc_delta_q = read_delta_q(rb);
   1785    quant_params->u_ac_delta_q = read_delta_q(rb);
   1786    if (diff_uv_delta) {
   1787      quant_params->v_dc_delta_q = read_delta_q(rb);
   1788      quant_params->v_ac_delta_q = read_delta_q(rb);
   1789    } else {
   1790      quant_params->v_dc_delta_q = quant_params->u_dc_delta_q;
   1791      quant_params->v_ac_delta_q = quant_params->u_ac_delta_q;
   1792    }
   1793  } else {
   1794    quant_params->u_dc_delta_q = 0;
   1795    quant_params->u_ac_delta_q = 0;
   1796    quant_params->v_dc_delta_q = 0;
   1797    quant_params->v_ac_delta_q = 0;
   1798  }
   1799  quant_params->using_qmatrix = aom_rb_read_bit(rb);
   1800  if (quant_params->using_qmatrix) {
   1801    quant_params->qmatrix_level_y = aom_rb_read_literal(rb, QM_LEVEL_BITS);
   1802    quant_params->qmatrix_level_u = aom_rb_read_literal(rb, QM_LEVEL_BITS);
   1803    if (!separate_uv_delta_q)
   1804      quant_params->qmatrix_level_v = quant_params->qmatrix_level_u;
   1805    else
   1806      quant_params->qmatrix_level_v = aom_rb_read_literal(rb, QM_LEVEL_BITS);
   1807  } else {
   1808    quant_params->qmatrix_level_y = 0;
   1809    quant_params->qmatrix_level_u = 0;
   1810    quant_params->qmatrix_level_v = 0;
   1811  }
   1812 }
   1813 
   1814 // Get global dequant matrix.
   1815 static const qm_val_t *get_iqmatrix(const CommonQuantParams *quant_params,
   1816                                    int qmlevel, int plane, TX_SIZE tx_size) {
   1817  assert(quant_params->giqmatrix[qmlevel][plane][tx_size] != NULL ||
   1818         qmlevel == NUM_QM_LEVELS - 1);
   1819  return quant_params->giqmatrix[qmlevel][plane][tx_size];
   1820 }
   1821 
   1822 // Build y/uv dequant values based on segmentation.
   1823 static inline void setup_segmentation_dequant(AV1_COMMON *const cm,
   1824                                              MACROBLOCKD *const xd) {
   1825  const int bit_depth = cm->seq_params->bit_depth;
   1826  // When segmentation is disabled, only the first value is used.  The
   1827  // remaining are don't cares.
   1828  const int max_segments = cm->seg.enabled ? MAX_SEGMENTS : 1;
   1829  CommonQuantParams *const quant_params = &cm->quant_params;
   1830  for (int i = 0; i < max_segments; ++i) {
   1831    const int qindex = xd->qindex[i];
   1832    quant_params->y_dequant_QTX[i][0] =
   1833        av1_dc_quant_QTX(qindex, quant_params->y_dc_delta_q, bit_depth);
   1834    quant_params->y_dequant_QTX[i][1] = av1_ac_quant_QTX(qindex, 0, bit_depth);
   1835    quant_params->u_dequant_QTX[i][0] =
   1836        av1_dc_quant_QTX(qindex, quant_params->u_dc_delta_q, bit_depth);
   1837    quant_params->u_dequant_QTX[i][1] =
   1838        av1_ac_quant_QTX(qindex, quant_params->u_ac_delta_q, bit_depth);
   1839    quant_params->v_dequant_QTX[i][0] =
   1840        av1_dc_quant_QTX(qindex, quant_params->v_dc_delta_q, bit_depth);
   1841    quant_params->v_dequant_QTX[i][1] =
   1842        av1_ac_quant_QTX(qindex, quant_params->v_ac_delta_q, bit_depth);
   1843    const int use_qmatrix = av1_use_qmatrix(quant_params, xd, i);
   1844    // NB: depends on base index so there is only 1 set per frame
   1845    // No quant weighting when lossless or signalled not using QM
   1846    const int qmlevel_y =
   1847        use_qmatrix ? quant_params->qmatrix_level_y : NUM_QM_LEVELS - 1;
   1848    for (int j = 0; j < TX_SIZES_ALL; ++j) {
   1849      quant_params->y_iqmatrix[i][j] =
   1850          get_iqmatrix(quant_params, qmlevel_y, AOM_PLANE_Y, j);
   1851    }
   1852    const int qmlevel_u =
   1853        use_qmatrix ? quant_params->qmatrix_level_u : NUM_QM_LEVELS - 1;
   1854    for (int j = 0; j < TX_SIZES_ALL; ++j) {
   1855      quant_params->u_iqmatrix[i][j] =
   1856          get_iqmatrix(quant_params, qmlevel_u, AOM_PLANE_U, j);
   1857    }
   1858    const int qmlevel_v =
   1859        use_qmatrix ? quant_params->qmatrix_level_v : NUM_QM_LEVELS - 1;
   1860    for (int j = 0; j < TX_SIZES_ALL; ++j) {
   1861      quant_params->v_iqmatrix[i][j] =
   1862          get_iqmatrix(quant_params, qmlevel_v, AOM_PLANE_V, j);
   1863    }
   1864  }
   1865 }
   1866 
   1867 static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
   1868  return aom_rb_read_bit(rb) ? SWITCHABLE
   1869                             : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
   1870 }
   1871 
   1872 static void read_frame_size(struct aom_read_bit_buffer *rb, int num_bits_width,
   1873                            int num_bits_height, int *width, int *height) {
   1874  *width = aom_rb_read_literal(rb, num_bits_width) + 1;
   1875  *height = aom_rb_read_literal(rb, num_bits_height) + 1;
   1876 }
   1877 
   1878 static inline void setup_render_size(AV1_COMMON *cm,
   1879                                     struct aom_read_bit_buffer *rb) {
   1880  cm->render_width = cm->superres_upscaled_width;
   1881  cm->render_height = cm->superres_upscaled_height;
   1882  if (aom_rb_read_bit(rb))
   1883    read_frame_size(rb, 16, 16, &cm->render_width, &cm->render_height);
   1884 }
   1885 
   1886 // TODO(afergs): make "struct aom_read_bit_buffer *const rb"?
   1887 static inline void setup_superres(AV1_COMMON *const cm,
   1888                                  struct aom_read_bit_buffer *rb, int *width,
   1889                                  int *height) {
   1890  cm->superres_upscaled_width = *width;
   1891  cm->superres_upscaled_height = *height;
   1892 
   1893  const SequenceHeader *const seq_params = cm->seq_params;
   1894  if (!seq_params->enable_superres) return;
   1895 
   1896  if (aom_rb_read_bit(rb)) {
   1897    cm->superres_scale_denominator =
   1898        (uint8_t)aom_rb_read_literal(rb, SUPERRES_SCALE_BITS);
   1899    cm->superres_scale_denominator += SUPERRES_SCALE_DENOMINATOR_MIN;
   1900    // Don't edit cm->width or cm->height directly, or the buffers won't get
   1901    // resized correctly
   1902    av1_calculate_scaled_superres_size(width, height,
   1903                                       cm->superres_scale_denominator);
   1904  } else {
   1905    // 1:1 scaling - ie. no scaling, scale not provided
   1906    cm->superres_scale_denominator = SCALE_NUMERATOR;
   1907  }
   1908 }
   1909 
   1910 static inline void resize_context_buffers(AV1_COMMON *cm, int width,
   1911                                          int height) {
   1912 #if CONFIG_SIZE_LIMIT
   1913  if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
   1914    aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
   1915                       "Dimensions of %dx%d beyond allowed size of %dx%d.",
   1916                       width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
   1917 #endif
   1918  if (cm->width != width || cm->height != height) {
   1919    const int new_mi_rows = CEIL_POWER_OF_TWO(height, MI_SIZE_LOG2);
   1920    const int new_mi_cols = CEIL_POWER_OF_TWO(width, MI_SIZE_LOG2);
   1921 
   1922    // Allocations in av1_alloc_context_buffers() depend on individual
   1923    // dimensions as well as the overall size.
   1924    if (new_mi_cols > cm->mi_params.mi_cols ||
   1925        new_mi_rows > cm->mi_params.mi_rows) {
   1926      if (av1_alloc_context_buffers(cm, width, height, BLOCK_4X4)) {
   1927        // The cm->mi_* values have been cleared and any existing context
   1928        // buffers have been freed. Clear cm->width and cm->height to be
   1929        // consistent and to force a realloc next time.
   1930        cm->width = 0;
   1931        cm->height = 0;
   1932        aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
   1933                           "Failed to allocate context buffers");
   1934      }
   1935    } else {
   1936      cm->mi_params.set_mb_mi(&cm->mi_params, width, height, BLOCK_4X4);
   1937    }
   1938    av1_init_mi_buffers(&cm->mi_params);
   1939    cm->width = width;
   1940    cm->height = height;
   1941  }
   1942 
   1943  ensure_mv_buffer(cm->cur_frame, cm);
   1944  cm->cur_frame->width = cm->width;
   1945  cm->cur_frame->height = cm->height;
   1946 }
   1947 
   1948 static inline void setup_buffer_pool(AV1_COMMON *cm) {
   1949  BufferPool *const pool = cm->buffer_pool;
   1950  const SequenceHeader *const seq_params = cm->seq_params;
   1951 
   1952  lock_buffer_pool(pool);
   1953  if (aom_realloc_frame_buffer(
   1954          &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
   1955          seq_params->subsampling_y, seq_params->use_highbitdepth,
   1956          AOM_DEC_BORDER_IN_PIXELS, cm->features.byte_alignment,
   1957          &cm->cur_frame->raw_frame_buffer, pool->get_fb_cb, pool->cb_priv,
   1958          false, 0)) {
   1959    unlock_buffer_pool(pool);
   1960    aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
   1961                       "Failed to allocate frame buffer");
   1962  }
   1963  unlock_buffer_pool(pool);
   1964 
   1965  cm->cur_frame->buf.bit_depth = (unsigned int)seq_params->bit_depth;
   1966  cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
   1967  cm->cur_frame->buf.transfer_characteristics =
   1968      seq_params->transfer_characteristics;
   1969  cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
   1970  cm->cur_frame->buf.monochrome = seq_params->monochrome;
   1971  cm->cur_frame->buf.chroma_sample_position =
   1972      seq_params->chroma_sample_position;
   1973  cm->cur_frame->buf.color_range = seq_params->color_range;
   1974  cm->cur_frame->buf.render_width = cm->render_width;
   1975  cm->cur_frame->buf.render_height = cm->render_height;
   1976 }
   1977 
   1978 static inline void setup_frame_size(AV1_COMMON *cm,
   1979                                    int frame_size_override_flag,
   1980                                    struct aom_read_bit_buffer *rb) {
   1981  const SequenceHeader *const seq_params = cm->seq_params;
   1982  int width, height;
   1983 
   1984  if (frame_size_override_flag) {
   1985    int num_bits_width = seq_params->num_bits_width;
   1986    int num_bits_height = seq_params->num_bits_height;
   1987    read_frame_size(rb, num_bits_width, num_bits_height, &width, &height);
   1988    if (width > seq_params->max_frame_width ||
   1989        height > seq_params->max_frame_height) {
   1990      aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
   1991                         "Frame dimensions are larger than the maximum values");
   1992    }
   1993  } else {
   1994    width = seq_params->max_frame_width;
   1995    height = seq_params->max_frame_height;
   1996  }
   1997 
   1998  setup_superres(cm, rb, &width, &height);
   1999  resize_context_buffers(cm, width, height);
   2000  setup_render_size(cm, rb);
   2001  setup_buffer_pool(cm);
   2002 }
   2003 
   2004 static inline void setup_sb_size(SequenceHeader *seq_params,
   2005                                 struct aom_read_bit_buffer *rb) {
   2006  set_sb_size(seq_params, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
   2007 }
   2008 
   2009 static inline int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
   2010                                          int ref_xss, int ref_yss,
   2011                                          aom_bit_depth_t this_bit_depth,
   2012                                          int this_xss, int this_yss) {
   2013  return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
   2014         ref_yss == this_yss;
   2015 }
   2016 
   2017 static inline void setup_frame_size_with_refs(AV1_COMMON *cm,
   2018                                              struct aom_read_bit_buffer *rb) {
   2019  int width, height;
   2020  int found = 0;
   2021  int has_valid_ref_frame = 0;
   2022  for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
   2023    if (aom_rb_read_bit(rb)) {
   2024      const RefCntBuffer *const ref_buf = get_ref_frame_buf(cm, i);
   2025      // This will never be NULL in a normal stream, as streams are required to
   2026      // have a shown keyframe before any inter frames, which would refresh all
   2027      // the reference buffers. However, it might be null if we're starting in
   2028      // the middle of a stream, and static analysis will error if we don't do
   2029      // a null check here.
   2030      if (ref_buf == NULL) {
   2031        aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
   2032                           "Invalid condition: invalid reference buffer");
   2033      } else {
   2034        const YV12_BUFFER_CONFIG *const buf = &ref_buf->buf;
   2035        width = buf->y_crop_width;
   2036        height = buf->y_crop_height;
   2037        cm->render_width = buf->render_width;
   2038        cm->render_height = buf->render_height;
   2039        setup_superres(cm, rb, &width, &height);
   2040        resize_context_buffers(cm, width, height);
   2041        found = 1;
   2042        break;
   2043      }
   2044    }
   2045  }
   2046 
   2047  const SequenceHeader *const seq_params = cm->seq_params;
   2048  if (!found) {
   2049    int num_bits_width = seq_params->num_bits_width;
   2050    int num_bits_height = seq_params->num_bits_height;
   2051 
   2052    read_frame_size(rb, num_bits_width, num_bits_height, &width, &height);
   2053    setup_superres(cm, rb, &width, &height);
   2054    resize_context_buffers(cm, width, height);
   2055    setup_render_size(cm, rb);
   2056  }
   2057 
   2058  if (width <= 0 || height <= 0)
   2059    aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
   2060                       "Invalid frame size");
   2061 
   2062  // Check to make sure at least one of frames that this frame references
   2063  // has valid dimensions.
   2064  for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
   2065    const RefCntBuffer *const ref_frame = get_ref_frame_buf(cm, i);
   2066    has_valid_ref_frame |=
   2067        valid_ref_frame_size(ref_frame->buf.y_crop_width,
   2068                             ref_frame->buf.y_crop_height, width, height);
   2069  }
   2070  if (!has_valid_ref_frame)
   2071    aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
   2072                       "Referenced frame has invalid size");
   2073  for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
   2074    const RefCntBuffer *const ref_frame = get_ref_frame_buf(cm, i);
   2075    if (!valid_ref_frame_img_fmt(
   2076            ref_frame->buf.bit_depth, ref_frame->buf.subsampling_x,
   2077            ref_frame->buf.subsampling_y, seq_params->bit_depth,
   2078            seq_params->subsampling_x, seq_params->subsampling_y))
   2079      aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
   2080                         "Referenced frame has incompatible color format");
   2081  }
   2082  setup_buffer_pool(cm);
   2083 }
   2084 
   2085 // Same function as av1_read_uniform but reading from uncompresses header wb
   2086 static int rb_read_uniform(struct aom_read_bit_buffer *const rb, int n) {
   2087  const int l = get_unsigned_bits(n);
   2088  const int m = (1 << l) - n;
   2089  const int v = aom_rb_read_literal(rb, l - 1);
   2090  assert(l != 0);
   2091  if (v < m)
   2092    return v;
   2093  else
   2094    return (v << 1) - m + aom_rb_read_bit(rb);
   2095 }
   2096 
   2097 static inline void read_tile_info_max_tile(
   2098    AV1_COMMON *const cm, struct aom_read_bit_buffer *const rb) {
   2099  const SequenceHeader *const seq_params = cm->seq_params;
   2100  CommonTileParams *const tiles = &cm->tiles;
   2101  int width_sb =
   2102      CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, seq_params->mib_size_log2);
   2103  int height_sb =
   2104      CEIL_POWER_OF_TWO(cm->mi_params.mi_rows, seq_params->mib_size_log2);
   2105 
   2106  av1_get_tile_limits(cm);
   2107  tiles->uniform_spacing = aom_rb_read_bit(rb);
   2108 
   2109  // Read tile columns
   2110  if (tiles->uniform_spacing) {
   2111    tiles->log2_cols = tiles->min_log2_cols;
   2112    while (tiles->log2_cols < tiles->max_log2_cols) {
   2113      if (!aom_rb_read_bit(rb)) {
   2114        break;
   2115      }
   2116      tiles->log2_cols++;
   2117    }
   2118  } else {
   2119    int i;
   2120    int start_sb;
   2121    for (i = 0, start_sb = 0; width_sb > 0 && i < MAX_TILE_COLS; i++) {
   2122      const int size_sb =
   2123          1 + rb_read_uniform(rb, AOMMIN(width_sb, tiles->max_width_sb));
   2124      tiles->col_start_sb[i] = start_sb;
   2125      start_sb += size_sb;
   2126      width_sb -= size_sb;
   2127    }
   2128    tiles->cols = i;
   2129    tiles->col_start_sb[i] = start_sb + width_sb;
   2130  }
   2131  av1_calculate_tile_cols(seq_params, cm->mi_params.mi_rows,
   2132                          cm->mi_params.mi_cols, tiles);
   2133 
   2134  // Read tile rows
   2135  if (tiles->uniform_spacing) {
   2136    tiles->log2_rows = tiles->min_log2_rows;
   2137    while (tiles->log2_rows < tiles->max_log2_rows) {
   2138      if (!aom_rb_read_bit(rb)) {
   2139        break;
   2140      }
   2141      tiles->log2_rows++;
   2142    }
   2143  } else {
   2144    int i;
   2145    int start_sb;
   2146    for (i = 0, start_sb = 0; height_sb > 0 && i < MAX_TILE_ROWS; i++) {
   2147      const int size_sb =
   2148          1 + rb_read_uniform(rb, AOMMIN(height_sb, tiles->max_height_sb));
   2149      tiles->row_start_sb[i] = start_sb;
   2150      start_sb += size_sb;
   2151      height_sb -= size_sb;
   2152    }
   2153    tiles->rows = i;
   2154    tiles->row_start_sb[i] = start_sb + height_sb;
   2155  }
   2156  av1_calculate_tile_rows(seq_params, cm->mi_params.mi_rows, tiles);
   2157 }
   2158 
   2159 void av1_set_single_tile_decoding_mode(AV1_COMMON *const cm) {
   2160  cm->tiles.single_tile_decoding = 0;
   2161  if (cm->tiles.large_scale) {
   2162    struct loopfilter *lf = &cm->lf;
   2163    RestorationInfo *const rst_info = cm->rst_info;
   2164    const CdefInfo *const cdef_info = &cm->cdef_info;
   2165 
   2166    // Figure out single_tile_decoding by loopfilter_level.
   2167    const int no_loopfilter = !(lf->filter_level[0] || lf->filter_level[1]);
   2168    const int no_cdef = cdef_info->cdef_bits == 0 &&
   2169                        cdef_info->cdef_strengths[0] == 0 &&
   2170                        cdef_info->cdef_uv_strengths[0] == 0;
   2171    const int no_restoration =
   2172        rst_info[0].frame_restoration_type == RESTORE_NONE &&
   2173        rst_info[1].frame_restoration_type == RESTORE_NONE &&
   2174        rst_info[2].frame_restoration_type == RESTORE_NONE;
   2175    assert(IMPLIES(cm->features.coded_lossless, no_loopfilter && no_cdef));
   2176    assert(IMPLIES(cm->features.all_lossless, no_restoration));
   2177    cm->tiles.single_tile_decoding = no_loopfilter && no_cdef && no_restoration;
   2178  }
   2179 }
   2180 
   2181 static inline void read_tile_info(AV1Decoder *const pbi,
   2182                                  struct aom_read_bit_buffer *const rb) {
   2183  AV1_COMMON *const cm = &pbi->common;
   2184 
   2185  read_tile_info_max_tile(cm, rb);
   2186 
   2187  pbi->context_update_tile_id = 0;
   2188  if (cm->tiles.rows * cm->tiles.cols > 1) {
   2189    // tile to use for cdf update
   2190    pbi->context_update_tile_id =
   2191        aom_rb_read_literal(rb, cm->tiles.log2_rows + cm->tiles.log2_cols);
   2192    if (pbi->context_update_tile_id >= cm->tiles.rows * cm->tiles.cols) {
   2193      aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   2194                         "Invalid context_update_tile_id");
   2195    }
   2196    // tile size magnitude
   2197    pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
   2198  }
   2199 }
   2200 
   2201 #if EXT_TILE_DEBUG
   2202 static inline void read_ext_tile_info(AV1Decoder *const pbi,
   2203                                      struct aom_read_bit_buffer *const rb) {
   2204  AV1_COMMON *const cm = &pbi->common;
   2205 
   2206  // This information is stored as a separate byte.
   2207  int mod = rb->bit_offset % CHAR_BIT;
   2208  if (mod > 0) aom_rb_read_literal(rb, CHAR_BIT - mod);
   2209  assert(rb->bit_offset % CHAR_BIT == 0);
   2210 
   2211  if (cm->tiles.cols * cm->tiles.rows > 1) {
   2212    // Read the number of bytes used to store tile size
   2213    pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
   2214    pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
   2215  }
   2216 }
   2217 #endif  // EXT_TILE_DEBUG
   2218 
   2219 static size_t mem_get_varsize(const uint8_t *src, int sz) {
   2220  switch (sz) {
   2221    case 1: return src[0];
   2222    case 2: return mem_get_le16(src);
   2223    case 3: return mem_get_le24(src);
   2224    case 4: return mem_get_le32(src);
   2225    default: assert(0 && "Invalid size"); return -1;
   2226  }
   2227 }
   2228 
   2229 #if EXT_TILE_DEBUG
   2230 // Reads the next tile returning its size and adjusting '*data' accordingly
   2231 // based on 'is_last'. On return, '*data' is updated to point to the end of the
   2232 // raw tile buffer in the bit stream.
   2233 static inline void get_ls_tile_buffer(
   2234    const uint8_t *const data_end, struct aom_internal_error_info *error_info,
   2235    const uint8_t **data, TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
   2236    int tile_size_bytes, int col, int row, int tile_copy_mode) {
   2237  size_t size;
   2238 
   2239  size_t copy_size = 0;
   2240  const uint8_t *copy_data = NULL;
   2241 
   2242  if (!read_is_valid(*data, tile_size_bytes, data_end))
   2243    aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
   2244                       "Truncated packet or corrupt tile length");
   2245  size = mem_get_varsize(*data, tile_size_bytes);
   2246 
   2247  // If tile_copy_mode = 1, then the top bit of the tile header indicates copy
   2248  // mode.
   2249  if (tile_copy_mode && (size >> (tile_size_bytes * 8 - 1)) == 1) {
   2250    // The remaining bits in the top byte signal the row offset
   2251    int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
   2252    if (offset > row) {
   2253      aom_internal_error(
   2254          error_info, AOM_CODEC_CORRUPT_FRAME,
   2255          "Invalid row offset in tile copy mode: row=%d offset=%d", row,
   2256          offset);
   2257    }
   2258 
   2259    // Currently, only use tiles in same column as reference tiles.
   2260    copy_data = tile_buffers[row - offset][col].data;
   2261    copy_size = tile_buffers[row - offset][col].size;
   2262    size = 0;
   2263  } else {
   2264    size += AV1_MIN_TILE_SIZE_BYTES;
   2265  }
   2266 
   2267  *data += tile_size_bytes;
   2268 
   2269  if (size > (size_t)(data_end - *data))
   2270    aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
   2271                       "Truncated packet or corrupt tile size");
   2272 
   2273  if (size > 0) {
   2274    tile_buffers[row][col].data = *data;
   2275    tile_buffers[row][col].size = size;
   2276  } else {
   2277    tile_buffers[row][col].data = copy_data;
   2278    tile_buffers[row][col].size = copy_size;
   2279  }
   2280 
   2281  *data += size;
   2282 }
   2283 
   2284 // Returns the end of the last tile buffer
   2285 // (tile_buffers[cm->tiles.rows - 1][cm->tiles.cols - 1]).
   2286 static const uint8_t *get_ls_tile_buffers(
   2287    AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
   2288    TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
   2289  AV1_COMMON *const cm = &pbi->common;
   2290  const int tile_cols = cm->tiles.cols;
   2291  const int tile_rows = cm->tiles.rows;
   2292  const int have_tiles = tile_cols * tile_rows > 1;
   2293  const uint8_t *raw_data_end;  // The end of the last tile buffer
   2294 
   2295  if (!have_tiles) {
   2296    const size_t tile_size = data_end - data;
   2297    tile_buffers[0][0].data = data;
   2298    tile_buffers[0][0].size = tile_size;
   2299    raw_data_end = NULL;
   2300  } else {
   2301    // We locate only the tile buffers that are required, which are the ones
   2302    // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
   2303    // need the last (bottom right) tile buffer, as we need to know where the
   2304    // end of the compressed frame buffer is for proper superframe decoding.
   2305 
   2306    const uint8_t *tile_col_data_end[MAX_TILE_COLS] = { NULL };
   2307    const uint8_t *const data_start = data;
   2308 
   2309    const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
   2310    const int single_row = pbi->dec_tile_row >= 0;
   2311    const int tile_rows_start = single_row ? dec_tile_row : 0;
   2312    const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
   2313    const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
   2314    const int single_col = pbi->dec_tile_col >= 0;
   2315    const int tile_cols_start = single_col ? dec_tile_col : 0;
   2316    const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
   2317 
   2318    const int tile_col_size_bytes = pbi->tile_col_size_bytes;
   2319    const int tile_size_bytes = pbi->tile_size_bytes;
   2320    int tile_width, tile_height;
   2321    if (!av1_get_uniform_tile_size(cm, &tile_width, &tile_height)) {
   2322      aom_internal_error(
   2323          &pbi->error, AOM_CODEC_CORRUPT_FRAME,
   2324          "Not all the tiles in the tile list have the same size.");
   2325    }
   2326    const int tile_copy_mode =
   2327        ((AOMMAX(tile_width, tile_height) << MI_SIZE_LOG2) <= 256) ? 1 : 0;
   2328    // Read tile column sizes for all columns (we need the last tile buffer)
   2329    for (int c = 0; c < tile_cols; ++c) {
   2330      const int is_last = c == tile_cols - 1;
   2331      size_t tile_col_size;
   2332 
   2333      if (!is_last) {
   2334        if (tile_col_size_bytes > data_end - data) {
   2335          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   2336                             "Not enough data to read tile_col_size");
   2337        }
   2338        tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
   2339        data += tile_col_size_bytes;
   2340        if (tile_col_size > (size_t)(data_end - data)) {
   2341          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   2342                             "tile_col_data_end[%d] is out of bound", c);
   2343        }
   2344        tile_col_data_end[c] = data + tile_col_size;
   2345      } else {
   2346        tile_col_size = data_end - data;
   2347        tile_col_data_end[c] = data_end;
   2348      }
   2349      data += tile_col_size;
   2350    }
   2351 
   2352    data = data_start;
   2353 
   2354    // Read the required tile sizes.
   2355    for (int c = tile_cols_start; c < tile_cols_end; ++c) {
   2356      const int is_last = c == tile_cols - 1;
   2357 
   2358      if (c > 0) data = tile_col_data_end[c - 1];
   2359 
   2360      if (!is_last) data += tile_col_size_bytes;
   2361 
   2362      // Get the whole of the last column, otherwise stop at the required tile.
   2363      for (int r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
   2364        get_ls_tile_buffer(tile_col_data_end[c], &pbi->error, &data,
   2365                           tile_buffers, tile_size_bytes, c, r, tile_copy_mode);
   2366      }
   2367    }
   2368 
   2369    // If we have not read the last column, then read it to get the last tile.
   2370    if (tile_cols_end != tile_cols) {
   2371      const int c = tile_cols - 1;
   2372 
   2373      data = tile_col_data_end[c - 1];
   2374 
   2375      for (int r = 0; r < tile_rows; ++r) {
   2376        get_ls_tile_buffer(tile_col_data_end[c], &pbi->error, &data,
   2377                           tile_buffers, tile_size_bytes, c, r, tile_copy_mode);
   2378      }
   2379    }
   2380    raw_data_end = data;
   2381  }
   2382  return raw_data_end;
   2383 }
   2384 #endif  // EXT_TILE_DEBUG
   2385 
   2386 static const uint8_t *get_ls_single_tile_buffer(
   2387    AV1Decoder *pbi, const uint8_t *data,
   2388    TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
   2389  assert(pbi->dec_tile_row >= 0 && pbi->dec_tile_col >= 0);
   2390  tile_buffers[pbi->dec_tile_row][pbi->dec_tile_col].data = data;
   2391  tile_buffers[pbi->dec_tile_row][pbi->dec_tile_col].size =
   2392      (size_t)pbi->coded_tile_data_size;
   2393  return data + pbi->coded_tile_data_size;
   2394 }
   2395 
   2396 // Reads the next tile returning its size and adjusting '*data' accordingly
   2397 // based on 'is_last'.
   2398 static inline void get_tile_buffer(const uint8_t *const data_end,
   2399                                   const int tile_size_bytes, int is_last,
   2400                                   struct aom_internal_error_info *error_info,
   2401                                   const uint8_t **data,
   2402                                   TileBufferDec *const buf) {
   2403  size_t size;
   2404 
   2405  if (!is_last) {
   2406    if (!read_is_valid(*data, tile_size_bytes, data_end))
   2407      aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
   2408                         "Not enough data to read tile size");
   2409 
   2410    size = mem_get_varsize(*data, tile_size_bytes) + AV1_MIN_TILE_SIZE_BYTES;
   2411    *data += tile_size_bytes;
   2412 
   2413    if (size > (size_t)(data_end - *data))
   2414      aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
   2415                         "Truncated packet or corrupt tile size");
   2416  } else {
   2417    size = data_end - *data;
   2418  }
   2419 
   2420  buf->data = *data;
   2421  buf->size = size;
   2422 
   2423  *data += size;
   2424 }
   2425 
   2426 static inline void get_tile_buffers(
   2427    AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
   2428    TileBufferDec (*const tile_buffers)[MAX_TILE_COLS], int start_tile,
   2429    int end_tile) {
   2430  AV1_COMMON *const cm = &pbi->common;
   2431  const int tile_cols = cm->tiles.cols;
   2432  const int tile_rows = cm->tiles.rows;
   2433  int tc = 0;
   2434 
   2435  for (int r = 0; r < tile_rows; ++r) {
   2436    for (int c = 0; c < tile_cols; ++c, ++tc) {
   2437      TileBufferDec *const buf = &tile_buffers[r][c];
   2438 
   2439      const int is_last = (tc == end_tile);
   2440      const size_t hdr_offset = 0;
   2441 
   2442      if (tc < start_tile || tc > end_tile) continue;
   2443 
   2444      if (data + hdr_offset >= data_end)
   2445        aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   2446                           "Data ended before all tiles were read.");
   2447      data += hdr_offset;
   2448      get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &pbi->error,
   2449                      &data, buf);
   2450    }
   2451  }
   2452 }
   2453 
   2454 static inline void set_cb_buffer(AV1Decoder *pbi, DecoderCodingBlock *dcb,
   2455                                 CB_BUFFER *cb_buffer_base,
   2456                                 const int num_planes, int mi_row, int mi_col) {
   2457  AV1_COMMON *const cm = &pbi->common;
   2458  int mib_size_log2 = cm->seq_params->mib_size_log2;
   2459  int stride = (cm->mi_params.mi_cols >> mib_size_log2) + 1;
   2460  int offset = (mi_row >> mib_size_log2) * stride + (mi_col >> mib_size_log2);
   2461  CB_BUFFER *cb_buffer = cb_buffer_base + offset;
   2462 
   2463  for (int plane = 0; plane < num_planes; ++plane) {
   2464    dcb->dqcoeff_block[plane] = cb_buffer->dqcoeff[plane];
   2465    dcb->eob_data[plane] = cb_buffer->eob_data[plane];
   2466    dcb->cb_offset[plane] = 0;
   2467    dcb->txb_offset[plane] = 0;
   2468  }
   2469  MACROBLOCKD *const xd = &dcb->xd;
   2470  xd->plane[0].color_index_map = cb_buffer->color_index_map[0];
   2471  xd->plane[1].color_index_map = cb_buffer->color_index_map[1];
   2472  xd->color_index_map_offset[0] = 0;
   2473  xd->color_index_map_offset[1] = 0;
   2474 }
   2475 
   2476 static inline void decoder_alloc_tile_data(AV1Decoder *pbi, const int n_tiles) {
   2477  AV1_COMMON *const cm = &pbi->common;
   2478  aom_free(pbi->tile_data);
   2479  pbi->allocated_tiles = 0;
   2480  CHECK_MEM_ERROR(cm, pbi->tile_data,
   2481                  aom_memalign(32, n_tiles * sizeof(*pbi->tile_data)));
   2482  pbi->allocated_tiles = n_tiles;
   2483  for (int i = 0; i < n_tiles; i++) {
   2484    TileDataDec *const tile_data = pbi->tile_data + i;
   2485    av1_zero(tile_data->dec_row_mt_sync);
   2486  }
   2487  pbi->allocated_row_mt_sync_rows = 0;
   2488 }
   2489 
   2490 // Set up nsync by width.
   2491 static inline int get_sync_range(int width) {
   2492 // nsync numbers are picked by testing.
   2493 #if 0
   2494  if (width < 640)
   2495    return 1;
   2496  else if (width <= 1280)
   2497    return 2;
   2498  else if (width <= 4096)
   2499    return 4;
   2500  else
   2501    return 8;
   2502 #else
   2503  (void)width;
   2504 #endif
   2505  return 1;
   2506 }
   2507 
   2508 // Allocate memory for decoder row synchronization
   2509 static inline void dec_row_mt_alloc(AV1DecRowMTSync *dec_row_mt_sync,
   2510                                    AV1_COMMON *cm, int rows) {
   2511  dec_row_mt_sync->allocated_sb_rows = rows;
   2512 #if CONFIG_MULTITHREAD
   2513  {
   2514    int i;
   2515 
   2516    CHECK_MEM_ERROR(cm, dec_row_mt_sync->mutex_,
   2517                    aom_malloc(sizeof(*(dec_row_mt_sync->mutex_)) * rows));
   2518    if (dec_row_mt_sync->mutex_) {
   2519      for (i = 0; i < rows; ++i) {
   2520        pthread_mutex_init(&dec_row_mt_sync->mutex_[i], NULL);
   2521      }
   2522    }
   2523 
   2524    CHECK_MEM_ERROR(cm, dec_row_mt_sync->cond_,
   2525                    aom_malloc(sizeof(*(dec_row_mt_sync->cond_)) * rows));
   2526    if (dec_row_mt_sync->cond_) {
   2527      for (i = 0; i < rows; ++i) {
   2528        pthread_cond_init(&dec_row_mt_sync->cond_[i], NULL);
   2529      }
   2530    }
   2531  }
   2532 #endif  // CONFIG_MULTITHREAD
   2533 
   2534  CHECK_MEM_ERROR(cm, dec_row_mt_sync->cur_sb_col,
   2535                  aom_malloc(sizeof(*(dec_row_mt_sync->cur_sb_col)) * rows));
   2536 
   2537  // Set up nsync.
   2538  dec_row_mt_sync->sync_range = get_sync_range(cm->width);
   2539 }
   2540 
   2541 // Deallocate decoder row synchronization related mutex and data
   2542 void av1_dec_row_mt_dealloc(AV1DecRowMTSync *dec_row_mt_sync) {
   2543  if (dec_row_mt_sync != NULL) {
   2544 #if CONFIG_MULTITHREAD
   2545    int i;
   2546    if (dec_row_mt_sync->mutex_ != NULL) {
   2547      for (i = 0; i < dec_row_mt_sync->allocated_sb_rows; ++i) {
   2548        pthread_mutex_destroy(&dec_row_mt_sync->mutex_[i]);
   2549      }
   2550      aom_free(dec_row_mt_sync->mutex_);
   2551    }
   2552    if (dec_row_mt_sync->cond_ != NULL) {
   2553      for (i = 0; i < dec_row_mt_sync->allocated_sb_rows; ++i) {
   2554        pthread_cond_destroy(&dec_row_mt_sync->cond_[i]);
   2555      }
   2556      aom_free(dec_row_mt_sync->cond_);
   2557    }
   2558 #endif  // CONFIG_MULTITHREAD
   2559    aom_free(dec_row_mt_sync->cur_sb_col);
   2560 
   2561    // clear the structure as the source of this call may be a resize in which
   2562    // case this call will be followed by an _alloc() which may fail.
   2563    av1_zero(*dec_row_mt_sync);
   2564  }
   2565 }
   2566 
   2567 static inline void sync_read(AV1DecRowMTSync *const dec_row_mt_sync, int r,
   2568                             int c) {
   2569 #if CONFIG_MULTITHREAD
   2570  const int nsync = dec_row_mt_sync->sync_range;
   2571 
   2572  if (r && !(c & (nsync - 1))) {
   2573    pthread_mutex_t *const mutex = &dec_row_mt_sync->mutex_[r - 1];
   2574    pthread_mutex_lock(mutex);
   2575 
   2576    while (c > dec_row_mt_sync->cur_sb_col[r - 1] - nsync -
   2577                   dec_row_mt_sync->intrabc_extra_top_right_sb_delay) {
   2578      pthread_cond_wait(&dec_row_mt_sync->cond_[r - 1], mutex);
   2579    }
   2580    pthread_mutex_unlock(mutex);
   2581  }
   2582 #else
   2583  (void)dec_row_mt_sync;
   2584  (void)r;
   2585  (void)c;
   2586 #endif  // CONFIG_MULTITHREAD
   2587 }
   2588 
   2589 static inline void sync_write(AV1DecRowMTSync *const dec_row_mt_sync, int r,
   2590                              int c, const int sb_cols) {
   2591 #if CONFIG_MULTITHREAD
   2592  const int nsync = dec_row_mt_sync->sync_range;
   2593  int cur;
   2594  int sig = 1;
   2595 
   2596  if (c < sb_cols - 1) {
   2597    cur = c;
   2598    if (c % nsync) sig = 0;
   2599  } else {
   2600    cur = sb_cols + nsync + dec_row_mt_sync->intrabc_extra_top_right_sb_delay;
   2601  }
   2602 
   2603  if (sig) {
   2604    pthread_mutex_lock(&dec_row_mt_sync->mutex_[r]);
   2605 
   2606    dec_row_mt_sync->cur_sb_col[r] = cur;
   2607 
   2608    pthread_cond_signal(&dec_row_mt_sync->cond_[r]);
   2609    pthread_mutex_unlock(&dec_row_mt_sync->mutex_[r]);
   2610  }
   2611 #else
   2612  (void)dec_row_mt_sync;
   2613  (void)r;
   2614  (void)c;
   2615  (void)sb_cols;
   2616 #endif  // CONFIG_MULTITHREAD
   2617 }
   2618 
   2619 static inline void signal_decoding_done_for_erroneous_row(
   2620    AV1Decoder *const pbi, const MACROBLOCKD *const xd) {
   2621  AV1_COMMON *const cm = &pbi->common;
   2622  const TileInfo *const tile = &xd->tile;
   2623  const int sb_row_in_tile =
   2624      ((xd->mi_row - tile->mi_row_start) >> cm->seq_params->mib_size_log2);
   2625  const int sb_cols_in_tile = av1_get_sb_cols_in_tile(cm, tile);
   2626  TileDataDec *const tile_data =
   2627      pbi->tile_data + tile->tile_row * cm->tiles.cols + tile->tile_col;
   2628  AV1DecRowMTSync *dec_row_mt_sync = &tile_data->dec_row_mt_sync;
   2629 
   2630  sync_write(dec_row_mt_sync, sb_row_in_tile, sb_cols_in_tile - 1,
   2631             sb_cols_in_tile);
   2632 }
   2633 
   2634 static inline void decode_tile_sb_row(AV1Decoder *pbi, ThreadData *const td,
   2635                                      const TileInfo *tile_info,
   2636                                      const int mi_row) {
   2637  AV1_COMMON *const cm = &pbi->common;
   2638  const int num_planes = av1_num_planes(cm);
   2639  TileDataDec *const tile_data = pbi->tile_data +
   2640                                 tile_info->tile_row * cm->tiles.cols +
   2641                                 tile_info->tile_col;
   2642  const int sb_cols_in_tile = av1_get_sb_cols_in_tile(cm, tile_info);
   2643  const int sb_row_in_tile =
   2644      (mi_row - tile_info->mi_row_start) >> cm->seq_params->mib_size_log2;
   2645  int sb_col_in_tile = 0;
   2646  int row_mt_exit = 0;
   2647 
   2648  for (int mi_col = tile_info->mi_col_start; mi_col < tile_info->mi_col_end;
   2649       mi_col += cm->seq_params->mib_size, sb_col_in_tile++) {
   2650    set_cb_buffer(pbi, &td->dcb, pbi->cb_buffer_base, num_planes, mi_row,
   2651                  mi_col);
   2652 
   2653    sync_read(&tile_data->dec_row_mt_sync, sb_row_in_tile, sb_col_in_tile);
   2654 
   2655 #if CONFIG_MULTITHREAD
   2656    pthread_mutex_lock(pbi->row_mt_mutex_);
   2657 #endif
   2658    row_mt_exit = pbi->frame_row_mt_info.row_mt_exit;
   2659 #if CONFIG_MULTITHREAD
   2660    pthread_mutex_unlock(pbi->row_mt_mutex_);
   2661 #endif
   2662 
   2663    if (!row_mt_exit) {
   2664      // Decoding of the super-block
   2665      decode_partition(pbi, td, mi_row, mi_col, td->bit_reader,
   2666                       cm->seq_params->sb_size, 0x2);
   2667    }
   2668 
   2669    sync_write(&tile_data->dec_row_mt_sync, sb_row_in_tile, sb_col_in_tile,
   2670               sb_cols_in_tile);
   2671  }
   2672 }
   2673 
   2674 static int check_trailing_bits_after_symbol_coder(aom_reader *r) {
   2675  if (aom_reader_has_overflowed(r)) return -1;
   2676 
   2677  uint32_t nb_bits = aom_reader_tell(r);
   2678  uint32_t nb_bytes = (nb_bits + 7) >> 3;
   2679  const uint8_t *p = aom_reader_find_begin(r) + nb_bytes;
   2680 
   2681  // aom_reader_tell() returns 1 for a newly initialized decoder, and the
   2682  // return value only increases as values are decoded. So nb_bits > 0, and
   2683  // thus p > p_begin. Therefore accessing p[-1] is safe.
   2684  uint8_t last_byte = p[-1];
   2685  uint8_t pattern = 128 >> ((nb_bits - 1) & 7);
   2686  if ((last_byte & (2 * pattern - 1)) != pattern) return -1;
   2687 
   2688  // Make sure that all padding bytes are zero as required by the spec.
   2689  const uint8_t *p_end = aom_reader_find_end(r);
   2690  while (p < p_end) {
   2691    if (*p != 0) return -1;
   2692    p++;
   2693  }
   2694  return 0;
   2695 }
   2696 
   2697 static inline void set_decode_func_pointers(ThreadData *td,
   2698                                            int parse_decode_flag) {
   2699  td->read_coeffs_tx_intra_block_visit = decode_block_void;
   2700  td->predict_and_recon_intra_block_visit = decode_block_void;
   2701  td->read_coeffs_tx_inter_block_visit = decode_block_void;
   2702  td->inverse_tx_inter_block_visit = decode_block_void;
   2703  td->predict_inter_block_visit = predict_inter_block_void;
   2704  td->cfl_store_inter_block_visit = cfl_store_inter_block_void;
   2705 
   2706  if (parse_decode_flag & 0x1) {
   2707    td->read_coeffs_tx_intra_block_visit = read_coeffs_tx_intra_block;
   2708    td->read_coeffs_tx_inter_block_visit = av1_read_coeffs_txb;
   2709  }
   2710  if (parse_decode_flag & 0x2) {
   2711    td->predict_and_recon_intra_block_visit =
   2712        predict_and_reconstruct_intra_block;
   2713    td->inverse_tx_inter_block_visit = inverse_transform_inter_block;
   2714    td->predict_inter_block_visit = predict_inter_block;
   2715    td->cfl_store_inter_block_visit = cfl_store_inter_block;
   2716  }
   2717 }
   2718 
   2719 static inline void decode_tile(AV1Decoder *pbi, ThreadData *const td,
   2720                               int tile_row, int tile_col) {
   2721  TileInfo tile_info;
   2722 
   2723  AV1_COMMON *const cm = &pbi->common;
   2724  const int num_planes = av1_num_planes(cm);
   2725 
   2726  av1_tile_set_row(&tile_info, cm, tile_row);
   2727  av1_tile_set_col(&tile_info, cm, tile_col);
   2728  DecoderCodingBlock *const dcb = &td->dcb;
   2729  MACROBLOCKD *const xd = &dcb->xd;
   2730 
   2731  av1_zero_above_context(cm, xd, tile_info.mi_col_start, tile_info.mi_col_end,
   2732                         tile_row);
   2733  av1_reset_loop_filter_delta(xd, num_planes);
   2734  av1_reset_loop_restoration(xd, num_planes);
   2735 
   2736  for (int mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
   2737       mi_row += cm->seq_params->mib_size) {
   2738    av1_zero_left_context(xd);
   2739 
   2740    for (int mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
   2741         mi_col += cm->seq_params->mib_size) {
   2742      set_cb_buffer(pbi, dcb, &td->cb_buffer_base, num_planes, 0, 0);
   2743 
   2744      // Bit-stream parsing and decoding of the superblock
   2745      decode_partition(pbi, td, mi_row, mi_col, td->bit_reader,
   2746                       cm->seq_params->sb_size, 0x3);
   2747 
   2748      if (aom_reader_has_overflowed(td->bit_reader)) {
   2749        aom_merge_corrupted_flag(&dcb->corrupted, 1);
   2750        return;
   2751      }
   2752    }
   2753  }
   2754 
   2755  int corrupted =
   2756      (check_trailing_bits_after_symbol_coder(td->bit_reader)) ? 1 : 0;
   2757  aom_merge_corrupted_flag(&dcb->corrupted, corrupted);
   2758 }
   2759 
   2760 static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
   2761                                   const uint8_t *data_end, int start_tile,
   2762                                   int end_tile) {
   2763  AV1_COMMON *const cm = &pbi->common;
   2764  ThreadData *const td = &pbi->td;
   2765  CommonTileParams *const tiles = &cm->tiles;
   2766  const int tile_cols = tiles->cols;
   2767  const int tile_rows = tiles->rows;
   2768  const int n_tiles = tile_cols * tile_rows;
   2769  TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
   2770  const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
   2771  const int single_row = pbi->dec_tile_row >= 0;
   2772  const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
   2773  const int single_col = pbi->dec_tile_col >= 0;
   2774  int tile_rows_start;
   2775  int tile_rows_end;
   2776  int tile_cols_start;
   2777  int tile_cols_end;
   2778  int inv_col_order;
   2779  int inv_row_order;
   2780  int tile_row, tile_col;
   2781  uint8_t allow_update_cdf;
   2782  const uint8_t *raw_data_end = NULL;
   2783 
   2784  if (tiles->large_scale) {
   2785    tile_rows_start = single_row ? dec_tile_row : 0;
   2786    tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
   2787    tile_cols_start = single_col ? dec_tile_col : 0;
   2788    tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
   2789    inv_col_order = pbi->inv_tile_order && !single_col;
   2790    inv_row_order = pbi->inv_tile_order && !single_row;
   2791    allow_update_cdf = 0;
   2792  } else {
   2793    tile_rows_start = 0;
   2794    tile_rows_end = tile_rows;
   2795    tile_cols_start = 0;
   2796    tile_cols_end = tile_cols;
   2797    inv_col_order = pbi->inv_tile_order;
   2798    inv_row_order = pbi->inv_tile_order;
   2799    allow_update_cdf = 1;
   2800  }
   2801 
   2802  // No tiles to decode.
   2803  if (tile_rows_end <= tile_rows_start || tile_cols_end <= tile_cols_start ||
   2804      // First tile is larger than end_tile.
   2805      tile_rows_start * tiles->cols + tile_cols_start > end_tile ||
   2806      // Last tile is smaller than start_tile.
   2807      (tile_rows_end - 1) * tiles->cols + tile_cols_end - 1 < start_tile)
   2808    return data;
   2809 
   2810  allow_update_cdf = allow_update_cdf && !cm->features.disable_cdf_update;
   2811 
   2812  assert(tile_rows <= MAX_TILE_ROWS);
   2813  assert(tile_cols <= MAX_TILE_COLS);
   2814 
   2815 #if EXT_TILE_DEBUG
   2816  if (tiles->large_scale && !pbi->ext_tile_debug)
   2817    raw_data_end = get_ls_single_tile_buffer(pbi, data, tile_buffers);
   2818  else if (tiles->large_scale && pbi->ext_tile_debug)
   2819    raw_data_end = get_ls_tile_buffers(pbi, data, data_end, tile_buffers);
   2820  else
   2821 #endif  // EXT_TILE_DEBUG
   2822    get_tile_buffers(pbi, data, data_end, tile_buffers, start_tile, end_tile);
   2823 
   2824  if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
   2825    decoder_alloc_tile_data(pbi, n_tiles);
   2826  }
   2827  if (pbi->dcb.xd.seg_mask == NULL)
   2828    CHECK_MEM_ERROR(cm, pbi->dcb.xd.seg_mask,
   2829                    (uint8_t *)aom_memalign(
   2830                        16, 2 * MAX_SB_SQUARE * sizeof(*pbi->dcb.xd.seg_mask)));
   2831 #if CONFIG_ACCOUNTING
   2832  if (pbi->acct_enabled) {
   2833    aom_accounting_reset(&pbi->accounting);
   2834  }
   2835 #endif
   2836 
   2837  set_decode_func_pointers(&pbi->td, 0x3);
   2838 
   2839  // Load all tile information into thread_data.
   2840  td->dcb = pbi->dcb;
   2841 
   2842  td->dcb.corrupted = 0;
   2843  td->dcb.mc_buf[0] = td->mc_buf[0];
   2844  td->dcb.mc_buf[1] = td->mc_buf[1];
   2845  td->dcb.xd.tmp_conv_dst = td->tmp_conv_dst;
   2846  for (int j = 0; j < 2; ++j) {
   2847    td->dcb.xd.tmp_obmc_bufs[j] = td->tmp_obmc_bufs[j];
   2848  }
   2849 
   2850  for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
   2851    const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
   2852 
   2853    for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
   2854      const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
   2855      TileDataDec *const tile_data = pbi->tile_data + row * tiles->cols + col;
   2856      const TileBufferDec *const tile_bs_buf = &tile_buffers[row][col];
   2857 
   2858      if (row * tiles->cols + col < start_tile ||
   2859          row * tiles->cols + col > end_tile)
   2860        continue;
   2861 
   2862      td->bit_reader = &tile_data->bit_reader;
   2863      av1_zero(td->cb_buffer_base.dqcoeff);
   2864      av1_tile_init(&td->dcb.xd.tile, cm, row, col);
   2865      td->dcb.xd.current_base_qindex = cm->quant_params.base_qindex;
   2866      setup_bool_decoder(&td->dcb.xd, tile_bs_buf->data, data_end,
   2867                         tile_bs_buf->size, &pbi->error, td->bit_reader,
   2868                         allow_update_cdf);
   2869 #if CONFIG_ACCOUNTING
   2870      if (pbi->acct_enabled) {
   2871        td->bit_reader->accounting = &pbi->accounting;
   2872        td->bit_reader->accounting->last_tell_frac =
   2873            aom_reader_tell_frac(td->bit_reader);
   2874      } else {
   2875        td->bit_reader->accounting = NULL;
   2876      }
   2877 #endif
   2878      av1_init_macroblockd(cm, &td->dcb.xd);
   2879      av1_init_above_context(&cm->above_contexts, av1_num_planes(cm), row,
   2880                             &td->dcb.xd);
   2881 
   2882      // Initialise the tile context from the frame context
   2883      tile_data->tctx = *cm->fc;
   2884      td->dcb.xd.tile_ctx = &tile_data->tctx;
   2885 
   2886      // decode tile
   2887      decode_tile(pbi, td, row, col);
   2888      aom_merge_corrupted_flag(&pbi->dcb.corrupted, td->dcb.corrupted);
   2889      if (pbi->dcb.corrupted)
   2890        aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   2891                           "Failed to decode tile data");
   2892    }
   2893  }
   2894 
   2895  if (tiles->large_scale) {
   2896    if (n_tiles == 1) {
   2897      // Find the end of the single tile buffer
   2898      return aom_reader_find_end(&pbi->tile_data->bit_reader);
   2899    }
   2900    // Return the end of the last tile buffer
   2901    return raw_data_end;
   2902  }
   2903  TileDataDec *const tile_data = pbi->tile_data + end_tile;
   2904 
   2905  return aom_reader_find_end(&tile_data->bit_reader);
   2906 }
   2907 
   2908 static TileJobsDec *get_dec_job_info(AV1DecTileMT *tile_mt_info) {
   2909  TileJobsDec *cur_job_info = NULL;
   2910 #if CONFIG_MULTITHREAD
   2911  pthread_mutex_lock(tile_mt_info->job_mutex);
   2912 
   2913  if (tile_mt_info->jobs_dequeued < tile_mt_info->jobs_enqueued) {
   2914    cur_job_info = tile_mt_info->job_queue + tile_mt_info->jobs_dequeued;
   2915    tile_mt_info->jobs_dequeued++;
   2916  }
   2917 
   2918  pthread_mutex_unlock(tile_mt_info->job_mutex);
   2919 #else
   2920  (void)tile_mt_info;
   2921 #endif
   2922  return cur_job_info;
   2923 }
   2924 
   2925 static inline void tile_worker_hook_init(AV1Decoder *const pbi,
   2926                                         DecWorkerData *const thread_data,
   2927                                         const TileBufferDec *const tile_buffer,
   2928                                         TileDataDec *const tile_data,
   2929                                         uint8_t allow_update_cdf) {
   2930  AV1_COMMON *cm = &pbi->common;
   2931  ThreadData *const td = thread_data->td;
   2932  int tile_row = tile_data->tile_info.tile_row;
   2933  int tile_col = tile_data->tile_info.tile_col;
   2934 
   2935  td->bit_reader = &tile_data->bit_reader;
   2936  av1_zero(td->cb_buffer_base.dqcoeff);
   2937 
   2938  MACROBLOCKD *const xd = &td->dcb.xd;
   2939  av1_tile_init(&xd->tile, cm, tile_row, tile_col);
   2940  xd->current_base_qindex = cm->quant_params.base_qindex;
   2941 
   2942  setup_bool_decoder(xd, tile_buffer->data, thread_data->data_end,
   2943                     tile_buffer->size, &thread_data->error_info,
   2944                     td->bit_reader, allow_update_cdf);
   2945 #if CONFIG_ACCOUNTING
   2946  if (pbi->acct_enabled) {
   2947    td->bit_reader->accounting = &pbi->accounting;
   2948    td->bit_reader->accounting->last_tell_frac =
   2949        aom_reader_tell_frac(td->bit_reader);
   2950  } else {
   2951    td->bit_reader->accounting = NULL;
   2952  }
   2953 #endif
   2954  av1_init_macroblockd(cm, xd);
   2955  xd->error_info = &thread_data->error_info;
   2956  av1_init_above_context(&cm->above_contexts, av1_num_planes(cm), tile_row, xd);
   2957 
   2958  // Initialise the tile context from the frame context
   2959  tile_data->tctx = *cm->fc;
   2960  xd->tile_ctx = &tile_data->tctx;
   2961 #if CONFIG_ACCOUNTING
   2962  if (pbi->acct_enabled) {
   2963    tile_data->bit_reader.accounting->last_tell_frac =
   2964        aom_reader_tell_frac(&tile_data->bit_reader);
   2965  }
   2966 #endif
   2967 }
   2968 
   2969 static int tile_worker_hook(void *arg1, void *arg2) {
   2970  DecWorkerData *const thread_data = (DecWorkerData *)arg1;
   2971  AV1Decoder *const pbi = (AV1Decoder *)arg2;
   2972  AV1_COMMON *cm = &pbi->common;
   2973  ThreadData *const td = thread_data->td;
   2974  uint8_t allow_update_cdf;
   2975 
   2976  // The jmp_buf is valid only for the duration of the function that calls
   2977  // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
   2978  // before it returns.
   2979  if (setjmp(thread_data->error_info.jmp)) {
   2980    thread_data->error_info.setjmp = 0;
   2981    thread_data->td->dcb.corrupted = 1;
   2982    return 0;
   2983  }
   2984  thread_data->error_info.setjmp = 1;
   2985 
   2986  allow_update_cdf = cm->tiles.large_scale ? 0 : 1;
   2987  allow_update_cdf = allow_update_cdf && !cm->features.disable_cdf_update;
   2988 
   2989  set_decode_func_pointers(td, 0x3);
   2990 
   2991  assert(cm->tiles.cols > 0);
   2992  while (!td->dcb.corrupted) {
   2993    TileJobsDec *cur_job_info = get_dec_job_info(&pbi->tile_mt_info);
   2994 
   2995    if (cur_job_info != NULL) {
   2996      const TileBufferDec *const tile_buffer = cur_job_info->tile_buffer;
   2997      TileDataDec *const tile_data = cur_job_info->tile_data;
   2998      tile_worker_hook_init(pbi, thread_data, tile_buffer, tile_data,
   2999                            allow_update_cdf);
   3000      // decode tile
   3001      int tile_row = tile_data->tile_info.tile_row;
   3002      int tile_col = tile_data->tile_info.tile_col;
   3003      decode_tile(pbi, td, tile_row, tile_col);
   3004    } else {
   3005      break;
   3006    }
   3007  }
   3008  thread_data->error_info.setjmp = 0;
   3009  return !td->dcb.corrupted;
   3010 }
   3011 
   3012 static inline int get_max_row_mt_workers_per_tile(AV1_COMMON *cm,
   3013                                                  const TileInfo *tile) {
   3014  // NOTE: Currently value of max workers is calculated based
   3015  // on the parse and decode time. As per the theoretical estimate
   3016  // when percentage of parse time is equal to percentage of decode
   3017  // time, number of workers needed to parse + decode a tile can not
   3018  // exceed more than 2.
   3019  // TODO(any): Modify this value if parsing is optimized in future.
   3020  int sb_rows = av1_get_sb_rows_in_tile(cm, tile);
   3021  int max_workers =
   3022      sb_rows == 1 ? AOM_MIN_THREADS_PER_TILE : AOM_MAX_THREADS_PER_TILE;
   3023  return max_workers;
   3024 }
   3025 
   3026 // The caller must hold pbi->row_mt_mutex_ when calling this function.
   3027 // Returns 1 if either the next job is stored in *next_job_info or 1 is stored
   3028 // in *end_of_frame.
   3029 // NOTE: The caller waits on pbi->row_mt_cond_ if this function returns 0.
   3030 // The return value of this function depends on the following variables:
   3031 // - frame_row_mt_info->mi_rows_parse_done
   3032 // - frame_row_mt_info->mi_rows_decode_started
   3033 // - frame_row_mt_info->row_mt_exit
   3034 // Therefore we may need to signal or broadcast pbi->row_mt_cond_ if any of
   3035 // these variables is modified.
   3036 static int get_next_job_info(AV1Decoder *const pbi,
   3037                             AV1DecRowMTJobInfo *next_job_info,
   3038                             int *end_of_frame) {
   3039  AV1_COMMON *cm = &pbi->common;
   3040  TileDataDec *tile_data;
   3041  AV1DecRowMTSync *dec_row_mt_sync;
   3042  AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
   3043  const int tile_rows_start = frame_row_mt_info->tile_rows_start;
   3044  const int tile_rows_end = frame_row_mt_info->tile_rows_end;
   3045  const int tile_cols_start = frame_row_mt_info->tile_cols_start;
   3046  const int tile_cols_end = frame_row_mt_info->tile_cols_end;
   3047  const int start_tile = frame_row_mt_info->start_tile;
   3048  const int end_tile = frame_row_mt_info->end_tile;
   3049  const int sb_mi_size = mi_size_wide[cm->seq_params->sb_size];
   3050  int num_mis_to_decode, num_threads_working;
   3051  int num_mis_waiting_for_decode;
   3052  int min_threads_working = INT_MAX;
   3053  int max_mis_to_decode = 0;
   3054  int tile_row_idx, tile_col_idx;
   3055  int tile_row = -1;
   3056  int tile_col = -1;
   3057 
   3058  memset(next_job_info, 0, sizeof(*next_job_info));
   3059 
   3060  // Frame decode is completed or error is encountered.
   3061  *end_of_frame = (frame_row_mt_info->mi_rows_decode_started ==
   3062                   frame_row_mt_info->mi_rows_to_decode) ||
   3063                  (frame_row_mt_info->row_mt_exit == 1);
   3064  if (*end_of_frame) {
   3065    return 1;
   3066  }
   3067 
   3068  // Decoding cannot start as bit-stream parsing is not complete.
   3069  assert(frame_row_mt_info->mi_rows_parse_done >=
   3070         frame_row_mt_info->mi_rows_decode_started);
   3071  if (frame_row_mt_info->mi_rows_parse_done ==
   3072      frame_row_mt_info->mi_rows_decode_started)
   3073    return 0;
   3074 
   3075  // Choose the tile to decode.
   3076  for (tile_row_idx = tile_rows_start; tile_row_idx < tile_rows_end;
   3077       ++tile_row_idx) {
   3078    for (tile_col_idx = tile_cols_start; tile_col_idx < tile_cols_end;
   3079         ++tile_col_idx) {
   3080      if (tile_row_idx * cm->tiles.cols + tile_col_idx < start_tile ||
   3081          tile_row_idx * cm->tiles.cols + tile_col_idx > end_tile)
   3082        continue;
   3083 
   3084      tile_data = pbi->tile_data + tile_row_idx * cm->tiles.cols + tile_col_idx;
   3085      dec_row_mt_sync = &tile_data->dec_row_mt_sync;
   3086 
   3087      num_threads_working = dec_row_mt_sync->num_threads_working;
   3088      num_mis_waiting_for_decode = (dec_row_mt_sync->mi_rows_parse_done -
   3089                                    dec_row_mt_sync->mi_rows_decode_started) *
   3090                                   dec_row_mt_sync->mi_cols;
   3091      num_mis_to_decode =
   3092          (dec_row_mt_sync->mi_rows - dec_row_mt_sync->mi_rows_decode_started) *
   3093          dec_row_mt_sync->mi_cols;
   3094 
   3095      assert(num_mis_to_decode >= num_mis_waiting_for_decode);
   3096 
   3097      // Pick the tile which has minimum number of threads working on it.
   3098      if (num_mis_waiting_for_decode > 0) {
   3099        if (num_threads_working < min_threads_working) {
   3100          min_threads_working = num_threads_working;
   3101          max_mis_to_decode = 0;
   3102        }
   3103        if (num_threads_working == min_threads_working &&
   3104            num_mis_to_decode > max_mis_to_decode &&
   3105            num_threads_working <
   3106                get_max_row_mt_workers_per_tile(cm, &tile_data->tile_info)) {
   3107          max_mis_to_decode = num_mis_to_decode;
   3108          tile_row = tile_row_idx;
   3109          tile_col = tile_col_idx;
   3110        }
   3111      }
   3112    }
   3113  }
   3114  // No job found to process
   3115  if (tile_row == -1 || tile_col == -1) return 0;
   3116 
   3117  tile_data = pbi->tile_data + tile_row * cm->tiles.cols + tile_col;
   3118  dec_row_mt_sync = &tile_data->dec_row_mt_sync;
   3119 
   3120  next_job_info->tile_row = tile_row;
   3121  next_job_info->tile_col = tile_col;
   3122  next_job_info->mi_row = dec_row_mt_sync->mi_rows_decode_started +
   3123                          tile_data->tile_info.mi_row_start;
   3124 
   3125  dec_row_mt_sync->num_threads_working++;
   3126  dec_row_mt_sync->mi_rows_decode_started += sb_mi_size;
   3127  frame_row_mt_info->mi_rows_decode_started += sb_mi_size;
   3128  assert(frame_row_mt_info->mi_rows_parse_done >=
   3129         frame_row_mt_info->mi_rows_decode_started);
   3130 #if CONFIG_MULTITHREAD
   3131  if (frame_row_mt_info->mi_rows_decode_started ==
   3132      frame_row_mt_info->mi_rows_to_decode) {
   3133    pthread_cond_broadcast(pbi->row_mt_cond_);
   3134  }
   3135 #endif
   3136 
   3137  return 1;
   3138 }
   3139 
   3140 static inline void signal_parse_sb_row_done(AV1Decoder *const pbi,
   3141                                            TileDataDec *const tile_data,
   3142                                            const int sb_mi_size) {
   3143  AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
   3144 #if CONFIG_MULTITHREAD
   3145  pthread_mutex_lock(pbi->row_mt_mutex_);
   3146 #endif
   3147  assert(frame_row_mt_info->mi_rows_parse_done >=
   3148         frame_row_mt_info->mi_rows_decode_started);
   3149  tile_data->dec_row_mt_sync.mi_rows_parse_done += sb_mi_size;
   3150  frame_row_mt_info->mi_rows_parse_done += sb_mi_size;
   3151 #if CONFIG_MULTITHREAD
   3152  // A new decode job is available. Wake up one worker thread to handle the
   3153  // new decode job.
   3154  // NOTE: This assumes we bump mi_rows_parse_done and mi_rows_decode_started
   3155  // by the same increment (sb_mi_size).
   3156  pthread_cond_signal(pbi->row_mt_cond_);
   3157  pthread_mutex_unlock(pbi->row_mt_mutex_);
   3158 #endif
   3159 }
   3160 
   3161 // This function is very similar to decode_tile(). It would be good to figure
   3162 // out how to share code.
   3163 static inline void parse_tile_row_mt(AV1Decoder *pbi, ThreadData *const td,
   3164                                     TileDataDec *const tile_data) {
   3165  AV1_COMMON *const cm = &pbi->common;
   3166  const int sb_mi_size = mi_size_wide[cm->seq_params->sb_size];
   3167  const int num_planes = av1_num_planes(cm);
   3168  const TileInfo *const tile_info = &tile_data->tile_info;
   3169  int tile_row = tile_info->tile_row;
   3170  DecoderCodingBlock *const dcb = &td->dcb;
   3171  MACROBLOCKD *const xd = &dcb->xd;
   3172 
   3173  av1_zero_above_context(cm, xd, tile_info->mi_col_start, tile_info->mi_col_end,
   3174                         tile_row);
   3175  av1_reset_loop_filter_delta(xd, num_planes);
   3176  av1_reset_loop_restoration(xd, num_planes);
   3177 
   3178  for (int mi_row = tile_info->mi_row_start; mi_row < tile_info->mi_row_end;
   3179       mi_row += cm->seq_params->mib_size) {
   3180    av1_zero_left_context(xd);
   3181 
   3182    for (int mi_col = tile_info->mi_col_start; mi_col < tile_info->mi_col_end;
   3183         mi_col += cm->seq_params->mib_size) {
   3184      set_cb_buffer(pbi, dcb, pbi->cb_buffer_base, num_planes, mi_row, mi_col);
   3185 
   3186      // Bit-stream parsing of the superblock
   3187      decode_partition(pbi, td, mi_row, mi_col, td->bit_reader,
   3188                       cm->seq_params->sb_size, 0x1);
   3189 
   3190      if (aom_reader_has_overflowed(td->bit_reader)) {
   3191        aom_merge_corrupted_flag(&dcb->corrupted, 1);
   3192        return;
   3193      }
   3194    }
   3195    signal_parse_sb_row_done(pbi, tile_data, sb_mi_size);
   3196  }
   3197 
   3198  int corrupted =
   3199      (check_trailing_bits_after_symbol_coder(td->bit_reader)) ? 1 : 0;
   3200  aom_merge_corrupted_flag(&dcb->corrupted, corrupted);
   3201 }
   3202 
   3203 static int row_mt_worker_hook(void *arg1, void *arg2) {
   3204  DecWorkerData *const thread_data = (DecWorkerData *)arg1;
   3205  AV1Decoder *const pbi = (AV1Decoder *)arg2;
   3206  ThreadData *const td = thread_data->td;
   3207  uint8_t allow_update_cdf;
   3208  AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
   3209  td->dcb.corrupted = 0;
   3210 
   3211  // The jmp_buf is valid only for the duration of the function that calls
   3212  // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
   3213  // before it returns.
   3214  if (setjmp(thread_data->error_info.jmp)) {
   3215    thread_data->error_info.setjmp = 0;
   3216    thread_data->td->dcb.corrupted = 1;
   3217 #if CONFIG_MULTITHREAD
   3218    pthread_mutex_lock(pbi->row_mt_mutex_);
   3219 #endif
   3220    frame_row_mt_info->row_mt_exit = 1;
   3221 #if CONFIG_MULTITHREAD
   3222    pthread_cond_broadcast(pbi->row_mt_cond_);
   3223    pthread_mutex_unlock(pbi->row_mt_mutex_);
   3224 #endif
   3225    // If any SB row (erroneous row) processed by a thread encounters an
   3226    // internal error, there is a need to indicate other threads that decoding
   3227    // of the erroneous row is complete. This ensures that other threads which
   3228    // wait upon the completion of SB's present in erroneous row are not waiting
   3229    // indefinitely.
   3230    signal_decoding_done_for_erroneous_row(pbi, &thread_data->td->dcb.xd);
   3231    return 0;
   3232  }
   3233  thread_data->error_info.setjmp = 1;
   3234 
   3235  AV1_COMMON *cm = &pbi->common;
   3236  allow_update_cdf = cm->tiles.large_scale ? 0 : 1;
   3237  allow_update_cdf = allow_update_cdf && !cm->features.disable_cdf_update;
   3238 
   3239  set_decode_func_pointers(td, 0x1);
   3240 
   3241  assert(cm->tiles.cols > 0);
   3242  while (!td->dcb.corrupted) {
   3243    TileJobsDec *cur_job_info = get_dec_job_info(&pbi->tile_mt_info);
   3244 
   3245    if (cur_job_info != NULL) {
   3246      const TileBufferDec *const tile_buffer = cur_job_info->tile_buffer;
   3247      TileDataDec *const tile_data = cur_job_info->tile_data;
   3248      tile_worker_hook_init(pbi, thread_data, tile_buffer, tile_data,
   3249                            allow_update_cdf);
   3250 #if CONFIG_MULTITHREAD
   3251      pthread_mutex_lock(pbi->row_mt_mutex_);
   3252 #endif
   3253      tile_data->dec_row_mt_sync.num_threads_working++;
   3254 #if CONFIG_MULTITHREAD
   3255      pthread_mutex_unlock(pbi->row_mt_mutex_);
   3256 #endif
   3257      // decode tile
   3258      parse_tile_row_mt(pbi, td, tile_data);
   3259 #if CONFIG_MULTITHREAD
   3260      pthread_mutex_lock(pbi->row_mt_mutex_);
   3261 #endif
   3262      tile_data->dec_row_mt_sync.num_threads_working--;
   3263 #if CONFIG_MULTITHREAD
   3264      pthread_mutex_unlock(pbi->row_mt_mutex_);
   3265 #endif
   3266    } else {
   3267      break;
   3268    }
   3269  }
   3270 
   3271  if (td->dcb.corrupted) {
   3272    thread_data->error_info.setjmp = 0;
   3273 #if CONFIG_MULTITHREAD
   3274    pthread_mutex_lock(pbi->row_mt_mutex_);
   3275 #endif
   3276    frame_row_mt_info->row_mt_exit = 1;
   3277 #if CONFIG_MULTITHREAD
   3278    pthread_cond_broadcast(pbi->row_mt_cond_);
   3279    pthread_mutex_unlock(pbi->row_mt_mutex_);
   3280 #endif
   3281    return 0;
   3282  }
   3283 
   3284  set_decode_func_pointers(td, 0x2);
   3285 
   3286  while (1) {
   3287    AV1DecRowMTJobInfo next_job_info;
   3288    int end_of_frame = 0;
   3289 
   3290 #if CONFIG_MULTITHREAD
   3291    pthread_mutex_lock(pbi->row_mt_mutex_);
   3292 #endif
   3293    while (!get_next_job_info(pbi, &next_job_info, &end_of_frame)) {
   3294 #if CONFIG_MULTITHREAD
   3295      pthread_cond_wait(pbi->row_mt_cond_, pbi->row_mt_mutex_);
   3296 #endif
   3297    }
   3298 #if CONFIG_MULTITHREAD
   3299    pthread_mutex_unlock(pbi->row_mt_mutex_);
   3300 #endif
   3301 
   3302    if (end_of_frame) break;
   3303 
   3304    int tile_row = next_job_info.tile_row;
   3305    int tile_col = next_job_info.tile_col;
   3306    int mi_row = next_job_info.mi_row;
   3307 
   3308    TileDataDec *tile_data =
   3309        pbi->tile_data + tile_row * cm->tiles.cols + tile_col;
   3310    AV1DecRowMTSync *dec_row_mt_sync = &tile_data->dec_row_mt_sync;
   3311 
   3312    av1_tile_init(&td->dcb.xd.tile, cm, tile_row, tile_col);
   3313    av1_init_macroblockd(cm, &td->dcb.xd);
   3314    td->dcb.xd.error_info = &thread_data->error_info;
   3315 
   3316    decode_tile_sb_row(pbi, td, &tile_data->tile_info, mi_row);
   3317 
   3318 #if CONFIG_MULTITHREAD
   3319    pthread_mutex_lock(pbi->row_mt_mutex_);
   3320 #endif
   3321    dec_row_mt_sync->num_threads_working--;
   3322 #if CONFIG_MULTITHREAD
   3323    pthread_mutex_unlock(pbi->row_mt_mutex_);
   3324 #endif
   3325  }
   3326  thread_data->error_info.setjmp = 0;
   3327  return !td->dcb.corrupted;
   3328 }
   3329 
   3330 // sorts in descending order
   3331 static int compare_tile_buffers(const void *a, const void *b) {
   3332  const TileJobsDec *const buf1 = (const TileJobsDec *)a;
   3333  const TileJobsDec *const buf2 = (const TileJobsDec *)b;
   3334  return (((int)buf2->tile_buffer->size) - ((int)buf1->tile_buffer->size));
   3335 }
   3336 
   3337 static inline void enqueue_tile_jobs(AV1Decoder *pbi, AV1_COMMON *cm,
   3338                                     int tile_rows_start, int tile_rows_end,
   3339                                     int tile_cols_start, int tile_cols_end,
   3340                                     int start_tile, int end_tile) {
   3341  AV1DecTileMT *tile_mt_info = &pbi->tile_mt_info;
   3342  TileJobsDec *tile_job_queue = tile_mt_info->job_queue;
   3343  tile_mt_info->jobs_enqueued = 0;
   3344  tile_mt_info->jobs_dequeued = 0;
   3345 
   3346  for (int row = tile_rows_start; row < tile_rows_end; row++) {
   3347    for (int col = tile_cols_start; col < tile_cols_end; col++) {
   3348      if (row * cm->tiles.cols + col < start_tile ||
   3349          row * cm->tiles.cols + col > end_tile)
   3350        continue;
   3351      tile_job_queue->tile_buffer = &pbi->tile_buffers[row][col];
   3352      tile_job_queue->tile_data = pbi->tile_data + row * cm->tiles.cols + col;
   3353      tile_job_queue++;
   3354      tile_mt_info->jobs_enqueued++;
   3355    }
   3356  }
   3357 }
   3358 
   3359 static inline void alloc_dec_jobs(AV1DecTileMT *tile_mt_info, AV1_COMMON *cm,
   3360                                  int tile_rows, int tile_cols) {
   3361  tile_mt_info->alloc_tile_rows = tile_rows;
   3362  tile_mt_info->alloc_tile_cols = tile_cols;
   3363  int num_tiles = tile_rows * tile_cols;
   3364 #if CONFIG_MULTITHREAD
   3365  {
   3366    CHECK_MEM_ERROR(cm, tile_mt_info->job_mutex,
   3367                    aom_malloc(sizeof(*tile_mt_info->job_mutex) * num_tiles));
   3368 
   3369    for (int i = 0; i < num_tiles; i++) {
   3370      pthread_mutex_init(&tile_mt_info->job_mutex[i], NULL);
   3371    }
   3372  }
   3373 #endif
   3374  CHECK_MEM_ERROR(cm, tile_mt_info->job_queue,
   3375                  aom_malloc(sizeof(*tile_mt_info->job_queue) * num_tiles));
   3376 }
   3377 
   3378 void av1_free_mc_tmp_buf(ThreadData *thread_data) {
   3379  int ref;
   3380  for (ref = 0; ref < 2; ref++) {
   3381    if (thread_data->mc_buf_use_highbd)
   3382      aom_free(CONVERT_TO_SHORTPTR(thread_data->mc_buf[ref]));
   3383    else
   3384      aom_free(thread_data->mc_buf[ref]);
   3385    thread_data->mc_buf[ref] = NULL;
   3386  }
   3387  thread_data->mc_buf_size = 0;
   3388  thread_data->mc_buf_use_highbd = 0;
   3389 
   3390  aom_free(thread_data->tmp_conv_dst);
   3391  thread_data->tmp_conv_dst = NULL;
   3392  aom_free(thread_data->seg_mask);
   3393  thread_data->seg_mask = NULL;
   3394  for (int i = 0; i < 2; ++i) {
   3395    aom_free(thread_data->tmp_obmc_bufs[i]);
   3396    thread_data->tmp_obmc_bufs[i] = NULL;
   3397  }
   3398 }
   3399 
   3400 static inline void allocate_mc_tmp_buf(AV1_COMMON *const cm,
   3401                                       ThreadData *thread_data, int buf_size,
   3402                                       int use_highbd) {
   3403  for (int ref = 0; ref < 2; ref++) {
   3404    // The mc_buf/hbd_mc_buf must be zeroed to fix a intermittent valgrind error
   3405    // 'Conditional jump or move depends on uninitialised value' from the loop
   3406    // filter. Uninitialized reads in convolve function (e.g. horiz_4tap path in
   3407    // av1_convolve_2d_sr_avx2()) from mc_buf/hbd_mc_buf are seen to be the
   3408    // potential reason for this issue.
   3409    if (use_highbd) {
   3410      uint16_t *hbd_mc_buf;
   3411      CHECK_MEM_ERROR(cm, hbd_mc_buf, (uint16_t *)aom_memalign(16, buf_size));
   3412      memset(hbd_mc_buf, 0, buf_size);
   3413      thread_data->mc_buf[ref] = CONVERT_TO_BYTEPTR(hbd_mc_buf);
   3414    } else {
   3415      CHECK_MEM_ERROR(cm, thread_data->mc_buf[ref],
   3416                      (uint8_t *)aom_memalign(16, buf_size));
   3417      memset(thread_data->mc_buf[ref], 0, buf_size);
   3418    }
   3419  }
   3420  thread_data->mc_buf_size = buf_size;
   3421  thread_data->mc_buf_use_highbd = use_highbd;
   3422 
   3423  CHECK_MEM_ERROR(cm, thread_data->tmp_conv_dst,
   3424                  aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE *
   3425                                       sizeof(*thread_data->tmp_conv_dst)));
   3426  CHECK_MEM_ERROR(cm, thread_data->seg_mask,
   3427                  (uint8_t *)aom_memalign(
   3428                      16, 2 * MAX_SB_SQUARE * sizeof(*thread_data->seg_mask)));
   3429 
   3430  for (int i = 0; i < 2; ++i) {
   3431    CHECK_MEM_ERROR(
   3432        cm, thread_data->tmp_obmc_bufs[i],
   3433        aom_memalign(16, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
   3434                             sizeof(*thread_data->tmp_obmc_bufs[i])));
   3435  }
   3436 }
   3437 
   3438 static inline void reset_dec_workers(AV1Decoder *pbi, AVxWorkerHook worker_hook,
   3439                                     int num_workers) {
   3440  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
   3441 
   3442  // Reset tile decoding hook
   3443  for (int worker_idx = 0; worker_idx < num_workers; ++worker_idx) {
   3444    AVxWorker *const worker = &pbi->tile_workers[worker_idx];
   3445    DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
   3446    thread_data->td->dcb = pbi->dcb;
   3447    thread_data->td->dcb.corrupted = 0;
   3448    thread_data->td->dcb.mc_buf[0] = thread_data->td->mc_buf[0];
   3449    thread_data->td->dcb.mc_buf[1] = thread_data->td->mc_buf[1];
   3450    thread_data->td->dcb.xd.tmp_conv_dst = thread_data->td->tmp_conv_dst;
   3451    if (worker_idx)
   3452      thread_data->td->dcb.xd.seg_mask = thread_data->td->seg_mask;
   3453    for (int j = 0; j < 2; ++j) {
   3454      thread_data->td->dcb.xd.tmp_obmc_bufs[j] =
   3455          thread_data->td->tmp_obmc_bufs[j];
   3456    }
   3457    winterface->sync(worker);
   3458 
   3459    worker->hook = worker_hook;
   3460    worker->data1 = thread_data;
   3461    worker->data2 = pbi;
   3462  }
   3463 #if CONFIG_ACCOUNTING
   3464  if (pbi->acct_enabled) {
   3465    aom_accounting_reset(&pbi->accounting);
   3466  }
   3467 #endif
   3468 }
   3469 
   3470 static inline void launch_dec_workers(AV1Decoder *pbi, const uint8_t *data_end,
   3471                                      int num_workers) {
   3472  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
   3473 
   3474  for (int worker_idx = num_workers - 1; worker_idx >= 0; --worker_idx) {
   3475    AVxWorker *const worker = &pbi->tile_workers[worker_idx];
   3476    DecWorkerData *const thread_data = (DecWorkerData *)worker->data1;
   3477 
   3478    thread_data->data_end = data_end;
   3479 
   3480    worker->had_error = 0;
   3481    if (worker_idx == 0) {
   3482      winterface->execute(worker);
   3483    } else {
   3484      winterface->launch(worker);
   3485    }
   3486  }
   3487 }
   3488 
   3489 static inline void sync_dec_workers(AV1Decoder *pbi, int num_workers) {
   3490  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
   3491  int corrupted = 0;
   3492 
   3493  for (int worker_idx = num_workers; worker_idx > 0; --worker_idx) {
   3494    AVxWorker *const worker = &pbi->tile_workers[worker_idx - 1];
   3495    aom_merge_corrupted_flag(&corrupted, !winterface->sync(worker));
   3496  }
   3497 
   3498  pbi->dcb.corrupted = corrupted;
   3499 }
   3500 
   3501 static inline void decode_mt_init(AV1Decoder *pbi) {
   3502  AV1_COMMON *const cm = &pbi->common;
   3503  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
   3504  int worker_idx;
   3505 
   3506  // Create workers and thread_data
   3507  if (pbi->num_workers == 0) {
   3508    const int num_threads = pbi->max_threads;
   3509    CHECK_MEM_ERROR(cm, pbi->tile_workers,
   3510                    aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
   3511    CHECK_MEM_ERROR(cm, pbi->thread_data,
   3512                    aom_calloc(num_threads, sizeof(*pbi->thread_data)));
   3513 
   3514    for (worker_idx = 0; worker_idx < num_threads; ++worker_idx) {
   3515      AVxWorker *const worker = &pbi->tile_workers[worker_idx];
   3516      DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
   3517 
   3518      winterface->init(worker);
   3519      worker->thread_name = "aom tile worker";
   3520      if (worker_idx != 0 && !winterface->reset(worker)) {
   3521        aom_internal_error(&pbi->error, AOM_CODEC_ERROR,
   3522                           "Tile decoder thread creation failed");
   3523      }
   3524      ++pbi->num_workers;
   3525 
   3526      if (worker_idx != 0) {
   3527        // Allocate thread data.
   3528        CHECK_MEM_ERROR(cm, thread_data->td,
   3529                        aom_memalign(32, sizeof(*thread_data->td)));
   3530        av1_zero(*thread_data->td);
   3531      } else {
   3532        // Main thread acts as a worker and uses the thread data in pbi
   3533        thread_data->td = &pbi->td;
   3534      }
   3535      thread_data->error_info.error_code = AOM_CODEC_OK;
   3536      thread_data->error_info.setjmp = 0;
   3537    }
   3538  }
   3539  const int use_highbd = cm->seq_params->use_highbitdepth;
   3540  const int buf_size = MC_TEMP_BUF_PELS << use_highbd;
   3541  for (worker_idx = 1; worker_idx < pbi->max_threads; ++worker_idx) {
   3542    DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
   3543    if (thread_data->td->mc_buf_size != buf_size) {
   3544      av1_free_mc_tmp_buf(thread_data->td);
   3545      allocate_mc_tmp_buf(cm, thread_data->td, buf_size, use_highbd);
   3546    }
   3547  }
   3548 }
   3549 
   3550 static inline void tile_mt_queue(AV1Decoder *pbi, int tile_cols, int tile_rows,
   3551                                 int tile_rows_start, int tile_rows_end,
   3552                                 int tile_cols_start, int tile_cols_end,
   3553                                 int start_tile, int end_tile) {
   3554  AV1_COMMON *const cm = &pbi->common;
   3555  if (pbi->tile_mt_info.alloc_tile_cols != tile_cols ||
   3556      pbi->tile_mt_info.alloc_tile_rows != tile_rows) {
   3557    av1_dealloc_dec_jobs(&pbi->tile_mt_info);
   3558    alloc_dec_jobs(&pbi->tile_mt_info, cm, tile_rows, tile_cols);
   3559  }
   3560  enqueue_tile_jobs(pbi, cm, tile_rows_start, tile_rows_end, tile_cols_start,
   3561                    tile_cols_end, start_tile, end_tile);
   3562  qsort(pbi->tile_mt_info.job_queue, pbi->tile_mt_info.jobs_enqueued,
   3563        sizeof(pbi->tile_mt_info.job_queue[0]), compare_tile_buffers);
   3564 }
   3565 
   3566 static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
   3567                                      const uint8_t *data_end, int start_tile,
   3568                                      int end_tile) {
   3569  AV1_COMMON *const cm = &pbi->common;
   3570  CommonTileParams *const tiles = &cm->tiles;
   3571  const int tile_cols = tiles->cols;
   3572  const int tile_rows = tiles->rows;
   3573  const int n_tiles = tile_cols * tile_rows;
   3574  TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
   3575  const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
   3576  const int single_row = pbi->dec_tile_row >= 0;
   3577  const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
   3578  const int single_col = pbi->dec_tile_col >= 0;
   3579  int tile_rows_start;
   3580  int tile_rows_end;
   3581  int tile_cols_start;
   3582  int tile_cols_end;
   3583  int tile_count_tg;
   3584  int num_workers;
   3585  const uint8_t *raw_data_end = NULL;
   3586 
   3587  if (tiles->large_scale) {
   3588    tile_rows_start = single_row ? dec_tile_row : 0;
   3589    tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
   3590    tile_cols_start = single_col ? dec_tile_col : 0;
   3591    tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
   3592  } else {
   3593    tile_rows_start = 0;
   3594    tile_rows_end = tile_rows;
   3595    tile_cols_start = 0;
   3596    tile_cols_end = tile_cols;
   3597  }
   3598  tile_count_tg = end_tile - start_tile + 1;
   3599  num_workers = AOMMIN(pbi->max_threads, tile_count_tg);
   3600 
   3601  // No tiles to decode.
   3602  if (tile_rows_end <= tile_rows_start || tile_cols_end <= tile_cols_start ||
   3603      // First tile is larger than end_tile.
   3604      tile_rows_start * tile_cols + tile_cols_start > end_tile ||
   3605      // Last tile is smaller than start_tile.
   3606      (tile_rows_end - 1) * tile_cols + tile_cols_end - 1 < start_tile)
   3607    return data;
   3608 
   3609  assert(tile_rows <= MAX_TILE_ROWS);
   3610  assert(tile_cols <= MAX_TILE_COLS);
   3611  assert(tile_count_tg > 0);
   3612  assert(num_workers > 0);
   3613  assert(start_tile <= end_tile);
   3614  assert(start_tile >= 0 && end_tile < n_tiles);
   3615 
   3616  decode_mt_init(pbi);
   3617 
   3618  // get tile size in tile group
   3619 #if EXT_TILE_DEBUG
   3620  if (tiles->large_scale) assert(pbi->ext_tile_debug == 1);
   3621  if (tiles->large_scale)
   3622    raw_data_end = get_ls_tile_buffers(pbi, data, data_end, tile_buffers);
   3623  else
   3624 #endif  // EXT_TILE_DEBUG
   3625    get_tile_buffers(pbi, data, data_end, tile_buffers, start_tile, end_tile);
   3626 
   3627  if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
   3628    decoder_alloc_tile_data(pbi, n_tiles);
   3629  }
   3630  if (pbi->dcb.xd.seg_mask == NULL)
   3631    CHECK_MEM_ERROR(cm, pbi->dcb.xd.seg_mask,
   3632                    (uint8_t *)aom_memalign(
   3633                        16, 2 * MAX_SB_SQUARE * sizeof(*pbi->dcb.xd.seg_mask)));
   3634 
   3635  for (int row = 0; row < tile_rows; row++) {
   3636    for (int col = 0; col < tile_cols; col++) {
   3637      TileDataDec *tile_data = pbi->tile_data + row * tiles->cols + col;
   3638      av1_tile_init(&tile_data->tile_info, cm, row, col);
   3639    }
   3640  }
   3641 
   3642  tile_mt_queue(pbi, tile_cols, tile_rows, tile_rows_start, tile_rows_end,
   3643                tile_cols_start, tile_cols_end, start_tile, end_tile);
   3644 
   3645  reset_dec_workers(pbi, tile_worker_hook, num_workers);
   3646  launch_dec_workers(pbi, data_end, num_workers);
   3647  sync_dec_workers(pbi, num_workers);
   3648 
   3649  if (pbi->dcb.corrupted)
   3650    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   3651                       "Failed to decode tile data");
   3652 
   3653  if (tiles->large_scale) {
   3654    if (n_tiles == 1) {
   3655      // Find the end of the single tile buffer
   3656      return aom_reader_find_end(&pbi->tile_data->bit_reader);
   3657    }
   3658    // Return the end of the last tile buffer
   3659    return raw_data_end;
   3660  }
   3661  TileDataDec *const tile_data = pbi->tile_data + end_tile;
   3662 
   3663  return aom_reader_find_end(&tile_data->bit_reader);
   3664 }
   3665 
   3666 static inline void dec_alloc_cb_buf(AV1Decoder *pbi) {
   3667  AV1_COMMON *const cm = &pbi->common;
   3668  int size = ((cm->mi_params.mi_rows >> cm->seq_params->mib_size_log2) + 1) *
   3669             ((cm->mi_params.mi_cols >> cm->seq_params->mib_size_log2) + 1);
   3670 
   3671  if (pbi->cb_buffer_alloc_size < size) {
   3672    av1_dec_free_cb_buf(pbi);
   3673    CHECK_MEM_ERROR(cm, pbi->cb_buffer_base,
   3674                    aom_memalign(32, sizeof(*pbi->cb_buffer_base) * size));
   3675    memset(pbi->cb_buffer_base, 0, sizeof(*pbi->cb_buffer_base) * size);
   3676    pbi->cb_buffer_alloc_size = size;
   3677  }
   3678 }
   3679 
   3680 static inline void row_mt_frame_init(AV1Decoder *pbi, int tile_rows_start,
   3681                                     int tile_rows_end, int tile_cols_start,
   3682                                     int tile_cols_end, int start_tile,
   3683                                     int end_tile, int max_sb_rows) {
   3684  AV1_COMMON *const cm = &pbi->common;
   3685  AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
   3686 
   3687  frame_row_mt_info->tile_rows_start = tile_rows_start;
   3688  frame_row_mt_info->tile_rows_end = tile_rows_end;
   3689  frame_row_mt_info->tile_cols_start = tile_cols_start;
   3690  frame_row_mt_info->tile_cols_end = tile_cols_end;
   3691  frame_row_mt_info->start_tile = start_tile;
   3692  frame_row_mt_info->end_tile = end_tile;
   3693  frame_row_mt_info->mi_rows_to_decode = 0;
   3694  frame_row_mt_info->mi_rows_parse_done = 0;
   3695  frame_row_mt_info->mi_rows_decode_started = 0;
   3696  frame_row_mt_info->row_mt_exit = 0;
   3697 
   3698  for (int tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
   3699    for (int tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
   3700      if (tile_row * cm->tiles.cols + tile_col < start_tile ||
   3701          tile_row * cm->tiles.cols + tile_col > end_tile)
   3702        continue;
   3703 
   3704      TileDataDec *const tile_data =
   3705          pbi->tile_data + tile_row * cm->tiles.cols + tile_col;
   3706      const TileInfo *const tile_info = &tile_data->tile_info;
   3707 
   3708      tile_data->dec_row_mt_sync.mi_rows_parse_done = 0;
   3709      tile_data->dec_row_mt_sync.mi_rows_decode_started = 0;
   3710      tile_data->dec_row_mt_sync.num_threads_working = 0;
   3711      tile_data->dec_row_mt_sync.mi_rows =
   3712          ALIGN_POWER_OF_TWO(tile_info->mi_row_end - tile_info->mi_row_start,
   3713                             cm->seq_params->mib_size_log2);
   3714      tile_data->dec_row_mt_sync.mi_cols =
   3715          ALIGN_POWER_OF_TWO(tile_info->mi_col_end - tile_info->mi_col_start,
   3716                             cm->seq_params->mib_size_log2);
   3717      tile_data->dec_row_mt_sync.intrabc_extra_top_right_sb_delay =
   3718          av1_get_intrabc_extra_top_right_sb_delay(cm);
   3719 
   3720      frame_row_mt_info->mi_rows_to_decode +=
   3721          tile_data->dec_row_mt_sync.mi_rows;
   3722 
   3723      // Initialize cur_sb_col to -1 for all SB rows.
   3724      memset(tile_data->dec_row_mt_sync.cur_sb_col, -1,
   3725             sizeof(*tile_data->dec_row_mt_sync.cur_sb_col) * max_sb_rows);
   3726    }
   3727  }
   3728 
   3729 #if CONFIG_MULTITHREAD
   3730  if (pbi->row_mt_mutex_ == NULL) {
   3731    CHECK_MEM_ERROR(cm, pbi->row_mt_mutex_,
   3732                    aom_malloc(sizeof(*(pbi->row_mt_mutex_))));
   3733    if (pbi->row_mt_mutex_) {
   3734      pthread_mutex_init(pbi->row_mt_mutex_, NULL);
   3735    }
   3736  }
   3737 
   3738  if (pbi->row_mt_cond_ == NULL) {
   3739    CHECK_MEM_ERROR(cm, pbi->row_mt_cond_,
   3740                    aom_malloc(sizeof(*(pbi->row_mt_cond_))));
   3741    if (pbi->row_mt_cond_) {
   3742      pthread_cond_init(pbi->row_mt_cond_, NULL);
   3743    }
   3744  }
   3745 #endif
   3746 }
   3747 
   3748 static const uint8_t *decode_tiles_row_mt(AV1Decoder *pbi, const uint8_t *data,
   3749                                          const uint8_t *data_end,
   3750                                          int start_tile, int end_tile) {
   3751  AV1_COMMON *const cm = &pbi->common;
   3752  CommonTileParams *const tiles = &cm->tiles;
   3753  const int tile_cols = tiles->cols;
   3754  const int tile_rows = tiles->rows;
   3755  const int n_tiles = tile_cols * tile_rows;
   3756  TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
   3757  const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
   3758  const int single_row = pbi->dec_tile_row >= 0;
   3759  const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
   3760  const int single_col = pbi->dec_tile_col >= 0;
   3761  int tile_rows_start;
   3762  int tile_rows_end;
   3763  int tile_cols_start;
   3764  int tile_cols_end;
   3765  int tile_count_tg;
   3766  int num_workers = 0;
   3767  int max_threads;
   3768  const uint8_t *raw_data_end = NULL;
   3769  int max_sb_rows = 0;
   3770 
   3771  if (tiles->large_scale) {
   3772    tile_rows_start = single_row ? dec_tile_row : 0;
   3773    tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
   3774    tile_cols_start = single_col ? dec_tile_col : 0;
   3775    tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
   3776  } else {
   3777    tile_rows_start = 0;
   3778    tile_rows_end = tile_rows;
   3779    tile_cols_start = 0;
   3780    tile_cols_end = tile_cols;
   3781  }
   3782  tile_count_tg = end_tile - start_tile + 1;
   3783  max_threads = pbi->max_threads;
   3784 
   3785  // No tiles to decode.
   3786  if (tile_rows_end <= tile_rows_start || tile_cols_end <= tile_cols_start ||
   3787      // First tile is larger than end_tile.
   3788      tile_rows_start * tile_cols + tile_cols_start > end_tile ||
   3789      // Last tile is smaller than start_tile.
   3790      (tile_rows_end - 1) * tile_cols + tile_cols_end - 1 < start_tile)
   3791    return data;
   3792 
   3793  assert(tile_rows <= MAX_TILE_ROWS);
   3794  assert(tile_cols <= MAX_TILE_COLS);
   3795  assert(tile_count_tg > 0);
   3796  assert(max_threads > 0);
   3797  assert(start_tile <= end_tile);
   3798  assert(start_tile >= 0 && end_tile < n_tiles);
   3799 
   3800  (void)tile_count_tg;
   3801 
   3802  decode_mt_init(pbi);
   3803 
   3804  // get tile size in tile group
   3805 #if EXT_TILE_DEBUG
   3806  if (tiles->large_scale) assert(pbi->ext_tile_debug == 1);
   3807  if (tiles->large_scale)
   3808    raw_data_end = get_ls_tile_buffers(pbi, data, data_end, tile_buffers);
   3809  else
   3810 #endif  // EXT_TILE_DEBUG
   3811    get_tile_buffers(pbi, data, data_end, tile_buffers, start_tile, end_tile);
   3812 
   3813  if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
   3814    if (pbi->tile_data != NULL) {
   3815      for (int i = 0; i < pbi->allocated_tiles; i++) {
   3816        TileDataDec *const tile_data = pbi->tile_data + i;
   3817        av1_dec_row_mt_dealloc(&tile_data->dec_row_mt_sync);
   3818      }
   3819    }
   3820    decoder_alloc_tile_data(pbi, n_tiles);
   3821  }
   3822  if (pbi->dcb.xd.seg_mask == NULL)
   3823    CHECK_MEM_ERROR(cm, pbi->dcb.xd.seg_mask,
   3824                    (uint8_t *)aom_memalign(
   3825                        16, 2 * MAX_SB_SQUARE * sizeof(*pbi->dcb.xd.seg_mask)));
   3826 
   3827  for (int row = 0; row < tile_rows; row++) {
   3828    for (int col = 0; col < tile_cols; col++) {
   3829      TileDataDec *tile_data = pbi->tile_data + row * tiles->cols + col;
   3830      av1_tile_init(&tile_data->tile_info, cm, row, col);
   3831 
   3832      max_sb_rows = AOMMAX(max_sb_rows,
   3833                           av1_get_sb_rows_in_tile(cm, &tile_data->tile_info));
   3834      num_workers += get_max_row_mt_workers_per_tile(cm, &tile_data->tile_info);
   3835    }
   3836  }
   3837  num_workers = AOMMIN(num_workers, max_threads);
   3838 
   3839  if (pbi->allocated_row_mt_sync_rows != max_sb_rows) {
   3840    for (int i = 0; i < n_tiles; ++i) {
   3841      TileDataDec *const tile_data = pbi->tile_data + i;
   3842      av1_dec_row_mt_dealloc(&tile_data->dec_row_mt_sync);
   3843      dec_row_mt_alloc(&tile_data->dec_row_mt_sync, cm, max_sb_rows);
   3844    }
   3845    pbi->allocated_row_mt_sync_rows = max_sb_rows;
   3846  }
   3847 
   3848  tile_mt_queue(pbi, tile_cols, tile_rows, tile_rows_start, tile_rows_end,
   3849                tile_cols_start, tile_cols_end, start_tile, end_tile);
   3850 
   3851  dec_alloc_cb_buf(pbi);
   3852 
   3853  row_mt_frame_init(pbi, tile_rows_start, tile_rows_end, tile_cols_start,
   3854                    tile_cols_end, start_tile, end_tile, max_sb_rows);
   3855 
   3856  reset_dec_workers(pbi, row_mt_worker_hook, num_workers);
   3857  launch_dec_workers(pbi, data_end, num_workers);
   3858  sync_dec_workers(pbi, num_workers);
   3859 
   3860  if (pbi->dcb.corrupted)
   3861    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   3862                       "Failed to decode tile data");
   3863 
   3864  if (tiles->large_scale) {
   3865    if (n_tiles == 1) {
   3866      // Find the end of the single tile buffer
   3867      return aom_reader_find_end(&pbi->tile_data->bit_reader);
   3868    }
   3869    // Return the end of the last tile buffer
   3870    return raw_data_end;
   3871  }
   3872  TileDataDec *const tile_data = pbi->tile_data + end_tile;
   3873 
   3874  return aom_reader_find_end(&tile_data->bit_reader);
   3875 }
   3876 
   3877 static inline void error_handler(void *data) {
   3878  AV1_COMMON *const cm = (AV1_COMMON *)data;
   3879  aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
   3880 }
   3881 
   3882 // Reads the high_bitdepth and twelve_bit fields in color_config() and sets
   3883 // seq_params->bit_depth based on the values of those fields and
   3884 // seq_params->profile. Reports errors by calling rb->error_handler() or
   3885 // aom_internal_error().
   3886 static inline void read_bitdepth(struct aom_read_bit_buffer *rb,
   3887                                 SequenceHeader *seq_params,
   3888                                 struct aom_internal_error_info *error_info) {
   3889  const int high_bitdepth = aom_rb_read_bit(rb);
   3890  if (seq_params->profile == PROFILE_2 && high_bitdepth) {
   3891    const int twelve_bit = aom_rb_read_bit(rb);
   3892    seq_params->bit_depth = twelve_bit ? AOM_BITS_12 : AOM_BITS_10;
   3893  } else if (seq_params->profile <= PROFILE_2) {
   3894    seq_params->bit_depth = high_bitdepth ? AOM_BITS_10 : AOM_BITS_8;
   3895  } else {
   3896    aom_internal_error(error_info, AOM_CODEC_UNSUP_BITSTREAM,
   3897                       "Unsupported profile/bit-depth combination");
   3898  }
   3899 #if !CONFIG_AV1_HIGHBITDEPTH
   3900  if (seq_params->bit_depth > AOM_BITS_8) {
   3901    aom_internal_error(error_info, AOM_CODEC_UNSUP_BITSTREAM,
   3902                       "Bit-depth %d not supported", seq_params->bit_depth);
   3903  }
   3904 #endif
   3905 }
   3906 
   3907 static void read_film_grain_params(AV1_COMMON *cm,
   3908                                   struct aom_read_bit_buffer *rb) {
   3909  aom_film_grain_t *pars = &cm->film_grain_params;
   3910  const SequenceHeader *const seq_params = cm->seq_params;
   3911 
   3912  pars->apply_grain = aom_rb_read_bit(rb);
   3913  if (!pars->apply_grain) {
   3914    memset(pars, 0, sizeof(*pars));
   3915    return;
   3916  }
   3917 
   3918  pars->random_seed = aom_rb_read_literal(rb, 16);
   3919  if (cm->current_frame.frame_type == INTER_FRAME)
   3920    pars->update_parameters = aom_rb_read_bit(rb);
   3921  else
   3922    pars->update_parameters = 1;
   3923 
   3924  pars->bit_depth = seq_params->bit_depth;
   3925 
   3926  if (!pars->update_parameters) {
   3927    // inherit parameters from a previous reference frame
   3928    int film_grain_params_ref_idx = aom_rb_read_literal(rb, 3);
   3929    // Section 6.8.20: It is a requirement of bitstream conformance that
   3930    // film_grain_params_ref_idx is equal to ref_frame_idx[ j ] for some value
   3931    // of j in the range 0 to REFS_PER_FRAME - 1.
   3932    int found = 0;
   3933    for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
   3934      if (film_grain_params_ref_idx == cm->remapped_ref_idx[i]) {
   3935        found = 1;
   3936        break;
   3937      }
   3938    }
   3939    if (!found) {
   3940      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
   3941                         "Invalid film grain reference idx %d. ref_frame_idx = "
   3942                         "{%d, %d, %d, %d, %d, %d, %d}",
   3943                         film_grain_params_ref_idx, cm->remapped_ref_idx[0],
   3944                         cm->remapped_ref_idx[1], cm->remapped_ref_idx[2],
   3945                         cm->remapped_ref_idx[3], cm->remapped_ref_idx[4],
   3946                         cm->remapped_ref_idx[5], cm->remapped_ref_idx[6]);
   3947    }
   3948    RefCntBuffer *const buf = cm->ref_frame_map[film_grain_params_ref_idx];
   3949    if (buf == NULL) {
   3950      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
   3951                         "Invalid Film grain reference idx");
   3952    }
   3953    if (!buf->film_grain_params_present) {
   3954      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
   3955                         "Film grain reference parameters not available");
   3956    }
   3957    uint16_t random_seed = pars->random_seed;
   3958    *pars = buf->film_grain_params;   // inherit paramaters
   3959    pars->random_seed = random_seed;  // with new random seed
   3960    return;
   3961  }
   3962 
   3963  // Scaling functions parameters
   3964  pars->num_y_points = aom_rb_read_literal(rb, 4);  // max 14
   3965  if (pars->num_y_points > 14)
   3966    aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
   3967                       "Number of points for film grain luma scaling function "
   3968                       "exceeds the maximum value.");
   3969  for (int i = 0; i < pars->num_y_points; i++) {
   3970    pars->scaling_points_y[i][0] = aom_rb_read_literal(rb, 8);
   3971    if (i && pars->scaling_points_y[i - 1][0] >= pars->scaling_points_y[i][0])
   3972      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
   3973                         "First coordinate of the scaling function points "
   3974                         "shall be increasing.");
   3975    pars->scaling_points_y[i][1] = aom_rb_read_literal(rb, 8);
   3976  }
   3977 
   3978  if (!seq_params->monochrome)
   3979    pars->chroma_scaling_from_luma = aom_rb_read_bit(rb);
   3980  else
   3981    pars->chroma_scaling_from_luma = 0;
   3982 
   3983  if (seq_params->monochrome || pars->chroma_scaling_from_luma ||
   3984      ((seq_params->subsampling_x == 1) && (seq_params->subsampling_y == 1) &&
   3985       (pars->num_y_points == 0))) {
   3986    pars->num_cb_points = 0;
   3987    pars->num_cr_points = 0;
   3988  } else {
   3989    pars->num_cb_points = aom_rb_read_literal(rb, 4);  // max 10
   3990    if (pars->num_cb_points > 10)
   3991      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
   3992                         "Number of points for film grain cb scaling function "
   3993                         "exceeds the maximum value.");
   3994    for (int i = 0; i < pars->num_cb_points; i++) {
   3995      pars->scaling_points_cb[i][0] = aom_rb_read_literal(rb, 8);
   3996      if (i &&
   3997          pars->scaling_points_cb[i - 1][0] >= pars->scaling_points_cb[i][0])
   3998        aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
   3999                           "First coordinate of the scaling function points "
   4000                           "shall be increasing.");
   4001      pars->scaling_points_cb[i][1] = aom_rb_read_literal(rb, 8);
   4002    }
   4003 
   4004    pars->num_cr_points = aom_rb_read_literal(rb, 4);  // max 10
   4005    if (pars->num_cr_points > 10)
   4006      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
   4007                         "Number of points for film grain cr scaling function "
   4008                         "exceeds the maximum value.");
   4009    for (int i = 0; i < pars->num_cr_points; i++) {
   4010      pars->scaling_points_cr[i][0] = aom_rb_read_literal(rb, 8);
   4011      if (i &&
   4012          pars->scaling_points_cr[i - 1][0] >= pars->scaling_points_cr[i][0])
   4013        aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
   4014                           "First coordinate of the scaling function points "
   4015                           "shall be increasing.");
   4016      pars->scaling_points_cr[i][1] = aom_rb_read_literal(rb, 8);
   4017    }
   4018 
   4019    if ((seq_params->subsampling_x == 1) && (seq_params->subsampling_y == 1) &&
   4020        (((pars->num_cb_points == 0) && (pars->num_cr_points != 0)) ||
   4021         ((pars->num_cb_points != 0) && (pars->num_cr_points == 0))))
   4022      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
   4023                         "In YCbCr 4:2:0, film grain shall be applied "
   4024                         "to both chroma components or neither.");
   4025  }
   4026 
   4027  pars->scaling_shift = aom_rb_read_literal(rb, 2) + 8;  // 8 + value
   4028 
   4029  // AR coefficients
   4030  // Only sent if the corresponsing scaling function has
   4031  // more than 0 points
   4032 
   4033  pars->ar_coeff_lag = aom_rb_read_literal(rb, 2);
   4034 
   4035  int num_pos_luma = 2 * pars->ar_coeff_lag * (pars->ar_coeff_lag + 1);
   4036  int num_pos_chroma = num_pos_luma;
   4037  if (pars->num_y_points > 0) ++num_pos_chroma;
   4038 
   4039  if (pars->num_y_points)
   4040    for (int i = 0; i < num_pos_luma; i++)
   4041      pars->ar_coeffs_y[i] = aom_rb_read_literal(rb, 8) - 128;
   4042 
   4043  if (pars->num_cb_points || pars->chroma_scaling_from_luma)
   4044    for (int i = 0; i < num_pos_chroma; i++)
   4045      pars->ar_coeffs_cb[i] = aom_rb_read_literal(rb, 8) - 128;
   4046 
   4047  if (pars->num_cr_points || pars->chroma_scaling_from_luma)
   4048    for (int i = 0; i < num_pos_chroma; i++)
   4049      pars->ar_coeffs_cr[i] = aom_rb_read_literal(rb, 8) - 128;
   4050 
   4051  pars->ar_coeff_shift = aom_rb_read_literal(rb, 2) + 6;  // 6 + value
   4052 
   4053  pars->grain_scale_shift = aom_rb_read_literal(rb, 2);
   4054 
   4055  if (pars->num_cb_points) {
   4056    pars->cb_mult = aom_rb_read_literal(rb, 8);
   4057    pars->cb_luma_mult = aom_rb_read_literal(rb, 8);
   4058    pars->cb_offset = aom_rb_read_literal(rb, 9);
   4059  }
   4060 
   4061  if (pars->num_cr_points) {
   4062    pars->cr_mult = aom_rb_read_literal(rb, 8);
   4063    pars->cr_luma_mult = aom_rb_read_literal(rb, 8);
   4064    pars->cr_offset = aom_rb_read_literal(rb, 9);
   4065  }
   4066 
   4067  pars->overlap_flag = aom_rb_read_bit(rb);
   4068 
   4069  pars->clip_to_restricted_range = aom_rb_read_bit(rb);
   4070 }
   4071 
   4072 static inline void read_film_grain(AV1_COMMON *cm,
   4073                                   struct aom_read_bit_buffer *rb) {
   4074  if (cm->seq_params->film_grain_params_present &&
   4075      (cm->show_frame || cm->showable_frame)) {
   4076    read_film_grain_params(cm, rb);
   4077  } else {
   4078    memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
   4079  }
   4080  cm->film_grain_params.bit_depth = cm->seq_params->bit_depth;
   4081  cm->cur_frame->film_grain_params = cm->film_grain_params;
   4082 }
   4083 
   4084 void av1_read_color_config(struct aom_read_bit_buffer *rb,
   4085                           int allow_lowbitdepth, SequenceHeader *seq_params,
   4086                           struct aom_internal_error_info *error_info) {
   4087  read_bitdepth(rb, seq_params, error_info);
   4088 
   4089  seq_params->use_highbitdepth =
   4090      seq_params->bit_depth > AOM_BITS_8 || !allow_lowbitdepth;
   4091  // monochrome bit (not needed for PROFILE_1)
   4092  const int is_monochrome =
   4093      seq_params->profile != PROFILE_1 ? aom_rb_read_bit(rb) : 0;
   4094  seq_params->monochrome = is_monochrome;
   4095  int color_description_present_flag = aom_rb_read_bit(rb);
   4096  if (color_description_present_flag) {
   4097    seq_params->color_primaries = aom_rb_read_literal(rb, 8);
   4098    seq_params->transfer_characteristics = aom_rb_read_literal(rb, 8);
   4099    seq_params->matrix_coefficients = aom_rb_read_literal(rb, 8);
   4100  } else {
   4101    seq_params->color_primaries = AOM_CICP_CP_UNSPECIFIED;
   4102    seq_params->transfer_characteristics = AOM_CICP_TC_UNSPECIFIED;
   4103    seq_params->matrix_coefficients = AOM_CICP_MC_UNSPECIFIED;
   4104  }
   4105  if (is_monochrome) {
   4106    // [16,235] (including xvycc) vs [0,255] range
   4107    seq_params->color_range = aom_rb_read_bit(rb);
   4108    seq_params->subsampling_y = seq_params->subsampling_x = 1;
   4109    seq_params->chroma_sample_position = AOM_CSP_UNKNOWN;
   4110    seq_params->separate_uv_delta_q = 0;
   4111    return;
   4112  }
   4113  if (seq_params->color_primaries == AOM_CICP_CP_BT_709 &&
   4114      seq_params->transfer_characteristics == AOM_CICP_TC_SRGB &&
   4115      seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY) {
   4116    seq_params->subsampling_y = seq_params->subsampling_x = 0;
   4117    seq_params->color_range = 1;  // assume full color-range
   4118    if (!(seq_params->profile == PROFILE_1 ||
   4119          (seq_params->profile == PROFILE_2 &&
   4120           seq_params->bit_depth == AOM_BITS_12))) {
   4121      aom_internal_error(
   4122          error_info, AOM_CODEC_UNSUP_BITSTREAM,
   4123          "sRGB colorspace not compatible with specified profile");
   4124    }
   4125  } else {
   4126    // [16,235] (including xvycc) vs [0,255] range
   4127    seq_params->color_range = aom_rb_read_bit(rb);
   4128    if (seq_params->profile == PROFILE_0) {
   4129      // 420 only
   4130      seq_params->subsampling_x = seq_params->subsampling_y = 1;
   4131    } else if (seq_params->profile == PROFILE_1) {
   4132      // 444 only
   4133      seq_params->subsampling_x = seq_params->subsampling_y = 0;
   4134    } else {
   4135      assert(seq_params->profile == PROFILE_2);
   4136      if (seq_params->bit_depth == AOM_BITS_12) {
   4137        seq_params->subsampling_x = aom_rb_read_bit(rb);
   4138        if (seq_params->subsampling_x)
   4139          seq_params->subsampling_y = aom_rb_read_bit(rb);  // 422 or 420
   4140        else
   4141          seq_params->subsampling_y = 0;  // 444
   4142      } else {
   4143        // 422
   4144        seq_params->subsampling_x = 1;
   4145        seq_params->subsampling_y = 0;
   4146      }
   4147    }
   4148    if (seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY &&
   4149        (seq_params->subsampling_x || seq_params->subsampling_y)) {
   4150      aom_internal_error(
   4151          error_info, AOM_CODEC_UNSUP_BITSTREAM,
   4152          "Identity CICP Matrix incompatible with non 4:4:4 color sampling");
   4153    }
   4154    if (seq_params->subsampling_x && seq_params->subsampling_y) {
   4155      seq_params->chroma_sample_position = aom_rb_read_literal(rb, 2);
   4156    }
   4157  }
   4158  seq_params->separate_uv_delta_q = aom_rb_read_bit(rb);
   4159 }
   4160 
   4161 void av1_read_timing_info_header(aom_timing_info_t *timing_info,
   4162                                 struct aom_internal_error_info *error,
   4163                                 struct aom_read_bit_buffer *rb) {
   4164  timing_info->num_units_in_display_tick =
   4165      aom_rb_read_unsigned_literal(rb,
   4166                                   32);  // Number of units in a display tick
   4167  timing_info->time_scale = aom_rb_read_unsigned_literal(rb, 32);  // Time scale
   4168  if (timing_info->num_units_in_display_tick == 0 ||
   4169      timing_info->time_scale == 0) {
   4170    aom_internal_error(
   4171        error, AOM_CODEC_UNSUP_BITSTREAM,
   4172        "num_units_in_display_tick and time_scale must be greater than 0.");
   4173  }
   4174  timing_info->equal_picture_interval =
   4175      aom_rb_read_bit(rb);  // Equal picture interval bit
   4176  if (timing_info->equal_picture_interval) {
   4177    const uint32_t num_ticks_per_picture_minus_1 = aom_rb_read_uvlc(rb);
   4178    if (num_ticks_per_picture_minus_1 == UINT32_MAX) {
   4179      aom_internal_error(
   4180          error, AOM_CODEC_UNSUP_BITSTREAM,
   4181          "num_ticks_per_picture_minus_1 cannot be (1 << 32) - 1.");
   4182    }
   4183    timing_info->num_ticks_per_picture = num_ticks_per_picture_minus_1 + 1;
   4184  }
   4185 }
   4186 
   4187 void av1_read_decoder_model_info(aom_dec_model_info_t *decoder_model_info,
   4188                                 struct aom_read_bit_buffer *rb) {
   4189  decoder_model_info->encoder_decoder_buffer_delay_length =
   4190      aom_rb_read_literal(rb, 5) + 1;
   4191  decoder_model_info->num_units_in_decoding_tick =
   4192      aom_rb_read_unsigned_literal(rb,
   4193                                   32);  // Number of units in a decoding tick
   4194  decoder_model_info->buffer_removal_time_length =
   4195      aom_rb_read_literal(rb, 5) + 1;
   4196  decoder_model_info->frame_presentation_time_length =
   4197      aom_rb_read_literal(rb, 5) + 1;
   4198 }
   4199 
   4200 void av1_read_op_parameters_info(aom_dec_model_op_parameters_t *op_params,
   4201                                 int buffer_delay_length,
   4202                                 struct aom_read_bit_buffer *rb) {
   4203  op_params->decoder_buffer_delay =
   4204      aom_rb_read_unsigned_literal(rb, buffer_delay_length);
   4205  op_params->encoder_buffer_delay =
   4206      aom_rb_read_unsigned_literal(rb, buffer_delay_length);
   4207  op_params->low_delay_mode_flag = aom_rb_read_bit(rb);
   4208 }
   4209 
   4210 static inline void read_temporal_point_info(AV1_COMMON *const cm,
   4211                                            struct aom_read_bit_buffer *rb) {
   4212  cm->frame_presentation_time = aom_rb_read_unsigned_literal(
   4213      rb, cm->seq_params->decoder_model_info.frame_presentation_time_length);
   4214 }
   4215 
   4216 void av1_read_sequence_header(AV1_COMMON *cm, struct aom_read_bit_buffer *rb,
   4217                              SequenceHeader *seq_params) {
   4218  const int num_bits_width = aom_rb_read_literal(rb, 4) + 1;
   4219  const int num_bits_height = aom_rb_read_literal(rb, 4) + 1;
   4220  const int max_frame_width = aom_rb_read_literal(rb, num_bits_width) + 1;
   4221  const int max_frame_height = aom_rb_read_literal(rb, num_bits_height) + 1;
   4222 
   4223  seq_params->num_bits_width = num_bits_width;
   4224  seq_params->num_bits_height = num_bits_height;
   4225  seq_params->max_frame_width = max_frame_width;
   4226  seq_params->max_frame_height = max_frame_height;
   4227 
   4228  if (seq_params->reduced_still_picture_hdr) {
   4229    seq_params->frame_id_numbers_present_flag = 0;
   4230  } else {
   4231    seq_params->frame_id_numbers_present_flag = aom_rb_read_bit(rb);
   4232  }
   4233  if (seq_params->frame_id_numbers_present_flag) {
   4234    // We must always have delta_frame_id_length < frame_id_length,
   4235    // in order for a frame to be referenced with a unique delta.
   4236    // Avoid wasting bits by using a coding that enforces this restriction.
   4237    seq_params->delta_frame_id_length = aom_rb_read_literal(rb, 4) + 2;
   4238    seq_params->frame_id_length =
   4239        aom_rb_read_literal(rb, 3) + seq_params->delta_frame_id_length + 1;
   4240    if (seq_params->frame_id_length > 16)
   4241      aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
   4242                         "Invalid frame_id_length");
   4243  }
   4244 
   4245  setup_sb_size(seq_params, rb);
   4246 
   4247  seq_params->enable_filter_intra = aom_rb_read_bit(rb);
   4248  seq_params->enable_intra_edge_filter = aom_rb_read_bit(rb);
   4249 
   4250  if (seq_params->reduced_still_picture_hdr) {
   4251    seq_params->enable_interintra_compound = 0;
   4252    seq_params->enable_masked_compound = 0;
   4253    seq_params->enable_warped_motion = 0;
   4254    seq_params->enable_dual_filter = 0;
   4255    seq_params->order_hint_info.enable_order_hint = 0;
   4256    seq_params->order_hint_info.enable_dist_wtd_comp = 0;
   4257    seq_params->order_hint_info.enable_ref_frame_mvs = 0;
   4258    seq_params->force_screen_content_tools = 2;  // SELECT_SCREEN_CONTENT_TOOLS
   4259    seq_params->force_integer_mv = 2;            // SELECT_INTEGER_MV
   4260    seq_params->order_hint_info.order_hint_bits_minus_1 = -1;
   4261  } else {
   4262    seq_params->enable_interintra_compound = aom_rb_read_bit(rb);
   4263    seq_params->enable_masked_compound = aom_rb_read_bit(rb);
   4264    seq_params->enable_warped_motion = aom_rb_read_bit(rb);
   4265    seq_params->enable_dual_filter = aom_rb_read_bit(rb);
   4266 
   4267    seq_params->order_hint_info.enable_order_hint = aom_rb_read_bit(rb);
   4268    seq_params->order_hint_info.enable_dist_wtd_comp =
   4269        seq_params->order_hint_info.enable_order_hint ? aom_rb_read_bit(rb) : 0;
   4270    seq_params->order_hint_info.enable_ref_frame_mvs =
   4271        seq_params->order_hint_info.enable_order_hint ? aom_rb_read_bit(rb) : 0;
   4272 
   4273    if (aom_rb_read_bit(rb)) {
   4274      seq_params->force_screen_content_tools =
   4275          2;  // SELECT_SCREEN_CONTENT_TOOLS
   4276    } else {
   4277      seq_params->force_screen_content_tools = aom_rb_read_bit(rb);
   4278    }
   4279 
   4280    if (seq_params->force_screen_content_tools > 0) {
   4281      if (aom_rb_read_bit(rb)) {
   4282        seq_params->force_integer_mv = 2;  // SELECT_INTEGER_MV
   4283      } else {
   4284        seq_params->force_integer_mv = aom_rb_read_bit(rb);
   4285      }
   4286    } else {
   4287      seq_params->force_integer_mv = 2;  // SELECT_INTEGER_MV
   4288    }
   4289    seq_params->order_hint_info.order_hint_bits_minus_1 =
   4290        seq_params->order_hint_info.enable_order_hint
   4291            ? aom_rb_read_literal(rb, 3)
   4292            : -1;
   4293  }
   4294 
   4295  seq_params->enable_superres = aom_rb_read_bit(rb);
   4296  seq_params->enable_cdef = aom_rb_read_bit(rb);
   4297  seq_params->enable_restoration = aom_rb_read_bit(rb);
   4298 }
   4299 
   4300 static int read_global_motion_params(WarpedMotionParams *params,
   4301                                     const WarpedMotionParams *ref_params,
   4302                                     struct aom_read_bit_buffer *rb,
   4303                                     int allow_hp) {
   4304  TransformationType type = aom_rb_read_bit(rb);
   4305  if (type != IDENTITY) {
   4306    if (aom_rb_read_bit(rb))
   4307      type = ROTZOOM;
   4308    else
   4309      type = aom_rb_read_bit(rb) ? TRANSLATION : AFFINE;
   4310  }
   4311 
   4312  *params = default_warp_params;
   4313  params->wmtype = type;
   4314 
   4315  if (type >= ROTZOOM) {
   4316    params->wmmat[2] = aom_rb_read_signed_primitive_refsubexpfin(
   4317                           rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
   4318                           (ref_params->wmmat[2] >> GM_ALPHA_PREC_DIFF) -
   4319                               (1 << GM_ALPHA_PREC_BITS)) *
   4320                           GM_ALPHA_DECODE_FACTOR +
   4321                       (1 << WARPEDMODEL_PREC_BITS);
   4322    params->wmmat[3] = aom_rb_read_signed_primitive_refsubexpfin(
   4323                           rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
   4324                           (ref_params->wmmat[3] >> GM_ALPHA_PREC_DIFF)) *
   4325                       GM_ALPHA_DECODE_FACTOR;
   4326  }
   4327 
   4328  if (type >= AFFINE) {
   4329    params->wmmat[4] = aom_rb_read_signed_primitive_refsubexpfin(
   4330                           rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
   4331                           (ref_params->wmmat[4] >> GM_ALPHA_PREC_DIFF)) *
   4332                       GM_ALPHA_DECODE_FACTOR;
   4333    params->wmmat[5] = aom_rb_read_signed_primitive_refsubexpfin(
   4334                           rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
   4335                           (ref_params->wmmat[5] >> GM_ALPHA_PREC_DIFF) -
   4336                               (1 << GM_ALPHA_PREC_BITS)) *
   4337                           GM_ALPHA_DECODE_FACTOR +
   4338                       (1 << WARPEDMODEL_PREC_BITS);
   4339  } else {
   4340    params->wmmat[4] = -params->wmmat[3];
   4341    params->wmmat[5] = params->wmmat[2];
   4342  }
   4343 
   4344  if (type >= TRANSLATION) {
   4345    const int trans_bits = (type == TRANSLATION)
   4346                               ? GM_ABS_TRANS_ONLY_BITS - !allow_hp
   4347                               : GM_ABS_TRANS_BITS;
   4348    const int trans_dec_factor =
   4349        (type == TRANSLATION) ? GM_TRANS_ONLY_DECODE_FACTOR * (1 << !allow_hp)
   4350                              : GM_TRANS_DECODE_FACTOR;
   4351    const int trans_prec_diff = (type == TRANSLATION)
   4352                                    ? GM_TRANS_ONLY_PREC_DIFF + !allow_hp
   4353                                    : GM_TRANS_PREC_DIFF;
   4354    params->wmmat[0] = aom_rb_read_signed_primitive_refsubexpfin(
   4355                           rb, (1 << trans_bits) + 1, SUBEXPFIN_K,
   4356                           (ref_params->wmmat[0] >> trans_prec_diff)) *
   4357                       trans_dec_factor;
   4358    params->wmmat[1] = aom_rb_read_signed_primitive_refsubexpfin(
   4359                           rb, (1 << trans_bits) + 1, SUBEXPFIN_K,
   4360                           (ref_params->wmmat[1] >> trans_prec_diff)) *
   4361                       trans_dec_factor;
   4362  }
   4363 
   4364  int good_shear_params = av1_get_shear_params(params);
   4365  if (!good_shear_params) return 0;
   4366 
   4367  return 1;
   4368 }
   4369 
   4370 static inline void read_global_motion(AV1_COMMON *cm,
   4371                                      struct aom_read_bit_buffer *rb) {
   4372  for (int frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
   4373    const WarpedMotionParams *ref_params =
   4374        cm->prev_frame ? &cm->prev_frame->global_motion[frame]
   4375                       : &default_warp_params;
   4376    int good_params =
   4377        read_global_motion_params(&cm->global_motion[frame], ref_params, rb,
   4378                                  cm->features.allow_high_precision_mv);
   4379    if (!good_params) {
   4380 #if WARPED_MOTION_DEBUG
   4381      printf("Warning: unexpected global motion shear params from aomenc\n");
   4382 #endif
   4383      cm->global_motion[frame].invalid = 1;
   4384    }
   4385 
   4386    // TODO(sarahparker, debargha): The logic in the commented out code below
   4387    // does not work currently and causes mismatches when resize is on. Fix it
   4388    // before turning the optimization back on.
   4389    /*
   4390    YV12_BUFFER_CONFIG *ref_buf = get_ref_frame(cm, frame);
   4391    if (cm->width == ref_buf->y_crop_width &&
   4392        cm->height == ref_buf->y_crop_height) {
   4393      read_global_motion_params(&cm->global_motion[frame],
   4394                                &cm->prev_frame->global_motion[frame], rb,
   4395                                cm->features.allow_high_precision_mv);
   4396    } else {
   4397      cm->global_motion[frame] = default_warp_params;
   4398    }
   4399    */
   4400    /*
   4401    printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
   4402           frame, cm->current_frame.frame_number, cm->show_frame,
   4403           cm->global_motion[frame].wmmat[0],
   4404           cm->global_motion[frame].wmmat[1],
   4405           cm->global_motion[frame].wmmat[2],
   4406           cm->global_motion[frame].wmmat[3]);
   4407           */
   4408  }
   4409  memcpy(cm->cur_frame->global_motion, cm->global_motion,
   4410         REF_FRAMES * sizeof(WarpedMotionParams));
   4411 }
   4412 
   4413 // Release the references to the frame buffers in cm->ref_frame_map and reset
   4414 // all elements of cm->ref_frame_map to NULL.
   4415 static inline void reset_ref_frame_map(AV1_COMMON *const cm) {
   4416  BufferPool *const pool = cm->buffer_pool;
   4417 
   4418  for (int i = 0; i < REF_FRAMES; i++) {
   4419    decrease_ref_count(cm->ref_frame_map[i], pool);
   4420    cm->ref_frame_map[i] = NULL;
   4421  }
   4422 }
   4423 
   4424 // If the refresh_frame_flags bitmask is set, update reference frame id values
   4425 // and mark frames as valid for reference.
   4426 static inline void update_ref_frame_id(AV1Decoder *const pbi) {
   4427  AV1_COMMON *const cm = &pbi->common;
   4428  int refresh_frame_flags = cm->current_frame.refresh_frame_flags;
   4429  for (int i = 0; i < REF_FRAMES; i++) {
   4430    if ((refresh_frame_flags >> i) & 1) {
   4431      cm->ref_frame_id[i] = cm->current_frame_id;
   4432      pbi->valid_for_referencing[i] = 1;
   4433    }
   4434  }
   4435 }
   4436 
   4437 static inline void show_existing_frame_reset(AV1Decoder *const pbi,
   4438                                             int existing_frame_idx) {
   4439  AV1_COMMON *const cm = &pbi->common;
   4440 
   4441  assert(cm->show_existing_frame);
   4442 
   4443  cm->current_frame.frame_type = KEY_FRAME;
   4444 
   4445  cm->current_frame.refresh_frame_flags = (1 << REF_FRAMES) - 1;
   4446 
   4447  for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
   4448    cm->remapped_ref_idx[i] = INVALID_IDX;
   4449  }
   4450 
   4451  if (pbi->need_resync) {
   4452    reset_ref_frame_map(cm);
   4453    pbi->need_resync = 0;
   4454  }
   4455 
   4456  // Note that the displayed frame must be valid for referencing in order to
   4457  // have been selected.
   4458  cm->current_frame_id = cm->ref_frame_id[existing_frame_idx];
   4459  update_ref_frame_id(pbi);
   4460 
   4461  cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
   4462 }
   4463 
   4464 static inline void reset_frame_buffers(AV1_COMMON *cm) {
   4465  RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
   4466  int i;
   4467 
   4468  lock_buffer_pool(cm->buffer_pool);
   4469  reset_ref_frame_map(cm);
   4470  assert(cm->cur_frame->ref_count == 1);
   4471  for (i = 0; i < cm->buffer_pool->num_frame_bufs; ++i) {
   4472    // Reset all unreferenced frame buffers. We can also reset cm->cur_frame
   4473    // because we are the sole owner of cm->cur_frame.
   4474    if (frame_bufs[i].ref_count > 0 && &frame_bufs[i] != cm->cur_frame) {
   4475      continue;
   4476    }
   4477    frame_bufs[i].order_hint = 0;
   4478    av1_zero(frame_bufs[i].ref_order_hints);
   4479  }
   4480  av1_zero_unused_internal_frame_buffers(&cm->buffer_pool->int_frame_buffers);
   4481  unlock_buffer_pool(cm->buffer_pool);
   4482 }
   4483 
   4484 // On success, returns 0. On failure, calls aom_internal_error and does not
   4485 // return.
   4486 static int read_uncompressed_header(AV1Decoder *pbi,
   4487                                    struct aom_read_bit_buffer *rb) {
   4488  AV1_COMMON *const cm = &pbi->common;
   4489  const SequenceHeader *const seq_params = cm->seq_params;
   4490  CurrentFrame *const current_frame = &cm->current_frame;
   4491  FeatureFlags *const features = &cm->features;
   4492  MACROBLOCKD *const xd = &pbi->dcb.xd;
   4493  BufferPool *const pool = cm->buffer_pool;
   4494  RefCntBuffer *const frame_bufs = pool->frame_bufs;
   4495  aom_s_frame_info *sframe_info = &pbi->sframe_info;
   4496  sframe_info->is_s_frame = 0;
   4497  sframe_info->is_s_frame_at_altref = 0;
   4498 
   4499  if (!pbi->sequence_header_ready) {
   4500    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   4501                       "No sequence header");
   4502  }
   4503 
   4504  if (seq_params->reduced_still_picture_hdr) {
   4505    cm->show_existing_frame = 0;
   4506    cm->show_frame = 1;
   4507    current_frame->frame_type = KEY_FRAME;
   4508    if (pbi->sequence_header_changed) {
   4509      // This is the start of a new coded video sequence.
   4510      pbi->sequence_header_changed = 0;
   4511      pbi->decoding_first_frame = 1;
   4512      reset_frame_buffers(cm);
   4513    }
   4514    features->error_resilient_mode = 1;
   4515  } else {
   4516    cm->show_existing_frame = aom_rb_read_bit(rb);
   4517    pbi->reset_decoder_state = 0;
   4518 
   4519    if (cm->show_existing_frame) {
   4520      if (pbi->sequence_header_changed) {
   4521        aom_internal_error(
   4522            &pbi->error, AOM_CODEC_CORRUPT_FRAME,
   4523            "New sequence header starts with a show_existing_frame.");
   4524      }
   4525      // Show an existing frame directly.
   4526      const int existing_frame_idx = aom_rb_read_literal(rb, 3);
   4527      RefCntBuffer *const frame_to_show = cm->ref_frame_map[existing_frame_idx];
   4528      if (frame_to_show == NULL) {
   4529        aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
   4530                           "Buffer does not contain a decoded frame");
   4531      }
   4532      if (seq_params->decoder_model_info_present_flag &&
   4533          seq_params->timing_info.equal_picture_interval == 0) {
   4534        read_temporal_point_info(cm, rb);
   4535      }
   4536      if (seq_params->frame_id_numbers_present_flag) {
   4537        int frame_id_length = seq_params->frame_id_length;
   4538        int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
   4539        /* Compare display_frame_id with ref_frame_id and check valid for
   4540         * referencing */
   4541        if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
   4542            pbi->valid_for_referencing[existing_frame_idx] == 0)
   4543          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   4544                             "Reference buffer frame ID mismatch");
   4545      }
   4546      lock_buffer_pool(pool);
   4547      assert(frame_to_show->ref_count > 0);
   4548      // cm->cur_frame should be the buffer referenced by the return value
   4549      // of the get_free_fb() call in assign_cur_frame_new_fb() (called by
   4550      // av1_receive_compressed_data()), so the ref_count should be 1.
   4551      assert(cm->cur_frame->ref_count == 1);
   4552      // assign_frame_buffer_p() decrements ref_count directly rather than
   4553      // call decrease_ref_count(). If cm->cur_frame->raw_frame_buffer has
   4554      // already been allocated, it will not be released by
   4555      // assign_frame_buffer_p()!
   4556      assert(!cm->cur_frame->raw_frame_buffer.data);
   4557      assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
   4558      pbi->reset_decoder_state = frame_to_show->frame_type == KEY_FRAME;
   4559      unlock_buffer_pool(pool);
   4560 
   4561      cm->lf.filter_level[0] = 0;
   4562      cm->lf.filter_level[1] = 0;
   4563      cm->show_frame = 1;
   4564      current_frame->order_hint = frame_to_show->order_hint;
   4565 
   4566      // Section 6.8.2: It is a requirement of bitstream conformance that when
   4567      // show_existing_frame is used to show a previous frame, that the value
   4568      // of showable_frame for the previous frame was equal to 1.
   4569      if (!frame_to_show->showable_frame) {
   4570        aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
   4571                           "Buffer does not contain a showable frame");
   4572      }
   4573      // Section 6.8.2: It is a requirement of bitstream conformance that when
   4574      // show_existing_frame is used to show a previous frame with
   4575      // RefFrameType[ frame_to_show_map_idx ] equal to KEY_FRAME, that the
   4576      // frame is output via the show_existing_frame mechanism at most once.
   4577      if (pbi->reset_decoder_state) frame_to_show->showable_frame = 0;
   4578 
   4579      cm->film_grain_params = frame_to_show->film_grain_params;
   4580 
   4581      if (pbi->reset_decoder_state) {
   4582        show_existing_frame_reset(pbi, existing_frame_idx);
   4583      } else {
   4584        current_frame->refresh_frame_flags = 0;
   4585      }
   4586 
   4587      return 0;
   4588    }
   4589 
   4590    current_frame->frame_type = (FRAME_TYPE)aom_rb_read_literal(rb, 2);
   4591    if (pbi->sequence_header_changed) {
   4592      if (current_frame->frame_type == KEY_FRAME) {
   4593        // This is the start of a new coded video sequence.
   4594        pbi->sequence_header_changed = 0;
   4595        pbi->decoding_first_frame = 1;
   4596        reset_frame_buffers(cm);
   4597      } else {
   4598        aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   4599                           "Sequence header has changed without a keyframe.");
   4600      }
   4601    }
   4602 
   4603    cm->show_frame = aom_rb_read_bit(rb);
   4604    if (cm->show_frame == 0) pbi->is_arf_frame_present = 1;
   4605    if (cm->show_frame == 0 && cm->current_frame.frame_type == KEY_FRAME)
   4606      pbi->is_fwd_kf_present = 1;
   4607    if (cm->current_frame.frame_type == S_FRAME) {
   4608      sframe_info->is_s_frame = 1;
   4609      sframe_info->is_s_frame_at_altref = cm->show_frame ? 0 : 1;
   4610    }
   4611    if (seq_params->still_picture &&
   4612        (current_frame->frame_type != KEY_FRAME || !cm->show_frame)) {
   4613      aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   4614                         "Still pictures must be coded as shown keyframes");
   4615    }
   4616    cm->showable_frame = current_frame->frame_type != KEY_FRAME;
   4617    if (cm->show_frame) {
   4618      if (seq_params->decoder_model_info_present_flag &&
   4619          seq_params->timing_info.equal_picture_interval == 0)
   4620        read_temporal_point_info(cm, rb);
   4621    } else {
   4622      // See if this frame can be used as show_existing_frame in future
   4623      cm->showable_frame = aom_rb_read_bit(rb);
   4624    }
   4625    cm->cur_frame->showable_frame = cm->showable_frame;
   4626    features->error_resilient_mode =
   4627        frame_is_sframe(cm) ||
   4628                (current_frame->frame_type == KEY_FRAME && cm->show_frame)
   4629            ? 1
   4630            : aom_rb_read_bit(rb);
   4631  }
   4632 
   4633  if (current_frame->frame_type == KEY_FRAME && cm->show_frame) {
   4634    /* All frames need to be marked as not valid for referencing */
   4635    for (int i = 0; i < REF_FRAMES; i++) {
   4636      pbi->valid_for_referencing[i] = 0;
   4637    }
   4638  }
   4639  features->disable_cdf_update = aom_rb_read_bit(rb);
   4640  if (seq_params->force_screen_content_tools == 2) {
   4641    features->allow_screen_content_tools = aom_rb_read_bit(rb);
   4642  } else {
   4643    features->allow_screen_content_tools =
   4644        seq_params->force_screen_content_tools;
   4645  }
   4646 
   4647  if (features->allow_screen_content_tools) {
   4648    if (seq_params->force_integer_mv == 2) {
   4649      features->cur_frame_force_integer_mv = aom_rb_read_bit(rb);
   4650    } else {
   4651      features->cur_frame_force_integer_mv = seq_params->force_integer_mv;
   4652    }
   4653  } else {
   4654    features->cur_frame_force_integer_mv = 0;
   4655  }
   4656 
   4657  int frame_size_override_flag = 0;
   4658  features->allow_intrabc = 0;
   4659  features->primary_ref_frame = PRIMARY_REF_NONE;
   4660 
   4661  if (!seq_params->reduced_still_picture_hdr) {
   4662    if (seq_params->frame_id_numbers_present_flag) {
   4663      int frame_id_length = seq_params->frame_id_length;
   4664      int diff_len = seq_params->delta_frame_id_length;
   4665      int prev_frame_id = 0;
   4666      int have_prev_frame_id =
   4667          !pbi->decoding_first_frame &&
   4668          !(current_frame->frame_type == KEY_FRAME && cm->show_frame);
   4669      if (have_prev_frame_id) {
   4670        prev_frame_id = cm->current_frame_id;
   4671      }
   4672      cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
   4673 
   4674      if (have_prev_frame_id) {
   4675        int diff_frame_id;
   4676        if (cm->current_frame_id > prev_frame_id) {
   4677          diff_frame_id = cm->current_frame_id - prev_frame_id;
   4678        } else {
   4679          diff_frame_id =
   4680              (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
   4681        }
   4682        /* Check current_frame_id for conformance */
   4683        if (prev_frame_id == cm->current_frame_id ||
   4684            diff_frame_id >= (1 << (frame_id_length - 1))) {
   4685          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   4686                             "Invalid value of current_frame_id");
   4687        }
   4688      }
   4689      /* Check if some frames need to be marked as not valid for referencing */
   4690      for (int i = 0; i < REF_FRAMES; i++) {
   4691        if (cm->current_frame_id - (1 << diff_len) > 0) {
   4692          if (cm->ref_frame_id[i] > cm->current_frame_id ||
   4693              cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
   4694            pbi->valid_for_referencing[i] = 0;
   4695        } else {
   4696          if (cm->ref_frame_id[i] > cm->current_frame_id &&
   4697              cm->ref_frame_id[i] < (1 << frame_id_length) +
   4698                                        cm->current_frame_id - (1 << diff_len))
   4699            pbi->valid_for_referencing[i] = 0;
   4700        }
   4701      }
   4702    }
   4703 
   4704    frame_size_override_flag = frame_is_sframe(cm) ? 1 : aom_rb_read_bit(rb);
   4705 
   4706    current_frame->order_hint = aom_rb_read_literal(
   4707        rb, seq_params->order_hint_info.order_hint_bits_minus_1 + 1);
   4708 
   4709    if (seq_params->order_hint_info.enable_order_hint)
   4710      current_frame->frame_number = current_frame->order_hint;
   4711 
   4712    if (!features->error_resilient_mode && !frame_is_intra_only(cm)) {
   4713      features->primary_ref_frame = aom_rb_read_literal(rb, PRIMARY_REF_BITS);
   4714    }
   4715  }
   4716 
   4717  if (seq_params->decoder_model_info_present_flag) {
   4718    pbi->buffer_removal_time_present = aom_rb_read_bit(rb);
   4719    if (pbi->buffer_removal_time_present) {
   4720      for (int op_num = 0;
   4721           op_num < seq_params->operating_points_cnt_minus_1 + 1; op_num++) {
   4722        if (seq_params->op_params[op_num].decoder_model_param_present_flag) {
   4723          if (seq_params->operating_point_idc[op_num] == 0 ||
   4724              (((seq_params->operating_point_idc[op_num] >>
   4725                 cm->temporal_layer_id) &
   4726                0x1) &&
   4727               ((seq_params->operating_point_idc[op_num] >>
   4728                 (cm->spatial_layer_id + 8)) &
   4729                0x1))) {
   4730            cm->buffer_removal_times[op_num] = aom_rb_read_unsigned_literal(
   4731                rb, seq_params->decoder_model_info.buffer_removal_time_length);
   4732          } else {
   4733            cm->buffer_removal_times[op_num] = 0;
   4734          }
   4735        } else {
   4736          cm->buffer_removal_times[op_num] = 0;
   4737        }
   4738      }
   4739    }
   4740  }
   4741  if (current_frame->frame_type == KEY_FRAME) {
   4742    if (!cm->show_frame) {  // unshown keyframe (forward keyframe)
   4743      current_frame->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
   4744    } else {  // shown keyframe
   4745      current_frame->refresh_frame_flags = (1 << REF_FRAMES) - 1;
   4746    }
   4747 
   4748    for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
   4749      cm->remapped_ref_idx[i] = INVALID_IDX;
   4750    }
   4751    if (pbi->need_resync) {
   4752      reset_ref_frame_map(cm);
   4753      pbi->need_resync = 0;
   4754    }
   4755  } else {
   4756    if (current_frame->frame_type == INTRA_ONLY_FRAME) {
   4757      current_frame->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
   4758      if (current_frame->refresh_frame_flags == 0xFF) {
   4759        aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
   4760                           "Intra only frames cannot have refresh flags 0xFF");
   4761      }
   4762      if (pbi->need_resync) {
   4763        reset_ref_frame_map(cm);
   4764        pbi->need_resync = 0;
   4765      }
   4766    } else if (pbi->need_resync != 1) { /* Skip if need resync */
   4767      current_frame->refresh_frame_flags =
   4768          frame_is_sframe(cm) ? 0xFF : aom_rb_read_literal(rb, REF_FRAMES);
   4769    }
   4770  }
   4771 
   4772  if (!frame_is_intra_only(cm) || current_frame->refresh_frame_flags != 0xFF) {
   4773    // Read all ref frame order hints if error_resilient_mode == 1
   4774    if (features->error_resilient_mode &&
   4775        seq_params->order_hint_info.enable_order_hint) {
   4776      for (int ref_idx = 0; ref_idx < REF_FRAMES; ref_idx++) {
   4777        // Read order hint from bit stream
   4778        unsigned int order_hint = aom_rb_read_literal(
   4779            rb, seq_params->order_hint_info.order_hint_bits_minus_1 + 1);
   4780        // Get buffer
   4781        RefCntBuffer *buf = cm->ref_frame_map[ref_idx];
   4782        if (buf == NULL || order_hint != buf->order_hint) {
   4783          if (buf != NULL) {
   4784            lock_buffer_pool(pool);
   4785            decrease_ref_count(buf, pool);
   4786            unlock_buffer_pool(pool);
   4787            cm->ref_frame_map[ref_idx] = NULL;
   4788          }
   4789          // If no corresponding buffer exists, allocate a new buffer with all
   4790          // pixels set to neutral grey.
   4791          int buf_idx = get_free_fb(cm);
   4792          if (buf_idx == INVALID_IDX) {
   4793            aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
   4794                               "Unable to find free frame buffer");
   4795          }
   4796          buf = &frame_bufs[buf_idx];
   4797          lock_buffer_pool(pool);
   4798 #if CONFIG_SIZE_LIMIT
   4799          if (seq_params->max_frame_width > DECODE_WIDTH_LIMIT ||
   4800              seq_params->max_frame_height > DECODE_HEIGHT_LIMIT) {
   4801            decrease_ref_count(buf, pool);
   4802            unlock_buffer_pool(pool);
   4803            aom_internal_error(
   4804                cm->error, AOM_CODEC_CORRUPT_FRAME,
   4805                "Dimensions of %dx%d beyond allowed size of %dx%d.",
   4806                seq_params->max_frame_width, seq_params->max_frame_height,
   4807                DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
   4808          }
   4809 #endif
   4810          if (aom_realloc_frame_buffer(
   4811                  &buf->buf, seq_params->max_frame_width,
   4812                  seq_params->max_frame_height, seq_params->subsampling_x,
   4813                  seq_params->subsampling_y, seq_params->use_highbitdepth,
   4814                  AOM_BORDER_IN_PIXELS, features->byte_alignment,
   4815                  &buf->raw_frame_buffer, pool->get_fb_cb, pool->cb_priv, false,
   4816                  0)) {
   4817            decrease_ref_count(buf, pool);
   4818            unlock_buffer_pool(pool);
   4819            aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
   4820                               "Failed to allocate frame buffer");
   4821          }
   4822          unlock_buffer_pool(pool);
   4823          // According to the specification, valid bitstreams are required to
   4824          // never use missing reference frames so the filling process for
   4825          // missing frames is not normatively defined and RefValid for missing
   4826          // frames is set to 0.
   4827 
   4828          // To make libaom more robust when the bitstream has been corrupted
   4829          // by the loss of some frames of data, this code adds a neutral grey
   4830          // buffer in place of missing frames, i.e.
   4831          //
   4832          set_planes_to_neutral_grey(seq_params, &buf->buf, 0);
   4833          //
   4834          // and allows the frames to be used for referencing, i.e.
   4835          //
   4836          pbi->valid_for_referencing[ref_idx] = 1;
   4837          //
   4838          // Please note such behavior is not normative and other decoders may
   4839          // use a different approach.
   4840          cm->ref_frame_map[ref_idx] = buf;
   4841          buf->order_hint = order_hint;
   4842        }
   4843      }
   4844    }
   4845  }
   4846 
   4847  if (current_frame->frame_type == KEY_FRAME) {
   4848    setup_frame_size(cm, frame_size_override_flag, rb);
   4849 
   4850    if (features->allow_screen_content_tools && !av1_superres_scaled(cm))
   4851      features->allow_intrabc = aom_rb_read_bit(rb);
   4852    features->allow_ref_frame_mvs = 0;
   4853    cm->prev_frame = NULL;
   4854  } else {
   4855    features->allow_ref_frame_mvs = 0;
   4856 
   4857    if (current_frame->frame_type == INTRA_ONLY_FRAME) {
   4858      cm->cur_frame->film_grain_params_present =
   4859          seq_params->film_grain_params_present;
   4860      setup_frame_size(cm, frame_size_override_flag, rb);
   4861      if (features->allow_screen_content_tools && !av1_superres_scaled(cm))
   4862        features->allow_intrabc = aom_rb_read_bit(rb);
   4863 
   4864    } else if (pbi->need_resync != 1) { /* Skip if need resync */
   4865      int frame_refs_short_signaling = 0;
   4866      // Frame refs short signaling is off when error resilient mode is on.
   4867      if (seq_params->order_hint_info.enable_order_hint)
   4868        frame_refs_short_signaling = aom_rb_read_bit(rb);
   4869 
   4870      if (frame_refs_short_signaling) {
   4871        // == LAST_FRAME ==
   4872        const int lst_ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
   4873        const RefCntBuffer *const lst_buf = cm->ref_frame_map[lst_ref];
   4874 
   4875        // == GOLDEN_FRAME ==
   4876        const int gld_ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
   4877        const RefCntBuffer *const gld_buf = cm->ref_frame_map[gld_ref];
   4878 
   4879        // Most of the time, streams start with a keyframe. In that case,
   4880        // ref_frame_map will have been filled in at that point and will not
   4881        // contain any NULLs. However, streams are explicitly allowed to start
   4882        // with an intra-only frame, so long as they don't then signal a
   4883        // reference to a slot that hasn't been set yet. That's what we are
   4884        // checking here.
   4885        if (lst_buf == NULL)
   4886          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   4887                             "Inter frame requests nonexistent reference");
   4888        if (gld_buf == NULL)
   4889          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   4890                             "Inter frame requests nonexistent reference");
   4891 
   4892        av1_set_frame_refs(cm, cm->remapped_ref_idx, lst_ref, gld_ref);
   4893      }
   4894 
   4895      for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
   4896        int ref = 0;
   4897        if (!frame_refs_short_signaling) {
   4898          ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
   4899 
   4900          // Most of the time, streams start with a keyframe. In that case,
   4901          // ref_frame_map will have been filled in at that point and will not
   4902          // contain any NULLs. However, streams are explicitly allowed to start
   4903          // with an intra-only frame, so long as they don't then signal a
   4904          // reference to a slot that hasn't been set yet. That's what we are
   4905          // checking here.
   4906          if (cm->ref_frame_map[ref] == NULL)
   4907            aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   4908                               "Inter frame requests nonexistent reference");
   4909          cm->remapped_ref_idx[i] = ref;
   4910        } else {
   4911          ref = cm->remapped_ref_idx[i];
   4912        }
   4913        // Check valid for referencing
   4914        if (pbi->valid_for_referencing[ref] == 0)
   4915          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   4916                             "Reference frame not valid for referencing");
   4917 
   4918        cm->ref_frame_sign_bias[LAST_FRAME + i] = 0;
   4919 
   4920        if (seq_params->frame_id_numbers_present_flag) {
   4921          int frame_id_length = seq_params->frame_id_length;
   4922          int diff_len = seq_params->delta_frame_id_length;
   4923          int delta_frame_id_minus_1 = aom_rb_read_literal(rb, diff_len);
   4924          int ref_frame_id =
   4925              ((cm->current_frame_id - (delta_frame_id_minus_1 + 1) +
   4926                (1 << frame_id_length)) %
   4927               (1 << frame_id_length));
   4928          // Compare values derived from delta_frame_id_minus_1 and
   4929          // refresh_frame_flags.
   4930          if (ref_frame_id != cm->ref_frame_id[ref])
   4931            aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   4932                               "Reference buffer frame ID mismatch");
   4933        }
   4934      }
   4935 
   4936      if (!features->error_resilient_mode && frame_size_override_flag) {
   4937        setup_frame_size_with_refs(cm, rb);
   4938      } else {
   4939        setup_frame_size(cm, frame_size_override_flag, rb);
   4940      }
   4941 
   4942      if (features->cur_frame_force_integer_mv) {
   4943        features->allow_high_precision_mv = 0;
   4944      } else {
   4945        features->allow_high_precision_mv = aom_rb_read_bit(rb);
   4946      }
   4947      features->interp_filter = read_frame_interp_filter(rb);
   4948      features->switchable_motion_mode = aom_rb_read_bit(rb);
   4949    }
   4950 
   4951    cm->prev_frame = get_primary_ref_frame_buf(cm);
   4952    if (features->primary_ref_frame != PRIMARY_REF_NONE &&
   4953        get_primary_ref_frame_buf(cm) == NULL) {
   4954      aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   4955                         "Reference frame containing this frame's initial "
   4956                         "frame context is unavailable.");
   4957    }
   4958 
   4959    if (!(current_frame->frame_type == INTRA_ONLY_FRAME) &&
   4960        pbi->need_resync != 1) {
   4961      if (frame_might_allow_ref_frame_mvs(cm))
   4962        features->allow_ref_frame_mvs = aom_rb_read_bit(rb);
   4963      else
   4964        features->allow_ref_frame_mvs = 0;
   4965 
   4966      for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
   4967        const RefCntBuffer *const ref_buf = get_ref_frame_buf(cm, i);
   4968        struct scale_factors *const ref_scale_factors =
   4969            get_ref_scale_factors(cm, i);
   4970        av1_setup_scale_factors_for_frame(
   4971            ref_scale_factors, ref_buf->buf.y_crop_width,
   4972            ref_buf->buf.y_crop_height, cm->width, cm->height);
   4973        if ((!av1_is_valid_scale(ref_scale_factors)))
   4974          aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
   4975                             "Reference frame has invalid dimensions");
   4976      }
   4977    }
   4978  }
   4979 
   4980  av1_setup_frame_buf_refs(cm);
   4981 
   4982  av1_setup_frame_sign_bias(cm);
   4983 
   4984  cm->cur_frame->frame_type = current_frame->frame_type;
   4985 
   4986  update_ref_frame_id(pbi);
   4987 
   4988  const int might_bwd_adapt = !(seq_params->reduced_still_picture_hdr) &&
   4989                              !(features->disable_cdf_update);
   4990  if (might_bwd_adapt) {
   4991    features->refresh_frame_context = aom_rb_read_bit(rb)
   4992                                          ? REFRESH_FRAME_CONTEXT_DISABLED
   4993                                          : REFRESH_FRAME_CONTEXT_BACKWARD;
   4994  } else {
   4995    features->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
   4996  }
   4997 
   4998  cm->cur_frame->buf.bit_depth = seq_params->bit_depth;
   4999  cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
   5000  cm->cur_frame->buf.transfer_characteristics =
   5001      seq_params->transfer_characteristics;
   5002  cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
   5003  cm->cur_frame->buf.monochrome = seq_params->monochrome;
   5004  cm->cur_frame->buf.chroma_sample_position =
   5005      seq_params->chroma_sample_position;
   5006  cm->cur_frame->buf.color_range = seq_params->color_range;
   5007  cm->cur_frame->buf.render_width = cm->render_width;
   5008  cm->cur_frame->buf.render_height = cm->render_height;
   5009 
   5010  if (pbi->need_resync) {
   5011    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   5012                       "Keyframe / intra-only frame required to reset decoder"
   5013                       " state");
   5014  }
   5015 
   5016  if (features->allow_intrabc) {
   5017    // Set parameters corresponding to no filtering.
   5018    struct loopfilter *lf = &cm->lf;
   5019    lf->filter_level[0] = 0;
   5020    lf->filter_level[1] = 0;
   5021    cm->cdef_info.cdef_bits = 0;
   5022    cm->cdef_info.cdef_strengths[0] = 0;
   5023    cm->cdef_info.nb_cdef_strengths = 1;
   5024    cm->cdef_info.cdef_uv_strengths[0] = 0;
   5025    cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
   5026    cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
   5027    cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
   5028  }
   5029 
   5030  read_tile_info(pbi, rb);
   5031  if (!av1_is_min_tile_width_satisfied(cm)) {
   5032    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   5033                       "Minimum tile width requirement not satisfied");
   5034  }
   5035 
   5036  CommonQuantParams *const quant_params = &cm->quant_params;
   5037  setup_quantization(quant_params, av1_num_planes(cm),
   5038                     cm->seq_params->separate_uv_delta_q, rb);
   5039  xd->bd = (int)seq_params->bit_depth;
   5040 
   5041  CommonContexts *const above_contexts = &cm->above_contexts;
   5042  if (above_contexts->num_planes < av1_num_planes(cm) ||
   5043      above_contexts->num_mi_cols < cm->mi_params.mi_cols ||
   5044      above_contexts->num_tile_rows < cm->tiles.rows) {
   5045    av1_free_above_context_buffers(above_contexts);
   5046    if (av1_alloc_above_context_buffers(above_contexts, cm->tiles.rows,
   5047                                        cm->mi_params.mi_cols,
   5048                                        av1_num_planes(cm))) {
   5049      aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
   5050                         "Failed to allocate context buffers");
   5051    }
   5052  }
   5053 
   5054  if (features->primary_ref_frame == PRIMARY_REF_NONE) {
   5055    av1_setup_past_independence(cm);
   5056  }
   5057 
   5058  setup_segmentation(cm, rb);
   5059 
   5060  cm->delta_q_info.delta_q_res = 1;
   5061  cm->delta_q_info.delta_lf_res = 1;
   5062  cm->delta_q_info.delta_lf_present_flag = 0;
   5063  cm->delta_q_info.delta_lf_multi = 0;
   5064  cm->delta_q_info.delta_q_present_flag =
   5065      quant_params->base_qindex > 0 ? aom_rb_read_bit(rb) : 0;
   5066  if (cm->delta_q_info.delta_q_present_flag) {
   5067    xd->current_base_qindex = quant_params->base_qindex;
   5068    cm->delta_q_info.delta_q_res = 1 << aom_rb_read_literal(rb, 2);
   5069    if (!features->allow_intrabc)
   5070      cm->delta_q_info.delta_lf_present_flag = aom_rb_read_bit(rb);
   5071    if (cm->delta_q_info.delta_lf_present_flag) {
   5072      cm->delta_q_info.delta_lf_res = 1 << aom_rb_read_literal(rb, 2);
   5073      cm->delta_q_info.delta_lf_multi = aom_rb_read_bit(rb);
   5074      av1_reset_loop_filter_delta(xd, av1_num_planes(cm));
   5075    }
   5076  }
   5077 
   5078  xd->cur_frame_force_integer_mv = features->cur_frame_force_integer_mv;
   5079 
   5080  for (int i = 0; i < MAX_SEGMENTS; ++i) {
   5081    const int qindex = av1_get_qindex(&cm->seg, i, quant_params->base_qindex);
   5082    xd->lossless[i] =
   5083        qindex == 0 && quant_params->y_dc_delta_q == 0 &&
   5084        quant_params->u_dc_delta_q == 0 && quant_params->u_ac_delta_q == 0 &&
   5085        quant_params->v_dc_delta_q == 0 && quant_params->v_ac_delta_q == 0;
   5086    xd->qindex[i] = qindex;
   5087  }
   5088  features->coded_lossless = is_coded_lossless(cm, xd);
   5089  features->all_lossless = features->coded_lossless && !av1_superres_scaled(cm);
   5090  setup_segmentation_dequant(cm, xd);
   5091  if (features->coded_lossless) {
   5092    cm->lf.filter_level[0] = 0;
   5093    cm->lf.filter_level[1] = 0;
   5094  }
   5095  if (features->coded_lossless || !seq_params->enable_cdef) {
   5096    cm->cdef_info.cdef_bits = 0;
   5097    cm->cdef_info.cdef_strengths[0] = 0;
   5098    cm->cdef_info.cdef_uv_strengths[0] = 0;
   5099  }
   5100  if (features->all_lossless || !seq_params->enable_restoration) {
   5101    cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
   5102    cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
   5103    cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
   5104  }
   5105  setup_loopfilter(cm, rb);
   5106 
   5107  if (!features->coded_lossless && seq_params->enable_cdef) {
   5108    setup_cdef(cm, rb);
   5109  }
   5110  if (!features->all_lossless && seq_params->enable_restoration) {
   5111    decode_restoration_mode(cm, rb);
   5112  }
   5113 
   5114  features->tx_mode = read_tx_mode(rb, features->coded_lossless);
   5115  current_frame->reference_mode = read_frame_reference_mode(cm, rb);
   5116 
   5117  av1_setup_skip_mode_allowed(cm);
   5118  current_frame->skip_mode_info.skip_mode_flag =
   5119      current_frame->skip_mode_info.skip_mode_allowed ? aom_rb_read_bit(rb) : 0;
   5120 
   5121  if (frame_might_allow_warped_motion(cm))
   5122    features->allow_warped_motion = aom_rb_read_bit(rb);
   5123  else
   5124    features->allow_warped_motion = 0;
   5125 
   5126  features->reduced_tx_set_used = aom_rb_read_bit(rb);
   5127 
   5128  if (features->allow_ref_frame_mvs && !frame_might_allow_ref_frame_mvs(cm)) {
   5129    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   5130                       "Frame wrongly requests reference frame MVs");
   5131  }
   5132 
   5133  if (!frame_is_intra_only(cm)) read_global_motion(cm, rb);
   5134 
   5135  cm->cur_frame->film_grain_params_present =
   5136      seq_params->film_grain_params_present;
   5137  read_film_grain(cm, rb);
   5138 
   5139 #if EXT_TILE_DEBUG
   5140  if (pbi->ext_tile_debug && cm->tiles.large_scale) {
   5141    read_ext_tile_info(pbi, rb);
   5142    av1_set_single_tile_decoding_mode(cm);
   5143  }
   5144 #endif  // EXT_TILE_DEBUG
   5145  return 0;
   5146 }
   5147 
   5148 struct aom_read_bit_buffer *av1_init_read_bit_buffer(
   5149    AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
   5150    const uint8_t *data_end) {
   5151  rb->bit_offset = 0;
   5152  rb->error_handler = error_handler;
   5153  rb->error_handler_data = &pbi->common;
   5154  rb->bit_buffer = data;
   5155  rb->bit_buffer_end = data_end;
   5156  return rb;
   5157 }
   5158 
   5159 BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
   5160  int profile = aom_rb_read_literal(rb, PROFILE_BITS);
   5161  return (BITSTREAM_PROFILE)profile;
   5162 }
   5163 
   5164 static inline void superres_post_decode(AV1Decoder *pbi) {
   5165  AV1_COMMON *const cm = &pbi->common;
   5166  BufferPool *const pool = cm->buffer_pool;
   5167 
   5168  if (!av1_superres_scaled(cm)) return;
   5169  assert(!cm->features.all_lossless);
   5170 
   5171  av1_superres_upscale(cm, pool, 0);
   5172 }
   5173 
   5174 uint32_t av1_decode_frame_headers_and_setup(AV1Decoder *pbi,
   5175                                            struct aom_read_bit_buffer *rb,
   5176                                            int trailing_bits_present) {
   5177  AV1_COMMON *const cm = &pbi->common;
   5178  const int num_planes = av1_num_planes(cm);
   5179  MACROBLOCKD *const xd = &pbi->dcb.xd;
   5180 
   5181 #if CONFIG_BITSTREAM_DEBUG
   5182  if (cm->seq_params->order_hint_info.enable_order_hint) {
   5183    aom_bitstream_queue_set_frame_read(cm->current_frame.order_hint * 2 +
   5184                                       cm->show_frame);
   5185  } else {
   5186    // This is currently used in RTC encoding. cm->show_frame is always 1.
   5187    assert(cm->show_frame);
   5188    aom_bitstream_queue_set_frame_read(cm->current_frame.frame_number);
   5189  }
   5190 #endif
   5191 #if CONFIG_MISMATCH_DEBUG
   5192  mismatch_move_frame_idx_r();
   5193 #endif
   5194 
   5195  for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
   5196    cm->global_motion[i] = default_warp_params;
   5197    cm->cur_frame->global_motion[i] = default_warp_params;
   5198  }
   5199  xd->global_motion = cm->global_motion;
   5200 
   5201  read_uncompressed_header(pbi, rb);
   5202 
   5203  if (trailing_bits_present) av1_check_trailing_bits(pbi, rb);
   5204 
   5205  if (!cm->tiles.single_tile_decoding &&
   5206      (pbi->dec_tile_row >= 0 || pbi->dec_tile_col >= 0)) {
   5207    pbi->dec_tile_row = -1;
   5208    pbi->dec_tile_col = -1;
   5209  }
   5210 
   5211  const uint32_t uncomp_hdr_size =
   5212      (uint32_t)aom_rb_bytes_read(rb);  // Size of the uncompressed header
   5213  YV12_BUFFER_CONFIG *new_fb = &cm->cur_frame->buf;
   5214  xd->cur_buf = new_fb;
   5215  if (av1_allow_intrabc(cm)) {
   5216    av1_setup_scale_factors_for_frame(
   5217        &cm->sf_identity, xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height,
   5218        xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height);
   5219  }
   5220 
   5221  // Showing a frame directly.
   5222  if (cm->show_existing_frame) {
   5223    if (pbi->reset_decoder_state) {
   5224      // Use the default frame context values.
   5225      *cm->fc = *cm->default_frame_context;
   5226      if (!cm->fc->initialized)
   5227        aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   5228                           "Uninitialized entropy context.");
   5229    }
   5230    return uncomp_hdr_size;
   5231  }
   5232 
   5233  cm->mi_params.setup_mi(&cm->mi_params);
   5234 
   5235  av1_calculate_ref_frame_side(cm);
   5236  if (cm->features.allow_ref_frame_mvs) av1_setup_motion_field(cm);
   5237 
   5238  av1_setup_block_planes(xd, cm->seq_params->subsampling_x,
   5239                         cm->seq_params->subsampling_y, num_planes);
   5240  if (cm->features.primary_ref_frame == PRIMARY_REF_NONE) {
   5241    // use the default frame context values
   5242    *cm->fc = *cm->default_frame_context;
   5243  } else {
   5244    *cm->fc = get_primary_ref_frame_buf(cm)->frame_context;
   5245  }
   5246  if (!cm->fc->initialized)
   5247    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   5248                       "Uninitialized entropy context.");
   5249 
   5250  pbi->dcb.corrupted = 0;
   5251  return uncomp_hdr_size;
   5252 }
   5253 
   5254 // Once-per-frame initialization
   5255 static inline void setup_frame_info(AV1Decoder *pbi) {
   5256  AV1_COMMON *const cm = &pbi->common;
   5257 
   5258  if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
   5259      cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
   5260      cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
   5261    av1_alloc_restoration_buffers(cm, /*is_sgr_enabled =*/true);
   5262    for (int p = 0; p < av1_num_planes(cm); p++) {
   5263      av1_alloc_restoration_struct(cm, &cm->rst_info[p], p > 0);
   5264    }
   5265  }
   5266 
   5267  const int use_highbd = cm->seq_params->use_highbitdepth;
   5268  const int buf_size = MC_TEMP_BUF_PELS << use_highbd;
   5269  if (pbi->td.mc_buf_size != buf_size) {
   5270    av1_free_mc_tmp_buf(&pbi->td);
   5271    allocate_mc_tmp_buf(cm, &pbi->td, buf_size, use_highbd);
   5272  }
   5273 }
   5274 
   5275 void av1_decode_tg_tiles_and_wrapup(AV1Decoder *pbi, const uint8_t *data,
   5276                                    const uint8_t *data_end,
   5277                                    const uint8_t **p_data_end, int start_tile,
   5278                                    int end_tile, int initialize_flag) {
   5279 #if CONFIG_COLLECT_COMPONENT_TIMING
   5280  start_timing(pbi, av1_decode_tg_tiles_and_wrapup_time);
   5281 #endif
   5282  AV1_COMMON *const cm = &pbi->common;
   5283  CommonTileParams *const tiles = &cm->tiles;
   5284  MACROBLOCKD *const xd = &pbi->dcb.xd;
   5285  const int tile_count_tg = end_tile - start_tile + 1;
   5286 
   5287  xd->error_info = cm->error;
   5288  if (initialize_flag) setup_frame_info(pbi);
   5289  const int num_planes = av1_num_planes(cm);
   5290 
   5291  if (pbi->max_threads > 1 && !(tiles->large_scale && !pbi->ext_tile_debug) &&
   5292      pbi->row_mt)
   5293    *p_data_end =
   5294        decode_tiles_row_mt(pbi, data, data_end, start_tile, end_tile);
   5295  else if (pbi->max_threads > 1 && tile_count_tg > 1 &&
   5296           !(tiles->large_scale && !pbi->ext_tile_debug))
   5297    *p_data_end = decode_tiles_mt(pbi, data, data_end, start_tile, end_tile);
   5298  else
   5299    *p_data_end = decode_tiles(pbi, data, data_end, start_tile, end_tile);
   5300 
   5301  // If the bit stream is monochrome, set the U and V buffers to a constant.
   5302  if (num_planes < 3) {
   5303    set_planes_to_neutral_grey(cm->seq_params, xd->cur_buf, 1);
   5304  }
   5305 
   5306  if (end_tile != tiles->rows * tiles->cols - 1) {
   5307    return;
   5308  }
   5309 
   5310  av1_alloc_cdef_buffers(cm, &pbi->cdef_worker, &pbi->cdef_sync,
   5311                         pbi->num_workers, 1);
   5312  av1_alloc_cdef_sync(cm, &pbi->cdef_sync, pbi->num_workers);
   5313 
   5314  if (!cm->features.allow_intrabc && !tiles->single_tile_decoding) {
   5315    if (cm->lf.filter_level[0] || cm->lf.filter_level[1]) {
   5316      av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, &pbi->dcb.xd, 0,
   5317                               num_planes, 0, pbi->tile_workers,
   5318                               pbi->num_workers, &pbi->lf_row_sync, 0);
   5319    }
   5320 
   5321    const int do_cdef =
   5322        !pbi->skip_loop_filter && !cm->features.coded_lossless &&
   5323        (cm->cdef_info.cdef_bits || cm->cdef_info.cdef_strengths[0] ||
   5324         cm->cdef_info.cdef_uv_strengths[0]);
   5325    const int do_superres = av1_superres_scaled(cm);
   5326    const int optimized_loop_restoration = !do_cdef && !do_superres;
   5327    const int do_loop_restoration =
   5328        cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
   5329        cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
   5330        cm->rst_info[2].frame_restoration_type != RESTORE_NONE;
   5331    // Frame border extension is not required in the decoder
   5332    // as it happens in extend_mc_border().
   5333    int do_extend_border_mt = 0;
   5334    if (!optimized_loop_restoration) {
   5335      if (do_loop_restoration)
   5336        av1_loop_restoration_save_boundary_lines(&pbi->common.cur_frame->buf,
   5337                                                 cm, 0);
   5338 
   5339      if (do_cdef) {
   5340        if (pbi->num_workers > 1) {
   5341          av1_cdef_frame_mt(cm, &pbi->dcb.xd, pbi->cdef_worker,
   5342                            pbi->tile_workers, &pbi->cdef_sync,
   5343                            pbi->num_workers, av1_cdef_init_fb_row_mt,
   5344                            do_extend_border_mt);
   5345        } else {
   5346          av1_cdef_frame(&pbi->common.cur_frame->buf, cm, &pbi->dcb.xd,
   5347                         av1_cdef_init_fb_row);
   5348        }
   5349      }
   5350 
   5351      superres_post_decode(pbi);
   5352 
   5353      if (do_loop_restoration) {
   5354        av1_loop_restoration_save_boundary_lines(&pbi->common.cur_frame->buf,
   5355                                                 cm, 1);
   5356        if (pbi->num_workers > 1) {
   5357          av1_loop_restoration_filter_frame_mt(
   5358              (YV12_BUFFER_CONFIG *)xd->cur_buf, cm, optimized_loop_restoration,
   5359              pbi->tile_workers, pbi->num_workers, &pbi->lr_row_sync,
   5360              &pbi->lr_ctxt, do_extend_border_mt);
   5361        } else {
   5362          av1_loop_restoration_filter_frame((YV12_BUFFER_CONFIG *)xd->cur_buf,
   5363                                            cm, optimized_loop_restoration,
   5364                                            &pbi->lr_ctxt);
   5365        }
   5366      }
   5367    } else {
   5368      // In no cdef and no superres case. Provide an optimized version of
   5369      // loop_restoration_filter.
   5370      if (do_loop_restoration) {
   5371        if (pbi->num_workers > 1) {
   5372          av1_loop_restoration_filter_frame_mt(
   5373              (YV12_BUFFER_CONFIG *)xd->cur_buf, cm, optimized_loop_restoration,
   5374              pbi->tile_workers, pbi->num_workers, &pbi->lr_row_sync,
   5375              &pbi->lr_ctxt, do_extend_border_mt);
   5376        } else {
   5377          av1_loop_restoration_filter_frame((YV12_BUFFER_CONFIG *)xd->cur_buf,
   5378                                            cm, optimized_loop_restoration,
   5379                                            &pbi->lr_ctxt);
   5380        }
   5381      }
   5382    }
   5383  }
   5384 
   5385  if (!pbi->dcb.corrupted) {
   5386    if (cm->features.refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
   5387      assert(pbi->context_update_tile_id < pbi->allocated_tiles);
   5388      *cm->fc = pbi->tile_data[pbi->context_update_tile_id].tctx;
   5389      av1_reset_cdf_symbol_counters(cm->fc);
   5390    }
   5391  } else {
   5392    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
   5393                       "Decode failed. Frame data is corrupted.");
   5394  }
   5395 
   5396 #if CONFIG_INSPECTION
   5397  if (pbi->inspect_cb != NULL) {
   5398    (*pbi->inspect_cb)(pbi, pbi->inspect_ctx);
   5399  }
   5400 #endif
   5401 
   5402  // Non frame parallel update frame context here.
   5403  if (!tiles->large_scale) {
   5404    cm->cur_frame->frame_context = *cm->fc;
   5405  }
   5406 
   5407  if (cm->show_frame && !cm->seq_params->order_hint_info.enable_order_hint) {
   5408    ++cm->current_frame.frame_number;
   5409  }
   5410 
   5411 #if CONFIG_COLLECT_COMPONENT_TIMING
   5412  end_timing(pbi, av1_decode_tg_tiles_and_wrapup_time);
   5413 #endif
   5414 }