26 #ifndef AVCODEC_GET_BITS_H
27 #define AVCODEC_GET_BITS_H
49 #ifndef UNCHECKED_BITSTREAM_READER
50 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
60 #define VLC_TYPE int16_t
116 #ifdef LONG_BITSTREAM_READER
117 # define MIN_CACHE_BITS 32
119 # define MIN_CACHE_BITS 25
122 #if UNCHECKED_BITSTREAM_READER
123 #define OPEN_READER(name, gb) \
124 unsigned int name##_index = (gb)->index; \
125 av_unused unsigned int name##_cache
127 #define HAVE_BITS_REMAINING(name, gb) 1
129 #define OPEN_READER(name, gb) \
130 unsigned int name##_index = (gb)->index; \
131 unsigned int av_unused name##_cache = 0; \
132 unsigned int av_unused name##_size_plus8 = \
133 (gb)->size_in_bits_plus8
135 #define HAVE_BITS_REMAINING(name, gb) \
136 name##_index < name##_size_plus8
139 #define CLOSE_READER(name, gb) (gb)->index = name##_index
141 #ifdef BITSTREAM_READER_LE
143 # ifdef LONG_BITSTREAM_READER
144 # define UPDATE_CACHE(name, gb) name##_cache = \
145 AV_RL64((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
147 # define UPDATE_CACHE(name, gb) name##_cache = \
148 AV_RL32((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
151 # define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
155 # ifdef LONG_BITSTREAM_READER
156 # define UPDATE_CACHE(name, gb) name##_cache = \
157 AV_RB64((gb)->buffer + (name##_index >> 3)) >> (32 - (name##_index & 7))
159 # define UPDATE_CACHE(name, gb) name##_cache = \
160 AV_RB32((gb)->buffer + (name##_index >> 3)) << (name##_index & 7)
163 # define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
167 #if UNCHECKED_BITSTREAM_READER
168 # define SKIP_COUNTER(name, gb, num) name##_index += (num)
170 # define SKIP_COUNTER(name, gb, num) \
171 name##_index = FFMIN(name##_size_plus8, name##_index + (num))
174 #define SKIP_BITS(name, gb, num) do { \
175 SKIP_CACHE(name, gb, num); \
176 SKIP_COUNTER(name, gb, num); \
179 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
181 #ifdef BITSTREAM_READER_LE
182 # define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
183 # define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
185 # define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
186 # define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
189 #define GET_CACHE(name, gb) ((uint32_t)name##_cache)
197 #if UNCHECKED_BITSTREAM_READER
219 return (
NEG_USR32(sign ^ cache, n) ^ sign) - sign;
274 #ifdef BITSTREAM_READER_LE
275 result >>= index & 7;
278 result <<= index & 7;
281 #if !UNCHECKED_BITSTREAM_READER
310 #ifdef BITSTREAM_READER_LE
312 return ret | (
get_bits(s, n-16) << 16);
314 unsigned ret =
get_bits(s, 16) << (n-16);
328 #ifdef BITSTREAM_READER_LE
382 if (bit_size >= INT_MAX - 7 || bit_size < 0 || !buffer) {
383 buffer_size = bit_size = 0;
388 buffer_size = (bit_size + 7) >> 3;
409 if (byte_size > INT_MAX / 8 || byte_size < 0)
420 #define init_vlc(vlc, nb_bits, nb_codes, \
421 bits, bits_wrap, bits_size, \
422 codes, codes_wrap, codes_size, \
424 ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
425 bits, bits_wrap, bits_size, \
426 codes, codes_wrap, codes_size, \
430 const void *
bits,
int bits_wrap,
int bits_size,
431 const void *codes,
int codes_wrap,
int codes_size,
432 const void *symbols,
int symbols_wrap,
int symbols_size,
434 #define INIT_VLC_LE 2
435 #define INIT_VLC_USE_NEW_STATIC 4
438 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do { \
439 static VLC_TYPE table[static_size][2]; \
440 (vlc)->table = table; \
441 (vlc)->table_allocated = static_size; \
442 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC); \
451 #define GET_VLC(code, name, gb, table, bits, max_depth) \
454 unsigned int index; \
456 index = SHOW_UBITS(name, gb, bits); \
457 code = table[index][0]; \
458 n = table[index][1]; \
460 if (max_depth > 1 && n < 0) { \
461 LAST_SKIP_BITS(name, gb, bits); \
462 UPDATE_CACHE(name, gb); \
466 index = SHOW_UBITS(name, gb, nb_bits) + code; \
467 code = table[index][0]; \
468 n = table[index][1]; \
469 if (max_depth > 2 && n < 0) { \
470 LAST_SKIP_BITS(name, gb, nb_bits); \
471 UPDATE_CACHE(name, gb); \
475 index = SHOW_UBITS(name, gb, nb_bits) + code; \
476 code = table[index][0]; \
477 n = table[index][1]; \
480 SKIP_BITS(name, gb, n); \
483 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) \
486 unsigned int index; \
488 index = SHOW_UBITS(name, gb, bits); \
489 level = table[index].level; \
490 n = table[index].len; \
492 if (max_depth > 1 && n < 0) { \
493 SKIP_BITS(name, gb, bits); \
495 UPDATE_CACHE(name, gb); \
500 index = SHOW_UBITS(name, gb, nb_bits) + level; \
501 level = table[index].level; \
502 n = table[index].len; \
504 run = table[index].run; \
505 SKIP_BITS(name, gb, n); \
518 int bits,
int max_depth)
557 static inline void print_bin(
int bits,
int n)
561 for (i = n-1; i >= 0; i--) {
564 for (i = n; i < 24; i++)
568 static inline int get_bits_trace(
GetBitContext *s,
int n,
const char *file,
579 int bits,
int max_depth,
const char *file,
580 const char *func,
int line)
588 print_bin(bits2, len);
591 bits2, len, r, pos, file, func, line);
594 static inline int get_xbits_trace(
GetBitContext *s,
int n,
const char *file,
595 const char *func,
int line)
606 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
607 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
608 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
609 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
610 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
612 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
615 #define tprintf(p, ...) {}