Coverage Report

Created: 2025-06-22 08:04

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