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
21 #ifdef HAVE_AV_CONFIG_H
22 /* only include the following when compiling package */
40 #define M_PI 3.14159265358979323846
45 # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
48 #define AVOPTION_CODEC_BOOL(name, help, field) \
49 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
50 #define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
51 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
52 #define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
53 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
54 #define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
55 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
56 #define AVOPTION_CODEC_STRING(name, help, field, str, val) \
57 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
58 #define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
59 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
60 #define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
61 #define AVOPTION_END() AVOPTION_SUB(NULL)
65 extern const struct AVOption avoptions_common[3 + 5];
67 extern const struct AVOption avoptions_common[3];
69 extern const struct AVOption avoptions_workaround_bug[11];
71 #endif /* HAVE_AV_CONFIG_H */
73 /* Suppress restrict if it was not defined in config.h. */
78 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
79 # define always_inline __attribute__((always_inline)) inline
81 # define always_inline inline
88 typedef unsigned short uint16_t;
89 typedef signed short int16_t;
90 typedef unsigned char uint8_t;
91 typedef unsigned int uint32_t;
92 typedef unsigned __int64 uint64_t;
93 typedef signed char int8_t;
94 typedef signed int int32_t;
95 typedef signed __int64 int64_t;
98 # define int64_t_C(c) (c ## i64)
99 # define uint64_t_C(c) (c ## i64)
101 # define inline __inline
104 # define int64_t_C(c) (c ## LL)
105 # define uint64_t_C(c) (c ## ULL)
106 # endif /* __MINGW32__ */
112 # define snprintf _snprintf
113 # define vsnprintf _vsnprintf
115 /* CONFIG_WIN32 end */
116 #elif defined (CONFIG_OS2)
119 #include <inttypes.h>
121 #ifdef HAVE_AV_CONFIG_H
124 #define int64_t_C(c) (c ## LL)
125 #define uint64_t_C(c) (c ## ULL)
128 #ifdef USE_FASTMEMCPY
129 #include "fastmemcpy.h"
134 #endif /* HAVE_AV_CONFIG_H */
141 # include <inttypes.h>
143 # ifdef HAVE_AV_CONFIG_H
145 # define int64_t_C(c) (c ## LL)
146 # define uint64_t_C(c) (c ## ULL)
149 # ifdef USE_FASTMEMCPY
150 # include "fastmemcpy.h"
152 # endif /* HAVE_AV_CONFIG_H */
154 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
156 #ifdef HAVE_AV_CONFIG_H
160 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
161 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
162 # define MANGLE(a) "_" #a
164 # define MANGLE(a) #a
175 # if defined(CONFIG_WIN32) && !defined(__MINGW32__)
177 inline void dprintf(const char* fmt,...) {}
182 # define dprintf(fmt,args...) printf(fmt, ## args)
184 # define dprintf(fmt,args...)
187 # endif /* !CONFIG_WIN32 */
189 # define av_abort() do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
191 //rounded divison & shift
192 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
194 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
195 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
197 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
198 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
200 extern const uint32_t inverse[256];
203 # define FASTDIV(a,b) \
208 :"=d"(ret),"=a"(dmy)\
209 :"1"(a),"g"(inverse[b])\
213 #elif defined(CONFIG_FASTDIV)
214 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
216 # define FASTDIV(a,b) ((a)/(b))
220 // avoid +32 for shift optimization (gcc should do that ...)
221 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
222 asm ("sarl %1, %0\n\t"
224 : "ic" ((uint8_t)(-s))
228 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
229 asm ("shrl %1, %0\n\t"
231 : "ic" ((uint8_t)(-s))
236 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
237 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
242 struct PutBitContext;
244 typedef void (*WriteDataFunc)(void *, uint8_t *, int);
246 typedef struct PutBitContext {
247 #ifdef ALT_BITSTREAM_WRITER
248 uint8_t *buf, *buf_end;
253 uint8_t *buf, *buf_ptr, *buf_end;
255 int64_t data_out_size; /* in bytes */
258 void init_put_bits(PutBitContext *s,
259 uint8_t *buffer, int buffer_size,
261 void (*write_data)(void *, uint8_t *, int));
263 int64_t get_bit_count(PutBitContext *s); /* XXX: change function name */
264 void align_put_bits(PutBitContext *s);
265 void flush_put_bits(PutBitContext *s);
266 void put_string(PutBitContext * pbc, char *s);
270 typedef struct GetBitContext {
271 const uint8_t *buffer, *buffer_end;
272 #ifdef ALT_BITSTREAM_READER
274 #elif defined LIBMPEG2_BITSTREAM_READER
278 #elif defined A32_BITSTREAM_READER
279 uint32_t *buffer_ptr;
287 static inline int get_bits_count(GetBitContext *s);
289 #define VLC_TYPE int16_t
293 VLC_TYPE (*table)[2]; ///< code, bits
294 int table_size, table_allocated;
297 typedef struct RL_VLC_ELEM {
304 #define UNALIGNED_STORES_ARE_BAD
307 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
309 # define unaligned32(a) (*(uint32_t*)(a))
312 static inline uint32_t unaligned32(const void *v) {
315 } __attribute__((packed));
317 return ((const struct Unaligned *) v)->i;
319 # elif defined(__DECC)
320 static inline uint32_t unaligned32(const void *v) {
321 return *(const __unaligned uint32_t *) v;
324 static inline uint32_t unaligned32(const void *v) {
325 return *(const uint32_t *) v;
330 #ifndef ALT_BITSTREAM_WRITER
331 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
333 unsigned int bit_buf;
337 st_out_bit_counts[st_current_index] += n;
339 // printf("put_bits=%d %x\n", n, value);
340 assert(n == 32 || value < (1U << n));
342 bit_buf = s->bit_buf;
343 bit_left = s->bit_left;
345 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
348 bit_buf = (bit_buf<<n) | value;
352 bit_buf |= value >> (n - bit_left);
353 #ifdef UNALIGNED_STORES_ARE_BAD
354 if (3 & (int) s->buf_ptr) {
355 s->buf_ptr[0] = bit_buf >> 24;
356 s->buf_ptr[1] = bit_buf >> 16;
357 s->buf_ptr[2] = bit_buf >> 8;
358 s->buf_ptr[3] = bit_buf ;
361 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
362 //printf("bitbuf = %08x\n", bit_buf);
368 s->bit_buf = bit_buf;
369 s->bit_left = bit_left;
374 #ifdef ALT_BITSTREAM_WRITER
375 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
377 # ifdef ALIGNED_BITSTREAM_WRITER
380 "movl %0, %%ecx \n\t"
381 "xorl %%eax, %%eax \n\t"
382 "shrdl %%cl, %1, %%eax \n\t"
384 "movl %0, %%ecx \n\t"
385 "shrl $3, %%ecx \n\t"
386 "andl $0xFFFFFFFC, %%ecx \n\t"
388 "orl %1, (%2, %%ecx) \n\t"
391 "movl %%eax, 4(%2, %%ecx) \n\t"
392 : "=&r" (s->index), "=&r" (value)
393 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
398 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
402 ptr[0] |= be2me_32(value>>(index&31));
403 ptr[1] = be2me_32(value<<(32-(index&31)));
404 //if(n>24) printf("%d %d\n", n, value);
408 # else //ALIGNED_BITSTREAM_WRITER
411 "movl $7, %%ecx \n\t"
412 "andl %0, %%ecx \n\t"
413 "addl %3, %%ecx \n\t"
417 "movl %0, %%ecx \n\t"
418 "shrl $3, %%ecx \n\t"
419 "orl %1, (%%ecx, %2) \n\t"
421 "movl $0, 4(%%ecx, %2) \n\t"
422 : "=&r" (s->index), "=&r" (value)
423 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
428 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
430 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
432 //if(n>24) printf("%d %d\n", n, value);
436 # endif //!ALIGNED_BITSTREAM_WRITER
441 static inline uint8_t* pbBufPtr(PutBitContext *s)
443 #ifdef ALT_BITSTREAM_WRITER
444 return s->buf + (s->index>>3);
450 /* Bitstream reader API docs:
452 abritary name which is used as prefix for the internal variables
457 OPEN_READER(name, gb)
458 loads gb into local variables
460 CLOSE_READER(name, gb)
461 stores local vars in gb
463 UPDATE_CACHE(name, gb)
464 refills the internal cache from the bitstream
465 after this call at least MIN_CACHE_BITS will be available,
468 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
470 SHOW_UBITS(name, gb, num)
471 will return the nest num bits
473 SHOW_SBITS(name, gb, num)
474 will return the nest num bits and do sign extension
476 SKIP_BITS(name, gb, num)
477 will skip over the next num bits
478 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
480 SKIP_CACHE(name, gb, num)
481 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
483 SKIP_COUNTER(name, gb, num)
484 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
486 LAST_SKIP_CACHE(name, gb, num)
487 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
489 LAST_SKIP_BITS(name, gb, num)
490 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
492 for examples see get_bits, show_bits, skip_bits, get_vlc
495 static inline int unaligned32_be(const void *v)
499 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
501 return be2me_32( unaligned32(v)); //original
505 #ifdef ALT_BITSTREAM_READER
506 # define MIN_CACHE_BITS 25
508 # define OPEN_READER(name, gb)\
509 int name##_index= (gb)->index;\
510 int name##_cache= 0;\
512 # define CLOSE_READER(name, gb)\
513 (gb)->index= name##_index;\
515 # define UPDATE_CACHE(name, gb)\
516 name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
518 # define SKIP_CACHE(name, gb, num)\
519 name##_cache <<= (num);\
522 # define SKIP_COUNTER(name, gb, num)\
523 name##_index += (num);\
525 # define SKIP_BITS(name, gb, num)\
527 SKIP_CACHE(name, gb, num)\
528 SKIP_COUNTER(name, gb, num)\
531 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
532 # define LAST_SKIP_CACHE(name, gb, num) ;
534 # define SHOW_UBITS(name, gb, num)\
535 NEG_USR32(name##_cache, num)
537 # define SHOW_SBITS(name, gb, num)\
538 NEG_SSR32(name##_cache, num)
540 # define GET_CACHE(name, gb)\
541 ((uint32_t)name##_cache)
543 static inline int get_bits_count(GetBitContext *s){
546 #elif defined LIBMPEG2_BITSTREAM_READER
547 //libmpeg2 like reader
549 # define MIN_CACHE_BITS 17
551 # define OPEN_READER(name, gb)\
552 int name##_bit_count=(gb)->bit_count;\
553 int name##_cache= (gb)->cache;\
554 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
556 # define CLOSE_READER(name, gb)\
557 (gb)->bit_count= name##_bit_count;\
558 (gb)->cache= name##_cache;\
559 (gb)->buffer_ptr= name##_buffer_ptr;\
561 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
563 # define UPDATE_CACHE(name, gb)\
564 if(name##_bit_count >= 0){\
565 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
566 ((uint16_t*)name##_buffer_ptr)++;\
567 name##_bit_count-= 16;\
572 # define UPDATE_CACHE(name, gb)\
573 if(name##_bit_count >= 0){\
574 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
575 name##_buffer_ptr+=2;\
576 name##_bit_count-= 16;\
581 # define SKIP_CACHE(name, gb, num)\
582 name##_cache <<= (num);\
584 # define SKIP_COUNTER(name, gb, num)\
585 name##_bit_count += (num);\
587 # define SKIP_BITS(name, gb, num)\
589 SKIP_CACHE(name, gb, num)\
590 SKIP_COUNTER(name, gb, num)\
593 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
594 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
596 # define SHOW_UBITS(name, gb, num)\
597 NEG_USR32(name##_cache, num)
599 # define SHOW_SBITS(name, gb, num)\
600 NEG_SSR32(name##_cache, num)
602 # define GET_CACHE(name, gb)\
603 ((uint32_t)name##_cache)
605 static inline int get_bits_count(GetBitContext *s){
606 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
609 #elif defined A32_BITSTREAM_READER
611 # define MIN_CACHE_BITS 32
613 # define OPEN_READER(name, gb)\
614 int name##_bit_count=(gb)->bit_count;\
615 uint32_t name##_cache0= (gb)->cache0;\
616 uint32_t name##_cache1= (gb)->cache1;\
617 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
619 # define CLOSE_READER(name, gb)\
620 (gb)->bit_count= name##_bit_count;\
621 (gb)->cache0= name##_cache0;\
622 (gb)->cache1= name##_cache1;\
623 (gb)->buffer_ptr= name##_buffer_ptr;\
625 # define UPDATE_CACHE(name, gb)\
626 if(name##_bit_count > 0){\
627 const uint32_t next= be2me_32( *name##_buffer_ptr );\
628 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
629 name##_cache1 |= next<<name##_bit_count;\
630 name##_buffer_ptr++;\
631 name##_bit_count-= 32;\
635 # define SKIP_CACHE(name, gb, num)\
637 "shldl %2, %1, %0 \n\t"\
639 : "+r" (name##_cache0), "+r" (name##_cache1)\
640 : "Ic" ((uint8_t)num)\
643 # define SKIP_CACHE(name, gb, num)\
644 name##_cache0 <<= (num);\
645 name##_cache0 |= NEG_USR32(name##_cache1,num);\
646 name##_cache1 <<= (num);
649 # define SKIP_COUNTER(name, gb, num)\
650 name##_bit_count += (num);\
652 # define SKIP_BITS(name, gb, num)\
654 SKIP_CACHE(name, gb, num)\
655 SKIP_COUNTER(name, gb, num)\
658 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
659 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
661 # define SHOW_UBITS(name, gb, num)\
662 NEG_USR32(name##_cache0, num)
664 # define SHOW_SBITS(name, gb, num)\
665 NEG_SSR32(name##_cache0, num)
667 # define GET_CACHE(name, gb)\
670 static inline int get_bits_count(GetBitContext *s){
671 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
677 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
678 * if MSB not set it is negative
679 * @param n length in bits
682 static inline int get_xbits(GetBitContext *s, int n){
684 register int32_t cache;
687 cache = GET_CACHE(re,s);
688 if ((int32_t)cache<0) { //MSB=1
689 tmp = NEG_USR32(cache,n);
691 // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
692 // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
693 tmp = - NEG_USR32(~cache,n);
695 LAST_SKIP_BITS(re, s, n)
700 static inline int get_sbits(GetBitContext *s, int n){
704 tmp= SHOW_SBITS(re, s, n);
705 LAST_SKIP_BITS(re, s, n)
712 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
714 static inline unsigned int get_bits(GetBitContext *s, int n){
718 tmp= SHOW_UBITS(re, s, n);
719 LAST_SKIP_BITS(re, s, n)
724 unsigned int get_bits_long(GetBitContext *s, int n);
728 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
730 static inline unsigned int show_bits(GetBitContext *s, int n){
734 tmp= SHOW_UBITS(re, s, n);
735 // CLOSE_READER(re, s)
739 unsigned int show_bits_long(GetBitContext *s, int n);
741 static inline void skip_bits(GetBitContext *s, int n){
742 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
745 LAST_SKIP_BITS(re, s, n)
749 static inline unsigned int get_bits1(GetBitContext *s){
750 #ifdef ALT_BITSTREAM_READER
752 uint8_t result= s->buffer[ index>>3 ];
753 result<<= (index&0x07);
760 return get_bits(s, 1);
764 static inline unsigned int show_bits1(GetBitContext *s){
765 return show_bits(s, 1);
768 static inline void skip_bits1(GetBitContext *s){
772 void init_get_bits(GetBitContext *s,
773 const uint8_t *buffer, int buffer_size);
775 int check_marker(GetBitContext *s, const char *msg);
776 void align_get_bits(GetBitContext *s);
777 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
778 const void *bits, int bits_wrap, int bits_size,
779 const void *codes, int codes_wrap, int codes_size);
780 void free_vlc(VLC *vlc);
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
788 #define GET_VLC(code, name, gb, table, bits, max_depth)\
790 int n, index, nb_bits;\
792 index= SHOW_UBITS(name, gb, bits);\
793 code = table[index][0];\
794 n = table[index][1];\
796 if(max_depth > 1 && n < 0){\
797 LAST_SKIP_BITS(name, gb, bits)\
798 UPDATE_CACHE(name, gb)\
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)\
811 index= SHOW_UBITS(name, gb, nb_bits) + code;\
812 code = table[index][0];\
813 n = table[index][1];\
816 SKIP_BITS(name, gb, n)\
819 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
821 int n, index, nb_bits;\
823 index= SHOW_UBITS(name, gb, bits);\
824 level = table[index].level;\
825 n = table[index].len;\
827 if(max_depth > 1 && n < 0){\
828 LAST_SKIP_BITS(name, gb, bits)\
829 UPDATE_CACHE(name, gb)\
833 index= SHOW_UBITS(name, gb, nb_bits) + level;\
834 level = table[index].level;\
835 n = table[index].len;\
837 run= table[index].run;\
838 SKIP_BITS(name, gb, n)\
841 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
842 static inline int get_vlc(GetBitContext *s, VLC *vlc)
845 VLC_TYPE (*table)[2]= vlc->table;
850 GET_VLC(code, re, s, table, vlc->bits, 3)
857 * parses a vlc code, faster then get_vlc()
858 * @param bits is the number of bits which will be read at once, must be
859 * identical to nb_bits in init_vlc()
860 * @param max_depth is the number of times bits bits must be readed to completly
861 * read the longest vlc code
862 * = (max_vlc_length + bits - 1) / bits
864 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
865 int bits, int max_depth)
872 GET_VLC(code, re, s, table, bits, max_depth)
882 static inline void print_bin(int bits, int n){
885 for(i=n-1; i>=0; i--){
886 printf("%d", (bits>>i)&1);
892 static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
893 int r= get_bits(s, n);
896 printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
899 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
900 int show= show_bits(s, 24);
901 int pos= get_bits_count(s);
902 int r= get_vlc2(s, table, bits, max_depth);
903 int len= get_bits_count(s) - pos;
904 int bits2= show>>(24-len);
906 print_bin(bits2, len);
908 printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
911 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
912 int show= show_bits(s, n);
913 int r= get_xbits(s, n);
916 printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
920 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
921 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
922 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
923 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
924 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
926 #define tprintf printf
929 #define tprintf(_arg...) {}
932 /* define it to include statistics code (useful only for optimizing
949 extern int st_current_index;
950 extern unsigned int st_bit_counts[ST_NB];
951 extern unsigned int st_out_bit_counts[ST_NB];
953 void print_stats(void);
956 /* misc math functions */
957 extern const uint8_t ff_log2_tab[256];
959 static inline int av_log2(unsigned int v)
964 if (v & 0xffff0000) {
977 static inline int av_log2_16bit(unsigned int v)
993 static inline int mid_pred(int a, int b, int c)
1007 return a + b + c - vmin - vmax;
1010 static inline int clip(int a, int amin, int amax)
1021 extern const uint8_t ff_sqrt_tab[128];
1023 int64_t ff_gcd(int64_t a, int64_t b);
1025 static inline int ff_sqrt(int a)
1031 if(a<128) return ff_sqrt_tab[a];
1033 for(s=15; s>=0; s--){
1034 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
1044 * converts fourcc string to int
1046 static inline int ff_get_fourcc(const char *s){
1047 assert( strlen(s)==4 );
1049 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
1052 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
1053 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
1056 void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max);
1060 #define MASK_ABS(mask, level)\
1065 : "+a" (level), "=&d" (mask)\
1068 #define MASK_ABS(mask, level)\
1070 level= (level^mask)-mask;
1074 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
1075 #define COPY3_IF_LT(x,y,a,b,c,d)\
1078 "cmovl %3, %0 \n\t"\
1079 "cmovl %4, %1 \n\t"\
1080 "cmovl %5, %2 \n\t"\
1081 : "+r" (x), "+r" (a), "+r" (c)\
1082 : "r" (y), "r" (b), "r" (d)\
1085 #define COPY3_IF_LT(x,y,a,b,c,d)\
1094 static inline long long rdtsc()
1097 asm volatile( "rdtsc\n\t"
1103 #define START_TIMER \
1104 static uint64_t tsum=0;\
1105 static int tcount=0;\
1106 static int tskip_count=0;\
1108 uint64_t tstart= rdtsc();\
1110 #define STOP_TIMER(id) \
1112 if(tcount<2 || tend - tstart < 4*tsum/tcount){\
1113 tsum+= tend - tstart;\
1117 if(256*256*256*64%(tcount+tskip_count)==0){\
1118 fprintf(stderr, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
1122 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
1124 /* avoid usage of various functions */
1125 #define malloc please_use_av_malloc
1126 #define free please_use_av_free
1127 #define realloc please_use_av_realloc
1129 #define CHECKED_ALLOCZ(p, size)\
1131 p= av_mallocz(size);\
1138 #endif /* HAVE_AV_CONFIG_H */
1140 #endif /* COMMON_H */