26 #ifndef AVCODEC_GET_BITS_H
27 #define AVCODEC_GET_BITS_H
50 #ifndef UNCHECKED_BITSTREAM_READER
51 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
61 #define VLC_TYPE int16_t
119 #ifdef LONG_BITSTREAM_READER
120 # define MIN_CACHE_BITS 32
122 # define MIN_CACHE_BITS 25
125 #if UNCHECKED_BITSTREAM_READER
126 #define OPEN_READER(name, gb) \
127 unsigned int name ## _index = (gb)->index; \
128 unsigned int av_unused name ## _cache
130 #define HAVE_BITS_REMAINING(name, gb) 1
132 #define OPEN_READER(name, gb) \
133 unsigned int name ## _index = (gb)->index; \
134 unsigned int av_unused name ## _cache = 0; \
135 unsigned int av_unused name ## _size_plus8 = (gb)->size_in_bits_plus8
137 #define HAVE_BITS_REMAINING(name, gb) name ## _index < name ## _size_plus8
140 #define CLOSE_READER(name, gb) (gb)->index = name ## _index
142 # ifdef LONG_BITSTREAM_READER
144 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
145 AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
147 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
148 AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))
152 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
153 AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
155 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
156 AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)
161 #ifdef BITSTREAM_READER_LE
163 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
165 # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
169 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
171 # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
175 #if UNCHECKED_BITSTREAM_READER
176 # define SKIP_COUNTER(name, gb, num) name ## _index += (num)
178 # define SKIP_COUNTER(name, gb, num) \
179 name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
182 #define SKIP_BITS(name, gb, num) \
184 SKIP_CACHE(name, gb, num); \
185 SKIP_COUNTER(name, gb, num); \
188 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
190 #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)
191 #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)
193 #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)
194 #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)
196 #ifdef BITSTREAM_READER_LE
197 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)
198 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)
200 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)
201 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)
204 #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
213 #if UNCHECKED_BITSTREAM_READER
236 return (
NEG_USR32(sign ^ cache, n) ^ sign) - sign;
303 #ifdef BITSTREAM_READER_LE
304 result >>= index & 7;
307 result <<= index & 7;
310 #if !UNCHECKED_BITSTREAM_READER
339 #ifdef BITSTREAM_READER_LE
341 return ret | (
get_bits(s, n - 16) << 16);
343 unsigned ret =
get_bits(s, 16) << (n - 16);
357 #ifdef BITSTREAM_READER_LE
411 if (bit_size >= INT_MAX - 7 || bit_size < 0 || !buffer) {
412 buffer_size = bit_size = 0;
417 buffer_size = (bit_size + 7) >> 3;
439 if (byte_size > INT_MAX / 8 || byte_size < 0)
452 #define init_vlc(vlc, nb_bits, nb_codes, \
453 bits, bits_wrap, bits_size, \
454 codes, codes_wrap, codes_size, \
456 ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
457 bits, bits_wrap, bits_size, \
458 codes, codes_wrap, codes_size, \
462 const void *
bits,
int bits_wrap,
int bits_size,
463 const void *codes,
int codes_wrap,
int codes_size,
464 const void *symbols,
int symbols_wrap,
int symbols_size,
468 #define INIT_VLC_LE 2
469 #define INIT_VLC_USE_NEW_STATIC 4
471 #define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \
473 static VLC_TYPE table[static_size][2]; \
474 (vlc)->table = table; \
475 (vlc)->table_allocated = static_size; \
476 init_vlc(vlc, bits, a, b, c, d, e, f, g, INIT_VLC_USE_NEW_STATIC); \
484 #define GET_VLC(code, name, gb, table, bits, max_depth) \
487 unsigned int index; \
489 index = SHOW_UBITS(name, gb, bits); \
490 code = table[index][0]; \
491 n = table[index][1]; \
493 if (max_depth > 1 && n < 0) { \
494 LAST_SKIP_BITS(name, gb, bits); \
495 UPDATE_CACHE(name, gb); \
499 index = SHOW_UBITS(name, gb, nb_bits) + code; \
500 code = table[index][0]; \
501 n = table[index][1]; \
502 if (max_depth > 2 && n < 0) { \
503 LAST_SKIP_BITS(name, gb, nb_bits); \
504 UPDATE_CACHE(name, gb); \
508 index = SHOW_UBITS(name, gb, nb_bits) + code; \
509 code = table[index][0]; \
510 n = table[index][1]; \
513 SKIP_BITS(name, gb, n); \
516 #define GET_RL_VLC(level, run, name, gb, table, bits, \
517 max_depth, need_update) \
520 unsigned int index; \
522 index = SHOW_UBITS(name, gb, bits); \
523 level = table[index].level; \
524 n = table[index].len; \
526 if (max_depth > 1 && n < 0) { \
527 SKIP_BITS(name, gb, bits); \
529 UPDATE_CACHE(name, gb); \
534 index = SHOW_UBITS(name, gb, nb_bits) + level; \
535 level = table[index].level; \
536 n = table[index].len; \
538 run = table[index].run; \
539 SKIP_BITS(name, gb, n); \
551 int bits,
int max_depth)
591 static inline void print_bin(
int bits,
int n)
595 for (i = n - 1; i >= 0; i--)
597 for (i = n; i < 24; i++)
601 static inline int get_bits_trace(
GetBitContext *s,
int n,
const char *file,
614 int bits,
int max_depth,
const char *file,
615 const char *func,
int line)
623 print_bin(bits2, len);
626 bits2, len, r, pos, file, func, line);
631 static inline int get_xbits_trace(
GetBitContext *s,
int n,
const char *file,
632 const char *func,
int line)
644 #define get_bits(s, n) get_bits_trace(s , n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
645 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
646 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
648 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
649 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
651 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
654 #define tprintf(p, ...) { }