Coverage Report

Created: 2024-06-18 06:48

/src/aom/av1/decoder/decodeframe.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3
 *
4
 * This source code is subject to the terms of the BSD 2 Clause License and
5
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6
 * was not distributed with this source code in the LICENSE file, you can
7
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8
 * Media Patent License 1.0 was not distributed with this source code in the
9
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
 */
11
12
#include <assert.h>
13
#include <stdbool.h>
14
#include <stddef.h>
15
16
#include "config/aom_config.h"
17
#include "config/aom_scale_rtcd.h"
18
19
#include "aom/aom_codec.h"
20
#include "aom/aom_image.h"
21
#include "aom/internal/aom_codec_internal.h"
22
#include "aom_dsp/aom_dsp_common.h"
23
#include "aom_dsp/binary_codes_reader.h"
24
#include "aom_dsp/bitreader.h"
25
#include "aom_dsp/bitreader_buffer.h"
26
#include "aom_dsp/txfm_common.h"
27
#include "aom_mem/aom_mem.h"
28
#include "aom_ports/aom_timer.h"
29
#include "aom_ports/mem.h"
30
#include "aom_ports/mem_ops.h"
31
#include "aom_scale/aom_scale.h"
32
#include "aom_scale/yv12config.h"
33
#include "aom_util/aom_pthread.h"
34
#include "aom_util/aom_thread.h"
35
36
#if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
37
#include "aom_util/debug_util.h"
38
#endif  // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
39
40
#include "av1/common/alloccommon.h"
41
#include "av1/common/av1_common_int.h"
42
#include "av1/common/blockd.h"
43
#include "av1/common/cdef.h"
44
#include "av1/common/cfl.h"
45
#include "av1/common/common_data.h"
46
#include "av1/common/common.h"
47
#include "av1/common/entropy.h"
48
#include "av1/common/entropymode.h"
49
#include "av1/common/entropymv.h"
50
#include "av1/common/enums.h"
51
#include "av1/common/frame_buffers.h"
52
#include "av1/common/idct.h"
53
#include "av1/common/mv.h"
54
#include "av1/common/mvref_common.h"
55
#include "av1/common/obmc.h"
56
#include "av1/common/pred_common.h"
57
#include "av1/common/quant_common.h"
58
#include "av1/common/reconinter.h"
59
#include "av1/common/reconintra.h"
60
#include "av1/common/resize.h"
61
#include "av1/common/restoration.h"
62
#include "av1/common/scale.h"
63
#include "av1/common/seg_common.h"
64
#include "av1/common/thread_common.h"
65
#include "av1/common/tile_common.h"
66
#include "av1/common/warped_motion.h"
67
68
#include "av1/decoder/decodeframe.h"
69
#include "av1/decoder/decodemv.h"
70
#include "av1/decoder/decoder.h"
71
#include "av1/decoder/decodetxb.h"
72
#include "av1/decoder/detokenize.h"
73
#if CONFIG_INSPECTION
74
#include "av1/decoder/inspection.h"
75
#endif
76
77
#define ACCT_STR __func__
78
79
94.1k
#define AOM_MIN_THREADS_PER_TILE 1
80
287k
#define AOM_MAX_THREADS_PER_TILE 2
81
82
// This is needed by ext_tile related unit tests.
83
#define EXT_TILE_DEBUG 1
84
#define MC_TEMP_BUF_PELS                       \
85
314k
  (((MAX_SB_SIZE)*2 + (AOM_INTERP_EXTEND)*2) * \
86
314k
   ((MAX_SB_SIZE)*2 + (AOM_INTERP_EXTEND)*2))
87
88
// Checks that the remaining bits start with a 1 and ends with 0s.
89
// It consumes an additional byte, if already byte aligned before the check.
90
77.7k
int av1_check_trailing_bits(AV1Decoder *pbi, struct aom_read_bit_buffer *rb) {
91
  // bit_offset is set to 0 (mod 8) when the reader is already byte aligned
92
77.7k
  int bits_before_alignment = 8 - rb->bit_offset % 8;
93
77.7k
  int trailing = aom_rb_read_literal(rb, bits_before_alignment);
94
77.7k
  if (trailing != (1 << (bits_before_alignment - 1))) {
95
5.20k
    pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
96
5.20k
    return -1;
97
5.20k
  }
98
72.5k
  return 0;
99
77.7k
}
100
101
// Use only_chroma = 1 to only set the chroma planes
102
static AOM_INLINE void set_planes_to_neutral_grey(
103
    const SequenceHeader *const seq_params, const YV12_BUFFER_CONFIG *const buf,
104
188k
    int only_chroma) {
105
188k
  if (seq_params->use_highbitdepth) {
106
62.7k
    const int val = 1 << (seq_params->bit_depth - 1);
107
246k
    for (int plane = only_chroma; plane < MAX_MB_PLANE; plane++) {
108
184k
      const int is_uv = plane > 0;
109
184k
      uint16_t *const base = CONVERT_TO_SHORTPTR(buf->buffers[plane]);
110
      // Set the first row to neutral grey. Then copy the first row to all
111
      // subsequent rows.
112
184k
      if (buf->crop_heights[is_uv] > 0) {
113
184k
        aom_memset16(base, val, buf->crop_widths[is_uv]);
114
82.8M
        for (int row_idx = 1; row_idx < buf->crop_heights[is_uv]; row_idx++) {
115
82.7M
          memcpy(&base[row_idx * buf->strides[is_uv]], base,
116
82.7M
                 sizeof(*base) * buf->crop_widths[is_uv]);
117
82.7M
        }
118
184k
      }
119
184k
    }
120
125k
  } else {
121
500k
    for (int plane = only_chroma; plane < MAX_MB_PLANE; plane++) {
122
374k
      const int is_uv = plane > 0;
123
52.4M
      for (int row_idx = 0; row_idx < buf->crop_heights[is_uv]; row_idx++) {
124
52.0M
        memset(&buf->buffers[plane][row_idx * buf->strides[is_uv]], 1 << 7,
125
52.0M
               buf->crop_widths[is_uv]);
126
52.0M
      }
127
374k
    }
128
125k
  }
129
188k
}
130
131
static AOM_INLINE void loop_restoration_read_sb_coeffs(
132
    const AV1_COMMON *const cm, MACROBLOCKD *xd, aom_reader *const r, int plane,
133
    int runit_idx);
134
135
241k
static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
136
241k
  return len != 0 && len <= (size_t)(end - start);
137
241k
}
138
139
static TX_MODE read_tx_mode(struct aom_read_bit_buffer *rb,
140
203k
                            int coded_lossless) {
141
203k
  if (coded_lossless) return ONLY_4X4;
142
179k
  return aom_rb_read_bit(rb) ? TX_MODE_SELECT : TX_MODE_LARGEST;
143
203k
}
144
145
static REFERENCE_MODE read_frame_reference_mode(
146
203k
    const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
147
203k
  if (frame_is_intra_only(cm)) {
148
112k
    return SINGLE_REFERENCE;
149
112k
  } else {
150
90.7k
    return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
151
90.7k
  }
152
203k
}
153
154
static AOM_INLINE void inverse_transform_block(DecoderCodingBlock *dcb,
155
                                               int plane, const TX_TYPE tx_type,
156
                                               const TX_SIZE tx_size,
157
                                               uint8_t *dst, int stride,
158
21.8M
                                               int reduced_tx_set) {
159
21.8M
  tran_low_t *const dqcoeff = dcb->dqcoeff_block[plane] + dcb->cb_offset[plane];
160
21.8M
  eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
161
21.8M
  uint16_t scan_line = eob_data->max_scan_line;
162
21.8M
  uint16_t eob = eob_data->eob;
163
21.8M
  av1_inverse_transform_block(&dcb->xd, dqcoeff, plane, tx_type, tx_size, dst,
164
21.8M
                              stride, eob, reduced_tx_set);
165
21.8M
  memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
166
21.8M
}
167
168
static AOM_INLINE void read_coeffs_tx_intra_block(
169
    const AV1_COMMON *const cm, DecoderCodingBlock *dcb, aom_reader *const r,
170
106M
    const int plane, const int row, const int col, const TX_SIZE tx_size) {
171
106M
  MB_MODE_INFO *mbmi = dcb->xd.mi[0];
172
106M
  if (!mbmi->skip_txfm) {
173
#if TXCOEFF_TIMER
174
    struct aom_usec_timer timer;
175
    aom_usec_timer_start(&timer);
176
#endif
177
30.4M
    av1_read_coeffs_txb_facade(cm, dcb, r, plane, row, col, tx_size);
178
#if TXCOEFF_TIMER
179
    aom_usec_timer_mark(&timer);
180
    const int64_t elapsed_time = aom_usec_timer_elapsed(&timer);
181
    cm->txcoeff_timer += elapsed_time;
182
    ++cm->txb_count;
183
#endif
184
30.4M
  }
185
106M
}
186
187
static AOM_INLINE void decode_block_void(const AV1_COMMON *const cm,
188
                                         DecoderCodingBlock *dcb,
189
                                         aom_reader *const r, const int plane,
190
                                         const int row, const int col,
191
155M
                                         const TX_SIZE tx_size) {
192
155M
  (void)cm;
193
155M
  (void)dcb;
194
155M
  (void)r;
195
155M
  (void)plane;
196
155M
  (void)row;
197
155M
  (void)col;
198
155M
  (void)tx_size;
199
155M
}
200
201
static AOM_INLINE void predict_inter_block_void(AV1_COMMON *const cm,
202
                                                DecoderCodingBlock *dcb,
203
5.35M
                                                BLOCK_SIZE bsize) {
204
5.35M
  (void)cm;
205
5.35M
  (void)dcb;
206
5.35M
  (void)bsize;
207
5.35M
}
208
209
static AOM_INLINE void cfl_store_inter_block_void(AV1_COMMON *const cm,
210
5.34M
                                                  MACROBLOCKD *const xd) {
211
5.34M
  (void)cm;
212
5.34M
  (void)xd;
213
5.34M
}
214
215
static AOM_INLINE void predict_and_reconstruct_intra_block(
216
    const AV1_COMMON *const cm, DecoderCodingBlock *dcb, aom_reader *const r,
217
97.5M
    const int plane, const int row, const int col, const TX_SIZE tx_size) {
218
97.5M
  (void)r;
219
97.5M
  MACROBLOCKD *const xd = &dcb->xd;
220
97.5M
  MB_MODE_INFO *mbmi = xd->mi[0];
221
97.5M
  PLANE_TYPE plane_type = get_plane_type(plane);
222
223
97.5M
  av1_predict_intra_block_facade(cm, xd, plane, col, row, tx_size);
224
225
97.5M
  if (!mbmi->skip_txfm) {
226
23.0M
    eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
227
23.0M
    if (eob_data->eob) {
228
12.1M
      const bool reduced_tx_set_used = cm->features.reduced_tx_set_used;
229
      // tx_type was read out in av1_read_coeffs_txb.
230
12.1M
      const TX_TYPE tx_type = av1_get_tx_type(xd, plane_type, row, col, tx_size,
231
12.1M
                                              reduced_tx_set_used);
232
12.1M
      struct macroblockd_plane *const pd = &xd->plane[plane];
233
12.1M
      uint8_t *dst = &pd->dst.buf[(row * pd->dst.stride + col) << MI_SIZE_LOG2];
234
12.1M
      inverse_transform_block(dcb, plane, tx_type, tx_size, dst, pd->dst.stride,
235
12.1M
                              reduced_tx_set_used);
236
12.1M
    }
237
23.0M
  }
238
97.5M
  if (plane == AOM_PLANE_Y && store_cfl_required(cm, xd)) {
239
2.41M
    cfl_store_tx(xd, row, col, tx_size, mbmi->bsize);
240
2.41M
  }
241
97.5M
}
242
243
static AOM_INLINE void inverse_transform_inter_block(
244
    const AV1_COMMON *const cm, DecoderCodingBlock *dcb, aom_reader *const r,
245
    const int plane, const int blk_row, const int blk_col,
246
9.73M
    const TX_SIZE tx_size) {
247
9.73M
  (void)r;
248
9.73M
  MACROBLOCKD *const xd = &dcb->xd;
249
9.73M
  PLANE_TYPE plane_type = get_plane_type(plane);
250
9.73M
  const struct macroblockd_plane *const pd = &xd->plane[plane];
251
9.73M
  const bool reduced_tx_set_used = cm->features.reduced_tx_set_used;
252
  // tx_type was read out in av1_read_coeffs_txb.
253
9.73M
  const TX_TYPE tx_type = av1_get_tx_type(xd, plane_type, blk_row, blk_col,
254
9.73M
                                          tx_size, reduced_tx_set_used);
255
256
9.73M
  uint8_t *dst =
257
9.73M
      &pd->dst.buf[(blk_row * pd->dst.stride + blk_col) << MI_SIZE_LOG2];
258
9.73M
  inverse_transform_block(dcb, plane, tx_type, tx_size, dst, pd->dst.stride,
259
9.73M
                          reduced_tx_set_used);
260
#if CONFIG_MISMATCH_DEBUG
261
  int pixel_c, pixel_r;
262
  BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
263
  int blk_w = block_size_wide[bsize];
264
  int blk_h = block_size_high[bsize];
265
  const int mi_row = -xd->mb_to_top_edge >> (3 + MI_SIZE_LOG2);
266
  const int mi_col = -xd->mb_to_left_edge >> (3 + MI_SIZE_LOG2);
267
  mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, blk_col, blk_row,
268
                  pd->subsampling_x, pd->subsampling_y);
269
  mismatch_check_block_tx(dst, pd->dst.stride, cm->current_frame.order_hint,
270
                          plane, pixel_c, pixel_r, blk_w, blk_h,
271
                          xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
272
#endif
273
9.73M
}
274
275
static AOM_INLINE void set_cb_buffer_offsets(DecoderCodingBlock *dcb,
276
183M
                                             TX_SIZE tx_size, int plane) {
277
183M
  dcb->cb_offset[plane] += tx_size_wide[tx_size] * tx_size_high[tx_size];
278
183M
  dcb->txb_offset[plane] =
279
183M
      dcb->cb_offset[plane] / (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
280
183M
}
281
282
static AOM_INLINE void decode_reconstruct_tx(
283
    AV1_COMMON *cm, ThreadData *const td, aom_reader *r,
284
    MB_MODE_INFO *const mbmi, int plane, BLOCK_SIZE plane_bsize, int blk_row,
285
20.5M
    int blk_col, int block, TX_SIZE tx_size, int *eob_total) {
286
20.5M
  DecoderCodingBlock *const dcb = &td->dcb;
287
20.5M
  MACROBLOCKD *const xd = &dcb->xd;
288
20.5M
  const struct macroblockd_plane *const pd = &xd->plane[plane];
289
20.5M
  const TX_SIZE plane_tx_size =
290
20.5M
      plane ? av1_get_max_uv_txsize(mbmi->bsize, pd->subsampling_x,
291
11.9M
                                    pd->subsampling_y)
292
20.5M
            : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row,
293
8.62M
                                                         blk_col)];
294
  // Scale to match transform block unit.
295
20.5M
  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
296
20.5M
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
297
298
20.6M
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
299
300
20.5M
  if (tx_size == plane_tx_size || plane) {
301
20.1M
    td->read_coeffs_tx_inter_block_visit(cm, dcb, r, plane, blk_row, blk_col,
302
20.1M
                                         tx_size);
303
304
20.1M
    td->inverse_tx_inter_block_visit(cm, dcb, r, plane, blk_row, blk_col,
305
20.1M
                                     tx_size);
306
20.1M
    eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
307
20.1M
    *eob_total += eob_data->eob;
308
20.1M
    set_cb_buffer_offsets(dcb, tx_size, plane);
309
20.1M
  } else {
310
408k
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
311
408k
    assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
312
479k
    assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
313
479k
    const int bsw = tx_size_wide_unit[sub_txs];
314
479k
    const int bsh = tx_size_high_unit[sub_txs];
315
479k
    const int sub_step = bsw * bsh;
316
479k
    const int row_end =
317
479k
        AOMMIN(tx_size_high_unit[tx_size], max_blocks_high - blk_row);
318
479k
    const int col_end =
319
479k
        AOMMIN(tx_size_wide_unit[tx_size], max_blocks_wide - blk_col);
320
321
479k
    assert(bsw > 0 && bsh > 0);
322
323
1.27M
    for (int row = 0; row < row_end; row += bsh) {
324
796k
      const int offsetr = blk_row + row;
325
2.16M
      for (int col = 0; col < col_end; col += bsw) {
326
1.36M
        const int offsetc = blk_col + col;
327
328
1.36M
        decode_reconstruct_tx(cm, td, r, mbmi, plane, plane_bsize, offsetr,
329
1.36M
                              offsetc, block, sub_txs, eob_total);
330
1.36M
        block += sub_step;
331
1.36M
      }
332
796k
    }
333
479k
  }
334
20.5M
}
335
336
static AOM_INLINE void set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
337
                                   BLOCK_SIZE bsize, int mi_row, int mi_col,
338
16.6M
                                   int bw, int bh, int x_mis, int y_mis) {
339
16.6M
  const int num_planes = av1_num_planes(cm);
340
16.6M
  const CommonModeInfoParams *const mi_params = &cm->mi_params;
341
16.6M
  const TileInfo *const tile = &xd->tile;
342
343
16.6M
  set_mi_offsets(mi_params, xd, mi_row, mi_col);
344
16.6M
  xd->mi[0]->bsize = bsize;
345
#if CONFIG_RD_DEBUG
346
  xd->mi[0]->mi_row = mi_row;
347
  xd->mi[0]->mi_col = mi_col;
348
#endif
349
350
16.6M
  assert(x_mis && y_mis);
351
87.9M
  for (int x = 1; x < x_mis; ++x) xd->mi[x] = xd->mi[0];
352
16.6M
  int idx = mi_params->mi_stride;
353
83.6M
  for (int y = 1; y < y_mis; ++y) {
354
66.9M
    memcpy(&xd->mi[idx], &xd->mi[0], x_mis * sizeof(xd->mi[0]));
355
66.9M
    idx += mi_params->mi_stride;
356
66.9M
  }
357
358
16.6M
  set_plane_n4(xd, bw, bh, num_planes);
359
16.6M
  set_entropy_context(xd, mi_row, mi_col, num_planes);
360
361
  // Distance of Mb to the various image edges. These are specified to 8th pel
362
  // as they are always compared to values that are in 1/8th pel units
363
16.6M
  set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, mi_params->mi_rows,
364
16.6M
                 mi_params->mi_cols);
365
366
16.6M
  av1_setup_dst_planes(xd->plane, bsize, &cm->cur_frame->buf, mi_row, mi_col, 0,
367
16.6M
                       num_planes);
368
16.6M
}
369
370
static AOM_INLINE void decode_mbmi_block(AV1Decoder *const pbi,
371
                                         DecoderCodingBlock *dcb, int mi_row,
372
                                         int mi_col, aom_reader *r,
373
                                         PARTITION_TYPE partition,
374
16.6M
                                         BLOCK_SIZE bsize) {
375
16.6M
  AV1_COMMON *const cm = &pbi->common;
376
16.6M
  const SequenceHeader *const seq_params = cm->seq_params;
377
16.6M
  const int bw = mi_size_wide[bsize];
378
16.6M
  const int bh = mi_size_high[bsize];
379
16.6M
  const int x_mis = AOMMIN(bw, cm->mi_params.mi_cols - mi_col);
380
16.6M
  const int y_mis = AOMMIN(bh, cm->mi_params.mi_rows - mi_row);
381
16.6M
  MACROBLOCKD *const xd = &dcb->xd;
382
383
#if CONFIG_ACCOUNTING
384
  aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
385
#endif
386
16.6M
  set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
387
16.6M
  xd->mi[0]->partition = partition;
388
16.6M
  av1_read_mode_info(pbi, dcb, r, x_mis, y_mis);
389
16.6M
  if (bsize >= BLOCK_8X8 &&
390
16.6M
      (seq_params->subsampling_x || seq_params->subsampling_y)) {
391
9.84M
    const BLOCK_SIZE uv_subsize =
392
9.84M
        av1_ss_size_lookup[bsize][seq_params->subsampling_x]
393
9.84M
                          [seq_params->subsampling_y];
394
9.84M
    if (uv_subsize == BLOCK_INVALID)
395
0
      aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
396
0
                         "Invalid block size.");
397
9.84M
  }
398
16.6M
}
399
400
typedef struct PadBlock {
401
  int x0;
402
  int x1;
403
  int y0;
404
  int y1;
405
} PadBlock;
406
407
#if CONFIG_AV1_HIGHBITDEPTH
408
static AOM_INLINE void highbd_build_mc_border(const uint8_t *src8,
409
                                              int src_stride, uint8_t *dst8,
410
                                              int dst_stride, int x, int y,
411
1.37M
                                              int b_w, int b_h, int w, int h) {
412
  // Get a pointer to the start of the real data for this row.
413
1.37M
  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
414
1.37M
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
415
1.37M
  const uint16_t *ref_row = src - x - y * src_stride;
416
417
1.37M
  if (y >= h)
418
398k
    ref_row += (h - 1) * src_stride;
419
975k
  else if (y > 0)
420
696k
    ref_row += y * src_stride;
421
422
25.8M
  do {
423
25.8M
    int right = 0, copy;
424
25.8M
    int left = x < 0 ? -x : 0;
425
426
25.8M
    if (left > b_w) left = b_w;
427
428
25.8M
    if (x + b_w > w) right = x + b_w - w;
429
430
25.8M
    if (right > b_w) right = b_w;
431
432
25.8M
    copy = b_w - left - right;
433
434
25.8M
    if (left) aom_memset16(dst, ref_row[0], left);
435
436
25.8M
    if (copy) memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t));
437
438
25.8M
    if (right) aom_memset16(dst + left + copy, ref_row[w - 1], right);
439
440
25.8M
    dst += dst_stride;
441
25.8M
    ++y;
442
443
25.8M
    if (y > 0 && y < h) ref_row += src_stride;
444
25.8M
  } while (--b_h);
445
1.37M
}
446
#endif  // CONFIG_AV1_HIGHBITDEPTH
447
448
static AOM_INLINE void build_mc_border(const uint8_t *src, int src_stride,
449
                                       uint8_t *dst, int dst_stride, int x,
450
1.72M
                                       int y, int b_w, int b_h, int w, int h) {
451
  // Get a pointer to the start of the real data for this row.
452
1.72M
  const uint8_t *ref_row = src - x - y * src_stride;
453
454
1.72M
  if (y >= h)
455
116k
    ref_row += (h - 1) * src_stride;
456
1.61M
  else if (y > 0)
457
1.10M
    ref_row += y * src_stride;
458
459
34.2M
  do {
460
34.2M
    int right = 0, copy;
461
34.2M
    int left = x < 0 ? -x : 0;
462
463
34.2M
    if (left > b_w) left = b_w;
464
465
34.2M
    if (x + b_w > w) right = x + b_w - w;
466
467
34.2M
    if (right > b_w) right = b_w;
468
469
34.2M
    copy = b_w - left - right;
470
471
34.2M
    if (left) memset(dst, ref_row[0], left);
472
473
34.2M
    if (copy) memcpy(dst + left, ref_row + x + left, copy);
474
475
34.2M
    if (right) memset(dst + left + copy, ref_row[w - 1], right);
476
477
34.2M
    dst += dst_stride;
478
34.2M
    ++y;
479
480
34.2M
    if (y > 0 && y < h) ref_row += src_stride;
481
34.2M
  } while (--b_h);
482
1.72M
}
483
484
static INLINE int update_extend_mc_border_params(
485
    const struct scale_factors *const sf, struct buf_2d *const pre_buf,
486
    MV32 scaled_mv, PadBlock *block, int subpel_x_mv, int subpel_y_mv,
487
24.2M
    int do_warp, int is_intrabc, int *x_pad, int *y_pad) {
488
24.2M
  const int is_scaled = av1_is_scaled(sf);
489
  // Get reference width and height.
490
24.2M
  int frame_width = pre_buf->width;
491
24.2M
  int frame_height = pre_buf->height;
492
493
  // Do border extension if there is motion or
494
  // width/height is not a multiple of 8 pixels.
495
24.2M
  if ((!is_intrabc) && (!do_warp) &&
496
24.2M
      (is_scaled || scaled_mv.col || scaled_mv.row || (frame_width & 0x7) ||
497
23.3M
       (frame_height & 0x7))) {
498
18.3M
    if (subpel_x_mv || (sf->x_step_q4 != SUBPEL_SHIFTS)) {
499
18.3M
      block->x0 -= AOM_INTERP_EXTEND - 1;
500
18.3M
      block->x1 += AOM_INTERP_EXTEND;
501
18.3M
      *x_pad = 1;
502
18.3M
    }
503
504
18.3M
    if (subpel_y_mv || (sf->y_step_q4 != SUBPEL_SHIFTS)) {
505
18.3M
      block->y0 -= AOM_INTERP_EXTEND - 1;
506
18.3M
      block->y1 += AOM_INTERP_EXTEND;
507
18.3M
      *y_pad = 1;
508
18.3M
    }
509
510
    // Skip border extension if block is inside the frame.
511
18.3M
    if (block->x0 < 0 || block->x1 > frame_width - 1 || block->y0 < 0 ||
512
18.3M
        block->y1 > frame_height - 1) {
513
3.10M
      return 1;
514
3.10M
    }
515
18.3M
  }
516
21.1M
  return 0;
517
24.2M
}
518
519
static INLINE void extend_mc_border(const struct scale_factors *const sf,
520
                                    struct buf_2d *const pre_buf,
521
                                    MV32 scaled_mv, PadBlock block,
522
                                    int subpel_x_mv, int subpel_y_mv,
523
                                    int do_warp, int is_intrabc, int highbd,
524
                                    uint8_t *mc_buf, uint8_t **pre,
525
24.2M
                                    int *src_stride) {
526
24.2M
  int x_pad = 0, y_pad = 0;
527
24.2M
  if (update_extend_mc_border_params(sf, pre_buf, scaled_mv, &block,
528
24.2M
                                     subpel_x_mv, subpel_y_mv, do_warp,
529
24.2M
                                     is_intrabc, &x_pad, &y_pad)) {
530
    // Get reference block pointer.
531
3.10M
    const uint8_t *const buf_ptr =
532
3.10M
        pre_buf->buf0 + block.y0 * pre_buf->stride + block.x0;
533
3.10M
    int buf_stride = pre_buf->stride;
534
3.10M
    const int b_w = block.x1 - block.x0;
535
3.10M
    const int b_h = block.y1 - block.y0;
536
537
3.10M
#if CONFIG_AV1_HIGHBITDEPTH
538
    // Extend the border.
539
3.10M
    if (highbd) {
540
1.37M
      highbd_build_mc_border(buf_ptr, buf_stride, mc_buf, b_w, block.x0,
541
1.37M
                             block.y0, b_w, b_h, pre_buf->width,
542
1.37M
                             pre_buf->height);
543
1.72M
    } else {
544
1.72M
      build_mc_border(buf_ptr, buf_stride, mc_buf, b_w, block.x0, block.y0, b_w,
545
1.72M
                      b_h, pre_buf->width, pre_buf->height);
546
1.72M
    }
547
#else
548
    (void)highbd;
549
    build_mc_border(buf_ptr, buf_stride, mc_buf, b_w, block.x0, block.y0, b_w,
550
                    b_h, pre_buf->width, pre_buf->height);
551
#endif
552
3.10M
    *src_stride = b_w;
553
3.10M
    *pre = mc_buf + y_pad * (AOM_INTERP_EXTEND - 1) * b_w +
554
3.10M
           x_pad * (AOM_INTERP_EXTEND - 1);
555
3.10M
  }
556
24.2M
}
557
558
static AOM_INLINE void dec_calc_subpel_params(
559
    const MV *const src_mv, InterPredParams *const inter_pred_params,
560
    const MACROBLOCKD *const xd, int mi_x, int mi_y, uint8_t **pre,
561
    SubpelParams *subpel_params, int *src_stride, PadBlock *block,
562
24.2M
    MV32 *scaled_mv, int *subpel_x_mv, int *subpel_y_mv) {
563
24.2M
  const struct scale_factors *sf = inter_pred_params->scale_factors;
564
24.2M
  struct buf_2d *pre_buf = &inter_pred_params->ref_frame_buf;
565
24.2M
  const int bw = inter_pred_params->block_width;
566
24.2M
  const int bh = inter_pred_params->block_height;
567
24.2M
  const int is_scaled = av1_is_scaled(sf);
568
24.2M
  if (is_scaled) {
569
3.40M
    int ssx = inter_pred_params->subsampling_x;
570
3.40M
    int ssy = inter_pred_params->subsampling_y;
571
3.40M
    int orig_pos_y = inter_pred_params->pix_row << SUBPEL_BITS;
572
3.40M
    orig_pos_y += src_mv->row * (1 << (1 - ssy));
573
3.40M
    int orig_pos_x = inter_pred_params->pix_col << SUBPEL_BITS;
574
3.40M
    orig_pos_x += src_mv->col * (1 << (1 - ssx));
575
3.40M
    int pos_y = av1_scaled_y(orig_pos_y, sf);
576
3.40M
    int pos_x = av1_scaled_x(orig_pos_x, sf);
577
3.40M
    pos_x += SCALE_EXTRA_OFF;
578
3.40M
    pos_y += SCALE_EXTRA_OFF;
579
580
3.40M
    const int top = -AOM_LEFT_TOP_MARGIN_SCALED(ssy);
581
3.40M
    const int left = -AOM_LEFT_TOP_MARGIN_SCALED(ssx);
582
3.40M
    const int bottom = (pre_buf->height + AOM_INTERP_EXTEND)
583
3.40M
                       << SCALE_SUBPEL_BITS;
584
3.40M
    const int right = (pre_buf->width + AOM_INTERP_EXTEND) << SCALE_SUBPEL_BITS;
585
3.40M
    pos_y = clamp(pos_y, top, bottom);
586
3.40M
    pos_x = clamp(pos_x, left, right);
587
588
3.40M
    subpel_params->subpel_x = pos_x & SCALE_SUBPEL_MASK;
589
3.40M
    subpel_params->subpel_y = pos_y & SCALE_SUBPEL_MASK;
590
3.40M
    subpel_params->xs = sf->x_step_q4;
591
3.40M
    subpel_params->ys = sf->y_step_q4;
592
593
    // Get reference block top left coordinate.
594
3.40M
    block->x0 = pos_x >> SCALE_SUBPEL_BITS;
595
3.40M
    block->y0 = pos_y >> SCALE_SUBPEL_BITS;
596
597
    // Get reference block bottom right coordinate.
598
3.40M
    block->x1 =
599
3.40M
        ((pos_x + (bw - 1) * subpel_params->xs) >> SCALE_SUBPEL_BITS) + 1;
600
3.40M
    block->y1 =
601
3.40M
        ((pos_y + (bh - 1) * subpel_params->ys) >> SCALE_SUBPEL_BITS) + 1;
602
603
3.40M
    MV temp_mv;
604
3.40M
    temp_mv = clamp_mv_to_umv_border_sb(xd, src_mv, bw, bh,
605
3.40M
                                        inter_pred_params->subsampling_x,
606
3.40M
                                        inter_pred_params->subsampling_y);
607
3.40M
    *scaled_mv = av1_scale_mv(&temp_mv, mi_x, mi_y, sf);
608
3.40M
    scaled_mv->row += SCALE_EXTRA_OFF;
609
3.40M
    scaled_mv->col += SCALE_EXTRA_OFF;
610
611
3.40M
    *subpel_x_mv = scaled_mv->col & SCALE_SUBPEL_MASK;
612
3.40M
    *subpel_y_mv = scaled_mv->row & SCALE_SUBPEL_MASK;
613
20.8M
  } else {
614
    // Get block position in current frame.
615
20.8M
    int pos_x = inter_pred_params->pix_col << SUBPEL_BITS;
616
20.8M
    int pos_y = inter_pred_params->pix_row << SUBPEL_BITS;
617
618
20.8M
    const MV mv_q4 = clamp_mv_to_umv_border_sb(
619
20.8M
        xd, src_mv, bw, bh, inter_pred_params->subsampling_x,
620
20.8M
        inter_pred_params->subsampling_y);
621
20.8M
    subpel_params->xs = subpel_params->ys = SCALE_SUBPEL_SHIFTS;
622
20.8M
    subpel_params->subpel_x = (mv_q4.col & SUBPEL_MASK) << SCALE_EXTRA_BITS;
623
20.8M
    subpel_params->subpel_y = (mv_q4.row & SUBPEL_MASK) << SCALE_EXTRA_BITS;
624
625
    // Get reference block top left coordinate.
626
20.8M
    pos_x += mv_q4.col;
627
20.8M
    pos_y += mv_q4.row;
628
20.8M
    block->x0 = pos_x >> SUBPEL_BITS;
629
20.8M
    block->y0 = pos_y >> SUBPEL_BITS;
630
631
    // Get reference block bottom right coordinate.
632
20.8M
    block->x1 = (pos_x >> SUBPEL_BITS) + (bw - 1) + 1;
633
20.8M
    block->y1 = (pos_y >> SUBPEL_BITS) + (bh - 1) + 1;
634
635
20.8M
    scaled_mv->row = mv_q4.row;
636
20.8M
    scaled_mv->col = mv_q4.col;
637
20.8M
    *subpel_x_mv = scaled_mv->col & SUBPEL_MASK;
638
20.8M
    *subpel_y_mv = scaled_mv->row & SUBPEL_MASK;
639
20.8M
  }
640
24.2M
  *pre = pre_buf->buf0 + block->y0 * pre_buf->stride + block->x0;
641
24.2M
  *src_stride = pre_buf->stride;
642
24.2M
}
643
644
static AOM_INLINE void dec_calc_subpel_params_and_extend(
645
    const MV *const src_mv, InterPredParams *const inter_pred_params,
646
    MACROBLOCKD *const xd, int mi_x, int mi_y, int ref, uint8_t **mc_buf,
647
24.2M
    uint8_t **pre, SubpelParams *subpel_params, int *src_stride) {
648
24.2M
  PadBlock block;
649
24.2M
  MV32 scaled_mv;
650
24.2M
  int subpel_x_mv, subpel_y_mv;
651
24.2M
  dec_calc_subpel_params(src_mv, inter_pred_params, xd, mi_x, mi_y, pre,
652
24.2M
                         subpel_params, src_stride, &block, &scaled_mv,
653
24.2M
                         &subpel_x_mv, &subpel_y_mv);
654
24.2M
  extend_mc_border(
655
24.2M
      inter_pred_params->scale_factors, &inter_pred_params->ref_frame_buf,
656
24.2M
      scaled_mv, block, subpel_x_mv, subpel_y_mv,
657
24.2M
      inter_pred_params->mode == WARP_PRED, inter_pred_params->is_intrabc,
658
24.2M
      inter_pred_params->use_hbd_buf, mc_buf[ref], pre, src_stride);
659
24.2M
}
660
661
#define IS_DEC 1
662
#include "av1/common/reconinter_template.inc"
663
#undef IS_DEC
664
665
static void dec_build_inter_predictors(const AV1_COMMON *cm,
666
                                       DecoderCodingBlock *dcb, int plane,
667
                                       const MB_MODE_INFO *mi,
668
                                       int build_for_obmc, int bw, int bh,
669
19.7M
                                       int mi_x, int mi_y) {
670
19.7M
  build_inter_predictors(cm, &dcb->xd, plane, mi, build_for_obmc, bw, bh, mi_x,
671
19.7M
                         mi_y, dcb->mc_buf);
672
19.7M
}
673
674
static AOM_INLINE void dec_build_inter_predictor(const AV1_COMMON *cm,
675
                                                 DecoderCodingBlock *dcb,
676
                                                 int mi_row, int mi_col,
677
5.66M
                                                 BLOCK_SIZE bsize) {
678
5.66M
  MACROBLOCKD *const xd = &dcb->xd;
679
5.66M
  const int num_planes = av1_num_planes(cm);
680
20.7M
  for (int plane = 0; plane < num_planes; ++plane) {
681
16.0M
    if (plane && !xd->is_chroma_ref) break;
682
15.1M
    const int mi_x = mi_col * MI_SIZE;
683
15.1M
    const int mi_y = mi_row * MI_SIZE;
684
15.1M
    dec_build_inter_predictors(cm, dcb, plane, xd->mi[0], 0,
685
15.1M
                               xd->plane[plane].width, xd->plane[plane].height,
686
15.1M
                               mi_x, mi_y);
687
15.1M
    if (is_interintra_pred(xd->mi[0])) {
688
1.04M
      BUFFER_SET ctx = { { xd->plane[0].dst.buf, xd->plane[1].dst.buf,
689
1.04M
                           xd->plane[2].dst.buf },
690
1.04M
                         { xd->plane[0].dst.stride, xd->plane[1].dst.stride,
691
1.04M
                           xd->plane[2].dst.stride } };
692
1.04M
      av1_build_interintra_predictor(cm, xd, xd->plane[plane].dst.buf,
693
1.04M
                                     xd->plane[plane].dst.stride, &ctx, plane,
694
1.04M
                                     bsize);
695
1.04M
    }
696
15.1M
  }
697
5.66M
}
698
699
static INLINE void dec_build_prediction_by_above_pred(
700
    MACROBLOCKD *const xd, int rel_mi_row, int rel_mi_col, uint8_t op_mi_size,
701
962k
    int dir, MB_MODE_INFO *above_mbmi, void *fun_ctxt, const int num_planes) {
702
962k
  struct build_prediction_ctxt *ctxt = (struct build_prediction_ctxt *)fun_ctxt;
703
962k
  const int above_mi_col = xd->mi_col + rel_mi_col;
704
962k
  int mi_x, mi_y;
705
962k
  MB_MODE_INFO backup_mbmi = *above_mbmi;
706
707
962k
  (void)rel_mi_row;
708
962k
  (void)dir;
709
710
962k
  av1_setup_build_prediction_by_above_pred(xd, rel_mi_col, op_mi_size,
711
962k
                                           &backup_mbmi, ctxt, num_planes);
712
962k
  mi_x = above_mi_col << MI_SIZE_LOG2;
713
962k
  mi_y = xd->mi_row << MI_SIZE_LOG2;
714
715
962k
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
716
717
3.84M
  for (int j = 0; j < num_planes; ++j) {
718
2.88M
    const struct macroblockd_plane *pd = &xd->plane[j];
719
2.88M
    int bw = (op_mi_size * MI_SIZE) >> pd->subsampling_x;
720
2.88M
    int bh = clamp(block_size_high[bsize] >> (pd->subsampling_y + 1), 4,
721
2.88M
                   block_size_high[BLOCK_64X64] >> (pd->subsampling_y + 1));
722
723
2.88M
    if (av1_skip_u4x4_pred_in_obmc(bsize, pd, 0)) continue;
724
1.82M
    dec_build_inter_predictors(ctxt->cm, (DecoderCodingBlock *)ctxt->dcb, j,
725
1.82M
                               &backup_mbmi, 1, bw, bh, mi_x, mi_y);
726
1.82M
  }
727
962k
}
728
729
static AOM_INLINE void dec_build_prediction_by_above_preds(
730
    const AV1_COMMON *cm, DecoderCodingBlock *dcb,
731
    uint8_t *tmp_buf[MAX_MB_PLANE], int tmp_width[MAX_MB_PLANE],
732
949k
    int tmp_height[MAX_MB_PLANE], int tmp_stride[MAX_MB_PLANE]) {
733
949k
  MACROBLOCKD *const xd = &dcb->xd;
734
949k
  if (!xd->up_available) return;
735
736
  // Adjust mb_to_bottom_edge to have the correct value for the OBMC
737
  // prediction block. This is half the height of the original block,
738
  // except for 128-wide blocks, where we only use a height of 32.
739
936k
  const int this_height = xd->height * MI_SIZE;
740
936k
  const int pred_height = AOMMIN(this_height / 2, 32);
741
936k
  xd->mb_to_bottom_edge += GET_MV_SUBPEL(this_height - pred_height);
742
936k
  struct build_prediction_ctxt ctxt = {
743
936k
    cm, tmp_buf, tmp_width, tmp_height, tmp_stride, xd->mb_to_right_edge, dcb
744
936k
  };
745
936k
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
746
936k
  foreach_overlappable_nb_above(cm, xd,
747
936k
                                max_neighbor_obmc[mi_size_wide_log2[bsize]],
748
936k
                                dec_build_prediction_by_above_pred, &ctxt);
749
750
936k
  xd->mb_to_left_edge = -GET_MV_SUBPEL(xd->mi_col * MI_SIZE);
751
936k
  xd->mb_to_right_edge = ctxt.mb_to_far_edge;
752
936k
  xd->mb_to_bottom_edge -= GET_MV_SUBPEL(this_height - pred_height);
753
936k
}
754
755
static INLINE void dec_build_prediction_by_left_pred(
756
    MACROBLOCKD *const xd, int rel_mi_row, int rel_mi_col, uint8_t op_mi_size,
757
959k
    int dir, MB_MODE_INFO *left_mbmi, void *fun_ctxt, const int num_planes) {
758
959k
  struct build_prediction_ctxt *ctxt = (struct build_prediction_ctxt *)fun_ctxt;
759
959k
  const int left_mi_row = xd->mi_row + rel_mi_row;
760
959k
  int mi_x, mi_y;
761
959k
  MB_MODE_INFO backup_mbmi = *left_mbmi;
762
763
959k
  (void)rel_mi_col;
764
959k
  (void)dir;
765
766
959k
  av1_setup_build_prediction_by_left_pred(xd, rel_mi_row, op_mi_size,
767
959k
                                          &backup_mbmi, ctxt, num_planes);
768
959k
  mi_x = xd->mi_col << MI_SIZE_LOG2;
769
959k
  mi_y = left_mi_row << MI_SIZE_LOG2;
770
959k
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
771
772
3.83M
  for (int j = 0; j < num_planes; ++j) {
773
2.87M
    const struct macroblockd_plane *pd = &xd->plane[j];
774
2.87M
    int bw = clamp(block_size_wide[bsize] >> (pd->subsampling_x + 1), 4,
775
2.87M
                   block_size_wide[BLOCK_64X64] >> (pd->subsampling_x + 1));
776
2.87M
    int bh = (op_mi_size << MI_SIZE_LOG2) >> pd->subsampling_y;
777
778
2.87M
    if (av1_skip_u4x4_pred_in_obmc(bsize, pd, 1)) continue;
779
2.87M
    dec_build_inter_predictors(ctxt->cm, (DecoderCodingBlock *)ctxt->dcb, j,
780
2.87M
                               &backup_mbmi, 1, bw, bh, mi_x, mi_y);
781
2.87M
  }
782
959k
}
783
784
static AOM_INLINE void dec_build_prediction_by_left_preds(
785
    const AV1_COMMON *cm, DecoderCodingBlock *dcb,
786
    uint8_t *tmp_buf[MAX_MB_PLANE], int tmp_width[MAX_MB_PLANE],
787
949k
    int tmp_height[MAX_MB_PLANE], int tmp_stride[MAX_MB_PLANE]) {
788
949k
  MACROBLOCKD *const xd = &dcb->xd;
789
949k
  if (!xd->left_available) return;
790
791
  // Adjust mb_to_right_edge to have the correct value for the OBMC
792
  // prediction block. This is half the width of the original block,
793
  // except for 128-wide blocks, where we only use a width of 32.
794
939k
  const int this_width = xd->width * MI_SIZE;
795
939k
  const int pred_width = AOMMIN(this_width / 2, 32);
796
939k
  xd->mb_to_right_edge += GET_MV_SUBPEL(this_width - pred_width);
797
798
939k
  struct build_prediction_ctxt ctxt = {
799
939k
    cm, tmp_buf, tmp_width, tmp_height, tmp_stride, xd->mb_to_bottom_edge, dcb
800
939k
  };
801
939k
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
802
939k
  foreach_overlappable_nb_left(cm, xd,
803
939k
                               max_neighbor_obmc[mi_size_high_log2[bsize]],
804
939k
                               dec_build_prediction_by_left_pred, &ctxt);
805
806
939k
  xd->mb_to_top_edge = -GET_MV_SUBPEL(xd->mi_row * MI_SIZE);
807
939k
  xd->mb_to_right_edge -= GET_MV_SUBPEL(this_width - pred_width);
808
939k
  xd->mb_to_bottom_edge = ctxt.mb_to_far_edge;
809
939k
}
810
811
static AOM_INLINE void dec_build_obmc_inter_predictors_sb(
812
949k
    const AV1_COMMON *cm, DecoderCodingBlock *dcb) {
813
949k
  const int num_planes = av1_num_planes(cm);
814
949k
  uint8_t *dst_buf1[MAX_MB_PLANE], *dst_buf2[MAX_MB_PLANE];
815
949k
  int dst_stride1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
816
949k
  int dst_stride2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
817
949k
  int dst_width1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
818
949k
  int dst_width2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
819
949k
  int dst_height1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
820
949k
  int dst_height2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
821
822
949k
  MACROBLOCKD *const xd = &dcb->xd;
823
949k
  av1_setup_obmc_dst_bufs(xd, dst_buf1, dst_buf2);
824
825
949k
  dec_build_prediction_by_above_preds(cm, dcb, dst_buf1, dst_width1,
826
949k
                                      dst_height1, dst_stride1);
827
949k
  dec_build_prediction_by_left_preds(cm, dcb, dst_buf2, dst_width2, dst_height2,
828
949k
                                     dst_stride2);
829
949k
  const int mi_row = xd->mi_row;
830
949k
  const int mi_col = xd->mi_col;
831
949k
  av1_setup_dst_planes(xd->plane, xd->mi[0]->bsize, &cm->cur_frame->buf, mi_row,
832
949k
                       mi_col, 0, num_planes);
833
949k
  av1_build_obmc_inter_prediction(cm, xd, dst_buf1, dst_stride1, dst_buf2,
834
949k
                                  dst_stride2);
835
949k
}
836
837
static AOM_INLINE void cfl_store_inter_block(AV1_COMMON *const cm,
838
5.66M
                                             MACROBLOCKD *const xd) {
839
5.66M
  MB_MODE_INFO *mbmi = xd->mi[0];
840
5.66M
  if (store_cfl_required(cm, xd)) {
841
923k
    cfl_store_block(xd, mbmi->bsize, mbmi->tx_size);
842
923k
  }
843
5.66M
}
844
845
static AOM_INLINE void predict_inter_block(AV1_COMMON *const cm,
846
                                           DecoderCodingBlock *dcb,
847
5.66M
                                           BLOCK_SIZE bsize) {
848
5.66M
  MACROBLOCKD *const xd = &dcb->xd;
849
5.66M
  MB_MODE_INFO *mbmi = xd->mi[0];
850
5.66M
  const int num_planes = av1_num_planes(cm);
851
5.66M
  const int mi_row = xd->mi_row;
852
5.66M
  const int mi_col = xd->mi_col;
853
12.3M
  for (int ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
854
6.64M
    const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
855
6.64M
    if (frame < LAST_FRAME) {
856
58.9k
      assert(is_intrabc_block(mbmi));
857
58.9k
      assert(frame == INTRA_FRAME);
858
58.9k
      assert(ref == 0);
859
6.58M
    } else {
860
6.58M
      const RefCntBuffer *ref_buf = get_ref_frame_buf(cm, frame);
861
6.58M
      const struct scale_factors *ref_scale_factors =
862
6.58M
          get_ref_scale_factors_const(cm, frame);
863
864
6.58M
      xd->block_ref_scale_factors[ref] = ref_scale_factors;
865
6.58M
      av1_setup_pre_planes(xd, ref, &ref_buf->buf, mi_row, mi_col,
866
6.58M
                           ref_scale_factors, num_planes);
867
6.58M
    }
868
6.64M
  }
869
870
5.66M
  dec_build_inter_predictor(cm, dcb, mi_row, mi_col, bsize);
871
5.66M
  if (mbmi->motion_mode == OBMC_CAUSAL) {
872
949k
    dec_build_obmc_inter_predictors_sb(cm, dcb);
873
949k
  }
874
#if CONFIG_MISMATCH_DEBUG
875
  for (int plane = 0; plane < num_planes; ++plane) {
876
    const struct macroblockd_plane *pd = &xd->plane[plane];
877
    int pixel_c, pixel_r;
878
    mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, 0, 0, pd->subsampling_x,
879
                    pd->subsampling_y);
880
    if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
881
                             pd->subsampling_y))
