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
30 #include "libavutil/common.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/log.h"
36 * Safe bitstream reading:
37 * optionally, the get_bits API can check to ensure that we
38 * don't read past input buffer boundaries. This is protected
39 * with CONFIG_SAFE_BITSTREAM_READER at the global level, and
40 * then below that with UNCHECKED_BITSTREAM_READER at the per-
41 * decoder level. This means that decoders that check internally
42 * can "#define UNCHECKED_BITSTREAM_READER 1" to disable
44 * Boundary checking causes a minor performance penalty so for
45 * applications that won't want/need this, it can be disabled
46 * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
48 #ifndef UNCHECKED_BITSTREAM_READER
49 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
52 typedef struct GetBitContext {
53 const uint8_t *buffer, *buffer_end;
56 #if !UNCHECKED_BITSTREAM_READER
57 int size_in_bits_plus8;
61 #define VLC_TYPE int16_t
65 VLC_TYPE (*table)[2]; ///< code, bits
66 int table_size, table_allocated;
69 typedef struct RL_VLC_ELEM {
75 /* Bitstream reader API docs:
77 arbitrary name which is used as prefix for the internal variables
83 load gb into local variables
85 CLOSE_READER(name, gb)
86 store local vars in gb
88 UPDATE_CACHE(name, gb)
89 refill the internal cache from the bitstream
90 after this call at least MIN_CACHE_BITS will be available,
93 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
95 SHOW_UBITS(name, gb, num)
96 will return the next num bits
98 SHOW_SBITS(name, gb, num)
99 will return the next num bits and do sign extension
101 SKIP_BITS(name, gb, num)
102 will skip over the next num bits
103 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
105 SKIP_CACHE(name, gb, num)
106 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
108 SKIP_COUNTER(name, gb, num)
109 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
111 LAST_SKIP_BITS(name, gb, num)
112 like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER
114 for examples see get_bits, show_bits, skip_bits, get_vlc
117 #ifdef LONG_BITSTREAM_READER
118 # define MIN_CACHE_BITS 32
120 # define MIN_CACHE_BITS 25
123 #if UNCHECKED_BITSTREAM_READER
124 #define OPEN_READER(name, gb) \
125 unsigned int name##_index = (gb)->index; \
126 unsigned int av_unused name##_cache = 0
128 #define HAVE_BITS_REMAINING(name, gb) 1
130 #define OPEN_READER(name, gb) \
131 unsigned int name##_index = (gb)->index; \
132 unsigned int av_unused name##_cache = 0; \
133 unsigned int av_unused name##_size_plus8 = \
134 (gb)->size_in_bits_plus8
136 #define HAVE_BITS_REMAINING(name, gb) \
137 name##_index < name##_size_plus8
140 #define CLOSE_READER(name, gb) (gb)->index = name##_index
142 #ifdef BITSTREAM_READER_LE
144 # ifdef LONG_BITSTREAM_READER
145 # define UPDATE_CACHE(name, gb) name##_cache = \
146 AV_RL64((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
148 # define UPDATE_CACHE(name, gb) name##_cache = \
149 AV_RL32((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
152 # define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
156 # ifdef LONG_BITSTREAM_READER
157 # define UPDATE_CACHE(name, gb) name##_cache = \
158 AV_RB64((gb)->buffer + (name##_index >> 3)) >> (32 - (name##_index & 7))
160 # define UPDATE_CACHE(name, gb) name##_cache = \
161 AV_RB32((gb)->buffer + (name##_index >> 3)) << (name##_index & 7)
164 # define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
168 #if UNCHECKED_BITSTREAM_READER
169 # define SKIP_COUNTER(name, gb, num) name##_index += (num)
171 # define SKIP_COUNTER(name, gb, num) \
172 name##_index = FFMIN(name##_size_plus8, name##_index + (num))
175 #define SKIP_BITS(name, gb, num) do { \
176 SKIP_CACHE(name, gb, num); \
177 SKIP_COUNTER(name, gb, num); \
180 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
182 #ifdef BITSTREAM_READER_LE
183 # define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
184 # define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
186 # define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
187 # define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
190 #define GET_CACHE(name, gb) ((uint32_t)name##_cache)
192 static inline int get_bits_count(const GetBitContext *s)
197 static inline void skip_bits_long(GetBitContext *s, int n){
198 #if UNCHECKED_BITSTREAM_READER
201 s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
206 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
207 * if MSB not set it is negative
208 * @param n length in bits
210 static inline int get_xbits(GetBitContext *s, int n)
213 register int32_t cache;
216 cache = GET_CACHE(re, s);
218 LAST_SKIP_BITS(re, s, n);
220 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
223 static inline int get_sbits(GetBitContext *s, int n)
228 tmp = SHOW_SBITS(re, s, n);
229 LAST_SKIP_BITS(re, s, n);
237 static inline unsigned int get_bits(GetBitContext *s, int n)
242 tmp = SHOW_UBITS(re, s, n);
243 LAST_SKIP_BITS(re, s, n);
251 static inline unsigned int show_bits(GetBitContext *s, int n)
256 tmp = SHOW_UBITS(re, s, n);
260 static inline void skip_bits(GetBitContext *s, int n)
264 LAST_SKIP_BITS(re, s, n);
268 static inline unsigned int get_bits1(GetBitContext *s)
270 unsigned int index = s->index;
271 uint8_t result = s->buffer[index>>3];
272 #ifdef BITSTREAM_READER_LE
273 result >>= index & 7;
276 result <<= index & 7;
279 #if !UNCHECKED_BITSTREAM_READER
280 if (s->index < s->size_in_bits_plus8)
288 static inline unsigned int show_bits1(GetBitContext *s)
290 return show_bits(s, 1);
293 static inline void skip_bits1(GetBitContext *s)
301 static inline unsigned int get_bits_long(GetBitContext *s, int n)
303 if (n <= MIN_CACHE_BITS)
304 return get_bits(s, n);
306 #ifdef BITSTREAM_READER_LE
307 int ret = get_bits(s, 16);
308 return ret | (get_bits(s, n-16) << 16);
310 int ret = get_bits(s, 16) << (n-16);
311 return ret | get_bits(s, n-16);
317 * Read 0-32 bits as a signed integer.
319 static inline int get_sbits_long(GetBitContext *s, int n)
321 return sign_extend(get_bits_long(s, n), n);
327 static inline unsigned int show_bits_long(GetBitContext *s, int n)
329 if (n <= MIN_CACHE_BITS)
330 return show_bits(s, n);
332 GetBitContext gb = *s;
333 return get_bits_long(&gb, n);
337 static inline int check_marker(GetBitContext *s, const char *msg)
339 int bit = get_bits1(s);
341 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
347 * Inititalize GetBitContext.
348 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger than the actual read bits
349 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
350 * @param bit_size the size of the buffer in bits
352 static inline void init_get_bits(GetBitContext *s, const uint8_t *buffer,
355 int buffer_size = (bit_size+7)>>3;
356 if (buffer_size < 0 || bit_size < 0) {
357 buffer_size = bit_size = 0;
362 s->size_in_bits = bit_size;
363 #if !UNCHECKED_BITSTREAM_READER
364 s->size_in_bits_plus8 = bit_size + 8;
366 s->buffer_end = buffer + buffer_size;
370 static inline void align_get_bits(GetBitContext *s)
372 int n = -get_bits_count(s) & 7;
373 if (n) skip_bits(s, n);
376 #define init_vlc(vlc, nb_bits, nb_codes, \
377 bits, bits_wrap, bits_size, \
378 codes, codes_wrap, codes_size, \
380 ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
381 bits, bits_wrap, bits_size, \
382 codes, codes_wrap, codes_size, \
385 int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
386 const void *bits, int bits_wrap, int bits_size,
387 const void *codes, int codes_wrap, int codes_size,
388 const void *symbols, int symbols_wrap, int symbols_size,
390 #define INIT_VLC_LE 2
391 #define INIT_VLC_USE_NEW_STATIC 4
392 void ff_free_vlc(VLC *vlc);
394 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do { \
395 static VLC_TYPE table[static_size][2]; \
396 (vlc)->table = table; \
397 (vlc)->table_allocated = static_size; \
398 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC); \
403 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
404 * If the vlc code is invalid and max_depth>1, then the number of bits removed
407 #define GET_VLC(code, name, gb, table, bits, max_depth) \
410 unsigned int index; \
412 index = SHOW_UBITS(name, gb, bits); \
413 code = table[index][0]; \
414 n = table[index][1]; \
416 if (max_depth > 1 && n < 0) { \
417 LAST_SKIP_BITS(name, gb, bits); \
418 UPDATE_CACHE(name, gb); \
422 index = SHOW_UBITS(name, gb, nb_bits) + code; \
423 code = table[index][0]; \
424 n = table[index][1]; \
425 if (max_depth > 2 && n < 0) { \
426 LAST_SKIP_BITS(name, gb, nb_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]; \
436 SKIP_BITS(name, gb, n); \
439 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) \
442 unsigned int index; \
444 index = SHOW_UBITS(name, gb, bits); \
445 level = table[index].level; \
446 n = table[index].len; \
448 if (max_depth > 1 && n < 0) { \
449 SKIP_BITS(name, gb, bits); \
451 UPDATE_CACHE(name, gb); \
456 index = SHOW_UBITS(name, gb, nb_bits) + level; \
457 level = table[index].level; \
458 n = table[index].len; \
460 run = table[index].run; \
461 SKIP_BITS(name, gb, n); \
467 * @param bits is the number of bits which will be read at once, must be
468 * identical to nb_bits in init_vlc()
469 * @param max_depth is the number of times bits bits must be read to completely
470 * read the longest vlc code
471 * = (max_vlc_length + bits - 1) / bits
473 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
474 int bits, int max_depth)
481 GET_VLC(code, re, s, table, bits, max_depth);
487 static inline int decode012(GetBitContext *gb)
494 return get_bits1(gb) + 1;
497 static inline int decode210(GetBitContext *gb)
502 return 2 - get_bits1(gb);
505 static inline int get_bits_left(GetBitContext *gb)
507 return gb->size_in_bits - get_bits_count(gb);
513 static inline void print_bin(int bits, int n)
517 for (i = n-1; i >= 0; i--) {
518 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
520 for (i = n; i < 24; i++)
521 av_log(NULL, AV_LOG_DEBUG, " ");
524 static inline int get_bits_trace(GetBitContext *s, int n, const char *file,
525 const char *func, int line)
527 int r = get_bits(s, n);
530 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
531 r, n, r, get_bits_count(s)-n, file, func, line);
534 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
535 int bits, int max_depth, const char *file,
536 const char *func, int line)
538 int show = show_bits(s, 24);
539 int pos = get_bits_count(s);
540 int r = get_vlc2(s, table, bits, max_depth);
541 int len = get_bits_count(s) - pos;
542 int bits2 = show >> (24-len);
544 print_bin(bits2, len);
546 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
547 bits2, len, r, pos, file, func, line);
550 static inline int get_xbits_trace(GetBitContext *s, int n, const char *file,
551 const char *func, int line)
553 int show = show_bits(s, n);
554 int r = get_xbits(s, n);
557 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
558 show, n, r, get_bits_count(s)-n, file, func, line);
562 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
563 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
564 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
565 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
566 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
568 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
571 #define tprintf(p, ...) {}
574 #endif /* AVCODEC_GET_BITS_H */