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 api header.
29 #define av_always_inline inline
30 #define attribute_deprecated
36 #define CONFIG_ALIGN 1
39 #ifdef ROCKBOX_BIG_ENDIAN
40 #define WORDS_BIGENDIAN
45 extern const uint8_t ff_log2_tab[256];
48 /*misc utility functions added to make it compile */
49 static inline int av_log2(unsigned int v)
67 #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
68 #define ALT_BITSTREAM_READER
71 //#define ALT_BITSTREAM_WRITER
72 //#define ALIGNED_BITSTREAM_WRITER
73 #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
75 # define A32_BITSTREAM_READER
77 #define ALT_BITSTREAM_READER
78 //#define LIBMPEG2_BITSTREAM_READER
79 //#define A32_BITSTREAM_READER
82 #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
84 extern const uint8_t ff_reverse[256];
87 // avoid +32 for shift optimization (gcc should do that ...)
88 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
89 asm ("sarl %1, %0\n\t"
91 : "ic" ((uint8_t)(-s))
95 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
96 asm ("shrl %1, %0\n\t"
98 : "ic" ((uint8_t)(-s))
103 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
104 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
109 /* buf and buf_end must be present and used by every alternative writer. */
110 typedef struct PutBitContext {
111 #ifdef ALT_BITSTREAM_WRITER
112 uint8_t *buf, *buf_end;
117 uint8_t *buf, *buf_ptr, *buf_end;
121 static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
123 if(buffer_size < 0) {
129 s->buf_end = s->buf + buffer_size;
130 #ifdef ALT_BITSTREAM_WRITER
132 ((uint32_t*)(s->buf))[0]=0;
133 // memset(buffer, 0, buffer_size);
141 /* return the number of bits output */
142 static inline int put_bits_count(PutBitContext *s)
144 #ifdef ALT_BITSTREAM_WRITER
147 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
151 /* pad the end of the output stream with zeros */
152 static inline void flush_put_bits(PutBitContext *s)
154 #ifdef ALT_BITSTREAM_WRITER
157 s->bit_buf<<= s->bit_left;
158 while (s->bit_left < 32) {
159 /* XXX: should test end of buffer */
160 *s->buf_ptr++=s->bit_buf >> 24;
169 void align_put_bits(PutBitContext *s);
170 void ff_put_string(PutBitContext * pbc, char *s, int put_zero);
173 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
174 typedef struct GetBitContext {
175 const uint8_t *buffer, *buffer_end;
176 #ifdef ALT_BITSTREAM_READER
178 #elif defined LIBMPEG2_BITSTREAM_READER
182 #elif defined A32_BITSTREAM_READER
183 uint32_t *buffer_ptr;
191 #define VLC_TYPE int16_t
195 VLC_TYPE (*table)[2]; /* code, bits */
196 int table_size, table_allocated;
199 typedef struct RL_VLC_ELEM {
205 #if defined(ARCH_SPARC) || defined(ARCH_ARMV4L) || defined(ARCH_MIPS) || defined(ARCH_BFIN)
206 #define UNALIGNED_STORES_ARE_BAD
209 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
210 #if defined(ARCH_X86) || defined(CPU_COLDFIRE)
211 # define unaligned16(a) (*(const uint16_t*)(a))
212 # define unaligned32(a) (*(const uint32_t*)(a))
213 # define unaligned64(a) (*(const uint64_t*)(a))
216 # define unaligned(x) \
217 static inline uint##x##_t unaligned##x(const void *v) { \
220 } __attribute__((packed)); \
222 return ((const struct Unaligned *) v)->i; \
224 # elif defined(__DECC)
225 # define unaligned(x) \
226 static inline uint##x##_t unaligned##x(const void *v) { \
227 return *(const __unaligned uint##x##_t *) v; \
230 # define unaligned(x) \
231 static inline uint##x##_t unaligned##x(const void *v) { \
232 return *(const uint##x##_t *) v; \
239 #endif /* defined(ARCH_X86) */
241 #ifndef ALT_BITSTREAM_WRITER
242 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
244 unsigned int bit_buf;
247 // printf("put_bits=%d %x\n", n, value);
248 // assert(n == 32 || value < (1U << n));
249 bit_buf = s->bit_buf;
250 bit_left = s->bit_left;
252 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
255 bit_buf = (bit_buf<<n) | value;
259 bit_buf |= value >> (n - bit_left);
260 #ifdef UNALIGNED_STORES_ARE_BAD
261 if (3 & (intptr_t) s->buf_ptr) {
262 s->buf_ptr[0] = bit_buf >> 24;
263 s->buf_ptr[1] = bit_buf >> 16;
264 s->buf_ptr[2] = bit_buf >> 8;
265 s->buf_ptr[3] = bit_buf ;
268 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
269 //printf("bitbuf = %08x\n", bit_buf);
275 s->bit_buf = bit_buf;
276 s->bit_left = bit_left;
280 #ifdef ALT_BITSTREAM_WRITER
281 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
283 # ifdef ALIGNED_BITSTREAM_WRITER
284 # if defined(ARCH_X86)
286 "movl %0, %%ecx \n\t"
287 "xorl %%eax, %%eax \n\t"
288 "shrdl %%cl, %1, %%eax \n\t"
290 "movl %0, %%ecx \n\t"
291 "shrl $3, %%ecx \n\t"
292 "andl $0xFFFFFFFC, %%ecx \n\t"
294 "orl %1, (%2, %%ecx) \n\t"
297 "movl %%eax, 4(%2, %%ecx) \n\t"
298 : "=&r" (s->index), "=&r" (value)
299 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
304 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
308 ptr[0] |= be2me_32(value>>(index&31));
309 ptr[1] = be2me_32(value<<(32-(index&31)));
310 //if(n>24) printf("%d %d\n", n, value);
314 # else //ALIGNED_BITSTREAM_WRITER
315 # if defined(ARCH_X86)
317 "movl $7, %%ecx \n\t"
318 "andl %0, %%ecx \n\t"
319 "addl %3, %%ecx \n\t"
323 "movl %0, %%ecx \n\t"
324 "shrl $3, %%ecx \n\t"
325 "orl %1, (%%ecx, %2) \n\t"
327 "movl $0, 4(%%ecx, %2) \n\t"
328 : "=&r" (s->index), "=&r" (value)
329 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
334 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
336 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
338 //if(n>24) printf("%d %d\n", n, value);
342 # endif //!ALIGNED_BITSTREAM_WRITER
346 static inline uint8_t* pbBufPtr(PutBitContext *s)
348 #ifdef ALT_BITSTREAM_WRITER
349 return s->buf + (s->index>>3);
357 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
359 static inline void skip_put_bytes(PutBitContext *s, int n){
360 // assert((put_bits_count(s)&7)==0);
361 #ifdef ALT_BITSTREAM_WRITER
362 FIXME may need some cleaning of the buffer
365 // assert(s->bit_left==32);
371 * skips the given number of bits.
372 * must only be used if the actual values in the bitstream dont matter
374 static inline void skip_put_bits(PutBitContext *s, int n){
375 #ifdef ALT_BITSTREAM_WRITER
379 s->buf_ptr-= s->bit_left>>5;
385 * Changes the end of the buffer.
387 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
388 s->buf_end= s->buf + size;
391 /* Bitstream reader API docs:
393 abritary name which is used as prefix for the internal variables
398 OPEN_READER(name, gb)
399 loads gb into local variables
401 CLOSE_READER(name, gb)
402 stores local vars in gb
404 UPDATE_CACHE(name, gb)
405 refills the internal cache from the bitstream
406 after this call at least MIN_CACHE_BITS will be available,
409 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
411 SHOW_UBITS(name, gb, num)
412 will return the next num bits
414 SHOW_SBITS(name, gb, num)
415 will return the next num bits and do sign extension
417 SKIP_BITS(name, gb, num)
418 will skip over the next num bits
419 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
421 SKIP_CACHE(name, gb, num)
422 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
424 SKIP_COUNTER(name, gb, num)
425 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
427 LAST_SKIP_CACHE(name, gb, num)
428 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
430 LAST_SKIP_BITS(name, gb, num)
431 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
433 for examples see get_bits, show_bits, skip_bits, get_vlc
436 static inline int unaligned32_be(const void *v)
440 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
442 return be2me_32( unaligned32(v)); //original
446 static inline int unaligned32_le(const void *v)
450 return (((p[3]<<8) | p[2])<<16) | (p[1]<<8) | (p[0]);
452 return le2me_32( unaligned32(v)); //original
456 #ifdef ALT_BITSTREAM_READER
457 # define MIN_CACHE_BITS 25
459 # define OPEN_READER(name, gb)\
460 int name##_index= (gb)->index;\
461 int name##_cache= 0;\
463 # define CLOSE_READER(name, gb)\
464 (gb)->index= name##_index;\
466 # ifdef ALT_BITSTREAM_READER_LE
467 # define UPDATE_CACHE(name, gb)\
468 name##_cache= unaligned32_le( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
470 # define SKIP_CACHE(name, gb, num)\
471 name##_cache >>= (num);
473 # define UPDATE_CACHE(name, gb)\
474 name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
476 # define SKIP_CACHE(name, gb, num)\
477 name##_cache <<= (num);
481 # define SKIP_COUNTER(name, gb, num)\
482 name##_index += (num);\
484 # define SKIP_BITS(name, gb, num)\
486 SKIP_CACHE(name, gb, num)\
487 SKIP_COUNTER(name, gb, num)\
490 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
491 # define LAST_SKIP_CACHE(name, gb, num) ;
493 # ifdef ALT_BITSTREAM_READER_LE
494 # define SHOW_UBITS(name, gb, num)\
495 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
497 # define SHOW_SBITS(name, gb, num)\
498 NEG_SSR32((name##_cache)<<(32-(num)), num)
500 # define SHOW_UBITS(name, gb, num)\
501 NEG_USR32(name##_cache, num)
503 # define SHOW_SBITS(name, gb, num)\
504 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){
514 static inline void skip_bits_long(GetBitContext *s, int n){
518 #elif defined LIBMPEG2_BITSTREAM_READER
519 //libmpeg2 like reader
521 # define MIN_CACHE_BITS 17
523 # define OPEN_READER(name, gb)\
524 int name##_bit_count=(gb)->bit_count;\
525 int name##_cache= (gb)->cache;\
526 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
528 # define CLOSE_READER(name, gb)\
529 (gb)->bit_count= name##_bit_count;\
530 (gb)->cache= name##_cache;\
531 (gb)->buffer_ptr= name##_buffer_ptr;\
533 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
535 # define UPDATE_CACHE(name, gb)\
536 if(name##_bit_count >= 0){\
537 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
538 name##_buffer_ptr += 2;\
539 name##_bit_count-= 16;\
544 # define UPDATE_CACHE(name, gb)\
545 if(name##_bit_count >= 0){\
546 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
547 name##_buffer_ptr+=2;\
548 name##_bit_count-= 16;\
553 # define SKIP_CACHE(name, gb, num)\
554 name##_cache <<= (num);\
556 # define SKIP_COUNTER(name, gb, num)\
557 name##_bit_count += (num);\
559 # define SKIP_BITS(name, gb, num)\
561 SKIP_CACHE(name, gb, num)\
562 SKIP_COUNTER(name, gb, num)\
565 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
566 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
568 # define SHOW_UBITS(name, gb, num)\
569 NEG_USR32(name##_cache, num)
571 # define SHOW_SBITS(name, gb, num)\
572 NEG_SSR32(name##_cache, num)
574 # define GET_CACHE(name, gb)\
575 ((uint32_t)name##_cache)
577 static inline int get_bits_count(GetBitContext *s){
578 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
581 static inline void skip_bits_long(GetBitContext *s, int n){
584 re_buffer_ptr += 2*(re_bit_count>>4);
586 re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
591 #elif defined A32_BITSTREAM_READER
593 # define MIN_CACHE_BITS 32
595 # define OPEN_READER(name, gb)\
596 int name##_bit_count=(gb)->bit_count;\
597 uint32_t name##_cache0= (gb)->cache0;\
598 uint32_t name##_cache1= (gb)->cache1;\
599 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
601 # define CLOSE_READER(name, gb)\
602 (gb)->bit_count= name##_bit_count;\
603 (gb)->cache0= name##_cache0;\
604 (gb)->cache1= name##_cache1;\
605 (gb)->buffer_ptr= name##_buffer_ptr;\
607 # define UPDATE_CACHE(name, gb)\
608 if(name##_bit_count > 0){\
609 const uint32_t next= be2me_32( *name##_buffer_ptr );\
610 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
611 name##_cache1 |= next<<name##_bit_count;\
612 name##_buffer_ptr++;\
613 name##_bit_count-= 32;\
616 #if defined(ARCH_X86)
617 # define SKIP_CACHE(name, gb, num)\
619 "shldl %2, %1, %0 \n\t"\
621 : "+r" (name##_cache0), "+r" (name##_cache1)\
622 : "Ic" ((uint8_t)(num))\
625 # define SKIP_CACHE(name, gb, num)\
626 name##_cache0 <<= (num);\
627 name##_cache0 |= NEG_USR32(name##_cache1,num);\
628 name##_cache1 <<= (num);
631 # define SKIP_COUNTER(name, gb, num)\
632 name##_bit_count += (num);\
634 # define SKIP_BITS(name, gb, num)\
636 SKIP_CACHE(name, gb, num)\
637 SKIP_COUNTER(name, gb, num)\
640 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
641 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
643 # define SHOW_UBITS(name, gb, num)\
644 NEG_USR32(name##_cache0, num)
646 # define SHOW_SBITS(name, gb, num)\
647 NEG_SSR32(name##_cache0, num)
649 # define GET_CACHE(name, gb)\
652 static inline int get_bits_count(GetBitContext *s){
653 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
656 static inline void skip_bits_long(GetBitContext *s, int n){
659 re_buffer_ptr += re_bit_count>>5;
661 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
670 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
671 * if MSB not set it is negative
672 * @param n length in bits
675 static inline int get_xbits(GetBitContext *s, int n){
677 register int32_t cache;
680 cache = GET_CACHE(re,s);
682 LAST_SKIP_BITS(re, s, n)
684 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
687 static inline int get_sbits(GetBitContext *s, int n){
691 tmp= SHOW_SBITS(re, s, n);
692 LAST_SKIP_BITS(re, s, n)
699 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
701 static inline unsigned int get_bits(GetBitContext *s, int n){
705 tmp= SHOW_UBITS(re, s, n);
706 LAST_SKIP_BITS(re, s, n)
713 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
715 static inline unsigned int show_bits(GetBitContext *s, int n){
719 tmp= SHOW_UBITS(re, s, n);
720 // CLOSE_READER(re, s)
724 static inline void skip_bits(GetBitContext *s, int n){
725 /* Note: gcc seems to optimize this to s->index+=n for the ALT_READER :)) */
728 LAST_SKIP_BITS(re, s, n)
732 static inline unsigned int get_bits1(GetBitContext *s){
733 #ifdef ALT_BITSTREAM_READER
735 uint8_t result= s->buffer[ index>>3 ];
736 #ifdef ALT_BITSTREAM_READER_LE
737 result>>= (index&0x07);
740 result<<= (index&0x07);
748 return get_bits(s, 1);
752 static inline unsigned int show_bits1(GetBitContext *s){
753 return show_bits(s, 1);
756 static inline void skip_bits1(GetBitContext *s){
763 static inline unsigned int get_bits_long(GetBitContext *s, int n){
764 if(n<=17) return get_bits(s, n);
766 #ifdef ALT_BITSTREAM_READER_LE
767 int ret= get_bits(s, 16);
768 return ret | (get_bits(s, n-16) << 16);
770 int ret= get_bits(s, 16) << (n-16);
771 return ret | get_bits(s, n-16);
779 static inline unsigned int show_bits_long(GetBitContext *s, int n){
780 if(n<=17) return show_bits(s, n);
782 GetBitContext gb= *s;
783 int ret= get_bits_long(s, n);
790 static inline int check_marker(GetBitContext *s, const char *msg)
792 int bit= get_bits1(s);
794 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
801 * init GetBitContext.
802 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
803 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
804 * @param bit_size the size of the buffer in bits
806 static inline void init_get_bits(GetBitContext *s,
807 const uint8_t *buffer, int bit_size)
809 int buffer_size= (bit_size+7)>>3;
810 if(buffer_size < 0 || bit_size < 0) {
811 buffer_size = bit_size = 0;
816 s->size_in_bits= bit_size;
817 s->buffer_end= buffer + buffer_size;
818 #ifdef ALT_BITSTREAM_READER
820 #elif defined LIBMPEG2_BITSTREAM_READER
821 s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
822 s->bit_count = 16 + 8*((intptr_t)buffer&1);
823 skip_bits_long(s, 0);
824 #elif defined A32_BITSTREAM_READER
825 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
826 s->bit_count = 32 + 8*((intptr_t)buffer&3);
827 skip_bits_long(s, 0);
831 static inline void align_get_bits(GetBitContext *s)
833 int n= (-get_bits_count(s)) & 7;
834 if(n) skip_bits(s, n);
837 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
838 const void *bits, int bits_wrap, int bits_size,
839 const void *codes, int codes_wrap, int codes_size,
841 #define INIT_VLC_USE_STATIC 1
842 #define INIT_VLC_LE 2
843 void free_vlc(VLC *vlc);
847 * if the vlc code is invalid and max_depth=1 than no bits will be removed
848 * if the vlc code is invalid and max_depth>1 than the number of bits removed
851 #define GET_VLC(code, name, gb, table, bits, max_depth)\
853 int n, index, nb_bits;\
855 index= SHOW_UBITS(name, gb, bits);\
856 code = table[index][0];\
857 n = table[index][1];\
859 if(max_depth > 1 && n < 0){\
860 LAST_SKIP_BITS(name, gb, bits)\
861 UPDATE_CACHE(name, gb)\
865 index= SHOW_UBITS(name, gb, nb_bits) + code;\
866 code = table[index][0];\
867 n = table[index][1];\
868 if(max_depth > 2 && n < 0){\
869 LAST_SKIP_BITS(name, gb, nb_bits)\
870 UPDATE_CACHE(name, gb)\
874 index= SHOW_UBITS(name, gb, nb_bits) + code;\
875 code = table[index][0];\
876 n = table[index][1];\
879 SKIP_BITS(name, gb, n)\
882 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
884 int n, index, nb_bits;\
886 index= SHOW_UBITS(name, gb, bits);\
887 level = table[index].level;\
888 n = table[index].len;\
890 if(max_depth > 1 && n < 0){\
891 SKIP_BITS(name, gb, bits)\
893 UPDATE_CACHE(name, gb)\
898 index= SHOW_UBITS(name, gb, nb_bits) + level;\
899 level = table[index].level;\
900 n = table[index].len;\
902 run= table[index].run;\
903 SKIP_BITS(name, gb, n)\
908 * parses a vlc code, faster then get_vlc()
909 * @param bits is the number of bits which will be read at once, must be
910 * identical to nb_bits in init_vlc()
911 * @param max_depth is the number of times bits bits must be read to completely
912 * read the longest vlc code
913 * = (max_vlc_length + bits - 1) / bits
915 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
916 int bits, int max_depth)
923 GET_VLC(code, re, s, table, bits, max_depth)
930 static inline void print_bin(int bits, int n){
933 for(i=n-1; i>=0; i--){
934 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
937 av_log(NULL, AV_LOG_DEBUG, " ");
940 static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
941 int r= get_bits(s, n);
944 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);
948 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
949 int show= show_bits(s, 24);
950 int pos= get_bits_count(s);
951 int r= get_vlc2(s, table, bits, max_depth);
952 int len= get_bits_count(s) - pos;
953 int bits2= show>>(24-len);
955 print_bin(bits2, len);
957 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
961 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
962 int show= show_bits(s, n);
963 int r= get_xbits(s, n);
966 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);
970 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
971 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
972 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
973 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
974 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
976 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
979 #define tprintf(p, ...) {}
982 static inline int decode012(GetBitContext *gb){
988 return get_bits1(gb) + 1;
991 #endif /* BITSTREAM_H */