Coverage Report

Created: 2026-02-14 07:00

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