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