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