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
29 #include "ffmpeg_bswap.h"
31 /* The following 2 defines are taken from libavutil/intreadwrite.h */
32 #define AV_RB32(x) ((((const uint8_t*)(x))[0] << 24) | \
33 (((const uint8_t*)(x))[1] << 16) | \
34 (((const uint8_t*)(x))[2] << 8) | \
35 ((const uint8_t*)(x))[3])
36 #define AV_WB32(p, d) do { \
37 ((uint8_t*)(p))[3] = (d); \
38 ((uint8_t*)(p))[2] = (d)>>8; \
39 ((uint8_t*)(p))[1] = (d)>>16; \
40 ((uint8_t*)(p))[0] = (d)>>24; } while(0)
42 #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
43 # define ALT_BITSTREAM_READER
46 //#define ALT_BITSTREAM_WRITER
47 //#define ALIGNED_BITSTREAM_WRITER
48 #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
49 # if defined(ARCH_ARM)
50 # define A32_BITSTREAM_READER
52 # define ALT_BITSTREAM_READER
53 //#define LIBMPEG2_BITSTREAM_READER
54 //#define A32_BITSTREAM_READER
58 extern const uint8_t ff_reverse
[256];
61 // avoid +32 for shift optimization (gcc should do that ...)
62 static inline int32_t NEG_SSR32( int32_t a
, int8_t s
){
63 __asm__ ("sarl %1, %0\n\t"
65 : "ic" ((uint8_t)(-s
))
69 static inline uint32_t NEG_USR32(uint32_t a
, int8_t s
){
70 __asm__ ("shrl %1, %0\n\t"
72 : "ic" ((uint8_t)(-s
))
77 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
78 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
83 /* buf and buf_end must be present and used by every alternative writer. */
84 typedef struct PutBitContext
{
85 #ifdef ALT_BITSTREAM_WRITER
86 uint8_t *buf
, *buf_end
;
91 uint8_t *buf
, *buf_ptr
, *buf_end
;
96 static inline void init_put_bits(PutBitContext
*s
, uint8_t *buffer
, int buffer_size
)
103 s
->size_in_bits
= 8*buffer_size
;
105 s
->buf_end
= s
->buf
+ buffer_size
;
106 #ifdef ALT_BITSTREAM_WRITER
108 ((uint32_t*)(s
->buf
))[0]=0;
109 // memset(buffer, 0, buffer_size);
117 /* return the number of bits output */
118 static inline int put_bits_count(PutBitContext
*s
)
120 #ifdef ALT_BITSTREAM_WRITER
123 return (s
->buf_ptr
- s
->buf
) * 8 + 32 - s
->bit_left
;
127 /* pad the end of the output stream with zeros */
128 static inline void flush_put_bits(PutBitContext
*s
)
130 #ifdef ALT_BITSTREAM_WRITER
133 #ifndef BITSTREAM_WRITER_LE
134 s
->bit_buf
<<= s
->bit_left
;
136 while (s
->bit_left
< 32) {
137 /* XXX: should test end of buffer */
138 #ifdef BITSTREAM_WRITER_LE
139 *s
->buf_ptr
++=s
->bit_buf
;
142 *s
->buf_ptr
++=s
->bit_buf
>> 24;
152 void align_put_bits(PutBitContext
*s
);
153 void ff_put_string(PutBitContext
* pbc
, const char *s
, int put_zero
);
154 void ff_copy_bits(PutBitContext
*pb
, const uint8_t *src
, int length
);
157 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
158 typedef struct GetBitContext
{
159 const uint8_t *buffer
, *buffer_end
;
160 #ifdef ALT_BITSTREAM_READER
162 #elif defined LIBMPEG2_BITSTREAM_READER
166 #elif defined A32_BITSTREAM_READER
167 uint32_t *buffer_ptr
;
175 #define VLC_TYPE int16_t
179 VLC_TYPE (*table
)[2]; ///< code, bits
180 int table_size
, table_allocated
;
183 typedef struct RL_VLC_ELEM
{
189 #ifndef ALT_BITSTREAM_WRITER
190 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
192 unsigned int bit_buf
;
195 // printf("put_bits=%d %x\n", n, value);
196 //assert(n == 32 || value < (1U << n));
198 bit_buf
= s
->bit_buf
;
199 bit_left
= s
->bit_left
;
201 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
203 #ifdef BITSTREAM_WRITER_LE
204 bit_buf
|= value
<< (32 - bit_left
);
206 #if !HAVE_FAST_UNALIGNED
207 if (3 & (intptr_t) s
->buf_ptr
) {
208 AV_WL32(s
->buf_ptr
, bit_buf
);
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 #if !defined(HAVE_FAST_UNALIGNED)
225 if (3 & (intptr_t) s
->buf_ptr
) {
226 AV_WB32(s
->buf_ptr
, bit_buf
);
229 *(uint32_t *)s
->buf_ptr
= be2me_32(bit_buf
);
230 //printf("bitbuf = %08x\n", bit_buf);
237 s
->bit_buf
= bit_buf
;
238 s
->bit_left
= bit_left
;
243 #ifdef ALT_BITSTREAM_WRITER
244 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
246 # ifdef ALIGNED_BITSTREAM_WRITER
249 "movl %0, %%ecx \n\t"
250 "xorl %%eax, %%eax \n\t"
251 "shrdl %%cl, %1, %%eax \n\t"
253 "movl %0, %%ecx \n\t"
254 "shrl $3, %%ecx \n\t"
255 "andl $0xFFFFFFFC, %%ecx \n\t"
257 "orl %1, (%2, %%ecx) \n\t"
260 "movl %%eax, 4(%2, %%ecx) \n\t"
261 : "=&r" (s
->index
), "=&r" (value
)
262 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
<<(-n
))
267 uint32_t *ptr
= ((uint32_t *)s
->buf
)+(index
>>5);
271 ptr
[0] |= be2me_32(value
>>(index
&31));
272 ptr
[1] = be2me_32(value
<<(32-(index
&31)));
273 //if(n>24) printf("%d %d\n", n, value);
277 # else //ALIGNED_BITSTREAM_WRITER
280 "movl $7, %%ecx \n\t"
281 "andl %0, %%ecx \n\t"
282 "addl %3, %%ecx \n\t"
286 "movl %0, %%ecx \n\t"
287 "shrl $3, %%ecx \n\t"
288 "orl %1, (%%ecx, %2) \n\t"
290 "movl $0, 4(%%ecx, %2) \n\t"
291 : "=&r" (s
->index
), "=&r" (value
)
292 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
)
297 uint32_t *ptr
= (uint32_t*)(((uint8_t *)s
->buf
)+(index
>>3));
299 ptr
[0] |= be2me_32(value
<<(32-n
-(index
&7) ));
301 //if(n>24) printf("%d %d\n", n, value);
305 # endif //!ALIGNED_BITSTREAM_WRITER
309 static inline void put_sbits(PutBitContext
*pb
, int bits
, int32_t val
)
311 //assert(bits >= 0 && bits <= 31);
313 put_bits(pb
, bits
, val
& ((1<<bits
)-1));
317 static inline uint8_t* pbBufPtr(PutBitContext
*s
)
319 #ifdef ALT_BITSTREAM_WRITER
320 return s
->buf
+ (s
->index
>>3);
328 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
330 static inline void skip_put_bytes(PutBitContext
*s
, int n
){
331 //assert((put_bits_count(s)&7)==0);
332 #ifdef ALT_BITSTREAM_WRITER
333 FIXME may need some cleaning of the buffer
336 //assert(s->bit_left==32);
342 * Skips the given number of bits.
343 * Must only be used if the actual values in the bitstream do not matter.
345 static inline void skip_put_bits(PutBitContext
*s
, int n
){
346 #ifdef ALT_BITSTREAM_WRITER
350 s
->buf_ptr
-= s
->bit_left
>>5;
356 * Changes the end of the buffer.
358 static inline void set_put_bits_buffer_size(PutBitContext
*s
, int size
){
359 s
->buf_end
= s
->buf
+ size
;
362 /* Bitstream reader API docs:
364 arbitrary name which is used as prefix for the internal variables
369 OPEN_READER(name, gb)
370 loads gb into local variables
372 CLOSE_READER(name, gb)
373 stores local vars in gb
375 UPDATE_CACHE(name, gb)
376 refills the internal cache from the bitstream
377 after this call at least MIN_CACHE_BITS will be available,
380 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
382 SHOW_UBITS(name, gb, num)
383 will return the next num bits
385 SHOW_SBITS(name, gb, num)
386 will return the next num bits and do sign extension
388 SKIP_BITS(name, gb, num)
389 will skip over the next num bits
390 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
392 SKIP_CACHE(name, gb, num)
393 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
395 SKIP_COUNTER(name, gb, num)
396 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
398 LAST_SKIP_CACHE(name, gb, num)
399 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
401 LAST_SKIP_BITS(name, gb, num)
402 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
404 for examples see get_bits, show_bits, skip_bits, get_vlc
407 #ifdef ALT_BITSTREAM_READER
408 # define MIN_CACHE_BITS 25
410 # define OPEN_READER(name, gb)\
411 int name##_index= (gb)->index;\
412 int name##_cache= 0;\
414 # define CLOSE_READER(name, gb)\
415 (gb)->index= name##_index;\
417 # ifdef ALT_BITSTREAM_READER_LE
418 # define UPDATE_CACHE(name, gb)\
419 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
421 # define SKIP_CACHE(name, gb, num)\
422 name##_cache >>= (num);
424 # define UPDATE_CACHE(name, gb)\
425 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
427 # define SKIP_CACHE(name, gb, num)\
428 name##_cache <<= (num);
432 # define SKIP_COUNTER(name, gb, num)\
433 name##_index += (num);\
435 # define SKIP_BITS(name, gb, num)\
437 SKIP_CACHE(name, gb, num)\
438 SKIP_COUNTER(name, gb, num)\
441 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
442 # define LAST_SKIP_CACHE(name, gb, num) ;
444 # ifdef ALT_BITSTREAM_READER_LE
445 # define SHOW_UBITS(name, gb, num)\
446 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
448 # define SHOW_SBITS(name, gb, num)\
449 NEG_SSR32((name##_cache)<<(32-(num)), num)
451 # define SHOW_UBITS(name, gb, num)\
452 NEG_USR32(name##_cache, num)
454 # define SHOW_SBITS(name, gb, num)\
455 NEG_SSR32(name##_cache, num)
458 # define GET_CACHE(name, gb)\
459 ((uint32_t)name##_cache)
461 static inline int get_bits_count(GetBitContext
*s
){
465 static inline void skip_bits_long(GetBitContext
*s
, int n
){
469 #elif defined LIBMPEG2_BITSTREAM_READER
470 //libmpeg2 like reader
472 # define MIN_CACHE_BITS 17
474 # define OPEN_READER(name, gb)\
475 int name##_bit_count=(gb)->bit_count;\
476 int name##_cache= (gb)->cache;\
477 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
479 # define CLOSE_READER(name, gb)\
480 (gb)->bit_count= name##_bit_count;\
481 (gb)->cache= name##_cache;\
482 (gb)->buffer_ptr= name##_buffer_ptr;\
484 # define UPDATE_CACHE(name, gb)\
485 if(name##_bit_count >= 0){\
486 name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
487 name##_buffer_ptr+=2;\
488 name##_bit_count-= 16;\
491 # define SKIP_CACHE(name, gb, num)\
492 name##_cache <<= (num);\
494 # define SKIP_COUNTER(name, gb, num)\
495 name##_bit_count += (num);\
497 # define SKIP_BITS(name, gb, num)\
499 SKIP_CACHE(name, gb, num)\
500 SKIP_COUNTER(name, gb, num)\
503 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
504 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
506 # define SHOW_UBITS(name, gb, num)\
507 NEG_USR32(name##_cache, num)
509 # define SHOW_SBITS(name, gb, num)\
510 NEG_SSR32(name##_cache, num)
512 # define GET_CACHE(name, gb)\
513 ((uint32_t)name##_cache)
515 static inline int get_bits_count(GetBitContext
*s
){
516 return (s
->buffer_ptr
- s
->buffer
)*8 - 16 + s
->bit_count
;
519 static inline void skip_bits_long(GetBitContext
*s
, int n
){
522 re_buffer_ptr
+= 2*(re_bit_count
>>4);
524 re_cache
= ((re_buffer_ptr
[-2]<<8) + re_buffer_ptr
[-1]) << (16+re_bit_count
);
529 #elif defined A32_BITSTREAM_READER
531 # define MIN_CACHE_BITS 32
533 # define OPEN_READER(name, gb)\
534 int name##_bit_count=(gb)->bit_count;\
535 uint32_t name##_cache0= (gb)->cache0;\
536 uint32_t name##_cache1= (gb)->cache1;\
537 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
539 # define CLOSE_READER(name, gb)\
540 (gb)->bit_count= name##_bit_count;\
541 (gb)->cache0= name##_cache0;\
542 (gb)->cache1= name##_cache1;\
543 (gb)->buffer_ptr= name##_buffer_ptr;\
545 # define UPDATE_CACHE(name, gb)\
546 if(name##_bit_count > 0){\
547 const uint32_t next= be2me_32( *name##_buffer_ptr );\
548 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
549 name##_cache1 |= next<<name##_bit_count;\
550 name##_buffer_ptr++;\
551 name##_bit_count-= 32;\
555 # define SKIP_CACHE(name, gb, num)\
557 "shldl %2, %1, %0 \n\t"\
559 : "+r" (name##_cache0), "+r" (name##_cache1)\
560 : "Ic" ((uint8_t)(num))\
563 # define SKIP_CACHE(name, gb, num)\
564 name##_cache0 <<= (num);\
565 name##_cache0 |= NEG_USR32(name##_cache1,num);\
566 name##_cache1 <<= (num);
569 # define SKIP_COUNTER(name, gb, num)\
570 name##_bit_count += (num);\
572 # define SKIP_BITS(name, gb, num)\
574 SKIP_CACHE(name, gb, num)\
575 SKIP_COUNTER(name, gb, num)\
578 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
579 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
581 # define SHOW_UBITS(name, gb, num)\
582 NEG_USR32(name##_cache0, num)
584 # define SHOW_SBITS(name, gb, num)\
585 NEG_SSR32(name##_cache0, num)
587 # define GET_CACHE(name, gb)\
590 static inline int get_bits_count(GetBitContext
*s
){
591 return ((uint8_t*)s
->buffer_ptr
- s
->buffer
)*8 - 32 + s
->bit_count
;
594 static inline void skip_bits_long(GetBitContext
*s
, int n
){
597 re_buffer_ptr
+= re_bit_count
>>5;
599 re_cache0
= be2me_32( re_buffer_ptr
[-1] ) << re_bit_count
;
608 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
609 * if MSB not set it is negative
610 * @param n length in bits
613 static inline int get_xbits(GetBitContext
*s
, int n
){
615 register int32_t cache
;
618 cache
= GET_CACHE(re
,s
);
620 LAST_SKIP_BITS(re
, s
, n
)
622 return (NEG_USR32(sign
^ cache
, n
) ^ sign
) - sign
;
625 static inline int get_sbits(GetBitContext
*s
, int n
){
629 tmp
= SHOW_SBITS(re
, s
, n
);
630 LAST_SKIP_BITS(re
, s
, n
)
637 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
639 static inline unsigned int get_bits(GetBitContext
*s
, int n
){
643 tmp
= SHOW_UBITS(re
, s
, n
);
644 LAST_SKIP_BITS(re
, s
, n
)
651 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
653 static inline unsigned int show_bits(GetBitContext
*s
, int n
){
657 tmp
= SHOW_UBITS(re
, s
, n
);
658 // CLOSE_READER(re, s)
662 static inline void skip_bits(GetBitContext
*s
, int n
){
663 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
666 LAST_SKIP_BITS(re
, s
, n
)
670 static inline unsigned int get_bits1(GetBitContext
*s
){
671 #ifdef ALT_BITSTREAM_READER
673 uint8_t result
= s
->buffer
[ index
>>3 ];
674 #ifdef ALT_BITSTREAM_READER_LE
675 result
>>= (index
&0x07);
678 result
<<= (index
&0x07);
686 return get_bits(s
, 1);
690 static inline unsigned int show_bits1(GetBitContext
*s
){
691 return show_bits(s
, 1);
694 static inline void skip_bits1(GetBitContext
*s
){
701 static inline unsigned int get_bits_long(GetBitContext
*s
, int n
){
702 if(n
<=17) return get_bits(s
, n
);
704 #ifdef ALT_BITSTREAM_READER_LE
705 int ret
= get_bits(s
, 16);
706 return ret
| (get_bits(s
, n
-16) << 16);
708 int ret
= get_bits(s
, 16) << (n
-16);
709 return ret
| get_bits(s
, n
-16);
716 * reads 0-32 bits as a signed integer.
718 static inline int get_sbits_long(GetBitContext
*s
, int n
) {
719 return sign_extend(get_bits_long(s
, n
), n
);
726 static inline unsigned int show_bits_long(GetBitContext
*s
, int n
){
727 if(n
<=17) return show_bits(s
, n
);
729 GetBitContext gb
= *s
;
730 return get_bits_long(&gb
, n
);
735 static inline int check_marker(GetBitContext
*s
, const char *msg
)
737 int bit
= get_bits1(s
);
739 printf("Marker bit missing %s\n", msg
);
746 * init GetBitContext.
747 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
748 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
749 * @param bit_size the size of the buffer in bits
751 static inline void init_get_bits(GetBitContext
*s
,
752 const uint8_t *buffer
, int bit_size
)
754 int buffer_size
= (bit_size
+7)>>3;
755 if(buffer_size
< 0 || bit_size
< 0) {
756 buffer_size
= bit_size
= 0;
761 s
->size_in_bits
= bit_size
;
762 s
->buffer_end
= buffer
+ buffer_size
;
763 #ifdef ALT_BITSTREAM_READER
765 #elif defined LIBMPEG2_BITSTREAM_READER
766 s
->buffer_ptr
= (uint8_t*)((intptr_t)buffer
&(~1));
767 s
->bit_count
= 16 + 8*((intptr_t)buffer
&1);
768 skip_bits_long(s
, 0);
769 #elif defined A32_BITSTREAM_READER
770 s
->buffer_ptr
= (uint32_t*)((intptr_t)buffer
&(~3));
771 s
->bit_count
= 32 + 8*((intptr_t)buffer
&3);
772 skip_bits_long(s
, 0);
776 static inline void align_get_bits(GetBitContext
*s
)
778 int n
= (-get_bits_count(s
)) & 7;
779 if(n
) skip_bits(s
, n
);
782 #define init_vlc(vlc, nb_bits, nb_codes,\
783 bits, bits_wrap, bits_size,\
784 codes, codes_wrap, codes_size,\
786 init_vlc_sparse(vlc, nb_bits, nb_codes,\
787 bits, bits_wrap, bits_size,\
788 codes, codes_wrap, codes_size,\
791 int init_vlc_sparse(VLC
*vlc
, int nb_bits
, int nb_codes
,
792 const void *bits
, int bits_wrap
, int bits_size
,
793 const void *codes
, int codes_wrap
, int codes_size
,
794 const void *symbols
, int symbols_wrap
, int symbols_size
,
796 #define INIT_VLC_USE_STATIC 1 ///< VERY strongly deprecated and forbidden
797 #define INIT_VLC_LE 2
798 #define INIT_VLC_USE_NEW_STATIC 4
799 void free_vlc(VLC
*vlc
);
801 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
803 static VLC_TYPE table[static_size][2];\
804 (vlc)->table= table;\
805 (vlc)->table_allocated= static_size;\
806 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
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 read to completely
877 * read the longest vlc code
878 * = (max_vlc_length + bits - 1) / bits
880 static 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 printf("%d", (bits
>>i
)&1);
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 printf("%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 printf("%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 printf("%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(p, ...) printf
944 #define tprintf(p, ...) {}
947 static inline int decode012(GetBitContext
*gb
){
953 return get_bits1(gb
) + 1;
956 static inline int decode210(GetBitContext
*gb
){
960 return 2 - get_bits1(gb
);
963 #endif /* BITSTREAM_H */