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