3 * bitstream api header.
9 //#define ALT_BITSTREAM_WRITER
10 //#define ALIGNED_BITSTREAM_WRITER
12 #define ALT_BITSTREAM_READER
13 //#define LIBMPEG2_BITSTREAM_READER
14 //#define A32_BITSTREAM_READER
15 #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
17 extern const uint8_t ff_reverse[256];
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"
24 : "ic" ((uint8_t)(-s))
28 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
29 asm ("shrl %1, %0\n\t"
31 : "ic" ((uint8_t)(-s))
36 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
37 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
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;
50 uint8_t *buf, *buf_ptr, *buf_end;
54 static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
62 s->buf_end = s->buf + buffer_size;
63 #ifdef ALT_BITSTREAM_WRITER
65 ((uint32_t*)(s->buf))[0]=0;
66 // memset(buffer, 0, buffer_size);
74 /* return the number of bits output */
75 static inline int put_bits_count(PutBitContext *s)
77 #ifdef ALT_BITSTREAM_WRITER
80 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
84 /* pad the end of the output stream with zeros */
85 static inline void flush_put_bits(PutBitContext *s)
87 #ifdef ALT_BITSTREAM_WRITER
90 s->bit_buf<<= s->bit_left;
91 while (s->bit_left < 32) {
92 /* XXX: should test end of buffer */
93 *s->buf_ptr++=s->bit_buf >> 24;
102 void align_put_bits(PutBitContext *s);
103 void ff_put_string(PutBitContext * pbc, char *s, int put_zero);
106 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
107 typedef struct GetBitContext {
108 const uint8_t *buffer, *buffer_end;
109 #ifdef ALT_BITSTREAM_READER
111 #elif defined LIBMPEG2_BITSTREAM_READER
115 #elif defined A32_BITSTREAM_READER
116 uint32_t *buffer_ptr;
124 #define VLC_TYPE int16_t
128 VLC_TYPE (*table)[2]; ///< code, bits
129 int table_size, table_allocated;
132 typedef struct RL_VLC_ELEM {
138 #if defined(ARCH_SPARC) || defined(ARCH_ARMV4L)
139 #define UNALIGNED_STORES_ARE_BAD
142 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
143 #if defined(ARCH_X86) || defined(ARCH_X86_64)
144 # define unaligned32(a) (*(const uint32_t*)(a))
147 static inline uint32_t unaligned32(const void *v) {
150 } __attribute__((packed));
152 return ((const struct Unaligned *) v)->i;
154 # elif defined(__DECC)
155 static inline uint32_t unaligned32(const void *v) {
156 return *(const __unaligned uint32_t *) v;
159 static inline uint32_t unaligned32(const void *v) {
160 return *(const uint32_t *) v;
165 #ifndef ALT_BITSTREAM_WRITER
166 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
168 unsigned int bit_buf;
172 st_out_bit_counts[st_current_index] += n;
174 // printf("put_bits=%d %x\n", n, value);
175 assert(n == 32 || value < (1U << n));
177 bit_buf = s->bit_buf;
178 bit_left = s->bit_left;
180 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
183 bit_buf = (bit_buf<<n) | value;
187 bit_buf |= value >> (n - bit_left);
188 #ifdef UNALIGNED_STORES_ARE_BAD
189 if (3 & (intptr_t) s->buf_ptr) {
190 s->buf_ptr[0] = bit_buf >> 24;
191 s->buf_ptr[1] = bit_buf >> 16;
192 s->buf_ptr[2] = bit_buf >> 8;
193 s->buf_ptr[3] = bit_buf ;
196 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
197 //printf("bitbuf = %08x\n", bit_buf);
203 s->bit_buf = bit_buf;
204 s->bit_left = bit_left;
209 #ifdef ALT_BITSTREAM_WRITER
210 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
212 # ifdef ALIGNED_BITSTREAM_WRITER
213 # if defined(ARCH_X86) || defined(ARCH_X86_64)
215 "movl %0, %%ecx \n\t"
216 "xorl %%eax, %%eax \n\t"
217 "shrdl %%cl, %1, %%eax \n\t"
219 "movl %0, %%ecx \n\t"
220 "shrl $3, %%ecx \n\t"
221 "andl $0xFFFFFFFC, %%ecx \n\t"
223 "orl %1, (%2, %%ecx) \n\t"
226 "movl %%eax, 4(%2, %%ecx) \n\t"
227 : "=&r" (s->index), "=&r" (value)
228 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
233 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
237 ptr[0] |= be2me_32(value>>(index&31));
238 ptr[1] = be2me_32(value<<(32-(index&31)));
239 //if(n>24) printf("%d %d\n", n, value);
243 # else //ALIGNED_BITSTREAM_WRITER
244 # if defined(ARCH_X86) || defined(ARCH_X86_64)
246 "movl $7, %%ecx \n\t"
247 "andl %0, %%ecx \n\t"
248 "addl %3, %%ecx \n\t"
252 "movl %0, %%ecx \n\t"
253 "shrl $3, %%ecx \n\t"
254 "orl %1, (%%ecx, %2) \n\t"
256 "movl $0, 4(%%ecx, %2) \n\t"
257 : "=&r" (s->index), "=&r" (value)
258 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
263 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
265 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
267 //if(n>24) printf("%d %d\n", n, value);
271 # endif //!ALIGNED_BITSTREAM_WRITER
276 static inline uint8_t* pbBufPtr(PutBitContext *s)
278 #ifdef ALT_BITSTREAM_WRITER
279 return s->buf + (s->index>>3);
287 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
289 static inline void skip_put_bytes(PutBitContext *s, int n){
290 assert((put_bits_count(s)&7)==0);
291 #ifdef ALT_BITSTREAM_WRITER
292 FIXME may need some cleaning of the buffer
295 assert(s->bit_left==32);
301 * skips the given number of bits.
302 * must only be used if the actual values in the bitstream dont matter
304 static inline void skip_put_bits(PutBitContext *s, int n){
305 #ifdef ALT_BITSTREAM_WRITER
309 s->buf_ptr-= s->bit_left>>5;
315 * Changes the end of the buffer.
317 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
318 s->buf_end= s->buf + size;
321 /* Bitstream reader API docs:
323 abritary name which is used as prefix for the internal variables
328 OPEN_READER(name, gb)
329 loads gb into local variables
331 CLOSE_READER(name, gb)
332 stores local vars in gb
334 UPDATE_CACHE(name, gb)
335 refills the internal cache from the bitstream
336 after this call at least MIN_CACHE_BITS will be available,
339 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
341 SHOW_UBITS(name, gb, num)
342 will return the next num bits
344 SHOW_SBITS(name, gb, num)
345 will return the next num bits and do sign extension
347 SKIP_BITS(name, gb, num)
348 will skip over the next num bits
349 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
351 SKIP_CACHE(name, gb, num)
352 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
354 SKIP_COUNTER(name, gb, num)
355 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
357 LAST_SKIP_CACHE(name, gb, num)
358 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
360 LAST_SKIP_BITS(name, gb, num)
361 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
363 for examples see get_bits, show_bits, skip_bits, get_vlc
366 static inline int unaligned32_be(const void *v)
370 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
372 return be2me_32( unaligned32(v)); //original
376 static inline int unaligned32_le(const void *v)
380 return (((p[3]<<8) | p[2])<<16) | (p[1]<<8) | (p[0]);
382 return le2me_32( unaligned32(v)); //original
386 #ifdef ALT_BITSTREAM_READER
387 # define MIN_CACHE_BITS 25
389 # define OPEN_READER(name, gb)\
390 int name##_index= (gb)->index;\
391 int name##_cache= 0;\
393 # define CLOSE_READER(name, gb)\
394 (gb)->index= name##_index;\
396 # ifdef ALT_BITSTREAM_READER_LE
397 # define UPDATE_CACHE(name, gb)\
398 name##_cache= unaligned32_le( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
400 # define SKIP_CACHE(name, gb, num)\
401 name##_cache >>= (num);
403 # define UPDATE_CACHE(name, gb)\
404 name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
406 # define SKIP_CACHE(name, gb, num)\
407 name##_cache <<= (num);
411 # define SKIP_COUNTER(name, gb, num)\
412 name##_index += (num);\
414 # define SKIP_BITS(name, gb, num)\
416 SKIP_CACHE(name, gb, num)\
417 SKIP_COUNTER(name, gb, num)\
420 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
421 # define LAST_SKIP_CACHE(name, gb, num) ;
423 # ifdef ALT_BITSTREAM_READER_LE
424 # define SHOW_UBITS(name, gb, num)\
425 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
427 # define SHOW_UBITS(name, gb, num)\
428 NEG_USR32(name##_cache, num)
431 # define SHOW_SBITS(name, gb, num)\
432 NEG_SSR32(name##_cache, num)
434 # define GET_CACHE(name, gb)\
435 ((uint32_t)name##_cache)
437 static inline int get_bits_count(GetBitContext *s){
440 #elif defined LIBMPEG2_BITSTREAM_READER
441 //libmpeg2 like reader
443 # define MIN_CACHE_BITS 17
445 # define OPEN_READER(name, gb)\
446 int name##_bit_count=(gb)->bit_count;\
447 int name##_cache= (gb)->cache;\
448 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
450 # define CLOSE_READER(name, gb)\
451 (gb)->bit_count= name##_bit_count;\
452 (gb)->cache= name##_cache;\
453 (gb)->buffer_ptr= name##_buffer_ptr;\
455 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
457 # define UPDATE_CACHE(name, gb)\
458 if(name##_bit_count >= 0){\
459 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
460 name##_buffer_ptr += 2;\
461 name##_bit_count-= 16;\
466 # define UPDATE_CACHE(name, gb)\
467 if(name##_bit_count >= 0){\
468 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
469 name##_buffer_ptr+=2;\
470 name##_bit_count-= 16;\
475 # define SKIP_CACHE(name, gb, num)\
476 name##_cache <<= (num);\
478 # define SKIP_COUNTER(name, gb, num)\
479 name##_bit_count += (num);\
481 # define SKIP_BITS(name, gb, num)\
483 SKIP_CACHE(name, gb, num)\
484 SKIP_COUNTER(name, gb, num)\
487 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
488 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
490 # define SHOW_UBITS(name, gb, num)\
491 NEG_USR32(name##_cache, num)
493 # define SHOW_SBITS(name, gb, num)\
494 NEG_SSR32(name##_cache, num)
496 # define GET_CACHE(name, gb)\
497 ((uint32_t)name##_cache)
499 static inline int get_bits_count(GetBitContext *s){
500 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
503 #elif defined A32_BITSTREAM_READER
505 # define MIN_CACHE_BITS 32
507 # define OPEN_READER(name, gb)\
508 int name##_bit_count=(gb)->bit_count;\
509 uint32_t name##_cache0= (gb)->cache0;\
510 uint32_t name##_cache1= (gb)->cache1;\
511 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
513 # define CLOSE_READER(name, gb)\
514 (gb)->bit_count= name##_bit_count;\
515 (gb)->cache0= name##_cache0;\
516 (gb)->cache1= name##_cache1;\
517 (gb)->buffer_ptr= name##_buffer_ptr;\
519 # define UPDATE_CACHE(name, gb)\
520 if(name##_bit_count > 0){\
521 const uint32_t next= be2me_32( *name##_buffer_ptr );\
522 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
523 name##_cache1 |= next<<name##_bit_count;\
524 name##_buffer_ptr++;\
525 name##_bit_count-= 32;\
528 #if defined(ARCH_X86) || defined(ARCH_X86_64)
529 # define SKIP_CACHE(name, gb, num)\
531 "shldl %2, %1, %0 \n\t"\
533 : "+r" (name##_cache0), "+r" (name##_cache1)\
534 : "Ic" ((uint8_t)num)\
537 # define SKIP_CACHE(name, gb, num)\
538 name##_cache0 <<= (num);\
539 name##_cache0 |= NEG_USR32(name##_cache1,num);\
540 name##_cache1 <<= (num);
543 # define SKIP_COUNTER(name, gb, num)\
544 name##_bit_count += (num);\
546 # define SKIP_BITS(name, gb, num)\
548 SKIP_CACHE(name, gb, num)\
549 SKIP_COUNTER(name, gb, num)\
552 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
553 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
555 # define SHOW_UBITS(name, gb, num)\
556 NEG_USR32(name##_cache0, num)
558 # define SHOW_SBITS(name, gb, num)\
559 NEG_SSR32(name##_cache0, num)
561 # define GET_CACHE(name, gb)\
564 static inline int get_bits_count(GetBitContext *s){
565 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
571 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
572 * if MSB not set it is negative
573 * @param n length in bits
576 static inline int get_xbits(GetBitContext *s, int n){
578 register int32_t cache;
581 cache = GET_CACHE(re,s);
582 if ((int32_t)cache<0) { //MSB=1
583 tmp = NEG_USR32(cache,n);
585 // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
586 // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
587 tmp = - NEG_USR32(~cache,n);
589 LAST_SKIP_BITS(re, s, n)
594 static inline int get_sbits(GetBitContext *s, int n){
598 tmp= SHOW_SBITS(re, s, n);
599 LAST_SKIP_BITS(re, s, n)
606 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
608 static inline unsigned int get_bits(GetBitContext *s, int n){
612 tmp= SHOW_UBITS(re, s, n);
613 LAST_SKIP_BITS(re, s, n)
618 unsigned int get_bits_long(GetBitContext *s, int n);
622 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
624 static inline unsigned int show_bits(GetBitContext *s, int n){
628 tmp= SHOW_UBITS(re, s, n);
629 // CLOSE_READER(re, s)
633 unsigned int show_bits_long(GetBitContext *s, int n);
635 static inline void skip_bits(GetBitContext *s, int n){
636 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
639 LAST_SKIP_BITS(re, s, n)
643 static inline unsigned int get_bits1(GetBitContext *s){
644 #ifdef ALT_BITSTREAM_READER
646 uint8_t result= s->buffer[ index>>3 ];
647 #ifdef ALT_BITSTREAM_READER_LE
648 result>>= (index&0x07);
651 result<<= (index&0x07);
659 return get_bits(s, 1);
663 static inline unsigned int show_bits1(GetBitContext *s){
664 return show_bits(s, 1);
667 static inline void skip_bits1(GetBitContext *s){
672 * init GetBitContext.
673 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
674 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
675 * @param bit_size the size of the buffer in bits
677 static inline void init_get_bits(GetBitContext *s,
678 const uint8_t *buffer, int bit_size)
680 int buffer_size= (bit_size+7)>>3;
681 if(buffer_size < 0 || bit_size < 0) {
682 buffer_size = bit_size = 0;
687 s->size_in_bits= bit_size;
688 s->buffer_end= buffer + buffer_size;
689 #ifdef ALT_BITSTREAM_READER
691 #elif defined LIBMPEG2_BITSTREAM_READER
692 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
695 s->cache = (*buffer++)<<24;
696 s->buffer_ptr = buffer;
701 s->buffer_ptr = buffer;
705 #elif defined A32_BITSTREAM_READER
706 s->buffer_ptr = (uint32_t*)buffer;
717 #ifdef A32_BITSTREAM_READER
722 int check_marker(GetBitContext *s, const char *msg);
723 void align_get_bits(GetBitContext *s);
724 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
725 const void *bits, int bits_wrap, int bits_size,
726 const void *codes, int codes_wrap, int codes_size,
728 #define INIT_VLC_USE_STATIC 1
729 #define INIT_VLC_LE 2
730 void free_vlc(VLC *vlc);
734 * if the vlc code is invalid and max_depth=1 than no bits will be removed
735 * if the vlc code is invalid and max_depth>1 than the number of bits removed
738 #define GET_VLC(code, name, gb, table, bits, max_depth)\
740 int n, index, nb_bits;\
742 index= SHOW_UBITS(name, gb, bits);\
743 code = table[index][0];\
744 n = table[index][1];\
746 if(max_depth > 1 && n < 0){\
747 LAST_SKIP_BITS(name, gb, bits)\
748 UPDATE_CACHE(name, gb)\
752 index= SHOW_UBITS(name, gb, nb_bits) + code;\
753 code = table[index][0];\
754 n = table[index][1];\
755 if(max_depth > 2 && n < 0){\
756 LAST_SKIP_BITS(name, gb, nb_bits)\
757 UPDATE_CACHE(name, gb)\
761 index= SHOW_UBITS(name, gb, nb_bits) + code;\
762 code = table[index][0];\
763 n = table[index][1];\
766 SKIP_BITS(name, gb, n)\
769 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
771 int n, index, nb_bits;\
773 index= SHOW_UBITS(name, gb, bits);\
774 level = table[index].level;\
775 n = table[index].len;\
777 if(max_depth > 1 && n < 0){\
778 SKIP_BITS(name, gb, bits)\
780 UPDATE_CACHE(name, gb)\
785 index= SHOW_UBITS(name, gb, nb_bits) + level;\
786 level = table[index].level;\
787 n = table[index].len;\
789 run= table[index].run;\
790 SKIP_BITS(name, gb, n)\
795 * parses a vlc code, faster then get_vlc()
796 * @param bits is the number of bits which will be read at once, must be
797 * identical to nb_bits in init_vlc()
798 * @param max_depth is the number of times bits bits must be readed to completly
799 * read the longest vlc code
800 * = (max_vlc_length + bits - 1) / bits
802 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
803 int bits, int max_depth)
810 GET_VLC(code, re, s, table, bits, max_depth)
820 static inline void print_bin(int bits, int n){
823 for(i=n-1; i>=0; i--){
824 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
827 av_log(NULL, AV_LOG_DEBUG, " ");
830 static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
831 int r= get_bits(s, n);
834 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);
837 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
838 int show= show_bits(s, 24);
839 int pos= get_bits_count(s);
840 int r= get_vlc2(s, table, bits, max_depth);
841 int len= get_bits_count(s) - pos;
842 int bits2= show>>(24-len);
844 print_bin(bits2, len);
846 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
849 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
850 int show= show_bits(s, n);
851 int r= get_xbits(s, n);
854 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);
858 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
859 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
860 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
861 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
862 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
864 #define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
867 #define tprintf(...) {}
870 static inline int decode012(GetBitContext *gb){
876 return get_bits1(gb) + 1;
879 #endif /* BITSTREAM_H */