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