Line data Source code
1 : /*
2 : * Copyright(c) 2019 Intel Corporation
3 : * SPDX - License - Identifier: BSD - 2 - Clause - Patent
4 : */
5 :
6 : #ifndef EbBitstreamUnit_h
7 : #define EbBitstreamUnit_h
8 :
9 : #include "EbDefinitions.h"
10 : #include "EbUtility.h"
11 : #include "EbObject.h"
12 : #include <stdint.h>
13 : #ifdef __cplusplus
14 : extern "C" {
15 : #endif
16 :
17 : #ifdef _MSC_VER
18 : #if defined(_M_X64) || defined(_M_IX86)
19 : #include <intrin.h>
20 : #define USE_MSC_INTRINSICS
21 : #endif
22 : #endif
23 :
24 : // Bistream Slice Buffer Size
25 : #define EB_BITSTREAM_SLICE_BUFFER_SIZE 0x300000
26 : #define SLICE_HEADER_COUNT 256
27 :
28 : /**********************************
29 : * Bitstream Unit Types
30 : **********************************/
31 : typedef struct OutputBitstreamUnit
32 : {
33 : EbDctor dctor;
34 : uint32_t size; // allocated buffer size
35 : uint32_t written_bits_count; // count of written bits
36 : uint8_t *buffer_begin_av1; // the byte buffer
37 : uint8_t *buffer_av1; // the byte buffer
38 : } OutputBitstreamUnit;
39 :
40 : /**********************************
41 : * Extern Function Declarations
42 : **********************************/
43 : extern EbErrorType output_bitstream_unit_ctor(
44 : OutputBitstreamUnit *bitstream_ptr,
45 : uint32_t buffer_size);
46 :
47 : extern EbErrorType output_bitstream_reset(OutputBitstreamUnit *bitstream_ptr);
48 :
49 : extern EbErrorType output_bitstream_rbsp_to_payload(
50 : OutputBitstreamUnit *bitstream_ptr,
51 : EbByte output_buffer,
52 : uint32_t *output_buffer_index,
53 : uint32_t *output_buffer_size,
54 : uint32_t startLocation);
55 :
56 : /********************************************************************************************************************************/
57 : /********************************************************************************************************************************/
58 : /********************************************************************************************************************************/
59 : #include "EbCabacContextModel.h"
60 : /********************************************************************************************************************************/
61 : // bitops.h
62 : // These versions of get_msb() are only valid when n != 0 because all
63 : // of the optimized versions are undefined when n == 0:
64 : // https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
65 :
66 : // use GNU builtins where available.
67 : #if defined(__GNUC__) && \
68 : ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
69 106325022 : static INLINE int32_t get_msb(uint32_t n) {
70 : assert(n != 0);
71 106325022 : return 31 ^ __builtin_clz(n);
72 : }
73 : #elif defined(USE_MSC_INTRINSICS)
74 : #pragma intrinsic(_BitScanReverse)
75 :
76 : static INLINE int32_t get_msb(uint32_t n) {
77 : unsigned long first_set_bit;
78 : assert(n != 0);
79 : _BitScanReverse(&first_set_bit, n);
80 : return first_set_bit;
81 : }
82 : #undef USE_MSC_INTRINSICS
83 : #else
84 : // Returns (int32_t)floor(log2(n)). n must be > 0.
85 : /*static*/ INLINE int32_t get_msb(uint32_t n) {
86 : int32_t log = 0;
87 : uint32_t value = n;
88 : int32_t i;
89 :
90 : assert(n != 0);
91 :
92 : for (i = 4; i >= 0; --i) {
93 : const int32_t shift = (1 << i);
94 : const uint32_t x = value >> shift;
95 : if (x != 0) {
96 : value = x;
97 : log += shift;
98 : }
99 : }
100 : return log;
101 : }
102 : #endif
103 : /********************************************************************************************************************************/
104 : //odintrin.h
105 : typedef int32_t od_coeff;
106 :
107 : #define OD_DIVU_DMAX (1024)
108 :
109 : extern uint32_t od_divu_small_consts[OD_DIVU_DMAX][2];
110 :
111 : /*Translate unsigned division by small divisors into multiplications.*/
112 : #define OD_DIVU_SMALL(_x, _d) \
113 : ((uint32_t)((od_divu_small_consts[(_d)-1][0] * (uint64_t)(_x) + \
114 : od_divu_small_consts[(_d)-1][1]) >> \
115 : 32) >> \
116 : (OD_ILOG_NZ(_d) - 1))
117 :
118 : #define OD_DIVU(_x, _d) \
119 : (((_d) < OD_DIVU_DMAX) ? (OD_DIVU_SMALL((_x), (_d))) : ((_x) / (_d)))
120 :
121 : #define OD_MINI MIN
122 : #define OD_MAXI MAX
123 : #define OD_CLAMPI(min, val, max) (OD_MAXI(min, OD_MINI(val, max)))
124 :
125 : #define OD_CLZ0 (1)
126 : #define OD_CLZ(x) (-get_msb(x))
127 : #define OD_ILOG_NZ(x) (OD_CLZ0 - OD_CLZ(x))
128 :
129 : /*Enable special features for gcc and compatible compilers.*/
130 : #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
131 : #define OD_GNUC_PREREQ(maj, min, pat) \
132 : ((__GNUC__ << 16) + (__GNUC_MINOR__ << 8) + __GNUC_PATCHLEVEL__ >= \
133 : ((maj) << 16) + ((min) << 8) + pat) // NOLINT
134 : #else
135 : #define OD_GNUC_PREREQ(maj, min, pat) (0)
136 : #endif
137 :
138 : #if OD_GNUC_PREREQ(3, 4, 0)
139 : #define OD_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
140 : #else
141 : #define OD_WARN_UNUSED_RESULT
142 : #endif
143 :
144 : #if OD_GNUC_PREREQ(3, 4, 0)
145 : #define OD_ARG_NONNULL(x) __attribute__((__nonnull__(x)))
146 : #else
147 : #define OD_ARG_NONNULL(x)
148 : #endif
149 :
150 : /** Copy n elements of memory from src to dst. The 0* term provides
151 : compile-time type checking */
152 : #if !defined(OVERRIDE_OD_COPY)
153 : #define OD_COPY(dst, src, n) \
154 : (memcpy((dst), (src), sizeof(*(dst)) * (n) + 0 * ((dst) - (src))))
155 : #endif
156 :
157 : /** Copy n elements of memory from src to dst, allowing overlapping regions.
158 : The 0* term provides compile-time type checking */
159 : #if !defined(OVERRIDE_OD_MOVE)
160 : # define OD_MOVE(dst, src, n) \
161 : (memmove((dst), (src), sizeof(*(dst))*(n) + 0*((dst) - (src)) ))
162 : #endif
163 :
164 : /*All of these macros should expect floats as arguments.*/
165 : # define OD_SIGNMASK(a) (-((a) < 0))
166 : # define OD_FLIPSIGNI(a, b) (((a) + OD_SIGNMASK(b)) ^ OD_SIGNMASK(b))
167 :
168 : /********************************************************************************************************************************/
169 : //entcode.h
170 : #define EC_PROB_SHIFT 6
171 : #define EC_MIN_PROB 4 // must be <= (1<<EC_PROB_SHIFT)/16
172 :
173 : /*OPT: od_ec_window must be at least 32 bits, but if you have fast arithmetic
174 : on a larger type, you can speed up the decoder by using it here.*/
175 : typedef uint32_t od_ec_window;
176 :
177 : #define OD_EC_WINDOW_SIZE ((int32_t)sizeof(od_ec_window) * CHAR_BIT)
178 :
179 : /*The resolution of fractional-precision bit usage measurements, i.e.,
180 : 3 => 1/8th bits.*/
181 : #define OD_BITRES (3)
182 :
183 : #define OD_ICDF AOM_ICDF
184 :
185 : /*See entcode.c for further documentation.*/
186 :
187 : OD_WARN_UNUSED_RESULT uint32_t eb_od_ec_tell_frac(uint32_t nbits_total,
188 : uint32_t rng);
189 :
190 : /********************************************************************************************************************************/
191 : //entenc.h
192 : typedef struct OdEcEnc OdEcEnc;
193 :
194 : #define OD_MEASURE_EC_OVERHEAD (0)
195 :
196 : /*The entropy encoder context.*/
197 : struct OdEcEnc
198 : {
199 : /*Buffered output.
200 : This contains only the raw bits until the final call to eb_od_ec_enc_done(),
201 : where all the arithmetic-coded data gets prepended to it.*/
202 : uint8_t *buf;
203 : /*The size of the buffer.*/
204 : uint32_t storage;
205 : /*The offset at which the last byte containing raw bits was written.*/
206 :
207 : /*A buffer for output bytes with their associated carry flags.*/
208 : uint16_t *precarry_buf;
209 : /*The size of the pre-carry buffer.*/
210 : uint32_t precarry_storage;
211 : /*The offset at which the next entropy-coded byte will be written.*/
212 : uint32_t offs;
213 : /*The low end of the current range.*/
214 : od_ec_window low;
215 : /*The number of values in the current range.*/
216 : uint16_t rng;
217 : /*The number of bits of data in the current value.*/
218 : int16_t cnt;
219 : /*Nonzero if an error occurred.*/
220 : int32_t error;
221 : #if OD_MEASURE_EC_OVERHEAD
222 : double entropy;
223 : int32_t nb_symbols;
224 : #endif
225 : };
226 :
227 : /*See entenc.c for further documentation.*/
228 :
229 : void eb_od_ec_enc_init(OdEcEnc *enc, uint32_t size) OD_ARG_NONNULL(1);
230 : void eb_od_ec_enc_reset(OdEcEnc *enc) OD_ARG_NONNULL(1);
231 : void eb_od_ec_enc_clear(OdEcEnc *enc) OD_ARG_NONNULL(1);
232 :
233 : void eb_od_ec_encode_bool_q15(OdEcEnc *enc, int32_t val, unsigned f_q15)
234 : OD_ARG_NONNULL(1);
235 : void eb_od_ec_encode_cdf_q15(OdEcEnc *enc, int32_t s, const uint16_t *cdf, int32_t nsyms)
236 : OD_ARG_NONNULL(1) OD_ARG_NONNULL(3);
237 :
238 : void od_ec_enc_bits(OdEcEnc *enc, uint32_t fl, unsigned ftb)
239 : OD_ARG_NONNULL(1);
240 :
241 : OD_WARN_UNUSED_RESULT uint8_t *eb_od_ec_enc_done(OdEcEnc *enc,
242 : uint32_t *nbytes)
243 : OD_ARG_NONNULL(1) OD_ARG_NONNULL(2);
244 :
245 : OD_WARN_UNUSED_RESULT int32_t eb_od_ec_enc_tell(const OdEcEnc *enc)
246 : OD_ARG_NONNULL(1);
247 :
248 : void eb_od_ec_enc_checkpoint(OdEcEnc *dst, const OdEcEnc *src);
249 : void eb_od_ec_enc_rollback(OdEcEnc *dst, const OdEcEnc *src);
250 :
251 : /********************************************************************************************************************************/
252 : //daalaboolwriter.h
253 : struct DaalaWriter {
254 : uint32_t pos;
255 : uint8_t *buffer;
256 : OdEcEnc ec;
257 : uint8_t allow_update_cdf;
258 : };
259 :
260 : typedef struct DaalaWriter DaalaWriter;
261 :
262 : void eb_aom_daala_start_encode(DaalaWriter *w, uint8_t *buffer);
263 : int32_t eb_aom_daala_stop_encode(DaalaWriter *w);
264 :
265 169400 : static INLINE void aom_daala_write(DaalaWriter *w, int32_t bit, int32_t prob) {
266 169400 : int32_t p = (0x7FFFFF - (prob << 15) + prob) >> 8;
267 : #if CONFIG_BITSTREAM_DEBUG
268 : AomCdfProb cdf[2] = { (AomCdfProb)p, 32767 };
269 : bitstream_queue_push(bit, cdf, 2);
270 : #endif
271 169400 : eb_od_ec_encode_bool_q15(&w->ec, bit, p);
272 169400 : }
273 :
274 962413 : static INLINE void daala_write_symbol(DaalaWriter *w, int32_t symb,
275 : const AomCdfProb *cdf, int32_t nsymbs) {
276 : #if CONFIG_BITSTREAM_DEBUG
277 : bitstream_queue_push(symb, cdf, nsymbs);
278 : #endif
279 962413 : eb_od_ec_encode_cdf_q15(&w->ec, symb, cdf, nsymbs);
280 962489 : }
281 :
282 : /********************************************************************************************************************************/
283 : // bitwriter.h
284 : typedef struct DaalaWriter AomWriter;
285 :
286 : typedef struct TokenStats
287 : {
288 : int32_t cost;
289 : #if CONFIG_RD_DEBUG
290 : int32_t txb_coeff_cost_map[TXB_COEFF_COST_MAP_SIZE][TXB_COEFF_COST_MAP_SIZE];
291 : #endif
292 : } TokenStats;
293 :
294 : static INLINE void init_token_stats(TokenStats *token_stats) {
295 : #if CONFIG_RD_DEBUG
296 : int32_t r, c;
297 : for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r) {
298 : for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c)
299 : token_stats->txb_coeff_cost_map[r][c] = 0;
300 : }
301 : #endif
302 : token_stats->cost = 0;
303 : }
304 :
305 120 : static INLINE void aom_start_encode(AomWriter *bc, uint8_t *buffer) {
306 120 : eb_aom_daala_start_encode(bc, buffer);
307 120 : }
308 :
309 120 : static INLINE int32_t aom_stop_encode(AomWriter *bc) {
310 120 : return eb_aom_daala_stop_encode(bc);
311 : }
312 :
313 169400 : static INLINE void aom_write(AomWriter *br, int32_t bit, int32_t probability) {
314 169400 : aom_daala_write(br, bit, probability);
315 169400 : }
316 :
317 169400 : static INLINE void aom_write_bit(AomWriter *w, int32_t bit) {
318 169400 : aom_write(w, bit, 128); // aom_prob_half
319 169400 : }
320 :
321 2233 : static INLINE void aom_write_literal(AomWriter *w, int32_t data, int32_t bits) {
322 : int32_t bit;
323 :
324 6684 : for (bit = bits - 1; bit >= 0; bit--) aom_write_bit(w, 1 & (data >> bit));
325 2233 : }
326 :
327 962403 : static INLINE void aom_write_cdf(AomWriter *w, int32_t symb,
328 : const AomCdfProb *cdf, int32_t nsymbs) {
329 962403 : daala_write_symbol(w, symb, cdf, nsymbs);
330 962489 : }
331 :
332 962432 : static INLINE void aom_write_symbol(AomWriter *w, int32_t symb, AomCdfProb *cdf,
333 : int32_t nsymbs) {
334 962432 : aom_write_cdf(w, symb, cdf, nsymbs);
335 962489 : if (w->allow_update_cdf) update_cdf(cdf, symb, nsymbs);
336 962466 : }
337 :
338 : /********************************************************************************************************************************/
339 : /********************************************************************************************************************************/
340 : #ifdef __cplusplus
341 : }
342 : #endif
343 :
344 : #endif // EbBitstreamUnit_h
|