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];
85 
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  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 
155 typedef 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 
185 typedef 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 
192 typedef struct TileBufferDec {
193  const uint8_t *data;
194  size_t size;
195 } TileBufferDec;
196 
197 typedef struct DataBuffer {
198  const uint8_t *data;
199  size_t size;
200 } DataBuffer;
201 
202 typedef struct EXTERNAL_REFERENCES {
203  YV12_BUFFER_CONFIG refs[MAX_EXTERNAL_REFERENCES];
204  int num;
205 } EXTERNAL_REFERENCES;
206 
207 typedef struct TileJobsDec {
208  TileBufferDec *tile_buffer;
209  TileDataDec *tile_data;
210 } TileJobsDec;
211 
212 typedef 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 
223 typedef struct AV1Decoder {
224  DecoderCodingBlock dcb;
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  AVxWorker *tile_workers;
233  int num_workers;
234  DecWorkerData *thread_data;
235  ThreadData td;
236  TileDataDec *tile_data;
237  int allocated_tiles;
238 
239  TileBufferDec tile_buffers[MAX_TILE_ROWS][MAX_TILE_COLS];
240  AV1DecTileMT tile_mt_info;
241 
242  // Each time the decoder is called, we expect to receive a full temporal unit.
243  // This can contain up to one shown frame per spatial layer in the current
244  // operating point (note that some layers may be entirely omitted).
245  // If the 'output_all_layers' option is true, we save all of these shown
246  // frames so that they can be returned to the application. If the
247  // 'output_all_layers' option is false, then we only output one image per
248  // temporal unit.
249  //
250  // Note: The saved buffers are released at the start of the next time the
251  // application calls aom_codec_decode().
252  int output_all_layers;
253  RefCntBuffer *output_frames[MAX_NUM_SPATIAL_LAYERS];
254  size_t num_output_frames; // How many frames are queued up so far?
255 
256  // In order to properly support random-access decoding, we need
257  // to behave slightly differently for the very first frame we decode.
258  // So we track whether this is the first frame or not.
259  int decoding_first_frame;
260 
261  int allow_lowbitdepth;
262  int max_threads;
263  int inv_tile_order;
264  int need_resync; // wait for key/intra-only frame.
265  int reset_decoder_state;
266 
267  int tile_size_bytes;
268  int tile_col_size_bytes;
269  int dec_tile_row, dec_tile_col; // always -1 for non-VR tile encoding
270 #if CONFIG_ACCOUNTING
271  int acct_enabled;
272  Accounting accounting;
273 #endif
274  int sequence_header_ready;
275  int sequence_header_changed;
276 #if CONFIG_INSPECTION
277  aom_inspect_cb inspect_cb;
278  void *inspect_ctx;
279 #endif
280  int operating_point;
281  int current_operating_point;
282  int seen_frame_header;
283  // The expected start_tile (tg_start syntax element) of the next tile group.
284  int next_start_tile;
285 
286  // State if the camera frame header is already decoded while
287  // large_scale_tile = 1.
288  int camera_frame_header_ready;
289  size_t frame_header_size;
290  DataBuffer obu_size_hdr;
291  int output_frame_width_in_tiles_minus_1;
292  int output_frame_height_in_tiles_minus_1;
293  int tile_count_minus_1;
294  uint32_t coded_tile_data_size;
295  unsigned int ext_tile_debug; // for ext-tile software debug & testing
296 
297  // Decoder has 3 modes of operation:
298  // (1) Single-threaded decoding.
299  // (2) Multi-threaded decoding with each tile decoded in parallel.
300  // (3) In addition to (2), each thread decodes 1 superblock row in parallel.
301  // row_mt = 1 triggers mode (3) above, while row_mt = 0, will trigger mode (1)
302  // or (2) depending on 'max_threads'.
303  unsigned int row_mt;
304 
305  EXTERNAL_REFERENCES ext_refs;
306  YV12_BUFFER_CONFIG tile_list_outbuf;
307 
308  // Coding block buffer for the current frame.
309  // Allocated and used only for multi-threaded decoding with 'row_mt == 0'.
310  // See also: similar buffer in 'ThreadData' struct.
311  CB_BUFFER *cb_buffer_base;
312  // Allocated size of 'cb_buffer_base'. Currently same as the number of
313  // superblocks in the coded frame.
314  int cb_buffer_alloc_size;
315 
316  int allocated_row_mt_sync_rows;
317 
318 #if CONFIG_MULTITHREAD
319  pthread_mutex_t *row_mt_mutex_;
320  pthread_cond_t *row_mt_cond_;
321 #endif
322 
323  AV1DecRowMTInfo frame_row_mt_info;
324  aom_metadata_array_t *metadata;
325 
326  int context_update_tile_id;
327  int skip_loop_filter;
328  int skip_film_grain;
329  int is_annexb;
330  int valid_for_referencing[REF_FRAMES];
331  int is_fwd_kf_present;
332  int is_arf_frame_present;
333  int num_tile_groups;
334  aom_s_frame_info sframe_info;
335 } AV1Decoder;
336 
337 // Returns 0 on success. Sets pbi->common.error.error_code to a nonzero error
338 // code and returns a nonzero value on failure.
339 int av1_receive_compressed_data(struct AV1Decoder *pbi, size_t size,
340  const uint8_t **psource);
341 
342 // Get the frame at a particular index in the output queue
343 int av1_get_raw_frame(AV1Decoder *pbi, size_t index, YV12_BUFFER_CONFIG **sd,
344  aom_film_grain_t **grain_params);
345 
346 int av1_get_frame_to_show(struct AV1Decoder *pbi, YV12_BUFFER_CONFIG *frame);
347 
348 aom_codec_err_t av1_copy_reference_dec(struct AV1Decoder *pbi, int idx,
349  YV12_BUFFER_CONFIG *sd);
350 
351 aom_codec_err_t av1_set_reference_dec(AV1_COMMON *cm, int idx,
352  int use_external_ref,
353  YV12_BUFFER_CONFIG *sd);
354 aom_codec_err_t av1_copy_new_frame_dec(AV1_COMMON *cm,
355  YV12_BUFFER_CONFIG *new_frame,
356  YV12_BUFFER_CONFIG *sd);
357 
358 struct AV1Decoder *av1_decoder_create(BufferPool *const pool);
359 
360 void av1_decoder_remove(struct AV1Decoder *pbi);
361 void av1_dealloc_dec_jobs(struct AV1DecTileMTData *tile_mt_info);
362 
363 void av1_dec_row_mt_dealloc(AV1DecRowMTSync *dec_row_mt_sync);
364 
365 void av1_dec_free_cb_buf(AV1Decoder *pbi);
366 
367 static INLINE void decrease_ref_count(RefCntBuffer *const buf,
368  BufferPool *const pool) {
369  if (buf != NULL) {
370  --buf->ref_count;
371  // Reference counts should never become negative. If this assertion fails,
372  // there is a bug in our reference count management.
373  assert(buf->ref_count >= 0);
374  // A worker may only get a free framebuffer index when calling get_free_fb.
375  // But the raw frame buffer is not set up until we finish decoding header.
376  // So if any error happens during decoding header, frame_bufs[idx] will not
377  // have a valid raw frame buffer.
378  if (buf->ref_count == 0 && buf->raw_frame_buffer.data) {
379  pool->release_fb_cb(pool->cb_priv, &buf->raw_frame_buffer);
380  buf->raw_frame_buffer.data = NULL;
381  buf->raw_frame_buffer.size = 0;
382  buf->raw_frame_buffer.priv = NULL;
383  }
384  }
385 }
386 
387 #define ACCT_STR __func__
388 static INLINE int av1_read_uniform(aom_reader *r, int n) {
389  const int l = get_unsigned_bits(n);
390  const int m = (1 << l) - n;
391  const int v = aom_read_literal(r, l - 1, ACCT_STR);
392  assert(l != 0);
393  if (v < m)
394  return v;
395  else
396  return (v << 1) - m + aom_read_literal(r, 1, ACCT_STR);
397 }
398 
399 typedef void (*palette_visitor_fn_t)(MACROBLOCKD *const xd, int plane,
400  aom_reader *r);
401 
402 void av1_visit_palette(AV1Decoder *const pbi, MACROBLOCKD *const xd,
403  aom_reader *r, palette_visitor_fn_t visit);
404 
405 typedef void (*block_visitor_fn_t)(AV1Decoder *const pbi, ThreadData *const td,
406  int mi_row, int mi_col, aom_reader *r,
407  PARTITION_TYPE partition, BLOCK_SIZE bsize);
408 
411 #ifdef __cplusplus
412 } // extern "C"
413 #endif
414 
415 #endif // AOM_AV1_DECODER_DECODER_H_
void(* aom_inspect_cb)(void *decoder, void *ctx)
Definition: aomdx.h:55
int corrupted
Definition: decoder.h:53
eob_info * eob_data[3]
Definition: decoder.h:73
uint16_t txb_offset[3]
Definition: decoder.h:78
tran_low_t * dqcoeff_block[3]
Definition: decoder.h:63
Describes the codec algorithm interface to applications.
Contains coding block data required by the decoder.
Definition: decoder.h:45
uint16_t cb_offset[3]
Definition: decoder.h:68
uint8_t ref_mv_count[MODE_CTX_REF_FRAMES]
Definition: decoder.h:83
struct aom_metadata_array aom_metadata_array_t
Array of aom_metadata structs for an image.
Definition: aom_image.h:160
YV12 frame buffer data structure.
Definition: yv12config.h:38
uint8_t * mc_buf[2]
Definition: decoder.h:58
MACROBLOCKD xd
Definition: decoder.h:49
Structure to hold information about S_FRAME.
Definition: aomdx.h:153
Variables related to current coding block.
Definition: blockd.h:568
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:723
struct Accounting Accounting
Definition: aomdx.h:50