xref: /aosp_15_r20/external/libaom/av1/decoder/decodemv.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
13*77c1e3ccSAndroid Build Coastguard Worker 
14*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/cfl.h"
15*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/common.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/entropy.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/entropymode.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/entropymv.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/mvref_common.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/pred_common.h"
21*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/reconinter.h"
22*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/reconintra.h"
23*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/seg_common.h"
24*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/warped_motion.h"
25*77c1e3ccSAndroid Build Coastguard Worker 
26*77c1e3ccSAndroid Build Coastguard Worker #include "av1/decoder/decodeframe.h"
27*77c1e3ccSAndroid Build Coastguard Worker #include "av1/decoder/decodemv.h"
28*77c1e3ccSAndroid Build Coastguard Worker 
29*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/aom_dsp_common.h"
30*77c1e3ccSAndroid Build Coastguard Worker 
31*77c1e3ccSAndroid Build Coastguard Worker #define ACCT_STR __func__
32*77c1e3ccSAndroid Build Coastguard Worker 
33*77c1e3ccSAndroid Build Coastguard Worker #define DEC_MISMATCH_DEBUG 0
34*77c1e3ccSAndroid Build Coastguard Worker 
read_intra_mode(aom_reader * r,aom_cdf_prob * cdf)35*77c1e3ccSAndroid Build Coastguard Worker static PREDICTION_MODE read_intra_mode(aom_reader *r, aom_cdf_prob *cdf) {
36*77c1e3ccSAndroid Build Coastguard Worker   return (PREDICTION_MODE)aom_read_symbol(r, cdf, INTRA_MODES, ACCT_STR);
37*77c1e3ccSAndroid Build Coastguard Worker }
38*77c1e3ccSAndroid Build Coastguard Worker 
read_cdef(AV1_COMMON * cm,aom_reader * r,MACROBLOCKD * const xd)39*77c1e3ccSAndroid Build Coastguard Worker static void read_cdef(AV1_COMMON *cm, aom_reader *r, MACROBLOCKD *const xd) {
40*77c1e3ccSAndroid Build Coastguard Worker   const int skip_txfm = xd->mi[0]->skip_txfm;
41*77c1e3ccSAndroid Build Coastguard Worker   if (cm->features.coded_lossless) return;
42*77c1e3ccSAndroid Build Coastguard Worker   if (cm->features.allow_intrabc) {
43*77c1e3ccSAndroid Build Coastguard Worker     assert(cm->cdef_info.cdef_bits == 0);
44*77c1e3ccSAndroid Build Coastguard Worker     return;
45*77c1e3ccSAndroid Build Coastguard Worker   }
46*77c1e3ccSAndroid Build Coastguard Worker 
47*77c1e3ccSAndroid Build Coastguard Worker   // At the start of a superblock, mark that we haven't yet read CDEF strengths
48*77c1e3ccSAndroid Build Coastguard Worker   // for any of the CDEF units contained in this superblock.
49*77c1e3ccSAndroid Build Coastguard Worker   const int sb_mask = (cm->seq_params->mib_size - 1);
50*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row_in_sb = (xd->mi_row & sb_mask);
51*77c1e3ccSAndroid Build Coastguard Worker   const int mi_col_in_sb = (xd->mi_col & sb_mask);
52*77c1e3ccSAndroid Build Coastguard Worker   if (mi_row_in_sb == 0 && mi_col_in_sb == 0) {
53*77c1e3ccSAndroid Build Coastguard Worker     xd->cdef_transmitted[0] = xd->cdef_transmitted[1] =
54*77c1e3ccSAndroid Build Coastguard Worker         xd->cdef_transmitted[2] = xd->cdef_transmitted[3] = false;
55*77c1e3ccSAndroid Build Coastguard Worker   }
56*77c1e3ccSAndroid Build Coastguard Worker 
57*77c1e3ccSAndroid Build Coastguard Worker   // CDEF unit size is 64x64 irrespective of the superblock size.
58*77c1e3ccSAndroid Build Coastguard Worker   const int cdef_size = 1 << (6 - MI_SIZE_LOG2);
59*77c1e3ccSAndroid Build Coastguard Worker 
60*77c1e3ccSAndroid Build Coastguard Worker   // Find index of this CDEF unit in this superblock.
61*77c1e3ccSAndroid Build Coastguard Worker   const int index_mask = cdef_size;
62*77c1e3ccSAndroid Build Coastguard Worker   const int cdef_unit_row_in_sb = ((xd->mi_row & index_mask) != 0);
63*77c1e3ccSAndroid Build Coastguard Worker   const int cdef_unit_col_in_sb = ((xd->mi_col & index_mask) != 0);
64*77c1e3ccSAndroid Build Coastguard Worker   const int index = (cm->seq_params->sb_size == BLOCK_128X128)
65*77c1e3ccSAndroid Build Coastguard Worker                         ? cdef_unit_col_in_sb + 2 * cdef_unit_row_in_sb
66*77c1e3ccSAndroid Build Coastguard Worker                         : 0;
67*77c1e3ccSAndroid Build Coastguard Worker 
68*77c1e3ccSAndroid Build Coastguard Worker   // Read CDEF strength from the first non-skip coding block in this CDEF unit.
69*77c1e3ccSAndroid Build Coastguard Worker   if (!xd->cdef_transmitted[index] && !skip_txfm) {
70*77c1e3ccSAndroid Build Coastguard Worker     // CDEF strength for this CDEF unit needs to be read into the MB_MODE_INFO
71*77c1e3ccSAndroid Build Coastguard Worker     // of the 1st block in this CDEF unit.
72*77c1e3ccSAndroid Build Coastguard Worker     const int first_block_mask = ~(cdef_size - 1);
73*77c1e3ccSAndroid Build Coastguard Worker     CommonModeInfoParams *const mi_params = &cm->mi_params;
74*77c1e3ccSAndroid Build Coastguard Worker     const int grid_idx =
75*77c1e3ccSAndroid Build Coastguard Worker         get_mi_grid_idx(mi_params, xd->mi_row & first_block_mask,
76*77c1e3ccSAndroid Build Coastguard Worker                         xd->mi_col & first_block_mask);
77*77c1e3ccSAndroid Build Coastguard Worker     MB_MODE_INFO *const mbmi = mi_params->mi_grid_base[grid_idx];
78*77c1e3ccSAndroid Build Coastguard Worker     mbmi->cdef_strength =
79*77c1e3ccSAndroid Build Coastguard Worker         aom_read_literal(r, cm->cdef_info.cdef_bits, ACCT_STR);
80*77c1e3ccSAndroid Build Coastguard Worker     xd->cdef_transmitted[index] = true;
81*77c1e3ccSAndroid Build Coastguard Worker   }
82*77c1e3ccSAndroid Build Coastguard Worker }
83*77c1e3ccSAndroid Build Coastguard Worker 
read_delta_qindex(AV1_COMMON * cm,const MACROBLOCKD * xd,aom_reader * r,MB_MODE_INFO * const mbmi)84*77c1e3ccSAndroid Build Coastguard Worker static int read_delta_qindex(AV1_COMMON *cm, const MACROBLOCKD *xd,
85*77c1e3ccSAndroid Build Coastguard Worker                              aom_reader *r, MB_MODE_INFO *const mbmi) {
86*77c1e3ccSAndroid Build Coastguard Worker   int sign, abs, reduced_delta_qindex = 0;
87*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_SIZE bsize = mbmi->bsize;
88*77c1e3ccSAndroid Build Coastguard Worker   const int b_col = xd->mi_col & (cm->seq_params->mib_size - 1);
89*77c1e3ccSAndroid Build Coastguard Worker   const int b_row = xd->mi_row & (cm->seq_params->mib_size - 1);
90*77c1e3ccSAndroid Build Coastguard Worker   const int read_delta_q_flag = (b_col == 0 && b_row == 0);
91*77c1e3ccSAndroid Build Coastguard Worker   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
92*77c1e3ccSAndroid Build Coastguard Worker 
93*77c1e3ccSAndroid Build Coastguard Worker   if ((bsize != cm->seq_params->sb_size || mbmi->skip_txfm == 0) &&
94*77c1e3ccSAndroid Build Coastguard Worker       read_delta_q_flag) {
95*77c1e3ccSAndroid Build Coastguard Worker     abs = aom_read_symbol(r, ec_ctx->delta_q_cdf, DELTA_Q_PROBS + 1, ACCT_STR);
96*77c1e3ccSAndroid Build Coastguard Worker     const int smallval = (abs < DELTA_Q_SMALL);
97*77c1e3ccSAndroid Build Coastguard Worker 
98*77c1e3ccSAndroid Build Coastguard Worker     if (!smallval) {
99*77c1e3ccSAndroid Build Coastguard Worker       const int rem_bits = aom_read_literal(r, 3, ACCT_STR) + 1;
100*77c1e3ccSAndroid Build Coastguard Worker       const int thr = (1 << rem_bits) + 1;
101*77c1e3ccSAndroid Build Coastguard Worker       abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
102*77c1e3ccSAndroid Build Coastguard Worker     }
103*77c1e3ccSAndroid Build Coastguard Worker 
104*77c1e3ccSAndroid Build Coastguard Worker     if (abs) {
105*77c1e3ccSAndroid Build Coastguard Worker       sign = aom_read_bit(r, ACCT_STR);
106*77c1e3ccSAndroid Build Coastguard Worker     } else {
107*77c1e3ccSAndroid Build Coastguard Worker       sign = 1;
108*77c1e3ccSAndroid Build Coastguard Worker     }
109*77c1e3ccSAndroid Build Coastguard Worker 
110*77c1e3ccSAndroid Build Coastguard Worker     reduced_delta_qindex = sign ? -abs : abs;
111*77c1e3ccSAndroid Build Coastguard Worker   }
112*77c1e3ccSAndroid Build Coastguard Worker   return reduced_delta_qindex;
113*77c1e3ccSAndroid Build Coastguard Worker }
read_delta_lflevel(const AV1_COMMON * const cm,aom_reader * r,aom_cdf_prob * const cdf,const MB_MODE_INFO * const mbmi,int mi_col,int mi_row)114*77c1e3ccSAndroid Build Coastguard Worker static int read_delta_lflevel(const AV1_COMMON *const cm, aom_reader *r,
115*77c1e3ccSAndroid Build Coastguard Worker                               aom_cdf_prob *const cdf,
116*77c1e3ccSAndroid Build Coastguard Worker                               const MB_MODE_INFO *const mbmi, int mi_col,
117*77c1e3ccSAndroid Build Coastguard Worker                               int mi_row) {
118*77c1e3ccSAndroid Build Coastguard Worker   int reduced_delta_lflevel = 0;
119*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = mbmi->bsize;
120*77c1e3ccSAndroid Build Coastguard Worker   const int b_col = mi_col & (cm->seq_params->mib_size - 1);
121*77c1e3ccSAndroid Build Coastguard Worker   const int b_row = mi_row & (cm->seq_params->mib_size - 1);
122*77c1e3ccSAndroid Build Coastguard Worker   const int read_delta_lf_flag = (b_col == 0 && b_row == 0);
123*77c1e3ccSAndroid Build Coastguard Worker 
124*77c1e3ccSAndroid Build Coastguard Worker   if ((bsize != cm->seq_params->sb_size || mbmi->skip_txfm == 0) &&
125*77c1e3ccSAndroid Build Coastguard Worker       read_delta_lf_flag) {
126*77c1e3ccSAndroid Build Coastguard Worker     int abs = aom_read_symbol(r, cdf, DELTA_LF_PROBS + 1, ACCT_STR);
127*77c1e3ccSAndroid Build Coastguard Worker     const int smallval = (abs < DELTA_LF_SMALL);
128*77c1e3ccSAndroid Build Coastguard Worker     if (!smallval) {
129*77c1e3ccSAndroid Build Coastguard Worker       const int rem_bits = aom_read_literal(r, 3, ACCT_STR) + 1;
130*77c1e3ccSAndroid Build Coastguard Worker       const int thr = (1 << rem_bits) + 1;
131*77c1e3ccSAndroid Build Coastguard Worker       abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
132*77c1e3ccSAndroid Build Coastguard Worker     }
133*77c1e3ccSAndroid Build Coastguard Worker     const int sign = abs ? aom_read_bit(r, ACCT_STR) : 1;
134*77c1e3ccSAndroid Build Coastguard Worker     reduced_delta_lflevel = sign ? -abs : abs;
135*77c1e3ccSAndroid Build Coastguard Worker   }
136*77c1e3ccSAndroid Build Coastguard Worker   return reduced_delta_lflevel;
137*77c1e3ccSAndroid Build Coastguard Worker }
138*77c1e3ccSAndroid Build Coastguard Worker 
read_intra_mode_uv(FRAME_CONTEXT * ec_ctx,aom_reader * r,CFL_ALLOWED_TYPE cfl_allowed,PREDICTION_MODE y_mode)139*77c1e3ccSAndroid Build Coastguard Worker static UV_PREDICTION_MODE read_intra_mode_uv(FRAME_CONTEXT *ec_ctx,
140*77c1e3ccSAndroid Build Coastguard Worker                                              aom_reader *r,
141*77c1e3ccSAndroid Build Coastguard Worker                                              CFL_ALLOWED_TYPE cfl_allowed,
142*77c1e3ccSAndroid Build Coastguard Worker                                              PREDICTION_MODE y_mode) {
143*77c1e3ccSAndroid Build Coastguard Worker   const UV_PREDICTION_MODE uv_mode =
144*77c1e3ccSAndroid Build Coastguard Worker       aom_read_symbol(r, ec_ctx->uv_mode_cdf[cfl_allowed][y_mode],
145*77c1e3ccSAndroid Build Coastguard Worker                       UV_INTRA_MODES - !cfl_allowed, ACCT_STR);
146*77c1e3ccSAndroid Build Coastguard Worker   return uv_mode;
147*77c1e3ccSAndroid Build Coastguard Worker }
148*77c1e3ccSAndroid Build Coastguard Worker 
read_cfl_alphas(FRAME_CONTEXT * const ec_ctx,aom_reader * r,int8_t * signs_out)149*77c1e3ccSAndroid Build Coastguard Worker static uint8_t read_cfl_alphas(FRAME_CONTEXT *const ec_ctx, aom_reader *r,
150*77c1e3ccSAndroid Build Coastguard Worker                                int8_t *signs_out) {
151*77c1e3ccSAndroid Build Coastguard Worker   const int8_t joint_sign =
152*77c1e3ccSAndroid Build Coastguard Worker       aom_read_symbol(r, ec_ctx->cfl_sign_cdf, CFL_JOINT_SIGNS, "cfl:signs");
153*77c1e3ccSAndroid Build Coastguard Worker   uint8_t idx = 0;
154*77c1e3ccSAndroid Build Coastguard Worker   // Magnitudes are only coded for nonzero values
155*77c1e3ccSAndroid Build Coastguard Worker   if (CFL_SIGN_U(joint_sign) != CFL_SIGN_ZERO) {
156*77c1e3ccSAndroid Build Coastguard Worker     aom_cdf_prob *cdf_u = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
157*77c1e3ccSAndroid Build Coastguard Worker     idx = (uint8_t)aom_read_symbol(r, cdf_u, CFL_ALPHABET_SIZE, "cfl:alpha_u")
158*77c1e3ccSAndroid Build Coastguard Worker           << CFL_ALPHABET_SIZE_LOG2;
159*77c1e3ccSAndroid Build Coastguard Worker   }
160*77c1e3ccSAndroid Build Coastguard Worker   if (CFL_SIGN_V(joint_sign) != CFL_SIGN_ZERO) {
161*77c1e3ccSAndroid Build Coastguard Worker     aom_cdf_prob *cdf_v = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)];
162*77c1e3ccSAndroid Build Coastguard Worker     idx += (uint8_t)aom_read_symbol(r, cdf_v, CFL_ALPHABET_SIZE, "cfl:alpha_v");
163*77c1e3ccSAndroid Build Coastguard Worker   }
164*77c1e3ccSAndroid Build Coastguard Worker   *signs_out = joint_sign;
165*77c1e3ccSAndroid Build Coastguard Worker   return idx;
166*77c1e3ccSAndroid Build Coastguard Worker }
167*77c1e3ccSAndroid Build Coastguard Worker 
read_interintra_mode(MACROBLOCKD * xd,aom_reader * r,int size_group)168*77c1e3ccSAndroid Build Coastguard Worker static INTERINTRA_MODE read_interintra_mode(MACROBLOCKD *xd, aom_reader *r,
169*77c1e3ccSAndroid Build Coastguard Worker                                             int size_group) {
170*77c1e3ccSAndroid Build Coastguard Worker   const INTERINTRA_MODE ii_mode = (INTERINTRA_MODE)aom_read_symbol(
171*77c1e3ccSAndroid Build Coastguard Worker       r, xd->tile_ctx->interintra_mode_cdf[size_group], INTERINTRA_MODES,
172*77c1e3ccSAndroid Build Coastguard Worker       ACCT_STR);
173*77c1e3ccSAndroid Build Coastguard Worker   return ii_mode;
174*77c1e3ccSAndroid Build Coastguard Worker }
175*77c1e3ccSAndroid Build Coastguard Worker 
read_inter_mode(FRAME_CONTEXT * ec_ctx,aom_reader * r,int16_t ctx)176*77c1e3ccSAndroid Build Coastguard Worker static PREDICTION_MODE read_inter_mode(FRAME_CONTEXT *ec_ctx, aom_reader *r,
177*77c1e3ccSAndroid Build Coastguard Worker                                        int16_t ctx) {
178*77c1e3ccSAndroid Build Coastguard Worker   int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
179*77c1e3ccSAndroid Build Coastguard Worker   int is_newmv, is_zeromv, is_refmv;
180*77c1e3ccSAndroid Build Coastguard Worker   is_newmv = aom_read_symbol(r, ec_ctx->newmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
181*77c1e3ccSAndroid Build Coastguard Worker   if (is_newmv) return NEWMV;
182*77c1e3ccSAndroid Build Coastguard Worker 
183*77c1e3ccSAndroid Build Coastguard Worker   mode_ctx = (ctx >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
184*77c1e3ccSAndroid Build Coastguard Worker   is_zeromv =
185*77c1e3ccSAndroid Build Coastguard Worker       aom_read_symbol(r, ec_ctx->zeromv_cdf[mode_ctx], 2, ACCT_STR) == 0;
186*77c1e3ccSAndroid Build Coastguard Worker   if (is_zeromv) return GLOBALMV;
187*77c1e3ccSAndroid Build Coastguard Worker 
188*77c1e3ccSAndroid Build Coastguard Worker   mode_ctx = (ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
189*77c1e3ccSAndroid Build Coastguard Worker   is_refmv = aom_read_symbol(r, ec_ctx->refmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
190*77c1e3ccSAndroid Build Coastguard Worker   if (is_refmv)
191*77c1e3ccSAndroid Build Coastguard Worker     return NEARESTMV;
192*77c1e3ccSAndroid Build Coastguard Worker   else
193*77c1e3ccSAndroid Build Coastguard Worker     return NEARMV;
194*77c1e3ccSAndroid Build Coastguard Worker }
195*77c1e3ccSAndroid Build Coastguard Worker 
read_drl_idx(FRAME_CONTEXT * ec_ctx,DecoderCodingBlock * dcb,MB_MODE_INFO * mbmi,aom_reader * r)196*77c1e3ccSAndroid Build Coastguard Worker static void read_drl_idx(FRAME_CONTEXT *ec_ctx, DecoderCodingBlock *dcb,
197*77c1e3ccSAndroid Build Coastguard Worker                          MB_MODE_INFO *mbmi, aom_reader *r) {
198*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &dcb->xd;
199*77c1e3ccSAndroid Build Coastguard Worker   uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
200*77c1e3ccSAndroid Build Coastguard Worker   mbmi->ref_mv_idx = 0;
201*77c1e3ccSAndroid Build Coastguard Worker   if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV) {
202*77c1e3ccSAndroid Build Coastguard Worker     for (int idx = 0; idx < 2; ++idx) {
203*77c1e3ccSAndroid Build Coastguard Worker       if (dcb->ref_mv_count[ref_frame_type] > idx + 1) {
204*77c1e3ccSAndroid Build Coastguard Worker         uint8_t drl_ctx = av1_drl_ctx(xd->weight[ref_frame_type], idx);
205*77c1e3ccSAndroid Build Coastguard Worker         int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
206*77c1e3ccSAndroid Build Coastguard Worker         mbmi->ref_mv_idx = idx + drl_idx;
207*77c1e3ccSAndroid Build Coastguard Worker         if (!drl_idx) return;
208*77c1e3ccSAndroid Build Coastguard Worker       }
209*77c1e3ccSAndroid Build Coastguard Worker     }
210*77c1e3ccSAndroid Build Coastguard Worker   }
211*77c1e3ccSAndroid Build Coastguard Worker   if (have_nearmv_in_inter_mode(mbmi->mode)) {
212*77c1e3ccSAndroid Build Coastguard Worker     // Offset the NEARESTMV mode.
213*77c1e3ccSAndroid Build Coastguard Worker     // TODO(jingning): Unify the two syntax decoding loops after the NEARESTMV
214*77c1e3ccSAndroid Build Coastguard Worker     // mode is factored in.
215*77c1e3ccSAndroid Build Coastguard Worker     for (int idx = 1; idx < 3; ++idx) {
216*77c1e3ccSAndroid Build Coastguard Worker       if (dcb->ref_mv_count[ref_frame_type] > idx + 1) {
217*77c1e3ccSAndroid Build Coastguard Worker         uint8_t drl_ctx = av1_drl_ctx(xd->weight[ref_frame_type], idx);
218*77c1e3ccSAndroid Build Coastguard Worker         int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
219*77c1e3ccSAndroid Build Coastguard Worker         mbmi->ref_mv_idx = idx + drl_idx - 1;
220*77c1e3ccSAndroid Build Coastguard Worker         if (!drl_idx) return;
221*77c1e3ccSAndroid Build Coastguard Worker       }
222*77c1e3ccSAndroid Build Coastguard Worker     }
223*77c1e3ccSAndroid Build Coastguard Worker   }
224*77c1e3ccSAndroid Build Coastguard Worker }
225*77c1e3ccSAndroid Build Coastguard Worker 
read_motion_mode(AV1_COMMON * cm,MACROBLOCKD * xd,MB_MODE_INFO * mbmi,aom_reader * r)226*77c1e3ccSAndroid Build Coastguard Worker static MOTION_MODE read_motion_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
227*77c1e3ccSAndroid Build Coastguard Worker                                     MB_MODE_INFO *mbmi, aom_reader *r) {
228*77c1e3ccSAndroid Build Coastguard Worker   if (cm->features.switchable_motion_mode == 0) return SIMPLE_TRANSLATION;
229*77c1e3ccSAndroid Build Coastguard Worker   if (mbmi->skip_mode) return SIMPLE_TRANSLATION;
230*77c1e3ccSAndroid Build Coastguard Worker 
231*77c1e3ccSAndroid Build Coastguard Worker   const MOTION_MODE last_motion_mode_allowed = motion_mode_allowed(
232*77c1e3ccSAndroid Build Coastguard Worker       xd->global_motion, xd, mbmi, cm->features.allow_warped_motion);
233*77c1e3ccSAndroid Build Coastguard Worker   int motion_mode;
234*77c1e3ccSAndroid Build Coastguard Worker 
235*77c1e3ccSAndroid Build Coastguard Worker   if (last_motion_mode_allowed == SIMPLE_TRANSLATION) return SIMPLE_TRANSLATION;
236*77c1e3ccSAndroid Build Coastguard Worker 
237*77c1e3ccSAndroid Build Coastguard Worker   if (last_motion_mode_allowed == OBMC_CAUSAL) {
238*77c1e3ccSAndroid Build Coastguard Worker     motion_mode =
239*77c1e3ccSAndroid Build Coastguard Worker         aom_read_symbol(r, xd->tile_ctx->obmc_cdf[mbmi->bsize], 2, ACCT_STR);
240*77c1e3ccSAndroid Build Coastguard Worker     return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
241*77c1e3ccSAndroid Build Coastguard Worker   } else {
242*77c1e3ccSAndroid Build Coastguard Worker     motion_mode = aom_read_symbol(r, xd->tile_ctx->motion_mode_cdf[mbmi->bsize],
243*77c1e3ccSAndroid Build Coastguard Worker                                   MOTION_MODES, ACCT_STR);
244*77c1e3ccSAndroid Build Coastguard Worker     return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
245*77c1e3ccSAndroid Build Coastguard Worker   }
246*77c1e3ccSAndroid Build Coastguard Worker }
247*77c1e3ccSAndroid Build Coastguard Worker 
read_inter_compound_mode(MACROBLOCKD * xd,aom_reader * r,int16_t ctx)248*77c1e3ccSAndroid Build Coastguard Worker static PREDICTION_MODE read_inter_compound_mode(MACROBLOCKD *xd, aom_reader *r,
249*77c1e3ccSAndroid Build Coastguard Worker                                                 int16_t ctx) {
250*77c1e3ccSAndroid Build Coastguard Worker   const int mode =
251*77c1e3ccSAndroid Build Coastguard Worker       aom_read_symbol(r, xd->tile_ctx->inter_compound_mode_cdf[ctx],
252*77c1e3ccSAndroid Build Coastguard Worker                       INTER_COMPOUND_MODES, ACCT_STR);
253*77c1e3ccSAndroid Build Coastguard Worker   assert(is_inter_compound_mode(NEAREST_NEARESTMV + mode));
254*77c1e3ccSAndroid Build Coastguard Worker   return NEAREST_NEARESTMV + mode;
255*77c1e3ccSAndroid Build Coastguard Worker }
256*77c1e3ccSAndroid Build Coastguard Worker 
av1_neg_deinterleave(int diff,int ref,int max)257*77c1e3ccSAndroid Build Coastguard Worker int av1_neg_deinterleave(int diff, int ref, int max) {
258*77c1e3ccSAndroid Build Coastguard Worker   if (!ref) return diff;
259*77c1e3ccSAndroid Build Coastguard Worker   if (ref >= (max - 1)) return max - diff - 1;
260*77c1e3ccSAndroid Build Coastguard Worker   if (2 * ref < max) {
261*77c1e3ccSAndroid Build Coastguard Worker     if (diff <= 2 * ref) {
262*77c1e3ccSAndroid Build Coastguard Worker       if (diff & 1)
263*77c1e3ccSAndroid Build Coastguard Worker         return ref + ((diff + 1) >> 1);
264*77c1e3ccSAndroid Build Coastguard Worker       else
265*77c1e3ccSAndroid Build Coastguard Worker         return ref - (diff >> 1);
266*77c1e3ccSAndroid Build Coastguard Worker     }
267*77c1e3ccSAndroid Build Coastguard Worker     return diff;
268*77c1e3ccSAndroid Build Coastguard Worker   } else {
269*77c1e3ccSAndroid Build Coastguard Worker     if (diff <= 2 * (max - ref - 1)) {
270*77c1e3ccSAndroid Build Coastguard Worker       if (diff & 1)
271*77c1e3ccSAndroid Build Coastguard Worker         return ref + ((diff + 1) >> 1);
272*77c1e3ccSAndroid Build Coastguard Worker       else
273*77c1e3ccSAndroid Build Coastguard Worker         return ref - (diff >> 1);
274*77c1e3ccSAndroid Build Coastguard Worker     }
275*77c1e3ccSAndroid Build Coastguard Worker     return max - (diff + 1);
276*77c1e3ccSAndroid Build Coastguard Worker   }
277*77c1e3ccSAndroid Build Coastguard Worker }
278*77c1e3ccSAndroid Build Coastguard Worker 
read_segment_id(AV1_COMMON * const cm,const MACROBLOCKD * const xd,aom_reader * r,int skip)279*77c1e3ccSAndroid Build Coastguard Worker static int read_segment_id(AV1_COMMON *const cm, const MACROBLOCKD *const xd,
280*77c1e3ccSAndroid Build Coastguard Worker                            aom_reader *r, int skip) {
281*77c1e3ccSAndroid Build Coastguard Worker   int cdf_num;
282*77c1e3ccSAndroid Build Coastguard Worker   const uint8_t pred = av1_get_spatial_seg_pred(cm, xd, &cdf_num, 0);
283*77c1e3ccSAndroid Build Coastguard Worker   if (skip) return pred;
284*77c1e3ccSAndroid Build Coastguard Worker 
285*77c1e3ccSAndroid Build Coastguard Worker   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
286*77c1e3ccSAndroid Build Coastguard Worker   struct segmentation *const seg = &cm->seg;
287*77c1e3ccSAndroid Build Coastguard Worker   struct segmentation_probs *const segp = &ec_ctx->seg;
288*77c1e3ccSAndroid Build Coastguard Worker   aom_cdf_prob *pred_cdf = segp->spatial_pred_seg_cdf[cdf_num];
289*77c1e3ccSAndroid Build Coastguard Worker   const int coded_id = aom_read_symbol(r, pred_cdf, MAX_SEGMENTS, ACCT_STR);
290*77c1e3ccSAndroid Build Coastguard Worker   const int segment_id =
291*77c1e3ccSAndroid Build Coastguard Worker       av1_neg_deinterleave(coded_id, pred, seg->last_active_segid + 1);
292*77c1e3ccSAndroid Build Coastguard Worker 
293*77c1e3ccSAndroid Build Coastguard Worker   if (segment_id < 0 || segment_id > seg->last_active_segid) {
294*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
295*77c1e3ccSAndroid Build Coastguard Worker                        "Corrupted segment_ids");
296*77c1e3ccSAndroid Build Coastguard Worker   }
297*77c1e3ccSAndroid Build Coastguard Worker   return segment_id;
298*77c1e3ccSAndroid Build Coastguard Worker }
299*77c1e3ccSAndroid Build Coastguard Worker 
dec_get_segment_id(const AV1_COMMON * cm,const uint8_t * segment_ids,int mi_offset,int x_mis,int y_mis)300*77c1e3ccSAndroid Build Coastguard Worker static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
301*77c1e3ccSAndroid Build Coastguard Worker                               int mi_offset, int x_mis, int y_mis) {
302*77c1e3ccSAndroid Build Coastguard Worker   int segment_id = INT_MAX;
303*77c1e3ccSAndroid Build Coastguard Worker 
304*77c1e3ccSAndroid Build Coastguard Worker   for (int y = 0; y < y_mis; y++)
305*77c1e3ccSAndroid Build Coastguard Worker     for (int x = 0; x < x_mis; x++)
306*77c1e3ccSAndroid Build Coastguard Worker       segment_id = AOMMIN(
307*77c1e3ccSAndroid Build Coastguard Worker           segment_id, segment_ids[mi_offset + y * cm->mi_params.mi_cols + x]);
308*77c1e3ccSAndroid Build Coastguard Worker 
309*77c1e3ccSAndroid Build Coastguard Worker   assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
310*77c1e3ccSAndroid Build Coastguard Worker   return segment_id;
311*77c1e3ccSAndroid Build Coastguard Worker }
312*77c1e3ccSAndroid Build Coastguard Worker 
read_intra_segment_id(AV1_COMMON * const cm,const MACROBLOCKD * const xd,BLOCK_SIZE bsize,aom_reader * r,int skip)313*77c1e3ccSAndroid Build Coastguard Worker static int read_intra_segment_id(AV1_COMMON *const cm,
314*77c1e3ccSAndroid Build Coastguard Worker                                  const MACROBLOCKD *const xd, BLOCK_SIZE bsize,
315*77c1e3ccSAndroid Build Coastguard Worker                                  aom_reader *r, int skip) {
316*77c1e3ccSAndroid Build Coastguard Worker   struct segmentation *const seg = &cm->seg;
317*77c1e3ccSAndroid Build Coastguard Worker   if (!seg->enabled) return 0;  // Default for disabled segmentation
318*77c1e3ccSAndroid Build Coastguard Worker   assert(seg->update_map && !seg->temporal_update);
319*77c1e3ccSAndroid Build Coastguard Worker 
320*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cm->mi_params;
321*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row = xd->mi_row;
322*77c1e3ccSAndroid Build Coastguard Worker   const int mi_col = xd->mi_col;
323*77c1e3ccSAndroid Build Coastguard Worker   const int mi_stride = cm->mi_params.mi_cols;
324*77c1e3ccSAndroid Build Coastguard Worker   const int mi_offset = mi_row * mi_stride + mi_col;
325*77c1e3ccSAndroid Build Coastguard Worker   const int bw = mi_size_wide[bsize];
326*77c1e3ccSAndroid Build Coastguard Worker   const int bh = mi_size_high[bsize];
327*77c1e3ccSAndroid Build Coastguard Worker   const int x_mis = AOMMIN(mi_params->mi_cols - mi_col, bw);
328*77c1e3ccSAndroid Build Coastguard Worker   const int y_mis = AOMMIN(mi_params->mi_rows - mi_row, bh);
329*77c1e3ccSAndroid Build Coastguard Worker   const int segment_id = read_segment_id(cm, xd, r, skip);
330*77c1e3ccSAndroid Build Coastguard Worker   set_segment_id(cm->cur_frame->seg_map, mi_offset, x_mis, y_mis, mi_stride,
331*77c1e3ccSAndroid Build Coastguard Worker                  segment_id);
332*77c1e3ccSAndroid Build Coastguard Worker   return segment_id;
333*77c1e3ccSAndroid Build Coastguard Worker }
334*77c1e3ccSAndroid Build Coastguard Worker 
copy_segment_id(const CommonModeInfoParams * const mi_params,const uint8_t * last_segment_ids,uint8_t * current_segment_ids,int mi_offset,int x_mis,int y_mis)335*77c1e3ccSAndroid Build Coastguard Worker static void copy_segment_id(const CommonModeInfoParams *const mi_params,
336*77c1e3ccSAndroid Build Coastguard Worker                             const uint8_t *last_segment_ids,
337*77c1e3ccSAndroid Build Coastguard Worker                             uint8_t *current_segment_ids, int mi_offset,
338*77c1e3ccSAndroid Build Coastguard Worker                             int x_mis, int y_mis) {
339*77c1e3ccSAndroid Build Coastguard Worker   const int stride = mi_params->mi_cols;
340*77c1e3ccSAndroid Build Coastguard Worker   if (last_segment_ids) {
341*77c1e3ccSAndroid Build Coastguard Worker     assert(last_segment_ids != current_segment_ids);
342*77c1e3ccSAndroid Build Coastguard Worker     for (int y = 0; y < y_mis; y++) {
343*77c1e3ccSAndroid Build Coastguard Worker       memcpy(&current_segment_ids[mi_offset + y * stride],
344*77c1e3ccSAndroid Build Coastguard Worker              &last_segment_ids[mi_offset + y * stride],
345*77c1e3ccSAndroid Build Coastguard Worker              sizeof(current_segment_ids[0]) * x_mis);
346*77c1e3ccSAndroid Build Coastguard Worker     }
347*77c1e3ccSAndroid Build Coastguard Worker   } else {
348*77c1e3ccSAndroid Build Coastguard Worker     for (int y = 0; y < y_mis; y++) {
349*77c1e3ccSAndroid Build Coastguard Worker       memset(&current_segment_ids[mi_offset + y * stride], 0,
350*77c1e3ccSAndroid Build Coastguard Worker              sizeof(current_segment_ids[0]) * x_mis);
351*77c1e3ccSAndroid Build Coastguard Worker     }
352*77c1e3ccSAndroid Build Coastguard Worker   }
353*77c1e3ccSAndroid Build Coastguard Worker }
354*77c1e3ccSAndroid Build Coastguard Worker 
get_predicted_segment_id(AV1_COMMON * const cm,int mi_offset,int x_mis,int y_mis)355*77c1e3ccSAndroid Build Coastguard Worker static int get_predicted_segment_id(AV1_COMMON *const cm, int mi_offset,
356*77c1e3ccSAndroid Build Coastguard Worker                                     int x_mis, int y_mis) {
357*77c1e3ccSAndroid Build Coastguard Worker   return cm->last_frame_seg_map ? dec_get_segment_id(cm, cm->last_frame_seg_map,
358*77c1e3ccSAndroid Build Coastguard Worker                                                      mi_offset, x_mis, y_mis)
359*77c1e3ccSAndroid Build Coastguard Worker                                 : 0;
360*77c1e3ccSAndroid Build Coastguard Worker }
361*77c1e3ccSAndroid Build Coastguard Worker 
read_inter_segment_id(AV1_COMMON * const cm,MACROBLOCKD * const xd,int preskip,aom_reader * r)362*77c1e3ccSAndroid Build Coastguard Worker static int read_inter_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
363*77c1e3ccSAndroid Build Coastguard Worker                                  int preskip, aom_reader *r) {
364*77c1e3ccSAndroid Build Coastguard Worker   struct segmentation *const seg = &cm->seg;
365*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cm->mi_params;
366*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *const mbmi = xd->mi[0];
367*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row = xd->mi_row;
368*77c1e3ccSAndroid Build Coastguard Worker   const int mi_col = xd->mi_col;
369*77c1e3ccSAndroid Build Coastguard Worker   const int mi_offset = mi_row * mi_params->mi_cols + mi_col;
370*77c1e3ccSAndroid Build Coastguard Worker   const int bw = mi_size_wide[mbmi->bsize];
371*77c1e3ccSAndroid Build Coastguard Worker   const int bh = mi_size_high[mbmi->bsize];
372*77c1e3ccSAndroid Build Coastguard Worker 
373*77c1e3ccSAndroid Build Coastguard Worker   // TODO(slavarnway): move x_mis, y_mis into xd ?????
374*77c1e3ccSAndroid Build Coastguard Worker   const int x_mis = AOMMIN(mi_params->mi_cols - mi_col, bw);
375*77c1e3ccSAndroid Build Coastguard Worker   const int y_mis = AOMMIN(mi_params->mi_rows - mi_row, bh);
376*77c1e3ccSAndroid Build Coastguard Worker 
377*77c1e3ccSAndroid Build Coastguard Worker   if (!seg->enabled) return 0;  // Default for disabled segmentation
378*77c1e3ccSAndroid Build Coastguard Worker 
379*77c1e3ccSAndroid Build Coastguard Worker   if (!seg->update_map) {
380*77c1e3ccSAndroid Build Coastguard Worker     copy_segment_id(mi_params, cm->last_frame_seg_map, cm->cur_frame->seg_map,
381*77c1e3ccSAndroid Build Coastguard Worker                     mi_offset, x_mis, y_mis);
382*77c1e3ccSAndroid Build Coastguard Worker     return get_predicted_segment_id(cm, mi_offset, x_mis, y_mis);
383*77c1e3ccSAndroid Build Coastguard Worker   }
384*77c1e3ccSAndroid Build Coastguard Worker 
385*77c1e3ccSAndroid Build Coastguard Worker   uint8_t segment_id;
386*77c1e3ccSAndroid Build Coastguard Worker   const int mi_stride = cm->mi_params.mi_cols;
387*77c1e3ccSAndroid Build Coastguard Worker   if (preskip) {
388*77c1e3ccSAndroid Build Coastguard Worker     if (!seg->segid_preskip) return 0;
389*77c1e3ccSAndroid Build Coastguard Worker   } else {
390*77c1e3ccSAndroid Build Coastguard Worker     if (mbmi->skip_txfm) {
391*77c1e3ccSAndroid Build Coastguard Worker       if (seg->temporal_update) {
392*77c1e3ccSAndroid Build Coastguard Worker         mbmi->seg_id_predicted = 0;
393*77c1e3ccSAndroid Build Coastguard Worker       }
394*77c1e3ccSAndroid Build Coastguard Worker       segment_id = read_segment_id(cm, xd, r, 1);
395*77c1e3ccSAndroid Build Coastguard Worker       set_segment_id(cm->cur_frame->seg_map, mi_offset, x_mis, y_mis, mi_stride,
396*77c1e3ccSAndroid Build Coastguard Worker                      segment_id);
397*77c1e3ccSAndroid Build Coastguard Worker       return segment_id;
398*77c1e3ccSAndroid Build Coastguard Worker     }
399*77c1e3ccSAndroid Build Coastguard Worker   }
400*77c1e3ccSAndroid Build Coastguard Worker 
401*77c1e3ccSAndroid Build Coastguard Worker   if (seg->temporal_update) {
402*77c1e3ccSAndroid Build Coastguard Worker     const uint8_t ctx = av1_get_pred_context_seg_id(xd);
403*77c1e3ccSAndroid Build Coastguard Worker     FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
404*77c1e3ccSAndroid Build Coastguard Worker     struct segmentation_probs *const segp = &ec_ctx->seg;
405*77c1e3ccSAndroid Build Coastguard Worker     aom_cdf_prob *pred_cdf = segp->pred_cdf[ctx];
406*77c1e3ccSAndroid Build Coastguard Worker     mbmi->seg_id_predicted = aom_read_symbol(r, pred_cdf, 2, ACCT_STR);
407*77c1e3ccSAndroid Build Coastguard Worker     if (mbmi->seg_id_predicted) {
408*77c1e3ccSAndroid Build Coastguard Worker       segment_id = get_predicted_segment_id(cm, mi_offset, x_mis, y_mis);
409*77c1e3ccSAndroid Build Coastguard Worker     } else {
410*77c1e3ccSAndroid Build Coastguard Worker       segment_id = read_segment_id(cm, xd, r, 0);
411*77c1e3ccSAndroid Build Coastguard Worker     }
412*77c1e3ccSAndroid Build Coastguard Worker   } else {
413*77c1e3ccSAndroid Build Coastguard Worker     segment_id = read_segment_id(cm, xd, r, 0);
414*77c1e3ccSAndroid Build Coastguard Worker   }
415*77c1e3ccSAndroid Build Coastguard Worker   set_segment_id(cm->cur_frame->seg_map, mi_offset, x_mis, y_mis, mi_stride,
416*77c1e3ccSAndroid Build Coastguard Worker                  segment_id);
417*77c1e3ccSAndroid Build Coastguard Worker   return segment_id;
418*77c1e3ccSAndroid Build Coastguard Worker }
419*77c1e3ccSAndroid Build Coastguard Worker 
read_skip_mode(AV1_COMMON * cm,const MACROBLOCKD * xd,int segment_id,aom_reader * r)420*77c1e3ccSAndroid Build Coastguard Worker static int read_skip_mode(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
421*77c1e3ccSAndroid Build Coastguard Worker                           aom_reader *r) {
422*77c1e3ccSAndroid Build Coastguard Worker   if (!cm->current_frame.skip_mode_info.skip_mode_flag) return 0;
423*77c1e3ccSAndroid Build Coastguard Worker 
424*77c1e3ccSAndroid Build Coastguard Worker   if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
425*77c1e3ccSAndroid Build Coastguard Worker     return 0;
426*77c1e3ccSAndroid Build Coastguard Worker   }
427*77c1e3ccSAndroid Build Coastguard Worker 
428*77c1e3ccSAndroid Build Coastguard Worker   if (!is_comp_ref_allowed(xd->mi[0]->bsize)) return 0;
429*77c1e3ccSAndroid Build Coastguard Worker 
430*77c1e3ccSAndroid Build Coastguard Worker   if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME) ||
431*77c1e3ccSAndroid Build Coastguard Worker       segfeature_active(&cm->seg, segment_id, SEG_LVL_GLOBALMV)) {
432*77c1e3ccSAndroid Build Coastguard Worker     // These features imply single-reference mode, while skip mode implies
433*77c1e3ccSAndroid Build Coastguard Worker     // compound reference. Hence, the two are mutually exclusive.
434*77c1e3ccSAndroid Build Coastguard Worker     // In other words, skip_mode is implicitly 0 here.
435*77c1e3ccSAndroid Build Coastguard Worker     return 0;
436*77c1e3ccSAndroid Build Coastguard Worker   }
437*77c1e3ccSAndroid Build Coastguard Worker 
438*77c1e3ccSAndroid Build Coastguard Worker   const int ctx = av1_get_skip_mode_context(xd);
439*77c1e3ccSAndroid Build Coastguard Worker   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
440*77c1e3ccSAndroid Build Coastguard Worker   const int skip_mode =
441*77c1e3ccSAndroid Build Coastguard Worker       aom_read_symbol(r, ec_ctx->skip_mode_cdfs[ctx], 2, ACCT_STR);
442*77c1e3ccSAndroid Build Coastguard Worker   return skip_mode;
443*77c1e3ccSAndroid Build Coastguard Worker }
444*77c1e3ccSAndroid Build Coastguard Worker 
read_skip_txfm(AV1_COMMON * cm,const MACROBLOCKD * xd,int segment_id,aom_reader * r)445*77c1e3ccSAndroid Build Coastguard Worker static int read_skip_txfm(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
446*77c1e3ccSAndroid Build Coastguard Worker                           aom_reader *r) {
447*77c1e3ccSAndroid Build Coastguard Worker   if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
448*77c1e3ccSAndroid Build Coastguard Worker     return 1;
449*77c1e3ccSAndroid Build Coastguard Worker   } else {
450*77c1e3ccSAndroid Build Coastguard Worker     const int ctx = av1_get_skip_txfm_context(xd);
451*77c1e3ccSAndroid Build Coastguard Worker     FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
452*77c1e3ccSAndroid Build Coastguard Worker     const int skip_txfm =
453*77c1e3ccSAndroid Build Coastguard Worker         aom_read_symbol(r, ec_ctx->skip_txfm_cdfs[ctx], 2, ACCT_STR);
454*77c1e3ccSAndroid Build Coastguard Worker     return skip_txfm;
455*77c1e3ccSAndroid Build Coastguard Worker   }
456*77c1e3ccSAndroid Build Coastguard Worker }
457*77c1e3ccSAndroid Build Coastguard Worker 
458*77c1e3ccSAndroid Build Coastguard Worker // Merge the sorted list of cached colors(cached_colors[0...n_cached_colors-1])
459*77c1e3ccSAndroid Build Coastguard Worker // and the sorted list of transmitted colors(colors[n_cached_colors...n-1]) into
460*77c1e3ccSAndroid Build Coastguard Worker // one single sorted list(colors[...]).
merge_colors(uint16_t * colors,uint16_t * cached_colors,int n_colors,int n_cached_colors)461*77c1e3ccSAndroid Build Coastguard Worker static void merge_colors(uint16_t *colors, uint16_t *cached_colors,
462*77c1e3ccSAndroid Build Coastguard Worker                          int n_colors, int n_cached_colors) {
463*77c1e3ccSAndroid Build Coastguard Worker   if (n_cached_colors == 0) return;
464*77c1e3ccSAndroid Build Coastguard Worker   int cache_idx = 0, trans_idx = n_cached_colors;
465*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < n_colors; ++i) {
466*77c1e3ccSAndroid Build Coastguard Worker     if (cache_idx < n_cached_colors &&
467*77c1e3ccSAndroid Build Coastguard Worker         (trans_idx >= n_colors ||
468*77c1e3ccSAndroid Build Coastguard Worker          cached_colors[cache_idx] <= colors[trans_idx])) {
469*77c1e3ccSAndroid Build Coastguard Worker       colors[i] = cached_colors[cache_idx++];
470*77c1e3ccSAndroid Build Coastguard Worker     } else {
471*77c1e3ccSAndroid Build Coastguard Worker       assert(trans_idx < n_colors);
472*77c1e3ccSAndroid Build Coastguard Worker       colors[i] = colors[trans_idx++];
473*77c1e3ccSAndroid Build Coastguard Worker     }
474*77c1e3ccSAndroid Build Coastguard Worker   }
475*77c1e3ccSAndroid Build Coastguard Worker }
476*77c1e3ccSAndroid Build Coastguard Worker 
read_palette_colors_y(MACROBLOCKD * const xd,int bit_depth,PALETTE_MODE_INFO * const pmi,aom_reader * r)477*77c1e3ccSAndroid Build Coastguard Worker static void read_palette_colors_y(MACROBLOCKD *const xd, int bit_depth,
478*77c1e3ccSAndroid Build Coastguard Worker                                   PALETTE_MODE_INFO *const pmi, aom_reader *r) {
479*77c1e3ccSAndroid Build Coastguard Worker   uint16_t color_cache[2 * PALETTE_MAX_SIZE];
480*77c1e3ccSAndroid Build Coastguard Worker   uint16_t cached_colors[PALETTE_MAX_SIZE];
481*77c1e3ccSAndroid Build Coastguard Worker   const int n_cache = av1_get_palette_cache(xd, 0, color_cache);
482*77c1e3ccSAndroid Build Coastguard Worker   const int n = pmi->palette_size[0];
483*77c1e3ccSAndroid Build Coastguard Worker   int idx = 0;
484*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < n_cache && idx < n; ++i)
485*77c1e3ccSAndroid Build Coastguard Worker     if (aom_read_bit(r, ACCT_STR)) cached_colors[idx++] = color_cache[i];
486*77c1e3ccSAndroid Build Coastguard Worker   if (idx < n) {
487*77c1e3ccSAndroid Build Coastguard Worker     const int n_cached_colors = idx;
488*77c1e3ccSAndroid Build Coastguard Worker     pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
489*77c1e3ccSAndroid Build Coastguard Worker     if (idx < n) {
490*77c1e3ccSAndroid Build Coastguard Worker       const int min_bits = bit_depth - 3;
491*77c1e3ccSAndroid Build Coastguard Worker       int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
492*77c1e3ccSAndroid Build Coastguard Worker       int range = (1 << bit_depth) - pmi->palette_colors[idx - 1] - 1;
493*77c1e3ccSAndroid Build Coastguard Worker       for (; idx < n; ++idx) {
494*77c1e3ccSAndroid Build Coastguard Worker         assert(range >= 0);
495*77c1e3ccSAndroid Build Coastguard Worker         const int delta = aom_read_literal(r, bits, ACCT_STR) + 1;
496*77c1e3ccSAndroid Build Coastguard Worker         pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
497*77c1e3ccSAndroid Build Coastguard Worker                                          0, (1 << bit_depth) - 1);
498*77c1e3ccSAndroid Build Coastguard Worker         range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
499*77c1e3ccSAndroid Build Coastguard Worker         bits = AOMMIN(bits, av1_ceil_log2(range));
500*77c1e3ccSAndroid Build Coastguard Worker       }
501*77c1e3ccSAndroid Build Coastguard Worker     }
502*77c1e3ccSAndroid Build Coastguard Worker     merge_colors(pmi->palette_colors, cached_colors, n, n_cached_colors);
503*77c1e3ccSAndroid Build Coastguard Worker   } else {
504*77c1e3ccSAndroid Build Coastguard Worker     memcpy(pmi->palette_colors, cached_colors, n * sizeof(cached_colors[0]));
505*77c1e3ccSAndroid Build Coastguard Worker   }
506*77c1e3ccSAndroid Build Coastguard Worker }
507*77c1e3ccSAndroid Build Coastguard Worker 
read_palette_colors_uv(MACROBLOCKD * const xd,int bit_depth,PALETTE_MODE_INFO * const pmi,aom_reader * r)508*77c1e3ccSAndroid Build Coastguard Worker static void read_palette_colors_uv(MACROBLOCKD *const xd, int bit_depth,
509*77c1e3ccSAndroid Build Coastguard Worker                                    PALETTE_MODE_INFO *const pmi,
510*77c1e3ccSAndroid Build Coastguard Worker                                    aom_reader *r) {
511*77c1e3ccSAndroid Build Coastguard Worker   const int n = pmi->palette_size[1];
512*77c1e3ccSAndroid Build Coastguard Worker   // U channel colors.
513*77c1e3ccSAndroid Build Coastguard Worker   uint16_t color_cache[2 * PALETTE_MAX_SIZE];
514*77c1e3ccSAndroid Build Coastguard Worker   uint16_t cached_colors[PALETTE_MAX_SIZE];
515*77c1e3ccSAndroid Build Coastguard Worker   const int n_cache = av1_get_palette_cache(xd, 1, color_cache);
516*77c1e3ccSAndroid Build Coastguard Worker   int idx = 0;
517*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < n_cache && idx < n; ++i)
518*77c1e3ccSAndroid Build Coastguard Worker     if (aom_read_bit(r, ACCT_STR)) cached_colors[idx++] = color_cache[i];
519*77c1e3ccSAndroid Build Coastguard Worker   if (idx < n) {
520*77c1e3ccSAndroid Build Coastguard Worker     const int n_cached_colors = idx;
521*77c1e3ccSAndroid Build Coastguard Worker     idx += PALETTE_MAX_SIZE;
522*77c1e3ccSAndroid Build Coastguard Worker     pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
523*77c1e3ccSAndroid Build Coastguard Worker     if (idx < PALETTE_MAX_SIZE + n) {
524*77c1e3ccSAndroid Build Coastguard Worker       const int min_bits = bit_depth - 3;
525*77c1e3ccSAndroid Build Coastguard Worker       int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
526*77c1e3ccSAndroid Build Coastguard Worker       int range = (1 << bit_depth) - pmi->palette_colors[idx - 1];
527*77c1e3ccSAndroid Build Coastguard Worker       for (; idx < PALETTE_MAX_SIZE + n; ++idx) {
528*77c1e3ccSAndroid Build Coastguard Worker         assert(range >= 0);
529*77c1e3ccSAndroid Build Coastguard Worker         const int delta = aom_read_literal(r, bits, ACCT_STR);
530*77c1e3ccSAndroid Build Coastguard Worker         pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
531*77c1e3ccSAndroid Build Coastguard Worker                                          0, (1 << bit_depth) - 1);
532*77c1e3ccSAndroid Build Coastguard Worker         range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
533*77c1e3ccSAndroid Build Coastguard Worker         bits = AOMMIN(bits, av1_ceil_log2(range));
534*77c1e3ccSAndroid Build Coastguard Worker       }
535*77c1e3ccSAndroid Build Coastguard Worker     }
536*77c1e3ccSAndroid Build Coastguard Worker     merge_colors(pmi->palette_colors + PALETTE_MAX_SIZE, cached_colors, n,
537*77c1e3ccSAndroid Build Coastguard Worker                  n_cached_colors);
538*77c1e3ccSAndroid Build Coastguard Worker   } else {
539*77c1e3ccSAndroid Build Coastguard Worker     memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, cached_colors,
540*77c1e3ccSAndroid Build Coastguard Worker            n * sizeof(cached_colors[0]));
541*77c1e3ccSAndroid Build Coastguard Worker   }
542*77c1e3ccSAndroid Build Coastguard Worker 
543*77c1e3ccSAndroid Build Coastguard Worker   // V channel colors.
544*77c1e3ccSAndroid Build Coastguard Worker   if (aom_read_bit(r, ACCT_STR)) {  // Delta encoding.
545*77c1e3ccSAndroid Build Coastguard Worker     const int min_bits_v = bit_depth - 4;
546*77c1e3ccSAndroid Build Coastguard Worker     const int max_val = 1 << bit_depth;
547*77c1e3ccSAndroid Build Coastguard Worker     int bits = min_bits_v + aom_read_literal(r, 2, ACCT_STR);
548*77c1e3ccSAndroid Build Coastguard Worker     pmi->palette_colors[2 * PALETTE_MAX_SIZE] =
549*77c1e3ccSAndroid Build Coastguard Worker         aom_read_literal(r, bit_depth, ACCT_STR);
550*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 1; i < n; ++i) {
551*77c1e3ccSAndroid Build Coastguard Worker       int delta = aom_read_literal(r, bits, ACCT_STR);
552*77c1e3ccSAndroid Build Coastguard Worker       if (delta && aom_read_bit(r, ACCT_STR)) delta = -delta;
553*77c1e3ccSAndroid Build Coastguard Worker       int val = (int)pmi->palette_colors[2 * PALETTE_MAX_SIZE + i - 1] + delta;
554*77c1e3ccSAndroid Build Coastguard Worker       if (val < 0) val += max_val;
555*77c1e3ccSAndroid Build Coastguard Worker       if (val >= max_val) val -= max_val;
556*77c1e3ccSAndroid Build Coastguard Worker       pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] = val;
557*77c1e3ccSAndroid Build Coastguard Worker     }
558*77c1e3ccSAndroid Build Coastguard Worker   } else {
559*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < n; ++i) {
560*77c1e3ccSAndroid Build Coastguard Worker       pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
561*77c1e3ccSAndroid Build Coastguard Worker           aom_read_literal(r, bit_depth, ACCT_STR);
562*77c1e3ccSAndroid Build Coastguard Worker     }
563*77c1e3ccSAndroid Build Coastguard Worker   }
564*77c1e3ccSAndroid Build Coastguard Worker }
565*77c1e3ccSAndroid Build Coastguard Worker 
read_palette_mode_info(AV1_COMMON * const cm,MACROBLOCKD * const xd,aom_reader * r)566*77c1e3ccSAndroid Build Coastguard Worker static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
567*77c1e3ccSAndroid Build Coastguard Worker                                    aom_reader *r) {
568*77c1e3ccSAndroid Build Coastguard Worker   const int num_planes = av1_num_planes(cm);
569*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *const mbmi = xd->mi[0];
570*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = mbmi->bsize;
571*77c1e3ccSAndroid Build Coastguard Worker   assert(av1_allow_palette(cm->features.allow_screen_content_tools, bsize));
572*77c1e3ccSAndroid Build Coastguard Worker   PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
573*77c1e3ccSAndroid Build Coastguard Worker   const int bsize_ctx = av1_get_palette_bsize_ctx(bsize);
574*77c1e3ccSAndroid Build Coastguard Worker 
575*77c1e3ccSAndroid Build Coastguard Worker   if (mbmi->mode == DC_PRED) {
576*77c1e3ccSAndroid Build Coastguard Worker     const int palette_mode_ctx = av1_get_palette_mode_ctx(xd);
577*77c1e3ccSAndroid Build Coastguard Worker     const int modev = aom_read_symbol(
578*77c1e3ccSAndroid Build Coastguard Worker         r, xd->tile_ctx->palette_y_mode_cdf[bsize_ctx][palette_mode_ctx], 2,
579*77c1e3ccSAndroid Build Coastguard Worker         ACCT_STR);
580*77c1e3ccSAndroid Build Coastguard Worker     if (modev) {
581*77c1e3ccSAndroid Build Coastguard Worker       pmi->palette_size[0] =
582*77c1e3ccSAndroid Build Coastguard Worker           aom_read_symbol(r, xd->tile_ctx->palette_y_size_cdf[bsize_ctx],
583*77c1e3ccSAndroid Build Coastguard Worker                           PALETTE_SIZES, ACCT_STR) +
584*77c1e3ccSAndroid Build Coastguard Worker           2;
585*77c1e3ccSAndroid Build Coastguard Worker       read_palette_colors_y(xd, cm->seq_params->bit_depth, pmi, r);
586*77c1e3ccSAndroid Build Coastguard Worker     }
587*77c1e3ccSAndroid Build Coastguard Worker   }
588*77c1e3ccSAndroid Build Coastguard Worker   if (num_planes > 1 && mbmi->uv_mode == UV_DC_PRED && xd->is_chroma_ref) {
589*77c1e3ccSAndroid Build Coastguard Worker     const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
590*77c1e3ccSAndroid Build Coastguard Worker     const int modev = aom_read_symbol(
591*77c1e3ccSAndroid Build Coastguard Worker         r, xd->tile_ctx->palette_uv_mode_cdf[palette_uv_mode_ctx], 2, ACCT_STR);
592*77c1e3ccSAndroid Build Coastguard Worker     if (modev) {
593*77c1e3ccSAndroid Build Coastguard Worker       pmi->palette_size[1] =
594*77c1e3ccSAndroid Build Coastguard Worker           aom_read_symbol(r, xd->tile_ctx->palette_uv_size_cdf[bsize_ctx],
595*77c1e3ccSAndroid Build Coastguard Worker                           PALETTE_SIZES, ACCT_STR) +
596*77c1e3ccSAndroid Build Coastguard Worker           2;
597*77c1e3ccSAndroid Build Coastguard Worker       read_palette_colors_uv(xd, cm->seq_params->bit_depth, pmi, r);
598*77c1e3ccSAndroid Build Coastguard Worker     }
599*77c1e3ccSAndroid Build Coastguard Worker   }
600*77c1e3ccSAndroid Build Coastguard Worker }
601*77c1e3ccSAndroid Build Coastguard Worker 
read_angle_delta(aom_reader * r,aom_cdf_prob * cdf)602*77c1e3ccSAndroid Build Coastguard Worker static int read_angle_delta(aom_reader *r, aom_cdf_prob *cdf) {
603*77c1e3ccSAndroid Build Coastguard Worker   const int sym = aom_read_symbol(r, cdf, 2 * MAX_ANGLE_DELTA + 1, ACCT_STR);
604*77c1e3ccSAndroid Build Coastguard Worker   return sym - MAX_ANGLE_DELTA;
605*77c1e3ccSAndroid Build Coastguard Worker }
606*77c1e3ccSAndroid Build Coastguard Worker 
read_filter_intra_mode_info(const AV1_COMMON * const cm,MACROBLOCKD * const xd,aom_reader * r)607*77c1e3ccSAndroid Build Coastguard Worker static void read_filter_intra_mode_info(const AV1_COMMON *const cm,
608*77c1e3ccSAndroid Build Coastguard Worker                                         MACROBLOCKD *const xd, aom_reader *r) {
609*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *const mbmi = xd->mi[0];
610*77c1e3ccSAndroid Build Coastguard Worker   FILTER_INTRA_MODE_INFO *filter_intra_mode_info =
611*77c1e3ccSAndroid Build Coastguard Worker       &mbmi->filter_intra_mode_info;
612*77c1e3ccSAndroid Build Coastguard Worker 
613*77c1e3ccSAndroid Build Coastguard Worker   if (av1_filter_intra_allowed(cm, mbmi)) {
614*77c1e3ccSAndroid Build Coastguard Worker     filter_intra_mode_info->use_filter_intra = aom_read_symbol(
615*77c1e3ccSAndroid Build Coastguard Worker         r, xd->tile_ctx->filter_intra_cdfs[mbmi->bsize], 2, ACCT_STR);
616*77c1e3ccSAndroid Build Coastguard Worker     if (filter_intra_mode_info->use_filter_intra) {
617*77c1e3ccSAndroid Build Coastguard Worker       filter_intra_mode_info->filter_intra_mode = aom_read_symbol(
618*77c1e3ccSAndroid Build Coastguard Worker           r, xd->tile_ctx->filter_intra_mode_cdf, FILTER_INTRA_MODES, ACCT_STR);
619*77c1e3ccSAndroid Build Coastguard Worker     }
620*77c1e3ccSAndroid Build Coastguard Worker   } else {
621*77c1e3ccSAndroid Build Coastguard Worker     filter_intra_mode_info->use_filter_intra = 0;
622*77c1e3ccSAndroid Build Coastguard Worker   }
623*77c1e3ccSAndroid Build Coastguard Worker }
624*77c1e3ccSAndroid Build Coastguard Worker 
av1_read_tx_type(const AV1_COMMON * const cm,MACROBLOCKD * xd,int blk_row,int blk_col,TX_SIZE tx_size,aom_reader * r)625*77c1e3ccSAndroid Build Coastguard Worker void av1_read_tx_type(const AV1_COMMON *const cm, MACROBLOCKD *xd, int blk_row,
626*77c1e3ccSAndroid Build Coastguard Worker                       int blk_col, TX_SIZE tx_size, aom_reader *r) {
627*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *mbmi = xd->mi[0];
628*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *tx_type =
629*77c1e3ccSAndroid Build Coastguard Worker       &xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col];
630*77c1e3ccSAndroid Build Coastguard Worker   *tx_type = DCT_DCT;
631*77c1e3ccSAndroid Build Coastguard Worker 
632*77c1e3ccSAndroid Build Coastguard Worker   // No need to read transform type if block is skipped.
633*77c1e3ccSAndroid Build Coastguard Worker   if (mbmi->skip_txfm ||
634*77c1e3ccSAndroid Build Coastguard Worker       segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP))
635*77c1e3ccSAndroid Build Coastguard Worker     return;
636*77c1e3ccSAndroid Build Coastguard Worker 
637*77c1e3ccSAndroid Build Coastguard Worker   // No need to read transform type for lossless mode(qindex==0).
638*77c1e3ccSAndroid Build Coastguard Worker   const int qindex = xd->qindex[mbmi->segment_id];
639*77c1e3ccSAndroid Build Coastguard Worker   if (qindex == 0) return;
640*77c1e3ccSAndroid Build Coastguard Worker 
641*77c1e3ccSAndroid Build Coastguard Worker   const int inter_block = is_inter_block(mbmi);
642*77c1e3ccSAndroid Build Coastguard Worker   if (get_ext_tx_types(tx_size, inter_block, cm->features.reduced_tx_set_used) >
643*77c1e3ccSAndroid Build Coastguard Worker       1) {
644*77c1e3ccSAndroid Build Coastguard Worker     const TxSetType tx_set_type = av1_get_ext_tx_set_type(
645*77c1e3ccSAndroid Build Coastguard Worker         tx_size, inter_block, cm->features.reduced_tx_set_used);
646*77c1e3ccSAndroid Build Coastguard Worker     const int eset =
647*77c1e3ccSAndroid Build Coastguard Worker         get_ext_tx_set(tx_size, inter_block, cm->features.reduced_tx_set_used);
648*77c1e3ccSAndroid Build Coastguard Worker     // eset == 0 should correspond to a set with only DCT_DCT and
649*77c1e3ccSAndroid Build Coastguard Worker     // there is no need to read the tx_type
650*77c1e3ccSAndroid Build Coastguard Worker     assert(eset != 0);
651*77c1e3ccSAndroid Build Coastguard Worker 
652*77c1e3ccSAndroid Build Coastguard Worker     const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
653*77c1e3ccSAndroid Build Coastguard Worker     FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
654*77c1e3ccSAndroid Build Coastguard Worker     if (inter_block) {
655*77c1e3ccSAndroid Build Coastguard Worker       *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
656*77c1e3ccSAndroid Build Coastguard Worker           r, ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
657*77c1e3ccSAndroid Build Coastguard Worker           av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
658*77c1e3ccSAndroid Build Coastguard Worker     } else {
659*77c1e3ccSAndroid Build Coastguard Worker       const PREDICTION_MODE intra_mode =
660*77c1e3ccSAndroid Build Coastguard Worker           mbmi->filter_intra_mode_info.use_filter_intra
661*77c1e3ccSAndroid Build Coastguard Worker               ? fimode_to_intradir[mbmi->filter_intra_mode_info
662*77c1e3ccSAndroid Build Coastguard Worker                                        .filter_intra_mode]
663*77c1e3ccSAndroid Build Coastguard Worker               : mbmi->mode;
664*77c1e3ccSAndroid Build Coastguard Worker       *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
665*77c1e3ccSAndroid Build Coastguard Worker           r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][intra_mode],
666*77c1e3ccSAndroid Build Coastguard Worker           av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
667*77c1e3ccSAndroid Build Coastguard Worker     }
668*77c1e3ccSAndroid Build Coastguard Worker   }
669*77c1e3ccSAndroid Build Coastguard Worker }
670*77c1e3ccSAndroid Build Coastguard Worker 
671*77c1e3ccSAndroid Build Coastguard Worker static inline void read_mv(aom_reader *r, MV *mv, const MV *ref,
672*77c1e3ccSAndroid Build Coastguard Worker                            nmv_context *ctx, MvSubpelPrecision precision);
673*77c1e3ccSAndroid Build Coastguard Worker 
674*77c1e3ccSAndroid Build Coastguard Worker static inline int is_mv_valid(const MV *mv);
675*77c1e3ccSAndroid Build Coastguard Worker 
assign_dv(AV1_COMMON * cm,MACROBLOCKD * xd,int_mv * mv,const int_mv * ref_mv,int mi_row,int mi_col,BLOCK_SIZE bsize,aom_reader * r)676*77c1e3ccSAndroid Build Coastguard Worker static inline int assign_dv(AV1_COMMON *cm, MACROBLOCKD *xd, int_mv *mv,
677*77c1e3ccSAndroid Build Coastguard Worker                             const int_mv *ref_mv, int mi_row, int mi_col,
678*77c1e3ccSAndroid Build Coastguard Worker                             BLOCK_SIZE bsize, aom_reader *r) {
679*77c1e3ccSAndroid Build Coastguard Worker   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
680*77c1e3ccSAndroid Build Coastguard Worker   read_mv(r, &mv->as_mv, &ref_mv->as_mv, &ec_ctx->ndvc, MV_SUBPEL_NONE);
681*77c1e3ccSAndroid Build Coastguard Worker   // DV should not have sub-pel.
682*77c1e3ccSAndroid Build Coastguard Worker   assert((mv->as_mv.col & 7) == 0);
683*77c1e3ccSAndroid Build Coastguard Worker   assert((mv->as_mv.row & 7) == 0);
684*77c1e3ccSAndroid Build Coastguard Worker   mv->as_mv.col = (mv->as_mv.col >> 3) * 8;
685*77c1e3ccSAndroid Build Coastguard Worker   mv->as_mv.row = (mv->as_mv.row >> 3) * 8;
686*77c1e3ccSAndroid Build Coastguard Worker   int valid = is_mv_valid(&mv->as_mv) &&
687*77c1e3ccSAndroid Build Coastguard Worker               av1_is_dv_valid(mv->as_mv, cm, xd, mi_row, mi_col, bsize,
688*77c1e3ccSAndroid Build Coastguard Worker                               cm->seq_params->mib_size_log2);
689*77c1e3ccSAndroid Build Coastguard Worker   return valid;
690*77c1e3ccSAndroid Build Coastguard Worker }
691*77c1e3ccSAndroid Build Coastguard Worker 
read_intrabc_info(AV1_COMMON * const cm,DecoderCodingBlock * dcb,aom_reader * r)692*77c1e3ccSAndroid Build Coastguard Worker static void read_intrabc_info(AV1_COMMON *const cm, DecoderCodingBlock *dcb,
693*77c1e3ccSAndroid Build Coastguard Worker                               aom_reader *r) {
694*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &dcb->xd;
695*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *const mbmi = xd->mi[0];
696*77c1e3ccSAndroid Build Coastguard Worker   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
697*77c1e3ccSAndroid Build Coastguard Worker   mbmi->use_intrabc = aom_read_symbol(r, ec_ctx->intrabc_cdf, 2, ACCT_STR);
698*77c1e3ccSAndroid Build Coastguard Worker   if (mbmi->use_intrabc) {
699*77c1e3ccSAndroid Build Coastguard Worker     BLOCK_SIZE bsize = mbmi->bsize;
700*77c1e3ccSAndroid Build Coastguard Worker     mbmi->mode = DC_PRED;
701*77c1e3ccSAndroid Build Coastguard Worker     mbmi->uv_mode = UV_DC_PRED;
702*77c1e3ccSAndroid Build Coastguard Worker     mbmi->interp_filters = av1_broadcast_interp_filter(BILINEAR);
703*77c1e3ccSAndroid Build Coastguard Worker     mbmi->motion_mode = SIMPLE_TRANSLATION;
704*77c1e3ccSAndroid Build Coastguard Worker 
705*77c1e3ccSAndroid Build Coastguard Worker     int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
706*77c1e3ccSAndroid Build Coastguard Worker     int_mv ref_mvs[INTRA_FRAME + 1][MAX_MV_REF_CANDIDATES];
707*77c1e3ccSAndroid Build Coastguard Worker 
708*77c1e3ccSAndroid Build Coastguard Worker     av1_find_mv_refs(cm, xd, mbmi, INTRA_FRAME, dcb->ref_mv_count,
709*77c1e3ccSAndroid Build Coastguard Worker                      xd->ref_mv_stack, xd->weight, ref_mvs, /*global_mvs=*/NULL,
710*77c1e3ccSAndroid Build Coastguard Worker                      inter_mode_ctx);
711*77c1e3ccSAndroid Build Coastguard Worker 
712*77c1e3ccSAndroid Build Coastguard Worker     int_mv nearestmv, nearmv;
713*77c1e3ccSAndroid Build Coastguard Worker 
714*77c1e3ccSAndroid Build Coastguard Worker     av1_find_best_ref_mvs(0, ref_mvs[INTRA_FRAME], &nearestmv, &nearmv, 0);
715*77c1e3ccSAndroid Build Coastguard Worker     int_mv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv;
716*77c1e3ccSAndroid Build Coastguard Worker     if (dv_ref.as_int == 0)
717*77c1e3ccSAndroid Build Coastguard Worker       av1_find_ref_dv(&dv_ref, &xd->tile, cm->seq_params->mib_size, xd->mi_row);
718*77c1e3ccSAndroid Build Coastguard Worker     // Ref DV should not have sub-pel.
719*77c1e3ccSAndroid Build Coastguard Worker     int valid_dv = (dv_ref.as_mv.col & 7) == 0 && (dv_ref.as_mv.row & 7) == 0;
720*77c1e3ccSAndroid Build Coastguard Worker     dv_ref.as_mv.col = (dv_ref.as_mv.col >> 3) * 8;
721*77c1e3ccSAndroid Build Coastguard Worker     dv_ref.as_mv.row = (dv_ref.as_mv.row >> 3) * 8;
722*77c1e3ccSAndroid Build Coastguard Worker     valid_dv = valid_dv && assign_dv(cm, xd, &mbmi->mv[0], &dv_ref, xd->mi_row,
723*77c1e3ccSAndroid Build Coastguard Worker                                      xd->mi_col, bsize, r);
724*77c1e3ccSAndroid Build Coastguard Worker     if (!valid_dv) {
725*77c1e3ccSAndroid Build Coastguard Worker       // Intra bc motion vectors are not valid - signal corrupt frame
726*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
727*77c1e3ccSAndroid Build Coastguard Worker                          "Invalid intrabc dv");
728*77c1e3ccSAndroid Build Coastguard Worker     }
729*77c1e3ccSAndroid Build Coastguard Worker   }
730*77c1e3ccSAndroid Build Coastguard Worker }
731*77c1e3ccSAndroid Build Coastguard Worker 
732*77c1e3ccSAndroid Build Coastguard Worker // If delta q is present, reads delta_q index.
733*77c1e3ccSAndroid Build Coastguard Worker // Also reads delta_q loop filter levels, if present.
read_delta_q_params(AV1_COMMON * const cm,MACROBLOCKD * const xd,aom_reader * r)734*77c1e3ccSAndroid Build Coastguard Worker static void read_delta_q_params(AV1_COMMON *const cm, MACROBLOCKD *const xd,
735*77c1e3ccSAndroid Build Coastguard Worker                                 aom_reader *r) {
736*77c1e3ccSAndroid Build Coastguard Worker   DeltaQInfo *const delta_q_info = &cm->delta_q_info;
737*77c1e3ccSAndroid Build Coastguard Worker 
738*77c1e3ccSAndroid Build Coastguard Worker   if (delta_q_info->delta_q_present_flag) {
739*77c1e3ccSAndroid Build Coastguard Worker     MB_MODE_INFO *const mbmi = xd->mi[0];
740*77c1e3ccSAndroid Build Coastguard Worker     xd->current_base_qindex +=
741*77c1e3ccSAndroid Build Coastguard Worker         read_delta_qindex(cm, xd, r, mbmi) * delta_q_info->delta_q_res;
742*77c1e3ccSAndroid Build Coastguard Worker     /* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
743*77c1e3ccSAndroid Build Coastguard Worker     xd->current_base_qindex = clamp(xd->current_base_qindex, 1, MAXQ);
744*77c1e3ccSAndroid Build Coastguard Worker     FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
745*77c1e3ccSAndroid Build Coastguard Worker     if (delta_q_info->delta_lf_present_flag) {
746*77c1e3ccSAndroid Build Coastguard Worker       const int mi_row = xd->mi_row;
747*77c1e3ccSAndroid Build Coastguard Worker       const int mi_col = xd->mi_col;
748*77c1e3ccSAndroid Build Coastguard Worker       if (delta_q_info->delta_lf_multi) {
749*77c1e3ccSAndroid Build Coastguard Worker         const int frame_lf_count =
750*77c1e3ccSAndroid Build Coastguard Worker             av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
751*77c1e3ccSAndroid Build Coastguard Worker         for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id) {
752*77c1e3ccSAndroid Build Coastguard Worker           const int tmp_lvl =
753*77c1e3ccSAndroid Build Coastguard Worker               xd->delta_lf[lf_id] +
754*77c1e3ccSAndroid Build Coastguard Worker               read_delta_lflevel(cm, r, ec_ctx->delta_lf_multi_cdf[lf_id], mbmi,
755*77c1e3ccSAndroid Build Coastguard Worker                                  mi_col, mi_row) *
756*77c1e3ccSAndroid Build Coastguard Worker                   delta_q_info->delta_lf_res;
757*77c1e3ccSAndroid Build Coastguard Worker           mbmi->delta_lf[lf_id] = xd->delta_lf[lf_id] =
758*77c1e3ccSAndroid Build Coastguard Worker               clamp(tmp_lvl, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
759*77c1e3ccSAndroid Build Coastguard Worker         }
760*77c1e3ccSAndroid Build Coastguard Worker       } else {
761*77c1e3ccSAndroid Build Coastguard Worker         const int tmp_lvl = xd->delta_lf_from_base +
762*77c1e3ccSAndroid Build Coastguard Worker                             read_delta_lflevel(cm, r, ec_ctx->delta_lf_cdf,
763*77c1e3ccSAndroid Build Coastguard Worker                                                mbmi, mi_col, mi_row) *
764*77c1e3ccSAndroid Build Coastguard Worker                                 delta_q_info->delta_lf_res;
765*77c1e3ccSAndroid Build Coastguard Worker         mbmi->delta_lf_from_base = xd->delta_lf_from_base =
766*77c1e3ccSAndroid Build Coastguard Worker             clamp(tmp_lvl, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
767*77c1e3ccSAndroid Build Coastguard Worker       }
768*77c1e3ccSAndroid Build Coastguard Worker     }
769*77c1e3ccSAndroid Build Coastguard Worker   }
770*77c1e3ccSAndroid Build Coastguard Worker }
771*77c1e3ccSAndroid Build Coastguard Worker 
read_intra_frame_mode_info(AV1_COMMON * const cm,DecoderCodingBlock * dcb,aom_reader * r)772*77c1e3ccSAndroid Build Coastguard Worker static void read_intra_frame_mode_info(AV1_COMMON *const cm,
773*77c1e3ccSAndroid Build Coastguard Worker                                        DecoderCodingBlock *dcb, aom_reader *r) {
774*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &dcb->xd;
775*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *const mbmi = xd->mi[0];
776*77c1e3ccSAndroid Build Coastguard Worker   const MB_MODE_INFO *above_mi = xd->above_mbmi;
777*77c1e3ccSAndroid Build Coastguard Worker   const MB_MODE_INFO *left_mi = xd->left_mbmi;
778*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = mbmi->bsize;
779*77c1e3ccSAndroid Build Coastguard Worker   struct segmentation *const seg = &cm->seg;
780*77c1e3ccSAndroid Build Coastguard Worker 
781*77c1e3ccSAndroid Build Coastguard Worker   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
782*77c1e3ccSAndroid Build Coastguard Worker 
783*77c1e3ccSAndroid Build Coastguard Worker   if (seg->segid_preskip)
784*77c1e3ccSAndroid Build Coastguard Worker     mbmi->segment_id = read_intra_segment_id(cm, xd, bsize, r, 0);
785*77c1e3ccSAndroid Build Coastguard Worker 
786*77c1e3ccSAndroid Build Coastguard Worker   mbmi->skip_txfm = read_skip_txfm(cm, xd, mbmi->segment_id, r);
787*77c1e3ccSAndroid Build Coastguard Worker 
788*77c1e3ccSAndroid Build Coastguard Worker   if (!seg->segid_preskip)
789*77c1e3ccSAndroid Build Coastguard Worker     mbmi->segment_id = read_intra_segment_id(cm, xd, bsize, r, mbmi->skip_txfm);
790*77c1e3ccSAndroid Build Coastguard Worker 
791*77c1e3ccSAndroid Build Coastguard Worker   read_cdef(cm, r, xd);
792*77c1e3ccSAndroid Build Coastguard Worker 
793*77c1e3ccSAndroid Build Coastguard Worker   read_delta_q_params(cm, xd, r);
794*77c1e3ccSAndroid Build Coastguard Worker 
795*77c1e3ccSAndroid Build Coastguard Worker   mbmi->current_qindex = xd->current_base_qindex;
796*77c1e3ccSAndroid Build Coastguard Worker 
797*77c1e3ccSAndroid Build Coastguard Worker   mbmi->ref_frame[0] = INTRA_FRAME;
798*77c1e3ccSAndroid Build Coastguard Worker   mbmi->ref_frame[1] = NONE_FRAME;
799*77c1e3ccSAndroid Build Coastguard Worker   mbmi->palette_mode_info.palette_size[0] = 0;
800*77c1e3ccSAndroid Build Coastguard Worker   mbmi->palette_mode_info.palette_size[1] = 0;
801*77c1e3ccSAndroid Build Coastguard Worker   mbmi->filter_intra_mode_info.use_filter_intra = 0;
802*77c1e3ccSAndroid Build Coastguard Worker 
803*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row = xd->mi_row;
804*77c1e3ccSAndroid Build Coastguard Worker   const int mi_col = xd->mi_col;
805*77c1e3ccSAndroid Build Coastguard Worker   xd->above_txfm_context = cm->above_contexts.txfm[xd->tile.tile_row] + mi_col;
806*77c1e3ccSAndroid Build Coastguard Worker   xd->left_txfm_context =
807*77c1e3ccSAndroid Build Coastguard Worker       xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
808*77c1e3ccSAndroid Build Coastguard Worker 
809*77c1e3ccSAndroid Build Coastguard Worker   if (av1_allow_intrabc(cm)) {
810*77c1e3ccSAndroid Build Coastguard Worker     read_intrabc_info(cm, dcb, r);
811*77c1e3ccSAndroid Build Coastguard Worker     if (is_intrabc_block(mbmi)) return;
812*77c1e3ccSAndroid Build Coastguard Worker   }
813*77c1e3ccSAndroid Build Coastguard Worker 
814*77c1e3ccSAndroid Build Coastguard Worker   mbmi->mode = read_intra_mode(r, get_y_mode_cdf(ec_ctx, above_mi, left_mi));
815*77c1e3ccSAndroid Build Coastguard Worker 
816*77c1e3ccSAndroid Build Coastguard Worker   const int use_angle_delta = av1_use_angle_delta(bsize);
817*77c1e3ccSAndroid Build Coastguard Worker   mbmi->angle_delta[PLANE_TYPE_Y] =
818*77c1e3ccSAndroid Build Coastguard Worker       (use_angle_delta && av1_is_directional_mode(mbmi->mode))
819*77c1e3ccSAndroid Build Coastguard Worker           ? read_angle_delta(r, ec_ctx->angle_delta_cdf[mbmi->mode - V_PRED])
820*77c1e3ccSAndroid Build Coastguard Worker           : 0;
821*77c1e3ccSAndroid Build Coastguard Worker 
822*77c1e3ccSAndroid Build Coastguard Worker   if (!cm->seq_params->monochrome && xd->is_chroma_ref) {
823*77c1e3ccSAndroid Build Coastguard Worker     mbmi->uv_mode =
824*77c1e3ccSAndroid Build Coastguard Worker         read_intra_mode_uv(ec_ctx, r, is_cfl_allowed(xd), mbmi->mode);
825*77c1e3ccSAndroid Build Coastguard Worker     if (mbmi->uv_mode == UV_CFL_PRED) {
826*77c1e3ccSAndroid Build Coastguard Worker       mbmi->cfl_alpha_idx = read_cfl_alphas(ec_ctx, r, &mbmi->cfl_alpha_signs);
827*77c1e3ccSAndroid Build Coastguard Worker     }
828*77c1e3ccSAndroid Build Coastguard Worker     const PREDICTION_MODE intra_mode = get_uv_mode(mbmi->uv_mode);
829*77c1e3ccSAndroid Build Coastguard Worker     mbmi->angle_delta[PLANE_TYPE_UV] =
830*77c1e3ccSAndroid Build Coastguard Worker         (use_angle_delta && av1_is_directional_mode(intra_mode))
831*77c1e3ccSAndroid Build Coastguard Worker             ? read_angle_delta(r, ec_ctx->angle_delta_cdf[intra_mode - V_PRED])
832*77c1e3ccSAndroid Build Coastguard Worker             : 0;
833*77c1e3ccSAndroid Build Coastguard Worker   } else {
834*77c1e3ccSAndroid Build Coastguard Worker     // Avoid decoding angle_info if there is no chroma prediction
835*77c1e3ccSAndroid Build Coastguard Worker     mbmi->uv_mode = UV_DC_PRED;
836*77c1e3ccSAndroid Build Coastguard Worker   }
837*77c1e3ccSAndroid Build Coastguard Worker   xd->cfl.store_y = store_cfl_required(cm, xd);
838*77c1e3ccSAndroid Build Coastguard Worker 
839*77c1e3ccSAndroid Build Coastguard Worker   if (av1_allow_palette(cm->features.allow_screen_content_tools, bsize))
840*77c1e3ccSAndroid Build Coastguard Worker     read_palette_mode_info(cm, xd, r);
841*77c1e3ccSAndroid Build Coastguard Worker 
842*77c1e3ccSAndroid Build Coastguard Worker   read_filter_intra_mode_info(cm, xd, r);
843*77c1e3ccSAndroid Build Coastguard Worker }
844*77c1e3ccSAndroid Build Coastguard Worker 
read_mv_component(aom_reader * r,nmv_component * mvcomp,int use_subpel,int usehp)845*77c1e3ccSAndroid Build Coastguard Worker static int read_mv_component(aom_reader *r, nmv_component *mvcomp,
846*77c1e3ccSAndroid Build Coastguard Worker                              int use_subpel, int usehp) {
847*77c1e3ccSAndroid Build Coastguard Worker   int mag, d, fr, hp;
848*77c1e3ccSAndroid Build Coastguard Worker   const int sign = aom_read_symbol(r, mvcomp->sign_cdf, 2, ACCT_STR);
849*77c1e3ccSAndroid Build Coastguard Worker   const int mv_class =
850*77c1e3ccSAndroid Build Coastguard Worker       aom_read_symbol(r, mvcomp->classes_cdf, MV_CLASSES, ACCT_STR);
851*77c1e3ccSAndroid Build Coastguard Worker   const int class0 = mv_class == MV_CLASS_0;
852*77c1e3ccSAndroid Build Coastguard Worker 
853*77c1e3ccSAndroid Build Coastguard Worker   // Integer part
854*77c1e3ccSAndroid Build Coastguard Worker   if (class0) {
855*77c1e3ccSAndroid Build Coastguard Worker     d = aom_read_symbol(r, mvcomp->class0_cdf, CLASS0_SIZE, ACCT_STR);
856*77c1e3ccSAndroid Build Coastguard Worker     mag = 0;
857*77c1e3ccSAndroid Build Coastguard Worker   } else {
858*77c1e3ccSAndroid Build Coastguard Worker     const int n = mv_class + CLASS0_BITS - 1;  // number of bits
859*77c1e3ccSAndroid Build Coastguard Worker     d = 0;
860*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < n; ++i)
861*77c1e3ccSAndroid Build Coastguard Worker       d |= aom_read_symbol(r, mvcomp->bits_cdf[i], 2, ACCT_STR) << i;
862*77c1e3ccSAndroid Build Coastguard Worker     mag = CLASS0_SIZE << (mv_class + 2);
863*77c1e3ccSAndroid Build Coastguard Worker   }
864*77c1e3ccSAndroid Build Coastguard Worker 
865*77c1e3ccSAndroid Build Coastguard Worker   if (use_subpel) {
866*77c1e3ccSAndroid Build Coastguard Worker     // Fractional part
867*77c1e3ccSAndroid Build Coastguard Worker     fr = aom_read_symbol(r, class0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf,
868*77c1e3ccSAndroid Build Coastguard Worker                          MV_FP_SIZE, ACCT_STR);
869*77c1e3ccSAndroid Build Coastguard Worker 
870*77c1e3ccSAndroid Build Coastguard Worker     // High precision part (if hp is not used, the default value of the hp is 1)
871*77c1e3ccSAndroid Build Coastguard Worker     hp = usehp ? aom_read_symbol(
872*77c1e3ccSAndroid Build Coastguard Worker                      r, class0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf, 2,
873*77c1e3ccSAndroid Build Coastguard Worker                      ACCT_STR)
874*77c1e3ccSAndroid Build Coastguard Worker                : 1;
875*77c1e3ccSAndroid Build Coastguard Worker   } else {
876*77c1e3ccSAndroid Build Coastguard Worker     fr = 3;
877*77c1e3ccSAndroid Build Coastguard Worker     hp = 1;
878*77c1e3ccSAndroid Build Coastguard Worker   }
879*77c1e3ccSAndroid Build Coastguard Worker 
880*77c1e3ccSAndroid Build Coastguard Worker   // Result
881*77c1e3ccSAndroid Build Coastguard Worker   mag += ((d << 3) | (fr << 1) | hp) + 1;
882*77c1e3ccSAndroid Build Coastguard Worker   return sign ? -mag : mag;
883*77c1e3ccSAndroid Build Coastguard Worker }
884*77c1e3ccSAndroid Build Coastguard Worker 
read_mv(aom_reader * r,MV * mv,const MV * ref,nmv_context * ctx,MvSubpelPrecision precision)885*77c1e3ccSAndroid Build Coastguard Worker static inline void read_mv(aom_reader *r, MV *mv, const MV *ref,
886*77c1e3ccSAndroid Build Coastguard Worker                            nmv_context *ctx, MvSubpelPrecision precision) {
887*77c1e3ccSAndroid Build Coastguard Worker   MV diff = kZeroMv;
888*77c1e3ccSAndroid Build Coastguard Worker   const MV_JOINT_TYPE joint_type =
889*77c1e3ccSAndroid Build Coastguard Worker       (MV_JOINT_TYPE)aom_read_symbol(r, ctx->joints_cdf, MV_JOINTS, ACCT_STR);
890*77c1e3ccSAndroid Build Coastguard Worker 
891*77c1e3ccSAndroid Build Coastguard Worker   if (mv_joint_vertical(joint_type))
892*77c1e3ccSAndroid Build Coastguard Worker     diff.row = read_mv_component(r, &ctx->comps[0], precision > MV_SUBPEL_NONE,
893*77c1e3ccSAndroid Build Coastguard Worker                                  precision > MV_SUBPEL_LOW_PRECISION);
894*77c1e3ccSAndroid Build Coastguard Worker 
895*77c1e3ccSAndroid Build Coastguard Worker   if (mv_joint_horizontal(joint_type))
896*77c1e3ccSAndroid Build Coastguard Worker     diff.col = read_mv_component(r, &ctx->comps[1], precision > MV_SUBPEL_NONE,
897*77c1e3ccSAndroid Build Coastguard Worker                                  precision > MV_SUBPEL_LOW_PRECISION);
898*77c1e3ccSAndroid Build Coastguard Worker 
899*77c1e3ccSAndroid Build Coastguard Worker   mv->row = ref->row + diff.row;
900*77c1e3ccSAndroid Build Coastguard Worker   mv->col = ref->col + diff.col;
901*77c1e3ccSAndroid Build Coastguard Worker }
902*77c1e3ccSAndroid Build Coastguard Worker 
read_block_reference_mode(AV1_COMMON * cm,const MACROBLOCKD * xd,aom_reader * r)903*77c1e3ccSAndroid Build Coastguard Worker static REFERENCE_MODE read_block_reference_mode(AV1_COMMON *cm,
904*77c1e3ccSAndroid Build Coastguard Worker                                                 const MACROBLOCKD *xd,
905*77c1e3ccSAndroid Build Coastguard Worker                                                 aom_reader *r) {
906*77c1e3ccSAndroid Build Coastguard Worker   if (!is_comp_ref_allowed(xd->mi[0]->bsize)) return SINGLE_REFERENCE;
907*77c1e3ccSAndroid Build Coastguard Worker   if (cm->current_frame.reference_mode == REFERENCE_MODE_SELECT) {
908*77c1e3ccSAndroid Build Coastguard Worker     const int ctx = av1_get_reference_mode_context(xd);
909*77c1e3ccSAndroid Build Coastguard Worker     const REFERENCE_MODE mode = (REFERENCE_MODE)aom_read_symbol(
910*77c1e3ccSAndroid Build Coastguard Worker         r, xd->tile_ctx->comp_inter_cdf[ctx], 2, ACCT_STR);
911*77c1e3ccSAndroid Build Coastguard Worker     return mode;  // SINGLE_REFERENCE or COMPOUND_REFERENCE
912*77c1e3ccSAndroid Build Coastguard Worker   } else {
913*77c1e3ccSAndroid Build Coastguard Worker     assert(cm->current_frame.reference_mode == SINGLE_REFERENCE);
914*77c1e3ccSAndroid Build Coastguard Worker     return cm->current_frame.reference_mode;
915*77c1e3ccSAndroid Build Coastguard Worker   }
916*77c1e3ccSAndroid Build Coastguard Worker }
917*77c1e3ccSAndroid Build Coastguard Worker 
918*77c1e3ccSAndroid Build Coastguard Worker #define READ_REF_BIT(pname) \
919*77c1e3ccSAndroid Build Coastguard Worker   aom_read_symbol(r, av1_get_pred_cdf_##pname(xd), 2, ACCT_STR)
920*77c1e3ccSAndroid Build Coastguard Worker 
read_comp_reference_type(const MACROBLOCKD * xd,aom_reader * r)921*77c1e3ccSAndroid Build Coastguard Worker static COMP_REFERENCE_TYPE read_comp_reference_type(const MACROBLOCKD *xd,
922*77c1e3ccSAndroid Build Coastguard Worker                                                     aom_reader *r) {
923*77c1e3ccSAndroid Build Coastguard Worker   const int ctx = av1_get_comp_reference_type_context(xd);
924*77c1e3ccSAndroid Build Coastguard Worker   const COMP_REFERENCE_TYPE comp_ref_type =
925*77c1e3ccSAndroid Build Coastguard Worker       (COMP_REFERENCE_TYPE)aom_read_symbol(
926*77c1e3ccSAndroid Build Coastguard Worker           r, xd->tile_ctx->comp_ref_type_cdf[ctx], 2, ACCT_STR);
927*77c1e3ccSAndroid Build Coastguard Worker   return comp_ref_type;  // UNIDIR_COMP_REFERENCE or BIDIR_COMP_REFERENCE
928*77c1e3ccSAndroid Build Coastguard Worker }
929*77c1e3ccSAndroid Build Coastguard Worker 
set_ref_frames_for_skip_mode(AV1_COMMON * const cm,MV_REFERENCE_FRAME ref_frame[2])930*77c1e3ccSAndroid Build Coastguard Worker static void set_ref_frames_for_skip_mode(AV1_COMMON *const cm,
931*77c1e3ccSAndroid Build Coastguard Worker                                          MV_REFERENCE_FRAME ref_frame[2]) {
932*77c1e3ccSAndroid Build Coastguard Worker   ref_frame[0] = LAST_FRAME + cm->current_frame.skip_mode_info.ref_frame_idx_0;
933*77c1e3ccSAndroid Build Coastguard Worker   ref_frame[1] = LAST_FRAME + cm->current_frame.skip_mode_info.ref_frame_idx_1;
934*77c1e3ccSAndroid Build Coastguard Worker }
935*77c1e3ccSAndroid Build Coastguard Worker 
936*77c1e3ccSAndroid Build Coastguard Worker // Read the referncence frame
read_ref_frames(AV1_COMMON * const cm,MACROBLOCKD * const xd,aom_reader * r,int segment_id,MV_REFERENCE_FRAME ref_frame[2])937*77c1e3ccSAndroid Build Coastguard Worker static void read_ref_frames(AV1_COMMON *const cm, MACROBLOCKD *const xd,
938*77c1e3ccSAndroid Build Coastguard Worker                             aom_reader *r, int segment_id,
939*77c1e3ccSAndroid Build Coastguard Worker                             MV_REFERENCE_FRAME ref_frame[2]) {
940*77c1e3ccSAndroid Build Coastguard Worker   if (xd->mi[0]->skip_mode) {
941*77c1e3ccSAndroid Build Coastguard Worker     set_ref_frames_for_skip_mode(cm, ref_frame);
942*77c1e3ccSAndroid Build Coastguard Worker     return;
943*77c1e3ccSAndroid Build Coastguard Worker   }
944*77c1e3ccSAndroid Build Coastguard Worker 
945*77c1e3ccSAndroid Build Coastguard Worker   if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
946*77c1e3ccSAndroid Build Coastguard Worker     ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
947*77c1e3ccSAndroid Build Coastguard Worker                                                    SEG_LVL_REF_FRAME);
948*77c1e3ccSAndroid Build Coastguard Worker     ref_frame[1] = NONE_FRAME;
949*77c1e3ccSAndroid Build Coastguard Worker   } else if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP) ||
950*77c1e3ccSAndroid Build Coastguard Worker              segfeature_active(&cm->seg, segment_id, SEG_LVL_GLOBALMV)) {
951*77c1e3ccSAndroid Build Coastguard Worker     ref_frame[0] = LAST_FRAME;
952*77c1e3ccSAndroid Build Coastguard Worker     ref_frame[1] = NONE_FRAME;
953*77c1e3ccSAndroid Build Coastguard Worker   } else {
954*77c1e3ccSAndroid Build Coastguard Worker     const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
955*77c1e3ccSAndroid Build Coastguard Worker 
956*77c1e3ccSAndroid Build Coastguard Worker     if (mode == COMPOUND_REFERENCE) {
957*77c1e3ccSAndroid Build Coastguard Worker       const COMP_REFERENCE_TYPE comp_ref_type = read_comp_reference_type(xd, r);
958*77c1e3ccSAndroid Build Coastguard Worker 
959*77c1e3ccSAndroid Build Coastguard Worker       if (comp_ref_type == UNIDIR_COMP_REFERENCE) {
960*77c1e3ccSAndroid Build Coastguard Worker         const int bit = READ_REF_BIT(uni_comp_ref_p);
961*77c1e3ccSAndroid Build Coastguard Worker         if (bit) {
962*77c1e3ccSAndroid Build Coastguard Worker           ref_frame[0] = BWDREF_FRAME;
963*77c1e3ccSAndroid Build Coastguard Worker           ref_frame[1] = ALTREF_FRAME;
964*77c1e3ccSAndroid Build Coastguard Worker         } else {
965*77c1e3ccSAndroid Build Coastguard Worker           const int bit1 = READ_REF_BIT(uni_comp_ref_p1);
966*77c1e3ccSAndroid Build Coastguard Worker           if (bit1) {
967*77c1e3ccSAndroid Build Coastguard Worker             const int bit2 = READ_REF_BIT(uni_comp_ref_p2);
968*77c1e3ccSAndroid Build Coastguard Worker             if (bit2) {
969*77c1e3ccSAndroid Build Coastguard Worker               ref_frame[0] = LAST_FRAME;
970*77c1e3ccSAndroid Build Coastguard Worker               ref_frame[1] = GOLDEN_FRAME;
971*77c1e3ccSAndroid Build Coastguard Worker             } else {
972*77c1e3ccSAndroid Build Coastguard Worker               ref_frame[0] = LAST_FRAME;
973*77c1e3ccSAndroid Build Coastguard Worker               ref_frame[1] = LAST3_FRAME;
974*77c1e3ccSAndroid Build Coastguard Worker             }
975*77c1e3ccSAndroid Build Coastguard Worker           } else {
976*77c1e3ccSAndroid Build Coastguard Worker             ref_frame[0] = LAST_FRAME;
977*77c1e3ccSAndroid Build Coastguard Worker             ref_frame[1] = LAST2_FRAME;
978*77c1e3ccSAndroid Build Coastguard Worker           }
979*77c1e3ccSAndroid Build Coastguard Worker         }
980*77c1e3ccSAndroid Build Coastguard Worker 
981*77c1e3ccSAndroid Build Coastguard Worker         return;
982*77c1e3ccSAndroid Build Coastguard Worker       }
983*77c1e3ccSAndroid Build Coastguard Worker 
984*77c1e3ccSAndroid Build Coastguard Worker       assert(comp_ref_type == BIDIR_COMP_REFERENCE);
985*77c1e3ccSAndroid Build Coastguard Worker 
986*77c1e3ccSAndroid Build Coastguard Worker       const int idx = 1;
987*77c1e3ccSAndroid Build Coastguard Worker       const int bit = READ_REF_BIT(comp_ref_p);
988*77c1e3ccSAndroid Build Coastguard Worker       // Decode forward references.
989*77c1e3ccSAndroid Build Coastguard Worker       if (!bit) {
990*77c1e3ccSAndroid Build Coastguard Worker         const int bit1 = READ_REF_BIT(comp_ref_p1);
991*77c1e3ccSAndroid Build Coastguard Worker         ref_frame[!idx] = bit1 ? LAST2_FRAME : LAST_FRAME;
992*77c1e3ccSAndroid Build Coastguard Worker       } else {
993*77c1e3ccSAndroid Build Coastguard Worker         const int bit2 = READ_REF_BIT(comp_ref_p2);
994*77c1e3ccSAndroid Build Coastguard Worker         ref_frame[!idx] = bit2 ? GOLDEN_FRAME : LAST3_FRAME;
995*77c1e3ccSAndroid Build Coastguard Worker       }
996*77c1e3ccSAndroid Build Coastguard Worker 
997*77c1e3ccSAndroid Build Coastguard Worker       // Decode backward references.
998*77c1e3ccSAndroid Build Coastguard Worker       const int bit_bwd = READ_REF_BIT(comp_bwdref_p);
999*77c1e3ccSAndroid Build Coastguard Worker       if (!bit_bwd) {
1000*77c1e3ccSAndroid Build Coastguard Worker         const int bit1_bwd = READ_REF_BIT(comp_bwdref_p1);
1001*77c1e3ccSAndroid Build Coastguard Worker         ref_frame[idx] = bit1_bwd ? ALTREF2_FRAME : BWDREF_FRAME;
1002*77c1e3ccSAndroid Build Coastguard Worker       } else {
1003*77c1e3ccSAndroid Build Coastguard Worker         ref_frame[idx] = ALTREF_FRAME;
1004*77c1e3ccSAndroid Build Coastguard Worker       }
1005*77c1e3ccSAndroid Build Coastguard Worker     } else if (mode == SINGLE_REFERENCE) {
1006*77c1e3ccSAndroid Build Coastguard Worker       const int bit0 = READ_REF_BIT(single_ref_p1);
1007*77c1e3ccSAndroid Build Coastguard Worker       if (bit0) {
1008*77c1e3ccSAndroid Build Coastguard Worker         const int bit1 = READ_REF_BIT(single_ref_p2);
1009*77c1e3ccSAndroid Build Coastguard Worker         if (!bit1) {
1010*77c1e3ccSAndroid Build Coastguard Worker           const int bit5 = READ_REF_BIT(single_ref_p6);
1011*77c1e3ccSAndroid Build Coastguard Worker           ref_frame[0] = bit5 ? ALTREF2_FRAME : BWDREF_FRAME;
1012*77c1e3ccSAndroid Build Coastguard Worker         } else {
1013*77c1e3ccSAndroid Build Coastguard Worker           ref_frame[0] = ALTREF_FRAME;
1014*77c1e3ccSAndroid Build Coastguard Worker         }
1015*77c1e3ccSAndroid Build Coastguard Worker       } else {
1016*77c1e3ccSAndroid Build Coastguard Worker         const int bit2 = READ_REF_BIT(single_ref_p3);
1017*77c1e3ccSAndroid Build Coastguard Worker         if (bit2) {
1018*77c1e3ccSAndroid Build Coastguard Worker           const int bit4 = READ_REF_BIT(single_ref_p5);
1019*77c1e3ccSAndroid Build Coastguard Worker           ref_frame[0] = bit4 ? GOLDEN_FRAME : LAST3_FRAME;
1020*77c1e3ccSAndroid Build Coastguard Worker         } else {
1021*77c1e3ccSAndroid Build Coastguard Worker           const int bit3 = READ_REF_BIT(single_ref_p4);
1022*77c1e3ccSAndroid Build Coastguard Worker           ref_frame[0] = bit3 ? LAST2_FRAME : LAST_FRAME;
1023*77c1e3ccSAndroid Build Coastguard Worker         }
1024*77c1e3ccSAndroid Build Coastguard Worker       }
1025*77c1e3ccSAndroid Build Coastguard Worker 
1026*77c1e3ccSAndroid Build Coastguard Worker       ref_frame[1] = NONE_FRAME;
1027*77c1e3ccSAndroid Build Coastguard Worker     } else {
1028*77c1e3ccSAndroid Build Coastguard Worker       assert(0 && "Invalid prediction mode.");
1029*77c1e3ccSAndroid Build Coastguard Worker     }
1030*77c1e3ccSAndroid Build Coastguard Worker   }
1031*77c1e3ccSAndroid Build Coastguard Worker }
1032*77c1e3ccSAndroid Build Coastguard Worker 
read_mb_interp_filter(const MACROBLOCKD * const xd,InterpFilter interp_filter,bool enable_dual_filter,MB_MODE_INFO * const mbmi,aom_reader * r)1033*77c1e3ccSAndroid Build Coastguard Worker static inline void read_mb_interp_filter(const MACROBLOCKD *const xd,
1034*77c1e3ccSAndroid Build Coastguard Worker                                          InterpFilter interp_filter,
1035*77c1e3ccSAndroid Build Coastguard Worker                                          bool enable_dual_filter,
1036*77c1e3ccSAndroid Build Coastguard Worker                                          MB_MODE_INFO *const mbmi,
1037*77c1e3ccSAndroid Build Coastguard Worker                                          aom_reader *r) {
1038*77c1e3ccSAndroid Build Coastguard Worker   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1039*77c1e3ccSAndroid Build Coastguard Worker 
1040*77c1e3ccSAndroid Build Coastguard Worker   if (!av1_is_interp_needed(xd)) {
1041*77c1e3ccSAndroid Build Coastguard Worker     set_default_interp_filters(mbmi, interp_filter);
1042*77c1e3ccSAndroid Build Coastguard Worker     return;
1043*77c1e3ccSAndroid Build Coastguard Worker   }
1044*77c1e3ccSAndroid Build Coastguard Worker 
1045*77c1e3ccSAndroid Build Coastguard Worker   if (interp_filter != SWITCHABLE) {
1046*77c1e3ccSAndroid Build Coastguard Worker     mbmi->interp_filters = av1_broadcast_interp_filter(interp_filter);
1047*77c1e3ccSAndroid Build Coastguard Worker   } else {
1048*77c1e3ccSAndroid Build Coastguard Worker     InterpFilter ref0_filter[2] = { EIGHTTAP_REGULAR, EIGHTTAP_REGULAR };
1049*77c1e3ccSAndroid Build Coastguard Worker     for (int dir = 0; dir < 2; ++dir) {
1050*77c1e3ccSAndroid Build Coastguard Worker       const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
1051*77c1e3ccSAndroid Build Coastguard Worker       ref0_filter[dir] = (InterpFilter)aom_read_symbol(
1052*77c1e3ccSAndroid Build Coastguard Worker           r, ec_ctx->switchable_interp_cdf[ctx], SWITCHABLE_FILTERS, ACCT_STR);
1053*77c1e3ccSAndroid Build Coastguard Worker       if (!enable_dual_filter) {
1054*77c1e3ccSAndroid Build Coastguard Worker         ref0_filter[1] = ref0_filter[0];
1055*77c1e3ccSAndroid Build Coastguard Worker         break;
1056*77c1e3ccSAndroid Build Coastguard Worker       }
1057*77c1e3ccSAndroid Build Coastguard Worker     }
1058*77c1e3ccSAndroid Build Coastguard Worker     // The index system works as: (0, 1) -> (vertical, horizontal) filter types
1059*77c1e3ccSAndroid Build Coastguard Worker     mbmi->interp_filters.as_filters.x_filter = ref0_filter[1];
1060*77c1e3ccSAndroid Build Coastguard Worker     mbmi->interp_filters.as_filters.y_filter = ref0_filter[0];
1061*77c1e3ccSAndroid Build Coastguard Worker   }
1062*77c1e3ccSAndroid Build Coastguard Worker }
1063*77c1e3ccSAndroid Build Coastguard Worker 
read_intra_block_mode_info(AV1_COMMON * const cm,MACROBLOCKD * const xd,MB_MODE_INFO * const mbmi,aom_reader * r)1064*77c1e3ccSAndroid Build Coastguard Worker static void read_intra_block_mode_info(AV1_COMMON *const cm,
1065*77c1e3ccSAndroid Build Coastguard Worker                                        MACROBLOCKD *const xd,
1066*77c1e3ccSAndroid Build Coastguard Worker                                        MB_MODE_INFO *const mbmi,
1067*77c1e3ccSAndroid Build Coastguard Worker                                        aom_reader *r) {
1068*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = mbmi->bsize;
1069*77c1e3ccSAndroid Build Coastguard Worker   const int use_angle_delta = av1_use_angle_delta(bsize);
1070*77c1e3ccSAndroid Build Coastguard Worker 
1071*77c1e3ccSAndroid Build Coastguard Worker   mbmi->ref_frame[0] = INTRA_FRAME;
1072*77c1e3ccSAndroid Build Coastguard Worker   mbmi->ref_frame[1] = NONE_FRAME;
1073*77c1e3ccSAndroid Build Coastguard Worker 
1074*77c1e3ccSAndroid Build Coastguard Worker   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1075*77c1e3ccSAndroid Build Coastguard Worker 
1076*77c1e3ccSAndroid Build Coastguard Worker   mbmi->mode = read_intra_mode(r, ec_ctx->y_mode_cdf[size_group_lookup[bsize]]);
1077*77c1e3ccSAndroid Build Coastguard Worker 
1078*77c1e3ccSAndroid Build Coastguard Worker   mbmi->angle_delta[PLANE_TYPE_Y] =
1079*77c1e3ccSAndroid Build Coastguard Worker       use_angle_delta && av1_is_directional_mode(mbmi->mode)
1080*77c1e3ccSAndroid Build Coastguard Worker           ? read_angle_delta(r, ec_ctx->angle_delta_cdf[mbmi->mode - V_PRED])
1081*77c1e3ccSAndroid Build Coastguard Worker           : 0;
1082*77c1e3ccSAndroid Build Coastguard Worker   if (!cm->seq_params->monochrome && xd->is_chroma_ref) {
1083*77c1e3ccSAndroid Build Coastguard Worker     mbmi->uv_mode =
1084*77c1e3ccSAndroid Build Coastguard Worker         read_intra_mode_uv(ec_ctx, r, is_cfl_allowed(xd), mbmi->mode);
1085*77c1e3ccSAndroid Build Coastguard Worker     if (mbmi->uv_mode == UV_CFL_PRED) {
1086*77c1e3ccSAndroid Build Coastguard Worker       mbmi->cfl_alpha_idx =
1087*77c1e3ccSAndroid Build Coastguard Worker           read_cfl_alphas(xd->tile_ctx, r, &mbmi->cfl_alpha_signs);
1088*77c1e3ccSAndroid Build Coastguard Worker     }
1089*77c1e3ccSAndroid Build Coastguard Worker     const PREDICTION_MODE intra_mode = get_uv_mode(mbmi->uv_mode);
1090*77c1e3ccSAndroid Build Coastguard Worker     mbmi->angle_delta[PLANE_TYPE_UV] =
1091*77c1e3ccSAndroid Build Coastguard Worker         use_angle_delta && av1_is_directional_mode(intra_mode)
1092*77c1e3ccSAndroid Build Coastguard Worker             ? read_angle_delta(r, ec_ctx->angle_delta_cdf[intra_mode - V_PRED])
1093*77c1e3ccSAndroid Build Coastguard Worker             : 0;
1094*77c1e3ccSAndroid Build Coastguard Worker   } else {
1095*77c1e3ccSAndroid Build Coastguard Worker     // Avoid decoding angle_info if there is no chroma prediction
1096*77c1e3ccSAndroid Build Coastguard Worker     mbmi->uv_mode = UV_DC_PRED;
1097*77c1e3ccSAndroid Build Coastguard Worker   }
1098*77c1e3ccSAndroid Build Coastguard Worker   xd->cfl.store_y = store_cfl_required(cm, xd);
1099*77c1e3ccSAndroid Build Coastguard Worker 
1100*77c1e3ccSAndroid Build Coastguard Worker   mbmi->palette_mode_info.palette_size[0] = 0;
1101*77c1e3ccSAndroid Build Coastguard Worker   mbmi->palette_mode_info.palette_size[1] = 0;
1102*77c1e3ccSAndroid Build Coastguard Worker   if (av1_allow_palette(cm->features.allow_screen_content_tools, bsize))
1103*77c1e3ccSAndroid Build Coastguard Worker     read_palette_mode_info(cm, xd, r);
1104*77c1e3ccSAndroid Build Coastguard Worker 
1105*77c1e3ccSAndroid Build Coastguard Worker   read_filter_intra_mode_info(cm, xd, r);
1106*77c1e3ccSAndroid Build Coastguard Worker }
1107*77c1e3ccSAndroid Build Coastguard Worker 
is_mv_valid(const MV * mv)1108*77c1e3ccSAndroid Build Coastguard Worker static inline int is_mv_valid(const MV *mv) {
1109*77c1e3ccSAndroid Build Coastguard Worker   return mv->row > MV_LOW && mv->row < MV_UPP && mv->col > MV_LOW &&
1110*77c1e3ccSAndroid Build Coastguard Worker          mv->col < MV_UPP;
1111*77c1e3ccSAndroid Build Coastguard Worker }
1112*77c1e3ccSAndroid Build Coastguard Worker 
assign_mv(AV1_COMMON * cm,MACROBLOCKD * xd,PREDICTION_MODE mode,MV_REFERENCE_FRAME ref_frame[2],int_mv mv[2],int_mv ref_mv[2],int_mv nearest_mv[2],int_mv near_mv[2],int is_compound,int allow_hp,aom_reader * r)1113*77c1e3ccSAndroid Build Coastguard Worker static inline int assign_mv(AV1_COMMON *cm, MACROBLOCKD *xd,
1114*77c1e3ccSAndroid Build Coastguard Worker                             PREDICTION_MODE mode,
1115*77c1e3ccSAndroid Build Coastguard Worker                             MV_REFERENCE_FRAME ref_frame[2], int_mv mv[2],
1116*77c1e3ccSAndroid Build Coastguard Worker                             int_mv ref_mv[2], int_mv nearest_mv[2],
1117*77c1e3ccSAndroid Build Coastguard Worker                             int_mv near_mv[2], int is_compound, int allow_hp,
1118*77c1e3ccSAndroid Build Coastguard Worker                             aom_reader *r) {
1119*77c1e3ccSAndroid Build Coastguard Worker   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1120*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *mbmi = xd->mi[0];
1121*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_SIZE bsize = mbmi->bsize;
1122*77c1e3ccSAndroid Build Coastguard Worker   FeatureFlags *const features = &cm->features;
1123*77c1e3ccSAndroid Build Coastguard Worker   if (features->cur_frame_force_integer_mv) {
1124*77c1e3ccSAndroid Build Coastguard Worker     allow_hp = MV_SUBPEL_NONE;
1125*77c1e3ccSAndroid Build Coastguard Worker   }
1126*77c1e3ccSAndroid Build Coastguard Worker   switch (mode) {
1127*77c1e3ccSAndroid Build Coastguard Worker     case NEWMV: {
1128*77c1e3ccSAndroid Build Coastguard Worker       nmv_context *const nmvc = &ec_ctx->nmvc;
1129*77c1e3ccSAndroid Build Coastguard Worker       read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, allow_hp);
1130*77c1e3ccSAndroid Build Coastguard Worker       break;
1131*77c1e3ccSAndroid Build Coastguard Worker     }
1132*77c1e3ccSAndroid Build Coastguard Worker     case NEARESTMV: {
1133*77c1e3ccSAndroid Build Coastguard Worker       mv[0].as_int = nearest_mv[0].as_int;
1134*77c1e3ccSAndroid Build Coastguard Worker       break;
1135*77c1e3ccSAndroid Build Coastguard Worker     }
1136*77c1e3ccSAndroid Build Coastguard Worker     case NEARMV: {
1137*77c1e3ccSAndroid Build Coastguard Worker       mv[0].as_int = near_mv[0].as_int;
1138*77c1e3ccSAndroid Build Coastguard Worker       break;
1139*77c1e3ccSAndroid Build Coastguard Worker     }
1140*77c1e3ccSAndroid Build Coastguard Worker     case GLOBALMV: {
1141*77c1e3ccSAndroid Build Coastguard Worker       mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
1142*77c1e3ccSAndroid Build Coastguard Worker                                           features->allow_high_precision_mv,
1143*77c1e3ccSAndroid Build Coastguard Worker                                           bsize, xd->mi_col, xd->mi_row,
1144*77c1e3ccSAndroid Build Coastguard Worker                                           features->cur_frame_force_integer_mv)
1145*77c1e3ccSAndroid Build Coastguard Worker                          .as_int;
1146*77c1e3ccSAndroid Build Coastguard Worker       break;
1147*77c1e3ccSAndroid Build Coastguard Worker     }
1148*77c1e3ccSAndroid Build Coastguard Worker     case NEW_NEWMV: {
1149*77c1e3ccSAndroid Build Coastguard Worker       assert(is_compound);
1150*77c1e3ccSAndroid Build Coastguard Worker       for (int i = 0; i < 2; ++i) {
1151*77c1e3ccSAndroid Build Coastguard Worker         nmv_context *const nmvc = &ec_ctx->nmvc;
1152*77c1e3ccSAndroid Build Coastguard Worker         read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, nmvc, allow_hp);
1153*77c1e3ccSAndroid Build Coastguard Worker       }
1154*77c1e3ccSAndroid Build Coastguard Worker       break;
1155*77c1e3ccSAndroid Build Coastguard Worker     }
1156*77c1e3ccSAndroid Build Coastguard Worker     case NEAREST_NEARESTMV: {
1157*77c1e3ccSAndroid Build Coastguard Worker       assert(is_compound);
1158*77c1e3ccSAndroid Build Coastguard Worker       mv[0].as_int = nearest_mv[0].as_int;
1159*77c1e3ccSAndroid Build Coastguard Worker       mv[1].as_int = nearest_mv[1].as_int;
1160*77c1e3ccSAndroid Build Coastguard Worker       break;
1161*77c1e3ccSAndroid Build Coastguard Worker     }
1162*77c1e3ccSAndroid Build Coastguard Worker     case NEAR_NEARMV: {
1163*77c1e3ccSAndroid Build Coastguard Worker       assert(is_compound);
1164*77c1e3ccSAndroid Build Coastguard Worker       mv[0].as_int = near_mv[0].as_int;
1165*77c1e3ccSAndroid Build Coastguard Worker       mv[1].as_int = near_mv[1].as_int;
1166*77c1e3ccSAndroid Build Coastguard Worker       break;
1167*77c1e3ccSAndroid Build Coastguard Worker     }
1168*77c1e3ccSAndroid Build Coastguard Worker     case NEW_NEARESTMV: {
1169*77c1e3ccSAndroid Build Coastguard Worker       nmv_context *const nmvc = &ec_ctx->nmvc;
1170*77c1e3ccSAndroid Build Coastguard Worker       read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, allow_hp);
1171*77c1e3ccSAndroid Build Coastguard Worker       assert(is_compound);
1172*77c1e3ccSAndroid Build Coastguard Worker       mv[1].as_int = nearest_mv[1].as_int;
1173*77c1e3ccSAndroid Build Coastguard Worker       break;
1174*77c1e3ccSAndroid Build Coastguard Worker     }
1175*77c1e3ccSAndroid Build Coastguard Worker     case NEAREST_NEWMV: {
1176*77c1e3ccSAndroid Build Coastguard Worker       nmv_context *const nmvc = &ec_ctx->nmvc;
1177*77c1e3ccSAndroid Build Coastguard Worker       mv[0].as_int = nearest_mv[0].as_int;
1178*77c1e3ccSAndroid Build Coastguard Worker       read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, allow_hp);
1179*77c1e3ccSAndroid Build Coastguard Worker       assert(is_compound);
1180*77c1e3ccSAndroid Build Coastguard Worker       break;
1181*77c1e3ccSAndroid Build Coastguard Worker     }
1182*77c1e3ccSAndroid Build Coastguard Worker     case NEAR_NEWMV: {
1183*77c1e3ccSAndroid Build Coastguard Worker       nmv_context *const nmvc = &ec_ctx->nmvc;
1184*77c1e3ccSAndroid Build Coastguard Worker       mv[0].as_int = near_mv[0].as_int;
1185*77c1e3ccSAndroid Build Coastguard Worker       read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, allow_hp);
1186*77c1e3ccSAndroid Build Coastguard Worker       assert(is_compound);
1187*77c1e3ccSAndroid Build Coastguard Worker       break;
1188*77c1e3ccSAndroid Build Coastguard Worker     }
1189*77c1e3ccSAndroid Build Coastguard Worker     case NEW_NEARMV: {
1190*77c1e3ccSAndroid Build Coastguard Worker       nmv_context *const nmvc = &ec_ctx->nmvc;
1191*77c1e3ccSAndroid Build Coastguard Worker       read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, allow_hp);
1192*77c1e3ccSAndroid Build Coastguard Worker       assert(is_compound);
1193*77c1e3ccSAndroid Build Coastguard Worker       mv[1].as_int = near_mv[1].as_int;
1194*77c1e3ccSAndroid Build Coastguard Worker       break;
1195*77c1e3ccSAndroid Build Coastguard Worker     }
1196*77c1e3ccSAndroid Build Coastguard Worker     case GLOBAL_GLOBALMV: {
1197*77c1e3ccSAndroid Build Coastguard Worker       assert(is_compound);
1198*77c1e3ccSAndroid Build Coastguard Worker       mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
1199*77c1e3ccSAndroid Build Coastguard Worker                                           features->allow_high_precision_mv,
1200*77c1e3ccSAndroid Build Coastguard Worker                                           bsize, xd->mi_col, xd->mi_row,
1201*77c1e3ccSAndroid Build Coastguard Worker                                           features->cur_frame_force_integer_mv)
1202*77c1e3ccSAndroid Build Coastguard Worker                          .as_int;
1203*77c1e3ccSAndroid Build Coastguard Worker       mv[1].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[1]],
1204*77c1e3ccSAndroid Build Coastguard Worker                                           features->allow_high_precision_mv,
1205*77c1e3ccSAndroid Build Coastguard Worker                                           bsize, xd->mi_col, xd->mi_row,
1206*77c1e3ccSAndroid Build Coastguard Worker                                           features->cur_frame_force_integer_mv)
1207*77c1e3ccSAndroid Build Coastguard Worker                          .as_int;
1208*77c1e3ccSAndroid Build Coastguard Worker       break;
1209*77c1e3ccSAndroid Build Coastguard Worker     }
1210*77c1e3ccSAndroid Build Coastguard Worker     default: {
1211*77c1e3ccSAndroid Build Coastguard Worker       return 0;
1212*77c1e3ccSAndroid Build Coastguard Worker     }
1213*77c1e3ccSAndroid Build Coastguard Worker   }
1214*77c1e3ccSAndroid Build Coastguard Worker 
1215*77c1e3ccSAndroid Build Coastguard Worker   int ret = is_mv_valid(&mv[0].as_mv);
1216*77c1e3ccSAndroid Build Coastguard Worker   if (is_compound) {
1217*77c1e3ccSAndroid Build Coastguard Worker     ret = ret && is_mv_valid(&mv[1].as_mv);
1218*77c1e3ccSAndroid Build Coastguard Worker   }
1219*77c1e3ccSAndroid Build Coastguard Worker   return ret;
1220*77c1e3ccSAndroid Build Coastguard Worker }
1221*77c1e3ccSAndroid Build Coastguard Worker 
read_is_inter_block(AV1_COMMON * const cm,MACROBLOCKD * const xd,int segment_id,aom_reader * r)1222*77c1e3ccSAndroid Build Coastguard Worker static int read_is_inter_block(AV1_COMMON *const cm, MACROBLOCKD *const xd,
1223*77c1e3ccSAndroid Build Coastguard Worker                                int segment_id, aom_reader *r) {
1224*77c1e3ccSAndroid Build Coastguard Worker   if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
1225*77c1e3ccSAndroid Build Coastguard Worker     const int frame = get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME);
1226*77c1e3ccSAndroid Build Coastguard Worker     if (frame < LAST_FRAME) return 0;
1227*77c1e3ccSAndroid Build Coastguard Worker     return frame != INTRA_FRAME;
1228*77c1e3ccSAndroid Build Coastguard Worker   }
1229*77c1e3ccSAndroid Build Coastguard Worker   if (segfeature_active(&cm->seg, segment_id, SEG_LVL_GLOBALMV)) {
1230*77c1e3ccSAndroid Build Coastguard Worker     return 1;
1231*77c1e3ccSAndroid Build Coastguard Worker   }
1232*77c1e3ccSAndroid Build Coastguard Worker   const int ctx = av1_get_intra_inter_context(xd);
1233*77c1e3ccSAndroid Build Coastguard Worker   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1234*77c1e3ccSAndroid Build Coastguard Worker   const int is_inter =
1235*77c1e3ccSAndroid Build Coastguard Worker       aom_read_symbol(r, ec_ctx->intra_inter_cdf[ctx], 2, ACCT_STR);
1236*77c1e3ccSAndroid Build Coastguard Worker   return is_inter;
1237*77c1e3ccSAndroid Build Coastguard Worker }
1238*77c1e3ccSAndroid Build Coastguard Worker 
1239*77c1e3ccSAndroid Build Coastguard Worker #if DEC_MISMATCH_DEBUG
dec_dump_logs(AV1_COMMON * cm,MB_MODE_INFO * const mbmi,int mi_row,int mi_col,int16_t mode_ctx)1240*77c1e3ccSAndroid Build Coastguard Worker static void dec_dump_logs(AV1_COMMON *cm, MB_MODE_INFO *const mbmi, int mi_row,
1241*77c1e3ccSAndroid Build Coastguard Worker                           int mi_col, int16_t mode_ctx) {
1242*77c1e3ccSAndroid Build Coastguard Worker   int_mv mv[2] = { { 0 } };
1243*77c1e3ccSAndroid Build Coastguard Worker   for (int ref = 0; ref < 1 + has_second_ref(mbmi); ++ref)
1244*77c1e3ccSAndroid Build Coastguard Worker     mv[ref].as_mv = mbmi->mv[ref].as_mv;
1245*77c1e3ccSAndroid Build Coastguard Worker 
1246*77c1e3ccSAndroid Build Coastguard Worker   const int16_t newmv_ctx = mode_ctx & NEWMV_CTX_MASK;
1247*77c1e3ccSAndroid Build Coastguard Worker   int16_t zeromv_ctx = -1;
1248*77c1e3ccSAndroid Build Coastguard Worker   int16_t refmv_ctx = -1;
1249*77c1e3ccSAndroid Build Coastguard Worker   if (mbmi->mode != NEWMV) {
1250*77c1e3ccSAndroid Build Coastguard Worker     zeromv_ctx = (mode_ctx >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
1251*77c1e3ccSAndroid Build Coastguard Worker     if (mbmi->mode != GLOBALMV)
1252*77c1e3ccSAndroid Build Coastguard Worker       refmv_ctx = (mode_ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
1253*77c1e3ccSAndroid Build Coastguard Worker   }
1254*77c1e3ccSAndroid Build Coastguard Worker 
1255*77c1e3ccSAndroid Build Coastguard Worker #define FRAME_TO_CHECK 11
1256*77c1e3ccSAndroid Build Coastguard Worker   if (cm->current_frame.frame_number == FRAME_TO_CHECK && cm->show_frame == 1) {
1257*77c1e3ccSAndroid Build Coastguard Worker     printf(
1258*77c1e3ccSAndroid Build Coastguard Worker         "=== DECODER ===: "
1259*77c1e3ccSAndroid Build Coastguard Worker         "Frame=%d, (mi_row,mi_col)=(%d,%d), skip_mode=%d, mode=%d, bsize=%d, "
1260*77c1e3ccSAndroid Build Coastguard Worker         "show_frame=%d, mv[0]=(%d,%d), mv[1]=(%d,%d), ref[0]=%d, "
1261*77c1e3ccSAndroid Build Coastguard Worker         "ref[1]=%d, motion_mode=%d, mode_ctx=%d, "
1262*77c1e3ccSAndroid Build Coastguard Worker         "newmv_ctx=%d, zeromv_ctx=%d, refmv_ctx=%d, tx_size=%d\n",
1263*77c1e3ccSAndroid Build Coastguard Worker         cm->current_frame.frame_number, mi_row, mi_col, mbmi->skip_mode,
1264*77c1e3ccSAndroid Build Coastguard Worker         mbmi->mode, mbmi->sb_type, cm->show_frame, mv[0].as_mv.row,
1265*77c1e3ccSAndroid Build Coastguard Worker         mv[0].as_mv.col, mv[1].as_mv.row, mv[1].as_mv.col, mbmi->ref_frame[0],
1266*77c1e3ccSAndroid Build Coastguard Worker         mbmi->ref_frame[1], mbmi->motion_mode, mode_ctx, newmv_ctx, zeromv_ctx,
1267*77c1e3ccSAndroid Build Coastguard Worker         refmv_ctx, mbmi->tx_size);
1268*77c1e3ccSAndroid Build Coastguard Worker   }
1269*77c1e3ccSAndroid Build Coastguard Worker }
1270*77c1e3ccSAndroid Build Coastguard Worker #endif  // DEC_MISMATCH_DEBUG
1271*77c1e3ccSAndroid Build Coastguard Worker 
read_inter_block_mode_info(AV1Decoder * const pbi,DecoderCodingBlock * dcb,MB_MODE_INFO * const mbmi,aom_reader * r)1272*77c1e3ccSAndroid Build Coastguard Worker static void read_inter_block_mode_info(AV1Decoder *const pbi,
1273*77c1e3ccSAndroid Build Coastguard Worker                                        DecoderCodingBlock *dcb,
1274*77c1e3ccSAndroid Build Coastguard Worker                                        MB_MODE_INFO *const mbmi,
1275*77c1e3ccSAndroid Build Coastguard Worker                                        aom_reader *r) {
1276*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &pbi->common;
1277*77c1e3ccSAndroid Build Coastguard Worker   FeatureFlags *const features = &cm->features;
1278*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize = mbmi->bsize;
1279*77c1e3ccSAndroid Build Coastguard Worker   const int allow_hp = features->allow_high_precision_mv;
1280*77c1e3ccSAndroid Build Coastguard Worker   int_mv nearestmv[2], nearmv[2];
1281*77c1e3ccSAndroid Build Coastguard Worker   int_mv ref_mvs[MODE_CTX_REF_FRAMES][MAX_MV_REF_CANDIDATES] = { { { 0 } } };
1282*77c1e3ccSAndroid Build Coastguard Worker   int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
1283*77c1e3ccSAndroid Build Coastguard Worker   int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
1284*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &dcb->xd;
1285*77c1e3ccSAndroid Build Coastguard Worker   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1286*77c1e3ccSAndroid Build Coastguard Worker 
1287*77c1e3ccSAndroid Build Coastguard Worker   mbmi->uv_mode = UV_DC_PRED;
1288*77c1e3ccSAndroid Build Coastguard Worker   mbmi->palette_mode_info.palette_size[0] = 0;
1289*77c1e3ccSAndroid Build Coastguard Worker   mbmi->palette_mode_info.palette_size[1] = 0;
1290*77c1e3ccSAndroid Build Coastguard Worker 
1291*77c1e3ccSAndroid Build Coastguard Worker   av1_collect_neighbors_ref_counts(xd);
1292*77c1e3ccSAndroid Build Coastguard Worker 
1293*77c1e3ccSAndroid Build Coastguard Worker   read_ref_frames(cm, xd, r, mbmi->segment_id, mbmi->ref_frame);
1294*77c1e3ccSAndroid Build Coastguard Worker   const int is_compound = has_second_ref(mbmi);
1295*77c1e3ccSAndroid Build Coastguard Worker 
1296*77c1e3ccSAndroid Build Coastguard Worker   const MV_REFERENCE_FRAME ref_frame = av1_ref_frame_type(mbmi->ref_frame);
1297*77c1e3ccSAndroid Build Coastguard Worker   av1_find_mv_refs(cm, xd, mbmi, ref_frame, dcb->ref_mv_count, xd->ref_mv_stack,
1298*77c1e3ccSAndroid Build Coastguard Worker                    xd->weight, ref_mvs, /*global_mvs=*/NULL, inter_mode_ctx);
1299*77c1e3ccSAndroid Build Coastguard Worker 
1300*77c1e3ccSAndroid Build Coastguard Worker   mbmi->ref_mv_idx = 0;
1301*77c1e3ccSAndroid Build Coastguard Worker 
1302*77c1e3ccSAndroid Build Coastguard Worker   if (mbmi->skip_mode) {
1303*77c1e3ccSAndroid Build Coastguard Worker     assert(is_compound);
1304*77c1e3ccSAndroid Build Coastguard Worker     mbmi->mode = NEAREST_NEARESTMV;
1305*77c1e3ccSAndroid Build Coastguard Worker   } else {
1306*77c1e3ccSAndroid Build Coastguard Worker     if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP) ||
1307*77c1e3ccSAndroid Build Coastguard Worker         segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_GLOBALMV)) {
1308*77c1e3ccSAndroid Build Coastguard Worker       mbmi->mode = GLOBALMV;
1309*77c1e3ccSAndroid Build Coastguard Worker     } else {
1310*77c1e3ccSAndroid Build Coastguard Worker       const int mode_ctx =
1311*77c1e3ccSAndroid Build Coastguard Worker           av1_mode_context_analyzer(inter_mode_ctx, mbmi->ref_frame);
1312*77c1e3ccSAndroid Build Coastguard Worker       if (is_compound)
1313*77c1e3ccSAndroid Build Coastguard Worker         mbmi->mode = read_inter_compound_mode(xd, r, mode_ctx);
1314*77c1e3ccSAndroid Build Coastguard Worker       else
1315*77c1e3ccSAndroid Build Coastguard Worker         mbmi->mode = read_inter_mode(ec_ctx, r, mode_ctx);
1316*77c1e3ccSAndroid Build Coastguard Worker       if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV ||
1317*77c1e3ccSAndroid Build Coastguard Worker           have_nearmv_in_inter_mode(mbmi->mode))
1318*77c1e3ccSAndroid Build Coastguard Worker         read_drl_idx(ec_ctx, dcb, mbmi, r);
1319*77c1e3ccSAndroid Build Coastguard Worker     }
1320*77c1e3ccSAndroid Build Coastguard Worker   }
1321*77c1e3ccSAndroid Build Coastguard Worker 
1322*77c1e3ccSAndroid Build Coastguard Worker   if (is_compound != is_inter_compound_mode(mbmi->mode)) {
1323*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
1324*77c1e3ccSAndroid Build Coastguard Worker                        "Prediction mode %d invalid with ref frame %d %d",
1325*77c1e3ccSAndroid Build Coastguard Worker                        mbmi->mode, mbmi->ref_frame[0], mbmi->ref_frame[1]);
1326*77c1e3ccSAndroid Build Coastguard Worker   }
1327*77c1e3ccSAndroid Build Coastguard Worker 
1328*77c1e3ccSAndroid Build Coastguard Worker   if (!is_compound && mbmi->mode != GLOBALMV) {
1329*77c1e3ccSAndroid Build Coastguard Worker     av1_find_best_ref_mvs(allow_hp, ref_mvs[mbmi->ref_frame[0]], &nearestmv[0],
1330*77c1e3ccSAndroid Build Coastguard Worker                           &nearmv[0], features->cur_frame_force_integer_mv);
1331*77c1e3ccSAndroid Build Coastguard Worker   }
1332*77c1e3ccSAndroid Build Coastguard Worker 
1333*77c1e3ccSAndroid Build Coastguard Worker   if (is_compound && mbmi->mode != GLOBAL_GLOBALMV) {
1334*77c1e3ccSAndroid Build Coastguard Worker     const int ref_mv_idx = mbmi->ref_mv_idx + 1;
1335*77c1e3ccSAndroid Build Coastguard Worker     nearestmv[0] = xd->ref_mv_stack[ref_frame][0].this_mv;
1336*77c1e3ccSAndroid Build Coastguard Worker     nearestmv[1] = xd->ref_mv_stack[ref_frame][0].comp_mv;
1337*77c1e3ccSAndroid Build Coastguard Worker     nearmv[0] = xd->ref_mv_stack[ref_frame][ref_mv_idx].this_mv;
1338*77c1e3ccSAndroid Build Coastguard Worker     nearmv[1] = xd->ref_mv_stack[ref_frame][ref_mv_idx].comp_mv;
1339*77c1e3ccSAndroid Build Coastguard Worker     lower_mv_precision(&nearestmv[0].as_mv, allow_hp,
1340*77c1e3ccSAndroid Build Coastguard Worker                        features->cur_frame_force_integer_mv);
1341*77c1e3ccSAndroid Build Coastguard Worker     lower_mv_precision(&nearestmv[1].as_mv, allow_hp,
1342*77c1e3ccSAndroid Build Coastguard Worker                        features->cur_frame_force_integer_mv);
1343*77c1e3ccSAndroid Build Coastguard Worker     lower_mv_precision(&nearmv[0].as_mv, allow_hp,
1344*77c1e3ccSAndroid Build Coastguard Worker                        features->cur_frame_force_integer_mv);
1345*77c1e3ccSAndroid Build Coastguard Worker     lower_mv_precision(&nearmv[1].as_mv, allow_hp,
1346*77c1e3ccSAndroid Build Coastguard Worker                        features->cur_frame_force_integer_mv);
1347*77c1e3ccSAndroid Build Coastguard Worker   } else if (mbmi->ref_mv_idx > 0 && mbmi->mode == NEARMV) {
1348*77c1e3ccSAndroid Build Coastguard Worker     nearmv[0] =
1349*77c1e3ccSAndroid Build Coastguard Worker         xd->ref_mv_stack[mbmi->ref_frame[0]][1 + mbmi->ref_mv_idx].this_mv;
1350*77c1e3ccSAndroid Build Coastguard Worker   }
1351*77c1e3ccSAndroid Build Coastguard Worker 
1352*77c1e3ccSAndroid Build Coastguard Worker   int_mv ref_mv[2] = { nearestmv[0], nearestmv[1] };
1353*77c1e3ccSAndroid Build Coastguard Worker 
1354*77c1e3ccSAndroid Build Coastguard Worker   if (is_compound) {
1355*77c1e3ccSAndroid Build Coastguard Worker     int ref_mv_idx = mbmi->ref_mv_idx;
1356*77c1e3ccSAndroid Build Coastguard Worker     // Special case: NEAR_NEWMV and NEW_NEARMV modes use
1357*77c1e3ccSAndroid Build Coastguard Worker     // 1 + mbmi->ref_mv_idx (like NEARMV) instead of
1358*77c1e3ccSAndroid Build Coastguard Worker     // mbmi->ref_mv_idx (like NEWMV)
1359*77c1e3ccSAndroid Build Coastguard Worker     if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV)
1360*77c1e3ccSAndroid Build Coastguard Worker       ref_mv_idx = 1 + mbmi->ref_mv_idx;
1361*77c1e3ccSAndroid Build Coastguard Worker 
1362*77c1e3ccSAndroid Build Coastguard Worker     // TODO(jingning, yunqing): Do we need a lower_mv_precision() call here?
1363*77c1e3ccSAndroid Build Coastguard Worker     if (compound_ref0_mode(mbmi->mode) == NEWMV)
1364*77c1e3ccSAndroid Build Coastguard Worker       ref_mv[0] = xd->ref_mv_stack[ref_frame][ref_mv_idx].this_mv;
1365*77c1e3ccSAndroid Build Coastguard Worker 
1366*77c1e3ccSAndroid Build Coastguard Worker     if (compound_ref1_mode(mbmi->mode) == NEWMV)
1367*77c1e3ccSAndroid Build Coastguard Worker       ref_mv[1] = xd->ref_mv_stack[ref_frame][ref_mv_idx].comp_mv;
1368*77c1e3ccSAndroid Build Coastguard Worker   } else {
1369*77c1e3ccSAndroid Build Coastguard Worker     if (mbmi->mode == NEWMV) {
1370*77c1e3ccSAndroid Build Coastguard Worker       if (dcb->ref_mv_count[ref_frame] > 1)
1371*77c1e3ccSAndroid Build Coastguard Worker         ref_mv[0] = xd->ref_mv_stack[ref_frame][mbmi->ref_mv_idx].this_mv;
1372*77c1e3ccSAndroid Build Coastguard Worker     }
1373*77c1e3ccSAndroid Build Coastguard Worker   }
1374*77c1e3ccSAndroid Build Coastguard Worker 
1375*77c1e3ccSAndroid Build Coastguard Worker   if (mbmi->skip_mode) assert(mbmi->mode == NEAREST_NEARESTMV);
1376*77c1e3ccSAndroid Build Coastguard Worker 
1377*77c1e3ccSAndroid Build Coastguard Worker   const int mv_corrupted_flag =
1378*77c1e3ccSAndroid Build Coastguard Worker       !assign_mv(cm, xd, mbmi->mode, mbmi->ref_frame, mbmi->mv, ref_mv,
1379*77c1e3ccSAndroid Build Coastguard Worker                  nearestmv, nearmv, is_compound, allow_hp, r);
1380*77c1e3ccSAndroid Build Coastguard Worker   aom_merge_corrupted_flag(&dcb->corrupted, mv_corrupted_flag);
1381*77c1e3ccSAndroid Build Coastguard Worker 
1382*77c1e3ccSAndroid Build Coastguard Worker   mbmi->use_wedge_interintra = 0;
1383*77c1e3ccSAndroid Build Coastguard Worker   if (cm->seq_params->enable_interintra_compound && !mbmi->skip_mode &&
1384*77c1e3ccSAndroid Build Coastguard Worker       is_interintra_allowed(mbmi)) {
1385*77c1e3ccSAndroid Build Coastguard Worker     const int bsize_group = size_group_lookup[bsize];
1386*77c1e3ccSAndroid Build Coastguard Worker     const int interintra =
1387*77c1e3ccSAndroid Build Coastguard Worker         aom_read_symbol(r, ec_ctx->interintra_cdf[bsize_group], 2, ACCT_STR);
1388*77c1e3ccSAndroid Build Coastguard Worker     assert(mbmi->ref_frame[1] == NONE_FRAME);
1389*77c1e3ccSAndroid Build Coastguard Worker     if (interintra) {
1390*77c1e3ccSAndroid Build Coastguard Worker       const INTERINTRA_MODE interintra_mode =
1391*77c1e3ccSAndroid Build Coastguard Worker           read_interintra_mode(xd, r, bsize_group);
1392*77c1e3ccSAndroid Build Coastguard Worker       mbmi->ref_frame[1] = INTRA_FRAME;
1393*77c1e3ccSAndroid Build Coastguard Worker       mbmi->interintra_mode = interintra_mode;
1394*77c1e3ccSAndroid Build Coastguard Worker       mbmi->angle_delta[PLANE_TYPE_Y] = 0;
1395*77c1e3ccSAndroid Build Coastguard Worker       mbmi->angle_delta[PLANE_TYPE_UV] = 0;
1396*77c1e3ccSAndroid Build Coastguard Worker       mbmi->filter_intra_mode_info.use_filter_intra = 0;
1397*77c1e3ccSAndroid Build Coastguard Worker       if (av1_is_wedge_used(bsize)) {
1398*77c1e3ccSAndroid Build Coastguard Worker         mbmi->use_wedge_interintra = aom_read_symbol(
1399*77c1e3ccSAndroid Build Coastguard Worker             r, ec_ctx->wedge_interintra_cdf[bsize], 2, ACCT_STR);
1400*77c1e3ccSAndroid Build Coastguard Worker         if (mbmi->use_wedge_interintra) {
1401*77c1e3ccSAndroid Build Coastguard Worker           mbmi->interintra_wedge_index = (int8_t)aom_read_symbol(
1402*77c1e3ccSAndroid Build Coastguard Worker               r, ec_ctx->wedge_idx_cdf[bsize], MAX_WEDGE_TYPES, ACCT_STR);
1403*77c1e3ccSAndroid Build Coastguard Worker         }
1404*77c1e3ccSAndroid Build Coastguard Worker       }
1405*77c1e3ccSAndroid Build Coastguard Worker     }
1406*77c1e3ccSAndroid Build Coastguard Worker   }
1407*77c1e3ccSAndroid Build Coastguard Worker 
1408*77c1e3ccSAndroid Build Coastguard Worker   for (int ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
1409*77c1e3ccSAndroid Build Coastguard Worker     const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
1410*77c1e3ccSAndroid Build Coastguard Worker     xd->block_ref_scale_factors[ref] = get_ref_scale_factors_const(cm, frame);
1411*77c1e3ccSAndroid Build Coastguard Worker   }
1412*77c1e3ccSAndroid Build Coastguard Worker 
1413*77c1e3ccSAndroid Build Coastguard Worker   mbmi->motion_mode = SIMPLE_TRANSLATION;
1414*77c1e3ccSAndroid Build Coastguard Worker   if (is_motion_variation_allowed_bsize(mbmi->bsize) && !mbmi->skip_mode &&
1415*77c1e3ccSAndroid Build Coastguard Worker       !has_second_ref(mbmi)) {
1416*77c1e3ccSAndroid Build Coastguard Worker     mbmi->num_proj_ref = av1_findSamples(cm, xd, pts, pts_inref);
1417*77c1e3ccSAndroid Build Coastguard Worker   }
1418*77c1e3ccSAndroid Build Coastguard Worker   av1_count_overlappable_neighbors(cm, xd);
1419*77c1e3ccSAndroid Build Coastguard Worker 
1420*77c1e3ccSAndroid Build Coastguard Worker   if (mbmi->ref_frame[1] != INTRA_FRAME)
1421*77c1e3ccSAndroid Build Coastguard Worker     mbmi->motion_mode = read_motion_mode(cm, xd, mbmi, r);
1422*77c1e3ccSAndroid Build Coastguard Worker 
1423*77c1e3ccSAndroid Build Coastguard Worker   // init
1424*77c1e3ccSAndroid Build Coastguard Worker   mbmi->comp_group_idx = 0;
1425*77c1e3ccSAndroid Build Coastguard Worker   mbmi->compound_idx = 1;
1426*77c1e3ccSAndroid Build Coastguard Worker   mbmi->interinter_comp.type = COMPOUND_AVERAGE;
1427*77c1e3ccSAndroid Build Coastguard Worker 
1428*77c1e3ccSAndroid Build Coastguard Worker   if (has_second_ref(mbmi) && !mbmi->skip_mode) {
1429*77c1e3ccSAndroid Build Coastguard Worker     // Read idx to indicate current compound inter prediction mode group
1430*77c1e3ccSAndroid Build Coastguard Worker     const int masked_compound_used = is_any_masked_compound_used(bsize) &&
1431*77c1e3ccSAndroid Build Coastguard Worker                                      cm->seq_params->enable_masked_compound;
1432*77c1e3ccSAndroid Build Coastguard Worker 
1433*77c1e3ccSAndroid Build Coastguard Worker     if (masked_compound_used) {
1434*77c1e3ccSAndroid Build Coastguard Worker       const int ctx_comp_group_idx = get_comp_group_idx_context(xd);
1435*77c1e3ccSAndroid Build Coastguard Worker       mbmi->comp_group_idx = (uint8_t)aom_read_symbol(
1436*77c1e3ccSAndroid Build Coastguard Worker           r, ec_ctx->comp_group_idx_cdf[ctx_comp_group_idx], 2, ACCT_STR);
1437*77c1e3ccSAndroid Build Coastguard Worker     }
1438*77c1e3ccSAndroid Build Coastguard Worker 
1439*77c1e3ccSAndroid Build Coastguard Worker     if (mbmi->comp_group_idx == 0) {
1440*77c1e3ccSAndroid Build Coastguard Worker       if (cm->seq_params->order_hint_info.enable_dist_wtd_comp) {
1441*77c1e3ccSAndroid Build Coastguard Worker         const int comp_index_ctx = get_comp_index_context(cm, xd);
1442*77c1e3ccSAndroid Build Coastguard Worker         mbmi->compound_idx = (uint8_t)aom_read_symbol(
1443*77c1e3ccSAndroid Build Coastguard Worker             r, ec_ctx->compound_index_cdf[comp_index_ctx], 2, ACCT_STR);
1444*77c1e3ccSAndroid Build Coastguard Worker         mbmi->interinter_comp.type =
1445*77c1e3ccSAndroid Build Coastguard Worker             mbmi->compound_idx ? COMPOUND_AVERAGE : COMPOUND_DISTWTD;
1446*77c1e3ccSAndroid Build Coastguard Worker       } else {
1447*77c1e3ccSAndroid Build Coastguard Worker         // Distance-weighted compound is disabled, so always use average
1448*77c1e3ccSAndroid Build Coastguard Worker         mbmi->compound_idx = 1;
1449*77c1e3ccSAndroid Build Coastguard Worker         mbmi->interinter_comp.type = COMPOUND_AVERAGE;
1450*77c1e3ccSAndroid Build Coastguard Worker       }
1451*77c1e3ccSAndroid Build Coastguard Worker     } else {
1452*77c1e3ccSAndroid Build Coastguard Worker       assert(cm->current_frame.reference_mode != SINGLE_REFERENCE &&
1453*77c1e3ccSAndroid Build Coastguard Worker              is_inter_compound_mode(mbmi->mode) &&
1454*77c1e3ccSAndroid Build Coastguard Worker              mbmi->motion_mode == SIMPLE_TRANSLATION);
1455*77c1e3ccSAndroid Build Coastguard Worker       assert(masked_compound_used);
1456*77c1e3ccSAndroid Build Coastguard Worker 
1457*77c1e3ccSAndroid Build Coastguard Worker       // compound_diffwtd, wedge
1458*77c1e3ccSAndroid Build Coastguard Worker       if (is_interinter_compound_used(COMPOUND_WEDGE, bsize)) {
1459*77c1e3ccSAndroid Build Coastguard Worker         mbmi->interinter_comp.type =
1460*77c1e3ccSAndroid Build Coastguard Worker             COMPOUND_WEDGE + aom_read_symbol(r,
1461*77c1e3ccSAndroid Build Coastguard Worker                                              ec_ctx->compound_type_cdf[bsize],
1462*77c1e3ccSAndroid Build Coastguard Worker                                              MASKED_COMPOUND_TYPES, ACCT_STR);
1463*77c1e3ccSAndroid Build Coastguard Worker       } else {
1464*77c1e3ccSAndroid Build Coastguard Worker         mbmi->interinter_comp.type = COMPOUND_DIFFWTD;
1465*77c1e3ccSAndroid Build Coastguard Worker       }
1466*77c1e3ccSAndroid Build Coastguard Worker 
1467*77c1e3ccSAndroid Build Coastguard Worker       if (mbmi->interinter_comp.type == COMPOUND_WEDGE) {
1468*77c1e3ccSAndroid Build Coastguard Worker         assert(is_interinter_compound_used(COMPOUND_WEDGE, bsize));
1469*77c1e3ccSAndroid Build Coastguard Worker         mbmi->interinter_comp.wedge_index = (int8_t)aom_read_symbol(
1470*77c1e3ccSAndroid Build Coastguard Worker             r, ec_ctx->wedge_idx_cdf[bsize], MAX_WEDGE_TYPES, ACCT_STR);
1471*77c1e3ccSAndroid Build Coastguard Worker         mbmi->interinter_comp.wedge_sign = (int8_t)aom_read_bit(r, ACCT_STR);
1472*77c1e3ccSAndroid Build Coastguard Worker       } else {
1473*77c1e3ccSAndroid Build Coastguard Worker         assert(mbmi->interinter_comp.type == COMPOUND_DIFFWTD);
1474*77c1e3ccSAndroid Build Coastguard Worker         mbmi->interinter_comp.mask_type =
1475*77c1e3ccSAndroid Build Coastguard Worker             aom_read_literal(r, MAX_DIFFWTD_MASK_BITS, ACCT_STR);
1476*77c1e3ccSAndroid Build Coastguard Worker       }
1477*77c1e3ccSAndroid Build Coastguard Worker     }
1478*77c1e3ccSAndroid Build Coastguard Worker   }
1479*77c1e3ccSAndroid Build Coastguard Worker 
1480*77c1e3ccSAndroid Build Coastguard Worker   read_mb_interp_filter(xd, features->interp_filter,
1481*77c1e3ccSAndroid Build Coastguard Worker                         cm->seq_params->enable_dual_filter, mbmi, r);
1482*77c1e3ccSAndroid Build Coastguard Worker 
1483*77c1e3ccSAndroid Build Coastguard Worker   if (mbmi->motion_mode == WARPED_CAUSAL) {
1484*77c1e3ccSAndroid Build Coastguard Worker     const int mi_row = xd->mi_row;
1485*77c1e3ccSAndroid Build Coastguard Worker     const int mi_col = xd->mi_col;
1486*77c1e3ccSAndroid Build Coastguard Worker     mbmi->wm_params.wmtype = DEFAULT_WMTYPE;
1487*77c1e3ccSAndroid Build Coastguard Worker     mbmi->wm_params.invalid = 0;
1488*77c1e3ccSAndroid Build Coastguard Worker 
1489*77c1e3ccSAndroid Build Coastguard Worker     if (mbmi->num_proj_ref > 1) {
1490*77c1e3ccSAndroid Build Coastguard Worker       mbmi->num_proj_ref = av1_selectSamples(&mbmi->mv[0].as_mv, pts, pts_inref,
1491*77c1e3ccSAndroid Build Coastguard Worker                                              mbmi->num_proj_ref, bsize);
1492*77c1e3ccSAndroid Build Coastguard Worker     }
1493*77c1e3ccSAndroid Build Coastguard Worker 
1494*77c1e3ccSAndroid Build Coastguard Worker     if (av1_find_projection(mbmi->num_proj_ref, pts, pts_inref, bsize,
1495*77c1e3ccSAndroid Build Coastguard Worker                             mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col,
1496*77c1e3ccSAndroid Build Coastguard Worker                             &mbmi->wm_params, mi_row, mi_col)) {
1497*77c1e3ccSAndroid Build Coastguard Worker #if WARPED_MOTION_DEBUG
1498*77c1e3ccSAndroid Build Coastguard Worker       printf("Warning: unexpected warped model from aomenc\n");
1499*77c1e3ccSAndroid Build Coastguard Worker #endif
1500*77c1e3ccSAndroid Build Coastguard Worker       mbmi->wm_params.invalid = 1;
1501*77c1e3ccSAndroid Build Coastguard Worker     }
1502*77c1e3ccSAndroid Build Coastguard Worker   }
1503*77c1e3ccSAndroid Build Coastguard Worker 
1504*77c1e3ccSAndroid Build Coastguard Worker   xd->cfl.store_y = store_cfl_required(cm, xd);
1505*77c1e3ccSAndroid Build Coastguard Worker 
1506*77c1e3ccSAndroid Build Coastguard Worker #if DEC_MISMATCH_DEBUG
1507*77c1e3ccSAndroid Build Coastguard Worker   dec_dump_logs(cm, mi, mi_row, mi_col, mode_ctx);
1508*77c1e3ccSAndroid Build Coastguard Worker #endif  // DEC_MISMATCH_DEBUG
1509*77c1e3ccSAndroid Build Coastguard Worker }
1510*77c1e3ccSAndroid Build Coastguard Worker 
read_inter_frame_mode_info(AV1Decoder * const pbi,DecoderCodingBlock * dcb,aom_reader * r)1511*77c1e3ccSAndroid Build Coastguard Worker static void read_inter_frame_mode_info(AV1Decoder *const pbi,
1512*77c1e3ccSAndroid Build Coastguard Worker                                        DecoderCodingBlock *dcb, aom_reader *r) {
1513*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &pbi->common;
1514*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &dcb->xd;
1515*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *const mbmi = xd->mi[0];
1516*77c1e3ccSAndroid Build Coastguard Worker   int inter_block = 1;
1517*77c1e3ccSAndroid Build Coastguard Worker 
1518*77c1e3ccSAndroid Build Coastguard Worker   mbmi->mv[0].as_int = 0;
1519*77c1e3ccSAndroid Build Coastguard Worker   mbmi->mv[1].as_int = 0;
1520*77c1e3ccSAndroid Build Coastguard Worker   mbmi->segment_id = read_inter_segment_id(cm, xd, 1, r);
1521*77c1e3ccSAndroid Build Coastguard Worker 
1522*77c1e3ccSAndroid Build Coastguard Worker   mbmi->skip_mode = read_skip_mode(cm, xd, mbmi->segment_id, r);
1523*77c1e3ccSAndroid Build Coastguard Worker 
1524*77c1e3ccSAndroid Build Coastguard Worker   if (mbmi->skip_mode)
1525*77c1e3ccSAndroid Build Coastguard Worker     mbmi->skip_txfm = 1;
1526*77c1e3ccSAndroid Build Coastguard Worker   else
1527*77c1e3ccSAndroid Build Coastguard Worker     mbmi->skip_txfm = read_skip_txfm(cm, xd, mbmi->segment_id, r);
1528*77c1e3ccSAndroid Build Coastguard Worker 
1529*77c1e3ccSAndroid Build Coastguard Worker   if (!cm->seg.segid_preskip)
1530*77c1e3ccSAndroid Build Coastguard Worker     mbmi->segment_id = read_inter_segment_id(cm, xd, 0, r);
1531*77c1e3ccSAndroid Build Coastguard Worker 
1532*77c1e3ccSAndroid Build Coastguard Worker   read_cdef(cm, r, xd);
1533*77c1e3ccSAndroid Build Coastguard Worker 
1534*77c1e3ccSAndroid Build Coastguard Worker   read_delta_q_params(cm, xd, r);
1535*77c1e3ccSAndroid Build Coastguard Worker 
1536*77c1e3ccSAndroid Build Coastguard Worker   if (!mbmi->skip_mode)
1537*77c1e3ccSAndroid Build Coastguard Worker     inter_block = read_is_inter_block(cm, xd, mbmi->segment_id, r);
1538*77c1e3ccSAndroid Build Coastguard Worker 
1539*77c1e3ccSAndroid Build Coastguard Worker   mbmi->current_qindex = xd->current_base_qindex;
1540*77c1e3ccSAndroid Build Coastguard Worker 
1541*77c1e3ccSAndroid Build Coastguard Worker   xd->above_txfm_context =
1542*77c1e3ccSAndroid Build Coastguard Worker       cm->above_contexts.txfm[xd->tile.tile_row] + xd->mi_col;
1543*77c1e3ccSAndroid Build Coastguard Worker   xd->left_txfm_context =
1544*77c1e3ccSAndroid Build Coastguard Worker       xd->left_txfm_context_buffer + (xd->mi_row & MAX_MIB_MASK);
1545*77c1e3ccSAndroid Build Coastguard Worker 
1546*77c1e3ccSAndroid Build Coastguard Worker   if (inter_block)
1547*77c1e3ccSAndroid Build Coastguard Worker     read_inter_block_mode_info(pbi, dcb, mbmi, r);
1548*77c1e3ccSAndroid Build Coastguard Worker   else
1549*77c1e3ccSAndroid Build Coastguard Worker     read_intra_block_mode_info(cm, xd, mbmi, r);
1550*77c1e3ccSAndroid Build Coastguard Worker }
1551*77c1e3ccSAndroid Build Coastguard Worker 
intra_copy_frame_mvs(AV1_COMMON * const cm,int mi_row,int mi_col,int x_mis,int y_mis)1552*77c1e3ccSAndroid Build Coastguard Worker static void intra_copy_frame_mvs(AV1_COMMON *const cm, int mi_row, int mi_col,
1553*77c1e3ccSAndroid Build Coastguard Worker                                  int x_mis, int y_mis) {
1554*77c1e3ccSAndroid Build Coastguard Worker   const int frame_mvs_stride = ROUND_POWER_OF_TWO(cm->mi_params.mi_cols, 1);
1555*77c1e3ccSAndroid Build Coastguard Worker   MV_REF *frame_mvs =
1556*77c1e3ccSAndroid Build Coastguard Worker       cm->cur_frame->mvs + (mi_row >> 1) * frame_mvs_stride + (mi_col >> 1);
1557*77c1e3ccSAndroid Build Coastguard Worker   x_mis = ROUND_POWER_OF_TWO(x_mis, 1);
1558*77c1e3ccSAndroid Build Coastguard Worker   y_mis = ROUND_POWER_OF_TWO(y_mis, 1);
1559*77c1e3ccSAndroid Build Coastguard Worker 
1560*77c1e3ccSAndroid Build Coastguard Worker   for (int h = 0; h < y_mis; h++) {
1561*77c1e3ccSAndroid Build Coastguard Worker     MV_REF *mv = frame_mvs;
1562*77c1e3ccSAndroid Build Coastguard Worker     for (int w = 0; w < x_mis; w++) {
1563*77c1e3ccSAndroid Build Coastguard Worker       mv->ref_frame = NONE_FRAME;
1564*77c1e3ccSAndroid Build Coastguard Worker       mv++;
1565*77c1e3ccSAndroid Build Coastguard Worker     }
1566*77c1e3ccSAndroid Build Coastguard Worker     frame_mvs += frame_mvs_stride;
1567*77c1e3ccSAndroid Build Coastguard Worker   }
1568*77c1e3ccSAndroid Build Coastguard Worker }
1569*77c1e3ccSAndroid Build Coastguard Worker 
av1_read_mode_info(AV1Decoder * const pbi,DecoderCodingBlock * dcb,aom_reader * r,int x_mis,int y_mis)1570*77c1e3ccSAndroid Build Coastguard Worker void av1_read_mode_info(AV1Decoder *const pbi, DecoderCodingBlock *dcb,
1571*77c1e3ccSAndroid Build Coastguard Worker                         aom_reader *r, int x_mis, int y_mis) {
1572*77c1e3ccSAndroid Build Coastguard Worker   AV1_COMMON *const cm = &pbi->common;
1573*77c1e3ccSAndroid Build Coastguard Worker   MACROBLOCKD *const xd = &dcb->xd;
1574*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO *const mi = xd->mi[0];
1575*77c1e3ccSAndroid Build Coastguard Worker   mi->use_intrabc = 0;
1576*77c1e3ccSAndroid Build Coastguard Worker 
1577*77c1e3ccSAndroid Build Coastguard Worker   if (frame_is_intra_only(cm)) {
1578*77c1e3ccSAndroid Build Coastguard Worker     read_intra_frame_mode_info(cm, dcb, r);
1579*77c1e3ccSAndroid Build Coastguard Worker     if (cm->seq_params->order_hint_info.enable_ref_frame_mvs)
1580*77c1e3ccSAndroid Build Coastguard Worker       intra_copy_frame_mvs(cm, xd->mi_row, xd->mi_col, x_mis, y_mis);
1581*77c1e3ccSAndroid Build Coastguard Worker   } else {
1582*77c1e3ccSAndroid Build Coastguard Worker     read_inter_frame_mode_info(pbi, dcb, r);
1583*77c1e3ccSAndroid Build Coastguard Worker     if (cm->seq_params->order_hint_info.enable_ref_frame_mvs)
1584*77c1e3ccSAndroid Build Coastguard Worker       av1_copy_frame_mvs(cm, mi, xd->mi_row, xd->mi_col, x_mis, y_mis);
1585*77c1e3ccSAndroid Build Coastguard Worker   }
1586*77c1e3ccSAndroid Build Coastguard Worker }
1587