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