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)
57 s->buf_end = s->buf + buffer_size;
58 #ifdef ALT_BITSTREAM_WRITER
60 ((uint32_t*)(s->buf))[0]=0;
61 // memset(buffer, 0, buffer_size);
69 /* return the number of bits output */
70 static inline int put_bits_count(PutBitContext *s)
72 #ifdef ALT_BITSTREAM_WRITER
75 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
79 /* pad the end of the output stream with zeros */
80 static inline void flush_put_bits(PutBitContext *s)
82 #ifdef ALT_BITSTREAM_WRITER
85 s->bit_buf<<= s->bit_left;
86 while (s->bit_left < 32) {
87 /* XXX: should test end of buffer */
88 *s->buf_ptr++=s->bit_buf >> 24;
97 void align_put_bits(PutBitContext *s);
98 void put_string(PutBitContext * pbc, char *s, int put_zero);
101 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
102 typedef struct GetBitContext {
103 const uint8_t *buffer, *buffer_end;
104 #ifdef ALT_BITSTREAM_READER
106 #elif defined LIBMPEG2_BITSTREAM_READER
110 #elif defined A32_BITSTREAM_READER
111 uint32_t *buffer_ptr;
119 #define VLC_TYPE int16_t
123 VLC_TYPE (*table)[2]; ///< code, bits
124 int table_size, table_allocated;
127 typedef struct RL_VLC_ELEM {
134 #define UNALIGNED_STORES_ARE_BAD
137 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
138 #if defined(ARCH_X86) || defined(ARCH_X86_64)
139 # define unaligned32(a) (*(const uint32_t*)(a))
142 static inline uint32_t unaligned32(const void *v) {
145 } __attribute__((packed));
147 return ((const struct Unaligned *) v)->i;
149 # elif defined(__DECC)
150 static inline uint32_t unaligned32(const void *v) {
151 return *(const __unaligned uint32_t *) v;
154 static inline uint32_t unaligned32(const void *v) {
155 return *(const uint32_t *) v;
160 #ifndef ALT_BITSTREAM_WRITER
161 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
163 unsigned int bit_buf;
167 st_out_bit_counts[st_current_index] += n;
169 // printf("put_bits=%d %x\n", n, value);
170 assert(n == 32 || value < (1U << n));
172 bit_buf = s->bit_buf;
173 bit_left = s->bit_left;
175 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
178 bit_buf = (bit_buf<<n) | value;
182 bit_buf |= value >> (n - bit_left);
183 #ifdef UNALIGNED_STORES_ARE_BAD
184 if (3 & (intptr_t) s->buf_ptr) {
185 s->buf_ptr[0] = bit_buf >> 24;
186 s->buf_ptr[1] = bit_buf >> 16;
187 s->buf_ptr[2] = bit_buf >> 8;
188 s->buf_ptr[3] = bit_buf ;
191 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
192 //printf("bitbuf = %08x\n", bit_buf);
198 s->bit_buf = bit_buf;
199 s->bit_left = bit_left;
204 #ifdef ALT_BITSTREAM_WRITER
205 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
207 # ifdef ALIGNED_BITSTREAM_WRITER
208 # if defined(ARCH_X86) || defined(ARCH_X86_64)
210 "movl %0, %%ecx \n\t"
211 "xorl %%eax, %%eax \n\t"
212 "shrdl %%cl, %1, %%eax \n\t"
214 "movl %0, %%ecx \n\t"
215 "shrl $3, %%ecx \n\t"
216 "andl $0xFFFFFFFC, %%ecx \n\t"
218 "orl %1, (%2, %%ecx) \n\t"
221 "movl %%eax, 4(%2, %%ecx) \n\t"
222 : "=&r" (s->index), "=&r" (value)
223 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
228 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
232 ptr[0] |= be2me_32(value>>(index&31));
233 ptr[1] = be2me_32(value<<(32-(index&31)));
234 //if(n>24) printf("%d %d\n", n, value);
238 # else //ALIGNED_BITSTREAM_WRITER
239 # if defined(ARCH_X86) || defined(ARCH_X86_64)
241 "movl $7, %%ecx \n\t"
242 "andl %0, %%ecx \n\t"
243 "addl %3, %%ecx \n\t"
247 "movl %0, %%ecx \n\t"
248 "shrl $3, %%ecx \n\t"
249 "orl %1, (%%ecx, %2) \n\t"
251 "movl $0, 4(%%ecx, %2) \n\t"
252 : "=&r" (s->index), "=&r" (value)
253 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
258 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
260 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
262 //if(n>24) printf("%d %d\n", n, value);
266 # endif //!ALIGNED_BITSTREAM_WRITER
271 static inline uint8_t* pbBufPtr(PutBitContext *s)
273 #ifdef ALT_BITSTREAM_WRITER
274 return s->buf + (s->index>>3);
282 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
284 static inline void skip_put_bytes(PutBitContext *s, int n){
285 assert((put_bits_count(s)&7)==0);
286 #ifdef ALT_BITSTREAM_WRITER
287 FIXME may need some cleaning of the buffer
290 assert(s->bit_left==32);
296 * skips the given number of bits.
297 * must only be used if the actual values in the bitstream dont matter
299 static inline void skip_put_bits(PutBitContext *s, int n){
300 #ifdef ALT_BITSTREAM_WRITER
304 s->buf_ptr-= s->bit_left>>5;
310 * Changes the end of the buffer.
312 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
313 s->buf_end= s->buf + size;
316 /* Bitstream reader API docs:
318 abritary name which is used as prefix for the internal variables
323 OPEN_READER(name, gb)
324 loads gb into local variables
326 CLOSE_READER(name, gb)
327 stores local vars in gb
329 UPDATE_CACHE(name, gb)
330 refills the internal cache from the bitstream
331 after this call at least MIN_CACHE_BITS will be available,
334 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
336 SHOW_UBITS(name, gb, num)
337 will return the nest num bits
339 SHOW_SBITS(name, gb, num)
340 will return the nest num bits and do sign extension
342 SKIP_BITS(name, gb, num)
343 will skip over the next num bits
344 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
346 SKIP_CACHE(name, gb, num)
347 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
349 SKIP_COUNTER(name, gb, num)
350 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
352 LAST_SKIP_CACHE(name, gb, num)
353 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
355 LAST_SKIP_BITS(name, gb, num)
356 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
358 for examples see get_bits, show_bits, skip_bits, get_vlc
361 static inline int unaligned32_be(const void *v)
365 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
367 return be2me_32( unaligned32(v)); //original
371 #ifdef ALT_BITSTREAM_READER
372 # define MIN_CACHE_BITS 25
374 # define OPEN_READER(name, gb)\
375 int name##_index= (gb)->index;\
376 int name##_cache= 0;\
378 # define CLOSE_READER(name, gb)\
379 (gb)->index= name##_index;\
381 # define UPDATE_CACHE(name, gb)\
382 name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
384 # define SKIP_CACHE(name, gb, num)\
385 name##_cache <<= (num);\
388 # define SKIP_COUNTER(name, gb, num)\
389 name##_index += (num);\
391 # define SKIP_BITS(name, gb, num)\
393 SKIP_CACHE(name, gb, num)\
394 SKIP_COUNTER(name, gb, num)\
397 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
398 # define LAST_SKIP_CACHE(name, gb, num) ;
400 # define SHOW_UBITS(name, gb, num)\
401 NEG_USR32(name##_cache, num)
403 # define SHOW_SBITS(name, gb, num)\
404 NEG_SSR32(name##_cache, num)
406 # define GET_CACHE(name, gb)\
407 ((uint32_t)name##_cache)
409 static inline int get_bits_count(GetBitContext *s){
412 #elif defined LIBMPEG2_BITSTREAM_READER
413 //libmpeg2 like reader
415 # define MIN_CACHE_BITS 17
417 # define OPEN_READER(name, gb)\
418 int name##_bit_count=(gb)->bit_count;\
419 int name##_cache= (gb)->cache;\
420 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
422 # define CLOSE_READER(name, gb)\
423 (gb)->bit_count= name##_bit_count;\
424 (gb)->cache= name##_cache;\
425 (gb)->buffer_ptr= name##_buffer_ptr;\
427 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
429 # define UPDATE_CACHE(name, gb)\
430 if(name##_bit_count >= 0){\
431 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
432 name##_buffer_ptr += 2;\
433 name##_bit_count-= 16;\
438 # define UPDATE_CACHE(name, gb)\
439 if(name##_bit_count >= 0){\
440 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
441 name##_buffer_ptr+=2;\
442 name##_bit_count-= 16;\
447 # define SKIP_CACHE(name, gb, num)\
448 name##_cache <<= (num);\
450 # define SKIP_COUNTER(name, gb, num)\
451 name##_bit_count += (num);\
453 # define SKIP_BITS(name, gb, num)\
455 SKIP_CACHE(name, gb, num)\
456 SKIP_COUNTER(name, gb, num)\
459 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
460 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
462 # define SHOW_UBITS(name, gb, num)\
463 NEG_USR32(name##_cache, num)
465 # define SHOW_SBITS(name, gb, num)\
466 NEG_SSR32(name##_cache, num)
468 # define GET_CACHE(name, gb)\
469 ((uint32_t)name##_cache)
471 static inline int get_bits_count(GetBitContext *s){
472 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
475 #elif defined A32_BITSTREAM_READER
477 # define MIN_CACHE_BITS 32
479 # define OPEN_READER(name, gb)\
480 int name##_bit_count=(gb)->bit_count;\
481 uint32_t name##_cache0= (gb)->cache0;\
482 uint32_t name##_cache1= (gb)->cache1;\
483 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
485 # define CLOSE_READER(name, gb)\
486 (gb)->bit_count= name##_bit_count;\
487 (gb)->cache0= name##_cache0;\
488 (gb)->cache1= name##_cache1;\
489 (gb)->buffer_ptr= name##_buffer_ptr;\
491 # define UPDATE_CACHE(name, gb)\
492 if(name##_bit_count > 0){\
493 const uint32_t next= be2me_32( *name##_buffer_ptr );\
494 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
495 name##_cache1 |= next<<name##_bit_count;\
496 name##_buffer_ptr++;\
497 name##_bit_count-= 32;\
500 #if defined(ARCH_X86) || defined(ARCH_X86_64)
501 # define SKIP_CACHE(name, gb, num)\
503 "shldl %2, %1, %0 \n\t"\
505 : "+r" (name##_cache0), "+r" (name##_cache1)\
506 : "Ic" ((uint8_t)num)\
509 # define SKIP_CACHE(name, gb, num)\
510 name##_cache0 <<= (num);\
511 name##_cache0 |= NEG_USR32(name##_cache1,num);\
512 name##_cache1 <<= (num);
515 # define SKIP_COUNTER(name, gb, num)\
516 name##_bit_count += (num);\
518 # define SKIP_BITS(name, gb, num)\
520 SKIP_CACHE(name, gb, num)\
521 SKIP_COUNTER(name, gb, num)\
524 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
525 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
527 # define SHOW_UBITS(name, gb, num)\
528 NEG_USR32(name##_cache0, num)
530 # define SHOW_SBITS(name, gb, num)\
531 NEG_SSR32(name##_cache0, num)
533 # define GET_CACHE(name, gb)\
536 static inline int get_bits_count(GetBitContext *s){
537 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
543 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
544 * if MSB not set it is negative
545 * @param n length in bits
548 static inline int get_xbits(GetBitContext *s, int n){
550 register int32_t cache;
553 cache = GET_CACHE(re,s);
554 if ((int32_t)cache<0) { //MSB=1
555 tmp = NEG_USR32(cache,n);
557 // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
558 // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
559 tmp = - NEG_USR32(~cache,n);
561 LAST_SKIP_BITS(re, s, n)
566 static inline int get_sbits(GetBitContext *s, int n){
570 tmp= SHOW_SBITS(re, s, n);
571 LAST_SKIP_BITS(re, s, n)
578 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
580 static inline unsigned int get_bits(GetBitContext *s, int n){
584 tmp= SHOW_UBITS(re, s, n);
585 LAST_SKIP_BITS(re, s, n)
590 unsigned int get_bits_long(GetBitContext *s, int n);
594 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
596 static inline unsigned int show_bits(GetBitContext *s, int n){
600 tmp= SHOW_UBITS(re, s, n);
601 // CLOSE_READER(re, s)
605 unsigned int show_bits_long(GetBitContext *s, int n);
607 static inline void skip_bits(GetBitContext *s, int n){
608 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
611 LAST_SKIP_BITS(re, s, n)
615 static inline unsigned int get_bits1(GetBitContext *s){
616 #ifdef ALT_BITSTREAM_READER
618 uint8_t result= s->buffer[ index>>3 ];
619 result<<= (index&0x07);
626 return get_bits(s, 1);
630 static inline unsigned int show_bits1(GetBitContext *s){
631 return show_bits(s, 1);
634 static inline void skip_bits1(GetBitContext *s){
639 * init GetBitContext.
640 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
641 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
642 * @param bit_size the size of the buffer in bits
644 static inline void init_get_bits(GetBitContext *s,
645 const uint8_t *buffer, int bit_size)
647 const int buffer_size= (bit_size+7)>>3;
650 s->size_in_bits= bit_size;
651 s->buffer_end= buffer + buffer_size;
652 #ifdef ALT_BITSTREAM_READER
654 #elif defined LIBMPEG2_BITSTREAM_READER
655 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
658 s->cache = (*buffer++)<<24;
659 s->buffer_ptr = buffer;
664 s->buffer_ptr = buffer;
668 #elif defined A32_BITSTREAM_READER
669 s->buffer_ptr = (uint32_t*)buffer;
680 #ifdef A32_BITSTREAM_READER
685 int check_marker(GetBitContext *s, const char *msg);
686 void align_get_bits(GetBitContext *s);
687 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
688 const void *bits, int bits_wrap, int bits_size,
689 const void *codes, int codes_wrap, int codes_size,
691 void free_vlc(VLC *vlc);
695 * if the vlc code is invalid and max_depth=1 than no bits will be removed
696 * if the vlc code is invalid and max_depth>1 than the number of bits removed
699 #define GET_VLC(code, name, gb, table, bits, max_depth)\
701 int n, index, nb_bits;\
703 index= SHOW_UBITS(name, gb, bits);\
704 code = table[index][0];\
705 n = table[index][1];\
707 if(max_depth > 1 && n < 0){\
708 LAST_SKIP_BITS(name, gb, bits)\
709 UPDATE_CACHE(name, gb)\
713 index= SHOW_UBITS(name, gb, nb_bits) + code;\
714 code = table[index][0];\
715 n = table[index][1];\
716 if(max_depth > 2 && n < 0){\
717 LAST_SKIP_BITS(name, gb, nb_bits)\
718 UPDATE_CACHE(name, gb)\
722 index= SHOW_UBITS(name, gb, nb_bits) + code;\
723 code = table[index][0];\
724 n = table[index][1];\
727 SKIP_BITS(name, gb, n)\
730 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
732 int n, index, nb_bits;\
734 index= SHOW_UBITS(name, gb, bits);\
735 level = table[index].level;\
736 n = table[index].len;\
738 if(max_depth > 1 && n < 0){\
739 LAST_SKIP_BITS(name, gb, bits)\
740 UPDATE_CACHE(name, gb)\
744 index= SHOW_UBITS(name, gb, nb_bits) + level;\
745 level = table[index].level;\
746 n = table[index].len;\
748 run= table[index].run;\
749 SKIP_BITS(name, gb, n)\
752 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
753 static inline int get_vlc(GetBitContext *s, VLC *vlc)
756 VLC_TYPE (*table)[2]= vlc->table;
761 GET_VLC(code, re, s, table, vlc->bits, 3)
768 * parses a vlc code, faster then get_vlc()
769 * @param bits is the number of bits which will be read at once, must be
770 * identical to nb_bits in init_vlc()
771 * @param max_depth is the number of times bits bits must be readed to completly
772 * read the longest vlc code
773 * = (max_vlc_length + bits - 1) / bits
775 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
776 int bits, int max_depth)
783 GET_VLC(code, re, s, table, bits, max_depth)
793 static inline void print_bin(int bits, int n){
796 for(i=n-1; i>=0; i--){
797 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
800 av_log(NULL, AV_LOG_DEBUG, " ");
803 static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
804 int r= get_bits(s, n);
807 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);
810 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
811 int show= show_bits(s, 24);
812 int pos= get_bits_count(s);
813 int r= get_vlc2(s, table, bits, max_depth);
814 int len= get_bits_count(s) - pos;
815 int bits2= show>>(24-len);
817 print_bin(bits2, len);
819 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
822 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
823 int show= show_bits(s, n);
824 int r= get_xbits(s, n);
827 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);
831 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
832 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
833 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
834 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
835 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
837 #define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
840 #define tprintf(...) {}
843 static inline int decode012(GetBitContext *gb){
849 return get_bits1(gb) + 1;
852 #endif /* BITSTREAM_H */