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"
37 #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
38 # define ALT_BITSTREAM_READER
41 //#define ALT_BITSTREAM_WRITER
42 //#define ALIGNED_BITSTREAM_WRITER
43 #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
45 # define A32_BITSTREAM_READER
47 # define ALT_BITSTREAM_READER
48 //#define LIBMPEG2_BITSTREAM_READER
49 //#define A32_BITSTREAM_READER
53 extern const uint8_t ff_reverse[256];
56 // avoid +32 for shift optimization (gcc should do that ...)
57 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
58 __asm__ ("sarl %1, %0\n\t"
60 : "ic" ((uint8_t)(-s))
64 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
65 __asm__ ("shrl %1, %0\n\t"
67 : "ic" ((uint8_t)(-s))
72 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
73 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
78 /* buf and buf_end must be present and used by every alternative writer. */
79 typedef struct PutBitContext {
80 #ifdef ALT_BITSTREAM_WRITER
81 uint8_t *buf, *buf_end;
86 uint8_t *buf, *buf_ptr, *buf_end;
91 static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
98 s->size_in_bits= 8*buffer_size;
100 s->buf_end = s->buf + buffer_size;
101 #ifdef ALT_BITSTREAM_WRITER
103 ((uint32_t*)(s->buf))[0]=0;
104 // memset(buffer, 0, buffer_size);
112 /* return the number of bits output */
113 static inline int put_bits_count(PutBitContext *s)
115 #ifdef ALT_BITSTREAM_WRITER
118 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
122 /* pad the end of the output stream with zeros */
123 static inline void flush_put_bits(PutBitContext *s)
125 #ifdef ALT_BITSTREAM_WRITER
128 #ifndef BITSTREAM_WRITER_LE
129 s->bit_buf<<= s->bit_left;
131 while (s->bit_left < 32) {
132 /* XXX: should test end of buffer */
133 #ifdef BITSTREAM_WRITER_LE
134 *s->buf_ptr++=s->bit_buf;
137 *s->buf_ptr++=s->bit_buf >> 24;
147 void align_put_bits(PutBitContext *s);
148 void ff_put_string(PutBitContext * pbc, const char *s, int put_zero);
149 void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
152 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
153 typedef struct GetBitContext {
154 const uint8_t *buffer, *buffer_end;
155 #ifdef ALT_BITSTREAM_READER
157 #elif defined LIBMPEG2_BITSTREAM_READER
161 #elif defined A32_BITSTREAM_READER
162 uint32_t *buffer_ptr;
170 #define VLC_TYPE int16_t
174 VLC_TYPE (*table)[2]; ///< code, bits
175 int table_size, table_allocated;
178 typedef struct RL_VLC_ELEM {
184 #ifndef ALT_BITSTREAM_WRITER
185 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
187 unsigned int bit_buf;
190 // printf("put_bits=%d %x\n", n, value);
191 assert(n == 32 || value < (1U << n));
193 bit_buf = s->bit_buf;
194 bit_left = s->bit_left;
196 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
198 #ifdef BITSTREAM_WRITER_LE
199 bit_buf |= value << (32 - bit_left);
201 #if !HAVE_FAST_UNALIGNED
202 if (3 & (intptr_t) s->buf_ptr) {
203 AV_WL32(s->buf_ptr, bit_buf);
206 *(uint32_t *)s->buf_ptr = le2me_32(bit_buf);
208 bit_buf = (bit_left==32)?0:value >> bit_left;
214 bit_buf = (bit_buf<<n) | value;
218 bit_buf |= value >> (n - bit_left);
219 #if !HAVE_FAST_UNALIGNED
220 if (3 & (intptr_t) s->buf_ptr) {
221 AV_WB32(s->buf_ptr, bit_buf);
224 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
225 //printf("bitbuf = %08x\n", bit_buf);
232 s->bit_buf = bit_buf;
233 s->bit_left = bit_left;
238 #ifdef ALT_BITSTREAM_WRITER
239 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
241 # ifdef ALIGNED_BITSTREAM_WRITER
244 "movl %0, %%ecx \n\t"
245 "xorl %%eax, %%eax \n\t"
246 "shrdl %%cl, %1, %%eax \n\t"
248 "movl %0, %%ecx \n\t"
249 "shrl $3, %%ecx \n\t"
250 "andl $0xFFFFFFFC, %%ecx \n\t"
252 "orl %1, (%2, %%ecx) \n\t"
255 "movl %%eax, 4(%2, %%ecx) \n\t"
256 : "=&r" (s->index), "=&r" (value)
257 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
262 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
266 ptr[0] |= be2me_32(value>>(index&31));
267 ptr[1] = be2me_32(value<<(32-(index&31)));
268 //if(n>24) printf("%d %d\n", n, value);
272 # else //ALIGNED_BITSTREAM_WRITER
275 "movl $7, %%ecx \n\t"
276 "andl %0, %%ecx \n\t"
277 "addl %3, %%ecx \n\t"
281 "movl %0, %%ecx \n\t"
282 "shrl $3, %%ecx \n\t"
283 "orl %1, (%%ecx, %2) \n\t"
285 "movl $0, 4(%%ecx, %2) \n\t"
286 : "=&r" (s->index), "=&r" (value)
287 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
292 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
294 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
296 //if(n>24) printf("%d %d\n", n, value);
300 # endif //!ALIGNED_BITSTREAM_WRITER
304 static inline void put_sbits(PutBitContext *pb, int bits, int32_t val)
306 assert(bits >= 0 && bits <= 31);
308 put_bits(pb, bits, val & ((1<<bits)-1));
312 static inline uint8_t* pbBufPtr(PutBitContext *s)
314 #ifdef ALT_BITSTREAM_WRITER
315 return s->buf + (s->index>>3);
323 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
325 static inline void skip_put_bytes(PutBitContext *s, int n){
326 assert((put_bits_count(s)&7)==0);
327 #ifdef ALT_BITSTREAM_WRITER
328 FIXME may need some cleaning of the buffer
331 assert(s->bit_left==32);
337 * Skips the given number of bits.
338 * Must only be used if the actual values in the bitstream do not matter.
340 static inline void skip_put_bits(PutBitContext *s, int n){
341 #ifdef ALT_BITSTREAM_WRITER
345 s->buf_ptr-= s->bit_left>>5;
351 * Changes the end of the buffer.
353 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
354 s->buf_end= s->buf + size;
357 /* Bitstream reader API docs:
359 arbitrary name which is used as prefix for the internal variables
364 OPEN_READER(name, gb)
365 loads gb into local variables
367 CLOSE_READER(name, gb)
368 stores local vars in gb
370 UPDATE_CACHE(name, gb)
371 refills the internal cache from the bitstream
372 after this call at least MIN_CACHE_BITS will be available,
375 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
377 SHOW_UBITS(name, gb, num)
378 will return the next num bits
380 SHOW_SBITS(name, gb, num)
381 will return the next num bits and do sign extension
383 SKIP_BITS(name, gb, num)
384 will skip over the next num bits
385 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
387 SKIP_CACHE(name, gb, num)
388 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
390 SKIP_COUNTER(name, gb, num)
391 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
393 LAST_SKIP_CACHE(name, gb, num)
394 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
396 LAST_SKIP_BITS(name, gb, num)
397 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
399 for examples see get_bits, show_bits, skip_bits, get_vlc
402 #ifdef ALT_BITSTREAM_READER
403 # define MIN_CACHE_BITS 25
405 # define OPEN_READER(name, gb)\
406 int name##_index= (gb)->index;\
407 int name##_cache= 0;\
409 # define CLOSE_READER(name, gb)\
410 (gb)->index= name##_index;\
412 # ifdef ALT_BITSTREAM_READER_LE
413 # define UPDATE_CACHE(name, gb)\
414 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
416 # define SKIP_CACHE(name, gb, num)\
417 name##_cache >>= (num);
419 # define UPDATE_CACHE(name, gb)\
420 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
422 # define SKIP_CACHE(name, gb, num)\
423 name##_cache <<= (num);
427 # define SKIP_COUNTER(name, gb, num)\
428 name##_index += (num);\
430 # define SKIP_BITS(name, gb, num)\
432 SKIP_CACHE(name, gb, num)\
433 SKIP_COUNTER(name, gb, num)\
436 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
437 # define LAST_SKIP_CACHE(name, gb, num) ;
439 # ifdef ALT_BITSTREAM_READER_LE
440 # define SHOW_UBITS(name, gb, num)\
441 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
443 # define SHOW_SBITS(name, gb, num)\
444 NEG_SSR32((name##_cache)<<(32-(num)), num)
446 # define SHOW_UBITS(name, gb, num)\
447 NEG_USR32(name##_cache, num)
449 # define SHOW_SBITS(name, gb, num)\
450 NEG_SSR32(name##_cache, num)
453 # define GET_CACHE(name, gb)\
454 ((uint32_t)name##_cache)
456 static inline int get_bits_count(GetBitContext *s){
460 static inline void skip_bits_long(GetBitContext *s, int n){
464 #elif defined LIBMPEG2_BITSTREAM_READER
465 //libmpeg2 like reader
467 # define MIN_CACHE_BITS 17
469 # define OPEN_READER(name, gb)\
470 int name##_bit_count=(gb)->bit_count;\
471 int name##_cache= (gb)->cache;\
472 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
474 # define CLOSE_READER(name, gb)\
475 (gb)->bit_count= name##_bit_count;\
476 (gb)->cache= name##_cache;\
477 (gb)->buffer_ptr= name##_buffer_ptr;\
479 # define UPDATE_CACHE(name, gb)\
480 if(name##_bit_count >= 0){\
481 name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
482 name##_buffer_ptr+=2;\
483 name##_bit_count-= 16;\
486 # define SKIP_CACHE(name, gb, num)\
487 name##_cache <<= (num);\
489 # define SKIP_COUNTER(name, gb, num)\
490 name##_bit_count += (num);\
492 # define SKIP_BITS(name, gb, num)\
494 SKIP_CACHE(name, gb, num)\
495 SKIP_COUNTER(name, gb, num)\
498 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
499 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
501 # define SHOW_UBITS(name, gb, num)\
502 NEG_USR32(name##_cache, num)
504 # define SHOW_SBITS(name, gb, num)\
505 NEG_SSR32(name##_cache, num)
507 # define GET_CACHE(name, gb)\
508 ((uint32_t)name##_cache)
510 static inline int get_bits_count(GetBitContext *s){
511 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
514 static inline void skip_bits_long(GetBitContext *s, int n){
517 re_buffer_ptr += 2*(re_bit_count>>4);
519 re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
524 #elif defined A32_BITSTREAM_READER
526 # define MIN_CACHE_BITS 32
528 # define OPEN_READER(name, gb)\
529 int name##_bit_count=(gb)->bit_count;\
530 uint32_t name##_cache0= (gb)->cache0;\
531 uint32_t name##_cache1= (gb)->cache1;\
532 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
534 # define CLOSE_READER(name, gb)\
535 (gb)->bit_count= name##_bit_count;\
536 (gb)->cache0= name##_cache0;\
537 (gb)->cache1= name##_cache1;\
538 (gb)->buffer_ptr= name##_buffer_ptr;\
540 # define UPDATE_CACHE(name, gb)\
541 if(name##_bit_count > 0){\
542 const uint32_t next= be2me_32( *name##_buffer_ptr );\
543 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
544 name##_cache1 |= next<<name##_bit_count;\
545 name##_buffer_ptr++;\
546 name##_bit_count-= 32;\
550 # define SKIP_CACHE(name, gb, num)\
552 "shldl %2, %1, %0 \n\t"\
554 : "+r" (name##_cache0), "+r" (name##_cache1)\
555 : "Ic" ((uint8_t)(num))\
558 # define SKIP_CACHE(name, gb, num)\
559 name##_cache0 <<= (num);\
560 name##_cache0 |= NEG_USR32(name##_cache1,num);\
561 name##_cache1 <<= (num);
564 # define SKIP_COUNTER(name, gb, num)\
565 name##_bit_count += (num);\
567 # define SKIP_BITS(name, gb, num)\
569 SKIP_CACHE(name, gb, num)\
570 SKIP_COUNTER(name, gb, num)\
573 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
574 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
576 # define SHOW_UBITS(name, gb, num)\
577 NEG_USR32(name##_cache0, num)
579 # define SHOW_SBITS(name, gb, num)\
580 NEG_SSR32(name##_cache0, num)
582 # define GET_CACHE(name, gb)\
585 static inline int get_bits_count(GetBitContext *s){
586 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
589 static inline void skip_bits_long(GetBitContext *s, int n){
592 re_buffer_ptr += re_bit_count>>5;
594 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
603 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
604 * if MSB not set it is negative
605 * @param n length in bits
608 static inline int get_xbits(GetBitContext *s, int n){
610 register int32_t cache;
613 cache = GET_CACHE(re,s);
615 LAST_SKIP_BITS(re, s, n)
617 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
620 static inline int get_sbits(GetBitContext *s, int n){
624 tmp= SHOW_SBITS(re, s, n);
625 LAST_SKIP_BITS(re, s, n)
632 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
634 static inline unsigned int get_bits(GetBitContext *s, int n){
638 tmp= SHOW_UBITS(re, s, n);
639 LAST_SKIP_BITS(re, s, n)
646 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
648 static inline unsigned int show_bits(GetBitContext *s, int n){
652 tmp= SHOW_UBITS(re, s, n);
653 // CLOSE_READER(re, s)
657 static inline void skip_bits(GetBitContext *s, int n){
658 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
661 LAST_SKIP_BITS(re, s, n)
665 static inline unsigned int get_bits1(GetBitContext *s){
666 #ifdef ALT_BITSTREAM_READER
668 uint8_t result= s->buffer[ index>>3 ];
669 #ifdef ALT_BITSTREAM_READER_LE
670 result>>= (index&0x07);
673 result<<= (index&0x07);
681 return get_bits(s, 1);
685 static inline unsigned int show_bits1(GetBitContext *s){
686 return show_bits(s, 1);
689 static inline void skip_bits1(GetBitContext *s){
696 static inline unsigned int get_bits_long(GetBitContext *s, int n){
697 if(n<=17) return get_bits(s, n);
699 #ifdef ALT_BITSTREAM_READER_LE
700 int ret= get_bits(s, 16);
701 return ret | (get_bits(s, n-16) << 16);
703 int ret= get_bits(s, 16) << (n-16);
704 return ret | get_bits(s, n-16);
712 static inline unsigned int show_bits_long(GetBitContext *s, int n){
713 if(n<=17) return show_bits(s, n);
715 GetBitContext gb= *s;
716 int ret= get_bits_long(s, n);
722 static inline int check_marker(GetBitContext *s, const char *msg)
724 int bit= get_bits1(s);
726 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
732 * init GetBitContext.
733 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
734 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
735 * @param bit_size the size of the buffer in bits
737 static inline void init_get_bits(GetBitContext *s,
738 const uint8_t *buffer, int bit_size)
740 int buffer_size= (bit_size+7)>>3;
741 if(buffer_size < 0 || bit_size < 0) {
742 buffer_size = bit_size = 0;
747 s->size_in_bits= bit_size;
748 s->buffer_end= buffer + buffer_size;
749 #ifdef ALT_BITSTREAM_READER
751 #elif defined LIBMPEG2_BITSTREAM_READER
752 s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
753 s->bit_count = 16 + 8*((intptr_t)buffer&1);
754 skip_bits_long(s, 0);
755 #elif defined A32_BITSTREAM_READER
756 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
757 s->bit_count = 32 + 8*((intptr_t)buffer&3);
758 skip_bits_long(s, 0);
762 static inline void align_get_bits(GetBitContext *s)
764 int n= (-get_bits_count(s)) & 7;
765 if(n) skip_bits(s, n);
768 #define init_vlc(vlc, nb_bits, nb_codes,\
769 bits, bits_wrap, bits_size,\
770 codes, codes_wrap, codes_size,\
772 init_vlc_sparse(vlc, nb_bits, nb_codes,\
773 bits, bits_wrap, bits_size,\
774 codes, codes_wrap, codes_size,\
777 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
778 const void *bits, int bits_wrap, int bits_size,
779 const void *codes, int codes_wrap, int codes_size,
780 const void *symbols, int symbols_wrap, int symbols_size,
782 #define INIT_VLC_USE_STATIC 1 ///< VERY strongly deprecated and forbidden
783 #define INIT_VLC_LE 2
784 #define INIT_VLC_USE_NEW_STATIC 4
785 void free_vlc(VLC *vlc);
787 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
789 static VLC_TYPE table[static_size][2];\
790 (vlc)->table= table;\
791 (vlc)->table_allocated= static_size;\
792 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
798 * if the vlc code is invalid and max_depth=1 than no bits will be removed
799 * if the vlc code is invalid and max_depth>1 than the number of bits removed
802 #define GET_VLC(code, name, gb, table, bits, max_depth)\
804 int n, index, nb_bits;\
806 index= SHOW_UBITS(name, gb, bits);\
807 code = table[index][0];\
808 n = table[index][1];\
810 if(max_depth > 1 && n < 0){\
811 LAST_SKIP_BITS(name, gb, bits)\
812 UPDATE_CACHE(name, gb)\
816 index= SHOW_UBITS(name, gb, nb_bits) + code;\
817 code = table[index][0];\
818 n = table[index][1];\
819 if(max_depth > 2 && n < 0){\
820 LAST_SKIP_BITS(name, gb, nb_bits)\
821 UPDATE_CACHE(name, gb)\
825 index= SHOW_UBITS(name, gb, nb_bits) + code;\
826 code = table[index][0];\
827 n = table[index][1];\
830 SKIP_BITS(name, gb, n)\
833 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
835 int n, index, nb_bits;\
837 index= SHOW_UBITS(name, gb, bits);\
838 level = table[index].level;\
839 n = table[index].len;\
841 if(max_depth > 1 && n < 0){\
842 SKIP_BITS(name, gb, bits)\
844 UPDATE_CACHE(name, gb)\
849 index= SHOW_UBITS(name, gb, nb_bits) + level;\
850 level = table[index].level;\
851 n = table[index].len;\
853 run= table[index].run;\
854 SKIP_BITS(name, gb, n)\
859 * parses a vlc code, faster then get_vlc()
860 * @param bits is the number of bits which will be read at once, must be
861 * identical to nb_bits in init_vlc()
862 * @param max_depth is the number of times bits bits must be read to completely
863 * read the longest vlc code
864 * = (max_vlc_length + bits - 1) / bits
866 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
867 int bits, int max_depth)
874 GET_VLC(code, re, s, table, bits, max_depth)
883 static inline void print_bin(int bits, int n){
886 for(i=n-1; i>=0; i--){
887 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
890 av_log(NULL, AV_LOG_DEBUG, " ");
893 static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
894 int r= get_bits(s, n);
897 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);
900 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
901 int show= show_bits(s, 24);
902 int pos= get_bits_count(s);
903 int r= get_vlc2(s, table, bits, max_depth);
904 int len= get_bits_count(s) - pos;
905 int bits2= show>>(24-len);
907 print_bin(bits2, len);
909 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
912 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
913 int show= show_bits(s, n);
914 int r= get_xbits(s, n);
917 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);
921 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
922 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
923 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
924 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
925 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
927 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
930 #define tprintf(p, ...) {}
933 static inline int decode012(GetBitContext *gb){
939 return get_bits1(gb) + 1;
942 static inline int decode210(GetBitContext *gb){
946 return 2 - get_bits1(gb);
949 #endif /* AVCODEC_BITSTREAM_H */