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
33 #include "ffmpeg_config.h"
36 #include "ffmpeg_bswap.h"
40 #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
41 #define ALT_BITSTREAM_READER
44 //#define ALT_BITSTREAM_WRITER
45 //#define ALIGNED_BITSTREAM_WRITER
46 #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
48 # define A32_BITSTREAM_READER
50 #define ALT_BITSTREAM_READER
51 //#define LIBMPEG2_BITSTREAM_READER
52 //#define A32_BITSTREAM_READER
55 #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
57 extern const uint8_t ff_reverse
[256];
60 // avoid +32 for shift optimization (gcc should do that ...)
61 static inline int32_t NEG_SSR32( int32_t a
, int8_t s
){
62 asm ("sarl %1, %0\n\t"
64 : "ic" ((uint8_t)(-s
))
68 static inline uint32_t NEG_USR32(uint32_t a
, int8_t s
){
69 asm ("shrl %1, %0\n\t"
71 : "ic" ((uint8_t)(-s
))
76 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
77 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
82 /* buf and buf_end must be present and used by every alternative writer. */
83 typedef struct PutBitContext
{
84 #ifdef ALT_BITSTREAM_WRITER
85 uint8_t *buf
, *buf_end
;
90 uint8_t *buf
, *buf_ptr
, *buf_end
;
94 static inline void init_put_bits(PutBitContext
*s
, uint8_t *buffer
, int buffer_size
)
102 s
->buf_end
= s
->buf
+ buffer_size
;
103 #ifdef ALT_BITSTREAM_WRITER
105 ((uint32_t*)(s
->buf
))[0]=0;
106 // memset(buffer, 0, buffer_size);
114 /* return the number of bits output */
115 static inline int put_bits_count(PutBitContext
*s
)
117 #ifdef ALT_BITSTREAM_WRITER
120 return (s
->buf_ptr
- s
->buf
) * 8 + 32 - s
->bit_left
;
124 /* pad the end of the output stream with zeros */
125 static inline void flush_put_bits(PutBitContext
*s
)
127 #ifdef ALT_BITSTREAM_WRITER
130 s
->bit_buf
<<= s
->bit_left
;
131 while (s
->bit_left
< 32) {
132 /* XXX: should test end of buffer */
133 *s
->buf_ptr
++=s
->bit_buf
>> 24;
142 void align_put_bits(PutBitContext
*s
);
143 void ff_put_string(PutBitContext
* pbc
, char *s
, int put_zero
);
146 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
147 typedef struct GetBitContext
{
148 const uint8_t *buffer
, *buffer_end
;
149 #ifdef ALT_BITSTREAM_READER
151 #elif defined LIBMPEG2_BITSTREAM_READER
155 #elif defined A32_BITSTREAM_READER
156 uint32_t *buffer_ptr
;
164 #define VLC_TYPE int16_t
168 VLC_TYPE (*table
)[2]; ///< code, bits
169 int table_size
, table_allocated
;
172 typedef struct RL_VLC_ELEM
{
178 #if defined(ARCH_SPARC) || defined(ARCH_ARMV4L) || defined(ARCH_MIPS) || defined(ARCH_BFIN)
179 #define UNALIGNED_STORES_ARE_BAD
182 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
183 #if defined(ARCH_X86) || defined(CPU_COLDFIRE)
184 # define unaligned16(a) (*(const uint16_t*)(a))
185 # define unaligned32(a) (*(const uint32_t*)(a))
186 # define unaligned64(a) (*(const uint64_t*)(a))
189 # define unaligned(x) \
190 static inline uint##x##_t unaligned##x(const void *v) { \
193 } __attribute__((packed)); \
195 return ((const struct Unaligned *) v)->i; \
197 # elif defined(__DECC)
198 # define unaligned(x) \
199 static inline uint##x##_t unaligned##x(const void *v) { \
200 return *(const __unaligned uint##x##_t *) v; \
203 # define unaligned(x) \
204 static inline uint##x##_t unaligned##x(const void *v) { \
205 return *(const uint##x##_t *) v; \
212 #endif /* defined(ARCH_X86) */
214 #ifndef ALT_BITSTREAM_WRITER
215 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
217 unsigned int bit_buf
;
220 // printf("put_bits=%d %x\n", n, value);
221 // assert(n == 32 || value < (1U << n));
223 bit_buf
= s
->bit_buf
;
224 bit_left
= s
->bit_left
;
226 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
229 bit_buf
= (bit_buf
<<n
) | value
;
233 bit_buf
|= value
>> (n
- bit_left
);
234 #ifdef UNALIGNED_STORES_ARE_BAD
235 if (3 & (intptr_t) s
->buf_ptr
) {
236 s
->buf_ptr
[0] = bit_buf
>> 24;
237 s
->buf_ptr
[1] = bit_buf
>> 16;
238 s
->buf_ptr
[2] = bit_buf
>> 8;
239 s
->buf_ptr
[3] = bit_buf
;
242 *(uint32_t *)s
->buf_ptr
= be2me_32(bit_buf
);
243 //printf("bitbuf = %08x\n", bit_buf);
249 s
->bit_buf
= bit_buf
;
250 s
->bit_left
= bit_left
;
255 #ifdef ALT_BITSTREAM_WRITER
256 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
258 # ifdef ALIGNED_BITSTREAM_WRITER
259 # if defined(ARCH_X86)
261 "movl %0, %%ecx \n\t"
262 "xorl %%eax, %%eax \n\t"
263 "shrdl %%cl, %1, %%eax \n\t"
265 "movl %0, %%ecx \n\t"
266 "shrl $3, %%ecx \n\t"
267 "andl $0xFFFFFFFC, %%ecx \n\t"
269 "orl %1, (%2, %%ecx) \n\t"
272 "movl %%eax, 4(%2, %%ecx) \n\t"
273 : "=&r" (s
->index
), "=&r" (value
)
274 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
<<(-n
))
279 uint32_t *ptr
= ((uint32_t *)s
->buf
)+(index
>>5);
283 ptr
[0] |= be2me_32(value
>>(index
&31));
284 ptr
[1] = be2me_32(value
<<(32-(index
&31)));
285 //if(n>24) printf("%d %d\n", n, value);
289 # else //ALIGNED_BITSTREAM_WRITER
290 # if defined(ARCH_X86)
292 "movl $7, %%ecx \n\t"
293 "andl %0, %%ecx \n\t"
294 "addl %3, %%ecx \n\t"
298 "movl %0, %%ecx \n\t"
299 "shrl $3, %%ecx \n\t"
300 "orl %1, (%%ecx, %2) \n\t"
302 "movl $0, 4(%%ecx, %2) \n\t"
303 : "=&r" (s
->index
), "=&r" (value
)
304 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
)
309 uint32_t *ptr
= (uint32_t*)(((uint8_t *)s
->buf
)+(index
>>3));
311 ptr
[0] |= be2me_32(value
<<(32-n
-(index
&7) ));
313 //if(n>24) printf("%d %d\n", n, value);
317 # endif //!ALIGNED_BITSTREAM_WRITER
322 static inline uint8_t* pbBufPtr(PutBitContext
*s
)
324 #ifdef ALT_BITSTREAM_WRITER
325 return s
->buf
+ (s
->index
>>3);
333 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
335 static inline void skip_put_bytes(PutBitContext
*s
, int n
){
336 // assert((put_bits_count(s)&7)==0);
337 #ifdef ALT_BITSTREAM_WRITER
338 FIXME may need some cleaning of the buffer
341 // assert(s->bit_left==32);
347 * skips the given number of bits.
348 * must only be used if the actual values in the bitstream dont matter
350 static inline void skip_put_bits(PutBitContext
*s
, int n
){
351 #ifdef ALT_BITSTREAM_WRITER
355 s
->buf_ptr
-= s
->bit_left
>>5;
361 * Changes the end of the buffer.
363 static inline void set_put_bits_buffer_size(PutBitContext
*s
, int size
){
364 s
->buf_end
= s
->buf
+ size
;
367 /* Bitstream reader API docs:
369 abritary name which is used as prefix for the internal variables
374 OPEN_READER(name, gb)
375 loads gb into local variables
377 CLOSE_READER(name, gb)
378 stores local vars in gb
380 UPDATE_CACHE(name, gb)
381 refills the internal cache from the bitstream
382 after this call at least MIN_CACHE_BITS will be available,
385 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
387 SHOW_UBITS(name, gb, num)
388 will return the next num bits
390 SHOW_SBITS(name, gb, num)
391 will return the next num bits and do sign extension
393 SKIP_BITS(name, gb, num)
394 will skip over the next num bits
395 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
397 SKIP_CACHE(name, gb, num)
398 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
400 SKIP_COUNTER(name, gb, num)
401 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
403 LAST_SKIP_CACHE(name, gb, num)
404 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
406 LAST_SKIP_BITS(name, gb, num)
407 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
409 for examples see get_bits, show_bits, skip_bits, get_vlc
412 static inline int unaligned32_be(const void *v
)
416 return (((p
[0]<<8) | p
[1])<<16) | (p
[2]<<8) | (p
[3]);
418 return be2me_32( unaligned32(v
)); //original
422 static inline int unaligned32_le(const void *v
)
426 return (((p
[3]<<8) | p
[2])<<16) | (p
[1]<<8) | (p
[0]);
428 return le2me_32( unaligned32(v
)); //original
432 #ifdef ALT_BITSTREAM_READER
433 # define MIN_CACHE_BITS 25
435 # define OPEN_READER(name, gb)\
436 int name##_index= (gb)->index;\
437 int name##_cache= 0;\
439 # define CLOSE_READER(name, gb)\
440 (gb)->index= name##_index;\
442 # ifdef ALT_BITSTREAM_READER_LE
443 # define UPDATE_CACHE(name, gb)\
444 name##_cache= unaligned32_le( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
446 # define SKIP_CACHE(name, gb, num)\
447 name##_cache >>= (num);
449 # define UPDATE_CACHE(name, gb)\
450 name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
452 # define SKIP_CACHE(name, gb, num)\
453 name##_cache <<= (num);
457 # define SKIP_COUNTER(name, gb, num)\
458 name##_index += (num);\
460 # define SKIP_BITS(name, gb, num)\
462 SKIP_CACHE(name, gb, num)\
463 SKIP_COUNTER(name, gb, num)\
466 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
467 # define LAST_SKIP_CACHE(name, gb, num) ;
469 # ifdef ALT_BITSTREAM_READER_LE
470 # define SHOW_UBITS(name, gb, num)\
471 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
473 # define SHOW_SBITS(name, gb, num)\
474 NEG_SSR32((name##_cache)<<(32-(num)), num)
476 # define SHOW_UBITS(name, gb, num)\
477 NEG_USR32(name##_cache, num)
479 # define SHOW_SBITS(name, gb, num)\
480 NEG_SSR32(name##_cache, num)
483 # define GET_CACHE(name, gb)\
484 ((uint32_t)name##_cache)
486 static inline int get_bits_count(GetBitContext
*s
){
490 static inline void skip_bits_long(GetBitContext
*s
, int n
){
494 #elif defined LIBMPEG2_BITSTREAM_READER
495 //libmpeg2 like reader
497 # define MIN_CACHE_BITS 17
499 # define OPEN_READER(name, gb)\
500 int name##_bit_count=(gb)->bit_count;\
501 int name##_cache= (gb)->cache;\
502 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
504 # define CLOSE_READER(name, gb)\
505 (gb)->bit_count= name##_bit_count;\
506 (gb)->cache= name##_cache;\
507 (gb)->buffer_ptr= name##_buffer_ptr;\
509 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
511 # define UPDATE_CACHE(name, gb)\
512 if(name##_bit_count >= 0){\
513 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
514 name##_buffer_ptr += 2;\
515 name##_bit_count-= 16;\
520 # define UPDATE_CACHE(name, gb)\
521 if(name##_bit_count >= 0){\
522 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
523 name##_buffer_ptr+=2;\
524 name##_bit_count-= 16;\
529 # define SKIP_CACHE(name, gb, num)\
530 name##_cache <<= (num);\
532 # define SKIP_COUNTER(name, gb, num)\
533 name##_bit_count += (num);\
535 # define SKIP_BITS(name, gb, num)\
537 SKIP_CACHE(name, gb, num)\
538 SKIP_COUNTER(name, gb, num)\
541 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
542 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
544 # define SHOW_UBITS(name, gb, num)\
545 NEG_USR32(name##_cache, num)
547 # define SHOW_SBITS(name, gb, num)\
548 NEG_SSR32(name##_cache, num)
550 # define GET_CACHE(name, gb)\
551 ((uint32_t)name##_cache)
553 static inline int get_bits_count(GetBitContext
*s
){
554 return (s
->buffer_ptr
- s
->buffer
)*8 - 16 + s
->bit_count
;
557 static inline void skip_bits_long(GetBitContext
*s
, int n
){
560 re_buffer_ptr
+= 2*(re_bit_count
>>4);
562 re_cache
= ((re_buffer_ptr
[-2]<<8) + re_buffer_ptr
[-1]) << (16+re_bit_count
);
567 #elif defined A32_BITSTREAM_READER
569 # define MIN_CACHE_BITS 32
571 # define OPEN_READER(name, gb)\
572 int name##_bit_count=(gb)->bit_count;\
573 uint32_t name##_cache0= (gb)->cache0;\
574 uint32_t name##_cache1= (gb)->cache1;\
575 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
577 # define CLOSE_READER(name, gb)\
578 (gb)->bit_count= name##_bit_count;\
579 (gb)->cache0= name##_cache0;\
580 (gb)->cache1= name##_cache1;\
581 (gb)->buffer_ptr= name##_buffer_ptr;\
583 # define UPDATE_CACHE(name, gb)\
584 if(name##_bit_count > 0){\
585 const uint32_t next= be2me_32( *name##_buffer_ptr );\
586 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
587 name##_cache1 |= next<<name##_bit_count;\
588 name##_buffer_ptr++;\
589 name##_bit_count-= 32;\
592 #if defined(ARCH_X86)
593 # define SKIP_CACHE(name, gb, num)\
595 "shldl %2, %1, %0 \n\t"\
597 : "+r" (name##_cache0), "+r" (name##_cache1)\
598 : "Ic" ((uint8_t)(num))\
601 # define SKIP_CACHE(name, gb, num)\
602 name##_cache0 <<= (num);\
603 name##_cache0 |= NEG_USR32(name##_cache1,num);\
604 name##_cache1 <<= (num);
607 # define SKIP_COUNTER(name, gb, num)\
608 name##_bit_count += (num);\
610 # define SKIP_BITS(name, gb, num)\
612 SKIP_CACHE(name, gb, num)\
613 SKIP_COUNTER(name, gb, num)\
616 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
617 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
619 # define SHOW_UBITS(name, gb, num)\
620 NEG_USR32(name##_cache0, num)
622 # define SHOW_SBITS(name, gb, num)\
623 NEG_SSR32(name##_cache0, num)
625 # define GET_CACHE(name, gb)\
628 static inline int get_bits_count(GetBitContext
*s
){
629 return ((uint8_t*)s
->buffer_ptr
- s
->buffer
)*8 - 32 + s
->bit_count
;
632 static inline void skip_bits_long(GetBitContext
*s
, int n
){
635 re_buffer_ptr
+= re_bit_count
>>5;
637 re_cache0
= be2me_32( re_buffer_ptr
[-1] ) << re_bit_count
;
646 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
647 * if MSB not set it is negative
648 * @param n length in bits
651 static inline int get_xbits(GetBitContext
*s
, int n
){
653 register int32_t cache
;
656 cache
= GET_CACHE(re
,s
);
658 LAST_SKIP_BITS(re
, s
, n
)
660 return (NEG_USR32(sign
^ cache
, n
) ^ sign
) - sign
;
663 static inline int get_sbits(GetBitContext
*s
, int n
){
667 tmp
= SHOW_SBITS(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 get_bits(GetBitContext
*s
, int n
){
681 tmp
= SHOW_UBITS(re
, s
, n
);
682 LAST_SKIP_BITS(re
, s
, n
)
689 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
691 static inline unsigned int show_bits(GetBitContext
*s
, int n
){
695 tmp
= SHOW_UBITS(re
, s
, n
);
696 // CLOSE_READER(re, s)
700 static inline void skip_bits(GetBitContext
*s
, int n
){
701 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
704 LAST_SKIP_BITS(re
, s
, n
)
708 static inline unsigned int get_bits1(GetBitContext
*s
){
709 #ifdef ALT_BITSTREAM_READER
711 uint8_t result
= s
->buffer
[ index
>>3 ];
712 #ifdef ALT_BITSTREAM_READER_LE
713 result
>>= (index
&0x07);
716 result
<<= (index
&0x07);
724 return get_bits(s
, 1);
728 static inline unsigned int show_bits1(GetBitContext
*s
){
729 return show_bits(s
, 1);
732 static inline void skip_bits1(GetBitContext
*s
){
739 static inline unsigned int get_bits_long(GetBitContext
*s
, int n
){
740 if(n
<=17) return get_bits(s
, n
);
742 #ifdef ALT_BITSTREAM_READER_LE
743 int ret
= get_bits(s
, 16);
744 return ret
| (get_bits(s
, n
-16) << 16);
746 int ret
= get_bits(s
, 16) << (n
-16);
747 return ret
| get_bits(s
, n
-16);
755 static inline unsigned int show_bits_long(GetBitContext
*s
, int n
){
756 if(n
<=17) return show_bits(s
, n
);
758 GetBitContext gb
= *s
;
759 int ret
= get_bits_long(s
, n
);
766 static inline int check_marker(GetBitContext *s, const char *msg)
768 int bit= get_bits1(s);
770 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
776 * init GetBitContext.
777 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
778 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
779 * @param bit_size the size of the buffer in bits
781 static inline void init_get_bits(GetBitContext
*s
,
782 const uint8_t *buffer
, int bit_size
)
784 int buffer_size
= (bit_size
+7)>>3;
785 if(buffer_size
< 0 || bit_size
< 0) {
786 buffer_size
= bit_size
= 0;
791 s
->size_in_bits
= bit_size
;
792 s
->buffer_end
= buffer
+ buffer_size
;
793 #ifdef ALT_BITSTREAM_READER
795 #elif defined LIBMPEG2_BITSTREAM_READER
796 s
->buffer_ptr
= (uint8_t*)((intptr_t)buffer
&(~1));
797 s
->bit_count
= 16 + 8*((intptr_t)buffer
&1);
798 skip_bits_long(s
, 0);
799 #elif defined A32_BITSTREAM_READER
800 s
->buffer_ptr
= (uint32_t*)((intptr_t)buffer
&(~3));
801 s
->bit_count
= 32 + 8*((intptr_t)buffer
&3);
802 skip_bits_long(s
, 0);
806 static inline void align_get_bits(GetBitContext
*s
)
808 int n
= (-get_bits_count(s
)) & 7;
809 if(n
) skip_bits(s
, n
);
812 int init_vlc(VLC
*vlc
, int nb_bits
, int nb_codes
,
813 const void *bits
, int bits_wrap
, int bits_size
,
814 const void *codes
, int codes_wrap
, int codes_size
,
816 #define INIT_VLC_USE_STATIC 1
817 #define INIT_VLC_LE 2
818 void free_vlc(VLC
*vlc
);
822 * if the vlc code is invalid and max_depth=1 than no bits will be removed
823 * if the vlc code is invalid and max_depth>1 than the number of bits removed
826 #define GET_VLC(code, name, gb, table, bits, max_depth)\
828 int n, index, nb_bits;\
830 index= SHOW_UBITS(name, gb, bits);\
831 code = table[index][0];\
832 n = table[index][1];\
834 if(max_depth > 1 && n < 0){\
835 LAST_SKIP_BITS(name, gb, bits)\
836 UPDATE_CACHE(name, gb)\
840 index= SHOW_UBITS(name, gb, nb_bits) + code;\
841 code = table[index][0];\
842 n = table[index][1];\
843 if(max_depth > 2 && n < 0){\
844 LAST_SKIP_BITS(name, gb, nb_bits)\
845 UPDATE_CACHE(name, gb)\
849 index= SHOW_UBITS(name, gb, nb_bits) + code;\
850 code = table[index][0];\
851 n = table[index][1];\
854 SKIP_BITS(name, gb, n)\
857 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
859 int n, index, nb_bits;\
861 index= SHOW_UBITS(name, gb, bits);\
862 level = table[index].level;\
863 n = table[index].len;\
865 if(max_depth > 1 && n < 0){\
866 SKIP_BITS(name, gb, bits)\
868 UPDATE_CACHE(name, gb)\
873 index= SHOW_UBITS(name, gb, nb_bits) + level;\
874 level = table[index].level;\
875 n = table[index].len;\
877 run= table[index].run;\
878 SKIP_BITS(name, gb, n)\
883 * parses a vlc code, faster then get_vlc()
884 * @param bits is the number of bits which will be read at once, must be
885 * identical to nb_bits in init_vlc()
886 * @param max_depth is the number of times bits bits must be read to completely
887 * read the longest vlc code
888 * = (max_vlc_length + bits - 1) / bits
890 static av_always_inline
int get_vlc2(GetBitContext
*s
, VLC_TYPE (*table
)[2],
891 int bits
, int max_depth
)
898 GET_VLC(code
, re
, s
, table
, bits
, max_depth
)
907 static inline void print_bin(int bits
, int n
){
910 for(i
=n
-1; i
>=0; i
--){
911 av_log(NULL
, AV_LOG_DEBUG
, "%d", (bits
>>i
)&1);
914 av_log(NULL
, AV_LOG_DEBUG
, " ");
917 static inline int get_bits_trace(GetBitContext
*s
, int n
, char *file
, const char *func
, int line
){
918 int r
= get_bits(s
, n
);
921 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
);
924 static inline int get_vlc_trace(GetBitContext
*s
, VLC_TYPE (*table
)[2], int bits
, int max_depth
, char *file
, const char *func
, int line
){
925 int show
= show_bits(s
, 24);
926 int pos
= get_bits_count(s
);
927 int r
= get_vlc2(s
, table
, bits
, max_depth
);
928 int len
= get_bits_count(s
) - pos
;
929 int bits2
= show
>>(24-len
);
931 print_bin(bits2
, len
);
933 av_log(NULL
, AV_LOG_DEBUG
, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2
, len
, r
, pos
, file
, func
, line
);
936 static inline int get_xbits_trace(GetBitContext
*s
, int n
, char *file
, const char *func
, int line
){
937 int show
= show_bits(s
, n
);
938 int r
= get_xbits(s
, n
);
941 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
);
945 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
946 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
947 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
948 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
949 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
951 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
954 #define tprintf(p, ...) {}
957 static inline int decode012(GetBitContext
*gb
){
963 return get_bits1(gb
) + 1;
966 #endif /* BITSTREAM_H */