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