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