xref: /aosp_15_r20/external/libaom/av1/decoder/decoder.h (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 #ifndef AOM_AV1_DECODER_DECODER_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AV1_DECODER_DECODER_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
16*77c1e3ccSAndroid Build Coastguard Worker 
17*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_codec.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/bitreader.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "aom_scale/yv12config.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "aom_util/aom_thread.h"
21*77c1e3ccSAndroid Build Coastguard Worker 
22*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/av1_common_int.h"
23*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/thread_common.h"
24*77c1e3ccSAndroid Build Coastguard Worker #include "av1/decoder/dthread.h"
25*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ACCOUNTING
26*77c1e3ccSAndroid Build Coastguard Worker #include "av1/decoder/accounting.h"
27*77c1e3ccSAndroid Build Coastguard Worker #endif
28*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INSPECTION
29*77c1e3ccSAndroid Build Coastguard Worker #include "av1/decoder/inspection.h"
30*77c1e3ccSAndroid Build Coastguard Worker #endif
31*77c1e3ccSAndroid Build Coastguard Worker 
32*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
33*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
34*77c1e3ccSAndroid Build Coastguard Worker #endif
35*77c1e3ccSAndroid Build Coastguard Worker 
36*77c1e3ccSAndroid Build Coastguard Worker /*!
37*77c1e3ccSAndroid Build Coastguard Worker  * \brief Contains coding block data required by the decoder.
38*77c1e3ccSAndroid Build Coastguard Worker  *
39*77c1e3ccSAndroid Build Coastguard Worker  * This includes:
40*77c1e3ccSAndroid Build Coastguard Worker  * - Coding block info that is common between encoder and decoder.
41*77c1e3ccSAndroid Build Coastguard Worker  * - Other coding block info only needed by the decoder.
42*77c1e3ccSAndroid Build Coastguard Worker  * Contrast this with a similar struct MACROBLOCK on encoder side.
43*77c1e3ccSAndroid Build Coastguard Worker  * This data is also common between ThreadData and AV1Decoder structs.
44*77c1e3ccSAndroid Build Coastguard Worker  */
45*77c1e3ccSAndroid Build Coastguard Worker typedef struct DecoderCodingBlock {
46*77c1e3ccSAndroid Build Coastguard Worker   /*!
47*77c1e3ccSAndroid Build Coastguard Worker    * Coding block info that is common between encoder and decoder.
48*77c1e3ccSAndroid Build Coastguard Worker    */
49*77c1e3ccSAndroid Build Coastguard Worker   DECLARE_ALIGNED(32, MACROBLOCKD, xd);
50*77c1e3ccSAndroid Build Coastguard Worker   /*!
51*77c1e3ccSAndroid Build Coastguard Worker    * True if the at least one of the coding blocks decoded was corrupted.
52*77c1e3ccSAndroid Build Coastguard Worker    */
53*77c1e3ccSAndroid Build Coastguard Worker   int corrupted;
54*77c1e3ccSAndroid Build Coastguard Worker   /*!
55*77c1e3ccSAndroid Build Coastguard Worker    * Pointer to 'mc_buf' inside 'pbi->td' (single-threaded decoding) or
56*77c1e3ccSAndroid Build Coastguard Worker    * 'pbi->thread_data[i].td' (multi-threaded decoding).
57*77c1e3ccSAndroid Build Coastguard Worker    */
58*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *mc_buf[2];
59*77c1e3ccSAndroid Build Coastguard Worker   /*!
60*77c1e3ccSAndroid Build Coastguard Worker    * Pointer to 'dqcoeff' inside 'td->cb_buffer_base' or 'pbi->cb_buffer_base'
61*77c1e3ccSAndroid Build Coastguard Worker    * with appropriate offset for the current superblock, for each plane.
62*77c1e3ccSAndroid Build Coastguard Worker    */
63*77c1e3ccSAndroid Build Coastguard Worker   tran_low_t *dqcoeff_block[MAX_MB_PLANE];
64*77c1e3ccSAndroid Build Coastguard Worker   /*!
65*77c1e3ccSAndroid Build Coastguard Worker    * cb_offset[p] is the offset into the dqcoeff_block[p] for the current coding
66*77c1e3ccSAndroid Build Coastguard Worker    * block, for each plane 'p'.
67*77c1e3ccSAndroid Build Coastguard Worker    */
68*77c1e3ccSAndroid Build Coastguard Worker   uint16_t cb_offset[MAX_MB_PLANE];
69*77c1e3ccSAndroid Build Coastguard Worker   /*!
70*77c1e3ccSAndroid Build Coastguard Worker    * Pointer to 'eob_data' inside 'td->cb_buffer_base' or 'pbi->cb_buffer_base'
71*77c1e3ccSAndroid Build Coastguard Worker    * with appropriate offset for the current superblock, for each plane.
72*77c1e3ccSAndroid Build Coastguard Worker    */
73*77c1e3ccSAndroid Build Coastguard Worker   eob_info *eob_data[MAX_MB_PLANE];
74*77c1e3ccSAndroid Build Coastguard Worker   /*!
75*77c1e3ccSAndroid Build Coastguard Worker    * txb_offset[p] is the offset into the eob_data[p] for the current coding
76*77c1e3ccSAndroid Build Coastguard Worker    * block, for each plane 'p'.
77*77c1e3ccSAndroid Build Coastguard Worker    */
78*77c1e3ccSAndroid Build Coastguard Worker   uint16_t txb_offset[MAX_MB_PLANE];
79*77c1e3ccSAndroid Build Coastguard Worker   /*!
80*77c1e3ccSAndroid Build Coastguard Worker    * ref_mv_count[i] specifies the number of number of motion vector candidates
81*77c1e3ccSAndroid Build Coastguard Worker    * in xd->ref_mv_stack[i].
82*77c1e3ccSAndroid Build Coastguard Worker    */
83*77c1e3ccSAndroid Build Coastguard Worker   uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
84*77c1e3ccSAndroid Build Coastguard Worker } DecoderCodingBlock;
85*77c1e3ccSAndroid Build Coastguard Worker 
86*77c1e3ccSAndroid Build Coastguard Worker /*!\cond */
87*77c1e3ccSAndroid Build Coastguard Worker 
88*77c1e3ccSAndroid Build Coastguard Worker typedef void (*decode_block_visitor_fn_t)(const AV1_COMMON *const cm,
89*77c1e3ccSAndroid Build Coastguard Worker                                           DecoderCodingBlock *dcb,
90*77c1e3ccSAndroid Build Coastguard Worker                                           aom_reader *const r, const int plane,
91*77c1e3ccSAndroid Build Coastguard Worker                                           const int row, const int col,
92*77c1e3ccSAndroid Build Coastguard Worker                                           const TX_SIZE tx_size);
93*77c1e3ccSAndroid Build Coastguard Worker 
94*77c1e3ccSAndroid Build Coastguard Worker typedef void (*predict_inter_block_visitor_fn_t)(AV1_COMMON *const cm,
95*77c1e3ccSAndroid Build Coastguard Worker                                                  DecoderCodingBlock *dcb,
96*77c1e3ccSAndroid Build Coastguard Worker                                                  BLOCK_SIZE bsize);
97*77c1e3ccSAndroid Build Coastguard Worker 
98*77c1e3ccSAndroid Build Coastguard Worker typedef void (*cfl_store_inter_block_visitor_fn_t)(AV1_COMMON *const cm,
99*77c1e3ccSAndroid Build Coastguard Worker                                                    MACROBLOCKD *const xd);
100*77c1e3ccSAndroid Build Coastguard Worker 
101*77c1e3ccSAndroid Build Coastguard Worker typedef struct ThreadData {
102*77c1e3ccSAndroid Build Coastguard Worker   DecoderCodingBlock dcb;
103*77c1e3ccSAndroid Build Coastguard Worker 
104*77c1e3ccSAndroid Build Coastguard Worker   // Coding block buffer for the current superblock.
105*77c1e3ccSAndroid Build Coastguard Worker   // Used only for single-threaded decoding and multi-threaded decoding with
106*77c1e3ccSAndroid Build Coastguard Worker   // row_mt == 1 cases.
107*77c1e3ccSAndroid Build Coastguard Worker   // See also: similar buffer in 'AV1Decoder'.
108*77c1e3ccSAndroid Build Coastguard Worker   CB_BUFFER cb_buffer_base;
109*77c1e3ccSAndroid Build Coastguard Worker 
110*77c1e3ccSAndroid Build Coastguard Worker   aom_reader *bit_reader;
111*77c1e3ccSAndroid Build Coastguard Worker 
112*77c1e3ccSAndroid Build Coastguard Worker   // Motion compensation buffer used to get a prediction buffer with extended
113*77c1e3ccSAndroid Build Coastguard Worker   // borders. One buffer for each of the two possible references.
114*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *mc_buf[2];
115*77c1e3ccSAndroid Build Coastguard Worker   // Mask for this block used for compound prediction.
116*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *seg_mask;
117*77c1e3ccSAndroid Build Coastguard Worker   // Allocated size of 'mc_buf'.
118*77c1e3ccSAndroid Build Coastguard Worker   int32_t mc_buf_size;
119*77c1e3ccSAndroid Build Coastguard Worker   // If true, the pointers in 'mc_buf' were converted from highbd pointers.
120*77c1e3ccSAndroid Build Coastguard Worker   int mc_buf_use_highbd;  // Boolean: whether the byte pointers stored in
121*77c1e3ccSAndroid Build Coastguard Worker                           // mc_buf were converted from highbd pointers.
122*77c1e3ccSAndroid Build Coastguard Worker 
123*77c1e3ccSAndroid Build Coastguard Worker   CONV_BUF_TYPE *tmp_conv_dst;
124*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *tmp_obmc_bufs[2];
125*77c1e3ccSAndroid Build Coastguard Worker 
126*77c1e3ccSAndroid Build Coastguard Worker   decode_block_visitor_fn_t read_coeffs_tx_intra_block_visit;
127*77c1e3ccSAndroid Build Coastguard Worker   decode_block_visitor_fn_t predict_and_recon_intra_block_visit;
128*77c1e3ccSAndroid Build Coastguard Worker   decode_block_visitor_fn_t read_coeffs_tx_inter_block_visit;
129*77c1e3ccSAndroid Build Coastguard Worker   decode_block_visitor_fn_t inverse_tx_inter_block_visit;
130*77c1e3ccSAndroid Build Coastguard Worker   predict_inter_block_visitor_fn_t predict_inter_block_visit;
131*77c1e3ccSAndroid Build Coastguard Worker   cfl_store_inter_block_visitor_fn_t cfl_store_inter_block_visit;
132*77c1e3ccSAndroid Build Coastguard Worker } ThreadData;
133*77c1e3ccSAndroid Build Coastguard Worker 
134*77c1e3ccSAndroid Build Coastguard Worker typedef struct AV1DecRowMTJobInfo {
135*77c1e3ccSAndroid Build Coastguard Worker   int tile_row;
136*77c1e3ccSAndroid Build Coastguard Worker   int tile_col;
137*77c1e3ccSAndroid Build Coastguard Worker   int mi_row;
138*77c1e3ccSAndroid Build Coastguard Worker } AV1DecRowMTJobInfo;
139*77c1e3ccSAndroid Build Coastguard Worker 
140*77c1e3ccSAndroid Build Coastguard Worker typedef struct AV1DecRowMTSyncData {
141*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
142*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *mutex_;
143*77c1e3ccSAndroid Build Coastguard Worker   pthread_cond_t *cond_;
144*77c1e3ccSAndroid Build Coastguard Worker #endif
145*77c1e3ccSAndroid Build Coastguard Worker   int allocated_sb_rows;
146*77c1e3ccSAndroid Build Coastguard Worker   int *cur_sb_col;
147*77c1e3ccSAndroid Build Coastguard Worker   // Denotes the superblock interval at which conditional signalling should
148*77c1e3ccSAndroid Build Coastguard Worker   // happen. Also denotes the minimum number of extra superblocks of the top row
149*77c1e3ccSAndroid Build Coastguard Worker   // to be complete to start decoding the current superblock. A value of 1
150*77c1e3ccSAndroid Build Coastguard Worker   // indicates top-right dependency.
151*77c1e3ccSAndroid Build Coastguard Worker   int sync_range;
152*77c1e3ccSAndroid Build Coastguard Worker   // Denotes the additional number of superblocks in the previous row to be
153*77c1e3ccSAndroid Build Coastguard Worker   // complete to start decoding the current superblock when intraBC tool is
154*77c1e3ccSAndroid Build Coastguard Worker   // enabled. This additional top-right delay is required to satisfy the
155*77c1e3ccSAndroid Build Coastguard Worker   // hardware constraints for intraBC tool when row multithreading is enabled.
156*77c1e3ccSAndroid Build Coastguard Worker   int intrabc_extra_top_right_sb_delay;
157*77c1e3ccSAndroid Build Coastguard Worker   int mi_rows;
158*77c1e3ccSAndroid Build Coastguard Worker   int mi_cols;
159*77c1e3ccSAndroid Build Coastguard Worker   int mi_rows_parse_done;
160*77c1e3ccSAndroid Build Coastguard Worker   int mi_rows_decode_started;
161*77c1e3ccSAndroid Build Coastguard Worker   int num_threads_working;
162*77c1e3ccSAndroid Build Coastguard Worker } AV1DecRowMTSync;
163*77c1e3ccSAndroid Build Coastguard Worker 
164*77c1e3ccSAndroid Build Coastguard Worker typedef struct AV1DecRowMTInfo {
165*77c1e3ccSAndroid Build Coastguard Worker   int tile_rows_start;
166*77c1e3ccSAndroid Build Coastguard Worker   int tile_rows_end;
167*77c1e3ccSAndroid Build Coastguard Worker   int tile_cols_start;
168*77c1e3ccSAndroid Build Coastguard Worker   int tile_cols_end;
169*77c1e3ccSAndroid Build Coastguard Worker   int start_tile;
170*77c1e3ccSAndroid Build Coastguard Worker   int end_tile;
171*77c1e3ccSAndroid Build Coastguard Worker   int mi_rows_to_decode;
172*77c1e3ccSAndroid Build Coastguard Worker 
173*77c1e3ccSAndroid Build Coastguard Worker   // Invariant:
174*77c1e3ccSAndroid Build Coastguard Worker   //   mi_rows_parse_done >= mi_rows_decode_started.
175*77c1e3ccSAndroid Build Coastguard Worker   // mi_rows_parse_done and mi_rows_decode_started are both initialized to 0.
176*77c1e3ccSAndroid Build Coastguard Worker   // mi_rows_parse_done is incremented freely. mi_rows_decode_started may only
177*77c1e3ccSAndroid Build Coastguard Worker   // be incremented to catch up with mi_rows_parse_done but is not allowed to
178*77c1e3ccSAndroid Build Coastguard Worker   // surpass mi_rows_parse_done.
179*77c1e3ccSAndroid Build Coastguard Worker   //
180*77c1e3ccSAndroid Build Coastguard Worker   // When mi_rows_decode_started reaches mi_rows_to_decode, there are no more
181*77c1e3ccSAndroid Build Coastguard Worker   // decode jobs.
182*77c1e3ccSAndroid Build Coastguard Worker 
183*77c1e3ccSAndroid Build Coastguard Worker   // Indicates the progress of the bit-stream parsing of superblocks.
184*77c1e3ccSAndroid Build Coastguard Worker   // Initialized to 0. Incremented by sb_mi_size when parse sb row is done.
185*77c1e3ccSAndroid Build Coastguard Worker   int mi_rows_parse_done;
186*77c1e3ccSAndroid Build Coastguard Worker   // Indicates the progress of the decoding of superblocks.
187*77c1e3ccSAndroid Build Coastguard Worker   // Initialized to 0. Incremented by sb_mi_size when decode sb row is started.
188*77c1e3ccSAndroid Build Coastguard Worker   int mi_rows_decode_started;
189*77c1e3ccSAndroid Build Coastguard Worker   // Boolean: Initialized to 0 (false). Set to 1 (true) on error to abort
190*77c1e3ccSAndroid Build Coastguard Worker   // decoding.
191*77c1e3ccSAndroid Build Coastguard Worker   int row_mt_exit;
192*77c1e3ccSAndroid Build Coastguard Worker } AV1DecRowMTInfo;
193*77c1e3ccSAndroid Build Coastguard Worker 
194*77c1e3ccSAndroid Build Coastguard Worker typedef struct TileDataDec {
195*77c1e3ccSAndroid Build Coastguard Worker   TileInfo tile_info;
196*77c1e3ccSAndroid Build Coastguard Worker   aom_reader bit_reader;
197*77c1e3ccSAndroid Build Coastguard Worker   DECLARE_ALIGNED(16, FRAME_CONTEXT, tctx);
198*77c1e3ccSAndroid Build Coastguard Worker   AV1DecRowMTSync dec_row_mt_sync;
199*77c1e3ccSAndroid Build Coastguard Worker } TileDataDec;
200*77c1e3ccSAndroid Build Coastguard Worker 
201*77c1e3ccSAndroid Build Coastguard Worker typedef struct TileBufferDec {
202*77c1e3ccSAndroid Build Coastguard Worker   const uint8_t *data;
203*77c1e3ccSAndroid Build Coastguard Worker   size_t size;
204*77c1e3ccSAndroid Build Coastguard Worker } TileBufferDec;
205*77c1e3ccSAndroid Build Coastguard Worker 
206*77c1e3ccSAndroid Build Coastguard Worker typedef struct DataBuffer {
207*77c1e3ccSAndroid Build Coastguard Worker   const uint8_t *data;
208*77c1e3ccSAndroid Build Coastguard Worker   size_t size;
209*77c1e3ccSAndroid Build Coastguard Worker } DataBuffer;
210*77c1e3ccSAndroid Build Coastguard Worker 
211*77c1e3ccSAndroid Build Coastguard Worker typedef struct EXTERNAL_REFERENCES {
212*77c1e3ccSAndroid Build Coastguard Worker   YV12_BUFFER_CONFIG refs[MAX_EXTERNAL_REFERENCES];
213*77c1e3ccSAndroid Build Coastguard Worker   int num;
214*77c1e3ccSAndroid Build Coastguard Worker } EXTERNAL_REFERENCES;
215*77c1e3ccSAndroid Build Coastguard Worker 
216*77c1e3ccSAndroid Build Coastguard Worker typedef struct TileJobsDec {
217*77c1e3ccSAndroid Build Coastguard Worker   TileBufferDec *tile_buffer;
218*77c1e3ccSAndroid Build Coastguard Worker   TileDataDec *tile_data;
219*77c1e3ccSAndroid Build Coastguard Worker } TileJobsDec;
220*77c1e3ccSAndroid Build Coastguard Worker 
221*77c1e3ccSAndroid Build Coastguard Worker typedef struct AV1DecTileMTData {
222*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
223*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *job_mutex;
224*77c1e3ccSAndroid Build Coastguard Worker #endif
225*77c1e3ccSAndroid Build Coastguard Worker   TileJobsDec *job_queue;
226*77c1e3ccSAndroid Build Coastguard Worker   int jobs_enqueued;
227*77c1e3ccSAndroid Build Coastguard Worker   int jobs_dequeued;
228*77c1e3ccSAndroid Build Coastguard Worker   int alloc_tile_rows;
229*77c1e3ccSAndroid Build Coastguard Worker   int alloc_tile_cols;
230*77c1e3ccSAndroid Build Coastguard Worker } AV1DecTileMT;
231*77c1e3ccSAndroid Build Coastguard Worker 
232*77c1e3ccSAndroid Build Coastguard Worker typedef struct AV1Decoder {
233*77c1e3ccSAndroid Build Coastguard Worker   DecoderCodingBlock dcb;
234*77c1e3ccSAndroid Build Coastguard Worker 
235*77c1e3ccSAndroid Build Coastguard Worker   DECLARE_ALIGNED(32, AV1_COMMON, common);
236*77c1e3ccSAndroid Build Coastguard Worker 
237*77c1e3ccSAndroid Build Coastguard Worker   AVxWorker lf_worker;
238*77c1e3ccSAndroid Build Coastguard Worker   AV1LfSync lf_row_sync;
239*77c1e3ccSAndroid Build Coastguard Worker   AV1LrSync lr_row_sync;
240*77c1e3ccSAndroid Build Coastguard Worker   AV1LrStruct lr_ctxt;
241*77c1e3ccSAndroid Build Coastguard Worker   AV1CdefSync cdef_sync;
242*77c1e3ccSAndroid Build Coastguard Worker   AV1CdefWorkerData *cdef_worker;
243*77c1e3ccSAndroid Build Coastguard Worker   AVxWorker *tile_workers;
244*77c1e3ccSAndroid Build Coastguard Worker   int num_workers;
245*77c1e3ccSAndroid Build Coastguard Worker   DecWorkerData *thread_data;
246*77c1e3ccSAndroid Build Coastguard Worker   ThreadData td;
247*77c1e3ccSAndroid Build Coastguard Worker   TileDataDec *tile_data;
248*77c1e3ccSAndroid Build Coastguard Worker   int allocated_tiles;
249*77c1e3ccSAndroid Build Coastguard Worker 
250*77c1e3ccSAndroid Build Coastguard Worker   TileBufferDec tile_buffers[MAX_TILE_ROWS][MAX_TILE_COLS];
251*77c1e3ccSAndroid Build Coastguard Worker   AV1DecTileMT tile_mt_info;
252*77c1e3ccSAndroid Build Coastguard Worker 
253*77c1e3ccSAndroid Build Coastguard Worker   // Each time the decoder is called, we expect to receive a full temporal unit.
254*77c1e3ccSAndroid Build Coastguard Worker   // This can contain up to one shown frame per spatial layer in the current
255*77c1e3ccSAndroid Build Coastguard Worker   // operating point (note that some layers may be entirely omitted).
256*77c1e3ccSAndroid Build Coastguard Worker   // If the 'output_all_layers' option is true, we save all of these shown
257*77c1e3ccSAndroid Build Coastguard Worker   // frames so that they can be returned to the application. If the
258*77c1e3ccSAndroid Build Coastguard Worker   // 'output_all_layers' option is false, then we only output one image per
259*77c1e3ccSAndroid Build Coastguard Worker   // temporal unit.
260*77c1e3ccSAndroid Build Coastguard Worker   //
261*77c1e3ccSAndroid Build Coastguard Worker   // Note: The saved buffers are released at the start of the next time the
262*77c1e3ccSAndroid Build Coastguard Worker   // application calls aom_codec_decode().
263*77c1e3ccSAndroid Build Coastguard Worker   int output_all_layers;
264*77c1e3ccSAndroid Build Coastguard Worker   RefCntBuffer *output_frames[MAX_NUM_SPATIAL_LAYERS];
265*77c1e3ccSAndroid Build Coastguard Worker   size_t num_output_frames;  // How many frames are queued up so far?
266*77c1e3ccSAndroid Build Coastguard Worker 
267*77c1e3ccSAndroid Build Coastguard Worker   // In order to properly support random-access decoding, we need
268*77c1e3ccSAndroid Build Coastguard Worker   // to behave slightly differently for the very first frame we decode.
269*77c1e3ccSAndroid Build Coastguard Worker   // So we track whether this is the first frame or not.
270*77c1e3ccSAndroid Build Coastguard Worker   int decoding_first_frame;
271*77c1e3ccSAndroid Build Coastguard Worker 
272*77c1e3ccSAndroid Build Coastguard Worker   int allow_lowbitdepth;
273*77c1e3ccSAndroid Build Coastguard Worker   int max_threads;
274*77c1e3ccSAndroid Build Coastguard Worker   int inv_tile_order;
275*77c1e3ccSAndroid Build Coastguard Worker   int need_resync;  // wait for key/intra-only frame.
276*77c1e3ccSAndroid Build Coastguard Worker   int reset_decoder_state;
277*77c1e3ccSAndroid Build Coastguard Worker 
278*77c1e3ccSAndroid Build Coastguard Worker   int tile_size_bytes;
279*77c1e3ccSAndroid Build Coastguard Worker   int tile_col_size_bytes;
280*77c1e3ccSAndroid Build Coastguard Worker   int dec_tile_row, dec_tile_col;  // always -1 for non-VR tile encoding
281*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_ACCOUNTING
282*77c1e3ccSAndroid Build Coastguard Worker   int acct_enabled;
283*77c1e3ccSAndroid Build Coastguard Worker   Accounting accounting;
284*77c1e3ccSAndroid Build Coastguard Worker #endif
285*77c1e3ccSAndroid Build Coastguard Worker   int sequence_header_ready;
286*77c1e3ccSAndroid Build Coastguard Worker   int sequence_header_changed;
287*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INSPECTION
288*77c1e3ccSAndroid Build Coastguard Worker   aom_inspect_cb inspect_cb;
289*77c1e3ccSAndroid Build Coastguard Worker   void *inspect_ctx;
290*77c1e3ccSAndroid Build Coastguard Worker #endif
291*77c1e3ccSAndroid Build Coastguard Worker   int operating_point;
292*77c1e3ccSAndroid Build Coastguard Worker   int current_operating_point;
293*77c1e3ccSAndroid Build Coastguard Worker   int seen_frame_header;
294*77c1e3ccSAndroid Build Coastguard Worker   // The expected start_tile (tg_start syntax element) of the next tile group.
295*77c1e3ccSAndroid Build Coastguard Worker   int next_start_tile;
296*77c1e3ccSAndroid Build Coastguard Worker 
297*77c1e3ccSAndroid Build Coastguard Worker   // State if the camera frame header is already decoded while
298*77c1e3ccSAndroid Build Coastguard Worker   // large_scale_tile = 1.
299*77c1e3ccSAndroid Build Coastguard Worker   int camera_frame_header_ready;
300*77c1e3ccSAndroid Build Coastguard Worker   size_t frame_header_size;
301*77c1e3ccSAndroid Build Coastguard Worker   DataBuffer obu_size_hdr;
302*77c1e3ccSAndroid Build Coastguard Worker   int output_frame_width_in_tiles_minus_1;
303*77c1e3ccSAndroid Build Coastguard Worker   int output_frame_height_in_tiles_minus_1;
304*77c1e3ccSAndroid Build Coastguard Worker   int tile_count_minus_1;
305*77c1e3ccSAndroid Build Coastguard Worker   uint32_t coded_tile_data_size;
306*77c1e3ccSAndroid Build Coastguard Worker   unsigned int ext_tile_debug;  // for ext-tile software debug & testing
307*77c1e3ccSAndroid Build Coastguard Worker 
308*77c1e3ccSAndroid Build Coastguard Worker   // Decoder has 3 modes of operation:
309*77c1e3ccSAndroid Build Coastguard Worker   // (1) Single-threaded decoding.
310*77c1e3ccSAndroid Build Coastguard Worker   // (2) Multi-threaded decoding with each tile decoded in parallel.
311*77c1e3ccSAndroid Build Coastguard Worker   // (3) In addition to (2), each thread decodes 1 superblock row in parallel.
312*77c1e3ccSAndroid Build Coastguard Worker   // row_mt = 1 triggers mode (3) above, while row_mt = 0, will trigger mode (1)
313*77c1e3ccSAndroid Build Coastguard Worker   // or (2) depending on 'max_threads'.
314*77c1e3ccSAndroid Build Coastguard Worker   unsigned int row_mt;
315*77c1e3ccSAndroid Build Coastguard Worker 
316*77c1e3ccSAndroid Build Coastguard Worker   EXTERNAL_REFERENCES ext_refs;
317*77c1e3ccSAndroid Build Coastguard Worker   YV12_BUFFER_CONFIG tile_list_outbuf;
318*77c1e3ccSAndroid Build Coastguard Worker 
319*77c1e3ccSAndroid Build Coastguard Worker   // Coding block buffer for the current frame.
320*77c1e3ccSAndroid Build Coastguard Worker   // Allocated and used only for multi-threaded decoding with 'row_mt == 0'.
321*77c1e3ccSAndroid Build Coastguard Worker   // See also: similar buffer in 'ThreadData' struct.
322*77c1e3ccSAndroid Build Coastguard Worker   CB_BUFFER *cb_buffer_base;
323*77c1e3ccSAndroid Build Coastguard Worker   // Allocated size of 'cb_buffer_base'. Currently same as the number of
324*77c1e3ccSAndroid Build Coastguard Worker   // superblocks in the coded frame.
325*77c1e3ccSAndroid Build Coastguard Worker   int cb_buffer_alloc_size;
326*77c1e3ccSAndroid Build Coastguard Worker 
327*77c1e3ccSAndroid Build Coastguard Worker   int allocated_row_mt_sync_rows;
328*77c1e3ccSAndroid Build Coastguard Worker 
329*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
330*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *row_mt_mutex_;
331*77c1e3ccSAndroid Build Coastguard Worker   pthread_cond_t *row_mt_cond_;
332*77c1e3ccSAndroid Build Coastguard Worker #endif
333*77c1e3ccSAndroid Build Coastguard Worker 
334*77c1e3ccSAndroid Build Coastguard Worker   AV1DecRowMTInfo frame_row_mt_info;
335*77c1e3ccSAndroid Build Coastguard Worker   aom_metadata_array_t *metadata;
336*77c1e3ccSAndroid Build Coastguard Worker 
337*77c1e3ccSAndroid Build Coastguard Worker   int context_update_tile_id;
338*77c1e3ccSAndroid Build Coastguard Worker   int skip_loop_filter;
339*77c1e3ccSAndroid Build Coastguard Worker   int skip_film_grain;
340*77c1e3ccSAndroid Build Coastguard Worker   int is_annexb;
341*77c1e3ccSAndroid Build Coastguard Worker   int valid_for_referencing[REF_FRAMES];
342*77c1e3ccSAndroid Build Coastguard Worker   int is_fwd_kf_present;
343*77c1e3ccSAndroid Build Coastguard Worker   int is_arf_frame_present;
344*77c1e3ccSAndroid Build Coastguard Worker   int num_tile_groups;
345*77c1e3ccSAndroid Build Coastguard Worker   aom_s_frame_info sframe_info;
346*77c1e3ccSAndroid Build Coastguard Worker 
347*77c1e3ccSAndroid Build Coastguard Worker   /*!
348*77c1e3ccSAndroid Build Coastguard Worker    * Elements part of the sequence header, that are applicable for all the
349*77c1e3ccSAndroid Build Coastguard Worker    * frames in the video.
350*77c1e3ccSAndroid Build Coastguard Worker    */
351*77c1e3ccSAndroid Build Coastguard Worker   SequenceHeader seq_params;
352*77c1e3ccSAndroid Build Coastguard Worker 
353*77c1e3ccSAndroid Build Coastguard Worker   /*!
354*77c1e3ccSAndroid Build Coastguard Worker    * If true, buffer removal times are present.
355*77c1e3ccSAndroid Build Coastguard Worker    */
356*77c1e3ccSAndroid Build Coastguard Worker   bool buffer_removal_time_present;
357*77c1e3ccSAndroid Build Coastguard Worker 
358*77c1e3ccSAndroid Build Coastguard Worker   /*!
359*77c1e3ccSAndroid Build Coastguard Worker    * Code and details about current error status.
360*77c1e3ccSAndroid Build Coastguard Worker    */
361*77c1e3ccSAndroid Build Coastguard Worker   struct aom_internal_error_info error;
362*77c1e3ccSAndroid Build Coastguard Worker 
363*77c1e3ccSAndroid Build Coastguard Worker   /*!
364*77c1e3ccSAndroid Build Coastguard Worker    * Number of temporal layers: may be > 1 for SVC (scalable vector coding).
365*77c1e3ccSAndroid Build Coastguard Worker    */
366*77c1e3ccSAndroid Build Coastguard Worker   unsigned int number_temporal_layers;
367*77c1e3ccSAndroid Build Coastguard Worker 
368*77c1e3ccSAndroid Build Coastguard Worker   /*!
369*77c1e3ccSAndroid Build Coastguard Worker    * Number of spatial layers: may be > 1 for SVC (scalable vector coding).
370*77c1e3ccSAndroid Build Coastguard Worker    */
371*77c1e3ccSAndroid Build Coastguard Worker   unsigned int number_spatial_layers;
372*77c1e3ccSAndroid Build Coastguard Worker } AV1Decoder;
373*77c1e3ccSAndroid Build Coastguard Worker 
374*77c1e3ccSAndroid Build Coastguard Worker // Returns 0 on success. Sets pbi->common.error.error_code to a nonzero error
375*77c1e3ccSAndroid Build Coastguard Worker // code and returns a nonzero value on failure.
376*77c1e3ccSAndroid Build Coastguard Worker int av1_receive_compressed_data(struct AV1Decoder *pbi, size_t size,
377*77c1e3ccSAndroid Build Coastguard Worker                                 const uint8_t **psource);
378*77c1e3ccSAndroid Build Coastguard Worker 
379*77c1e3ccSAndroid Build Coastguard Worker // Get the frame at a particular index in the output queue
380*77c1e3ccSAndroid Build Coastguard Worker int av1_get_raw_frame(AV1Decoder *pbi, size_t index, YV12_BUFFER_CONFIG **sd,
381*77c1e3ccSAndroid Build Coastguard Worker                       aom_film_grain_t **grain_params);
382*77c1e3ccSAndroid Build Coastguard Worker 
383*77c1e3ccSAndroid Build Coastguard Worker int av1_get_frame_to_show(struct AV1Decoder *pbi, YV12_BUFFER_CONFIG *frame);
384*77c1e3ccSAndroid Build Coastguard Worker 
385*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t av1_copy_reference_dec(struct AV1Decoder *pbi, int idx,
386*77c1e3ccSAndroid Build Coastguard Worker                                        YV12_BUFFER_CONFIG *sd);
387*77c1e3ccSAndroid Build Coastguard Worker 
388*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t av1_set_reference_dec(AV1_COMMON *cm, int idx,
389*77c1e3ccSAndroid Build Coastguard Worker                                       int use_external_ref,
390*77c1e3ccSAndroid Build Coastguard Worker                                       YV12_BUFFER_CONFIG *sd);
391*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t av1_copy_new_frame_dec(AV1_COMMON *cm,
392*77c1e3ccSAndroid Build Coastguard Worker                                        YV12_BUFFER_CONFIG *new_frame,
393*77c1e3ccSAndroid Build Coastguard Worker                                        YV12_BUFFER_CONFIG *sd);
394*77c1e3ccSAndroid Build Coastguard Worker 
395*77c1e3ccSAndroid Build Coastguard Worker struct AV1Decoder *av1_decoder_create(BufferPool *const pool);
396*77c1e3ccSAndroid Build Coastguard Worker 
397*77c1e3ccSAndroid Build Coastguard Worker void av1_decoder_remove(struct AV1Decoder *pbi);
398*77c1e3ccSAndroid Build Coastguard Worker void av1_dealloc_dec_jobs(struct AV1DecTileMTData *tile_mt_info);
399*77c1e3ccSAndroid Build Coastguard Worker 
400*77c1e3ccSAndroid Build Coastguard Worker void av1_dec_row_mt_dealloc(AV1DecRowMTSync *dec_row_mt_sync);
401*77c1e3ccSAndroid Build Coastguard Worker 
402*77c1e3ccSAndroid Build Coastguard Worker void av1_dec_free_cb_buf(AV1Decoder *pbi);
403*77c1e3ccSAndroid Build Coastguard Worker 
decrease_ref_count(RefCntBuffer * const buf,BufferPool * const pool)404*77c1e3ccSAndroid Build Coastguard Worker static inline void decrease_ref_count(RefCntBuffer *const buf,
405*77c1e3ccSAndroid Build Coastguard Worker                                       BufferPool *const pool) {
406*77c1e3ccSAndroid Build Coastguard Worker   if (buf != NULL) {
407*77c1e3ccSAndroid Build Coastguard Worker     --buf->ref_count;
408*77c1e3ccSAndroid Build Coastguard Worker     // Reference counts should never become negative. If this assertion fails,
409*77c1e3ccSAndroid Build Coastguard Worker     // there is a bug in our reference count management.
410*77c1e3ccSAndroid Build Coastguard Worker     assert(buf->ref_count >= 0);
411*77c1e3ccSAndroid Build Coastguard Worker     // A worker may only get a free framebuffer index when calling get_free_fb.
412*77c1e3ccSAndroid Build Coastguard Worker     // But the raw frame buffer is not set up until we finish decoding header.
413*77c1e3ccSAndroid Build Coastguard Worker     // So if any error happens during decoding header, frame_bufs[idx] will not
414*77c1e3ccSAndroid Build Coastguard Worker     // have a valid raw frame buffer.
415*77c1e3ccSAndroid Build Coastguard Worker     if (buf->ref_count == 0 && buf->raw_frame_buffer.data) {
416*77c1e3ccSAndroid Build Coastguard Worker       pool->release_fb_cb(pool->cb_priv, &buf->raw_frame_buffer);
417*77c1e3ccSAndroid Build Coastguard Worker       buf->raw_frame_buffer.data = NULL;
418*77c1e3ccSAndroid Build Coastguard Worker       buf->raw_frame_buffer.size = 0;
419*77c1e3ccSAndroid Build Coastguard Worker       buf->raw_frame_buffer.priv = NULL;
420*77c1e3ccSAndroid Build Coastguard Worker     }
421*77c1e3ccSAndroid Build Coastguard Worker   }
422*77c1e3ccSAndroid Build Coastguard Worker }
423*77c1e3ccSAndroid Build Coastguard Worker 
424*77c1e3ccSAndroid Build Coastguard Worker #define ACCT_STR __func__
av1_read_uniform(aom_reader * r,int n)425*77c1e3ccSAndroid Build Coastguard Worker static inline int av1_read_uniform(aom_reader *r, int n) {
426*77c1e3ccSAndroid Build Coastguard Worker   const int l = get_unsigned_bits(n);
427*77c1e3ccSAndroid Build Coastguard Worker   const int m = (1 << l) - n;
428*77c1e3ccSAndroid Build Coastguard Worker   const int v = aom_read_literal(r, l - 1, ACCT_STR);
429*77c1e3ccSAndroid Build Coastguard Worker   assert(l != 0);
430*77c1e3ccSAndroid Build Coastguard Worker   if (v < m)
431*77c1e3ccSAndroid Build Coastguard Worker     return v;
432*77c1e3ccSAndroid Build Coastguard Worker   else
433*77c1e3ccSAndroid Build Coastguard Worker     return (v << 1) - m + aom_read_literal(r, 1, ACCT_STR);
434*77c1e3ccSAndroid Build Coastguard Worker }
435*77c1e3ccSAndroid Build Coastguard Worker 
436*77c1e3ccSAndroid Build Coastguard Worker typedef void (*palette_visitor_fn_t)(MACROBLOCKD *const xd, int plane,
437*77c1e3ccSAndroid Build Coastguard Worker                                      aom_reader *r);
438*77c1e3ccSAndroid Build Coastguard Worker 
439*77c1e3ccSAndroid Build Coastguard Worker void av1_visit_palette(AV1Decoder *const pbi, MACROBLOCKD *const xd,
440*77c1e3ccSAndroid Build Coastguard Worker                        aom_reader *r, palette_visitor_fn_t visit);
441*77c1e3ccSAndroid Build Coastguard Worker 
442*77c1e3ccSAndroid Build Coastguard Worker typedef void (*block_visitor_fn_t)(AV1Decoder *const pbi, ThreadData *const td,
443*77c1e3ccSAndroid Build Coastguard Worker                                    int mi_row, int mi_col, aom_reader *r,
444*77c1e3ccSAndroid Build Coastguard Worker                                    PARTITION_TYPE partition, BLOCK_SIZE bsize);
445*77c1e3ccSAndroid Build Coastguard Worker 
446*77c1e3ccSAndroid Build Coastguard Worker /*!\endcond */
447*77c1e3ccSAndroid Build Coastguard Worker 
448*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
449*77c1e3ccSAndroid Build Coastguard Worker }  // extern "C"
450*77c1e3ccSAndroid Build Coastguard Worker #endif
451*77c1e3ccSAndroid Build Coastguard Worker 
452*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AV1_DECODER_DECODER_H_
453