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