Coverage Report

Created: 2026-01-20 07:37

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