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 FFMPEG_BITSTREAM_H
27 #define FFMPEG_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
52 #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
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
;
91 static inline void init_put_bits(PutBitContext
*s
, uint8_t *buffer
, int buffer_size
)
99 s
->buf_end
= s
->buf
+ buffer_size
;
100 #ifdef ALT_BITSTREAM_WRITER
102 ((uint32_t*)(s
->buf
))[0]=0;
103 // memset(buffer, 0, buffer_size);
111 /* return the number of bits output */
112 static inline int put_bits_count(PutBitContext
*s
)
114 #ifdef ALT_BITSTREAM_WRITER
117 return (s
->buf_ptr
- s
->buf
) * 8 + 32 - s
->bit_left
;
121 /* pad the end of the output stream with zeros */
122 static inline void flush_put_bits(PutBitContext
*s
)
124 #ifdef ALT_BITSTREAM_WRITER
127 s
->bit_buf
<<= s
->bit_left
;
128 while (s
->bit_left
< 32) {
129 /* XXX: should test end of buffer */
130 *s
->buf_ptr
++=s
->bit_buf
>> 24;
139 void align_put_bits(PutBitContext
*s
);
140 void ff_put_string(PutBitContext
* pbc
, const char *s
, int put_zero
);
141 void ff_copy_bits(PutBitContext
*pb
, const uint8_t *src
, int length
);
144 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
145 typedef struct GetBitContext
{
146 const uint8_t *buffer
, *buffer_end
;
147 #ifdef ALT_BITSTREAM_READER
149 #elif defined LIBMPEG2_BITSTREAM_READER
153 #elif defined A32_BITSTREAM_READER
154 uint32_t *buffer_ptr
;
162 #define VLC_TYPE int16_t
166 VLC_TYPE (*table
)[2]; ///< code, bits
167 int table_size
, table_allocated
;
170 typedef struct RL_VLC_ELEM
{
176 #if defined(ARCH_SPARC) || defined(ARCH_ARMV4L) || defined(ARCH_MIPS) || defined(ARCH_BFIN)
177 #define UNALIGNED_STORES_ARE_BAD
180 /* used to avoid misaligned exceptions on some archs (alpha, ...) */
181 #if defined(ARCH_X86)
182 # define unaligned16(a) (*(const uint16_t*)(a))
183 # define unaligned32(a) (*(const uint32_t*)(a))
184 # define unaligned64(a) (*(const uint64_t*)(a))
187 # define unaligned(x) \
188 static inline uint##x##_t unaligned##x(const void *v) { \
191 } __attribute__((packed)); \
193 return ((const struct Unaligned *) v)->i; \
195 # elif defined(__DECC)
196 # define unaligned(x) \
197 static inline uint##x##_t unaligned##x(const void *v) { \
198 return *(const __unaligned uint##x##_t *) v; \
201 # define unaligned(x) \
202 static inline uint##x##_t unaligned##x(const void *v) { \
203 return *(const uint##x##_t *) v; \
210 #endif /* defined(ARCH_X86) */
212 #ifndef ALT_BITSTREAM_WRITER
213 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
215 unsigned int bit_buf
;
218 // printf("put_bits=%d %x\n", n, value);
219 assert(n
== 32 || value
< (1U << n
));
221 bit_buf
= s
->bit_buf
;
222 bit_left
= s
->bit_left
;
224 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
227 bit_buf
= (bit_buf
<<n
) | value
;
231 bit_buf
|= value
>> (n
- bit_left
);
232 #ifdef UNALIGNED_STORES_ARE_BAD
233 if (3 & (intptr_t) s
->buf_ptr
) {
234 s
->buf_ptr
[0] = bit_buf
>> 24;
235 s
->buf_ptr
[1] = bit_buf
>> 16;
236 s
->buf_ptr
[2] = bit_buf
>> 8;
237 s
->buf_ptr
[3] = bit_buf
;
240 *(uint32_t *)s
->buf_ptr
= be2me_32(bit_buf
);
241 //printf("bitbuf = %08x\n", bit_buf);
247 s
->bit_buf
= bit_buf
;
248 s
->bit_left
= bit_left
;
253 #ifdef ALT_BITSTREAM_WRITER
254 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
256 # ifdef ALIGNED_BITSTREAM_WRITER
257 # if defined(ARCH_X86)
259 "movl %0, %%ecx \n\t"
260 "xorl %%eax, %%eax \n\t"
261 "shrdl %%cl, %1, %%eax \n\t"
263 "movl %0, %%ecx \n\t"
264 "shrl $3, %%ecx \n\t"
265 "andl $0xFFFFFFFC, %%ecx \n\t"
267 "orl %1, (%2, %%ecx) \n\t"
270 "movl %%eax, 4(%2, %%ecx) \n\t"
271 : "=&r" (s
->index
), "=&r" (value
)
272 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
<<(-n
))
277 uint32_t *ptr
= ((uint32_t *)s
->buf
)+(index
>>5);
281 ptr
[0] |= be2me_32(value
>>(index
&31));
282 ptr
[1] = be2me_32(value
<<(32-(index
&31)));
283 //if(n>24) printf("%d %d\n", n, value);
287 # else //ALIGNED_BITSTREAM_WRITER
288 # if defined(ARCH_X86)
290 "movl $7, %%ecx \n\t"
291 "andl %0, %%ecx \n\t"
292 "addl %3, %%ecx \n\t"
296 "movl %0, %%ecx \n\t"
297 "shrl $3, %%ecx \n\t"
298 "orl %1, (%%ecx, %2) \n\t"
300 "movl $0, 4(%%ecx, %2) \n\t"
301 : "=&r" (s
->index
), "=&r" (value
)
302 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
)
307 uint32_t *ptr
= (uint32_t*)(((uint8_t *)s
->buf
)+(index
>>3));
309 ptr
[0] |= be2me_32(value
<<(32-n
-(index
&7) ));
311 //if(n>24) printf("%d %d\n", n, value);
315 # endif //!ALIGNED_BITSTREAM_WRITER
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 abritary 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 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
489 # define UPDATE_CACHE(name, gb)\
490 if(name##_bit_count >= 0){\
491 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
492 name##_buffer_ptr += 2;\
493 name##_bit_count-= 16;\
498 # define UPDATE_CACHE(name, gb)\
499 if(name##_bit_count >= 0){\
500 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
501 name##_buffer_ptr+=2;\
502 name##_bit_count-= 16;\
507 # define SKIP_CACHE(name, gb, num)\
508 name##_cache <<= (num);\
510 # define SKIP_COUNTER(name, gb, num)\
511 name##_bit_count += (num);\
513 # define SKIP_BITS(name, gb, num)\
515 SKIP_CACHE(name, gb, num)\
516 SKIP_COUNTER(name, gb, num)\
519 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
520 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
522 # define SHOW_UBITS(name, gb, num)\
523 NEG_USR32(name##_cache, num)
525 # define SHOW_SBITS(name, gb, num)\
526 NEG_SSR32(name##_cache, num)
528 # define GET_CACHE(name, gb)\
529 ((uint32_t)name##_cache)
531 static inline int get_bits_count(GetBitContext
*s
){
532 return (s
->buffer_ptr
- s
->buffer
)*8 - 16 + s
->bit_count
;
535 static inline void skip_bits_long(GetBitContext
*s
, int n
){
538 re_buffer_ptr
+= 2*(re_bit_count
>>4);
540 re_cache
= ((re_buffer_ptr
[-2]<<8) + re_buffer_ptr
[-1]) << (16+re_bit_count
);
545 #elif defined A32_BITSTREAM_READER
547 # define MIN_CACHE_BITS 32
549 # define OPEN_READER(name, gb)\
550 int name##_bit_count=(gb)->bit_count;\
551 uint32_t name##_cache0= (gb)->cache0;\
552 uint32_t name##_cache1= (gb)->cache1;\
553 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
555 # define CLOSE_READER(name, gb)\
556 (gb)->bit_count= name##_bit_count;\
557 (gb)->cache0= name##_cache0;\
558 (gb)->cache1= name##_cache1;\
559 (gb)->buffer_ptr= name##_buffer_ptr;\
561 # define UPDATE_CACHE(name, gb)\
562 if(name##_bit_count > 0){\
563 const uint32_t next= be2me_32( *name##_buffer_ptr );\
564 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
565 name##_cache1 |= next<<name##_bit_count;\
566 name##_buffer_ptr++;\
567 name##_bit_count-= 32;\
570 #if defined(ARCH_X86)
571 # define SKIP_CACHE(name, gb, num)\
573 "shldl %2, %1, %0 \n\t"\
575 : "+r" (name##_cache0), "+r" (name##_cache1)\
576 : "Ic" ((uint8_t)(num))\
579 # define SKIP_CACHE(name, gb, num)\
580 name##_cache0 <<= (num);\
581 name##_cache0 |= NEG_USR32(name##_cache1,num);\
582 name##_cache1 <<= (num);
585 # define SKIP_COUNTER(name, gb, num)\
586 name##_bit_count += (num);\
588 # define SKIP_BITS(name, gb, num)\
590 SKIP_CACHE(name, gb, num)\
591 SKIP_COUNTER(name, gb, num)\
594 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
595 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
597 # define SHOW_UBITS(name, gb, num)\
598 NEG_USR32(name##_cache0, num)
600 # define SHOW_SBITS(name, gb, num)\
601 NEG_SSR32(name##_cache0, num)
603 # define GET_CACHE(name, gb)\
606 static inline int get_bits_count(GetBitContext
*s
){
607 return ((uint8_t*)s
->buffer_ptr
- s
->buffer
)*8 - 32 + s
->bit_count
;
610 static inline void skip_bits_long(GetBitContext
*s
, int n
){
613 re_buffer_ptr
+= re_bit_count
>>5;
615 re_cache0
= be2me_32( re_buffer_ptr
[-1] ) << re_bit_count
;
624 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
625 * if MSB not set it is negative
626 * @param n length in bits
629 static inline int get_xbits(GetBitContext
*s
, int n
){
631 register int32_t cache
;
634 cache
= GET_CACHE(re
,s
);
636 LAST_SKIP_BITS(re
, s
, n
)
638 return (NEG_USR32(sign
^ cache
, n
) ^ sign
) - sign
;
641 static inline int get_sbits(GetBitContext
*s
, int n
){
645 tmp
= SHOW_SBITS(re
, s
, n
);
646 LAST_SKIP_BITS(re
, s
, n
)
653 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
655 static inline unsigned int get_bits(GetBitContext
*s
, int n
){
659 tmp
= SHOW_UBITS(re
, s
, n
);
660 LAST_SKIP_BITS(re
, s
, n
)
667 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
669 static inline unsigned int show_bits(GetBitContext
*s
, int n
){
673 tmp
= SHOW_UBITS(re
, s
, n
);
674 // CLOSE_READER(re, s)
678 static inline void skip_bits(GetBitContext
*s
, int n
){
679 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
682 LAST_SKIP_BITS(re
, s
, n
)
686 static inline unsigned int get_bits1(GetBitContext
*s
){
687 #ifdef ALT_BITSTREAM_READER
689 uint8_t result
= s
->buffer
[ index
>>3 ];
690 #ifdef ALT_BITSTREAM_READER_LE
691 result
>>= (index
&0x07);
694 result
<<= (index
&0x07);
702 return get_bits(s
, 1);
706 static inline unsigned int show_bits1(GetBitContext
*s
){
707 return show_bits(s
, 1);
710 static inline void skip_bits1(GetBitContext
*s
){
717 static inline unsigned int get_bits_long(GetBitContext
*s
, int n
){
718 if(n
<=17) return get_bits(s
, n
);
720 #ifdef ALT_BITSTREAM_READER_LE
721 int ret
= get_bits(s
, 16);
722 return ret
| (get_bits(s
, n
-16) << 16);
724 int ret
= get_bits(s
, 16) << (n
-16);
725 return ret
| get_bits(s
, n
-16);
733 static inline unsigned int show_bits_long(GetBitContext
*s
, int n
){
734 if(n
<=17) return show_bits(s
, n
);
736 GetBitContext gb
= *s
;
737 int ret
= get_bits_long(s
, n
);
743 static inline int check_marker(GetBitContext
*s
, const char *msg
)
745 int bit
= get_bits1(s
);
747 av_log(NULL
, AV_LOG_INFO
, "Marker bit missing %s\n", msg
);
753 * init GetBitContext.
754 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
755 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
756 * @param bit_size the size of the buffer in bits
758 static inline void init_get_bits(GetBitContext
*s
,
759 const uint8_t *buffer
, int bit_size
)
761 int buffer_size
= (bit_size
+7)>>3;
762 if(buffer_size
< 0 || bit_size
< 0) {
763 buffer_size
= bit_size
= 0;
768 s
->size_in_bits
= bit_size
;
769 s
->buffer_end
= buffer
+ buffer_size
;
770 #ifdef ALT_BITSTREAM_READER
772 #elif defined LIBMPEG2_BITSTREAM_READER
773 s
->buffer_ptr
= (uint8_t*)((intptr_t)buffer
&(~1));
774 s
->bit_count
= 16 + 8*((intptr_t)buffer
&1);
775 skip_bits_long(s
, 0);
776 #elif defined A32_BITSTREAM_READER
777 s
->buffer_ptr
= (uint32_t*)((intptr_t)buffer
&(~3));
778 s
->bit_count
= 32 + 8*((intptr_t)buffer
&3);
779 skip_bits_long(s
, 0);
783 static inline void align_get_bits(GetBitContext
*s
)
785 int n
= (-get_bits_count(s
)) & 7;
786 if(n
) skip_bits(s
, n
);
789 #define init_vlc(vlc, nb_bits, nb_codes,\
790 bits, bits_wrap, bits_size,\
791 codes, codes_wrap, codes_size,\
793 init_vlc_sparse(vlc, nb_bits, nb_codes,\
794 bits, bits_wrap, bits_size,\
795 codes, codes_wrap, codes_size,\
798 int init_vlc_sparse(VLC
*vlc
, int nb_bits
, int nb_codes
,
799 const void *bits
, int bits_wrap
, int bits_size
,
800 const void *codes
, int codes_wrap
, int codes_size
,
801 const void *symbols
, int symbols_wrap
, int symbols_size
,
803 #define INIT_VLC_USE_STATIC 1 ///< VERY strongly deprecated and forbidden
804 #define INIT_VLC_LE 2
805 #define INIT_VLC_USE_NEW_STATIC 4
806 void free_vlc(VLC
*vlc
);
808 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
810 static VLC_TYPE table[static_size][2];\
811 (vlc)->table= table;\
812 (vlc)->table_allocated= static_size;\
813 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
819 * if the vlc code is invalid and max_depth=1 than no bits will be removed
820 * if the vlc code is invalid and max_depth>1 than the number of bits removed
823 #define GET_VLC(code, name, gb, table, bits, max_depth)\
825 int n, index, nb_bits;\
827 index= SHOW_UBITS(name, gb, bits);\
828 code = table[index][0];\
829 n = table[index][1];\
831 if(max_depth > 1 && n < 0){\
832 LAST_SKIP_BITS(name, gb, bits)\
833 UPDATE_CACHE(name, gb)\
837 index= SHOW_UBITS(name, gb, nb_bits) + code;\
838 code = table[index][0];\
839 n = table[index][1];\
840 if(max_depth > 2 && n < 0){\
841 LAST_SKIP_BITS(name, gb, nb_bits)\
842 UPDATE_CACHE(name, gb)\
846 index= SHOW_UBITS(name, gb, nb_bits) + code;\
847 code = table[index][0];\
848 n = table[index][1];\
851 SKIP_BITS(name, gb, n)\
854 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
856 int n, index, nb_bits;\
858 index= SHOW_UBITS(name, gb, bits);\
859 level = table[index].level;\
860 n = table[index].len;\
862 if(max_depth > 1 && n < 0){\
863 SKIP_BITS(name, gb, bits)\
865 UPDATE_CACHE(name, gb)\
870 index= SHOW_UBITS(name, gb, nb_bits) + level;\
871 level = table[index].level;\
872 n = table[index].len;\
874 run= table[index].run;\
875 SKIP_BITS(name, gb, n)\
880 * parses a vlc code, faster then get_vlc()
881 * @param bits is the number of bits which will be read at once, must be
882 * identical to nb_bits in init_vlc()
883 * @param max_depth is the number of times bits bits must be read to completely
884 * read the longest vlc code
885 * = (max_vlc_length + bits - 1) / bits
887 static av_always_inline
int get_vlc2(GetBitContext
*s
, VLC_TYPE (*table
)[2],
888 int bits
, int max_depth
)
895 GET_VLC(code
, re
, s
, table
, bits
, max_depth
)
904 static inline void print_bin(int bits
, int n
){
907 for(i
=n
-1; i
>=0; i
--){
908 av_log(NULL
, AV_LOG_DEBUG
, "%d", (bits
>>i
)&1);
911 av_log(NULL
, AV_LOG_DEBUG
, " ");
914 static inline int get_bits_trace(GetBitContext
*s
, int n
, char *file
, const char *func
, int line
){
915 int r
= get_bits(s
, n
);
918 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
);
921 static inline int get_vlc_trace(GetBitContext
*s
, VLC_TYPE (*table
)[2], int bits
, int max_depth
, char *file
, const char *func
, int line
){
922 int show
= show_bits(s
, 24);
923 int pos
= get_bits_count(s
);
924 int r
= get_vlc2(s
, table
, bits
, max_depth
);
925 int len
= get_bits_count(s
) - pos
;
926 int bits2
= show
>>(24-len
);
928 print_bin(bits2
, len
);
930 av_log(NULL
, AV_LOG_DEBUG
, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2
, len
, r
, pos
, file
, func
, line
);
933 static inline int get_xbits_trace(GetBitContext
*s
, int n
, char *file
, const char *func
, int line
){
934 int show
= show_bits(s
, n
);
935 int r
= get_xbits(s
, n
);
938 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
);
942 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
943 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
944 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
945 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
946 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
948 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
951 #define tprintf(p, ...) {}
954 static inline int decode012(GetBitContext
*gb
){
960 return get_bits1(gb
) + 1;
963 static inline int decode210(GetBitContext
*gb
){
967 return 2 - get_bits1(gb
);
970 #endif /* FFMPEG_BITSTREAM_H */