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;
302 #ifdef BITSTREAM_READER_LE
303 result >>= index & 7;
306 result <<= index & 7;
309 #if !UNCHECKED_BITSTREAM_READER
338 #ifdef BITSTREAM_READER_LE
340 return ret | (
get_bits(s, n - 16) << 16);
342 unsigned ret =
get_bits(s, 16) << (n - 16);
356 #ifdef BITSTREAM_READER_LE
410 if (bit_size >= INT_MAX - 7 || bit_size < 0 || !buffer) {
411 buffer_size = bit_size = 0;
416 buffer_size = (bit_size + 7) >> 3;
438 if (byte_size > INT_MAX / 8 || byte_size < 0)
451 #define init_vlc(vlc, nb_bits, nb_codes, \
452 bits, bits_wrap, bits_size, \
453 codes, codes_wrap, codes_size, \
455 ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
456 bits, bits_wrap, bits_size, \
457 codes, codes_wrap, codes_size, \
461 const void *
bits,
int bits_wrap,
int bits_size,
462 const void *codes,
int codes_wrap,
int codes_size,
463 const void *symbols,
int symbols_wrap,
int symbols_size,
467 #define INIT_VLC_LE 2
468 #define INIT_VLC_USE_NEW_STATIC 4
470 #define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \
472 static VLC_TYPE table[static_size][2]; \
473 (vlc)->table = table; \
474 (vlc)->table_allocated = static_size; \
475 init_vlc(vlc, bits, a, b, c, d, e, f, g, INIT_VLC_USE_NEW_STATIC); \
483 #define GET_VLC(code, name, gb, table, bits, max_depth) \
486 unsigned int index; \
488 index = SHOW_UBITS(name, gb, bits); \
489 code = table[index][0]; \
490 n = table[index][1]; \
492 if (max_depth > 1 && n < 0) { \
493 LAST_SKIP_BITS(name, gb, bits); \
494 UPDATE_CACHE(name, gb); \
498 index = SHOW_UBITS(name, gb, nb_bits) + code; \
499 code = table[index][0]; \
500 n = table[index][1]; \
501 if (max_depth > 2 && n < 0) { \
502 LAST_SKIP_BITS(name, gb, nb_bits); \
503 UPDATE_CACHE(name, gb); \
507 index = SHOW_UBITS(name, gb, nb_bits) + code; \
508 code = table[index][0]; \
509 n = table[index][1]; \
512 SKIP_BITS(name, gb, n); \
515 #define GET_RL_VLC_INTERNAL(level, run, name, gb, table, bits, \
516 max_depth, need_update) \
519 unsigned int index; \
521 index = SHOW_UBITS(name, gb, bits); \
522 level = table[index].level; \
523 n = table[index].len; \
525 if (max_depth > 1 && n < 0) { \
526 SKIP_BITS(name, gb, bits); \
528 UPDATE_CACHE(name, gb); \
533 index = SHOW_UBITS(name, gb, nb_bits) + level; \
534 level = table[index].level; \
535 n = table[index].len; \
537 run = table[index].run; \
538 SKIP_BITS(name, gb, n); \
550 int bits,
int max_depth)
604 static inline void print_bin(
int bits,
int n)
608 for (i = n - 1; i >= 0; i--)
610 for (i = n; i < 24; i++)
614 static inline int get_bits_trace(
GetBitContext *s,
int n,
const char *file,
627 int bits,
int max_depth,
const char *file,
628 const char *func,
int line)
636 print_bin(bits2, len);
639 bits2, len, r, pos, file, func, line);
644 #define GET_RL_VLC(level, run, name, gb, table, bits, \
645 max_depth, need_update) \
647 int show = SHOW_UBITS(name, gb, 24); \
649 int pos = name ## _index; \
651 GET_RL_VLC_INTERNAL(level, run, name, gb, table, bits,max_depth, need_update); \
653 len = name ## _index - pos + 1; \
654 show = show >> (24 - len); \
656 print_bin(show, len); \
658 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d/%-3d rlv @%5d in %s %s:%d\n",\
659 show, len, run-1, level, pos, __FILE__, __PRETTY_FUNCTION__, __LINE__);\
663 static inline int get_xbits_trace(
GetBitContext *s,
int n,
const char *file,
664 const char *func,
int line)
676 #define get_bits(s, n) get_bits_trace(s , n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
677 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
678 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
680 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
681 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
683 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
686 #define tprintf(p, ...) { }
687 #define GET_RL_VLC GET_RL_VLC_INTERNAL