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