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.
31 #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
32 #define ALT_BITSTREAM_READER
35 //#define ALT_BITSTREAM_WRITER
36 //#define ALIGNED_BITSTREAM_WRITER
37 #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
39 # define A32_BITSTREAM_READER
41 #define ALT_BITSTREAM_READER
42 //#define LIBMPEG2_BITSTREAM_READER
43 //#define A32_BITSTREAM_READER
46 #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
48 extern const uint8_t ff_reverse
[256];
50 #if defined(ARCH_X86) || defined(ARCH_X86_64)
51 // avoid +32 for shift optimization (gcc should do that ...)
52 static inline int32_t NEG_SSR32( int32_t a
, int8_t s
){
53 asm ("sarl %1, %0\n\t"
55 : "ic" ((uint8_t)(-s
))
59 static inline uint32_t NEG_USR32(uint32_t a
, int8_t s
){
60 asm ("shrl %1, %0\n\t"
62 : "ic" ((uint8_t)(-s
))
67 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
68 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
73 /* buf and buf_end must be present and used by every alternative writer. */
74 typedef struct PutBitContext
{
75 #ifdef ALT_BITSTREAM_WRITER
76 uint8_t *buf
, *buf_end
;
81 uint8_t *buf
, *buf_ptr
, *buf_end
;
85 static inline void init_put_bits(PutBitContext
*s
, uint8_t *buffer
, int buffer_size
)
93 s
->buf_end
= s
->buf
+ buffer_size
;
94 #ifdef ALT_BITSTREAM_WRITER
96 ((uint32_t*)(s
->buf
))[0]=0;
97 // memset(buffer, 0, buffer_size);
105 /* return the number of bits output */
106 static inline int put_bits_count(PutBitContext
*s
)
108 #ifdef ALT_BITSTREAM_WRITER
111 return (s
->buf_ptr
- s
->buf
) * 8 + 32 - s
->bit_left
;
115 /* pad the end of the output stream with zeros */
116 static inline void flush_put_bits(PutBitContext
*s
)
118 #ifdef ALT_BITSTREAM_WRITER
121 s
->bit_buf
<<= s
->bit_left
;
122 while (s
->bit_left
< 32) {
123 /* XXX: should test end of buffer */
124 *s
->buf_ptr
++=s
->bit_buf
>> 24;
133 void align_put_bits(PutBitContext
*s
);
134 void ff_put_string(PutBitContext
* pbc
, char *s
, int put_zero
);
137 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
138 typedef struct GetBitContext
{
139 const uint8_t *buffer
, *buffer_end
;
140 #ifdef ALT_BITSTREAM_READER
142 #elif defined LIBMPEG2_BITSTREAM_READER
146 #elif defined A32_BITSTREAM_READER
147 uint32_t *buffer_ptr
;
155 #define VLC_TYPE int16_t
159 VLC_TYPE (*table
)[2]; ///< code, bits
160 int table_size
, table_allocated
;
163 typedef struct RL_VLC_ELEM
{
169 #if defined(ARCH_SPARC) || defined(ARCH_ARMV4L) || defined(ARCH_MIPS)
170 #define UNALIGNED_STORES_ARE_BAD
173 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
174 #if defined(ARCH_X86) || defined(ARCH_X86_64)
175 # define unaligned16(a) (*(const uint16_t*)(a))
176 # define unaligned32(a) (*(const uint32_t*)(a))
177 # define unaligned64(a) (*(const uint64_t*)(a))
180 # define unaligned(x) \
181 static inline uint##x##_t unaligned##x(const void *v) { \
184 } __attribute__((packed)); \
186 return ((const struct Unaligned *) v)->i; \
188 # elif defined(__DECC)
189 # define unaligned(x) \
190 static inline uint##x##_t unaligned##x##(const void *v) { \
191 return *(const __unaligned uint##x##_t *) v; \
194 # define unaligned(x) \
195 static inline uint##x##_t unaligned##x##(const void *v) { \
196 return *(const uint##x##_t *) v; \
203 #endif /* defined(ARCH_X86) || defined(ARCH_X86_64) */
205 #ifndef ALT_BITSTREAM_WRITER
206 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
208 unsigned int bit_buf
;
211 // printf("put_bits=%d %x\n", n, value);
212 assert(n
== 32 || value
< (1U << n
));
214 bit_buf
= s
->bit_buf
;
215 bit_left
= s
->bit_left
;
217 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
220 bit_buf
= (bit_buf
<<n
) | value
;
224 bit_buf
|= value
>> (n
- bit_left
);
225 #ifdef UNALIGNED_STORES_ARE_BAD
226 if (3 & (intptr_t) s
->buf_ptr
) {
227 s
->buf_ptr
[0] = bit_buf
>> 24;
228 s
->buf_ptr
[1] = bit_buf
>> 16;
229 s
->buf_ptr
[2] = bit_buf
>> 8;
230 s
->buf_ptr
[3] = bit_buf
;
233 *(uint32_t *)s
->buf_ptr
= be2me_32(bit_buf
);
234 //printf("bitbuf = %08x\n", bit_buf);
240 s
->bit_buf
= bit_buf
;
241 s
->bit_left
= bit_left
;
246 #ifdef ALT_BITSTREAM_WRITER
247 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
249 # ifdef ALIGNED_BITSTREAM_WRITER
250 # if defined(ARCH_X86) || defined(ARCH_X86_64)
252 "movl %0, %%ecx \n\t"
253 "xorl %%eax, %%eax \n\t"
254 "shrdl %%cl, %1, %%eax \n\t"
256 "movl %0, %%ecx \n\t"
257 "shrl $3, %%ecx \n\t"
258 "andl $0xFFFFFFFC, %%ecx \n\t"
260 "orl %1, (%2, %%ecx) \n\t"
263 "movl %%eax, 4(%2, %%ecx) \n\t"
264 : "=&r" (s
->index
), "=&r" (value
)
265 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
<<(-n
))
270 uint32_t *ptr
= ((uint32_t *)s
->buf
)+(index
>>5);
274 ptr
[0] |= be2me_32(value
>>(index
&31));
275 ptr
[1] = be2me_32(value
<<(32-(index
&31)));
276 //if(n>24) printf("%d %d\n", n, value);
280 # else //ALIGNED_BITSTREAM_WRITER
281 # if defined(ARCH_X86) || defined(ARCH_X86_64)
283 "movl $7, %%ecx \n\t"
284 "andl %0, %%ecx \n\t"
285 "addl %3, %%ecx \n\t"
289 "movl %0, %%ecx \n\t"
290 "shrl $3, %%ecx \n\t"
291 "orl %1, (%%ecx, %2) \n\t"
293 "movl $0, 4(%%ecx, %2) \n\t"
294 : "=&r" (s
->index
), "=&r" (value
)
295 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
)
300 uint32_t *ptr
= (uint32_t*)(((uint8_t *)s
->buf
)+(index
>>3));
302 ptr
[0] |= be2me_32(value
<<(32-n
-(index
&7) ));
304 //if(n>24) printf("%d %d\n", n, value);
308 # endif //!ALIGNED_BITSTREAM_WRITER
313 static inline uint8_t* pbBufPtr(PutBitContext
*s
)
315 #ifdef ALT_BITSTREAM_WRITER
316 return s
->buf
+ (s
->index
>>3);
324 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
326 static inline void skip_put_bytes(PutBitContext
*s
, int n
){
327 assert((put_bits_count(s
)&7)==0);
328 #ifdef ALT_BITSTREAM_WRITER
329 FIXME may need some cleaning of the buffer
332 assert(s
->bit_left
==32);
338 * skips the given number of bits.
339 * must only be used if the actual values in the bitstream dont matter
341 static inline void skip_put_bits(PutBitContext
*s
, int n
){
342 #ifdef ALT_BITSTREAM_WRITER
346 s
->buf_ptr
-= s
->bit_left
>>5;
352 * Changes the end of the buffer.
354 static inline void set_put_bits_buffer_size(PutBitContext
*s
, int size
){
355 s
->buf_end
= s
->buf
+ size
;
358 /* Bitstream reader API docs:
360 abritary name which is used as prefix for the internal variables
365 OPEN_READER(name, gb)
366 loads gb into local variables
368 CLOSE_READER(name, gb)
369 stores local vars in gb
371 UPDATE_CACHE(name, gb)
372 refills the internal cache from the bitstream
373 after this call at least MIN_CACHE_BITS will be available,
376 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
378 SHOW_UBITS(name, gb, num)
379 will return the next num bits
381 SHOW_SBITS(name, gb, num)
382 will return the next num bits and do sign extension
384 SKIP_BITS(name, gb, num)
385 will skip over the next num bits
386 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
388 SKIP_CACHE(name, gb, num)
389 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
391 SKIP_COUNTER(name, gb, num)
392 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
394 LAST_SKIP_CACHE(name, gb, num)
395 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
397 LAST_SKIP_BITS(name, gb, num)
398 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
400 for examples see get_bits, show_bits, skip_bits, get_vlc
403 static inline int unaligned32_be(const void *v
)
407 return (((p
[0]<<8) | p
[1])<<16) | (p
[2]<<8) | (p
[3]);
409 return be2me_32( unaligned32(v
)); //original
413 static inline int unaligned32_le(const void *v
)
417 return (((p
[3]<<8) | p
[2])<<16) | (p
[1]<<8) | (p
[0]);
419 return le2me_32( unaligned32(v
)); //original
423 #ifdef ALT_BITSTREAM_READER
424 # define MIN_CACHE_BITS 25
426 # define OPEN_READER(name, gb)\
427 int name##_index= (gb)->index;\
428 int name##_cache= 0;\
430 # define CLOSE_READER(name, gb)\
431 (gb)->index= name##_index;\
433 # ifdef ALT_BITSTREAM_READER_LE
434 # define UPDATE_CACHE(name, gb)\
435 name##_cache= unaligned32_le( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
437 # define SKIP_CACHE(name, gb, num)\
438 name##_cache >>= (num);
440 # define UPDATE_CACHE(name, gb)\
441 name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
443 # define SKIP_CACHE(name, gb, num)\
444 name##_cache <<= (num);
448 # define SKIP_COUNTER(name, gb, num)\
449 name##_index += (num);\
451 # define SKIP_BITS(name, gb, num)\
453 SKIP_CACHE(name, gb, num)\
454 SKIP_COUNTER(name, gb, num)\
457 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
458 # define LAST_SKIP_CACHE(name, gb, num) ;
460 # ifdef ALT_BITSTREAM_READER_LE
461 # define SHOW_UBITS(name, gb, num)\
462 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
464 # define SHOW_SBITS(name, gb, num)\
465 NEG_SSR32((name##_cache)<<(32-(num)), num)
467 # define SHOW_UBITS(name, gb, num)\
468 NEG_USR32(name##_cache, num)
470 # define SHOW_SBITS(name, gb, num)\
471 NEG_SSR32(name##_cache, num)
474 # define GET_CACHE(name, gb)\
475 ((uint32_t)name##_cache)
477 static inline int get_bits_count(GetBitContext
*s
){
481 static inline void skip_bits_long(GetBitContext
*s
, int n
){
485 #elif defined LIBMPEG2_BITSTREAM_READER
486 //libmpeg2 like reader
488 # define MIN_CACHE_BITS 17
490 # define OPEN_READER(name, gb)\
491 int name##_bit_count=(gb)->bit_count;\
492 int name##_cache= (gb)->cache;\
493 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
495 # define CLOSE_READER(name, gb)\
496 (gb)->bit_count= name##_bit_count;\
497 (gb)->cache= name##_cache;\
498 (gb)->buffer_ptr= name##_buffer_ptr;\
500 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
502 # define UPDATE_CACHE(name, gb)\
503 if(name##_bit_count >= 0){\
504 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
505 name##_buffer_ptr += 2;\
506 name##_bit_count-= 16;\
511 # define UPDATE_CACHE(name, gb)\
512 if(name##_bit_count >= 0){\
513 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
514 name##_buffer_ptr+=2;\
515 name##_bit_count-= 16;\
520 # define SKIP_CACHE(name, gb, num)\
521 name##_cache <<= (num);\
523 # define SKIP_COUNTER(name, gb, num)\
524 name##_bit_count += (num);\
526 # define SKIP_BITS(name, gb, num)\
528 SKIP_CACHE(name, gb, num)\
529 SKIP_COUNTER(name, gb, num)\
532 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
533 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
535 # define SHOW_UBITS(name, gb, num)\
536 NEG_USR32(name##_cache, num)
538 # define SHOW_SBITS(name, gb, num)\
539 NEG_SSR32(name##_cache, num)
541 # define GET_CACHE(name, gb)\
542 ((uint32_t)name##_cache)
544 static inline int get_bits_count(GetBitContext
*s
){
545 return (s
->buffer_ptr
- s
->buffer
)*8 - 16 + s
->bit_count
;
548 static inline void skip_bits_long(GetBitContext
*s
, int n
){
551 re_buffer_ptr
+= 2*(re_bit_count
>>4);
553 re_cache
= ((re_buffer_ptr
[-2]<<8) + re_buffer_ptr
[-1]) << (16+re_bit_count
);
558 #elif defined A32_BITSTREAM_READER
560 # define MIN_CACHE_BITS 32
562 # define OPEN_READER(name, gb)\
563 int name##_bit_count=(gb)->bit_count;\
564 uint32_t name##_cache0= (gb)->cache0;\
565 uint32_t name##_cache1= (gb)->cache1;\
566 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
568 # define CLOSE_READER(name, gb)\
569 (gb)->bit_count= name##_bit_count;\
570 (gb)->cache0= name##_cache0;\
571 (gb)->cache1= name##_cache1;\
572 (gb)->buffer_ptr= name##_buffer_ptr;\
574 # define UPDATE_CACHE(name, gb)\
575 if(name##_bit_count > 0){\
576 const uint32_t next= be2me_32( *name##_buffer_ptr );\
577 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
578 name##_cache1 |= next<<name##_bit_count;\
579 name##_buffer_ptr++;\
580 name##_bit_count-= 32;\
583 #if defined(ARCH_X86) || defined(ARCH_X86_64)
584 # define SKIP_CACHE(name, gb, num)\
586 "shldl %2, %1, %0 \n\t"\
588 : "+r" (name##_cache0), "+r" (name##_cache1)\
589 : "Ic" ((uint8_t)(num))\
592 # define SKIP_CACHE(name, gb, num)\
593 name##_cache0 <<= (num);\
594 name##_cache0 |= NEG_USR32(name##_cache1,num);\
595 name##_cache1 <<= (num);
598 # define SKIP_COUNTER(name, gb, num)\
599 name##_bit_count += (num);\
601 # define SKIP_BITS(name, gb, num)\
603 SKIP_CACHE(name, gb, num)\
604 SKIP_COUNTER(name, gb, num)\
607 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
608 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
610 # define SHOW_UBITS(name, gb, num)\
611 NEG_USR32(name##_cache0, num)
613 # define SHOW_SBITS(name, gb, num)\
614 NEG_SSR32(name##_cache0, num)
616 # define GET_CACHE(name, gb)\
619 static inline int get_bits_count(GetBitContext
*s
){
620 return ((uint8_t*)s
->buffer_ptr
- s
->buffer
)*8 - 32 + s
->bit_count
;
623 static inline void skip_bits_long(GetBitContext
*s
, int n
){
626 re_buffer_ptr
+= re_bit_count
>>5;
628 re_cache0
= be2me_32( re_buffer_ptr
[-1] ) << re_bit_count
;
637 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
638 * if MSB not set it is negative
639 * @param n length in bits
642 static inline int get_xbits(GetBitContext
*s
, int n
){
644 register int32_t cache
;
647 cache
= GET_CACHE(re
,s
);
649 LAST_SKIP_BITS(re
, s
, n
)
651 return (NEG_USR32(sign
^ cache
, n
) ^ sign
) - sign
;
654 static inline int get_sbits(GetBitContext
*s
, int n
){
658 tmp
= SHOW_SBITS(re
, s
, n
);
659 LAST_SKIP_BITS(re
, s
, n
)
666 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
668 static inline unsigned int get_bits(GetBitContext
*s
, int n
){
672 tmp
= SHOW_UBITS(re
, s
, n
);
673 LAST_SKIP_BITS(re
, s
, n
)
680 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
682 static inline unsigned int show_bits(GetBitContext
*s
, int n
){
686 tmp
= SHOW_UBITS(re
, s
, n
);
687 // CLOSE_READER(re, s)
691 static inline void skip_bits(GetBitContext
*s
, int n
){
692 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
695 LAST_SKIP_BITS(re
, s
, n
)
699 static inline unsigned int get_bits1(GetBitContext
*s
){
700 #ifdef ALT_BITSTREAM_READER
702 uint8_t result
= s
->buffer
[ index
>>3 ];
703 #ifdef ALT_BITSTREAM_READER_LE
704 result
>>= (index
&0x07);
707 result
<<= (index
&0x07);
715 return get_bits(s
, 1);
719 static inline unsigned int show_bits1(GetBitContext
*s
){
720 return show_bits(s
, 1);
723 static inline void skip_bits1(GetBitContext
*s
){
730 static inline unsigned int get_bits_long(GetBitContext
*s
, int n
){
731 if(n
<=17) return get_bits(s
, n
);
733 #ifdef ALT_BITSTREAM_READER_LE
734 int ret
= get_bits(s
, 16);
735 return ret
| (get_bits(s
, n
-16) << 16);
737 int ret
= get_bits(s
, 16) << (n
-16);
738 return ret
| get_bits(s
, n
-16);
746 static inline unsigned int show_bits_long(GetBitContext
*s
, int n
){
747 if(n
<=17) return show_bits(s
, n
);
749 GetBitContext gb
= *s
;
750 int ret
= get_bits_long(s
, n
);
756 static inline int check_marker(GetBitContext
*s
, const char *msg
)
758 int bit
= get_bits1(s
);
760 av_log(NULL
, AV_LOG_INFO
, "Marker bit missing %s\n", msg
);
766 * init GetBitContext.
767 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
768 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
769 * @param bit_size the size of the buffer in bits
771 static inline void init_get_bits(GetBitContext
*s
,
772 const uint8_t *buffer
, int bit_size
)
774 int buffer_size
= (bit_size
+7)>>3;
775 if(buffer_size
< 0 || bit_size
< 0) {
776 buffer_size
= bit_size
= 0;
781 s
->size_in_bits
= bit_size
;
782 s
->buffer_end
= buffer
+ buffer_size
;
783 #ifdef ALT_BITSTREAM_READER
785 #elif defined LIBMPEG2_BITSTREAM_READER
786 s
->buffer_ptr
= (uint8_t*)((intptr_t)buffer
&(~1));
787 s
->bit_count
= 16 + 8*((intptr_t)buffer
&1);
788 skip_bits_long(s
, 0);
789 #elif defined A32_BITSTREAM_READER
790 s
->buffer_ptr
= (uint32_t*)((intptr_t)buffer
&(~3));
791 s
->bit_count
= 32 + 8*((intptr_t)buffer
&3);
792 skip_bits_long(s
, 0);
796 static inline void align_get_bits(GetBitContext
*s
)
798 int n
= (-get_bits_count(s
)) & 7;
799 if(n
) skip_bits(s
, n
);
802 int init_vlc(VLC
*vlc
, int nb_bits
, int nb_codes
,
803 const void *bits
, int bits_wrap
, int bits_size
,
804 const void *codes
, int codes_wrap
, int codes_size
,
806 #define INIT_VLC_USE_STATIC 1
807 #define INIT_VLC_LE 2
808 void free_vlc(VLC
*vlc
);
812 * if the vlc code is invalid and max_depth=1 than no bits will be removed
813 * if the vlc code is invalid and max_depth>1 than the number of bits removed
816 #define GET_VLC(code, name, gb, table, bits, max_depth)\
818 int n, index, nb_bits;\
820 index= SHOW_UBITS(name, gb, bits);\
821 code = table[index][0];\
822 n = table[index][1];\
824 if(max_depth > 1 && n < 0){\
825 LAST_SKIP_BITS(name, gb, bits)\
826 UPDATE_CACHE(name, gb)\
830 index= SHOW_UBITS(name, gb, nb_bits) + code;\
831 code = table[index][0];\
832 n = table[index][1];\
833 if(max_depth > 2 && n < 0){\
834 LAST_SKIP_BITS(name, gb, nb_bits)\
835 UPDATE_CACHE(name, gb)\
839 index= SHOW_UBITS(name, gb, nb_bits) + code;\
840 code = table[index][0];\
841 n = table[index][1];\
844 SKIP_BITS(name, gb, n)\
847 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
849 int n, index, nb_bits;\
851 index= SHOW_UBITS(name, gb, bits);\
852 level = table[index].level;\
853 n = table[index].len;\
855 if(max_depth > 1 && n < 0){\
856 SKIP_BITS(name, gb, bits)\
858 UPDATE_CACHE(name, gb)\
863 index= SHOW_UBITS(name, gb, nb_bits) + level;\
864 level = table[index].level;\
865 n = table[index].len;\
867 run= table[index].run;\
868 SKIP_BITS(name, gb, n)\
873 * parses a vlc code, faster then get_vlc()
874 * @param bits is the number of bits which will be read at once, must be
875 * identical to nb_bits in init_vlc()
876 * @param max_depth is the number of times bits bits must be readed to completly
877 * read the longest vlc code
878 * = (max_vlc_length + bits - 1) / bits
880 static always_inline
int get_vlc2(GetBitContext
*s
, VLC_TYPE (*table
)[2],
881 int bits
, int max_depth
)
888 GET_VLC(code
, re
, s
, table
, bits
, max_depth
)
897 static inline void print_bin(int bits
, int n
){
900 for(i
=n
-1; i
>=0; i
--){
901 av_log(NULL
, AV_LOG_DEBUG
, "%d", (bits
>>i
)&1);
904 av_log(NULL
, AV_LOG_DEBUG
, " ");
907 static inline int get_bits_trace(GetBitContext
*s
, int n
, char *file
, const char *func
, int line
){
908 int r
= get_bits(s
, n
);
911 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
);
914 static inline int get_vlc_trace(GetBitContext
*s
, VLC_TYPE (*table
)[2], int bits
, int max_depth
, char *file
, const char *func
, int line
){
915 int show
= show_bits(s
, 24);
916 int pos
= get_bits_count(s
);
917 int r
= get_vlc2(s
, table
, bits
, max_depth
);
918 int len
= get_bits_count(s
) - pos
;
919 int bits2
= show
>>(24-len
);
921 print_bin(bits2
, len
);
923 av_log(NULL
, AV_LOG_DEBUG
, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2
, len
, r
, pos
, file
, func
, line
);
926 static inline int get_xbits_trace(GetBitContext
*s
, int n
, char *file
, const char *func
, int line
){
927 int show
= show_bits(s
, n
);
928 int r
= get_xbits(s
, n
);
931 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
);
935 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
936 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
937 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
938 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
939 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
941 #define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
944 #define tprintf(...) {}
947 static inline int decode012(GetBitContext
*gb
){
953 return get_bits1(gb
) + 1;
956 #endif /* BITSTREAM_H */