882
      continue;
883
    mismatch_check_block_pre(pd->dst.buf, pd->dst.stride,
884
                             cm->current_frame.order_hint, plane, pixel_c,
885
                             pixel_r, pd->width, pd->height,
886
                             xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
887
  }
888
#endif
889
5.66M
}
890
891
static AOM_INLINE void set_color_index_map_offset(MACROBLOCKD *const xd,
892
101k
                                                  int plane, aom_reader *r) {
893
101k
  (void)r;
894
101k
  Av1ColorMapParam params;
895
101k
  const MB_MODE_INFO *const mbmi = xd->mi[0];
896
101k
  av1_get_block_dimensions(mbmi->bsize, plane, xd, &params.plane_width,
897
101k
                           &params.plane_height, NULL, NULL);
898
101k
  xd->color_index_map_offset[plane] += params.plane_width * params.plane_height;
899
101k
}
900
901
static AOM_INLINE void decode_token_recon_block(AV1Decoder *const pbi,
902
                                                ThreadData *const td,
903
                                                aom_reader *r,
904
26.1M
                                                BLOCK_SIZE bsize) {
905
26.1M
  AV1_COMMON *const cm = &pbi->common;
906
26.1M
  DecoderCodingBlock *const dcb = &td->dcb;
907
26.1M
  MACROBLOCKD *const xd = &dcb->xd;
908
26.1M
  const int num_planes = av1_num_planes(cm);
909
26.1M
  MB_MODE_INFO *mbmi = xd->mi[0];
910
911
26.1M
  if (!is_inter_block(mbmi)) {
912
15.1M
    int row, col;
913
15.1M
    assert(bsize == get_plane_block_size(bsize, xd->plane[0].subsampling_x,
914
15.1M
                                         xd->plane[0].subsampling_y));
915
15.1M
    const int max_blocks_wide = max_block_wide(xd, bsize, 0);
916
15.1M
    const int max_blocks_high = max_block_high(xd, bsize, 0);
917
15.1M
    const BLOCK_SIZE max_unit_bsize = BLOCK_64X64;
918
15.1M
    int mu_blocks_wide = mi_size_wide[max_unit_bsize];
919
15.1M
    int mu_blocks_high = mi_size_high[max_unit_bsize];
920
15.1M
    mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
921
15.1M
    mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
922
923
30.3M
    for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
924
30.9M
      for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
925
59.6M
        for (int plane = 0; plane < num_planes; ++plane) {
926
45.0M
          if (plane && !xd->is_chroma_ref) break;
927
43.9M
          const struct macroblockd_plane *const pd = &xd->plane[plane];
928
43.9M
          const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
929
43.9M
          const int stepr = tx_size_high_unit[tx_size];
930
43.9M
          const int stepc = tx_size_wide_unit[tx_size];
931
932
43.9M
          const int unit_height = ROUND_POWER_OF_TWO(
933
43.9M
              AOMMIN(mu_blocks_high + row, max_blocks_high), pd->subsampling_y);
934
43.9M
          const int unit_width = ROUND_POWER_OF_TWO(
935
43.9M
              AOMMIN(mu_blocks_wide + col, max_blocks_wide), pd->subsampling_x);
936
937
99.4M
          for (int blk_row = row >> pd->subsampling_y; blk_row < unit_height;
938
55.5M
               blk_row += stepr) {
939
219M
            for (int blk_col = col >> pd->subsampling_x; blk_col < unit_width;
940
164M
                 blk_col += stepc) {
941
164M
              td->read_coeffs_tx_intra_block_visit(cm, dcb, r, plane, blk_row,
942
164M
                                                   blk_col, tx_size);
943
164M
              td->predict_and_recon_intra_block_visit(
944
164M
                  cm, dcb, r, plane, blk_row, blk_col, tx_size);
945
164M
              set_cb_buffer_offsets(dcb, tx_size, plane);
946
164M
            }
947
55.5M
          }
948
43.9M
        }
949
15.6M
      }
950
15.2M
    }
951
15.1M
  } else {
952
11.0M
    td->predict_inter_block_visit(cm, dcb, bsize);
953
    // Reconstruction
954
11.0M
    if (!mbmi->skip_txfm) {
955
6.84M
      int eobtotal = 0;
956
957
6.84M
      const int max_blocks_wide = max_block_wide(xd, bsize, 0);
958
6.84M
      const int max_blocks_high = max_block_high(xd, bsize, 0);
959
6.84M
      int row, col;
960
961
6.84M
      const BLOCK_SIZE max_unit_bsize = BLOCK_64X64;
962
6.84M
      assert(max_unit_bsize ==
963
6.84M
             get_plane_block_size(BLOCK_64X64, xd->plane[0].subsampling_x,
964
6.84M
                                  xd->plane[0].subsampling_y));
965
6.82M
      int mu_blocks_wide = mi_size_wide[max_unit_bsize];
966
6.82M
      int mu_blocks_high = mi_size_high[max_unit_bsize];
967
968
6.82M
      mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
969
6.82M
      mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
970
971
13.6M
      for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
972
13.7M
        for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
973
24.9M
          for (int plane = 0; plane < num_planes; ++plane) {
974
19.3M
            if (plane && !xd->is_chroma_ref) break;
975
18.0M
            const struct macroblockd_plane *const pd = &xd->plane[plane];
976
18.0M
            const int ss_x = pd->subsampling_x;
977
18.0M
            const int ss_y = pd->subsampling_y;
978
18.0M
            const BLOCK_SIZE plane_bsize =
979
18.0M
                get_plane_block_size(bsize, ss_x, ss_y);
980
18.0M
            const TX_SIZE max_tx_size =
981
18.0M
                get_vartx_max_txsize(xd, plane_bsize, plane);
982
18.0M
            const int bh_var_tx = tx_size_high_unit[max_tx_size];
983
18.0M
            const int bw_var_tx = tx_size_wide_unit[max_tx_size];
984
18.0M
            int block = 0;
985
18.0M
            int step =
986
18.0M
                tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
987
18.0M
            int blk_row, blk_col;
988
18.0M
            const int unit_height = ROUND_POWER_OF_TWO(
989
18.0M
                AOMMIN(mu_blocks_high + row, max_blocks_high), ss_y);
990
18.0M
            const int unit_width = ROUND_POWER_OF_TWO(
991
18.0M
                AOMMIN(mu_blocks_wide + col, max_blocks_wide), ss_x);
992
993
36.0M
            for (blk_row = row >> ss_y; blk_row < unit_height;
994
18.0M
                 blk_row += bh_var_tx) {
995
37.2M
              for (blk_col = col >> ss_x; blk_col < unit_width;
996
19.1M
                   blk_col += bw_var_tx) {
997
19.1M
                decode_reconstruct_tx(cm, td, r, mbmi, plane, plane_bsize,
998
19.1M
                                      blk_row, blk_col, block, max_tx_size,
999
19.1M
                                      &eobtotal);
1000
19.1M
                block += step;
1001
19.1M
              }
1002
18.0M
            }
1003
18.0M
          }
1004
6.90M
        }
1005
6.85M
      }
1006
6.82M
    }
1007
10.9M
    td->cfl_store_inter_block_visit(cm, xd);
1008
10.9M
  }
1009
1010
26.1M
  av1_visit_palette(pbi, xd, r, set_color_index_map_offset);
1011
26.1M
}
1012
1013
static AOM_INLINE void set_inter_tx_size(MB_MODE_INFO *mbmi, int stride_log2,
1014
                                         int tx_w_log2, int tx_h_log2,
1015
                                         int min_txs, int split_size, int txs,
1016
1.17M
                                         int blk_row, int blk_col) {
1017
2.76M
  for (int idy = 0; idy < tx_size_high_unit[split_size];
1018
1.58M
       idy += tx_size_high_unit[min_txs]) {
1019
3.94M
    for (int idx = 0; idx < tx_size_wide_unit[split_size];
1020
2.35M
         idx += tx_size_wide_unit[min_txs]) {
1021
2.35M
      const int index = (((blk_row + idy) >> tx_h_log2) << stride_log2) +
1022
2.35M
                        ((blk_col + idx) >> tx_w_log2);
1023
2.35M
      mbmi->inter_tx_size[index] = txs;
1024
2.35M
    }
1025
1.58M
  }
1026
1.17M
}
1027
1028
static AOM_INLINE void read_tx_size_vartx(MACROBLOCKD *xd, MB_MODE_INFO *mbmi,
1029
                                          TX_SIZE tx_size, int depth,
1030
                                          int blk_row, int blk_col,
1031
1.38M
                                          aom_reader *r) {
1032
1.38M
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1033
1.38M
  int is_split = 0;
1034
1.38M
  const BLOCK_SIZE bsize = mbmi->bsize;
1035
1.38M
  const int max_blocks_high = max_block_high(xd, bsize, 0);
1036
1.38M
  const int max_blocks_wide = max_block_wide(xd, bsize, 0);
1037
1.38M
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
1038
1.38M
  assert(tx_size > TX_4X4);
1039
1.38M
  TX_SIZE txs = max_txsize_rect_lookup[bsize];
1040
2.77M
  for (int level = 0; level < MAX_VARTX_DEPTH - 1; ++level)
1041
1.38M
    txs = sub_tx_size_map[txs];
1042
1.38M
  const int tx_w_log2 = tx_size_wide_log2[txs] - MI_SIZE_LOG2;
1043
1.38M
  const int tx_h_log2 = tx_size_high_log2[txs] - MI_SIZE_LOG2;
1044
1.38M
  const int bw_log2 = mi_size_wide_log2[bsize];
1045
1.38M
  const int stride_log2 = bw_log2 - tx_w_log2;
1046
1047
1.38M
  if (depth == MAX_VARTX_DEPTH) {
1048
141k
    set_inter_tx_size(mbmi, stride_log2, tx_w_log2, tx_h_log2, txs, tx_size,
1049
141k
                      tx_size, blk_row, blk_col);
1050
141k
    mbmi->tx_size = tx_size;
1051
141k
    txfm_partition_update(xd->above_txfm_context + blk_col,
1052
141k
                          xd->left_txfm_context + blk_row, tx_size, tx_size);
1053
141k
    return;
1054
141k
  }
1055
1056
1.24M
  const int ctx = txfm_partition_context(xd->above_txfm_context + blk_col,
1057
1.24M
                                         xd->left_txfm_context + blk_row,
1058
1.24M
                                         mbmi->bsize, tx_size);
1059
1.24M
  is_split = aom_read_symbol(r, ec_ctx->txfm_partition_cdf[ctx], 2, ACCT_STR);
1060
1061
1.24M
  if (is_split) {
1062
289k
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
1063
289k
    const int bsw = tx_size_wide_unit[sub_txs];
1064
289k
    const int bsh = tx_size_high_unit[sub_txs];
1065
1066
289k
    if (sub_txs == TX_4X4) {
1067
73.7k
      set_inter_tx_size(mbmi, stride_log2, tx_w_log2, tx_h_log2, txs, tx_size,
1068
73.7k
                        sub_txs, blk_row, blk_col);
1069
73.7k
      mbmi->tx_size = sub_txs;
1070
73.7k
      txfm_partition_update(xd->above_txfm_context + blk_col,
1071
73.7k
                            xd->left_txfm_context + blk_row, sub_txs, tx_size);
1072
73.7k
      return;
1073
73.7k
    }
1074
1075
215k
    assert(bsw > 0 && bsh > 0);
1076
563k
    for (int row = 0; row < tx_size_high_unit[tx_size]; row += bsh) {
1077
925k
      for (int col = 0; col < tx_size_wide_unit[tx_size]; col += bsw) {
1078
577k
        int offsetr = blk_row + row;
1079
577k
        int offsetc = blk_col + col;
1080
577k
        read_tx_size_vartx(xd, mbmi, sub_txs, depth + 1, offsetr, offsetc, r);
1081
577k
      }
1082
347k
    }
1083
955k
  } else {
1084
955k
    set_inter_tx_size(mbmi, stride_log2, tx_w_log2, tx_h_log2, txs, tx_size,
1085
955k
                      tx_size, blk_row, blk_col);
1086
955k
    mbmi->tx_size = tx_size;
1087
955k
    txfm_partition_update(xd->above_txfm_context + blk_col,
1088
955k
                          xd->left_txfm_context + blk_row, tx_size, tx_size);
1089
955k
  }
1090
1.24M
}
1091
1092
static TX_SIZE read_selected_tx_size(const MACROBLOCKD *const xd,
1093
1.48M
                                     aom_reader *r) {
1094
  // TODO(debargha): Clean up the logic here. This function should only
1095
  // be called for intra.
1096
1.48M
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
1097
1.48M
  const int32_t tx_size_cat = bsize_to_tx_size_cat(bsize);
1098
1.48M
  const int max_depths = bsize_to_max_depth(bsize);
1099
1.48M
  const int ctx = get_tx_size_context(xd);
1100
1.48M
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1101
1.48M
  const int depth = aom_read_symbol(r, ec_ctx->tx_size_cdf[tx_size_cat][ctx],
1102
1.48M
                                    max_depths + 1, ACCT_STR);
1103
1.48M
  assert(depth >= 0 && depth <= max_depths);
1104
1.48M
  const TX_SIZE tx_size = depth_to_tx_size(depth, bsize);
1105
1.48M
  return tx_size;
1106
1.48M
}
1107
1108
static TX_SIZE read_tx_size(const MACROBLOCKD *const xd, TX_MODE tx_mode,
1109
                            int is_inter, int allow_select_inter,
1110
15.8M
                            aom_reader *r) {
1111
15.8M
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
1112
15.8M
  if (xd->lossless[xd->mi[0]->segment_id]) return TX_4X4;
1113
1114
15.7M
  if (block_signals_txsize(bsize)) {
1115
14.8M
    if ((!is_inter || allow_select_inter) && tx_mode == TX_MODE_SELECT) {
1116
1.48M
      const TX_SIZE coded_tx_size = read_selected_tx_size(xd, r);
1117
1.48M
      return coded_tx_size;
1118
13.4M
    } else {
1119
13.4M
      return tx_size_from_tx_mode(bsize, tx_mode);
1120
13.4M
    }
1121
14.8M
  } else {
1122
883k
    assert(IMPLIES(tx_mode == ONLY_4X4, bsize == BLOCK_4X4));
1123
884k
    return max_txsize_rect_lookup[bsize];
1124
883k
  }
1125
15.7M
}
1126
1127
static AOM_INLINE void parse_decode_block(AV1Decoder *const pbi,
1128
                                          ThreadData *const td, int mi_row,
1129
                                          int mi_col, aom_reader *r,
1130
                                          PARTITION_TYPE partition,
1131
16.6M
                                          BLOCK_SIZE bsize) {
1132
16.6M
  DecoderCodingBlock *const dcb = &td->dcb;
1133
16.6M
  MACROBLOCKD *const xd = &dcb->xd;
1134
16.6M
  decode_mbmi_block(pbi, dcb, mi_row, mi_col, r, partition, bsize);
1135
1136
16.6M
  av1_visit_palette(pbi, xd, r, av1_decode_palette_tokens);
1137
1138
16.6M
  AV1_COMMON *cm = &pbi->common;
1139
16.6M
  const int num_planes = av1_num_planes(cm);
1140
16.6M
  MB_MODE_INFO *mbmi = xd->mi[0];
1141
16.6M
  int inter_block_tx = is_inter_block(mbmi) || is_intrabc_block(mbmi);
1142
16.6M
  if (cm->features.tx_mode == TX_MODE_SELECT && block_signals_txsize(bsize) &&
1143
16.6M
      !mbmi->skip_txfm && inter_block_tx && !xd->lossless[mbmi->segment_id]) {
1144
791k
    const TX_SIZE max_tx_size = max_txsize_rect_lookup[bsize];
1145
791k
    const int bh = tx_size_high_unit[max_tx_size];
1146
791k
    const int bw = tx_size_wide_unit[max_tx_size];
1147
791k
    const int width = mi_size_wide[bsize];
1148
791k
    const int height = mi_size_high[bsize];
1149
1150
1.58M
    for (int idy = 0; idy < height; idy += bh)
1151
1.60M
      for (int idx = 0; idx < width; idx += bw)
1152
810k
        read_tx_size_vartx(xd, mbmi, max_tx_size, 0, idy, idx, r);
1153
15.9M
  } else {
1154
15.9M
    mbmi->tx_size = read_tx_size(xd, cm->features.tx_mode, inter_block_tx,
1155
15.9M
                                 !mbmi->skip_txfm, r);
1156
15.9M
    if (inter_block_tx)
1157
5.23M
      memset(mbmi->inter_tx_size, mbmi->tx_size, sizeof(mbmi->inter_tx_size));
1158
15.9M
    set_txfm_ctxs(mbmi->tx_size, xd->width, xd->height,
1159
15.9M
                  mbmi->skip_txfm && is_inter_block(mbmi), xd);
1160
15.9M
  }
1161
1162
16.6M
  if (cm->delta_q_info.delta_q_present_flag) {
1163
18.2M
    for (int i = 0; i < MAX_SEGMENTS; i++) {
1164
16.2M
      const int current_qindex =
1165
16.2M
          av1_get_qindex(&cm->seg, i, xd->current_base_qindex);
1166
16.2M
      const CommonQuantParams *const quant_params = &cm->quant_params;
1167
64.3M
      for (int j = 0; j < num_planes; ++j) {
1168
48.1M
        const int dc_delta_q = j == 0 ? quant_params->y_dc_delta_q
1169
48.1M
                                      : (j == 1 ? quant_params->u_dc_delta_q
1170
31.8M
                                                : quant_params->v_dc_delta_q);
1171
48.1M
        const int ac_delta_q = j == 0 ? 0
1172
48.1M
                                      : (j == 1 ? quant_params->u_ac_delta_q
1173
31.8M
                                                : quant_params->v_ac_delta_q);
1174
48.1M
        xd->plane[j].seg_dequant_QTX[i][0] = av1_dc_quant_QTX(
1175
48.1M
            current_qindex, dc_delta_q, cm->seq_params->bit_depth);
1176
48.1M
        xd->plane[j].seg_dequant_QTX[i][1] = av1_ac_quant_QTX(
1177
48.1M
            current_qindex, ac_delta_q, cm->seq_params->bit_depth);
1178
48.1M
      }
1179
16.2M
    }
1180
2.03M
  }
1181
16.6M
  if (mbmi->skip_txfm) av1_reset_entropy_context(xd, bsize, num_planes);
1182
1183
16.6M
  decode_token_recon_block(pbi, td, r, bsize);
1184
16.6M
}
1185
1186
static AOM_INLINE void set_offsets_for_pred_and_recon(AV1Decoder *const pbi,
1187
                                                      ThreadData *const td,
1188
                                                      int mi_row, int mi_col,
1189
9.47M
                                                      BLOCK_SIZE bsize) {
1190
9.47M
  AV1_COMMON *const cm = &pbi->common;
1191
9.47M
  const CommonModeInfoParams *const mi_params = &cm->mi_params;
1192
9.47M
  DecoderCodingBlock *const dcb = &td->dcb;
1193
9.47M
  MACROBLOCKD *const xd = &dcb->xd;
1194
9.47M
  const int bw = mi_size_wide[bsize];
1195
9.47M
  const int bh = mi_size_high[bsize];
1196
9.47M
  const int num_planes = av1_num_planes(cm);
1197
1198
9.47M
  const int offset = mi_row * mi_params->mi_stride + mi_col;
1199
9.47M
  const TileInfo *const tile = &xd->tile;
1200
1201
9.47M
  xd->mi = mi_params->mi_grid_base + offset;
1202
9.47M
  xd->tx_type_map =
1203
9.47M
      &mi_params->tx_type_map[mi_row * mi_params->mi_stride + mi_col];
1204
9.47M
  xd->tx_type_map_stride = mi_params->mi_stride;
1205
1206
9.47M
  set_plane_n4(xd, bw, bh, num_planes);
1207
1208
  // Distance of Mb to the various image edges. These are specified to 8th pel
1209
  // as they are always compared to values that are in 1/8th pel units
1210
9.47M
  set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, mi_params->mi_rows,
1211
9.47M
                 mi_params->mi_cols);
1212
1213
9.47M
  av1_setup_dst_planes(xd->plane, bsize, &cm->cur_frame->buf, mi_row, mi_col, 0,
1214
9.47M
                       num_planes);
1215
9.47M
}
1216
1217
static AOM_INLINE void decode_block(AV1Decoder *const pbi, ThreadData *const td,
1218
                                    int mi_row, int mi_col, aom_reader *r,
1219
                                    PARTITION_TYPE partition,
1220
9.47M
                                    BLOCK_SIZE bsize) {
1221
9.47M
  (void)partition;
1222
9.47M
  set_offsets_for_pred_and_recon(pbi, td, mi_row, mi_col, bsize);
1223
9.47M
  decode_token_recon_block(pbi, td, r, bsize);
1224
9.47M
}
1225
1226
static PARTITION_TYPE read_partition(MACROBLOCKD *xd, int mi_row, int mi_col,
1227
                                     aom_reader *r, int has_rows, int has_cols,
1228
11.4M
                                     BLOCK_SIZE bsize) {
1229
11.4M
  const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
1230
11.4M
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1231
1232
11.4M
  if (!has_rows && !has_cols) return PARTITION_SPLIT;
1233
1234
11.3M
  assert(ctx >= 0);
1235
11.3M
  aom_cdf_prob *partition_cdf = ec_ctx->partition_cdf[ctx];
1236
11.3M
  if (has_rows && has_cols) {
1237
10.9M
    return (PARTITION_TYPE)aom_read_symbol(
1238
10.9M
        r, partition_cdf, partition_cdf_length(bsize), ACCT_STR);
1239
10.9M
  } else if (!has_rows && has_cols) {
1240
149k
    assert(bsize > BLOCK_8X8);
1241
149k
    aom_cdf_prob cdf[2];
1242
149k
    partition_gather_vert_alike(cdf, partition_cdf, bsize);
1243
149k
    assert(cdf[1] == AOM_ICDF(CDF_PROB_TOP));
1244
149k
    return aom_read_cdf(r, cdf, 2, ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
1245
306k
  } else {
1246
306k
    assert(has_rows && !has_cols);
1247
306k
    assert(bsize > BLOCK_8X8);
1248
306k
    aom_cdf_prob cdf[2];
1249
306k
    partition_gather_horz_alike(cdf, partition_cdf, bsize);
1250
306k
    assert(cdf[1] == AOM_ICDF(CDF_PROB_TOP));
1251
306k
    return aom_read_cdf(r, cdf, 2, ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
1252
306k
  }
1253
11.3M
}
1254
1255
// TODO(slavarnway): eliminate bsize and subsize in future commits
1256
static AOM_INLINE void decode_partition(AV1Decoder *const pbi,
1257
                                        ThreadData *const td, int mi_row,
1258
                                        int mi_col, aom_reader *reader,
1259
                                        BLOCK_SIZE bsize,
1260
20.3M
                                        int parse_decode_flag) {
1261
20.3M
  assert(bsize < BLOCK_SIZES_ALL);
1262
20.3M
  AV1_COMMON *const cm = &pbi->common;
1263
20.3M
  DecoderCodingBlock *const dcb = &td->dcb;
1264
20.3M
  MACROBLOCKD *const xd = &dcb->xd;
1265
20.3M
  const int bw = mi_size_wide[bsize];
1266
20.3M
  const int hbs = bw >> 1;
1267
20.3M
  PARTITION_TYPE partition;
1268
20.3M
  BLOCK_SIZE subsize;
1269
20.3M
  const int quarter_step = bw / 4;
1270
20.3M
  BLOCK_SIZE bsize2 = get_partition_subsize(bsize, PARTITION_SPLIT);
1271
20.3M
  const int has_rows = (mi_row + hbs) < cm->mi_params.mi_rows;
1272
20.3M
  const int has_cols = (mi_col + hbs) < cm->mi_params.mi_cols;
1273
1274
20.3M
  if (mi_row >= cm->mi_params.mi_rows || mi_col >= cm->mi_params.mi_cols)
1275
858k
    return;
1276
1277
  // parse_decode_flag takes the following values :
1278
  // 01 - do parse only
1279
  // 10 - do decode only
1280
  // 11 - do parse and decode
1281
19.4M
  static const block_visitor_fn_t block_visit[4] = { NULL, parse_decode_block,
1282
19.4M
                                                     decode_block,
1283
19.4M
                                                     parse_decode_block };
1284
1285
19.4M
  if (parse_decode_flag & 1) {
1286
12.3M
    const int num_planes = av1_num_planes(cm);
1287
48.9M
    for (int plane = 0; plane < num_planes; ++plane) {
1288
36.6M
      int rcol0, rcol1, rrow0, rrow1;
1289
1290
      // Skip some unnecessary work if loop restoration is disabled
1291
36.6M
      if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
1292
1293
20.4M
      if (av1_loop_restoration_corners_in_sb(cm, plane, mi_row, mi_col, bsize,
1294
20.4M
                                             &rcol0, &rcol1, &rrow0, &rrow1)) {
1295
265k
        const int rstride = cm->rst_info[plane].horz_units;
1296
530k
        for (int rrow = rrow0; rrow < rrow1; ++rrow) {
1297
532k
          for (int rcol = rcol0; rcol < rcol1; ++rcol) {
1298
267k
            const int runit_idx = rcol + rrow * rstride;
1299
267k
            loop_restoration_read_sb_coeffs(cm, xd, reader, plane, runit_idx);
1300
267k
          }
1301
265k
        }
1302
265k
      }
1303
20.4M
    }
1304
1305
12.3M
    partition = (bsize < BLOCK_8X8) ? PARTITION_NONE
1306
12.3M
                                    : read_partition(xd, mi_row, mi_col, reader,
1307
11.4M
                                                     has_rows, has_cols, bsize);
1308
12.3M
  } else {
1309
7.12M
    partition = get_partition(cm, mi_row, mi_col, bsize);
1310
7.12M
  }
1311
19.4M
  subsize = get_partition_subsize(bsize, partition);
1312
19.4M
  if (subsize == BLOCK_INVALID) {
1313
    // When an internal error occurs ensure that xd->mi_row is set appropriately
1314
    // w.r.t. current tile, which is used to signal processing of current row is
1315
    // done.
1316
0
    xd->mi_row = mi_row;
1317
0
    aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
1318
0
                       "Partition is invalid for block size %dx%d",
1319
0
                       block_size_wide[bsize], block_size_high[bsize]);
1320
0
  }
1321
  // Check the bitstream is conformant: if there is subsampling on the
1322
  // chroma planes, subsize must subsample to a valid block size.
1323
19.4M
  const struct macroblockd_plane *const pd_u = &xd->plane[1];
1324
19.4M
  if (get_plane_block_size(subsize, pd_u->subsampling_x, pd_u->subsampling_y) ==
1325
19.4M
      BLOCK_INVALID) {
1326
    // When an internal error occurs ensure that xd->mi_row is set appropriately
1327
    // w.r.t. current tile, which is used to signal processing of current row is
1328
    // done.
1329
694
    xd->mi_row = mi_row;
1330
694
    aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
1331
694
                       "Block size %dx%d invalid with this subsampling mode",
1332
694
                       block_size_wide[subsize], block_size_high[subsize]);
1333
694
  }
1334
1335
19.4M
#define DEC_BLOCK_STX_ARG
1336
26.1M
#define DEC_BLOCK_EPT_ARG partition,
1337
19.4M
#define DEC_BLOCK(db_r, db_c, db_subsize)                                  \
1338
26.1M
  block_visit[parse_decode_flag](pbi, td, DEC_BLOCK_STX_ARG(db_r), (db_c), \
1339
26.1M
                                 reader, DEC_BLOCK_EPT_ARG(db_subsize))
1340
19.4M
#define DEC_PARTITION(db_r, db_c, db_subsize)                        \
1341
19.4M
  decode_partition(pbi, td, DEC_BLOCK_STX_ARG(db_r), (db_c), reader, \
1342
18.1M
                   (db_subsize), parse_decode_flag)
1343
1344
19.4M
  switch (partition) {
1345
8.15M
    case PARTITION_NONE: DEC_BLOCK(mi_row, mi_col, subsize); break;
1346
1.99M
    case PARTITION_HORZ:
1347
1.99M
      DEC_BLOCK(mi_row, mi_col, subsize);
1348
1.99M
      if (has_rows) DEC_BLOCK(mi_row + hbs, mi_col, subsize);
1349
1.99M
      break;
1350
1.70M
    case PARTITION_VERT:
1351
1.70M
      DEC_BLOCK(mi_row, mi_col, subsize);
1352
1.70M
      if (has_cols) DEC_BLOCK(mi_row, mi_col + hbs, subsize);
1353
1.70M
      break;
1354
4.53M
    case PARTITION_SPLIT:
1355
4.53M
      DEC_PARTITION(mi_row, mi_col, subsize);
1356
4.53M
      DEC_PARTITION(mi_row, mi_col + hbs, subsize);
1357
4.53M
      DEC_PARTITION(mi_row + hbs, mi_col, subsize);
1358
4.53M
      DEC_PARTITION(mi_row + hbs, mi_col + hbs, subsize);
1359
4.53M
      break;
1360
384k
    case PARTITION_HORZ_A:
1361
384k
      DEC_BLOCK(mi_row, mi_col, bsize2);
1362
384k
      DEC_BLOCK(mi_row, mi_col + hbs, bsize2);
1363
384k
      DEC_BLOCK(mi_row + hbs, mi_col, subsize);
1364
384k
      break;
1365
398k
    case PARTITION_HORZ_B:
1366
398k
      DEC_BLOCK(mi_row, mi_col, subsize);
1367
398k
      DEC_BLOCK(mi_row + hbs, mi_col, bsize2);
1368
398k
      DEC_BLOCK(mi_row + hbs, mi_col + hbs, bsize2);
1369
398k
      break;
1370
282k
    case PARTITION_VERT_A:
1371
282k
      DEC_BLOCK(mi_row, mi_col, bsize2);
1372
282k
      DEC_BLOCK(mi_row + hbs, mi_col, bsize2);
1373
282k
      DEC_BLOCK(mi_row, mi_col + hbs, subsize);
1374
282k
      break;
1375
300k
    case PARTITION_VERT_B:
1376
300k
      DEC_BLOCK(mi_row, mi_col, subsize);
1377
300k
      DEC_BLOCK(mi_row, mi_col + hbs, bsize2);
1378
300k
      DEC_BLOCK(mi_row + hbs, mi_col + hbs, bsize2);
1379
300k
      break;
1380
1.11M
    case PARTITION_HORZ_4:
1381
5.56M
      for (int i = 0; i < 4; ++i) {
1382
4.45M
        int this_mi_row = mi_row + i * quarter_step;
1383
4.45M
        if (i > 0 && this_mi_row >= cm->mi_params.mi_rows) break;
1384
4.45M
        DEC_BLOCK(this_mi_row, mi_col, subsize);
1385
4.45M
      }
1386
1.11M
      break;
1387
634k
    case PARTITION_VERT_4:
1388
3.16M
      for (int i = 0; i < 4; ++i) {
1389
2.53M
        int this_mi_col = mi_col + i * quarter_step;
1390
2.53M
        if (i > 0 && this_mi_col >= cm->mi_params.mi_cols) break;
1391
2.53M
        DEC_BLOCK(mi_row, this_mi_col, subsize);
1392
2.53M
      }
1393
634k
      break;
1394
0
    default: assert(0 && "Invalid partition type");
1395
19.4M
  }
1396
1397
19.4M
#undef DEC_PARTITION
1398
19.4M
#undef DEC_BLOCK
1399
19.4M
#undef DEC_BLOCK_EPT_ARG
1400
19.4M
#undef DEC_BLOCK_STX_ARG
1401
1402
19.4M
  if (parse_decode_flag & 1)
1403
12.3M
    update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition);
