2 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * bitstream reader API header.
26#ifndef AVCODEC_GET_BITS_H
27#define AVCODEC_GET_BITS_H
40 * Safe bitstream reading:
41 * optionally, the get_bits API can check to ensure that we
42 * don't read past input buffer boundaries. This is protected
43 * with CONFIG_SAFE_BITSTREAM_READER at the global level, and
44 * then below that with UNCHECKED_BITSTREAM_READER at the per-
45 * decoder level. This means that decoders that check internally
46 * can "#define UNCHECKED_BITSTREAM_READER 1" to disable
48 * Boundary checking causes a minor performance penalty so for
49 * applications that won't want/need this, it can be disabled
50 * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
52#ifndef UNCHECKED_BITSTREAM_READER
53 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
56#ifndef CACHED_BITSTREAM_READER
57 #define CACHED_BITSTREAM_READER 0
60#if CACHED_BITSTREAM_READER
62// we always want the LE implementation, to provide get_bits_le()
65#ifndef BITSTREAM_READER_LE
67# define BITSTREAM_DEFAULT_BE
74#undef BITSTREAM_DEFAULT_BE
78#define get_bits_count bits_tell
79#define get_bits_bytesize bits_bytesize
80#define get_bits_left bits_left
81#define skip_bits_long bits_skip
82#define skip_bits bits_skip
83#define get_bits bits_read_nz
84#define get_bitsz bits_read
85#define get_bits_long bits_read
86#define get_bits1 bits_read_bit
87#define get_bits64 bits_read_64
88#define get_xbits bits_read_xbits
89#define get_sbits bits_read_signed_nz
90#define get_sbits_long bits_read_signed
91#define show_bits bits_peek
92#define show_bits_long bits_peek
93#define init_get_bits bits_init
94#define init_get_bits8 bits_init8
95#define align_get_bits bits_align
96#define get_vlc2 bits_read_vlc
97#define get_vlc_multi bits_read_vlc_multi
99#define init_get_bits8_le(s, buffer, byte_size) bits_init8_le((BitstreamContextLE*)s, buffer, byte_size)
100#define get_bits_le(s, n) bits_read_le((BitstreamContextLE*)s, n)
102#define show_bits1(s) bits_peek(s, 1)
103#define skip_bits1(s) bits_skip(s, 1)
105#define skip_1stop_8data_bits bits_skip_1stop_8data
107#else // CACHED_BITSTREAM_READER
120/* Bitstream reader API docs:
122 * arbitrary name which is used as prefix for the internal variables
127 * OPEN_READER(name, gb)
128 * load gb into local variables
130 * CLOSE_READER(name, gb)
131 * store local vars in gb
133 * UPDATE_CACHE(name, gb)
134 * Refill the internal cache from the bitstream.
135 * After this call at least MIN_CACHE_BITS will be available.
137 * GET_CACHE(name, gb)
138 * Will output the contents of the internal cache,
139 * next bit is MSB of 32 or 64 bits (FIXME 64 bits).
141 * SHOW_UBITS(name, gb, num)
142 * Will return the next num bits.
144 * SHOW_SBITS(name, gb, num)
145 * Will return the next num bits and do sign extension.
147 * SKIP_BITS(name, gb, num)
148 * Will skip over the next num bits.
149 * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER.
151 * SKIP_CACHE(name, gb, num)
152 * Will remove the next num bits from the cache (note SKIP_COUNTER
153 * MUST be called before UPDATE_CACHE / CLOSE_READER).
155 * SKIP_COUNTER(name, gb, num)
156 * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS).
158 * LAST_SKIP_BITS(name, gb, num)
159 * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER.
161 * BITS_LEFT(name, gb)
162 * Return the number of bits left
164 * For examples see get_bits, show_bits, skip_bits, get_vlc.
167 #define MIN_CACHE_BITS 25
169 #define OPEN_READER_NOSIZE_NOCACHE(name, gb) \
170 unsigned int name ## _index = (gb)->index
172 #define OPEN_READER_NOSIZE(name, gb) \
173 OPEN_READER_NOSIZE_NOCACHE(name, gb); \
174 unsigned int name ## _cache
176#if UNCHECKED_BITSTREAM_READER
177#define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb)
178#define OPEN_READER_SIZE(name, gb) ((void)0)
179#define BITS_AVAILABLE(name, gb) 1
181 #define OPEN_READER_SIZE(name, gb) unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8
182 #define OPEN_READER(name, gb) \
183 OPEN_READER_NOSIZE(name, gb); \
184 OPEN_READER_SIZE(name, gb)
186 #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8
189 #define CLOSE_READER(name, gb) (gb)->index = name ## _index
191 #define UPDATE_CACHE_BE_EXT(name, gb, bits, dst_bits) name ## _cache = \
192 AV_RB ## bits((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7) >> (bits - dst_bits)
194 #define UPDATE_CACHE_LE_EXT(name, gb, bits, dst_bits) name ## _cache = \
195 (uint ## dst_bits ## _t)(AV_RL ## bits((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7))
197/* Using these two macros ensures that 32 bits are available. */
198 # define UPDATE_CACHE_LE_32(name, gb) UPDATE_CACHE_LE_EXT(name, (gb), 64, 32)
199 # define UPDATE_CACHE_BE_32(name, gb) UPDATE_CACHE_BE_EXT(name, (gb), 64, 32)
201 # define UPDATE_CACHE_LE(name, gb) UPDATE_CACHE_LE_EXT(name, (gb), 32, 32)
202 # define UPDATE_CACHE_BE(name, gb) UPDATE_CACHE_BE_EXT(name, (gb), 32, 32)
204#ifdef BITSTREAM_READER_LE
206# define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
207# define UPDATE_CACHE_32(name, gb) UPDATE_CACHE_LE_32(name, (gb))
209# define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
213 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
214 # define UPDATE_CACHE_32(name, gb) UPDATE_CACHE_BE_32(name, (gb))
216 # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
220#if UNCHECKED_BITSTREAM_READER
221# define SKIP_COUNTER(name, gb, num) name ## _index += (num)
223 # define SKIP_COUNTER(name, gb, num) \
224 name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
227 #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index))
229 #define SKIP_BITS(name, gb, num) \
231 SKIP_CACHE(name, gb, num); \
232 SKIP_COUNTER(name, gb, num); \
235 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
237 #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)
238 #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)
240 #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)
241 #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)
243#ifdef BITSTREAM_READER_LE
244# define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)
245# define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)
247 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)
248 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)
251 #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
260 * Get the size of the GetBitContext's buffer in bytes.
262 * @param s the GetBitContext
263 * @param round_up If set, the number of bits will be rounded up to full bytes;
264 * this does not matter if the number of bits is known to be
265 * a multiple of eight, e.g. if the GetBitContext has been
266 * initialized with init_get_bits8.
270 return (
s->size_in_bits + (round_up ? 7 : 0)) >> 3;
274 * Skips the specified number of bits.
275 * @param n the number of bits to skip,
276 * For the UNCHECKED_BITSTREAM_READER this must not cause the distance
277 * from the start to overflow int32_t. Staying within the bitstream + padding
278 * is sufficient, too.
282#if UNCHECKED_BITSTREAM_READER
285 s->index +=
av_clip(n, -
s->index,
s->size_in_bits_plus8 -
s->index);
290 * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
291 * if MSB not set it is negative
292 * @param n length in bits
305 return (
NEG_USR32(sign ^ cache, n) ^ sign) - sign;
319 return (
zero_extend(sign ^ cache, n) ^ sign) - sign;
339 register unsigned int tmp;
375 register unsigned int tmp;
393 unsigned int index =
s->index;
394 uint8_t result =
s->buffer[
index >> 3];
395#ifdef BITSTREAM_READER_LE
396 result >>=
index & 7;
399 result <<=
index & 7;
402#if !UNCHECKED_BITSTREAM_READER
403 if (
s->index <
s->size_in_bits_plus8)
442#ifdef BITSTREAM_READER_LE
444 return ret | (
get_bits(
s, n - 16) << 16);
446 unsigned ret =
get_bits(
s, 16) << (n - 16);
461#ifdef BITSTREAM_READER_LE
472 * Read 0-32 bits as a signed integer.
476 // sign_extend(x, 0) is undefined
484 * Read 0-64 bits as a signed integer.
488 // sign_extend(x, 0) is undefined
510 * Initialize GetBitContext.
511 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
512 * larger than the actual read bits because some optimized bitstream
513 * readers read 32 or 64 bit at once and could read over the end
514 * @param bit_size the size of the buffer in bits
515 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
529 s->size_in_bits = bit_size;
530 s->size_in_bits_plus8 = bit_size + 8;
537 * Initialize GetBitContext.
538 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
539 * larger than the actual read bits because some optimized bitstream
540 * readers read 32 or 64 bit at once and could read over the end
541 * @param byte_size the size of the buffer in bytes
542 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
547 if (byte_size > INT_MAX / 8 || byte_size < 0)
555 if (byte_size > INT_MAX / 8 || byte_size < 0)
565 return s->buffer + (
s->index >> 3);
569 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
570 * If the vlc code is invalid and max_depth>1, then the number of bits removed
573 #define GET_VLC(code, name, gb, table, bits, max_depth) \
575 unsigned idx_ = SHOW_UBITS(name, gb, bits); \
576 code = table[idx_].sym; \
577 int n_ = table[idx_].len; \
579 if (max_depth > 1 && n_ < 0) { \
580 LAST_SKIP_BITS(name, gb, bits); \
581 UPDATE_CACHE(name, gb); \
583 int nb__bits = -n_; \
585 idx_ = SHOW_UBITS(name, gb, nb__bits) + code; \
586 code = table[idx_].sym; \
587 n_ = table[idx_].len; \
588 if (max_depth > 2 && n_ < 0) { \
589 LAST_SKIP_BITS(name, gb, nb__bits); \
590 UPDATE_CACHE(name, gb); \
594 idx_ = SHOW_UBITS(name, gb, nb__bits) + code; \
595 code = table[idx_].sym; \
596 n_ = table[idx_].len; \
599 SKIP_BITS(name, gb, n_); \
602 #define GET_RL_VLC(level, run, name, gb, table, bits, \
603 max_depth, need_update) \
605 unsigned idx_ = SHOW_UBITS(name, gb, bits); \
606 level = table[idx_].level; \
607 int n_ = table[idx_].len8; \
609 if (max_depth > 1 && n_ < 0) { \
610 SKIP_BITS(name, gb, bits); \
612 UPDATE_CACHE(name, gb); \
615 int nb__bits = -n_; \
617 idx_ = SHOW_UBITS(name, gb, nb__bits) + level; \
618 level = table[idx_].level; \
619 n_ = table[idx_].len8; \
620 if (max_depth > 2 && n_ < 0) { \
621 LAST_SKIP_BITS(name, gb, nb__bits); \
623 UPDATE_CACHE(name, gb); \
627 idx_ = SHOW_UBITS(name, gb, nb__bits) + level; \
628 level = table[idx_].level; \
629 n_ = table[idx_].len8; \
632 run = table[idx_].run; \
633 SKIP_BITS(name, gb, n_); \
638 * @param bits is the number of bits which will be read at once, must be
639 * identical to nb_bits in vlc_init()
640 * @param max_depth is the number of times bits bits must be read to completely
641 * read the longest vlc code
642 * = (max_vlc_length + bits - 1) / bits
643 * @returns the code parsed or -1 if no vlc matches
646 int bits,
int max_depth)
663 const int bits,
const int max_depth,
707#endif // CACHED_BITSTREAM_READER
709#endif /* AVCODEC_GET_BITS_H */
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
bitstream reader API header.
common internal and external API header
Misc types and constants that do not belong anywhere else.
static const uint8_t bits[8]
static unsigned int get_bits_le(GetBitContext *s, int n)
static int get_sbits_long(GetBitContext *s, int n)
Read 0-32 bits as a signed integer.
#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.
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static int init_get_bits8_le(GetBitContext *s, const uint8_t *buffer, int byte_size)
static int decode012(GetBitContext *gb)
#define GET_CACHE(name, gb)
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
static int get_sbits(GetBitContext *s, int n)
#define CLOSE_READER(name, gb)
static int get_bits_left(GetBitContext *gb)
static int get_vlc_multi(GetBitContext *s, uint8_t *dst, av_unused const VLC_MULTI_ELEM *const Jtable, const VLCElem *const table, const int bits, const int max_depth, av_unused const int symbols_size)
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
static int get_xbits_le(GetBitContext *s, int n)
#define SHOW_UBITS(name, gb, num)
static unsigned int get_bits1(GetBitContext *s)
#define SHOW_SBITS(name, gb, num)
static int decode210(GetBitContext *gb)
#define OPEN_READER_NOSIZE(name, gb)
#define OPEN_READER(name, gb)
static void skip_bits(GetBitContext *s, int n)
static int skip_1stop_8data_bits(GetBitContext *gb)
#define UPDATE_CACHE(name, gb)
static uint64_t get_bits64(GetBitContext *s, int n)
Read 0-64 bits.
#define OPEN_READER_SIZE(name, gb)
#define SHOW_UBITS_LE(name, gb, num)
#define OPEN_READER_NOSIZE_NOCACHE(name, gb)
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
#define LAST_SKIP_BITS(name, gb, num)
static const uint8_t * align_get_bits(GetBitContext *s)
static int get_bits_count(const GetBitContext *s)
static int64_t get_sbits64(GetBitContext *s, int n)
Read 0-64 bits as a signed integer.
static void skip_bits1(GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define UPDATE_CACHE_LE(name, gb)
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
#define UPDATE_CACHE_32(name, gb)
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
static int get_bits_bytesize(const GetBitContext *s, int round_up)
Get the size of the GetBitContext's buffer in bytes.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
static unsigned int show_bits1(GetBitContext *s)
static int get_xbits(GetBitContext *s, int n)
Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define av_builtin_constant_p
static av_const unsigned zero_extend(unsigned val, unsigned bits)
static av_const int sign_extend(int val, unsigned bits)
static av_const int64_t sign_extend64(int64_t val, unsigned bits)
static const uint16_t table[]