26 #ifndef AVCODEC_GET_BITS_H
27 #define AVCODEC_GET_BITS_H
48 #ifndef UNCHECKED_BITSTREAM_READER
49 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
56 #if !UNCHECKED_BITSTREAM_READER
57 int size_in_bits_plus8;
61 #define VLC_TYPE int16_t
117 #ifdef LONG_BITSTREAM_READER
118 # define MIN_CACHE_BITS 32
120 # define MIN_CACHE_BITS 25
123 #if UNCHECKED_BITSTREAM_READER
124 #define OPEN_READER(name, gb) \
125 unsigned int name##_index = (gb)->index; \
126 unsigned int av_unused name##_cache = 0
128 #define HAVE_BITS_REMAINING(name, gb) 1
130 #define OPEN_READER(name, gb) \
131 unsigned int name##_index = (gb)->index; \
132 unsigned int av_unused name##_cache = 0; \
133 unsigned int av_unused name##_size_plus8 = \
134 (gb)->size_in_bits_plus8
136 #define HAVE_BITS_REMAINING(name, gb) \
137 name##_index < name##_size_plus8
140 #define CLOSE_READER(name, gb) (gb)->index = name##_index
142 #ifdef BITSTREAM_READER_LE
144 # ifdef LONG_BITSTREAM_READER
145 # define UPDATE_CACHE(name, gb) name##_cache = \
146 AV_RL64((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
148 # define UPDATE_CACHE(name, gb) name##_cache = \
149 AV_RL32((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
152 # define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
156 # ifdef LONG_BITSTREAM_READER
157 # define UPDATE_CACHE(name, gb) name##_cache = \
158 AV_RB64((gb)->buffer + (name##_index >> 3)) >> (32 - (name##_index & 7))
160 # define UPDATE_CACHE(name, gb) name##_cache = \
161 AV_RB32((gb)->buffer + (name##_index >> 3)) << (name##_index & 7)
164 # define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
168 #if UNCHECKED_BITSTREAM_READER
169 # define SKIP_COUNTER(name, gb, num) name##_index += (num)
171 # define SKIP_COUNTER(name, gb, num) \
172 name##_index = FFMIN(name##_size_plus8, name##_index + (num))
175 #define SKIP_BITS(name, gb, num) do { \
176 SKIP_CACHE(name, gb, num); \
177 SKIP_COUNTER(name, gb, num); \
180 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
182 #ifdef BITSTREAM_READER_LE
183 # define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
184 # define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
186 # define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
187 # define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
190 #define GET_CACHE(name, gb) ((uint32_t)name##_cache)
198 #if UNCHECKED_BITSTREAM_READER
213 register int32_t cache;
220 return (
NEG_USR32(sign ^ cache, n) ^ sign) - sign;
271 uint8_t result = s->
buffer[index>>3];
272 #ifdef BITSTREAM_READER_LE
273 result >>= index & 7;
276 result <<= index & 7;
279 #if !UNCHECKED_BITSTREAM_READER
280 if (s->
index < s->size_in_bits_plus8)
306 #ifdef BITSTREAM_READER_LE
308 return ret | (
get_bits(s, n-16) << 16);
310 int ret =
get_bits(s, 16) << (n-16);
360 if (bit_size > INT_MAX - 7 || bit_size < 0 || !buffer) {
361 buffer_size = bit_size = 0;
366 buffer_size = (bit_size + 7) >> 3;
370 #if !UNCHECKED_BITSTREAM_READER
371 s->size_in_bits_plus8 = bit_size + 8;
384 #define init_vlc(vlc, nb_bits, nb_codes, \
385 bits, bits_wrap, bits_size, \
386 codes, codes_wrap, codes_size, \
388 ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
389 bits, bits_wrap, bits_size, \
390 codes, codes_wrap, codes_size, \
394 const void *
bits,
int bits_wrap,
int bits_size,
395 const void *codes,
int codes_wrap,
int codes_size,
396 const void *symbols,
int symbols_wrap,
int symbols_size,
398 #define INIT_VLC_LE 2
399 #define INIT_VLC_USE_NEW_STATIC 4
402 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do { \
403 static VLC_TYPE table[static_size][2]; \
404 (vlc)->table = table; \
405 (vlc)->table_allocated = static_size; \
406 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC); \
415 #define GET_VLC(code, name, gb, table, bits, max_depth) \
418 unsigned int index; \
420 index = SHOW_UBITS(name, gb, bits); \
421 code = table[index][0]; \
422 n = table[index][1]; \
424 if (max_depth > 1 && n < 0) { \
425 LAST_SKIP_BITS(name, gb, bits); \
426 UPDATE_CACHE(name, gb); \
430 index = SHOW_UBITS(name, gb, nb_bits) + code; \
431 code = table[index][0]; \
432 n = table[index][1]; \
433 if (max_depth > 2 && n < 0) { \
434 LAST_SKIP_BITS(name, gb, nb_bits); \
435 UPDATE_CACHE(name, gb); \
439 index = SHOW_UBITS(name, gb, nb_bits) + code; \
440 code = table[index][0]; \
441 n = table[index][1]; \
444 SKIP_BITS(name, gb, n); \
447 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) \
450 unsigned int index; \
452 index = SHOW_UBITS(name, gb, bits); \
453 level = table[index].level; \
454 n = table[index].len; \
456 if (max_depth > 1 && n < 0) { \
457 SKIP_BITS(name, gb, bits); \
459 UPDATE_CACHE(name, gb); \
464 index = SHOW_UBITS(name, gb, nb_bits) + level; \
465 level = table[index].level; \
466 n = table[index].len; \
468 run = table[index].run; \
469 SKIP_BITS(name, gb, n); \
482 int bits,
int max_depth)
489 GET_VLC(code,
re, s, table, bits, max_depth);
521 static inline void print_bin(
int bits,
int n)
525 for (i = n-1; i >= 0; i--) {
528 for (i = n; i < 24; i++)
532 static inline int get_bits_trace(
GetBitContext *s,
int n,
char *file,
533 const char *func,
int line)
543 int bits,
int max_depth,
char *file,
544 const char *func,
int line)
548 int r =
get_vlc2(s, table, bits, max_depth);
552 print_bin(bits2, len);
555 bits2, len, r, pos, file, func, line);
558 static inline int get_xbits_trace(
GetBitContext *s,
int n,
char *file,
559 const char *func,
int line)
570 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
571 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
572 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
573 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
574 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
576 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
579 #define tprintf(p, ...) {}