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.
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"
37 #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
38 # define ALT_BITSTREAM_READER
41 //#define ALT_BITSTREAM_WRITER
42 //#define ALIGNED_BITSTREAM_WRITER
43 #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
45 # define A32_BITSTREAM_READER
47 # define ALT_BITSTREAM_READER
48 //#define LIBMPEG2_BITSTREAM_READER
49 //#define A32_BITSTREAM_READER
53 extern const uint8_t ff_reverse
[256];
56 // avoid +32 for shift optimization (gcc should do that ...)
57 static inline int32_t NEG_SSR32( int32_t a
, int8_t s
){
58 __asm__ ("sarl %1, %0\n\t"
60 : "ic" ((uint8_t)(-s
))
64 static inline uint32_t NEG_USR32(uint32_t a
, int8_t s
){
65 __asm__ ("shrl %1, %0\n\t"
67 : "ic" ((uint8_t)(-s
))
72 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
73 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
78 /* buf and buf_end must be present and used by every alternative writer. */
79 typedef struct PutBitContext
{
80 #ifdef ALT_BITSTREAM_WRITER
81 uint8_t *buf
, *buf_end
;
86 uint8_t *buf
, *buf_ptr
, *buf_end
;
90 static inline void init_put_bits(PutBitContext
*s
, uint8_t *buffer
, int buffer_size
)
98 s
->buf_end
= s
->buf
+ buffer_size
;
99 #ifdef ALT_BITSTREAM_WRITER
101 ((uint32_t*)(s
->buf
))[0]=0;
102 // memset(buffer, 0, buffer_size);
110 /* return the number of bits output */
111 static inline int put_bits_count(PutBitContext
*s
)
113 #ifdef ALT_BITSTREAM_WRITER
116 return (s
->buf_ptr
- s
->buf
) * 8 + 32 - s
->bit_left
;
120 /* pad the end of the output stream with zeros */
121 static inline void flush_put_bits(PutBitContext
*s
)
123 #ifdef ALT_BITSTREAM_WRITER
126 #ifndef BITSTREAM_WRITER_LE
127 s
->bit_buf
<<= s
->bit_left
;
129 while (s
->bit_left
< 32) {
130 /* XXX: should test end of buffer */
131 #ifdef BITSTREAM_WRITER_LE
132 *s
->buf_ptr
++=s
->bit_buf
;
135 *s
->buf_ptr
++=s
->bit_buf
>> 24;
145 void align_put_bits(PutBitContext
*s
);
146 void ff_put_string(PutBitContext
* pbc
, const char *s
, int put_zero
);
147 void ff_copy_bits(PutBitContext
*pb
, const uint8_t *src
, int length
);
150 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
151 typedef struct GetBitContext
{
152 const uint8_t *buffer
, *buffer_end
;
153 #ifdef ALT_BITSTREAM_READER
155 #elif defined LIBMPEG2_BITSTREAM_READER
159 #elif defined A32_BITSTREAM_READER
160 uint32_t *buffer_ptr
;
168 #define VLC_TYPE int16_t
172 VLC_TYPE (*table
)[2]; ///< code, bits
173 int table_size
, table_allocated
;
176 typedef struct RL_VLC_ELEM
{
182 #if defined(ARCH_SPARC) || defined(ARCH_ARMV4L) || defined(ARCH_MIPS) || defined(ARCH_BFIN)
183 #define UNALIGNED_STORES_ARE_BAD
186 #ifndef ALT_BITSTREAM_WRITER
187 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
189 unsigned int bit_buf
;
192 // printf("put_bits=%d %x\n", n, value);
193 assert(n
== 32 || value
< (1U << n
));
195 bit_buf
= s
->bit_buf
;
196 bit_left
= s
->bit_left
;
198 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
200 #ifdef BITSTREAM_WRITER_LE
201 bit_buf
|= value
<< (32 - bit_left
);
203 #ifdef UNALIGNED_STORES_ARE_BAD
204 if (3 & (intptr_t) s
->buf_ptr
) {
205 s
->buf_ptr
[0] = bit_buf
;
206 s
->buf_ptr
[1] = bit_buf
>> 8;
207 s
->buf_ptr
[2] = bit_buf
>> 16;
208 s
->buf_ptr
[3] = bit_buf
>> 24;
211 *(uint32_t *)s
->buf_ptr
= le2me_32(bit_buf
);
213 bit_buf
= (bit_left
==32)?0:value
>> bit_left
;
219 bit_buf
= (bit_buf
<<n
) | value
;
223 bit_buf
|= value
>> (n
- bit_left
);
224 #ifdef UNALIGNED_STORES_ARE_BAD
225 if (3 & (intptr_t) s
->buf_ptr
) {
226 s
->buf_ptr
[0] = bit_buf
>> 24;
227 s
->buf_ptr
[1] = bit_buf
>> 16;
228 s
->buf_ptr
[2] = bit_buf
>> 8;
229 s
->buf_ptr
[3] = bit_buf
;
232 *(uint32_t *)s
->buf_ptr
= be2me_32(bit_buf
);
233 //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)
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)
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
312 static inline void put_sbits(PutBitContext
*pb
, int bits
, int32_t val
)
314 assert(bits
>= 0 && bits
<= 31);
316 put_bits(pb
, bits
, val
& ((1<<bits
)-1));
320 static inline uint8_t* pbBufPtr(PutBitContext
*s
)
322 #ifdef ALT_BITSTREAM_WRITER
323 return s
->buf
+ (s
->index
>>3);
331 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
333 static inline void skip_put_bytes(PutBitContext
*s
, int n
){
334 assert((put_bits_count(s
)&7)==0);
335 #ifdef ALT_BITSTREAM_WRITER
336 FIXME may need some cleaning of the buffer
339 assert(s
->bit_left
==32);
345 * Skips the given number of bits.
346 * Must only be used if the actual values in the bitstream do not matter.
348 static inline void skip_put_bits(PutBitContext
*s
, int n
){
349 #ifdef ALT_BITSTREAM_WRITER
353 s
->buf_ptr
-= s
->bit_left
>>5;
359 * Changes the end of the buffer.
361 static inline void set_put_bits_buffer_size(PutBitContext
*s
, int size
){
362 s
->buf_end
= s
->buf
+ size
;
365 /* Bitstream reader API docs:
367 arbitrary name which is used as prefix for the internal variables
372 OPEN_READER(name, gb)
373 loads gb into local variables
375 CLOSE_READER(name, gb)
376 stores local vars in gb
378 UPDATE_CACHE(name, gb)
379 refills the internal cache from the bitstream
380 after this call at least MIN_CACHE_BITS will be available,
383 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
385 SHOW_UBITS(name, gb, num)
386 will return the next num bits
388 SHOW_SBITS(name, gb, num)
389 will return the next num bits and do sign extension
391 SKIP_BITS(name, gb, num)
392 will skip over the next num bits
393 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
395 SKIP_CACHE(name, gb, num)
396 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
398 SKIP_COUNTER(name, gb, num)
399 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
401 LAST_SKIP_CACHE(name, gb, num)
402 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
404 LAST_SKIP_BITS(name, gb, num)
405 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
407 for examples see get_bits, show_bits, skip_bits, get_vlc
410 #ifdef ALT_BITSTREAM_READER
411 # define MIN_CACHE_BITS 25
413 # define OPEN_READER(name, gb)\
414 int name##_index= (gb)->index;\
415 int name##_cache= 0;\
417 # define CLOSE_READER(name, gb)\
418 (gb)->index= name##_index;\
420 # ifdef ALT_BITSTREAM_READER_LE
421 # define UPDATE_CACHE(name, gb)\
422 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
424 # define SKIP_CACHE(name, gb, num)\
425 name##_cache >>= (num);
427 # define UPDATE_CACHE(name, gb)\
428 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
430 # define SKIP_CACHE(name, gb, num)\
431 name##_cache <<= (num);
435 # define SKIP_COUNTER(name, gb, num)\
436 name##_index += (num);\
438 # define SKIP_BITS(name, gb, num)\
440 SKIP_CACHE(name, gb, num)\
441 SKIP_COUNTER(name, gb, num)\
444 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
445 # define LAST_SKIP_CACHE(name, gb, num) ;
447 # ifdef ALT_BITSTREAM_READER_LE
448 # define SHOW_UBITS(name, gb, num)\
449 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
451 # define SHOW_SBITS(name, gb, num)\
452 NEG_SSR32((name##_cache)<<(32-(num)), num)
454 # define SHOW_UBITS(name, gb, num)\
455 NEG_USR32(name##_cache, num)
457 # define SHOW_SBITS(name, gb, num)\
458 NEG_SSR32(name##_cache, num)
461 # define GET_CACHE(name, gb)\
462 ((uint32_t)name##_cache)
464 static inline int get_bits_count(GetBitContext
*s
){
468 static inline void skip_bits_long(GetBitContext
*s
, int n
){
472 #elif defined LIBMPEG2_BITSTREAM_READER
473 //libmpeg2 like reader
475 # define MIN_CACHE_BITS 17
477 # define OPEN_READER(name, gb)\
478 int name##_bit_count=(gb)->bit_count;\
479 int name##_cache= (gb)->cache;\
480 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
482 # define CLOSE_READER(name, gb)\
483 (gb)->bit_count= name##_bit_count;\
484 (gb)->cache= name##_cache;\
485 (gb)->buffer_ptr= name##_buffer_ptr;\
487 # define UPDATE_CACHE(name, gb)\
488 if(name##_bit_count >= 0){\
489 name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
490 name##_buffer_ptr+=2;\
491 name##_bit_count-= 16;\
494 # define SKIP_CACHE(name, gb, num)\
495 name##_cache <<= (num);\
497 # define SKIP_COUNTER(name, gb, num)\
498 name##_bit_count += (num);\
500 # define SKIP_BITS(name, gb, num)\
502 SKIP_CACHE(name, gb, num)\
503 SKIP_COUNTER(name, gb, num)\
506 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
507 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
509 # define SHOW_UBITS(name, gb, num)\
510 NEG_USR32(name##_cache, num)
512 # define SHOW_SBITS(name, gb, num)\
513 NEG_SSR32(name##_cache, num)
515 # define GET_CACHE(name, gb)\
516 ((uint32_t)name##_cache)
518 static inline int get_bits_count(GetBitContext
*s
){
519 return (s
->buffer_ptr
- s
->buffer
)*8 - 16 + s
->bit_count
;
522 static inline void skip_bits_long(GetBitContext
*s
, int n
){
525 re_buffer_ptr
+= 2*(re_bit_count
>>4);
527 re_cache
= ((re_buffer_ptr
[-2]<<8) + re_buffer_ptr
[-1]) << (16+re_bit_count
);
532 #elif defined A32_BITSTREAM_READER
534 # define MIN_CACHE_BITS 32
536 # define OPEN_READER(name, gb)\
537 int name##_bit_count=(gb)->bit_count;\
538 uint32_t name##_cache0= (gb)->cache0;\
539 uint32_t name##_cache1= (gb)->cache1;\
540 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
542 # define CLOSE_READER(name, gb)\
543 (gb)->bit_count= name##_bit_count;\
544 (gb)->cache0= name##_cache0;\
545 (gb)->cache1= name##_cache1;\
546 (gb)->buffer_ptr= name##_buffer_ptr;\
548 # define UPDATE_CACHE(name, gb)\
549 if(name##_bit_count > 0){\
550 const uint32_t next= be2me_32( *name##_buffer_ptr );\
551 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
552 name##_cache1 |= next<<name##_bit_count;\
553 name##_buffer_ptr++;\
554 name##_bit_count-= 32;\
557 #if defined(ARCH_X86)
558 # define SKIP_CACHE(name, gb, num)\
560 "shldl %2, %1, %0 \n\t"\
562 : "+r" (name##_cache0), "+r" (name##_cache1)\
563 : "Ic" ((uint8_t)(num))\
566 # define SKIP_CACHE(name, gb, num)\
567 name##_cache0 <<= (num);\
568 name##_cache0 |= NEG_USR32(name##_cache1,num);\
569 name##_cache1 <<= (num);
572 # define SKIP_COUNTER(name, gb, num)\
573 name##_bit_count += (num);\
575 # define SKIP_BITS(name, gb, num)\
577 SKIP_CACHE(name, gb, num)\
578 SKIP_COUNTER(name, gb, num)\
581 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
582 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
584 # define SHOW_UBITS(name, gb, num)\
585 NEG_USR32(name##_cache0, num)
587 # define SHOW_SBITS(name, gb, num)\
588 NEG_SSR32(name##_cache0, num)
590 # define GET_CACHE(name, gb)\
593 static inline int get_bits_count(GetBitContext
*s
){
594 return ((uint8_t*)s
->buffer_ptr
- s
->buffer
)*8 - 32 + s
->bit_count
;
597 static inline void skip_bits_long(GetBitContext
*s
, int n
){
600 re_buffer_ptr
+= re_bit_count
>>5;
602 re_cache0
= be2me_32( re_buffer_ptr
[-1] ) << re_bit_count
;
611 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
612 * if MSB not set it is negative
613 * @param n length in bits
616 static inline int get_xbits(GetBitContext
*s
, int n
){
618 register int32_t cache
;
621 cache
= GET_CACHE(re
,s
);
623 LAST_SKIP_BITS(re
, s
, n
)
625 return (NEG_USR32(sign
^ cache
, n
) ^ sign
) - sign
;
628 static inline int get_sbits(GetBitContext
*s
, int n
){
632 tmp
= SHOW_SBITS(re
, s
, n
);
633 LAST_SKIP_BITS(re
, s
, n
)
640 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
642 static inline unsigned int get_bits(GetBitContext
*s
, int n
){
646 tmp
= SHOW_UBITS(re
, s
, n
);
647 LAST_SKIP_BITS(re
, s
, n
)
654 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
656 static inline unsigned int show_bits(GetBitContext
*s
, int n
){
660 tmp
= SHOW_UBITS(re
, s
, n
);
661 // CLOSE_READER(re, s)
665 static inline void skip_bits(GetBitContext
*s
, int n
){
666 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
669 LAST_SKIP_BITS(re
, s
, n
)
673 static inline unsigned int get_bits1(GetBitContext
*s
){
674 #ifdef ALT_BITSTREAM_READER
676 uint8_t result
= s
->buffer
[ index
>>3 ];
677 #ifdef ALT_BITSTREAM_READER_LE
678 result
>>= (index
&0x07);
681 result
<<= (index
&0x07);
689 return get_bits(s
, 1);
693 static inline unsigned int show_bits1(GetBitContext
*s
){
694 return show_bits(s
, 1);
697 static inline void skip_bits1(GetBitContext
*s
){
704 static inline unsigned int get_bits_long(GetBitContext
*s
, int n
){
705 if(n
<=17) return get_bits(s
, n
);
707 #ifdef ALT_BITSTREAM_READER_LE
708 int ret
= get_bits(s
, 16);
709 return ret
| (get_bits(s
, n
-16) << 16);
711 int ret
= get_bits(s
, 16) << (n
-16);
712 return ret
| get_bits(s
, n
-16);
720 static inline unsigned int show_bits_long(GetBitContext
*s
, int n
){
721 if(n
<=17) return show_bits(s
, n
);
723 GetBitContext gb
= *s
;
724 int ret
= get_bits_long(s
, n
);
730 static inline int check_marker(GetBitContext
*s
, const char *msg
)
732 int bit
= get_bits1(s
);
734 av_log(NULL
, AV_LOG_INFO
, "Marker bit missing %s\n", msg
);
740 * init GetBitContext.
741 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
742 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
743 * @param bit_size the size of the buffer in bits
745 static inline void init_get_bits(GetBitContext
*s
,
746 const uint8_t *buffer
, int bit_size
)
748 int buffer_size
= (bit_size
+7)>>3;
749 if(buffer_size
< 0 || bit_size
< 0) {
750 buffer_size
= bit_size
= 0;
755 s
->size_in_bits
= bit_size
;
756 s
->buffer_end
= buffer
+ buffer_size
;
757 #ifdef ALT_BITSTREAM_READER
759 #elif defined LIBMPEG2_BITSTREAM_READER
760 s
->buffer_ptr
= (uint8_t*)((intptr_t)buffer
&(~1));
761 s
->bit_count
= 16 + 8*((intptr_t)buffer
&1);
762 skip_bits_long(s
, 0);
763 #elif defined A32_BITSTREAM_READER
764 s
->buffer_ptr
= (uint32_t*)((intptr_t)buffer
&(~3));
765 s
->bit_count
= 32 + 8*((intptr_t)buffer
&3);
766 skip_bits_long(s
, 0);
770 static inline void align_get_bits(GetBitContext
*s
)
772 int n
= (-get_bits_count(s
)) & 7;
773 if(n
) skip_bits(s
, n
);
776 #define init_vlc(vlc, nb_bits, nb_codes,\
777 bits, bits_wrap, bits_size,\
778 codes, codes_wrap, codes_size,\
780 init_vlc_sparse(vlc, nb_bits, nb_codes,\
781 bits, bits_wrap, bits_size,\
782 codes, codes_wrap, codes_size,\
785 int init_vlc_sparse(VLC
*vlc
, int nb_bits
, int nb_codes
,
786 const void *bits
, int bits_wrap
, int bits_size
,
787 const void *codes
, int codes_wrap
, int codes_size
,
788 const void *symbols
, int symbols_wrap
, int symbols_size
,
790 #define INIT_VLC_USE_STATIC 1 ///< VERY strongly deprecated and forbidden
791 #define INIT_VLC_LE 2
792 #define INIT_VLC_USE_NEW_STATIC 4
793 void free_vlc(VLC
*vlc
);
795 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
797 static VLC_TYPE table[static_size][2];\
798 (vlc)->table= table;\
799 (vlc)->table_allocated= static_size;\
800 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
806 * if the vlc code is invalid and max_depth=1 than no bits will be removed
807 * if the vlc code is invalid and max_depth>1 than the number of bits removed
810 #define GET_VLC(code, name, gb, table, bits, max_depth)\
812 int n, index, nb_bits;\
814 index= SHOW_UBITS(name, gb, bits);\
815 code = table[index][0];\
816 n = table[index][1];\
818 if(max_depth > 1 && n < 0){\
819 LAST_SKIP_BITS(name, gb, bits)\
820 UPDATE_CACHE(name, gb)\
824 index= SHOW_UBITS(name, gb, nb_bits) + code;\
825 code = table[index][0];\
826 n = table[index][1];\
827 if(max_depth > 2 && n < 0){\
828 LAST_SKIP_BITS(name, gb, nb_bits)\
829 UPDATE_CACHE(name, gb)\
833 index= SHOW_UBITS(name, gb, nb_bits) + code;\
834 code = table[index][0];\
835 n = table[index][1];\
838 SKIP_BITS(name, gb, n)\
841 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
843 int n, index, nb_bits;\
845 index= SHOW_UBITS(name, gb, bits);\
846 level = table[index].level;\
847 n = table[index].len;\
849 if(max_depth > 1 && n < 0){\
850 SKIP_BITS(name, gb, bits)\
852 UPDATE_CACHE(name, gb)\
857 index= SHOW_UBITS(name, gb, nb_bits) + level;\
858 level = table[index].level;\
859 n = table[index].len;\
861 run= table[index].run;\
862 SKIP_BITS(name, gb, n)\
867 * parses a vlc code, faster then get_vlc()
868 * @param bits is the number of bits which will be read at once, must be
869 * identical to nb_bits in init_vlc()
870 * @param max_depth is the number of times bits bits must be read to completely
871 * read the longest vlc code
872 * = (max_vlc_length + bits - 1) / bits
874 static av_always_inline
int get_vlc2(GetBitContext
*s
, VLC_TYPE (*table
)[2],
875 int bits
, int max_depth
)
882 GET_VLC(code
, re
, s
, table
, bits
, max_depth
)
891 static inline void print_bin(int bits
, int n
){
894 for(i
=n
-1; i
>=0; i
--){
895 av_log(NULL
, AV_LOG_DEBUG
, "%d", (bits
>>i
)&1);
898 av_log(NULL
, AV_LOG_DEBUG
, " ");
901 static inline int get_bits_trace(GetBitContext
*s
, int n
, char *file
, const char *func
, int line
){
902 int r
= get_bits(s
, n
);
905 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
);
908 static inline int get_vlc_trace(GetBitContext
*s
, VLC_TYPE (*table
)[2], int bits
, int max_depth
, char *file
, const char *func
, int line
){
909 int show
= show_bits(s
, 24);
910 int pos
= get_bits_count(s
);
911 int r
= get_vlc2(s
, table
, bits
, max_depth
);
912 int len
= get_bits_count(s
) - pos
;
913 int bits2
= show
>>(24-len
);
915 print_bin(bits2
, len
);
917 av_log(NULL
, AV_LOG_DEBUG
, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2
, len
, r
, pos
, file
, func
, line
);
920 static inline int get_xbits_trace(GetBitContext
*s
, int n
, char *file
, const char *func
, int line
){
921 int show
= show_bits(s
, n
);
922 int r
= get_xbits(s
, n
);
925 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
);
929 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
930 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
931 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
932 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
933 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
935 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
938 #define tprintf(p, ...) {}
941 static inline int decode012(GetBitContext
*gb
){
947 return get_bits1(gb
) + 1;
950 static inline int decode210(GetBitContext
*gb
){
954 return 2 - get_bits1(gb
);
957 #endif /* AVCODEC_BITSTREAM_H */