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