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