100l, fix broken AMD64 patch. To whoever applied it: Did you actually _try_
[libav.git] / libavcodec / bitstream.h
CommitLineData
caa336b4
MN
1/**
2 * @file bitstream.h
3 * bitstream api header.
4 */
5
6#ifndef BITSTREAM_H
7#define BITSTREAM_H
8
9//#define ALT_BITSTREAM_WRITER
10//#define ALIGNED_BITSTREAM_WRITER
11
12#define ALT_BITSTREAM_READER
13//#define LIBMPEG2_BITSTREAM_READER
14//#define A32_BITSTREAM_READER
15#define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
16
17#if defined(ARCH_X86) || defined(ARCH_X86_64)
18// avoid +32 for shift optimization (gcc should do that ...)
19static inline int32_t NEG_SSR32( int32_t a, int8_t s){
20 asm ("sarl %1, %0\n\t"
21 : "+r" (a)
22 : "ic" ((uint8_t)(-s))
23 );
24 return a;
25}
26static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
27 asm ("shrl %1, %0\n\t"
28 : "+r" (a)
29 : "ic" ((uint8_t)(-s))
30 );
31 return a;
32}
33#else
34# define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
35# define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
36#endif
37
38/* bit output */
39
40/* buf and buf_end must be present and used by every alternative writer. */
41typedef struct PutBitContext {
42#ifdef ALT_BITSTREAM_WRITER
43 uint8_t *buf, *buf_end;
44 int index;
45#else
46 uint32_t bit_buf;
47 int bit_left;
48 uint8_t *buf, *buf_ptr, *buf_end;
49#endif
50} PutBitContext;
51
52static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
53{
54 s->buf = buffer;
55 s->buf_end = s->buf + buffer_size;
56#ifdef ALT_BITSTREAM_WRITER
57 s->index=0;
58 ((uint32_t*)(s->buf))[0]=0;
59// memset(buffer, 0, buffer_size);
60#else
61 s->buf_ptr = s->buf;
62 s->bit_left=32;
63 s->bit_buf=0;
64#endif
65}
66
67/* return the number of bits output */
68static inline int put_bits_count(PutBitContext *s)
69{
70#ifdef ALT_BITSTREAM_WRITER
71 return s->index;
72#else
73 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
74#endif
75}
76
77/* pad the end of the output stream with zeros */
78static inline void flush_put_bits(PutBitContext *s)
79{
80#ifdef ALT_BITSTREAM_WRITER
81 align_put_bits(s);
82#else
83 s->bit_buf<<= s->bit_left;
84 while (s->bit_left < 32) {
85 /* XXX: should test end of buffer */
86 *s->buf_ptr++=s->bit_buf >> 24;
87 s->bit_buf<<=8;
88 s->bit_left+=8;
89 }
90 s->bit_left=32;
91 s->bit_buf=0;
92#endif
93}
94
95void align_put_bits(PutBitContext *s);
96void put_string(PutBitContext * pbc, char *s, int put_zero);
97
98/* bit input */
99/* buffer, buffer_end and size_in_bits must be present and used by every reader */
100typedef struct GetBitContext {
101 const uint8_t *buffer, *buffer_end;
102#ifdef ALT_BITSTREAM_READER
103 int index;
104#elif defined LIBMPEG2_BITSTREAM_READER
105 uint8_t *buffer_ptr;
106 uint32_t cache;
107 int bit_count;
108#elif defined A32_BITSTREAM_READER
109 uint32_t *buffer_ptr;
110 uint32_t cache0;
111 uint32_t cache1;
112 int bit_count;
113#endif
114 int size_in_bits;
115} GetBitContext;
116
117#define VLC_TYPE int16_t
118
119typedef struct VLC {
120 int bits;
121 VLC_TYPE (*table)[2]; ///< code, bits
122 int table_size, table_allocated;
123} VLC;
124
125typedef struct RL_VLC_ELEM {
126 int16_t level;
127 int8_t len;
128 uint8_t run;
129} RL_VLC_ELEM;
130
131#ifdef ARCH_SPARC
132#define UNALIGNED_STORES_ARE_BAD
133#endif
134
135/* used to avoid missaligned exceptions on some archs (alpha, ...) */
136#if defined(ARCH_X86) || defined(ARCH_X86_64)
137# define unaligned32(a) (*(const uint32_t*)(a))
138#else
139# ifdef __GNUC__
140static inline uint32_t unaligned32(const void *v) {
141 struct Unaligned {
142 uint32_t i;
143 } __attribute__((packed));
144
145 return ((const struct Unaligned *) v)->i;
146}
147# elif defined(__DECC)
148static inline uint32_t unaligned32(const void *v) {
149 return *(const __unaligned uint32_t *) v;
150}
151# else
152static inline uint32_t unaligned32(const void *v) {
153 return *(const uint32_t *) v;
154}
155# endif
156#endif //!ARCH_X86
157
158#ifndef ALT_BITSTREAM_WRITER
159static inline void put_bits(PutBitContext *s, int n, unsigned int value)
160{
161 unsigned int bit_buf;
162 int bit_left;
163
164#ifdef STATS
165 st_out_bit_counts[st_current_index] += n;
166#endif
167 // printf("put_bits=%d %x\n", n, value);
168 assert(n == 32 || value < (1U << n));
169
170 bit_buf = s->bit_buf;
171 bit_left = s->bit_left;
172
173 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
174 /* XXX: optimize */
175 if (n < bit_left) {
176 bit_buf = (bit_buf<<n) | value;
177 bit_left-=n;
178 } else {
179 bit_buf<<=bit_left;
180 bit_buf |= value >> (n - bit_left);
181#ifdef UNALIGNED_STORES_ARE_BAD
182 if (3 & (intptr_t) s->buf_ptr) {
183 s->buf_ptr[0] = bit_buf >> 24;
184 s->buf_ptr[1] = bit_buf >> 16;
185 s->buf_ptr[2] = bit_buf >> 8;
186 s->buf_ptr[3] = bit_buf ;
187 } else
188#endif
189 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
190 //printf("bitbuf = %08x\n", bit_buf);
191 s->buf_ptr+=4;
192 bit_left+=32 - n;
193 bit_buf = value;
194 }
195
196 s->bit_buf = bit_buf;
197 s->bit_left = bit_left;
198}
199#endif
200
201
202#ifdef ALT_BITSTREAM_WRITER
203static inline void put_bits(PutBitContext *s, int n, unsigned int value)
204{
205# ifdef ALIGNED_BITSTREAM_WRITER
206# if defined(ARCH_X86) || defined(ARCH_X86_64)
207 asm volatile(
208 "movl %0, %%ecx \n\t"
209 "xorl %%eax, %%eax \n\t"
210 "shrdl %%cl, %1, %%eax \n\t"
211 "shrl %%cl, %1 \n\t"
212 "movl %0, %%ecx \n\t"
213 "shrl $3, %%ecx \n\t"
214 "andl $0xFFFFFFFC, %%ecx \n\t"
215 "bswapl %1 \n\t"
216 "orl %1, (%2, %%ecx) \n\t"
217 "bswapl %%eax \n\t"
218 "addl %3, %0 \n\t"
219 "movl %%eax, 4(%2, %%ecx) \n\t"
220 : "=&r" (s->index), "=&r" (value)
221 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
222 : "%eax", "%ecx"
223 );
224# else
225 int index= s->index;
226 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
227
228 value<<= 32-n;
229
230 ptr[0] |= be2me_32(value>>(index&31));
231 ptr[1] = be2me_32(value<<(32-(index&31)));
232//if(n>24) printf("%d %d\n", n, value);
233 index+= n;
234 s->index= index;
235# endif
236# else //ALIGNED_BITSTREAM_WRITER
237# if defined(ARCH_X86) || defined(ARCH_X86_64)
238 asm volatile(
239 "movl $7, %%ecx \n\t"
240 "andl %0, %%ecx \n\t"
241 "addl %3, %%ecx \n\t"
242 "negl %%ecx \n\t"
243 "shll %%cl, %1 \n\t"
244 "bswapl %1 \n\t"
245 "movl %0, %%ecx \n\t"
246 "shrl $3, %%ecx \n\t"
247 "orl %1, (%%ecx, %2) \n\t"
248 "addl %3, %0 \n\t"
249 "movl $0, 4(%%ecx, %2) \n\t"
250 : "=&r" (s->index), "=&r" (value)
251 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
252 : "%ecx"
253 );
254# else
255 int index= s->index;
256 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
257
258 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
259 ptr[1] = 0;
260//if(n>24) printf("%d %d\n", n, value);
261 index+= n;
262 s->index= index;
263# endif
264# endif //!ALIGNED_BITSTREAM_WRITER
265}
266#endif
267
268
269static inline uint8_t* pbBufPtr(PutBitContext *s)
270{
271#ifdef ALT_BITSTREAM_WRITER
272 return s->buf + (s->index>>3);
273#else
274 return s->buf_ptr;
275#endif
276}
277
278/**
279 *
280 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
281 */
282static inline void skip_put_bytes(PutBitContext *s, int n){
283 assert((put_bits_count(s)&7)==0);
284#ifdef ALT_BITSTREAM_WRITER
285 FIXME may need some cleaning of the buffer
286 s->index += n<<3;
287#else
288 assert(s->bit_left==32);
289 s->buf_ptr += n;
290#endif
291}
292
293/**
294 * Changes the end of the buffer.
295 */
296static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
297 s->buf_end= s->buf + size;
298}
299
300/* Bitstream reader API docs:
301name
302 abritary name which is used as prefix for the internal variables
303
304gb
305 getbitcontext
306
307OPEN_READER(name, gb)
308 loads gb into local variables
309
310CLOSE_READER(name, gb)
311 stores local vars in gb
312
313UPDATE_CACHE(name, gb)
314 refills the internal cache from the bitstream
315 after this call at least MIN_CACHE_BITS will be available,
316
317GET_CACHE(name, gb)
318 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
319
320SHOW_UBITS(name, gb, num)
321 will return the nest num bits
322
323SHOW_SBITS(name, gb, num)
324 will return the nest num bits and do sign extension
325
326SKIP_BITS(name, gb, num)
327 will skip over the next num bits
328 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
329
330SKIP_CACHE(name, gb, num)
331 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
332
333SKIP_COUNTER(name, gb, num)
334 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
335
336LAST_SKIP_CACHE(name, gb, num)
337 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
338
339LAST_SKIP_BITS(name, gb, num)
340 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
341
342for examples see get_bits, show_bits, skip_bits, get_vlc
343*/
344
345static inline int unaligned32_be(const void *v)
346{
347#ifdef CONFIG_ALIGN
348 const uint8_t *p=v;
349 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
350#else
351 return be2me_32( unaligned32(v)); //original
352#endif
353}
354
355#ifdef ALT_BITSTREAM_READER
356# define MIN_CACHE_BITS 25
357
358# define OPEN_READER(name, gb)\
359 int name##_index= (gb)->index;\
360 int name##_cache= 0;\
361
362# define CLOSE_READER(name, gb)\
363 (gb)->index= name##_index;\
364
365# define UPDATE_CACHE(name, gb)\
366 name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
367
368# define SKIP_CACHE(name, gb, num)\
369 name##_cache <<= (num);\
370
371// FIXME name?
372# define SKIP_COUNTER(name, gb, num)\
373 name##_index += (num);\
374
375# define SKIP_BITS(name, gb, num)\
376 {\
377 SKIP_CACHE(name, gb, num)\
378 SKIP_COUNTER(name, gb, num)\
379 }\
380
381# define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
382# define LAST_SKIP_CACHE(name, gb, num) ;
383
384# define SHOW_UBITS(name, gb, num)\
385 NEG_USR32(name##_cache, num)
386
387# define SHOW_SBITS(name, gb, num)\
388 NEG_SSR32(name##_cache, num)
389
390# define GET_CACHE(name, gb)\
391 ((uint32_t)name##_cache)
392
393static inline int get_bits_count(GetBitContext *s){
394 return s->index;
395}
396#elif defined LIBMPEG2_BITSTREAM_READER
397//libmpeg2 like reader
398
399# define MIN_CACHE_BITS 17
400
401# define OPEN_READER(name, gb)\
402 int name##_bit_count=(gb)->bit_count;\
403 int name##_cache= (gb)->cache;\
404 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
405
406# define CLOSE_READER(name, gb)\
407 (gb)->bit_count= name##_bit_count;\
408 (gb)->cache= name##_cache;\
409 (gb)->buffer_ptr= name##_buffer_ptr;\
410
411#ifdef LIBMPEG2_BITSTREAM_READER_HACK
412
413# define UPDATE_CACHE(name, gb)\
414 if(name##_bit_count >= 0){\
415 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
416 ((uint16_t*)name##_buffer_ptr)++;\
417 name##_bit_count-= 16;\
418 }\
419
420#else
421
422# define UPDATE_CACHE(name, gb)\
423 if(name##_bit_count >= 0){\
424 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
425 name##_buffer_ptr+=2;\
426 name##_bit_count-= 16;\
427 }\
428
429#endif
430
431# define SKIP_CACHE(name, gb, num)\
432 name##_cache <<= (num);\
433
434# define SKIP_COUNTER(name, gb, num)\
435 name##_bit_count += (num);\
436
437# define SKIP_BITS(name, gb, num)\
438 {\
439 SKIP_CACHE(name, gb, num)\
440 SKIP_COUNTER(name, gb, num)\
441 }\
442
443# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
444# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
445
446# define SHOW_UBITS(name, gb, num)\
447 NEG_USR32(name##_cache, num)
448
449# define SHOW_SBITS(name, gb, num)\
450 NEG_SSR32(name##_cache, num)
451
452# define GET_CACHE(name, gb)\
453 ((uint32_t)name##_cache)
454
455static inline int get_bits_count(GetBitContext *s){
456 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
457}
458
459#elif defined A32_BITSTREAM_READER
460
461# define MIN_CACHE_BITS 32
462
463# define OPEN_READER(name, gb)\
464 int name##_bit_count=(gb)->bit_count;\
465 uint32_t name##_cache0= (gb)->cache0;\
466 uint32_t name##_cache1= (gb)->cache1;\
467 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
468
469# define CLOSE_READER(name, gb)\
470 (gb)->bit_count= name##_bit_count;\
471 (gb)->cache0= name##_cache0;\
472 (gb)->cache1= name##_cache1;\
473 (gb)->buffer_ptr= name##_buffer_ptr;\
474
475# define UPDATE_CACHE(name, gb)\
476 if(name##_bit_count > 0){\
477 const uint32_t next= be2me_32( *name##_buffer_ptr );\
478 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
479 name##_cache1 |= next<<name##_bit_count;\
480 name##_buffer_ptr++;\
481 name##_bit_count-= 32;\
482 }\
483
484#if defined(ARCH_X86) || defined(ARCH_X86_64)
485# define SKIP_CACHE(name, gb, num)\
486 asm(\
487 "shldl %2, %1, %0 \n\t"\
488 "shll %2, %1 \n\t"\
489 : "+r" (name##_cache0), "+r" (name##_cache1)\
490 : "Ic" ((uint8_t)num)\
491 );
492#else
493# define SKIP_CACHE(name, gb, num)\
494 name##_cache0 <<= (num);\
495 name##_cache0 |= NEG_USR32(name##_cache1,num);\
496 name##_cache1 <<= (num);
497#endif
498
499# define SKIP_COUNTER(name, gb, num)\
500 name##_bit_count += (num);\
501
502# define SKIP_BITS(name, gb, num)\
503 {\
504 SKIP_CACHE(name, gb, num)\
505 SKIP_COUNTER(name, gb, num)\
506 }\
507
508# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
509# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
510
511# define SHOW_UBITS(name, gb, num)\
512 NEG_USR32(name##_cache0, num)
513
514# define SHOW_SBITS(name, gb, num)\
515 NEG_SSR32(name##_cache0, num)
516
517# define GET_CACHE(name, gb)\
518 (name##_cache0)
519
520static inline int get_bits_count(GetBitContext *s){
521 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
522}
523
524#endif
525
526/**
527 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
528 * if MSB not set it is negative
529 * @param n length in bits
530 * @author BERO
531 */
532static inline int get_xbits(GetBitContext *s, int n){
533 register int tmp;
534 register int32_t cache;
535 OPEN_READER(re, s)
536 UPDATE_CACHE(re, s)
537 cache = GET_CACHE(re,s);
538 if ((int32_t)cache<0) { //MSB=1
539 tmp = NEG_USR32(cache,n);
540 } else {
541 // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
542 // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
543 tmp = - NEG_USR32(~cache,n);
544 }
545 LAST_SKIP_BITS(re, s, n)
546 CLOSE_READER(re, s)
547 return tmp;
548}
549
550static inline int get_sbits(GetBitContext *s, int n){
551 register int tmp;
552 OPEN_READER(re, s)
553 UPDATE_CACHE(re, s)
554 tmp= SHOW_SBITS(re, s, n);
555 LAST_SKIP_BITS(re, s, n)
556 CLOSE_READER(re, s)
557 return tmp;
558}
559
560/**
561 * reads 0-17 bits.
562 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
563 */
564static inline unsigned int get_bits(GetBitContext *s, int n){
565 register int tmp;
566 OPEN_READER(re, s)
567 UPDATE_CACHE(re, s)
568 tmp= SHOW_UBITS(re, s, n);
569 LAST_SKIP_BITS(re, s, n)
570 CLOSE_READER(re, s)
571 return tmp;
572}
573
574unsigned int get_bits_long(GetBitContext *s, int n);
575
576/**
577 * shows 0-17 bits.
578 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
579 */
580static inline unsigned int show_bits(GetBitContext *s, int n){
581 register int tmp;
582 OPEN_READER(re, s)
583 UPDATE_CACHE(re, s)
584 tmp= SHOW_UBITS(re, s, n);
585// CLOSE_READER(re, s)
586 return tmp;
587}
588
589unsigned int show_bits_long(GetBitContext *s, int n);
590
591static inline void skip_bits(GetBitContext *s, int n){
592 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
593 OPEN_READER(re, s)
594 UPDATE_CACHE(re, s)
595 LAST_SKIP_BITS(re, s, n)
596 CLOSE_READER(re, s)
597}
598
599static inline unsigned int get_bits1(GetBitContext *s){
600#ifdef ALT_BITSTREAM_READER
601 int index= s->index;
602 uint8_t result= s->buffer[ index>>3 ];
603 result<<= (index&0x07);
604 result>>= 8 - 1;
605 index++;
606 s->index= index;
607
608 return result;
609#else
610 return get_bits(s, 1);
611#endif
612}
613
614static inline unsigned int show_bits1(GetBitContext *s){
615 return show_bits(s, 1);
616}
617
618static inline void skip_bits1(GetBitContext *s){
619 skip_bits(s, 1);
620}
621
622/**
623 * init GetBitContext.
624 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
625 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
626 * @param bit_size the size of the buffer in bits
627 */
628static inline void init_get_bits(GetBitContext *s,
629 const uint8_t *buffer, int bit_size)
630{
631 const int buffer_size= (bit_size+7)>>3;
632
633 s->buffer= buffer;
634 s->size_in_bits= bit_size;
635 s->buffer_end= buffer + buffer_size;
636#ifdef ALT_BITSTREAM_READER
637 s->index=0;
638#elif defined LIBMPEG2_BITSTREAM_READER
639#ifdef LIBMPEG2_BITSTREAM_READER_HACK
640 if ((int)buffer&1) {
641 /* word alignment */
642 s->cache = (*buffer++)<<24;
643 s->buffer_ptr = buffer;
644 s->bit_count = 16-8;
645 } else
646#endif
647 {
648 s->buffer_ptr = buffer;
649 s->bit_count = 16;
650 s->cache = 0;
651 }
652#elif defined A32_BITSTREAM_READER
653 s->buffer_ptr = (uint32_t*)buffer;
654 s->bit_count = 32;
655 s->cache0 = 0;
656 s->cache1 = 0;
657#endif
658 {
659 OPEN_READER(re, s)
660 UPDATE_CACHE(re, s)
661 UPDATE_CACHE(re, s)
662 CLOSE_READER(re, s)
663 }
664#ifdef A32_BITSTREAM_READER
665 s->cache1 = 0;
666#endif
667}
668
669int check_marker(GetBitContext *s, const char *msg);
670void align_get_bits(GetBitContext *s);
671int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
672 const void *bits, int bits_wrap, int bits_size,
673 const void *codes, int codes_wrap, int codes_size,
674 int use_static);
675void free_vlc(VLC *vlc);
676
677/**
678 *
679 * if the vlc code is invalid and max_depth=1 than no bits will be removed
680 * if the vlc code is invalid and max_depth>1 than the number of bits removed
681 * is undefined
682 */
683#define GET_VLC(code, name, gb, table, bits, max_depth)\
684{\
685 int n, index, nb_bits;\
686\
687 index= SHOW_UBITS(name, gb, bits);\
688 code = table[index][0];\
689 n = table[index][1];\
690\
691 if(max_depth > 1 && n < 0){\
692 LAST_SKIP_BITS(name, gb, bits)\
693 UPDATE_CACHE(name, gb)\
694\
695 nb_bits = -n;\
696\
697 index= SHOW_UBITS(name, gb, nb_bits) + code;\
698 code = table[index][0];\
699 n = table[index][1];\
700 if(max_depth > 2 && n < 0){\
701 LAST_SKIP_BITS(name, gb, nb_bits)\
702 UPDATE_CACHE(name, gb)\
703\
704 nb_bits = -n;\
705\
706 index= SHOW_UBITS(name, gb, nb_bits) + code;\
707 code = table[index][0];\
708 n = table[index][1];\
709 }\
710 }\
711 SKIP_BITS(name, gb, n)\
712}
713
714#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
715{\
716 int n, index, nb_bits;\
717\
718 index= SHOW_UBITS(name, gb, bits);\
719 level = table[index].level;\
720 n = table[index].len;\
721\
722 if(max_depth > 1 && n < 0){\
723 LAST_SKIP_BITS(name, gb, bits)\
724 UPDATE_CACHE(name, gb)\
725\
726 nb_bits = -n;\
727\
728 index= SHOW_UBITS(name, gb, nb_bits) + level;\
729 level = table[index].level;\
730 n = table[index].len;\
731 }\
732 run= table[index].run;\
733 SKIP_BITS(name, gb, n)\
734}
735
736// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
737static inline int get_vlc(GetBitContext *s, VLC *vlc)
738{
739 int code;
740 VLC_TYPE (*table)[2]= vlc->table;
741
742 OPEN_READER(re, s)
743 UPDATE_CACHE(re, s)
744
745 GET_VLC(code, re, s, table, vlc->bits, 3)
746
747 CLOSE_READER(re, s)
748 return code;
749}
750
751/**
752 * parses a vlc code, faster then get_vlc()
753 * @param bits is the number of bits which will be read at once, must be
754 * identical to nb_bits in init_vlc()
755 * @param max_depth is the number of times bits bits must be readed to completly
756 * read the longest vlc code
757 * = (max_vlc_length + bits - 1) / bits
758 */
759static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
760 int bits, int max_depth)
761{
762 int code;
763
764 OPEN_READER(re, s)
765 UPDATE_CACHE(re, s)
766
767 GET_VLC(code, re, s, table, bits, max_depth)
768
769 CLOSE_READER(re, s)
770 return code;
771}
772
773//#define TRACE
774
775#ifdef TRACE
776
777static inline void print_bin(int bits, int n){
778 int i;
779
780 for(i=n-1; i>=0; i--){
781 printf("%d", (bits>>i)&1);
782 }
783 for(i=n; i<24; i++)
784 printf(" ");
785}
786
787static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
788 int r= get_bits(s, n);
789
790 print_bin(r, n);
791 printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
792 return r;
793}
794static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
795 int show= show_bits(s, 24);
796 int pos= get_bits_count(s);
797 int r= get_vlc2(s, table, bits, max_depth);
798 int len= get_bits_count(s) - pos;
799 int bits2= show>>(24-len);
800
801 print_bin(bits2, len);
802
803 printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
804 return r;
805}
806static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
807 int show= show_bits(s, n);
808 int r= get_xbits(s, n);
809
810 print_bin(show, n);
811 printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
812 return r;
813}
814
815#define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
816#define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
817#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
818#define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
819#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
820
821#define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
822
823#else //TRACE
824#define tprintf(...) {}
825#endif
826
827#endif /* BITSTREAM_H */