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);
355 int buffer_size = (bit_size+7)>>3;
356 if (buffer_size < 0 || bit_size < 0) {
357 buffer_size = bit_size = 0;
363 #if !UNCHECKED_BITSTREAM_READER
364 s->size_in_bits_plus8 = bit_size + 8;
376 #define init_vlc(vlc, nb_bits, nb_codes, \
377 bits, bits_wrap, bits_size, \
378 codes, codes_wrap, codes_size, \
380 init_vlc_sparse(vlc, nb_bits, nb_codes, \
381 bits, bits_wrap, bits_size, \
382 codes, codes_wrap, codes_size, \
386 const void *
bits,
int bits_wrap,
int bits_size,
387 const void *codes,
int codes_wrap,
int codes_size,
388 const void *symbols,
int symbols_wrap,
int symbols_size,
390 #define INIT_VLC_LE 2
391 #define INIT_VLC_USE_NEW_STATIC 4
394 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do { \
395 static VLC_TYPE table[static_size][2]; \
396 (vlc)->table = table; \
397 (vlc)->table_allocated = static_size; \
398 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC); \
407 #define GET_VLC(code, name, gb, table, bits, max_depth) \
410 unsigned int index; \
412 index = SHOW_UBITS(name, gb, bits); \
413 code = table[index][0]; \
414 n = table[index][1]; \
416 if (max_depth > 1 && n < 0) { \
417 LAST_SKIP_BITS(name, gb, bits); \
418 UPDATE_CACHE(name, gb); \
422 index = SHOW_UBITS(name, gb, nb_bits) + code; \
423 code = table[index][0]; \
424 n = table[index][1]; \
425 if (max_depth > 2 && n < 0) { \
426 LAST_SKIP_BITS(name, gb, nb_bits); \
427 UPDATE_CACHE(name, gb); \
431 index = SHOW_UBITS(name, gb, nb_bits) + code; \
432 code = table[index][0]; \
433 n = table[index][1]; \
436 SKIP_BITS(name, gb, n); \
439 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) \
442 unsigned int index; \
444 index = SHOW_UBITS(name, gb, bits); \
445 level = table[index].level; \
446 n = table[index].len; \
448 if (max_depth > 1 && n < 0) { \
449 SKIP_BITS(name, gb, bits); \
451 UPDATE_CACHE(name, gb); \
456 index = SHOW_UBITS(name, gb, nb_bits) + level; \
457 level = table[index].level; \
458 n = table[index].len; \
460 run = table[index].run; \
461 SKIP_BITS(name, gb, n); \
474 int bits,
int max_depth)
481 GET_VLC(code,
re, s, table, bits, max_depth);
513 static inline void print_bin(
int bits,
int n)
517 for (i = n-1; i >= 0; i--) {
520 for (i = n; i < 24; i++)
524 static inline int get_bits_trace(
GetBitContext *s,
int n,
char *file,
525 const char *func,
int line)
535 int bits,
int max_depth,
char *file,
536 const char *func,
int line)
540 int r =
get_vlc2(s, table, bits, max_depth);
544 print_bin(bits2, len);
547 bits2, len, r, pos, file, func, line);
550 static inline int get_xbits_trace(
GetBitContext *s,
int n,
char *file,
551 const char *func,
int line)
562 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
563 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
564 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
565 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
566 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
568 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
571 #define tprintf(p, ...) {}