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