| 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
33 extern "C" {
34#endif
35
45 typedef struct DecoderCodingBlock {
49 DECLARE_ALIGNED(32, MACROBLOCKD, xd);
53 int corrupted;
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];
84} DecoderCodingBlock;
85
87
88 typedef void (*decode_block_visitor_fn_t)(const AV1_COMMON *const cm,
89 DecoderCodingBlock *dcb,
90 aom_reader *const r, const int plane,
91 const int row, const int col,
92 const TX_SIZE tx_size);
93
94 typedef void (*predict_inter_block_visitor_fn_t)(AV1_COMMON *const cm,
95 DecoderCodingBlock *dcb,
96 BLOCK_SIZE bsize);
97
98 typedef void (*cfl_store_inter_block_visitor_fn_t)(AV1_COMMON *const cm,
99 MACROBLOCKD *const xd);
100
101 typedef struct ThreadData {
102 DecoderCodingBlock dcb;
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
134 typedef struct AV1DecRowMTJobInfo {
135 int tile_row;
136 int tile_col;
137 int mi_row;
138} AV1DecRowMTJobInfo;
139
140 typedef 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 // Denotes the superblock interval at which conditional signalling should
148 // happen. Also denotes the minimum number of extra superblocks of the top row
149 // to be complete to start decoding the current superblock. A value of 1
150 // indicates top-right dependency.
151 int sync_range;
152 // Denotes the additional number of superblocks in the previous row to be
153 // complete to start decoding the current superblock when intraBC tool is
154 // enabled. This additional top-right delay is required to satisfy the
155 // hardware constraints for intraBC tool when row multithreading is enabled.
156 int intrabc_extra_top_right_sb_delay;
157 int mi_rows;
158 int mi_cols;
159 int mi_rows_parse_done;
160 int mi_rows_decode_started;
161 int num_threads_working;
162} AV1DecRowMTSync;
163
164 typedef struct AV1DecRowMTInfo {
165 int tile_rows_start;
166 int tile_rows_end;
167 int tile_cols_start;
168 int tile_cols_end;
169 int start_tile;
170 int end_tile;
171 int mi_rows_to_decode;
172
173 // Invariant:
174 // mi_rows_parse_done >= mi_rows_decode_started.
175 // mi_rows_parse_done and mi_rows_decode_started are both initialized to 0.
176 // mi_rows_parse_done is incremented freely. mi_rows_decode_started may only
177 // be incremented to catch up with mi_rows_parse_done but is not allowed to
178 // surpass mi_rows_parse_done.
179 //
180 // When mi_rows_decode_started reaches mi_rows_to_decode, there are no more
181 // decode jobs.
182
183 // Indicates the progress of the bit-stream parsing of superblocks.
184 // Initialized to 0. Incremented by sb_mi_size when parse sb row is done.
185 int mi_rows_parse_done;
186 // Indicates the progress of the decoding of superblocks.
187 // Initialized to 0. Incremented by sb_mi_size when decode sb row is started.
188 int mi_rows_decode_started;
189 // Boolean: Initialized to 0 (false). Set to 1 (true) on error to abort
190 // decoding.
191 int row_mt_exit;
192} AV1DecRowMTInfo;
193
194 typedef struct TileDataDec {
195 TileInfo tile_info;
196 aom_reader bit_reader;
197 DECLARE_ALIGNED(16, FRAME_CONTEXT, tctx);
198 AV1DecRowMTSync dec_row_mt_sync;
199} TileDataDec;
200
201 typedef struct TileBufferDec {
202 const uint8_t *data;
203 size_t size;
204} TileBufferDec;
205
206 typedef struct DataBuffer {
207 const uint8_t *data;
208 size_t size;
209} DataBuffer;
210
211 typedef struct EXTERNAL_REFERENCES {
212 YV12_BUFFER_CONFIG refs[MAX_EXTERNAL_REFERENCES];
213 int num;
214} EXTERNAL_REFERENCES;
215
216 typedef struct TileJobsDec {
217 TileBufferDec *tile_buffer;
218 TileDataDec *tile_data;
219} TileJobsDec;
220
221 typedef struct AV1DecTileMTData {
222#if CONFIG_MULTITHREAD
223 pthread_mutex_t *job_mutex;
224#endif
225 TileJobsDec *job_queue;
226 int jobs_enqueued;
227 int jobs_dequeued;
228 int alloc_tile_rows;
229 int alloc_tile_cols;
230} AV1DecTileMT;
231
232 typedef struct AV1Decoder {
233 DecoderCodingBlock dcb;
234
235 DECLARE_ALIGNED(32, AV1_COMMON, common);
236
237 AVxWorker lf_worker;
238 AV1LfSync lf_row_sync;
239 AV1LrSync lr_row_sync;
240 AV1LrStruct lr_ctxt;
241 AV1CdefSync cdef_sync;
242 AV1CdefWorkerData *cdef_worker;
243 AVxWorker *tile_workers;
244 int num_workers;
245 DecWorkerData *thread_data;
246 ThreadData td;
247 TileDataDec *tile_data;
248 int allocated_tiles;
249
250 TileBufferDec tile_buffers[MAX_TILE_ROWS][MAX_TILE_COLS];
251 AV1DecTileMT tile_mt_info;
252
253 // Each time the decoder is called, we expect to receive a full temporal unit.
254 // This can contain up to one shown frame per spatial layer in the current
255 // operating point (note that some layers may be entirely omitted).
256 // If the 'output_all_layers' option is true, we save all of these shown
257 // frames so that they can be returned to the application. If the
258 // 'output_all_layers' option is false, then we only output one image per
259 // temporal unit.
260 //
261 // Note: The saved buffers are released at the start of the next time the
262 // application calls aom_codec_decode().
263 int output_all_layers;
264 RefCntBuffer *output_frames[MAX_NUM_SPATIAL_LAYERS];
265 size_t num_output_frames; // How many frames are queued up so far?
266
267 // In order to properly support random-access decoding, we need
268 // to behave slightly differently for the very first frame we decode.
269 // So we track whether this is the first frame or not.
270 int decoding_first_frame;
271
272 int allow_lowbitdepth;
273 int max_threads;
274 int inv_tile_order;
275 int need_resync; // wait for key/intra-only frame.
276 int reset_decoder_state;
277
278 int tile_size_bytes;
279 int tile_col_size_bytes;
280 int dec_tile_row, dec_tile_col; // always -1 for non-VR tile encoding
281#if CONFIG_ACCOUNTING
282 int acct_enabled;
283 Accounting accounting;
284#endif
285 int sequence_header_ready;
286 int sequence_header_changed;
287#if CONFIG_INSPECTION
288 aom_inspect_cb inspect_cb;
289 void *inspect_ctx;
290#endif
291 int operating_point;
292 int current_operating_point;
293 int seen_frame_header;
294 // The expected start_tile (tg_start syntax element) of the next tile group.
295 int next_start_tile;
296
297 // State if the camera frame header is already decoded while
298 // large_scale_tile = 1.
299 int camera_frame_header_ready;
300 size_t frame_header_size;
301 DataBuffer obu_size_hdr;
302 int output_frame_width_in_tiles_minus_1;
303 int output_frame_height_in_tiles_minus_1;
304 int tile_count_minus_1;
305 uint32_t coded_tile_data_size;
306 unsigned int ext_tile_debug; // for ext-tile software debug & testing
307
308 // Decoder has 3 modes of operation:
309 // (1) Single-threaded decoding.
310 // (2) Multi-threaded decoding with each tile decoded in parallel.
311 // (3) In addition to (2), each thread decodes 1 superblock row in parallel.
312 // row_mt = 1 triggers mode (3) above, while row_mt = 0, will trigger mode (1)
313 // or (2) depending on 'max_threads'.
314 unsigned int row_mt;
315
316 EXTERNAL_REFERENCES ext_refs;
317 YV12_BUFFER_CONFIG tile_list_outbuf;
318
319 // Coding block buffer for the current frame.
320 // Allocated and used only for multi-threaded decoding with 'row_mt == 0'.
321 // See also: similar buffer in 'ThreadData' struct.
322 CB_BUFFER *cb_buffer_base;
323 // Allocated size of 'cb_buffer_base'. Currently same as the number of
324 // superblocks in the coded frame.
325 int cb_buffer_alloc_size;
326
327 int allocated_row_mt_sync_rows;
328
329#if CONFIG_MULTITHREAD
330 pthread_mutex_t *row_mt_mutex_;
331 pthread_cond_t *row_mt_cond_;
332#endif
333
334 AV1DecRowMTInfo frame_row_mt_info;
335 aom_metadata_array_t *metadata;
336
337 int context_update_tile_id;
338 int skip_loop_filter;
339 int skip_film_grain;
340 int is_annexb;
341 int valid_for_referencing[REF_FRAMES];
342 int is_fwd_kf_present;
343 int is_arf_frame_present;
344 int num_tile_groups;
345 aom_s_frame_info sframe_info;
346
351 SequenceHeader seq_params;
352
356 bool buffer_removal_time_present;
357
361 struct aom_internal_error_info error;
362
366 unsigned int number_temporal_layers;
367
371 unsigned int number_spatial_layers;
372} AV1Decoder;
373
374// Returns 0 on success. Sets pbi->common.error.error_code to a nonzero error
375// code and returns a nonzero value on failure.
376 int av1_receive_compressed_data(struct AV1Decoder *pbi, size_t size,
377 const uint8_t **psource);
378
379// Get the frame at a particular index in the output queue
380 int av1_get_raw_frame(AV1Decoder *pbi, size_t index, YV12_BUFFER_CONFIG **sd,
381 aom_film_grain_t **grain_params);
382
383 int av1_get_frame_to_show(struct AV1Decoder *pbi, YV12_BUFFER_CONFIG *frame);
384
385 aom_codec_err_t av1_copy_reference_dec(struct AV1Decoder *pbi, int idx,
386 YV12_BUFFER_CONFIG *sd);
387
388 aom_codec_err_t av1_set_reference_dec(AV1_COMMON *cm, int idx,
389 int use_external_ref,
390 YV12_BUFFER_CONFIG *sd);
391 aom_codec_err_t av1_copy_new_frame_dec(AV1_COMMON *cm,
392 YV12_BUFFER_CONFIG *new_frame,
393 YV12_BUFFER_CONFIG *sd);
394
395 struct AV1Decoder *av1_decoder_create(BufferPool *const pool);
396
397 void av1_decoder_remove(struct AV1Decoder *pbi);
398 void av1_dealloc_dec_jobs(struct AV1DecTileMTData *tile_mt_info);
399
400 void av1_dec_row_mt_dealloc(AV1DecRowMTSync *dec_row_mt_sync);
401
402 void av1_dec_free_cb_buf(AV1Decoder *pbi);
403
404 static inline void decrease_ref_count(RefCntBuffer *const buf,
405 BufferPool *const pool) {
406 if (buf != NULL) {
407 --buf->ref_count;
408 // Reference counts should never become negative. If this assertion fails,
409 // there is a bug in our reference count management.
410 assert(buf->ref_count >= 0);
411 // A worker may only get a free framebuffer index when calling get_free_fb.
412 // But the raw frame buffer is not set up until we finish decoding header.
413 // So if any error happens during decoding header, frame_bufs[idx] will not
414 // have a valid raw frame buffer.
415 if (buf->ref_count == 0 && buf->raw_frame_buffer.data) {
416 pool->release_fb_cb(pool->cb_priv, &buf->raw_frame_buffer);
417 buf->raw_frame_buffer.data = NULL;
418 buf->raw_frame_buffer.size = 0;
419 buf->raw_frame_buffer.priv = NULL;
420 }
421 }
422}
423
424#define ACCT_STR __func__
425 static inline int av1_read_uniform(aom_reader *r, int n) {
426 const int l = get_unsigned_bits(n);
427 const int m = (1 << l) - n;
428 const int v = aom_read_literal(r, l - 1, ACCT_STR);
429 assert(l != 0);
430 if (v < m)
431 return v;
432 else
433 return (v << 1) - m + aom_read_literal(r, 1, ACCT_STR);
434}
435
436 typedef void (*palette_visitor_fn_t)(MACROBLOCKD *const xd, int plane,
437 aom_reader *r);
438
439 void av1_visit_palette(AV1Decoder *const pbi, MACROBLOCKD *const xd,
440 aom_reader *r, palette_visitor_fn_t visit);
441
442 typedef void (*block_visitor_fn_t)(AV1Decoder *const pbi, ThreadData *const td,
443 int mi_row, int mi_col, aom_reader *r,
444 PARTITION_TYPE partition, BLOCK_SIZE bsize);
445
447
448#ifdef __cplusplus
449} // extern "C"
450#endif
451
452#endif // AOM_AV1_DECODER_DECODER_H_
aom_codec.h
Describes the codec algorithm interface to applications.
aom_metadata_array_t
struct aom_metadata_array aom_metadata_array_t
Array of aom_metadata structs for an image.
Definition aom_image.h:171
aom_inspect_cb
void(* aom_inspect_cb)(void *decoder, void *ctx)
Definition aomdx.h:55
Accounting
struct Accounting Accounting
Definition aomdx.h:50
aom_s_frame_info
struct aom_s_frame_info aom_s_frame_info
Structure to hold information about S_FRAME.
aom_codec_err_t
aom_codec_err_t
Algorithm return codes.
Definition aom_codec.h:155
DecoderCodingBlock
Contains coding block data required by the decoder.
Definition decoder.h:45
DecoderCodingBlock::eob_data
eob_info * eob_data[3]
Definition decoder.h:73
DecoderCodingBlock::mc_buf
uint8_t * mc_buf[2]
Definition decoder.h:58
DecoderCodingBlock::dqcoeff_block
tran_low_t * dqcoeff_block[3]
Definition decoder.h:63
DecoderCodingBlock::txb_offset
uint16_t txb_offset[3]
Definition decoder.h:78
DecoderCodingBlock::corrupted
int corrupted
Definition decoder.h:53
DecoderCodingBlock::xd
MACROBLOCKD xd
Definition decoder.h:49
DecoderCodingBlock::ref_mv_count
uint8_t ref_mv_count[MODE_CTX_REF_FRAMES]
Definition decoder.h:83
DecoderCodingBlock::cb_offset
uint16_t cb_offset[3]
Definition decoder.h:68