Coverage Report

Created: 2026-04-01 07:24

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