1404
19.4M
}
1405
1406
static AOM_INLINE void setup_bool_decoder(
1407
    MACROBLOCKD *const xd, const uint8_t *data, const uint8_t *data_end,
1408
    const size_t read_size, struct aom_internal_error_info *error_info,
1409
205k
    aom_reader *r, uint8_t allow_update_cdf) {
1410
  // Validate the calculated partition length. If the buffer
1411
  // described by the partition can't be fully read, then restrict
1412
  // it to the portion that can be (for EC mode) or throw an error.
1413
205k
  if (!read_is_valid(data, read_size, data_end)) {
1414
    // When internal error occurs ensure that xd->mi_row is set appropriately
1415
    // w.r.t. current tile, which is used to signal processing of current row is
1416
    // done in row-mt decoding.
1417
236
    xd->mi_row = xd->tile.mi_row_start;
1418
1419
236
    aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
1420
236
                       "Truncated packet or corrupt tile length");
1421
236
  }
1422
205k
  if (aom_reader_init(r, data, read_size)) {
1423
    // When internal error occurs ensure that xd->mi_row is set appropriately
1424
    // w.r.t. current tile, which is used to signal processing of current row is
1425
    // done in row-mt decoding.
1426
0
    xd->mi_row = xd->tile.mi_row_start;
1427
1428
0
    aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
1429
0
                       "Failed to allocate bool decoder %d", 1);
1430
0
  }
1431
1432
205k
  r->allow_update_cdf = allow_update_cdf;
1433
205k
}
1434
1435
static AOM_INLINE void setup_segmentation(AV1_COMMON *const cm,
1436
208k
                                          struct aom_read_bit_buffer *rb) {
1437
208k
  struct segmentation *const seg = &cm->seg;
1438
1439
208k
  seg->update_map = 0;
1440
208k
  seg->update_data = 0;
1441
208k
  seg->temporal_update = 0;
1442
1443
208k
  seg->enabled = aom_rb_read_bit(rb);
1444
208k
  if (!seg->enabled) {
1445
184k
    if (cm->cur_frame->seg_map) {
1446
184k
      memset(cm->cur_frame->seg_map, 0,
1447
184k
             (cm->cur_frame->mi_rows * cm->cur_frame->mi_cols));
1448
184k
    }
1449
1450
184k
    memset(seg, 0, sizeof(*seg));
1451
184k
    segfeatures_copy(&cm->cur_frame->seg, seg);
1452
184k
    return;
1453
184k
  }
1454
23.7k
  if (cm->seg.enabled && cm->prev_frame &&
1455
23.7k
      (cm->mi_params.mi_rows == cm->prev_frame->mi_rows) &&
1456
23.7k
      (cm->mi_params.mi_cols == cm->prev_frame->mi_cols)) {
1457
8.28k
    cm->last_frame_seg_map = cm->prev_frame->seg_map;
1458
15.5k
  } else {
1459
15.5k
    cm->last_frame_seg_map = NULL;
1460
15.5k
  }
1461
  // Read update flags
1462
23.7k
  if (cm->features.primary_ref_frame == PRIMARY_REF_NONE) {
1463
    // These frames can't use previous frames, so must signal map + features
1464
13.1k
    seg->update_map = 1;
1465
13.1k
    seg->temporal_update = 0;
1466
13.1k
    seg->update_data = 1;
1467
13.1k
  } else {
1468
10.6k
    seg->update_map = aom_rb_read_bit(rb);
1469
10.6k
    if (seg->update_map) {
1470
5.53k
      seg->temporal_update = aom_rb_read_bit(rb);
1471
5.53k
    } else {
1472
5.12k
      seg->temporal_update = 0;
1473
5.12k
    }
1474
10.6k
    seg->update_data = aom_rb_read_bit(rb);
1475
10.6k
  }
1476
1477
  // Segmentation data update
1478
23.7k
  if (seg->update_data) {
1479
17.8k
    av1_clearall_segfeatures(seg);
1480
1481
157k
    for (int i = 0; i < MAX_SEGMENTS; i++) {
1482
1.24M
      for (int j = 0; j < SEG_LVL_MAX; j++) {
1483
1.10M
        int data = 0;
1484
1.10M
        const int feature_enabled = aom_rb_read_bit(rb);
1485
1.10M
        if (feature_enabled) {
1486
309k
          av1_enable_segfeature(seg, i, j);
1487
1488
309k
          const int data_max = av1_seg_feature_data_max(j);
1489
309k
          const int data_min = -data_max;
1490
309k
          const int ubits = get_unsigned_bits(data_max);
1491
1492
309k
          if (av1_is_segfeature_signed(j)) {
1493
177k
            data = aom_rb_read_inv_signed_literal(rb, ubits);
1494
177k
          } else {
1495
131k
            data = aom_rb_read_literal(rb, ubits);
1496
131k
          }
1497
1498
309k
          data = clamp(data, data_min, data_max);
1499
309k
        }
1500
1.10M
        av1_set_segdata(seg, i, j, data);
1501
1.10M
      }
1502
139k
    }
1503
17.8k
    av1_calculate_segdata(seg);
1504
17.8k
  } else if (cm->prev_frame) {
1505
5.92k
    segfeatures_copy(seg, &cm->prev_frame->seg);
1506
5.92k
  }
1507
23.7k
  segfeatures_copy(&cm->cur_frame->seg, seg);
1508
23.7k
}
1509
1510
static AOM_INLINE void decode_restoration_mode(AV1_COMMON *cm,
1511
129k
                                               struct aom_read_bit_buffer *rb) {
1512
129k
  assert(!cm->features.all_lossless);
1513
129k
  const int num_planes = av1_num_planes(cm);
1514
129k
  if (cm->features.allow_intrabc) return;
1515
116k
  int all_none = 1, chroma_none = 1;
1516
460k
  for (int p = 0; p < num_planes; ++p) {
1517
344k
    RestorationInfo *rsi = &cm->rst_info[p];
1518
344k
    if (aom_rb_read_bit(rb)) {
1519
46.0k
      rsi->frame_restoration_type =
1520
46.0k
          aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
1521
298k
    } else {
1522
298k
      rsi->frame_restoration_type =
1523
298k
          aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
1524
298k
    }
1525
344k
    if (rsi->frame_restoration_type != RESTORE_NONE) {
1526
57.2k
      all_none = 0;
1527
57.2k
      chroma_none &= p == 0;
1528
57.2k
    }
1529
344k
  }
1530
116k
  if (!all_none) {
1531
27.9k
    assert(cm->seq_params->sb_size == BLOCK_64X64 ||
1532
27.9k
           cm->seq_params->sb_size == BLOCK_128X128);
1533
27.9k
    const int sb_size = cm->seq_params->sb_size == BLOCK_128X128 ? 128 : 64;
1534
1535
106k
    for (int p = 0; p < num_planes; ++p)
1536
79.0k
      cm->rst_info[p].restoration_unit_size = sb_size;
1537
1538
27.9k
    RestorationInfo *rsi = &cm->rst_info[0];
1539
1540
27.9k
    if (sb_size == 64) {
1541
6.83k
      rsi->restoration_unit_size <<= aom_rb_read_bit(rb);
1542
6.83k
    }
1543
27.9k
    if (rsi->restoration_unit_size > 64) {
1544
24.3k
      rsi->restoration_unit_size <<= aom_rb_read_bit(rb);
1545
24.3k
    }
1546
88.9k
  } else {
1547
88.9k
    const int size = RESTORATION_UNITSIZE_MAX;
1548
353k
    for (int p = 0; p < num_planes; ++p)
1549
265k
      cm->rst_info[p].restoration_unit_size = size;
1550
88.9k
  }
1551
1552
116k
  if (num_planes > 1) {
1553
113k
    int s =
1554
113k
        AOMMIN(cm->seq_params->subsampling_x, cm->seq_params->subsampling_y);
1555
113k
    if (s && !chroma_none) {
1556
18.9k
      cm->rst_info[1].restoration_unit_size =
1557
18.9k
          cm->rst_info[0].restoration_unit_size >> (aom_rb_read_bit(rb) * s);
1558
94.7k
    } else {
1559
94.7k
      cm->rst_info[1].restoration_unit_size =
1560
94.7k
          cm->rst_info[0].restoration_unit_size;
1561
94.7k
    }
1562
113k
    cm->rst_info[2].restoration_unit_size =
1563
113k
        cm->rst_info[1].restoration_unit_size;
1564
113k
  }
1565
116k
}
1566
1567
static AOM_INLINE void read_wiener_filter(int wiener_win,
1568
                                          WienerInfo *wiener_info,
1569
                                          WienerInfo *ref_wiener_info,
1570
55.3k
                                          aom_reader *rb) {
1571
55.3k
  memset(wiener_info->vfilter, 0, sizeof(wiener_info->vfilter));
1572
55.3k
  memset(wiener_info->hfilter, 0, sizeof(wiener_info->hfilter));
1573
1574
55.3k
  if (wiener_win == WIENER_WIN)
1575
27.8k
    wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
1576
27.8k
        aom_read_primitive_refsubexpfin(
1577
27.8k
            rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1578
27.8k
            WIENER_FILT_TAP0_SUBEXP_K,
1579
27.8k
            ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV, ACCT_STR) +
1580
27.8k
        WIENER_FILT_TAP0_MINV;
1581
27.4k
  else
1582
27.4k
    wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] = 0;
1583
55.3k
  wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
1584
55.3k
      aom_read_primitive_refsubexpfin(
1585
55.3k
          rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1586
55.3k
          WIENER_FILT_TAP1_SUBEXP_K,
1587
55.3k
          ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV, ACCT_STR) +
1588
55.3k
      WIENER_FILT_TAP1_MINV;
1589
55.3k
  wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
1590
55.3k
      aom_read_primitive_refsubexpfin(
1591
55.3k
          rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1592
55.3k
          WIENER_FILT_TAP2_SUBEXP_K,
1593
55.3k
          ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV, ACCT_STR) +
1594
55.3k
      WIENER_FILT_TAP2_MINV;
1595
  // The central element has an implicit +WIENER_FILT_STEP
1596
55.3k
  wiener_info->vfilter[WIENER_HALFWIN] =
1597
55.3k
      -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
1598
55.3k
            wiener_info->vfilter[2]);
1599
1600
55.3k
  if (wiener_win == WIENER_WIN)
1601
27.8k
    wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
1602
27.8k
        aom_read_primitive_refsubexpfin(
1603
27.8k
            rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1604
27.8k
            WIENER_FILT_TAP0_SUBEXP_K,
1605
27.8k
            ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV, ACCT_STR) +
1606
27.8k
        WIENER_FILT_TAP0_MINV;
1607
27.4k
  else
1608
27.4k
    wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] = 0;
1609
55.3k
  wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
1610
55.3k
      aom_read_primitive_refsubexpfin(
1611
55.3k
          rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1612
55.3k
          WIENER_FILT_TAP1_SUBEXP_K,
1613
55.3k
          ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV, ACCT_STR) +
1614
55.3k
      WIENER_FILT_TAP1_MINV;
1615
55.3k
  wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
1616
55.3k
      aom_read_primitive_refsubexpfin(
1617
55.3k
          rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1618
55.3k
          WIENER_FILT_TAP2_SUBEXP_K,
1619
55.3k
          ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV, ACCT_STR) +
1620
55.3k
      WIENER_FILT_TAP2_MINV;
1621
  // The central element has an implicit +WIENER_FILT_STEP
1622
55.3k
  wiener_info->hfilter[WIENER_HALFWIN] =
1623
55.3k
      -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
1624
55.3k
            wiener_info->hfilter[2]);
1625
55.3k
  memcpy(ref_wiener_info, wiener_info, sizeof(*wiener_info));
1626
55.3k
}
1627
1628
static AOM_INLINE void read_sgrproj_filter(SgrprojInfo *sgrproj_info,
1629
                                           SgrprojInfo *ref_sgrproj_info,
1630
97.8k
                                           aom_reader *rb) {
1631
97.8k
  sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
1632
97.8k
  const sgr_params_type *params = &av1_sgr_params[sgrproj_info->ep];
1633
1634
97.8k
  if (params->r[0] == 0) {
1635
17.4k
    sgrproj_info->xqd[0] = 0;
1636
17.4k
    sgrproj_info->xqd[1] =
1637
17.4k
        aom_read_primitive_refsubexpfin(
1638
17.4k
            rb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
1639
17.4k
            ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1, ACCT_STR) +
1640
17.4k
        SGRPROJ_PRJ_MIN1;
1641
80.4k
  } else if (params->r[1] == 0) {
1642
15.7k
    sgrproj_info->xqd[0] =
1643
15.7k
        aom_read_primitive_refsubexpfin(
1644
15.7k
            rb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
1645
15.7k
            ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0, ACCT_STR) +
1646
15.7k
        SGRPROJ_PRJ_MIN0;
1647
15.7k
    sgrproj_info->xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - sgrproj_info->xqd[0],
1648
15.7k
                                 SGRPROJ_PRJ_MIN1, SGRPROJ_PRJ_MAX1);
1649
64.6k
  } else {
1650
64.6k
    sgrproj_info->xqd[0] =
1651
64.6k
        aom_read_primitive_refsubexpfin(
1652
64.6k
            rb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
1653
64.6k
            ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0, ACCT_STR) +
1654
64.6k
        SGRPROJ_PRJ_MIN0;
1655
64.6k
    sgrproj_info->xqd[1] =
1656
64.6k
        aom_read_primitive_refsubexpfin(
1657
64.6k
            rb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
1658
64.6k
            ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1, ACCT_STR) +
1659
64.6k
        SGRPROJ_PRJ_MIN1;
1660
64.6k
  }
1661
1662
97.8k
  memcpy(ref_sgrproj_info, sgrproj_info, sizeof(*sgrproj_info));
1663
97.8k
}
1664
1665
static AOM_INLINE void loop_restoration_read_sb_coeffs(
1666
    const AV1_COMMON *const cm, MACROBLOCKD *xd, aom_reader *const r, int plane,
1667
267k
    int runit_idx) {
1668
267k
  const RestorationInfo *rsi = &cm->rst_info[plane];
1669
267k
  RestorationUnitInfo *rui = &rsi->unit_info[runit_idx];
1670
267k
  assert(rsi->frame_restoration_type != RESTORE_NONE);
1671
1672
267k
  assert(!cm->features.all_lossless);
1673
1674
267k
  const int wiener_win = (plane > 0) ? WIENER_WIN_CHROMA : WIENER_WIN;
1675
267k
  WienerInfo *wiener_info = xd->wiener_info + plane;
1676
267k
  SgrprojInfo *sgrproj_info = xd->sgrproj_info + plane;
1677
1678
267k
  if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
1679
84.7k
    rui->restoration_type =
1680
84.7k
        aom_read_symbol(r, xd->tile_ctx->switchable_restore_cdf,
1681
84.7k
                        RESTORE_SWITCHABLE_TYPES, ACCT_STR);
1682
84.7k
    switch (rui->restoration_type) {
1683
29.2k
      case RESTORE_WIENER:
1684
29.2k
        read_wiener_filter(wiener_win, &rui->wiener_info, wiener_info, r);
1685
29.2k
        break;
1686
25.3k
      case RESTORE_SGRPROJ:
1687
25.3k
        read_sgrproj_filter(&rui->sgrproj_info, sgrproj_info, r);
1688
25.3k
        break;
1689
30.0k
      default: assert(rui->restoration_type == RESTORE_NONE); break;
1690
84.7k
    }
1691
182k
  } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
1692
45.7k
    if (aom_read_symbol(r, xd->tile_ctx->wiener_restore_cdf, 2, ACCT_STR)) {
1693
26.0k
      rui->restoration_type = RESTORE_WIENER;
1694
26.0k
      read_wiener_filter(wiener_win, &rui->wiener_info, wiener_info, r);
1695
26.0k
    } else {
1696
19.6k
      rui->restoration_type = RESTORE_NONE;
1697
19.6k
    }
1698
136k
  } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
1699
136k
    if (aom_read_symbol(r, xd->tile_ctx->sgrproj_restore_cdf, 2, ACCT_STR)) {
1700
72.5k
      rui->restoration_type = RESTORE_SGRPROJ;
1701
72.5k
      read_sgrproj_filter(&rui->sgrproj_info, sgrproj_info, r);
1702
72.5k
    } else {
1703
64.1k
      rui->restoration_type = RESTORE_NONE;
1704
64.1k
    }
1705
136k
  }
1706
267k
}
1707
1708
static AOM_INLINE void setup_loopfilter(AV1_COMMON *cm,
1709
206k
                                        struct aom_read_bit_buffer *rb) {
1710
206k
  const int num_planes = av1_num_planes(cm);
1711
206k
  struct loopfilter *lf = &cm->lf;
1712
1713
206k
  if (cm->features.allow_intrabc || cm->features.coded_lossless) {
1714
    // write default deltas to frame buffer
1715
57.8k
    av1_set_default_ref_deltas(cm->cur_frame->ref_deltas);
1716
57.8k
    av1_set_default_mode_deltas(cm->cur_frame->mode_deltas);
1717
57.8k
    return;
1718
57.8k
  }
1719
148k
  assert(!cm->features.coded_lossless);
1720
148k
  if (cm->prev_frame) {
1721
    // write deltas to frame buffer
1722
85.0k
    memcpy(lf->ref_deltas, cm->prev_frame->ref_deltas, REF_FRAMES);
1723
85.0k
    memcpy(lf->mode_deltas, cm->prev_frame->mode_deltas, MAX_MODE_LF_DELTAS);
1724
85.0k
  } else {
1725
63.6k
    av1_set_default_ref_deltas(lf->ref_deltas);
1726
63.6k
    av1_set_default_mode_deltas(lf->mode_deltas);
1727
63.6k
  }
1728
148k
  lf->filter_level[0] = aom_rb_read_literal(rb, 6);
1729
148k
  lf->filter_level[1] = aom_rb_read_literal(rb, 6);
1730
148k
  if (num_planes > 1) {
1731
138k
    if (lf->filter_level[0] || lf->filter_level[1]) {
1732
48.5k
      lf->filter_level_u = aom_rb_read_literal(rb, 6);
1733
48.5k
      lf->filter_level_v = aom_rb_read_literal(rb, 6);
1734
48.5k
    }
1735
138k
  }
1736
148k
  lf->sharpness_level = aom_rb_read_literal(rb, 3);
1737
1738
  // Read in loop filter deltas applied at the MB level based on mode or ref
1739
  // frame.
1740
148k
  lf->mode_ref_delta_update = 0;
1741
1742
148k
  lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
1743
148k
  if (lf->mode_ref_delta_enabled) {
1744
53.1k
    lf->mode_ref_delta_update = aom_rb_read_bit(rb);
1745
53.1k
    if (lf->mode_ref_delta_update) {
1746
164k
      for (int i = 0; i < REF_FRAMES; i++)
1747
146k
        if (aom_rb_read_bit(rb))
1748
40.7k
          lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
1749
1750
54.6k
      for (int i = 0; i < MAX_MODE_LF_DELTAS; i++)
1751
36.4k
        if (aom_rb_read_bit(rb))
1752
5.80k
          lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
1753
18.2k
    }
1754
53.1k
  }
1755
1756
  // write deltas to frame buffer
1757
148k
  memcpy(cm->cur_frame->ref_deltas, lf->ref_deltas, REF_FRAMES);
1758
148k
  memcpy(cm->cur_frame->mode_deltas, lf->mode_deltas, MAX_MODE_LF_DELTAS);
1759
148k
}
1760
1761
static AOM_INLINE void setup_cdef(AV1_COMMON *cm,
1762
119k
                                  struct aom_read_bit_buffer *rb) {
1763
119k
  const int num_planes = av1_num_planes(cm);
1764
119k
  CdefInfo *const cdef_info = &cm->cdef_info;
1765
1766
119k
  if (cm->features.allow_intrabc) return;
1767
107k
  cdef_info->cdef_damping = aom_rb_read_literal(rb, 2) + 3;
1768
107k
  cdef_info->cdef_bits = aom_rb_read_literal(rb, 2);
1769
107k
  cdef_info->nb_cdef_strengths = 1 << cdef_info->cdef_bits;
1770
263k
  for (int i = 0; i < cdef_info->nb_cdef_strengths; i++) {
1771
156k
    cdef_info->cdef_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
1772
156k
    cdef_info->cdef_uv_strengths[i] =
1773
156k
        num_planes > 1 ? aom_rb_read_literal(rb, CDEF_STRENGTH_BITS) : 0;
1774
156k
  }
1775
107k
}
1776
1777
590k
static INLINE int read_delta_q(struct aom_read_bit_buffer *rb) {
1778
590k
  return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
1779
590k
}
1780
1781
static AOM_INLINE void setup_quantization(CommonQuantParams *quant_params,
1782
                                          int num_planes,
1783
                                          bool separate_uv_delta_q,
1784
208k
                                          struct aom_read_bit_buffer *rb) {
1785
208k
  quant_params->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
1786
208k
  quant_params->y_dc_delta_q = read_delta_q(rb);
1787
208k
  if (num_planes > 1) {
1788
188k
    int diff_uv_delta = 0;
1789
188k
    if (separate_uv_delta_q) diff_uv_delta = aom_rb_read_bit(rb);
1790
188k
    quant_params->u_dc_delta_q = read_delta_q(rb);
1791
188k
    quant_params->u_ac_delta_q = read_delta_q(rb);
1792
188k
    if (diff_uv_delta) {
1793
2.08k
      quant_params->v_dc_delta_q = read_delta_q(rb);
1794
2.08k
      quant_params->v_ac_delta_q = read_delta_q(rb);
1795
186k
    } else {
1796
186k
      quant_params->v_dc_delta_q = quant_params->u_dc_delta_q;
1797
186k
      quant_params->v_ac_delta_q = quant_params->u_ac_delta_q;
1798
186k
    }
1799
188k
  } else {
1800
19.9k
    quant_params->u_dc_delta_q = 0;
1801
19.9k
    quant_params->u_ac_delta_q = 0;
1802
19.9k
    quant_params->v_dc_delta_q = 0;
1803
19.9k
    quant_params->v_ac_delta_q = 0;
1804
19.9k
  }
1805
208k
  quant_params->using_qmatrix = aom_rb_read_bit(rb);
1806
208k
  if (quant_params->using_qmatrix) {
1807
35.7k
    quant_params->qmatrix_level_y = aom_rb_read_literal(rb, QM_LEVEL_BITS);
1808
35.7k
    quant_params->qmatrix_level_u = aom_rb_read_literal(rb, QM_LEVEL_BITS);
1809
35.7k
    if (!separate_uv_delta_q)
1810
33.6k
      quant_params->qmatrix_level_v = quant_params->qmatrix_level_u;
1811
2.11k
    else
1812
2.11k
      quant_params->qmatrix_level_v = aom_rb_read_literal(rb, QM_LEVEL_BITS);
1813
173k
  } else {
1814
173k
    quant_params->qmatrix_level_y = 0;
1815
173k
    quant_params->qmatrix_level_u = 0;
1816
173k
    quant_params->qmatrix_level_v = 0;
1817
173k
  }
1818
208k
}
1819
1820
// Build y/uv dequant values based on segmentation.
1821
static AOM_INLINE void setup_segmentation_dequant(AV1_COMMON *const cm,
1822
206k
                                                  MACROBLOCKD *const xd) {
1823
206k
  const int bit_depth = cm->seq_params->bit_depth;
1824
  // When segmentation is disabled, only the first value is used.  The
1825
  // remaining are don't cares.
1826
206k
  const int max_segments = cm->seg.enabled ? MAX_SEGMENTS : 1;
1827
206k
  CommonQuantParams *const quant_params = &cm->quant_params;
1828
567k
  for (int i = 0; i < max_segments; ++i) {
1829
360k
    const int qindex = xd->qindex[i];
1830
360k
    quant_params->y_dequant_QTX[i][0] =
1831
360k
        av1_dc_quant_QTX(qindex, quant_params->y_dc_delta_q, bit_depth);
1832
360k
    quant_params->y_dequant_QTX[i][1] = av1_ac_quant_QTX(qindex, 0, bit_depth);
1833
360k
    quant_params->u_dequant_QTX[i][0] =
1834
360k
        av1_dc_quant_QTX(qindex, quant_params->u_dc_delta_q, bit_depth);
1835
360k
    quant_params->u_dequant_QTX[i][1] =
1836
360k
        av1_ac_quant_QTX(qindex, quant_params->u_ac_delta_q, bit_depth);
1837
360k
    quant_params->v_dequant_QTX[i][0] =
1838
360k
        av1_dc_quant_QTX(qindex, quant_params->v_dc_delta_q, bit_depth);
1839
360k
    quant_params->v_dequant_QTX[i][1] =
1840
360k
        av1_ac_quant_QTX(qindex, quant_params->v_ac_delta_q, bit_depth);
1841
360k
    const int use_qmatrix = av1_use_qmatrix(quant_params, xd, i);
1842
    // NB: depends on base index so there is only 1 set per frame
1843
    // No quant weighting when lossless or signalled not using QM
1844
360k
    const int qmlevel_y =
1845
360k
        use_qmatrix ? quant_params->qmatrix_level_y : NUM_QM_LEVELS - 1;
1846
7.21M
    for (int j = 0; j < TX_SIZES_ALL; ++j) {
1847
6.85M
      quant_params->y_iqmatrix[i][j] =
1848
6.85M
          av1_iqmatrix(quant_params, qmlevel_y, AOM_PLANE_Y, j);
1849
6.85M
    }
1850
360k
    const int qmlevel_u =
1851
360k
        use_qmatrix ? quant_params->qmatrix_level_u : NUM_QM_LEVELS - 1;
1852
7.21M
    for (int j = 0; j < TX_SIZES_ALL; ++j) {
1853
6.85M
      quant_params->u_iqmatrix[i][j] =
1854
6.85M
          av1_iqmatrix(quant_params, qmlevel_u, AOM_PLANE_U, j);
1855
6.85M
    }
1856
360k
    const int qmlevel_v =
1857
360k
        use_qmatrix ? quant_params->qmatrix_level_v : NUM_QM_LEVELS - 1;
1858
7.21M
    for (int j = 0; j < TX_SIZES_ALL; ++j) {
1859
6.85M
      quant_params->v_iqmatrix[i][j] =
1860
6.85M
          av1_iqmatrix(quant_params, qmlevel_v, AOM_PLANE_V, j);
1861
6.85M
    }
1862
360k
  }
1863
206k
}
1864
1865
95.6k
static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
1866
95.6k
  return aom_rb_read_bit(rb) ? SWITCHABLE
1867
95.6k
                             : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
1868
95.6k
}
1869
1870
static AOM_INLINE void setup_render_size(AV1_COMMON *cm,
1871
137k
                                         struct aom_read_bit_buffer *rb) {
1872
137k
  cm->render_width = cm->superres_upscaled_width;
1873
137k
  cm->render_height = cm->superres_upscaled_height;
1874
137k
  if (aom_rb_read_bit(rb))
1875
31.1k
    av1_read_frame_size(rb, 16, 16, &cm->render_width, &cm->render_height);
1876
137k
}
1877
1878
// TODO(afergs): make "struct aom_read_bit_buffer *const rb"?
1879
static AOM_INLINE void setup_superres(AV1_COMMON *const cm,
1880
                                      struct aom_read_bit_buffer *rb,
1881
219k
                                      int *width, int *height) {
1882
219k
  cm->superres_upscaled_width = *width;
1883
219k
  cm->superres_upscaled_height = *height;
1884
1885
219k
  const SequenceHeader *const seq_params = cm->seq_params;
1886
219k
  if (!seq_params->enable_superres) return;
1887
1888
62.4k
  if (aom_rb_read_bit(rb)) {
1889
22.0k
    cm->superres_scale_denominator =
1890
22.0k
        (uint8_t)aom_rb_read_literal(rb, SUPERRES_SCALE_BITS);
1891
22.0k
    cm->superres_scale_denominator += SUPERRES_SCALE_DENOMINATOR_MIN;
1892
    // Don't edit cm->width or cm->height directly, or the buffers won't get
1893
    // resized correctly
1894
22.0k
    av1_calculate_scaled_superres_size(width, height,
1895
22.0k
                                       cm->superres_scale_denominator);
1896
40.3k
  } else {
1897
    // 1:1 scaling - ie. no scaling, scale not provided
1898
40.3k
    cm->superres_scale_denominator = SCALE_NUMERATOR;
1899
40.3k
  }
1900
62.4k
}
1901
1902
static AOM_INLINE void resize_context_buffers(AV1_COMMON *cm, int width,
1903
219k
                                              int height) {
1904
219k
#if CONFIG_SIZE_LIMIT
1905
219k
  if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
1906
276
    aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
1907
276
                       "Dimensions of %dx%d beyond allowed size of %dx%d.",
1908
276
                       width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
1909
219k
#endif
1910
219k
  if (cm->width != width || cm->height != height) {
1911
66.9k
    const int new_mi_rows = CEIL_POWER_OF_TWO(height, MI_SIZE_LOG2);
1912
66.9k
    const int new_mi_cols = CEIL_POWER_OF_TWO(width, MI_SIZE_LOG2);
1913
1914
    // Allocations in av1_alloc_context_buffers() depend on individual
1915
    // dimensions as well as the overall size.
1916
66.9k
    if (new_mi_cols > cm->mi_params.mi_cols ||
1917
66.9k
        new_mi_rows > cm->mi_params.mi_rows) {
1918
42.4k
      if (av1_alloc_context_buffers(cm, width, height, BLOCK_4X4)) {
1919
        // The cm->mi_* values have been cleared and any existing context
1920
        // buffers have been freed. Clear cm->width and cm->height to be
1921
        // consistent and to force a realloc next time.
1922
313
        cm->width = 0;
1923
313
        cm->height = 0;
1924
313
        aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
1925
313
                           "Failed to allocate context buffers");
1926
313
      }
1927
42.4k
    } else {
1928
24.5k
      cm->mi_params.set_mb_mi(&cm->mi_params, width, height, BLOCK_4X4);
1929
24.5k
    }
1930
66.9k
    av1_init_mi_buffers(&cm->mi_params);
1931
66.9k
    cm->width = width;
1932
66.9k
    cm->height = height;
1933
66.9k
  }
1934
1935
219k
  ensure_mv_buffer(cm->cur_frame, cm);
1936
219k
  cm->cur_frame->width = cm->width;
1937
219k
  cm->cur_frame->height = cm->height;
1938
219k
}
1939
1940
214k
static AOM_INLINE void setup_buffer_pool(AV1_COMMON *cm) {
1941
214k
  BufferPool *const pool = cm->buffer_pool;
1942
214k
  const SequenceHeader *const seq_params = cm->seq_params;
1943
1944
214k
  lock_buffer_pool(pool);
1945
214k
  if (aom_realloc_frame_buffer(
1946
214k
          &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
1947
214k
          seq_params->subsampling_y, seq_params->use_highbitdepth,
1948
214k
          AOM_DEC_BORDER_IN_PIXELS, cm->features.byte_alignment,
1949
214k
          &cm->cur_frame->raw_frame_buffer, pool->get_fb_cb, pool->cb_priv,
1950
214k
          false, 0)) {
1951
87
    unlock_buffer_pool(pool);
1952
87
    aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
1953
87
                       "Failed to allocate frame buffer");
1954
87
  }
1955
214k
  unlock_buffer_pool(pool);
1956
1957
214k
  cm->cur_frame->buf.bit_depth = (unsigned int)seq_params->bit_depth;
1958
214k
  cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
1959
214k
  cm->cur_frame->buf.transfer_characteristics =
1960
214k
      seq_params->transfer_characteristics;
1961
214k
  cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
1962
214k
  cm->cur_frame->buf.monochrome = seq_params->monochrome;
1963
214k
  cm->cur_frame->buf.chroma_sample_position =
1964
214k
      seq_params->chroma_sample_position;
1965
214k
  cm->cur_frame->buf.color_range = seq_params->color_range;
1966
214k
  cm->cur_frame->buf.render_width = cm->render_width;
1967
214k
  cm->cur_frame->buf.render_height = cm->render_height;
