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  // Allocated size of 'mc_buf'.
116  int32_t mc_buf_size;
117  // If true, the pointers in 'mc_buf' were converted from highbd pointers.
118  int mc_buf_use_highbd; // Boolean: whether the byte pointers stored in
119  // mc_buf were converted from highbd pointers.
120 
121  CONV_BUF_TYPE *tmp_conv_dst;
122  uint8_t *tmp_obmc_bufs[2];
123 
124  decode_block_visitor_fn_t read_coeffs_tx_intra_block_visit;
125  decode_block_visitor_fn_t predict_and_recon_intra_block_visit;
126  decode_block_visitor_fn_t read_coeffs_tx_inter_block_visit;
127  decode_block_visitor_fn_t inverse_tx_inter_block_visit;
128  predict_inter_block_visitor_fn_t predict_inter_block_visit;
129  cfl_store_inter_block_visitor_fn_t cfl_store_inter_block_visit;
130 } ThreadData;
131 
132 typedef struct AV1DecRowMTJobInfo {
133  int tile_row;
134  int tile_col;
135  int mi_row;
136 } AV1DecRowMTJobInfo;
137 
138 typedef struct AV1DecRowMTSyncData {
139 #if CONFIG_MULTITHREAD
140  pthread_mutex_t *mutex_;
141  pthread_cond_t *cond_;
142 #endif
143  int allocated_sb_rows;
144  int *cur_sb_col;
145  int sync_range;
146  int mi_rows;
147  int mi_cols;
148  int mi_rows_parse_done;
149  int mi_rows_decode_started;
150  int num_threads_working;
151 } AV1DecRowMTSync;
152 
153 typedef struct AV1DecRowMTInfo {
154  int tile_rows_start;
155  int tile_rows_end;
156  int tile_cols_start;
157  int tile_cols_end;
158  int start_tile;
159  int end_tile;
160  int mi_rows_to_decode;
161 
162  // Invariant:
163  // mi_rows_parse_done >= mi_rows_decode_started.
164  // mi_rows_parse_done and mi_rows_decode_started are both initialized to 0.
165  // mi_rows_parse_done is incremented freely. mi_rows_decode_started may only
166  // be incremented to catch up with mi_rows_parse_done but is not allowed to
167  // surpass mi_rows_parse_done.
168  //
169  // When mi_rows_decode_started reaches mi_rows_to_decode, there are no more
170  // decode jobs.
171 
172  // Indicates the progress of the bit-stream parsing of superblocks.
173  // Initialized to 0. Incremented by sb_mi_size when parse sb row is done.
174  int mi_rows_parse_done;
175  // Indicates the progress of the decoding of superblocks.
176  // Initialized to 0. Incremented by sb_mi_size when decode sb row is started.
177  int mi_rows_decode_started;
178  // Boolean: Initialized to 0 (false). Set to 1 (true) on error to abort
179  // decoding.
180  int row_mt_exit;
181 } AV1DecRowMTInfo;
182 
183 typedef struct TileDataDec {
184  TileInfo tile_info;
185  aom_reader bit_reader;
186  DECLARE_ALIGNED(16, FRAME_CONTEXT, tctx);
187  AV1DecRowMTSync dec_row_mt_sync;
188 } TileDataDec;
189 
190 typedef struct TileBufferDec {
191  const uint8_t *data;
192  size_t size;
193 } TileBufferDec;
194 
195 typedef struct DataBuffer {
196  const uint8_t *data;
197  size_t size;
198 } DataBuffer;
199 
200 typedef struct EXTERNAL_REFERENCES {
201  YV12_BUFFER_CONFIG refs[MAX_EXTERNAL_REFERENCES];
202  int num;
203 } EXTERNAL_REFERENCES;
204 
205 typedef struct TileJobsDec {
206  TileBufferDec *tile_buffer;
207  TileDataDec *tile_data;
208 } TileJobsDec;
209 
210 typedef struct AV1DecTileMTData {
211 #if CONFIG_MULTITHREAD
212  pthread_mutex_t *job_mutex;
213 #endif
214  TileJobsDec *job_queue;
215  int jobs_enqueued;
216  int jobs_dequeued;
217  int alloc_tile_rows;
218  int alloc_tile_cols;
219 } AV1DecTileMT;
220 
221 typedef struct AV1Decoder {
222  DecoderCodingBlock dcb;
223 
224  DECLARE_ALIGNED(32, AV1_COMMON, common);
225 
226  AVxWorker lf_worker;
227  AV1LfSync lf_row_sync;
228  AV1LrSync lr_row_sync;
229  AV1LrStruct lr_ctxt;
230  AVxWorker *tile_workers;
231  int num_workers;
232  DecWorkerData *thread_data;
233  ThreadData td;
234  TileDataDec *tile_data;
235  int allocated_tiles;
236 
237  TileBufferDec tile_buffers[MAX_TILE_ROWS][MAX_TILE_COLS];
238  AV1DecTileMT tile_mt_info;
239 
240  // Each time the decoder is called, we expect to receive a full temporal unit.
241  // This can contain up to one shown frame per spatial layer in the current
242  // operating point (note that some layers may be entirely omitted).
243  // If the 'output_all_layers' option is true, we save all of these shown
244  // frames so that they can be returned to the application. If the
245  // 'output_all_layers' option is false, then we only output one image per
246  // temporal unit.
247  //
248  // Note: The saved buffers are released at the start of the next time the
249  // application calls aom_codec_decode().
250  int output_all_layers;
251  RefCntBuffer *output_frames[MAX_NUM_SPATIAL_LAYERS];
252  size_t num_output_frames; // How many frames are queued up so far?
253 
254  // In order to properly support random-access decoding, we need
255  // to behave slightly differently for the very first frame we decode.
256  // So we track whether this is the first frame or not.
257  int decoding_first_frame;
258 
259  int allow_lowbitdepth;
260  int max_threads;
261  int inv_tile_order;
262  int need_resync; // wait for key/intra-only frame.
263  int reset_decoder_state;
264 
265  int tile_size_bytes;
266  int tile_col_size_bytes;
267  int dec_tile_row, dec_tile_col; // always -1 for non-VR tile encoding
268 #if CONFIG_ACCOUNTING
269  int acct_enabled;
270  Accounting accounting;
271 #endif
272  int sequence_header_ready;
273  int sequence_header_changed;
274 #if CONFIG_INSPECTION
275  aom_inspect_cb inspect_cb;
276  void *inspect_ctx;
277 #endif
278  int operating_point;
279  int current_operating_point;
280  int seen_frame_header;
281  // The expected start_tile (tg_start syntax element) of the next tile group.
282  int next_start_tile;
283 
284  // State if the camera frame header is already decoded while
285  // large_scale_tile = 1.
286  int camera_frame_header_ready;
287  size_t frame_header_size;
288  DataBuffer obu_size_hdr;
289  int output_frame_width_in_tiles_minus_1;
290  int output_frame_height_in_tiles_minus_1;
291  int tile_count_minus_1;
292  uint32_t coded_tile_data_size;
293  unsigned int ext_tile_debug; // for ext-tile software debug & testing
294 
295  // Decoder has 3 modes of operation:
296  // (1) Single-threaded decoding.
297  // (2) Multi-threaded decoding with each tile decoded in parallel.
298  // (3) In addition to (2), each thread decodes 1 superblock row in parallel.
299  // row_mt = 1 triggers mode (3) above, while row_mt = 0, will trigger mode (1)
300  // or (2) depending on 'max_threads'.
301  unsigned int row_mt;
302 
303  EXTERNAL_REFERENCES ext_refs;
304  YV12_BUFFER_CONFIG tile_list_outbuf;
305 
306  // Coding block buffer for the current frame.
307  // Allocated and used only for multi-threaded decoding with 'row_mt == 0'.
308  // See also: similar buffer in 'ThreadData' struct.
309  CB_BUFFER *cb_buffer_base;
310  // Allocated size of 'cb_buffer_base'. Currently same as the number of
311  // superblocks in the coded frame.
312  int cb_buffer_alloc_size;
313 
314  int allocated_row_mt_sync_rows;
315 
316 #if CONFIG_MULTITHREAD
317  pthread_mutex_t *row_mt_mutex_;
318  pthread_cond_t *row_mt_cond_;
319 #endif
320 
321  AV1DecRowMTInfo frame_row_mt_info;
322  aom_metadata_array_t *metadata;
323 
324  int context_update_tile_id;
325  int skip_loop_filter;
326  int skip_film_grain;
327  int is_annexb;
328  int valid_for_referencing[REF_FRAMES];
329  int is_fwd_kf_present;
330  int is_arf_frame_present;
331  int num_tile_groups;
332  aom_s_frame_info sframe_info;
333 } AV1Decoder;
334 
335 // Returns 0 on success. Sets pbi->common.error.error_code to a nonzero error
336 // code and returns a nonzero value on failure.
337 int av1_receive_compressed_data(struct AV1Decoder *pbi, size_t size,
338  const uint8_t **psource);
339 
340 // Get the frame at a particular index in the output queue
341 int av1_get_raw_frame(AV1Decoder *pbi, size_t index, YV12_BUFFER_CONFIG **sd,
342  aom_film_grain_t **grain_params);
343 
344 int av1_get_frame_to_show(struct AV1Decoder *pbi, YV12_BUFFER_CONFIG *frame);
345 
346 aom_codec_err_t av1_copy_reference_dec(struct AV1Decoder *pbi, int idx,
347  YV12_BUFFER_CONFIG *sd);
348 
349 aom_codec_err_t av1_set_reference_dec(AV1_COMMON *cm, int idx,
350  int use_external_ref,
351  YV12_BUFFER_CONFIG *sd);
352 aom_codec_err_t av1_copy_new_frame_dec(AV1_COMMON *cm,
353  YV12_BUFFER_CONFIG *new_frame,
354  YV12_BUFFER_CONFIG *sd);
355 
356 struct AV1Decoder *av1_decoder_create(BufferPool *const pool);
357 
358 void av1_decoder_remove(struct AV1Decoder *pbi);
359 void av1_dealloc_dec_jobs(struct AV1DecTileMTData *tile_mt_info);
360 
361 void av1_dec_row_mt_dealloc(AV1DecRowMTSync *dec_row_mt_sync);
362 
363 void av1_dec_free_cb_buf(AV1Decoder *pbi);
364 
365 static INLINE void decrease_ref_count(RefCntBuffer *const buf,
366  BufferPool *const pool) {
367  if (buf != NULL) {
368  --buf->ref_count;
369  // Reference counts should never become negative. If this assertion fails,
370  // there is a bug in our reference count management.
371  assert(buf->ref_count >= 0);
372  // A worker may only get a free framebuffer index when calling get_free_fb.
373  // But the raw frame buffer is not set up until we finish decoding header.
374  // So if any error happens during decoding header, frame_bufs[idx] will not
375  // have a valid raw frame buffer.
376  if (buf->ref_count == 0 && buf->raw_frame_buffer.data) {
377  pool->release_fb_cb(pool->cb_priv, &buf->raw_frame_buffer);
378  buf->raw_frame_buffer.data = NULL;
379  buf->raw_frame_buffer.size = 0;
380  buf->raw_frame_buffer.priv = NULL;
381  }
382  }
383 }
384 
385 #define ACCT_STR __func__
386 static INLINE int av1_read_uniform(aom_reader *r, int n) {
387  const int l = get_unsigned_bits(n);
388  const int m = (1 << l) - n;
389  const int v = aom_read_literal(r, l - 1, ACCT_STR);
390  assert(l != 0);
391  if (v < m)
392  return v;
393  else
394  return (v << 1) - m + aom_read_literal(r, 1, ACCT_STR);
395 }
396 
397 typedef void (*palette_visitor_fn_t)(MACROBLOCKD *const xd, int plane,
398  aom_reader *r);
399 
400 void av1_visit_palette(AV1Decoder *const pbi, MACROBLOCKD *const xd,
401  aom_reader *r, palette_visitor_fn_t visit);
402 
403 typedef void (*block_visitor_fn_t)(AV1Decoder *const pbi, ThreadData *const td,
404  int mi_row, int mi_col, aom_reader *r,
405  PARTITION_TYPE partition, BLOCK_SIZE bsize);
406 
409 #ifdef __cplusplus
410 } // extern "C"
411 #endif
412 
413 #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:160
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:723
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:38