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