1968
214k
}
1969
1970
static AOM_INLINE void setup_frame_size(AV1_COMMON *cm,
1971
                                        int frame_size_override_flag,
1972
135k
                                        struct aom_read_bit_buffer *rb) {
1973
135k
  const SequenceHeader *const seq_params = cm->seq_params;
1974
135k
  int width, height;
1975
1976
135k
  if (frame_size_override_flag) {
1977
48.2k
    int num_bits_width = seq_params->num_bits_width;
1978
48.2k
    int num_bits_height = seq_params->num_bits_height;
1979
48.2k
    av1_read_frame_size(rb, num_bits_width, num_bits_height, &width, &height);
1980
48.2k
    if (width > seq_params->max_frame_width ||
1981
48.2k
        height > seq_params->max_frame_height) {
1982
2.76k
      aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
1983
2.76k
                         "Frame dimensions are larger than the maximum values");
1984
2.76k
    }
1985
87.3k
  } else {
1986
87.3k
    width = seq_params->max_frame_width;
1987
87.3k
    height = seq_params->max_frame_height;
1988
87.3k
  }
1989
1990
135k
  setup_superres(cm, rb, &width, &height);
1991
135k
  resize_context_buffers(cm, width, height);
1992
135k
  setup_render_size(cm, rb);
1993
135k
  setup_buffer_pool(cm);
1994
135k
}
1995
1996
static AOM_INLINE void setup_sb_size(SequenceHeader *seq_params,
1997
72.4k
                                     struct aom_read_bit_buffer *rb) {
1998
72.4k
  set_sb_size(seq_params, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
1999
72.4k
}
2000
2001
static INLINE int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
2002
                                          int ref_xss, int ref_yss,
2003
                                          aom_bit_depth_t this_bit_depth,
2004
583k
                                          int this_xss, int this_yss) {
2005
583k
  return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2006
583k
         ref_yss == this_yss;
2007
583k
}
2008
2009
static AOM_INLINE void setup_frame_size_with_refs(
2010
86.7k
    AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
2011
86.7k
  int width, height;
2012
86.7k
  int found = 0;
2013
86.7k
  int has_valid_ref_frame = 0;
2014
324k
  for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
2015
319k
    if (aom_rb_read_bit(rb)) {
2016
81.5k
      const RefCntBuffer *const ref_buf = get_ref_frame_buf(cm, i);
2017
      // This will never be NULL in a normal stream, as streams are required to
2018
      // have a shown keyframe before any inter frames, which would refresh all
2019
      // the reference buffers. However, it might be null if we're starting in
2020
      // the middle of a stream, and static analysis will error if we don't do
2021
      // a null check here.
2022
81.5k
      if (ref_buf == NULL) {
2023
0
        aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
2024
0
                           "Invalid condition: invalid reference buffer");
2025
81.5k
      } else {
2026
81.5k
        const YV12_BUFFER_CONFIG *const buf = &ref_buf->buf;
2027
81.5k
        width = buf->y_crop_width;
2028
81.5k
        height = buf->y_crop_height;
2029
81.5k
        cm->render_width = buf->render_width;
2030
81.5k
        cm->render_height = buf->render_height;
2031
81.5k
        setup_superres(cm, rb, &width, &height);
2032
81.5k
        resize_context_buffers(cm, width, height);
2033
81.5k
        found = 1;
2034
81.5k
        break;
2035
81.5k
      }
2036
81.5k
    }
2037
319k
  }
2038
2039
86.7k
  const SequenceHeader *const seq_params = cm->seq_params;
2040
86.7k
  if (!found) {
2041
5.23k
    int num_bits_width = seq_params->num_bits_width;
2042
5.23k
    int num_bits_height = seq_params->num_bits_height;
2043
2044
5.23k
    av1_read_frame_size(rb, num_bits_width, num_bits_height, &width, &height);
2045
5.23k
    setup_superres(cm, rb, &width, &height);
2046
5.23k
    resize_context_buffers(cm, width, height);
2047
5.23k
    setup_render_size(cm, rb);
2048
5.23k
  }
2049
2050
86.7k
  if (width <= 0 || height <= 0)
2051
0
    aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
2052
0
                       "Invalid frame size");
2053
2054
  // Check to make sure at least one of frames that this frame references
2055
  // has valid dimensions.
2056
693k
  for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
2057
607k
    const RefCntBuffer *const ref_frame = get_ref_frame_buf(cm, i);
2058
607k
    has_valid_ref_frame |=
2059
607k
        valid_ref_frame_size(ref_frame->buf.y_crop_width,
2060
607k
                             ref_frame->buf.y_crop_height, width, height);
2061
607k
  }
2062
86.7k
  if (!has_valid_ref_frame)
2063
2.17k
    aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
2064
2.17k
                       "Referenced frame has invalid size");
2065
670k
  for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
2066
583k
    const RefCntBuffer *const ref_frame = get_ref_frame_buf(cm, i);
2067
583k
    if (!valid_ref_frame_img_fmt(
2068
583k
            ref_frame->buf.bit_depth, ref_frame->buf.subsampling_x,
2069
583k
            ref_frame->buf.subsampling_y, seq_params->bit_depth,
2070
583k
            seq_params->subsampling_x, seq_params->subsampling_y))
2071
2.13k
      aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
2072
2.13k
                         "Referenced frame has incompatible color format");
2073
583k
  }
2074
86.7k
  setup_buffer_pool(cm);
2075
86.7k
}
2076
2077
// Same function as av1_read_uniform but reading from uncompresses header wb
2078
269k
static int rb_read_uniform(struct aom_read_bit_buffer *const rb, int n) {
2079
269k
  const int l = get_unsigned_bits(n);
2080
269k
  const int m = (1 << l) - n;
2081
269k
  const int v = aom_rb_read_literal(rb, l - 1);
2082
269k
  assert(l != 0);
2083
269k
  if (v < m)
2084
214k
    return v;
2085
54.7k
  else
2086
54.7k
    return (v << 1) - m + aom_rb_read_bit(rb);
2087
269k
}
2088
2089
static AOM_INLINE void read_tile_info_max_tile(
2090
210k
    AV1_COMMON *const cm, struct aom_read_bit_buffer *const rb) {
2091
210k
  const SequenceHeader *const seq_params = cm->seq_params;
2092
210k
  CommonTileParams *const tiles = &cm->tiles;
2093
210k
  int width_sb =
2094
210k
      CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, seq_params->mib_size_log2);
2095
210k
  int height_sb =
2096
210k
      CEIL_POWER_OF_TWO(cm->mi_params.mi_rows, seq_params->mib_size_log2);
2097
2098
210k
  av1_get_tile_limits(cm);
2099
210k
  tiles->uniform_spacing = aom_rb_read_bit(rb);
2100
2101
  // Read tile columns
2102
210k
  if (tiles->uniform_spacing) {
2103
100k
    tiles->log2_cols = tiles->min_log2_cols;
2104
102k
    while (tiles->log2_cols < tiles->max_log2_cols) {
2105
76.2k
      if (!aom_rb_read_bit(rb)) {
2106
73.5k
        break;
2107
73.5k
      }
2108
2.66k
      tiles->log2_cols++;
2109
2.66k
    }
2110
110k
  } else {
2111
110k
    int i;
2112
110k
    int start_sb;
2113
245k
    for (i = 0, start_sb = 0; width_sb > 0 && i < MAX_TILE_COLS; i++) {
2114
135k
      const int size_sb =
2115
135k
          1 + rb_read_uniform(rb, AOMMIN(width_sb, tiles->max_width_sb));
2116
135k
      tiles->col_start_sb[i] = start_sb;
2117
135k
      start_sb += size_sb;
2118
135k
      width_sb -= size_sb;
2119
135k
    }
2120
110k
    tiles->cols = i;
2121
110k
    tiles->col_start_sb[i] = start_sb + width_sb;
2122
110k
  }
2123
210k
  av1_calculate_tile_cols(seq_params, cm->mi_params.mi_rows,
2124
210k
                          cm->mi_params.mi_cols, tiles);
2125
2126
  // Read tile rows
2127
210k
  if (tiles->uniform_spacing) {
2128
100k
    tiles->log2_rows = tiles->min_log2_rows;
2129
106k
    while (tiles->log2_rows < tiles->max_log2_rows) {
2130
77.5k
      if (!aom_rb_read_bit(rb)) {
2131
70.5k
        break;
2132
70.5k
      }
2133
6.96k
      tiles->log2_rows++;
2134
6.96k
    }
2135
110k
  } else {
2136
110k
    int i;
2137
110k
    int start_sb;
2138
244k
    for (i = 0, start_sb = 0; height_sb > 0 && i < MAX_TILE_ROWS; i++) {
2139
134k
      const int size_sb =
2140
134k
          1 + rb_read_uniform(rb, AOMMIN(height_sb, tiles->max_height_sb));
2141
134k
      tiles->row_start_sb[i] = start_sb;
2142
134k
      start_sb += size_sb;
2143
134k
      height_sb -= size_sb;
2144
134k
    }
2145
110k
    tiles->rows = i;
2146
110k
    tiles->row_start_sb[i] = start_sb + height_sb;
2147
110k
  }
2148
210k
  av1_calculate_tile_rows(seq_params, cm->mi_params.mi_rows, tiles);
2149
210k
}
2150
2151
14.0k
void av1_set_single_tile_decoding_mode(AV1_COMMON *const cm) {
2152
14.0k
  cm->tiles.single_tile_decoding = 0;
2153
14.0k
  if (cm->tiles.large_scale) {
2154
14.0k
    struct loopfilter *lf = &cm->lf;
2155
14.0k
    RestorationInfo *const rst_info = cm->rst_info;
2156
14.0k
    const CdefInfo *const cdef_info = &cm->cdef_info;
2157
2158
    // Figure out single_tile_decoding by loopfilter_level.
2159
14.0k
    const int no_loopfilter = !(lf->filter_level[0] || lf->filter_level[1]);
2160
14.0k
    const int no_cdef = cdef_info->cdef_bits == 0 &&
2161
14.0k
                        cdef_info->cdef_strengths[0] == 0 &&
2162
14.0k
                        cdef_info->cdef_uv_strengths[0] == 0;
2163
14.0k
    const int no_restoration =
2164
14.0k
        rst_info[0].frame_restoration_type == RESTORE_NONE &&
2165
14.0k
        rst_info[1].frame_restoration_type == RESTORE_NONE &&
2166
14.0k
        rst_info[2].frame_restoration_type == RESTORE_NONE;
2167
14.0k
    assert(IMPLIES(cm->features.coded_lossless, no_loopfilter && no_cdef));
2168
14.0k
    assert(IMPLIES(cm->features.all_lossless, no_restoration));
2169
14.0k
    cm->tiles.single_tile_decoding = no_loopfilter && no_cdef && no_restoration;
2170
14.0k
  }
2171
14.0k
}
2172
2173
static AOM_INLINE void read_tile_info(AV1Decoder *const pbi,
2174
210k
                                      struct aom_read_bit_buffer *const rb) {
2175
210k
  AV1_COMMON *const cm = &pbi->common;
2176
2177
210k
  read_tile_info_max_tile(cm, rb);
2178
2179
210k
  pbi->context_update_tile_id = 0;
2180
210k
  if (cm->tiles.rows * cm->tiles.cols > 1) {
2181
    // tile to use for cdf update
2182
19.0k
    pbi->context_update_tile_id =
2183
19.0k
        aom_rb_read_literal(rb, cm->tiles.log2_rows + cm->tiles.log2_cols);
2184
19.0k
    if (pbi->context_update_tile_id >= cm->tiles.rows * cm->tiles.cols) {
2185
613
      aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
2186
613
                         "Invalid context_update_tile_id");
2187
613
    }
2188
    // tile size magnitude
2189
19.0k
    pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2190
19.0k
  }
2191
210k
}
2192
2193
#if EXT_TILE_DEBUG
2194
static AOM_INLINE void read_ext_tile_info(
2195
13.5k
    AV1Decoder *const pbi, struct aom_read_bit_buffer *const rb) {
2196
13.5k
  AV1_COMMON *const cm = &pbi->common;
2197
2198
  // This information is stored as a separate byte.
2199
13.5k
  int mod = rb->bit_offset % CHAR_BIT;
2200
13.5k
  if (mod > 0) aom_rb_read_literal(rb, CHAR_BIT - mod);
2201
13.5k
  assert(rb->bit_offset % CHAR_BIT == 0);
2202
2203
13.5k
  if (cm->tiles.cols * cm->tiles.rows > 1) {
2204
    // Read the number of bytes used to store tile size
2205
4.65k
    pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2206
4.65k
    pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2207
4.65k
  }
2208
13.5k
}
2209
#endif  // EXT_TILE_DEBUG
2210
2211
36.9k
static size_t mem_get_varsize(const uint8_t *src, int sz) {
2212
36.9k
  switch (sz) {
2213
31.4k
    case 1: return src[0];
2214
4.13k
    case 2: return mem_get_le16(src);
2215
821
    case 3: return mem_get_le24(src);
2216
510
    case 4: return mem_get_le32(src);
2217
0
    default: assert(0 && "Invalid size"); return -1;
2218
36.9k
  }
2219
36.9k
}
2220
2221
#if EXT_TILE_DEBUG
2222
// Reads the next tile returning its size and adjusting '*data' accordingly
2223
// based on 'is_last'. On return, '*data' is updated to point to the end of the
2224
// raw tile buffer in the bit stream.
2225
static AOM_INLINE void get_ls_tile_buffer(
2226
    const uint8_t *const data_end, struct aom_internal_error_info *error_info,
2227
    const uint8_t **data, TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
2228
19.4k
    int tile_size_bytes, int col, int row, int tile_copy_mode) {
2229
19.4k
  size_t size;
2230
2231
19.4k
  size_t copy_size = 0;
2232
19.4k
  const uint8_t *copy_data = NULL;
2233
2234
19.4k
  if (!read_is_valid(*data, tile_size_bytes, data_end))
2235
234
    aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
2236
234
                       "Truncated packet or corrupt tile length");
2237
19.4k
  size = mem_get_varsize(*data, tile_size_bytes);
2238
2239
  // If tile_copy_mode = 1, then the top bit of the tile header indicates copy
2240
  // mode.
2241
19.4k
  if (tile_copy_mode && (size >> (tile_size_bytes * 8 - 1)) == 1) {
2242
    // The remaining bits in the top byte signal the row offset
2243
11.8k
    int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
2244
11.8k
    if (offset > row) {
2245
83
      aom_internal_error(
2246
83
          error_info, AOM_CODEC_CORRUPT_FRAME,
2247
83
          "Invalid row offset in tile copy mode: row=%d offset=%d", row,
2248
83
          offset);
2249
83
    }
2250
2251
    // Currently, only use tiles in same column as reference tiles.
2252
11.8k
    copy_data = tile_buffers[row - offset][col].data;
2253
11.8k
    copy_size = tile_buffers[row - offset][col].size;
2254
11.8k
    size = 0;
2255
11.8k
  } else {
2256
7.62k
    size += AV1_MIN_TILE_SIZE_BYTES;
2257
7.62k
  }
2258
2259
19.4k
  *data += tile_size_bytes;
2260
2261
19.4k
  if (size > (size_t)(data_end - *data))
2262
877
    aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
2263
877
                       "Truncated packet or corrupt tile size");
2264
2265
19.4k
  if (size > 0) {
2266
6.51k
    tile_buffers[row][col].data = *data;
2267
6.51k
    tile_buffers[row][col].size = size;
2268
12.9k
  } else {
2269
12.9k
    tile_buffers[row][col].data = copy_data;
2270
12.9k
    tile_buffers[row][col].size = copy_size;
2271
12.9k
  }
2272
2273
19.4k
  *data += size;
2274
19.4k
}
2275
2276
// Returns the end of the last tile buffer
2277
// (tile_buffers[cm->tiles.rows - 1][cm->tiles.cols - 1]).
2278
static const uint8_t *get_ls_tile_buffers(
2279
    AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
2280
13.3k
    TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
2281
13.3k
  AV1_COMMON *const cm = &pbi->common;
2282
13.3k
  const int tile_cols = cm->tiles.cols;
2283
13.3k
  const int tile_rows = cm->tiles.rows;
2284
13.3k
  const int have_tiles = tile_cols * tile_rows > 1;
2285
13.3k
  const uint8_t *raw_data_end;  // The end of the last tile buffer
2286
2287
13.3k
  if (!have_tiles) {
2288
8.91k
    const size_t tile_size = data_end - data;
2289
8.91k
    tile_buffers[0][0].data = data;
2290
8.91k
    tile_buffers[0][0].size = tile_size;
2291
8.91k
    raw_data_end = NULL;
2292
8.91k
  } else {
2293
    // We locate only the tile buffers that are required, which are the ones
2294
    // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
2295
    // need the last (bottom right) tile buffer, as we need to know where the
2296
    // end of the compressed frame buffer is for proper superframe decoding.
2297
2298
4.43k
    const uint8_t *tile_col_data_end[MAX_TILE_COLS] = { NULL };
2299
4.43k
    const uint8_t *const data_start = data;
2300
2301
4.43k
    const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
2302
4.43k
    const int single_row = pbi->dec_tile_row >= 0;
2303
4.43k
    const int tile_rows_start = single_row ? dec_tile_row : 0;
2304
4.43k
    const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
2305
4.43k
    const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
2306
4.43k
    const int single_col = pbi->dec_tile_col >= 0;
2307
4.43k
    const int tile_cols_start = single_col ? dec_tile_col : 0;
2308
4.43k
    const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
2309
2310
4.43k
    const int tile_col_size_bytes = pbi->tile_col_size_bytes;
2311
4.43k
    const int tile_size_bytes = pbi->tile_size_bytes;
2312
4.43k
    int tile_width, tile_height;
2313
4.43k
    if (!av1_get_uniform_tile_size(cm, &tile_width, &tile_height)) {
2314
924
      aom_internal_error(
2315
924
          &pbi->error, AOM_CODEC_CORRUPT_FRAME,
2316
924
          "Not all the tiles in the tile list have the same size.");
2317
924
    }
2318
4.43k
    const int tile_copy_mode =
2319
4.43k
        ((AOMMAX(tile_width, tile_height) << MI_SIZE_LOG2) <= 256) ? 1 : 0;
2320
    // Read tile column sizes for all columns (we need the last tile buffer)
2321
8.53k
    for (int c = 0; c < tile_cols; ++c) {
2322
4.10k
      const int is_last = c == tile_cols - 1;
2323
4.10k
      size_t tile_col_size;
2324
2325
4.10k
      if (!is_last) {
2326
1.25k
        if (tile_col_size_bytes > data_end - data) {
2327
24
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
2328
24
                             "Not enough data to read tile_col_size");
2329
24
        }
2330
1.25k
        tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
2331
1.25k
        data += tile_col_size_bytes;
2332
1.25k
        if (tile_col_size > (size_t)(data_end - data)) {
2333
638
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
2334
638
                             "tile_col_data_end[%d] is out of bound", c);
2335
638
        }
2336
1.25k
        tile_col_data_end[c] = data + tile_col_size;
2337
2.84k
      } else {
2338
2.84k
        tile_col_size = data_end - data;
2339
2.84k
        tile_col_data_end[c] = data_end;
2340
2.84k
      }
2341
4.10k
      data += tile_col_size;
2342
4.10k
    }
2343
2344
4.43k
    data = data_start;
2345
2346
    // Read the required tile sizes.
2347
7.28k
    for (int c = tile_cols_start; c < tile_cols_end; ++c) {
2348
2.85k
      const int is_last = c == tile_cols - 1;
2349
2350
2.85k
      if (c > 0) data = tile_col_data_end[c - 1];
2351
2352
2.85k
      if (!is_last) data += tile_col_size_bytes;
2353
2354
      // Get the whole of the last column, otherwise stop at the required tile.
2355
22.2k
      for (int r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
2356
19.4k
        get_ls_tile_buffer(tile_col_data_end[c], &pbi->error, &data,
2357
19.4k
                           tile_buffers, tile_size_bytes, c, r, tile_copy_mode);
2358
19.4k
      }
2359
2.85k
    }
2360
2361
    // If we have not read the last column, then read it to get the last tile.
2362
4.43k
    if (tile_cols_end != tile_cols) {
2363
0
      const int c = tile_cols - 1;
2364
2365
0
      data = tile_col_data_end[c - 1];
2366
2367
0
      for (int r = 0; r < tile_rows; ++r) {
2368
0
        get_ls_tile_buffer(tile_col_data_end[c], &pbi->error, &data,
2369
0
                           tile_buffers, tile_size_bytes, c, r, tile_copy_mode);
2370
0
      }
2371
0
    }
2372
4.43k
    raw_data_end = data;
2373
4.43k
  }
2374
13.3k
  return raw_data_end;
2375
13.3k
}
2376
#endif  // EXT_TILE_DEBUG
2377
2378
static const uint8_t *get_ls_single_tile_buffer(
2379
    AV1Decoder *pbi, const uint8_t *data,
2380
0
    TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
2381
0
  assert(pbi->dec_tile_row >= 0 && pbi->dec_tile_col >= 0);
2382
0
  tile_buffers[pbi->dec_tile_row][pbi->dec_tile_col].data = data;
2383
0
  tile_buffers[pbi->dec_tile_row][pbi->dec_tile_col].size =
2384
0
      (size_t)pbi->coded_tile_data_size;
2385
0
  return data + pbi->coded_tile_data_size;
2386
0
}
2387
2388
// Reads the next tile returning its size and adjusting '*data' accordingly
2389
// based on 'is_last'.
2390
static AOM_INLINE void get_tile_buffer(
2391
    const uint8_t *const data_end, const int tile_size_bytes, int is_last,
2392
    struct aom_internal_error_info *error_info, const uint8_t **data,
2393
191k
    TileBufferDec *const buf) {
2394
191k
  size_t size;
2395
2396
191k
  if (!is_last) {
2397
16.7k
    if (!read_is_valid(*data, tile_size_bytes, data_end))
2398
282
      aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
2399
282
                         "Not enough data to read tile size");
2400
2401
16.7k
    size = mem_get_varsize(*data, tile_size_bytes) + AV1_MIN_TILE_SIZE_BYTES;
2402
16.7k
    *data += tile_size_bytes;
2403
2404
16.7k
    if (size > (size_t)(data_end - *data))
2405
1.25k
      aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
2406
1.25k
                         "Truncated packet or corrupt tile size");
2407
174k
  } else {
2408
174k
    size = data_end - *data;
2409
174k
  }
2410
2411
191k
  buf->data = *data;
2412
191k
  buf->size = size;
2413
2414
191k
  *data += size;
2415
191k
}
2416
2417
static AOM_INLINE void get_tile_buffers(
2418
    AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
2419
    TileBufferDec (*const tile_buffers)[MAX_TILE_COLS], int start_tile,
2420
176k
    int end_tile) {
2421
176k
  AV1_COMMON *const cm = &pbi->common;
2422
176k
  const int tile_cols = cm->tiles.cols;
2423
176k
  const int tile_rows = cm->tiles.rows;
2424
176k
  int tc = 0;
2425
2426
357k
  for (int r = 0; r < tile_rows; ++r) {
2427
373k
    for (int c = 0; c < tile_cols; ++c, ++tc) {
2428
192k
      TileBufferDec *const buf = &tile_buffers[r][c];
2429
2430
192k
      const int is_last = (tc == end_tile);
2431
192k
      const size_t hdr_offset = 0;
2432
2433
192k
      if (tc < start_tile || tc > end_tile) continue;
2434
2435
191k
      if (data + hdr_offset >= data_end)
2436
211
        aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
2437
211
                           "Data ended before all tiles were read.");
2438
191k
      data += hdr_offset;
2439
191k
      get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &pbi->error,
2440
191k
                      &data, buf);
2441
191k
    }
2442
180k
  }
2443
176k
}
2444
2445
static AOM_INLINE void set_cb_buffer(AV1Decoder *pbi, DecoderCodingBlock *dcb,
2446
                                     CB_BUFFER *cb_buffer_base,
2447
                                     const int num_planes, int mi_row,
2448
2.25M
                                     int mi_col) {
2449
2.25M
  AV1_COMMON *const cm = &pbi->common;
2450
2.25M
  int mib_size_log2 = cm->seq_params->mib_size_log2;
2451
2.25M
  int stride = (cm->mi_params.mi_cols >> mib_size_log2) + 1;
2452
2.25M
  int offset = (mi_row >> mib_size_log2) * stride + (mi_col >> mib_size_log2);
2453
2.25M
  CB_BUFFER *cb_buffer = cb_buffer_base + offset;
2454
2455
8.86M
  for (int plane = 0; plane < num_planes; ++plane) {
2456
6.60M
    dcb->dqcoeff_block[plane] = cb_buffer->dqcoeff[plane];
2457
6.60M
    dcb->eob_data[plane] = cb_buffer->eob_data[plane];
2458
6.60M
    dcb->cb_offset[plane] = 0;
2459
6.60M
    dcb->txb_offset[plane] = 0;
2460
6.60M
  }
2461
2.25M
  MACROBLOCKD *const xd = &dcb->xd;
2462
2.25M
  xd->plane[0].color_index_map = cb_buffer->color_index_map[0];
2463
2.25M
  xd->plane[1].color_index_map = cb_buffer->color_index_map[1];
2464
2.25M
  xd->color_index_map_offset[0] = 0;
2465
2.25M
  xd->color_index_map_offset[1] = 0;
2466
2.25M
}
2467
2468
static AOM_INLINE void decoder_alloc_tile_data(AV1Decoder *pbi,
2469
16.6k
                                               const int n_tiles) {
2470
16.6k
  AV1_COMMON *const cm = &pbi->common;
2471
16.6k
  aom_free(pbi->tile_data);
2472
16.6k
  pbi->allocated_tiles = 0;
2473
16.6k
  CHECK_MEM_ERROR(cm, pbi->tile_data,
2474
16.6k
                  aom_memalign(32, n_tiles * sizeof(*pbi->tile_data)));
2475
16.6k
  pbi->allocated_tiles = n_tiles;
2476
56.6k
  for (int i = 0; i < n_tiles; i++) {
2477
40.0k
    TileDataDec *const tile_data = pbi->tile_data + i;
2478
40.0k
    av1_zero(tile_data->dec_row_mt_sync);
2479
40.0k
  }
2480
16.6k
  pbi->allocated_row_mt_sync_rows = 0;
2481
16.6k
}
2482
2483
// Set up nsync by width.
2484
31.2k
static INLINE int get_sync_range(int width) {
2485
// nsync numbers are picked by testing.
2486
#if 0
2487
  if (width < 640)
2488
    return 1;
2489
  else if (width <= 1280)
2490
    return 2;
2491
  else if (width <= 4096)
2492
    return 4;
2493
  else
2494
    return 8;
2495
#else
2496
31.2k
  (void)width;
2497
31.2k
#endif
2498
31.2k
  return 1;
2499
31.2k
}
2500
2501
// Allocate memory for decoder row synchronization
2502
static AOM_INLINE void dec_row_mt_alloc(AV1DecRowMTSync *dec_row_mt_sync,
2503
31.2k
                                        AV1_COMMON *cm, int rows) {
2504
31.2k
  dec_row_mt_sync->allocated_sb_rows = rows;
2505
31.2k
#if CONFIG_MULTITHREAD
2506
31.2k
  {
2507
31.2k
    int i;
2508
2509
31.2k
    CHECK_MEM_ERROR(cm, dec_row_mt_sync->mutex_,
2510
31.2k
                    aom_malloc(sizeof(*(dec_row_mt_sync->mutex_)) * rows));
2511
31.2k
    if (dec_row_mt_sync->mutex_) {
2512
95.4k
      for (i = 0; i < rows; ++i) {
2513
64.1k
        pthread_mutex_init(&dec_row_mt_sync->mutex_[i], NULL);
2514
64.1k
      }
2515
31.2k
    }
2516
2517
31.2k
    CHECK_MEM_ERROR(cm, dec_row_mt_sync->cond_,
2518
31.2k
                    aom_malloc(sizeof(*(dec_row_mt_sync->cond_)) * rows));
2519
31.2k
    if (dec_row_mt_sync->cond_) {
2520
95.4k
      for (i = 0; i < rows; ++i) {
2521
64.1k
        pthread_cond_init(&dec_row_mt_sync->cond_[i], NULL);
2522
64.1k
      }
2523
31.2k
    }
2524
31.2k
  }
2525
31.2k
#endif  // CONFIG_MULTITHREAD
2526
2527
31.2k
  CHECK_MEM_ERROR(cm, dec_row_mt_sync->cur_sb_col,
2528
31.2k
                  aom_malloc(sizeof(*(dec_row_mt_sync->cur_sb_col)) * rows));
2529
2530
  // Set up nsync.
2531
31.2k
  dec_row_mt_sync->sync_range = get_sync_range(cm->width);
2532
31.2k
}
2533
2534
// Deallocate decoder row synchronization related mutex and data
2535
66.5k
void av1_dec_row_mt_dealloc(AV1DecRowMTSync *dec_row_mt_sync) {
2536
66.5k
  if (dec_row_mt_sync != NULL) {
2537
66.5k
#if CONFIG_MULTITHREAD
2538
66.5k
    int i;
2539
66.5k
    if (dec_row_mt_sync->mutex_ != NULL) {
2540
95.4k
      for (i = 0; i < dec_row_mt_sync->allocated_sb_rows; ++i) {
2541
64.1k
        pthread_mutex_destroy(&dec_row_mt_sync->mutex_[i]);
2542
64.1k
      }
2543
31.2k
      aom_free(dec_row_mt_sync->mutex_);
2544
31.2k
    }
2545
66.5k
    if (dec_row_mt_sync->cond_ != NULL) {
2546
95.4k
      for (i = 0; i < dec_row_mt_sync->allocated_sb_rows; ++i) {
2547
64.1k
        pthread_cond_destroy(&dec_row_mt_sync->cond_[i]);
2548
64.1k
      }
2549
31.2k
      aom_free(dec_row_mt_sync->cond_);
2550
31.2k
    }
2551
66.5k
#endif  // CONFIG_MULTITHREAD
2552
66.5k
    aom_free(dec_row_mt_sync->cur_sb_col);
2553
2554
    // clear the structure as the source of this call may be a resize in which
2555
    // case this call will be followed by an _alloc() which may fail.
2556
66.5k
    av1_zero(*dec_row_mt_sync);
2557
66.5k
  }
2558
66.5k
}
2559
2560
static INLINE void sync_read(AV1DecRowMTSync *const dec_row_mt_sync, int r,
2561
808k
                             int c) {
2562
808k
#if CONFIG_MULTITHREAD
2563
808k
  const int nsync = dec_row_mt_sync->sync_range;
2564
2565
808k
  if (r && !(c & (nsync - 1))) {
2566
491k
    pthread_mutex_t *const mutex = &dec_row_mt_sync->mutex_[r - 1];
2567
491k
    pthread_mutex_lock(mutex);
2568
2569
637k
    while (c > dec_row_mt_sync->cur_sb_col[r - 1] - nsync -
2570
637k
                   dec_row_mt_sync->intrabc_extra_top_right_sb_delay) {
2571
145k
      pthread_cond_wait(&dec_row_mt_sync->cond_[r - 1], mutex);
2572
145k
    }
2573
491k
    pthread_mutex_unlock(mutex);
2574
491k
  }
2575
#else
2576
  (void)dec_row_mt_sync;
2577
  (void)r;
2578
  (void)c;
2579
#endif  // CONFIG_MULTITHREAD
2580
808k
}
2581
2582
static INLINE void sync_write(AV1DecRowMTSync *const dec_row_mt_sync, int r,
2583
813k
                              int c, const int sb_cols) {
2584
813k
#if CONFIG_MULTITHREAD
2585
813k
  const int nsync = dec_row_mt_sync->sync_range;
2586
813k
  int cur;
2587
813k
  int sig = 1;
2588
2589
813k
  if (c < sb_cols - 1) {
2590
580k
    cur = c;
2591
580k
    if (c % nsync) sig = 0;
2592
580k
  } else {
2593
233k
    cur = sb_cols + nsync + dec_row_mt_sync->intrabc_extra_top_right_sb_delay;
2594
233k
  }
2595
2596
813k
  if (sig) {
2597
813k
    pthread_mutex_lock(&dec_row_mt_sync->mutex_[r]);
2598
2599
813k
    dec_row_mt_sync->cur_sb_col[r] = cur;
2600
2601
813k
    pthread_cond_signal(&dec_row_mt_sync->cond_[r]);
2602
813k
    pthread_mutex_unlock(&dec_row_mt_sync->mutex_[r]);
2603
813k
  }
2604
#else
2605
  (void)dec_row_mt_sync;
2606
  (void)r;
2607
  (void)c;
2608
  (void)sb_cols;
2609
#endif  // CONFIG_MULTITHREAD
2610
813k
}
2611
2612
static INLINE void signal_decoding_done_for_erroneous_row(
2613
934
    AV1Decoder *const pbi, const MACROBLOCKD *const xd) {
2614
934
  AV1_COMMON *const cm = &pbi->common;
2615
934
  const TileInfo *const tile = &xd->tile;
2616
934
  const int sb_row_in_tile =
2617
934
      ((xd->mi_row - tile->mi_row_start) >> cm->seq_params->mib_size_log2);
2618
934
  const int sb_cols_in_tile = av1_get_sb_cols_in_tile(cm, tile);
2619
934
  TileDataDec *const tile_data =
2620
934
      pbi->tile_data + tile->tile_row * cm->tiles.cols + tile->tile_col;
2621
934
  AV1DecRowMTSync *dec_row_mt_sync = &tile_data->dec_row_mt_sync;
2622
2623
934
  sync_write(dec_row_mt_sync, sb_row_in_tile, sb_cols_in_tile - 1,
2624
934
             sb_cols_in_tile);
2625
934
}
2626
2627
static AOM_INLINE void decode_tile_sb_row(AV1Decoder *pbi, ThreadData *const td,
2628
                                          const TileInfo *tile_info,
2629
228k
                                          const int mi_row) {
2630
228k
  AV1_COMMON *const cm = &pbi->common;
2631
228k
  const int num_planes = av1_num_planes(cm);
2632
228k
  TileDataDec *const tile_data = pbi->tile_data +
2633
228k
                                 tile_info->tile_row * cm->tiles.cols +
2634
228k
                                 tile_info->tile_col;
2635
228k
  const int sb_cols_in_tile = av1_get_sb_cols_in_tile(cm, tile_info);
2636
228k
  const int sb_row_in_tile =
2637
228k
      (mi_row - tile_info->mi_row_start) >> cm->seq_params->mib_size_log2;
2638
228k
  int sb_col_in_tile = 0;
2639
228k
  int row_mt_exit = 0;
2640
2641
1.03M
  for (int mi_col = tile_info->mi_col_start; mi_col < tile_info->mi_col_end;
2642
808k
       mi_col += cm->seq_params->mib_size, sb_col_in_tile++) {
2643
808k
    set_cb_buffer(pbi, &td->dcb, pbi->cb_buffer_base, num_planes, mi_row,
2644
808k
                  mi_col);
2645
2646
808k
    sync_read(&tile_data->dec_row_mt_sync, sb_row_in_tile, sb_col_in_tile);
2647
2648
808k
#if CONFIG_MULTITHREAD
2649
808k
    pthread_mutex_lock(pbi->row_mt_mutex_);
2650
808k
#endif
2651
808k
    row_mt_exit = pbi->frame_row_mt_info.row_mt_exit;
2652
808k
#if CONFIG_MULTITHREAD
2653
808k
    pthread_mutex_unlock(pbi->row_mt_mutex_);
2654
808k
#endif
2655
2656
809k
    if (!row_mt_exit) {
2657
      // Decoding of the super-block
2658
809k
      decode_partition(pbi, td, mi_row, mi_col, td->bit_reader,
2659
809k
                       cm->seq_params->sb_size, 0x2);
2660
809k
    }
2661
2662
808k
    sync_write(&tile_data->dec_row_mt_sync, sb_row_in_tile, sb_col_in_tile,
2663
808k
               sb_cols_in_tile);
2664
808k
  }
2665
228k
}
2666
2667
149k
static int check_trailing_bits_after_symbol_coder(aom_reader *r) {
2668
149k
  if (aom_reader_has_overflowed(r)) return -1;
2669
2670
149k
  uint32_t nb_bits = aom_reader_tell(r);
2671
149k
  uint32_t nb_bytes = (nb_bits + 7) >> 3;
2672
149k
  const uint8_t *p = aom_reader_find_begin(r) + nb_bytes;
2673
2674
  // aom_reader_tell() returns 1 for a newly initialized decoder, and the
2675
  // return value only increases as values are decoded. So nb_bits > 0, and
2676
  // thus p > p_begin. Therefore accessing p[-1] is safe.
2677
149k
  uint8_t last_byte = p[-1];
2678
149k
  uint8_t pattern = 128 >> ((nb_bits - 1) & 7);
2679
149k
  if ((last_byte & (2 * pattern - 1)) != pattern) return -1;
2680
2681
  // Make sure that all padding bytes are zero as required by the spec.
2682
140k
  const uint8_t *p_end = aom_reader_find_end(r);
2683
198k
  while (p < p_end) {
2684
58.7k
    if (*p != 0) return -1;
2685
57.3k
    p++;
2686
57.3k
  }
2687
139k
  return 0;
2688
140k
}
2689
2690
static AOM_INLINE void set_decode_func_pointers(ThreadData *td,
2691
499k
                                                int parse_decode_flag) {
2692
499k
  td->read_coeffs_tx_intra_block_visit = decode_block_void;
2693
499k
  td->predict_and_recon_intra_block_visit = decode_block_void;
2694
499k
  td->read_coeffs_tx_inter_block_visit = decode_block_void;
2695
499k
  td->inverse_tx_inter_block_visit = decode_block_void;
2696
499k
  td->predict_inter_block_visit = predict_inter_block_void;
2697
499k
  td->cfl_store_inter_block_visit = cfl_store_inter_block_void;
2698
2699
499k
  if (parse_decode_flag & 0x1) {
2700
292k
    td->read_coeffs_tx_intra_block_visit = read_coeffs_tx_intra_block;
2701
292k
    td->read_coeffs_tx_inter_block_visit = av1_read_coeffs_txb_facade;
2702
292k
  }
2703
499k
  if (parse_decode_flag & 0x2) {
2704
269k
    td->predict_and_recon_intra_block_visit =
2705
269k
        predict_and_reconstruct_intra_block;
2706
269k
    td->inverse_tx_inter_block_visit = inverse_transform_inter_block;
2707
269k
    td->predict_inter_block_visit = predict_inter_block;
2708
269k
    td->cfl_store_inter_block_visit = cfl_store_inter_block;
2709
269k
  }
2710
499k
}
2711
2712
static AOM_INLINE void decode_tile(AV1Decoder *pbi, ThreadData *const td,
2713
62.0k
                                   int tile_row, int tile_col) {
2714
62.0k
  TileInfo tile_info;
2715
2716
62.0k
  AV1_COMMON *const cm = &pbi->common;
2717
62.0k
  const int num_planes = av1_num_planes(cm);
2718
2719
62.0k
  av1_tile_set_row(&tile_info, cm, tile_row);
2720
62.0k
  av1_tile_set_col(&tile_info, cm, tile_col);
2721
62.0k
  DecoderCodingBlock *const dcb = &td->dcb;
2722
62.0k
  MACROBLOCKD *const xd = &dcb->xd;
2723
2724
62.0k
  av1_zero_above_context(cm, xd, tile_info.mi_col_start, tile_info.mi_col_end,
2725
62.0k
                         tile_row);
2726
62.0k
  av1_reset_loop_filter_delta(xd, num_planes);
2727
62.0k
  av1_reset_loop_restoration(xd, num_planes);
2728
2729
153k
  for (int mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
2730
125k
       mi_row += cm->seq_params->mib_size) {
2731
125k
    av1_zero_left_context(xd);
2732
2733
682k
    for (int mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
2734
590k
         mi_col += cm->seq_params->mib_size) {
2735
590k
      set_cb_buffer(pbi, dcb, &td->cb_buffer_base, num_planes, 0, 0);
2736
2737
      // Bit-stream parsing and decoding of the superblock
2738
590k
      decode_partition(pbi, td, mi_row, mi_col, td->bit_reader,
2739
590k
                       cm->seq_params->sb_size, 0x3);
2740
2741
590k
      if (aom_reader_has_overflowed(td->bit_reader)) {
2742
33.8k
        aom_merge_corrupted_flag(&dcb->corrupted, 1);
2743
33.8k
        return;
2744
33.8k
      }
2745
590k
    }
2746
125k
  }
2747
2748
28.2k
  int corrupted =
2749
28.2k
      (check_trailing_bits_after_symbol_coder(td->bit_reader)) ? 1 : 0;
2750
28.2k
  aom_merge_corrupted_flag(&dcb->corrupted, corrupted);
2751
28.2k
}
2752
2753
static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
2754
                                   const uint8_t *data_end, int start_tile,
