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 #define OPEN_READER(name, gb) \
122 unsigned int name##_index = (gb)->index; \
123 av_unused unsigned int name##_cache
125 #define CLOSE_READER(name, gb) (gb)->index = name##_index
127 #ifdef BITSTREAM_READER_LE
129 # ifdef LONG_BITSTREAM_READER
130 # define UPDATE_CACHE(name, gb) name##_cache = \
131 AV_RL64((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
133 # define UPDATE_CACHE(name, gb) name##_cache = \
134 AV_RL32((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
137 # define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
141 # ifdef LONG_BITSTREAM_READER
142 # define UPDATE_CACHE(name, gb) name##_cache = \
143 AV_RB64((gb)->buffer + (name##_index >> 3)) >> (32 - (name##_index & 7))
145 # define UPDATE_CACHE(name, gb) name##_cache = \
146 AV_RB32((gb)->buffer + (name##_index >> 3)) << (name##_index & 7)
149 # define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
153 #if UNCHECKED_BITSTREAM_READER
154 # define SKIP_COUNTER(name, gb, num) name##_index += (num)
156 # define SKIP_COUNTER(name, gb, num) \
157 name##_index = FFMIN((gb)->size_in_bits_plus8, name##_index + (num))
160 #define SKIP_BITS(name, gb, num) do { \
161 SKIP_CACHE(name, gb, num); \
162 SKIP_COUNTER(name, gb, num); \
165 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
167 #ifdef BITSTREAM_READER_LE
168 # define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
169 # define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
171 # define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
172 # define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
175 #define GET_CACHE(name, gb) ((uint32_t)name##_cache)
177 static inline int get_bits_count(const GetBitContext *s)
182 static inline void skip_bits_long(GetBitContext *s, int n){
183 #if UNCHECKED_BITSTREAM_READER
186 s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
191 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
192 * if MSB not set it is negative
193 * @param n length in bits
195 static inline int get_xbits(GetBitContext *s, int n)
198 register int32_t cache;
201 cache = GET_CACHE(re, s);
203 LAST_SKIP_BITS(re, s, n);
205 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
208 static inline int get_sbits(GetBitContext *s, int n)
213 tmp = SHOW_SBITS(re, s, n);
214 LAST_SKIP_BITS(re, s, n);
222 static inline unsigned int get_bits(GetBitContext *s, int n)
227 tmp = SHOW_UBITS(re, s, n);
228 LAST_SKIP_BITS(re, s, n);
236 static inline unsigned int show_bits(GetBitContext *s, int n)
241 tmp = SHOW_UBITS(re, s, n);
245 static inline void skip_bits(GetBitContext *s, int n)
249 LAST_SKIP_BITS(re, s, n);
253 static inline unsigned int get_bits1(GetBitContext *s)
255 unsigned int index = s->index;
256 uint8_t result = s->buffer[index>>3];
257 #ifdef BITSTREAM_READER_LE
258 result >>= index & 7;
261 result <<= index & 7;
264 #if !UNCHECKED_BITSTREAM_READER
265 if (s->index < s->size_in_bits_plus8)
273 static inline unsigned int show_bits1(GetBitContext *s)
275 return show_bits(s, 1);
278 static inline void skip_bits1(GetBitContext *s)
286 static inline unsigned int get_bits_long(GetBitContext *s, int n)
288 if (n <= MIN_CACHE_BITS)
289 return get_bits(s, n);
291 #ifdef BITSTREAM_READER_LE
292 int ret = get_bits(s, 16);
293 return ret | (get_bits(s, n-16) << 16);
295 int ret = get_bits(s, 16) << (n-16);
296 return ret | get_bits(s, n-16);
302 * Read 0-32 bits as a signed integer.
304 static inline int get_sbits_long(GetBitContext *s, int n)
306 return sign_extend(get_bits_long(s, n), n);
312 static inline unsigned int show_bits_long(GetBitContext *s, int n)
314 if (n <= MIN_CACHE_BITS)
315 return show_bits(s, n);
317 GetBitContext gb = *s;
318 return get_bits_long(&gb, n);
322 static inline int check_marker(GetBitContext *s, const char *msg)
324 int bit = get_bits1(s);
326 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
332 * Inititalize GetBitContext.
333 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger than the actual read bits
334 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
335 * @param bit_size the size of the buffer in bits
337 static inline void init_get_bits(GetBitContext *s, const uint8_t *buffer,
340 int buffer_size = (bit_size+7)>>3;
341 if (buffer_size < 0 || bit_size < 0) {
342 buffer_size = bit_size = 0;
347 s->size_in_bits = bit_size;
348 s->size_in_bits_plus8 = bit_size + 8;
349 s->buffer_end = buffer + buffer_size;
353 static inline void align_get_bits(GetBitContext *s)
355 int n = -get_bits_count(s) & 7;
356 if (n) skip_bits(s, n);
359 #define init_vlc(vlc, nb_bits, nb_codes, \
360 bits, bits_wrap, bits_size, \
361 codes, codes_wrap, codes_size, \
363 init_vlc_sparse(vlc, nb_bits, nb_codes, \
364 bits, bits_wrap, bits_size, \
365 codes, codes_wrap, codes_size, \
368 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
369 const void *bits, int bits_wrap, int bits_size,
370 const void *codes, int codes_wrap, int codes_size,
371 const void *symbols, int symbols_wrap, int symbols_size,
373 #define INIT_VLC_LE 2
374 #define INIT_VLC_USE_NEW_STATIC 4
375 void free_vlc(VLC *vlc);
377 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do { \
378 static VLC_TYPE table[static_size][2]; \
379 (vlc)->table = table; \
380 (vlc)->table_allocated = static_size; \
381 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC); \
386 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
387 * If the vlc code is invalid and max_depth>1, then the number of bits removed
390 #define GET_VLC(code, name, gb, table, bits, max_depth) \
393 unsigned int index; \
395 index = SHOW_UBITS(name, gb, bits); \
396 code = table[index][0]; \
397 n = table[index][1]; \
399 if (max_depth > 1 && n < 0) { \
400 LAST_SKIP_BITS(name, gb, bits); \
401 UPDATE_CACHE(name, gb); \
405 index = SHOW_UBITS(name, gb, nb_bits) + code; \
406 code = table[index][0]; \
407 n = table[index][1]; \
408 if (max_depth > 2 && n < 0) { \
409 LAST_SKIP_BITS(name, gb, nb_bits); \
410 UPDATE_CACHE(name, gb); \
414 index = SHOW_UBITS(name, gb, nb_bits) + code; \
415 code = table[index][0]; \
416 n = table[index][1]; \
419 SKIP_BITS(name, gb, n); \
422 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) \
425 unsigned int index; \
427 index = SHOW_UBITS(name, gb, bits); \
428 level = table[index].level; \
429 n = table[index].len; \
431 if (max_depth > 1 && n < 0) { \
432 SKIP_BITS(name, gb, bits); \
434 UPDATE_CACHE(name, gb); \
439 index = SHOW_UBITS(name, gb, nb_bits) + level; \
440 level = table[index].level; \
441 n = table[index].len; \
443 run = table[index].run; \
444 SKIP_BITS(name, gb, n); \
450 * @param bits is the number of bits which will be read at once, must be
451 * identical to nb_bits in init_vlc()
452 * @param max_depth is the number of times bits bits must be read to completely
453 * read the longest vlc code
454 * = (max_vlc_length + bits - 1) / bits
456 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
457 int bits, int max_depth)
464 GET_VLC(code, re, s, table, bits, max_depth);
470 static inline int decode012(GetBitContext *gb)
477 return get_bits1(gb) + 1;
480 static inline int decode210(GetBitContext *gb)
485 return 2 - get_bits1(gb);
488 static inline int get_bits_left(GetBitContext *gb)
490 return gb->size_in_bits - get_bits_count(gb);
496 static inline void print_bin(int bits, int n)
500 for (i = n-1; i >= 0; i--) {
501 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
503 for (i = n; i < 24; i++)
504 av_log(NULL, AV_LOG_DEBUG, " ");
507 static inline int get_bits_trace(GetBitContext *s, int n, char *file,
508 const char *func, int line)
510 int r = get_bits(s, n);
513 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
514 r, n, r, get_bits_count(s)-n, file, func, line);
517 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
518 int bits, int max_depth, char *file,
519 const char *func, int line)
521 int show = show_bits(s, 24);
522 int pos = get_bits_count(s);
523 int r = get_vlc2(s, table, bits, max_depth);
524 int len = get_bits_count(s) - pos;
525 int bits2 = show >> (24-len);
527 print_bin(bits2, len);
529 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
530 bits2, len, r, pos, file, func, line);
533 static inline int get_xbits_trace(GetBitContext *s, int n, char *file,
534 const char *func, int line)
536 int show = show_bits(s, n);
537 int r = get_xbits(s, n);
540 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
541 show, n, r, get_bits_count(s)-n, file, func, line);
545 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
546 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
547 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
548 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
549 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
551 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
554 #define tprintf(p, ...) {}
557 #endif /* AVCODEC_GET_BITS_H */