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
22 * @file libavcodec/get_bits.h
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(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
44 # define A32_BITSTREAM_READER
46 # define ALT_BITSTREAM_READER
47 //#define LIBMPEG2_BITSTREAM_READER
48 //#define A32_BITSTREAM_READER
52 extern const uint8_t ff_reverse[256];
55 // avoid +32 for shift optimization (gcc should do that ...)
56 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
57 __asm__ ("sarl %1, %0\n\t"
59 : "ic" ((uint8_t)(-s))
63 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
64 __asm__ ("shrl %1, %0\n\t"
66 : "ic" ((uint8_t)(-s))
71 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
72 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
76 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
77 typedef struct GetBitContext {
78 const uint8_t *buffer, *buffer_end;
79 #ifdef ALT_BITSTREAM_READER
81 #elif defined LIBMPEG2_BITSTREAM_READER
85 #elif defined A32_BITSTREAM_READER
94 #define VLC_TYPE int16_t
98 VLC_TYPE (*table)[2]; ///< code, bits
99 int table_size, table_allocated;
102 typedef struct RL_VLC_ELEM {
108 /* Bitstream reader API docs:
110 arbitrary name which is used as prefix for the internal variables
115 OPEN_READER(name, gb)
116 loads gb into local variables
118 CLOSE_READER(name, gb)
119 stores local vars in gb
121 UPDATE_CACHE(name, gb)
122 refills the internal cache from the bitstream
123 after this call at least MIN_CACHE_BITS will be available,
126 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
128 SHOW_UBITS(name, gb, num)
129 will return the next num bits
131 SHOW_SBITS(name, gb, num)
132 will return the next num bits and do sign extension
134 SKIP_BITS(name, gb, num)
135 will skip over the next num bits
136 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
138 SKIP_CACHE(name, gb, num)
139 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
141 SKIP_COUNTER(name, gb, num)
142 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
144 LAST_SKIP_CACHE(name, gb, num)
145 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
147 LAST_SKIP_BITS(name, gb, num)
148 is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER
150 for examples see get_bits, show_bits, skip_bits, get_vlc
153 #ifdef ALT_BITSTREAM_READER
154 # define MIN_CACHE_BITS 25
156 # define OPEN_READER(name, gb)\
157 int name##_index= (gb)->index;\
158 int name##_cache= 0;\
160 # define CLOSE_READER(name, gb)\
161 (gb)->index= name##_index;\
163 # ifdef ALT_BITSTREAM_READER_LE
164 # define UPDATE_CACHE(name, gb)\
165 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
167 # define SKIP_CACHE(name, gb, num)\
168 name##_cache >>= (num);
170 # define UPDATE_CACHE(name, gb)\
171 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
173 # define SKIP_CACHE(name, gb, num)\
174 name##_cache <<= (num);
178 # define SKIP_COUNTER(name, gb, num)\
179 name##_index += (num);\
181 # define SKIP_BITS(name, gb, num)\
183 SKIP_CACHE(name, gb, num)\
184 SKIP_COUNTER(name, gb, num)\
187 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
188 # define LAST_SKIP_CACHE(name, gb, num) ;
190 # ifdef ALT_BITSTREAM_READER_LE
191 # define SHOW_UBITS(name, gb, num)\
192 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
194 # define SHOW_SBITS(name, gb, num)\
195 NEG_SSR32((name##_cache)<<(32-(num)), num)
197 # define SHOW_UBITS(name, gb, num)\
198 NEG_USR32(name##_cache, num)
200 # define SHOW_SBITS(name, gb, num)\
201 NEG_SSR32(name##_cache, num)
204 # define GET_CACHE(name, gb)\
205 ((uint32_t)name##_cache)
207 static inline int get_bits_count(GetBitContext *s){
211 static inline void skip_bits_long(GetBitContext *s, int n){
215 #elif defined LIBMPEG2_BITSTREAM_READER
216 //libmpeg2 like reader
218 # define MIN_CACHE_BITS 17
220 # define OPEN_READER(name, gb)\
221 int name##_bit_count=(gb)->bit_count;\
222 int name##_cache= (gb)->cache;\
223 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
225 # define CLOSE_READER(name, gb)\
226 (gb)->bit_count= name##_bit_count;\
227 (gb)->cache= name##_cache;\
228 (gb)->buffer_ptr= name##_buffer_ptr;\
230 # define UPDATE_CACHE(name, gb)\
231 if(name##_bit_count >= 0){\
232 name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
233 name##_buffer_ptr+=2;\
234 name##_bit_count-= 16;\
237 # define SKIP_CACHE(name, gb, num)\
238 name##_cache <<= (num);\
240 # define SKIP_COUNTER(name, gb, num)\
241 name##_bit_count += (num);\
243 # define SKIP_BITS(name, gb, num)\
245 SKIP_CACHE(name, gb, num)\
246 SKIP_COUNTER(name, gb, num)\
249 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
250 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
252 # define SHOW_UBITS(name, gb, num)\
253 NEG_USR32(name##_cache, num)
255 # define SHOW_SBITS(name, gb, num)\
256 NEG_SSR32(name##_cache, num)
258 # define GET_CACHE(name, gb)\
259 ((uint32_t)name##_cache)
261 static inline int get_bits_count(GetBitContext *s){
262 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
265 static inline void skip_bits_long(GetBitContext *s, int n){
268 re_buffer_ptr += 2*(re_bit_count>>4);
270 re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
275 #elif defined A32_BITSTREAM_READER
277 # define MIN_CACHE_BITS 32
279 # define OPEN_READER(name, gb)\
280 int name##_bit_count=(gb)->bit_count;\
281 uint32_t name##_cache0= (gb)->cache0;\
282 uint32_t name##_cache1= (gb)->cache1;\
283 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
285 # define CLOSE_READER(name, gb)\
286 (gb)->bit_count= name##_bit_count;\
287 (gb)->cache0= name##_cache0;\
288 (gb)->cache1= name##_cache1;\
289 (gb)->buffer_ptr= name##_buffer_ptr;\
291 # define UPDATE_CACHE(name, gb)\
292 if(name##_bit_count > 0){\
293 const uint32_t next= be2me_32( *name##_buffer_ptr );\
294 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
295 name##_cache1 |= next<<name##_bit_count;\
296 name##_buffer_ptr++;\
297 name##_bit_count-= 32;\
301 # define SKIP_CACHE(name, gb, num)\
303 "shldl %2, %1, %0 \n\t"\
305 : "+r" (name##_cache0), "+r" (name##_cache1)\
306 : "Ic" ((uint8_t)(num))\
309 # define SKIP_CACHE(name, gb, num)\
310 name##_cache0 <<= (num);\
311 name##_cache0 |= NEG_USR32(name##_cache1,num);\
312 name##_cache1 <<= (num);
315 # define SKIP_COUNTER(name, gb, num)\
316 name##_bit_count += (num);\
318 # define SKIP_BITS(name, gb, num)\
320 SKIP_CACHE(name, gb, num)\
321 SKIP_COUNTER(name, gb, num)\
324 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
325 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
327 # define SHOW_UBITS(name, gb, num)\
328 NEG_USR32(name##_cache0, num)
330 # define SHOW_SBITS(name, gb, num)\
331 NEG_SSR32(name##_cache0, num)
333 # define GET_CACHE(name, gb)\
336 static inline int get_bits_count(GetBitContext *s){
337 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
340 static inline void skip_bits_long(GetBitContext *s, int n){
343 re_buffer_ptr += re_bit_count>>5;
345 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
354 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
355 * if MSB not set it is negative
356 * @param n length in bits
359 static inline int get_xbits(GetBitContext *s, int n){
361 register int32_t cache;
364 cache = GET_CACHE(re,s);
366 LAST_SKIP_BITS(re, s, n)
368 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
371 static inline int get_sbits(GetBitContext *s, int n){
375 tmp= SHOW_SBITS(re, s, n);
376 LAST_SKIP_BITS(re, s, n)
383 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
385 static inline unsigned int get_bits(GetBitContext *s, int n){
389 tmp= SHOW_UBITS(re, s, n);
390 LAST_SKIP_BITS(re, s, n)
397 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
399 static inline unsigned int show_bits(GetBitContext *s, int n){
403 tmp= SHOW_UBITS(re, s, n);
404 // CLOSE_READER(re, s)
408 static inline void skip_bits(GetBitContext *s, int n){
409 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
412 LAST_SKIP_BITS(re, s, n)
416 static inline unsigned int get_bits1(GetBitContext *s){
417 #ifdef ALT_BITSTREAM_READER
419 uint8_t result= s->buffer[ index>>3 ];
420 #ifdef ALT_BITSTREAM_READER_LE
421 result>>= (index&0x07);
424 result<<= (index&0x07);
432 return get_bits(s, 1);
436 static inline unsigned int show_bits1(GetBitContext *s){
437 return show_bits(s, 1);
440 static inline void skip_bits1(GetBitContext *s){
447 static inline unsigned int get_bits_long(GetBitContext *s, int n){
448 if(n<=17) return get_bits(s, n);
450 #ifdef ALT_BITSTREAM_READER_LE
451 int ret= get_bits(s, 16);
452 return ret | (get_bits(s, n-16) << 16);
454 int ret= get_bits(s, 16) << (n-16);
455 return ret | get_bits(s, n-16);
461 * reads 0-32 bits as a signed integer.
463 static inline int get_sbits_long(GetBitContext *s, int n) {
464 return sign_extend(get_bits_long(s, n), n);
470 static inline unsigned int show_bits_long(GetBitContext *s, int n){
471 if(n<=17) return show_bits(s, n);
473 GetBitContext gb= *s;
474 return get_bits_long(&gb, n);
478 static inline int check_marker(GetBitContext *s, const char *msg)
480 int bit= get_bits1(s);
482 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
488 * init GetBitContext.
489 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
490 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
491 * @param bit_size the size of the buffer in bits
493 * While GetBitContext stores the buffer size, for performance reasons you are
494 * responsible for checking for the buffer end yourself (take advantage of the padding)!
496 static inline void init_get_bits(GetBitContext *s,
497 const uint8_t *buffer, int bit_size)
499 int buffer_size= (bit_size+7)>>3;
500 if(buffer_size < 0 || bit_size < 0) {
501 buffer_size = bit_size = 0;
506 s->size_in_bits= bit_size;
507 s->buffer_end= buffer + buffer_size;
508 #ifdef ALT_BITSTREAM_READER
510 #elif defined LIBMPEG2_BITSTREAM_READER
511 s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
512 s->bit_count = 16 + 8*((intptr_t)buffer&1);
513 skip_bits_long(s, 0);
514 #elif defined A32_BITSTREAM_READER
515 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
516 s->bit_count = 32 + 8*((intptr_t)buffer&3);
517 skip_bits_long(s, 0);
521 static inline void align_get_bits(GetBitContext *s)
523 int n= (-get_bits_count(s)) & 7;
524 if(n) skip_bits(s, n);
527 #define init_vlc(vlc, nb_bits, nb_codes,\
528 bits, bits_wrap, bits_size,\
529 codes, codes_wrap, codes_size,\
531 init_vlc_sparse(vlc, nb_bits, nb_codes,\
532 bits, bits_wrap, bits_size,\
533 codes, codes_wrap, codes_size,\
536 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
537 const void *bits, int bits_wrap, int bits_size,
538 const void *codes, int codes_wrap, int codes_size,
539 const void *symbols, int symbols_wrap, int symbols_size,
541 #define INIT_VLC_LE 2
542 #define INIT_VLC_USE_NEW_STATIC 4
543 void free_vlc(VLC *vlc);
545 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
547 static VLC_TYPE table[static_size][2];\
548 (vlc)->table= table;\
549 (vlc)->table_allocated= static_size;\
550 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
556 * if the vlc code is invalid and max_depth=1 than no bits will be removed
557 * if the vlc code is invalid and max_depth>1 than the number of bits removed
560 #define GET_VLC(code, name, gb, table, bits, max_depth)\
562 int n, index, nb_bits;\
564 index= SHOW_UBITS(name, gb, bits);\
565 code = table[index][0];\
566 n = table[index][1];\
568 if(max_depth > 1 && n < 0){\
569 LAST_SKIP_BITS(name, gb, bits)\
570 UPDATE_CACHE(name, gb)\
574 index= SHOW_UBITS(name, gb, nb_bits) + code;\
575 code = table[index][0];\
576 n = table[index][1];\
577 if(max_depth > 2 && n < 0){\
578 LAST_SKIP_BITS(name, gb, nb_bits)\
579 UPDATE_CACHE(name, gb)\
583 index= SHOW_UBITS(name, gb, nb_bits) + code;\
584 code = table[index][0];\
585 n = table[index][1];\
588 SKIP_BITS(name, gb, n)\
591 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
593 int n, index, nb_bits;\
595 index= SHOW_UBITS(name, gb, bits);\
596 level = table[index].level;\
597 n = table[index].len;\
599 if(max_depth > 1 && n < 0){\
600 SKIP_BITS(name, gb, bits)\
602 UPDATE_CACHE(name, gb)\
607 index= SHOW_UBITS(name, gb, nb_bits) + level;\
608 level = table[index].level;\
609 n = table[index].len;\
611 run= table[index].run;\
612 SKIP_BITS(name, gb, n)\
617 * parses a vlc code, faster then get_vlc()
618 * @param bits is the number of bits which will be read at once, must be
619 * identical to nb_bits in init_vlc()
620 * @param max_depth is the number of times bits bits must be read to completely
621 * read the longest vlc code
622 * = (max_vlc_length + bits - 1) / bits
624 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
625 int bits, int max_depth)
632 GET_VLC(code, re, s, table, bits, max_depth)
641 static inline void print_bin(int bits, int n){
644 for(i=n-1; i>=0; i--){
645 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
648 av_log(NULL, AV_LOG_DEBUG, " ");
651 static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
652 int r= get_bits(s, n);
655 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);
658 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
659 int show= show_bits(s, 24);
660 int pos= get_bits_count(s);
661 int r= get_vlc2(s, table, bits, max_depth);
662 int len= get_bits_count(s) - pos;
663 int bits2= show>>(24-len);
665 print_bin(bits2, len);
667 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
670 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
671 int show= show_bits(s, n);
672 int r= get_xbits(s, n);
675 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);
679 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
680 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
681 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
682 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
683 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
685 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
688 #define tprintf(p, ...) {}
691 static inline int decode012(GetBitContext *gb){
697 return get_bits1(gb) + 1;
700 static inline int decode210(GetBitContext *gb){
704 return 2 - get_bits1(gb);
707 #endif /* AVCODEC_GET_BITS_H */