2 * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * bitstream api header.
29 #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
30 #define ALT_BITSTREAM_READER
33 //#define ALT_BITSTREAM_WRITER
34 //#define ALIGNED_BITSTREAM_WRITER
35 #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
37 # define A32_BITSTREAM_READER
39 #define ALT_BITSTREAM_READER
40 //#define LIBMPEG2_BITSTREAM_READER
41 //#define A32_BITSTREAM_READER
44 #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
46 extern const uint8_t ff_reverse[256];
48 #if defined(ARCH_X86) || defined(ARCH_X86_64)
49 // avoid +32 for shift optimization (gcc should do that ...)
50 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
51 asm ("sarl %1, %0\n\t"
53 : "ic" ((uint8_t)(-s))
57 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
58 asm ("shrl %1, %0\n\t"
60 : "ic" ((uint8_t)(-s))
65 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
66 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
71 /* buf and buf_end must be present and used by every alternative writer. */
72 typedef struct PutBitContext {
73 #ifdef ALT_BITSTREAM_WRITER
74 uint8_t *buf, *buf_end;
79 uint8_t *buf, *buf_ptr, *buf_end;
83 static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
91 s->buf_end = s->buf + buffer_size;
92 #ifdef ALT_BITSTREAM_WRITER
94 ((uint32_t*)(s->buf))[0]=0;
95 // memset(buffer, 0, buffer_size);
103 /* return the number of bits output */
104 static inline int put_bits_count(PutBitContext *s)
106 #ifdef ALT_BITSTREAM_WRITER
109 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
113 /* pad the end of the output stream with zeros */
114 static inline void flush_put_bits(PutBitContext *s)
116 #ifdef ALT_BITSTREAM_WRITER
119 s->bit_buf<<= s->bit_left;
120 while (s->bit_left < 32) {
121 /* XXX: should test end of buffer */
122 *s->buf_ptr++=s->bit_buf >> 24;
131 void align_put_bits(PutBitContext *s);
132 void ff_put_string(PutBitContext * pbc, char *s, int put_zero);
135 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
136 typedef struct GetBitContext {
137 const uint8_t *buffer, *buffer_end;
138 #ifdef ALT_BITSTREAM_READER
140 #elif defined LIBMPEG2_BITSTREAM_READER
144 #elif defined A32_BITSTREAM_READER
145 uint32_t *buffer_ptr;
153 #define VLC_TYPE int16_t
157 VLC_TYPE (*table)[2]; ///< code, bits
158 int table_size, table_allocated;
161 typedef struct RL_VLC_ELEM {
167 #if defined(ARCH_SPARC) || defined(ARCH_ARMV4L) || defined(ARCH_MIPS)
168 #define UNALIGNED_STORES_ARE_BAD
171 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
172 #if defined(ARCH_X86) || defined(ARCH_X86_64)
173 # define unaligned16(a) (*(const uint16_t*)(a))
174 # define unaligned32(a) (*(const uint32_t*)(a))
175 # define unaligned64(a) (*(const uint64_t*)(a))
178 # define unaligned(x) \
179 static inline uint##x##_t unaligned##x(const void *v) { \
182 } __attribute__((packed)); \
184 return ((const struct Unaligned *) v)->i; \
186 # elif defined(__DECC)
187 # define unaligned(x) \
188 static inline uint##x##_t unaligned##x##(const void *v) { \
189 return *(const __unaligned uint##x##_t *) v; \
192 # define unaligned(x) \
193 static inline uint##x##_t unaligned##x##(const void *v) { \
194 return *(const uint##x##_t *) v; \
203 #ifndef ALT_BITSTREAM_WRITER
204 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
206 unsigned int bit_buf;
209 // printf("put_bits=%d %x\n", n, value);
210 assert(n == 32 || value < (1U << n));
212 bit_buf = s->bit_buf;
213 bit_left = s->bit_left;
215 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
218 bit_buf = (bit_buf<<n) | value;
222 bit_buf |= value >> (n - bit_left);
223 #ifdef UNALIGNED_STORES_ARE_BAD
224 if (3 & (intptr_t) s->buf_ptr) {
225 s->buf_ptr[0] = bit_buf >> 24;
226 s->buf_ptr[1] = bit_buf >> 16;
227 s->buf_ptr[2] = bit_buf >> 8;
228 s->buf_ptr[3] = bit_buf ;
231 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
232 //printf("bitbuf = %08x\n", bit_buf);
238 s->bit_buf = bit_buf;
239 s->bit_left = bit_left;
244 #ifdef ALT_BITSTREAM_WRITER
245 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
247 # ifdef ALIGNED_BITSTREAM_WRITER
248 # if defined(ARCH_X86) || defined(ARCH_X86_64)
250 "movl %0, %%ecx \n\t"
251 "xorl %%eax, %%eax \n\t"
252 "shrdl %%cl, %1, %%eax \n\t"
254 "movl %0, %%ecx \n\t"
255 "shrl $3, %%ecx \n\t"
256 "andl $0xFFFFFFFC, %%ecx \n\t"
258 "orl %1, (%2, %%ecx) \n\t"
261 "movl %%eax, 4(%2, %%ecx) \n\t"
262 : "=&r" (s->index), "=&r" (value)
263 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
268 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
272 ptr[0] |= be2me_32(value>>(index&31));
273 ptr[1] = be2me_32(value<<(32-(index&31)));
274 //if(n>24) printf("%d %d\n", n, value);
278 # else //ALIGNED_BITSTREAM_WRITER
279 # if defined(ARCH_X86) || defined(ARCH_X86_64)
281 "movl $7, %%ecx \n\t"
282 "andl %0, %%ecx \n\t"
283 "addl %3, %%ecx \n\t"
287 "movl %0, %%ecx \n\t"
288 "shrl $3, %%ecx \n\t"
289 "orl %1, (%%ecx, %2) \n\t"
291 "movl $0, 4(%%ecx, %2) \n\t"
292 : "=&r" (s->index), "=&r" (value)
293 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
298 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
300 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
302 //if(n>24) printf("%d %d\n", n, value);
306 # endif //!ALIGNED_BITSTREAM_WRITER
311 static inline uint8_t* pbBufPtr(PutBitContext *s)
313 #ifdef ALT_BITSTREAM_WRITER
314 return s->buf + (s->index>>3);
322 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
324 static inline void skip_put_bytes(PutBitContext *s, int n){
325 assert((put_bits_count(s)&7)==0);
326 #ifdef ALT_BITSTREAM_WRITER
327 FIXME may need some cleaning of the buffer
330 assert(s->bit_left==32);
336 * skips the given number of bits.
337 * must only be used if the actual values in the bitstream dont matter
339 static inline void skip_put_bits(PutBitContext *s, int n){
340 #ifdef ALT_BITSTREAM_WRITER
344 s->buf_ptr-= s->bit_left>>5;
350 * Changes the end of the buffer.
352 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
353 s->buf_end= s->buf + size;
356 /* Bitstream reader API docs:
358 abritary name which is used as prefix for the internal variables
363 OPEN_READER(name, gb)
364 loads gb into local variables
366 CLOSE_READER(name, gb)
367 stores local vars in gb
369 UPDATE_CACHE(name, gb)
370 refills the internal cache from the bitstream
371 after this call at least MIN_CACHE_BITS will be available,
374 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
376 SHOW_UBITS(name, gb, num)
377 will return the next num bits
379 SHOW_SBITS(name, gb, num)
380 will return the next num bits and do sign extension
382 SKIP_BITS(name, gb, num)
383 will skip over the next num bits
384 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
386 SKIP_CACHE(name, gb, num)
387 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
389 SKIP_COUNTER(name, gb, num)
390 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
392 LAST_SKIP_CACHE(name, gb, num)
393 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
395 LAST_SKIP_BITS(name, gb, num)
396 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
398 for examples see get_bits, show_bits, skip_bits, get_vlc
401 static inline int unaligned32_be(const void *v)
405 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
407 return be2me_32( unaligned32(v)); //original
411 static inline int unaligned32_le(const void *v)
415 return (((p[3]<<8) | p[2])<<16) | (p[1]<<8) | (p[0]);
417 return le2me_32( unaligned32(v)); //original
421 #ifdef ALT_BITSTREAM_READER
422 # define MIN_CACHE_BITS 25
424 # define OPEN_READER(name, gb)\
425 int name##_index= (gb)->index;\
426 int name##_cache= 0;\
428 # define CLOSE_READER(name, gb)\
429 (gb)->index= name##_index;\
431 # ifdef ALT_BITSTREAM_READER_LE
432 # define UPDATE_CACHE(name, gb)\
433 name##_cache= unaligned32_le( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
435 # define SKIP_CACHE(name, gb, num)\
436 name##_cache >>= (num);
438 # define UPDATE_CACHE(name, gb)\
439 name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
441 # define SKIP_CACHE(name, gb, num)\
442 name##_cache <<= (num);
446 # define SKIP_COUNTER(name, gb, num)\
447 name##_index += (num);\
449 # define SKIP_BITS(name, gb, num)\
451 SKIP_CACHE(name, gb, num)\
452 SKIP_COUNTER(name, gb, num)\
455 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
456 # define LAST_SKIP_CACHE(name, gb, num) ;
458 # ifdef ALT_BITSTREAM_READER_LE
459 # define SHOW_UBITS(name, gb, num)\
460 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
462 # define SHOW_UBITS(name, gb, num)\
463 NEG_USR32(name##_cache, num)
466 # define SHOW_SBITS(name, gb, num)\
467 NEG_SSR32(name##_cache, num)
469 # define GET_CACHE(name, gb)\
470 ((uint32_t)name##_cache)
472 static inline int get_bits_count(GetBitContext *s){
476 static inline void skip_bits_long(GetBitContext *s, int n){
480 #elif defined LIBMPEG2_BITSTREAM_READER
481 //libmpeg2 like reader
483 # define MIN_CACHE_BITS 17
485 # define OPEN_READER(name, gb)\
486 int name##_bit_count=(gb)->bit_count;\
487 int name##_cache= (gb)->cache;\
488 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
490 # define CLOSE_READER(name, gb)\
491 (gb)->bit_count= name##_bit_count;\
492 (gb)->cache= name##_cache;\
493 (gb)->buffer_ptr= name##_buffer_ptr;\
495 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
497 # define UPDATE_CACHE(name, gb)\
498 if(name##_bit_count >= 0){\
499 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
500 name##_buffer_ptr += 2;\
501 name##_bit_count-= 16;\
506 # define UPDATE_CACHE(name, gb)\
507 if(name##_bit_count >= 0){\
508 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
509 name##_buffer_ptr+=2;\
510 name##_bit_count-= 16;\
515 # define SKIP_CACHE(name, gb, num)\
516 name##_cache <<= (num);\
518 # define SKIP_COUNTER(name, gb, num)\
519 name##_bit_count += (num);\
521 # define SKIP_BITS(name, gb, num)\
523 SKIP_CACHE(name, gb, num)\
524 SKIP_COUNTER(name, gb, num)\
527 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
528 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
530 # define SHOW_UBITS(name, gb, num)\
531 NEG_USR32(name##_cache, num)
533 # define SHOW_SBITS(name, gb, num)\
534 NEG_SSR32(name##_cache, num)
536 # define GET_CACHE(name, gb)\
537 ((uint32_t)name##_cache)
539 static inline int get_bits_count(GetBitContext *s){
540 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
543 static inline void skip_bits_long(GetBitContext *s, int n){
546 re_buffer_ptr += 2*(re_bit_count>>4);
548 re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
553 #elif defined A32_BITSTREAM_READER
555 # define MIN_CACHE_BITS 32
557 # define OPEN_READER(name, gb)\
558 int name##_bit_count=(gb)->bit_count;\
559 uint32_t name##_cache0= (gb)->cache0;\
560 uint32_t name##_cache1= (gb)->cache1;\
561 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
563 # define CLOSE_READER(name, gb)\
564 (gb)->bit_count= name##_bit_count;\
565 (gb)->cache0= name##_cache0;\
566 (gb)->cache1= name##_cache1;\
567 (gb)->buffer_ptr= name##_buffer_ptr;\
569 # define UPDATE_CACHE(name, gb)\
570 if(name##_bit_count > 0){\
571 const uint32_t next= be2me_32( *name##_buffer_ptr );\
572 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
573 name##_cache1 |= next<<name##_bit_count;\
574 name##_buffer_ptr++;\
575 name##_bit_count-= 32;\
578 #if defined(ARCH_X86) || defined(ARCH_X86_64)
579 # define SKIP_CACHE(name, gb, num)\
581 "shldl %2, %1, %0 \n\t"\
583 : "+r" (name##_cache0), "+r" (name##_cache1)\
584 : "Ic" ((uint8_t)(num))\
587 # define SKIP_CACHE(name, gb, num)\
588 name##_cache0 <<= (num);\
589 name##_cache0 |= NEG_USR32(name##_cache1,num);\
590 name##_cache1 <<= (num);
593 # define SKIP_COUNTER(name, gb, num)\
594 name##_bit_count += (num);\
596 # define SKIP_BITS(name, gb, num)\
598 SKIP_CACHE(name, gb, num)\
599 SKIP_COUNTER(name, gb, num)\
602 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
603 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
605 # define SHOW_UBITS(name, gb, num)\
606 NEG_USR32(name##_cache0, num)
608 # define SHOW_SBITS(name, gb, num)\
609 NEG_SSR32(name##_cache0, num)
611 # define GET_CACHE(name, gb)\
614 static inline int get_bits_count(GetBitContext *s){
615 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
618 static inline void skip_bits_long(GetBitContext *s, int n){
621 re_buffer_ptr += re_bit_count>>5;
623 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
632 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
633 * if MSB not set it is negative
634 * @param n length in bits
637 static inline int get_xbits(GetBitContext *s, int n){
639 register int32_t cache;
642 cache = GET_CACHE(re,s);
644 LAST_SKIP_BITS(re, s, n)
646 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
649 static inline int get_sbits(GetBitContext *s, int n){
653 tmp= SHOW_SBITS(re, s, n);
654 LAST_SKIP_BITS(re, s, n)
661 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
663 static inline unsigned int get_bits(GetBitContext *s, int n){
667 tmp= SHOW_UBITS(re, s, n);
668 LAST_SKIP_BITS(re, s, n)
675 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
677 static inline unsigned int show_bits(GetBitContext *s, int n){
681 tmp= SHOW_UBITS(re, s, n);
682 // CLOSE_READER(re, s)
686 static inline void skip_bits(GetBitContext *s, int n){
687 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
690 LAST_SKIP_BITS(re, s, n)
694 static inline unsigned int get_bits1(GetBitContext *s){
695 #ifdef ALT_BITSTREAM_READER
697 uint8_t result= s->buffer[ index>>3 ];
698 #ifdef ALT_BITSTREAM_READER_LE
699 result>>= (index&0x07);
702 result<<= (index&0x07);
710 return get_bits(s, 1);
714 static inline unsigned int show_bits1(GetBitContext *s){
715 return show_bits(s, 1);
718 static inline void skip_bits1(GetBitContext *s){
725 static inline unsigned int get_bits_long(GetBitContext *s, int n){
726 if(n<=17) return get_bits(s, n);
728 #ifdef ALT_BITSTREAM_READER_LE
729 int ret= get_bits(s, 16);
730 return ret | (get_bits(s, n-16) << 16);
732 int ret= get_bits(s, 16) << (n-16);
733 return ret | get_bits(s, n-16);
741 static inline unsigned int show_bits_long(GetBitContext *s, int n){
742 if(n<=17) return show_bits(s, n);
744 GetBitContext gb= *s;
745 int ret= get_bits_long(s, n);
751 static inline int check_marker(GetBitContext *s, const char *msg)
753 int bit= get_bits1(s);
755 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
761 * init GetBitContext.
762 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
763 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
764 * @param bit_size the size of the buffer in bits
766 static inline void init_get_bits(GetBitContext *s,
767 const uint8_t *buffer, int bit_size)
769 int buffer_size= (bit_size+7)>>3;
770 if(buffer_size < 0 || bit_size < 0) {
771 buffer_size = bit_size = 0;
776 s->size_in_bits= bit_size;
777 s->buffer_end= buffer + buffer_size;
778 #ifdef ALT_BITSTREAM_READER
780 #elif defined LIBMPEG2_BITSTREAM_READER
781 s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
782 s->bit_count = 16 + 8*((intptr_t)buffer&1);
783 skip_bits_long(s, 0);
784 #elif defined A32_BITSTREAM_READER
785 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
786 s->bit_count = 32 + 8*((intptr_t)buffer&3);
787 skip_bits_long(s, 0);
791 static inline void align_get_bits(GetBitContext *s)
793 int n= (-get_bits_count(s)) & 7;
794 if(n) skip_bits(s, n);
797 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
798 const void *bits, int bits_wrap, int bits_size,
799 const void *codes, int codes_wrap, int codes_size,
801 #define INIT_VLC_USE_STATIC 1
802 #define INIT_VLC_LE 2
803 void free_vlc(VLC *vlc);
807 * if the vlc code is invalid and max_depth=1 than no bits will be removed
808 * if the vlc code is invalid and max_depth>1 than the number of bits removed
811 #define GET_VLC(code, name, gb, table, bits, max_depth)\
813 int n, index, nb_bits;\
815 index= SHOW_UBITS(name, gb, bits);\
816 code = table[index][0];\
817 n = table[index][1];\
819 if(max_depth > 1 && n < 0){\
820 LAST_SKIP_BITS(name, gb, 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];\
828 if(max_depth > 2 && n < 0){\
829 LAST_SKIP_BITS(name, gb, nb_bits)\
830 UPDATE_CACHE(name, gb)\
834 index= SHOW_UBITS(name, gb, nb_bits) + code;\
835 code = table[index][0];\
836 n = table[index][1];\
839 SKIP_BITS(name, gb, n)\
842 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
844 int n, index, nb_bits;\
846 index= SHOW_UBITS(name, gb, bits);\
847 level = table[index].level;\
848 n = table[index].len;\
850 if(max_depth > 1 && n < 0){\
851 SKIP_BITS(name, gb, bits)\
853 UPDATE_CACHE(name, gb)\
858 index= SHOW_UBITS(name, gb, nb_bits) + level;\
859 level = table[index].level;\
860 n = table[index].len;\
862 run= table[index].run;\
863 SKIP_BITS(name, gb, n)\
868 * parses a vlc code, faster then get_vlc()
869 * @param bits is the number of bits which will be read at once, must be
870 * identical to nb_bits in init_vlc()
871 * @param max_depth is the number of times bits bits must be readed to completly
872 * read the longest vlc code
873 * = (max_vlc_length + bits - 1) / bits
875 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
876 int bits, int max_depth)
883 GET_VLC(code, re, s, table, bits, max_depth)
892 static inline void print_bin(int bits, int n){
895 for(i=n-1; i>=0; i--){
896 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
899 av_log(NULL, AV_LOG_DEBUG, " ");
902 static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
903 int r= get_bits(s, n);
906 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);
909 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
910 int show= show_bits(s, 24);
911 int pos= get_bits_count(s);
912 int r= get_vlc2(s, table, bits, max_depth);
913 int len= get_bits_count(s) - pos;
914 int bits2= show>>(24-len);
916 print_bin(bits2, len);
918 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
921 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
922 int show= show_bits(s, n);
923 int r= get_xbits(s, n);
926 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);
930 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
931 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
932 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
933 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
934 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
936 #define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
939 #define tprintf(...) {}
942 static inline int decode012(GetBitContext *gb){
948 return get_bits1(gb) + 1;
951 #endif /* BITSTREAM_H */