2755
65.5k
                                   int end_tile) {
2756
65.5k
  AV1_COMMON *const cm = &pbi->common;
2757
65.5k
  ThreadData *const td = &pbi->td;
2758
65.5k
  CommonTileParams *const tiles = &cm->tiles;
2759
65.5k
  const int tile_cols = tiles->cols;
2760
65.5k
  const int tile_rows = tiles->rows;
2761
65.5k
  const int n_tiles = tile_cols * tile_rows;
2762
65.5k
  TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
2763
65.5k
  const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
2764
65.5k
  const int single_row = pbi->dec_tile_row >= 0;
2765
65.5k
  const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
2766
65.5k
  const int single_col = pbi->dec_tile_col >= 0;
2767
65.5k
  int tile_rows_start;
2768
65.5k
  int tile_rows_end;
2769
65.5k
  int tile_cols_start;
2770
65.5k
  int tile_cols_end;
2771
65.5k
  int inv_col_order;
2772
65.5k
  int inv_row_order;
2773
65.5k
  int tile_row, tile_col;
2774
65.5k
  uint8_t allow_update_cdf;
2775
65.5k
  const uint8_t *raw_data_end = NULL;
2776
2777
65.5k
  if (tiles->large_scale) {
2778
9.73k
    tile_rows_start = single_row ? dec_tile_row : 0;
2779
9.73k
    tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
2780
9.73k
    tile_cols_start = single_col ? dec_tile_col : 0;
2781
9.73k
    tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
2782
9.73k
    inv_col_order = pbi->inv_tile_order && !single_col;
2783
9.73k
    inv_row_order = pbi->inv_tile_order && !single_row;
2784
9.73k
    allow_update_cdf = 0;
2785
55.8k
  } else {
2786
55.8k
    tile_rows_start = 0;
2787
55.8k
    tile_rows_end = tile_rows;
2788
55.8k
    tile_cols_start = 0;
2789
55.8k
    tile_cols_end = tile_cols;
2790
55.8k
    inv_col_order = pbi->inv_tile_order;
2791
55.8k
    inv_row_order = pbi->inv_tile_order;
2792
55.8k
    allow_update_cdf = 1;
2793
55.8k
  }
2794
2795
  // No tiles to decode.
2796
65.5k
  if (tile_rows_end <= tile_rows_start || tile_cols_end <= tile_cols_start ||
2797
      // First tile is larger than end_tile.
2798
65.5k
      tile_rows_start * tiles->cols + tile_cols_start > end_tile ||
2799
      // Last tile is smaller than start_tile.
2800
65.5k
      (tile_rows_end - 1) * tiles->cols + tile_cols_end - 1 < start_tile)
2801
0
    return data;
2802
2803
65.5k
  allow_update_cdf = allow_update_cdf && !cm->features.disable_cdf_update;
2804
2805
65.5k
  assert(tile_rows <= MAX_TILE_ROWS);
2806
65.5k
  assert(tile_cols <= MAX_TILE_COLS);
2807
2808
65.5k
#if EXT_TILE_DEBUG
2809
65.5k
  if (tiles->large_scale && !pbi->ext_tile_debug)
2810
0
    raw_data_end = get_ls_single_tile_buffer(pbi, data, tile_buffers);
2811
65.5k
  else if (tiles->large_scale && pbi->ext_tile_debug)
2812
9.73k
    raw_data_end = get_ls_tile_buffers(pbi, data, data_end, tile_buffers);
2813
55.8k
  else
2814
55.8k
#endif  // EXT_TILE_DEBUG
2815
55.8k
    get_tile_buffers(pbi, data, data_end, tile_buffers, start_tile, end_tile);
2816
2817
65.5k
  if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
2818
11.7k
    decoder_alloc_tile_data(pbi, n_tiles);
2819
11.7k
  }
2820
65.5k
  if (pbi->dcb.xd.seg_mask == NULL)
2821
65.5k
    CHECK_MEM_ERROR(cm, pbi->dcb.xd.seg_mask,
2822
65.5k
                    (uint8_t *)aom_memalign(
2823
65.5k
                        16, 2 * MAX_SB_SQUARE * sizeof(*pbi->dcb.xd.seg_mask)));
2824
#if CONFIG_ACCOUNTING
2825
  if (pbi->acct_enabled) {
2826
    aom_accounting_reset(&pbi->accounting);
2827
  }
2828
#endif
2829
2830
65.5k
  set_decode_func_pointers(&pbi->td, 0x3);
2831
2832
  // Load all tile information into thread_data.
2833
65.5k
  td->dcb = pbi->dcb;
2834
2835
65.5k
  td->dcb.corrupted = 0;
2836
65.5k
  td->dcb.mc_buf[0] = td->mc_buf[0];
2837
65.5k
  td->dcb.mc_buf[1] = td->mc_buf[1];
2838
65.5k
  td->dcb.xd.tmp_conv_dst = td->tmp_conv_dst;
2839
189k
  for (int j = 0; j < 2; ++j) {
2840
123k
    td->dcb.xd.tmp_obmc_bufs[j] = td->tmp_obmc_bufs[j];
2841
123k
  }
2842
2843
127k
  for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
2844
61.9k
    const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
2845
2846
124k
    for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
2847
62.2k
      const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
2848
62.2k
      TileDataDec *const tile_data = pbi->tile_data + row * tiles->cols + col;
2849
62.2k
      const TileBufferDec *const tile_bs_buf = &tile_buffers[row][col];
2850
2851
62.2k
      if (row * tiles->cols + col < start_tile ||
2852
62.2k
          row * tiles->cols + col > end_tile)
2853
90
        continue;
2854
2855
62.1k
      td->bit_reader = &tile_data->bit_reader;
2856
62.1k
      av1_zero(td->cb_buffer_base.dqcoeff);
2857
62.1k
      av1_tile_init(&td->dcb.xd.tile, cm, row, col);
2858
62.1k
      td->dcb.xd.current_base_qindex = cm->quant_params.base_qindex;
2859
62.1k
      setup_bool_decoder(&td->dcb.xd, tile_bs_buf->data, data_end,
2860
62.1k
                         tile_bs_buf->size, &pbi->error, td->bit_reader,
2861
62.1k
                         allow_update_cdf);
2862
#if CONFIG_ACCOUNTING
2863
      if (pbi->acct_enabled) {
2864
        td->bit_reader->accounting = &pbi->accounting;
2865
        td->bit_reader->accounting->last_tell_frac =
2866
            aom_reader_tell_frac(td->bit_reader);
2867
      } else {
2868
        td->bit_reader->accounting = NULL;
2869
      }
2870
#endif
2871
62.1k
      av1_init_macroblockd(cm, &td->dcb.xd);
2872
62.1k
      av1_init_above_context(&cm->above_contexts, av1_num_planes(cm), row,
2873
62.1k
                             &td->dcb.xd);
2874
2875
      // Initialise the tile context from the frame context
2876
62.1k
      tile_data->tctx = *cm->fc;
2877
62.1k
      td->dcb.xd.tile_ctx = &tile_data->tctx;
2878
2879
      // decode tile
2880
62.1k
      decode_tile(pbi, td, row, col);
2881
62.1k
      aom_merge_corrupted_flag(&pbi->dcb.corrupted, td->dcb.corrupted);
2882
62.1k
      if (pbi->dcb.corrupted)
2883
35.8k
        aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
2884
35.8k
                           "Failed to decode tile data");
2885
62.1k
    }
2886
61.9k
  }
2887
2888
65.5k
  if (tiles->large_scale) {
2889
3.23k
    if (n_tiles == 1) {
2890
      // Find the end of the single tile buffer
2891
3.23k
      return aom_reader_find_end(&pbi->tile_data->bit_reader);
2892
3.23k
    }
2893
    // Return the end of the last tile buffer
2894
0
    return raw_data_end;
2895
3.23k
  }
2896
62.3k
  TileDataDec *const tile_data = pbi->tile_data + end_tile;
2897
2898
62.3k
  return aom_reader_find_end(&tile_data->bit_reader);
2899
65.5k
}
2900
2901
350k
static TileJobsDec *get_dec_job_info(AV1DecTileMT *tile_mt_info) {
2902
350k
  TileJobsDec *cur_job_info = NULL;
2903
350k
#if CONFIG_MULTITHREAD
2904
350k
  pthread_mutex_lock(tile_mt_info->job_mutex);
2905
2906
350k
  if (tile_mt_info->jobs_dequeued < tile_mt_info->jobs_enqueued) {
2907
143k
    cur_job_info = tile_mt_info->job_queue + tile_mt_info->jobs_dequeued;
2908
143k
    tile_mt_info->jobs_dequeued++;
2909
143k
  }
2910
2911
350k
  pthread_mutex_unlock(tile_mt_info->job_mutex);
2912
#else
2913
  (void)tile_mt_info;
2914
#endif
2915
350k
  return cur_job_info;
2916
350k
}
2917
2918
static AOM_INLINE void tile_worker_hook_init(
2919
    AV1Decoder *const pbi, DecWorkerData *const thread_data,
2920
    const TileBufferDec *const tile_buffer, TileDataDec *const tile_data,
2921
143k
    uint8_t allow_update_cdf) {
2922
143k
  AV1_COMMON *cm = &pbi->common;
2923
143k
  ThreadData *const td = thread_data->td;
2924
143k
  int tile_row = tile_data->tile_info.tile_row;
2925
143k
  int tile_col = tile_data->tile_info.tile_col;
2926
2927
143k
  td->bit_reader = &tile_data->bit_reader;
2928
143k
  av1_zero(td->cb_buffer_base.dqcoeff);
2929
2930
143k
  MACROBLOCKD *const xd = &td->dcb.xd;
2931
143k
  av1_tile_init(&xd->tile, cm, tile_row, tile_col);
2932
143k
  xd->current_base_qindex = cm->quant_params.base_qindex;
2933
2934
143k
  setup_bool_decoder(xd, tile_buffer->data, thread_data->data_end,
2935
143k
                     tile_buffer->size, &thread_data->error_info,
2936
143k
                     td->bit_reader, allow_update_cdf);
2937
#if CONFIG_ACCOUNTING
2938
  if (pbi->acct_enabled) {
2939
    td->bit_reader->accounting = &pbi->accounting;
2940
    td->bit_reader->accounting->last_tell_frac =
2941
        aom_reader_tell_frac(td->bit_reader);
2942
  } else {
2943
    td->bit_reader->accounting = NULL;
2944
  }
2945
#endif
2946
143k
  av1_init_macroblockd(cm, xd);
2947
143k
  xd->error_info = &thread_data->error_info;
2948
143k
  av1_init_above_context(&cm->above_contexts, av1_num_planes(cm), tile_row, xd);
2949
2950
  // Initialise the tile context from the frame context
2951
143k
  tile_data->tctx = *cm->fc;
2952
143k
  xd->tile_ctx = &tile_data->tctx;
2953
#if CONFIG_ACCOUNTING
2954
  if (pbi->acct_enabled) {
2955
    tile_data->bit_reader.accounting->last_tell_frac =
2956
        aom_reader_tell_frac(&tile_data->bit_reader);
2957
  }
2958
#endif
2959
143k
}
2960
2961
0
static int tile_worker_hook(void *arg1, void *arg2) {
2962
0
  DecWorkerData *const thread_data = (DecWorkerData *)arg1;
2963
0
  AV1Decoder *const pbi = (AV1Decoder *)arg2;
2964
0
  AV1_COMMON *cm = &pbi->common;
2965
0
  ThreadData *const td = thread_data->td;
2966
0
  uint8_t allow_update_cdf;
2967
2968
  // The jmp_buf is valid only for the duration of the function that calls
2969
  // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2970
  // before it returns.
2971
0
  if (setjmp(thread_data->error_info.jmp)) {
2972
0
    thread_data->error_info.setjmp = 0;
2973
0
    thread_data->td->dcb.corrupted = 1;
2974
0
    return 0;
2975
0
  }
2976
0
  thread_data->error_info.setjmp = 1;
2977
2978
0
  allow_update_cdf = cm->tiles.large_scale ? 0 : 1;
2979
0
  allow_update_cdf = allow_update_cdf && !cm->features.disable_cdf_update;
2980
2981
0
  set_decode_func_pointers(td, 0x3);
2982
2983
0
  assert(cm->tiles.cols > 0);
2984
0
  while (!td->dcb.corrupted) {
2985
0
    TileJobsDec *cur_job_info = get_dec_job_info(&pbi->tile_mt_info);
2986
2987
0
    if (cur_job_info != NULL) {
2988
0
      const TileBufferDec *const tile_buffer = cur_job_info->tile_buffer;
2989
0
      TileDataDec *const tile_data = cur_job_info->tile_data;
2990
0
      tile_worker_hook_init(pbi, thread_data, tile_buffer, tile_data,
2991
0
                            allow_update_cdf);
2992
      // decode tile
2993
0
      int tile_row = tile_data->tile_info.tile_row;
2994
0
      int tile_col = tile_data->tile_info.tile_col;
2995
0
      decode_tile(pbi, td, tile_row, tile_col);
2996
0
    } else {
2997
0
      break;
2998
0
    }
2999
0
  }
3000
0
  thread_data->error_info.setjmp = 0;
3001
0
  return !td->dcb.corrupted;
3002
0
}
3003
3004
static INLINE int get_max_row_mt_workers_per_tile(AV1_COMMON *cm,
3005
382k
                                                  const TileInfo *tile) {
3006
  // NOTE: Currently value of max workers is calculated based
3007
  // on the parse and decode time. As per the theoretical estimate
3008
  // when percentage of parse time is equal to percentage of decode
3009
  // time, number of workers needed to parse + decode a tile can not
3010
  // exceed more than 2.
3011
  // TODO(any): Modify this value if parsing is optimized in future.
3012
382k
  int sb_rows = av1_get_sb_rows_in_tile(cm, tile);
3013
382k
  int max_workers =
3014
382k
      sb_rows == 1 ? AOM_MIN_THREADS_PER_TILE : AOM_MAX_THREADS_PER_TILE;
3015
382k
  return max_workers;
3016
382k
}
3017
3018
// The caller must hold pbi->row_mt_mutex_ when calling this function.
3019
// Returns 1 if either the next job is stored in *next_job_info or 1 is stored
3020
// in *end_of_frame.
3021
// NOTE: The caller waits on pbi->row_mt_cond_ if this function returns 0.
3022
// The return value of this function depends on the following variables:
3023
// - frame_row_mt_info->mi_rows_parse_done
3024
// - frame_row_mt_info->mi_rows_decode_started
3025
// - frame_row_mt_info->row_mt_exit
3026
// Therefore we may need to signal or broadcast pbi->row_mt_cond_ if any of
3027
// these variables is modified.
3028
static int get_next_job_info(AV1Decoder *const pbi,
3029
                             AV1DecRowMTJobInfo *next_job_info,
3030
523k
                             int *end_of_frame) {
3031
523k
  AV1_COMMON *cm = &pbi->common;
3032
523k
  TileDataDec *tile_data;
3033
523k
  AV1DecRowMTSync *dec_row_mt_sync;
3034
523k
  AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
3035
523k
  const int tile_rows_start = frame_row_mt_info->tile_rows_start;
3036
523k
  const int tile_rows_end = frame_row_mt_info->tile_rows_end;
3037
523k
  const int tile_cols_start = frame_row_mt_info->tile_cols_start;
3038
523k
  const int tile_cols_end = frame_row_mt_info->tile_cols_end;
3039
523k
  const int start_tile = frame_row_mt_info->start_tile;
3040
523k
  const int end_tile = frame_row_mt_info->end_tile;
3041
523k
  const int sb_mi_size = mi_size_wide[cm->seq_params->sb_size];
3042
523k
  int num_mis_to_decode, num_threads_working;
3043
523k
  int num_mis_waiting_for_decode;
3044
523k
  int min_threads_working = INT_MAX;
3045
523k
  int max_mis_to_decode = 0;
3046
523k
  int tile_row_idx, tile_col_idx;
3047
523k
  int tile_row = -1;
3048
523k
  int tile_col = -1;
3049
3050
523k
  memset(next_job_info, 0, sizeof(*next_job_info));
3051
3052
  // Frame decode is completed or error is encountered.
3053
523k
  *end_of_frame = (frame_row_mt_info->mi_rows_decode_started ==
3054
523k
                   frame_row_mt_info->mi_rows_to_decode) ||
3055
523k
                  (frame_row_mt_info->row_mt_exit == 1);
3056
523k
  if (*end_of_frame) {
3057
207k
    return 1;
3058
207k
  }
3059
3060
  // Decoding cannot start as bit-stream parsing is not complete.
3061
316k
  assert(frame_row_mt_info->mi_rows_parse_done >=
3062
316k
         frame_row_mt_info->mi_rows_decode_started);
3063
316k
  if (frame_row_mt_info->mi_rows_parse_done ==
3064
316k
      frame_row_mt_info->mi_rows_decode_started)
3065
82.6k
    return 0;
3066
3067
  // Choose the tile to decode.
3068
474k
  for (tile_row_idx = tile_rows_start; tile_row_idx < tile_rows_end;
3069
240k
       ++tile_row_idx) {
3070
524k
    for (tile_col_idx = tile_cols_start; tile_col_idx < tile_cols_end;
3071
283k
         ++tile_col_idx) {
3072
283k
      if (tile_row_idx * cm->tiles.cols + tile_col_idx < start_tile ||
3073
283k
          tile_row_idx * cm->tiles.cols + tile_col_idx > end_tile)
3074
384
        continue;
3075
3076
283k
      tile_data = pbi->tile_data + tile_row_idx * cm->tiles.cols + tile_col_idx;
3077
283k
      dec_row_mt_sync = &tile_data->dec_row_mt_sync;
3078
3079
283k
      num_threads_working = dec_row_mt_sync->num_threads_working;
3080
283k
      num_mis_waiting_for_decode = (dec_row_mt_sync->mi_rows_parse_done -
3081
283k
                                    dec_row_mt_sync->mi_rows_decode_started) *
3082
283k
                                   dec_row_mt_sync->mi_cols;
3083
283k
      num_mis_to_decode =
3084
283k
          (dec_row_mt_sync->mi_rows - dec_row_mt_sync->mi_rows_decode_started) *
3085
283k
          dec_row_mt_sync->mi_cols;
3086
3087
283k
      assert(num_mis_to_decode >= num_mis_waiting_for_decode);
3088
3089
      // Pick the tile which has minimum number of threads working on it.
3090
283k
      if (num_mis_waiting_for_decode > 0) {
3091
239k
        if (num_threads_working < min_threads_working) {
3092
233k
          min_threads_working = num_threads_working;
3093
233k
          max_mis_to_decode = 0;
3094
233k
        }
3095
239k
        if (num_threads_working == min_threads_working &&
3096
239k
            num_mis_to_decode > max_mis_to_decode &&
3097
239k
            num_threads_working <
3098
235k
                get_max_row_mt_workers_per_tile(cm, &tile_data->tile_info)) {
3099
232k
          max_mis_to_decode = num_mis_to_decode;
3100
232k
          tile_row = tile_row_idx;
3101
232k
          tile_col = tile_col_idx;
3102
232k
        }
3103
239k
      }
3104
283k
    }
3105
240k
  }
3106
  // No job found to process
3107
233k
  if (tile_row == -1 || tile_col == -1) return 0;
3108
3109
232k
  tile_data = pbi->tile_data + tile_row * cm->tiles.cols + tile_col;
3110
232k
  dec_row_mt_sync = &tile_data->dec_row_mt_sync;
3111
3112
232k
  next_job_info->tile_row = tile_row;
3113
232k
  next_job_info->tile_col = tile_col;
3114
232k
  next_job_info->mi_row = dec_row_mt_sync->mi_rows_decode_started +
3115
232k
                          tile_data->tile_info.mi_row_start;
3116
3117
232k
  dec_row_mt_sync->num_threads_working++;
3118
232k
  dec_row_mt_sync->mi_rows_decode_started += sb_mi_size;
3119
232k
  frame_row_mt_info->mi_rows_decode_started += sb_mi_size;
3120
232k
  assert(frame_row_mt_info->mi_rows_parse_done >=
3121
232k
         frame_row_mt_info->mi_rows_decode_started);
3122
232k
#if CONFIG_MULTITHREAD
3123
232k
  if (frame_row_mt_info->mi_rows_decode_started ==
3124
232k
      frame_row_mt_info->mi_rows_to_decode) {
3125
115k
    pthread_cond_broadcast(pbi->row_mt_cond_);
3126
115k
  }
3127
232k
#endif
3128
3129
232k
  return 1;
3130
232k
}
3131
3132
static INLINE void signal_parse_sb_row_done(AV1Decoder *const pbi,
3133
                                            TileDataDec *const tile_data,
3134
242k
                                            const int sb_mi_size) {
3135
242k
  AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
3136
242k
#if CONFIG_MULTITHREAD
3137
242k
  pthread_mutex_lock(pbi->row_mt_mutex_);
3138
242k
#endif
3139
242k
  assert(frame_row_mt_info->mi_rows_parse_done >=
3140
242k
         frame_row_mt_info->mi_rows_decode_started);
3141
242k
  tile_data->dec_row_mt_sync.mi_rows_parse_done += sb_mi_size;
3142
242k
  frame_row_mt_info->mi_rows_parse_done += sb_mi_size;
3143
242k
#if CONFIG_MULTITHREAD
3144
  // A new decode job is available. Wake up one worker thread to handle the
3145
  // new decode job.
3146
  // NOTE: This assumes we bump mi_rows_parse_done and mi_rows_decode_started
3147
  // by the same increment (sb_mi_size).
3148
242k
  pthread_cond_signal(pbi->row_mt_cond_);
3149
242k
  pthread_mutex_unlock(pbi->row_mt_mutex_);
3150
242k
#endif
3151
242k
}
3152
3153
// This function is very similar to decode_tile(). It would be good to figure
3154
// out how to share code.
3155
static AOM_INLINE void parse_tile_row_mt(AV1Decoder *pbi, ThreadData *const td,
3156
143k
                                         TileDataDec *const tile_data) {
3157
143k
  AV1_COMMON *const cm = &pbi->common;
3158
143k
  const int sb_mi_size = mi_size_wide[cm->seq_params->sb_size];
3159
143k
  const int num_planes = av1_num_planes(cm);
3160
143k
  const TileInfo *const tile_info = &tile_data->tile_info;
3161
143k
  int tile_row = tile_info->tile_row;
3162
143k
  DecoderCodingBlock *const dcb = &td->dcb;
3163
143k
  MACROBLOCKD *const xd = &dcb->xd;
3164
3165
143k
  av1_zero_above_context(cm, xd, tile_info->mi_col_start, tile_info->mi_col_end,
3166
143k
                         tile_row);
3167
143k
  av1_reset_loop_filter_delta(xd, num_planes);
3168
143k
  av1_reset_loop_restoration(xd, num_planes);
3169
3170
386k
  for (int mi_row = tile_info->mi_row_start; mi_row < tile_info->mi_row_end;
3171
258k
       mi_row += cm->seq_params->mib_size) {
3172
258k
    av1_zero_left_context(xd);
3173
3174
1.09M
    for (int mi_col = tile_info->mi_col_start; mi_col < tile_info->mi_col_end;
3175
856k
         mi_col += cm->seq_params->mib_size) {
3176
856k
      set_cb_buffer(pbi, dcb, pbi->cb_buffer_base, num_planes, mi_row, mi_col);
3177
3178
      // Bit-stream parsing of the superblock
3179
856k
      decode_partition(pbi, td, mi_row, mi_col, td->bit_reader,
3180
856k
                       cm->seq_params->sb_size, 0x1);
3181
3182
856k
      if (aom_reader_has_overflowed(td->bit_reader)) {
3183
15.4k
        aom_merge_corrupted_flag(&dcb->corrupted, 1);
3184
15.4k
        return;
3185
15.4k
      }
3186
856k
    }
3187
243k
    signal_parse_sb_row_done(pbi, tile_data, sb_mi_size);
3188
243k
  }
3189
3190
128k
  int corrupted =
3191
128k
      (check_trailing_bits_after_symbol_coder(td->bit_reader)) ? 1 : 0;
3192
128k
  aom_merge_corrupted_flag(&dcb->corrupted, corrupted);
3193
128k
}
3194
3195
231k
static int row_mt_worker_hook(void *arg1, void *arg2) {
3196
231k
  DecWorkerData *const thread_data = (DecWorkerData *)arg1;
3197
231k
  AV1Decoder *const pbi = (AV1Decoder *)arg2;
3198
231k
  ThreadData *const td = thread_data->td;
3199
231k
  uint8_t allow_update_cdf;
3200
231k
  AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
3201
231k
  td->dcb.corrupted = 0;
3202
3203
  // The jmp_buf is valid only for the duration of the function that calls
3204
  // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
3205
  // before it returns.
3206
231k
  if (setjmp(thread_data->error_info.jmp)) {
3207
934
    thread_data->error_info.setjmp = 0;
3208
934
    thread_data->td->dcb.corrupted = 1;
3209
934
#if CONFIG_MULTITHREAD
3210
934
    pthread_mutex_lock(pbi->row_mt_mutex_);
3211
934
#endif
3212
934
    frame_row_mt_info->row_mt_exit = 1;
3213
934
#if CONFIG_MULTITHREAD
3214
934
    pthread_cond_broadcast(pbi->row_mt_cond_);
3215
934
    pthread_mutex_unlock(pbi->row_mt_mutex_);
3216
934
#endif
3217
    // If any SB row (erroneous row) processed by a thread encounters an
3218
    // internal error, there is a need to indicate other threads that decoding
3219
    // of the erroneous row is complete. This ensures that other threads which
3220
    // wait upon the completion of SB's present in erroneous row are not waiting
3221
    // indefinitely.
3222
934
    signal_decoding_done_for_erroneous_row(pbi, &thread_data->td->dcb.xd);
3223
934
    return 0;
3224
934
  }
3225
230k
  thread_data->error_info.setjmp = 1;
3226
3227
230k
  AV1_COMMON *cm = &pbi->common;
3228
230k
  allow_update_cdf = cm->tiles.large_scale ? 0 : 1;
3229
230k
  allow_update_cdf = allow_update_cdf && !cm->features.disable_cdf_update;
3230
3231
230k
  set_decode_func_pointers(td, 0x1);
3232
3233
230k
  assert(cm->tiles.cols > 0);
3234
374k
  while (!td->dcb.corrupted) {
3235
350k
    TileJobsDec *cur_job_info = get_dec_job_info(&pbi->tile_mt_info);
3236
3237
350k
    if (cur_job_info != NULL) {
3238
143k
      const TileBufferDec *const tile_buffer = cur_job_info->tile_buffer;
3239
143k
      TileDataDec *const tile_data = cur_job_info->tile_data;
3240
143k
      tile_worker_hook_init(pbi, thread_data, tile_buffer, tile_data,
3241
143k
                            allow_update_cdf);
3242
143k
#if CONFIG_MULTITHREAD
3243
143k
      pthread_mutex_lock(pbi->row_mt_mutex_);
3244
143k
#endif
3245
143k
      tile_data->dec_row_mt_sync.num_threads_working++;
3246
143k
#if CONFIG_MULTITHREAD
3247
143k
      pthread_mutex_unlock(pbi->row_mt_mutex_);
3248
143k
#endif
3249
      // decode tile
3250
143k
      parse_tile_row_mt(pbi, td, tile_data);
3251
143k
#if CONFIG_MULTITHREAD
3252
143k
      pthread_mutex_lock(pbi->row_mt_mutex_);
3253
143k
#endif
3254
143k
      tile_data->dec_row_mt_sync.num_threads_working--;
3255
143k
#if CONFIG_MULTITHREAD
3256
143k
      pthread_mutex_unlock(pbi->row_mt_mutex_);
3257
143k
#endif
3258
206k
    } else {
3259
206k
      break;
3260
206k
    }
3261
350k
  }
3262
3263
230k
  if (td->dcb.corrupted) {
3264
22.9k
    thread_data->error_info.setjmp = 0;
3265
22.9k
#if CONFIG_MULTITHREAD
3266
22.9k
    pthread_mutex_lock(pbi->row_mt_mutex_);
3267
22.9k
#endif
3268
22.9k
    frame_row_mt_info->row_mt_exit = 1;
3269
22.9k
#if CONFIG_MULTITHREAD
3270
22.9k
    pthread_cond_broadcast(pbi->row_mt_cond_);
3271
22.9k
    pthread_mutex_unlock(pbi->row_mt_mutex_);
3272
22.9k
#endif
3273
22.9k
    return 0;
3274
22.9k
  }
3275
3276
207k
  set_decode_func_pointers(td, 0x2);
3277
3278
440k
  while (1) {
3279
439k
    AV1DecRowMTJobInfo next_job_info;
3280
439k
    int end_of_frame = 0;
3281
3282
439k
#if CONFIG_MULTITHREAD
3283
439k
    pthread_mutex_lock(pbi->row_mt_mutex_);
3284
439k
#endif
3285
523k
    while (!get_next_job_info(pbi, &next_job_info, &end_of_frame)) {
3286
83.4k
#if CONFIG_MULTITHREAD
3287
83.4k
      pthread_cond_wait(pbi->row_mt_cond_, pbi->row_mt_mutex_);
3288
83.4k
#endif
3289
83.4k
    }
3290
439k
#if CONFIG_MULTITHREAD
3291
439k
    pthread_mutex_unlock(pbi->row_mt_mutex_);
3292
439k
#endif
3293
3294
439k
    if (end_of_frame) break;
3295
3296
232k
    int tile_row = next_job_info.tile_row;
3297
232k
    int tile_col = next_job_info.tile_col;
3298
232k
    int mi_row = next_job_info.mi_row;
3299
3300
232k
    TileDataDec *tile_data =
3301
232k
        pbi->tile_data + tile_row * cm->tiles.cols + tile_col;
3302
232k
    AV1DecRowMTSync *dec_row_mt_sync = &tile_data->dec_row_mt_sync;
3303
3304
232k
    av1_tile_init(&td->dcb.xd.tile, cm, tile_row, tile_col);
3305
232k
    av1_init_macroblockd(cm, &td->dcb.xd);
3306
232k
    td->dcb.xd.error_info = &thread_data->error_info;
3307
3308
232k
    decode_tile_sb_row(pbi, td, &tile_data->tile_info, mi_row);
3309
3310
232k
#if CONFIG_MULTITHREAD
3311
232k
    pthread_mutex_lock(pbi->row_mt_mutex_);
3312
232k
#endif
3313
232k
    dec_row_mt_sync->num_threads_working--;
3314
232k
#if CONFIG_MULTITHREAD
3315
232k
    pthread_mutex_unlock(pbi->row_mt_mutex_);
3316
232k
#endif
3317
232k
  }
3318
207k
  thread_data->error_info.setjmp = 0;
3319
207k
  return !td->dcb.corrupted;
3320
230k
}
3321
3322
// sorts in descending order
3323
70.5k
static int compare_tile_buffers(const void *a, const void *b) {
3324
70.5k
  const TileJobsDec *const buf1 = (const TileJobsDec *)a;
3325
70.5k
  const TileJobsDec *const buf2 = (const TileJobsDec *)b;
3326
70.5k
  return (((int)buf2->tile_buffer->size) - ((int)buf1->tile_buffer->size));
3327
70.5k
}
3328
3329
static AOM_INLINE void enqueue_tile_jobs(AV1Decoder *pbi, AV1_COMMON *cm,
3330
                                         int tile_rows_start, int tile_rows_end,
3331
                                         int tile_cols_start, int tile_cols_end,
3332
123k
                                         int start_tile, int end_tile) {
3333
123k
  AV1DecTileMT *tile_mt_info = &pbi->tile_mt_info;
3334
123k
  TileJobsDec *tile_job_queue = tile_mt_info->job_queue;
3335
123k
  tile_mt_info->jobs_enqueued = 0;
3336
123k
  tile_mt_info->jobs_dequeued = 0;
3337
3338
263k
  for (int row = tile_rows_start; row < tile_rows_end; row++) {
3339
286k
    for (int col = tile_cols_start; col < tile_cols_end; col++) {
3340
146k
      if (row * cm->tiles.cols + col < start_tile ||
3341
146k
          row * cm->tiles.cols + col > end_tile)
3342
121
        continue;
3343
146k
      tile_job_queue->tile_buffer = &pbi->tile_buffers[row][col];
3344
146k
      tile_job_queue->tile_data = pbi->tile_data + row * cm->tiles.cols + col;
3345
146k
      tile_job_queue++;
3346
146k
      tile_mt_info->jobs_enqueued++;
3347
146k
    }
3348
139k
  }
3349
123k
}
3350
3351
static AOM_INLINE void alloc_dec_jobs(AV1DecTileMT *tile_mt_info,
3352
                                      AV1_COMMON *cm, int tile_rows,
3353
4.89k
                                      int tile_cols) {
3354
4.89k
  tile_mt_info->alloc_tile_rows = tile_rows;
3355
4.89k
  tile_mt_info->alloc_tile_cols = tile_cols;
3356
4.89k
  int num_tiles = tile_rows * tile_cols;
3357
4.89k
#if CONFIG_MULTITHREAD
3358
4.89k
  {
3359
4.89k
    CHECK_MEM_ERROR(cm, tile_mt_info->job_mutex,
3360
4.89k
                    aom_malloc(sizeof(*tile_mt_info->job_mutex) * num_tiles));
3361
3362
29.3k
    for (int i = 0; i < num_tiles; i++) {
3363
24.4k
      pthread_mutex_init(&tile_mt_info->job_mutex[i], NULL);
3364
24.4k
    }
3365
4.89k
  }
3366
4.89k
#endif
3367
4.89k
  CHECK_MEM_ERROR(cm, tile_mt_info->job_queue,
3368
4.89k
                  aom_malloc(sizeof(*tile_mt_info->job_queue) * num_tiles));
3369
4.89k
}
3370
3371
300k
void av1_free_mc_tmp_buf(ThreadData *thread_data) {
3372
300k
  int ref;
3373
902k
  for (ref = 0; ref < 2; ref++) {
3374
601k
    if (thread_data->mc_buf_use_highbd)
3375
134k
      aom_free(CONVERT_TO_SHORTPTR(thread_data->mc_buf[ref]));
3376
467k
    else
3377
467k
      aom_free(thread_data->mc_buf[ref]);
3378
601k
    thread_data->mc_buf[ref] = NULL;
3379
601k
  }
3380
300k
  thread_data->mc_buf_size = 0;
3381
300k
  thread_data->mc_buf_use_highbd = 0;
3382
3383
300k
  aom_free(thread_data->tmp_conv_dst);
3384
300k
  thread_data->tmp_conv_dst = NULL;
3385
300k
  aom_free(thread_data->seg_mask);
3386
300k
  thread_data->seg_mask = NULL;
3387
902k
  for (int i = 0; i < 2; ++i) {
3388
601k
    aom_free(thread_data->tmp_obmc_bufs[i]);
3389
601k
    thread_data->tmp_obmc_bufs[i] = NULL;
3390
601k
  }
3391
300k
}
3392
3393
static AOM_INLINE void allocate_mc_tmp_buf(AV1_COMMON *const cm,
3394
                                           ThreadData *thread_data,
3395
155k
                                           int buf_size, int use_highbd) {
3396
465k
  for (int ref = 0; ref < 2; ref++) {
3397
    // The mc_buf/hbd_mc_buf must be zeroed to fix a intermittent valgrind error
3398
    // 'Conditional jump or move depends on uninitialised value' from the loop
3399
    // filter. Uninitialized reads in convolve function (e.g. horiz_4tap path in
3400
    // av1_convolve_2d_sr_avx2()) from mc_buf/hbd_mc_buf are seen to be the
3401
    // potential reason for this issue.
3402
310k
    if (use_highbd) {
3403
134k
      uint16_t *hbd_mc_buf;
3404
134k
      CHECK_MEM_ERROR(cm, hbd_mc_buf, (uint16_t *)aom_memalign(16, buf_size));
3405
134k
      memset(hbd_mc_buf, 0, buf_size);
3406
134k
      thread_data->mc_buf[ref] = CONVERT_TO_BYTEPTR(hbd_mc_buf);
3407
176k
    } else {
3408
176k
      CHECK_MEM_ERROR(cm, thread_data->mc_buf[ref],
3409
176k
                      (uint8_t *)aom_memalign(16, buf_size));
3410
176k
      memset(thread_data->mc_buf[ref], 0, buf_size);
3411
176k
    }
3412
310k
  }
3413
155k
  thread_data->mc_buf_size = buf_size;
3414
155k
  thread_data->mc_buf_use_highbd = use_highbd;
3415
3416
155k
  CHECK_MEM_ERROR(cm, thread_data->tmp_conv_dst,
3417
155k
                  aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE *
3418
155k
                                       sizeof(*thread_data->tmp_conv_dst)));
