OSDN Git Service

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