Home | History | Annotate | Download | only in decoder
      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/thread_common.h"
     23 #include "av1/common/onyxc_int.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 typedef void (*decode_block_visitor_fn_t)(const AV1_COMMON *const cm,
     37                                           MACROBLOCKD *const xd,
     38                                           aom_reader *const r, const int plane,
     39                                           const int row, const int col,
     40                                           const TX_SIZE tx_size);
     41 
     42 typedef void (*predict_inter_block_visitor_fn_t)(AV1_COMMON *const cm,
     43                                                  MACROBLOCKD *const xd,
     44                                                  int mi_row, int mi_col,
     45                                                  BLOCK_SIZE bsize);
     46 
     47 typedef void (*cfl_store_inter_block_visitor_fn_t)(AV1_COMMON *const cm,
     48                                                    MACROBLOCKD *const xd);
     49 
     50 typedef struct ThreadData {
     51   DECLARE_ALIGNED(32, MACROBLOCKD, xd);
     52   CB_BUFFER cb_buffer_base;
     53   aom_reader *bit_reader;
     54   uint8_t *mc_buf[2];
     55   int32_t mc_buf_size;
     56   int mc_buf_use_highbd;  // Boolean: whether the byte pointers stored in
     57                           // mc_buf were converted from highbd pointers.
     58 
     59   CONV_BUF_TYPE *tmp_conv_dst;
     60   uint8_t *tmp_obmc_bufs[2];
     61 
     62   decode_block_visitor_fn_t read_coeffs_tx_intra_block_visit;
     63   decode_block_visitor_fn_t predict_and_recon_intra_block_visit;
     64   decode_block_visitor_fn_t read_coeffs_tx_inter_block_visit;
     65   decode_block_visitor_fn_t inverse_tx_inter_block_visit;
     66   predict_inter_block_visitor_fn_t predict_inter_block_visit;
     67   cfl_store_inter_block_visitor_fn_t cfl_store_inter_block_visit;
     68 } ThreadData;
     69 
     70 typedef struct AV1DecRowMTJobInfo {
     71   int tile_row;
     72   int tile_col;
     73   int mi_row;
     74 } AV1DecRowMTJobInfo;
     75 
     76 typedef struct AV1DecRowMTSyncData {
     77 #if CONFIG_MULTITHREAD
     78   pthread_mutex_t *mutex_;
     79   pthread_cond_t *cond_;
     80 #endif
     81   int allocated_sb_rows;
     82   int *cur_sb_col;
     83   int sync_range;
     84   int mi_rows;
     85   int mi_cols;
     86   int mi_rows_parse_done;
     87   int mi_rows_decode_started;
     88   int num_threads_working;
     89 } AV1DecRowMTSync;
     90 
     91 typedef struct AV1DecRowMTInfo {
     92   int tile_rows_start;
     93   int tile_rows_end;
     94   int tile_cols_start;
     95   int tile_cols_end;
     96   int start_tile;
     97   int end_tile;
     98   int mi_rows_to_decode;
     99 
    100   // Invariant:
    101   //   mi_rows_parse_done >= mi_rows_decode_started.
    102   // mi_rows_parse_done and mi_rows_decode_started are both initialized to 0.
    103   // mi_rows_parse_done is incremented freely. mi_rows_decode_started may only
    104   // be incremented to catch up with mi_rows_parse_done but is not allowed to
    105   // surpass mi_rows_parse_done.
    106   //
    107   // When mi_rows_decode_started reaches mi_rows_to_decode, there are no more
    108   // decode jobs.
    109 
    110   // Indicates the progress of the bit-stream parsing of superblocks.
    111   // Initialized to 0. Incremented by sb_mi_size when parse sb row is done.
    112   int mi_rows_parse_done;
    113   // Indicates the progress of the decoding of superblocks.
    114   // Initialized to 0. Incremented by sb_mi_size when decode sb row is started.
    115   int mi_rows_decode_started;
    116   // Boolean: Initialized to 0 (false). Set to 1 (true) on error to abort
    117   // decoding.
    118   int row_mt_exit;
    119 } AV1DecRowMTInfo;
    120 
    121 typedef struct TileDataDec {
    122   TileInfo tile_info;
    123   aom_reader bit_reader;
    124   DECLARE_ALIGNED(16, FRAME_CONTEXT, tctx);
    125   AV1DecRowMTSync dec_row_mt_sync;
    126 } TileDataDec;
    127 
    128 typedef struct TileBufferDec {
    129   const uint8_t *data;
    130   size_t size;
    131 } TileBufferDec;
    132 
    133 typedef struct DataBuffer {
    134   const uint8_t *data;
    135   size_t size;
    136 } DataBuffer;
    137 
    138 typedef struct EXTERNAL_REFERENCES {
    139   YV12_BUFFER_CONFIG refs[MAX_EXTERNAL_REFERENCES];
    140   int num;
    141 } EXTERNAL_REFERENCES;
    142 
    143 typedef struct TileJobsDec {
    144   TileBufferDec *tile_buffer;
    145   TileDataDec *tile_data;
    146 } TileJobsDec;
    147 
    148 typedef struct AV1DecTileMTData {
    149 #if CONFIG_MULTITHREAD
    150   pthread_mutex_t *job_mutex;
    151 #endif
    152   TileJobsDec *job_queue;
    153   int jobs_enqueued;
    154   int jobs_dequeued;
    155   int alloc_tile_rows;
    156   int alloc_tile_cols;
    157 } AV1DecTileMT;
    158 
    159 typedef struct AV1Decoder {
    160   DECLARE_ALIGNED(32, MACROBLOCKD, mb);
    161 
    162   DECLARE_ALIGNED(32, AV1_COMMON, common);
    163 
    164   AVxWorker lf_worker;
    165   AV1LfSync lf_row_sync;
    166   AV1LrSync lr_row_sync;
    167   AV1LrStruct lr_ctxt;
    168   AVxWorker *tile_workers;
    169   int num_workers;
    170   DecWorkerData *thread_data;
    171   ThreadData td;
    172   TileDataDec *tile_data;
    173   int allocated_tiles;
    174 
    175   TileBufferDec tile_buffers[MAX_TILE_ROWS][MAX_TILE_COLS];
    176   AV1DecTileMT tile_mt_info;
    177 
    178   // Each time the decoder is called, we expect to receive a full temporal unit.
    179   // This can contain up to one shown frame per spatial layer in the current
    180   // operating point (note that some layers may be entirely omitted).
    181   // If the 'output_all_layers' option is true, we save all of these shown
    182   // frames so that they can be returned to the application. If the
    183   // 'output_all_layers' option is false, then we only output one image per
    184   // temporal unit.
    185   //
    186   // Note: The saved buffers are released at the start of the next time the
    187   // application calls aom_codec_decode().
    188   int output_all_layers;
    189   RefCntBuffer *output_frames[MAX_NUM_SPATIAL_LAYERS];
    190   size_t num_output_frames;  // How many frames are queued up so far?
    191 
    192   // In order to properly support random-access decoding, we need
    193   // to behave slightly differently for the very first frame we decode.
    194   // So we track whether this is the first frame or not.
    195   int decoding_first_frame;
    196 
    197   int allow_lowbitdepth;
    198   int max_threads;
    199   int inv_tile_order;
    200   int need_resync;   // wait for key/intra-only frame.
    201   int hold_ref_buf;  // Boolean: whether we are holding reference buffers in
    202                      // common.next_ref_frame_map.
    203   int reset_decoder_state;
    204 
    205   int tile_size_bytes;
    206   int tile_col_size_bytes;
    207   int dec_tile_row, dec_tile_col;  // always -1 for non-VR tile encoding
    208 #if CONFIG_ACCOUNTING
    209   int acct_enabled;
    210   Accounting accounting;
    211 #endif
    212   int tg_size;   // Number of tiles in the current tilegroup
    213   int tg_start;  // First tile in the current tilegroup
    214   int tg_size_bit_offset;
    215   int sequence_header_ready;
    216   int sequence_header_changed;
    217 #if CONFIG_INSPECTION
    218   aom_inspect_cb inspect_cb;
    219   void *inspect_ctx;
    220 #endif
    221   int operating_point;
    222   int current_operating_point;
    223   int seen_frame_header;
    224 
    225   // State if the camera frame header is already decoded while
    226   // large_scale_tile = 1.
    227   int camera_frame_header_ready;
    228   size_t frame_header_size;
    229   DataBuffer obu_size_hdr;
    230   int output_frame_width_in_tiles_minus_1;
    231   int output_frame_height_in_tiles_minus_1;
    232   int tile_count_minus_1;
    233   uint32_t coded_tile_data_size;
    234   unsigned int ext_tile_debug;  // for ext-tile software debug & testing
    235   unsigned int row_mt;
    236   EXTERNAL_REFERENCES ext_refs;
    237   YV12_BUFFER_CONFIG tile_list_outbuf;
    238 
    239   CB_BUFFER *cb_buffer_base;
    240   int cb_buffer_alloc_size;
    241 
    242   int allocated_row_mt_sync_rows;
    243 
    244 #if CONFIG_MULTITHREAD
    245   pthread_mutex_t *row_mt_mutex_;
    246   pthread_cond_t *row_mt_cond_;
    247 #endif
    248 
    249   AV1DecRowMTInfo frame_row_mt_info;
    250 } AV1Decoder;
    251 
    252 // Returns 0 on success. Sets pbi->common.error.error_code to a nonzero error
    253 // code and returns a nonzero value on failure.
    254 int av1_receive_compressed_data(struct AV1Decoder *pbi, size_t size,
    255                                 const uint8_t **psource);
    256 
    257 // Get the frame at a particular index in the output queue
    258 int av1_get_raw_frame(AV1Decoder *pbi, size_t index, YV12_BUFFER_CONFIG **sd,
    259                       aom_film_grain_t **grain_params);
    260 
    261 int av1_get_frame_to_show(struct AV1Decoder *pbi, YV12_BUFFER_CONFIG *frame);
    262 
    263 aom_codec_err_t av1_copy_reference_dec(struct AV1Decoder *pbi, int idx,
    264                                        YV12_BUFFER_CONFIG *sd);
    265 
    266 aom_codec_err_t av1_set_reference_dec(AV1_COMMON *cm, int idx,
    267                                       int use_external_ref,
    268                                       YV12_BUFFER_CONFIG *sd);
    269 aom_codec_err_t av1_copy_new_frame_dec(AV1_COMMON *cm,
    270                                        YV12_BUFFER_CONFIG *new_frame,
    271                                        YV12_BUFFER_CONFIG *sd);
    272 
    273 struct AV1Decoder *av1_decoder_create(BufferPool *const pool);
    274 
    275 void av1_decoder_remove(struct AV1Decoder *pbi);
    276 void av1_dealloc_dec_jobs(struct AV1DecTileMTData *tile_mt_info);
    277 
    278 void av1_dec_row_mt_dealloc(AV1DecRowMTSync *dec_row_mt_sync);
    279 
    280 void av1_dec_free_cb_buf(AV1Decoder *pbi);
    281 
    282 static INLINE void decrease_ref_count(RefCntBuffer *const buf,
    283                                       BufferPool *const pool) {
    284   if (buf != NULL) {
    285     --buf->ref_count;
    286     // Reference counts should never become negative. If this assertion fails,
    287     // there is a bug in our reference count management.
    288     assert(buf->ref_count >= 0);
    289     // A worker may only get a free framebuffer index when calling get_free_fb.
    290     // But the raw frame buffer is not set up until we finish decoding header.
    291     // So if any error happens during decoding header, frame_bufs[idx] will not
    292     // have a valid raw frame buffer.
    293     if (buf->ref_count == 0 && buf->raw_frame_buffer.data) {
    294       pool->release_fb_cb(pool->cb_priv, &buf->raw_frame_buffer);
    295       buf->raw_frame_buffer.data = NULL;
    296       buf->raw_frame_buffer.size = 0;
    297       buf->raw_frame_buffer.priv = NULL;
    298     }
    299   }
    300 }
    301 
    302 #define ACCT_STR __func__
    303 static INLINE int av1_read_uniform(aom_reader *r, int n) {
    304   const int l = get_unsigned_bits(n);
    305   const int m = (1 << l) - n;
    306   const int v = aom_read_literal(r, l - 1, ACCT_STR);
    307   assert(l != 0);
    308   if (v < m)
    309     return v;
    310   else
    311     return (v << 1) - m + aom_read_literal(r, 1, ACCT_STR);
    312 }
    313 
    314 typedef void (*palette_visitor_fn_t)(MACROBLOCKD *const xd, int plane,
    315                                      aom_reader *r);
    316 
    317 void av1_visit_palette(AV1Decoder *const pbi, MACROBLOCKD *const xd, int mi_row,
    318                        int mi_col, aom_reader *r, BLOCK_SIZE bsize,
    319                        palette_visitor_fn_t visit);
    320 
    321 typedef void (*block_visitor_fn_t)(AV1Decoder *const pbi, ThreadData *const td,
    322                                    int mi_row, int mi_col, aom_reader *r,
    323                                    PARTITION_TYPE partition, BLOCK_SIZE bsize);
    324 
    325 #ifdef __cplusplus
    326 }  // extern "C"
    327 #endif
    328 
    329 #endif  // AOM_AV1_DECODER_DECODER_H_
    330