00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #ifndef FFMPEG_COMMON_H
00027 #define FFMPEG_COMMON_H
00028
00029 #include <inttypes.h>
00030
00031 #ifdef HAVE_AV_CONFIG_H
00032
00033 # include "config.h"
00034
00035 # include <stdlib.h>
00036 # include <stdio.h>
00037 # include <string.h>
00038 # include <ctype.h>
00039 # include <limits.h>
00040 # include <errno.h>
00041 # include <math.h>
00042 #endif
00043
00044 #ifndef av_always_inline
00045 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
00046 # define av_always_inline __attribute__((always_inline)) inline
00047 #else
00048 # define av_always_inline inline
00049 #endif
00050 #endif
00051
00052 #ifndef av_noinline
00053 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
00054 # define av_noinline __attribute__((noinline))
00055 #else
00056 # define av_noinline
00057 #endif
00058 #endif
00059
00060 #ifdef HAVE_AV_CONFIG_H
00061 # include "internal.h"
00062 #endif
00063
00064 #ifndef attribute_deprecated
00065 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
00066 # define attribute_deprecated __attribute__((deprecated))
00067 #else
00068 # define attribute_deprecated
00069 #endif
00070 #endif
00071
00072 #ifndef av_unused
00073 #if defined(__GNUC__)
00074 # define av_unused __attribute__((unused))
00075 #else
00076 # define av_unused
00077 #endif
00078 #endif
00079
00080 #include "mem.h"
00081
00082
00083 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
00084
00085 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
00086 #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
00087 #define FFSIGN(a) ((a) > 0 ? 1 : -1)
00088
00089 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
00090 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
00091
00092 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
00093
00094
00095 extern const uint8_t ff_log2_tab[256];
00096
00097 static inline int av_log2(unsigned int v)
00098 {
00099 int n = 0;
00100 if (v & 0xffff0000) {
00101 v >>= 16;
00102 n += 16;
00103 }
00104 if (v & 0xff00) {
00105 v >>= 8;
00106 n += 8;
00107 }
00108 n += ff_log2_tab[v];
00109
00110 return n;
00111 }
00112
00113 static inline int av_log2_16bit(unsigned int v)
00114 {
00115 int n = 0;
00116 if (v & 0xff00) {
00117 v >>= 8;
00118 n += 8;
00119 }
00120 n += ff_log2_tab[v];
00121
00122 return n;
00123 }
00124
00125
00126 static inline int mid_pred(int a, int b, int c)
00127 {
00128 #ifdef HAVE_CMOV
00129 int i=b;
00130 asm volatile(
00131 "cmp %2, %1 \n\t"
00132 "cmovg %1, %0 \n\t"
00133 "cmovg %2, %1 \n\t"
00134 "cmp %3, %1 \n\t"
00135 "cmovl %3, %1 \n\t"
00136 "cmp %1, %0 \n\t"
00137 "cmovg %1, %0 \n\t"
00138 :"+&r"(i), "+&r"(a)
00139 :"r"(b), "r"(c)
00140 );
00141 return i;
00142 #elif 0
00143 int t= (a-b)&((a-b)>>31);
00144 a-=t;
00145 b+=t;
00146 b-= (b-c)&((b-c)>>31);
00147 b+= (a-b)&((a-b)>>31);
00148
00149 return b;
00150 #else
00151 if(a>b){
00152 if(c>b){
00153 if(c>a) b=a;
00154 else b=c;
00155 }
00156 }else{
00157 if(b>c){
00158 if(c>a) b=c;
00159 else b=a;
00160 }
00161 }
00162 return b;
00163 #endif
00164 }
00165
00173 static inline int av_clip(int a, int amin, int amax)
00174 {
00175 if (a < amin) return amin;
00176 else if (a > amax) return amax;
00177 else return a;
00178 }
00179
00185 static inline uint8_t av_clip_uint8(int a)
00186 {
00187 if (a&(~255)) return (-a)>>31;
00188 else return a;
00189 }
00190
00196 static inline int16_t av_clip_int16(int a)
00197 {
00198 if ((a+32768) & ~65535) return (a>>31) ^ 32767;
00199 else return a;
00200 }
00201
00202
00203 int64_t ff_gcd(int64_t a, int64_t b);
00204
00208 static inline int ff_get_fourcc(const char *s){
00209 #ifdef HAVE_AV_CONFIG_H
00210 assert( strlen(s)==4 );
00211 #endif
00212
00213 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
00214 }
00215
00216 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
00217 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
00218
00232 #define GET_UTF8(val, GET_BYTE, ERROR)\
00233 val= GET_BYTE;\
00234 {\
00235 int ones= 7 - av_log2(val ^ 255);\
00236 if(ones==1)\
00237 ERROR\
00238 val&= 127>>ones;\
00239 while(--ones > 0){\
00240 int tmp= GET_BYTE - 128;\
00241 if(tmp>>6)\
00242 ERROR\
00243 val= (val<<6) + tmp;\
00244 }\
00245 }
00246
00263 #define PUT_UTF8(val, tmp, PUT_BYTE)\
00264 {\
00265 int bytes, shift;\
00266 uint32_t in = val;\
00267 if (in < 0x80) {\
00268 tmp = in;\
00269 PUT_BYTE\
00270 } else {\
00271 bytes = (av_log2(in) + 4) / 5;\
00272 shift = (bytes - 1) * 6;\
00273 tmp = (256 - (256 >> bytes)) | (in >> shift);\
00274 PUT_BYTE\
00275 while (shift >= 6) {\
00276 shift -= 6;\
00277 tmp = 0x80 | ((in >> shift) & 0x3f);\
00278 PUT_BYTE\
00279 }\
00280 }\
00281 }
00282
00283 #if defined(ARCH_X86) || defined(ARCH_POWERPC) || defined(ARCH_BFIN)
00284 #define AV_READ_TIME read_time
00285 #if defined(ARCH_X86_64)
00286 static inline uint64_t read_time(void)
00287 {
00288 uint64_t a, d;
00289 asm volatile( "rdtsc\n\t"
00290 : "=a" (a), "=d" (d)
00291 );
00292 return (d << 32) | (a & 0xffffffff);
00293 }
00294 #elif defined(ARCH_X86_32)
00295 static inline long long read_time(void)
00296 {
00297 long long l;
00298 asm volatile( "rdtsc\n\t"
00299 : "=A" (l)
00300 );
00301 return l;
00302 }
00303 #elif ARCH_BFIN
00304 static inline uint64_t read_time(void)
00305 {
00306 union {
00307 struct {
00308 unsigned lo;
00309 unsigned hi;
00310 } p;
00311 unsigned long long c;
00312 } t;
00313 asm volatile ("%0=cycles; %1=cycles2;" : "=d" (t.p.lo), "=d" (t.p.hi));
00314 return t.c;
00315 }
00316 #else //FIXME check ppc64
00317 static inline uint64_t read_time(void)
00318 {
00319 uint32_t tbu, tbl, temp;
00320
00321
00322 __asm__ __volatile__(
00323 "1:\n"
00324 "mftbu %2\n"
00325 "mftb %0\n"
00326 "mftbu %1\n"
00327 "cmpw %2,%1\n"
00328 "bne 1b\n"
00329 : "=r"(tbl), "=r"(tbu), "=r"(temp)
00330 :
00331 : "cc");
00332
00333 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
00334 }
00335 #endif
00336 #elif defined(HAVE_GETHRTIME)
00337 #define AV_READ_TIME gethrtime
00338 #endif
00339
00340 #ifdef AV_READ_TIME
00341 #define START_TIMER \
00342 uint64_t tend;\
00343 uint64_t tstart= AV_READ_TIME();\
00344
00345 #define STOP_TIMER(id) \
00346 tend= AV_READ_TIME();\
00347 {\
00348 static uint64_t tsum=0;\
00349 static int tcount=0;\
00350 static int tskip_count=0;\
00351 if(tcount<2 || tend - tstart < FFMAX(8*tsum/tcount, 2000)){\
00352 tsum+= tend - tstart;\
00353 tcount++;\
00354 }else\
00355 tskip_count++;\
00356 if(((tcount+tskip_count)&(tcount+tskip_count-1))==0){\
00357 av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
00358 }\
00359 }
00360 #else
00361 #define START_TIMER
00362 #define STOP_TIMER(id) {}
00363 #endif
00364
00365 #endif