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