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);
162 void ff_put_string(PutBitContext * pbc, const char *s, int put_zero);
165 * Copies the content of \p src to the bitstream.
167 * @param length the number of bits of \p src to copy
169 void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
172 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
173 typedef struct GetBitContext {
174 const uint8_t *buffer, *buffer_end;
175 #ifdef ALT_BITSTREAM_READER
177 #elif defined LIBMPEG2_BITSTREAM_READER
181 #elif defined A32_BITSTREAM_READER
182 uint32_t *buffer_ptr;
190 #define VLC_TYPE int16_t
194 VLC_TYPE (*table)[2]; ///< code, bits
195 int table_size, table_allocated;
198 typedef struct RL_VLC_ELEM {
204 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
205 #ifndef ALT_BITSTREAM_WRITER
207 unsigned int bit_buf;
210 // printf("put_bits=%d %x\n", n, value);
211 assert(n == 32 || value < (1U << n));
213 bit_buf = s->bit_buf;
214 bit_left = s->bit_left;
216 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
218 #ifdef BITSTREAM_WRITER_LE
219 bit_buf |= value << (32 - bit_left);
221 #if !HAVE_FAST_UNALIGNED
222 if (3 & (intptr_t) s->buf_ptr) {
223 AV_WL32(s->buf_ptr, bit_buf);
226 *(uint32_t *)s->buf_ptr = le2me_32(bit_buf);
228 bit_buf = (bit_left==32)?0:value >> bit_left;
234 bit_buf = (bit_buf<<n) | value;
238 bit_buf |= value >> (n - bit_left);
239 #if !HAVE_FAST_UNALIGNED
240 if (3 & (intptr_t) s->buf_ptr) {
241 AV_WB32(s->buf_ptr, bit_buf);
244 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
245 //printf("bitbuf = %08x\n", bit_buf);
252 s->bit_buf = bit_buf;
253 s->bit_left = bit_left;
255 #else /* ALT_BITSTREAM_WRITER defined */
257 # ifdef ALIGNED_BITSTREAM_WRITER
260 "movl %0, %%ecx \n\t"
261 "xorl %%eax, %%eax \n\t"
262 "shrdl %%cl, %1, %%eax \n\t"
264 "movl %0, %%ecx \n\t"
265 "shrl $3, %%ecx \n\t"
266 "andl $0xFFFFFFFC, %%ecx \n\t"
268 "orl %1, (%2, %%ecx) \n\t"
271 "movl %%eax, 4(%2, %%ecx) \n\t"
272 : "=&r" (s->index), "=&r" (value)
273 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
278 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
282 ptr[0] |= be2me_32(value>>(index&31));
283 ptr[1] = be2me_32(value<<(32-(index&31)));
284 //if(n>24) printf("%d %d\n", n, value);
288 # else //ALIGNED_BITSTREAM_WRITER
291 "movl $7, %%ecx \n\t"
292 "andl %0, %%ecx \n\t"
293 "addl %3, %%ecx \n\t"
297 "movl %0, %%ecx \n\t"
298 "shrl $3, %%ecx \n\t"
299 "orl %1, (%%ecx, %2) \n\t"
301 "movl $0, 4(%%ecx, %2) \n\t"
302 : "=&r" (s->index), "=&r" (value)
303 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
308 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
310 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
312 //if(n>24) printf("%d %d\n", n, value);
316 # endif //!ALIGNED_BITSTREAM_WRITER
320 static inline void put_sbits(PutBitContext *pb, int bits, int32_t val)
322 assert(bits >= 0 && bits <= 31);
324 put_bits(pb, bits, val & ((1<<bits)-1));
328 static inline uint8_t* pbBufPtr(PutBitContext *s)
330 #ifdef ALT_BITSTREAM_WRITER
331 return s->buf + (s->index>>3);
338 * Skips the given number of bytes.
339 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
341 static inline void skip_put_bytes(PutBitContext *s, int n){
342 assert((put_bits_count(s)&7)==0);
343 #ifdef ALT_BITSTREAM_WRITER
344 FIXME may need some cleaning of the buffer
347 assert(s->bit_left==32);
353 * Skips the given number of bits.
354 * Must only be used if the actual values in the bitstream do not matter.
355 * If \p n is 0 the behavior is undefined.
357 static inline void skip_put_bits(PutBitContext *s, int n){
358 #ifdef ALT_BITSTREAM_WRITER
362 s->buf_ptr-= s->bit_left>>5;
368 * Changes the end of the buffer.
370 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
371 s->buf_end= s->buf + size;
374 /* Bitstream reader API docs:
376 arbitrary name which is used as prefix for the internal variables
381 OPEN_READER(name, gb)
382 loads gb into local variables
384 CLOSE_READER(name, gb)
385 stores local vars in gb
387 UPDATE_CACHE(name, gb)
388 refills the internal cache from the bitstream
389 after this call at least MIN_CACHE_BITS will be available,
392 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
394 SHOW_UBITS(name, gb, num)
395 will return the next num bits
397 SHOW_SBITS(name, gb, num)
398 will return the next num bits and do sign extension
400 SKIP_BITS(name, gb, num)
401 will skip over the next num bits
402 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
404 SKIP_CACHE(name, gb, num)
405 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
407 SKIP_COUNTER(name, gb, num)
408 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
410 LAST_SKIP_CACHE(name, gb, num)
411 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
413 LAST_SKIP_BITS(name, gb, num)
414 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
416 for examples see get_bits, show_bits, skip_bits, get_vlc
419 #ifdef ALT_BITSTREAM_READER
420 # define MIN_CACHE_BITS 25
422 # define OPEN_READER(name, gb)\
423 int name##_index= (gb)->index;\
424 int name##_cache= 0;\
426 # define CLOSE_READER(name, gb)\
427 (gb)->index= name##_index;\
429 # ifdef ALT_BITSTREAM_READER_LE
430 # define UPDATE_CACHE(name, gb)\
431 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
433 # define SKIP_CACHE(name, gb, num)\
434 name##_cache >>= (num);
436 # define UPDATE_CACHE(name, gb)\
437 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
439 # define SKIP_CACHE(name, gb, num)\
440 name##_cache <<= (num);
444 # define SKIP_COUNTER(name, gb, num)\
445 name##_index += (num);\
447 # define SKIP_BITS(name, gb, num)\
449 SKIP_CACHE(name, gb, num)\
450 SKIP_COUNTER(name, gb, num)\
453 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
454 # define LAST_SKIP_CACHE(name, gb, num) ;
456 # ifdef ALT_BITSTREAM_READER_LE
457 # define SHOW_UBITS(name, gb, num)\
458 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
460 # define SHOW_SBITS(name, gb, num)\
461 NEG_SSR32((name##_cache)<<(32-(num)), num)
463 # define SHOW_UBITS(name, gb, num)\
464 NEG_USR32(name##_cache, num)
466 # define SHOW_SBITS(name, gb, num)\
467 NEG_SSR32(name##_cache, num)
470 # define GET_CACHE(name, gb)\
471 ((uint32_t)name##_cache)
473 static inline int get_bits_count(GetBitContext *s){
477 static inline void skip_bits_long(GetBitContext *s, int n){
481 #elif defined LIBMPEG2_BITSTREAM_READER
482 //libmpeg2 like reader
484 # define MIN_CACHE_BITS 17
486 # define OPEN_READER(name, gb)\
487 int name##_bit_count=(gb)->bit_count;\
488 int name##_cache= (gb)->cache;\
489 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
491 # define CLOSE_READER(name, gb)\
492 (gb)->bit_count= name##_bit_count;\
493 (gb)->cache= name##_cache;\
494 (gb)->buffer_ptr= name##_buffer_ptr;\
496 # define UPDATE_CACHE(name, gb)\
497 if(name##_bit_count >= 0){\
498 name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
499 name##_buffer_ptr+=2;\
500 name##_bit_count-= 16;\
503 # define SKIP_CACHE(name, gb, num)\
504 name##_cache <<= (num);\
506 # define SKIP_COUNTER(name, gb, num)\
507 name##_bit_count += (num);\
509 # define SKIP_BITS(name, gb, num)\
511 SKIP_CACHE(name, gb, num)\
512 SKIP_COUNTER(name, gb, num)\
515 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
516 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
518 # define SHOW_UBITS(name, gb, num)\
519 NEG_USR32(name##_cache, num)
521 # define SHOW_SBITS(name, gb, num)\
522 NEG_SSR32(name##_cache, num)
524 # define GET_CACHE(name, gb)\
525 ((uint32_t)name##_cache)
527 static inline int get_bits_count(GetBitContext *s){
528 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
531 static inline void skip_bits_long(GetBitContext *s, int n){
534 re_buffer_ptr += 2*(re_bit_count>>4);
536 re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
541 #elif defined A32_BITSTREAM_READER
543 # define MIN_CACHE_BITS 32
545 # define OPEN_READER(name, gb)\
546 int name##_bit_count=(gb)->bit_count;\
547 uint32_t name##_cache0= (gb)->cache0;\
548 uint32_t name##_cache1= (gb)->cache1;\
549 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
551 # define CLOSE_READER(name, gb)\
552 (gb)->bit_count= name##_bit_count;\
553 (gb)->cache0= name##_cache0;\
554 (gb)->cache1= name##_cache1;\
555 (gb)->buffer_ptr= name##_buffer_ptr;\
557 # define UPDATE_CACHE(name, gb)\
558 if(name##_bit_count > 0){\
559 const uint32_t next= be2me_32( *name##_buffer_ptr );\
560 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
561 name##_cache1 |= next<<name##_bit_count;\
562 name##_buffer_ptr++;\
563 name##_bit_count-= 32;\
567 # define SKIP_CACHE(name, gb, num)\
569 "shldl %2, %1, %0 \n\t"\
571 : "+r" (name##_cache0), "+r" (name##_cache1)\
572 : "Ic" ((uint8_t)(num))\
575 # define SKIP_CACHE(name, gb, num)\
576 name##_cache0 <<= (num);\
577 name##_cache0 |= NEG_USR32(name##_cache1,num);\
578 name##_cache1 <<= (num);
581 # define SKIP_COUNTER(name, gb, num)\
582 name##_bit_count += (num);\
584 # define SKIP_BITS(name, gb, num)\
586 SKIP_CACHE(name, gb, num)\
587 SKIP_COUNTER(name, gb, num)\
590 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
591 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
593 # define SHOW_UBITS(name, gb, num)\
594 NEG_USR32(name##_cache0, num)
596 # define SHOW_SBITS(name, gb, num)\
597 NEG_SSR32(name##_cache0, num)
599 # define GET_CACHE(name, gb)\
602 static inline int get_bits_count(GetBitContext *s){
603 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
606 static inline void skip_bits_long(GetBitContext *s, int n){
609 re_buffer_ptr += re_bit_count>>5;
611 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
620 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
621 * if MSB not set it is negative
622 * @param n length in bits
625 static inline int get_xbits(GetBitContext *s, int n){
627 register int32_t cache;
630 cache = GET_CACHE(re,s);
632 LAST_SKIP_BITS(re, s, n)
634 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
637 static inline int get_sbits(GetBitContext *s, int n){
641 tmp= SHOW_SBITS(re, s, n);
642 LAST_SKIP_BITS(re, s, n)
649 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
651 static inline unsigned int get_bits(GetBitContext *s, int n){
655 tmp= SHOW_UBITS(re, s, n);
656 LAST_SKIP_BITS(re, s, n)
663 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
665 static inline unsigned int show_bits(GetBitContext *s, int n){
669 tmp= SHOW_UBITS(re, s, n);
670 // CLOSE_READER(re, s)
674 static inline void skip_bits(GetBitContext *s, int n){
675 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
678 LAST_SKIP_BITS(re, s, n)
682 static inline unsigned int get_bits1(GetBitContext *s){
683 #ifdef ALT_BITSTREAM_READER
685 uint8_t result= s->buffer[ index>>3 ];
686 #ifdef ALT_BITSTREAM_READER_LE
687 result>>= (index&0x07);
690 result<<= (index&0x07);
698 return get_bits(s, 1);
702 static inline unsigned int show_bits1(GetBitContext *s){
703 return show_bits(s, 1);
706 static inline void skip_bits1(GetBitContext *s){
713 static inline unsigned int get_bits_long(GetBitContext *s, int n){
714 if(n<=17) return get_bits(s, n);
716 #ifdef ALT_BITSTREAM_READER_LE
717 int ret= get_bits(s, 16);
718 return ret | (get_bits(s, n-16) << 16);
720 int ret= get_bits(s, 16) << (n-16);
721 return ret | get_bits(s, n-16);
727 * reads 0-32 bits as a signed integer.
729 static inline int get_sbits_long(GetBitContext *s, int n) {
730 return sign_extend(get_bits_long(s, n), n);
736 static inline unsigned int show_bits_long(GetBitContext *s, int n){
737 if(n<=17) return show_bits(s, n);
739 GetBitContext gb= *s;
740 return get_bits_long(&gb, n);
744 static inline int check_marker(GetBitContext *s, const char *msg)
746 int bit= get_bits1(s);
748 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
754 * init GetBitContext.
755 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
756 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
757 * @param bit_size the size of the buffer in bits
759 static inline void init_get_bits(GetBitContext *s,
760 const uint8_t *buffer, int bit_size)
762 int buffer_size= (bit_size+7)>>3;
763 if(buffer_size < 0 || bit_size < 0) {
764 buffer_size = bit_size = 0;
769 s->size_in_bits= bit_size;
770 s->buffer_end= buffer + buffer_size;
771 #ifdef ALT_BITSTREAM_READER
773 #elif defined LIBMPEG2_BITSTREAM_READER
774 s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
775 s->bit_count = 16 + 8*((intptr_t)buffer&1);
776 skip_bits_long(s, 0);
777 #elif defined A32_BITSTREAM_READER
778 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
779 s->bit_count = 32 + 8*((intptr_t)buffer&3);
780 skip_bits_long(s, 0);
784 static inline void align_get_bits(GetBitContext *s)
786 int n= (-get_bits_count(s)) & 7;
787 if(n) skip_bits(s, n);
790 #define init_vlc(vlc, nb_bits, nb_codes,\
791 bits, bits_wrap, bits_size,\
792 codes, codes_wrap, codes_size,\
794 init_vlc_sparse(vlc, nb_bits, nb_codes,\
795 bits, bits_wrap, bits_size,\
796 codes, codes_wrap, codes_size,\
799 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
800 const void *bits, int bits_wrap, int bits_size,
801 const void *codes, int codes_wrap, int codes_size,
802 const void *symbols, int symbols_wrap, int symbols_size,
804 #define INIT_VLC_USE_STATIC 1 ///< VERY strongly deprecated and forbidden
805 #define INIT_VLC_LE 2
806 #define INIT_VLC_USE_NEW_STATIC 4
807 void free_vlc(VLC *vlc);
809 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
811 static VLC_TYPE table[static_size][2];\
812 (vlc)->table= table;\
813 (vlc)->table_allocated= static_size;\
814 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
820 * if the vlc code is invalid and max_depth=1 than no bits will be removed
821 * if the vlc code is invalid and max_depth>1 than the number of bits removed
824 #define GET_VLC(code, name, gb, table, bits, max_depth)\
826 int n, index, nb_bits;\
828 index= SHOW_UBITS(name, gb, bits);\
829 code = table[index][0];\
830 n = table[index][1];\
832 if(max_depth > 1 && n < 0){\
833 LAST_SKIP_BITS(name, gb, bits)\
834 UPDATE_CACHE(name, gb)\
838 index= SHOW_UBITS(name, gb, nb_bits) + code;\
839 code = table[index][0];\
840 n = table[index][1];\
841 if(max_depth > 2 && n < 0){\
842 LAST_SKIP_BITS(name, gb, nb_bits)\
843 UPDATE_CACHE(name, gb)\
847 index= SHOW_UBITS(name, gb, nb_bits) + code;\
848 code = table[index][0];\
849 n = table[index][1];\
852 SKIP_BITS(name, gb, n)\
855 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
857 int n, index, nb_bits;\
859 index= SHOW_UBITS(name, gb, bits);\
860 level = table[index].level;\
861 n = table[index].len;\
863 if(max_depth > 1 && n < 0){\
864 SKIP_BITS(name, gb, bits)\
866 UPDATE_CACHE(name, gb)\
871 index= SHOW_UBITS(name, gb, nb_bits) + level;\
872 level = table[index].level;\
873 n = table[index].len;\
875 run= table[index].run;\
876 SKIP_BITS(name, gb, n)\
881 * parses a vlc code, faster then get_vlc()
882 * @param bits is the number of bits which will be read at once, must be
883 * identical to nb_bits in init_vlc()
884 * @param max_depth is the number of times bits bits must be read to completely
885 * read the longest vlc code
886 * = (max_vlc_length + bits - 1) / bits
888 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
889 int bits, int max_depth)
896 GET_VLC(code, re, s, table, bits, max_depth)
905 static inline void print_bin(int bits, int n){
908 for(i=n-1; i>=0; i--){
909 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
912 av_log(NULL, AV_LOG_DEBUG, " ");
915 static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
916 int r= get_bits(s, n);
919 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);
922 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
923 int show= show_bits(s, 24);
924 int pos= get_bits_count(s);
925 int r= get_vlc2(s, table, bits, max_depth);
926 int len= get_bits_count(s) - pos;
927 int bits2= show>>(24-len);
929 print_bin(bits2, len);
931 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
934 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
935 int show= show_bits(s, n);
936 int r= get_xbits(s, n);
939 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);
943 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
944 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
945 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
946 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
947 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
949 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
952 #define tprintf(p, ...) {}
955 static inline int decode012(GetBitContext *gb){
961 return get_bits1(gb) + 1;
964 static inline int decode210(GetBitContext *gb){
968 return 2 - get_bits1(gb);
971 #endif /* AVCODEC_BITSTREAM_H */