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
00066
00067
00068
00069
00070 # define attribute_deprecated
00071
00072 #endif
00073
00074 #ifndef av_unused
00075 #if defined(__GNUC__)
00076 # define av_unused __attribute__((unused))
00077 #else
00078 # define av_unused
00079 #endif
00080 #endif
00081
00082 #include "mem.h"
00083
00084
00085 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
00086
00087 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
00088 #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
00089 #define FFSIGN(a) ((a) > 0 ? 1 : -1)
00090
00091 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
00092 #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
00093 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
00094 #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
00095
00096 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
00097
00098
00099 extern const uint8_t ff_log2_tab[256];
00100
00101 static inline int av_log2(unsigned int v)
00102 {
00103 int n;
00104
00105 n = 0;
00106 if (v & 0xffff0000) {
00107 v >>= 16;
00108 n += 16;
00109 }
00110 if (v & 0xff00) {
00111 v >>= 8;
00112 n += 8;
00113 }
00114 n += ff_log2_tab[v];
00115
00116 return n;
00117 }
00118
00119 static inline int av_log2_16bit(unsigned int v)
00120 {
00121 int n;
00122
00123 n = 0;
00124 if (v & 0xff00) {
00125 v >>= 8;
00126 n += 8;
00127 }
00128 n += ff_log2_tab[v];
00129
00130 return n;
00131 }
00132
00133
00134 static inline int mid_pred(int a, int b, int c)
00135 {
00136 #ifdef HAVE_CMOV
00137 int i=b;
00138 asm volatile(
00139 "cmp %2, %1 \n\t"
00140 "cmovg %1, %0 \n\t"
00141 "cmovg %2, %1 \n\t"
00142 "cmp %3, %1 \n\t"
00143 "cmovl %3, %1 \n\t"
00144 "cmp %1, %0 \n\t"
00145 "cmovg %1, %0 \n\t"
00146 :"+&r"(i), "+&r"(a)
00147 :"r"(b), "r"(c)
00148 );
00149 return i;
00150 #elif 0
00151 int t= (a-b)&((a-b)>>31);
00152 a-=t;
00153 b+=t;
00154 b-= (b-c)&((b-c)>>31);
00155 b+= (a-b)&((a-b)>>31);
00156
00157 return b;
00158 #else
00159 if(a>b){
00160 if(c>b){
00161 if(c>a) b=a;
00162 else b=c;
00163 }
00164 }else{
00165 if(b>c){
00166 if(c>a) b=c;
00167 else b=a;
00168 }
00169 }
00170 return b;
00171 #endif
00172 }
00173
00181 static inline int av_clip(int a, int amin, int amax)
00182 {
00183 if (a < amin) return amin;
00184 else if (a > amax) return amax;
00185 else return a;
00186 }
00187
00193 static inline uint8_t av_clip_uint8(int a)
00194 {
00195 if (a&(~255)) return (-a)>>31;
00196 else return a;
00197 }
00198
00204 static inline int16_t av_clip_int16(int a)
00205 {
00206 if ((a+32768) & ~65535) return (a>>31) ^ 32767;
00207 else return a;
00208 }
00209
00210
00211 int64_t ff_gcd(int64_t a, int64_t b);
00212
00216 static inline int ff_get_fourcc(const char *s){
00217 #ifdef HAVE_AV_CONFIG_H
00218 assert( strlen(s)==4 );
00219 #endif
00220
00221 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
00222 }
00223
00224 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
00225 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
00226
00240 #define GET_UTF8(val, GET_BYTE, ERROR)\
00241 val= GET_BYTE;\
00242 {\
00243 int ones= 7 - av_log2(val ^ 255);\
00244 if(ones==1)\
00245 ERROR\
00246 val&= 127>>ones;\
00247 while(--ones > 0){\
00248 int tmp= GET_BYTE - 128;\
00249 if(tmp>>6)\
00250 ERROR\
00251 val= (val<<6) + tmp;\
00252 }\
00253 }
00254
00271 #define PUT_UTF8(val, tmp, PUT_BYTE)\
00272 {\
00273 int bytes, shift;\
00274 uint32_t in = val;\
00275 if (in < 0x80) {\
00276 tmp = in;\
00277 PUT_BYTE\
00278 } else {\
00279 bytes = (av_log2(in) + 4) / 5;\
00280 shift = (bytes - 1) * 6;\
00281 tmp = (256 - (256 >> bytes)) | (in >> shift);\
00282 PUT_BYTE\
00283 while (shift >= 6) {\
00284 shift -= 6;\
00285 tmp = 0x80 | ((in >> shift) & 0x3f);\
00286 PUT_BYTE\
00287 }\
00288 }\
00289 }
00290
00291 #if defined(ARCH_X86) || defined(ARCH_POWERPC) || defined(ARCH_BFIN)
00292 #define AV_READ_TIME read_time
00293 #if defined(ARCH_X86_64)
00294 static inline uint64_t read_time(void)
00295 {
00296 uint64_t a, d;
00297 asm volatile( "rdtsc\n\t"
00298 : "=a" (a), "=d" (d)
00299 );
00300 return (d << 32) | (a & 0xffffffff);
00301 }
00302 #elif defined(ARCH_X86_32)
00303 static inline long long read_time(void)
00304 {
00305 long long l;
00306 asm volatile( "rdtsc\n\t"
00307 : "=A" (l)
00308 );
00309 return l;
00310 }
00311 #elif ARCH_BFIN
00312 static inline uint64_t read_time(void)
00313 {
00314 union {
00315 struct {
00316 unsigned lo;
00317 unsigned hi;
00318 } p;
00319 unsigned long long c;
00320 } t;
00321 asm volatile ("%0=cycles; %1=cycles2;" : "=d" (t.p.lo), "=d" (t.p.hi));
00322 return t.c;
00323 }
00324 #else //FIXME check ppc64
00325 static inline uint64_t read_time(void)
00326 {
00327 uint32_t tbu, tbl, temp;
00328
00329
00330 __asm__ __volatile__(
00331 "1:\n"
00332 "mftbu %2\n"
00333 "mftb %0\n"
00334 "mftbu %1\n"
00335 "cmpw %2,%1\n"
00336 "bne 1b\n"
00337 : "=r"(tbl), "=r"(tbu), "=r"(temp)
00338 :
00339 : "cc");
00340
00341 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
00342 }
00343 #endif
00344 #elif defined(HAVE_GETHRTIME)
00345 #define AV_READ_TIME gethrtime
00346 #endif
00347
00348 #ifdef AV_READ_TIME
00349 #define START_TIMER \
00350 uint64_t tend;\
00351 uint64_t tstart= AV_READ_TIME();\
00352
00353 #define STOP_TIMER(id) \
00354 tend= AV_READ_TIME();\
00355 {\
00356 static uint64_t tsum=0;\
00357 static int tcount=0;\
00358 static int tskip_count=0;\
00359 if(tcount<2 || tend - tstart < FFMAX(8*tsum/tcount, 2000)){\
00360 tsum+= tend - tstart;\
00361 tcount++;\
00362 }else\
00363 tskip_count++;\
00364 if(((tcount+tskip_count)&(tcount+tskip_count-1))==0){\
00365 av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
00366 }\
00367 }
00368 #else
00369 #define START_TIMER
00370 #define STOP_TIMER(id) {}
00371 #endif
00372
00373 #endif