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
32 #include "libavutil/bswap.h"
33 #include "libavutil/common.h"
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/log.h"
39 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
40 typedef struct GetBitContext {
41 const uint8_t *buffer, *buffer_end;
46 #define VLC_TYPE int16_t
50 VLC_TYPE (*table)[2]; ///< code, bits
51 int table_size, table_allocated;
54 typedef struct RL_VLC_ELEM {
60 /* Bitstream reader API docs:
62 arbitrary name which is used as prefix for the internal variables
68 load gb into local variables
70 CLOSE_READER(name, gb)
71 store local vars in gb
73 UPDATE_CACHE(name, gb)
74 refill the internal cache from the bitstream
75 after this call at least MIN_CACHE_BITS will be available,
78 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
80 SHOW_UBITS(name, gb, num)
81 will return the next num bits
83 SHOW_SBITS(name, gb, num)
84 will return the next num bits and do sign extension
86 SKIP_BITS(name, gb, num)
87 will skip over the next num bits
88 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
90 SKIP_CACHE(name, gb, num)
91 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
93 SKIP_COUNTER(name, gb, num)
94 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
96 LAST_SKIP_CACHE(name, gb, num)
97 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
99 LAST_SKIP_BITS(name, gb, num)
100 is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER
102 for examples see get_bits, show_bits, skip_bits, get_vlc
105 #ifdef LONG_BITSTREAM_READER
106 # define MIN_CACHE_BITS 32
108 # define MIN_CACHE_BITS 25
111 # define OPEN_READER(name, gb) \
112 unsigned int name##_index = (gb)->index; \
113 unsigned int av_unused name##_cache = 0
115 # define CLOSE_READER(name, gb) (gb)->index = name##_index
117 # ifdef ALT_BITSTREAM_READER_LE
118 # ifdef LONG_BITSTREAM_READER
119 # define UPDATE_CACHE(name, gb) \
120 name##_cache = AV_RL64((gb)->buffer+(name##_index>>3)) >> (name##_index&0x07)
122 # define UPDATE_CACHE(name, gb) \
123 name##_cache = AV_RL32((gb)->buffer+(name##_index>>3)) >> (name##_index&0x07)
126 # define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
128 # ifdef LONG_BITSTREAM_READER
129 # define UPDATE_CACHE(name, gb) \
130 name##_cache = AV_RB64((gb)->buffer+(name##_index >> 3)) >> (32 - (name##_index & 0x07))
132 # define UPDATE_CACHE(name, gb) \
133 name##_cache = AV_RB32((gb)->buffer+(name##_index>>3)) << (name##_index&0x07)
136 # define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
140 # define SKIP_COUNTER(name, gb, num) name##_index += (num)
142 # define SKIP_BITS(name, gb, num) do { \
143 SKIP_CACHE(name, gb, num); \
144 SKIP_COUNTER(name, gb, num); \
147 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
148 # define LAST_SKIP_CACHE(name, gb, num)
150 # ifdef ALT_BITSTREAM_READER_LE
151 # define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
153 # define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
155 # define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
157 # define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
160 # define GET_CACHE(name, gb) ((uint32_t)name##_cache)
162 static inline int get_bits_count(const GetBitContext *s){
166 static inline void skip_bits_long(GetBitContext *s, int n){
171 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
172 * if MSB not set it is negative
173 * @param n length in bits
176 static inline int get_xbits(GetBitContext *s, int n){
178 register int32_t cache;
181 cache = GET_CACHE(re, s);
183 LAST_SKIP_BITS(re, s, n);
185 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
188 static inline int get_sbits(GetBitContext *s, int n){
192 tmp = SHOW_SBITS(re, s, n);
193 LAST_SKIP_BITS(re, s, n);
201 static inline unsigned int get_bits(GetBitContext *s, int n){
205 tmp = SHOW_UBITS(re, s, n);
206 LAST_SKIP_BITS(re, s, n);
214 static inline unsigned int show_bits(GetBitContext *s, int n){
218 tmp = SHOW_UBITS(re, s, n);
222 static inline void skip_bits(GetBitContext *s, int n){
223 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
226 LAST_SKIP_BITS(re, s, n);
230 static inline unsigned int get_bits1(GetBitContext *s){
231 unsigned int index = s->index;
232 uint8_t result = s->buffer[index>>3];
233 #ifdef ALT_BITSTREAM_READER_LE
234 result >>= index & 7;
237 result <<= index & 7;
246 static inline unsigned int show_bits1(GetBitContext *s){
247 return show_bits(s, 1);
250 static inline void skip_bits1(GetBitContext *s){
257 static inline unsigned int get_bits_long(GetBitContext *s, int n){
258 if (n <= MIN_CACHE_BITS) return get_bits(s, n);
260 #ifdef ALT_BITSTREAM_READER_LE
261 int ret = get_bits(s, 16);
262 return ret | (get_bits(s, n-16) << 16);
264 int ret = get_bits(s, 16) << (n-16);
265 return ret | get_bits(s, n-16);
271 * Read 0-32 bits as a signed integer.
273 static inline int get_sbits_long(GetBitContext *s, int n) {
274 return sign_extend(get_bits_long(s, n), n);
280 static inline unsigned int show_bits_long(GetBitContext *s, int n){
281 if (n <= MIN_CACHE_BITS) return show_bits(s, n);
283 GetBitContext gb = *s;
284 return get_bits_long(&gb, n);
288 static inline int check_marker(GetBitContext *s, const char *msg)
290 int bit = get_bits1(s);
292 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
298 * Inititalize GetBitContext.
299 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger than the actual read bits
300 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
301 * @param bit_size the size of the buffer in bits
303 * While GetBitContext stores the buffer size, for performance reasons you are
304 * responsible for checking for the buffer end yourself (take advantage of the padding)!
306 static inline void init_get_bits(GetBitContext *s,
307 const uint8_t *buffer, int bit_size)
309 int buffer_size = (bit_size+7)>>3;
310 if (buffer_size < 0 || bit_size < 0) {
311 buffer_size = bit_size = 0;
316 s->size_in_bits = bit_size;
317 s->buffer_end = buffer + buffer_size;
321 static inline void align_get_bits(GetBitContext *s)
323 int n = -get_bits_count(s) & 7;
324 if (n) skip_bits(s, n);
327 #define init_vlc(vlc, nb_bits, nb_codes, \
328 bits, bits_wrap, bits_size, \
329 codes, codes_wrap, codes_size, \
331 init_vlc_sparse(vlc, nb_bits, nb_codes, \
332 bits, bits_wrap, bits_size, \
333 codes, codes_wrap, codes_size, \
336 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
337 const void *bits, int bits_wrap, int bits_size,
338 const void *codes, int codes_wrap, int codes_size,
339 const void *symbols, int symbols_wrap, int symbols_size,
341 #define INIT_VLC_LE 2
342 #define INIT_VLC_USE_NEW_STATIC 4
343 void free_vlc(VLC *vlc);
345 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do { \
346 static VLC_TYPE table[static_size][2]; \
347 (vlc)->table = table; \
348 (vlc)->table_allocated = static_size; \
349 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC); \
354 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
355 * If the vlc code is invalid and max_depth>1, then the number of bits removed
358 #define GET_VLC(code, name, gb, table, bits, max_depth) do { \
360 unsigned int index; \
362 index = SHOW_UBITS(name, gb, bits); \
363 code = table[index][0]; \
364 n = table[index][1]; \
366 if (max_depth > 1 && n < 0) { \
367 LAST_SKIP_BITS(name, gb, bits); \
368 UPDATE_CACHE(name, gb); \
372 index = SHOW_UBITS(name, gb, nb_bits) + code; \
373 code = table[index][0]; \
374 n = table[index][1]; \
375 if (max_depth > 2 && n < 0) { \
376 LAST_SKIP_BITS(name, gb, nb_bits); \
377 UPDATE_CACHE(name, gb); \
381 index = SHOW_UBITS(name, gb, nb_bits) + code; \
382 code = table[index][0]; \
383 n = table[index][1]; \
386 SKIP_BITS(name, gb, n); \
389 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) do { \
391 unsigned int index; \
393 index = SHOW_UBITS(name, gb, bits); \
394 level = table[index].level; \
395 n = table[index].len; \
397 if (max_depth > 1 && n < 0) { \
398 SKIP_BITS(name, gb, bits); \
400 UPDATE_CACHE(name, gb); \
405 index = SHOW_UBITS(name, gb, nb_bits) + level; \
406 level = table[index].level; \
407 n = table[index].len; \
409 run = table[index].run; \
410 SKIP_BITS(name, gb, n); \
415 * Parse a vlc code, faster than get_vlc().
416 * @param bits is the number of bits which will be read at once, must be
417 * identical to nb_bits in init_vlc()
418 * @param max_depth is the number of times bits bits must be read to completely
419 * read the longest vlc code
420 * = (max_vlc_length + bits - 1) / bits
422 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
423 int bits, int max_depth)
430 GET_VLC(code, re, s, table, bits, max_depth);
436 static inline int decode012(GetBitContext *gb){
442 return get_bits1(gb) + 1;
445 static inline int decode210(GetBitContext *gb){
449 return 2 - get_bits1(gb);
452 static inline int get_bits_left(GetBitContext *gb)
454 return gb->size_in_bits - get_bits_count(gb);
460 static inline void print_bin(int bits, int n){
463 for (i = n-1; i >= 0; i--) {
464 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
466 for (i = n; i < 24; i++)
467 av_log(NULL, AV_LOG_DEBUG, " ");
470 static inline int get_bits_trace(GetBitContext *s, int n, char *file,
471 const char *func, int line){
472 int r = get_bits(s, n);
475 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
476 r, n, r, get_bits_count(s)-n, file, func, line);
479 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
480 int bits, int max_depth, char *file,
481 const char *func, int line){
482 int show = show_bits(s, 24);
483 int pos = get_bits_count(s);
484 int r = get_vlc2(s, table, bits, max_depth);
485 int len = get_bits_count(s) - pos;
486 int bits2 = show >> (24-len);
488 print_bin(bits2, len);
490 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
491 bits2, len, r, pos, file, func, line);
494 static inline int get_xbits_trace(GetBitContext *s, int n, char *file,
495 const char *func, int line){
496 int show = show_bits(s, n);
497 int r = get_xbits(s, n);
500 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
501 show, n, r, get_bits_count(s)-n, file, func, line);
505 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
506 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
507 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
508 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
509 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
511 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
514 #define tprintf(p, ...) {}
517 #endif /* AVCODEC_GET_BITS_H */