Coverage Report

Created: 2025-09-08 07:52

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