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