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