3419
155k
  CHECK_MEM_ERROR(cm, thread_data->seg_mask,
3420
155k
                  (uint8_t *)aom_memalign(
3421
155k
                      16, 2 * MAX_SB_SQUARE * sizeof(*thread_data->seg_mask)));
3422
3423
465k
  for (int i = 0; i < 2; ++i) {
3424
310k
    CHECK_MEM_ERROR(
3425
310k
        cm, thread_data->tmp_obmc_bufs[i],
3426
310k
        aom_memalign(16, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
3427
310k
                             sizeof(*thread_data->tmp_obmc_bufs[i])));
3428
310k
  }
3429
155k
}
3430
3431
static AOM_INLINE void reset_dec_workers(AV1Decoder *pbi,
3432
                                         AVxWorkerHook worker_hook,
3433
123k
                                         int num_workers) {
3434
123k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
3435
3436
  // Reset tile decoding hook
3437
354k
  for (int worker_idx = 0; worker_idx < num_workers; ++worker_idx) {
3438
231k
    AVxWorker *const worker = &pbi->tile_workers[worker_idx];
3439
231k
    DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
3440
231k
    thread_data->td->dcb = pbi->dcb;
3441
231k
    thread_data->td->dcb.corrupted = 0;
3442
231k
    thread_data->td->dcb.mc_buf[0] = thread_data->td->mc_buf[0];
3443
231k
    thread_data->td->dcb.mc_buf[1] = thread_data->td->mc_buf[1];
3444
231k
    thread_data->td->dcb.xd.tmp_conv_dst = thread_data->td->tmp_conv_dst;
3445
231k
    if (worker_idx)
3446
107k
      thread_data->td->dcb.xd.seg_mask = thread_data->td->seg_mask;
3447
693k
    for (int j = 0; j < 2; ++j) {
3448
462k
      thread_data->td->dcb.xd.tmp_obmc_bufs[j] =
3449
462k
          thread_data->td->tmp_obmc_bufs[j];
3450
462k
    }
3451
231k
    winterface->sync(worker);
3452
3453
231k
    worker->hook = worker_hook;
3454
231k
    worker->data1 = thread_data;
3455
231k
    worker->data2 = pbi;
3456
231k
  }
3457
#if CONFIG_ACCOUNTING
3458
  if (pbi->acct_enabled) {
3459
    aom_accounting_reset(&pbi->accounting);
3460
  }
3461
#endif
3462
123k
}
3463
3464
static AOM_INLINE void launch_dec_workers(AV1Decoder *pbi,
3465
                                          const uint8_t *data_end,
3466
123k
                                          int num_workers) {
3467
123k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
3468
3469
354k
  for (int worker_idx = num_workers - 1; worker_idx >= 0; --worker_idx) {
3470
231k
    AVxWorker *const worker = &pbi->tile_workers[worker_idx];
3471
231k
    DecWorkerData *const thread_data = (DecWorkerData *)worker->data1;
3472
3473
231k
    thread_data->data_end = data_end;
3474
3475
231k
    worker->had_error = 0;
3476
231k
    if (worker_idx == 0) {
3477
123k
      winterface->execute(worker);
3478
123k
    } else {
3479
107k
      winterface->launch(worker);
3480
107k
    }
3481
231k
  }
3482
123k
}
3483
3484
123k
static AOM_INLINE void sync_dec_workers(AV1Decoder *pbi, int num_workers) {
3485
123k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
3486
123k
  int corrupted = 0;
3487
3488
354k
  for (int worker_idx = num_workers; worker_idx > 0; --worker_idx) {
3489
231k
    AVxWorker *const worker = &pbi->tile_workers[worker_idx - 1];
3490
231k
    aom_merge_corrupted_flag(&corrupted, !winterface->sync(worker));
3491
231k
  }
3492
3493
123k
  pbi->dcb.corrupted = corrupted;
3494
123k
}
3495
3496
124k
static AOM_INLINE void decode_mt_init(AV1Decoder *pbi) {
3497
124k
  AV1_COMMON *const cm = &pbi->common;
3498
124k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
3499
124k
  int worker_idx;
3500
3501
  // Create workers and thread_data
3502
124k
  if (pbi->num_workers == 0) {
3503
3.82k
    const int num_threads = pbi->max_threads;
3504
3.82k
    CHECK_MEM_ERROR(cm, pbi->tile_workers,
3505
3.82k
                    aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
3506
3.82k
    CHECK_MEM_ERROR(cm, pbi->thread_data,
3507
3.82k
                    aom_calloc(num_threads, sizeof(*pbi->thread_data)));
3508
3509
136k
    for (worker_idx = 0; worker_idx < num_threads; ++worker_idx) {
3510
132k
      AVxWorker *const worker = &pbi->tile_workers[worker_idx];
3511
132k
      DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
3512
3513
132k
      winterface->init(worker);
3514
132k
      worker->thread_name = "aom tile worker";
3515
132k
      if (worker_idx != 0 && !winterface->reset(worker)) {
3516
0
        aom_internal_error(&pbi->error, AOM_CODEC_ERROR,
3517
0
                           "Tile decoder thread creation failed");
3518
0
      }
3519
132k
      ++pbi->num_workers;
3520
3521
132k
      if (worker_idx != 0) {
3522
        // Allocate thread data.
3523
128k
        CHECK_MEM_ERROR(cm, thread_data->td,
3524
128k
                        aom_memalign(32, sizeof(*thread_data->td)));
3525
128k
        av1_zero(*thread_data->td);
3526
128k
      } else {
3527
        // Main thread acts as a worker and uses the thread data in pbi
3528
3.82k
        thread_data->td = &pbi->td;
3529
3.82k
      }
3530
132k
      thread_data->error_info.error_code = AOM_CODEC_OK;
3531
132k
      thread_data->error_info.setjmp = 0;
3532
132k
    }
3533
3.82k
  }
3534
124k
  const int use_highbd = cm->seq_params->use_highbitdepth;
3535
124k
  const int buf_size = MC_TEMP_BUF_PELS << use_highbd;
3536
5.62M
  for (worker_idx = 1; worker_idx < pbi->max_threads; ++worker_idx) {
3537
5.50M
    DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
3538
5.50M
    if (thread_data->td->mc_buf_size != buf_size) {
3539
139k
      av1_free_mc_tmp_buf(thread_data->td);
3540
139k
      allocate_mc_tmp_buf(cm, thread_data->td, buf_size, use_highbd);
3541
139k
    }
3542
5.50M
  }
3543
124k
}
3544
3545
static AOM_INLINE void tile_mt_queue(AV1Decoder *pbi, int tile_cols,
3546
                                     int tile_rows, int tile_rows_start,
3547
                                     int tile_rows_end, int tile_cols_start,
3548
                                     int tile_cols_end, int start_tile,
3549
123k
                                     int end_tile) {
3550
123k
  AV1_COMMON *const cm = &pbi->common;
3551
123k
  if (pbi->tile_mt_info.alloc_tile_cols != tile_cols ||
3552
123k
      pbi->tile_mt_info.alloc_tile_rows != tile_rows) {
3553
4.89k
    av1_dealloc_dec_jobs(&pbi->tile_mt_info);
3554
4.89k
    alloc_dec_jobs(&pbi->tile_mt_info, cm, tile_rows, tile_cols);
3555
4.89k
  }
3556
123k
  enqueue_tile_jobs(pbi, cm, tile_rows_start, tile_rows_end, tile_cols_start,
3557
123k
                    tile_cols_end, start_tile, end_tile);
3558
123k
  qsort(pbi->tile_mt_info.job_queue, pbi->tile_mt_info.jobs_enqueued,
3559
123k
        sizeof(pbi->tile_mt_info.job_queue[0]), compare_tile_buffers);
3560
123k
}
3561
3562
static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
3563
                                      const uint8_t *data_end, int start_tile,
3564
0
                                      int end_tile) {
3565
0
  AV1_COMMON *const cm = &pbi->common;
3566
0
  CommonTileParams *const tiles = &cm->tiles;
3567
0
  const int tile_cols = tiles->cols;
3568
0
  const int tile_rows = tiles->rows;
3569
0
  const int n_tiles = tile_cols * tile_rows;
3570
0
  TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
3571
0
  const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
3572
0
  const int single_row = pbi->dec_tile_row >= 0;
3573
0
  const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
3574
0
  const int single_col = pbi->dec_tile_col >= 0;
3575
0
  int tile_rows_start;
3576
0
  int tile_rows_end;
3577
0
  int tile_cols_start;
3578
0
  int tile_cols_end;
3579
0
  int tile_count_tg;
3580
0
  int num_workers;
3581
0
  const uint8_t *raw_data_end = NULL;
3582
3583
0
  if (tiles->large_scale) {
3584
0
    tile_rows_start = single_row ? dec_tile_row : 0;
3585
0
    tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
3586
0
    tile_cols_start = single_col ? dec_tile_col : 0;
3587
0
    tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3588
0
  } else {
3589
0
    tile_rows_start = 0;
3590
0
    tile_rows_end = tile_rows;
3591
0
    tile_cols_start = 0;
3592
0
    tile_cols_end = tile_cols;
3593
0
  }
3594
0
  tile_count_tg = end_tile - start_tile + 1;
3595
0
  num_workers = AOMMIN(pbi->max_threads, tile_count_tg);
3596
3597
  // No tiles to decode.
3598
0
  if (tile_rows_end <= tile_rows_start || tile_cols_end <= tile_cols_start ||
3599
      // First tile is larger than end_tile.
3600
0
      tile_rows_start * tile_cols + tile_cols_start > end_tile ||
3601
      // Last tile is smaller than start_tile.
3602
0
      (tile_rows_end - 1) * tile_cols + tile_cols_end - 1 < start_tile)
3603
0
    return data;
3604
3605
0
  assert(tile_rows <= MAX_TILE_ROWS);
3606
0
  assert(tile_cols <= MAX_TILE_COLS);
3607
0
  assert(tile_count_tg > 0);
3608
0
  assert(num_workers > 0);
3609
0
  assert(start_tile <= end_tile);
3610
0
  assert(start_tile >= 0 && end_tile < n_tiles);
3611
3612
0
  decode_mt_init(pbi);
3613
3614
  // get tile size in tile group
3615
0
#if EXT_TILE_DEBUG
3616
0
  if (tiles->large_scale) assert(pbi->ext_tile_debug == 1);
3617
0
  if (tiles->large_scale)
3618
0
    raw_data_end = get_ls_tile_buffers(pbi, data, data_end, tile_buffers);
3619
0
  else
3620
0
#endif  // EXT_TILE_DEBUG
3621
0
    get_tile_buffers(pbi, data, data_end, tile_buffers, start_tile, end_tile);
3622
3623
0
  if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
3624
0
    decoder_alloc_tile_data(pbi, n_tiles);
3625
0
  }
3626
0
  if (pbi->dcb.xd.seg_mask == NULL)
3627
0
    CHECK_MEM_ERROR(cm, pbi->dcb.xd.seg_mask,
3628
0
                    (uint8_t *)aom_memalign(
3629
0
                        16, 2 * MAX_SB_SQUARE * sizeof(*pbi->dcb.xd.seg_mask)));
3630
3631
0
  for (int row = 0; row < tile_rows; row++) {
3632
0
    for (int col = 0; col < tile_cols; col++) {
3633
0
      TileDataDec *tile_data = pbi->tile_data + row * tiles->cols + col;
3634
0
      av1_tile_init(&tile_data->tile_info, cm, row, col);
3635
0
    }
3636
0
  }
3637
3638
0
  tile_mt_queue(pbi, tile_cols, tile_rows, tile_rows_start, tile_rows_end,
3639
0
                tile_cols_start, tile_cols_end, start_tile, end_tile);
3640
3641
0
  reset_dec_workers(pbi, tile_worker_hook, num_workers);
3642
0
  launch_dec_workers(pbi, data_end, num_workers);
3643
0
  sync_dec_workers(pbi, num_workers);
3644
3645
0
  if (pbi->dcb.corrupted)
3646
0
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
3647
0
                       "Failed to decode tile data");
3648
3649
0
  if (tiles->large_scale) {
3650
0
    if (n_tiles == 1) {
3651
      // Find the end of the single tile buffer
3652
0
      return aom_reader_find_end(&pbi->tile_data->bit_reader);
3653
0
    }
3654
    // Return the end of the last tile buffer
3655
0
    return raw_data_end;
3656
0
  }
3657
0
  TileDataDec *const tile_data = pbi->tile_data + end_tile;
3658
3659
0
  return aom_reader_find_end(&tile_data->bit_reader);
3660
0
}
3661
3662
123k
static AOM_INLINE void dec_alloc_cb_buf(AV1Decoder *pbi) {
3663
123k
  AV1_COMMON *const cm = &pbi->common;
3664
123k
  int size = ((cm->mi_params.mi_rows >> cm->seq_params->mib_size_log2) + 1) *
3665
123k
             ((cm->mi_params.mi_cols >> cm->seq_params->mib_size_log2) + 1);
3666
3667
123k
  if (pbi->cb_buffer_alloc_size < size) {
3668
4.40k
    av1_dec_free_cb_buf(pbi);
3669
4.40k
    CHECK_MEM_ERROR(cm, pbi->cb_buffer_base,
3670
4.40k
                    aom_memalign(32, sizeof(*pbi->cb_buffer_base) * size));
3671
4.40k
    memset(pbi->cb_buffer_base, 0, sizeof(*pbi->cb_buffer_base) * size);
3672
4.40k
    pbi->cb_buffer_alloc_size = size;
3673
4.40k
  }
3674
123k
}
3675
3676
static AOM_INLINE void row_mt_frame_init(AV1Decoder *pbi, int tile_rows_start,
3677
                                         int tile_rows_end, int tile_cols_start,
3678
                                         int tile_cols_end, int start_tile,
3679
123k
                                         int end_tile, int max_sb_rows) {
3680
123k
  AV1_COMMON *const cm = &pbi->common;
3681
123k
  AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
3682
3683
123k
  frame_row_mt_info->tile_rows_start = tile_rows_start;
3684
123k
  frame_row_mt_info->tile_rows_end = tile_rows_end;
3685
123k
  frame_row_mt_info->tile_cols_start = tile_cols_start;
3686
123k
  frame_row_mt_info->tile_cols_end = tile_cols_end;
3687
123k
  frame_row_mt_info->start_tile = start_tile;
3688
123k
  frame_row_mt_info->end_tile = end_tile;
3689
123k
  frame_row_mt_info->mi_rows_to_decode = 0;
3690
123k
  frame_row_mt_info->mi_rows_parse_done = 0;
3691
123k
  frame_row_mt_info->mi_rows_decode_started = 0;
3692
123k
  frame_row_mt_info->row_mt_exit = 0;
3693
3694
263k
  for (int tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3695
286k
    for (int tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3696
146k
      if (tile_row * cm->tiles.cols + tile_col < start_tile ||
3697
146k
          tile_row * cm->tiles.cols + tile_col > end_tile)
3698
121
        continue;
3699
3700
146k
      TileDataDec *const tile_data =
3701
146k
          pbi->tile_data + tile_row * cm->tiles.cols + tile_col;
3702
146k
      const TileInfo *const tile_info = &tile_data->tile_info;
3703
3704
146k
      tile_data->dec_row_mt_sync.mi_rows_parse_done = 0;
3705
146k
      tile_data->dec_row_mt_sync.mi_rows_decode_started = 0;
3706
146k
      tile_data->dec_row_mt_sync.num_threads_working = 0;
3707
146k
      tile_data->dec_row_mt_sync.mi_rows =
3708
146k
          ALIGN_POWER_OF_TWO(tile_info->mi_row_end - tile_info->mi_row_start,
3709
146k
                             cm->seq_params->mib_size_log2);
3710
146k
      tile_data->dec_row_mt_sync.mi_cols =
3711
146k
          ALIGN_POWER_OF_TWO(tile_info->mi_col_end - tile_info->mi_col_start,
3712
146k
                             cm->seq_params->mib_size_log2);
3713
146k
      tile_data->dec_row_mt_sync.intrabc_extra_top_right_sb_delay =
3714
146k
          av1_get_intrabc_extra_top_right_sb_delay(cm);
3715
3716
146k
      frame_row_mt_info->mi_rows_to_decode +=
3717
146k
          tile_data->dec_row_mt_sync.mi_rows;
3718
3719
      // Initialize cur_sb_col to -1 for all SB rows.
3720
146k
      memset(tile_data->dec_row_mt_sync.cur_sb_col, -1,
3721
146k
             sizeof(*tile_data->dec_row_mt_sync.cur_sb_col) * max_sb_rows);
3722
146k
    }
3723
139k
  }
3724
3725
123k
#if CONFIG_MULTITHREAD
3726
123k
  if (pbi->row_mt_mutex_ == NULL) {
3727
3.63k
    CHECK_MEM_ERROR(cm, pbi->row_mt_mutex_,
3728
3.63k
                    aom_malloc(sizeof(*(pbi->row_mt_mutex_))));
3729
3.63k
    if (pbi->row_mt_mutex_) {
3730
3.63k
      pthread_mutex_init(pbi->row_mt_mutex_, NULL);
3731
3.63k
    }
3732
3.63k
  }
3733
3734
123k
  if (pbi->row_mt_cond_ == NULL) {
3735
3.63k
    CHECK_MEM_ERROR(cm, pbi->row_mt_cond_,
3736
3.63k
                    aom_malloc(sizeof(*(pbi->row_mt_cond_))));
3737
3.63k
    if (pbi->row_mt_cond_) {
3738
3.63k
      pthread_cond_init(pbi->row_mt_cond_, NULL);
3739
3.63k
    }
3740
3.63k
  }
3741
123k
#endif
3742
123k
}
3743
3744
static const uint8_t *decode_tiles_row_mt(AV1Decoder *pbi, const uint8_t *data,
3745
                                          const uint8_t *data_end,
3746
124k
                                          int start_tile, int end_tile) {
3747
124k
  AV1_COMMON *const cm = &pbi->common;
3748
124k
  CommonTileParams *const tiles = &cm->tiles;
3749
124k
  const int tile_cols = tiles->cols;
3750
124k
  const int tile_rows = tiles->rows;
3751
124k
  const int n_tiles = tile_cols * tile_rows;
3752
124k
  TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
3753
124k
  const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
3754
124k
  const int single_row = pbi->dec_tile_row >= 0;
3755
124k
  const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
3756
124k
  const int single_col = pbi->dec_tile_col >= 0;
3757
124k
  int tile_rows_start;
3758
124k
  int tile_rows_end;
3759
124k
  int tile_cols_start;
3760
124k
  int tile_cols_end;
3761
124k
  int tile_count_tg;
3762
124k
  int num_workers = 0;
3763
124k
  int max_threads;
3764
124k
  const uint8_t *raw_data_end = NULL;
3765
124k
  int max_sb_rows = 0;
3766
3767
124k
  if (tiles->large_scale) {
3768
3.61k
    tile_rows_start = single_row ? dec_tile_row : 0;
3769
3.61k
    tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
3770
3.61k
    tile_cols_start = single_col ? dec_tile_col : 0;
3771
3.61k
    tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3772
120k
  } else {
3773
120k
    tile_rows_start = 0;
3774
120k
    tile_rows_end = tile_rows;
3775
120k
    tile_cols_start = 0;
3776
120k
    tile_cols_end = tile_cols;
3777
120k
  }
3778
124k
  tile_count_tg = end_tile - start_tile + 1;
3779
124k
  max_threads = pbi->max_threads;
3780
3781
  // No tiles to decode.
3782
124k
  if (tile_rows_end <= tile_rows_start || tile_cols_end <= tile_cols_start ||
3783
      // First tile is larger than end_tile.
3784
124k
      tile_rows_start * tile_cols + tile_cols_start > end_tile ||
3785
      // Last tile is smaller than start_tile.
3786
124k
      (tile_rows_end - 1) * tile_cols + tile_cols_end - 1 < start_tile)
3787
0
    return data;
3788
3789
124k
  assert(tile_rows <= MAX_TILE_ROWS);
3790
124k
  assert(tile_cols <= MAX_TILE_COLS);
3791
124k
  assert(tile_count_tg > 0);
3792
124k
  assert(max_threads > 0);
3793
124k
  assert(start_tile <= end_tile);
3794
124k
  assert(start_tile >= 0 && end_tile < n_tiles);
3795
3796
124k
  (void)tile_count_tg;
3797
3798
124k
  decode_mt_init(pbi);
3799
3800
  // get tile size in tile group
3801
124k
#if EXT_TILE_DEBUG
3802
124k
  if (tiles->large_scale) assert(pbi->ext_tile_debug == 1);
3803
124k
  if (tiles->large_scale)
3804
3.61k
    raw_data_end = get_ls_tile_buffers(pbi, data, data_end, tile_buffers);
3805
120k
  else
3806
120k
#endif  // EXT_TILE_DEBUG
3807
120k
    get_tile_buffers(pbi, data, data_end, tile_buffers, start_tile, end_tile);
3808
3809
124k
  if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
3810
4.89k
    if (pbi->tile_data != NULL) {
3811
7.00k
      for (int i = 0; i < pbi->allocated_tiles; i++) {
3812
5.75k
        TileDataDec *const tile_data = pbi->tile_data + i;
3813
5.75k
        av1_dec_row_mt_dealloc(&tile_data->dec_row_mt_sync);
3814
5.75k
      }
3815
1.25k
    }
3816
4.89k
    decoder_alloc_tile_data(pbi, n_tiles);
3817
4.89k
  }
3818
124k
  if (pbi->dcb.xd.seg_mask == NULL)
3819
124k
    CHECK_MEM_ERROR(cm, pbi->dcb.xd.seg_mask,
3820
124k
                    (uint8_t *)aom_memalign(
3821
124k
                        16, 2 * MAX_SB_SQUARE * sizeof(*pbi->dcb.xd.seg_mask)));
3822
3823
263k
  for (int row = 0; row < tile_rows; row++) {
3824
286k
    for (int col = 0; col < tile_cols; col++) {
3825
146k
      TileDataDec *tile_data = pbi->tile_data + row * tiles->cols + col;
3826
146k
      av1_tile_init(&tile_data->tile_info, cm, row, col);
3827
3828
146k
      max_sb_rows = AOMMAX(max_sb_rows,
3829
146k
                           av1_get_sb_rows_in_tile(cm, &tile_data->tile_info));
3830
146k
      num_workers += get_max_row_mt_workers_per_tile(cm, &tile_data->tile_info);
3831
146k
    }
3832
139k
  }
3833
124k
  num_workers = AOMMIN(num_workers, max_threads);
3834
3835
124k
  if (pbi->allocated_row_mt_sync_rows != max_sb_rows) {
3836
42.9k
    for (int i = 0; i < n_tiles; ++i) {
3837
31.2k
      TileDataDec *const tile_data = pbi->tile_data + i;
3838
31.2k
      av1_dec_row_mt_dealloc(&tile_data->dec_row_mt_sync);
3839
31.2k
      dec_row_mt_alloc(&tile_data->dec_row_mt_sync, cm, max_sb_rows);
3840
31.2k
    }
3841
11.7k
    pbi->allocated_row_mt_sync_rows = max_sb_rows;
3842
11.7k
  }
3843
3844
124k
  tile_mt_queue(pbi, tile_cols, tile_rows, tile_rows_start, tile_rows_end,
3845
124k
                tile_cols_start, tile_cols_end, start_tile, end_tile);
3846
3847
124k
  dec_alloc_cb_buf(pbi);
3848
3849
124k
  row_mt_frame_init(pbi, tile_rows_start, tile_rows_end, tile_cols_start,
3850
124k
                    tile_cols_end, start_tile, end_tile, max_sb_rows);
3851
3852
124k
  reset_dec_workers(pbi, row_mt_worker_hook, num_workers);
3853
124k
  launch_dec_workers(pbi, data_end, num_workers);
3854
124k
  sync_dec_workers(pbi, num_workers);
3855
3856
124k
  if (pbi->dcb.corrupted)
3857
7.72k
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
3858
7.72k
                       "Failed to decode tile data");
3859
3860
124k
  if (tiles->large_scale) {
3861
2.74k
    if (n_tiles == 1) {
3862
      // Find the end of the single tile buffer
3863
2.74k
      return aom_reader_find_end(&pbi->tile_data->bit_reader);
3864
2.74k
    }
3865
    // Return the end of the last tile buffer
3866
0
    return raw_data_end;
3867
2.74k
  }
3868
121k
  TileDataDec *const tile_data = pbi->tile_data + end_tile;
3869
3870
121k
  return aom_reader_find_end(&tile_data->bit_reader);
3871
124k
}
3872
3873
11.6k
static AOM_INLINE void error_handler(void *data) {
3874
11.6k
  AV1_COMMON *const cm = (AV1_COMMON *)data;
3875
11.6k
  aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
3876
11.6k
}
3877
3878
// Reads the high_bitdepth and twelve_bit fields in color_config() and sets
3879
// seq_params->bit_depth based on the values of those fields and
3880
// seq_params->profile. Reports errors by calling rb->error_handler() or
3881
// aom_internal_error().
3882
static AOM_INLINE void read_bitdepth(
3883
    struct aom_read_bit_buffer *rb, SequenceHeader *seq_params,
3884
72.4k
    struct aom_internal_error_info *error_info) {
3885
72.4k
  const int high_bitdepth = aom_rb_read_bit(rb);
3886
72.4k
  if (seq_params->profile == PROFILE_2 && high_bitdepth) {
3887
21.9k
    const int twelve_bit = aom_rb_read_bit(rb);
3888
21.9k
    seq_params->bit_depth = twelve_bit ? AOM_BITS_12 : AOM_BITS_10;
3889
50.5k
  } else if (seq_params->profile <= PROFILE_2) {
3890
50.3k
    seq_params->bit_depth = high_bitdepth ? AOM_BITS_10 : AOM_BITS_8;
3891
50.3k
  } else {
3892
185
    aom_internal_error(error_info, AOM_CODEC_UNSUP_BITSTREAM,
3893
185
                       "Unsupported profile/bit-depth combination");
3894
185
  }
3895
#if !CONFIG_AV1_HIGHBITDEPTH
3896
  if (seq_params->bit_depth > AOM_BITS_8) {
3897
    aom_internal_error(error_info, AOM_CODEC_UNSUP_BITSTREAM,
3898
                       "Bit-depth %d not supported", seq_params->bit_depth);
3899
  }
3900
#endif
3901
72.4k
}
3902
3903
static void read_film_grain_params(AV1_COMMON *cm,
3904
48.7k
                                   struct aom_read_bit_buffer *rb) {
3905
48.7k
  aom_film_grain_t *pars = &cm->film_grain_params;
3906
48.7k
  const SequenceHeader *const seq_params = cm->seq_params;
3907
3908
48.7k
  pars->apply_grain = aom_rb_read_bit(rb);
3909
48.7k
  if (!pars->apply_grain) {
3910
32.6k
    memset(pars, 0, sizeof(*pars));
3911
32.6k
    return;
3912
32.6k
  }
3913
3914
16.1k
  pars->random_seed = aom_rb_read_literal(rb, 16);
3915
16.1k
  if (cm->current_frame.frame_type == INTER_FRAME)
3916
741
    pars->update_parameters = aom_rb_read_bit(rb);
3917
15.3k
  else
3918
15.3k
    pars->update_parameters = 1;
3919
3920
16.1k
  pars->bit_depth = seq_params->bit_depth;
3921
3922
16.1k
  if (!pars->update_parameters) {
3923
    // inherit parameters from a previous reference frame
3924
500
    int film_grain_params_ref_idx = aom_rb_read_literal(rb, 3);
3925
    // Section 6.8.20: It is a requirement of bitstream conformance that
3926
    // film_grain_params_ref_idx is equal to ref_frame_idx[ j ] for some value
3927
    // of j in the range 0 to REFS_PER_FRAME - 1.
3928
500
    int found = 0;
3929
2.15k
    for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3930
2.09k
      if (film_grain_params_ref_idx == cm->remapped_ref_idx[i]) {
3931
447
        found = 1;
3932
447
        break;
3933
447
      }
3934
2.09k
    }
3935
500
    if (!found) {
3936
52
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3937
52
                         "Invalid film grain reference idx %d. ref_frame_idx = "
3938
52
                         "{%d, %d, %d, %d, %d, %d, %d}",
3939
52
                         film_grain_params_ref_idx, cm->remapped_ref_idx[0],
3940
52
                         cm->remapped_ref_idx[1], cm->remapped_ref_idx[2],
3941
52
                         cm->remapped_ref_idx[3], cm->remapped_ref_idx[4],
3942
52
                         cm->remapped_ref_idx[5], cm->remapped_ref_idx[6]);
3943
52
    }
3944
500
    RefCntBuffer *const buf = cm->ref_frame_map[film_grain_params_ref_idx];
3945
500
    if (buf == NULL) {
3946
0
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3947
0
                         "Invalid Film grain reference idx");
3948
0
    }
3949
500
    if (!buf->film_grain_params_present) {
3950
62
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3951
62
                         "Film grain reference parameters not available");
3952
62
    }
3953
500
    uint16_t random_seed = pars->random_seed;
3954
500
    *pars = buf->film_grain_params;   // inherit paramaters
3955
500
    pars->random_seed = random_seed;  // with new random seed
3956
500
    return;
3957
500
  }
3958
3959
  // Scaling functions parameters
3960
15.6k
  pars->num_y_points = aom_rb_read_literal(rb, 4);  // max 14
3961
15.6k
  if (pars->num_y_points > 14)
3962
51
    aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3963
51
                       "Number of points for film grain luma scaling function "
3964
51
                       "exceeds the maximum value.");
3965
38.0k
  for (int i = 0; i < pars->num_y_points; i++) {
3966
22.4k
    pars->scaling_points_y[i][0] = aom_rb_read_literal(rb, 8);
3967
22.4k
    if (i && pars->scaling_points_y[i - 1][0] >= pars->scaling_points_y[i][0])
3968
527
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3969
527
                         "First coordinate of the scaling function points "
3970
527
                         "shall be increasing.");
3971
22.4k
    pars->scaling_points_y[i][1] = aom_rb_read_literal(rb, 8);
3972
22.4k
  }
3973
3974
15.6k
  if (!seq_params->monochrome)
3975
14.5k
    pars->chroma_scaling_from_luma = aom_rb_read_bit(rb);
3976
1.04k
  else
3977
1.04k
    pars->chroma_scaling_from_luma = 0;
3978
3979
15.6k
  if (seq_params->monochrome || pars->chroma_scaling_from_luma ||
3980
15.6k
      ((seq_params->subsampling_x == 1) && (seq_params->subsampling_y == 1) &&
3981
8.94k
       (pars->num_y_points == 0))) {
3982
8.46k
    pars->num_cb_points = 0;
3983
8.46k
    pars->num_cr_points = 0;
3984
8.46k
  } else {
3985
7.14k
    pars->num_cb_points = aom_rb_read_literal(rb, 4);  // max 10
3986
7.14k
    if (pars->num_cb_points > 10)
3987
109
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3988
109
                         "Number of points for film grain cb scaling function "
3989
109
                         "exceeds the maximum value.");
3990
12.5k
    for (int i = 0; i < pars->num_cb_points; i++) {
3991
5.36k
      pars->scaling_points_cb[i][0] = aom_rb_read_literal(rb, 8);
3992
5.36k
      if (i &&
3993
5.36k
          pars->scaling_points_cb[i - 1][0] >= pars->scaling_points_cb[i][0])
3994
172
        aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3995
172
                           "First coordinate of the scaling function points "
3996
172
                           "shall be increasing.");
3997
5.36k
      pars->scaling_points_cb[i][1] = aom_rb_read_literal(rb, 8);
3998
5.36k
    }
3999
4000
7.14k
    pars->num_cr_points = aom_rb_read_literal(rb, 4);  // max 10
4001
7.14k
    if (pars->num_cr_points > 10)
4002
89
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4003
89
                         "Number of points for film grain cr scaling function "
4004
89
                         "exceeds the maximum value.");
4005
16.7k
    for (int i = 0; i < pars->num_cr_points; i++) {
4006
9.59k
      pars->scaling_points_cr[i][0] = aom_rb_read_literal(rb, 8);
4007
9.59k
      if (i &&
4008
9.59k
          pars->scaling_points_cr[i - 1][0] >= pars->scaling_points_cr[i][0])
4009
135
        aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4010
135
                           "First coordinate of the scaling function points "
4011
135
                           "shall be increasing.");
4012
9.59k
      pars->scaling_points_cr[i][1] = aom_rb_read_literal(rb, 8);
4013
9.59k
    }
4014
4015
7.14k
    if ((seq_params->subsampling_x == 1) && (seq_params->subsampling_y == 1) &&
4016
7.14k
        (((pars->num_cb_points == 0) && (pars->num_cr_points != 0)) ||
4017
704
         ((pars->num_cb_points != 0) && (pars->num_cr_points == 0))))
4018
22
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4019
22
                         "In YCbCr 4:2:0, film grain shall be applied "
4020
22
                         "to both chroma components or neither.");
4021
7.14k
  }
4022
4023
15.6k
  pars->scaling_shift = aom_rb_read_literal(rb, 2) + 8;  // 8 + value
4024
4025
  // AR coefficients
4026
  // Only sent if the corresponsing scaling function has
4027
  // more than 0 points
4028
4029
15.6k
  pars->ar_coeff_lag = aom_rb_read_literal(rb, 2);
4030
4031
15.6k
  int num_pos_luma = 2 * pars->ar_coeff_lag * (pars->ar_coeff_lag + 1);
4032
15.6k
  int num_pos_chroma = num_pos_luma;
4033
15.6k
  if (pars->num_y_points > 0) ++num_pos_chroma;
4034
4035
15.6k
  if (pars->num_y_points)
4036
34.5k
    for (int i = 0; i < num_pos_luma; i++)
4037
24.2k
      pars->ar_coeffs_y[i] = aom_rb_read_literal(rb, 8) - 128;
4038
4039
15.6k
  if (pars->num_cb_points || pars->chroma_scaling_from_luma)
4040
36.6k
    for (int i = 0; i < num_pos_chroma; i++)
4041
29.7k
      pars->ar_coeffs_cb[i] = aom_rb_read_literal(rb, 8) - 128;
4042
4043
15.6k
  if (pars->num_cr_points || pars->chroma_scaling_from_luma)
4044
38.2k
    for (int i = 0; i < num_pos_chroma; i++)
4045
30.3k
      pars->ar_coeffs_cr[i] = aom_rb_read_literal(rb, 8) - 128;
4046
4047
15.6k
  pars->ar_coeff_shift = aom_rb_read_literal(rb, 2) + 6;  // 6 + value
4048
4049
15.6k
  pars->grain_scale_shift = aom_rb_read_literal(rb, 2);
