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
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 int size_in_bits_plus8;
59 #define VLC_TYPE int16_t
63 VLC_TYPE (*table)[2]; ///< code, bits
64 int table_size, table_allocated;
67 typedef struct RL_VLC_ELEM {
73 /* Bitstream reader API docs:
75 arbitrary name which is used as prefix for the internal variables
81 load gb into local variables
83 CLOSE_READER(name, gb)
84 store local vars in gb
86 UPDATE_CACHE(name, gb)
87 refill the internal cache from the bitstream
88 after this call at least MIN_CACHE_BITS will be available,
91 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
93 SHOW_UBITS(name, gb, num)
94 will return the next num bits
96 SHOW_SBITS(name, gb, num)
97 will return the next num bits and do sign extension
99 SKIP_BITS(name, gb, num)
100 will skip over the next num bits
101 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
103 SKIP_CACHE(name, gb, num)
104 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
106 SKIP_COUNTER(name, gb, num)
107 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
109 LAST_SKIP_BITS(name, gb, num)
110 like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER
112 for examples see get_bits, show_bits, skip_bits, get_vlc
115 #ifdef LONG_BITSTREAM_READER
116 # define MIN_CACHE_BITS 32
118 # define MIN_CACHE_BITS 25
121 #if UNCHECKED_BITSTREAM_READER
122 #define OPEN_READER(name, gb) \
123 unsigned int name##_index = (gb)->index; \
124 av_unused unsigned int name##_cache
126 #define HAVE_BITS_REMAINING(name, gb) 1
128 #define OPEN_READER(name, gb) \
129 unsigned int name##_index = (gb)->index; \
130 unsigned int av_unused name##_cache = 0; \
131 unsigned int av_unused name##_size_plus8 = \
132 (gb)->size_in_bits_plus8
134 #define HAVE_BITS_REMAINING(name, gb) \
135 name##_index < name##_size_plus8
138 #define CLOSE_READER(name, gb) (gb)->index = name##_index
140 #ifdef BITSTREAM_READER_LE
142 # ifdef LONG_BITSTREAM_READER
143 # define UPDATE_CACHE(name, gb) name##_cache = \
144 AV_RL64((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
146 # define UPDATE_CACHE(name, gb) name##_cache = \
147 AV_RL32((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
150 # define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
154 # ifdef LONG_BITSTREAM_READER
155 # define UPDATE_CACHE(name, gb) name##_cache = \
156 AV_RB64((gb)->buffer + (name##_index >> 3)) >> (32 - (name##_index & 7))
158 # define UPDATE_CACHE(name, gb) name##_cache = \
159 AV_RB32((gb)->buffer + (name##_index >> 3)) << (name##_index & 7)
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 SKIP_BITS(name, gb, num) do { \
174 SKIP_CACHE(name, gb, num); \
175 SKIP_COUNTER(name, gb, num); \
178 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
180 #ifdef BITSTREAM_READER_LE
181 # define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
182 # define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
184 # define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
185 # define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
188 #define GET_CACHE(name, gb) ((uint32_t)name##_cache)
190 static inline int get_bits_count(const GetBitContext *s)
195 static inline void skip_bits_long(GetBitContext *s, int n){
196 #if UNCHECKED_BITSTREAM_READER
199 s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
204 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
205 * if MSB not set it is negative
206 * @param n length in bits
208 static inline int get_xbits(GetBitContext *s, int n)
211 register int32_t cache;
214 cache = GET_CACHE(re, s);
216 LAST_SKIP_BITS(re, s, n);
218 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
221 static inline int get_sbits(GetBitContext *s, int n)
226 tmp = SHOW_SBITS(re, s, n);
227 LAST_SKIP_BITS(re, s, n);
235 static inline unsigned int get_bits(GetBitContext *s, int n)
240 tmp = SHOW_UBITS(re, s, n);
241 LAST_SKIP_BITS(re, s, n);
249 static inline unsigned int show_bits(GetBitContext *s, int n)
254 tmp = SHOW_UBITS(re, s, n);
258 static inline void skip_bits(GetBitContext *s, int n)
262 LAST_SKIP_BITS(re, s, n);
266 static inline unsigned int get_bits1(GetBitContext *s)
268 unsigned int index = s->index;
269 uint8_t result = s->buffer[index>>3];
270 #ifdef BITSTREAM_READER_LE
271 result >>= index & 7;
274 result <<= index & 7;
277 #if !UNCHECKED_BITSTREAM_READER
278 if (s->index < s->size_in_bits_plus8)
286 static inline unsigned int show_bits1(GetBitContext *s)
288 return show_bits(s, 1);
291 static inline void skip_bits1(GetBitContext *s)
299 static inline unsigned int get_bits_long(GetBitContext *s, int n)
301 if (n <= MIN_CACHE_BITS)
302 return get_bits(s, n);
304 #ifdef BITSTREAM_READER_LE
305 int ret = get_bits(s, 16);
306 return ret | (get_bits(s, n-16) << 16);
308 int ret = get_bits(s, 16) << (n-16);
309 return ret | get_bits(s, n-16);
315 * Read 0-32 bits as a signed integer.
317 static inline int get_sbits_long(GetBitContext *s, int n)
319 return sign_extend(get_bits_long(s, n), n);
325 static inline unsigned int show_bits_long(GetBitContext *s, int n)
327 if (n <= MIN_CACHE_BITS)
328 return show_bits(s, n);
330 GetBitContext gb = *s;
331 return get_bits_long(&gb, n);
335 static inline int check_marker(GetBitContext *s, const char *msg)
337 int bit = get_bits1(s);
339 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
345 * Inititalize GetBitContext.
346 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger than the actual read bits
347 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
348 * @param bit_size the size of the buffer in bits
350 static inline void init_get_bits(GetBitContext *s, const uint8_t *buffer,
353 int buffer_size = (bit_size+7)>>3;
354 if (buffer_size < 0 || bit_size < 0) {
355 buffer_size = bit_size = 0;
360 s->size_in_bits = bit_size;
361 s->size_in_bits_plus8 = bit_size + 8;
362 s->buffer_end = buffer + buffer_size;
366 static inline void align_get_bits(GetBitContext *s)
368 int n = -get_bits_count(s) & 7;
369 if (n) skip_bits(s, n);
372 #define init_vlc(vlc, nb_bits, nb_codes, \
373 bits, bits_wrap, bits_size, \
374 codes, codes_wrap, codes_size, \
376 ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
377 bits, bits_wrap, bits_size, \
378 codes, codes_wrap, codes_size, \
381 int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
382 const void *bits, int bits_wrap, int bits_size,
383 const void *codes, int codes_wrap, int codes_size,
384 const void *symbols, int symbols_wrap, int symbols_size,
386 #define INIT_VLC_LE 2
387 #define INIT_VLC_USE_NEW_STATIC 4
388 void ff_free_vlc(VLC *vlc);
390 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do { \
391 static VLC_TYPE table[static_size][2]; \
392 (vlc)->table = table; \
393 (vlc)->table_allocated = static_size; \
394 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC); \
399 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
400 * If the vlc code is invalid and max_depth>1, then the number of bits removed
403 #define GET_VLC(code, name, gb, table, bits, max_depth) \
406 unsigned int index; \
408 index = SHOW_UBITS(name, gb, bits); \
409 code = table[index][0]; \
410 n = table[index][1]; \
412 if (max_depth > 1 && n < 0) { \
413 LAST_SKIP_BITS(name, gb, 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]; \
421 if (max_depth > 2 && n < 0) { \
422 LAST_SKIP_BITS(name, gb, nb_bits); \
423 UPDATE_CACHE(name, gb); \
427 index = SHOW_UBITS(name, gb, nb_bits) + code; \
428 code = table[index][0]; \
429 n = table[index][1]; \
432 SKIP_BITS(name, gb, n); \
435 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) \
438 unsigned int index; \
440 index = SHOW_UBITS(name, gb, bits); \
441 level = table[index].level; \
442 n = table[index].len; \
444 if (max_depth > 1 && n < 0) { \
445 SKIP_BITS(name, gb, bits); \
447 UPDATE_CACHE(name, gb); \
452 index = SHOW_UBITS(name, gb, nb_bits) + level; \
453 level = table[index].level; \
454 n = table[index].len; \
456 run = table[index].run; \
457 SKIP_BITS(name, gb, n); \
463 * @param bits is the number of bits which will be read at once, must be
464 * identical to nb_bits in init_vlc()
465 * @param max_depth is the number of times bits bits must be read to completely
466 * read the longest vlc code
467 * = (max_vlc_length + bits - 1) / bits
469 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
470 int bits, int max_depth)
477 GET_VLC(code, re, s, table, bits, max_depth);
483 static inline int decode012(GetBitContext *gb)
490 return get_bits1(gb) + 1;
493 static inline int decode210(GetBitContext *gb)
498 return 2 - get_bits1(gb);
501 static inline int get_bits_left(GetBitContext *gb)
503 return gb->size_in_bits - get_bits_count(gb);
509 static inline void print_bin(int bits, int n)
513 for (i = n-1; i >= 0; i--) {
514 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
516 for (i = n; i < 24; i++)
517 av_log(NULL, AV_LOG_DEBUG, " ");
520 static inline int get_bits_trace(GetBitContext *s, int n, char *file,
521 const char *func, int line)
523 int r = get_bits(s, n);
526 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
527 r, n, r, get_bits_count(s)-n, file, func, line);
530 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
531 int bits, int max_depth, char *file,
532 const char *func, int line)
534 int show = show_bits(s, 24);
535 int pos = get_bits_count(s);
536 int r = get_vlc2(s, table, bits, max_depth);
537 int len = get_bits_count(s) - pos;
538 int bits2 = show >> (24-len);
540 print_bin(bits2, len);
542 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
543 bits2, len, r, pos, file, func, line);
546 static inline int get_xbits_trace(GetBitContext *s, int n, char *file,
547 const char *func, int line)
549 int show = show_bits(s, n);
550 int r = get_xbits(s, n);
553 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
554 show, n, r, get_bits_count(s)-n, file, func, line);
558 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
559 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
560 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
561 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
562 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
564 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
567 #define tprintf(p, ...) {}
570 #endif /* AVCODEC_GET_BITS_H */