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
31 #include "libavutil/common.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/log.h"
34 #include "libavutil/avassert.h"
39 * Safe bitstream reading:
40 * optionally, the get_bits API can check to ensure that we
41 * don't read past input buffer boundaries. This is protected
42 * with CONFIG_SAFE_BITSTREAM_READER at the global level, and
43 * then below that with UNCHECKED_BITSTREAM_READER at the per-
44 * decoder level. This means that decoders that check internally
45 * can "#define UNCHECKED_BITSTREAM_READER 1" to disable
47 * Boundary checking causes a minor performance penalty so for
48 * applications that won't want/need this, it can be disabled
49 * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
51 #ifndef UNCHECKED_BITSTREAM_READER
52 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
55 typedef struct GetBitContext {
56 const uint8_t *buffer, *buffer_end;
59 int size_in_bits_plus8;
62 /* Bitstream reader API docs:
64 * arbitrary name which is used as prefix for the internal variables
69 * OPEN_READER(name, gb)
70 * load gb into local variables
72 * CLOSE_READER(name, gb)
73 * store local vars in gb
75 * UPDATE_CACHE(name, gb)
76 * Refill the internal cache from the bitstream.
77 * After this call at least MIN_CACHE_BITS will be available.
80 * Will output the contents of the internal cache,
81 * next bit is MSB of 32 or 64 bits (FIXME 64 bits).
83 * SHOW_UBITS(name, gb, num)
84 * Will return the next num bits.
86 * SHOW_SBITS(name, gb, num)
87 * Will return the next num bits and do sign extension.
89 * SKIP_BITS(name, gb, num)
90 * Will skip over the next num bits.
91 * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER.
93 * SKIP_CACHE(name, gb, num)
94 * Will remove the next num bits from the cache (note SKIP_COUNTER
95 * MUST be called before UPDATE_CACHE / CLOSE_READER).
97 * SKIP_COUNTER(name, gb, num)
98 * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS).
100 * LAST_SKIP_BITS(name, gb, num)
101 * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER.
103 * BITS_LEFT(name, gb)
104 * Return the number of bits left
106 * For examples see get_bits, show_bits, skip_bits, get_vlc.
109 #ifdef LONG_BITSTREAM_READER
110 # define MIN_CACHE_BITS 32
112 # define MIN_CACHE_BITS 25
115 #define OPEN_READER_NOSIZE(name, gb) \
116 unsigned int name ## _index = (gb)->index; \
117 unsigned int av_unused name ## _cache
119 #if UNCHECKED_BITSTREAM_READER
120 #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb)
122 #define BITS_AVAILABLE(name, gb) 1
124 #define OPEN_READER(name, gb) \
125 OPEN_READER_NOSIZE(name, gb); \
126 unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8
128 #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8
131 #define CLOSE_READER(name, gb) (gb)->index = name ## _index
133 # ifdef LONG_BITSTREAM_READER
135 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
136 AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
138 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
139 AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))
143 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
144 AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
146 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
147 AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)
152 #ifdef BITSTREAM_READER_LE
154 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
156 # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
160 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
162 # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
166 #if UNCHECKED_BITSTREAM_READER
167 # define SKIP_COUNTER(name, gb, num) name ## _index += (num)
169 # define SKIP_COUNTER(name, gb, num) \
170 name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
173 #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index))
175 #define SKIP_BITS(name, gb, num) \
177 SKIP_CACHE(name, gb, num); \
178 SKIP_COUNTER(name, gb, num); \
181 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
183 #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)
184 #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)
186 #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)
187 #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)
189 #ifdef BITSTREAM_READER_LE
190 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)
191 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)
193 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)
194 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)
197 #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
199 static inline int get_bits_count(const GetBitContext *s)
204 static inline void skip_bits_long(GetBitContext *s, int n)
206 #if UNCHECKED_BITSTREAM_READER
209 s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
214 * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
215 * if MSB not set it is negative
216 * @param n length in bits
218 static inline int get_xbits(GetBitContext *s, int n)
221 register int32_t cache;
223 av_assert2(n>0 && n<=25);
225 cache = GET_CACHE(re, s);
227 LAST_SKIP_BITS(re, s, n);
229 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
232 static inline int get_xbits_le(GetBitContext *s, int n)
235 register int32_t cache;
237 av_assert2(n>0 && n<=25);
238 UPDATE_CACHE_LE(re, s);
239 cache = GET_CACHE(re, s);
240 sign = sign_extend(~cache, n) >> 31;
241 LAST_SKIP_BITS(re, s, n);
243 return (zero_extend(sign ^ cache, n) ^ sign) - sign;
246 static inline int get_sbits(GetBitContext *s, int n)
250 av_assert2(n>0 && n<=25);
252 tmp = SHOW_SBITS(re, s, n);
253 LAST_SKIP_BITS(re, s, n);
261 static inline unsigned int get_bits(GetBitContext *s, int n)
265 av_assert2(n>0 && n<=25);
267 tmp = SHOW_UBITS(re, s, n);
268 LAST_SKIP_BITS(re, s, n);
276 static av_always_inline int get_bitsz(GetBitContext *s, int n)
278 return n ? get_bits(s, n) : 0;
281 static inline unsigned int get_bits_le(GetBitContext *s, int n)
285 av_assert2(n>0 && n<=25);
286 UPDATE_CACHE_LE(re, s);
287 tmp = SHOW_UBITS_LE(re, s, n);
288 LAST_SKIP_BITS(re, s, n);
296 static inline unsigned int show_bits(GetBitContext *s, int n)
299 OPEN_READER_NOSIZE(re, s);
300 av_assert2(n>0 && n<=25);
302 tmp = SHOW_UBITS(re, s, n);
306 static inline void skip_bits(GetBitContext *s, int n)
309 LAST_SKIP_BITS(re, s, n);
313 static inline unsigned int get_bits1(GetBitContext *s)
315 unsigned int index = s->index;
316 uint8_t result = s->buffer[index >> 3];
317 #ifdef BITSTREAM_READER_LE
318 result >>= index & 7;
321 result <<= index & 7;
324 #if !UNCHECKED_BITSTREAM_READER
325 if (s->index < s->size_in_bits_plus8)
333 static inline unsigned int show_bits1(GetBitContext *s)
335 return show_bits(s, 1);
338 static inline void skip_bits1(GetBitContext *s)
346 static inline unsigned int get_bits_long(GetBitContext *s, int n)
348 av_assert2(n>=0 && n<=32);
351 } else if (n <= MIN_CACHE_BITS) {
352 return get_bits(s, n);
354 #ifdef BITSTREAM_READER_LE
355 unsigned ret = get_bits(s, 16);
356 return ret | (get_bits(s, n - 16) << 16);
358 unsigned ret = get_bits(s, 16) << (n - 16);
359 return ret | get_bits(s, n - 16);
367 static inline uint64_t get_bits64(GetBitContext *s, int n)
370 return get_bits_long(s, n);
372 #ifdef BITSTREAM_READER_LE
373 uint64_t ret = get_bits_long(s, 32);
374 return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
376 uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
377 return ret | get_bits_long(s, 32);
383 * Read 0-32 bits as a signed integer.
385 static inline int get_sbits_long(GetBitContext *s, int n)
387 // sign_extend(x, 0) is undefined
391 return sign_extend(get_bits_long(s, n), n);
397 static inline unsigned int show_bits_long(GetBitContext *s, int n)
399 if (n <= MIN_CACHE_BITS) {
400 return show_bits(s, n);
402 GetBitContext gb = *s;
403 return get_bits_long(&gb, n);
407 static inline int check_marker(void *logctx, GetBitContext *s, const char *msg)
409 int bit = get_bits1(s);
411 av_log(logctx, AV_LOG_INFO, "Marker bit missing at %d of %d %s\n",
412 get_bits_count(s) - 1, s->size_in_bits, msg);
418 * Initialize GetBitContext.
419 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
420 * larger than the actual read bits because some optimized bitstream
421 * readers read 32 or 64 bit at once and could read over the end
422 * @param bit_size the size of the buffer in bits
423 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
425 static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
431 if (bit_size >= INT_MAX - 7 || bit_size < 0 || !buffer) {
434 ret = AVERROR_INVALIDDATA;
437 buffer_size = (bit_size + 7) >> 3;
440 s->size_in_bits = bit_size;
441 s->size_in_bits_plus8 = bit_size + 8;
442 s->buffer_end = buffer + buffer_size;
449 * Initialize GetBitContext.
450 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
451 * larger than the actual read bits because some optimized bitstream
452 * readers read 32 or 64 bit at once and could read over the end
453 * @param byte_size the size of the buffer in bytes
454 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
456 static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
459 if (byte_size > INT_MAX / 8 || byte_size < 0)
461 return init_get_bits(s, buffer, byte_size * 8);
464 static inline const uint8_t *align_get_bits(GetBitContext *s)
466 int n = -get_bits_count(s) & 7;
469 return s->buffer + (s->index >> 3);
473 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
474 * If the vlc code is invalid and max_depth>1, then the number of bits removed
477 #define GET_VLC(code, name, gb, table, bits, max_depth) \
480 unsigned int index; \
482 index = SHOW_UBITS(name, gb, bits); \
483 code = table[index][0]; \
484 n = table[index][1]; \
486 if (max_depth > 1 && n < 0) { \
487 LAST_SKIP_BITS(name, gb, bits); \
488 UPDATE_CACHE(name, gb); \
492 index = SHOW_UBITS(name, gb, nb_bits) + code; \
493 code = table[index][0]; \
494 n = table[index][1]; \
495 if (max_depth > 2 && n < 0) { \
496 LAST_SKIP_BITS(name, gb, nb_bits); \
497 UPDATE_CACHE(name, gb); \
501 index = SHOW_UBITS(name, gb, nb_bits) + code; \
502 code = table[index][0]; \
503 n = table[index][1]; \
506 SKIP_BITS(name, gb, n); \
509 #define GET_RL_VLC(level, run, name, gb, table, bits, \
510 max_depth, need_update) \
513 unsigned int index; \
515 index = SHOW_UBITS(name, gb, bits); \
516 level = table[index].level; \
517 n = table[index].len; \
519 if (max_depth > 1 && n < 0) { \
520 SKIP_BITS(name, gb, bits); \
522 UPDATE_CACHE(name, gb); \
527 index = SHOW_UBITS(name, gb, nb_bits) + level; \
528 level = table[index].level; \
529 n = table[index].len; \
530 if (max_depth > 2 && n < 0) { \
531 LAST_SKIP_BITS(name, gb, nb_bits); \
533 UPDATE_CACHE(name, gb); \
537 index = SHOW_UBITS(name, gb, nb_bits) + level; \
538 level = table[index].level; \
539 n = table[index].len; \
542 run = table[index].run; \
543 SKIP_BITS(name, gb, n); \
548 * @param bits is the number of bits which will be read at once, must be
549 * identical to nb_bits in init_vlc()
550 * @param max_depth is the number of times bits bits must be read to completely
551 * read the longest vlc code
552 * = (max_vlc_length + bits - 1) / bits
554 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
555 int bits, int max_depth)
562 GET_VLC(code, re, s, table, bits, max_depth);
569 static inline int decode012(GetBitContext *gb)
576 return get_bits1(gb) + 1;
579 static inline int decode210(GetBitContext *gb)
584 return 2 - get_bits1(gb);
587 static inline int get_bits_left(GetBitContext *gb)
589 return gb->size_in_bits - get_bits_count(gb);
592 static inline int skip_1stop_8data_bits(GetBitContext *gb)
594 if (get_bits_left(gb) <= 0)
595 return AVERROR_INVALIDDATA;
597 while (get_bits1(gb)) {
599 if (get_bits_left(gb) <= 0)
600 return AVERROR_INVALIDDATA;
606 #endif /* AVCODEC_GET_BITS_H */