3 * common internal api header.
9 #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
13 //#define ALT_BITSTREAM_WRITER
14 //#define ALIGNED_BITSTREAM_WRITER
16 #define ALT_BITSTREAM_READER
17 //#define LIBMPEG2_BITSTREAM_READER
18 //#define A32_BITSTREAM_READER
19 #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
22 #define M_PI 3.14159265358979323846
25 #ifdef HAVE_AV_CONFIG_H
26 /* only include the following when compiling package */
46 # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
49 #define AVOPTION_CODEC_BOOL(name, help, field) \
50 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
51 #define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
52 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
53 #define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
54 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
55 #define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
56 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
57 #define AVOPTION_CODEC_STRING(name, help, field, str, val) \
58 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
59 #define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
60 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
61 #define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
62 #define AVOPTION_END() AVOPTION_SUB(NULL)
66 extern const struct AVOption avoptions_common[3 + 5];
68 extern const struct AVOption avoptions_common[3];
70 extern const struct AVOption avoptions_workaround_bug[11];
72 #endif /* HAVE_AV_CONFIG_H */
74 /* Suppress restrict if it was not defined in config.h. */
79 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
80 # define always_inline __attribute__((always_inline)) inline
82 # define always_inline inline
85 #ifndef EMULATE_INTTYPES
86 # include <inttypes.h>
88 typedef signed char int8_t;
89 typedef signed short int16_t;
90 typedef signed int int32_t;
91 typedef unsigned char uint8_t;
92 typedef unsigned short uint16_t;
93 typedef unsigned int uint32_t;
96 typedef signed __int64 int64_t;
97 typedef unsigned __int64 uint64_t;
99 typedef signed long long int64_t;
100 typedef unsigned long long uint64_t;
101 # endif /* other OS */
102 #endif /* HAVE_INTTYPES_H */
105 #define INT64_MAX 9223372036854775807LL
108 #ifdef EMULATE_FAST_INT
109 /* note that we don't emulate 64bit ints */
110 typedef signed char int_fast8_t;
111 typedef signed int int_fast16_t;
112 typedef signed int int_fast32_t;
113 typedef unsigned char uint_fast8_t;
114 typedef unsigned int uint_fast16_t;
115 typedef unsigned int uint_fast32_t;
118 #if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
119 static inline float floorf(float f) {
129 # define int64_t_C(c) (c ## i64)
130 # define uint64_t_C(c) (c ## i64)
132 # ifdef HAVE_AV_CONFIG_H
133 # define inline __inline
137 # define int64_t_C(c) (c ## LL)
138 # define uint64_t_C(c) (c ## ULL)
139 # endif /* __MINGW32__ */
141 # ifdef HAVE_AV_CONFIG_H
146 # define snprintf _snprintf
147 # define vsnprintf _vsnprintf
150 /* CONFIG_WIN32 end */
151 #elif defined (CONFIG_OS2)
155 #define int64_t_C(c) (c ## LL)
156 #define uint64_t_C(c) (c ## ULL)
159 #ifdef HAVE_AV_CONFIG_H
161 #ifdef USE_FASTMEMCPY
162 #include "fastmemcpy.h"
167 #endif /* HAVE_AV_CONFIG_H */
175 #define int64_t_C(c) (c ## LL)
176 #define uint64_t_C(c) (c ## ULL)
179 #ifdef HAVE_AV_CONFIG_H
181 # ifdef USE_FASTMEMCPY
182 # include "fastmemcpy.h"
184 # endif /* HAVE_AV_CONFIG_H */
186 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
188 #ifdef HAVE_AV_CONFIG_H
192 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
193 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
194 # define MANGLE(a) "_" #a
196 # define MANGLE(a) #a
207 # if defined(CONFIG_WIN32) && !defined(__MINGW32__)
209 inline void dprintf(const char* fmt,...) {}
214 # define dprintf(fmt,...) printf(fmt, __VA_ARGS__)
216 # define dprintf(fmt,...)
219 # endif /* !CONFIG_WIN32 */
221 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
223 //rounded divison & shift
224 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
226 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
227 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
229 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
230 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
232 extern const uint32_t inverse[256];
235 # define FASTDIV(a,b) \
240 :"=d"(ret),"=a"(dmy)\
241 :"1"(a),"g"(inverse[b])\
245 #elif defined(CONFIG_FASTDIV)
246 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
248 # define FASTDIV(a,b) ((a)/(b))
252 // avoid +32 for shift optimization (gcc should do that ...)
253 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
254 asm ("sarl %1, %0\n\t"
256 : "ic" ((uint8_t)(-s))
260 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
261 asm ("shrl %1, %0\n\t"
263 : "ic" ((uint8_t)(-s))
268 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
269 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
274 struct PutBitContext;
276 typedef void (*WriteDataFunc)(void *, uint8_t *, int);
278 typedef struct PutBitContext {
279 #ifdef ALT_BITSTREAM_WRITER
280 uint8_t *buf, *buf_end;
285 uint8_t *buf, *buf_ptr, *buf_end;
289 void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size);
291 int put_bits_count(PutBitContext *s);
292 void align_put_bits(PutBitContext *s);
293 void flush_put_bits(PutBitContext *s);
294 void put_string(PutBitContext * pbc, char *s, int put_zero);
298 typedef struct GetBitContext {
299 const uint8_t *buffer, *buffer_end;
300 #ifdef ALT_BITSTREAM_READER
302 #elif defined LIBMPEG2_BITSTREAM_READER
306 #elif defined A32_BITSTREAM_READER
307 uint32_t *buffer_ptr;
315 static inline int get_bits_count(GetBitContext *s);
317 #define VLC_TYPE int16_t
321 VLC_TYPE (*table)[2]; ///< code, bits
322 int table_size, table_allocated;
325 typedef struct RL_VLC_ELEM {
332 #define UNALIGNED_STORES_ARE_BAD
335 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
337 # define unaligned32(a) (*(uint32_t*)(a))
340 static inline uint32_t unaligned32(const void *v) {
343 } __attribute__((packed));
345 return ((const struct Unaligned *) v)->i;
347 # elif defined(__DECC)
348 static inline uint32_t unaligned32(const void *v) {
349 return *(const __unaligned uint32_t *) v;
352 static inline uint32_t unaligned32(const void *v) {
353 return *(const uint32_t *) v;
358 #ifndef ALT_BITSTREAM_WRITER
359 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
361 unsigned int bit_buf;
365 st_out_bit_counts[st_current_index] += n;
367 // printf("put_bits=%d %x\n", n, value);
368 assert(n == 32 || value < (1U << n));
370 bit_buf = s->bit_buf;
371 bit_left = s->bit_left;
373 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
376 bit_buf = (bit_buf<<n) | value;
380 bit_buf |= value >> (n - bit_left);
381 #ifdef UNALIGNED_STORES_ARE_BAD
382 if (3 & (int) s->buf_ptr) {
383 s->buf_ptr[0] = bit_buf >> 24;
384 s->buf_ptr[1] = bit_buf >> 16;
385 s->buf_ptr[2] = bit_buf >> 8;
386 s->buf_ptr[3] = bit_buf ;
389 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
390 //printf("bitbuf = %08x\n", bit_buf);
396 s->bit_buf = bit_buf;
397 s->bit_left = bit_left;
402 #ifdef ALT_BITSTREAM_WRITER
403 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
405 # ifdef ALIGNED_BITSTREAM_WRITER
408 "movl %0, %%ecx \n\t"
409 "xorl %%eax, %%eax \n\t"
410 "shrdl %%cl, %1, %%eax \n\t"
412 "movl %0, %%ecx \n\t"
413 "shrl $3, %%ecx \n\t"
414 "andl $0xFFFFFFFC, %%ecx \n\t"
416 "orl %1, (%2, %%ecx) \n\t"
419 "movl %%eax, 4(%2, %%ecx) \n\t"
420 : "=&r" (s->index), "=&r" (value)
421 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
426 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
430 ptr[0] |= be2me_32(value>>(index&31));
431 ptr[1] = be2me_32(value<<(32-(index&31)));
432 //if(n>24) printf("%d %d\n", n, value);
436 # else //ALIGNED_BITSTREAM_WRITER
439 "movl $7, %%ecx \n\t"
440 "andl %0, %%ecx \n\t"
441 "addl %3, %%ecx \n\t"
445 "movl %0, %%ecx \n\t"
446 "shrl $3, %%ecx \n\t"
447 "orl %1, (%%ecx, %2) \n\t"
449 "movl $0, 4(%%ecx, %2) \n\t"
450 : "=&r" (s->index), "=&r" (value)
451 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
456 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
458 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
460 //if(n>24) printf("%d %d\n", n, value);
464 # endif //!ALIGNED_BITSTREAM_WRITER
469 static inline uint8_t* pbBufPtr(PutBitContext *s)
471 #ifdef ALT_BITSTREAM_WRITER
472 return s->buf + (s->index>>3);
480 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
482 static inline void skip_put_bytes(PutBitContext *s, int n){
483 assert((put_bits_count(s)&7)==0);
484 #ifdef ALT_BITSTREAM_WRITER
485 FIXME may need some cleaning of the buffer
488 assert(s->bit_left==32);
494 * Changes the end of the buffer.
496 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
497 s->buf_end= s->buf + size;
500 /* Bitstream reader API docs:
502 abritary name which is used as prefix for the internal variables
507 OPEN_READER(name, gb)
508 loads gb into local variables
510 CLOSE_READER(name, gb)
511 stores local vars in gb
513 UPDATE_CACHE(name, gb)
514 refills the internal cache from the bitstream
515 after this call at least MIN_CACHE_BITS will be available,
518 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
520 SHOW_UBITS(name, gb, num)
521 will return the nest num bits
523 SHOW_SBITS(name, gb, num)
524 will return the nest num bits and do sign extension
526 SKIP_BITS(name, gb, num)
527 will skip over the next num bits
528 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
530 SKIP_CACHE(name, gb, num)
531 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
533 SKIP_COUNTER(name, gb, num)
534 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
536 LAST_SKIP_CACHE(name, gb, num)
537 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
539 LAST_SKIP_BITS(name, gb, num)
540 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
542 for examples see get_bits, show_bits, skip_bits, get_vlc
545 static inline int unaligned32_be(const void *v)
549 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
551 return be2me_32( unaligned32(v)); //original
555 #ifdef ALT_BITSTREAM_READER
556 # define MIN_CACHE_BITS 25
558 # define OPEN_READER(name, gb)\
559 int name##_index= (gb)->index;\
560 int name##_cache= 0;\
562 # define CLOSE_READER(name, gb)\
563 (gb)->index= name##_index;\
565 # define UPDATE_CACHE(name, gb)\
566 name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
568 # define SKIP_CACHE(name, gb, num)\
569 name##_cache <<= (num);\
572 # define SKIP_COUNTER(name, gb, num)\
573 name##_index += (num);\
575 # define SKIP_BITS(name, gb, num)\
577 SKIP_CACHE(name, gb, num)\
578 SKIP_COUNTER(name, gb, num)\
581 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
582 # define LAST_SKIP_CACHE(name, gb, num) ;
584 # define SHOW_UBITS(name, gb, num)\
585 NEG_USR32(name##_cache, num)
587 # define SHOW_SBITS(name, gb, num)\
588 NEG_SSR32(name##_cache, num)
590 # define GET_CACHE(name, gb)\
591 ((uint32_t)name##_cache)
593 static inline int get_bits_count(GetBitContext *s){
596 #elif defined LIBMPEG2_BITSTREAM_READER
597 //libmpeg2 like reader
599 # define MIN_CACHE_BITS 17
601 # define OPEN_READER(name, gb)\
602 int name##_bit_count=(gb)->bit_count;\
603 int name##_cache= (gb)->cache;\
604 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
606 # define CLOSE_READER(name, gb)\
607 (gb)->bit_count= name##_bit_count;\
608 (gb)->cache= name##_cache;\
609 (gb)->buffer_ptr= name##_buffer_ptr;\
611 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
613 # define UPDATE_CACHE(name, gb)\
614 if(name##_bit_count >= 0){\
615 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
616 ((uint16_t*)name##_buffer_ptr)++;\
617 name##_bit_count-= 16;\
622 # define UPDATE_CACHE(name, gb)\
623 if(name##_bit_count >= 0){\
624 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
625 name##_buffer_ptr+=2;\
626 name##_bit_count-= 16;\
631 # define SKIP_CACHE(name, gb, num)\
632 name##_cache <<= (num);\
634 # define SKIP_COUNTER(name, gb, num)\
635 name##_bit_count += (num);\
637 # define SKIP_BITS(name, gb, num)\
639 SKIP_CACHE(name, gb, num)\
640 SKIP_COUNTER(name, gb, num)\
643 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
644 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
646 # define SHOW_UBITS(name, gb, num)\
647 NEG_USR32(name##_cache, num)
649 # define SHOW_SBITS(name, gb, num)\
650 NEG_SSR32(name##_cache, num)
652 # define GET_CACHE(name, gb)\
653 ((uint32_t)name##_cache)
655 static inline int get_bits_count(GetBitContext *s){
656 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
659 #elif defined A32_BITSTREAM_READER
661 # define MIN_CACHE_BITS 32
663 # define OPEN_READER(name, gb)\
664 int name##_bit_count=(gb)->bit_count;\
665 uint32_t name##_cache0= (gb)->cache0;\
666 uint32_t name##_cache1= (gb)->cache1;\
667 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
669 # define CLOSE_READER(name, gb)\
670 (gb)->bit_count= name##_bit_count;\
671 (gb)->cache0= name##_cache0;\
672 (gb)->cache1= name##_cache1;\
673 (gb)->buffer_ptr= name##_buffer_ptr;\
675 # define UPDATE_CACHE(name, gb)\
676 if(name##_bit_count > 0){\
677 const uint32_t next= be2me_32( *name##_buffer_ptr );\
678 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
679 name##_cache1 |= next<<name##_bit_count;\
680 name##_buffer_ptr++;\
681 name##_bit_count-= 32;\
685 # define SKIP_CACHE(name, gb, num)\
687 "shldl %2, %1, %0 \n\t"\
689 : "+r" (name##_cache0), "+r" (name##_cache1)\
690 : "Ic" ((uint8_t)num)\
693 # define SKIP_CACHE(name, gb, num)\
694 name##_cache0 <<= (num);\
695 name##_cache0 |= NEG_USR32(name##_cache1,num);\
696 name##_cache1 <<= (num);
699 # define SKIP_COUNTER(name, gb, num)\
700 name##_bit_count += (num);\
702 # define SKIP_BITS(name, gb, num)\
704 SKIP_CACHE(name, gb, num)\
705 SKIP_COUNTER(name, gb, num)\
708 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
709 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
711 # define SHOW_UBITS(name, gb, num)\
712 NEG_USR32(name##_cache0, num)
714 # define SHOW_SBITS(name, gb, num)\
715 NEG_SSR32(name##_cache0, num)
717 # define GET_CACHE(name, gb)\
720 static inline int get_bits_count(GetBitContext *s){
721 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
727 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
728 * if MSB not set it is negative
729 * @param n length in bits
732 static inline int get_xbits(GetBitContext *s, int n){
734 register int32_t cache;
737 cache = GET_CACHE(re,s);
738 if ((int32_t)cache<0) { //MSB=1
739 tmp = NEG_USR32(cache,n);
741 // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
742 // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
743 tmp = - NEG_USR32(~cache,n);
745 LAST_SKIP_BITS(re, s, n)
750 static inline int get_sbits(GetBitContext *s, int n){
754 tmp= SHOW_SBITS(re, s, n);
755 LAST_SKIP_BITS(re, s, n)
762 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
764 static inline unsigned int get_bits(GetBitContext *s, int n){
768 tmp= SHOW_UBITS(re, s, n);
769 LAST_SKIP_BITS(re, s, n)
774 unsigned int get_bits_long(GetBitContext *s, int n);
778 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
780 static inline unsigned int show_bits(GetBitContext *s, int n){
784 tmp= SHOW_UBITS(re, s, n);
785 // CLOSE_READER(re, s)
789 unsigned int show_bits_long(GetBitContext *s, int n);
791 static inline void skip_bits(GetBitContext *s, int n){
792 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
795 LAST_SKIP_BITS(re, s, n)
799 static inline unsigned int get_bits1(GetBitContext *s){
800 #ifdef ALT_BITSTREAM_READER
802 uint8_t result= s->buffer[ index>>3 ];
803 result<<= (index&0x07);
810 return get_bits(s, 1);
814 static inline unsigned int show_bits1(GetBitContext *s){
815 return show_bits(s, 1);
818 static inline void skip_bits1(GetBitContext *s){
822 void init_get_bits(GetBitContext *s,
823 const uint8_t *buffer, int buffer_size);
825 int check_marker(GetBitContext *s, const char *msg);
826 void align_get_bits(GetBitContext *s);
827 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
828 const void *bits, int bits_wrap, int bits_size,
829 const void *codes, int codes_wrap, int codes_size);
830 void free_vlc(VLC *vlc);
834 * if the vlc code is invalid and max_depth=1 than no bits will be removed
835 * if the vlc code is invalid and max_depth>1 than the number of bits removed
838 #define GET_VLC(code, name, gb, table, bits, max_depth)\
840 int n, index, nb_bits;\
842 index= SHOW_UBITS(name, gb, bits);\
843 code = table[index][0];\
844 n = table[index][1];\
846 if(max_depth > 1 && n < 0){\
847 LAST_SKIP_BITS(name, gb, bits)\
848 UPDATE_CACHE(name, gb)\
852 index= SHOW_UBITS(name, gb, nb_bits) + code;\
853 code = table[index][0];\
854 n = table[index][1];\
855 if(max_depth > 2 && n < 0){\
856 LAST_SKIP_BITS(name, gb, nb_bits)\
857 UPDATE_CACHE(name, gb)\
861 index= SHOW_UBITS(name, gb, nb_bits) + code;\
862 code = table[index][0];\
863 n = table[index][1];\
866 SKIP_BITS(name, gb, n)\
869 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
871 int n, index, nb_bits;\
873 index= SHOW_UBITS(name, gb, bits);\
874 level = table[index].level;\
875 n = table[index].len;\
877 if(max_depth > 1 && n < 0){\
878 LAST_SKIP_BITS(name, gb, bits)\
879 UPDATE_CACHE(name, gb)\
883 index= SHOW_UBITS(name, gb, nb_bits) + level;\
884 level = table[index].level;\
885 n = table[index].len;\
887 run= table[index].run;\
888 SKIP_BITS(name, gb, n)\
891 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
892 static inline int get_vlc(GetBitContext *s, VLC *vlc)
895 VLC_TYPE (*table)[2]= vlc->table;
900 GET_VLC(code, re, s, table, vlc->bits, 3)
907 * parses a vlc code, faster then get_vlc()
908 * @param bits is the number of bits which will be read at once, must be
909 * identical to nb_bits in init_vlc()
910 * @param max_depth is the number of times bits bits must be readed to completly
911 * read the longest vlc code
912 * = (max_vlc_length + bits - 1) / bits
914 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
915 int bits, int max_depth)
922 GET_VLC(code, re, s, table, bits, max_depth)
932 static inline void print_bin(int bits, int n){
935 for(i=n-1; i>=0; i--){
936 printf("%d", (bits>>i)&1);
942 static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
943 int r= get_bits(s, n);
946 printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
949 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
950 int show= show_bits(s, 24);
951 int pos= get_bits_count(s);
952 int r= get_vlc2(s, table, bits, max_depth);
953 int len= get_bits_count(s) - pos;
954 int bits2= show>>(24-len);
956 print_bin(bits2, len);
958 printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
961 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
962 int show= show_bits(s, n);
963 int r= get_xbits(s, n);
966 printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
970 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
971 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
972 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
973 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
974 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
976 #define tprintf printf
979 #define tprintf(_arg...) {}
982 /* define it to include statistics code (useful only for optimizing
999 extern int st_current_index;
1000 extern unsigned int st_bit_counts[ST_NB];
1001 extern unsigned int st_out_bit_counts[ST_NB];
1003 void print_stats(void);
1006 /* misc math functions */
1007 extern const uint8_t ff_log2_tab[256];
1009 static inline int av_log2(unsigned int v)
1014 if (v & 0xffff0000) {
1022 n += ff_log2_tab[v];
1027 static inline int av_log2_16bit(unsigned int v)
1036 n += ff_log2_tab[v];
1042 static inline int mid_pred(int a, int b, int c)
1045 int t= (a-b)&((a-b)>>31);
1048 b-= (b-c)&((b-c)>>31);
1049 b+= (a-b)&((a-b)>>31);
1068 static inline int clip(int a, int amin, int amax)
1079 extern const uint8_t ff_sqrt_tab[128];
1081 int64_t ff_gcd(int64_t a, int64_t b);
1083 static inline int ff_sqrt(int a)
1089 if(a<128) return ff_sqrt_tab[a];
1091 for(s=15; s>=0; s--){
1092 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
1102 * converts fourcc string to int
1104 static inline int ff_get_fourcc(const char *s){
1105 assert( strlen(s)==4 );
1107 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
1110 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
1111 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
1115 #define MASK_ABS(mask, level)\
1120 : "+a" (level), "=&d" (mask)\
1123 #define MASK_ABS(mask, level)\
1125 level= (level^mask)-mask;
1129 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
1130 #define COPY3_IF_LT(x,y,a,b,c,d)\
1133 "cmovl %3, %0 \n\t"\
1134 "cmovl %4, %1 \n\t"\
1135 "cmovl %5, %2 \n\t"\
1136 : "+r" (x), "+r" (a), "+r" (c)\
1137 : "r" (y), "r" (b), "r" (d)\
1140 #define COPY3_IF_LT(x,y,a,b,c,d)\
1149 static inline long long rdtsc()
1152 asm volatile( "rdtsc\n\t"
1158 #define START_TIMER \
1160 uint64_t tstart= rdtsc();\
1162 #define STOP_TIMER(id) \
1165 static uint64_t tsum=0;\
1166 static int tcount=0;\
1167 static int tskip_count=0;\
1168 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
1169 tsum+= tend - tstart;\
1173 if(256*256*256*64%(tcount+tskip_count)==0){\
1174 av_log(NULL, AV_LOG_DEBUG, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
1179 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
1181 /* avoid usage of various functions */
1182 #define malloc please_use_av_malloc
1183 #define free please_use_av_free
1184 #define realloc please_use_av_realloc
1185 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
1186 #define printf please_use_av_log
1187 #define fprintf please_use_av_log
1190 #define CHECKED_ALLOCZ(p, size)\
1192 p= av_mallocz(size);\
1193 if(p==NULL && (size)!=0){\
1199 #endif /* HAVE_AV_CONFIG_H */
1201 #endif /* COMMON_H */