2 * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; 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
31 #include "libavutil/common.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/log.h"
38 * Safe bitstream reading:
39 * optionally, the get_bits API can check to ensure that we
40 * don't read past input buffer boundaries. This is protected
41 * with CONFIG_SAFE_BITSTREAM_READER at the global level, and
42 * then below that with UNCHECKED_BITSTREAM_READER at the per-
43 * decoder level. This means that decoders that check internally
44 * can "#define UNCHECKED_BITSTREAM_READER 1" to disable
46 * Boundary checking causes a minor performance penalty so for
47 * applications that won't want/need this, it can be disabled
48 * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
50 #ifndef UNCHECKED_BITSTREAM_READER
51 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
54 typedef struct GetBitContext {
55 const uint8_t *buffer, *buffer_end;
58 #if !UNCHECKED_BITSTREAM_READER
59 int size_in_bits_plus8;
63 /* Bitstream reader API docs:
65 * arbitrary name which is used as prefix for the internal variables
70 * OPEN_READER(name, gb)
71 * load gb into local variables
73 * CLOSE_READER(name, gb)
74 * store local vars in gb
76 * UPDATE_CACHE(name, gb)
77 * Refill the internal cache from the bitstream.
78 * After this call at least MIN_CACHE_BITS will be available.
81 * Will output the contents of the internal cache,
82 * next bit is MSB of 32 or 64 bits (FIXME 64 bits).
84 * SHOW_UBITS(name, gb, num)
85 * Will return the next num bits.
87 * SHOW_SBITS(name, gb, num)
88 * Will return the next num bits and do sign extension.
90 * SKIP_BITS(name, gb, num)
91 * Will skip over the next num bits.
92 * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER.
94 * SKIP_CACHE(name, gb, num)
95 * Will remove the next num bits from the cache (note SKIP_COUNTER
96 * MUST be called before UPDATE_CACHE / CLOSE_READER).
98 * SKIP_COUNTER(name, gb, num)
99 * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS).
101 * LAST_SKIP_BITS(name, gb, num)
102 * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER.
104 * For examples see get_bits, show_bits, skip_bits, get_vlc.
107 #ifdef LONG_BITSTREAM_READER
108 # define MIN_CACHE_BITS 32
110 # define MIN_CACHE_BITS 25
113 #define OPEN_READER_NOSIZE(name, gb) \
114 unsigned int name ## _index = (gb)->index; \
115 unsigned int av_unused name ## _cache = 0
117 #if UNCHECKED_BITSTREAM_READER
118 #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb)
120 #define BITS_AVAILABLE(name, gb) 1
122 #define OPEN_READER(name, gb) \
123 OPEN_READER_NOSIZE(name, gb); \
124 unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8
126 #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8
129 #define CLOSE_READER(name, gb) (gb)->index = name ## _index
131 #ifdef BITSTREAM_READER_LE
133 # ifdef LONG_BITSTREAM_READER
134 # define UPDATE_CACHE(name, gb) name ## _cache = \
135 AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
137 # define UPDATE_CACHE(name, gb) name ## _cache = \
138 AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
141 # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
145 # ifdef LONG_BITSTREAM_READER
146 # define UPDATE_CACHE(name, gb) name ## _cache = \
147 AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))
149 # define UPDATE_CACHE(name, gb) name ## _cache = \
150 AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)
153 # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
157 #if UNCHECKED_BITSTREAM_READER
158 # define SKIP_COUNTER(name, gb, num) name ## _index += (num)
160 # define SKIP_COUNTER(name, gb, num) \
161 name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
164 #define SKIP_BITS(name, gb, num) \
166 SKIP_CACHE(name, gb, num); \
167 SKIP_COUNTER(name, gb, num); \
170 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
172 #ifdef BITSTREAM_READER_LE
173 # define SHOW_UBITS(name, gb, num) zero_extend(name ## _cache, num)
174 # define SHOW_SBITS(name, gb, num) sign_extend(name ## _cache, num)
176 # define SHOW_UBITS(name, gb, num) NEG_USR32(name ## _cache, num)
177 # define SHOW_SBITS(name, gb, num) NEG_SSR32(name ## _cache, num)
180 #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
182 static inline int get_bits_count(const GetBitContext *s)
187 static inline void skip_bits_long(GetBitContext *s, int n)
189 #if UNCHECKED_BITSTREAM_READER
192 s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
197 * Read MPEG-1 dc-style VLC (sign bit + mantisse with no MSB).
198 * if MSB not set it is negative
199 * @param n length in bits
201 static inline int get_xbits(GetBitContext *s, int n)
204 register int32_t cache;
207 cache = GET_CACHE(re, s);
209 LAST_SKIP_BITS(re, s, n);
211 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
214 static inline int get_sbits(GetBitContext *s, int n)
219 tmp = SHOW_SBITS(re, s, n);
220 LAST_SKIP_BITS(re, s, n);
228 static inline unsigned int get_bits(GetBitContext *s, int n)
233 tmp = SHOW_UBITS(re, s, n);
234 LAST_SKIP_BITS(re, s, n);
242 static av_always_inline int get_bitsz(GetBitContext *s, int n)
244 return n ? get_bits(s, n) : 0;
250 static inline unsigned int show_bits(GetBitContext *s, int n)
253 OPEN_READER_NOSIZE(re, s);
255 tmp = SHOW_UBITS(re, s, n);
259 static inline void skip_bits(GetBitContext *s, int n)
263 LAST_SKIP_BITS(re, s, n);
267 static inline unsigned int get_bits1(GetBitContext *s)
269 unsigned int index = s->index;
270 uint8_t result = s->buffer[index >> 3];
271 #ifdef BITSTREAM_READER_LE
272 result >>= index & 7;
275 result <<= index & 7;
278 #if !UNCHECKED_BITSTREAM_READER
279 if (s->index < s->size_in_bits_plus8)
287 static inline unsigned int show_bits1(GetBitContext *s)
289 return show_bits(s, 1);
292 static inline void skip_bits1(GetBitContext *s)
300 static inline unsigned int get_bits_long(GetBitContext *s, int n)
302 if (n <= MIN_CACHE_BITS) {
303 return get_bits(s, n);
305 #ifdef BITSTREAM_READER_LE
306 int ret = get_bits(s, 16);
307 return ret | (get_bits(s, n - 16) << 16);
309 int ret = get_bits(s, 16) << (n - 16);
310 return ret | get_bits(s, n - 16);
318 static inline uint64_t get_bits64(GetBitContext *s, int n)
321 return get_bits_long(s, n);
323 #ifdef BITSTREAM_READER_LE
324 uint64_t ret = get_bits_long(s, 32);
325 return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
327 uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
328 return ret | get_bits_long(s, 32);
334 * Read 0-32 bits as a signed integer.
336 static inline int get_sbits_long(GetBitContext *s, int n)
338 return sign_extend(get_bits_long(s, n), n);
344 static inline unsigned int show_bits_long(GetBitContext *s, int n)
346 if (n <= MIN_CACHE_BITS) {
347 return show_bits(s, n);
349 GetBitContext gb = *s;
350 return get_bits_long(&gb, n);
355 * Initialize GetBitContext.
356 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
357 * larger than the actual read bits because some optimized bitstream
358 * readers read 32 or 64 bit at once and could read over the end
359 * @param bit_size the size of the buffer in bits
360 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
362 static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
368 if (bit_size > INT_MAX - 7 || bit_size < 0 || !buffer) {
371 ret = AVERROR_INVALIDDATA;
374 buffer_size = (bit_size + 7) >> 3;
377 s->size_in_bits = bit_size;
378 #if !UNCHECKED_BITSTREAM_READER
379 s->size_in_bits_plus8 = bit_size + 8;
381 s->buffer_end = buffer + buffer_size;
388 * Initialize GetBitContext.
389 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
390 * larger than the actual read bits because some optimized bitstream
391 * readers read 32 or 64 bit at once and could read over the end
392 * @param byte_size the size of the buffer in bytes
393 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
395 static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
398 if (byte_size > INT_MAX / 8)
399 return AVERROR_INVALIDDATA;
400 return init_get_bits(s, buffer, byte_size * 8);
403 static inline const uint8_t *align_get_bits(GetBitContext *s)
405 int n = -get_bits_count(s) & 7;
408 return s->buffer + (s->index >> 3);
412 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
413 * If the vlc code is invalid and max_depth>1, then the number of bits removed
416 #define GET_VLC(code, name, gb, table, bits, max_depth) \
419 unsigned int index; \
421 index = SHOW_UBITS(name, gb, bits); \
422 code = table[index][0]; \
423 n = table[index][1]; \
425 if (max_depth > 1 && n < 0) { \
426 LAST_SKIP_BITS(name, gb, 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]; \
434 if (max_depth > 2 && n < 0) { \
435 LAST_SKIP_BITS(name, gb, nb_bits); \
436 UPDATE_CACHE(name, gb); \
440 index = SHOW_UBITS(name, gb, nb_bits) + code; \
441 code = table[index][0]; \
442 n = table[index][1]; \
445 SKIP_BITS(name, gb, n); \
448 #define GET_RL_VLC(level, run, name, gb, table, bits, \
449 max_depth, need_update) \
452 unsigned int index; \
454 index = SHOW_UBITS(name, gb, bits); \
455 level = table[index].level; \
456 n = table[index].len; \
458 if (max_depth > 1 && n < 0) { \
459 SKIP_BITS(name, gb, bits); \
461 UPDATE_CACHE(name, gb); \
466 index = SHOW_UBITS(name, gb, nb_bits) + level; \
467 level = table[index].level; \
468 n = table[index].len; \
469 if (max_depth > 2 && n < 0) { \
470 LAST_SKIP_BITS(name, gb, nb_bits); \
472 UPDATE_CACHE(name, gb); \
476 index = SHOW_UBITS(name, gb, nb_bits) + level; \
477 level = table[index].level; \
478 n = table[index].len; \
481 run = table[index].run; \
482 SKIP_BITS(name, gb, n); \
487 * @param bits is the number of bits which will be read at once, must be
488 * identical to nb_bits in init_vlc()
489 * @param max_depth is the number of times bits bits must be read to completely
490 * read the longest vlc code
491 * = (max_vlc_length + bits - 1) / bits
493 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
494 int bits, int max_depth)
501 GET_VLC(code, re, s, table, bits, max_depth);
508 static inline int decode012(GetBitContext *gb)
515 return get_bits1(gb) + 1;
518 static inline int decode210(GetBitContext *gb)
523 return 2 - get_bits1(gb);
526 static inline int get_bits_left(GetBitContext *gb)
528 return gb->size_in_bits - get_bits_count(gb);
531 #endif /* AVCODEC_GET_BITS_H */