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