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