00001
00002
00003
00004
00005 #ifndef COMMON_H
00006 #define COMMON_H
00007
00008 #include <inttypes.h>
00009
00010 #ifdef HAVE_AV_CONFIG_H
00011
00012 # include "config.h"
00013
00014 # include <stdlib.h>
00015 # include <stdio.h>
00016 # include <string.h>
00017 # include <ctype.h>
00018 # include <limits.h>
00019 # ifndef __BEOS__
00020 # include <errno.h>
00021 # else
00022 # include "berrno.h"
00023 # endif
00024 # include <math.h>
00025 #endif
00026
00027 #ifndef av_always_inline
00028 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
00029 # define av_always_inline __attribute__((always_inline)) inline
00030 #else
00031 # define av_always_inline inline
00032 #endif
00033 #endif
00034
00035 #ifdef HAVE_AV_CONFIG_H
00036 # include "internal.h"
00037 #endif
00038
00039 #ifndef attribute_deprecated
00040 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
00041 # define attribute_deprecated __attribute__((deprecated))
00042 #else
00043 # define attribute_deprecated
00044 #endif
00045 #endif
00046
00047 #ifndef INT64_C
00048 #define INT64_C(c) (c ## LL)
00049 #define UINT64_C(c) (c ## ULL)
00050 #endif
00051
00052 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
00053
00054 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
00055 #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
00056 #define FFSIGN(a) ((a) > 0 ? 1 : -1)
00057
00058 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
00059 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
00060
00061 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
00062
00063 extern const uint8_t ff_log2_tab[256];
00064
00065 static inline int av_log2(unsigned int v)
00066 {
00067 int n;
00068
00069 n = 0;
00070 if (v & 0xffff0000) {
00071 v >>= 16;
00072 n += 16;
00073 }
00074 if (v & 0xff00) {
00075 v >>= 8;
00076 n += 8;
00077 }
00078 n += ff_log2_tab[v];
00079
00080 return n;
00081 }
00082
00083 static inline int av_log2_16bit(unsigned int v)
00084 {
00085 int n;
00086
00087 n = 0;
00088 if (v & 0xff00) {
00089 v >>= 8;
00090 n += 8;
00091 }
00092 n += ff_log2_tab[v];
00093
00094 return n;
00095 }
00096
00097 static inline int mid_pred(int a, int b, int c)
00098 {
00099 #ifdef HAVE_CMOV
00100 int i=b;
00101 asm volatile(
00102 "cmp %2, %1 \n\t"
00103 "cmovg %1, %0 \n\t"
00104 "cmovg %2, %1 \n\t"
00105 "cmp %3, %1 \n\t"
00106 "cmovl %3, %1 \n\t"
00107 "cmp %1, %0 \n\t"
00108 "cmovg %1, %0 \n\t"
00109 :"+&r"(i), "+&r"(a)
00110 :"r"(b), "r"(c)
00111 );
00112 return i;
00113 #elif 0
00114 int t= (a-b)&((a-b)>>31);
00115 a-=t;
00116 b+=t;
00117 b-= (b-c)&((b-c)>>31);
00118 b+= (a-b)&((a-b)>>31);
00119
00120 return b;
00121 #else
00122 if(a>b){
00123 if(c>b){
00124 if(c>a) b=a;
00125 else b=c;
00126 }
00127 }else{
00128 if(b>c){
00129 if(c>a) b=c;
00130 else b=a;
00131 }
00132 }
00133 return b;
00134 #endif
00135 }
00136
00137 static inline int clip(int a, int amin, int amax)
00138 {
00139 if (a < amin) return amin;
00140 else if (a > amax) return amax;
00141 else return a;
00142 }
00143
00144 static inline uint8_t clip_uint8(int a)
00145 {
00146 if (a&(~255)) return (-a)>>31;
00147 else return a;
00148 }
00149
00150 int64_t ff_gcd(int64_t a, int64_t b);
00151
00152 static inline int ff_get_fourcc(const char *s){
00153 #ifdef HAVE_AV_CONFIG_H
00154 assert( strlen(s)==4 );
00155 #endif
00156
00157 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
00158 }
00159
00160 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
00161 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
00162
00163 #define GET_UTF8(val, GET_BYTE, ERROR)\
00164 val= GET_BYTE;\
00165 {\
00166 int ones= 7 - av_log2(val ^ 255);\
00167 if(ones==1)\
00168 ERROR\
00169 val&= 127>>ones;\
00170 while(--ones > 0){\
00171 int tmp= GET_BYTE - 128;\
00172 if(tmp>>6)\
00173 ERROR\
00174 val= (val<<6) + tmp;\
00175 }\
00176 }
00177
00178 #define PUT_UTF8(val, tmp, PUT_BYTE)\
00179 {\
00180 int bytes, shift;\
00181 uint32_t in = val;\
00182 if (in < 0x80) {\
00183 tmp = in;\
00184 PUT_BYTE\
00185 } else {\
00186 bytes = (av_log2(in) + 4) / 5;\
00187 shift = (bytes - 1) * 6;\
00188 tmp = (256 - (256 >> bytes)) | (in >> shift);\
00189 PUT_BYTE\
00190 while (shift >= 6) {\
00191 shift -= 6;\
00192 tmp = 0x80 | ((in >> shift) & 0x3f);\
00193 PUT_BYTE\
00194 }\
00195 }\
00196 }
00197
00198 #if defined(ARCH_X86) || defined(ARCH_POWERPC)
00199 #if defined(ARCH_X86_64)
00200 static inline uint64_t read_time(void)
00201 {
00202 uint64_t a, d;
00203 asm volatile( "rdtsc\n\t"
00204 : "=a" (a), "=d" (d)
00205 );
00206 return (d << 32) | (a & 0xffffffff);
00207 }
00208 #elif defined(ARCH_X86_32)
00209 static inline long long read_time(void)
00210 {
00211 long long l;
00212 asm volatile( "rdtsc\n\t"
00213 : "=A" (l)
00214 );
00215 return l;
00216 }
00217 #else
00218 static inline uint64_t read_time(void)
00219 {
00220 uint32_t tbu, tbl, temp;
00221
00222
00223 __asm__ __volatile__(
00224 "1:\n"
00225 "mftbu %2\n"
00226 "mftb %0\n"
00227 "mftbu %1\n"
00228 "cmpw %2,%1\n"
00229 "bne 1b\n"
00230 : "=r"(tbl), "=r"(tbu), "=r"(temp)
00231 :
00232 : "cc");
00233
00234 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
00235 }
00236 #endif
00237
00238 #define START_TIMER \
00239 uint64_t tend;\
00240 uint64_t tstart= read_time();\
00241
00242 #define STOP_TIMER(id) \
00243 tend= read_time();\
00244 {\
00245 static uint64_t tsum=0;\
00246 static int tcount=0;\
00247 static int tskip_count=0;\
00248 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
00249 tsum+= tend - tstart;\
00250 tcount++;\
00251 }else\
00252 tskip_count++;\
00253 if(((tcount+tskip_count)&(tcount+tskip_count-1))==0){\
00254 av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
00255 }\
00256 }
00257 #else
00258 #define START_TIMER
00259 #define STOP_TIMER(id) {}
00260 #endif
00261
00262 #ifdef __GNUC__
00263 #define DECLARE_ALIGNED(n,t,v) t v __attribute__ ((aligned (n)))
00264 #else
00265 #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
00266 #endif
00267
00268 void *av_malloc(unsigned int size);
00269 void *av_realloc(void *ptr, unsigned int size);
00270 void av_free(void *ptr);
00271
00272 void *av_mallocz(unsigned int size);
00273 char *av_strdup(const char *s);
00274 void av_freep(void *ptr);
00275
00276 #endif