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/bitstream.h
23 * bitstream api header.
26 #ifndef AVCODEC_BITSTREAM_H
27 #define AVCODEC_BITSTREAM_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 //#define ALT_BITSTREAM_WRITER
43 //#define ALIGNED_BITSTREAM_WRITER
44 #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
46 # define A32_BITSTREAM_READER
48 # define ALT_BITSTREAM_READER
49 //#define LIBMPEG2_BITSTREAM_READER
50 //#define A32_BITSTREAM_READER
54 extern const uint8_t ff_reverse[256];
57 // avoid +32 for shift optimization (gcc should do that ...)
58 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
59 __asm__ ("sarl %1, %0\n\t"
61 : "ic" ((uint8_t)(-s))
65 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
66 __asm__ ("shrl %1, %0\n\t"
68 : "ic" ((uint8_t)(-s))
73 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
74 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
79 /* buf and buf_end must be present and used by every alternative writer. */
80 typedef struct PutBitContext {
81 #ifdef ALT_BITSTREAM_WRITER
82 uint8_t *buf, *buf_end;
87 uint8_t *buf, *buf_ptr, *buf_end;
93 * Initializes the PutBitContext \p s.
95 * @param buffer the buffer where to put bits
96 * @param buffer_size the size in bytes of \p buffer
98 static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
100 if(buffer_size < 0) {
105 s->size_in_bits= 8*buffer_size;
107 s->buf_end = s->buf + buffer_size;
108 #ifdef ALT_BITSTREAM_WRITER
110 ((uint32_t*)(s->buf))[0]=0;
111 // memset(buffer, 0, buffer_size);
120 * Returns the total number of bits written to the bitstream.
122 static inline int put_bits_count(PutBitContext *s)
124 #ifdef ALT_BITSTREAM_WRITER
127 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
132 * Pads the end of the output stream with zeros.
134 static inline void flush_put_bits(PutBitContext *s)
136 #ifdef ALT_BITSTREAM_WRITER
139 #ifndef BITSTREAM_WRITER_LE
140 s->bit_buf<<= s->bit_left;
142 while (s->bit_left < 32) {
143 /* XXX: should test end of buffer */
144 #ifdef BITSTREAM_WRITER_LE
145 *s->buf_ptr++=s->bit_buf;
148 *s->buf_ptr++=s->bit_buf >> 24;
159 * Pads the bitstream with zeros up to the next byte boundary.
161 void align_put_bits(PutBitContext *s);
164 * Puts the string \p s in the bitstream.
166 * @param terminate_string 0-terminates the written string if value is 1
168 void ff_put_string(PutBitContext * pbc, const char *s, int terminate_string);
171 * Copies the content of \p src to the bitstream.
173 * @param length the number of bits of \p src to copy
175 void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
178 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
179 typedef struct GetBitContext {
180 const uint8_t *buffer, *buffer_end;
181 #ifdef ALT_BITSTREAM_READER
183 #elif defined LIBMPEG2_BITSTREAM_READER
187 #elif defined A32_BITSTREAM_READER
188 uint32_t *buffer_ptr;
196 #define VLC_TYPE int16_t
200 VLC_TYPE (*table)[2]; ///< code, bits
201 int table_size, table_allocated;
204 typedef struct RL_VLC_ELEM {
210 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
211 #ifndef ALT_BITSTREAM_WRITER
213 unsigned int bit_buf;
216 // printf("put_bits=%d %x\n", n, value);
217 assert(n == 32 || value < (1U << n));
219 bit_buf = s->bit_buf;
220 bit_left = s->bit_left;
222 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
224 #ifdef BITSTREAM_WRITER_LE
225 bit_buf |= value << (32 - bit_left);
227 #if !HAVE_FAST_UNALIGNED
228 if (3 & (intptr_t) s->buf_ptr) {
229 AV_WL32(s->buf_ptr, bit_buf);
232 *(uint32_t *)s->buf_ptr = le2me_32(bit_buf);
234 bit_buf = (bit_left==32)?0:value >> bit_left;
240 bit_buf = (bit_buf<<n) | value;
244 bit_buf |= value >> (n - bit_left);
245 #if !HAVE_FAST_UNALIGNED
246 if (3 & (intptr_t) s->buf_ptr) {
247 AV_WB32(s->buf_ptr, bit_buf);
250 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
251 //printf("bitbuf = %08x\n", bit_buf);
258 s->bit_buf = bit_buf;
259 s->bit_left = bit_left;
261 #else /* ALT_BITSTREAM_WRITER defined */
263 # ifdef ALIGNED_BITSTREAM_WRITER
266 "movl %0, %%ecx \n\t"
267 "xorl %%eax, %%eax \n\t"
268 "shrdl %%cl, %1, %%eax \n\t"
270 "movl %0, %%ecx \n\t"
271 "shrl $3, %%ecx \n\t"
272 "andl $0xFFFFFFFC, %%ecx \n\t"
274 "orl %1, (%2, %%ecx) \n\t"
277 "movl %%eax, 4(%2, %%ecx) \n\t"
278 : "=&r" (s->index), "=&r" (value)
279 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
284 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
288 ptr[0] |= be2me_32(value>>(index&31));
289 ptr[1] = be2me_32(value<<(32-(index&31)));
290 //if(n>24) printf("%d %d\n", n, value);
294 # else //ALIGNED_BITSTREAM_WRITER
297 "movl $7, %%ecx \n\t"
298 "andl %0, %%ecx \n\t"
299 "addl %3, %%ecx \n\t"
303 "movl %0, %%ecx \n\t"
304 "shrl $3, %%ecx \n\t"
305 "orl %1, (%%ecx, %2) \n\t"
307 "movl $0, 4(%%ecx, %2) \n\t"
308 : "=&r" (s->index), "=&r" (value)
309 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
314 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
316 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
318 //if(n>24) printf("%d %d\n", n, value);
322 # endif //!ALIGNED_BITSTREAM_WRITER
326 static inline void put_sbits(PutBitContext *pb, int bits, int32_t val)
328 assert(bits >= 0 && bits <= 31);
330 put_bits(pb, bits, val & ((1<<bits)-1));
334 static inline uint8_t* pbBufPtr(PutBitContext *s)
336 #ifdef ALT_BITSTREAM_WRITER
337 return s->buf + (s->index>>3);
344 * Skips the given number of bytes.
345 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
347 static inline void skip_put_bytes(PutBitContext *s, int n){
348 assert((put_bits_count(s)&7)==0);
349 #ifdef ALT_BITSTREAM_WRITER
350 FIXME may need some cleaning of the buffer
353 assert(s->bit_left==32);
359 * Skips the given number of bits.
360 * Must only be used if the actual values in the bitstream do not matter.
361 * If \p n is 0 the behavior is undefined.
363 static inline void skip_put_bits(PutBitContext *s, int n){
364 #ifdef ALT_BITSTREAM_WRITER
368 s->buf_ptr-= s->bit_left>>5;
374 * Changes the end of the buffer.
376 * @param size the new size in bytes of the buffer where to put bits
378 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
379 s->buf_end= s->buf + size;
382 /* Bitstream reader API docs:
384 arbitrary name which is used as prefix for the internal variables
389 OPEN_READER(name, gb)
390 loads gb into local variables
392 CLOSE_READER(name, gb)
393 stores local vars in gb
395 UPDATE_CACHE(name, gb)
396 refills the internal cache from the bitstream
397 after this call at least MIN_CACHE_BITS will be available,
400 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
402 SHOW_UBITS(name, gb, num)
403 will return the next num bits
405 SHOW_SBITS(name, gb, num)
406 will return the next num bits and do sign extension
408 SKIP_BITS(name, gb, num)
409 will skip over the next num bits
410 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
412 SKIP_CACHE(name, gb, num)
413 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
415 SKIP_COUNTER(name, gb, num)
416 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
418 LAST_SKIP_CACHE(name, gb, num)
419 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
421 LAST_SKIP_BITS(name, gb, num)
422 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
424 for examples see get_bits, show_bits, skip_bits, get_vlc
427 #ifdef ALT_BITSTREAM_READER
428 # define MIN_CACHE_BITS 25
430 # define OPEN_READER(name, gb)\
431 int name##_index= (gb)->index;\
432 int name##_cache= 0;\
434 # define CLOSE_READER(name, gb)\
435 (gb)->index= name##_index;\
437 # ifdef ALT_BITSTREAM_READER_LE
438 # define UPDATE_CACHE(name, gb)\
439 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
441 # define SKIP_CACHE(name, gb, num)\
442 name##_cache >>= (num);
444 # define UPDATE_CACHE(name, gb)\
445 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
447 # define SKIP_CACHE(name, gb, num)\
448 name##_cache <<= (num);
452 # define SKIP_COUNTER(name, gb, num)\
453 name##_index += (num);\
455 # define SKIP_BITS(name, gb, num)\
457 SKIP_CACHE(name, gb, num)\
458 SKIP_COUNTER(name, gb, num)\
461 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
462 # define LAST_SKIP_CACHE(name, gb, num) ;
464 # ifdef ALT_BITSTREAM_READER_LE
465 # define SHOW_UBITS(name, gb, num)\
466 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
468 # define SHOW_SBITS(name, gb, num)\
469 NEG_SSR32((name##_cache)<<(32-(num)), num)
471 # define SHOW_UBITS(name, gb, num)\
472 NEG_USR32(name##_cache, num)
474 # define SHOW_SBITS(name, gb, num)\
475 NEG_SSR32(name##_cache, num)
478 # define GET_CACHE(name, gb)\
479 ((uint32_t)name##_cache)
481 static inline int get_bits_count(GetBitContext *s){
485 static inline void skip_bits_long(GetBitContext *s, int n){
489 #elif defined LIBMPEG2_BITSTREAM_READER
490 //libmpeg2 like reader
492 # define MIN_CACHE_BITS 17
494 # define OPEN_READER(name, gb)\
495 int name##_bit_count=(gb)->bit_count;\
496 int name##_cache= (gb)->cache;\
497 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
499 # define CLOSE_READER(name, gb)\
500 (gb)->bit_count= name##_bit_count;\
501 (gb)->cache= name##_cache;\
502 (gb)->buffer_ptr= name##_buffer_ptr;\
504 # define UPDATE_CACHE(name, gb)\
505 if(name##_bit_count >= 0){\
506 name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
507 name##_buffer_ptr+=2;\
508 name##_bit_count-= 16;\
511 # define SKIP_CACHE(name, gb, num)\
512 name##_cache <<= (num);\
514 # define SKIP_COUNTER(name, gb, num)\
515 name##_bit_count += (num);\
517 # define SKIP_BITS(name, gb, num)\
519 SKIP_CACHE(name, gb, num)\
520 SKIP_COUNTER(name, gb, num)\
523 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
524 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
526 # define SHOW_UBITS(name, gb, num)\
527 NEG_USR32(name##_cache, num)
529 # define SHOW_SBITS(name, gb, num)\
530 NEG_SSR32(name##_cache, num)
532 # define GET_CACHE(name, gb)\
533 ((uint32_t)name##_cache)
535 static inline int get_bits_count(GetBitContext *s){
536 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
539 static inline void skip_bits_long(GetBitContext *s, int n){
542 re_buffer_ptr += 2*(re_bit_count>>4);
544 re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
549 #elif defined A32_BITSTREAM_READER
551 # define MIN_CACHE_BITS 32
553 # define OPEN_READER(name, gb)\
554 int name##_bit_count=(gb)->bit_count;\
555 uint32_t name##_cache0= (gb)->cache0;\
556 uint32_t name##_cache1= (gb)->cache1;\
557 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
559 # define CLOSE_READER(name, gb)\
560 (gb)->bit_count= name##_bit_count;\
561 (gb)->cache0= name##_cache0;\
562 (gb)->cache1= name##_cache1;\
563 (gb)->buffer_ptr= name##_buffer_ptr;\
565 # define UPDATE_CACHE(name, gb)\
566 if(name##_bit_count > 0){\
567 const uint32_t next= be2me_32( *name##_buffer_ptr );\
568 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
569 name##_cache1 |= next<<name##_bit_count;\
570 name##_buffer_ptr++;\
571 name##_bit_count-= 32;\
575 # define SKIP_CACHE(name, gb, num)\
577 "shldl %2, %1, %0 \n\t"\
579 : "+r" (name##_cache0), "+r" (name##_cache1)\
580 : "Ic" ((uint8_t)(num))\
583 # define SKIP_CACHE(name, gb, num)\
584 name##_cache0 <<= (num);\
585 name##_cache0 |= NEG_USR32(name##_cache1,num);\
586 name##_cache1 <<= (num);
589 # define SKIP_COUNTER(name, gb, num)\
590 name##_bit_count += (num);\
592 # define SKIP_BITS(name, gb, num)\
594 SKIP_CACHE(name, gb, num)\
595 SKIP_COUNTER(name, gb, num)\
598 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
599 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
601 # define SHOW_UBITS(name, gb, num)\
602 NEG_USR32(name##_cache0, num)
604 # define SHOW_SBITS(name, gb, num)\
605 NEG_SSR32(name##_cache0, num)
607 # define GET_CACHE(name, gb)\
610 static inline int get_bits_count(GetBitContext *s){
611 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
614 static inline void skip_bits_long(GetBitContext *s, int n){
617 re_buffer_ptr += re_bit_count>>5;
619 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
628 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
629 * if MSB not set it is negative
630 * @param n length in bits
633 static inline int get_xbits(GetBitContext *s, int n){
635 register int32_t cache;
638 cache = GET_CACHE(re,s);
640 LAST_SKIP_BITS(re, s, n)
642 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
645 static inline int get_sbits(GetBitContext *s, int n){
649 tmp= SHOW_SBITS(re, s, n);
650 LAST_SKIP_BITS(re, s, n)
657 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
659 static inline unsigned int get_bits(GetBitContext *s, int n){
663 tmp= SHOW_UBITS(re, s, n);
664 LAST_SKIP_BITS(re, s, n)
671 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
673 static inline unsigned int show_bits(GetBitContext *s, int n){
677 tmp= SHOW_UBITS(re, s, n);
678 // CLOSE_READER(re, s)
682 static inline void skip_bits(GetBitContext *s, int n){
683 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
686 LAST_SKIP_BITS(re, s, n)
690 static inline unsigned int get_bits1(GetBitContext *s){
691 #ifdef ALT_BITSTREAM_READER
693 uint8_t result= s->buffer[ index>>3 ];
694 #ifdef ALT_BITSTREAM_READER_LE
695 result>>= (index&0x07);
698 result<<= (index&0x07);
706 return get_bits(s, 1);
710 static inline unsigned int show_bits1(GetBitContext *s){
711 return show_bits(s, 1);
714 static inline void skip_bits1(GetBitContext *s){
721 static inline unsigned int get_bits_long(GetBitContext *s, int n){
722 if(n<=17) return get_bits(s, n);
724 #ifdef ALT_BITSTREAM_READER_LE
725 int ret= get_bits(s, 16);
726 return ret | (get_bits(s, n-16) << 16);
728 int ret= get_bits(s, 16) << (n-16);
729 return ret | get_bits(s, n-16);
735 * reads 0-32 bits as a signed integer.
737 static inline int get_sbits_long(GetBitContext *s, int n) {
738 return sign_extend(get_bits_long(s, n), n);
744 static inline unsigned int show_bits_long(GetBitContext *s, int n){
745 if(n<=17) return show_bits(s, n);
747 GetBitContext gb= *s;
748 return get_bits_long(&gb, n);
752 static inline int check_marker(GetBitContext *s, const char *msg)
754 int bit= get_bits1(s);
756 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
762 * init GetBitContext.
763 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
764 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
765 * @param bit_size the size of the buffer in bits
767 static inline void init_get_bits(GetBitContext *s,
768 const uint8_t *buffer, int bit_size)
770 int buffer_size= (bit_size+7)>>3;
771 if(buffer_size < 0 || bit_size < 0) {
772 buffer_size = bit_size = 0;
777 s->size_in_bits= bit_size;
778 s->buffer_end= buffer + buffer_size;
779 #ifdef ALT_BITSTREAM_READER
781 #elif defined LIBMPEG2_BITSTREAM_READER
782 s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
783 s->bit_count = 16 + 8*((intptr_t)buffer&1);
784 skip_bits_long(s, 0);
785 #elif defined A32_BITSTREAM_READER
786 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
787 s->bit_count = 32 + 8*((intptr_t)buffer&3);
788 skip_bits_long(s, 0);
792 static inline void align_get_bits(GetBitContext *s)
794 int n= (-get_bits_count(s)) & 7;
795 if(n) skip_bits(s, n);
798 #define init_vlc(vlc, nb_bits, nb_codes,\
799 bits, bits_wrap, bits_size,\
800 codes, codes_wrap, codes_size,\
802 init_vlc_sparse(vlc, nb_bits, nb_codes,\
803 bits, bits_wrap, bits_size,\
804 codes, codes_wrap, codes_size,\
807 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
808 const void *bits, int bits_wrap, int bits_size,
809 const void *codes, int codes_wrap, int codes_size,
810 const void *symbols, int symbols_wrap, int symbols_size,
812 #define INIT_VLC_USE_STATIC 1 ///< VERY strongly deprecated and forbidden
813 #define INIT_VLC_LE 2
814 #define INIT_VLC_USE_NEW_STATIC 4
815 void free_vlc(VLC *vlc);
817 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
819 static VLC_TYPE table[static_size][2];\
820 (vlc)->table= table;\
821 (vlc)->table_allocated= static_size;\
822 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
828 * if the vlc code is invalid and max_depth=1 than no bits will be removed
829 * if the vlc code is invalid and max_depth>1 than the number of bits removed
832 #define GET_VLC(code, name, gb, table, bits, max_depth)\
834 int n, index, nb_bits;\
836 index= SHOW_UBITS(name, gb, bits);\
837 code = table[index][0];\
838 n = table[index][1];\
840 if(max_depth > 1 && n < 0){\
841 LAST_SKIP_BITS(name, gb, bits)\
842 UPDATE_CACHE(name, gb)\
846 index= SHOW_UBITS(name, gb, nb_bits) + code;\
847 code = table[index][0];\
848 n = table[index][1];\
849 if(max_depth > 2 && n < 0){\
850 LAST_SKIP_BITS(name, gb, nb_bits)\
851 UPDATE_CACHE(name, gb)\
855 index= SHOW_UBITS(name, gb, nb_bits) + code;\
856 code = table[index][0];\
857 n = table[index][1];\
860 SKIP_BITS(name, gb, n)\
863 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
865 int n, index, nb_bits;\
867 index= SHOW_UBITS(name, gb, bits);\
868 level = table[index].level;\
869 n = table[index].len;\
871 if(max_depth > 1 && n < 0){\
872 SKIP_BITS(name, gb, bits)\
874 UPDATE_CACHE(name, gb)\
879 index= SHOW_UBITS(name, gb, nb_bits) + level;\
880 level = table[index].level;\
881 n = table[index].len;\
883 run= table[index].run;\
884 SKIP_BITS(name, gb, n)\
889 * parses a vlc code, faster then get_vlc()
890 * @param bits is the number of bits which will be read at once, must be
891 * identical to nb_bits in init_vlc()
892 * @param max_depth is the number of times bits bits must be read to completely
893 * read the longest vlc code
894 * = (max_vlc_length + bits - 1) / bits
896 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
897 int bits, int max_depth)
904 GET_VLC(code, re, s, table, bits, max_depth)
913 static inline void print_bin(int bits, int n){
916 for(i=n-1; i>=0; i--){
917 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
920 av_log(NULL, AV_LOG_DEBUG, " ");
923 static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
924 int r= get_bits(s, n);
927 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);
930 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
931 int show= show_bits(s, 24);
932 int pos= get_bits_count(s);
933 int r= get_vlc2(s, table, bits, max_depth);
934 int len= get_bits_count(s) - pos;
935 int bits2= show>>(24-len);
937 print_bin(bits2, len);
939 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
942 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
943 int show= show_bits(s, n);
944 int r= get_xbits(s, n);
947 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);
951 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
952 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
953 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
954 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
955 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
957 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
960 #define tprintf(p, ...) {}
963 static inline int decode012(GetBitContext *gb){
969 return get_bits1(gb) + 1;
972 static inline int decode210(GetBitContext *gb){
976 return 2 - get_bits1(gb);
979 #endif /* AVCODEC_BITSTREAM_H */