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