decoder.h (16750B)
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 36 /*! 37 * \brief Contains coding block data required by the decoder. 38 * 39 * This includes: 40 * - Coding block info that is common between encoder and decoder. 41 * - Other coding block info only needed by the decoder. 42 * Contrast this with a similar struct MACROBLOCK on encoder side. 43 * This data is also common between ThreadData and AV1Decoder structs. 44 */ 45 typedef struct DecoderCodingBlock { 46 /*! 47 * Coding block info that is common between encoder and decoder. 48 */ 49 DECLARE_ALIGNED(32, MACROBLOCKD, xd); 50 /*! 51 * True if the at least one of the coding blocks decoded was corrupted. 52 */ 53 int corrupted; 54 /*! 55 * Pointer to 'mc_buf' inside 'pbi->td' (single-threaded decoding) or 56 * 'pbi->thread_data[i].td' (multi-threaded decoding). 57 */ 58 uint8_t *mc_buf[2]; 59 /*! 60 * Pointer to 'dqcoeff' inside 'td->cb_buffer_base' or 'pbi->cb_buffer_base' 61 * with appropriate offset for the current superblock, for each plane. 62 */ 63 tran_low_t *dqcoeff_block[MAX_MB_PLANE]; 64 /*! 65 * cb_offset[p] is the offset into the dqcoeff_block[p] for the current coding 66 * block, for each plane 'p'. 67 */ 68 uint16_t cb_offset[MAX_MB_PLANE]; 69 /*! 70 * Pointer to 'eob_data' inside 'td->cb_buffer_base' or 'pbi->cb_buffer_base' 71 * with appropriate offset for the current superblock, for each plane. 72 */ 73 eob_info *eob_data[MAX_MB_PLANE]; 74 /*! 75 * txb_offset[p] is the offset into the eob_data[p] for the current coding 76 * block, for each plane 'p'. 77 */ 78 uint16_t txb_offset[MAX_MB_PLANE]; 79 /*! 80 * ref_mv_count[i] specifies the number of number of motion vector candidates 81 * in xd->ref_mv_stack[i]. 82 */ 83 uint8_t ref_mv_count[MODE_CTX_REF_FRAMES]; 84 } DecoderCodingBlock; 85 86 /*!\cond */ 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 #if CONFIG_COLLECT_COMPONENT_TIMING 233 #include "aom_ports/aom_timer.h" 234 // Adjust the following to add new components. 235 enum { 236 av1_decode_tg_tiles_and_wrapup_time, 237 aom_decode_frame_from_obus_time, 238 kTimingComponents, 239 } UENUM1BYTE(TIMING_COMPONENT); 240 241 static inline char const *get_component_name(int index) { 242 switch (index) { 243 case av1_decode_tg_tiles_and_wrapup_time: 244 return "av1_decode_tg_tiles_and_wrapup_time"; 245 case aom_decode_frame_from_obus_time: 246 return "aom_decode_frame_from_obus_time"; 247 248 default: assert(0); 249 } 250 return "error"; 251 } 252 #endif 253 254 typedef struct AV1Decoder { 255 DecoderCodingBlock dcb; 256 257 DECLARE_ALIGNED(32, AV1_COMMON, common); 258 259 AVxWorker lf_worker; 260 AV1LfSync lf_row_sync; 261 AV1LrSync lr_row_sync; 262 AV1LrStruct lr_ctxt; 263 AV1CdefSync cdef_sync; 264 AV1CdefWorkerData *cdef_worker; 265 AVxWorker *tile_workers; 266 int num_workers; 267 DecWorkerData *thread_data; 268 ThreadData td; 269 TileDataDec *tile_data; 270 int allocated_tiles; 271 272 TileBufferDec tile_buffers[MAX_TILE_ROWS][MAX_TILE_COLS]; 273 AV1DecTileMT tile_mt_info; 274 275 // Each time the decoder is called, we expect to receive a full temporal unit. 276 // This can contain up to one shown frame per spatial layer in the current 277 // operating point (note that some layers may be entirely omitted). 278 // If the 'output_all_layers' option is true, we save all of these shown 279 // frames so that they can be returned to the application. If the 280 // 'output_all_layers' option is false, then we only output one image per 281 // temporal unit. 282 // 283 // Note: The saved buffers are released at the start of the next time the 284 // application calls aom_codec_decode(). 285 int output_all_layers; 286 RefCntBuffer *output_frames[MAX_NUM_SPATIAL_LAYERS]; 287 size_t num_output_frames; // How many frames are queued up so far? 288 289 // In order to properly support random-access decoding, we need 290 // to behave slightly differently for the very first frame we decode. 291 // So we track whether this is the first frame or not. 292 int decoding_first_frame; 293 294 int allow_lowbitdepth; 295 int max_threads; 296 int inv_tile_order; 297 int need_resync; // wait for key/intra-only frame. 298 int reset_decoder_state; 299 300 int tile_size_bytes; 301 int tile_col_size_bytes; 302 int dec_tile_row, dec_tile_col; // always -1 for non-VR tile encoding 303 #if CONFIG_ACCOUNTING 304 int acct_enabled; 305 Accounting accounting; 306 #endif 307 int sequence_header_ready; 308 int sequence_header_changed; 309 #if CONFIG_INSPECTION 310 aom_inspect_cb inspect_cb; 311 void *inspect_ctx; 312 #endif 313 int operating_point; 314 int current_operating_point; 315 int seen_frame_header; 316 // The expected start_tile (tg_start syntax element) of the next tile group. 317 int next_start_tile; 318 319 // State if the camera frame header is already decoded while 320 // large_scale_tile = 1. 321 int camera_frame_header_ready; 322 size_t frame_header_size; 323 DataBuffer obu_size_hdr; 324 int output_frame_width_in_tiles_minus_1; 325 int output_frame_height_in_tiles_minus_1; 326 int tile_count_minus_1; 327 uint32_t coded_tile_data_size; 328 unsigned int ext_tile_debug; // for ext-tile software debug & testing 329 330 // Decoder has 3 modes of operation: 331 // (1) Single-threaded decoding. 332 // (2) Multi-threaded decoding with each tile decoded in parallel. 333 // (3) In addition to (2), each thread decodes 1 superblock row in parallel. 334 // row_mt = 1 triggers mode (3) above, while row_mt = 0, will trigger mode (1) 335 // or (2) depending on 'max_threads'. 336 unsigned int row_mt; 337 338 EXTERNAL_REFERENCES ext_refs; 339 YV12_BUFFER_CONFIG tile_list_outbuf; 340 341 // Coding block buffer for the current frame. 342 // Allocated and used only for multi-threaded decoding with 'row_mt == 0'. 343 // See also: similar buffer in 'ThreadData' struct. 344 CB_BUFFER *cb_buffer_base; 345 // Allocated size of 'cb_buffer_base'. Currently same as the number of 346 // superblocks in the coded frame. 347 int cb_buffer_alloc_size; 348 349 int allocated_row_mt_sync_rows; 350 351 #if CONFIG_MULTITHREAD 352 pthread_mutex_t *row_mt_mutex_; 353 pthread_cond_t *row_mt_cond_; 354 #endif 355 356 AV1DecRowMTInfo frame_row_mt_info; 357 aom_metadata_array_t *metadata; 358 359 int context_update_tile_id; 360 int skip_loop_filter; 361 int skip_film_grain; 362 int is_annexb; 363 int valid_for_referencing[REF_FRAMES]; 364 int is_fwd_kf_present; 365 int is_arf_frame_present; 366 int num_tile_groups; 367 aom_s_frame_info sframe_info; 368 369 /*! 370 * Elements part of the sequence header, that are applicable for all the 371 * frames in the video. 372 */ 373 SequenceHeader seq_params; 374 375 /*! 376 * If true, buffer removal times are present. 377 */ 378 bool buffer_removal_time_present; 379 380 /*! 381 * Code and details about current error status. 382 */ 383 struct aom_internal_error_info error; 384 385 /*! 386 * Number of temporal layers: may be > 1 for SVC (scalable vector coding). 387 */ 388 unsigned int number_temporal_layers; 389 390 /*! 391 * Number of spatial layers: may be > 1 for SVC (scalable vector coding). 392 */ 393 unsigned int number_spatial_layers; 394 395 #if CONFIG_COLLECT_COMPONENT_TIMING 396 /*! 397 * component_time[] are initialized to zero while decoder starts. 398 */ 399 uint64_t component_time[kTimingComponents]; 400 struct aom_usec_timer component_timer[kTimingComponents]; 401 /*! 402 * frame_component_time[] are initialized to zero at beginning of each frame. 403 */ 404 uint64_t frame_component_time[kTimingComponents]; 405 #endif 406 } AV1Decoder; 407 408 // Returns 0 on success. Sets pbi->common.error.error_code to a nonzero error 409 // code and returns a nonzero value on failure. 410 int av1_receive_compressed_data(struct AV1Decoder *pbi, size_t size, 411 const uint8_t **psource); 412 413 // Get the frame at a particular index in the output queue 414 int av1_get_raw_frame(AV1Decoder *pbi, size_t index, YV12_BUFFER_CONFIG **sd, 415 aom_film_grain_t **grain_params); 416 417 int av1_get_frame_to_show(struct AV1Decoder *pbi, YV12_BUFFER_CONFIG *frame); 418 419 aom_codec_err_t av1_copy_reference_dec(struct AV1Decoder *pbi, int idx, 420 YV12_BUFFER_CONFIG *sd); 421 422 aom_codec_err_t av1_set_reference_dec(AV1_COMMON *cm, int idx, 423 int use_external_ref, 424 YV12_BUFFER_CONFIG *sd); 425 aom_codec_err_t av1_copy_new_frame_dec(AV1_COMMON *cm, 426 YV12_BUFFER_CONFIG *new_frame, 427 YV12_BUFFER_CONFIG *sd); 428 429 struct AV1Decoder *av1_decoder_create(BufferPool *const pool); 430 431 void av1_decoder_remove(struct AV1Decoder *pbi); 432 void av1_dealloc_dec_jobs(struct AV1DecTileMTData *tile_mt_info); 433 434 void av1_dec_row_mt_dealloc(AV1DecRowMTSync *dec_row_mt_sync); 435 436 void av1_dec_free_cb_buf(AV1Decoder *pbi); 437 438 static inline void decrease_ref_count(RefCntBuffer *const buf, 439 BufferPool *const pool) { 440 if (buf != NULL) { 441 --buf->ref_count; 442 // Reference counts should never become negative. If this assertion fails, 443 // there is a bug in our reference count management. 444 assert(buf->ref_count >= 0); 445 // A worker may only get a free framebuffer index when calling get_free_fb. 446 // But the raw frame buffer is not set up until we finish decoding header. 447 // So if any error happens during decoding header, frame_bufs[idx] will not 448 // have a valid raw frame buffer. 449 if (buf->ref_count == 0 && buf->raw_frame_buffer.data) { 450 pool->release_fb_cb(pool->cb_priv, &buf->raw_frame_buffer); 451 buf->raw_frame_buffer.data = NULL; 452 buf->raw_frame_buffer.size = 0; 453 buf->raw_frame_buffer.priv = NULL; 454 } 455 } 456 } 457 458 #define ACCT_STR __func__ 459 static inline int av1_read_uniform(aom_reader *r, int n) { 460 const int l = get_unsigned_bits(n); 461 const int m = (1 << l) - n; 462 const int v = aom_read_literal(r, l - 1, ACCT_STR); 463 assert(l != 0); 464 if (v < m) 465 return v; 466 else 467 return (v << 1) - m + aom_read_literal(r, 1, ACCT_STR); 468 } 469 470 typedef void (*palette_visitor_fn_t)(MACROBLOCKD *const xd, int plane, 471 aom_reader *r); 472 473 void av1_visit_palette(AV1Decoder *const pbi, MACROBLOCKD *const xd, 474 aom_reader *r, palette_visitor_fn_t visit); 475 476 typedef void (*block_visitor_fn_t)(AV1Decoder *const pbi, ThreadData *const td, 477 int mi_row, int mi_col, aom_reader *r, 478 PARTITION_TYPE partition, BLOCK_SIZE bsize); 479 480 /*!\endcond */ 481 482 #if CONFIG_COLLECT_COMPONENT_TIMING 483 static inline void start_timing(AV1Decoder *pbi, int component) { 484 aom_usec_timer_start(&pbi->component_timer[component]); 485 } 486 static inline void end_timing(AV1Decoder *pbi, int component) { 487 aom_usec_timer_mark(&pbi->component_timer[component]); 488 pbi->frame_component_time[component] += 489 aom_usec_timer_elapsed(&pbi->component_timer[component]); 490 } 491 492 static inline char const *get_frame_type_enum(int type) { 493 switch (type) { 494 case 0: return "KEY_FRAME"; 495 case 1: return "INTER_FRAME"; 496 case 2: return "INTRA_ONLY_FRAME"; 497 case 3: return "S_FRAME"; 498 default: assert(0); 499 } 500 return "error"; 501 } 502 #endif 503 504 #ifdef __cplusplus 505 } // extern "C" 506 #endif 507 508 #endif // AOM_AV1_DECODER_DECODER_H_