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