Skip to content
Snippets Groups Projects
common.h 31.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
                                      int bits, int max_depth)
    
    {
        int code;
        
        OPEN_READER(re, s)
        UPDATE_CACHE(re, s)
    
        GET_VLC(code, re, s, table, bits, max_depth)
    
    
    //#define TRACE
    
    #ifdef TRACE
    
    static inline void print_bin(int bits, int n){
        int i;
        
        for(i=n-1; i>=0; i--){
            printf("%d", (bits>>i)&1);
        }
        for(i=n; i<24; i++)
            printf(" ");
    }
    
    static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
        int r= get_bits(s, n);
        
        print_bin(r, n);
        printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
        return r;
    }
    static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
        int show= show_bits(s, 24);
        int pos= get_bits_count(s);
        int r= get_vlc2(s, table, bits, max_depth);
        int len= get_bits_count(s) - pos;
        int bits2= show>>(24-len);
        
        print_bin(bits2, len);
        
        printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
        return r;
    }
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
        int show= show_bits(s, n);
        int r= get_xbits(s, n);
        
        print_bin(show, n);
        printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
        return r;
    }
    
    
    #define get_bits(s, n)  get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
    #define get_bits1(s)    get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    #define get_vlc(s, vlc)            get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
    
    #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
    
    
    #define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    #define tprintf(...) {}
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    /* define it to include statistics code (useful only for optimizing
       codec efficiency */
    //#define STATS
    
    #ifdef STATS
    
    enum {
        ST_UNKNOWN,
        ST_DC,
        ST_INTRA_AC,
        ST_INTER_AC,
        ST_INTRA_MB,
        ST_INTER_MB,
        ST_MV,
        ST_NB,
    };
    
    extern int st_current_index;
    extern unsigned int st_bit_counts[ST_NB];
    extern unsigned int st_out_bit_counts[ST_NB];
    
    void print_stats(void);
    #endif
    
    /* misc math functions */
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    extern const uint8_t ff_log2_tab[256];
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
    static inline int av_log2(unsigned int v)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        int n;
    
        n = 0;
        if (v & 0xffff0000) {
            v >>= 16;
            n += 16;
        }
        if (v & 0xff00) {
            v >>= 8;
            n += 8;
        }
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
        n += ff_log2_tab[v];
    
        return n;
    }
    
    static inline int av_log2_16bit(unsigned int v)
    {
        int n;
    
        n = 0;
        if (v & 0xff00) {
            v >>= 8;
            n += 8;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
        n += ff_log2_tab[v];
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        return n;
    }
    
    
    /* median of 3 */
    static inline int mid_pred(int a, int b, int c)
    {
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    #if 0
        int t= (a-b)&((a-b)>>31);
        a-=t;
        b+=t;
        b-= (b-c)&((b-c)>>31);
        b+= (a-b)&((a-b)>>31);
    
        return b;
    #else
        if(a>b){
            if(c>b){
                if(c>a) b=a;
                else    b=c;
            }
        }else{
            if(b>c){
                if(c>a) b=c;
                else    b=a;
            }
        }
        return b;
    #endif
    
    static inline int clip(int a, int amin, int amax)
    {
        if (a < amin)
            return amin;
        else if (a > amax)
            return amax;
        else
            return a;
    }
    
    
    static inline int clip_uint8(int a)
    {
        if (a&(~255)) return (-a)>>31;
        else          return a;
    }
    
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    extern const uint8_t ff_sqrt_tab[128];
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    static inline int ff_sqrt(int a)
    {
        int ret=0;
        int s;
        int ret_sq=0;
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
        
        if(a<128) return ff_sqrt_tab[a];
        
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
        for(s=15; s>=0; s--){
            int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
            if(b<=a){
                ret_sq=b;
                ret+= 1<<s;
            }
        }
        return ret;
    }
    
    
    /**
     * converts fourcc string to int
     */
    
    Zdenek Kabelac's avatar
    Zdenek Kabelac committed
    static inline int ff_get_fourcc(const char *s){
    
        assert( strlen(s)==4 );
    
        return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
    }
    
    
    #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
    #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
    
    
    
    #ifdef ARCH_X86
    #define MASK_ABS(mask, level)\
                asm volatile(\
    		"cdq			\n\t"\
    		"xorl %1, %0		\n\t"\
    		"subl %1, %0		\n\t"\
    		: "+a" (level), "=&d" (mask)\
    	    );
    #else
    #define MASK_ABS(mask, level)\
                mask= level>>31;\
                level= (level^mask)-mask;
    #endif
    
    
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
    #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
    #define COPY3_IF_LT(x,y,a,b,c,d)\
    asm volatile (\
        "cmpl %0, %3	\n\t"\
        "cmovl %3, %0	\n\t"\
        "cmovl %4, %1	\n\t"\
        "cmovl %5, %2	\n\t"\
        : "+r" (x), "+r" (a), "+r" (c)\
        : "r" (y), "r" (b), "r" (d)\
    );
    #else
    #define COPY3_IF_LT(x,y,a,b,c,d)\
    if((y)<(x)){\
         (x)=(y);\
         (a)=(b);\
         (c)=(d);\
    }
    #endif
    
    
    #ifdef ARCH_X86
    static inline long long rdtsc()
    {
    	long long l;
    	asm volatile(	"rdtsc\n\t"
    		: "=A" (l)
    	);
    	return l;
    }
    
    #define START_TIMER \
    uint64_t tend;\
    uint64_t tstart= rdtsc();\
    
    #define STOP_TIMER(id) \
    tend= rdtsc();\
    
    {\
      static uint64_t tsum=0;\
      static int tcount=0;\
      static int tskip_count=0;\
      if(tcount<2 || tend - tstart < 8*tsum/tcount){\
          tsum+= tend - tstart;\
          tcount++;\
      }else\
          tskip_count++;\
      if(256*256*256*64%(tcount+tskip_count)==0){\
          av_log(NULL, AV_LOG_DEBUG, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
      }\
    
    #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
    
    
    /* avoid usage of various functions */
    #define malloc please_use_av_malloc
    #define free please_use_av_free
    #define realloc please_use_av_realloc
    
    #define time time_is_forbidden_due_to_security_issues
    #define rand rand_is_forbidden_due_to_state_trashing
    #define srand srand_is_forbidden_due_to_state_trashing
    
    #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
    #define printf please_use_av_log
    #define fprintf please_use_av_log
    #endif
    
    #define CHECKED_ALLOCZ(p, size)\
    {\
        p= av_mallocz(size);\
    
        if(p==NULL && (size)!=0){\
    
    #endif /* HAVE_AV_CONFIG_H */
    
    #endif /* COMMON_H */