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 #define OPEN_READER(name, gb) \
124 unsigned int name##_index = (gb)->index; \
125 unsigned int av_unused name##_cache = 0
127 #define CLOSE_READER(name, gb) (gb)->index = name##_index
129 #ifdef BITSTREAM_READER_LE
131 # ifdef LONG_BITSTREAM_READER
132 # define UPDATE_CACHE(name, gb) name##_cache = \
133 AV_RL64((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
135 # define UPDATE_CACHE(name, gb) name##_cache = \
136 AV_RL32((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
139 # define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
143 # ifdef LONG_BITSTREAM_READER
144 # define UPDATE_CACHE(name, gb) name##_cache = \
145 AV_RB64((gb)->buffer + (name##_index >> 3)) >> (32 - (name##_index & 7))
147 # define UPDATE_CACHE(name, gb) name##_cache = \
148 AV_RB32((gb)->buffer + (name##_index >> 3)) << (name##_index & 7)
151 # define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
155 #if UNCHECKED_BITSTREAM_READER
156 # define SKIP_COUNTER(name, gb, num) name##_index += (num)
158 # define SKIP_COUNTER(name, gb, num) \
159 name##_index = FFMIN((gb)->size_in_bits_plus8, name##_index + (num))
162 #define SKIP_BITS(name, gb, num) do { \
163 SKIP_CACHE(name, gb, num); \
164 SKIP_COUNTER(name, gb, num); \
167 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
169 #ifdef BITSTREAM_READER_LE
170 # define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
171 # define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
173 # define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
174 # define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
177 #define GET_CACHE(name, gb) ((uint32_t)name##_cache)
179 static inline int get_bits_count(const GetBitContext *s)
184 static inline void skip_bits_long(GetBitContext *s, int n){
185 #if UNCHECKED_BITSTREAM_READER
188 s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
193 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
194 * if MSB not set it is negative
195 * @param n length in bits
197 static inline int get_xbits(GetBitContext *s, int n)
200 register int32_t cache;
203 cache = GET_CACHE(re, s);
205 LAST_SKIP_BITS(re, s, n);
207 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
210 static inline int get_sbits(GetBitContext *s, int n)
215 tmp = SHOW_SBITS(re, s, n);
216 LAST_SKIP_BITS(re, s, n);
224 static inline unsigned int get_bits(GetBitContext *s, int n)
229 tmp = SHOW_UBITS(re, s, n);
230 LAST_SKIP_BITS(re, s, n);
238 static inline unsigned int show_bits(GetBitContext *s, int n)
243 tmp = SHOW_UBITS(re, s, n);
247 static inline void skip_bits(GetBitContext *s, int n)
251 LAST_SKIP_BITS(re, s, n);
255 static inline unsigned int get_bits1(GetBitContext *s)
257 unsigned int index = s->index;
258 uint8_t result = s->buffer[index>>3];
259 #ifdef BITSTREAM_READER_LE
260 result >>= index & 7;
263 result <<= index & 7;
266 #if !UNCHECKED_BITSTREAM_READER
267 if (s->index < s->size_in_bits_plus8)
275 static inline unsigned int show_bits1(GetBitContext *s)
277 return show_bits(s, 1);
280 static inline void skip_bits1(GetBitContext *s)
288 static inline unsigned int get_bits_long(GetBitContext *s, int n)
290 if (n <= MIN_CACHE_BITS)
291 return get_bits(s, n);
293 #ifdef BITSTREAM_READER_LE
294 int ret = get_bits(s, 16);
295 return ret | (get_bits(s, n-16) << 16);
297 int ret = get_bits(s, 16) << (n-16);
298 return ret | get_bits(s, n-16);
304 * Read 0-32 bits as a signed integer.
306 static inline int get_sbits_long(GetBitContext *s, int n)
308 return sign_extend(get_bits_long(s, n), n);
314 static inline unsigned int show_bits_long(GetBitContext *s, int n)
316 if (n <= MIN_CACHE_BITS)
317 return show_bits(s, n);
319 GetBitContext gb = *s;
320 return get_bits_long(&gb, n);
324 static inline int check_marker(GetBitContext *s, const char *msg)
326 int bit = get_bits1(s);
328 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
334 * Inititalize GetBitContext.
335 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger than the actual read bits
336 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
337 * @param bit_size the size of the buffer in bits
339 static inline void init_get_bits(GetBitContext *s, const uint8_t *buffer,
342 int buffer_size = (bit_size+7)>>3;
343 if (buffer_size < 0 || bit_size < 0) {
344 buffer_size = bit_size = 0;
349 s->size_in_bits = bit_size;
350 #if !UNCHECKED_BITSTREAM_READER
351 s->size_in_bits_plus8 = bit_size + 8;
353 s->buffer_end = buffer + buffer_size;
357 static inline void align_get_bits(GetBitContext *s)
359 int n = -get_bits_count(s) & 7;
360 if (n) skip_bits(s, n);
363 #define init_vlc(vlc, nb_bits, nb_codes, \
364 bits, bits_wrap, bits_size, \
365 codes, codes_wrap, codes_size, \
367 init_vlc_sparse(vlc, nb_bits, nb_codes, \
368 bits, bits_wrap, bits_size, \
369 codes, codes_wrap, codes_size, \
372 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
373 const void *bits, int bits_wrap, int bits_size,
374 const void *codes, int codes_wrap, int codes_size,
375 const void *symbols, int symbols_wrap, int symbols_size,
377 #define INIT_VLC_LE 2
378 #define INIT_VLC_USE_NEW_STATIC 4
379 void free_vlc(VLC *vlc);
381 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do { \
382 static VLC_TYPE table[static_size][2]; \
383 (vlc)->table = table; \
384 (vlc)->table_allocated = static_size; \
385 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC); \
390 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
391 * If the vlc code is invalid and max_depth>1, then the number of bits removed
394 #define GET_VLC(code, name, gb, table, bits, max_depth) \
397 unsigned int index; \
399 index = SHOW_UBITS(name, gb, bits); \
400 code = table[index][0]; \
401 n = table[index][1]; \
403 if (max_depth > 1 && n < 0) { \
404 LAST_SKIP_BITS(name, gb, bits); \
405 UPDATE_CACHE(name, gb); \
409 index = SHOW_UBITS(name, gb, nb_bits) + code; \
410 code = table[index][0]; \
411 n = table[index][1]; \
412 if (max_depth > 2 && n < 0) { \
413 LAST_SKIP_BITS(name, gb, nb_bits); \
414 UPDATE_CACHE(name, gb); \
418 index = SHOW_UBITS(name, gb, nb_bits) + code; \
419 code = table[index][0]; \
420 n = table[index][1]; \
423 SKIP_BITS(name, gb, n); \
426 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) \
429 unsigned int index; \
431 index = SHOW_UBITS(name, gb, bits); \
432 level = table[index].level; \
433 n = table[index].len; \
435 if (max_depth > 1 && n < 0) { \
436 SKIP_BITS(name, gb, bits); \
438 UPDATE_CACHE(name, gb); \
443 index = SHOW_UBITS(name, gb, nb_bits) + level; \
444 level = table[index].level; \
445 n = table[index].len; \
447 run = table[index].run; \
448 SKIP_BITS(name, gb, n); \
454 * @param bits is the number of bits which will be read at once, must be
455 * identical to nb_bits in init_vlc()
456 * @param max_depth is the number of times bits bits must be read to completely
457 * read the longest vlc code
458 * = (max_vlc_length + bits - 1) / bits
460 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
461 int bits, int max_depth)
468 GET_VLC(code, re, s, table, bits, max_depth);
474 static inline int decode012(GetBitContext *gb)
481 return get_bits1(gb) + 1;
484 static inline int decode210(GetBitContext *gb)
489 return 2 - get_bits1(gb);
492 static inline int get_bits_left(GetBitContext *gb)
494 return gb->size_in_bits - get_bits_count(gb);
500 static inline void print_bin(int bits, int n)
504 for (i = n-1; i >= 0; i--) {
505 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
507 for (i = n; i < 24; i++)
508 av_log(NULL, AV_LOG_DEBUG, " ");
511 static inline int get_bits_trace(GetBitContext *s, int n, char *file,
512 const char *func, int line)
514 int r = get_bits(s, n);
517 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
518 r, n, r, get_bits_count(s)-n, file, func, line);
521 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
522 int bits, int max_depth, char *file,
523 const char *func, int line)
525 int show = show_bits(s, 24);
526 int pos = get_bits_count(s);
527 int r = get_vlc2(s, table, bits, max_depth);
528 int len = get_bits_count(s) - pos;
529 int bits2 = show >> (24-len);
531 print_bin(bits2, len);
533 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
534 bits2, len, r, pos, file, func, line);
537 static inline int get_xbits_trace(GetBitContext *s, int n, char *file,
538 const char *func, int line)
540 int show = show_bits(s, n);
541 int r = get_xbits(s, n);
544 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
545 show, n, r, get_bits_count(s)-n, file, func, line);
549 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
550 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
551 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
552 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
553 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
555 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
558 #define tprintf(p, ...) {}
561 #endif /* AVCODEC_GET_BITS_H */