r1009: Move the dependencies to newer package names
[cinelerra_cv/mob.git] / quicktime / ffmpeg / libavcodec / bitstream.h
blob0e60ea1d444242d9d8a006cfec3db83c8c8bb920
1 /**
2 * @file bitstream.h
3 * bitstream api header.
4 */
6 #ifndef BITSTREAM_H
7 #define BITSTREAM_H
9 //#define ALT_BITSTREAM_WRITER
10 //#define ALIGNED_BITSTREAM_WRITER
12 #define ALT_BITSTREAM_READER
13 //#define LIBMPEG2_BITSTREAM_READER
14 //#define A32_BITSTREAM_READER
15 #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
17 extern const uint8_t ff_reverse[256];
19 #if defined(ARCH_X86) || defined(ARCH_X86_64)
20 // avoid +32 for shift optimization (gcc should do that ...)
21 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
22 asm ("sarl %1, %0\n\t"
23 : "+r" (a)
24 : "ic" ((uint8_t)(-s))
26 return a;
28 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
29 asm ("shrl %1, %0\n\t"
30 : "+r" (a)
31 : "ic" ((uint8_t)(-s))
33 return a;
35 #else
36 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
37 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
38 #endif
40 /* bit output */
42 /* buf and buf_end must be present and used by every alternative writer. */
43 typedef struct PutBitContext {
44 #ifdef ALT_BITSTREAM_WRITER
45 uint8_t *buf, *buf_end;
46 int index;
47 #else
48 uint32_t bit_buf;
49 int bit_left;
50 uint8_t *buf, *buf_ptr, *buf_end;
51 #endif
52 } PutBitContext;
54 static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
56 s->buf = buffer;
57 s->buf_end = s->buf + buffer_size;
58 #ifdef ALT_BITSTREAM_WRITER
59 s->index=0;
60 ((uint32_t*)(s->buf))[0]=0;
61 // memset(buffer, 0, buffer_size);
62 #else
63 s->buf_ptr = s->buf;
64 s->bit_left=32;
65 s->bit_buf=0;
66 #endif
69 /* return the number of bits output */
70 static inline int put_bits_count(PutBitContext *s)
72 #ifdef ALT_BITSTREAM_WRITER
73 return s->index;
74 #else
75 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
76 #endif
79 /* pad the end of the output stream with zeros */
80 static inline void flush_put_bits(PutBitContext *s)
82 #ifdef ALT_BITSTREAM_WRITER
83 align_put_bits(s);
84 #else
85 s->bit_buf<<= s->bit_left;
86 while (s->bit_left < 32) {
87 /* XXX: should test end of buffer */
88 *s->buf_ptr++=s->bit_buf >> 24;
89 s->bit_buf<<=8;
90 s->bit_left+=8;
92 s->bit_left=32;
93 s->bit_buf=0;
94 #endif
97 void align_put_bits(PutBitContext *s);
98 void put_string(PutBitContext * pbc, char *s, int put_zero);
100 /* bit input */
101 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
102 typedef struct GetBitContext {
103 const uint8_t *buffer, *buffer_end;
104 #ifdef ALT_BITSTREAM_READER
105 int index;
106 #elif defined LIBMPEG2_BITSTREAM_READER
107 uint8_t *buffer_ptr;
108 uint32_t cache;
109 int bit_count;
110 #elif defined A32_BITSTREAM_READER
111 uint32_t *buffer_ptr;
112 uint32_t cache0;
113 uint32_t cache1;
114 int bit_count;
115 #endif
116 int size_in_bits;
117 } GetBitContext;
119 #define VLC_TYPE int16_t
121 typedef struct VLC {
122 int bits;
123 VLC_TYPE (*table)[2]; ///< code, bits
124 int table_size, table_allocated;
125 } VLC;
127 typedef struct RL_VLC_ELEM {
128 int16_t level;
129 int8_t len;
130 uint8_t run;
131 } RL_VLC_ELEM;
133 #if defined(ARCH_SPARC) || defined(ARCH_ARMV4L)
134 #define UNALIGNED_STORES_ARE_BAD
135 #endif
137 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
138 #if defined(ARCH_X86) || defined(ARCH_X86_64)
139 # define unaligned32(a) (*(const uint32_t*)(a))
140 #else
141 # ifdef __GNUC__
142 static inline uint32_t unaligned32(const void *v) {
143 struct Unaligned {
144 uint32_t i;
145 } __attribute__((packed));
147 return ((const struct Unaligned *) v)->i;
149 # elif defined(__DECC)
150 static inline uint32_t unaligned32(const void *v) {
151 return *(const __unaligned uint32_t *) v;
153 # else
154 static inline uint32_t unaligned32(const void *v) {
155 return *(const uint32_t *) v;
157 # endif
158 #endif //!ARCH_X86
160 #ifndef ALT_BITSTREAM_WRITER
161 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
163 unsigned int bit_buf;
164 int bit_left;
166 #ifdef STATS
167 st_out_bit_counts[st_current_index] += n;
168 #endif
169 // printf("put_bits=%d %x\n", n, value);
170 assert(n == 32 || value < (1U << n));
172 bit_buf = s->bit_buf;
173 bit_left = s->bit_left;
175 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
176 /* XXX: optimize */
177 if (n < bit_left) {
178 bit_buf = (bit_buf<<n) | value;
179 bit_left-=n;
180 } else {
181 bit_buf<<=bit_left;
182 bit_buf |= value >> (n - bit_left);
183 #ifdef UNALIGNED_STORES_ARE_BAD
184 if (3 & (intptr_t) s->buf_ptr) {
185 s->buf_ptr[0] = bit_buf >> 24;
186 s->buf_ptr[1] = bit_buf >> 16;
187 s->buf_ptr[2] = bit_buf >> 8;
188 s->buf_ptr[3] = bit_buf ;
189 } else
190 #endif
191 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
192 //printf("bitbuf = %08x\n", bit_buf);
193 s->buf_ptr+=4;
194 bit_left+=32 - n;
195 bit_buf = value;
198 s->bit_buf = bit_buf;
199 s->bit_left = bit_left;
201 #endif
204 #ifdef ALT_BITSTREAM_WRITER
205 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
207 # ifdef ALIGNED_BITSTREAM_WRITER
208 # if defined(ARCH_X86) || defined(ARCH_X86_64)
209 asm volatile(
210 "movl %0, %%ecx \n\t"
211 "xorl %%eax, %%eax \n\t"
212 "shrdl %%cl, %1, %%eax \n\t"
213 "shrl %%cl, %1 \n\t"
214 "movl %0, %%ecx \n\t"
215 "shrl $3, %%ecx \n\t"
216 "andl $0xFFFFFFFC, %%ecx \n\t"
217 "bswapl %1 \n\t"
218 "orl %1, (%2, %%ecx) \n\t"
219 "bswapl %%eax \n\t"
220 "addl %3, %0 \n\t"
221 "movl %%eax, 4(%2, %%ecx) \n\t"
222 : "=&r" (s->index), "=&r" (value)
223 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
224 : "%eax", "%ecx"
226 # else
227 int index= s->index;
228 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
230 value<<= 32-n;
232 ptr[0] |= be2me_32(value>>(index&31));
233 ptr[1] = be2me_32(value<<(32-(index&31)));
234 //if(n>24) printf("%d %d\n", n, value);
235 index+= n;
236 s->index= index;
237 # endif
238 # else //ALIGNED_BITSTREAM_WRITER
239 # if defined(ARCH_X86) || defined(ARCH_X86_64)
240 asm volatile(
241 "movl $7, %%ecx \n\t"
242 "andl %0, %%ecx \n\t"
243 "addl %3, %%ecx \n\t"
244 "negl %%ecx \n\t"
245 "shll %%cl, %1 \n\t"
246 "bswapl %1 \n\t"
247 "movl %0, %%ecx \n\t"
248 "shrl $3, %%ecx \n\t"
249 "orl %1, (%%ecx, %2) \n\t"
250 "addl %3, %0 \n\t"
251 "movl $0, 4(%%ecx, %2) \n\t"
252 : "=&r" (s->index), "=&r" (value)
253 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
254 : "%ecx"
256 # else
257 int index= s->index;
258 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
260 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
261 ptr[1] = 0;
262 //if(n>24) printf("%d %d\n", n, value);
263 index+= n;
264 s->index= index;
265 # endif
266 # endif //!ALIGNED_BITSTREAM_WRITER
268 #endif
271 static inline uint8_t* pbBufPtr(PutBitContext *s)
273 #ifdef ALT_BITSTREAM_WRITER
274 return s->buf + (s->index>>3);
275 #else
276 return s->buf_ptr;
277 #endif
282 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
284 static inline void skip_put_bytes(PutBitContext *s, int n){
285 assert((put_bits_count(s)&7)==0);
286 #ifdef ALT_BITSTREAM_WRITER
287 FIXME may need some cleaning of the buffer
288 s->index += n<<3;
289 #else
290 assert(s->bit_left==32);
291 s->buf_ptr += n;
292 #endif
296 * skips the given number of bits.
297 * must only be used if the actual values in the bitstream dont matter
299 static inline void skip_put_bits(PutBitContext *s, int n){
300 #ifdef ALT_BITSTREAM_WRITER
301 s->index += n;
302 #else
303 s->bit_left -= n;
304 s->buf_ptr-= s->bit_left>>5;
305 s->bit_left &= 31;
306 #endif
310 * Changes the end of the buffer.
312 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
313 s->buf_end= s->buf + size;
316 /* Bitstream reader API docs:
317 name
318 abritary name which is used as prefix for the internal variables
321 getbitcontext
323 OPEN_READER(name, gb)
324 loads gb into local variables
326 CLOSE_READER(name, gb)
327 stores local vars in gb
329 UPDATE_CACHE(name, gb)
330 refills the internal cache from the bitstream
331 after this call at least MIN_CACHE_BITS will be available,
333 GET_CACHE(name, gb)
334 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
336 SHOW_UBITS(name, gb, num)
337 will return the next num bits
339 SHOW_SBITS(name, gb, num)
340 will return the next num bits and do sign extension
342 SKIP_BITS(name, gb, num)
343 will skip over the next num bits
344 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
346 SKIP_CACHE(name, gb, num)
347 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
349 SKIP_COUNTER(name, gb, num)
350 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
352 LAST_SKIP_CACHE(name, gb, num)
353 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
355 LAST_SKIP_BITS(name, gb, num)
356 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
358 for examples see get_bits, show_bits, skip_bits, get_vlc
361 static inline int unaligned32_be(const void *v)
363 #ifdef CONFIG_ALIGN
364 const uint8_t *p=v;
365 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
366 #else
367 return be2me_32( unaligned32(v)); //original
368 #endif
371 static inline int unaligned32_le(const void *v)
373 #ifdef CONFIG_ALIGN
374 const uint8_t *p=v;
375 return (((p[3]<<8) | p[2])<<16) | (p[1]<<8) | (p[0]);
376 #else
377 return le2me_32( unaligned32(v)); //original
378 #endif
381 #ifdef ALT_BITSTREAM_READER
382 # define MIN_CACHE_BITS 25
384 # define OPEN_READER(name, gb)\
385 int name##_index= (gb)->index;\
386 int name##_cache= 0;\
388 # define CLOSE_READER(name, gb)\
389 (gb)->index= name##_index;\
391 # ifdef ALT_BITSTREAM_READER_LE
392 # define UPDATE_CACHE(name, gb)\
393 name##_cache= unaligned32_le( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
395 # define SKIP_CACHE(name, gb, num)\
396 name##_cache >>= (num);
397 # else
398 # define UPDATE_CACHE(name, gb)\
399 name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
401 # define SKIP_CACHE(name, gb, num)\
402 name##_cache <<= (num);
403 # endif
405 // FIXME name?
406 # define SKIP_COUNTER(name, gb, num)\
407 name##_index += (num);\
409 # define SKIP_BITS(name, gb, num)\
411 SKIP_CACHE(name, gb, num)\
412 SKIP_COUNTER(name, gb, num)\
415 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
416 # define LAST_SKIP_CACHE(name, gb, num) ;
418 # ifdef ALT_BITSTREAM_READER_LE
419 # define SHOW_UBITS(name, gb, num)\
420 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
421 # else
422 # define SHOW_UBITS(name, gb, num)\
423 NEG_USR32(name##_cache, num)
424 # endif
426 # define SHOW_SBITS(name, gb, num)\
427 NEG_SSR32(name##_cache, num)
429 # define GET_CACHE(name, gb)\
430 ((uint32_t)name##_cache)
432 static inline int get_bits_count(GetBitContext *s){
433 return s->index;
435 #elif defined LIBMPEG2_BITSTREAM_READER
436 //libmpeg2 like reader
438 # define MIN_CACHE_BITS 17
440 # define OPEN_READER(name, gb)\
441 int name##_bit_count=(gb)->bit_count;\
442 int name##_cache= (gb)->cache;\
443 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
445 # define CLOSE_READER(name, gb)\
446 (gb)->bit_count= name##_bit_count;\
447 (gb)->cache= name##_cache;\
448 (gb)->buffer_ptr= name##_buffer_ptr;\
450 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
452 # define UPDATE_CACHE(name, gb)\
453 if(name##_bit_count >= 0){\
454 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
455 name##_buffer_ptr += 2;\
456 name##_bit_count-= 16;\
459 #else
461 # define UPDATE_CACHE(name, gb)\
462 if(name##_bit_count >= 0){\
463 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
464 name##_buffer_ptr+=2;\
465 name##_bit_count-= 16;\
468 #endif
470 # define SKIP_CACHE(name, gb, num)\
471 name##_cache <<= (num);\
473 # define SKIP_COUNTER(name, gb, num)\
474 name##_bit_count += (num);\
476 # define SKIP_BITS(name, gb, num)\
478 SKIP_CACHE(name, gb, num)\
479 SKIP_COUNTER(name, gb, num)\
482 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
483 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
485 # define SHOW_UBITS(name, gb, num)\
486 NEG_USR32(name##_cache, num)
488 # define SHOW_SBITS(name, gb, num)\
489 NEG_SSR32(name##_cache, num)
491 # define GET_CACHE(name, gb)\
492 ((uint32_t)name##_cache)
494 static inline int get_bits_count(GetBitContext *s){
495 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
498 #elif defined A32_BITSTREAM_READER
500 # define MIN_CACHE_BITS 32
502 # define OPEN_READER(name, gb)\
503 int name##_bit_count=(gb)->bit_count;\
504 uint32_t name##_cache0= (gb)->cache0;\
505 uint32_t name##_cache1= (gb)->cache1;\
506 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
508 # define CLOSE_READER(name, gb)\
509 (gb)->bit_count= name##_bit_count;\
510 (gb)->cache0= name##_cache0;\
511 (gb)->cache1= name##_cache1;\
512 (gb)->buffer_ptr= name##_buffer_ptr;\
514 # define UPDATE_CACHE(name, gb)\
515 if(name##_bit_count > 0){\
516 const uint32_t next= be2me_32( *name##_buffer_ptr );\
517 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
518 name##_cache1 |= next<<name##_bit_count;\
519 name##_buffer_ptr++;\
520 name##_bit_count-= 32;\
523 #if defined(ARCH_X86) || defined(ARCH_X86_64)
524 # define SKIP_CACHE(name, gb, num)\
525 asm(\
526 "shldl %2, %1, %0 \n\t"\
527 "shll %2, %1 \n\t"\
528 : "+r" (name##_cache0), "+r" (name##_cache1)\
529 : "Ic" ((uint8_t)num)\
531 #else
532 # define SKIP_CACHE(name, gb, num)\
533 name##_cache0 <<= (num);\
534 name##_cache0 |= NEG_USR32(name##_cache1,num);\
535 name##_cache1 <<= (num);
536 #endif
538 # define SKIP_COUNTER(name, gb, num)\
539 name##_bit_count += (num);\
541 # define SKIP_BITS(name, gb, num)\
543 SKIP_CACHE(name, gb, num)\
544 SKIP_COUNTER(name, gb, num)\
547 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
548 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
550 # define SHOW_UBITS(name, gb, num)\
551 NEG_USR32(name##_cache0, num)
553 # define SHOW_SBITS(name, gb, num)\
554 NEG_SSR32(name##_cache0, num)
556 # define GET_CACHE(name, gb)\
557 (name##_cache0)
559 static inline int get_bits_count(GetBitContext *s){
560 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
563 #endif
566 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
567 * if MSB not set it is negative
568 * @param n length in bits
569 * @author BERO
571 static inline int get_xbits(GetBitContext *s, int n){
572 register int tmp;
573 register int32_t cache;
574 OPEN_READER(re, s)
575 UPDATE_CACHE(re, s)
576 cache = GET_CACHE(re,s);
577 if ((int32_t)cache<0) { //MSB=1
578 tmp = NEG_USR32(cache,n);
579 } else {
580 // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
581 // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
582 tmp = - NEG_USR32(~cache,n);
584 LAST_SKIP_BITS(re, s, n)
585 CLOSE_READER(re, s)
586 return tmp;
589 static inline int get_sbits(GetBitContext *s, int n){
590 register int tmp;
591 OPEN_READER(re, s)
592 UPDATE_CACHE(re, s)
593 tmp= SHOW_SBITS(re, s, n);
594 LAST_SKIP_BITS(re, s, n)
595 CLOSE_READER(re, s)
596 return tmp;
600 * reads 0-17 bits.
601 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
603 static inline unsigned int get_bits(GetBitContext *s, int n){
604 register int tmp;
605 OPEN_READER(re, s)
606 UPDATE_CACHE(re, s)
607 tmp= SHOW_UBITS(re, s, n);
608 LAST_SKIP_BITS(re, s, n)
609 CLOSE_READER(re, s)
610 return tmp;
613 unsigned int get_bits_long(GetBitContext *s, int n);
616 * shows 0-17 bits.
617 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
619 static inline unsigned int show_bits(GetBitContext *s, int n){
620 register int tmp;
621 OPEN_READER(re, s)
622 UPDATE_CACHE(re, s)
623 tmp= SHOW_UBITS(re, s, n);
624 // CLOSE_READER(re, s)
625 return tmp;
628 unsigned int show_bits_long(GetBitContext *s, int n);
630 static inline void skip_bits(GetBitContext *s, int n){
631 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
632 OPEN_READER(re, s)
633 UPDATE_CACHE(re, s)
634 LAST_SKIP_BITS(re, s, n)
635 CLOSE_READER(re, s)
638 static inline unsigned int get_bits1(GetBitContext *s){
639 #ifdef ALT_BITSTREAM_READER
640 int index= s->index;
641 uint8_t result= s->buffer[ index>>3 ];
642 #ifdef ALT_BITSTREAM_READER_LE
643 result>>= (index&0x07);
644 result&= 1;
645 #else
646 result<<= (index&0x07);
647 result>>= 8 - 1;
648 #endif
649 index++;
650 s->index= index;
652 return result;
653 #else
654 return get_bits(s, 1);
655 #endif
658 static inline unsigned int show_bits1(GetBitContext *s){
659 return show_bits(s, 1);
662 static inline void skip_bits1(GetBitContext *s){
663 skip_bits(s, 1);
667 * init GetBitContext.
668 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
669 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
670 * @param bit_size the size of the buffer in bits
672 static inline void init_get_bits(GetBitContext *s,
673 const uint8_t *buffer, int bit_size)
675 const int buffer_size= (bit_size+7)>>3;
677 s->buffer= buffer;
678 s->size_in_bits= bit_size;
679 s->buffer_end= buffer + buffer_size;
680 #ifdef ALT_BITSTREAM_READER
681 s->index=0;
682 #elif defined LIBMPEG2_BITSTREAM_READER
683 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
684 if ((int)buffer&1) {
685 /* word alignment */
686 s->cache = (*buffer++)<<24;
687 s->buffer_ptr = buffer;
688 s->bit_count = 16-8;
689 } else
690 #endif
692 s->buffer_ptr = buffer;
693 s->bit_count = 16;
694 s->cache = 0;
696 #elif defined A32_BITSTREAM_READER
697 s->buffer_ptr = (uint32_t*)buffer;
698 s->bit_count = 32;
699 s->cache0 = 0;
700 s->cache1 = 0;
701 #endif
703 OPEN_READER(re, s)
704 UPDATE_CACHE(re, s)
705 UPDATE_CACHE(re, s)
706 CLOSE_READER(re, s)
708 #ifdef A32_BITSTREAM_READER
709 s->cache1 = 0;
710 #endif
713 int check_marker(GetBitContext *s, const char *msg);
714 void align_get_bits(GetBitContext *s);
715 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
716 const void *bits, int bits_wrap, int bits_size,
717 const void *codes, int codes_wrap, int codes_size,
718 int flags);
719 #define INIT_VLC_USE_STATIC 1
720 #define INIT_VLC_LE 2
721 void free_vlc(VLC *vlc);
725 * if the vlc code is invalid and max_depth=1 than no bits will be removed
726 * if the vlc code is invalid and max_depth>1 than the number of bits removed
727 * is undefined
729 #define GET_VLC(code, name, gb, table, bits, max_depth)\
731 int n, index, nb_bits;\
733 index= SHOW_UBITS(name, gb, bits);\
734 code = table[index][0];\
735 n = table[index][1];\
737 if(max_depth > 1 && n < 0){\
738 LAST_SKIP_BITS(name, gb, bits)\
739 UPDATE_CACHE(name, gb)\
741 nb_bits = -n;\
743 index= SHOW_UBITS(name, gb, nb_bits) + code;\
744 code = table[index][0];\
745 n = table[index][1];\
746 if(max_depth > 2 && n < 0){\
747 LAST_SKIP_BITS(name, gb, nb_bits)\
748 UPDATE_CACHE(name, gb)\
750 nb_bits = -n;\
752 index= SHOW_UBITS(name, gb, nb_bits) + code;\
753 code = table[index][0];\
754 n = table[index][1];\
757 SKIP_BITS(name, gb, n)\
760 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
762 int n, index, nb_bits;\
764 index= SHOW_UBITS(name, gb, bits);\
765 level = table[index].level;\
766 n = table[index].len;\
768 if(max_depth > 1 && n < 0){\
769 SKIP_BITS(name, gb, bits)\
770 if(need_update){\
771 UPDATE_CACHE(name, gb)\
774 nb_bits = -n;\
776 index= SHOW_UBITS(name, gb, nb_bits) + level;\
777 level = table[index].level;\
778 n = table[index].len;\
780 run= table[index].run;\
781 SKIP_BITS(name, gb, n)\
784 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
785 static inline int get_vlc(GetBitContext *s, VLC *vlc)
787 int code;
788 VLC_TYPE (*table)[2]= vlc->table;
790 OPEN_READER(re, s)
791 UPDATE_CACHE(re, s)
793 GET_VLC(code, re, s, table, vlc->bits, 3)
795 CLOSE_READER(re, s)
796 return code;
800 * parses a vlc code, faster then get_vlc()
801 * @param bits is the number of bits which will be read at once, must be
802 * identical to nb_bits in init_vlc()
803 * @param max_depth is the number of times bits bits must be readed to completly
804 * read the longest vlc code
805 * = (max_vlc_length + bits - 1) / bits
807 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
808 int bits, int max_depth)
810 int code;
812 OPEN_READER(re, s)
813 UPDATE_CACHE(re, s)
815 GET_VLC(code, re, s, table, bits, max_depth)
817 CLOSE_READER(re, s)
818 return code;
821 //#define TRACE
823 #ifdef TRACE
824 #include "avcodec.h"
825 static inline void print_bin(int bits, int n){
826 int i;
828 for(i=n-1; i>=0; i--){
829 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
831 for(i=n; i<24; i++)
832 av_log(NULL, AV_LOG_DEBUG, " ");
835 static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
836 int r= get_bits(s, n);
838 print_bin(r, n);
839 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);
840 return r;
842 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
843 int show= show_bits(s, 24);
844 int pos= get_bits_count(s);
845 int r= get_vlc2(s, table, bits, max_depth);
846 int len= get_bits_count(s) - pos;
847 int bits2= show>>(24-len);
849 print_bin(bits2, len);
851 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
852 return r;
854 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
855 int show= show_bits(s, n);
856 int r= get_xbits(s, n);
858 print_bin(show, n);
859 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);
860 return r;
863 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
864 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
865 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
866 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
867 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
869 #define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
871 #else //TRACE
872 #define tprintf(...) {}
873 #endif
875 static inline int decode012(GetBitContext *gb){
876 int n;
877 n = get_bits1(gb);
878 if (n == 0)
879 return 0;
880 else
881 return get_bits1(gb) + 1;
884 #endif /* BITSTREAM_H */