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
220 return (
NEG_USR32(sign ^ cache, n) ^ sign) - sign;
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);
324 #ifdef BITSTREAM_READER_LE
378 if (bit_size > INT_MAX - 7 || bit_size < 0 || !buffer) {
379 buffer_size = bit_size = 0;
384 buffer_size = (bit_size + 7) >> 3;
388 #if !UNCHECKED_BITSTREAM_READER
389 s->size_in_bits_plus8 = bit_size + 8;
407 if (byte_size > INT_MAX / 8)
418 #define init_vlc(vlc, nb_bits, nb_codes, \
419 bits, bits_wrap, bits_size, \
420 codes, codes_wrap, codes_size, \
422 ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
423 bits, bits_wrap, bits_size, \
424 codes, codes_wrap, codes_size, \
428 const void *
bits,
int bits_wrap,
int bits_size,
429 const void *codes,
int codes_wrap,
int codes_size,
430 const void *symbols,
int symbols_wrap,
int symbols_size,
432 #define INIT_VLC_LE 2
433 #define INIT_VLC_USE_NEW_STATIC 4
436 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do { \
437 static VLC_TYPE table[static_size][2]; \
438 (vlc)->table = table; \
439 (vlc)->table_allocated = static_size; \
440 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC); \
449 #define GET_VLC(code, name, gb, table, bits, max_depth) \
452 unsigned int index; \
454 index = SHOW_UBITS(name, gb, bits); \
455 code = table[index][0]; \
456 n = table[index][1]; \
458 if (max_depth > 1 && n < 0) { \
459 LAST_SKIP_BITS(name, gb, bits); \
460 UPDATE_CACHE(name, gb); \
464 index = SHOW_UBITS(name, gb, nb_bits) + code; \
465 code = table[index][0]; \
466 n = table[index][1]; \
467 if (max_depth > 2 && n < 0) { \
468 LAST_SKIP_BITS(name, gb, nb_bits); \
469 UPDATE_CACHE(name, gb); \
473 index = SHOW_UBITS(name, gb, nb_bits) + code; \
474 code = table[index][0]; \
475 n = table[index][1]; \
478 SKIP_BITS(name, gb, n); \
481 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) \
484 unsigned int index; \
486 index = SHOW_UBITS(name, gb, bits); \
487 level = table[index].level; \
488 n = table[index].len; \
490 if (max_depth > 1 && n < 0) { \
491 SKIP_BITS(name, gb, bits); \
493 UPDATE_CACHE(name, gb); \
498 index = SHOW_UBITS(name, gb, nb_bits) + level; \
499 level = table[index].level; \
500 n = table[index].len; \
502 run = table[index].run; \
503 SKIP_BITS(name, gb, n); \
516 int bits,
int max_depth)
523 GET_VLC(code,
re, s, table, bits, max_depth);
555 static inline void print_bin(
int bits,
int n)
559 for (i = n-1; i >= 0; i--) {
562 for (i = n; i < 24; i++)
566 static inline int get_bits_trace(
GetBitContext *s,
int n,
const char *file,
567 const char *func,
int line)
577 int bits,
int max_depth,
const char *file,
578 const char *func,
int line)
582 int r =
get_vlc2(s, table, bits, max_depth);
586 print_bin(bits2, len);
589 bits2, len, r, pos, file, func, line);
592 static inline int get_xbits_trace(
GetBitContext *s,
int n,
const char *file,
593 const char *func,
int line)
604 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
605 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
606 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
607 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
608 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
610 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
613 #define tprintf(p, ...) {}
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
static unsigned int show_bits1(GetBitContext *s)
struct GetBitContext GetBitContext
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static void skip_bits_long(GetBitContext *s, int n)
static void align_get_bits(GetBitContext *s)
static int get_sbits(GetBitContext *s, int n)
static int get_sbits_long(GetBitContext *s, int n)
Read 0-32 bits as a signed integer.
static int get_bits_count(const GetBitContext *s)
static const uint8_t bits2[81]
static int get_bits_left(GetBitContext *gb)
static uint64_t get_bits64(GetBitContext *s, int n)
struct RL_VLC_ELEM RL_VLC_ELEM
#define UPDATE_CACHE(name, gb)
void ff_free_vlc(VLC *vlc)
void av_log(void *avcl, int level, const char *fmt,...)
#define CLOSE_READER(name, gb)
static int check_marker(GetBitContext *s, const char *msg)
int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
#define LAST_SKIP_BITS(name, gb, num)
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
#define GET_VLC(code, name, gb, table, bits, max_depth)
If the vlc code is invalid and max_depth=1, then no bits will be removed.
#define SHOW_UBITS(name, gb, num)
static int decode210(GetBitContext *gb)
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
static int get_xbits(GetBitContext *s, int n)
read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
#define OPEN_READER(name, gb)
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits1(GetBitContext *s)
static void skip_bits(GetBitContext *s, int n)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
#define GET_CACHE(name, gb)
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static av_const int sign_extend(int val, unsigned bits)
#define SHOW_SBITS(name, gb, num)
common internal and external API header
const uint8_t * buffer_end
#define AVERROR_INVALIDDATA
static int decode012(GetBitContext *gb)
VLC_TYPE(* table)[2]
code, bits