4050
4051
15.6k
  if (pars->num_cb_points) {
4052
1.27k
    pars->cb_mult = aom_rb_read_literal(rb, 8);
4053
1.27k
    pars->cb_luma_mult = aom_rb_read_literal(rb, 8);
4054
1.27k
    pars->cb_offset = aom_rb_read_literal(rb, 9);
4055
1.27k
  }
4056
4057
15.6k
  if (pars->num_cr_points) {
4058
2.38k
    pars->cr_mult = aom_rb_read_literal(rb, 8);
4059
2.38k
    pars->cr_luma_mult = aom_rb_read_literal(rb, 8);
4060
2.38k
    pars->cr_offset = aom_rb_read_literal(rb, 9);
4061
2.38k
  }
4062
4063
15.6k
  pars->overlap_flag = aom_rb_read_bit(rb);
4064
4065
15.6k
  pars->clip_to_restricted_range = aom_rb_read_bit(rb);
4066
15.6k
}
4067
4068
static AOM_INLINE void read_film_grain(AV1_COMMON *cm,
4069
202k
                                       struct aom_read_bit_buffer *rb) {
4070
202k
  if (cm->seq_params->film_grain_params_present &&
4071
202k
      (cm->show_frame || cm->showable_frame)) {
4072
48.7k
    read_film_grain_params(cm, rb);
4073
153k
  } else {
4074
153k
    memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
4075
153k
  }
4076
202k
  cm->film_grain_params.bit_depth = cm->seq_params->bit_depth;
4077
202k
  memcpy(&cm->cur_frame->film_grain_params, &cm->film_grain_params,
4078
202k
         sizeof(aom_film_grain_t));
4079
202k
}
4080
4081
void av1_read_color_config(struct aom_read_bit_buffer *rb,
4082
                           int allow_lowbitdepth, SequenceHeader *seq_params,
4083
72.4k
                           struct aom_internal_error_info *error_info) {
4084
72.4k
  read_bitdepth(rb, seq_params, error_info);
4085
4086
72.4k
  seq_params->use_highbitdepth =
4087
72.4k
      seq_params->bit_depth > AOM_BITS_8 || !allow_lowbitdepth;
4088
  // monochrome bit (not needed for PROFILE_1)
4089
72.4k
  const int is_monochrome =
4090
72.4k
      seq_params->profile != PROFILE_1 ? aom_rb_read_bit(rb) : 0;
4091
72.4k
  seq_params->monochrome = is_monochrome;
4092
72.4k
  int color_description_present_flag = aom_rb_read_bit(rb);
4093
72.4k
  if (color_description_present_flag) {
4094
5.08k
    seq_params->color_primaries = aom_rb_read_literal(rb, 8);
4095
5.08k
    seq_params->transfer_characteristics = aom_rb_read_literal(rb, 8);
4096
5.08k
    seq_params->matrix_coefficients = aom_rb_read_literal(rb, 8);
4097
67.3k
  } else {
4098
67.3k
    seq_params->color_primaries = AOM_CICP_CP_UNSPECIFIED;
4099
67.3k
    seq_params->transfer_characteristics = AOM_CICP_TC_UNSPECIFIED;
4100
67.3k
    seq_params->matrix_coefficients = AOM_CICP_MC_UNSPECIFIED;
4101
67.3k
  }
4102
72.4k
  if (is_monochrome) {
4103
    // [16,235] (including xvycc) vs [0,255] range
4104
14.2k
    seq_params->color_range = aom_rb_read_bit(rb);
4105
14.2k
    seq_params->subsampling_y = seq_params->subsampling_x = 1;
4106
14.2k
    seq_params->chroma_sample_position = AOM_CSP_UNKNOWN;
4107
14.2k
    seq_params->separate_uv_delta_q = 0;
4108
14.2k
    return;
4109
14.2k
  }
4110
58.2k
  if (seq_params->color_primaries == AOM_CICP_CP_BT_709 &&
4111
58.2k
      seq_params->transfer_characteristics == AOM_CICP_TC_SRGB &&
4112
58.2k
      seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY) {
4113
2
    seq_params->subsampling_y = seq_params->subsampling_x = 0;
4114
2
    seq_params->color_range = 1;  // assume full color-range
4115
2
    if (!(seq_params->profile == PROFILE_1 ||
4116
2
          (seq_params->profile == PROFILE_2 &&
4117
2
           seq_params->bit_depth == AOM_BITS_12))) {
4118
2
      aom_internal_error(
4119
2
          error_info, AOM_CODEC_UNSUP_BITSTREAM,
4120
2
          "sRGB colorspace not compatible with specified profile");
4121
2
    }
4122
58.2k
  } else {
4123
    // [16,235] (including xvycc) vs [0,255] range
4124
58.2k
    seq_params->color_range = aom_rb_read_bit(rb);
4125
58.2k
    if (seq_params->profile == PROFILE_0) {
4126
      // 420 only
4127
25.3k
      seq_params->subsampling_x = seq_params->subsampling_y = 1;
4128
32.8k
    } else if (seq_params->profile == PROFILE_1) {
4129
      // 444 only
4130
14.8k
      seq_params->subsampling_x = seq_params->subsampling_y = 0;
4131
18.0k
    } else {
4132
18.0k
      assert(seq_params->profile == PROFILE_2);
4133
17.0k
      if (seq_params->bit_depth == AOM_BITS_12) {
4134
14.2k
        seq_params->subsampling_x = aom_rb_read_bit(rb);
4135
14.2k
        if (seq_params->subsampling_x)
4136
1.10k
          seq_params->subsampling_y = aom_rb_read_bit(rb);  // 422 or 420
4137
13.1k
        else
4138
13.1k
          seq_params->subsampling_y = 0;  // 444
4139
14.2k
      } else {
4140
        // 422
4141
2.85k
        seq_params->subsampling_x = 1;
4142
2.85k
        seq_params->subsampling_y = 0;
4143
2.85k
      }
4144
17.0k
    }
4145
57.3k
    if (seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY &&
4146
57.3k
        (seq_params->subsampling_x || seq_params->subsampling_y)) {
4147
11
      aom_internal_error(
4148
11
          error_info, AOM_CODEC_UNSUP_BITSTREAM,
4149
11
          "Identity CICP Matrix incompatible with non 4:4:4 color sampling");
4150
11
    }
4151
57.3k
    if (seq_params->subsampling_x && seq_params->subsampling_y) {
4152
25.4k
      seq_params->chroma_sample_position = aom_rb_read_literal(rb, 2);
4153
25.4k
    }
4154
57.3k
  }
4155
57.3k
  seq_params->separate_uv_delta_q = aom_rb_read_bit(rb);
4156
57.3k
}
4157
4158
void av1_read_timing_info_header(aom_timing_info_t *timing_info,
4159
                                 struct aom_internal_error_info *error,
4160
4.31k
                                 struct aom_read_bit_buffer *rb) {
4161
4.31k
  timing_info->num_units_in_display_tick =
4162
4.31k
      aom_rb_read_unsigned_literal(rb,
4163
4.31k
                                   32);  // Number of units in a display tick
4164
4.31k
  timing_info->time_scale = aom_rb_read_unsigned_literal(rb, 32);  // Time scale
4165
4.31k
  if (timing_info->num_units_in_display_tick == 0 ||
4166
4.31k
      timing_info->time_scale == 0) {
4167
329
    aom_internal_error(
4168
329
        error, AOM_CODEC_UNSUP_BITSTREAM,
4169
329
        "num_units_in_display_tick and time_scale must be greater than 0.");
4170
329
  }
4171
4.31k
  timing_info->equal_picture_interval =
4172
4.31k
      aom_rb_read_bit(rb);  // Equal picture interval bit
4173
4.31k
  if (timing_info->equal_picture_interval) {
4174
1.86k
    const uint32_t num_ticks_per_picture_minus_1 = aom_rb_read_uvlc(rb);
4175
1.86k
    if (num_ticks_per_picture_minus_1 == UINT32_MAX) {
4176
195
      aom_internal_error(
4177
195
          error, AOM_CODEC_UNSUP_BITSTREAM,
4178
195
          "num_ticks_per_picture_minus_1 cannot be (1 << 32) - 1.");
4179
195
    }
4180
1.86k
    timing_info->num_ticks_per_picture = num_ticks_per_picture_minus_1 + 1;
4181
1.86k
  }
4182
4.31k
}
4183
4184
void av1_read_decoder_model_info(aom_dec_model_info_t *decoder_model_info,
4185
1.09k
                                 struct aom_read_bit_buffer *rb) {
4186
1.09k
  decoder_model_info->encoder_decoder_buffer_delay_length =
4187
1.09k
      aom_rb_read_literal(rb, 5) + 1;
4188
1.09k
  decoder_model_info->num_units_in_decoding_tick =
4189
1.09k
      aom_rb_read_unsigned_literal(rb,
4190
1.09k
                                   32);  // Number of units in a decoding tick
4191
1.09k
  decoder_model_info->buffer_removal_time_length =
4192
1.09k
      aom_rb_read_literal(rb, 5) + 1;
4193
1.09k
  decoder_model_info->frame_presentation_time_length =
4194
1.09k
      aom_rb_read_literal(rb, 5) + 1;
4195
1.09k
}
4196
4197
void av1_read_op_parameters_info(aom_dec_model_op_parameters_t *op_params,
4198
                                 int buffer_delay_length,
4199
1.32k
                                 struct aom_read_bit_buffer *rb) {
4200
1.32k
  op_params->decoder_buffer_delay =
4201
1.32k
      aom_rb_read_unsigned_literal(rb, buffer_delay_length);
4202
1.32k
  op_params->encoder_buffer_delay =
4203
1.32k
      aom_rb_read_unsigned_literal(rb, buffer_delay_length);
4204
1.32k
  op_params->low_delay_mode_flag = aom_rb_read_bit(rb);
4205
1.32k
}
4206
4207
static AOM_INLINE void read_temporal_point_info(
4208
1.09k
    AV1_COMMON *const cm, struct aom_read_bit_buffer *rb) {
4209
1.09k
  cm->frame_presentation_time = aom_rb_read_unsigned_literal(
4210
1.09k
      rb, cm->seq_params->decoder_model_info.frame_presentation_time_length);
4211
1.09k
}
4212
4213
void av1_read_sequence_header(AV1_COMMON *cm, struct aom_read_bit_buffer *rb,
4214
73.1k
                              SequenceHeader *seq_params) {
4215
73.1k
  const int num_bits_width = aom_rb_read_literal(rb, 4) + 1;
4216
73.1k
  const int num_bits_height = aom_rb_read_literal(rb, 4) + 1;
4217
73.1k
  const int max_frame_width = aom_rb_read_literal(rb, num_bits_width) + 1;
4218
73.1k
  const int max_frame_height = aom_rb_read_literal(rb, num_bits_height) + 1;
4219
4220
73.1k
  seq_params->num_bits_width = num_bits_width;
4221
73.1k
  seq_params->num_bits_height = num_bits_height;
4222
73.1k
  seq_params->max_frame_width = max_frame_width;
4223
73.1k
  seq_params->max_frame_height = max_frame_height;
4224
4225
73.1k
  if (seq_params->reduced_still_picture_hdr) {
4226
20.4k
    seq_params->frame_id_numbers_present_flag = 0;
4227
52.7k
  } else {
4228
52.7k
    seq_params->frame_id_numbers_present_flag = aom_rb_read_bit(rb);
4229
52.7k
  }
4230
73.1k
  if (seq_params->frame_id_numbers_present_flag) {
4231
    // We must always have delta_frame_id_length < frame_id_length,
4232
    // in order for a frame to be referenced with a unique delta.
4233
    // Avoid wasting bits by using a coding that enforces this restriction.
4234
14.8k
    seq_params->delta_frame_id_length = aom_rb_read_literal(rb, 4) + 2;
4235
14.8k
    seq_params->frame_id_length =
4236
14.8k
        aom_rb_read_literal(rb, 3) + seq_params->delta_frame_id_length + 1;
4237
14.8k
    if (seq_params->frame_id_length > 16)
4238
154
      aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
4239
154
                         "Invalid frame_id_length");
4240
14.8k
  }
4241
4242
73.1k
  setup_sb_size(seq_params, rb);
4243
4244
73.1k
  seq_params->enable_filter_intra = aom_rb_read_bit(rb);
4245
73.1k
  seq_params->enable_intra_edge_filter = aom_rb_read_bit(rb);
4246
4247
73.1k
  if (seq_params->reduced_still_picture_hdr) {
4248
20.4k
    seq_params->enable_interintra_compound = 0;
4249
20.4k
    seq_params->enable_masked_compound = 0;
4250
20.4k
    seq_params->enable_warped_motion = 0;
4251
20.4k
    seq_params->enable_dual_filter = 0;
4252
20.4k
    seq_params->order_hint_info.enable_order_hint = 0;
4253
20.4k
    seq_params->order_hint_info.enable_dist_wtd_comp = 0;
4254
20.4k
    seq_params->order_hint_info.enable_ref_frame_mvs = 0;
4255
20.4k
    seq_params->force_screen_content_tools = 2;  // SELECT_SCREEN_CONTENT_TOOLS
4256
20.4k
    seq_params->force_integer_mv = 2;            // SELECT_INTEGER_MV
4257
20.4k
    seq_params->order_hint_info.order_hint_bits_minus_1 = -1;
4258
52.7k
  } else {
4259
52.7k
    seq_params->enable_interintra_compound = aom_rb_read_bit(rb);
4260
52.7k
    seq_params->enable_masked_compound = aom_rb_read_bit(rb);
4261
52.7k
    seq_params->enable_warped_motion = aom_rb_read_bit(rb);
4262
52.7k
    seq_params->enable_dual_filter = aom_rb_read_bit(rb);
4263
4264
52.7k
    seq_params->order_hint_info.enable_order_hint = aom_rb_read_bit(rb);
4265
52.7k
    seq_params->order_hint_info.enable_dist_wtd_comp =
4266
52.7k
        seq_params->order_hint_info.enable_order_hint ? aom_rb_read_bit(rb) : 0;
4267
52.7k
    seq_params->order_hint_info.enable_ref_frame_mvs =
4268
52.7k
        seq_params->order_hint_info.enable_order_hint ? aom_rb_read_bit(rb) : 0;
4269
4270
52.7k
    if (aom_rb_read_bit(rb)) {
4271
33.2k
      seq_params->force_screen_content_tools =
4272
33.2k
          2;  // SELECT_SCREEN_CONTENT_TOOLS
4273
33.2k
    } else {
4274
19.5k
      seq_params->force_screen_content_tools = aom_rb_read_bit(rb);
4275
19.5k
    }
4276
4277
52.7k
    if (seq_params->force_screen_content_tools > 0) {
4278
46.6k
      if (aom_rb_read_bit(rb)) {
4279
36.9k
        seq_params->force_integer_mv = 2;  // SELECT_INTEGER_MV
4280
36.9k
      } else {
4281
9.75k
        seq_params->force_integer_mv = aom_rb_read_bit(rb);
4282
9.75k
      }
4283
46.6k
    } else {
4284
6.12k
      seq_params->force_integer_mv = 2;  // SELECT_INTEGER_MV
4285
6.12k
    }
4286
52.7k
    seq_params->order_hint_info.order_hint_bits_minus_1 =
4287
52.7k
        seq_params->order_hint_info.enable_order_hint
4288
52.7k
            ? aom_rb_read_literal(rb, 3)
4289
52.7k
            : -1;
4290
52.7k
  }
4291
4292
73.1k
  seq_params->enable_superres = aom_rb_read_bit(rb);
4293
73.1k
  seq_params->enable_cdef = aom_rb_read_bit(rb);
4294
73.1k
  seq_params->enable_restoration = aom_rb_read_bit(rb);
4295
73.1k
}
4296
4297
static int read_global_motion_params(WarpedMotionParams *params,
4298
                                     const WarpedMotionParams *ref_params,
4299
                                     struct aom_read_bit_buffer *rb,
4300
633k
                                     int allow_hp) {
4301
633k
  TransformationType type = aom_rb_read_bit(rb);
4302
633k
  if (type != IDENTITY) {
4303
31.7k
    if (aom_rb_read_bit(rb))
4304
25.2k
      type = ROTZOOM;
4305
6.48k
    else
4306
6.48k
      type = aom_rb_read_bit(rb) ? TRANSLATION : AFFINE;
4307
31.7k
  }
4308
4309
633k
  *params = default_warp_params;
4310
633k
  params->wmtype = type;
4311
4312
633k
  if (type >= ROTZOOM) {
4313
29.1k
    params->wmmat[2] = aom_rb_read_signed_primitive_refsubexpfin(
4314
29.1k
                           rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4315
29.1k
                           (ref_params->wmmat[2] >> GM_ALPHA_PREC_DIFF) -
4316
29.1k
                               (1 << GM_ALPHA_PREC_BITS)) *
4317
29.1k
                           GM_ALPHA_DECODE_FACTOR +
4318
29.1k
                       (1 << WARPEDMODEL_PREC_BITS);
4319
29.1k
    params->wmmat[3] = aom_rb_read_signed_primitive_refsubexpfin(
4320
29.1k
                           rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4321
29.1k
                           (ref_params->wmmat[3] >> GM_ALPHA_PREC_DIFF)) *
4322
29.1k
                       GM_ALPHA_DECODE_FACTOR;
4323
29.1k
  }
4324
4325
633k
  if (type >= AFFINE) {
4326
3.53k
    params->wmmat[4] = aom_rb_read_signed_primitive_refsubexpfin(
4327
3.53k
                           rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4328
3.53k
                           (ref_params->wmmat[4] >> GM_ALPHA_PREC_DIFF)) *
4329
3.53k
                       GM_ALPHA_DECODE_FACTOR;
4330
3.53k
    params->wmmat[5] = aom_rb_read_signed_primitive_refsubexpfin(
4331
3.53k
                           rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4332
3.53k
                           (ref_params->wmmat[5] >> GM_ALPHA_PREC_DIFF) -
4333
3.53k
                               (1 << GM_ALPHA_PREC_BITS)) *
4334
3.53k
                           GM_ALPHA_DECODE_FACTOR +
4335
3.53k
                       (1 << WARPEDMODEL_PREC_BITS);
4336
629k
  } else {
4337
629k
    params->wmmat[4] = -params->wmmat[3];
4338
629k
    params->wmmat[5] = params->wmmat[2];
4339
629k
  }
4340
4341
633k
  if (type >= TRANSLATION) {
4342
31.3k
    const int trans_bits = (type == TRANSLATION)
4343
31.3k
                               ? GM_ABS_TRANS_ONLY_BITS - !allow_hp
4344
31.3k
                               : GM_ABS_TRANS_BITS;
4345
31.3k
    const int trans_dec_factor =
4346
31.3k
        (type == TRANSLATION) ? GM_TRANS_ONLY_DECODE_FACTOR * (1 << !allow_hp)
4347
31.3k
                              : GM_TRANS_DECODE_FACTOR;
4348
31.3k
    const int trans_prec_diff = (type == TRANSLATION)
4349
31.3k
                                    ? GM_TRANS_ONLY_PREC_DIFF + !allow_hp
4350
31.3k
                                    : GM_TRANS_PREC_DIFF;
4351
31.3k
    params->wmmat[0] = aom_rb_read_signed_primitive_refsubexpfin(
4352
31.3k
                           rb, (1 << trans_bits) + 1, SUBEXPFIN_K,
4353
31.3k
                           (ref_params->wmmat[0] >> trans_prec_diff)) *
4354
31.3k
                       trans_dec_factor;
4355
31.3k
    params->wmmat[1] = aom_rb_read_signed_primitive_refsubexpfin(
4356
31.3k
                           rb, (1 << trans_bits) + 1, SUBEXPFIN_K,
4357
31.3k
                           (ref_params->wmmat[1] >> trans_prec_diff)) *
4358
31.3k
                       trans_dec_factor;
4359
31.3k
  }
4360
4361
633k
  int good_shear_params = av1_get_shear_params(params);
4362
633k
  if (!good_shear_params) return 0;
4363
4364
617k
  return 1;
4365
633k
}
4366
4367
static AOM_INLINE void read_global_motion(AV1_COMMON *cm,
4368
90.7k
                                          struct aom_read_bit_buffer *rb) {
4369
724k
  for (int frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
4370
633k
    const WarpedMotionParams *ref_params =
4371
633k
        cm->prev_frame ? &cm->prev_frame->global_motion[frame]
4372
633k
                       : &default_warp_params;
4373
633k
    int good_params =
4374
633k
        read_global_motion_params(&cm->global_motion[frame], ref_params, rb,
4375
633k
                                  cm->features.allow_high_precision_mv);
4376
633k
    if (!good_params) {
4377
#if WARPED_MOTION_DEBUG
4378
      printf("Warning: unexpected global motion shear params from aomenc\n");
4379
#endif
4380
16.2k
      cm->global_motion[frame].invalid = 1;
4381
16.2k
    }
4382
4383
    // TODO(sarahparker, debargha): The logic in the commented out code below
4384
    // does not work currently and causes mismatches when resize is on. Fix it
4385
    // before turning the optimization back on.
4386
    /*
4387
    YV12_BUFFER_CONFIG *ref_buf = get_ref_frame(cm, frame);
4388
    if (cm->width == ref_buf->y_crop_width &&
4389
        cm->height == ref_buf->y_crop_height) {
4390
      read_global_motion_params(&cm->global_motion[frame],
4391
                                &cm->prev_frame->global_motion[frame], rb,
4392
                                cm->features.allow_high_precision_mv);
4393
    } else {
4394
      cm->global_motion[frame] = default_warp_params;
4395
    }
4396
    */
4397
    /*
4398
    printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4399
           frame, cm->current_frame.frame_number, cm->show_frame,
4400
           cm->global_motion[frame].wmmat[0],
4401
           cm->global_motion[frame].wmmat[1],
4402
           cm->global_motion[frame].wmmat[2],
4403
           cm->global_motion[frame].wmmat[3]);
4404
           */
4405
633k
  }
4406
90.7k
  memcpy(cm->cur_frame->global_motion, cm->global_motion,
4407
90.7k
         REF_FRAMES * sizeof(WarpedMotionParams));
4408
90.7k
}
4409
4410
// Release the references to the frame buffers in cm->ref_frame_map and reset
4411
// all elements of cm->ref_frame_map to NULL.
4412
113k
static AOM_INLINE void reset_ref_frame_map(AV1_COMMON *const cm) {
4413
113k
  BufferPool *const pool = cm->buffer_pool;
4414
4415
1.01M
  for (int i = 0; i < REF_FRAMES; i++) {
4416
906k
    decrease_ref_count(cm->ref_frame_map[i], pool);
4417
906k
    cm->ref_frame_map[i] = NULL;
4418
906k
  }
4419
113k
}
4420
4421
// If the refresh_frame_flags bitmask is set, update reference frame id values
4422
// and mark frames as valid for reference.
4423
218k
static AOM_INLINE void update_ref_frame_id(AV1Decoder *const pbi) {
4424
218k
  AV1_COMMON *const cm = &pbi->common;
4425
218k
  int refresh_frame_flags = cm->current_frame.refresh_frame_flags;
4426
1.96M
  for (int i = 0; i < REF_FRAMES; i++) {
4427
1.74M
    if ((refresh_frame_flags >> i) & 1) {
4428
805k
      cm->ref_frame_id[i] = cm->current_frame_id;
4429
805k
      pbi->valid_for_referencing[i] = 1;
4430
805k
    }
4431
1.74M
  }
4432
218k
}
4433
4434
static AOM_INLINE void show_existing_frame_reset(AV1Decoder *const pbi,
4435
167
                                                 int existing_frame_idx) {
4436
167
  AV1_COMMON *const cm = &pbi->common;
4437
4438
167
  assert(cm->show_existing_frame);
4439
4440
167
  cm->current_frame.frame_type = KEY_FRAME;
4441
4442
167
  cm->current_frame.refresh_frame_flags = (1 << REF_FRAMES) - 1;
4443
4444
1.33k
  for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4445
1.16k
    cm->remapped_ref_idx[i] = INVALID_IDX;
4446
1.16k
  }
4447
4448
167
  if (pbi->need_resync) {
4449
87
    reset_ref_frame_map(cm);
4450
87
    pbi->need_resync = 0;
4451
87
  }
4452
4453
  // Note that the displayed frame must be valid for referencing in order to
4454
  // have been selected.
4455
167
  cm->current_frame_id = cm->ref_frame_id[existing_frame_idx];
4456
167
  update_ref_frame_id(pbi);
4457
4458
167
  cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
4459
167
}
4460
4461
21.8k
static INLINE void reset_frame_buffers(AV1_COMMON *cm) {
4462
21.8k
  RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
4463
21.8k
  int i;
4464
4465
21.8k
  lock_buffer_pool(cm->buffer_pool);
4466
21.8k
  reset_ref_frame_map(cm);
4467
21.8k
  assert(cm->cur_frame->ref_count == 1);
4468
371k
  for (i = 0; i < cm->buffer_pool->num_frame_bufs; ++i) {
4469
    // Reset all unreferenced frame buffers. We can also reset cm->cur_frame
4470
    // because we are the sole owner of cm->cur_frame.
4471
349k
    if (frame_bufs[i].ref_count > 0 && &frame_bufs[i] != cm->cur_frame) {
4472
4.14k
      continue;
4473
4.14k
    }
4474
345k
    frame_bufs[i].order_hint = 0;
4475
345k
    av1_zero(frame_bufs[i].ref_order_hints);
4476
345k
  }
4477
21.8k
  av1_zero_unused_internal_frame_buffers(&cm->buffer_pool->int_frame_buffers);
4478
21.8k
  unlock_buffer_pool(cm->buffer_pool);
4479
21.8k
}
4480
4481
// On success, returns 0. On failure, calls aom_internal_error and does not
4482
// return.
4483
static int read_uncompressed_header(AV1Decoder *pbi,
4484
266k
                                    struct aom_read_bit_buffer *rb) {
4485
266k
  AV1_COMMON *const cm = &pbi->common;
4486
266k
  const SequenceHeader *const seq_params = cm->seq_params;
4487
266k
  CurrentFrame *const current_frame = &cm->current_frame;
4488
266k
  FeatureFlags *const features = &cm->features;
4489
266k
  MACROBLOCKD *const xd = &pbi->dcb.xd;
4490
266k
  BufferPool *const pool = cm->buffer_pool;
4491
266k
  RefCntBuffer *const frame_bufs = pool->frame_bufs;
4492
266k
  aom_s_frame_info *sframe_info = &pbi->sframe_info;
4493
266k
  sframe_info->is_s_frame = 0;
4494
266k
  sframe_info->is_s_frame_at_altref = 0;
4495
4496
266k
  if (!pbi->sequence_header_ready) {
4497
1.24k
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4498
1.24k
                       "No sequence header");
4499
1.24k
  }
4500
4501
266k
  if (seq_params->reduced_still_picture_hdr) {
4502
34.4k
    cm->show_existing_frame = 0;
4503
34.4k
    cm->show_frame = 1;
4504
34.4k
    current_frame->frame_type = KEY_FRAME;
4505
34.4k
    if (pbi->sequence_header_changed) {
4506
      // This is the start of a new coded video sequence.
4507
6.16k
      pbi->sequence_header_changed = 0;
4508
6.16k
      pbi->decoding_first_frame = 1;
4509
6.16k
      reset_frame_buffers(cm);
4510
6.16k
    }
4511
34.4k
    features->error_resilient_mode = 1;
4512
231k
  } else {
4513
231k
    cm->show_existing_frame = aom_rb_read_bit(rb);
4514
231k
    pbi->reset_decoder_state = 0;
4515
4516
231k
    if (cm->show_existing_frame) {
4517
8.31k
      if (pbi->sequence_header_changed) {
4518
152
        aom_internal_error(
4519
152
            &pbi->error, AOM_CODEC_CORRUPT_FRAME,
4520
152
            "New sequence header starts with a show_existing_frame.");
4521
152
      }
4522
      // Show an existing frame directly.
4523
8.31k
      const int existing_frame_idx = aom_rb_read_literal(rb, 3);
4524
8.31k
      RefCntBuffer *const frame_to_show = cm->ref_frame_map[existing_frame_idx];
4525
8.31k
      if (frame_to_show == NULL) {
4526
4.74k
        aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
4527
4.74k
                           "Buffer does not contain a decoded frame");
4528
4.74k
      }
4529
8.31k
      if (seq_params->decoder_model_info_present_flag &&
4530
8.31k
          seq_params->timing_info.equal_picture_interval == 0) {
4531
91
        read_temporal_point_info(cm, rb);
4532
91
      }
4533
8.31k
      if (seq_params->frame_id_numbers_present_flag) {
4534
2.20k
        int frame_id_length = seq_params->frame_id_length;
4535
2.20k
        int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
4536
        /* Compare display_frame_id with ref_frame_id and check valid for
4537
         * referencing */
4538
2.20k
        if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
4539
2.20k
            pbi->valid_for_referencing[existing_frame_idx] == 0)
4540
1.75k
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4541
1.75k
                             "Reference buffer frame ID mismatch");
4542
2.20k
      }
4543
8.31k
      lock_buffer_pool(pool);
4544
8.31k
      assert(frame_to_show->ref_count > 0);
4545
      // cm->cur_frame should be the buffer referenced by the return value
4546
      // of the get_free_fb() call in assign_cur_frame_new_fb() (called by
4547
      // av1_receive_compressed_data()), so the ref_count should be 1.
4548
1.65k
      assert(cm->cur_frame->ref_count == 1);
4549
      // assign_frame_buffer_p() decrements ref_count directly rather than
4550
      // call decrease_ref_count(). If cm->cur_frame->raw_frame_buffer has
4551
      // already been allocated, it will not be released by
4552
      // assign_frame_buffer_p()!
4553
1.65k
      assert(!cm->cur_frame->raw_frame_buffer.data);
4554
1.65k
      assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
4555
1.65k
      pbi->reset_decoder_state = frame_to_show->frame_type == KEY_FRAME;
4556
1.65k
      unlock_buffer_pool(pool);
4557
4558
1.65k
      cm->lf.filter_level[0] = 0;
4559
1.65k
      cm->lf.filter_level[1] = 0;
4560
1.65k
      cm->show_frame = 1;
4561
1.65k
      current_frame->order_hint = frame_to_show->order_hint;
4562
4563
      // Section 6.8.2: It is a requirement of bitstream conformance that when
4564
      // show_existing_frame is used to show a previous frame, that the value
4565
      // of showable_frame for the previous frame was equal to 1.
4566
1.65k
      if (!frame_to_show->showable_frame) {
4567
866
        aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
4568
866
                           "Buffer does not contain a showable frame");
4569
866
      }
4570
      // Section 6.8.2: It is a requirement of bitstream conformance that when
4571
      // show_existing_frame is used to show a previous frame with
4572
      // RefFrameType[ frame_to_show_map_idx ] equal to KEY_FRAME, that the
4573
      // frame is output via the show_existing_frame mechanism at most once.
4574
1.65k
      if (pbi->reset_decoder_state) frame_to_show->showable_frame = 0;
4575
4576
1.65k
      cm->film_grain_params = frame_to_show->film_grain_params;
4577
4578
1.65k
      if (pbi->reset_decoder_state) {
4579
167
        show_existing_frame_reset(pbi, existing_frame_idx);
4580
1.48k
      } else {
4581
1.48k
        current_frame->refresh_frame_flags = 0;
4582
1.48k
      }
4583
4584
1.65k
      return 0;
4585
1.65k
    }
4586
4587
223k
    current_frame->frame_type = (FRAME_TYPE)aom_rb_read_literal(rb, 2);
4588
223k
    if (pbi->sequence_header_changed) {
4589
16.1k
      if (current_frame->frame_type == KEY_FRAME) {
4590
        // This is the start of a new coded video sequence.
4591
15.7k
        pbi->sequence_header_changed = 0;
4592
15.7k
        pbi->decoding_first_frame = 1;
4593
15.7k
        reset_frame_buffers(cm);
4594
15.7k
      } else {
4595
463
        aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4596
463
                           "Sequence header has changed without a keyframe.");
4597
463
      }
4598
16.1k
    }
4599
4600
223k
    cm->show_frame = aom_rb_read_bit(rb);
4601
223k
    if (cm->show_frame == 0) pbi->is_arf_frame_present = 1;
4602
223k
    if (cm->show_frame == 0 && cm->current_frame.frame_type == KEY_FRAME)
4603
40.2k
      pbi->is_fwd_kf_present = 1;
4604
223k
    if (cm->current_frame.frame_type == S_FRAME) {
4605
159
      sframe_info->is_s_frame = 1;
4606
159
      sframe_info->is_s_frame_at_altref = cm->show_frame ? 0 : 1;
4607
159
    }
4608
223k
    if (seq_params->still_picture &&
4609
223k
        (current_frame->frame_type != KEY_FRAME || !cm->show_frame)) {
4610
167
      aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4611
167
                         "Still pictures must be coded as shown keyframes");
4612
167
    }
4613
223k
    cm->showable_frame = current_frame->frame_type != KEY_FRAME;
4614
223k
    if (cm->show_frame) {
4615
160k
      if (seq_params->decoder_model_info_present_flag &&
4616
160k
          seq_params->timing_info.equal_picture_interval == 0)
4617
1.00k
        read_temporal_point_info(cm, rb);
4618
160k
    } else {
4619
      // See if this frame can be used as show_existing_frame in future
4620
63.2k
      cm->showable_frame = aom_rb_read_bit(rb);
4621
63.2k
    }
4622
223k
    cm->cur_frame->showable_frame = cm->showable_frame;
4623
223k
    features->error_resilient_mode =
4624
223k
        frame_is_sframe(cm) ||
4625
223k
                (current_frame->frame_type == KEY_FRAME && cm->show_frame)
4626
223k
            ? 1
4627
223k
            : aom_rb_read_bit(rb);
4628
223k
  }
4629
4630
257k
  if (current_frame->frame_type == KEY_FRAME && cm->show_frame) {
4631
    /* All frames need to be marked as not valid for referencing */
4632
528k
    for (int i = 0; i < REF_FRAMES; i++) {
4633
469k
      pbi->valid_for_referencing[i] = 0;
4634
469k
    }
4635
58.6k
  }
4636
257k
  features->disable_cdf_update = aom_rb_read_bit(rb);
4637
257k
  if (seq_params->force_screen_content_tools == 2) {
4638
219k
    features->allow_screen_content_tools = aom_rb_read_bit(rb);
4639
219k
  } else {
4640
38.6k
    features->allow_screen_content_tools =
4641
38.6k
        seq_params->force_screen_content_tools;
4642
38.6k
  }
4643
4644
257k
  if (features->allow_screen_content_tools) {
4645
83.7k
    if (seq_params->force_integer_mv == 2) {
4646
66.3k
      features->cur_frame_force_integer_mv = aom_rb_read_bit(rb);
4647
66.3k
    } else {
4648
17.4k
      features->cur_frame_force_integer_mv = seq_params->force_integer_mv;
4649
17.4k
    }
4650
174k
  } else {
4651
174k
    features->cur_frame_force_integer_mv = 0;
4652
174k
  }
4653
4654
257k
  int frame_size_override_flag = 0;
4655
257k
  features->allow_intrabc = 0;
4656
257k
  features->primary_ref_frame = PRIMARY_REF_NONE;
