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 AV1_DECODER_DECODER_H_
13 #define AV1_DECODER_DECODER_H_
14
15 #include "./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 #if CONFIG_PVQ
33 #include "aom_dsp/entdec.h"
34 #include "av1/decoder/decint.h"
35 #include "av1/encoder/encodemb.h"
36 #endif
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 // TODO(hkuang): combine this with TileWorkerData.
43 typedef struct TileData {
44 AV1_COMMON *cm;
45 aom_reader bit_reader;
46 DECLARE_ALIGNED(16, MACROBLOCKD, xd);
47 /* dqcoeff are shared by all the planes. So planes must be decoded serially */
48 DECLARE_ALIGNED(16, tran_low_t, dqcoeff[MAX_TX_SQUARE]);
49 #if CONFIG_PVQ
50 /* forward transformed predicted image, a reference for PVQ */
51 DECLARE_ALIGNED(16, tran_low_t, pvq_ref_coeff[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
52 #endif
53 #if CONFIG_CFL
54 CFL_CTX cfl;
55 #endif
56 DECLARE_ALIGNED(16, FRAME_CONTEXT, tctx);
57 DECLARE_ALIGNED(16, uint8_t, color_index_map[2][MAX_SB_SQUARE]);
58 #if CONFIG_MRC_TX
59 DECLARE_ALIGNED(16, uint8_t, mrc_mask[MAX_SB_SQUARE]);
60 #endif // CONFIG_MRC_TX
61 } TileData;
62
63 typedef struct TileWorkerData {
64 struct AV1Decoder *pbi;
65 aom_reader bit_reader;
66 FRAME_COUNTS counts;
67 DECLARE_ALIGNED(16, MACROBLOCKD, xd);
68 /* dqcoeff are shared by all the planes. So planes must be decoded serially */
69 DECLARE_ALIGNED(16, tran_low_t, dqcoeff[MAX_TX_SQUARE]);
70 #if CONFIG_PVQ
71 /* forward transformed predicted image, a reference for PVQ */
72 DECLARE_ALIGNED(16, tran_low_t, pvq_ref_coeff[OD_TXSIZE_MAX * OD_TXSIZE_MAX]);
73 #endif
74 #if CONFIG_CFL
75 CFL_CTX cfl;
76 #endif
77 FRAME_CONTEXT tctx;
78 DECLARE_ALIGNED(16, uint8_t, color_index_map[2][MAX_SB_SQUARE]);
79 #if CONFIG_MRC_TX
80 DECLARE_ALIGNED(16, uint8_t, mrc_mask[MAX_SB_SQUARE]);
81 #endif // CONFIG_MRC_TX
82 struct aom_internal_error_info error_info;
83 } TileWorkerData;
84
85 typedef struct TileBufferDec {
86 const uint8_t *data;
87 size_t size;
88 const uint8_t *raw_data_end; // The end of the raw tile buffer in the
89 // bit stream.
90 int col; // only used with multi-threaded decoding
91 } TileBufferDec;
92
93 typedef struct AV1Decoder {
94 DECLARE_ALIGNED(16, MACROBLOCKD, mb);
95
96 DECLARE_ALIGNED(16, AV1_COMMON, common);
97
98 int ready_for_new_data;
99
100 int refresh_frame_flags;
101
102 // TODO(hkuang): Combine this with cur_buf in macroblockd as they are
103 // the same.
104 RefCntBuffer *cur_buf; // Current decoding frame buffer.
105
106 AVxWorker *frame_worker_owner; // frame_worker that owns this pbi.
107 AVxWorker lf_worker;
108 AVxWorker *tile_workers;
109 TileWorkerData *tile_worker_data;
110 TileInfo *tile_worker_info;
111 int num_tile_workers;
112
113 TileData *tile_data;
114 int allocated_tiles;
115
116 TileBufferDec tile_buffers[MAX_TILE_ROWS][MAX_TILE_COLS];
117
118 AV1LfSync lf_row_sync;
119
120 aom_decrypt_cb decrypt_cb;
121 void *decrypt_state;
122
123 int allow_lowbitdepth;
124 int max_threads;
125 int inv_tile_order;
126 int need_resync; // wait for key/intra-only frame.
127 int hold_ref_buf; // hold the reference buffer.
128
129 int tile_size_bytes;
130 #if CONFIG_EXT_TILE
131 int tile_col_size_bytes;
132 int dec_tile_row, dec_tile_col; // always -1 for non-VR tile encoding
133 #endif // CONFIG_EXT_TILE
134 #if CONFIG_ACCOUNTING
135 int acct_enabled;
136 Accounting accounting;
137 #endif
138 size_t uncomp_hdr_size; // Size of the uncompressed header
139 size_t first_partition_size; // Size of the compressed header
140 int tg_size; // Number of tiles in the current tilegroup
141 int tg_start; // First tile in the current tilegroup
142 int tg_size_bit_offset;
143 #if CONFIG_INSPECTION
144 aom_inspect_cb inspect_cb;
145 void *inspect_ctx;
146 #endif
147 } AV1Decoder;
148
149 int av1_receive_compressed_data(struct AV1Decoder *pbi, size_t size,
150 const uint8_t **dest);
151
152 int av1_get_raw_frame(struct AV1Decoder *pbi, YV12_BUFFER_CONFIG *sd);
153
154 int av1_get_frame_to_show(struct AV1Decoder *pbi, YV12_BUFFER_CONFIG *frame);
155
156 aom_codec_err_t av1_copy_reference_dec(struct AV1Decoder *pbi, int idx,
157 YV12_BUFFER_CONFIG *sd);
158
159 aom_codec_err_t av1_set_reference_dec(AV1_COMMON *cm, int idx,
160 YV12_BUFFER_CONFIG *sd);
161
read_marker(aom_decrypt_cb decrypt_cb,void * decrypt_state,const uint8_t * data)162 static INLINE uint8_t read_marker(aom_decrypt_cb decrypt_cb,
163 void *decrypt_state, const uint8_t *data) {
164 if (decrypt_cb) {
165 uint8_t marker;
166 decrypt_cb(decrypt_state, data, &marker, 1);
167 return marker;
168 }
169 return *data;
170 }
171
172 // This function is exposed for use in tests, as well as the inlined function
173 // "read_marker".
174 aom_codec_err_t av1_parse_superframe_index(const uint8_t *data, size_t data_sz,
175 uint32_t sizes[8], int *count,
176 int *index_size,
177 aom_decrypt_cb decrypt_cb,
178 void *decrypt_state);
179
180 struct AV1Decoder *av1_decoder_create(BufferPool *const pool);
181
182 void av1_decoder_remove(struct AV1Decoder *pbi);
183
decrease_ref_count(int idx,RefCntBuffer * const frame_bufs,BufferPool * const pool)184 static INLINE void decrease_ref_count(int idx, RefCntBuffer *const frame_bufs,
185 BufferPool *const pool) {
186 if (idx >= 0) {
187 --frame_bufs[idx].ref_count;
188 // A worker may only get a free framebuffer index when calling get_free_fb.
189 // But the private buffer is not set up until finish decoding header.
190 // So any error happens during decoding header, the frame_bufs will not
191 // have valid priv buffer.
192 if (frame_bufs[idx].ref_count == 0 &&
193 frame_bufs[idx].raw_frame_buffer.priv) {
194 pool->release_fb_cb(pool->cb_priv, &frame_bufs[idx].raw_frame_buffer);
195 }
196 }
197 }
198
199 #if CONFIG_EXT_REFS || CONFIG_TEMPMV_SIGNALING
dec_is_ref_frame_buf(AV1Decoder * const pbi,RefCntBuffer * frame_buf)200 static INLINE int dec_is_ref_frame_buf(AV1Decoder *const pbi,
201 RefCntBuffer *frame_buf) {
202 AV1_COMMON *const cm = &pbi->common;
203 int i;
204 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
205 RefBuffer *const ref_frame = &cm->frame_refs[i];
206 if (ref_frame->idx == INVALID_IDX) continue;
207 if (frame_buf == &cm->buffer_pool->frame_bufs[ref_frame->idx]) break;
208 }
209 return (i < INTER_REFS_PER_FRAME);
210 }
211 #endif // CONFIG_EXT_REFS
212
213 #define ACCT_STR __func__
av1_read_uniform(aom_reader * r,int n)214 static INLINE int av1_read_uniform(aom_reader *r, int n) {
215 const int l = get_unsigned_bits(n);
216 const int m = (1 << l) - n;
217 const int v = aom_read_literal(r, l - 1, ACCT_STR);
218 assert(l != 0);
219 if (v < m)
220 return v;
221 else
222 return (v << 1) - m + aom_read_literal(r, 1, ACCT_STR);
223 }
224
225 #ifdef __cplusplus
226 } // extern "C"
227 #endif
228
229 #endif // AV1_DECODER_DECODER_H_
230