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