2 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2016 Alexandra Hájková
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * bitstream reader API header.
27 #ifndef AVCODEC_GET_BITS_H
28 #define AVCODEC_GET_BITS_H
32 #include "libavutil/common.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/log.h"
35 #include "libavutil/avassert.h"
41 * Safe bitstream reading:
42 * optionally, the get_bits API can check to ensure that we
43 * don't read past input buffer boundaries. This is protected
44 * with CONFIG_SAFE_BITSTREAM_READER at the global level, and
45 * then below that with UNCHECKED_BITSTREAM_READER at the per-
46 * decoder level. This means that decoders that check internally
47 * can "#define UNCHECKED_BITSTREAM_READER 1" to disable
49 * Boundary checking causes a minor performance penalty so for
50 * applications that won't want/need this, it can be disabled
51 * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
53 #ifndef UNCHECKED_BITSTREAM_READER
54 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
57 #ifndef CACHED_BITSTREAM_READER
58 #define CACHED_BITSTREAM_READER 0
61 typedef struct GetBitContext {
62 const uint8_t *buffer, *buffer_end;
63 #if CACHED_BITSTREAM_READER
69 int size_in_bits_plus8;
72 static inline unsigned int get_bits(GetBitContext *s, int n);
73 static inline void skip_bits(GetBitContext *s, int n);
74 static inline unsigned int show_bits(GetBitContext *s, int n);
76 /* Bitstream reader API docs:
78 * arbitrary name which is used as prefix for the internal variables
83 * OPEN_READER(name, gb)
84 * load gb into local variables
86 * CLOSE_READER(name, gb)
87 * store local vars in gb
89 * UPDATE_CACHE(name, gb)
90 * Refill the internal cache from the bitstream.
91 * After this call at least MIN_CACHE_BITS will be available.
94 * Will output the contents of the internal cache,
95 * next bit is MSB of 32 or 64 bits (FIXME 64 bits).
97 * SHOW_UBITS(name, gb, num)
98 * Will return the next num bits.
100 * SHOW_SBITS(name, gb, num)
101 * Will return the next num bits and do sign extension.
103 * SKIP_BITS(name, gb, num)
104 * Will skip over the next num bits.
105 * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER.
107 * SKIP_CACHE(name, gb, num)
108 * Will remove the next num bits from the cache (note SKIP_COUNTER
109 * MUST be called before UPDATE_CACHE / CLOSE_READER).
111 * SKIP_COUNTER(name, gb, num)
112 * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS).
114 * LAST_SKIP_BITS(name, gb, num)
115 * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER.
117 * BITS_LEFT(name, gb)
118 * Return the number of bits left
120 * For examples see get_bits, show_bits, skip_bits, get_vlc.
123 #if CACHED_BITSTREAM_READER
124 # define MIN_CACHE_BITS 64
125 #elif defined LONG_BITSTREAM_READER
126 # define MIN_CACHE_BITS 32
128 # define MIN_CACHE_BITS 25
131 #if !CACHED_BITSTREAM_READER
133 #define OPEN_READER_NOSIZE(name, gb) \
134 unsigned int name ## _index = (gb)->index; \
135 unsigned int av_unused name ## _cache
137 #if UNCHECKED_BITSTREAM_READER
138 #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb)
140 #define BITS_AVAILABLE(name, gb) 1
142 #define OPEN_READER(name, gb) \
143 OPEN_READER_NOSIZE(name, gb); \
144 unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8
146 #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8
149 #define CLOSE_READER(name, gb) (gb)->index = name ## _index
151 # ifdef LONG_BITSTREAM_READER
153 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
154 AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
156 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
157 AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))
161 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
162 AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
164 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
165 AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)
170 #ifdef BITSTREAM_READER_LE
172 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
174 # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
178 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
180 # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
184 #if UNCHECKED_BITSTREAM_READER
185 # define SKIP_COUNTER(name, gb, num) name ## _index += (num)
187 # define SKIP_COUNTER(name, gb, num) \
188 name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
191 #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index))
193 #define SKIP_BITS(name, gb, num) \
195 SKIP_CACHE(name, gb, num); \
196 SKIP_COUNTER(name, gb, num); \
199 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
201 #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)
202 #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)
204 #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)
205 #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)
207 #ifdef BITSTREAM_READER_LE
208 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)
209 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)
211 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)
212 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)
215 #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
219 static inline int get_bits_count(const GetBitContext *s)
221 #if CACHED_BITSTREAM_READER
222 return s->index - s->bits_left;
228 #if CACHED_BITSTREAM_READER
229 static inline void refill_32(GetBitContext *s, int is_le)
231 #if !UNCHECKED_BITSTREAM_READER
232 if (s->index >> 3 >= s->buffer_end - s->buffer)
237 s->cache = (uint64_t)AV_RL32(s->buffer + (s->index >> 3)) << s->bits_left | s->cache;
239 s->cache = s->cache | (uint64_t)AV_RB32(s->buffer + (s->index >> 3)) << (32 - s->bits_left);
244 static inline void refill_64(GetBitContext *s, int is_le)
246 #if !UNCHECKED_BITSTREAM_READER
247 if (s->index >> 3 >= s->buffer_end - s->buffer)
252 s->cache = AV_RL64(s->buffer + (s->index >> 3));
254 s->cache = AV_RB64(s->buffer + (s->index >> 3));
259 static inline uint64_t get_val(GetBitContext *s, unsigned n, int is_le)
262 av_assert2(n>0 && n<=63);
264 ret = s->cache & ((UINT64_C(1) << n) - 1);
267 ret = s->cache >> (64 - n);
274 static inline unsigned show_val(const GetBitContext *s, unsigned n)
276 #ifdef BITSTREAM_READER_LE
277 return s->cache & ((UINT64_C(1) << n) - 1);
279 return s->cache >> (64 - n);
285 * Skips the specified number of bits.
286 * @param n the number of bits to skip,
287 * For the UNCHECKED_BITSTREAM_READER this must not cause the distance
288 * from the start to overflow int32_t. Staying within the bitstream + padding
289 * is sufficient, too.
291 static inline void skip_bits_long(GetBitContext *s, int n)
293 #if CACHED_BITSTREAM_READER
296 #if UNCHECKED_BITSTREAM_READER
299 s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
304 #if CACHED_BITSTREAM_READER
305 static inline void skip_remaining(GetBitContext *s, unsigned n)
307 #ifdef BITSTREAM_READER_LE
317 * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
318 * if MSB not set it is negative
319 * @param n length in bits
321 static inline int get_xbits(GetBitContext *s, int n)
323 #if CACHED_BITSTREAM_READER
324 int32_t cache = show_bits(s, 32);
325 int sign = ~cache >> 31;
326 skip_remaining(s, n);
328 return ((((uint32_t)(sign ^ cache)) >> (32 - n)) ^ sign) - sign;
331 register int32_t cache;
333 av_assert2(n>0 && n<=25);
335 cache = GET_CACHE(re, s);
337 LAST_SKIP_BITS(re, s, n);
339 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
343 #if !CACHED_BITSTREAM_READER
344 static inline int get_xbits_le(GetBitContext *s, int n)
347 register int32_t cache;
349 av_assert2(n>0 && n<=25);
350 UPDATE_CACHE_LE(re, s);
351 cache = GET_CACHE(re, s);
352 sign = sign_extend(~cache, n) >> 31;
353 LAST_SKIP_BITS(re, s, n);
355 return (zero_extend(sign ^ cache, n) ^ sign) - sign;
359 static inline int get_sbits(GetBitContext *s, int n)
362 #if CACHED_BITSTREAM_READER
363 av_assert2(n>0 && n<=25);
364 tmp = sign_extend(get_bits(s, n), n);
367 av_assert2(n>0 && n<=25);
369 tmp = SHOW_SBITS(re, s, n);
370 LAST_SKIP_BITS(re, s, n);
379 static inline unsigned int get_bits(GetBitContext *s, int n)
381 register unsigned int tmp;
382 #if CACHED_BITSTREAM_READER
384 av_assert2(n>0 && n<=32);
385 if (n > s->bits_left) {
386 #ifdef BITSTREAM_READER_LE
391 if (s->bits_left < 32)
395 #ifdef BITSTREAM_READER_LE
396 tmp = get_val(s, n, 1);
398 tmp = get_val(s, n, 0);
402 av_assert2(n>0 && n<=25);
404 tmp = SHOW_UBITS(re, s, n);
405 LAST_SKIP_BITS(re, s, n);
408 av_assert2(tmp < UINT64_C(1) << n);
415 static av_always_inline int get_bitsz(GetBitContext *s, int n)
417 return n ? get_bits(s, n) : 0;
420 static inline unsigned int get_bits_le(GetBitContext *s, int n)
422 #if CACHED_BITSTREAM_READER
423 av_assert2(n>0 && n<=32);
424 if (n > s->bits_left) {
426 if (s->bits_left < 32)
430 return get_val(s, n, 1);
434 av_assert2(n>0 && n<=25);
435 UPDATE_CACHE_LE(re, s);
436 tmp = SHOW_UBITS_LE(re, s, n);
437 LAST_SKIP_BITS(re, s, n);
446 static inline unsigned int show_bits(GetBitContext *s, int n)
448 register unsigned int tmp;
449 #if CACHED_BITSTREAM_READER
450 if (n > s->bits_left)
451 #ifdef BITSTREAM_READER_LE
457 tmp = show_val(s, n);
459 OPEN_READER_NOSIZE(re, s);
460 av_assert2(n>0 && n<=25);
462 tmp = SHOW_UBITS(re, s, n);
467 static inline void skip_bits(GetBitContext *s, int n)
469 #if CACHED_BITSTREAM_READER
470 if (n < s->bits_left)
471 skip_remaining(s, n);
478 unsigned skip = (n / 8) * 8;
483 #ifdef BITSTREAM_READER_LE
489 skip_remaining(s, n);
493 LAST_SKIP_BITS(re, s, n);
498 static inline unsigned int get_bits1(GetBitContext *s)
500 #if CACHED_BITSTREAM_READER
502 #ifdef BITSTREAM_READER_LE
508 #ifdef BITSTREAM_READER_LE
509 return get_val(s, 1, 1);
511 return get_val(s, 1, 0);
514 unsigned int index = s->index;
515 uint8_t result = s->buffer[index >> 3];
516 #ifdef BITSTREAM_READER_LE
517 result >>= index & 7;
520 result <<= index & 7;
523 #if !UNCHECKED_BITSTREAM_READER
524 if (s->index < s->size_in_bits_plus8)
533 static inline unsigned int show_bits1(GetBitContext *s)
535 return show_bits(s, 1);
538 static inline void skip_bits1(GetBitContext *s)
546 static inline unsigned int get_bits_long(GetBitContext *s, int n)
548 av_assert2(n>=0 && n<=32);
551 #if CACHED_BITSTREAM_READER
553 return get_bits(s, n);
555 } else if (n <= MIN_CACHE_BITS) {
556 return get_bits(s, n);
558 #ifdef BITSTREAM_READER_LE
559 unsigned ret = get_bits(s, 16);
560 return ret | (get_bits(s, n - 16) << 16);
562 unsigned ret = get_bits(s, 16) << (n - 16);
563 return ret | get_bits(s, n - 16);
572 static inline uint64_t get_bits64(GetBitContext *s, int n)
575 return get_bits_long(s, n);
577 #ifdef BITSTREAM_READER_LE
578 uint64_t ret = get_bits_long(s, 32);
579 return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
581 uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
582 return ret | get_bits_long(s, 32);
588 * Read 0-32 bits as a signed integer.
590 static inline int get_sbits_long(GetBitContext *s, int n)
592 // sign_extend(x, 0) is undefined
596 return sign_extend(get_bits_long(s, n), n);
602 static inline unsigned int show_bits_long(GetBitContext *s, int n)
604 if (n <= MIN_CACHE_BITS) {
605 return show_bits(s, n);
607 GetBitContext gb = *s;
608 return get_bits_long(&gb, n);
612 static inline int check_marker(void *logctx, GetBitContext *s, const char *msg)
614 int bit = get_bits1(s);
616 av_log(logctx, AV_LOG_INFO, "Marker bit missing at %d of %d %s\n",
617 get_bits_count(s) - 1, s->size_in_bits, msg);
622 static inline int init_get_bits_xe(GetBitContext *s, const uint8_t *buffer,
623 int bit_size, int is_le)
628 if (bit_size >= INT_MAX - FFMAX(7, AV_INPUT_BUFFER_PADDING_SIZE*8) || bit_size < 0 || !buffer) {
631 ret = AVERROR_INVALIDDATA;
634 buffer_size = (bit_size + 7) >> 3;
637 s->size_in_bits = bit_size;
638 s->size_in_bits_plus8 = bit_size + 8;
639 s->buffer_end = buffer + buffer_size;
642 #if CACHED_BITSTREAM_READER
652 * Initialize GetBitContext.
653 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
654 * larger than the actual read bits because some optimized bitstream
655 * readers read 32 or 64 bit at once and could read over the end
656 * @param bit_size the size of the buffer in bits
657 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
659 static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
662 #ifdef BITSTREAM_READER_LE
663 return init_get_bits_xe(s, buffer, bit_size, 1);
665 return init_get_bits_xe(s, buffer, bit_size, 0);
670 * Initialize GetBitContext.
671 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
672 * larger than the actual read bits because some optimized bitstream
673 * readers read 32 or 64 bit at once and could read over the end
674 * @param byte_size the size of the buffer in bytes
675 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
677 static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
680 if (byte_size > INT_MAX / 8 || byte_size < 0)
682 return init_get_bits(s, buffer, byte_size * 8);
685 static inline int init_get_bits8_le(GetBitContext *s, const uint8_t *buffer,
688 if (byte_size > INT_MAX / 8 || byte_size < 0)
690 return init_get_bits_xe(s, buffer, byte_size * 8, 1);
693 static inline const uint8_t *align_get_bits(GetBitContext *s)
695 int n = -get_bits_count(s) & 7;
698 return s->buffer + (s->index >> 3);
702 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
703 * If the vlc code is invalid and max_depth>1, then the number of bits removed
706 #define GET_VLC(code, name, gb, table, bits, max_depth) \
709 unsigned int index; \
711 index = SHOW_UBITS(name, gb, bits); \
712 code = table[index][0]; \
713 n = table[index][1]; \
715 if (max_depth > 1 && n < 0) { \
716 LAST_SKIP_BITS(name, gb, bits); \
717 UPDATE_CACHE(name, gb); \
721 index = SHOW_UBITS(name, gb, nb_bits) + code; \
722 code = table[index][0]; \
723 n = table[index][1]; \
724 if (max_depth > 2 && n < 0) { \
725 LAST_SKIP_BITS(name, gb, nb_bits); \
726 UPDATE_CACHE(name, gb); \
730 index = SHOW_UBITS(name, gb, nb_bits) + code; \
731 code = table[index][0]; \
732 n = table[index][1]; \
735 SKIP_BITS(name, gb, n); \
738 #define GET_RL_VLC(level, run, name, gb, table, bits, \
739 max_depth, need_update) \
742 unsigned int index; \
744 index = SHOW_UBITS(name, gb, bits); \
745 level = table[index].level; \
746 n = table[index].len; \
748 if (max_depth > 1 && n < 0) { \
749 SKIP_BITS(name, gb, bits); \
751 UPDATE_CACHE(name, gb); \
756 index = SHOW_UBITS(name, gb, nb_bits) + level; \
757 level = table[index].level; \
758 n = table[index].len; \
759 if (max_depth > 2 && n < 0) { \
760 LAST_SKIP_BITS(name, gb, nb_bits); \
762 UPDATE_CACHE(name, gb); \
766 index = SHOW_UBITS(name, gb, nb_bits) + level; \
767 level = table[index].level; \
768 n = table[index].len; \
771 run = table[index].run; \
772 SKIP_BITS(name, gb, n); \
775 /* Return the LUT element for the given bitstream configuration. */
776 static inline int set_idx(GetBitContext *s, int code, int *n, int *nb_bits,
777 VLC_TYPE (*table)[2])
782 idx = show_bits(s, *nb_bits) + code;
785 return table[idx][0];
790 * @param bits is the number of bits which will be read at once, must be
791 * identical to nb_bits in init_vlc()
792 * @param max_depth is the number of times bits bits must be read to completely
793 * read the longest vlc code
794 * = (max_vlc_length + bits - 1) / bits
795 * @returns the code parsed or -1 if no vlc matches
797 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
798 int bits, int max_depth)
800 #if CACHED_BITSTREAM_READER
802 unsigned idx = show_bits(s, bits);
803 int code = table[idx][0];
804 int n = table[idx][1];
806 if (max_depth > 1 && n < 0) {
807 skip_remaining(s, bits);
808 code = set_idx(s, code, &n, &nb_bits, table);
809 if (max_depth > 2 && n < 0) {
810 skip_remaining(s, nb_bits);
811 code = set_idx(s, code, &n, &nb_bits, table);
814 skip_remaining(s, n);
823 GET_VLC(code, re, s, table, bits, max_depth);
831 static inline int decode012(GetBitContext *gb)
838 return get_bits1(gb) + 1;
841 static inline int decode210(GetBitContext *gb)
846 return 2 - get_bits1(gb);
849 static inline int get_bits_left(GetBitContext *gb)
851 return gb->size_in_bits - get_bits_count(gb);
854 static inline int skip_1stop_8data_bits(GetBitContext *gb)
856 if (get_bits_left(gb) <= 0)
857 return AVERROR_INVALIDDATA;
859 while (get_bits1(gb)) {
861 if (get_bits_left(gb) <= 0)
862 return AVERROR_INVALIDDATA;
868 #endif /* AVCODEC_GET_BITS_H */