Coverage Report

Created: 2026-06-30 06:53

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