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
32 #include "libavutil/bswap.h"
33 #include "libavutil/common.h"
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/log.h"
38 #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
39 # define ALT_BITSTREAM_READER
42 #if !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
43 # if ARCH_ARM && !HAVE_FAST_UNALIGNED
44 # define A32_BITSTREAM_READER
46 # define ALT_BITSTREAM_READER
47 //#define A32_BITSTREAM_READER
52 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
53 typedef struct GetBitContext {
54 const uint8_t *buffer, *buffer_end;
55 #ifdef ALT_BITSTREAM_READER
57 #elif defined A32_BITSTREAM_READER
66 #define VLC_TYPE int16_t
70 VLC_TYPE (*table)[2]; ///< code, bits
71 int table_size, table_allocated;
74 typedef struct RL_VLC_ELEM {
80 /* Bitstream reader API docs:
82 arbitrary name which is used as prefix for the internal variables
88 loads gb into local variables
90 CLOSE_READER(name, gb)
91 stores local vars in gb
93 UPDATE_CACHE(name, gb)
94 refills the internal cache from the bitstream
95 after this call at least MIN_CACHE_BITS will be available,
98 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
100 SHOW_UBITS(name, gb, num)
101 will return the next num bits
103 SHOW_SBITS(name, gb, num)
104 will return the next num bits and do sign extension
106 SKIP_BITS(name, gb, num)
107 will skip over the next num bits
108 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
110 SKIP_CACHE(name, gb, num)
111 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
113 SKIP_COUNTER(name, gb, num)
114 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
116 LAST_SKIP_CACHE(name, gb, num)
117 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
119 LAST_SKIP_BITS(name, gb, num)
120 is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER
122 for examples see get_bits, show_bits, skip_bits, get_vlc
125 #ifdef ALT_BITSTREAM_READER
126 # define MIN_CACHE_BITS 25
128 # define OPEN_READER(name, gb)\
129 unsigned int name##_index= (gb)->index;\
130 int name##_cache= 0;\
132 # define CLOSE_READER(name, gb)\
133 (gb)->index= name##_index;\
135 # ifdef ALT_BITSTREAM_READER_LE
136 # define UPDATE_CACHE(name, gb)\
137 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
139 # define SKIP_CACHE(name, gb, num)\
140 name##_cache >>= (num);
142 # define UPDATE_CACHE(name, gb)\
143 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
145 # define SKIP_CACHE(name, gb, num)\
146 name##_cache <<= (num);
150 # define SKIP_COUNTER(name, gb, num)\
151 name##_index += (num);\
153 # define SKIP_BITS(name, gb, num)\
155 SKIP_CACHE(name, gb, num)\
156 SKIP_COUNTER(name, gb, num)\
159 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
160 # define LAST_SKIP_CACHE(name, gb, num) ;
162 # ifdef ALT_BITSTREAM_READER_LE
163 # define SHOW_UBITS(name, gb, num)\
164 zero_extend(name##_cache, num)
166 # define SHOW_SBITS(name, gb, num)\
167 sign_extend(name##_cache, num)
169 # define SHOW_UBITS(name, gb, num)\
170 NEG_USR32(name##_cache, num)
172 # define SHOW_SBITS(name, gb, num)\
173 NEG_SSR32(name##_cache, num)
176 # define GET_CACHE(name, gb)\
177 ((uint32_t)name##_cache)
179 static inline int get_bits_count(const GetBitContext *s){
183 static inline void skip_bits_long(GetBitContext *s, int n){
187 #elif defined A32_BITSTREAM_READER
189 # define MIN_CACHE_BITS 32
191 # define OPEN_READER(name, gb)\
192 int name##_bit_count=(gb)->bit_count;\
193 uint32_t name##_cache0= (gb)->cache0;\
194 uint32_t name##_cache1= (gb)->cache1;\
195 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
197 # define CLOSE_READER(name, gb)\
198 (gb)->bit_count= name##_bit_count;\
199 (gb)->cache0= name##_cache0;\
200 (gb)->cache1= name##_cache1;\
201 (gb)->buffer_ptr= name##_buffer_ptr;\
203 # define UPDATE_CACHE(name, gb)\
204 if(name##_bit_count > 0){\
205 const uint32_t next= av_be2ne32( *name##_buffer_ptr );\
206 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
207 name##_cache1 |= next<<name##_bit_count;\
208 name##_buffer_ptr++;\
209 name##_bit_count-= 32;\
213 # define SKIP_CACHE(name, gb, num)\
215 "shldl %2, %1, %0 \n\t"\
217 : "+r" (name##_cache0), "+r" (name##_cache1)\
218 : "Ic" ((uint8_t)(num))\
221 # define SKIP_CACHE(name, gb, num)\
222 name##_cache0 <<= (num);\
223 name##_cache0 |= NEG_USR32(name##_cache1,num);\
224 name##_cache1 <<= (num);
227 # define SKIP_COUNTER(name, gb, num)\
228 name##_bit_count += (num);\
230 # define SKIP_BITS(name, gb, num)\
232 SKIP_CACHE(name, gb, num)\
233 SKIP_COUNTER(name, gb, num)\
236 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
237 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
239 # define SHOW_UBITS(name, gb, num)\
240 NEG_USR32(name##_cache0, num)
242 # define SHOW_SBITS(name, gb, num)\
243 NEG_SSR32(name##_cache0, num)
245 # define GET_CACHE(name, gb)\
248 static inline int get_bits_count(const GetBitContext *s){
249 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
252 static inline void skip_bits_long(GetBitContext *s, int n){
255 re_buffer_ptr += re_bit_count>>5;
257 re_cache0 = av_be2ne32( re_buffer_ptr[-1] ) << re_bit_count;
266 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
267 * if MSB not set it is negative
268 * @param n length in bits
271 static inline int get_xbits(GetBitContext *s, int n){
273 register int32_t cache;
276 cache = GET_CACHE(re,s);
278 LAST_SKIP_BITS(re, s, n)
280 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
283 static inline int get_sbits(GetBitContext *s, int n){
287 tmp= SHOW_SBITS(re, s, n);
288 LAST_SKIP_BITS(re, s, n)
296 static inline unsigned int get_bits(GetBitContext *s, int n){
300 tmp= SHOW_UBITS(re, s, n);
301 LAST_SKIP_BITS(re, s, n)
309 static inline unsigned int show_bits(GetBitContext *s, int n){
313 tmp= SHOW_UBITS(re, s, n);
314 // CLOSE_READER(re, s)
318 static inline void skip_bits(GetBitContext *s, int n){
319 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
322 LAST_SKIP_BITS(re, s, n)
326 static inline unsigned int get_bits1(GetBitContext *s){
327 #ifdef ALT_BITSTREAM_READER
328 unsigned int index= s->index;
329 uint8_t result= s->buffer[ index>>3 ];
330 #ifdef ALT_BITSTREAM_READER_LE
331 result>>= (index&0x07);
334 result<<= (index&0x07);
342 return get_bits(s, 1);
346 static inline unsigned int show_bits1(GetBitContext *s){
347 return show_bits(s, 1);
350 static inline void skip_bits1(GetBitContext *s){
357 static inline unsigned int get_bits_long(GetBitContext *s, int n){
358 if(n<=MIN_CACHE_BITS) return get_bits(s, n);
360 #ifdef ALT_BITSTREAM_READER_LE
361 int ret= get_bits(s, 16);
362 return ret | (get_bits(s, n-16) << 16);
364 int ret= get_bits(s, 16) << (n-16);
365 return ret | get_bits(s, n-16);
371 * reads 0-32 bits as a signed integer.
373 static inline int get_sbits_long(GetBitContext *s, int n) {
374 return sign_extend(get_bits_long(s, n), n);
380 static inline unsigned int show_bits_long(GetBitContext *s, int n){
381 if(n<=MIN_CACHE_BITS) return show_bits(s, n);
383 GetBitContext gb= *s;
384 return get_bits_long(&gb, n);
388 static inline int check_marker(GetBitContext *s, const char *msg)
390 int bit= get_bits1(s);
392 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
398 * init GetBitContext.
399 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
400 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
401 * @param bit_size the size of the buffer in bits
403 * While GetBitContext stores the buffer size, for performance reasons you are
404 * responsible for checking for the buffer end yourself (take advantage of the padding)!
406 static inline void init_get_bits(GetBitContext *s,
407 const uint8_t *buffer, int bit_size)
409 int buffer_size= (bit_size+7)>>3;
410 if(buffer_size < 0 || bit_size < 0) {
411 buffer_size = bit_size = 0;
416 s->size_in_bits= bit_size;
417 s->buffer_end= buffer + buffer_size;
418 #ifdef ALT_BITSTREAM_READER
420 #elif defined A32_BITSTREAM_READER
421 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
422 s->bit_count = 32 + 8*((intptr_t)buffer&3);
423 skip_bits_long(s, 0);
427 static inline void align_get_bits(GetBitContext *s)
429 int n= (-get_bits_count(s)) & 7;
430 if(n) skip_bits(s, n);
433 #define init_vlc(vlc, nb_bits, nb_codes,\
434 bits, bits_wrap, bits_size,\
435 codes, codes_wrap, codes_size,\
437 init_vlc_sparse(vlc, nb_bits, nb_codes,\
438 bits, bits_wrap, bits_size,\
439 codes, codes_wrap, codes_size,\
442 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
443 const void *bits, int bits_wrap, int bits_size,
444 const void *codes, int codes_wrap, int codes_size,
445 const void *symbols, int symbols_wrap, int symbols_size,
447 #define INIT_VLC_LE 2
448 #define INIT_VLC_USE_NEW_STATIC 4
449 void free_vlc(VLC *vlc);
451 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
453 static VLC_TYPE table[static_size][2];\
454 (vlc)->table= table;\
455 (vlc)->table_allocated= static_size;\
456 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
462 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
463 * If the vlc code is invalid and max_depth>1, then the number of bits removed
466 #define GET_VLC(code, name, gb, table, bits, max_depth)\
471 index= SHOW_UBITS(name, gb, bits);\
472 code = table[index][0];\
473 n = table[index][1];\
475 if(max_depth > 1 && n < 0){\
476 LAST_SKIP_BITS(name, gb, bits)\
477 UPDATE_CACHE(name, gb)\
481 index= SHOW_UBITS(name, gb, nb_bits) + code;\
482 code = table[index][0];\
483 n = table[index][1];\
484 if(max_depth > 2 && n < 0){\
485 LAST_SKIP_BITS(name, gb, nb_bits)\
486 UPDATE_CACHE(name, gb)\
490 index= SHOW_UBITS(name, gb, nb_bits) + code;\
491 code = table[index][0];\
492 n = table[index][1];\
495 SKIP_BITS(name, gb, n)\
498 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
503 index= SHOW_UBITS(name, gb, bits);\
504 level = table[index].level;\
505 n = table[index].len;\
507 if(max_depth > 1 && n < 0){\
508 SKIP_BITS(name, gb, bits)\
510 UPDATE_CACHE(name, gb)\
515 index= SHOW_UBITS(name, gb, nb_bits) + level;\
516 level = table[index].level;\
517 n = table[index].len;\
519 run= table[index].run;\
520 SKIP_BITS(name, gb, n)\
525 * parses a vlc code, faster then get_vlc()
526 * @param bits is the number of bits which will be read at once, must be
527 * identical to nb_bits in init_vlc()
528 * @param max_depth is the number of times bits bits must be read to completely
529 * read the longest vlc code
530 * = (max_vlc_length + bits - 1) / bits
532 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
533 int bits, int max_depth)
540 GET_VLC(code, re, s, table, bits, max_depth)
549 static inline void print_bin(int bits, int n){
552 for(i=n-1; i>=0; i--){
553 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
556 av_log(NULL, AV_LOG_DEBUG, " ");
559 static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
560 int r= get_bits(s, n);
563 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
566 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
567 int show= show_bits(s, 24);
568 int pos= get_bits_count(s);
569 int r= get_vlc2(s, table, bits, max_depth);
570 int len= get_bits_count(s) - pos;
571 int bits2= show>>(24-len);
573 print_bin(bits2, len);
575 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
578 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
579 int show= show_bits(s, n);
580 int r= get_xbits(s, n);
583 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
587 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
588 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
589 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
590 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
591 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
593 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
596 #define tprintf(p, ...) {}
599 static inline int decode012(GetBitContext *gb){
605 return get_bits1(gb) + 1;
608 static inline int decode210(GetBitContext *gb){
612 return 2 - get_bits1(gb);
615 static inline int get_bits_left(GetBitContext *gb)
617 return gb->size_in_bits - get_bits_count(gb);
620 #endif /* AVCODEC_GET_BITS_H */