LCOV - code coverage report
Current view: top level - third_party/aom/av1/decoder - decodeframe.c (source / functions) Hit Total Coverage
Test: output.info Lines: 0 1233 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 58 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <stdlib.h>  // qsort()
      14             : 
      15             : #include "./aom_config.h"
      16             : #include "./aom_dsp_rtcd.h"
      17             : #include "./aom_scale_rtcd.h"
      18             : #include "./av1_rtcd.h"
      19             : 
      20             : #include "aom/aom_codec.h"
      21             : #include "aom_dsp/aom_dsp_common.h"
      22             : #include "aom_dsp/bitreader.h"
      23             : #include "aom_dsp/bitreader_buffer.h"
      24             : #include "aom_dsp/binary_codes_reader.h"
      25             : #include "aom_mem/aom_mem.h"
      26             : #include "aom_ports/mem.h"
      27             : #include "aom_ports/mem_ops.h"
      28             : #include "aom_scale/aom_scale.h"
      29             : #include "aom_util/aom_thread.h"
      30             : 
      31             : #if CONFIG_BITSTREAM_DEBUG
      32             : #include "aom_util/debug_util.h"
      33             : #endif  // CONFIG_BITSTREAM_DEBUG
      34             : 
      35             : #include "av1/common/alloccommon.h"
      36             : #if CONFIG_CDEF
      37             : #include "av1/common/cdef.h"
      38             : #include "av1/common/clpf.h"
      39             : #endif
      40             : #if CONFIG_INSPECTION
      41             : #include "av1/decoder/inspection.h"
      42             : #endif
      43             : #include "av1/common/common.h"
      44             : #include "av1/common/entropy.h"
      45             : #include "av1/common/entropymode.h"
      46             : #include "av1/common/entropymv.h"
      47             : #include "av1/common/idct.h"
      48             : #include "av1/common/pred_common.h"
      49             : #include "av1/common/quant_common.h"
      50             : #include "av1/common/reconinter.h"
      51             : #include "av1/common/reconintra.h"
      52             : #include "av1/common/seg_common.h"
      53             : #include "av1/common/thread_common.h"
      54             : #include "av1/common/tile_common.h"
      55             : 
      56             : #include "av1/decoder/decodeframe.h"
      57             : #include "av1/decoder/decodemv.h"
      58             : #include "av1/decoder/decoder.h"
      59             : #if CONFIG_LV_MAP
      60             : #include "av1/decoder/decodetxb.h"
      61             : #endif
      62             : #include "av1/decoder/detokenize.h"
      63             : #include "av1/decoder/dsubexp.h"
      64             : 
      65             : #if CONFIG_WARPED_MOTION || CONFIG_GLOBAL_MOTION
      66             : #include "av1/common/warped_motion.h"
      67             : #endif  // CONFIG_WARPED_MOTION || CONFIG_GLOBAL_MOTION
      68             : 
      69             : #define MAX_AV1_HEADER_SIZE 80
      70             : #define ACCT_STR __func__
      71             : 
      72             : #if CONFIG_PVQ
      73             : #include "av1/common/partition.h"
      74             : #include "av1/common/pvq.h"
      75             : #include "av1/common/scan.h"
      76             : #include "av1/decoder/decint.h"
      77             : #include "av1/decoder/pvq_decoder.h"
      78             : #include "av1/encoder/encodemb.h"
      79             : #include "av1/encoder/hybrid_fwd_txfm.h"
      80             : #endif
      81             : 
      82             : #if CONFIG_CFL
      83             : #include "av1/common/cfl.h"
      84             : #endif
      85             : 
      86             : static struct aom_read_bit_buffer *init_read_bit_buffer(
      87             :     AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
      88             :     const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]);
      89             : static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
      90             :                                   size_t partition_size);
      91             : static size_t read_uncompressed_header(AV1Decoder *pbi,
      92             :                                        struct aom_read_bit_buffer *rb);
      93             : 
      94           0 : static int is_compound_reference_allowed(const AV1_COMMON *cm) {
      95             : #if CONFIG_ONE_SIDED_COMPOUND  // Normative in decoder
      96           0 :   return !frame_is_intra_only(cm);
      97             : #else
      98             :   int i;
      99             :   if (frame_is_intra_only(cm)) return 0;
     100             :   for (i = 1; i < INTER_REFS_PER_FRAME; ++i)
     101             :     if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
     102             : 
     103             :   return 0;
     104             : #endif
     105             : }
     106             : 
     107           0 : static void setup_compound_reference_mode(AV1_COMMON *cm) {
     108             : #if CONFIG_EXT_REFS
     109           0 :   cm->comp_fwd_ref[0] = LAST_FRAME;
     110           0 :   cm->comp_fwd_ref[1] = LAST2_FRAME;
     111           0 :   cm->comp_fwd_ref[2] = LAST3_FRAME;
     112           0 :   cm->comp_fwd_ref[3] = GOLDEN_FRAME;
     113             : 
     114           0 :   cm->comp_bwd_ref[0] = BWDREF_FRAME;
     115           0 :   cm->comp_bwd_ref[1] = ALTREF_FRAME;
     116             : #else
     117             :   if (cm->ref_frame_sign_bias[LAST_FRAME] ==
     118             :       cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
     119             :     cm->comp_fixed_ref = ALTREF_FRAME;
     120             :     cm->comp_var_ref[0] = LAST_FRAME;
     121             :     cm->comp_var_ref[1] = GOLDEN_FRAME;
     122             :   } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
     123             :              cm->ref_frame_sign_bias[ALTREF_FRAME]) {
     124             :     cm->comp_fixed_ref = GOLDEN_FRAME;
     125             :     cm->comp_var_ref[0] = LAST_FRAME;
     126             :     cm->comp_var_ref[1] = ALTREF_FRAME;
     127             :   } else {
     128             :     cm->comp_fixed_ref = LAST_FRAME;
     129             :     cm->comp_var_ref[0] = GOLDEN_FRAME;
     130             :     cm->comp_var_ref[1] = ALTREF_FRAME;
     131             :   }
     132             : #endif  // CONFIG_EXT_REFS
     133           0 : }
     134             : 
     135           0 : static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
     136           0 :   return len != 0 && len <= (size_t)(end - start);
     137             : }
     138             : 
     139           0 : static int decode_unsigned_max(struct aom_read_bit_buffer *rb, int max) {
     140           0 :   const int data = aom_rb_read_literal(rb, get_unsigned_bits(max));
     141           0 :   return data > max ? max : data;
     142             : }
     143             : 
     144           0 : static TX_MODE read_tx_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
     145             :                             struct aom_read_bit_buffer *rb) {
     146           0 :   int i, all_lossless = 1;
     147             : #if CONFIG_TX64X64
     148             :   TX_MODE tx_mode;
     149             : #endif
     150             : 
     151           0 :   if (cm->seg.enabled) {
     152           0 :     for (i = 0; i < MAX_SEGMENTS; ++i) {
     153           0 :       if (!xd->lossless[i]) {
     154           0 :         all_lossless = 0;
     155           0 :         break;
     156             :       }
     157             :     }
     158             :   } else {
     159           0 :     all_lossless = xd->lossless[0];
     160             :   }
     161             : 
     162           0 :   if (all_lossless) return ONLY_4X4;
     163             : #if CONFIG_TX64X64
     164             :   tx_mode = aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
     165             :   if (tx_mode == ALLOW_32X32) tx_mode += aom_rb_read_bit(rb);
     166             :   return tx_mode;
     167             : #else
     168           0 :   return aom_rb_read_bit(rb) ? TX_MODE_SELECT : aom_rb_read_literal(rb, 2);
     169             : #endif  // CONFIG_TX64X64
     170             : }
     171             : 
     172             : #if !CONFIG_EC_ADAPT
     173             : static void read_tx_size_probs(FRAME_CONTEXT *fc, aom_reader *r) {
     174             :   int i, j, k;
     175             :   for (i = 0; i < MAX_TX_DEPTH; ++i)
     176             :     for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
     177             :       for (k = 0; k < i + 1; ++k)
     178             :         av1_diff_update_prob(r, &fc->tx_size_probs[i][j][k], ACCT_STR);
     179             : }
     180             : #endif
     181             : 
     182             : #if !CONFIG_EC_ADAPT
     183             : static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
     184             :   int i, j;
     185             :   for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
     186             :     for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
     187             :       av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i], ACCT_STR);
     188             :   }
     189             : }
     190             : #endif
     191             : 
     192           0 : static void read_inter_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
     193             :   int i;
     194           0 :   for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
     195           0 :     av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
     196           0 :   for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
     197           0 :     av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
     198           0 :   for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
     199           0 :     av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
     200           0 :   for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
     201           0 :     av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
     202           0 : }
     203             : 
     204             : #if CONFIG_EXT_INTER
     205           0 : static void read_inter_compound_mode_probs(FRAME_CONTEXT *fc, aom_reader *r) {
     206             :   int i, j;
     207           0 :   if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
     208           0 :     for (j = 0; j < INTER_MODE_CONTEXTS; ++j) {
     209           0 :       for (i = 0; i < INTER_COMPOUND_MODES - 1; ++i) {
     210           0 :         av1_diff_update_prob(r, &fc->inter_compound_mode_probs[j][i], ACCT_STR);
     211             :       }
     212             :     }
     213             :   }
     214           0 : }
     215             : #endif  // CONFIG_EXT_INTER
     216             : #if !CONFIG_EC_ADAPT
     217             : #if !CONFIG_EXT_TX
     218             : static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
     219             :   int i, j, k;
     220             :   if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
     221             :     for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
     222             :       for (j = 0; j < TX_TYPES; ++j) {
     223             :         for (k = 0; k < TX_TYPES - 1; ++k)
     224             :           av1_diff_update_prob(r, &fc->intra_ext_tx_prob[i][j][k], ACCT_STR);
     225             :       }
     226             :     }
     227             :   }
     228             :   if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
     229             :     for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
     230             :       for (k = 0; k < TX_TYPES - 1; ++k)
     231             :         av1_diff_update_prob(r, &fc->inter_ext_tx_prob[i][k], ACCT_STR);
     232             :     }
     233             :   }
     234             : }
     235             : #endif
     236             : #endif
     237             : 
     238           0 : static REFERENCE_MODE read_frame_reference_mode(
     239             :     const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
     240           0 :   if (is_compound_reference_allowed(cm)) {
     241             : #if CONFIG_REF_ADAPT
     242             :     return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
     243             : #else
     244           0 :     return aom_rb_read_bit(rb)
     245             :                ? REFERENCE_MODE_SELECT
     246           0 :                : (aom_rb_read_bit(rb) ? COMPOUND_REFERENCE : SINGLE_REFERENCE);
     247             : #endif  // CONFIG_REF_ADAPT
     248             :   } else {
     249           0 :     return SINGLE_REFERENCE;
     250             :   }
     251             : }
     252             : 
     253           0 : static void read_frame_reference_mode_probs(AV1_COMMON *cm, aom_reader *r) {
     254           0 :   FRAME_CONTEXT *const fc = cm->fc;
     255             :   int i, j;
     256             : 
     257           0 :   if (cm->reference_mode == REFERENCE_MODE_SELECT)
     258           0 :     for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
     259           0 :       av1_diff_update_prob(r, &fc->comp_inter_prob[i], ACCT_STR);
     260             : 
     261           0 :   if (cm->reference_mode != COMPOUND_REFERENCE) {
     262           0 :     for (i = 0; i < REF_CONTEXTS; ++i) {
     263           0 :       for (j = 0; j < (SINGLE_REFS - 1); ++j) {
     264           0 :         av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
     265             :       }
     266             :     }
     267             :   }
     268             : 
     269           0 :   if (cm->reference_mode != SINGLE_REFERENCE) {
     270           0 :     for (i = 0; i < REF_CONTEXTS; ++i) {
     271             : #if CONFIG_EXT_REFS
     272           0 :       for (j = 0; j < (FWD_REFS - 1); ++j)
     273           0 :         av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
     274           0 :       for (j = 0; j < (BWD_REFS - 1); ++j)
     275           0 :         av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j], ACCT_STR);
     276             : #else
     277             :       for (j = 0; j < (COMP_REFS - 1); ++j)
     278             :         av1_diff_update_prob(r, &fc->comp_ref_prob[i][j], ACCT_STR);
     279             : #endif  // CONFIG_EXT_REFS
     280             :     }
     281             :   }
     282           0 : }
     283             : 
     284           0 : static void update_mv_probs(aom_prob *p, int n, aom_reader *r) {
     285             :   int i;
     286           0 :   for (i = 0; i < n; ++i) av1_diff_update_prob(r, &p[i], ACCT_STR);
     287           0 : }
     288             : 
     289           0 : static void read_mv_probs(nmv_context *ctx, int allow_hp, aom_reader *r) {
     290             :   int i;
     291             : 
     292             : #if !CONFIG_EC_ADAPT
     293             :   int j;
     294             :   update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
     295             : 
     296             :   for (i = 0; i < 2; ++i) {
     297             :     nmv_component *const comp_ctx = &ctx->comps[i];
     298             :     update_mv_probs(&comp_ctx->sign, 1, r);
     299             :     update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
     300             :     update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
     301             :     update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
     302             :   }
     303             :   for (i = 0; i < 2; ++i) {
     304             :     nmv_component *const comp_ctx = &ctx->comps[i];
     305             :     for (j = 0; j < CLASS0_SIZE; ++j) {
     306             :       update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
     307             :     }
     308             :     update_mv_probs(comp_ctx->fp, MV_FP_SIZE - 1, r);
     309             :   }
     310             : #endif  // !CONFIG_EC_ADAPT
     311             : 
     312           0 :   if (allow_hp) {
     313           0 :     for (i = 0; i < 2; ++i) {
     314           0 :       nmv_component *const comp_ctx = &ctx->comps[i];
     315           0 :       update_mv_probs(&comp_ctx->class0_hp, 1, r);
     316           0 :       update_mv_probs(&comp_ctx->hp, 1, r);
     317             :     }
     318             :   }
     319           0 : }
     320             : 
     321           0 : static void inverse_transform_block(MACROBLOCKD *xd, int plane,
     322             :                                     const TX_TYPE tx_type,
     323             :                                     const TX_SIZE tx_size, uint8_t *dst,
     324             :                                     int stride, int16_t scan_line, int eob) {
     325           0 :   struct macroblockd_plane *const pd = &xd->plane[plane];
     326           0 :   tran_low_t *const dqcoeff = pd->dqcoeff;
     327           0 :   av1_inverse_transform_block(xd, dqcoeff, tx_type, tx_size, dst, stride, eob);
     328           0 :   memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
     329           0 : }
     330             : 
     331             : #if CONFIG_PVQ
     332             : static int av1_pvq_decode_helper(MACROBLOCKD *xd, tran_low_t *ref_coeff,
     333             :                                  tran_low_t *dqcoeff, int16_t *quant, int pli,
     334             :                                  int bs, TX_TYPE tx_type, int xdec,
     335             :                                  PVQ_SKIP_TYPE ac_dc_coded) {
     336             :   unsigned int flags;  // used for daala's stream analyzer.
     337             :   int off;
     338             :   const int is_keyframe = 0;
     339             :   const int has_dc_skip = 1;
     340             :   int coeff_shift = 3 - av1_get_tx_scale(bs);
     341             :   int hbd_downshift = 0;
     342             :   int rounding_mask;
     343             :   // DC quantizer for PVQ
     344             :   int pvq_dc_quant;
     345             :   int lossless = (quant[0] == 0);
     346             :   const int blk_size = tx_size_wide[bs];
     347             :   int eob = 0;
     348             :   int i;
     349             :   od_dec_ctx *dec = &xd->daala_dec;
     350             :   int use_activity_masking = dec->use_activity_masking;
     351             :   DECLARE_ALIGNED(16, tran_low_t, dqcoeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
     352             :   DECLARE_ALIGNED(16, tran_low_t, ref_coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
     353             : 
     354             :   od_coeff ref_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
     355             :   od_coeff out_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX];
     356             : 
     357             :   hbd_downshift = xd->bd - 8;
     358             : 
     359             :   od_raster_to_coding_order(ref_coeff_pvq, blk_size, tx_type, ref_coeff,
     360             :                             blk_size);
     361             : 
     362             :   assert(OD_COEFF_SHIFT >= 4);
     363             :   if (lossless)
     364             :     pvq_dc_quant = 1;
     365             :   else {
     366             :     if (use_activity_masking)
     367             :       pvq_dc_quant = OD_MAXI(
     368             :           1, (quant[0] << (OD_COEFF_SHIFT - 3) >> hbd_downshift) *
     369             :                      dec->state.pvq_qm_q4[pli][od_qm_get_index(bs, 0)] >>
     370             :                  4);
     371             :     else
     372             :       pvq_dc_quant =
     373             :           OD_MAXI(1, quant[0] << (OD_COEFF_SHIFT - 3) >> hbd_downshift);
     374             :   }
     375             : 
     376             :   off = od_qm_offset(bs, xdec);
     377             : 
     378             :   // copy int16 inputs to int32
     379             :   for (i = 0; i < blk_size * blk_size; i++) {
     380             :     ref_int32[i] =
     381             :         AOM_SIGNED_SHL(ref_coeff_pvq[i], OD_COEFF_SHIFT - coeff_shift) >>
     382             :         hbd_downshift;
     383             :   }
     384             : 
     385             :   od_pvq_decode(dec, ref_int32, out_int32,
     386             :                 OD_MAXI(1, quant[1] << (OD_COEFF_SHIFT - 3) >> hbd_downshift),
     387             :                 pli, bs, OD_PVQ_BETA[use_activity_masking][pli][bs],
     388             :                 is_keyframe, &flags, ac_dc_coded, dec->state.qm + off,
     389             :                 dec->state.qm_inv + off);
     390             : 
     391             :   if (!has_dc_skip || out_int32[0]) {
     392             :     out_int32[0] =
     393             :         has_dc_skip + generic_decode(dec->r, &dec->state.adapt->model_dc[pli],
     394             :                                      &dec->state.adapt->ex_dc[pli][bs][0], 2,
     395             :                                      "dc:mag");
     396             :     if (out_int32[0]) out_int32[0] *= aom_read_bit(dec->r, "dc:sign") ? -1 : 1;
     397             :   }
     398             :   out_int32[0] = out_int32[0] * pvq_dc_quant + ref_int32[0];
     399             : 
     400             :   // copy int32 result back to int16
     401             :   assert(OD_COEFF_SHIFT > coeff_shift);
     402             :   rounding_mask = (1 << (OD_COEFF_SHIFT - coeff_shift - 1)) - 1;
     403             :   for (i = 0; i < blk_size * blk_size; i++) {
     404             :     out_int32[i] = AOM_SIGNED_SHL(out_int32[i], hbd_downshift);
     405             :     dqcoeff_pvq[i] = (out_int32[i] + (out_int32[i] < 0) + rounding_mask) >>
     406             :                      (OD_COEFF_SHIFT - coeff_shift);
     407             :   }
     408             : 
     409             :   od_coding_order_to_raster(dqcoeff, blk_size, tx_type, dqcoeff_pvq, blk_size);
     410             : 
     411             :   eob = blk_size * blk_size;
     412             : 
     413             :   return eob;
     414             : }
     415             : 
     416             : static PVQ_SKIP_TYPE read_pvq_skip(AV1_COMMON *cm, MACROBLOCKD *const xd,
     417             :                                    int plane, TX_SIZE tx_size) {
     418             :   // decode ac/dc coded flag. bit0: DC coded, bit1 : AC coded
     419             :   // NOTE : we don't use 5 symbols for luma here in aom codebase,
     420             :   // since block partition is taken care of by aom.
     421             :   // So, only AC/DC skip info is coded
     422             :   const int ac_dc_coded = aom_read_symbol(
     423             :       xd->daala_dec.r,
     424             :       xd->daala_dec.state.adapt->skip_cdf[2 * tx_size + (plane != 0)], 4,
     425             :       "skip");
     426             :   if (ac_dc_coded < 0 || ac_dc_coded > 3) {
     427             :     aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
     428             :                        "Invalid PVQ Skip Type");
     429             :   }
     430             :   return ac_dc_coded;
     431             : }
     432             : 
     433             : static int av1_pvq_decode_helper2(AV1_COMMON *cm, MACROBLOCKD *const xd,
     434             :                                   MB_MODE_INFO *const mbmi, int plane, int row,
     435             :                                   int col, TX_SIZE tx_size, TX_TYPE tx_type) {
     436             :   struct macroblockd_plane *const pd = &xd->plane[plane];
     437             :   // transform block size in pixels
     438             :   int tx_blk_size = tx_size_wide[tx_size];
     439             :   int i, j;
     440             :   tran_low_t *pvq_ref_coeff = pd->pvq_ref_coeff;
     441             :   const int diff_stride = tx_blk_size;
     442             :   int16_t *pred = pd->pred;
     443             :   tran_low_t *const dqcoeff = pd->dqcoeff;
     444             :   uint8_t *dst;
     445             :   int eob;
     446             :   const PVQ_SKIP_TYPE ac_dc_coded = read_pvq_skip(cm, xd, plane, tx_size);
     447             : 
     448             :   eob = 0;
     449             :   dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
     450             : 
     451             :   if (ac_dc_coded) {
     452             :     int xdec = pd->subsampling_x;
     453             :     int seg_id = mbmi->segment_id;
     454             :     int16_t *quant;
     455             :     FWD_TXFM_PARAM fwd_txfm_param;
     456             :     // ToDo(yaowu): correct this with optimal number from decoding process.
     457             :     const int max_scan_line = tx_size_2d[tx_size];
     458             : #if CONFIG_HIGHBITDEPTH
     459             :     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
     460             :       for (j = 0; j < tx_blk_size; j++)
     461             :         for (i = 0; i < tx_blk_size; i++)
     462             :           pred[diff_stride * j + i] =
     463             :               CONVERT_TO_SHORTPTR(dst)[pd->dst.stride * j + i];
     464             :     } else {
     465             : #endif
     466             :       for (j = 0; j < tx_blk_size; j++)
     467             :         for (i = 0; i < tx_blk_size; i++)
     468             :           pred[diff_stride * j + i] = dst[pd->dst.stride * j + i];
     469             : #if CONFIG_HIGHBITDEPTH
     470             :     }
     471             : #endif
     472             : 
     473             :     fwd_txfm_param.tx_type = tx_type;
     474             :     fwd_txfm_param.tx_size = tx_size;
     475             :     fwd_txfm_param.lossless = xd->lossless[seg_id];
     476             : 
     477             : #if CONFIG_HIGHBITDEPTH
     478             :     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
     479             :       fwd_txfm_param.bd = xd->bd;
     480             :       av1_highbd_fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
     481             :     } else {
     482             : #endif  // CONFIG_HIGHBITDEPTH
     483             :       av1_fwd_txfm(pred, pvq_ref_coeff, diff_stride, &fwd_txfm_param);
     484             : #if CONFIG_HIGHBITDEPTH
     485             :     }
     486             : #endif  // CONFIG_HIGHBITDEPTH
     487             : 
     488             :     quant = &pd->seg_dequant[seg_id][0];  // aom's quantizer
     489             : 
     490             :     eob = av1_pvq_decode_helper(xd, pvq_ref_coeff, dqcoeff, quant, plane,
     491             :                                 tx_size, tx_type, xdec, ac_dc_coded);
     492             : 
     493             :     inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
     494             :                             max_scan_line, eob);
     495             :   }
     496             : 
     497             :   return eob;
     498             : }
     499             : #endif
     500             : 
     501           0 : static int get_block_idx(const MACROBLOCKD *xd, int plane, int row, int col) {
     502           0 :   const int bsize = xd->mi[0]->mbmi.sb_type;
     503           0 :   const struct macroblockd_plane *pd = &xd->plane[plane];
     504             : #if CONFIG_CB4X4
     505             : #if CONFIG_CHROMA_2X2
     506             :   const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
     507             : #else
     508           0 :   const BLOCK_SIZE plane_bsize =
     509           0 :       AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
     510             : #endif  // CONFIG_CHROMA_2X2
     511             : #else
     512             :   const BLOCK_SIZE plane_bsize =
     513             :       get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
     514             : #endif
     515           0 :   const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
     516           0 :   const TX_SIZE tx_size = get_tx_size(plane, xd);
     517           0 :   const uint8_t txh_unit = tx_size_high_unit[tx_size];
     518           0 :   return row * max_blocks_wide + col * txh_unit;
     519             : }
     520             : 
     521             : #if CONFIG_DPCM_INTRA
     522             : static void process_block_dpcm_vert(TX_SIZE tx_size, TX_TYPE_1D tx_type_1d,
     523             :                                     const tran_low_t *dqcoeff, uint8_t *dst,
     524             :                                     int dst_stride) {
     525             :   const int tx1d_width = tx_size_wide[tx_size];
     526             :   const int tx1d_height = tx_size_high[tx_size];
     527             :   dpcm_inv_txfm_add_func inverse_tx =
     528             :       av1_get_dpcm_inv_txfm_add_func(tx1d_width);
     529             :   for (int r = 0; r < tx1d_height; ++r) {
     530             :     if (r > 0) memcpy(dst, dst - dst_stride, tx1d_width * sizeof(dst[0]));
     531             :     inverse_tx(dqcoeff, 1, tx_type_1d, dst);
     532             :     dqcoeff += tx1d_width;
     533             :     dst += dst_stride;
     534             :   }
     535             : }
     536             : 
     537             : static void process_block_dpcm_horz(TX_SIZE tx_size, TX_TYPE_1D tx_type_1d,
     538             :                                     const tran_low_t *dqcoeff, uint8_t *dst,
     539             :                                     int dst_stride) {
     540             :   const int tx1d_width = tx_size_wide[tx_size];
     541             :   const int tx1d_height = tx_size_high[tx_size];
     542             :   dpcm_inv_txfm_add_func inverse_tx =
     543             :       av1_get_dpcm_inv_txfm_add_func(tx1d_height);
     544             :   tran_low_t tx_buff[64];
     545             :   for (int c = 0; c < tx1d_width; ++c, ++dqcoeff, ++dst) {
     546             :     for (int r = 0; r < tx1d_height; ++r) {
     547             :       if (c > 0) dst[r * dst_stride] = dst[r * dst_stride - 1];
     548             :       tx_buff[r] = dqcoeff[r * tx1d_width];
     549             :     }
     550             :     inverse_tx(tx_buff, dst_stride, tx_type_1d, dst);
     551             :   }
     552             : }
     553             : 
     554             : #if CONFIG_HIGHBITDEPTH
     555             : static void hbd_process_block_dpcm_vert(TX_SIZE tx_size, TX_TYPE_1D tx_type_1d,
     556             :                                         int bd, const tran_low_t *dqcoeff,
     557             :                                         uint8_t *dst8, int dst_stride) {
     558             :   uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
     559             :   const int tx1d_width = tx_size_wide[tx_size];
     560             :   const int tx1d_height = tx_size_high[tx_size];
     561             :   hbd_dpcm_inv_txfm_add_func inverse_tx =
     562             :       av1_get_hbd_dpcm_inv_txfm_add_func(tx1d_width);
     563             :   for (int r = 0; r < tx1d_height; ++r) {
     564             :     if (r > 0) memcpy(dst, dst - dst_stride, tx1d_width * sizeof(dst[0]));
     565             :     inverse_tx(dqcoeff, 1, tx_type_1d, bd, dst);
     566             :     dqcoeff += tx1d_width;
     567             :     dst += dst_stride;
     568             :   }
     569             : }
     570             : 
     571             : static void hbd_process_block_dpcm_horz(TX_SIZE tx_size, TX_TYPE_1D tx_type_1d,
     572             :                                         int bd, const tran_low_t *dqcoeff,
     573             :                                         uint8_t *dst8, int dst_stride) {
     574             :   uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
     575             :   const int tx1d_width = tx_size_wide[tx_size];
     576             :   const int tx1d_height = tx_size_high[tx_size];
     577             :   hbd_dpcm_inv_txfm_add_func inverse_tx =
     578             :       av1_get_hbd_dpcm_inv_txfm_add_func(tx1d_height);
     579             :   tran_low_t tx_buff[64];
     580             :   switch (tx1d_height) {
     581             :     case 4: inverse_tx = av1_hbd_dpcm_inv_txfm_add_4_c; break;
     582             :     case 8: inverse_tx = av1_hbd_dpcm_inv_txfm_add_8_c; break;
     583             :     case 16: inverse_tx = av1_hbd_dpcm_inv_txfm_add_16_c; break;
     584             :     case 32: inverse_tx = av1_hbd_dpcm_inv_txfm_add_32_c; break;
     585             :     default: assert(0);
     586             :   }
     587             : 
     588             :   for (int c = 0; c < tx1d_width; ++c, ++dqcoeff, ++dst) {
     589             :     for (int r = 0; r < tx1d_height; ++r) {
     590             :       if (c > 0) dst[r * dst_stride] = dst[r * dst_stride - 1];
     591             :       tx_buff[r] = dqcoeff[r * tx1d_width];
     592             :     }
     593             :     inverse_tx(tx_buff, dst_stride, tx_type_1d, bd, dst);
     594             :   }
     595             : }
     596             : #endif  // CONFIG_HIGHBITDEPTH
     597             : 
     598             : static void inverse_transform_block_dpcm(MACROBLOCKD *xd, int plane,
     599             :                                          PREDICTION_MODE mode, TX_SIZE tx_size,
     600             :                                          TX_TYPE tx_type, uint8_t *dst,
     601             :                                          int dst_stride, int16_t scan_line) {
     602             :   struct macroblockd_plane *const pd = &xd->plane[plane];
     603             :   tran_low_t *const dqcoeff = pd->dqcoeff;
     604             :   TX_TYPE_1D tx_type_1d = DCT_1D;
     605             :   switch (tx_type) {
     606             :     case IDTX: tx_type_1d = IDTX_1D; break;
     607             :     case V_DCT:
     608             :       assert(mode == H_PRED);
     609             :       tx_type_1d = DCT_1D;
     610             :       break;
     611             :     case H_DCT:
     612             :       assert(mode == V_PRED);
     613             :       tx_type_1d = DCT_1D;
     614             :       break;
     615             :     default: assert(0);
     616             :   }
     617             :   switch (mode) {
     618             :     case V_PRED:
     619             : #if CONFIG_HIGHBITDEPTH
     620             :       if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
     621             :         hbd_process_block_dpcm_vert(tx_size, tx_type_1d, xd->bd, dqcoeff, dst,
     622             :                                     dst_stride);
     623             :       } else {
     624             : #endif  // CONFIG_HIGHBITDEPTH
     625             :         process_block_dpcm_vert(tx_size, tx_type_1d, dqcoeff, dst, dst_stride);
     626             : #if CONFIG_HIGHBITDEPTH
     627             :       }
     628             : #endif  // CONFIG_HIGHBITDEPTH
     629             :       break;
     630             :     case H_PRED:
     631             : #if CONFIG_HIGHBITDEPTH
     632             :       if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
     633             :         hbd_process_block_dpcm_horz(tx_size, tx_type_1d, xd->bd, dqcoeff, dst,
     634             :                                     dst_stride);
     635             :       } else {
     636             : #endif  // CONFIG_HIGHBITDEPTH
     637             :         process_block_dpcm_horz(tx_size, tx_type_1d, dqcoeff, dst, dst_stride);
     638             : #if CONFIG_HIGHBITDEPTH
     639             :       }
     640             : #endif  // CONFIG_HIGHBITDEPTH
     641             :       break;
     642             :     default: assert(0);
     643             :   }
     644             :   memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
     645             : }
     646             : #endif  // CONFIG_DPCM_INTRA
     647             : 
     648           0 : static void predict_and_reconstruct_intra_block(
     649             :     AV1_COMMON *cm, MACROBLOCKD *const xd, aom_reader *const r,
     650             :     MB_MODE_INFO *const mbmi, int plane, int row, int col, TX_SIZE tx_size) {
     651           0 :   PLANE_TYPE plane_type = get_plane_type(plane);
     652           0 :   const int block_idx = get_block_idx(xd, plane, row, col);
     653             : #if CONFIG_PVQ
     654             :   (void)r;
     655             : #endif
     656           0 :   av1_predict_intra_block_facade(xd, plane, block_idx, col, row, tx_size);
     657             : 
     658           0 :   if (!mbmi->skip) {
     659             : #if !CONFIG_PVQ
     660           0 :     struct macroblockd_plane *const pd = &xd->plane[plane];
     661             : #if CONFIG_LV_MAP
     662             :     int16_t max_scan_line = 0;
     663             :     int eob;
     664             :     av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane,
     665             :                                pd->dqcoeff, &max_scan_line, &eob);
     666             :     // tx_type will be read out in av1_read_coeffs_txb_facade
     667             :     TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
     668             : #else   // CONFIG_LV_MAP
     669           0 :     TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
     670           0 :     const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 0);
     671           0 :     int16_t max_scan_line = 0;
     672           0 :     const int eob =
     673           0 :         av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
     674           0 :                                 tx_type, &max_scan_line, r, mbmi->segment_id);
     675             : #endif  // CONFIG_LV_MAP
     676           0 :     if (eob) {
     677           0 :       uint8_t *dst =
     678           0 :           &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
     679             : #if CONFIG_DPCM_INTRA
     680             :       const int block_raster_idx =
     681             :           av1_block_index_to_raster_order(tx_size, block_idx);
     682             :       const PREDICTION_MODE mode = (plane == 0)
     683             :                                        ? get_y_mode(xd->mi[0], block_raster_idx)
     684             :                                        : mbmi->uv_mode;
     685             :       if (av1_use_dpcm_intra(plane, mode, tx_type, mbmi)) {
     686             :         inverse_transform_block_dpcm(xd, plane, mode, tx_size, tx_type, dst,
     687             :                                      pd->dst.stride, max_scan_line);
     688             :       } else {
     689             : #endif  // CONFIG_DPCM_INTRA
     690           0 :         inverse_transform_block(xd, plane, tx_type, tx_size, dst,
     691             :                                 pd->dst.stride, max_scan_line, eob);
     692             : #if CONFIG_DPCM_INTRA
     693             :       }
     694             : #endif  // CONFIG_DPCM_INTRA
     695             :     }
     696             : #else
     697             :     TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
     698             :     av1_pvq_decode_helper2(cm, xd, mbmi, plane, row, col, tx_size, tx_type);
     699             : #endif
     700             :   }
     701             : #if CONFIG_CFL
     702             :   if (plane == AOM_PLANE_Y) {
     703             :     struct macroblockd_plane *const pd = &xd->plane[plane];
     704             :     uint8_t *dst =
     705             :         &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
     706             :     cfl_store(xd->cfl, dst, pd->dst.stride, row, col, tx_size);
     707             :   }
     708             : #endif
     709           0 : }
     710             : 
     711             : #if CONFIG_VAR_TX && !CONFIG_COEF_INTERLEAVE
     712           0 : static void decode_reconstruct_tx(AV1_COMMON *cm, MACROBLOCKD *const xd,
     713             :                                   aom_reader *r, MB_MODE_INFO *const mbmi,
     714             :                                   int plane, BLOCK_SIZE plane_bsize,
     715             :                                   int blk_row, int blk_col, TX_SIZE tx_size,
     716             :                                   int *eob_total) {
     717           0 :   const struct macroblockd_plane *const pd = &xd->plane[plane];
     718           0 :   const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
     719           0 :   const int tx_row = blk_row >> (1 - pd->subsampling_y);
     720           0 :   const int tx_col = blk_col >> (1 - pd->subsampling_x);
     721           0 :   const TX_SIZE plane_tx_size =
     722           0 :       plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
     723           0 :             : mbmi->inter_tx_size[tx_row][tx_col];
     724             :   // Scale to match transform block unit.
     725           0 :   const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
     726           0 :   const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
     727             : 
     728           0 :   if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
     729             : 
     730           0 :   if (tx_size == plane_tx_size) {
     731           0 :     PLANE_TYPE plane_type = get_plane_type(plane);
     732           0 :     int block_idx = get_block_idx(xd, plane, blk_row, blk_col);
     733             : #if CONFIG_LV_MAP
     734             :     int16_t max_scan_line = 0;
     735             :     int eob;
     736             :     av1_read_coeffs_txb_facade(cm, xd, r, blk_row, blk_col, block_idx, plane,
     737             :                                pd->dqcoeff, &max_scan_line, &eob);
     738             :     // tx_type will be read out in av1_read_coeffs_txb_facade
     739             :     TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
     740             : #else   // CONFIG_LV_MAP
     741           0 :     TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, plane_tx_size);
     742           0 :     const SCAN_ORDER *sc = get_scan(cm, plane_tx_size, tx_type, 1);
     743           0 :     int16_t max_scan_line = 0;
     744           0 :     const int eob = av1_decode_block_tokens(
     745             :         cm, xd, plane, sc, blk_col, blk_row, plane_tx_size, tx_type,
     746           0 :         &max_scan_line, r, mbmi->segment_id);
     747             : #endif  // CONFIG_LV_MAP
     748           0 :     inverse_transform_block(xd, plane, tx_type, plane_tx_size,
     749           0 :                             &pd->dst.buf[(blk_row * pd->dst.stride + blk_col)
     750           0 :                                          << tx_size_wide_log2[0]],
     751             :                             pd->dst.stride, max_scan_line, eob);
     752           0 :     *eob_total += eob;
     753             :   } else {
     754           0 :     const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
     755           0 :     const int bsl = tx_size_wide_unit[sub_txs];
     756           0 :     assert(sub_txs < tx_size);
     757             :     int i;
     758             : 
     759           0 :     assert(bsl > 0);
     760             : 
     761           0 :     for (i = 0; i < 4; ++i) {
     762           0 :       const int offsetr = blk_row + (i >> 1) * bsl;
     763           0 :       const int offsetc = blk_col + (i & 0x01) * bsl;
     764             : 
     765           0 :       if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
     766             : 
     767           0 :       decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, offsetr,
     768             :                             offsetc, sub_txs, eob_total);
     769             :     }
     770             :   }
     771             : }
     772             : #endif  // CONFIG_VAR_TX
     773             : 
     774             : #if !CONFIG_VAR_TX || CONFIG_SUPERTX || CONFIG_COEF_INTERLEAVE || \
     775             :     (!CONFIG_VAR_TX && CONFIG_EXT_TX && CONFIG_RECT_TX)
     776             : static int reconstruct_inter_block(AV1_COMMON *cm, MACROBLOCKD *const xd,
     777             :                                    aom_reader *const r, int segment_id,
     778             :                                    int plane, int row, int col,
     779             :                                    TX_SIZE tx_size) {
     780             :   PLANE_TYPE plane_type = get_plane_type(plane);
     781             :   int block_idx = get_block_idx(xd, plane, row, col);
     782             : #if CONFIG_PVQ
     783             :   int eob;
     784             :   (void)r;
     785             :   (void)segment_id;
     786             : #else
     787             :   struct macroblockd_plane *const pd = &xd->plane[plane];
     788             : #endif
     789             : 
     790             : #if !CONFIG_PVQ
     791             : #if CONFIG_LV_MAP
     792             :   (void)segment_id;
     793             :   int16_t max_scan_line = 0;
     794             :   int eob;
     795             :   av1_read_coeffs_txb_facade(cm, xd, r, row, col, block_idx, plane, pd->dqcoeff,
     796             :                              &max_scan_line, &eob);
     797             :   // tx_type will be read out in av1_read_coeffs_txb_facade
     798             :   TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
     799             : #else   // CONFIG_LV_MAP
     800             :   int16_t max_scan_line = 0;
     801             :   TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
     802             :   const SCAN_ORDER *scan_order = get_scan(cm, tx_size, tx_type, 1);
     803             :   const int eob =
     804             :       av1_decode_block_tokens(cm, xd, plane, scan_order, col, row, tx_size,
     805             :                               tx_type, &max_scan_line, r, segment_id);
     806             : #endif  // CONFIG_LV_MAP
     807             :   uint8_t *dst =
     808             :       &pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
     809             :   if (eob)
     810             :     inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
     811             :                             max_scan_line, eob);
     812             : #else
     813             :   TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
     814             :   eob = av1_pvq_decode_helper2(cm, xd, &xd->mi[0]->mbmi, plane, row, col,
     815             :                                tx_size, tx_type);
     816             : #endif
     817             :   return eob;
     818             : }
     819             : #endif  // !CONFIG_VAR_TX || CONFIG_SUPER_TX
     820             : 
     821           0 : static void set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
     822             :                         BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
     823             :                         int bh, int x_mis, int y_mis) {
     824           0 :   const int offset = mi_row * cm->mi_stride + mi_col;
     825             :   int x, y;
     826           0 :   const TileInfo *const tile = &xd->tile;
     827             : 
     828           0 :   xd->mi = cm->mi_grid_visible + offset;
     829           0 :   xd->mi[0] = &cm->mi[offset];
     830             :   // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
     831             :   // passing bsize from decode_partition().
     832           0 :   xd->mi[0]->mbmi.sb_type = bsize;
     833             : #if CONFIG_RD_DEBUG
     834             :   xd->mi[0]->mbmi.mi_row = mi_row;
     835             :   xd->mi[0]->mbmi.mi_col = mi_col;
     836             : #endif
     837           0 :   for (y = 0; y < y_mis; ++y)
     838           0 :     for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
     839             : 
     840           0 :   set_plane_n4(xd, bw, bh);
     841           0 :   set_skip_context(xd, mi_row, mi_col);
     842             : 
     843             : #if CONFIG_VAR_TX
     844           0 :   xd->max_tx_size = max_txsize_lookup[bsize];
     845             : #endif
     846             : 
     847             :   // Distance of Mb to the various image edges. These are specified to 8th pel
     848             :   // as they are always compared to values that are in 1/8th pel units
     849           0 :   set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw,
     850             : #if CONFIG_DEPENDENT_HORZTILES
     851             :                  cm->dependent_horz_tiles,
     852             : #endif  // CONFIG_DEPENDENT_HORZTILES
     853             :                  cm->mi_rows, cm->mi_cols);
     854             : 
     855           0 :   av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row,
     856             :                        mi_col);
     857           0 : }
     858             : 
     859             : #if CONFIG_SUPERTX
     860             : static MB_MODE_INFO *set_offsets_extend(AV1_COMMON *const cm,
     861             :                                         MACROBLOCKD *const xd,
     862             :                                         const TileInfo *const tile,
     863             :                                         BLOCK_SIZE bsize_pred, int mi_row_pred,
     864             :                                         int mi_col_pred, int mi_row_ori,
     865             :                                         int mi_col_ori) {
     866             :   // Used in supertx
     867             :   // (mi_row_ori, mi_col_ori): location for mv
     868             :   // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
     869             :   const int bw = mi_size_wide[bsize_pred];
     870             :   const int bh = mi_size_high[bsize_pred];
     871             :   const int offset = mi_row_ori * cm->mi_stride + mi_col_ori;
     872             :   xd->mi = cm->mi_grid_visible + offset;
     873             :   xd->mi[0] = cm->mi + offset;
     874             :   set_mi_row_col(xd, tile, mi_row_pred, bh, mi_col_pred, bw,
     875             : #if CONFIG_DEPENDENT_HORZTILES
     876             :                  cm->dependent_horz_tiles,
     877             : #endif  // CONFIG_DEPENDENT_HORZTILES
     878             :                  cm->mi_rows, cm->mi_cols);
     879             : 
     880             :   xd->up_available = (mi_row_ori > tile->mi_row_start);
     881             :   xd->left_available = (mi_col_ori > tile->mi_col_start);
     882             : 
     883             :   set_plane_n4(xd, bw, bh);
     884             : 
     885             :   return &xd->mi[0]->mbmi;
     886             : }
     887             : 
     888             : #if CONFIG_SUPERTX
     889             : static MB_MODE_INFO *set_mb_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
     890             :                                     BLOCK_SIZE bsize, int mi_row, int mi_col,
     891             :                                     int bw, int bh, int x_mis, int y_mis) {
     892             :   const int offset = mi_row * cm->mi_stride + mi_col;
     893             :   const TileInfo *const tile = &xd->tile;
     894             :   int x, y;
     895             : 
     896             :   xd->mi = cm->mi_grid_visible + offset;
     897             :   xd->mi[0] = cm->mi + offset;
     898             :   xd->mi[0]->mbmi.sb_type = bsize;
     899             :   for (y = 0; y < y_mis; ++y)
     900             :     for (x = !y; x < x_mis; ++x) xd->mi[y * cm->mi_stride + x] = xd->mi[0];
     901             : 
     902             :   set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw,
     903             : #if CONFIG_DEPENDENT_HORZTILES
     904             :                  cm->dependent_horz_tiles,
     905             : #endif  // CONFIG_DEPENDENT_HORZTILES
     906             :                  cm->mi_rows, cm->mi_cols);
     907             :   return &xd->mi[0]->mbmi;
     908             : }
     909             : #endif
     910             : 
     911             : static void set_offsets_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
     912             :                                  const TileInfo *const tile, BLOCK_SIZE bsize,
     913             :                                  int mi_row, int mi_col) {
     914             :   const int bw = mi_size_wide[bsize];
     915             :   const int bh = mi_size_high[bsize];
     916             :   const int offset = mi_row * cm->mi_stride + mi_col;
     917             : 
     918             :   xd->mi = cm->mi_grid_visible + offset;
     919             :   xd->mi[0] = cm->mi + offset;
     920             : 
     921             :   set_plane_n4(xd, bw, bh);
     922             : 
     923             :   set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw,
     924             : #if CONFIG_DEPENDENT_HORZTILES
     925             :                  cm->dependent_horz_tiles,
     926             : #endif  // CONFIG_DEPENDENT_HORZTILES
     927             :                  cm->mi_rows, cm->mi_cols);
     928             : 
     929             :   av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row,
     930             :                        mi_col);
     931             : }
     932             : 
     933             : static void set_param_topblock(AV1_COMMON *const cm, MACROBLOCKD *const xd,
     934             :                                BLOCK_SIZE bsize, int mi_row, int mi_col,
     935             :                                int txfm, int skip) {
     936             :   const int bw = mi_size_wide[bsize];
     937             :   const int bh = mi_size_high[bsize];
     938             :   const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
     939             :   const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
     940             :   const int offset = mi_row * cm->mi_stride + mi_col;
     941             :   int x, y;
     942             : 
     943             :   xd->mi = cm->mi_grid_visible + offset;
     944             :   xd->mi[0] = cm->mi + offset;
     945             : 
     946             :   for (y = 0; y < y_mis; ++y)
     947             :     for (x = 0; x < x_mis; ++x) {
     948             :       xd->mi[y * cm->mi_stride + x]->mbmi.skip = skip;
     949             :       xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
     950             :     }
     951             : #if CONFIG_VAR_TX
     952             :   xd->above_txfm_context = cm->above_txfm_context + mi_col;
     953             :   xd->left_txfm_context =
     954             :       xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
     955             :   set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bw, bh, skip, xd);
     956             : #endif
     957             : }
     958             : 
     959             : static void set_ref(AV1_COMMON *const cm, MACROBLOCKD *const xd, int idx,
     960             :                     int mi_row, int mi_col) {
     961             :   MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
     962             :   RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
     963             :   xd->block_refs[idx] = ref_buffer;
     964             :   if (!av1_is_valid_scale(&ref_buffer->sf))
     965             :     aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
     966             :                        "Invalid scale factors");
     967             :   av1_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
     968             :                        &ref_buffer->sf);
     969             :   aom_merge_corrupted_flag(&xd->corrupted, ref_buffer->buf->corrupted);
     970             : }
     971             : 
     972             : static void dec_predict_b_extend(
     973             :     AV1Decoder *const pbi, MACROBLOCKD *const xd, const TileInfo *const tile,
     974             :     int block, int mi_row_ori, int mi_col_ori, int mi_row_pred, int mi_col_pred,
     975             :     int mi_row_top, int mi_col_top, uint8_t *dst_buf[3], int dst_stride[3],
     976             :     BLOCK_SIZE bsize_top, BLOCK_SIZE bsize_pred, int b_sub8x8, int bextend) {
     977             :   // Used in supertx
     978             :   // (mi_row_ori, mi_col_ori): location for mv
     979             :   // (mi_row_pred, mi_col_pred, bsize_pred): region to predict
     980             :   // (mi_row_top, mi_col_top, bsize_top): region of the top partition size
     981             :   // block: sub location of sub8x8 blocks
     982             :   // b_sub8x8: 1: ori is sub8x8; 0: ori is not sub8x8
     983             :   // bextend: 1: region to predict is an extension of ori; 0: not
     984             :   int r = (mi_row_pred - mi_row_top) * MI_SIZE;
     985             :   int c = (mi_col_pred - mi_col_top) * MI_SIZE;
     986             :   const int mi_width_top = mi_size_wide[bsize_top];
     987             :   const int mi_height_top = mi_size_high[bsize_top];
     988             :   MB_MODE_INFO *mbmi;
     989             :   AV1_COMMON *const cm = &pbi->common;
     990             : 
     991             :   if (mi_row_pred < mi_row_top || mi_col_pred < mi_col_top ||
     992             :       mi_row_pred >= mi_row_top + mi_height_top ||
     993             :       mi_col_pred >= mi_col_top + mi_width_top || mi_row_pred >= cm->mi_rows ||
     994             :       mi_col_pred >= cm->mi_cols)
     995             :     return;
     996             : 
     997             :   mbmi = set_offsets_extend(cm, xd, tile, bsize_pred, mi_row_pred, mi_col_pred,
     998             :                             mi_row_ori, mi_col_ori);
     999             :   set_ref(cm, xd, 0, mi_row_pred, mi_col_pred);
    1000             :   if (has_second_ref(&xd->mi[0]->mbmi))
    1001             :     set_ref(cm, xd, 1, mi_row_pred, mi_col_pred);
    1002             : 
    1003             :   if (!bextend) mbmi->tx_size = max_txsize_lookup[bsize_top];
    1004             : 
    1005             :   xd->plane[0].dst.stride = dst_stride[0];
    1006             :   xd->plane[1].dst.stride = dst_stride[1];
    1007             :   xd->plane[2].dst.stride = dst_stride[2];
    1008             :   xd->plane[0].dst.buf = dst_buf[0] +
    1009             :                          (r >> xd->plane[0].subsampling_y) * dst_stride[0] +
    1010             :                          (c >> xd->plane[0].subsampling_x);
    1011             :   xd->plane[1].dst.buf = dst_buf[1] +
    1012             :                          (r >> xd->plane[1].subsampling_y) * dst_stride[1] +
    1013             :                          (c >> xd->plane[1].subsampling_x);
    1014             :   xd->plane[2].dst.buf = dst_buf[2] +
    1015             :                          (r >> xd->plane[2].subsampling_y) * dst_stride[2] +
    1016             :                          (c >> xd->plane[2].subsampling_x);
    1017             : 
    1018             :   if (!b_sub8x8)
    1019             :     av1_build_inter_predictors_sb_extend(&pbi->common, xd,
    1020             : #if CONFIG_EXT_INTER
    1021             :                                          mi_row_ori, mi_col_ori,
    1022             : #endif  // CONFIG_EXT_INTER
    1023             :                                          mi_row_pred, mi_col_pred, bsize_pred);
    1024             :   else
    1025             :     av1_build_inter_predictors_sb_sub8x8_extend(&pbi->common, xd,
    1026             : #if CONFIG_EXT_INTER
    1027             :                                                 mi_row_ori, mi_col_ori,
    1028             : #endif  // CONFIG_EXT_INTER
    1029             :                                                 mi_row_pred, mi_col_pred,
    1030             :                                                 bsize_pred, block);
    1031             : }
    1032             : 
    1033             : static void dec_extend_dir(AV1Decoder *const pbi, MACROBLOCKD *const xd,
    1034             :                            const TileInfo *const tile, int block,
    1035             :                            BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
    1036             :                            int mi_col, int mi_row_top, int mi_col_top,
    1037             :                            uint8_t *dst_buf[3], int dst_stride[3], int dir) {
    1038             :   // dir: 0-lower, 1-upper, 2-left, 3-right
    1039             :   //      4-lowerleft, 5-upperleft, 6-lowerright, 7-upperright
    1040             :   const int mi_width = mi_size_wide[bsize];
    1041             :   const int mi_height = mi_size_high[bsize];
    1042             :   int xss = xd->plane[1].subsampling_x;
    1043             :   int yss = xd->plane[1].subsampling_y;
    1044             : #if CONFIG_CB4X4
    1045             :   const int unify_bsize = 1;
    1046             : #else
    1047             :   const int unify_bsize = 0;
    1048             : #endif
    1049             :   int b_sub8x8 = (bsize < BLOCK_8X8) && !unify_bsize ? 1 : 0;
    1050             :   BLOCK_SIZE extend_bsize;
    1051             :   int mi_row_pred, mi_col_pred;
    1052             : 
    1053             :   int wide_unit, high_unit;
    1054             :   int i, j;
    1055             :   int ext_offset = 0;
    1056             : 
    1057             :   if (dir == 0 || dir == 1) {
    1058             :     extend_bsize =
    1059             :         (mi_width == mi_size_wide[BLOCK_8X8] || bsize < BLOCK_8X8 || xss < yss)
    1060             :             ? BLOCK_8X8
    1061             :             : BLOCK_16X8;
    1062             : #if CONFIG_CB4X4
    1063             :     if (bsize < BLOCK_8X8) {
    1064             :       extend_bsize = BLOCK_4X4;
    1065             :       ext_offset = mi_size_wide[BLOCK_8X8];
    1066             :     }
    1067             : #endif
    1068             : 
    1069             :     wide_unit = mi_size_wide[extend_bsize];
    1070             :     high_unit = mi_size_high[extend_bsize];
    1071             : 
    1072             :     mi_row_pred = mi_row + ((dir == 0) ? mi_height : -(mi_height + ext_offset));
    1073             :     mi_col_pred = mi_col;
    1074             : 
    1075             :     for (j = 0; j < mi_height + ext_offset; j += high_unit)
    1076             :       for (i = 0; i < mi_width + ext_offset; i += wide_unit)
    1077             :         dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
    1078             :                              mi_row_pred + j, mi_col_pred + i, mi_row_top,
    1079             :                              mi_col_top, dst_buf, dst_stride, top_bsize,
    1080             :                              extend_bsize, b_sub8x8, 1);
    1081             :   } else if (dir == 2 || dir == 3) {
    1082             :     extend_bsize =
    1083             :         (mi_height == mi_size_high[BLOCK_8X8] || bsize < BLOCK_8X8 || yss < xss)
    1084             :             ? BLOCK_8X8
    1085             :             : BLOCK_8X16;
    1086             : #if CONFIG_CB4X4
    1087             :     if (bsize < BLOCK_8X8) {
    1088             :       extend_bsize = BLOCK_4X4;
    1089             :       ext_offset = mi_size_wide[BLOCK_8X8];
    1090             :     }
    1091             : #endif
    1092             : 
    1093             :     wide_unit = mi_size_wide[extend_bsize];
    1094             :     high_unit = mi_size_high[extend_bsize];
    1095             : 
    1096             :     mi_row_pred = mi_row;
    1097             :     mi_col_pred = mi_col + ((dir == 3) ? mi_width : -(mi_width + ext_offset));
    1098             : 
    1099             :     for (j = 0; j < mi_height + ext_offset; j += high_unit)
    1100             :       for (i = 0; i < mi_width + ext_offset; i += wide_unit)
    1101             :         dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
    1102             :                              mi_row_pred + j, mi_col_pred + i, mi_row_top,
    1103             :                              mi_col_top, dst_buf, dst_stride, top_bsize,
    1104             :                              extend_bsize, b_sub8x8, 1);
    1105             :   } else {
    1106             :     extend_bsize = BLOCK_8X8;
    1107             : #if CONFIG_CB4X4
    1108             :     if (bsize < BLOCK_8X8) {
    1109             :       extend_bsize = BLOCK_4X4;
    1110             :       ext_offset = mi_size_wide[BLOCK_8X8];
    1111             :     }
    1112             : #endif
    1113             :     wide_unit = mi_size_wide[extend_bsize];
    1114             :     high_unit = mi_size_high[extend_bsize];
    1115             : 
    1116             :     mi_row_pred = mi_row + ((dir == 4 || dir == 6) ? mi_height
    1117             :                                                    : -(mi_height + ext_offset));
    1118             :     mi_col_pred =
    1119             :         mi_col + ((dir == 6 || dir == 7) ? mi_width : -(mi_width + ext_offset));
    1120             : 
    1121             :     for (j = 0; j < mi_height + ext_offset; j += high_unit)
    1122             :       for (i = 0; i < mi_width + ext_offset; i += wide_unit)
    1123             :         dec_predict_b_extend(pbi, xd, tile, block, mi_row, mi_col,
    1124             :                              mi_row_pred + j, mi_col_pred + i, mi_row_top,
    1125             :                              mi_col_top, dst_buf, dst_stride, top_bsize,
    1126             :                              extend_bsize, b_sub8x8, 1);
    1127             :   }
    1128             : }
    1129             : 
    1130             : static void dec_extend_all(AV1Decoder *const pbi, MACROBLOCKD *const xd,
    1131             :                            const TileInfo *const tile, int block,
    1132             :                            BLOCK_SIZE bsize, BLOCK_SIZE top_bsize, int mi_row,
    1133             :                            int mi_col, int mi_row_top, int mi_col_top,
    1134             :                            uint8_t *dst_buf[3], int dst_stride[3]) {
    1135             :   for (int i = 0; i < 8; ++i) {
    1136             :     dec_extend_dir(pbi, xd, tile, block, bsize, top_bsize, mi_row, mi_col,
    1137             :                    mi_row_top, mi_col_top, dst_buf, dst_stride, i);
    1138             :   }
    1139             : }
    1140             : 
    1141             : static void dec_predict_sb_complex(AV1Decoder *const pbi, MACROBLOCKD *const xd,
    1142             :                                    const TileInfo *const tile, int mi_row,
    1143             :                                    int mi_col, int mi_row_top, int mi_col_top,
    1144             :                                    BLOCK_SIZE bsize, BLOCK_SIZE top_bsize,
    1145             :                                    uint8_t *dst_buf[3], int dst_stride[3]) {
    1146             :   const AV1_COMMON *const cm = &pbi->common;
    1147             :   const int hbs = mi_size_wide[bsize] / 2;
    1148             :   const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
    1149             :   const BLOCK_SIZE subsize = get_subsize(bsize, partition);
    1150             : #if CONFIG_EXT_PARTITION_TYPES
    1151             :   const BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
    1152             : #endif
    1153             :   int i;
    1154             :   const int mi_offset = mi_row * cm->mi_stride + mi_col;
    1155             :   uint8_t *dst_buf1[3], *dst_buf2[3], *dst_buf3[3];
    1156             : #if CONFIG_CB4X4
    1157             :   const int unify_bsize = 1;
    1158             : #else
    1159             :   const int unify_bsize = 0;
    1160             : #endif
    1161             : 
    1162             :   DECLARE_ALIGNED(16, uint8_t, tmp_buf1[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
    1163             :   DECLARE_ALIGNED(16, uint8_t, tmp_buf2[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
    1164             :   DECLARE_ALIGNED(16, uint8_t, tmp_buf3[MAX_MB_PLANE * MAX_TX_SQUARE * 2]);
    1165             :   int dst_stride1[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
    1166             :   int dst_stride2[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
    1167             :   int dst_stride3[3] = { MAX_TX_SIZE, MAX_TX_SIZE, MAX_TX_SIZE };
    1168             : 
    1169             : #if CONFIG_HIGHBITDEPTH
    1170             :   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    1171             :     int len = sizeof(uint16_t);
    1172             :     dst_buf1[0] = CONVERT_TO_BYTEPTR(tmp_buf1);
    1173             :     dst_buf1[1] = CONVERT_TO_BYTEPTR(tmp_buf1 + MAX_TX_SQUARE * len);
    1174             :     dst_buf1[2] = CONVERT_TO_BYTEPTR(tmp_buf1 + 2 * MAX_TX_SQUARE * len);
    1175             :     dst_buf2[0] = CONVERT_TO_BYTEPTR(tmp_buf2);
    1176             :     dst_buf2[1] = CONVERT_TO_BYTEPTR(tmp_buf2 + MAX_TX_SQUARE * len);
    1177             :     dst_buf2[2] = CONVERT_TO_BYTEPTR(tmp_buf2 + 2 * MAX_TX_SQUARE * len);
    1178             :     dst_buf3[0] = CONVERT_TO_BYTEPTR(tmp_buf3);
    1179             :     dst_buf3[1] = CONVERT_TO_BYTEPTR(tmp_buf3 + MAX_TX_SQUARE * len);
    1180             :     dst_buf3[2] = CONVERT_TO_BYTEPTR(tmp_buf3 + 2 * MAX_TX_SQUARE * len);
    1181             :   } else {
    1182             : #endif
    1183             :     dst_buf1[0] = tmp_buf1;
    1184             :     dst_buf1[1] = tmp_buf1 + MAX_TX_SQUARE;
    1185             :     dst_buf1[2] = tmp_buf1 + 2 * MAX_TX_SQUARE;
    1186             :     dst_buf2[0] = tmp_buf2;
    1187             :     dst_buf2[1] = tmp_buf2 + MAX_TX_SQUARE;
    1188             :     dst_buf2[2] = tmp_buf2 + 2 * MAX_TX_SQUARE;
    1189             :     dst_buf3[0] = tmp_buf3;
    1190             :     dst_buf3[1] = tmp_buf3 + MAX_TX_SQUARE;
    1191             :     dst_buf3[2] = tmp_buf3 + 2 * MAX_TX_SQUARE;
    1192             : #if CONFIG_HIGHBITDEPTH
    1193             :   }
    1194             : #endif
    1195             : 
    1196             :   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
    1197             : 
    1198             :   xd->mi = cm->mi_grid_visible + mi_offset;
    1199             :   xd->mi[0] = cm->mi + mi_offset;
    1200             : 
    1201             :   for (i = 0; i < MAX_MB_PLANE; i++) {
    1202             :     xd->plane[i].dst.buf = dst_buf[i];
    1203             :     xd->plane[i].dst.stride = dst_stride[i];
    1204             :   }
    1205             : 
    1206             :   switch (partition) {
    1207             :     case PARTITION_NONE:
    1208             :       assert(bsize < top_bsize);
    1209             :       dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
    1210             :                            mi_row_top, mi_col_top, dst_buf, dst_stride,
    1211             :                            top_bsize, bsize, 0, 0);
    1212             :       dec_extend_all(pbi, xd, tile, 0, bsize, top_bsize, mi_row, mi_col,
    1213             :                      mi_row_top, mi_col_top, dst_buf, dst_stride);
    1214             :       break;
    1215             :     case PARTITION_HORZ:
    1216             :       if (bsize == BLOCK_8X8 && !unify_bsize) {
    1217             :         // For sub8x8, predict in 8x8 unit
    1218             :         // First half
    1219             :         dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
    1220             :                              mi_row_top, mi_col_top, dst_buf, dst_stride,
    1221             :                              top_bsize, BLOCK_8X8, 1, 0);
    1222             :         if (bsize < top_bsize)
    1223             :           dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
    1224             :                          mi_row_top, mi_col_top, dst_buf, dst_stride);
    1225             : 
    1226             :         // Second half
    1227             :         dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
    1228             :                              mi_row_top, mi_col_top, dst_buf1, dst_stride1,
    1229             :                              top_bsize, BLOCK_8X8, 1, 1);
    1230             :         if (bsize < top_bsize)
    1231             :           dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
    1232             :                          mi_row_top, mi_col_top, dst_buf1, dst_stride1);
    1233             : 
    1234             :         // weighted average to smooth the boundary
    1235             :         xd->plane[0].dst.buf = dst_buf[0];
    1236             :         xd->plane[0].dst.stride = dst_stride[0];
    1237             :         av1_build_masked_inter_predictor_complex(
    1238             :             xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
    1239             :             mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
    1240             :             0);
    1241             :       } else {
    1242             :         // First half
    1243             :         dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
    1244             :                              mi_row_top, mi_col_top, dst_buf, dst_stride,
    1245             :                              top_bsize, subsize, 0, 0);
    1246             :         if (bsize < top_bsize)
    1247             :           dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
    1248             :                          mi_row_top, mi_col_top, dst_buf, dst_stride);
    1249             :         else
    1250             :           dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
    1251             :                          mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
    1252             : 
    1253             :         if (mi_row + hbs < cm->mi_rows) {
    1254             :           // Second half
    1255             :           dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col,
    1256             :                                mi_row + hbs, mi_col, mi_row_top, mi_col_top,
    1257             :                                dst_buf1, dst_stride1, top_bsize, subsize, 0, 0);
    1258             :           if (bsize < top_bsize)
    1259             :             dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
    1260             :                            mi_col, mi_row_top, mi_col_top, dst_buf1,
    1261             :                            dst_stride1);
    1262             :           else
    1263             :             dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
    1264             :                            mi_col, mi_row_top, mi_col_top, dst_buf1,
    1265             :                            dst_stride1, 1);
    1266             : 
    1267             :           // weighted average to smooth the boundary
    1268             :           for (i = 0; i < MAX_MB_PLANE; i++) {
    1269             :             xd->plane[i].dst.buf = dst_buf[i];
    1270             :             xd->plane[i].dst.stride = dst_stride[i];
    1271             :             av1_build_masked_inter_predictor_complex(
    1272             :                 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
    1273             :                 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
    1274             :                 PARTITION_HORZ, i);
    1275             :           }
    1276             :         }
    1277             :       }
    1278             :       break;
    1279             :     case PARTITION_VERT:
    1280             :       if (bsize == BLOCK_8X8 && !unify_bsize) {
    1281             :         // First half
    1282             :         dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
    1283             :                              mi_row_top, mi_col_top, dst_buf, dst_stride,
    1284             :                              top_bsize, BLOCK_8X8, 1, 0);
    1285             :         if (bsize < top_bsize)
    1286             :           dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
    1287             :                          mi_row_top, mi_col_top, dst_buf, dst_stride);
    1288             : 
    1289             :         // Second half
    1290             :         dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
    1291             :                              mi_row_top, mi_col_top, dst_buf1, dst_stride1,
    1292             :                              top_bsize, BLOCK_8X8, 1, 1);
    1293             :         if (bsize < top_bsize)
    1294             :           dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
    1295             :                          mi_row_top, mi_col_top, dst_buf1, dst_stride1);
    1296             : 
    1297             :         // Smooth
    1298             :         xd->plane[0].dst.buf = dst_buf[0];
    1299             :         xd->plane[0].dst.stride = dst_stride[0];
    1300             :         av1_build_masked_inter_predictor_complex(
    1301             :             xd, dst_buf[0], dst_stride[0], dst_buf1[0], dst_stride1[0], mi_row,
    1302             :             mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
    1303             :             0);
    1304             :       } else {
    1305             :         // First half
    1306             :         dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
    1307             :                              mi_row_top, mi_col_top, dst_buf, dst_stride,
    1308             :                              top_bsize, subsize, 0, 0);
    1309             :         if (bsize < top_bsize)
    1310             :           dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
    1311             :                          mi_row_top, mi_col_top, dst_buf, dst_stride);
    1312             :         else
    1313             :           dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
    1314             :                          mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
    1315             : 
    1316             :         // Second half
    1317             :         if (mi_col + hbs < cm->mi_cols) {
    1318             :           dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
    1319             :                                mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
    1320             :                                dst_stride1, top_bsize, subsize, 0, 0);
    1321             :           if (bsize < top_bsize)
    1322             :             dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
    1323             :                            mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
    1324             :                            dst_stride1);
    1325             :           else
    1326             :             dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
    1327             :                            mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
    1328             :                            dst_stride1, 2);
    1329             : 
    1330             :           // Smooth
    1331             :           for (i = 0; i < MAX_MB_PLANE; i++) {
    1332             :             xd->plane[i].dst.buf = dst_buf[i];
    1333             :             xd->plane[i].dst.stride = dst_stride[i];
    1334             :             av1_build_masked_inter_predictor_complex(
    1335             :                 xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
    1336             :                 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
    1337             :                 PARTITION_VERT, i);
    1338             :           }
    1339             :         }
    1340             :       }
    1341             :       break;
    1342             :     case PARTITION_SPLIT:
    1343             :       if (bsize == BLOCK_8X8 && !unify_bsize) {
    1344             :         dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
    1345             :                              mi_row_top, mi_col_top, dst_buf, dst_stride,
    1346             :                              top_bsize, BLOCK_8X8, 1, 0);
    1347             :         dec_predict_b_extend(pbi, xd, tile, 1, mi_row, mi_col, mi_row, mi_col,
    1348             :                              mi_row_top, mi_col_top, dst_buf1, dst_stride1,
    1349             :                              top_bsize, BLOCK_8X8, 1, 1);
    1350             :         dec_predict_b_extend(pbi, xd, tile, 2, mi_row, mi_col, mi_row, mi_col,
    1351             :                              mi_row_top, mi_col_top, dst_buf2, dst_stride2,
    1352             :                              top_bsize, BLOCK_8X8, 1, 1);
    1353             :         dec_predict_b_extend(pbi, xd, tile, 3, mi_row, mi_col, mi_row, mi_col,
    1354             :                              mi_row_top, mi_col_top, dst_buf3, dst_stride3,
    1355             :                              top_bsize, BLOCK_8X8, 1, 1);
    1356             :         if (bsize < top_bsize) {
    1357             :           dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
    1358             :                          mi_row_top, mi_col_top, dst_buf, dst_stride);
    1359             :           dec_extend_all(pbi, xd, tile, 1, subsize, top_bsize, mi_row, mi_col,
    1360             :                          mi_row_top, mi_col_top, dst_buf1, dst_stride1);
    1361             :           dec_extend_all(pbi, xd, tile, 2, subsize, top_bsize, mi_row, mi_col,
    1362             :                          mi_row_top, mi_col_top, dst_buf2, dst_stride2);
    1363             :           dec_extend_all(pbi, xd, tile, 3, subsize, top_bsize, mi_row, mi_col,
    1364             :                          mi_row_top, mi_col_top, dst_buf3, dst_stride3);
    1365             :         }
    1366             :       } else {
    1367             :         dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row_top,
    1368             :                                mi_col_top, subsize, top_bsize, dst_buf,
    1369             :                                dst_stride);
    1370             :         if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols)
    1371             :           dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col + hbs,
    1372             :                                  mi_row_top, mi_col_top, subsize, top_bsize,
    1373             :                                  dst_buf1, dst_stride1);
    1374             :         if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols)
    1375             :           dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col,
    1376             :                                  mi_row_top, mi_col_top, subsize, top_bsize,
    1377             :                                  dst_buf2, dst_stride2);
    1378             :         if (mi_row + hbs < cm->mi_rows && mi_col + hbs < cm->mi_cols)
    1379             :           dec_predict_sb_complex(pbi, xd, tile, mi_row + hbs, mi_col + hbs,
    1380             :                                  mi_row_top, mi_col_top, subsize, top_bsize,
    1381             :                                  dst_buf3, dst_stride3);
    1382             :       }
    1383             :       for (i = 0; i < MAX_MB_PLANE; i++) {
    1384             : #if !CONFIG_CB4X4
    1385             :         if (bsize == BLOCK_8X8 && i != 0)
    1386             :           continue;  // Skip <4x4 chroma smoothing
    1387             : #endif
    1388             :         if (mi_row < cm->mi_rows && mi_col + hbs < cm->mi_cols) {
    1389             :           av1_build_masked_inter_predictor_complex(
    1390             :               xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i],
    1391             :               mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
    1392             :               PARTITION_VERT, i);
    1393             :           if (mi_row + hbs < cm->mi_rows) {
    1394             :             av1_build_masked_inter_predictor_complex(
    1395             :                 xd, dst_buf2[i], dst_stride2[i], dst_buf3[i], dst_stride3[i],
    1396             :                 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
    1397             :                 PARTITION_VERT, i);
    1398             :             av1_build_masked_inter_predictor_complex(
    1399             :                 xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
    1400             :                 mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
    1401             :                 PARTITION_HORZ, i);
    1402             :           }
    1403             :         } else if (mi_row + hbs < cm->mi_rows && mi_col < cm->mi_cols) {
    1404             :           av1_build_masked_inter_predictor_complex(
    1405             :               xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i],
    1406             :               mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
    1407             :               PARTITION_HORZ, i);
    1408             :         }
    1409             :       }
    1410             :       break;
    1411             : #if CONFIG_EXT_PARTITION_TYPES
    1412             :     case PARTITION_HORZ_A:
    1413             :       dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
    1414             :                            mi_row_top, mi_col_top, dst_buf, dst_stride,
    1415             :                            top_bsize, bsize2, 0, 0);
    1416             :       dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
    1417             :                      mi_row_top, mi_col_top, dst_buf, dst_stride);
    1418             : 
    1419             :       dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
    1420             :                            mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
    1421             :                            dst_stride1, top_bsize, bsize2, 0, 0);
    1422             :       dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
    1423             :                      mi_row_top, mi_col_top, dst_buf1, dst_stride1);
    1424             : 
    1425             :       dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
    1426             :                            mi_col, mi_row_top, mi_col_top, dst_buf2,
    1427             :                            dst_stride2, top_bsize, subsize, 0, 0);
    1428             :       if (bsize < top_bsize)
    1429             :         dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
    1430             :                        mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2);
    1431             :       else
    1432             :         dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row + hbs,
    1433             :                        mi_col, mi_row_top, mi_col_top, dst_buf2, dst_stride2,
    1434             :                        1);
    1435             : 
    1436             :       for (i = 0; i < MAX_MB_PLANE; i++) {
    1437             :         xd->plane[i].dst.buf = dst_buf[i];
    1438             :         xd->plane[i].dst.stride = dst_stride[i];
    1439             :         av1_build_masked_inter_predictor_complex(
    1440             :             xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
    1441             :             mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
    1442             :             i);
    1443             :       }
    1444             :       for (i = 0; i < MAX_MB_PLANE; i++) {
    1445             :         av1_build_masked_inter_predictor_complex(
    1446             :             xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
    1447             :             mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
    1448             :             i);
    1449             :       }
    1450             :       break;
    1451             :     case PARTITION_VERT_A:
    1452             : 
    1453             :       dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
    1454             :                            mi_row_top, mi_col_top, dst_buf, dst_stride,
    1455             :                            top_bsize, bsize2, 0, 0);
    1456             :       dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col,
    1457             :                      mi_row_top, mi_col_top, dst_buf, dst_stride);
    1458             : 
    1459             :       dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
    1460             :                            mi_col, mi_row_top, mi_col_top, dst_buf1,
    1461             :                            dst_stride1, top_bsize, bsize2, 0, 0);
    1462             :       dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
    1463             :                      mi_row_top, mi_col_top, dst_buf1, dst_stride1);
    1464             : 
    1465             :       dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
    1466             :                            mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
    1467             :                            dst_stride2, top_bsize, subsize, 0, 0);
    1468             :       if (bsize < top_bsize)
    1469             :         dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
    1470             :                        mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
    1471             :                        dst_stride2);
    1472             :       else
    1473             :         dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row,
    1474             :                        mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
    1475             :                        dst_stride2, 2);
    1476             : 
    1477             :       for (i = 0; i < MAX_MB_PLANE; i++) {
    1478             :         xd->plane[i].dst.buf = dst_buf[i];
    1479             :         xd->plane[i].dst.stride = dst_stride[i];
    1480             :         av1_build_masked_inter_predictor_complex(
    1481             :             xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
    1482             :             mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
    1483             :             i);
    1484             :       }
    1485             :       for (i = 0; i < MAX_MB_PLANE; i++) {
    1486             :         av1_build_masked_inter_predictor_complex(
    1487             :             xd, dst_buf[i], dst_stride[i], dst_buf2[i], dst_stride2[i], mi_row,
    1488             :             mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
    1489             :             i);
    1490             :       }
    1491             :       break;
    1492             :     case PARTITION_HORZ_B:
    1493             :       dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
    1494             :                            mi_row_top, mi_col_top, dst_buf, dst_stride,
    1495             :                            top_bsize, subsize, 0, 0);
    1496             :       if (bsize < top_bsize)
    1497             :         dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
    1498             :                        mi_row_top, mi_col_top, dst_buf, dst_stride);
    1499             :       else
    1500             :         dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
    1501             :                        mi_row_top, mi_col_top, dst_buf, dst_stride, 0);
    1502             : 
    1503             :       dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col, mi_row + hbs,
    1504             :                            mi_col, mi_row_top, mi_col_top, dst_buf1,
    1505             :                            dst_stride1, top_bsize, bsize2, 0, 0);
    1506             :       dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs, mi_col,
    1507             :                      mi_row_top, mi_col_top, dst_buf1, dst_stride1);
    1508             : 
    1509             :       dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
    1510             :                            mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
    1511             :                            dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
    1512             :       dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
    1513             :                      mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
    1514             :                      dst_stride2);
    1515             : 
    1516             :       for (i = 0; i < MAX_MB_PLANE; i++) {
    1517             :         xd->plane[i].dst.buf = dst_buf1[i];
    1518             :         xd->plane[i].dst.stride = dst_stride1[i];
    1519             :         av1_build_masked_inter_predictor_complex(
    1520             :             xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
    1521             :             mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
    1522             :             PARTITION_VERT, i);
    1523             :       }
    1524             :       for (i = 0; i < MAX_MB_PLANE; i++) {
    1525             :         xd->plane[i].dst.buf = dst_buf[i];
    1526             :         xd->plane[i].dst.stride = dst_stride[i];
    1527             :         av1_build_masked_inter_predictor_complex(
    1528             :             xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
    1529             :             mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_HORZ,
    1530             :             i);
    1531             :       }
    1532             :       break;
    1533             :     case PARTITION_VERT_B:
    1534             :       dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col, mi_row, mi_col,
    1535             :                            mi_row_top, mi_col_top, dst_buf, dst_stride,
    1536             :                            top_bsize, subsize, 0, 0);
    1537             :       if (bsize < top_bsize)
    1538             :         dec_extend_all(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
    1539             :                        mi_row_top, mi_col_top, dst_buf, dst_stride);
    1540             :       else
    1541             :         dec_extend_dir(pbi, xd, tile, 0, subsize, top_bsize, mi_row, mi_col,
    1542             :                        mi_row_top, mi_col_top, dst_buf, dst_stride, 3);
    1543             : 
    1544             :       dec_predict_b_extend(pbi, xd, tile, 0, mi_row, mi_col + hbs, mi_row,
    1545             :                            mi_col + hbs, mi_row_top, mi_col_top, dst_buf1,
    1546             :                            dst_stride1, top_bsize, bsize2, 0, 0);
    1547             :       dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row, mi_col + hbs,
    1548             :                      mi_row_top, mi_col_top, dst_buf1, dst_stride1);
    1549             : 
    1550             :       dec_predict_b_extend(pbi, xd, tile, 0, mi_row + hbs, mi_col + hbs,
    1551             :                            mi_row + hbs, mi_col + hbs, mi_row_top, mi_col_top,
    1552             :                            dst_buf2, dst_stride2, top_bsize, bsize2, 0, 0);
    1553             :       dec_extend_all(pbi, xd, tile, 0, bsize2, top_bsize, mi_row + hbs,
    1554             :                      mi_col + hbs, mi_row_top, mi_col_top, dst_buf2,
    1555             :                      dst_stride2);
    1556             : 
    1557             :       for (i = 0; i < MAX_MB_PLANE; i++) {
    1558             :         xd->plane[i].dst.buf = dst_buf1[i];
    1559             :         xd->plane[i].dst.stride = dst_stride1[i];
    1560             :         av1_build_masked_inter_predictor_complex(
    1561             :             xd, dst_buf1[i], dst_stride1[i], dst_buf2[i], dst_stride2[i],
    1562             :             mi_row, mi_col, mi_row_top, mi_col_top, bsize, top_bsize,
    1563             :             PARTITION_HORZ, i);
    1564             :       }
    1565             :       for (i = 0; i < MAX_MB_PLANE; i++) {
    1566             :         xd->plane[i].dst.buf = dst_buf[i];
    1567             :         xd->plane[i].dst.stride = dst_stride[i];
    1568             :         av1_build_masked_inter_predictor_complex(
    1569             :             xd, dst_buf[i], dst_stride[i], dst_buf1[i], dst_stride1[i], mi_row,
    1570             :             mi_col, mi_row_top, mi_col_top, bsize, top_bsize, PARTITION_VERT,
    1571             :             i);
    1572             :       }
    1573             :       break;
    1574             : #endif  // CONFIG_EXT_PARTITION_TYPES
    1575             :     default: assert(0);
    1576             :   }
    1577             : }
    1578             : 
    1579             : static void set_segment_id_supertx(const AV1_COMMON *const cm, int mi_row,
    1580             :                                    int mi_col, BLOCK_SIZE bsize) {
    1581             :   const struct segmentation *seg = &cm->seg;
    1582             :   const int miw = AOMMIN(mi_size_wide[bsize], cm->mi_cols - mi_col);
    1583             :   const int mih = AOMMIN(mi_size_high[bsize], cm->mi_rows - mi_row);
    1584             :   const int mi_offset = mi_row * cm->mi_stride + mi_col;
    1585             :   MODE_INFO **const mip = cm->mi_grid_visible + mi_offset;
    1586             :   int r, c;
    1587             :   int seg_id_supertx = MAX_SEGMENTS;
    1588             : 
    1589             :   if (!seg->enabled) {
    1590             :     seg_id_supertx = 0;
    1591             :   } else {
    1592             :     // Find the minimum segment_id
    1593             :     for (r = 0; r < mih; r++)
    1594             :       for (c = 0; c < miw; c++)
    1595             :         seg_id_supertx =
    1596             :             AOMMIN(mip[r * cm->mi_stride + c]->mbmi.segment_id, seg_id_supertx);
    1597             :     assert(0 <= seg_id_supertx && seg_id_supertx < MAX_SEGMENTS);
    1598             :   }
    1599             : 
    1600             :   // Assign the the segment_id back to segment_id_supertx
    1601             :   for (r = 0; r < mih; r++)
    1602             :     for (c = 0; c < miw; c++)
    1603             :       mip[r * cm->mi_stride + c]->mbmi.segment_id_supertx = seg_id_supertx;
    1604             : }
    1605             : #endif  // CONFIG_SUPERTX
    1606             : 
    1607           0 : static void decode_mbmi_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
    1608             : #if CONFIG_SUPERTX
    1609             :                               int supertx_enabled,
    1610             : #endif  // CONFIG_SUPERTX
    1611             :                               int mi_row, int mi_col, aom_reader *r,
    1612             : #if CONFIG_EXT_PARTITION_TYPES
    1613             :                               PARTITION_TYPE partition,
    1614             : #endif  // CONFIG_EXT_PARTITION_TYPES
    1615             :                               BLOCK_SIZE bsize) {
    1616           0 :   AV1_COMMON *const cm = &pbi->common;
    1617           0 :   const int bw = mi_size_wide[bsize];
    1618           0 :   const int bh = mi_size_high[bsize];
    1619           0 :   const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
    1620           0 :   const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
    1621             : 
    1622             : #if CONFIG_ACCOUNTING
    1623             :   aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
    1624             : #endif
    1625             : #if CONFIG_SUPERTX
    1626             :   if (supertx_enabled) {
    1627             :     set_mb_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
    1628             :   } else {
    1629             :     set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
    1630             :   }
    1631             : #if CONFIG_EXT_PARTITION_TYPES
    1632             :   xd->mi[0]->mbmi.partition = partition;
    1633             : #endif
    1634             :   av1_read_mode_info(pbi, xd, supertx_enabled, mi_row, mi_col, r, x_mis, y_mis);
    1635             : #else
    1636           0 :   set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
    1637             : #if CONFIG_EXT_PARTITION_TYPES
    1638             :   xd->mi[0]->mbmi.partition = partition;
    1639             : #endif
    1640           0 :   av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
    1641             : #endif  // CONFIG_SUPERTX
    1642             : 
    1643           0 :   if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
    1644           0 :     const BLOCK_SIZE uv_subsize =
    1645           0 :         ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
    1646           0 :     if (uv_subsize == BLOCK_INVALID)
    1647           0 :       aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
    1648             :                          "Invalid block size.");
    1649             :   }
    1650             : 
    1651             : #if CONFIG_SUPERTX
    1652             :   xd->mi[0]->mbmi.segment_id_supertx = MAX_SEGMENTS;
    1653             : #endif  // CONFIG_SUPERTX
    1654             : 
    1655           0 :   int reader_corrupted_flag = aom_reader_has_error(r);
    1656           0 :   aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
    1657           0 : }
    1658             : 
    1659           0 : static void decode_token_and_recon_block(AV1Decoder *const pbi,
    1660             :                                          MACROBLOCKD *const xd, int mi_row,
    1661             :                                          int mi_col, aom_reader *r,
    1662             :                                          BLOCK_SIZE bsize) {
    1663           0 :   AV1_COMMON *const cm = &pbi->common;
    1664           0 :   const int bw = mi_size_wide[bsize];
    1665           0 :   const int bh = mi_size_high[bsize];
    1666           0 :   const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
    1667           0 :   const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
    1668             : 
    1669           0 :   set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
    1670           0 :   MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
    1671             : 
    1672             : #if CONFIG_DELTA_Q
    1673           0 :   if (cm->delta_q_present_flag) {
    1674             :     int i;
    1675           0 :     for (i = 0; i < MAX_SEGMENTS; i++) {
    1676             : #if CONFIG_EXT_DELTA_Q
    1677           0 :       xd->plane[0].seg_dequant[i][0] =
    1678           0 :           av1_dc_quant(av1_get_qindex(&cm->seg, i, xd->current_qindex),
    1679             :                        cm->y_dc_delta_q, cm->bit_depth);
    1680           0 :       xd->plane[0].seg_dequant[i][1] = av1_ac_quant(
    1681           0 :           av1_get_qindex(&cm->seg, i, xd->current_qindex), 0, cm->bit_depth);
    1682           0 :       xd->plane[1].seg_dequant[i][0] =
    1683           0 :           av1_dc_quant(av1_get_qindex(&cm->seg, i, xd->current_qindex),
    1684             :                        cm->uv_dc_delta_q, cm->bit_depth);
    1685           0 :       xd->plane[1].seg_dequant[i][1] =
    1686           0 :           av1_ac_quant(av1_get_qindex(&cm->seg, i, xd->current_qindex),
    1687             :                        cm->uv_ac_delta_q, cm->bit_depth);
    1688           0 :       xd->plane[2].seg_dequant[i][0] =
    1689           0 :           av1_dc_quant(av1_get_qindex(&cm->seg, i, xd->current_qindex),
    1690             :                        cm->uv_dc_delta_q, cm->bit_depth);
    1691           0 :       xd->plane[2].seg_dequant[i][1] =
    1692           0 :           av1_ac_quant(av1_get_qindex(&cm->seg, i, xd->current_qindex),
    1693             :                        cm->uv_ac_delta_q, cm->bit_depth);
    1694             : #else
    1695             :       xd->plane[0].seg_dequant[i][0] =
    1696             :           av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
    1697             :       xd->plane[0].seg_dequant[i][1] =
    1698             :           av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
    1699             :       xd->plane[1].seg_dequant[i][0] =
    1700             :           av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
    1701             :       xd->plane[1].seg_dequant[i][1] =
    1702             :           av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
    1703             :       xd->plane[2].seg_dequant[i][0] =
    1704             :           av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
    1705             :       xd->plane[2].seg_dequant[i][1] =
    1706             :           av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
    1707             : #endif
    1708             :     }
    1709             :   }
    1710             : #endif
    1711             : 
    1712             : #if CONFIG_CB4X4
    1713           0 :   if (mbmi->skip) av1_reset_skip_context(xd, mi_row, mi_col, bsize);
    1714             : #else
    1715             :   if (mbmi->skip) {
    1716             :     av1_reset_skip_context(xd, mi_row, mi_col, AOMMAX(BLOCK_8X8, bsize));
    1717             :   }
    1718             : #endif
    1719             : 
    1720             : #if CONFIG_COEF_INTERLEAVE
    1721             :   {
    1722             :     const struct macroblockd_plane *const pd_y = &xd->plane[0];
    1723             :     const struct macroblockd_plane *const pd_c = &xd->plane[1];
    1724             :     const TX_SIZE tx_log2_y = mbmi->tx_size;
    1725             :     const TX_SIZE tx_log2_c = get_uv_tx_size(mbmi, pd_c);
    1726             :     const int tx_sz_y = (1 << tx_log2_y);
    1727             :     const int tx_sz_c = (1 << tx_log2_c);
    1728             :     const int num_4x4_w_y = pd_y->n4_w;
    1729             :     const int num_4x4_h_y = pd_y->n4_h;
    1730             :     const int num_4x4_w_c = pd_c->n4_w;
    1731             :     const int num_4x4_h_c = pd_c->n4_h;
    1732             :     const int max_4x4_w_y = get_max_4x4_size(num_4x4_w_y, xd->mb_to_right_edge,
    1733             :                                              pd_y->subsampling_x);
    1734             :     const int max_4x4_h_y = get_max_4x4_size(num_4x4_h_y, xd->mb_to_bottom_edge,
    1735             :                                              pd_y->subsampling_y);
    1736             :     const int max_4x4_w_c = get_max_4x4_size(num_4x4_w_c, xd->mb_to_right_edge,
    1737             :                                              pd_c->subsampling_x);
    1738             :     const int max_4x4_h_c = get_max_4x4_size(num_4x4_h_c, xd->mb_to_bottom_edge,
    1739             :                                              pd_c->subsampling_y);
    1740             : 
    1741             :     // The max_4x4_w/h may be smaller than tx_sz under some corner cases,
    1742             :     // i.e. when the SB is splitted by tile boundaries.
    1743             :     const int tu_num_w_y = (max_4x4_w_y + tx_sz_y - 1) / tx_sz_y;
    1744             :     const int tu_num_h_y = (max_4x4_h_y + tx_sz_y - 1) / tx_sz_y;
    1745             :     const int tu_num_w_c = (max_4x4_w_c + tx_sz_c - 1) / tx_sz_c;
    1746             :     const int tu_num_h_c = (max_4x4_h_c + tx_sz_c - 1) / tx_sz_c;
    1747             :     const int tu_num_c = tu_num_w_c * tu_num_h_c;
    1748             : 
    1749             :     if (!is_inter_block(mbmi)) {
    1750             :       int tu_idx_c = 0;
    1751             :       int row_y, col_y, row_c, col_c;
    1752             :       int plane;
    1753             : 
    1754             : #if CONFIG_PALETTE
    1755             :       for (plane = 0; plane <= 1; ++plane) {
    1756             :         if (mbmi->palette_mode_info.palette_size[plane])
    1757             :           av1_decode_palette_tokens(xd, plane, r);
    1758             :       }
    1759             : #endif
    1760             : 
    1761             :       for (row_y = 0; row_y < tu_num_h_y; row_y++) {
    1762             :         for (col_y = 0; col_y < tu_num_w_y; col_y++) {
    1763             :           // luma
    1764             :           predict_and_reconstruct_intra_block(
    1765             :               cm, xd, r, mbmi, 0, row_y * tx_sz_y, col_y * tx_sz_y, tx_log2_y);
    1766             :           // chroma
    1767             :           if (tu_idx_c < tu_num_c) {
    1768             :             row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
    1769             :             col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
    1770             :             predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c,
    1771             :                                                 col_c, tx_log2_c);
    1772             :             predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c,
    1773             :                                                 col_c, tx_log2_c);
    1774             :             tu_idx_c++;
    1775             :           }
    1776             :         }
    1777             :       }
    1778             : 
    1779             :       // In 422 case, it's possilbe that Chroma has more TUs than Luma
    1780             :       while (tu_idx_c < tu_num_c) {
    1781             :         row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
    1782             :         col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
    1783             :         predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 1, row_c, col_c,
    1784             :                                             tx_log2_c);
    1785             :         predict_and_reconstruct_intra_block(cm, xd, r, mbmi, 2, row_c, col_c,
    1786             :                                             tx_log2_c);
    1787             :         tu_idx_c++;
    1788             :       }
    1789             :     } else {
    1790             :       // Prediction
    1791             :       av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL,
    1792             :                                     AOMMAX(bsize, BLOCK_8X8));
    1793             : 
    1794             :       // Reconstruction
    1795             :       if (!mbmi->skip) {
    1796             :         int eobtotal = 0;
    1797             :         int tu_idx_c = 0;
    1798             :         int row_y, col_y, row_c, col_c;
    1799             : 
    1800             :         for (row_y = 0; row_y < tu_num_h_y; row_y++) {
    1801             :           for (col_y = 0; col_y < tu_num_w_y; col_y++) {
    1802             :             // luma
    1803             :             eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 0,
    1804             :                                                 row_y * tx_sz_y,
    1805             :                                                 col_y * tx_sz_y, tx_log2_y);
    1806             :             // chroma
    1807             :             if (tu_idx_c < tu_num_c) {
    1808             :               row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
    1809             :               col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
    1810             :               eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
    1811             :                                                   1, row_c, col_c, tx_log2_c);
    1812             :               eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
    1813             :                                                   2, row_c, col_c, tx_log2_c);
    1814             :               tu_idx_c++;
    1815             :             }
    1816             :           }
    1817             :         }
    1818             : 
    1819             :         // In 422 case, it's possilbe that Chroma has more TUs than Luma
    1820             :         while (tu_idx_c < tu_num_c) {
    1821             :           row_c = (tu_idx_c / tu_num_w_c) * tx_sz_c;
    1822             :           col_c = (tu_idx_c % tu_num_w_c) * tx_sz_c;
    1823             :           eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 1,
    1824             :                                               row_c, col_c, tx_log2_c);
    1825             :           eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id, 2,
    1826             :                                               row_c, col_c, tx_log2_c);
    1827             :           tu_idx_c++;
    1828             :         }
    1829             : 
    1830             :         // TODO(CONFIG_COEF_INTERLEAVE owners): bring eob == 0 corner case
    1831             :         // into line with the defaut configuration
    1832             :         if (bsize >= BLOCK_8X8 && eobtotal == 0) mbmi->skip = 1;
    1833             :       }
    1834             :     }
    1835             :   }
    1836             : #else  // CONFIG_COEF_INTERLEAVE
    1837           0 :   if (!is_inter_block(mbmi)) {
    1838             :     int plane;
    1839             : #if CONFIG_PALETTE
    1840           0 :     for (plane = 0; plane <= 1; ++plane) {
    1841           0 :       if (mbmi->palette_mode_info.palette_size[plane])
    1842           0 :         av1_decode_palette_tokens(xd, plane, r);
    1843             :     }
    1844             : #endif  // CONFIG_PALETTE
    1845           0 :     for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
    1846           0 :       const struct macroblockd_plane *const pd = &xd->plane[plane];
    1847           0 :       const TX_SIZE tx_size = get_tx_size(plane, xd);
    1848           0 :       const int stepr = tx_size_high_unit[tx_size];
    1849           0 :       const int stepc = tx_size_wide_unit[tx_size];
    1850             : #if CONFIG_CB4X4
    1851             : #if CONFIG_CHROMA_2X2
    1852             :       const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
    1853             : #else
    1854           0 :       const BLOCK_SIZE plane_bsize =
    1855           0 :           AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
    1856             : #endif  // CONFIG_CHROMA_2X2
    1857             : #else
    1858             :       const BLOCK_SIZE plane_bsize =
    1859             :           get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
    1860             : #endif
    1861             :       int row, col;
    1862           0 :       const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
    1863           0 :       const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
    1864             : #if CONFIG_CB4X4
    1865           0 :       if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
    1866             :                                pd->subsampling_y))
    1867           0 :         continue;
    1868             : #endif
    1869             : 
    1870           0 :       for (row = 0; row < max_blocks_high; row += stepr)
    1871           0 :         for (col = 0; col < max_blocks_wide; col += stepc)
    1872           0 :           predict_and_reconstruct_intra_block(cm, xd, r, mbmi, plane, row, col,
    1873             :                                               tx_size);
    1874             :     }
    1875             :   } else {
    1876             :     int ref;
    1877             : 
    1878           0 :     for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
    1879           0 :       const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
    1880           0 :       if (frame < LAST_FRAME) {
    1881             : #if CONFIG_INTRABC
    1882             :         assert(is_intrabc_block(mbmi));
    1883             :         assert(frame == INTRA_FRAME);
    1884             :         assert(ref == 0);
    1885             : #else
    1886           0 :         assert(0);
    1887             : #endif  // CONFIG_INTRABC
    1888             :       } else {
    1889           0 :         RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
    1890             : 
    1891           0 :         xd->block_refs[ref] = ref_buf;
    1892           0 :         if ((!av1_is_valid_scale(&ref_buf->sf)))
    1893           0 :           aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
    1894             :                              "Reference frame has invalid dimensions");
    1895           0 :         av1_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
    1896           0 :                              &ref_buf->sf);
    1897             :       }
    1898             :     }
    1899             : 
    1900             : #if CONFIG_CB4X4
    1901           0 :     av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL, bsize);
    1902             : #else
    1903             :     av1_build_inter_predictors_sb(cm, xd, mi_row, mi_col, NULL,
    1904             :                                   AOMMAX(bsize, BLOCK_8X8));
    1905             : #endif
    1906             : 
    1907             : #if CONFIG_MOTION_VAR
    1908           0 :     if (mbmi->motion_mode == OBMC_CAUSAL) {
    1909             : #if CONFIG_NCOBMC
    1910             :       av1_build_ncobmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
    1911             : #else
    1912           0 :       av1_build_obmc_inter_predictors_sb(cm, xd, mi_row, mi_col);
    1913             : #endif
    1914             :     }
    1915             : #endif  // CONFIG_MOTION_VAR
    1916             : 
    1917             :     // Reconstruction
    1918           0 :     if (!mbmi->skip) {
    1919           0 :       int eobtotal = 0;
    1920             :       int plane;
    1921             : 
    1922           0 :       for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
    1923           0 :         const struct macroblockd_plane *const pd = &xd->plane[plane];
    1924             : #if CONFIG_CB4X4
    1925             : #if CONFIG_CHROMA_2X2
    1926             :         const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
    1927             : #else
    1928           0 :         const BLOCK_SIZE plane_bsize =
    1929           0 :             AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
    1930             : #endif  // CONFIG_CHROMA_2X2
    1931             : #else
    1932             :         const BLOCK_SIZE plane_bsize =
    1933             :             get_plane_block_size(AOMMAX(BLOCK_8X8, bsize), pd);
    1934             : #endif
    1935           0 :         const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
    1936           0 :         const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
    1937             :         int row, col;
    1938             : 
    1939             : #if CONFIG_CB4X4
    1940           0 :         if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
    1941             :                                  pd->subsampling_y))
    1942           0 :           continue;
    1943             : #endif
    1944             : 
    1945             : #if CONFIG_VAR_TX
    1946           0 :         const TX_SIZE max_tx_size = get_vartx_max_txsize(mbmi, plane_bsize);
    1947           0 :         const int bh_var_tx = tx_size_high_unit[max_tx_size];
    1948           0 :         const int bw_var_tx = tx_size_wide_unit[max_tx_size];
    1949           0 :         for (row = 0; row < max_blocks_high; row += bh_var_tx)
    1950           0 :           for (col = 0; col < max_blocks_wide; col += bw_var_tx)
    1951           0 :             decode_reconstruct_tx(cm, xd, r, mbmi, plane, plane_bsize, row, col,
    1952             :                                   max_tx_size, &eobtotal);
    1953             : #else
    1954             :         const TX_SIZE tx_size = get_tx_size(plane, xd);
    1955             :         const int stepr = tx_size_high_unit[tx_size];
    1956             :         const int stepc = tx_size_wide_unit[tx_size];
    1957             :         for (row = 0; row < max_blocks_high; row += stepr)
    1958             :           for (col = 0; col < max_blocks_wide; col += stepc)
    1959             :             eobtotal += reconstruct_inter_block(cm, xd, r, mbmi->segment_id,
    1960             :                                                 plane, row, col, tx_size);
    1961             : #endif
    1962             :       }
    1963             :     }
    1964             :   }
    1965             : #endif  // CONFIG_COEF_INTERLEAVE
    1966             : 
    1967           0 :   int reader_corrupted_flag = aom_reader_has_error(r);
    1968           0 :   aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
    1969           0 : }
    1970             : 
    1971             : #if CONFIG_NCOBMC && CONFIG_MOTION_VAR
    1972             : static void detoken_and_recon_sb(AV1Decoder *const pbi, MACROBLOCKD *const xd,
    1973             :                                  int mi_row, int mi_col, aom_reader *r,
    1974             :                                  BLOCK_SIZE bsize) {
    1975             :   AV1_COMMON *const cm = &pbi->common;
    1976             :   const int hbs = mi_size_wide[bsize] >> 1;
    1977             : #if CONFIG_CB4X4
    1978             :   const int unify_bsize = 1;
    1979             : #else
    1980             :   const int unify_bsize = 0;
    1981             : #endif
    1982             : #if CONFIG_EXT_PARTITION_TYPES
    1983             :   BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
    1984             : #endif
    1985             :   PARTITION_TYPE partition;
    1986             :   BLOCK_SIZE subsize;
    1987             :   const int has_rows = (mi_row + hbs) < cm->mi_rows;
    1988             :   const int has_cols = (mi_col + hbs) < cm->mi_cols;
    1989             : 
    1990             :   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
    1991             : 
    1992             :   partition = get_partition(cm, mi_row, mi_col, bsize);
    1993             :   subsize = subsize_lookup[partition][bsize];
    1994             : 
    1995             :   if (!hbs && !unify_bsize) {
    1996             :     xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
    1997             :     xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
    1998             :     decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
    1999             :   } else {
    2000             :     switch (partition) {
    2001             :       case PARTITION_NONE:
    2002             :         decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
    2003             :         break;
    2004             :       case PARTITION_HORZ:
    2005             :         decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
    2006             :         if (has_rows)
    2007             :           decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r,
    2008             :                                        subsize);
    2009             :         break;
    2010             :       case PARTITION_VERT:
    2011             :         decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
    2012             :         if (has_cols)
    2013             :           decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r,
    2014             :                                        subsize);
    2015             :         break;
    2016             :       case PARTITION_SPLIT:
    2017             :         detoken_and_recon_sb(pbi, xd, mi_row, mi_col, r, subsize);
    2018             :         detoken_and_recon_sb(pbi, xd, mi_row, mi_col + hbs, r, subsize);
    2019             :         detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col, r, subsize);
    2020             :         detoken_and_recon_sb(pbi, xd, mi_row + hbs, mi_col + hbs, r, subsize);
    2021             :         break;
    2022             : #if CONFIG_EXT_PARTITION_TYPES
    2023             :       case PARTITION_HORZ_A:
    2024             :         decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
    2025             :         decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
    2026             :         decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, subsize);
    2027             :         break;
    2028             :       case PARTITION_HORZ_B:
    2029             :         decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
    2030             :         decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
    2031             :         decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
    2032             :                                      bsize2);
    2033             :         break;
    2034             :       case PARTITION_VERT_A:
    2035             :         decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize2);
    2036             :         decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col, r, bsize2);
    2037             :         decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, subsize);
    2038             :         break;
    2039             :       case PARTITION_VERT_B:
    2040             :         decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, subsize);
    2041             :         decode_token_and_recon_block(pbi, xd, mi_row, mi_col + hbs, r, bsize2);
    2042             :         decode_token_and_recon_block(pbi, xd, mi_row + hbs, mi_col + hbs, r,
    2043             :                                      bsize2);
    2044             :         break;
    2045             : #endif
    2046             :       default: assert(0 && "Invalid partition type");
    2047             :     }
    2048             :   }
    2049             : }
    2050             : #endif
    2051             : 
    2052           0 : static void decode_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
    2053             : #if CONFIG_SUPERTX
    2054             :                          int supertx_enabled,
    2055             : #endif  // CONFIG_SUPERTX
    2056             :                          int mi_row, int mi_col, aom_reader *r,
    2057             : #if CONFIG_EXT_PARTITION_TYPES
    2058             :                          PARTITION_TYPE partition,
    2059             : #endif  // CONFIG_EXT_PARTITION_TYPES
    2060             :                          BLOCK_SIZE bsize) {
    2061           0 :   decode_mbmi_block(pbi, xd,
    2062             : #if CONFIG_SUPERTX
    2063             :                     supertx_enabled,
    2064             : #endif
    2065             :                     mi_row, mi_col, r,
    2066             : #if CONFIG_EXT_PARTITION_TYPES
    2067             :                     partition,
    2068             : #endif
    2069             :                     bsize);
    2070             : #if !(CONFIG_MOTION_VAR && CONFIG_NCOBMC)
    2071             : #if CONFIG_SUPERTX
    2072             :   if (!supertx_enabled)
    2073             : #endif  // CONFIG_SUPERTX
    2074           0 :     decode_token_and_recon_block(pbi, xd, mi_row, mi_col, r, bsize);
    2075             : #endif
    2076           0 : }
    2077             : 
    2078           0 : static PARTITION_TYPE read_partition(AV1_COMMON *cm, MACROBLOCKD *xd,
    2079             :                                      int mi_row, int mi_col, aom_reader *r,
    2080             :                                      int has_rows, int has_cols,
    2081             :                                      BLOCK_SIZE bsize) {
    2082             : #if CONFIG_UNPOISON_PARTITION_CTX
    2083             :   const int ctx =
    2084             :       partition_plane_context(xd, mi_row, mi_col, has_rows, has_cols, bsize);
    2085             :   const aom_prob *const probs =
    2086             :       ctx < PARTITION_CONTEXTS ? cm->fc->partition_prob[ctx] : NULL;
    2087             :   FRAME_COUNTS *const counts = ctx < PARTITION_CONTEXTS ? xd->counts : NULL;
    2088             : #else
    2089           0 :   const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
    2090           0 :   const aom_prob *const probs = cm->fc->partition_prob[ctx];
    2091           0 :   FRAME_COUNTS *const counts = xd->counts;
    2092             : #endif
    2093             :   PARTITION_TYPE p;
    2094             : #if CONFIG_EC_ADAPT
    2095           0 :   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
    2096             :   (void)cm;
    2097             : #else
    2098             :   FRAME_CONTEXT *ec_ctx = cm->fc;
    2099             : #endif
    2100             : 
    2101           0 :   aom_cdf_prob *partition_cdf = (ctx >= 0) ? ec_ctx->partition_cdf[ctx] : NULL;
    2102             : 
    2103           0 :   if (has_rows && has_cols)
    2104             : #if CONFIG_EXT_PARTITION_TYPES
    2105             :     if (bsize <= BLOCK_8X8)
    2106             :       p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, PARTITION_TYPES,
    2107             :                                           ACCT_STR);
    2108             :     else
    2109             :       p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, EXT_PARTITION_TYPES,
    2110             :                                           ACCT_STR);
    2111             : #else
    2112           0 :     p = (PARTITION_TYPE)aom_read_symbol(r, partition_cdf, PARTITION_TYPES,
    2113             :                                         ACCT_STR);
    2114             : #endif  // CONFIG_EXT_PARTITION_TYPES
    2115           0 :   else if (!has_rows && has_cols)
    2116           0 :     p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
    2117           0 :   else if (has_rows && !has_cols)
    2118           0 :     p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
    2119             :   else
    2120           0 :     p = PARTITION_SPLIT;
    2121             : 
    2122           0 :   if (counts) ++counts->partition[ctx][p];
    2123             : 
    2124           0 :   return p;
    2125             : }
    2126             : 
    2127             : #if CONFIG_SUPERTX
    2128             : static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
    2129             :                      aom_reader *r) {
    2130             :   if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
    2131             :     return 1;
    2132             :   } else {
    2133             :     const int ctx = av1_get_skip_context(xd);
    2134             :     const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
    2135             :     FRAME_COUNTS *counts = xd->counts;
    2136             :     if (counts) ++counts->skip[ctx][skip];
    2137             :     return skip;
    2138             :   }
    2139             : }
    2140             : #endif  // CONFIG_SUPERTX
    2141             : 
    2142             : // TODO(slavarnway): eliminate bsize and subsize in future commits
    2143           0 : static void decode_partition(AV1Decoder *const pbi, MACROBLOCKD *const xd,
    2144             : #if CONFIG_SUPERTX
    2145             :                              int supertx_enabled,
    2146             : #endif
    2147             :                              int mi_row, int mi_col, aom_reader *r,
    2148             :                              BLOCK_SIZE bsize, int n4x4_l2) {
    2149           0 :   AV1_COMMON *const cm = &pbi->common;
    2150           0 :   const int n8x8_l2 = n4x4_l2 - 1;
    2151           0 :   const int num_8x8_wh = mi_size_wide[bsize];
    2152           0 :   const int hbs = num_8x8_wh >> 1;
    2153             : #if CONFIG_CB4X4
    2154           0 :   const int unify_bsize = 1;
    2155             : #else
    2156             :   const int unify_bsize = 0;
    2157             : #endif
    2158             :   PARTITION_TYPE partition;
    2159             :   BLOCK_SIZE subsize;
    2160             : #if CONFIG_EXT_PARTITION_TYPES
    2161             :   BLOCK_SIZE bsize2 = get_subsize(bsize, PARTITION_SPLIT);
    2162             : #endif
    2163           0 :   const int has_rows = (mi_row + hbs) < cm->mi_rows;
    2164           0 :   const int has_cols = (mi_col + hbs) < cm->mi_cols;
    2165             : #if CONFIG_SUPERTX
    2166             :   const int read_token = !supertx_enabled;
    2167             :   int skip = 0;
    2168             :   TX_SIZE supertx_size = max_txsize_lookup[bsize];
    2169             :   const TileInfo *const tile = &xd->tile;
    2170             :   int txfm = DCT_DCT;
    2171             : #endif  // CONFIG_SUPERTX
    2172             : 
    2173           0 :   if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
    2174             : 
    2175           0 :   partition = (bsize < BLOCK_8X8) ? PARTITION_NONE
    2176           0 :                                   : read_partition(cm, xd, mi_row, mi_col, r,
    2177             :                                                    has_rows, has_cols, bsize);
    2178           0 :   subsize = subsize_lookup[partition][bsize];  // get_subsize(bsize, partition);
    2179             : 
    2180             : #if CONFIG_PVQ
    2181             :   assert(partition < PARTITION_TYPES);
    2182             :   assert(subsize < BLOCK_SIZES);
    2183             : #endif
    2184             : #if CONFIG_SUPERTX
    2185             :   if (!frame_is_intra_only(cm) && partition != PARTITION_NONE &&
    2186             :       bsize <= MAX_SUPERTX_BLOCK_SIZE && !supertx_enabled && !xd->lossless[0]) {
    2187             :     const int supertx_context = partition_supertx_context_lookup[partition];
    2188             :     supertx_enabled = aom_read(
    2189             :         r, cm->fc->supertx_prob[supertx_context][supertx_size], ACCT_STR);
    2190             :     if (xd->counts)
    2191             :       xd->counts->supertx[supertx_context][supertx_size][supertx_enabled]++;
    2192             : #if CONFIG_VAR_TX
    2193             :     if (supertx_enabled) xd->supertx_size = supertx_size;
    2194             : #endif
    2195             :   }
    2196             : #endif  // CONFIG_SUPERTX
    2197           0 :   if (!hbs && !unify_bsize) {
    2198             :     // calculate bmode block dimensions (log 2)
    2199           0 :     xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
    2200           0 :     xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
    2201           0 :     decode_block(pbi, xd,
    2202             : #if CONFIG_SUPERTX
    2203             :                  supertx_enabled,
    2204             : #endif  // CONFIG_SUPERTX
    2205             :                  mi_row, mi_col, r,
    2206             : #if CONFIG_EXT_PARTITION_TYPES
    2207             :                  partition,
    2208             : #endif  // CONFIG_EXT_PARTITION_TYPES
    2209             :                  subsize);
    2210             :   } else {
    2211           0 :     switch (partition) {
    2212             :       case PARTITION_NONE:
    2213           0 :         decode_block(pbi, xd,
    2214             : #if CONFIG_SUPERTX
    2215             :                      supertx_enabled,
    2216             : #endif  // CONFIG_SUPERTX
    2217             :                      mi_row, mi_col, r,
    2218             : #if CONFIG_EXT_PARTITION_TYPES
    2219             :                      partition,
    2220             : #endif  // CONFIG_EXT_PARTITION_TYPES
    2221             :                      subsize);
    2222           0 :         break;
    2223             :       case PARTITION_HORZ:
    2224           0 :         decode_block(pbi, xd,
    2225             : #if CONFIG_SUPERTX
    2226             :                      supertx_enabled,
    2227             : #endif  // CONFIG_SUPERTX
    2228             :                      mi_row, mi_col, r,
    2229             : #if CONFIG_EXT_PARTITION_TYPES
    2230             :                      partition,
    2231             : #endif  // CONFIG_EXT_PARTITION_TYPES
    2232             :                      subsize);
    2233           0 :         if (has_rows)
    2234           0 :           decode_block(pbi, xd,
    2235             : #if CONFIG_SUPERTX
    2236             :                        supertx_enabled,
    2237             : #endif  // CONFIG_SUPERTX
    2238             :                        mi_row + hbs, mi_col, r,
    2239             : #if CONFIG_EXT_PARTITION_TYPES
    2240             :                        partition,
    2241             : #endif  // CONFIG_EXT_PARTITION_TYPES
    2242             :                        subsize);
    2243           0 :         break;
    2244             :       case PARTITION_VERT:
    2245           0 :         decode_block(pbi, xd,
    2246             : #if CONFIG_SUPERTX
    2247             :                      supertx_enabled,
    2248             : #endif  // CONFIG_SUPERTX
    2249             :                      mi_row, mi_col, r,
    2250             : #if CONFIG_EXT_PARTITION_TYPES
    2251             :                      partition,
    2252             : #endif  // CONFIG_EXT_PARTITION_TYPES
    2253             :                      subsize);
    2254           0 :         if (has_cols)
    2255           0 :           decode_block(pbi, xd,
    2256             : #if CONFIG_SUPERTX
    2257             :                        supertx_enabled,
    2258             : #endif  // CONFIG_SUPERTX
    2259             :                        mi_row, mi_col + hbs, r,
    2260             : #if CONFIG_EXT_PARTITION_TYPES
    2261             :                        partition,
    2262             : #endif  // CONFIG_EXT_PARTITION_TYPES
    2263             :                        subsize);
    2264           0 :         break;
    2265             :       case PARTITION_SPLIT:
    2266           0 :         decode_partition(pbi, xd,
    2267             : #if CONFIG_SUPERTX
    2268             :                          supertx_enabled,
    2269             : #endif  // CONFIG_SUPERTX
    2270             :                          mi_row, mi_col, r, subsize, n8x8_l2);
    2271           0 :         decode_partition(pbi, xd,
    2272             : #if CONFIG_SUPERTX
    2273             :                          supertx_enabled,
    2274             : #endif  // CONFIG_SUPERTX
    2275             :                          mi_row, mi_col + hbs, r, subsize, n8x8_l2);
    2276           0 :         decode_partition(pbi, xd,
    2277             : #if CONFIG_SUPERTX
    2278             :                          supertx_enabled,
    2279             : #endif  // CONFIG_SUPERTX
    2280             :                          mi_row + hbs, mi_col, r, subsize, n8x8_l2);
    2281           0 :         decode_partition(pbi, xd,
    2282             : #if CONFIG_SUPERTX
    2283             :                          supertx_enabled,
    2284             : #endif  // CONFIG_SUPERTX
    2285             :                          mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
    2286           0 :         break;
    2287             : #if CONFIG_EXT_PARTITION_TYPES
    2288             :       case PARTITION_HORZ_A:
    2289             :         decode_block(pbi, xd,
    2290             : #if CONFIG_SUPERTX
    2291             :                      supertx_enabled,
    2292             : #endif
    2293             :                      mi_row, mi_col, r, partition, bsize2);
    2294             :         decode_block(pbi, xd,
    2295             : #if CONFIG_SUPERTX
    2296             :                      supertx_enabled,
    2297             : #endif
    2298             :                      mi_row, mi_col + hbs, r, partition, bsize2);
    2299             :         decode_block(pbi, xd,
    2300             : #if CONFIG_SUPERTX
    2301             :                      supertx_enabled,
    2302             : #endif
    2303             :                      mi_row + hbs, mi_col, r, partition, subsize);
    2304             :         break;
    2305             :       case PARTITION_HORZ_B:
    2306             :         decode_block(pbi, xd,
    2307             : #if CONFIG_SUPERTX
    2308             :                      supertx_enabled,
    2309             : #endif
    2310             :                      mi_row, mi_col, r, partition, subsize);
    2311             :         decode_block(pbi, xd,
    2312             : #if CONFIG_SUPERTX
    2313             :                      supertx_enabled,
    2314             : #endif
    2315             :                      mi_row + hbs, mi_col, r, partition, bsize2);
    2316             :         decode_block(pbi, xd,
    2317             : #if CONFIG_SUPERTX
    2318             :                      supertx_enabled,
    2319             : #endif
    2320             :                      mi_row + hbs, mi_col + hbs, r, partition, bsize2);
    2321             :         break;
    2322             :       case PARTITION_VERT_A:
    2323             :         decode_block(pbi, xd,
    2324             : #if CONFIG_SUPERTX
    2325             :                      supertx_enabled,
    2326             : #endif
    2327             :                      mi_row, mi_col, r, partition, bsize2);
    2328             :         decode_block(pbi, xd,
    2329             : #if CONFIG_SUPERTX
    2330             :                      supertx_enabled,
    2331             : #endif
    2332             :                      mi_row + hbs, mi_col, r, partition, bsize2);
    2333             :         decode_block(pbi, xd,
    2334             : #if CONFIG_SUPERTX
    2335             :                      supertx_enabled,
    2336             : #endif
    2337             :                      mi_row, mi_col + hbs, r, partition, subsize);
    2338             :         break;
    2339             :       case PARTITION_VERT_B:
    2340             :         decode_block(pbi, xd,
    2341             : #if CONFIG_SUPERTX
    2342             :                      supertx_enabled,
    2343             : #endif
    2344             :                      mi_row, mi_col, r, partition, subsize);
    2345             :         decode_block(pbi, xd,
    2346             : #if CONFIG_SUPERTX
    2347             :                      supertx_enabled,
    2348             : #endif
    2349             :                      mi_row, mi_col + hbs, r, partition, bsize2);
    2350             :         decode_block(pbi, xd,
    2351             : #if CONFIG_SUPERTX
    2352             :                      supertx_enabled,
    2353             : #endif
    2354             :                      mi_row + hbs, mi_col + hbs, r, partition, bsize2);
    2355             :         break;
    2356             : #endif
    2357           0 :       default: assert(0 && "Invalid partition type");
    2358             :     }
    2359             :   }
    2360             : 
    2361             : #if CONFIG_SUPERTX
    2362             :   if (supertx_enabled && read_token) {
    2363             :     uint8_t *dst_buf[3];
    2364             :     int dst_stride[3], i;
    2365             :     int offset = mi_row * cm->mi_stride + mi_col;
    2366             : 
    2367             :     set_segment_id_supertx(cm, mi_row, mi_col, bsize);
    2368             : 
    2369             : #if CONFIG_DELTA_Q
    2370             :     if (cm->delta_q_present_flag) {
    2371             :       for (i = 0; i < MAX_SEGMENTS; i++) {
    2372             :         xd->plane[0].seg_dequant[i][0] =
    2373             :             av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
    2374             :         xd->plane[0].seg_dequant[i][1] =
    2375             :             av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
    2376             :         xd->plane[1].seg_dequant[i][0] =
    2377             :             av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
    2378             :         xd->plane[1].seg_dequant[i][1] =
    2379             :             av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
    2380             :         xd->plane[2].seg_dequant[i][0] =
    2381             :             av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
    2382             :         xd->plane[2].seg_dequant[i][1] =
    2383             :             av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
    2384             :       }
    2385             :     }
    2386             : #endif
    2387             : 
    2388             :     xd->mi = cm->mi_grid_visible + offset;
    2389             :     xd->mi[0] = cm->mi + offset;
    2390             :     set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col,
    2391             :                    mi_size_wide[bsize],
    2392             : #if CONFIG_DEPENDENT_HORZTILES
    2393             :                    cm->dependent_horz_tiles,
    2394             : #endif  // CONFIG_DEPENDENT_HORZTILES
    2395             :                    cm->mi_rows, cm->mi_cols);
    2396             :     set_skip_context(xd, mi_row, mi_col);
    2397             :     skip = read_skip(cm, xd, xd->mi[0]->mbmi.segment_id_supertx, r);
    2398             :     if (skip) {
    2399             :       av1_reset_skip_context(xd, mi_row, mi_col, bsize);
    2400             :     } else {
    2401             : #if CONFIG_EXT_TX
    2402             :       if (get_ext_tx_types(supertx_size, bsize, 1, cm->reduced_tx_set_used) >
    2403             :           1) {
    2404             :         const int eset =
    2405             :             get_ext_tx_set(supertx_size, bsize, 1, cm->reduced_tx_set_used);
    2406             :         if (eset > 0) {
    2407             :           txfm = aom_read_tree(r, av1_ext_tx_inter_tree[eset],
    2408             :                                cm->fc->inter_ext_tx_prob[eset][supertx_size],
    2409             :                                ACCT_STR);
    2410             :           if (xd->counts) ++xd->counts->inter_ext_tx[eset][supertx_size][txfm];
    2411             :         }
    2412             :       }
    2413             : #else
    2414             :       if (supertx_size < TX_32X32) {
    2415             :         txfm = aom_read_tree(r, av1_ext_tx_tree,
    2416             :                              cm->fc->inter_ext_tx_prob[supertx_size], ACCT_STR);
    2417             :         if (xd->counts) ++xd->counts->inter_ext_tx[supertx_size][txfm];
    2418             :       }
    2419             : #endif  // CONFIG_EXT_TX
    2420             :     }
    2421             : 
    2422             :     av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row,
    2423             :                          mi_col);
    2424             :     for (i = 0; i < MAX_MB_PLANE; i++) {
    2425             :       dst_buf[i] = xd->plane[i].dst.buf;
    2426             :       dst_stride[i] = xd->plane[i].dst.stride;
    2427             :     }
    2428             :     dec_predict_sb_complex(pbi, xd, tile, mi_row, mi_col, mi_row, mi_col, bsize,
    2429             :                            bsize, dst_buf, dst_stride);
    2430             : 
    2431             :     if (!skip) {
    2432             :       int eobtotal = 0;
    2433             :       MB_MODE_INFO *mbmi;
    2434             :       set_offsets_topblock(cm, xd, tile, bsize, mi_row, mi_col);
    2435             :       mbmi = &xd->mi[0]->mbmi;
    2436             :       mbmi->tx_type = txfm;
    2437             :       assert(mbmi->segment_id_supertx != MAX_SEGMENTS);
    2438             :       for (i = 0; i < MAX_MB_PLANE; ++i) {
    2439             :         const struct macroblockd_plane *const pd = &xd->plane[i];
    2440             :         int row, col;
    2441             :         const TX_SIZE tx_size = get_tx_size(i, xd);
    2442             :         const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
    2443             :         const int stepr = tx_size_high_unit[tx_size];
    2444             :         const int stepc = tx_size_wide_unit[tx_size];
    2445             :         const int max_blocks_wide = max_block_wide(xd, plane_bsize, i);
    2446             :         const int max_blocks_high = max_block_high(xd, plane_bsize, i);
    2447             : 
    2448             :         for (row = 0; row < max_blocks_high; row += stepr)
    2449             :           for (col = 0; col < max_blocks_wide; col += stepc)
    2450             :             eobtotal += reconstruct_inter_block(
    2451             :                 cm, xd, r, mbmi->segment_id_supertx, i, row, col, tx_size);
    2452             :       }
    2453             :       if ((unify_bsize || !(subsize < BLOCK_8X8)) && eobtotal == 0) skip = 1;
    2454             :     }
    2455             :     set_param_topblock(cm, xd, bsize, mi_row, mi_col, txfm, skip);
    2456             :   }
    2457             : #endif  // CONFIG_SUPERTX
    2458             : 
    2459             : #if CONFIG_EXT_PARTITION_TYPES
    2460             :   update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition);
    2461             : #else
    2462             :   // update partition context
    2463           0 :   if (bsize >= BLOCK_8X8 &&
    2464           0 :       (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
    2465           0 :     update_partition_context(xd, mi_row, mi_col, subsize, bsize);
    2466             : #endif  // CONFIG_EXT_PARTITION_TYPES
    2467             : 
    2468             : #if CONFIG_CDEF
    2469           0 :   if (bsize == cm->sb_size) {
    2470           0 :     if (!sb_all_skip(cm, mi_row, mi_col)) {
    2471           0 :       cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
    2472           0 :           aom_read_literal(r, cm->cdef_bits, ACCT_STR);
    2473             :     } else {
    2474           0 :       cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]->mbmi.cdef_strength =
    2475             :           -1;
    2476             :     }
    2477             :   }
    2478             : #endif  // CONFIG_CDEF
    2479             : }
    2480             : 
    2481           0 : static void setup_bool_decoder(const uint8_t *data, const uint8_t *data_end,
    2482             :                                const size_t read_size,
    2483             :                                struct aom_internal_error_info *error_info,
    2484             :                                aom_reader *r,
    2485             : #if CONFIG_ANS && ANS_MAX_SYMBOLS
    2486             :                                int window_size,
    2487             : #endif  // CONFIG_ANS && ANS_MAX_SYMBOLS
    2488             :                                aom_decrypt_cb decrypt_cb, void *decrypt_state) {
    2489             :   // Validate the calculated partition length. If the buffer
    2490             :   // described by the partition can't be fully read, then restrict
    2491             :   // it to the portion that can be (for EC mode) or throw an error.
    2492           0 :   if (!read_is_valid(data, read_size, data_end))
    2493           0 :     aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
    2494             :                        "Truncated packet or corrupt tile length");
    2495             : 
    2496             : #if CONFIG_ANS && ANS_MAX_SYMBOLS
    2497             :   r->window_size = window_size;
    2498             : #endif
    2499           0 :   if (aom_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
    2500           0 :     aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
    2501             :                        "Failed to allocate bool decoder %d", 1);
    2502           0 : }
    2503             : 
    2504             : #if !CONFIG_PVQ && !CONFIG_EC_ADAPT && !CONFIG_LV_MAP
    2505             : static void read_coef_probs_common(av1_coeff_probs_model *coef_probs,
    2506             :                                    aom_reader *r) {
    2507             :   int i, j, k, l, m;
    2508             : #if CONFIG_EC_ADAPT
    2509             :   const int node_limit = UNCONSTRAINED_NODES - 1;
    2510             : #else
    2511             :   const int node_limit = UNCONSTRAINED_NODES;
    2512             : #endif
    2513             : 
    2514             :   if (aom_read_bit(r, ACCT_STR))
    2515             :     for (i = 0; i < PLANE_TYPES; ++i)
    2516             :       for (j = 0; j < REF_TYPES; ++j)
    2517             :         for (k = 0; k < COEF_BANDS; ++k)
    2518             :           for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
    2519             :             for (m = 0; m < node_limit; ++m)
    2520             :               av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
    2521             : }
    2522             : 
    2523             : static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
    2524             :   const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
    2525             :   TX_SIZE tx_size;
    2526             :   for (tx_size = 0; tx_size <= max_tx_size; ++tx_size)
    2527             :     read_coef_probs_common(fc->coef_probs[tx_size], r);
    2528             : }
    2529             : #endif
    2530             : 
    2531           0 : static void setup_segmentation(AV1_COMMON *const cm,
    2532             :                                struct aom_read_bit_buffer *rb) {
    2533           0 :   struct segmentation *const seg = &cm->seg;
    2534             :   int i, j;
    2535             : 
    2536           0 :   seg->update_map = 0;
    2537           0 :   seg->update_data = 0;
    2538             : 
    2539           0 :   seg->enabled = aom_rb_read_bit(rb);
    2540           0 :   if (!seg->enabled) return;
    2541             : 
    2542             :   // Segmentation map update
    2543           0 :   if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
    2544           0 :     seg->update_map = 1;
    2545             :   } else {
    2546           0 :     seg->update_map = aom_rb_read_bit(rb);
    2547             :   }
    2548           0 :   if (seg->update_map) {
    2549           0 :     if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
    2550           0 :       seg->temporal_update = 0;
    2551             :     } else {
    2552           0 :       seg->temporal_update = aom_rb_read_bit(rb);
    2553             :     }
    2554             :   }
    2555             : 
    2556             :   // Segmentation data update
    2557           0 :   seg->update_data = aom_rb_read_bit(rb);
    2558           0 :   if (seg->update_data) {
    2559           0 :     seg->abs_delta = aom_rb_read_bit(rb);
    2560             : 
    2561           0 :     av1_clearall_segfeatures(seg);
    2562             : 
    2563           0 :     for (i = 0; i < MAX_SEGMENTS; i++) {
    2564           0 :       for (j = 0; j < SEG_LVL_MAX; j++) {
    2565           0 :         int data = 0;
    2566           0 :         const int feature_enabled = aom_rb_read_bit(rb);
    2567           0 :         if (feature_enabled) {
    2568           0 :           av1_enable_segfeature(seg, i, j);
    2569           0 :           data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
    2570           0 :           if (av1_is_segfeature_signed(j))
    2571           0 :             data = aom_rb_read_bit(rb) ? -data : data;
    2572             :         }
    2573           0 :         av1_set_segdata(seg, i, j, data);
    2574             :       }
    2575             :     }
    2576             :   }
    2577             : }
    2578             : 
    2579             : #if CONFIG_LOOP_RESTORATION
    2580             : static void decode_restoration_mode(AV1_COMMON *cm,
    2581             :                                     struct aom_read_bit_buffer *rb) {
    2582             :   int p;
    2583             :   RestorationInfo *rsi = &cm->rst_info[0];
    2584             :   if (aom_rb_read_bit(rb)) {
    2585             :     rsi->frame_restoration_type =
    2586             :         aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
    2587             :   } else {
    2588             :     rsi->frame_restoration_type =
    2589             :         aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
    2590             :   }
    2591             :   for (p = 1; p < MAX_MB_PLANE; ++p) {
    2592             :     rsi = &cm->rst_info[p];
    2593             :     if (aom_rb_read_bit(rb)) {
    2594             :       rsi->frame_restoration_type =
    2595             :           aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
    2596             :     } else {
    2597             :       rsi->frame_restoration_type = RESTORE_NONE;
    2598             :     }
    2599             :   }
    2600             : 
    2601             :   cm->rst_info[0].restoration_tilesize = RESTORATION_TILESIZE_MAX;
    2602             :   cm->rst_info[1].restoration_tilesize = RESTORATION_TILESIZE_MAX;
    2603             :   cm->rst_info[2].restoration_tilesize = RESTORATION_TILESIZE_MAX;
    2604             :   if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
    2605             :       cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
    2606             :       cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
    2607             :     rsi = &cm->rst_info[0];
    2608             :     rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
    2609             :     if (rsi->restoration_tilesize != RESTORATION_TILESIZE_MAX) {
    2610             :       rsi->restoration_tilesize >>= aom_rb_read_bit(rb);
    2611             :     }
    2612             :     cm->rst_info[1].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
    2613             :     cm->rst_info[2].restoration_tilesize = cm->rst_info[0].restoration_tilesize;
    2614             :   }
    2615             : }
    2616             : 
    2617             : static void read_wiener_filter(WienerInfo *wiener_info,
    2618             :                                WienerInfo *ref_wiener_info, aom_reader *rb) {
    2619             :   wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
    2620             :       aom_read_primitive_refsubexpfin(
    2621             :           rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
    2622             :           WIENER_FILT_TAP0_SUBEXP_K,
    2623             :           ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV, ACCT_STR) +
    2624             :       WIENER_FILT_TAP0_MINV;
    2625             :   wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
    2626             :       aom_read_primitive_refsubexpfin(
    2627             :           rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
    2628             :           WIENER_FILT_TAP1_SUBEXP_K,
    2629             :           ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV, ACCT_STR) +
    2630             :       WIENER_FILT_TAP1_MINV;
    2631             :   wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
    2632             :       aom_read_primitive_refsubexpfin(
    2633             :           rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
    2634             :           WIENER_FILT_TAP2_SUBEXP_K,
    2635             :           ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV, ACCT_STR) +
    2636             :       WIENER_FILT_TAP2_MINV;
    2637             :   // The central element has an implicit +WIENER_FILT_STEP
    2638             :   wiener_info->vfilter[WIENER_HALFWIN] =
    2639             :       -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
    2640             :             wiener_info->vfilter[2]);
    2641             : 
    2642             :   wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
    2643             :       aom_read_primitive_refsubexpfin(
    2644             :           rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
    2645             :           WIENER_FILT_TAP0_SUBEXP_K,
    2646             :           ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV, ACCT_STR) +
    2647             :       WIENER_FILT_TAP0_MINV;
    2648             :   wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
    2649             :       aom_read_primitive_refsubexpfin(
    2650             :           rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
    2651             :           WIENER_FILT_TAP1_SUBEXP_K,
    2652             :           ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV, ACCT_STR) +
    2653             :       WIENER_FILT_TAP1_MINV;
    2654             :   wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
    2655             :       aom_read_primitive_refsubexpfin(
    2656             :           rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
    2657             :           WIENER_FILT_TAP2_SUBEXP_K,
    2658             :           ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV, ACCT_STR) +
    2659             :       WIENER_FILT_TAP2_MINV;
    2660             :   // The central element has an implicit +WIENER_FILT_STEP
    2661             :   wiener_info->hfilter[WIENER_HALFWIN] =
    2662             :       -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
    2663             :             wiener_info->hfilter[2]);
    2664             :   memcpy(ref_wiener_info, wiener_info, sizeof(*wiener_info));
    2665             : }
    2666             : 
    2667             : static void read_sgrproj_filter(SgrprojInfo *sgrproj_info,
    2668             :                                 SgrprojInfo *ref_sgrproj_info, aom_reader *rb) {
    2669             :   sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
    2670             :   sgrproj_info->xqd[0] =
    2671             :       aom_read_primitive_refsubexpfin(
    2672             :           rb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
    2673             :           ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0, ACCT_STR) +
    2674             :       SGRPROJ_PRJ_MIN0;
    2675             :   sgrproj_info->xqd[1] =
    2676             :       aom_read_primitive_refsubexpfin(
    2677             :           rb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
    2678             :           ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1, ACCT_STR) +
    2679             :       SGRPROJ_PRJ_MIN1;
    2680             :   memcpy(ref_sgrproj_info, sgrproj_info, sizeof(*sgrproj_info));
    2681             : }
    2682             : 
    2683             : static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
    2684             :   int i, p;
    2685             :   SgrprojInfo ref_sgrproj_info;
    2686             :   WienerInfo ref_wiener_info;
    2687             :   set_default_wiener(&ref_wiener_info);
    2688             :   set_default_sgrproj(&ref_sgrproj_info);
    2689             :   const int ntiles = av1_get_rest_ntiles(cm->width, cm->height,
    2690             :                                          cm->rst_info[0].restoration_tilesize,
    2691             :                                          NULL, NULL, NULL, NULL);
    2692             :   const int ntiles_uv = av1_get_rest_ntiles(
    2693             :       ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
    2694             :       ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y),
    2695             :       cm->rst_info[1].restoration_tilesize, NULL, NULL, NULL, NULL);
    2696             :   RestorationInfo *rsi = &cm->rst_info[0];
    2697             :   if (rsi->frame_restoration_type != RESTORE_NONE) {
    2698             :     if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
    2699             :       for (i = 0; i < ntiles; ++i) {
    2700             :         rsi->restoration_type[i] =
    2701             :             aom_read_tree(rb, av1_switchable_restore_tree,
    2702             :                           cm->fc->switchable_restore_prob, ACCT_STR);
    2703             :         if (rsi->restoration_type[i] == RESTORE_WIENER) {
    2704             :           read_wiener_filter(&rsi->wiener_info[i], &ref_wiener_info, rb);
    2705             :         } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
    2706             :           read_sgrproj_filter(&rsi->sgrproj_info[i], &ref_sgrproj_info, rb);
    2707             :         }
    2708             :       }
    2709             :     } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
    2710             :       for (i = 0; i < ntiles; ++i) {
    2711             :         if (aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR)) {
    2712             :           rsi->restoration_type[i] = RESTORE_WIENER;
    2713             :           read_wiener_filter(&rsi->wiener_info[i], &ref_wiener_info, rb);
    2714             :         } else {
    2715             :           rsi->restoration_type[i] = RESTORE_NONE;
    2716             :         }
    2717             :       }
    2718             :     } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
    2719             :       for (i = 0; i < ntiles; ++i) {
    2720             :         if (aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)) {
    2721             :           rsi->restoration_type[i] = RESTORE_SGRPROJ;
    2722             :           read_sgrproj_filter(&rsi->sgrproj_info[i], &ref_sgrproj_info, rb);
    2723             :         } else {
    2724             :           rsi->restoration_type[i] = RESTORE_NONE;
    2725             :         }
    2726             :       }
    2727             :     }
    2728             :   }
    2729             :   for (p = 1; p < MAX_MB_PLANE; ++p) {
    2730             :     set_default_wiener(&ref_wiener_info);
    2731             :     set_default_sgrproj(&ref_sgrproj_info);
    2732             :     rsi = &cm->rst_info[p];
    2733             :     if (rsi->frame_restoration_type == RESTORE_WIENER) {
    2734             :       for (i = 0; i < ntiles_uv; ++i) {
    2735             :         if (ntiles_uv > 1)
    2736             :           rsi->restoration_type[i] =
    2737             :               aom_read(rb, RESTORE_NONE_WIENER_PROB, ACCT_STR) ? RESTORE_WIENER
    2738             :                                                                : RESTORE_NONE;
    2739             :         else
    2740             :           rsi->restoration_type[i] = RESTORE_WIENER;
    2741             :         if (rsi->restoration_type[i] == RESTORE_WIENER) {
    2742             :           read_wiener_filter(&rsi->wiener_info[i], &ref_wiener_info, rb);
    2743             :         }
    2744             :       }
    2745             :     } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
    2746             :       for (i = 0; i < ntiles_uv; ++i) {
    2747             :         if (ntiles_uv > 1)
    2748             :           rsi->restoration_type[i] =
    2749             :               aom_read(rb, RESTORE_NONE_SGRPROJ_PROB, ACCT_STR)
    2750             :                   ? RESTORE_SGRPROJ
    2751             :                   : RESTORE_NONE;
    2752             :         else
    2753             :           rsi->restoration_type[i] = RESTORE_SGRPROJ;
    2754             :         if (rsi->restoration_type[i] == RESTORE_SGRPROJ) {
    2755             :           read_sgrproj_filter(&rsi->sgrproj_info[i], &ref_sgrproj_info, rb);
    2756             :         }
    2757             :       }
    2758             :     } else if (rsi->frame_restoration_type != RESTORE_NONE) {
    2759             :       assert(0);
    2760             :     }
    2761             :   }
    2762             : }
    2763             : #endif  // CONFIG_LOOP_RESTORATION
    2764             : 
    2765           0 : static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
    2766           0 :   struct loopfilter *lf = &cm->lf;
    2767           0 :   lf->filter_level = aom_rb_read_literal(rb, 6);
    2768           0 :   lf->sharpness_level = aom_rb_read_literal(rb, 3);
    2769             : 
    2770             :   // Read in loop filter deltas applied at the MB level based on mode or ref
    2771             :   // frame.
    2772           0 :   lf->mode_ref_delta_update = 0;
    2773             : 
    2774           0 :   lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
    2775           0 :   if (lf->mode_ref_delta_enabled) {
    2776           0 :     lf->mode_ref_delta_update = aom_rb_read_bit(rb);
    2777           0 :     if (lf->mode_ref_delta_update) {
    2778             :       int i;
    2779             : 
    2780           0 :       for (i = 0; i < TOTAL_REFS_PER_FRAME; i++)
    2781           0 :         if (aom_rb_read_bit(rb))
    2782           0 :           lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
    2783             : 
    2784           0 :       for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
    2785           0 :         if (aom_rb_read_bit(rb))
    2786           0 :           lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
    2787             :     }
    2788             :   }
    2789           0 : }
    2790             : 
    2791             : #if CONFIG_CDEF
    2792           0 : static void setup_cdef(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
    2793             :   int i;
    2794           0 :   cm->cdef_dering_damping = aom_rb_read_literal(rb, 1) + 5;
    2795           0 :   cm->cdef_clpf_damping = aom_rb_read_literal(rb, 2) + 3;
    2796           0 :   cm->cdef_bits = aom_rb_read_literal(rb, 2);
    2797           0 :   cm->nb_cdef_strengths = 1 << cm->cdef_bits;
    2798           0 :   for (i = 0; i < cm->nb_cdef_strengths; i++) {
    2799           0 :     cm->cdef_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
    2800           0 :     cm->cdef_uv_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
    2801             :   }
    2802           0 : }
    2803             : #endif  // CONFIG_CDEF
    2804             : 
    2805           0 : static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
    2806           0 :   return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
    2807             : }
    2808             : 
    2809           0 : static void setup_quantization(AV1_COMMON *const cm,
    2810             :                                struct aom_read_bit_buffer *rb) {
    2811           0 :   cm->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
    2812           0 :   cm->y_dc_delta_q = read_delta_q(rb);
    2813           0 :   cm->uv_dc_delta_q = read_delta_q(rb);
    2814           0 :   cm->uv_ac_delta_q = read_delta_q(rb);
    2815           0 :   cm->dequant_bit_depth = cm->bit_depth;
    2816             : #if CONFIG_AOM_QM
    2817             :   cm->using_qmatrix = aom_rb_read_bit(rb);
    2818             :   if (cm->using_qmatrix) {
    2819             :     cm->min_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
    2820             :     cm->max_qmlevel = aom_rb_read_literal(rb, QM_LEVEL_BITS);
    2821             :   } else {
    2822             :     cm->min_qmlevel = 0;
    2823             :     cm->max_qmlevel = 0;
    2824             :   }
    2825             : #endif
    2826           0 : }
    2827             : 
    2828             : // Build y/uv dequant values based on segmentation.
    2829           0 : static void setup_segmentation_dequant(AV1_COMMON *const cm) {
    2830             : #if CONFIG_AOM_QM
    2831             :   const int using_qm = cm->using_qmatrix;
    2832             :   const int minqm = cm->min_qmlevel;
    2833             :   const int maxqm = cm->max_qmlevel;
    2834             : #endif
    2835             :   // When segmentation is disabled, only the first value is used.  The
    2836             :   // remaining are don't cares.
    2837           0 :   const int max_segments = cm->seg.enabled ? MAX_SEGMENTS : 1;
    2838           0 :   for (int i = 0; i < max_segments; ++i) {
    2839           0 :     const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
    2840           0 :     cm->y_dequant[i][0] = av1_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
    2841           0 :     cm->y_dequant[i][1] = av1_ac_quant(qindex, 0, cm->bit_depth);
    2842           0 :     cm->uv_dequant[i][0] =
    2843           0 :         av1_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
    2844           0 :     cm->uv_dequant[i][1] =
    2845           0 :         av1_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
    2846             : #if CONFIG_AOM_QM
    2847             :     const int lossless = qindex == 0 && cm->y_dc_delta_q == 0 &&
    2848             :                          cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
    2849             :     // NB: depends on base index so there is only 1 set per frame
    2850             :     // No quant weighting when lossless or signalled not using QM
    2851             :     const int qmlevel = (lossless || using_qm == 0)
    2852             :                             ? NUM_QM_LEVELS - 1
    2853             :                             : aom_get_qmlevel(cm->base_qindex, minqm, maxqm);
    2854             :     for (int j = 0; j < TX_SIZES_ALL; ++j) {
    2855             :       cm->y_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 0, j, 1);
    2856             :       cm->y_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 0, j, 0);
    2857             :       cm->uv_iqmatrix[i][1][j] = aom_iqmatrix(cm, qmlevel, 1, j, 1);
    2858             :       cm->uv_iqmatrix[i][0][j] = aom_iqmatrix(cm, qmlevel, 1, j, 0);
    2859             :     }
    2860             : #endif  // CONFIG_AOM_QM
    2861             : #if CONFIG_NEW_QUANT
    2862             :     for (int dq = 0; dq < QUANT_PROFILES; dq++) {
    2863             :       for (int b = 0; b < COEF_BANDS; ++b) {
    2864             :         av1_get_dequant_val_nuq(cm->y_dequant[i][b != 0], b,
    2865             :                                 cm->y_dequant_nuq[i][dq][b], NULL, dq);
    2866             :         av1_get_dequant_val_nuq(cm->uv_dequant[i][b != 0], b,
    2867             :                                 cm->uv_dequant_nuq[i][dq][b], NULL, dq);
    2868             :       }
    2869             :     }
    2870             : #endif  //  CONFIG_NEW_QUANT
    2871             :   }
    2872           0 : }
    2873             : 
    2874           0 : static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
    2875           0 :   return aom_rb_read_bit(rb) ? SWITCHABLE
    2876           0 :                              : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
    2877             : }
    2878             : 
    2879           0 : static void setup_render_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
    2880           0 :   cm->render_width = cm->width;
    2881           0 :   cm->render_height = cm->height;
    2882           0 :   if (aom_rb_read_bit(rb))
    2883           0 :     av1_read_frame_size(rb, &cm->render_width, &cm->render_height);
    2884           0 : }
    2885             : 
    2886             : #if CONFIG_FRAME_SUPERRES
    2887             : // TODO(afergs): make "struct aom_read_bit_buffer *const rb"?
    2888             : static void setup_superres_size(AV1_COMMON *const cm,
    2889             :                                 struct aom_read_bit_buffer *rb, int *width,
    2890             :                                 int *height) {
    2891             :   // TODO(afergs): Save input resolution - it's the upscaled resolution
    2892             :   if (aom_rb_read_bit(rb)) {
    2893             :     cm->superres_scale_numerator =
    2894             :         (uint8_t)aom_rb_read_literal(rb, SUPERRES_SCALE_BITS);
    2895             :     cm->superres_scale_numerator += SUPERRES_SCALE_NUMERATOR_MIN;
    2896             :     // Don't edit cm->width or cm->height directly, or the buffers won't get
    2897             :     // resized correctly
    2898             :     // TODO(afergs): Should the render resolution not be modified? It's the same
    2899             :     // by default (ie. when it isn't sent)...
    2900             :     // resize_context_buffers() will change cm->width to equal cm->render_width,
    2901             :     // then they'll be the same again
    2902             :     *width = *width * cm->superres_scale_numerator / SUPERRES_SCALE_DENOMINATOR;
    2903             :     *height =
    2904             :         *width * cm->superres_scale_numerator / SUPERRES_SCALE_DENOMINATOR;
    2905             :   } else {
    2906             :     // 1:1 scaling - ie. no scaling, scale not provided
    2907             :     cm->superres_scale_numerator = SUPERRES_SCALE_DENOMINATOR;
    2908             :   }
    2909             : }
    2910             : #endif  // CONFIG_FRAME_SUPERRES
    2911             : 
    2912           0 : static void resize_mv_buffer(AV1_COMMON *cm) {
    2913           0 :   aom_free(cm->cur_frame->mvs);
    2914           0 :   cm->cur_frame->mi_rows = cm->mi_rows;
    2915           0 :   cm->cur_frame->mi_cols = cm->mi_cols;
    2916           0 :   CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
    2917             :                   (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
    2918             :                                        sizeof(*cm->cur_frame->mvs)));
    2919           0 : }
    2920             : 
    2921           0 : static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
    2922             : #if CONFIG_SIZE_LIMIT
    2923           0 :   if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
    2924           0 :     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
    2925             :                        "Dimensions of %dx%d beyond allowed size of %dx%d.",
    2926             :                        width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
    2927             : #endif
    2928           0 :   if (cm->width != width || cm->height != height) {
    2929           0 :     const int new_mi_rows =
    2930           0 :         ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
    2931           0 :     const int new_mi_cols =
    2932           0 :         ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
    2933             : 
    2934             :     // Allocations in av1_alloc_context_buffers() depend on individual
    2935             :     // dimensions as well as the overall size.
    2936           0 :     if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
    2937           0 :       if (av1_alloc_context_buffers(cm, width, height))
    2938           0 :         aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
    2939             :                            "Failed to allocate context buffers");
    2940             :     } else {
    2941           0 :       av1_set_mb_mi(cm, width, height);
    2942             :     }
    2943           0 :     av1_init_context_buffers(cm);
    2944           0 :     cm->width = width;
    2945           0 :     cm->height = height;
    2946             :   }
    2947           0 :   if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
    2948           0 :       cm->mi_cols > cm->cur_frame->mi_cols) {
    2949           0 :     resize_mv_buffer(cm);
    2950             :   }
    2951           0 : }
    2952             : 
    2953           0 : static void setup_frame_size(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
    2954             :   int width, height;
    2955           0 :   BufferPool *const pool = cm->buffer_pool;
    2956           0 :   av1_read_frame_size(rb, &width, &height);
    2957           0 :   setup_render_size(cm, rb);
    2958             : #if CONFIG_FRAME_SUPERRES
    2959             :   setup_superres_size(cm, rb, &width, &height);
    2960             : #endif  // CONFIG_FRAME_SUPERRES
    2961           0 :   resize_context_buffers(cm, width, height);
    2962             : 
    2963           0 :   lock_buffer_pool(pool);
    2964           0 :   if (aom_realloc_frame_buffer(
    2965             :           get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
    2966             :           cm->subsampling_y,
    2967             : #if CONFIG_HIGHBITDEPTH
    2968             :           cm->use_highbitdepth,
    2969             : #endif
    2970             :           AOM_BORDER_IN_PIXELS, cm->byte_alignment,
    2971           0 :           &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
    2972             :           pool->cb_priv)) {
    2973           0 :     unlock_buffer_pool(pool);
    2974           0 :     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
    2975             :                        "Failed to allocate frame buffer");
    2976             :   }
    2977           0 :   unlock_buffer_pool(pool);
    2978             : 
    2979           0 :   pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
    2980           0 :   pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
    2981           0 :   pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
    2982           0 :   pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
    2983           0 :   pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
    2984           0 :   pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
    2985           0 :   pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
    2986           0 : }
    2987             : 
    2988           0 : static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
    2989             :                                           int ref_xss, int ref_yss,
    2990             :                                           aom_bit_depth_t this_bit_depth,
    2991             :                                           int this_xss, int this_yss) {
    2992           0 :   return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
    2993             :          ref_yss == this_yss;
    2994             : }
    2995             : 
    2996           0 : static void setup_frame_size_with_refs(AV1_COMMON *cm,
    2997             :                                        struct aom_read_bit_buffer *rb) {
    2998             :   int width, height;
    2999           0 :   int found = 0, i;
    3000           0 :   int has_valid_ref_frame = 0;
    3001           0 :   BufferPool *const pool = cm->buffer_pool;
    3002           0 :   for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
    3003           0 :     if (aom_rb_read_bit(rb)) {
    3004           0 :       YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
    3005           0 :       width = buf->y_crop_width;
    3006           0 :       height = buf->y_crop_height;
    3007           0 :       cm->render_width = buf->render_width;
    3008           0 :       cm->render_height = buf->render_height;
    3009           0 :       found = 1;
    3010           0 :       break;
    3011             :     }
    3012             :   }
    3013             : 
    3014           0 :   if (!found) {
    3015           0 :     av1_read_frame_size(rb, &width, &height);
    3016           0 :     setup_render_size(cm, rb);
    3017             : #if CONFIG_FRAME_SUPERRES
    3018             :     setup_superres_size(cm, rb, &width, &height);
    3019             : #endif  // CONFIG_FRAME_SUPERRES
    3020             :   }
    3021             : 
    3022           0 :   if (width <= 0 || height <= 0)
    3023           0 :     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
    3024             :                        "Invalid frame size");
    3025             : 
    3026             :   // Check to make sure at least one of frames that this frame references
    3027             :   // has valid dimensions.
    3028           0 :   for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
    3029           0 :     RefBuffer *const ref_frame = &cm->frame_refs[i];
    3030           0 :     has_valid_ref_frame |=
    3031           0 :         valid_ref_frame_size(ref_frame->buf->y_crop_width,
    3032           0 :                              ref_frame->buf->y_crop_height, width, height);
    3033             :   }
    3034           0 :   if (!has_valid_ref_frame)
    3035           0 :     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
    3036             :                        "Referenced frame has invalid size");
    3037           0 :   for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
    3038           0 :     RefBuffer *const ref_frame = &cm->frame_refs[i];
    3039           0 :     if (!valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
    3040           0 :                                  ref_frame->buf->subsampling_x,
    3041           0 :                                  ref_frame->buf->subsampling_y, cm->bit_depth,
    3042             :                                  cm->subsampling_x, cm->subsampling_y))
    3043           0 :       aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
    3044             :                          "Referenced frame has incompatible color format");
    3045             :   }
    3046             : 
    3047           0 :   resize_context_buffers(cm, width, height);
    3048             : 
    3049           0 :   lock_buffer_pool(pool);
    3050           0 :   if (aom_realloc_frame_buffer(
    3051             :           get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
    3052             :           cm->subsampling_y,
    3053             : #if CONFIG_HIGHBITDEPTH
    3054             :           cm->use_highbitdepth,
    3055             : #endif
    3056             :           AOM_BORDER_IN_PIXELS, cm->byte_alignment,
    3057           0 :           &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
    3058             :           pool->cb_priv)) {
    3059           0 :     unlock_buffer_pool(pool);
    3060           0 :     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
    3061             :                        "Failed to allocate frame buffer");
    3062             :   }
    3063           0 :   unlock_buffer_pool(pool);
    3064             : 
    3065           0 :   pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
    3066           0 :   pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
    3067           0 :   pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
    3068           0 :   pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
    3069           0 :   pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
    3070           0 :   pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
    3071           0 :   pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
    3072           0 : }
    3073             : 
    3074           0 : static void read_tile_info(AV1Decoder *const pbi,
    3075             :                            struct aom_read_bit_buffer *const rb) {
    3076           0 :   AV1_COMMON *const cm = &pbi->common;
    3077             : #if CONFIG_EXT_TILE
    3078             :   cm->tile_encoding_mode = aom_rb_read_literal(rb, 1);
    3079             : // Read the tile width/height
    3080             : #if CONFIG_EXT_PARTITION
    3081             :   if (cm->sb_size == BLOCK_128X128) {
    3082             :     cm->tile_width = aom_rb_read_literal(rb, 5) + 1;
    3083             :     cm->tile_height = aom_rb_read_literal(rb, 5) + 1;
    3084             :   } else
    3085             : #endif  // CONFIG_EXT_PARTITION
    3086             :   {
    3087             :     cm->tile_width = aom_rb_read_literal(rb, 6) + 1;
    3088             :     cm->tile_height = aom_rb_read_literal(rb, 6) + 1;
    3089             :   }
    3090             : 
    3091             : #if CONFIG_LOOPFILTERING_ACROSS_TILES
    3092             :   cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
    3093             : #endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
    3094             : 
    3095             :   cm->tile_width <<= cm->mib_size_log2;
    3096             :   cm->tile_height <<= cm->mib_size_log2;
    3097             : 
    3098             :   cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
    3099             :   cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
    3100             : 
    3101             :   // Get the number of tiles
    3102             :   cm->tile_cols = 1;
    3103             :   while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
    3104             : 
    3105             :   cm->tile_rows = 1;
    3106             :   while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
    3107             : 
    3108             :   if (cm->tile_cols * cm->tile_rows > 1) {
    3109             :     // Read the number of bytes used to store tile size
    3110             :     pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
    3111             :     pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
    3112             :   }
    3113             : 
    3114             : #if CONFIG_DEPENDENT_HORZTILES
    3115             :   if (cm->tile_rows <= 1)
    3116             :     cm->dependent_horz_tiles = aom_rb_read_bit(rb);
    3117             :   else
    3118             :     cm->dependent_horz_tiles = 0;
    3119             : #endif
    3120             : #else
    3121             :   int min_log2_tile_cols, max_log2_tile_cols, max_ones;
    3122           0 :   av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
    3123             : 
    3124             :   // columns
    3125           0 :   max_ones = max_log2_tile_cols - min_log2_tile_cols;
    3126           0 :   cm->log2_tile_cols = min_log2_tile_cols;
    3127           0 :   while (max_ones-- && aom_rb_read_bit(rb)) cm->log2_tile_cols++;
    3128             : 
    3129           0 :   if (cm->log2_tile_cols > 6)
    3130           0 :     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
    3131             :                        "Invalid number of tile columns");
    3132             : 
    3133             :   // rows
    3134           0 :   cm->log2_tile_rows = aom_rb_read_bit(rb);
    3135           0 :   if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
    3136             : #if CONFIG_DEPENDENT_HORZTILES
    3137             :   if (cm->log2_tile_rows != 0)
    3138             :     cm->dependent_horz_tiles = aom_rb_read_bit(rb);
    3139             :   else
    3140             :     cm->dependent_horz_tiles = 0;
    3141             : #endif
    3142             : #if CONFIG_LOOPFILTERING_ACROSS_TILES
    3143           0 :   cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
    3144             : #endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
    3145             : 
    3146           0 :   cm->tile_cols = 1 << cm->log2_tile_cols;
    3147           0 :   cm->tile_rows = 1 << cm->log2_tile_rows;
    3148             : 
    3149           0 :   cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
    3150           0 :   cm->tile_width >>= cm->log2_tile_cols;
    3151           0 :   cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
    3152           0 :   cm->tile_height >>= cm->log2_tile_rows;
    3153             : 
    3154             :   // round to integer multiples of superblock size
    3155           0 :   cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
    3156           0 :   cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
    3157             : 
    3158             : // tile size magnitude
    3159             : #if !CONFIG_TILE_GROUPS
    3160             :   if (cm->tile_rows > 1 || cm->tile_cols > 1)
    3161             : #endif
    3162           0 :     pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
    3163             : #endif  // CONFIG_EXT_TILE
    3164             : 
    3165             : #if CONFIG_TILE_GROUPS
    3166             :   // Store an index to the location of the tile group information
    3167           0 :   pbi->tg_size_bit_offset = rb->bit_offset;
    3168           0 :   pbi->tg_size = 1 << (cm->log2_tile_rows + cm->log2_tile_cols);
    3169           0 :   if (cm->log2_tile_rows + cm->log2_tile_cols > 0) {
    3170           0 :     pbi->tg_start =
    3171           0 :         aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
    3172           0 :     pbi->tg_size =
    3173           0 :         1 + aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
    3174             :   }
    3175             : #endif
    3176           0 : }
    3177             : 
    3178           0 : static int mem_get_varsize(const uint8_t *src, int sz) {
    3179           0 :   switch (sz) {
    3180           0 :     case 1: return src[0];
    3181           0 :     case 2: return mem_get_le16(src);
    3182           0 :     case 3: return mem_get_le24(src);
    3183           0 :     case 4: return mem_get_le32(src);
    3184           0 :     default: assert("Invalid size" && 0); return -1;
    3185             :   }
    3186             : }
    3187             : 
    3188             : #if CONFIG_EXT_TILE
    3189             : // Reads the next tile returning its size and adjusting '*data' accordingly
    3190             : // based on 'is_last'.
    3191             : static void get_tile_buffer(const uint8_t *const data_end,
    3192             :                             struct aom_internal_error_info *error_info,
    3193             :                             const uint8_t **data, aom_decrypt_cb decrypt_cb,
    3194             :                             void *decrypt_state,
    3195             :                             TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
    3196             :                             int tile_size_bytes, int col, int row,
    3197             :                             unsigned int tile_encoding_mode) {
    3198             :   size_t size;
    3199             : 
    3200             :   size_t copy_size = 0;
    3201             :   const uint8_t *copy_data = NULL;
    3202             : 
    3203             :   if (!read_is_valid(*data, tile_size_bytes, data_end))
    3204             :     aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
    3205             :                        "Truncated packet or corrupt tile length");
    3206             :   if (decrypt_cb) {
    3207             :     uint8_t be_data[4];
    3208             :     decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
    3209             : 
    3210             :     // Only read number of bytes in cm->tile_size_bytes.
    3211             :     size = mem_get_varsize(be_data, tile_size_bytes);
    3212             :   } else {
    3213             :     size = mem_get_varsize(*data, tile_size_bytes);
    3214             :   }
    3215             : 
    3216             :   // If cm->tile_encoding_mode = 1 (i.e. TILE_VR), then the top bit of the tile
    3217             :   // header indicates copy mode.
    3218             :   if (tile_encoding_mode && (size >> (tile_size_bytes * 8 - 1)) == 1) {
    3219             :     // The remaining bits in the top byte signal the row offset
    3220             :     int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
    3221             : 
    3222             :     // Currently, only use tiles in same column as reference tiles.
    3223             :     copy_data = tile_buffers[row - offset][col].data;
    3224             :     copy_size = tile_buffers[row - offset][col].size;
    3225             :     size = 0;
    3226             :   }
    3227             : 
    3228             :   *data += tile_size_bytes;
    3229             : 
    3230             :   if (size > (size_t)(data_end - *data))
    3231             :     aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
    3232             :                        "Truncated packet or corrupt tile size");
    3233             : 
    3234             :   if (size > 0) {
    3235             :     tile_buffers[row][col].data = *data;
    3236             :     tile_buffers[row][col].size = size;
    3237             :   } else {
    3238             :     tile_buffers[row][col].data = copy_data;
    3239             :     tile_buffers[row][col].size = copy_size;
    3240             :   }
    3241             : 
    3242             :   *data += size;
    3243             : 
    3244             :   tile_buffers[row][col].raw_data_end = *data;
    3245             : }
    3246             : 
    3247             : static void get_tile_buffers(
    3248             :     AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
    3249             :     TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
    3250             :   AV1_COMMON *const cm = &pbi->common;
    3251             :   const int tile_cols = cm->tile_cols;
    3252             :   const int tile_rows = cm->tile_rows;
    3253             :   const int have_tiles = tile_cols * tile_rows > 1;
    3254             : 
    3255             :   if (!have_tiles) {
    3256             :     const size_t tile_size = data_end - data;
    3257             :     tile_buffers[0][0].data = data;
    3258             :     tile_buffers[0][0].size = tile_size;
    3259             :     tile_buffers[0][0].raw_data_end = NULL;
    3260             :   } else {
    3261             :     // We locate only the tile buffers that are required, which are the ones
    3262             :     // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
    3263             :     // need the last (bottom right) tile buffer, as we need to know where the
    3264             :     // end of the compressed frame buffer is for proper superframe decoding.
    3265             : 
    3266             :     const uint8_t *tile_col_data_end[MAX_TILE_COLS];
    3267             :     const uint8_t *const data_start = data;
    3268             : 
    3269             :     const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
    3270             :     const int single_row = pbi->dec_tile_row >= 0;
    3271             :     const int tile_rows_start = single_row ? dec_tile_row : 0;
    3272             :     const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
    3273             :     const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
    3274             :     const int single_col = pbi->dec_tile_col >= 0;
    3275             :     const int tile_cols_start = single_col ? dec_tile_col : 0;
    3276             :     const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
    3277             : 
    3278             :     const int tile_col_size_bytes = pbi->tile_col_size_bytes;
    3279             :     const int tile_size_bytes = pbi->tile_size_bytes;
    3280             : 
    3281             :     size_t tile_col_size;
    3282             :     int r, c;
    3283             : 
    3284             :     // Read tile column sizes for all columns (we need the last tile buffer)
    3285             :     for (c = 0; c < tile_cols; ++c) {
    3286             :       const int is_last = c == tile_cols - 1;
    3287             :       if (!is_last) {
    3288             :         tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
    3289             :         data += tile_col_size_bytes;
    3290             :         tile_col_data_end[c] = data + tile_col_size;
    3291             :       } else {
    3292             :         tile_col_size = data_end - data;
    3293             :         tile_col_data_end[c] = data_end;
    3294             :       }
    3295             :       data += tile_col_size;
    3296             :     }
    3297             : 
    3298             :     data = data_start;
    3299             : 
    3300             :     // Read the required tile sizes.
    3301             :     for (c = tile_cols_start; c < tile_cols_end; ++c) {
    3302             :       const int is_last = c == tile_cols - 1;
    3303             : 
    3304             :       if (c > 0) data = tile_col_data_end[c - 1];
    3305             : 
    3306             :       if (!is_last) data += tile_col_size_bytes;
    3307             : 
    3308             :       // Get the whole of the last column, otherwise stop at the required tile.
    3309             :       for (r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
    3310             :         tile_buffers[r][c].col = c;
    3311             : 
    3312             :         get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
    3313             :                         pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
    3314             :                         tile_size_bytes, c, r, cm->tile_encoding_mode);
    3315             :       }
    3316             :     }
    3317             : 
    3318             :     // If we have not read the last column, then read it to get the last tile.
    3319             :     if (tile_cols_end != tile_cols) {
    3320             :       c = tile_cols - 1;
    3321             : 
    3322             :       data = tile_col_data_end[c - 1];
    3323             : 
    3324             :       for (r = 0; r < tile_rows; ++r) {
    3325             :         tile_buffers[r][c].col = c;
    3326             : 
    3327             :         get_tile_buffer(tile_col_data_end[c], &pbi->common.error, &data,
    3328             :                         pbi->decrypt_cb, pbi->decrypt_state, tile_buffers,
    3329             :                         tile_size_bytes, c, r, cm->tile_encoding_mode);
    3330             :       }
    3331             :     }
    3332             :   }
    3333             : }
    3334             : #else
    3335             : // Reads the next tile returning its size and adjusting '*data' accordingly
    3336             : // based on 'is_last'.
    3337           0 : static void get_tile_buffer(const uint8_t *const data_end,
    3338             :                             const int tile_size_bytes, int is_last,
    3339             :                             struct aom_internal_error_info *error_info,
    3340             :                             const uint8_t **data, aom_decrypt_cb decrypt_cb,
    3341             :                             void *decrypt_state, TileBufferDec *const buf) {
    3342             :   size_t size;
    3343             : 
    3344           0 :   if (!is_last) {
    3345           0 :     if (!read_is_valid(*data, tile_size_bytes, data_end))
    3346           0 :       aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
    3347             :                          "Truncated packet or corrupt tile length");
    3348             : 
    3349           0 :     if (decrypt_cb) {
    3350             :       uint8_t be_data[4];
    3351           0 :       decrypt_cb(decrypt_state, *data, be_data, tile_size_bytes);
    3352           0 :       size = mem_get_varsize(be_data, tile_size_bytes);
    3353             :     } else {
    3354           0 :       size = mem_get_varsize(*data, tile_size_bytes);
    3355             :     }
    3356           0 :     *data += tile_size_bytes;
    3357             : 
    3358           0 :     if (size > (size_t)(data_end - *data))
    3359           0 :       aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
    3360             :                          "Truncated packet or corrupt tile size");
    3361             :   } else {
    3362           0 :     size = data_end - *data;
    3363             :   }
    3364             : 
    3365           0 :   buf->data = *data;
    3366           0 :   buf->size = size;
    3367             : 
    3368           0 :   *data += size;
    3369           0 : }
    3370             : 
    3371           0 : static void get_tile_buffers(
    3372             :     AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
    3373             :     TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
    3374           0 :   AV1_COMMON *const cm = &pbi->common;
    3375             : #if CONFIG_TILE_GROUPS
    3376             :   int r, c;
    3377           0 :   const int tile_cols = cm->tile_cols;
    3378           0 :   const int tile_rows = cm->tile_rows;
    3379           0 :   int tc = 0;
    3380           0 :   int first_tile_in_tg = 0;
    3381             :   struct aom_read_bit_buffer rb_tg_hdr;
    3382             :   uint8_t clear_data[MAX_AV1_HEADER_SIZE];
    3383           0 :   const int num_tiles = tile_rows * tile_cols;
    3384           0 :   const int num_bits = OD_ILOG(num_tiles) - 1;
    3385           0 :   const size_t hdr_size = pbi->uncomp_hdr_size + pbi->first_partition_size;
    3386           0 :   const int tg_size_bit_offset = pbi->tg_size_bit_offset;
    3387             : #if CONFIG_DEPENDENT_HORZTILES
    3388             :   int tile_group_start_col = 0;
    3389             :   int tile_group_start_row = 0;
    3390             : #endif
    3391             : 
    3392           0 :   for (r = 0; r < tile_rows; ++r) {
    3393           0 :     for (c = 0; c < tile_cols; ++c, ++tc) {
    3394           0 :       TileBufferDec *const buf = &tile_buffers[r][c];
    3395           0 :       const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
    3396           0 :       const size_t hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
    3397             : 
    3398           0 :       buf->col = c;
    3399           0 :       if (hdr_offset) {
    3400           0 :         init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end, clear_data);
    3401           0 :         rb_tg_hdr.bit_offset = tg_size_bit_offset;
    3402           0 :         if (num_tiles) {
    3403           0 :           pbi->tg_start = aom_rb_read_literal(&rb_tg_hdr, num_bits);
    3404           0 :           pbi->tg_size = 1 + aom_rb_read_literal(&rb_tg_hdr, num_bits);
    3405             : #if CONFIG_DEPENDENT_HORZTILES
    3406             :           tile_group_start_row = r;
    3407             :           tile_group_start_col = c;
    3408             : #endif
    3409             :         }
    3410             :       }
    3411           0 :       first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
    3412           0 :       data += hdr_offset;
    3413           0 :       get_tile_buffer(data_end, pbi->tile_size_bytes, is_last,
    3414             :                       &pbi->common.error, &data, pbi->decrypt_cb,
    3415             :                       pbi->decrypt_state, buf);
    3416             : #if CONFIG_DEPENDENT_HORZTILES
    3417             :       cm->tile_group_start_row[r][c] = tile_group_start_row;
    3418             :       cm->tile_group_start_col[r][c] = tile_group_start_col;
    3419             : #endif
    3420             :     }
    3421             :   }
    3422             : #else
    3423             :   int r, c;
    3424             :   const int tile_cols = cm->tile_cols;
    3425             :   const int tile_rows = cm->tile_rows;
    3426             : 
    3427             :   for (r = 0; r < tile_rows; ++r) {
    3428             :     for (c = 0; c < tile_cols; ++c) {
    3429             :       const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
    3430             :       TileBufferDec *const buf = &tile_buffers[r][c];
    3431             :       buf->col = c;
    3432             :       get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &cm->error,
    3433             :                       &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
    3434             :     }
    3435             :   }
    3436             : #endif
    3437           0 : }
    3438             : #endif  // CONFIG_EXT_TILE
    3439             : 
    3440             : #if CONFIG_PVQ
    3441             : static void daala_dec_init(AV1_COMMON *const cm, daala_dec_ctx *daala_dec,
    3442             :                            aom_reader *r) {
    3443             :   daala_dec->r = r;
    3444             : 
    3445             :   // TODO(yushin) : activity masking info needs be signaled by a bitstream
    3446             :   daala_dec->use_activity_masking = AV1_PVQ_ENABLE_ACTIVITY_MASKING;
    3447             : 
    3448             : #if !CONFIG_DAALA_DIST
    3449             :   daala_dec->use_activity_masking = 0;
    3450             : #endif
    3451             : 
    3452             :   if (daala_dec->use_activity_masking)
    3453             :     daala_dec->qm = OD_HVS_QM;
    3454             :   else
    3455             :     daala_dec->qm = OD_FLAT_QM;
    3456             : 
    3457             :   od_init_qm(daala_dec->state.qm, daala_dec->state.qm_inv,
    3458             :              daala_dec->qm == OD_HVS_QM ? OD_QM8_Q4_HVS : OD_QM8_Q4_FLAT);
    3459             : 
    3460             :   if (daala_dec->use_activity_masking) {
    3461             :     int pli;
    3462             :     int use_masking = daala_dec->use_activity_masking;
    3463             :     int segment_id = 0;
    3464             :     int qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
    3465             : 
    3466             :     for (pli = 0; pli < MAX_MB_PLANE; pli++) {
    3467             :       int i;
    3468             :       int q;
    3469             : 
    3470             :       q = qindex;
    3471             :       if (q <= OD_DEFAULT_QMS[use_masking][0][pli].interp_q << OD_COEFF_SHIFT) {
    3472             :         od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
    3473             :                      &OD_DEFAULT_QMS[use_masking][0][pli], NULL);
    3474             :       } else {
    3475             :         i = 0;
    3476             :         while (OD_DEFAULT_QMS[use_masking][i + 1][pli].qm_q4 != NULL &&
    3477             :                q > OD_DEFAULT_QMS[use_masking][i + 1][pli].interp_q
    3478             :                        << OD_COEFF_SHIFT) {
    3479             :           i++;
    3480             :         }
    3481             :         od_interp_qm(&daala_dec->state.pvq_qm_q4[pli][0], q,
    3482             :                      &OD_DEFAULT_QMS[use_masking][i][pli],
    3483             :                      &OD_DEFAULT_QMS[use_masking][i + 1][pli]);
    3484             :       }
    3485             :     }
    3486             :   }
    3487             : }
    3488             : #endif  // #if CONFIG_PVQ
    3489             : 
    3490           0 : static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
    3491             :                                    const uint8_t *data_end) {
    3492           0 :   AV1_COMMON *const cm = &pbi->common;
    3493           0 :   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
    3494           0 :   const int tile_cols = cm->tile_cols;
    3495           0 :   const int tile_rows = cm->tile_rows;
    3496           0 :   const int n_tiles = tile_cols * tile_rows;
    3497           0 :   TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
    3498             : #if CONFIG_EXT_TILE
    3499             :   const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
    3500             :   const int single_row = pbi->dec_tile_row >= 0;
    3501             :   const int tile_rows_start = single_row ? dec_tile_row : 0;
    3502             :   const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
    3503             :   const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
    3504             :   const int single_col = pbi->dec_tile_col >= 0;
    3505             :   const int tile_cols_start = single_col ? dec_tile_col : 0;
    3506             :   const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
    3507             :   const int inv_col_order = pbi->inv_tile_order && !single_col;
    3508             :   const int inv_row_order = pbi->inv_tile_order && !single_row;
    3509             : #else
    3510           0 :   const int tile_rows_start = 0;
    3511           0 :   const int tile_rows_end = tile_rows;
    3512           0 :   const int tile_cols_start = 0;
    3513           0 :   const int tile_cols_end = tile_cols;
    3514           0 :   const int inv_col_order = pbi->inv_tile_order;
    3515           0 :   const int inv_row_order = pbi->inv_tile_order;
    3516             : #endif  // CONFIG_EXT_TILE
    3517             :   int tile_row, tile_col;
    3518             : 
    3519           0 :   if (cm->lf.filter_level && !cm->skip_loop_filter &&
    3520           0 :       pbi->lf_worker.data1 == NULL) {
    3521           0 :     CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
    3522             :                     aom_memalign(32, sizeof(LFWorkerData)));
    3523           0 :     pbi->lf_worker.hook = (AVxWorkerHook)av1_loop_filter_worker;
    3524           0 :     if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
    3525           0 :       aom_internal_error(&cm->error, AOM_CODEC_ERROR,
    3526             :                          "Loop filter thread creation failed");
    3527             :     }
    3528             :   }
    3529             : 
    3530           0 :   if (cm->lf.filter_level && !cm->skip_loop_filter) {
    3531           0 :     LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
    3532             :     // Be sure to sync as we might be resuming after a failed frame decode.
    3533           0 :     winterface->sync(&pbi->lf_worker);
    3534           0 :     av1_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
    3535           0 :                                pbi->mb.plane);
    3536             :   }
    3537             : 
    3538           0 :   assert(tile_rows <= MAX_TILE_ROWS);
    3539           0 :   assert(tile_cols <= MAX_TILE_COLS);
    3540             : 
    3541           0 :   get_tile_buffers(pbi, data, data_end, tile_buffers);
    3542             : 
    3543           0 :   if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
    3544           0 :     aom_free(pbi->tile_data);
    3545           0 :     CHECK_MEM_ERROR(cm, pbi->tile_data,
    3546             :                     aom_memalign(32, n_tiles * (sizeof(*pbi->tile_data))));
    3547           0 :     pbi->allocated_tiles = n_tiles;
    3548             :   }
    3549             : #if CONFIG_ACCOUNTING
    3550             :   if (pbi->acct_enabled) {
    3551             :     aom_accounting_reset(&pbi->accounting);
    3552             :   }
    3553             : #endif
    3554             :   // Load all tile information into tile_data.
    3555           0 :   for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
    3556           0 :     for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
    3557           0 :       const TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
    3558           0 :       TileData *const td = pbi->tile_data + tile_cols * tile_row + tile_col;
    3559             : 
    3560           0 :       td->cm = cm;
    3561           0 :       td->xd = pbi->mb;
    3562           0 :       td->xd.corrupted = 0;
    3563           0 :       td->xd.counts =
    3564           0 :           cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
    3565             :               ? &cm->counts
    3566           0 :               : NULL;
    3567           0 :       av1_zero(td->dqcoeff);
    3568             : #if CONFIG_PVQ
    3569             :       av1_zero(td->pvq_ref_coeff);
    3570             : #endif
    3571           0 :       av1_tile_init(&td->xd.tile, td->cm, tile_row, tile_col);
    3572           0 :       setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
    3573             :                          &td->bit_reader,
    3574             : #if CONFIG_ANS && ANS_MAX_SYMBOLS
    3575             :                          1 << cm->ans_window_size_log2,
    3576             : #endif  // CONFIG_ANS && ANS_MAX_SYMBOLS
    3577             :                          pbi->decrypt_cb, pbi->decrypt_state);
    3578             : #if CONFIG_ACCOUNTING
    3579             :       if (pbi->acct_enabled) {
    3580             :         td->bit_reader.accounting = &pbi->accounting;
    3581             :       } else {
    3582             :         td->bit_reader.accounting = NULL;
    3583             :       }
    3584             : #endif
    3585           0 :       av1_init_macroblockd(cm, &td->xd,
    3586             : #if CONFIG_PVQ
    3587             :                            td->pvq_ref_coeff,
    3588             : #endif
    3589             : #if CONFIG_CFL
    3590             :                            &td->cfl,
    3591             : #endif
    3592           0 :                            td->dqcoeff);
    3593             : 
    3594             : #if CONFIG_EC_ADAPT
    3595             :       // Initialise the tile context from the frame context
    3596           0 :       td->tctx = *cm->fc;
    3597           0 :       td->xd.tile_ctx = &td->tctx;
    3598             : #endif
    3599             : 
    3600             : #if CONFIG_PVQ
    3601             :       daala_dec_init(cm, &td->xd.daala_dec, &td->bit_reader);
    3602             :       td->xd.daala_dec.state.adapt = &td->tctx.pvq_context;
    3603             : #endif
    3604             : 
    3605             : #if CONFIG_PALETTE
    3606           0 :       td->xd.plane[0].color_index_map = td->color_index_map[0];
    3607           0 :       td->xd.plane[1].color_index_map = td->color_index_map[1];
    3608             : #endif  // CONFIG_PALETTE
    3609             :     }
    3610             :   }
    3611             : 
    3612           0 :   for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
    3613           0 :     const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
    3614           0 :     int mi_row = 0;
    3615             :     TileInfo tile_info;
    3616             : 
    3617           0 :     av1_tile_set_row(&tile_info, cm, row);
    3618             : 
    3619           0 :     for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
    3620           0 :       const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
    3621           0 :       TileData *const td = pbi->tile_data + tile_cols * row + col;
    3622             : #if CONFIG_ACCOUNTING
    3623             :       if (pbi->acct_enabled) {
    3624             :         td->bit_reader.accounting->last_tell_frac =
    3625             :             aom_reader_tell_frac(&td->bit_reader);
    3626             :       }
    3627             : #endif
    3628             : 
    3629           0 :       av1_tile_set_col(&tile_info, cm, col);
    3630             : 
    3631             : #if CONFIG_DEPENDENT_HORZTILES
    3632             : #if CONFIG_TILE_GROUPS
    3633             :       av1_tile_set_tg_boundary(&tile_info, cm, tile_row, tile_col);
    3634             :       if (!cm->dependent_horz_tiles || tile_row == 0 ||
    3635             :           tile_info.tg_horz_boundary) {
    3636             : #else
    3637             :       if (!cm->dependent_horz_tiles || tile_row == 0) {
    3638             : #endif
    3639             :         av1_zero_above_context(cm, tile_info.mi_col_start,
    3640             :                                tile_info.mi_col_end);
    3641             :       }
    3642             : #else
    3643           0 :       av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
    3644             : #endif
    3645             : 
    3646           0 :       for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
    3647           0 :            mi_row += cm->mib_size) {
    3648             :         int mi_col;
    3649             : 
    3650           0 :         av1_zero_left_context(&td->xd);
    3651             : 
    3652           0 :         for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
    3653           0 :              mi_col += cm->mib_size) {
    3654           0 :           av1_update_boundary_info(cm, &tile_info, mi_row, mi_col);
    3655           0 :           decode_partition(pbi, &td->xd,
    3656             : #if CONFIG_SUPERTX
    3657             :                            0,
    3658             : #endif  // CONFIG_SUPERTX
    3659           0 :                            mi_row, mi_col, &td->bit_reader, cm->sb_size,
    3660           0 :                            b_width_log2_lookup[cm->sb_size]);
    3661             : #if CONFIG_NCOBMC && CONFIG_MOTION_VAR
    3662             :           detoken_and_recon_sb(pbi, &td->xd, mi_row, mi_col, &td->bit_reader,
    3663             :                                cm->sb_size);
    3664             : #endif
    3665             :         }
    3666           0 :         aom_merge_corrupted_flag(&pbi->mb.corrupted, td->xd.corrupted);
    3667           0 :         if (pbi->mb.corrupted)
    3668           0 :           aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
    3669             :                              "Failed to decode tile data");
    3670             :       }
    3671             :     }
    3672             : 
    3673           0 :     assert(mi_row > 0);
    3674             : 
    3675             : // when Parallel deblocking is enabled, deblocking should not
    3676             : // be interleaved with decoding. Instead, deblocking should be done
    3677             : // after the entire frame is decoded.
    3678             : #if !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING && !CONFIG_CB4X4
    3679             :     // Loopfilter one tile row.
    3680             :     // Note: If out-of-order tile decoding is used(for example, inv_row_order
    3681             :     // = 1), the loopfiltering has be done after all tile rows are decoded.
    3682             :     if (!inv_row_order && cm->lf.filter_level && !cm->skip_loop_filter) {
    3683             :       LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
    3684             :       const int lf_start = AOMMAX(0, tile_info.mi_row_start - cm->mib_size);
    3685             :       const int lf_end = tile_info.mi_row_end - cm->mib_size;
    3686             : 
    3687             :       // Delay the loopfilter if the first tile row is only
    3688             :       // a single superblock high.
    3689             :       if (lf_end <= 0) continue;
    3690             : 
    3691             :       // Decoding has completed. Finish up the loop filter in this thread.
    3692             :       if (tile_info.mi_row_end >= cm->mi_rows) continue;
    3693             : 
    3694             :       winterface->sync(&pbi->lf_worker);
    3695             :       lf_data->start = lf_start;
    3696             :       lf_data->stop = lf_end;
    3697             :       if (pbi->max_threads > 1) {
    3698             :         winterface->launch(&pbi->lf_worker);
    3699             :       } else {
    3700             :         winterface->execute(&pbi->lf_worker);
    3701             :       }
    3702             :     }
    3703             : #endif  // !CONFIG_VAR_TX && !CONFIG_PARALLEL_DEBLOCKING
    3704             : 
    3705             :     // After loopfiltering, the last 7 row pixels in each superblock row may
    3706             :     // still be changed by the longest loopfilter of the next superblock row.
    3707           0 :     if (cm->frame_parallel_decode)
    3708           0 :       av1_frameworker_broadcast(pbi->cur_buf, mi_row << cm->mib_size_log2);
    3709             :   }
    3710             : 
    3711             : #if CONFIG_VAR_TX || CONFIG_CB4X4
    3712             :   // Loopfilter the whole frame.
    3713           0 :   av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
    3714             :                         cm->lf.filter_level, 0, 0);
    3715             : #else
    3716             : #if CONFIG_PARALLEL_DEBLOCKING
    3717             :   // Loopfilter all rows in the frame in the frame.
    3718             :   if (cm->lf.filter_level && !cm->skip_loop_filter) {
    3719             :     LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
    3720             :     winterface->sync(&pbi->lf_worker);
    3721             :     lf_data->start = 0;
    3722             :     lf_data->stop = cm->mi_rows;
    3723             :     winterface->execute(&pbi->lf_worker);
    3724             :   }
    3725             : #else
    3726             :   // Loopfilter remaining rows in the frame.
    3727             :   if (cm->lf.filter_level && !cm->skip_loop_filter) {
    3728             :     LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
    3729             :     winterface->sync(&pbi->lf_worker);
    3730             :     lf_data->start = lf_data->stop;
    3731             :     lf_data->stop = cm->mi_rows;
    3732             :     winterface->execute(&pbi->lf_worker);
    3733             :   }
    3734             : #endif  // CONFIG_PARALLEL_DEBLOCKING
    3735             : #endif  // CONFIG_VAR_TX
    3736           0 :   if (cm->frame_parallel_decode)
    3737           0 :     av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
    3738             : 
    3739             : #if CONFIG_EXT_TILE
    3740             :   if (n_tiles == 1) {
    3741             : #if CONFIG_ANS
    3742             :     return data_end;
    3743             : #else
    3744             :     // Find the end of the single tile buffer
    3745             :     return aom_reader_find_end(&pbi->tile_data->bit_reader);
    3746             : #endif  // CONFIG_ANS
    3747             :   } else {
    3748             :     // Return the end of the last tile buffer
    3749             :     return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
    3750             :   }
    3751             : #else
    3752             : #if CONFIG_ANS
    3753             :   return data_end;
    3754             : #else
    3755             :   {
    3756             :     // Get last tile data.
    3757           0 :     TileData *const td = pbi->tile_data + tile_cols * tile_rows - 1;
    3758           0 :     return aom_reader_find_end(&td->bit_reader);
    3759             :   }
    3760             : #endif  // CONFIG_ANS
    3761             : #endif  // CONFIG_EXT_TILE
    3762             : }
    3763             : 
    3764           0 : static int tile_worker_hook(TileWorkerData *const tile_data,
    3765             :                             const TileInfo *const tile) {
    3766           0 :   AV1Decoder *const pbi = tile_data->pbi;
    3767           0 :   const AV1_COMMON *const cm = &pbi->common;
    3768             :   int mi_row, mi_col;
    3769             : 
    3770           0 :   if (setjmp(tile_data->error_info.jmp)) {
    3771           0 :     tile_data->error_info.setjmp = 0;
    3772           0 :     aom_merge_corrupted_flag(&tile_data->xd.corrupted, 1);
    3773           0 :     return 0;
    3774             :   }
    3775             : 
    3776           0 :   tile_data->error_info.setjmp = 1;
    3777           0 :   tile_data->xd.error_info = &tile_data->error_info;
    3778             : #if CONFIG_DEPENDENT_HORZTILES
    3779             : #if CONFIG_TILE_GROUPS
    3780             :   if (!cm->dependent_horz_tiles || tile->tg_horz_boundary) {
    3781             : #else
    3782             :   if (!cm->dependent_horz_tiles) {
    3783             : #endif
    3784             :     av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
    3785             :   }
    3786             : #else
    3787           0 :   av1_zero_above_context(&pbi->common, tile->mi_col_start, tile->mi_col_end);
    3788             : #endif
    3789             : 
    3790           0 :   for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
    3791           0 :        mi_row += cm->mib_size) {
    3792           0 :     av1_zero_left_context(&tile_data->xd);
    3793             : 
    3794           0 :     for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
    3795           0 :          mi_col += cm->mib_size) {
    3796           0 :       decode_partition(pbi, &tile_data->xd,
    3797             : #if CONFIG_SUPERTX
    3798             :                        0,
    3799             : #endif
    3800           0 :                        mi_row, mi_col, &tile_data->bit_reader, cm->sb_size,
    3801           0 :                        b_width_log2_lookup[cm->sb_size]);
    3802             : #if CONFIG_NCOBMC && CONFIG_MOTION_VAR
    3803             :       detoken_and_recon_sb(pbi, &tile_data->xd, mi_row, mi_col,
    3804             :                            &tile_data->bit_reader, cm->sb_size);
    3805             : #endif
    3806             :     }
    3807             :   }
    3808           0 :   return !tile_data->xd.corrupted;
    3809             : }
    3810             : 
    3811             : // sorts in descending order
    3812           0 : static int compare_tile_buffers(const void *a, const void *b) {
    3813           0 :   const TileBufferDec *const buf1 = (const TileBufferDec *)a;
    3814           0 :   const TileBufferDec *const buf2 = (const TileBufferDec *)b;
    3815           0 :   return (int)(buf2->size - buf1->size);
    3816             : }
    3817             : 
    3818           0 : static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
    3819             :                                       const uint8_t *data_end) {
    3820           0 :   AV1_COMMON *const cm = &pbi->common;
    3821           0 :   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
    3822           0 :   const int tile_cols = cm->tile_cols;
    3823           0 :   const int tile_rows = cm->tile_rows;
    3824           0 :   const int num_workers = AOMMIN(pbi->max_threads & ~1, tile_cols);
    3825           0 :   TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
    3826             : #if CONFIG_EXT_TILE
    3827             :   const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
    3828             :   const int single_row = pbi->dec_tile_row >= 0;
    3829             :   const int tile_rows_start = single_row ? dec_tile_row : 0;
    3830             :   const int tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
    3831             :   const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
    3832             :   const int single_col = pbi->dec_tile_col >= 0;
    3833             :   const int tile_cols_start = single_col ? dec_tile_col : 0;
    3834             :   const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
    3835             : #else
    3836           0 :   const int tile_rows_start = 0;
    3837           0 :   const int tile_rows_end = tile_rows;
    3838           0 :   const int tile_cols_start = 0;
    3839           0 :   const int tile_cols_end = tile_cols;
    3840             : #endif  // CONFIG_EXT_TILE
    3841             :   int tile_row, tile_col;
    3842             :   int i;
    3843             : 
    3844             : #if !(CONFIG_ANS || CONFIG_EXT_TILE)
    3845           0 :   int final_worker = -1;
    3846             : #endif  // !(CONFIG_ANS || CONFIG_EXT_TILE)
    3847             : 
    3848           0 :   assert(tile_rows <= MAX_TILE_ROWS);
    3849           0 :   assert(tile_cols <= MAX_TILE_COLS);
    3850             : 
    3851           0 :   assert(tile_cols * tile_rows > 1);
    3852             : 
    3853             :   // TODO(jzern): See if we can remove the restriction of passing in max
    3854             :   // threads to the decoder.
    3855           0 :   if (pbi->num_tile_workers == 0) {
    3856           0 :     const int num_threads = pbi->max_threads & ~1;
    3857           0 :     CHECK_MEM_ERROR(cm, pbi->tile_workers,
    3858             :                     aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
    3859             :     // Ensure tile data offsets will be properly aligned. This may fail on
    3860             :     // platforms without DECLARE_ALIGNED().
    3861             :     assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
    3862           0 :     CHECK_MEM_ERROR(
    3863             :         cm, pbi->tile_worker_data,
    3864             :         aom_memalign(32, num_threads * sizeof(*pbi->tile_worker_data)));
    3865           0 :     CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
    3866             :                     aom_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
    3867           0 :     for (i = 0; i < num_threads; ++i) {
    3868           0 :       AVxWorker *const worker = &pbi->tile_workers[i];
    3869           0 :       ++pbi->num_tile_workers;
    3870             : 
    3871           0 :       winterface->init(worker);
    3872           0 :       if (i < num_threads - 1 && !winterface->reset(worker)) {
    3873           0 :         aom_internal_error(&cm->error, AOM_CODEC_ERROR,
    3874             :                            "Tile decoder thread creation failed");
    3875             :       }
    3876             :     }
    3877             :   }
    3878             : 
    3879             :   // Reset tile decoding hook
    3880           0 :   for (i = 0; i < num_workers; ++i) {
    3881           0 :     AVxWorker *const worker = &pbi->tile_workers[i];
    3882           0 :     winterface->sync(worker);
    3883           0 :     worker->hook = (AVxWorkerHook)tile_worker_hook;
    3884           0 :     worker->data1 = &pbi->tile_worker_data[i];
    3885           0 :     worker->data2 = &pbi->tile_worker_info[i];
    3886             :   }
    3887             : 
    3888             :   // Initialize thread frame counts.
    3889           0 :   if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
    3890           0 :     for (i = 0; i < num_workers; ++i) {
    3891           0 :       TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
    3892           0 :       av1_zero(twd->counts);
    3893             :     }
    3894             :   }
    3895             : 
    3896             :   // Load tile data into tile_buffers
    3897           0 :   get_tile_buffers(pbi, data, data_end, tile_buffers);
    3898             : 
    3899           0 :   for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
    3900             :     // Sort the buffers in this tile row based on size in descending order.
    3901           0 :     qsort(&tile_buffers[tile_row][tile_cols_start],
    3902           0 :           tile_cols_end - tile_cols_start, sizeof(tile_buffers[0][0]),
    3903             :           compare_tile_buffers);
    3904             : 
    3905             :     // Rearrange the tile buffers in this tile row such that per-tile group
    3906             :     // the largest, and presumably the most difficult tile will be decoded in
    3907             :     // the main thread. This should help minimize the number of instances
    3908             :     // where the main thread is waiting for a worker to complete.
    3909             :     {
    3910             :       int group_start;
    3911           0 :       for (group_start = tile_cols_start; group_start < tile_cols_end;
    3912           0 :            group_start += num_workers) {
    3913           0 :         const int group_end = AOMMIN(group_start + num_workers, tile_cols);
    3914           0 :         const TileBufferDec largest = tile_buffers[tile_row][group_start];
    3915           0 :         memmove(&tile_buffers[tile_row][group_start],
    3916           0 :                 &tile_buffers[tile_row][group_start + 1],
    3917           0 :                 (group_end - group_start - 1) * sizeof(tile_buffers[0][0]));
    3918           0 :         tile_buffers[tile_row][group_end - 1] = largest;
    3919             :       }
    3920             :     }
    3921             : 
    3922           0 :     for (tile_col = tile_cols_start; tile_col < tile_cols_end;) {
    3923             :       // Launch workers for individual columns
    3924           0 :       for (i = 0; i < num_workers && tile_col < tile_cols_end;
    3925           0 :            ++i, ++tile_col) {
    3926           0 :         TileBufferDec *const buf = &tile_buffers[tile_row][tile_col];
    3927           0 :         AVxWorker *const worker = &pbi->tile_workers[i];
    3928           0 :         TileWorkerData *const twd = (TileWorkerData *)worker->data1;
    3929           0 :         TileInfo *const tile_info = (TileInfo *)worker->data2;
    3930             : 
    3931           0 :         twd->pbi = pbi;
    3932           0 :         twd->xd = pbi->mb;
    3933           0 :         twd->xd.corrupted = 0;
    3934           0 :         twd->xd.counts =
    3935           0 :             cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD
    3936             :                 ? &twd->counts
    3937           0 :                 : NULL;
    3938           0 :         av1_zero(twd->dqcoeff);
    3939           0 :         av1_tile_init(tile_info, cm, tile_row, buf->col);
    3940           0 :         av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
    3941           0 :         setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
    3942             :                            &twd->bit_reader,
    3943             : #if CONFIG_ANS && ANS_MAX_SYMBOLS
    3944             :                            1 << cm->ans_window_size_log2,
    3945             : #endif  // CONFIG_ANS && ANS_MAX_SYMBOLS
    3946             :                            pbi->decrypt_cb, pbi->decrypt_state);
    3947           0 :         av1_init_macroblockd(cm, &twd->xd,
    3948             : #if CONFIG_PVQ
    3949             :                              twd->pvq_ref_coeff,
    3950             : #endif
    3951             : #if CONFIG_CFL
    3952             :                              &twd->cfl,
    3953             : #endif
    3954           0 :                              twd->dqcoeff);
    3955             : #if CONFIG_PVQ
    3956             :         daala_dec_init(cm, &twd->xd.daala_dec, &twd->bit_reader);
    3957             :         twd->xd.daala_dec.state.adapt = &twd->tctx.pvq_context;
    3958             : #endif
    3959             : #if CONFIG_EC_ADAPT
    3960             :         // Initialise the tile context from the frame context
    3961           0 :         twd->tctx = *cm->fc;
    3962           0 :         twd->xd.tile_ctx = &twd->tctx;
    3963             : #endif
    3964             : #if CONFIG_PALETTE
    3965           0 :         twd->xd.plane[0].color_index_map = twd->color_index_map[0];
    3966           0 :         twd->xd.plane[1].color_index_map = twd->color_index_map[1];
    3967             : #endif  // CONFIG_PALETTE
    3968             : 
    3969           0 :         worker->had_error = 0;
    3970           0 :         if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
    3971           0 :           winterface->execute(worker);
    3972             :         } else {
    3973           0 :           winterface->launch(worker);
    3974             :         }
    3975             : 
    3976             : #if !(CONFIG_ANS || CONFIG_EXT_TILE)
    3977           0 :         if (tile_row == tile_rows - 1 && buf->col == tile_cols - 1) {
    3978           0 :           final_worker = i;
    3979             :         }
    3980             : #endif  // !(CONFIG_ANS || CONFIG_EXT_TILE)
    3981             :       }
    3982             : 
    3983             :       // Sync all workers
    3984           0 :       for (; i > 0; --i) {
    3985           0 :         AVxWorker *const worker = &pbi->tile_workers[i - 1];
    3986             :         // TODO(jzern): The tile may have specific error data associated with
    3987             :         // its aom_internal_error_info which could be propagated to the main
    3988             :         // info in cm. Additionally once the threads have been synced and an
    3989             :         // error is detected, there's no point in continuing to decode tiles.
    3990           0 :         pbi->mb.corrupted |= !winterface->sync(worker);
    3991             :       }
    3992             :     }
    3993             :   }
    3994             : 
    3995             :   // Accumulate thread frame counts.
    3996           0 :   if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
    3997           0 :     for (i = 0; i < num_workers; ++i) {
    3998           0 :       TileWorkerData *const twd = (TileWorkerData *)pbi->tile_workers[i].data1;
    3999           0 :       av1_accumulate_frame_counts(&cm->counts, &twd->counts);
    4000             :     }
    4001             :   }
    4002             : 
    4003             : #if CONFIG_EXT_TILE
    4004             :   // Return the end of the last tile buffer
    4005             :   return tile_buffers[tile_rows - 1][tile_cols - 1].raw_data_end;
    4006             : #else
    4007             : #if CONFIG_ANS
    4008             :   return data_end;
    4009             : #else
    4010           0 :   assert(final_worker != -1);
    4011             :   {
    4012           0 :     TileWorkerData *const twd =
    4013           0 :         (TileWorkerData *)pbi->tile_workers[final_worker].data1;
    4014           0 :     return aom_reader_find_end(&twd->bit_reader);
    4015             :   }
    4016             : #endif  // CONFIG_ANS
    4017             : #endif  // CONFIG_EXT_TILE
    4018             : }
    4019             : 
    4020           0 : static void error_handler(void *data) {
    4021           0 :   AV1_COMMON *const cm = (AV1_COMMON *)data;
    4022           0 :   aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
    4023           0 : }
    4024             : 
    4025           0 : static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
    4026             :                                               struct aom_read_bit_buffer *rb) {
    4027           0 :   if (cm->profile >= PROFILE_2) {
    4028           0 :     cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
    4029             :   } else {
    4030           0 :     cm->bit_depth = AOM_BITS_8;
    4031             :   }
    4032             : 
    4033             : #if CONFIG_HIGHBITDEPTH
    4034           0 :   cm->use_highbitdepth = cm->bit_depth > AOM_BITS_8 || !CONFIG_LOWBITDEPTH;
    4035             : #endif
    4036             : 
    4037           0 :   cm->color_space = aom_rb_read_literal(rb, 3);
    4038           0 :   if (cm->color_space != AOM_CS_SRGB) {
    4039             :     // [16,235] (including xvycc) vs [0,255] range
    4040           0 :     cm->color_range = aom_rb_read_bit(rb);
    4041           0 :     if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
    4042           0 :       cm->subsampling_x = aom_rb_read_bit(rb);
    4043           0 :       cm->subsampling_y = aom_rb_read_bit(rb);
    4044           0 :       if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
    4045           0 :         aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
    4046             :                            "4:2:0 color not supported in profile 1 or 3");
    4047           0 :       if (aom_rb_read_bit(rb))
    4048           0 :         aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
    4049             :                            "Reserved bit set");
    4050             :     } else {
    4051           0 :       cm->subsampling_y = cm->subsampling_x = 1;
    4052             :     }
    4053             :   } else {
    4054           0 :     if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
    4055             :       // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
    4056             :       // 4:2:2 or 4:4:0 chroma sampling is not allowed.
    4057           0 :       cm->subsampling_y = cm->subsampling_x = 0;
    4058           0 :       if (aom_rb_read_bit(rb))
    4059           0 :         aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
    4060             :                            "Reserved bit set");
    4061             :     } else {
    4062           0 :       aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
    4063             :                          "4:4:4 color not supported in profile 0 or 2");
    4064             :     }
    4065             :   }
    4066           0 : }
    4067             : 
    4068             : #if CONFIG_REFERENCE_BUFFER
    4069           0 : void read_sequence_header(SequenceHeader *seq_params) {
    4070             :   /* Placeholder for actually reading from the bitstream */
    4071           0 :   seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG;
    4072           0 :   seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7;
    4073           0 :   seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2;
    4074           0 : }
    4075             : #endif
    4076             : 
    4077             : #if CONFIG_EXT_INTER
    4078           0 : static void read_compound_tools(AV1_COMMON *cm,
    4079             :                                 struct aom_read_bit_buffer *rb) {
    4080             :   (void)cm;
    4081             :   (void)rb;
    4082             : #if CONFIG_INTERINTRA
    4083           0 :   if (!frame_is_intra_only(cm) && cm->reference_mode != COMPOUND_REFERENCE) {
    4084           0 :     cm->allow_interintra_compound = aom_rb_read_bit(rb);
    4085             :   } else {
    4086           0 :     cm->allow_interintra_compound = 0;
    4087             :   }
    4088             : #endif  // CONFIG_INTERINTRA
    4089             : #if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT
    4090           0 :   if (!frame_is_intra_only(cm) && cm->reference_mode != SINGLE_REFERENCE) {
    4091           0 :     cm->allow_masked_compound = aom_rb_read_bit(rb);
    4092             :   } else {
    4093           0 :     cm->allow_masked_compound = 0;
    4094             :   }
    4095             : #endif  // CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT
    4096           0 : }
    4097             : #endif  // CONFIG_EXT_INTER
    4098             : 
    4099           0 : static size_t read_uncompressed_header(AV1Decoder *pbi,
    4100             :                                        struct aom_read_bit_buffer *rb) {
    4101           0 :   AV1_COMMON *const cm = &pbi->common;
    4102           0 :   MACROBLOCKD *const xd = &pbi->mb;
    4103           0 :   BufferPool *const pool = cm->buffer_pool;
    4104           0 :   RefCntBuffer *const frame_bufs = pool->frame_bufs;
    4105           0 :   int i, mask, ref_index = 0;
    4106             :   size_t sz;
    4107             : 
    4108             : #if CONFIG_REFERENCE_BUFFER
    4109             :   /* TODO: Move outside frame loop or inside key-frame branch */
    4110           0 :   read_sequence_header(&pbi->seq_params);
    4111             : #endif
    4112             : 
    4113           0 :   cm->last_frame_type = cm->frame_type;
    4114           0 :   cm->last_intra_only = cm->intra_only;
    4115             : 
    4116             : #if CONFIG_EXT_REFS
    4117             :   // NOTE: By default all coded frames to be used as a reference
    4118           0 :   cm->is_reference_frame = 1;
    4119             : #endif  // CONFIG_EXT_REFS
    4120             : 
    4121           0 :   if (aom_rb_read_literal(rb, 2) != AOM_FRAME_MARKER)
    4122           0 :     aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
    4123             :                        "Invalid frame marker");
    4124             : 
    4125           0 :   cm->profile = av1_read_profile(rb);
    4126             : 
    4127           0 :   const BITSTREAM_PROFILE MAX_SUPPORTED_PROFILE =
    4128             :       CONFIG_HIGHBITDEPTH ? MAX_PROFILES : PROFILE_2;
    4129             : 
    4130           0 :   if (cm->profile >= MAX_SUPPORTED_PROFILE)
    4131           0 :     aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
    4132             :                        "Unsupported bitstream profile");
    4133             : 
    4134           0 :   cm->show_existing_frame = aom_rb_read_bit(rb);
    4135             : 
    4136           0 :   if (cm->show_existing_frame) {
    4137             :     // Show an existing frame directly.
    4138           0 :     const int existing_frame_idx = aom_rb_read_literal(rb, 3);
    4139           0 :     const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
    4140             : #if CONFIG_REFERENCE_BUFFER
    4141           0 :     if (pbi->seq_params.frame_id_numbers_present_flag) {
    4142           0 :       int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
    4143           0 :       int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
    4144             :       /* Compare display_frame_id with ref_frame_id and check valid for
    4145             :       * referencing */
    4146           0 :       if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
    4147           0 :           cm->valid_for_referencing[existing_frame_idx] == 0)
    4148           0 :         aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
    4149             :                            "Reference buffer frame ID mismatch");
    4150             :     }
    4151             : #endif
    4152           0 :     lock_buffer_pool(pool);
    4153           0 :     if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
    4154           0 :       unlock_buffer_pool(pool);
    4155           0 :       aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
    4156             :                          "Buffer %d does not contain a decoded frame",
    4157             :                          frame_to_show);
    4158             :     }
    4159           0 :     ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
    4160           0 :     unlock_buffer_pool(pool);
    4161             : 
    4162           0 :     cm->lf.filter_level = 0;
    4163           0 :     cm->show_frame = 1;
    4164           0 :     pbi->refresh_frame_flags = 0;
    4165             : 
    4166           0 :     if (cm->frame_parallel_decode) {
    4167           0 :       for (i = 0; i < REF_FRAMES; ++i)
    4168           0 :         cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
    4169             :     }
    4170             : 
    4171           0 :     return 0;
    4172             :   }
    4173             : 
    4174           0 :   cm->frame_type = (FRAME_TYPE)aom_rb_read_bit(rb);
    4175           0 :   cm->show_frame = aom_rb_read_bit(rb);
    4176           0 :   cm->error_resilient_mode = aom_rb_read_bit(rb);
    4177             : #if CONFIG_REFERENCE_BUFFER
    4178           0 :   if (pbi->seq_params.frame_id_numbers_present_flag) {
    4179           0 :     int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
    4180           0 :     int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
    4181           0 :     int prev_frame_id = 0;
    4182           0 :     if (cm->frame_type != KEY_FRAME) {
    4183           0 :       prev_frame_id = cm->current_frame_id;
    4184             :     }
    4185           0 :     cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
    4186             : 
    4187           0 :     if (cm->frame_type != KEY_FRAME) {
    4188             :       int diff_frame_id;
    4189           0 :       if (cm->current_frame_id > prev_frame_id) {
    4190           0 :         diff_frame_id = cm->current_frame_id - prev_frame_id;
    4191             :       } else {
    4192           0 :         diff_frame_id =
    4193           0 :             (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
    4194             :       }
    4195             :       /* Check current_frame_id for conformance */
    4196           0 :       if (prev_frame_id == cm->current_frame_id ||
    4197           0 :           diff_frame_id >= (1 << (frame_id_length - 1))) {
    4198           0 :         aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
    4199             :                            "Invalid value of current_frame_id");
    4200             :       }
    4201             :     }
    4202             :     /* Check if some frames need to be marked as not valid for referencing */
    4203           0 :     for (i = 0; i < REF_FRAMES; i++) {
    4204           0 :       if (cm->frame_type == KEY_FRAME) {
    4205           0 :         cm->valid_for_referencing[i] = 0;
    4206           0 :       } else if (cm->current_frame_id - (1 << diff_len) > 0) {
    4207           0 :         if (cm->ref_frame_id[i] > cm->current_frame_id ||
    4208           0 :             cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
    4209           0 :           cm->valid_for_referencing[i] = 0;
    4210             :       } else {
    4211           0 :         if (cm->ref_frame_id[i] > cm->current_frame_id &&
    4212           0 :             cm->ref_frame_id[i] <
    4213           0 :                 (1 << frame_id_length) + cm->current_frame_id - (1 << diff_len))
    4214           0 :           cm->valid_for_referencing[i] = 0;
    4215             :       }
    4216             :     }
    4217             :   }
    4218             : #endif
    4219           0 :   if (cm->frame_type == KEY_FRAME) {
    4220           0 :     if (!av1_read_sync_code(rb))
    4221           0 :       aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
    4222             :                          "Invalid frame sync code");
    4223             : 
    4224           0 :     read_bitdepth_colorspace_sampling(cm, rb);
    4225           0 :     pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
    4226             : 
    4227           0 :     for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
    4228           0 :       cm->frame_refs[i].idx = INVALID_IDX;
    4229           0 :       cm->frame_refs[i].buf = NULL;
    4230             :     }
    4231             : 
    4232           0 :     setup_frame_size(cm, rb);
    4233           0 :     if (pbi->need_resync) {
    4234           0 :       memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
    4235           0 :       pbi->need_resync = 0;
    4236             :     }
    4237             : #if CONFIG_ANS && ANS_MAX_SYMBOLS
    4238             :     cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
    4239             : #endif  // CONFIG_ANS && ANS_MAX_SYMBOLS
    4240             : #if CONFIG_PALETTE || CONFIG_INTRABC
    4241           0 :     cm->allow_screen_content_tools = aom_rb_read_bit(rb);
    4242             : #endif  // CONFIG_PALETTE || CONFIG_INTRABC
    4243             : #if CONFIG_TEMPMV_SIGNALING
    4244           0 :     cm->use_prev_frame_mvs = 0;
    4245             : #endif
    4246             :   } else {
    4247           0 :     cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
    4248             : #if CONFIG_PALETTE || CONFIG_INTRABC
    4249           0 :     if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
    4250             : #endif  // CONFIG_PALETTE || CONFIG_INTRABC
    4251             : #if CONFIG_TEMPMV_SIGNALING
    4252           0 :     if (cm->intra_only || cm->error_resilient_mode) cm->use_prev_frame_mvs = 0;
    4253             : #endif
    4254           0 :     if (cm->error_resilient_mode) {
    4255           0 :       cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
    4256             :     } else {
    4257           0 :       if (cm->intra_only) {
    4258           0 :         cm->reset_frame_context = aom_rb_read_bit(rb)
    4259             :                                       ? RESET_FRAME_CONTEXT_ALL
    4260           0 :                                       : RESET_FRAME_CONTEXT_CURRENT;
    4261             :       } else {
    4262           0 :         cm->reset_frame_context = aom_rb_read_bit(rb)
    4263             :                                       ? RESET_FRAME_CONTEXT_CURRENT
    4264           0 :                                       : RESET_FRAME_CONTEXT_NONE;
    4265           0 :         if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
    4266           0 :           cm->reset_frame_context = aom_rb_read_bit(rb)
    4267             :                                         ? RESET_FRAME_CONTEXT_ALL
    4268           0 :                                         : RESET_FRAME_CONTEXT_CURRENT;
    4269             :       }
    4270             :     }
    4271             : 
    4272           0 :     if (cm->intra_only) {
    4273           0 :       if (!av1_read_sync_code(rb))
    4274           0 :         aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
    4275             :                            "Invalid frame sync code");
    4276             : 
    4277           0 :       read_bitdepth_colorspace_sampling(cm, rb);
    4278             : 
    4279           0 :       pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
    4280           0 :       setup_frame_size(cm, rb);
    4281           0 :       if (pbi->need_resync) {
    4282           0 :         memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
    4283           0 :         pbi->need_resync = 0;
    4284             :       }
    4285             : #if CONFIG_ANS && ANS_MAX_SYMBOLS
    4286             :       cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
    4287             : #endif
    4288           0 :     } else if (pbi->need_resync != 1) { /* Skip if need resync */
    4289           0 :       pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
    4290             : 
    4291             : #if CONFIG_EXT_REFS
    4292           0 :       if (!pbi->refresh_frame_flags) {
    4293             :         // NOTE: "pbi->refresh_frame_flags == 0" indicates that the coded frame
    4294             :         //       will not be used as a reference
    4295           0 :         cm->is_reference_frame = 0;
    4296             :       }
    4297             : #endif  // CONFIG_EXT_REFS
    4298             : 
    4299           0 :       for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
    4300           0 :         const int ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
    4301           0 :         const int idx = cm->ref_frame_map[ref];
    4302           0 :         RefBuffer *const ref_frame = &cm->frame_refs[i];
    4303           0 :         ref_frame->idx = idx;
    4304           0 :         ref_frame->buf = &frame_bufs[idx].buf;
    4305           0 :         cm->ref_frame_sign_bias[LAST_FRAME + i] = aom_rb_read_bit(rb);
    4306             : #if CONFIG_REFERENCE_BUFFER
    4307           0 :         if (pbi->seq_params.frame_id_numbers_present_flag) {
    4308           0 :           int frame_id_length = pbi->seq_params.frame_id_length_minus7 + 7;
    4309           0 :           int diff_len = pbi->seq_params.delta_frame_id_length_minus2 + 2;
    4310           0 :           int delta_frame_id_minus1 = aom_rb_read_literal(rb, diff_len);
    4311           0 :           int ref_frame_id =
    4312           0 :               ((cm->current_frame_id - (delta_frame_id_minus1 + 1) +
    4313           0 :                 (1 << frame_id_length)) %
    4314           0 :                (1 << frame_id_length));
    4315             :           /* Compare values derived from delta_frame_id_minus1 and
    4316             :           * refresh_frame_flags. Also, check valid for referencing */
    4317           0 :           if (ref_frame_id != cm->ref_frame_id[ref] ||
    4318           0 :               cm->valid_for_referencing[ref] == 0)
    4319           0 :             aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
    4320             :                                "Reference buffer frame ID mismatch");
    4321             :         }
    4322             : #endif
    4323             :       }
    4324             : 
    4325             : #if CONFIG_FRAME_SIZE
    4326             :       if (cm->error_resilient_mode == 0) {
    4327             :         setup_frame_size_with_refs(cm, rb);
    4328             :       } else {
    4329             :         setup_frame_size(cm, rb);
    4330             :       }
    4331             : #else
    4332           0 :       setup_frame_size_with_refs(cm, rb);
    4333             : #endif
    4334             : 
    4335           0 :       cm->allow_high_precision_mv = aom_rb_read_bit(rb);
    4336           0 :       cm->interp_filter = read_frame_interp_filter(rb);
    4337             : #if CONFIG_TEMPMV_SIGNALING
    4338           0 :       if (!cm->error_resilient_mode) {
    4339           0 :         cm->use_prev_frame_mvs = aom_rb_read_bit(rb);
    4340             :       }
    4341             : #endif
    4342           0 :       for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
    4343           0 :         RefBuffer *const ref_buf = &cm->frame_refs[i];
    4344             : #if CONFIG_HIGHBITDEPTH
    4345           0 :         av1_setup_scale_factors_for_frame(
    4346           0 :             &ref_buf->sf, ref_buf->buf->y_crop_width,
    4347           0 :             ref_buf->buf->y_crop_height, cm->width, cm->height,
    4348             :             cm->use_highbitdepth);
    4349             : #else
    4350             :         av1_setup_scale_factors_for_frame(
    4351             :             &ref_buf->sf, ref_buf->buf->y_crop_width,
    4352             :             ref_buf->buf->y_crop_height, cm->width, cm->height);
    4353             : #endif
    4354             :       }
    4355             :     }
    4356             :   }
    4357             : #if CONFIG_TEMPMV_SIGNALING
    4358           0 :   cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
    4359             : #endif
    4360             : 
    4361             : #if CONFIG_REFERENCE_BUFFER
    4362           0 :   if (pbi->seq_params.frame_id_numbers_present_flag) {
    4363             :     /* If bitmask is set, update reference frame id values and
    4364             :     mark frames as valid for reference */
    4365           0 :     int refresh_frame_flags =
    4366           0 :         cm->frame_type == KEY_FRAME ? 0xFF : pbi->refresh_frame_flags;
    4367           0 :     for (i = 0; i < REF_FRAMES; i++) {
    4368           0 :       if ((refresh_frame_flags >> i) & 1) {
    4369           0 :         cm->ref_frame_id[i] = cm->current_frame_id;
    4370           0 :         cm->valid_for_referencing[i] = 1;
    4371             :       }
    4372             :     }
    4373             :   }
    4374             : #endif
    4375             : 
    4376           0 :   get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
    4377           0 :   get_frame_new_buffer(cm)->color_space = cm->color_space;
    4378           0 :   get_frame_new_buffer(cm)->color_range = cm->color_range;
    4379           0 :   get_frame_new_buffer(cm)->render_width = cm->render_width;
    4380           0 :   get_frame_new_buffer(cm)->render_height = cm->render_height;
    4381             : 
    4382           0 :   if (pbi->need_resync) {
    4383           0 :     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
    4384             :                        "Keyframe / intra-only frame required to reset decoder"
    4385             :                        " state");
    4386             :   }
    4387             : 
    4388           0 :   if (!cm->error_resilient_mode) {
    4389           0 :     cm->refresh_frame_context = aom_rb_read_bit(rb)
    4390             :                                     ? REFRESH_FRAME_CONTEXT_FORWARD
    4391           0 :                                     : REFRESH_FRAME_CONTEXT_BACKWARD;
    4392             :   } else {
    4393           0 :     cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
    4394             :   }
    4395             : 
    4396             :   // This flag will be overridden by the call to av1_setup_past_independence
    4397             :   // below, forcing the use of context 0 for those frame types.
    4398           0 :   cm->frame_context_idx = aom_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
    4399             : 
    4400             :   // Generate next_ref_frame_map.
    4401           0 :   lock_buffer_pool(pool);
    4402           0 :   for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
    4403           0 :     if (mask & 1) {
    4404           0 :       cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
    4405           0 :       ++frame_bufs[cm->new_fb_idx].ref_count;
    4406             :     } else {
    4407           0 :       cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
    4408             :     }
    4409             :     // Current thread holds the reference frame.
    4410           0 :     if (cm->ref_frame_map[ref_index] >= 0)
    4411           0 :       ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
    4412           0 :     ++ref_index;
    4413             :   }
    4414             : 
    4415           0 :   for (; ref_index < REF_FRAMES; ++ref_index) {
    4416           0 :     cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
    4417             : 
    4418             :     // Current thread holds the reference frame.
    4419           0 :     if (cm->ref_frame_map[ref_index] >= 0)
    4420           0 :       ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
    4421             :   }
    4422           0 :   unlock_buffer_pool(pool);
    4423           0 :   pbi->hold_ref_buf = 1;
    4424             : 
    4425           0 :   if (frame_is_intra_only(cm) || cm->error_resilient_mode)
    4426           0 :     av1_setup_past_independence(cm);
    4427             : 
    4428             : #if CONFIG_EXT_PARTITION
    4429             :   set_sb_size(cm, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
    4430             : #else
    4431           0 :   set_sb_size(cm, BLOCK_64X64);
    4432             : #endif  // CONFIG_EXT_PARTITION
    4433             : 
    4434           0 :   setup_loopfilter(cm, rb);
    4435             : #if CONFIG_CDEF
    4436           0 :   setup_cdef(cm, rb);
    4437             : #endif
    4438             : #if CONFIG_LOOP_RESTORATION
    4439             :   decode_restoration_mode(cm, rb);
    4440             : #endif  // CONFIG_LOOP_RESTORATION
    4441           0 :   setup_quantization(cm, rb);
    4442           0 :   xd->bd = (int)cm->bit_depth;
    4443             : 
    4444             : #if CONFIG_Q_ADAPT_PROBS
    4445             :   av1_default_coef_probs(cm);
    4446             :   if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
    4447             :       cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
    4448             :     for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
    4449             :   } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
    4450             :     cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
    4451             :   }
    4452             : #endif  // CONFIG_Q_ADAPT_PROBS
    4453             : 
    4454           0 :   setup_segmentation(cm, rb);
    4455             : 
    4456             : #if CONFIG_DELTA_Q
    4457             :   {
    4458           0 :     struct segmentation *const seg = &cm->seg;
    4459           0 :     int segment_quantizer_active = 0;
    4460           0 :     for (i = 0; i < MAX_SEGMENTS; i++) {
    4461           0 :       if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
    4462           0 :         segment_quantizer_active = 1;
    4463             :       }
    4464             :     }
    4465             : 
    4466           0 :     cm->delta_q_res = 1;
    4467             : #if CONFIG_EXT_DELTA_Q
    4468           0 :     cm->delta_lf_res = 1;
    4469             : #endif
    4470           0 :     if (segment_quantizer_active == 0 && cm->base_qindex > 0) {
    4471           0 :       cm->delta_q_present_flag = aom_rb_read_bit(rb);
    4472             :     } else {
    4473           0 :       cm->delta_q_present_flag = 0;
    4474             :     }
    4475           0 :     if (cm->delta_q_present_flag) {
    4476           0 :       xd->prev_qindex = cm->base_qindex;
    4477           0 :       cm->delta_q_res = 1 << aom_rb_read_literal(rb, 2);
    4478             : #if CONFIG_EXT_DELTA_Q
    4479           0 :       if (segment_quantizer_active) {
    4480           0 :         assert(seg->abs_delta == SEGMENT_DELTADATA);
    4481             :       }
    4482           0 :       cm->delta_lf_present_flag = aom_rb_read_bit(rb);
    4483           0 :       if (cm->delta_lf_present_flag) {
    4484           0 :         xd->prev_delta_lf_from_base = 0;
    4485           0 :         cm->delta_lf_res = 1 << aom_rb_read_literal(rb, 2);
    4486             :       } else {
    4487           0 :         cm->delta_lf_present_flag = 0;
    4488             :       }
    4489             : #endif  // CONFIG_EXT_DELTA_Q
    4490             :     }
    4491             :   }
    4492             : #endif
    4493             : 
    4494           0 :   for (i = 0; i < MAX_SEGMENTS; ++i) {
    4495           0 :     const int qindex = cm->seg.enabled
    4496           0 :                            ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
    4497           0 :                            : cm->base_qindex;
    4498           0 :     xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
    4499           0 :                       cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
    4500           0 :     xd->qindex[i] = qindex;
    4501             :   }
    4502             : 
    4503           0 :   setup_segmentation_dequant(cm);
    4504           0 :   cm->tx_mode = read_tx_mode(cm, xd, rb);
    4505           0 :   cm->reference_mode = read_frame_reference_mode(cm, rb);
    4506             : #if CONFIG_EXT_INTER
    4507           0 :   read_compound_tools(cm, rb);
    4508             : #endif  // CONFIG_EXT_INTER
    4509             : 
    4510             : #if CONFIG_EXT_TX
    4511           0 :   cm->reduced_tx_set_used = aom_rb_read_bit(rb);
    4512             : #endif  // CONFIG_EXT_TX
    4513             : 
    4514           0 :   read_tile_info(pbi, rb);
    4515           0 :   sz = aom_rb_read_literal(rb, 16);
    4516             : 
    4517           0 :   if (sz == 0)
    4518           0 :     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
    4519             :                        "Invalid header size");
    4520           0 :   return sz;
    4521             : }
    4522             : 
    4523             : #if CONFIG_EXT_TX
    4524             : #if !CONFIG_EC_ADAPT
    4525             : static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
    4526             :   int i, j, k;
    4527             :   int s;
    4528             :   for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
    4529             :     if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
    4530             :       for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
    4531             :         if (!use_inter_ext_tx_for_txsize[s][i]) continue;
    4532             :         for (j = 0; j < num_ext_tx_set[ext_tx_set_type_inter[s]] - 1; ++j)
    4533             :           av1_diff_update_prob(r, &fc->inter_ext_tx_prob[s][i][j], ACCT_STR);
    4534             :       }
    4535             :     }
    4536             :   }
    4537             : 
    4538             :   for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
    4539             :     if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
    4540             :       for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
    4541             :         if (!use_intra_ext_tx_for_txsize[s][i]) continue;
    4542             :         for (j = 0; j < INTRA_MODES; ++j)
    4543             :           for (k = 0; k < num_ext_tx_set[ext_tx_set_type_intra[s]] - 1; ++k)
    4544             :             av1_diff_update_prob(r, &fc->intra_ext_tx_prob[s][i][j][k],
    4545             :                                  ACCT_STR);
    4546             :       }
    4547             :     }
    4548             :   }
    4549             : }
    4550             : #endif  // !CONFIG_EC_ADAPT
    4551             : #else
    4552             : 
    4553             : #endif  // CONFIG_EXT_TX
    4554             : #if CONFIG_SUPERTX
    4555             : static void read_supertx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
    4556             :   int i, j;
    4557             :   if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
    4558             :     for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) {
    4559             :       for (j = TX_8X8; j < TX_SIZES; ++j) {
    4560             :         av1_diff_update_prob(r, &fc->supertx_prob[i][j], ACCT_STR);
    4561             :       }
    4562             :     }
    4563             :   }
    4564             : }
    4565             : #endif  // CONFIG_SUPERTX
    4566             : 
    4567             : #if CONFIG_GLOBAL_MOTION
    4568           0 : static void read_global_motion_params(WarpedMotionParams *params,
    4569             :                                       WarpedMotionParams *ref_params,
    4570             :                                       aom_prob *probs, aom_reader *r,
    4571             :                                       int allow_hp) {
    4572           0 :   TransformationType type =
    4573           0 :       aom_read_tree(r, av1_global_motion_types_tree, probs, ACCT_STR);
    4574             :   int trans_bits;
    4575             :   int trans_dec_factor;
    4576             :   int trans_prec_diff;
    4577           0 :   set_default_warp_params(params);
    4578           0 :   params->wmtype = type;
    4579           0 :   switch (type) {
    4580             :     case HOMOGRAPHY:
    4581             :     case HORTRAPEZOID:
    4582             :     case VERTRAPEZOID:
    4583           0 :       if (type != HORTRAPEZOID)
    4584           0 :         params->wmmat[6] =
    4585           0 :             aom_read_signed_primitive_refsubexpfin(
    4586             :                 r, GM_ROW3HOMO_MAX + 1, SUBEXPFIN_K,
    4587           0 :                 (ref_params->wmmat[6] >> GM_ROW3HOMO_PREC_DIFF), ACCT_STR) *
    4588             :             GM_ROW3HOMO_DECODE_FACTOR;
    4589           0 :       if (type != VERTRAPEZOID)
    4590           0 :         params->wmmat[7] =
    4591           0 :             aom_read_signed_primitive_refsubexpfin(
    4592             :                 r, GM_ROW3HOMO_MAX + 1, SUBEXPFIN_K,
    4593           0 :                 (ref_params->wmmat[7] >> GM_ROW3HOMO_PREC_DIFF), ACCT_STR) *
    4594             :             GM_ROW3HOMO_DECODE_FACTOR;
    4595             :     case AFFINE:
    4596             :     case ROTZOOM:
    4597           0 :       params->wmmat[2] = aom_read_signed_primitive_refsubexpfin(
    4598             :                              r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
    4599             :                              (ref_params->wmmat[2] >> GM_ALPHA_PREC_DIFF) -
    4600             :                                  (1 << GM_ALPHA_PREC_BITS),
    4601             :                              ACCT_STR) *
    4602           0 :                              GM_ALPHA_DECODE_FACTOR +
    4603             :                          (1 << WARPEDMODEL_PREC_BITS);
    4604           0 :       if (type != VERTRAPEZOID)
    4605           0 :         params->wmmat[3] =
    4606           0 :             aom_read_signed_primitive_refsubexpfin(
    4607             :                 r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
    4608           0 :                 (ref_params->wmmat[3] >> GM_ALPHA_PREC_DIFF), ACCT_STR) *
    4609             :             GM_ALPHA_DECODE_FACTOR;
    4610           0 :       if (type >= AFFINE) {
    4611           0 :         if (type != HORTRAPEZOID)
    4612           0 :           params->wmmat[4] =
    4613           0 :               aom_read_signed_primitive_refsubexpfin(
    4614             :                   r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
    4615           0 :                   (ref_params->wmmat[4] >> GM_ALPHA_PREC_DIFF), ACCT_STR) *
    4616             :               GM_ALPHA_DECODE_FACTOR;
    4617           0 :         params->wmmat[5] = aom_read_signed_primitive_refsubexpfin(
    4618             :                                r, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
    4619             :                                (ref_params->wmmat[5] >> GM_ALPHA_PREC_DIFF) -
    4620             :                                    (1 << GM_ALPHA_PREC_BITS),
    4621             :                                ACCT_STR) *
    4622           0 :                                GM_ALPHA_DECODE_FACTOR +
    4623             :                            (1 << WARPEDMODEL_PREC_BITS);
    4624             :       } else {
    4625           0 :         params->wmmat[4] = -params->wmmat[3];
    4626           0 :         params->wmmat[5] = params->wmmat[2];
    4627             :       }
    4628             :     // fallthrough intended
    4629             :     case TRANSLATION:
    4630           0 :       trans_bits = (type == TRANSLATION) ? GM_ABS_TRANS_ONLY_BITS - !allow_hp
    4631           0 :                                          : GM_ABS_TRANS_BITS;
    4632           0 :       trans_dec_factor = (type == TRANSLATION)
    4633           0 :                              ? GM_TRANS_ONLY_DECODE_FACTOR * (1 << !allow_hp)
    4634           0 :                              : GM_TRANS_DECODE_FACTOR;
    4635           0 :       trans_prec_diff = (type == TRANSLATION)
    4636           0 :                             ? GM_TRANS_ONLY_PREC_DIFF + !allow_hp
    4637           0 :                             : GM_TRANS_PREC_DIFF;
    4638           0 :       params->wmmat[0] =
    4639           0 :           aom_read_signed_primitive_refsubexpfin(
    4640             :               r, (1 << trans_bits) + 1, SUBEXPFIN_K,
    4641           0 :               (ref_params->wmmat[0] >> trans_prec_diff), ACCT_STR) *
    4642             :           trans_dec_factor;
    4643           0 :       params->wmmat[1] =
    4644           0 :           aom_read_signed_primitive_refsubexpfin(
    4645             :               r, (1 << trans_bits) + 1, SUBEXPFIN_K,
    4646           0 :               (ref_params->wmmat[1] >> trans_prec_diff), ACCT_STR) *
    4647             :           trans_dec_factor;
    4648           0 :     case IDENTITY: break;
    4649           0 :     default: assert(0);
    4650             :   }
    4651           0 :   if (params->wmtype <= AFFINE)
    4652           0 :     if (!get_shear_params(params)) assert(0);
    4653           0 : }
    4654             : 
    4655           0 : static void read_global_motion(AV1_COMMON *cm, aom_reader *r) {
    4656             :   int frame;
    4657           0 :   for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
    4658           0 :     read_global_motion_params(
    4659           0 :         &cm->global_motion[frame], &cm->prev_frame->global_motion[frame],
    4660           0 :         cm->fc->global_motion_types_prob, r, cm->allow_high_precision_mv);
    4661             :     /*
    4662             :     printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
    4663             :            frame, cm->current_video_frame, cm->show_frame,
    4664             :            cm->global_motion[frame].wmmat[0],
    4665             :            cm->global_motion[frame].wmmat[1],
    4666             :            cm->global_motion[frame].wmmat[2],
    4667             :            cm->global_motion[frame].wmmat[3]);
    4668             :            */
    4669             :   }
    4670           0 :   memcpy(cm->cur_frame->global_motion, cm->global_motion,
    4671             :          TOTAL_REFS_PER_FRAME * sizeof(WarpedMotionParams));
    4672           0 : }
    4673             : #endif  // CONFIG_GLOBAL_MOTION
    4674             : 
    4675           0 : static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
    4676             :                                   size_t partition_size) {
    4677           0 :   AV1_COMMON *const cm = &pbi->common;
    4678             : #if CONFIG_SUPERTX
    4679             :   MACROBLOCKD *const xd = &pbi->mb;
    4680             : #endif
    4681           0 :   FRAME_CONTEXT *const fc = cm->fc;
    4682             :   aom_reader r;
    4683             :   int k, i;
    4684             : #if !CONFIG_EC_ADAPT || \
    4685             :     (CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION || CONFIG_EXT_INTER)
    4686             :   int j;
    4687             : #endif
    4688             : 
    4689             : #if CONFIG_ANS && ANS_MAX_SYMBOLS
    4690             :   r.window_size = 1 << cm->ans_window_size_log2;
    4691             : #endif
    4692           0 :   if (aom_reader_init(&r, data, partition_size, pbi->decrypt_cb,
    4693             :                       pbi->decrypt_state))
    4694           0 :     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
    4695             :                        "Failed to allocate bool decoder 0");
    4696             : 
    4697             : #if CONFIG_LOOP_RESTORATION
    4698             :   if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
    4699             :       cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
    4700             :       cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
    4701             :     av1_alloc_restoration_buffers(cm);
    4702             :     decode_restoration(cm, &r);
    4703             :   }
    4704             : #endif
    4705             : 
    4706             : #if !CONFIG_EC_ADAPT
    4707             :   if (cm->tx_mode == TX_MODE_SELECT) read_tx_size_probs(fc, &r);
    4708             : #endif
    4709             : #if CONFIG_EXT_TX && CONFIG_RECT_TX && CONFIG_RECT_TX_EXT
    4710             :   if (cm->tx_mode == TX_MODE_SELECT)
    4711             :     av1_diff_update_prob(&r, &fc->quarter_tx_size_prob, ACCT_STR);
    4712             : #endif  // CONFIG_EXT_TX && CONFIG_RECT_TX && CONFIG_RECT_TX_EXT
    4713             : 
    4714             : #if CONFIG_LV_MAP
    4715             :   av1_read_txb_probs(fc, cm->tx_mode, &r);
    4716             : #else  // CONFIG_LV_MAP
    4717             : #if !CONFIG_PVQ
    4718             : #if !CONFIG_EC_ADAPT
    4719             :   read_coef_probs(fc, cm->tx_mode, &r);
    4720             : #endif  // !CONFIG_EC_ADAPT
    4721             : #endif  // !CONFIG_PVQ
    4722             : #endif  // CONFIG_LV_MAP
    4723             : 
    4724             : #if CONFIG_VAR_TX
    4725           0 :   for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
    4726           0 :     av1_diff_update_prob(&r, &fc->txfm_partition_prob[k], ACCT_STR);
    4727             : #endif  // CONFIG_VAR_TX
    4728           0 :   for (k = 0; k < SKIP_CONTEXTS; ++k)
    4729           0 :     av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
    4730             : 
    4731             : #if CONFIG_DELTA_Q && !CONFIG_EC_ADAPT
    4732             : #if CONFIG_EXT_DELTA_Q
    4733             :   if (cm->delta_q_present_flag) {
    4734             :     for (k = 0; k < DELTA_Q_PROBS; ++k)
    4735             :       av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
    4736             :   }
    4737             :   if (cm->delta_lf_present_flag) {
    4738             :     for (k = 0; k < DELTA_LF_PROBS; ++k)
    4739             :       av1_diff_update_prob(&r, &fc->delta_lf_prob[k], ACCT_STR);
    4740             :   }
    4741             : #else
    4742             :   for (k = 0; k < DELTA_Q_PROBS; ++k)
    4743             :     av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
    4744             : #endif
    4745             : #endif
    4746             : 
    4747             : #if !CONFIG_EC_ADAPT
    4748             :   if (cm->seg.enabled && cm->seg.update_map) {
    4749             :     if (cm->seg.temporal_update) {
    4750             :       for (k = 0; k < PREDICTION_PROBS; k++)
    4751             :         av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
    4752             :     }
    4753             :     for (k = 0; k < MAX_SEGMENTS - 1; k++)
    4754             :       av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
    4755             :   }
    4756             : 
    4757             :   for (j = 0; j < INTRA_MODES; j++) {
    4758             :     for (i = 0; i < INTRA_MODES - 1; ++i)
    4759             :       av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
    4760             :   }
    4761             : 
    4762             : #if CONFIG_EXT_PARTITION_TYPES
    4763             :   for (j = 0; j < PARTITION_PLOFFSET; ++j)
    4764             :     for (i = 0; i < PARTITION_TYPES - 1; ++i)
    4765             :       av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
    4766             :   for (; j < PARTITION_CONTEXTS_PRIMARY; ++j)
    4767             :     for (i = 0; i < EXT_PARTITION_TYPES - 1; ++i)
    4768             :       av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
    4769             : #else
    4770             :   for (j = 0; j < PARTITION_CONTEXTS_PRIMARY; ++j)
    4771             :     for (i = 0; i < PARTITION_TYPES - 1; ++i)
    4772             :       av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
    4773             : #endif  // CONFIG_EXT_PARTITION_TYPES
    4774             : 
    4775             : #if CONFIG_UNPOISON_PARTITION_CTX
    4776             :   for (; j < PARTITION_CONTEXTS_PRIMARY + PARTITION_BLOCK_SIZES; ++j)
    4777             :     av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_VERT], ACCT_STR);
    4778             :   for (; j < PARTITION_CONTEXTS_PRIMARY + 2 * PARTITION_BLOCK_SIZES; ++j)
    4779             :     av1_diff_update_prob(&r, &fc->partition_prob[j][PARTITION_HORZ], ACCT_STR);
    4780             : #endif  // CONFIG_UNPOISON_PARTITION_CTX
    4781             : 
    4782             : #if CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
    4783             :   for (i = 0; i < INTRA_FILTERS + 1; ++i)
    4784             :     for (j = 0; j < INTRA_FILTERS - 1; ++j)
    4785             :       av1_diff_update_prob(&r, &fc->intra_filter_probs[i][j], ACCT_STR);
    4786             : #endif  // CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP
    4787             : #endif  // !CONFIG_EC_ADAPT
    4788             : 
    4789           0 :   if (frame_is_intra_only(cm)) {
    4790           0 :     av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob);
    4791           0 :     av1_copy(cm->fc->kf_y_cdf, av1_kf_y_mode_cdf);
    4792             : #if !CONFIG_EC_ADAPT
    4793             :     for (k = 0; k < INTRA_MODES; k++)
    4794             :       for (j = 0; j < INTRA_MODES; j++)
    4795             :         for (i = 0; i < INTRA_MODES - 1; ++i)
    4796             :           av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
    4797             : #endif
    4798             : #if CONFIG_INTRABC
    4799             :     if (cm->allow_screen_content_tools) {
    4800             :       av1_diff_update_prob(&r, &fc->intrabc_prob, ACCT_STR);
    4801             :     }
    4802             : #endif
    4803             :   } else {
    4804           0 :     read_inter_mode_probs(fc, &r);
    4805             : 
    4806             : #if CONFIG_EXT_INTER
    4807           0 :     read_inter_compound_mode_probs(fc, &r);
    4808             : #if CONFIG_INTERINTRA
    4809           0 :     if (cm->reference_mode != COMPOUND_REFERENCE &&
    4810           0 :         cm->allow_interintra_compound) {
    4811           0 :       for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
    4812           0 :         if (is_interintra_allowed_bsize_group(i)) {
    4813           0 :           av1_diff_update_prob(&r, &fc->interintra_prob[i], ACCT_STR);
    4814             :         }
    4815             :       }
    4816           0 :       for (i = 0; i < BLOCK_SIZE_GROUPS; i++) {
    4817           0 :         for (j = 0; j < INTERINTRA_MODES - 1; j++)
    4818           0 :           av1_diff_update_prob(&r, &fc->interintra_mode_prob[i][j], ACCT_STR);
    4819             :       }
    4820             : #if CONFIG_WEDGE
    4821           0 :       for (i = 0; i < BLOCK_SIZES; i++) {
    4822           0 :         if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) {
    4823           0 :           av1_diff_update_prob(&r, &fc->wedge_interintra_prob[i], ACCT_STR);
    4824             :         }
    4825             :       }
    4826             : #endif  // CONFIG_WEDGE
    4827             :     }
    4828             : #endif  // CONFIG_INTERINTRA
    4829             : #if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
    4830           0 :     if (cm->reference_mode != SINGLE_REFERENCE && cm->allow_masked_compound) {
    4831           0 :       for (i = 0; i < BLOCK_SIZES; i++) {
    4832           0 :         for (j = 0; j < COMPOUND_TYPES - 1; j++) {
    4833           0 :           av1_diff_update_prob(&r, &fc->compound_type_prob[i][j], ACCT_STR);
    4834             :         }
    4835             :       }
    4836             :     }
    4837             : #endif  // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
    4838             : #endif  // CONFIG_EXT_INTER
    4839             : 
    4840             : #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
    4841           0 :     for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) {
    4842           0 :       for (j = 0; j < MOTION_MODES - 1; ++j)
    4843           0 :         av1_diff_update_prob(&r, &fc->motion_mode_prob[i][j], ACCT_STR);
    4844             :     }
    4845             : #endif  // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
    4846             : 
    4847             : #if !CONFIG_EC_ADAPT
    4848             :     if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
    4849             : #endif
    4850             : 
    4851           0 :     for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
    4852           0 :       av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
    4853             : 
    4854           0 :     if (cm->reference_mode != SINGLE_REFERENCE)
    4855           0 :       setup_compound_reference_mode(cm);
    4856           0 :     read_frame_reference_mode_probs(cm, &r);
    4857             : 
    4858             : #if !CONFIG_EC_ADAPT
    4859             :     for (j = 0; j < BLOCK_SIZE_GROUPS; j++) {
    4860             :       for (i = 0; i < INTRA_MODES - 1; ++i)
    4861             :         av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
    4862             :     }
    4863             : #endif
    4864             : 
    4865           0 :     for (i = 0; i < NMV_CONTEXTS; ++i)
    4866           0 :       read_mv_probs(&fc->nmvc[i], cm->allow_high_precision_mv, &r);
    4867             : #if !CONFIG_EC_ADAPT
    4868             :     read_ext_tx_probs(fc, &r);
    4869             : #endif  // EC_ADAPT
    4870             : #if CONFIG_SUPERTX
    4871             :     if (!xd->lossless[0]) read_supertx_probs(fc, &r);
    4872             : #endif
    4873             : #if CONFIG_GLOBAL_MOTION
    4874           0 :     read_global_motion(cm, &r);
    4875             : #endif
    4876             :   }
    4877             : #if !CONFIG_EC_ADAPT
    4878             :   av1_coef_head_cdfs(fc);
    4879             :   /* Make tail distribution from head */
    4880             :   av1_coef_pareto_cdfs(fc);
    4881             :   for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]);
    4882             :   av1_set_mode_cdfs(cm);
    4883             : #endif  // !CONFIG_EC_ADAPT
    4884             : 
    4885           0 :   return aom_reader_has_error(&r);
    4886             : }
    4887             : 
    4888             : #ifdef NDEBUG
    4889             : #define debug_check_frame_counts(cm) (void)0
    4890             : #else  // !NDEBUG
    4891             : // Counts should only be incremented when frame_parallel_decoding_mode and
    4892             : // error_resilient_mode are disabled.
    4893           0 : static void debug_check_frame_counts(const AV1_COMMON *const cm) {
    4894             :   FRAME_COUNTS zero_counts;
    4895           0 :   av1_zero(zero_counts);
    4896           0 :   assert(cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD ||
    4897             :          cm->error_resilient_mode);
    4898           0 :   assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
    4899             :                  sizeof(cm->counts.y_mode)));
    4900           0 :   assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
    4901             :                  sizeof(cm->counts.uv_mode)));
    4902           0 :   assert(!memcmp(cm->counts.partition, zero_counts.partition,
    4903             :                  sizeof(cm->counts.partition)));
    4904           0 :   assert(!memcmp(cm->counts.coef, zero_counts.coef, sizeof(cm->counts.coef)));
    4905           0 :   assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
    4906             :                  sizeof(cm->counts.eob_branch)));
    4907           0 :   assert(!memcmp(cm->counts.blockz_count, zero_counts.blockz_count,
    4908             :                  sizeof(cm->counts.blockz_count)));
    4909           0 :   assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
    4910             :                  sizeof(cm->counts.switchable_interp)));
    4911           0 :   assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
    4912             :                  sizeof(cm->counts.inter_mode)));
    4913             : #if CONFIG_EXT_INTER
    4914           0 :   assert(!memcmp(cm->counts.inter_compound_mode,
    4915             :                  zero_counts.inter_compound_mode,
    4916             :                  sizeof(cm->counts.inter_compound_mode)));
    4917             : #if CONFIG_INTERINTRA
    4918           0 :   assert(!memcmp(cm->counts.interintra, zero_counts.interintra,
    4919             :                  sizeof(cm->counts.interintra)));
    4920             : #if CONFIG_WEDGE
    4921           0 :   assert(!memcmp(cm->counts.wedge_interintra, zero_counts.wedge_interintra,
    4922             :                  sizeof(cm->counts.wedge_interintra)));
    4923             : #endif  // CONFIG_WEDGE
    4924             : #endif  // CONFIG_INTERINTRA
    4925           0 :   assert(!memcmp(cm->counts.compound_interinter,
    4926             :                  zero_counts.compound_interinter,
    4927             :                  sizeof(cm->counts.compound_interinter)));
    4928             : #endif  // CONFIG_EXT_INTER
    4929             : #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
    4930           0 :   assert(!memcmp(cm->counts.motion_mode, zero_counts.motion_mode,
    4931             :                  sizeof(cm->counts.motion_mode)));
    4932             : #endif  // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
    4933           0 :   assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
    4934             :                  sizeof(cm->counts.intra_inter)));
    4935           0 :   assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
    4936             :                  sizeof(cm->counts.comp_inter)));
    4937           0 :   assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
    4938             :                  sizeof(cm->counts.single_ref)));
    4939           0 :   assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
    4940             :                  sizeof(cm->counts.comp_ref)));
    4941             : #if CONFIG_EXT_REFS
    4942           0 :   assert(!memcmp(cm->counts.comp_bwdref, zero_counts.comp_bwdref,
    4943             :                  sizeof(cm->counts.comp_bwdref)));
    4944             : #endif  // CONFIG_EXT_REFS
    4945           0 :   assert(!memcmp(&cm->counts.tx_size, &zero_counts.tx_size,
    4946             :                  sizeof(cm->counts.tx_size)));
    4947           0 :   assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
    4948           0 :   assert(
    4949             :       !memcmp(&cm->counts.mv[0], &zero_counts.mv[0], sizeof(cm->counts.mv[0])));
    4950           0 :   assert(
    4951             :       !memcmp(&cm->counts.mv[1], &zero_counts.mv[1], sizeof(cm->counts.mv[0])));
    4952           0 :   assert(!memcmp(cm->counts.inter_ext_tx, zero_counts.inter_ext_tx,
    4953             :                  sizeof(cm->counts.inter_ext_tx)));
    4954           0 :   assert(!memcmp(cm->counts.intra_ext_tx, zero_counts.intra_ext_tx,
    4955             :                  sizeof(cm->counts.intra_ext_tx)));
    4956           0 : }
    4957             : #endif  // NDEBUG
    4958             : 
    4959           0 : static struct aom_read_bit_buffer *init_read_bit_buffer(
    4960             :     AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
    4961             :     const uint8_t *data_end, uint8_t clear_data[MAX_AV1_HEADER_SIZE]) {
    4962           0 :   rb->bit_offset = 0;
    4963           0 :   rb->error_handler = error_handler;
    4964           0 :   rb->error_handler_data = &pbi->common;
    4965           0 :   if (pbi->decrypt_cb) {
    4966           0 :     const int n = (int)AOMMIN(MAX_AV1_HEADER_SIZE, data_end - data);
    4967           0 :     pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
    4968           0 :     rb->bit_buffer = clear_data;
    4969           0 :     rb->bit_buffer_end = clear_data + n;
    4970             :   } else {
    4971           0 :     rb->bit_buffer = data;
    4972           0 :     rb->bit_buffer_end = data_end;
    4973             :   }
    4974           0 :   return rb;
    4975             : }
    4976             : 
    4977             : //------------------------------------------------------------------------------
    4978             : 
    4979           0 : int av1_read_sync_code(struct aom_read_bit_buffer *const rb) {
    4980           0 :   return aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_0 &&
    4981           0 :          aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_1 &&
    4982           0 :          aom_rb_read_literal(rb, 8) == AV1_SYNC_CODE_2;
    4983             : }
    4984             : 
    4985           0 : void av1_read_frame_size(struct aom_read_bit_buffer *rb, int *width,
    4986             :                          int *height) {
    4987           0 :   *width = aom_rb_read_literal(rb, 16) + 1;
    4988           0 :   *height = aom_rb_read_literal(rb, 16) + 1;
    4989           0 : }
    4990             : 
    4991           0 : BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
    4992           0 :   int profile = aom_rb_read_bit(rb);
    4993           0 :   profile |= aom_rb_read_bit(rb) << 1;
    4994           0 :   if (profile > 2) profile += aom_rb_read_bit(rb);
    4995           0 :   return (BITSTREAM_PROFILE)profile;
    4996             : }
    4997             : 
    4998             : #if CONFIG_EC_ADAPT
    4999           0 : static void make_update_tile_list_dec(AV1Decoder *pbi, int tile_rows,
    5000             :                                       int tile_cols, FRAME_CONTEXT *ec_ctxs[]) {
    5001             :   int i;
    5002           0 :   for (i = 0; i < tile_rows * tile_cols; ++i)
    5003           0 :     ec_ctxs[i] = &pbi->tile_data[i].tctx;
    5004           0 : }
    5005             : #endif
    5006             : 
    5007           0 : void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
    5008             :                       const uint8_t *data_end, const uint8_t **p_data_end) {
    5009           0 :   AV1_COMMON *const cm = &pbi->common;
    5010           0 :   MACROBLOCKD *const xd = &pbi->mb;
    5011             :   struct aom_read_bit_buffer rb;
    5012           0 :   int context_updated = 0;
    5013             :   uint8_t clear_data[MAX_AV1_HEADER_SIZE];
    5014             :   size_t first_partition_size;
    5015             :   YV12_BUFFER_CONFIG *new_fb;
    5016             : #if CONFIG_EXT_REFS || CONFIG_TEMPMV_SIGNALING
    5017           0 :   RefBuffer *last_fb_ref_buf = &cm->frame_refs[LAST_FRAME - LAST_FRAME];
    5018             : #endif  // CONFIG_EXT_REFS || CONFIG_TEMPMV_SIGNALING
    5019             : 
    5020             : #if CONFIG_ADAPT_SCAN
    5021             :   av1_deliver_eob_threshold(cm, xd);
    5022             : #endif
    5023             : #if CONFIG_BITSTREAM_DEBUG
    5024             :   bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
    5025             : #endif
    5026             : 
    5027           0 :   first_partition_size = read_uncompressed_header(
    5028             :       pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
    5029             : 
    5030             : #if CONFIG_EXT_TILE
    5031             :   // If cm->tile_encoding_mode == TILE_NORMAL, the independent decoding of a
    5032             :   // single tile or a section of a frame is not allowed.
    5033             :   if (!cm->tile_encoding_mode &&
    5034             :       (pbi->dec_tile_row >= 0 || pbi->dec_tile_col >= 0)) {
    5035             :     pbi->dec_tile_row = -1;
    5036             :     pbi->dec_tile_col = -1;
    5037             :   }
    5038             : #endif  // CONFIG_EXT_TILE
    5039             : 
    5040             : #if CONFIG_TILE_GROUPS
    5041           0 :   pbi->first_partition_size = first_partition_size;
    5042           0 :   pbi->uncomp_hdr_size = aom_rb_bytes_read(&rb);
    5043             : #endif
    5044           0 :   new_fb = get_frame_new_buffer(cm);
    5045           0 :   xd->cur_buf = new_fb;
    5046             : #if CONFIG_INTRABC
    5047             : #if CONFIG_HIGHBITDEPTH
    5048             :   av1_setup_scale_factors_for_frame(
    5049             :       &xd->sf_identity, xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height,
    5050             :       xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height,
    5051             :       cm->use_highbitdepth);
    5052             : #else
    5053             :   av1_setup_scale_factors_for_frame(
    5054             :       &xd->sf_identity, xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height,
    5055             :       xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height);
    5056             : #endif  // CONFIG_HIGHBITDEPTH
    5057             : #endif  // CONFIG_INTRABC
    5058             : #if CONFIG_GLOBAL_MOTION
    5059             :   int i;
    5060           0 :   for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
    5061           0 :     set_default_warp_params(&cm->global_motion[i]);
    5062           0 :     set_default_warp_params(&cm->cur_frame->global_motion[i]);
    5063             :   }
    5064           0 :   xd->global_motion = cm->global_motion;
    5065             : #endif  // CONFIG_GLOBAL_MOTION
    5066             : 
    5067           0 :   if (!first_partition_size) {
    5068             :     // showing a frame directly
    5069           0 :     *p_data_end = data + aom_rb_bytes_read(&rb);
    5070           0 :     return;
    5071             :   }
    5072             : 
    5073           0 :   data += aom_rb_bytes_read(&rb);
    5074           0 :   if (!read_is_valid(data, first_partition_size, data_end))
    5075           0 :     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
    5076             :                        "Truncated packet or corrupt header length");
    5077             : 
    5078           0 :   cm->setup_mi(cm);
    5079             : 
    5080             : #if CONFIG_EXT_REFS || CONFIG_TEMPMV_SIGNALING
    5081             :   // NOTE(zoeliu): As cm->prev_frame can take neither a frame of
    5082             :   //               show_exisiting_frame=1, nor can it take a frame not used as
    5083             :   //               a reference, it is probable that by the time it is being
    5084             :   //               referred to, the frame buffer it originally points to may
    5085             :   //               already get expired and have been reassigned to the current
    5086             :   //               newly coded frame. Hence, we need to check whether this is
    5087             :   //               the case, and if yes, we have 2 choices:
    5088             :   //               (1) Simply disable the use of previous frame mvs; or
    5089             :   //               (2) Have cm->prev_frame point to one reference frame buffer,
    5090             :   //                   e.g. LAST_FRAME.
    5091           0 :   if (!dec_is_ref_frame_buf(pbi, cm->prev_frame)) {
    5092             :     // Reassign the LAST_FRAME buffer to cm->prev_frame.
    5093           0 :     cm->prev_frame = last_fb_ref_buf->idx != INVALID_IDX
    5094           0 :                          ? &cm->buffer_pool->frame_bufs[last_fb_ref_buf->idx]
    5095           0 :                          : NULL;
    5096             :   }
    5097             : #endif  // CONFIG_EXT_REFS || CONFIG_TEMPMV_SIGNALING
    5098             : 
    5099             : #if CONFIG_TEMPMV_SIGNALING
    5100           0 :   if (cm->use_prev_frame_mvs) {
    5101           0 :     assert(!cm->error_resilient_mode && cm->prev_frame &&
    5102             :            cm->width == last_fb_ref_buf->buf->y_width &&
    5103             :            cm->height == last_fb_ref_buf->buf->y_height &&
    5104             :            !cm->prev_frame->intra_only);
    5105             :   }
    5106             : #else
    5107             :   cm->use_prev_frame_mvs = !cm->error_resilient_mode && cm->prev_frame &&
    5108             :                            cm->width == cm->prev_frame->buf.y_crop_width &&
    5109             :                            cm->height == cm->prev_frame->buf.y_crop_height &&
    5110             :                            !cm->last_intra_only && cm->last_show_frame &&
    5111             :                            (cm->last_frame_type != KEY_FRAME);
    5112             : #endif  // CONFIG_TEMPMV_SIGNALING
    5113             : 
    5114           0 :   av1_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
    5115             : 
    5116           0 :   *cm->fc = cm->frame_contexts[cm->frame_context_idx];
    5117           0 :   cm->pre_fc = &cm->frame_contexts[cm->frame_context_idx];
    5118           0 :   if (!cm->fc->initialized)
    5119           0 :     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
    5120             :                        "Uninitialized entropy context.");
    5121             : 
    5122           0 :   av1_zero(cm->counts);
    5123             : 
    5124           0 :   xd->corrupted = 0;
    5125           0 :   new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
    5126           0 :   if (new_fb->corrupted)
    5127           0 :     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
    5128             :                        "Decode failed. Frame data header is corrupted.");
    5129             : 
    5130           0 :   if (cm->lf.filter_level && !cm->skip_loop_filter) {
    5131           0 :     av1_loop_filter_frame_init(cm, cm->lf.filter_level);
    5132             :   }
    5133             : 
    5134             :   // If encoded in frame parallel mode, frame context is ready after decoding
    5135             :   // the frame header.
    5136           0 :   if (cm->frame_parallel_decode &&
    5137           0 :       cm->refresh_frame_context != REFRESH_FRAME_CONTEXT_BACKWARD) {
    5138           0 :     AVxWorker *const worker = pbi->frame_worker_owner;
    5139           0 :     FrameWorkerData *const frame_worker_data = worker->data1;
    5140           0 :     if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD) {
    5141           0 :       context_updated = 1;
    5142           0 :       cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
    5143             :     }
    5144           0 :     av1_frameworker_lock_stats(worker);
    5145           0 :     pbi->cur_buf->row = -1;
    5146           0 :     pbi->cur_buf->col = -1;
    5147           0 :     frame_worker_data->frame_context_ready = 1;
    5148             :     // Signal the main thread that context is ready.
    5149           0 :     av1_frameworker_signal_stats(worker);
    5150           0 :     av1_frameworker_unlock_stats(worker);
    5151             :   }
    5152             : 
    5153             :   if (pbi->max_threads > 1 && !CONFIG_CB4X4 &&
    5154             : #if CONFIG_EXT_TILE
    5155             :       pbi->dec_tile_col < 0 &&  // Decoding all columns
    5156             : #endif                          // CONFIG_EXT_TILE
    5157             :       cm->tile_cols > 1) {
    5158             :     // Multi-threaded tile decoder
    5159             :     *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
    5160             :     if (!xd->corrupted) {
    5161             :       if (!cm->skip_loop_filter) {
    5162             :         // If multiple threads are used to decode tiles, then we use those
    5163             :         // threads to do parallel loopfiltering.
    5164             :         av1_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
    5165             :                                  0, 0, pbi->tile_workers, pbi->num_tile_workers,
    5166             :                                  &pbi->lf_row_sync);
    5167             :       }
    5168             :     } else {
    5169             :       aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
    5170             :                          "Decode failed. Frame data is corrupted.");
    5171             :     }
    5172             :   } else {
    5173           0 :     *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
    5174             :   }
    5175             : 
    5176             : #if CONFIG_CDEF
    5177           0 :   if (!cm->skip_loop_filter) {
    5178           0 :     av1_cdef_frame(&pbi->cur_buf->buf, cm, &pbi->mb);
    5179             :   }
    5180             : #endif  // CONFIG_CDEF
    5181             : 
    5182             : #if CONFIG_LOOP_RESTORATION
    5183             :   if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
    5184             :       cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
    5185             :       cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
    5186             :     av1_loop_restoration_frame(new_fb, cm, cm->rst_info, 7, 0, NULL);
    5187             :   }
    5188             : #endif  // CONFIG_LOOP_RESTORATION
    5189             : 
    5190           0 :   if (!xd->corrupted) {
    5191           0 :     if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
    5192             : #if CONFIG_EC_ADAPT
    5193           0 :       FRAME_CONTEXT **tile_ctxs = aom_malloc(cm->tile_rows * cm->tile_cols *
    5194             :                                              sizeof(&pbi->tile_data[0].tctx));
    5195           0 :       aom_cdf_prob **cdf_ptrs =
    5196           0 :           aom_malloc(cm->tile_rows * cm->tile_cols *
    5197             :                      sizeof(&pbi->tile_data[0].tctx.partition_cdf[0][0]));
    5198           0 :       make_update_tile_list_dec(pbi, cm->tile_rows, cm->tile_cols, tile_ctxs);
    5199             : #endif
    5200           0 :       av1_adapt_coef_probs(cm);
    5201           0 :       av1_adapt_intra_frame_probs(cm);
    5202             : #if CONFIG_EC_ADAPT
    5203           0 :       av1_average_tile_coef_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
    5204           0 :                                  cm->tile_rows * cm->tile_cols);
    5205           0 :       av1_average_tile_intra_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
    5206           0 :                                   cm->tile_rows * cm->tile_cols);
    5207             : #if CONFIG_PVQ
    5208             :       av1_average_tile_pvq_cdfs(pbi->common.fc, tile_ctxs,
    5209             :                                 cm->tile_rows * cm->tile_cols);
    5210             : #endif  // CONFIG_PVQ
    5211             : #endif  // CONFIG_EC_ADAPT
    5212             : #if CONFIG_ADAPT_SCAN
    5213             :       av1_adapt_scan_order(cm);
    5214             : #endif  // CONFIG_ADAPT_SCAN
    5215             : 
    5216           0 :       if (!frame_is_intra_only(cm)) {
    5217           0 :         av1_adapt_inter_frame_probs(cm);
    5218           0 :         av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
    5219             : #if CONFIG_EC_ADAPT
    5220           0 :         av1_average_tile_inter_cdfs(&pbi->common, pbi->common.fc, tile_ctxs,
    5221           0 :                                     cdf_ptrs, cm->tile_rows * cm->tile_cols);
    5222           0 :         av1_average_tile_mv_cdfs(pbi->common.fc, tile_ctxs, cdf_ptrs,
    5223           0 :                                  cm->tile_rows * cm->tile_cols);
    5224             : #endif
    5225             :       }
    5226             : #if CONFIG_EC_ADAPT
    5227           0 :       aom_free(tile_ctxs);
    5228           0 :       aom_free(cdf_ptrs);
    5229             : #endif
    5230             :     } else {
    5231           0 :       debug_check_frame_counts(cm);
    5232             :     }
    5233             :   } else {
    5234           0 :     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
    5235             :                        "Decode failed. Frame data is corrupted.");
    5236             :   }
    5237             : 
    5238             : #if CONFIG_INSPECTION
    5239             :   if (pbi->inspect_cb != NULL) {
    5240             :     (*pbi->inspect_cb)(pbi, pbi->inspect_ctx);
    5241             :   }
    5242             : #endif
    5243             : 
    5244             :   // Non frame parallel update frame context here.
    5245           0 :   if (!cm->error_resilient_mode && !context_updated)
    5246           0 :     cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
    5247             : }

Generated by: LCOV version 1.13