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