26 #ifndef AVCODEC_GET_BITS_H
27 #define AVCODEC_GET_BITS_H
31 #include "libavutil/common.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/log.h"
34 #include "libavutil/avassert.h"
50 #ifndef UNCHECKED_BITSTREAM_READER
51 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
61 #define VLC_TYPE int16_t
120 #ifdef LONG_BITSTREAM_READER
121 # define MIN_CACHE_BITS 32
123 # define MIN_CACHE_BITS 25
126 #if UNCHECKED_BITSTREAM_READER
127 #define OPEN_READER(name, gb) \
128 unsigned int name ## _index = (gb)->index; \
129 unsigned int av_unused name ## _cache
131 #define HAVE_BITS_REMAINING(name, gb) 1
133 #define OPEN_READER(name, gb) \
134 unsigned int name ## _index = (gb)->index; \
135 unsigned int av_unused name ## _cache = 0; \
136 unsigned int av_unused name ## _size_plus8 = (gb)->size_in_bits_plus8
138 #define HAVE_BITS_REMAINING(name, gb) name ## _index < name ## _size_plus8
141 #define CLOSE_READER(name, gb) (gb)->index = name ## _index
143 # ifdef LONG_BITSTREAM_READER
145 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
146 AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
148 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
149 AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))
153 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
154 AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
156 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
157 AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)
162 #ifdef BITSTREAM_READER_LE
164 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
166 # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
170 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
172 # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
176 #if UNCHECKED_BITSTREAM_READER
177 # define SKIP_COUNTER(name, gb, num) name ## _index += (num)
179 # define SKIP_COUNTER(name, gb, num) \
180 name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
183 #define SKIP_BITS(name, gb, num) \
185 SKIP_CACHE(name, gb, num); \
186 SKIP_COUNTER(name, gb, num); \
189 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
191 #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)
192 #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)
194 #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)
195 #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)
197 #ifdef BITSTREAM_READER_LE
198 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)
199 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)
201 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)
202 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)
205 #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
214 #if UNCHECKED_BITSTREAM_READER
237 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, ...) { }
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
static unsigned int show_bits1(GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static void skip_bits_long(GetBitContext *s, int n)
static struct endianess table[]
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
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.
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 int get_bits_count(const GetBitContext *s)
static const uint8_t bits2[81]
#define SHOW_UBITS_LE(name, gb, num)
static int get_bits_left(GetBitContext *gb)
static uint64_t get_bits64(GetBitContext *s, int n)
Read 0-64 bits.
#define UPDATE_CACHE(name, gb)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define CLOSE_READER(name, gb)
static int check_marker(GetBitContext *s, const char *msg)
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)
#define AV_LOG_INFO
Standard information.
void *volatile init_state
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)
#define UPDATE_CACHE_LE(name, gb)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
#define GET_CACHE(name, gb)
int(* func)(AVBPrint *dst, const char *in, const char *arg)
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static av_const int sign_extend(int val, unsigned bits)
static unsigned int get_bits_le(GetBitContext *s, int n)
#define SHOW_SBITS(name, gb, num)
const uint8_t * buffer_end
#define AVERROR_INVALIDDATA
static int decode012(GetBitContext *gb)
VLC_TYPE(* table)[2]
code, bits
static const uint8_t * align_get_bits(GetBitContext *s)
void ff_free_vlc(VLC *vlc)
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.