4657
4658
257k
  if (!seq_params->reduced_still_picture_hdr) {
4659
221k
    if (seq_params->frame_id_numbers_present_flag) {
4660
29.2k
      int frame_id_length = seq_params->frame_id_length;
4661
29.2k
      int diff_len = seq_params->delta_frame_id_length;
4662
29.2k
      int prev_frame_id = 0;
4663
29.2k
      int have_prev_frame_id =
4664
29.2k
          !pbi->decoding_first_frame &&
4665
29.2k
          !(current_frame->frame_type == KEY_FRAME && cm->show_frame);
4666
29.2k
      if (have_prev_frame_id) {
4667
6.64k
        prev_frame_id = cm->current_frame_id;
4668
6.64k
      }
4669
29.2k
      cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
4670
4671
29.2k
      if (have_prev_frame_id) {
4672
6.64k
        int diff_frame_id;
4673
6.64k
        if (cm->current_frame_id > prev_frame_id) {
4674
2.66k
          diff_frame_id = cm->current_frame_id - prev_frame_id;
4675
3.97k
        } else {
4676
3.97k
          diff_frame_id =
4677
3.97k
              (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
4678
3.97k
        }
4679
        /* Check current_frame_id for conformance */
4680
6.64k
        if (prev_frame_id == cm->current_frame_id ||
4681
6.64k
            diff_frame_id >= (1 << (frame_id_length - 1))) {
4682
3.86k
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4683
3.86k
                             "Invalid value of current_frame_id");
4684
3.86k
        }
4685
6.64k
      }
4686
      /* Check if some frames need to be marked as not valid for referencing */
4687
232k
      for (int i = 0; i < REF_FRAMES; i++) {
4688
203k
        if (cm->current_frame_id - (1 << diff_len) > 0) {
4689
101k
          if (cm->ref_frame_id[i] > cm->current_frame_id ||
4690
101k
              cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
4691
52.1k
            pbi->valid_for_referencing[i] = 0;
4692
101k
        } else {
4693
101k
          if (cm->ref_frame_id[i] > cm->current_frame_id &&
4694
101k
              cm->ref_frame_id[i] < (1 << frame_id_length) +
4695
51.7k
                                        cm->current_frame_id - (1 << diff_len))
4696
21.8k
            pbi->valid_for_referencing[i] = 0;
4697
101k
        }
4698
203k
      }
4699
29.2k
    }
4700
4701
221k
    frame_size_override_flag = frame_is_sframe(cm) ? 1 : aom_rb_read_bit(rb);
4702
4703
221k
    current_frame->order_hint = aom_rb_read_literal(
4704
221k
        rb, seq_params->order_hint_info.order_hint_bits_minus_1 + 1);
4705
4706
221k
    if (seq_params->order_hint_info.enable_order_hint)
4707
198k
      current_frame->frame_number = current_frame->order_hint;
4708
4709
221k
    if (!features->error_resilient_mode && !frame_is_intra_only(cm)) {
4710
123k
      features->primary_ref_frame = aom_rb_read_literal(rb, PRIMARY_REF_BITS);
4711
123k
    }
4712
221k
  }
4713
4714
257k
  if (seq_params->decoder_model_info_present_flag) {
4715
4.07k
    pbi->buffer_removal_time_present = aom_rb_read_bit(rb);
4716
4.07k
    if (pbi->buffer_removal_time_present) {
4717
1.63k
      for (int op_num = 0;
4718
8.10k
           op_num < seq_params->operating_points_cnt_minus_1 + 1; op_num++) {
4719
6.47k
        if (seq_params->op_params[op_num].decoder_model_param_present_flag) {
4720
2.55k
          if (seq_params->operating_point_idc[op_num] == 0 ||
4721
2.55k
              (((seq_params->operating_point_idc[op_num] >>
4722
2.13k
                 cm->temporal_layer_id) &
4723
2.13k
                0x1) &&
4724
2.13k
               ((seq_params->operating_point_idc[op_num] >>
4725
1.22k
                 (cm->spatial_layer_id + 8)) &
4726
1.22k
                0x1))) {
4727
1.18k
            cm->buffer_removal_times[op_num] = aom_rb_read_unsigned_literal(
4728
1.18k
                rb, seq_params->decoder_model_info.buffer_removal_time_length);
4729
1.36k
          } else {
4730
1.36k
            cm->buffer_removal_times[op_num] = 0;
4731
1.36k
          }
4732
3.92k
        } else {
4733
3.92k
          cm->buffer_removal_times[op_num] = 0;
4734
3.92k
        }
4735
6.47k
      }
4736
1.63k
    }
4737
4.07k
  }
4738
257k
  if (current_frame->frame_type == KEY_FRAME) {
4739
98.3k
    if (!cm->show_frame) {  // unshown keyframe (forward keyframe)
4740
39.7k
      current_frame->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
4741
58.6k
    } else {  // shown keyframe
4742
58.6k
      current_frame->refresh_frame_flags = (1 << REF_FRAMES) - 1;
4743
58.6k
    }
4744
4745
786k
    for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4746
688k
      cm->remapped_ref_idx[i] = INVALID_IDX;
4747
688k
    }
4748
98.3k
    if (pbi->need_resync) {
4749
69.7k
      reset_ref_frame_map(cm);
4750
69.7k
      pbi->need_resync = 0;
4751
69.7k
    }
4752
159k
  } else {
4753
159k
    if (current_frame->frame_type == INTRA_ONLY_FRAME) {
4754
24.7k
      current_frame->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
4755
24.7k
      if (current_frame->refresh_frame_flags == 0xFF) {
4756
632
        aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
4757
632
                           "Intra only frames cannot have refresh flags 0xFF");
4758
632
      }
4759
24.7k
      if (pbi->need_resync) {
4760
21.5k
        reset_ref_frame_map(cm);
4761
21.5k
        pbi->need_resync = 0;
4762
21.5k
      }
4763
134k
    } else if (pbi->need_resync != 1) { /* Skip if need resync */
4764
100k
      current_frame->refresh_frame_flags =
4765
100k
          frame_is_sframe(cm) ? 0xFF : aom_rb_read_literal(rb, REF_FRAMES);
4766
100k
    }
4767
159k
  }
4768
4769
257k
  if (!frame_is_intra_only(cm) || current_frame->refresh_frame_flags != 0xFF) {
4770
    // Read all ref frame order hints if error_resilient_mode == 1
4771
192k
    if (features->error_resilient_mode &&
4772
192k
        seq_params->order_hint_info.enable_order_hint) {
4773
221k
      for (int ref_idx = 0; ref_idx < REF_FRAMES; ref_idx++) {
4774
        // Read order hint from bit stream
4775
196k
        unsigned int order_hint = aom_rb_read_literal(
4776
196k
            rb, seq_params->order_hint_info.order_hint_bits_minus_1 + 1);
4777
        // Get buffer
4778
196k
        RefCntBuffer *buf = cm->ref_frame_map[ref_idx];
4779
196k
        if (buf == NULL || order_hint != buf->order_hint) {
4780
182k
          if (buf != NULL) {
4781
35.6k
            lock_buffer_pool(pool);
4782
35.6k
            decrease_ref_count(buf, pool);
4783
35.6k
            unlock_buffer_pool(pool);
4784
35.6k
            cm->ref_frame_map[ref_idx] = NULL;
4785
35.6k
          }
4786
          // If no corresponding buffer exists, allocate a new buffer with all
4787
          // pixels set to neutral grey.
4788
182k
          int buf_idx = get_free_fb(cm);
4789
182k
          if (buf_idx == INVALID_IDX) {
4790
0
            aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
4791
0
                               "Unable to find free frame buffer");
4792
0
          }
4793
182k
          buf = &frame_bufs[buf_idx];
4794
182k
          lock_buffer_pool(pool);
4795
182k
          if (aom_realloc_frame_buffer(
4796
182k
                  &buf->buf, seq_params->max_frame_width,
4797
182k
                  seq_params->max_frame_height, seq_params->subsampling_x,
4798
182k
                  seq_params->subsampling_y, seq_params->use_highbitdepth,
4799
182k
                  AOM_BORDER_IN_PIXELS, features->byte_alignment,
4800
182k
                  &buf->raw_frame_buffer, pool->get_fb_cb, pool->cb_priv, false,
4801
182k
                  0)) {
4802
186
            decrease_ref_count(buf, pool);
4803
186
            unlock_buffer_pool(pool);
4804
186
            aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
4805
186
                               "Failed to allocate frame buffer");
4806
186
          }
4807
182k
          unlock_buffer_pool(pool);
4808
          // According to the specification, valid bitstreams are required to
4809
          // never use missing reference frames so the filling process for
4810
          // missing frames is not normatively defined and RefValid for missing
4811
          // frames is set to 0.
4812
4813
          // To make libaom more robust when the bitstream has been corrupted
4814
          // by the loss of some frames of data, this code adds a neutral grey
4815
          // buffer in place of missing frames, i.e.
4816
          //
4817
182k
          set_planes_to_neutral_grey(seq_params, &buf->buf, 0);
4818
          //
4819
          // and allows the frames to be used for referencing, i.e.
4820
          //
4821
182k
          pbi->valid_for_referencing[ref_idx] = 1;
4822
          //
4823
          // Please note such behavior is not normative and other decoders may
4824
          // use a different approach.
4825
182k
          cm->ref_frame_map[ref_idx] = buf;
4826
182k
          buf->order_hint = order_hint;
4827
182k
        }
4828
196k
      }
4829
24.7k
    }
4830
192k
  }
4831
4832
257k
  if (current_frame->frame_type == KEY_FRAME) {
4833
98.3k
    setup_frame_size(cm, frame_size_override_flag, rb);
4834
4835
98.3k
    if (features->allow_screen_content_tools && !av1_superres_scaled(cm))
4836
48.5k
      features->allow_intrabc = aom_rb_read_bit(rb);
4837
98.3k
    features->allow_ref_frame_mvs = 0;
4838
98.3k
    cm->prev_frame = NULL;
4839
159k
  } else {
4840
159k
    features->allow_ref_frame_mvs = 0;
4841
4842
159k
    if (current_frame->frame_type == INTRA_ONLY_FRAME) {
4843
23.9k
      cm->cur_frame->film_grain_params_present =
4844
23.9k
          seq_params->film_grain_params_present;
4845
23.9k
      setup_frame_size(cm, frame_size_override_flag, rb);
4846
23.9k
      if (features->allow_screen_content_tools && !av1_superres_scaled(cm))
4847
18.2k
        features->allow_intrabc = aom_rb_read_bit(rb);
4848
4849
135k
    } else if (pbi->need_resync != 1) { /* Skip if need resync */
4850
100k
      int frame_refs_short_signaling = 0;
4851
      // Frame refs short signaling is off when error resilient mode is on.
4852
100k
      if (seq_params->order_hint_info.enable_order_hint)
4853
100k
        frame_refs_short_signaling = aom_rb_read_bit(rb);
4854
4855
100k
      if (frame_refs_short_signaling) {
4856
        // == LAST_FRAME ==
4857
59.7k
        const int lst_ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
4858
59.7k
        const RefCntBuffer *const lst_buf = cm->ref_frame_map[lst_ref];
4859
4860
        // == GOLDEN_FRAME ==
4861
59.7k
        const int gld_ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
4862
59.7k
        const RefCntBuffer *const gld_buf = cm->ref_frame_map[gld_ref];
4863
4864
        // Most of the time, streams start with a keyframe. In that case,
4865
        // ref_frame_map will have been filled in at that point and will not
4866
        // contain any NULLs. However, streams are explicitly allowed to start
4867
        // with an intra-only frame, so long as they don't then signal a
4868
        // reference to a slot that hasn't been set yet. That's what we are
4869
        // checking here.
4870
59.7k
        if (lst_buf == NULL)
4871
38
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4872
38
                             "Inter frame requests nonexistent reference");
4873
59.7k
        if (gld_buf == NULL)
4874
35
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4875
35
                             "Inter frame requests nonexistent reference");
4876
4877
59.7k
        av1_set_frame_refs(cm, cm->remapped_ref_idx, lst_ref, gld_ref);
4878
59.7k
      }
4879
4880
801k
      for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4881
700k
        int ref = 0;
4882
700k
        if (!frame_refs_short_signaling) {
4883
285k
          ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
4884
4885
          // Most of the time, streams start with a keyframe. In that case,
4886
          // ref_frame_map will have been filled in at that point and will not
4887
          // contain any NULLs. However, streams are explicitly allowed to start
4888
          // with an intra-only frame, so long as they don't then signal a
4889
          // reference to a slot that hasn't been set yet. That's what we are
4890
          // checking here.
4891
285k
          if (cm->ref_frame_map[ref] == NULL)
4892
283
            aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4893
283
                               "Inter frame requests nonexistent reference");
4894
285k
          cm->remapped_ref_idx[i] = ref;
4895
414k
        } else {
4896
414k
          ref = cm->remapped_ref_idx[i];
4897
414k
        }
4898
        // Check valid for referencing
4899
700k
        if (pbi->valid_for_referencing[ref] == 0)
4900
109
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4901
109
                             "Reference frame not valid for referencing");
4902
4903
700k
        cm->ref_frame_sign_bias[LAST_FRAME + i] = 0;
4904
4905
700k
        if (seq_params->frame_id_numbers_present_flag) {
4906
38
          int frame_id_length = seq_params->frame_id_length;
4907
38
          int diff_len = seq_params->delta_frame_id_length;
4908
38
          int delta_frame_id_minus_1 = aom_rb_read_literal(rb, diff_len);
4909
38
          int ref_frame_id =
4910
38
              ((cm->current_frame_id - (delta_frame_id_minus_1 + 1) +
4911
38
                (1 << frame_id_length)) %
4912
38
               (1 << frame_id_length));
4913
          // Compare values derived from delta_frame_id_minus_1 and
4914
          // refresh_frame_flags.
4915
38
          if (ref_frame_id != cm->ref_frame_id[ref])
4916
26
            aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4917
26
                               "Reference buffer frame ID mismatch");
4918
38
        }
4919
700k
      }
4920
4921
100k
      if (!features->error_resilient_mode && frame_size_override_flag) {
4922
86.7k
        setup_frame_size_with_refs(cm, rb);
4923
86.7k
      } else {
4924
14.1k
        setup_frame_size(cm, frame_size_override_flag, rb);
4925
14.1k
      }
4926
4927
100k
      if (features->cur_frame_force_integer_mv) {
4928
1.79k
        features->allow_high_precision_mv = 0;
4929
99.0k
      } else {
4930
99.0k
        features->allow_high_precision_mv = aom_rb_read_bit(rb);
4931
99.0k
      }
4932
100k
      features->interp_filter = read_frame_interp_filter(rb);
4933
100k
      features->switchable_motion_mode = aom_rb_read_bit(rb);
4934
100k
    }
4935
4936
159k
    cm->prev_frame = get_primary_ref_frame_buf(cm);
4937
159k
    if (features->primary_ref_frame != PRIMARY_REF_NONE &&
4938
159k
        get_primary_ref_frame_buf(cm) == NULL) {
4939
19.5k
      aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4940
19.5k
                         "Reference frame containing this frame's initial "
4941
19.5k
                         "frame context is unavailable.");
4942
19.5k
    }
4943
4944
159k
    if (!(current_frame->frame_type == INTRA_ONLY_FRAME) &&
4945
159k
        pbi->need_resync != 1) {
4946
95.6k
      if (frame_might_allow_ref_frame_mvs(cm))
4947
91.0k
        features->allow_ref_frame_mvs = aom_rb_read_bit(rb);
4948
4.66k
      else
4949
4.66k
        features->allow_ref_frame_mvs = 0;
4950
4951
746k
      for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
4952
651k
        const RefCntBuffer *const ref_buf = get_ref_frame_buf(cm, i);
4953
651k
        struct scale_factors *const ref_scale_factors =
4954
651k
            get_ref_scale_factors(cm, i);
4955
651k
        av1_setup_scale_factors_for_frame(
4956
651k
            ref_scale_factors, ref_buf->buf.y_crop_width,
4957
651k
            ref_buf->buf.y_crop_height, cm->width, cm->height);
4958
651k
        if ((!av1_is_valid_scale(ref_scale_factors)))
4959
4.49k
          aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
4960
4.49k
                             "Reference frame has invalid dimensions");
4961
651k
      }
4962
95.6k
    }
4963
159k
  }
4964
4965
257k
  av1_setup_frame_buf_refs(cm);
4966
4967
257k
  av1_setup_frame_sign_bias(cm);
4968
4969
257k
  cm->cur_frame->frame_type = current_frame->frame_type;
4970
4971
257k
  update_ref_frame_id(pbi);
4972
4973
257k
  const int might_bwd_adapt = !(seq_params->reduced_still_picture_hdr) &&
4974
257k
                              !(features->disable_cdf_update);
4975
257k
  if (might_bwd_adapt) {
4976
162k
    features->refresh_frame_context = aom_rb_read_bit(rb)
4977
162k
                                          ? REFRESH_FRAME_CONTEXT_DISABLED
4978
162k
                                          : REFRESH_FRAME_CONTEXT_BACKWARD;
4979
162k
  } else {
4980
95.5k
    features->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
4981
95.5k
  }
4982
4983
257k
  cm->cur_frame->buf.bit_depth = seq_params->bit_depth;
4984
257k
  cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
4985
257k
  cm->cur_frame->buf.transfer_characteristics =
4986
257k
      seq_params->transfer_characteristics;
4987
257k
  cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
4988
257k
  cm->cur_frame->buf.monochrome = seq_params->monochrome;
4989
257k
  cm->cur_frame->buf.chroma_sample_position =
4990
257k
      seq_params->chroma_sample_position;
4991
257k
  cm->cur_frame->buf.color_range = seq_params->color_range;
4992
257k
  cm->cur_frame->buf.render_width = cm->render_width;
4993
257k
  cm->cur_frame->buf.render_height = cm->render_height;
4994
4995
257k
  if (pbi->need_resync) {
4996
8.09k
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4997
8.09k
                       "Keyframe / intra-only frame required to reset decoder"
4998
8.09k
                       " state");
4999
8.09k
  }
5000
5001
257k
  if (features->allow_intrabc) {
5002
    // Set parameters corresponding to no filtering.
5003
35.4k
    struct loopfilter *lf = &cm->lf;
5004
35.4k
    lf->filter_level[0] = 0;
5005
35.4k
    lf->filter_level[1] = 0;
5006
35.4k
    cm->cdef_info.cdef_bits = 0;
5007
35.4k
    cm->cdef_info.cdef_strengths[0] = 0;
5008
35.4k
    cm->cdef_info.nb_cdef_strengths = 1;
5009
35.4k
    cm->cdef_info.cdef_uv_strengths[0] = 0;
5010
35.4k
    cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5011
35.4k
    cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5012
35.4k
    cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
5013
35.4k
  }
5014
5015
257k
  read_tile_info(pbi, rb);
5016
257k
  if (!av1_is_min_tile_width_satisfied(cm)) {
5017
357
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
5018
357
                       "Minimum tile width requirement not satisfied");
5019
357
  }
5020
5021
257k
  CommonQuantParams *const quant_params = &cm->quant_params;
5022
257k
  setup_quantization(quant_params, av1_num_planes(cm),
5023
257k
                     cm->seq_params->separate_uv_delta_q, rb);
5024
257k
  xd->bd = (int)seq_params->bit_depth;
5025
5026
257k
  CommonContexts *const above_contexts = &cm->above_contexts;
5027
257k
  if (above_contexts->num_planes < av1_num_planes(cm) ||
5028
257k
      above_contexts->num_mi_cols < cm->mi_params.mi_cols ||
5029
257k
      above_contexts->num_tile_rows < cm->tiles.rows) {
5030
18.3k
    av1_free_above_context_buffers(above_contexts);
5031
18.3k
    if (av1_alloc_above_context_buffers(above_contexts, cm->tiles.rows,
5032
18.3k
                                        cm->mi_params.mi_cols,
5033
18.3k
                                        av1_num_planes(cm))) {
5034
0
      aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
5035
0
                         "Failed to allocate context buffers");
5036
0
    }
5037
18.3k
  }
5038
5039
257k
  if (features->primary_ref_frame == PRIMARY_REF_NONE) {
5040
122k
    av1_setup_past_independence(cm);
5041
122k
  }
5042
5043
257k
  setup_segmentation(cm, rb);
5044
5045
257k
  cm->delta_q_info.delta_q_res = 1;
5046
257k
  cm->delta_q_info.delta_lf_res = 1;
5047
257k
  cm->delta_q_info.delta_lf_present_flag = 0;
5048
257k
  cm->delta_q_info.delta_lf_multi = 0;
5049
257k
  cm->delta_q_info.delta_q_present_flag =
5050
257k
      quant_params->base_qindex > 0 ? aom_rb_read_bit(rb) : 0;
5051
257k
  if (cm->delta_q_info.delta_q_present_flag) {
5052
18.9k
    xd->current_base_qindex = quant_params->base_qindex;
5053
18.9k
    cm->delta_q_info.delta_q_res = 1 << aom_rb_read_literal(rb, 2);
5054
18.9k
    if (!features->allow_intrabc)
5055
16.2k
      cm->delta_q_info.delta_lf_present_flag = aom_rb_read_bit(rb);
5056
18.9k
    if (cm->delta_q_info.delta_lf_present_flag) {
5057
9.11k
      cm->delta_q_info.delta_lf_res = 1 << aom_rb_read_literal(rb, 2);
5058
9.11k
      cm->delta_q_info.delta_lf_multi = aom_rb_read_bit(rb);
5059
9.11k
      av1_reset_loop_filter_delta(xd, av1_num_planes(cm));
5060
9.11k
    }
5061
18.9k
  }
5062
5063
257k
  xd->cur_frame_force_integer_mv = features->cur_frame_force_integer_mv;
5064
5065
1.90M
  for (int i = 0; i < MAX_SEGMENTS; ++i) {
5066
1.65M
    const int qindex = av1_get_qindex(&cm->seg, i, quant_params->base_qindex);
5067
1.65M
    xd->lossless[i] =
5068
1.65M
        qindex == 0 && quant_params->y_dc_delta_q == 0 &&
5069
1.65M
        quant_params->u_dc_delta_q == 0 && quant_params->u_ac_delta_q == 0 &&
5070
1.65M
        quant_params->v_dc_delta_q == 0 && quant_params->v_ac_delta_q == 0;
5071
1.65M
    xd->qindex[i] = qindex;
5072
1.65M
  }
5073
257k
  features->coded_lossless = is_coded_lossless(cm, xd);
5074
257k
  features->all_lossless = features->coded_lossless && !av1_superres_scaled(cm);
5075
257k
  setup_segmentation_dequant(cm, xd);
5076
257k
  if (features->coded_lossless) {
5077
23.5k
    cm->lf.filter_level[0] = 0;
5078
23.5k
    cm->lf.filter_level[1] = 0;
5079
23.5k
  }
5080
257k
  if (features->coded_lossless || !seq_params->enable_cdef) {
5081
87.2k
    cm->cdef_info.cdef_bits = 0;
5082
87.2k
    cm->cdef_info.cdef_strengths[0] = 0;
5083
87.2k
    cm->cdef_info.cdef_uv_strengths[0] = 0;
5084
87.2k
  }
5085
257k
  if (features->all_lossless || !seq_params->enable_restoration) {
5086
75.9k
    cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5087
75.9k
    cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5088
75.9k
    cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
5089
75.9k
  }
5090
257k
  setup_loopfilter(cm, rb);
5091
5092
257k
  if (!features->coded_lossless && seq_params->enable_cdef) {
5093
119k
    setup_cdef(cm, rb);
5094
119k
  }
5095
257k
  if (!features->all_lossless && seq_params->enable_restoration) {
5096
129k
    decode_restoration_mode(cm, rb);
5097
129k
  }
5098
5099
257k
  features->tx_mode = read_tx_mode(rb, features->coded_lossless);
5100
257k
  current_frame->reference_mode = read_frame_reference_mode(cm, rb);
5101
5102
257k
  av1_setup_skip_mode_allowed(cm);
5103
257k
  current_frame->skip_mode_info.skip_mode_flag =
5104
257k
      current_frame->skip_mode_info.skip_mode_allowed ? aom_rb_read_bit(rb) : 0;
5105
5106
257k
  if (frame_might_allow_warped_motion(cm))
5107
80.7k
    features->allow_warped_motion = aom_rb_read_bit(rb);
5108
177k
  else
5109
177k
    features->allow_warped_motion = 0;
5110
5111
257k
  features->reduced_tx_set_used = aom_rb_read_bit(rb);
5112
5113
257k
  if (features->allow_ref_frame_mvs && !frame_might_allow_ref_frame_mvs(cm)) {
5114
0
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
5115
0
                       "Frame wrongly requests reference frame MVs");
5116
0
  }
5117
5118
257k
  if (!frame_is_intra_only(cm)) read_global_motion(cm, rb);
5119
5120
257k
  cm->cur_frame->film_grain_params_present =
5121
257k
      seq_params->film_grain_params_present;
5122
257k
  read_film_grain(cm, rb);
5123
5124
257k
#if EXT_TILE_DEBUG
5125
257k
  if (pbi->ext_tile_debug && cm->tiles.large_scale) {
5126
13.5k
    read_ext_tile_info(pbi, rb);
5127
13.5k
    av1_set_single_tile_decoding_mode(cm);
5128
13.5k
  }
5129
257k
#endif  // EXT_TILE_DEBUG
5130
257k
  return 0;
5131
266k
}
5132
5133
struct aom_read_bit_buffer *av1_init_read_bit_buffer(
5134
    AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
5135
534k
    const uint8_t *data_end) {
5136
534k
  rb->bit_offset = 0;
5137
534k
  rb->error_handler = error_handler;
5138
534k
  rb->error_handler_data = &pbi->common;
5139
534k
  rb->bit_buffer = data;
5140
534k
  rb->bit_buffer_end = data_end;
5141
534k
  return rb;
5142
534k
}
5143
5144
void av1_read_frame_size(struct aom_read_bit_buffer *rb, int num_bits_width,
5145
84.6k
                         int num_bits_height, int *width, int *height) {
5146
84.6k
  *width = aom_rb_read_literal(rb, num_bits_width) + 1;
5147
84.6k
  *height = aom_rb_read_literal(rb, num_bits_height) + 1;
5148
84.6k
}
5149
5150
188k
BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
5151
188k
  int profile = aom_rb_read_literal(rb, PROFILE_BITS);
5152
188k
  return (BITSTREAM_PROFILE)profile;
5153
188k
}
5154
5155
42.1k
static AOM_INLINE void superres_post_decode(AV1Decoder *pbi) {
5156
42.1k
  AV1_COMMON *const cm = &pbi->common;
5157
42.1k
  BufferPool *const pool = cm->buffer_pool;
5158
5159
42.1k
  if (!av1_superres_scaled(cm)) return;
5160
10.3k
  assert(!cm->features.all_lossless);
5161
5162
10.3k
  av1_superres_upscale(cm, pool, 0);
5163
10.3k
}
5164
5165
uint32_t av1_decode_frame_headers_and_setup(AV1Decoder *pbi,
5166
                                            struct aom_read_bit_buffer *rb,
5167
266k
                                            int trailing_bits_present) {
5168
266k
  AV1_COMMON *const cm = &pbi->common;
5169
266k
  const int num_planes = av1_num_planes(cm);
5170
266k
  MACROBLOCKD *const xd = &pbi->dcb.xd;
5171
5172
#if CONFIG_BITSTREAM_DEBUG
5173
  if (cm->seq_params->order_hint_info.enable_order_hint) {
5174
    aom_bitstream_queue_set_frame_read(cm->current_frame.order_hint * 2 +
5175
                                       cm->show_frame);
5176
  } else {
5177
    // This is currently used in RTC encoding. cm->show_frame is always 1.
5178
    assert(cm->show_frame);
5179
    aom_bitstream_queue_set_frame_read(cm->current_frame.frame_number);
5180
  }
5181
#endif
5182
#if CONFIG_MISMATCH_DEBUG
5183
  mismatch_move_frame_idx_r();
5184
#endif
5185
5186
2.12M
  for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
5187
1.86M
    cm->global_motion[i] = default_warp_params;
5188
1.86M
    cm->cur_frame->global_motion[i] = default_warp_params;
5189
1.86M
  }
5190
266k
  xd->global_motion = cm->global_motion;
5191
5192
266k
  read_uncompressed_header(pbi, rb);
5193
5194
266k
  if (trailing_bits_present) av1_check_trailing_bits(pbi, rb);
5195
5196
266k
  if (!cm->tiles.single_tile_decoding &&
5197
266k
      (pbi->dec_tile_row >= 0 || pbi->dec_tile_col >= 0)) {
5198
0
    pbi->dec_tile_row = -1;
5199
0
    pbi->dec_tile_col = -1;
5200
0
  }
5201
5202
266k
  const uint32_t uncomp_hdr_size =
5203
266k
      (uint32_t)aom_rb_bytes_read(rb);  // Size of the uncompressed header
5204
266k
  YV12_BUFFER_CONFIG *new_fb = &cm->cur_frame->buf;
5205
266k
  xd->cur_buf = new_fb;
5206
266k
  if (av1_allow_intrabc(cm)) {
5207
34.8k
    av1_setup_scale_factors_for_frame(
5208
34.8k
        &cm->sf_identity, xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height,
5209
34.8k
        xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height);
5210
34.8k
  }
5211
5212
  // Showing a frame directly.
5213
266k
  if (cm->show_existing_frame) {
5214
789
    if (pbi->reset_decoder_state) {
5215
      // Use the default frame context values.
5216
167
      *cm->fc = *cm->default_frame_context;
5217
167
      if (!cm->fc->initialized)
5218
0
        aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
5219
0
                           "Uninitialized entropy context.");
5220
167
    }
5221
789
    return uncomp_hdr_size;
5222
789
  }
5223
5224
265k
  cm->mi_params.setup_mi(&cm->mi_params);
5225
5226
265k
  av1_calculate_ref_frame_side(cm);
5227
265k
  if (cm->features.allow_ref_frame_mvs) av1_setup_motion_field(cm);
5228
5229
265k
  av1_setup_block_planes(xd, cm->seq_params->subsampling_x,
5230
265k
                         cm->seq_params->subsampling_y, num_planes);
5231
265k
  if (cm->features.primary_ref_frame == PRIMARY_REF_NONE) {
5232
    // use the default frame context values
5233
115k
    *cm->fc = *cm->default_frame_context;
5234
149k
  } else {
5235
149k
    *cm->fc = get_primary_ref_frame_buf(cm)->frame_context;
5236
149k
  }
5237
265k
  if (!cm->fc->initialized)
5238
40
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
5239
40
                       "Uninitialized entropy context.");
5240
5241
265k
  pbi->dcb.corrupted = 0;
5242
265k
  return uncomp_hdr_size;
5243
266k
}
5244
5245
// Once-per-frame initialization
5246
189k
static AOM_INLINE void setup_frame_info(AV1Decoder *pbi) {
5247
189k
  AV1_COMMON *const cm = &pbi->common;
5248
5249
189k
  if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
5250
189k
      cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
5251
189k
      cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
5252
26.3k
    av1_alloc_restoration_buffers(cm, /*is_sgr_enabled =*/true);
5253
101k
    for (int p = 0; p < av1_num_planes(cm); p++) {
5254
74.8k
      av1_alloc_restoration_struct(cm, &cm->rst_info[p], p > 0);
5255
74.8k
    }
5256
26.3k
  }
5257
5258
189k
  const int use_highbd = cm->seq_params->use_highbitdepth;
5259
189k
  const int buf_size = MC_TEMP_BUF_PELS << use_highbd;
5260
189k
  if (pbi->td.mc_buf_size != buf_size) {
5261
15.6k
    av1_free_mc_tmp_buf(&pbi->td);
5262
15.6k
    allocate_mc_tmp_buf(cm, &pbi->td, buf_size, use_highbd);
5263
15.6k
  }
5264
189k
}
5265
5266
void av1_decode_tg_tiles_and_wrapup(AV1Decoder *pbi, const uint8_t *data,
5267
                                    const uint8_t *data_end,
5268
                                    const uint8_t **p_data_end, int start_tile,
5269
189k
                                    int end_tile, int initialize_flag) {
5270
189k
  AV1_COMMON *const cm = &pbi->common;
5271
189k
  CommonTileParams *const tiles = &cm->tiles;
5272
189k
  MACROBLOCKD *const xd = &pbi->dcb.xd;
5273
189k
  const int tile_count_tg = end_tile - start_tile + 1;
5274
5275
189k
  xd->error_info = cm->error;
5276
189k
  if (initialize_flag) setup_frame_info(pbi);
5277
189k
  const int num_planes = av1_num_planes(cm);
5278
5279
189k
  if (pbi->max_threads > 1 && !(tiles->large_scale && !pbi->ext_tile_debug) &&
5280
189k
      pbi->row_mt)
5281
124k
    *p_data_end =
5282
124k
        decode_tiles_row_mt(pbi, data, data_end, start_tile, end_tile);
5283
65.5k
  else if (pbi->max_threads > 1 && tile_count_tg > 1 &&
5284
65.5k
           !(tiles->large_scale && !pbi->ext_tile_debug))
5285
0
    *p_data_end = decode_tiles_mt(pbi, data, data_end, start_tile, end_tile);
5286
65.5k
  else
5287
65.5k
    *p_data_end = decode_tiles(pbi, data, data_end, start_tile, end_tile);
5288
5289
  // If the bit stream is monochrome, set the U and V buffers to a constant.
5290
189k
  if (num_planes < 3) {
5291
5.39k
    set_planes_to_neutral_grey(cm->seq_params, xd->cur_buf, 1);
5292
5.39k
  }
5293
5294
189k
  if (end_tile != tiles->rows * tiles->cols - 1) {
5295
27
    return;
5296
27
  }
5297
5298
189k
  av1_alloc_cdef_buffers(cm, &pbi->cdef_worker, &pbi->cdef_sync,
5299
189k
                         pbi->num_workers, 1);
5300
189k
  av1_alloc_cdef_sync(cm, &pbi->cdef_sync, pbi->num_workers);
5301
5302
189k
  if (!cm->features.allow_intrabc && !tiles->single_tile_decoding) {
5303
126k
    if (cm->lf.filter_level[0] || cm->lf.filter_level[1]) {
5304
35.5k
      av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, &pbi->dcb.xd, 0,
5305
35.5k
                               num_planes, 0, pbi->tile_workers,
5306
35.5k
                               pbi->num_workers, &pbi->lf_row_sync, 0);
5307
35.5k
    }
5308
5309
126k
    const int do_cdef =
5310
126k
        !pbi->skip_loop_filter && !cm->features.coded_lossless &&
5311
126k
        (cm->cdef_info.cdef_bits || cm->cdef_info.cdef_strengths[0] ||
5312
114k
         cm->cdef_info.cdef_uv_strengths[0]);
5313
126k
    const int do_superres = av1_superres_scaled(cm);
5314
126k
    const int optimized_loop_restoration = !do_cdef && !do_superres;
5315
126k
    const int do_loop_restoration =
5316
126k
        cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
5317
126k
        cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
5318
126k
        cm->rst_info[2].frame_restoration_type != RESTORE_NONE;
5319
    // Frame border extension is not required in the decoder
5320
    // as it happens in extend_mc_border().
5321
126k
    int do_extend_border_mt = 0;
5322
126k
    if (!optimized_loop_restoration) {
5323
42.1k
      if (do_loop_restoration)
5324
17.3k
        av1_loop_restoration_save_boundary_lines(&pbi->common.cur_frame->buf,
5325
17.3k
                                                 cm, 0);
5326
5327
42.1k
      if (do_cdef) {
5328
34.5k
        if (pbi->num_workers > 1) {
5329
28.1k
          av1_cdef_frame_mt(cm, &pbi->dcb.xd, pbi->cdef_worker,
5330
28.1k
                            pbi->tile_workers, &pbi->cdef_sync,
5331
28.1k
                            pbi->num_workers, av1_cdef_init_fb_row_mt,
5332
28.1k
                            do_extend_border_mt);
5333
28.1k
        } else {
5334
6.42k
          av1_cdef_frame(&pbi->common.cur_frame->buf, cm, &pbi->dcb.xd,
5335
6.42k
                         av1_cdef_init_fb_row);
5336
6.42k
        }
5337
34.5k
      }
5338
5339
42.1k
      superres_post_decode(pbi);
5340
5341
42.1k
      if (do_loop_restoration) {
5342
17.3k
        av1_loop_restoration_save_boundary_lines(&pbi->common.cur_frame->buf,
5343
17.3k
                                                 cm, 1);
5344
17.3k
        if (pbi->num_workers > 1) {
5345
12.0k
          av1_loop_restoration_filter_frame_mt(
5346
12.0k
              (YV12_BUFFER_CONFIG *)xd->cur_buf, cm, optimized_loop_restoration,
5347
12.0k
              pbi->tile_workers, pbi->num_workers, &pbi->lr_row_sync,
5348
12.0k
              &pbi->lr_ctxt, do_extend_border_mt);
5349
12.0k
        } else {
5350
5.28k
          av1_loop_restoration_filter_frame((YV12_BUFFER_CONFIG *)xd->cur_buf,
5351
5.28k
                                            cm, optimized_loop_restoration,
5352
5.28k
                                            &pbi->lr_ctxt);
5353
5.28k
        }
5354
17.3k
      }
5355
84.6k
    } else {
5356
      // In no cdef and no superres case. Provide an optimized version of
5357
      // loop_restoration_filter.
5358
84.6k
      if (do_loop_restoration) {
5359
3.94k
        if (pbi->num_workers > 1) {
5360
1.77k
          av1_loop_restoration_filter_frame_mt(
5361
1.77k
              (YV12_BUFFER_CONFIG *)xd->cur_buf, cm, optimized_loop_restoration,
5362
1.77k
              pbi->tile_workers, pbi->num_workers, &pbi->lr_row_sync,
5363
1.77k
              &pbi->lr_ctxt, do_extend_border_mt);
5364
2.17k
        } else {
5365
2.17k
          av1_loop_restoration_filter_frame((YV12_BUFFER_CONFIG *)xd->cur_buf,
5366
2.17k
                                            cm, optimized_loop_restoration,
5367
2.17k
                                            &pbi->lr_ctxt);
5368
2.17k
        }
5369
3.94k
      }
5370
84.6k
    }
5371
126k
  }
5372
5373
189k
  if (!pbi->dcb.corrupted) {
5374
135k
    if (cm->features.refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
5375
26.5k
      assert(pbi->context_update_tile_id < pbi->allocated_tiles);
5376
26.5k
      *cm->fc = pbi->tile_data[pbi->context_update_tile_id].tctx;
5377
26.5k
      av1_reset_cdf_symbol_counters(cm->fc);
5378
26.5k
    }
5379
135k
  } else {
5380
54.6k
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
5381
54.6k
                       "Decode failed. Frame data is corrupted.");
5382
54.6k
  }
5383
5384
#if CONFIG_INSPECTION
5385
  if (pbi->inspect_cb != NULL) {
5386
    (*pbi->inspect_cb)(pbi, pbi->inspect_ctx);
5387
  }
5388
#endif
5389
5390
  // Non frame parallel update frame context here.
5391
189k
  if (!tiles->large_scale) {
5392
129k
    cm->cur_frame->frame_context = *cm->fc;
5393
129k
  }
5394
5395
189k
  if (cm->show_frame && !cm->seq_params->order_hint_info.enable_order_hint) {
5396
9.23k
    ++cm->current_frame.frame_number;
5397
9.23k
  }
5398
189k
}