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