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