fix buffer handling
[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}
5a7bd283
MN
447
448static inline void skip_bits_long(GetBitContext *s, int n){
6e4703ca 449 s->index += n;
5a7bd283
MN
450}
451
caa336b4
MN
452#elif defined LIBMPEG2_BITSTREAM_READER
453//libmpeg2 like reader
454
455# define MIN_CACHE_BITS 17
456
457# define OPEN_READER(name, gb)\
458 int name##_bit_count=(gb)->bit_count;\
459 int name##_cache= (gb)->cache;\
460 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
461
462# define CLOSE_READER(name, gb)\
463 (gb)->bit_count= name##_bit_count;\
464 (gb)->cache= name##_cache;\
465 (gb)->buffer_ptr= name##_buffer_ptr;\
466
467#ifdef LIBMPEG2_BITSTREAM_READER_HACK
468
469# define UPDATE_CACHE(name, gb)\
470 if(name##_bit_count >= 0){\
471 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
5bb8a0da 472 name##_buffer_ptr += 2;\
caa336b4
MN
473 name##_bit_count-= 16;\
474 }\
475
476#else
477
478# define UPDATE_CACHE(name, gb)\
479 if(name##_bit_count >= 0){\
480 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
481 name##_buffer_ptr+=2;\
482 name##_bit_count-= 16;\
483 }\
484
485#endif
486
487# define SKIP_CACHE(name, gb, num)\
488 name##_cache <<= (num);\
489
490# define SKIP_COUNTER(name, gb, num)\
491 name##_bit_count += (num);\
492
493# define SKIP_BITS(name, gb, num)\
494 {\
495 SKIP_CACHE(name, gb, num)\
496 SKIP_COUNTER(name, gb, num)\
497 }\
498
499# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
500# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
501
502# define SHOW_UBITS(name, gb, num)\
503 NEG_USR32(name##_cache, num)
504
505# define SHOW_SBITS(name, gb, num)\
506 NEG_SSR32(name##_cache, num)
507
508# define GET_CACHE(name, gb)\
509 ((uint32_t)name##_cache)
510
511static inline int get_bits_count(GetBitContext *s){
512 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
513}
514
6e4703ca
MN
515static inline void skip_bits_long(GetBitContext *s, int n){
516 OPEN_READER(re, s)
517 re_bit_count += n;
518 re_buffer_ptr += 2*(re_bit_count>>4);
519 re_bit_count &= 15;
520 re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
521 UPDATE_CACHE(re, s)
522 CLOSE_READER(re, s)
523}
524
caa336b4
MN
525#elif defined A32_BITSTREAM_READER
526
527# define MIN_CACHE_BITS 32
528
529# define OPEN_READER(name, gb)\
530 int name##_bit_count=(gb)->bit_count;\
531 uint32_t name##_cache0= (gb)->cache0;\
532 uint32_t name##_cache1= (gb)->cache1;\
533 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
534
535# define CLOSE_READER(name, gb)\
536 (gb)->bit_count= name##_bit_count;\
537 (gb)->cache0= name##_cache0;\
538 (gb)->cache1= name##_cache1;\
539 (gb)->buffer_ptr= name##_buffer_ptr;\
540
541# define UPDATE_CACHE(name, gb)\
542 if(name##_bit_count > 0){\
543 const uint32_t next= be2me_32( *name##_buffer_ptr );\
544 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
545 name##_cache1 |= next<<name##_bit_count;\
546 name##_buffer_ptr++;\
547 name##_bit_count-= 32;\
548 }\
549
550#if defined(ARCH_X86) || defined(ARCH_X86_64)
551# define SKIP_CACHE(name, gb, num)\
552 asm(\
bb270c08
DB
553 "shldl %2, %1, %0 \n\t"\
554 "shll %2, %1 \n\t"\
caa336b4 555 : "+r" (name##_cache0), "+r" (name##_cache1)\
6ff3b2b8 556 : "Ic" ((uint8_t)(num))\
caa336b4
MN
557 );
558#else
559# define SKIP_CACHE(name, gb, num)\
560 name##_cache0 <<= (num);\
561 name##_cache0 |= NEG_USR32(name##_cache1,num);\
562 name##_cache1 <<= (num);
563#endif
564
565# define SKIP_COUNTER(name, gb, num)\
566 name##_bit_count += (num);\
567
568# define SKIP_BITS(name, gb, num)\
569 {\
570 SKIP_CACHE(name, gb, num)\
571 SKIP_COUNTER(name, gb, num)\
572 }\
573
574# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
575# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
576
577# define SHOW_UBITS(name, gb, num)\
578 NEG_USR32(name##_cache0, num)
579
580# define SHOW_SBITS(name, gb, num)\
581 NEG_SSR32(name##_cache0, num)
582
583# define GET_CACHE(name, gb)\
584 (name##_cache0)
585
586static inline int get_bits_count(GetBitContext *s){
587 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
588}
589
5a7bd283
MN
590static inline void skip_bits_long(GetBitContext *s, int n){
591 OPEN_READER(re, s)
592 re_bit_count += n;
3dddf21f 593 re_buffer_ptr += re_bit_count>>5;
5a7bd283 594 re_bit_count &= 31;
1491e21c
MN
595 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
596 re_cache1 = 0;
5a7bd283 597 UPDATE_CACHE(re, s)
5a7bd283
MN
598 CLOSE_READER(re, s)
599}
600
caa336b4
MN
601#endif
602
603/**
604 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
115329f1 605 * if MSB not set it is negative
caa336b4 606 * @param n length in bits
115329f1 607 * @author BERO
caa336b4
MN
608 */
609static inline int get_xbits(GetBitContext *s, int n){
6b250e47 610 register int sign;
caa336b4
MN
611 register int32_t cache;
612 OPEN_READER(re, s)
613 UPDATE_CACHE(re, s)
614 cache = GET_CACHE(re,s);
6b250e47 615 sign=(~cache)>>31;
caa336b4
MN
616 LAST_SKIP_BITS(re, s, n)
617 CLOSE_READER(re, s)
6b250e47 618 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
caa336b4
MN
619}
620
621static inline int get_sbits(GetBitContext *s, int n){
622 register int tmp;
623 OPEN_READER(re, s)
624 UPDATE_CACHE(re, s)
625 tmp= SHOW_SBITS(re, s, n);
626 LAST_SKIP_BITS(re, s, n)
627 CLOSE_READER(re, s)
628 return tmp;
629}
630
631/**
632 * reads 0-17 bits.
bb628dae 633 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
caa336b4
MN
634 */
635static inline unsigned int get_bits(GetBitContext *s, int n){
636 register int tmp;
637 OPEN_READER(re, s)
638 UPDATE_CACHE(re, s)
639 tmp= SHOW_UBITS(re, s, n);
640 LAST_SKIP_BITS(re, s, n)
641 CLOSE_READER(re, s)
642 return tmp;
643}
644
645unsigned int get_bits_long(GetBitContext *s, int n);
646
647/**
648 * shows 0-17 bits.
bb628dae 649 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
caa336b4
MN
650 */
651static inline unsigned int show_bits(GetBitContext *s, int n){
652 register int tmp;
653 OPEN_READER(re, s)
654 UPDATE_CACHE(re, s)
655 tmp= SHOW_UBITS(re, s, n);
656// CLOSE_READER(re, s)
657 return tmp;
658}
659
660unsigned int show_bits_long(GetBitContext *s, int n);
661
662static inline void skip_bits(GetBitContext *s, int n){
663 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
664 OPEN_READER(re, s)
665 UPDATE_CACHE(re, s)
666 LAST_SKIP_BITS(re, s, n)
667 CLOSE_READER(re, s)
668}
669
670static inline unsigned int get_bits1(GetBitContext *s){
671#ifdef ALT_BITSTREAM_READER
672 int index= s->index;
673 uint8_t result= s->buffer[ index>>3 ];
cea27ac7
MN
674#ifdef ALT_BITSTREAM_READER_LE
675 result>>= (index&0x07);
676 result&= 1;
677#else
caa336b4
MN
678 result<<= (index&0x07);
679 result>>= 8 - 1;
cea27ac7 680#endif
caa336b4
MN
681 index++;
682 s->index= index;
683
684 return result;
685#else
686 return get_bits(s, 1);
687#endif
688}
689
690static inline unsigned int show_bits1(GetBitContext *s){
691 return show_bits(s, 1);
692}
693
694static inline void skip_bits1(GetBitContext *s){
695 skip_bits(s, 1);
696}
697
698/**
699 * init GetBitContext.
700 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
701 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
702 * @param bit_size the size of the buffer in bits
703 */
704static inline void init_get_bits(GetBitContext *s,
705 const uint8_t *buffer, int bit_size)
706{
9061be9f 707 int buffer_size= (bit_size+7)>>3;
288f1e68
MB
708 if(buffer_size < 0 || bit_size < 0) {
709 buffer_size = bit_size = 0;
710 buffer = NULL;
711 }
caa336b4
MN
712
713 s->buffer= buffer;
714 s->size_in_bits= bit_size;
715 s->buffer_end= buffer + buffer_size;
716#ifdef ALT_BITSTREAM_READER
717 s->index=0;
718#elif defined LIBMPEG2_BITSTREAM_READER
6e4703ca
MN
719 s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
720 s->bit_count = 16 + 8*((intptr_t)buffer&1);
721 skip_bits_long(s, 0);
a42ec9f4
MN
722#elif defined A32_BITSTREAM_READER
723 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
724 s->bit_count = 32 + 8*((intptr_t)buffer&3);
725 skip_bits_long(s, 0);
caa336b4
MN
726#endif
727}
728
5a7bd283
MN
729static void align_get_bits(GetBitContext *s)
730{
731 int n= (-get_bits_count(s)) & 7;
732 if(n) skip_bits(s, n);
733}
734
caa336b4 735int check_marker(GetBitContext *s, const char *msg);
caa336b4
MN
736int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
737 const void *bits, int bits_wrap, int bits_size,
738 const void *codes, int codes_wrap, int codes_size,
cea27ac7
MN
739 int flags);
740#define INIT_VLC_USE_STATIC 1
741#define INIT_VLC_LE 2
caa336b4
MN
742void free_vlc(VLC *vlc);
743
744/**
745 *
746 * if the vlc code is invalid and max_depth=1 than no bits will be removed
747 * if the vlc code is invalid and max_depth>1 than the number of bits removed
748 * is undefined
749 */
750#define GET_VLC(code, name, gb, table, bits, max_depth)\
751{\
752 int n, index, nb_bits;\
753\
754 index= SHOW_UBITS(name, gb, bits);\
755 code = table[index][0];\
756 n = table[index][1];\
757\
758 if(max_depth > 1 && n < 0){\
759 LAST_SKIP_BITS(name, gb, bits)\
760 UPDATE_CACHE(name, gb)\
761\
762 nb_bits = -n;\
763\
764 index= SHOW_UBITS(name, gb, nb_bits) + code;\
765 code = table[index][0];\
766 n = table[index][1];\
767 if(max_depth > 2 && n < 0){\
768 LAST_SKIP_BITS(name, gb, nb_bits)\
769 UPDATE_CACHE(name, gb)\
770\
771 nb_bits = -n;\
772\
773 index= SHOW_UBITS(name, gb, nb_bits) + code;\
774 code = table[index][0];\
775 n = table[index][1];\
776 }\
777 }\
778 SKIP_BITS(name, gb, n)\
779}
780
e91f4bf1 781#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
caa336b4
MN
782{\
783 int n, index, nb_bits;\
784\
785 index= SHOW_UBITS(name, gb, bits);\
786 level = table[index].level;\
787 n = table[index].len;\
788\
789 if(max_depth > 1 && n < 0){\
e91f4bf1
MN
790 SKIP_BITS(name, gb, bits)\
791 if(need_update){\
792 UPDATE_CACHE(name, gb)\
793 }\
caa336b4
MN
794\
795 nb_bits = -n;\
796\
797 index= SHOW_UBITS(name, gb, nb_bits) + level;\
798 level = table[index].level;\
799 n = table[index].len;\
800 }\
801 run= table[index].run;\
802 SKIP_BITS(name, gb, n)\
803}
804
caa336b4
MN
805
806/**
807 * parses a vlc code, faster then get_vlc()
115329f1 808 * @param bits is the number of bits which will be read at once, must be
caa336b4
MN
809 * identical to nb_bits in init_vlc()
810 * @param max_depth is the number of times bits bits must be readed to completly
115329f1 811 * read the longest vlc code
caa336b4
MN
812 * = (max_vlc_length + bits - 1) / bits
813 */
814static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
815 int bits, int max_depth)
816{
817 int code;
115329f1 818
caa336b4
MN
819 OPEN_READER(re, s)
820 UPDATE_CACHE(re, s)
821
822 GET_VLC(code, re, s, table, bits, max_depth)
823
824 CLOSE_READER(re, s)
825 return code;
826}
827
828//#define TRACE
829
830#ifdef TRACE
61f040dd 831#include "avcodec.h"
caa336b4
MN
832static inline void print_bin(int bits, int n){
833 int i;
115329f1 834
caa336b4 835 for(i=n-1; i>=0; i--){
61f040dd 836 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
caa336b4
MN
837 }
838 for(i=n; i<24; i++)
61f040dd 839 av_log(NULL, AV_LOG_DEBUG, " ");
caa336b4
MN
840}
841
61f040dd 842static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
caa336b4 843 int r= get_bits(s, n);
115329f1 844
caa336b4 845 print_bin(r, n);
61f040dd 846 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
847 return r;
848}
61f040dd 849static 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
850 int show= show_bits(s, 24);
851 int pos= get_bits_count(s);
852 int r= get_vlc2(s, table, bits, max_depth);
853 int len= get_bits_count(s) - pos;
854 int bits2= show>>(24-len);
115329f1 855
caa336b4 856 print_bin(bits2, len);
115329f1 857
61f040dd 858 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
859 return r;
860}
61f040dd 861static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
caa336b4
MN
862 int show= show_bits(s, n);
863 int r= get_xbits(s, n);
115329f1 864
caa336b4 865 print_bin(show, n);
61f040dd 866 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
867 return r;
868}
869
870#define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
871#define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
872#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
873#define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
874#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
875
876#define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
877
878#else //TRACE
879#define tprintf(...) {}
880#endif
881
7d8b13b4 882static inline int decode012(GetBitContext *gb){
a64a006e
MN
883 int n;
884 n = get_bits1(gb);
885 if (n == 0)
886 return 0;
887 else
888 return get_bits1(gb) + 1;
889}
890
caa336b4 891#endif /* BITSTREAM_H */