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