00001
00006 #ifndef COMMON_H
00007 #define COMMON_H
00008
00009 #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
00010 # define CONFIG_WIN32
00011 #endif
00012
00013 #ifndef M_PI
00014 #define M_PI 3.14159265358979323846
00015 #endif
00016
00017 #ifdef HAVE_AV_CONFIG_H
00018
00019 # include "avconfig.h"
00020
00021 # include <stdlib.h>
00022 # include <stdio.h>
00023 # include <string.h>
00024 # include <ctype.h>
00025 # include <limits.h>
00026 # ifndef __BEOS__
00027 # include <errno.h>
00028 # else
00029 # include "berrno.h"
00030 # endif
00031 # include <math.h>
00032
00033 # ifndef ENODATA
00034 # define ENODATA 61
00035 # endif
00036
00037 #include <stddef.h>
00038 #ifndef offsetof
00039 # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
00040 #endif
00041
00042 #define AVOPTION_CODEC_BOOL(name, help, field) \
00043 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
00044 #define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
00045 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
00046 #define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
00047 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
00048 #define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
00049 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
00050 #define AVOPTION_CODEC_STRING(name, help, field, str, val) \
00051 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
00052 #define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
00053 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
00054 #define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
00055 #define AVOPTION_END() AVOPTION_SUB(NULL)
00056
00057 #endif
00058
00059
00060 #ifndef restrict
00061 # define restrict
00062 #endif
00063
00064 #ifndef always_inline
00065 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
00066 # define always_inline __attribute__((always_inline)) inline
00067 #else
00068 # define always_inline inline
00069 #endif
00070 #endif
00071
00072 #ifndef attribute_used
00073 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
00074 # define attribute_used __attribute__((used))
00075 #else
00076 # define attribute_used
00077 #endif
00078 #endif
00079
00080 #ifndef attribute_unused
00081 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
00082 # define attribute_unused __attribute__((unused))
00083 #else
00084 # define attribute_unused
00085 #endif
00086 #endif
00087
00088 #ifndef EMULATE_INTTYPES
00089 # include <inttypes.h>
00090 #else
00091 typedef signed char int8_t;
00092 typedef signed short int16_t;
00093 typedef signed int int32_t;
00094 typedef unsigned char uint8_t;
00095 typedef unsigned short uint16_t;
00096 typedef unsigned int uint32_t;
00097
00098 # ifdef CONFIG_WIN32
00099 typedef signed __int64 int64_t;
00100 typedef unsigned __int64 uint64_t;
00101 # else
00102 typedef signed long long int64_t;
00103 typedef unsigned long long uint64_t;
00104 # endif
00105 #endif
00106
00107 #ifndef INT16_MIN
00108 #define INT16_MIN (-0x7fff-1)
00109 #endif
00110
00111 #ifndef INT16_MAX
00112 #define INT16_MAX 0x7fff
00113 #endif
00114
00115 #ifndef INT64_MIN
00116 #define INT64_MIN (-0x7fffffffffffffffLL-1)
00117 #endif
00118
00119 #ifndef INT64_MAX
00120 #define INT64_MAX int64_t_C(9223372036854775807)
00121 #endif
00122
00123 #ifndef UINT64_MAX
00124 #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
00125 #endif
00126
00127 #ifdef EMULATE_FAST_INT
00128 typedef signed char int_fast8_t;
00129 typedef signed int int_fast16_t;
00130 typedef signed int int_fast32_t;
00131 typedef unsigned char uint_fast8_t;
00132 typedef unsigned int uint_fast16_t;
00133 typedef unsigned int uint_fast32_t;
00134 typedef uint64_t uint_fast64_t;
00135 #endif
00136
00137 #ifndef INT_BIT
00138 # if INT_MAX != 2147483647
00139 # define INT_BIT 64
00140 # else
00141 # define INT_BIT 32
00142 # endif
00143 #endif
00144
00145 #if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
00146 static inline float floorf(float f) {
00147 return floor(f);
00148 }
00149 #endif
00150
00151 #ifdef CONFIG_WIN32
00152
00153
00154
00155 # if !defined(__MINGW32__) && !defined(__CYGWIN__)
00156 # define int64_t_C(c) (c ## i64)
00157 # define uint64_t_C(c) (c ## i64)
00158
00159 # ifdef HAVE_AV_CONFIG_H
00160 # define inline __inline
00161 # endif
00162
00163 # else
00164 # define int64_t_C(c) (c ## LL)
00165 # define uint64_t_C(c) (c ## ULL)
00166 # endif
00167
00168 # ifdef HAVE_AV_CONFIG_H
00169 # ifdef _DEBUG
00170 # define DEBUG
00171 # endif
00172
00173 # define snprintf _snprintf
00174 # define vsnprintf _vsnprintf
00175 # endif
00176
00177
00178 #elif defined (CONFIG_OS2)
00179
00180
00181 #ifndef int64_t_C
00182 #define int64_t_C(c) (c ## LL)
00183 #define uint64_t_C(c) (c ## ULL)
00184 #endif
00185
00186 #ifdef HAVE_AV_CONFIG_H
00187
00188 #ifdef USE_FASTMEMCPY
00189 #include "fastmemcpy.h"
00190 #endif
00191
00192 #include <float.h>
00193
00194 #endif
00195
00196
00197 #else
00198
00199
00200
00201 #ifndef int64_t_C
00202 #define int64_t_C(c) (c ## LL)
00203 #define uint64_t_C(c) (c ## ULL)
00204 #endif
00205
00206 #ifdef HAVE_AV_CONFIG_H
00207
00208 # ifdef USE_FASTMEMCPY
00209 # include "fastmemcpy.h"
00210 # endif
00211 # endif
00212
00213 #endif
00214
00215 #ifdef HAVE_AV_CONFIG_H
00216
00217 # include "bswap.h"
00218
00219
00220 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
00221 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
00222 # if defined(ARCH_X86_64) && defined(PIC)
00223 # define MANGLE(a) "_" #a"(%%rip)"
00224 # else
00225 # define MANGLE(a) "_" #a
00226 # endif
00227 # else
00228 # if defined(ARCH_X86_64) && defined(PIC)
00229 # define MANGLE(a) #a"(%%rip)"
00230 # else
00231 # define MANGLE(a) #a
00232 # endif
00233 # endif
00234
00235
00236
00237 # ifndef DEBUG
00238 # define NDEBUG
00239 # endif
00240 # include <assert.h>
00241
00242
00243 # if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
00244
00245 inline void dprintf(const char* fmt,...) {}
00246
00247 # else
00248
00249 # ifdef DEBUG
00250 # define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
00251 # else
00252 # define dprintf(fmt,...)
00253 # endif
00254
00255 # endif
00256
00257 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
00258
00259
00260 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
00261
00262 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
00263 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
00264
00265 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
00266 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
00267
00268 extern const uint32_t inverse[256];
00269
00270 #if defined(ARCH_X86) || defined(ARCH_X86_64)
00271 # define FASTDIV(a,b) \
00272 ({\
00273 int ret,dmy;\
00274 asm volatile(\
00275 "mull %3"\
00276 :"=d"(ret),"=a"(dmy)\
00277 :"1"(a),"g"(inverse[b])\
00278 );\
00279 ret;\
00280 })
00281 #elif defined(CONFIG_FASTDIV)
00282 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
00283 #else
00284 # define FASTDIV(a,b) ((a)/(b))
00285 #endif
00286
00287
00288
00289
00290
00291 #ifdef STATS
00292
00293 enum {
00294 ST_UNKNOWN,
00295 ST_DC,
00296 ST_INTRA_AC,
00297 ST_INTER_AC,
00298 ST_INTRA_MB,
00299 ST_INTER_MB,
00300 ST_MV,
00301 ST_NB,
00302 };
00303
00304 extern int st_current_index;
00305 extern unsigned int st_bit_counts[ST_NB];
00306 extern unsigned int st_out_bit_counts[ST_NB];
00307
00308 void print_stats(void);
00309 #endif
00310
00311
00312 extern const uint8_t ff_log2_tab[256];
00313
00314 static inline int av_log2(unsigned int v)
00315 {
00316 int n;
00317
00318 n = 0;
00319 if (v & 0xffff0000) {
00320 v >>= 16;
00321 n += 16;
00322 }
00323 if (v & 0xff00) {
00324 v >>= 8;
00325 n += 8;
00326 }
00327 n += ff_log2_tab[v];
00328
00329 return n;
00330 }
00331
00332 static inline int av_log2_16bit(unsigned int v)
00333 {
00334 int n;
00335
00336 n = 0;
00337 if (v & 0xff00) {
00338 v >>= 8;
00339 n += 8;
00340 }
00341 n += ff_log2_tab[v];
00342
00343 return n;
00344 }
00345
00346
00347 static inline int mid_pred(int a, int b, int c)
00348 {
00349 #if 0
00350 int t= (a-b)&((a-b)>>31);
00351 a-=t;
00352 b+=t;
00353 b-= (b-c)&((b-c)>>31);
00354 b+= (a-b)&((a-b)>>31);
00355
00356 return b;
00357 #else
00358 if(a>b){
00359 if(c>b){
00360 if(c>a) b=a;
00361 else b=c;
00362 }
00363 }else{
00364 if(b>c){
00365 if(c>a) b=c;
00366 else b=a;
00367 }
00368 }
00369 return b;
00370 #endif
00371 }
00372
00373 static inline int clip(int a, int amin, int amax)
00374 {
00375 if (a < amin)
00376 return amin;
00377 else if (a > amax)
00378 return amax;
00379 else
00380 return a;
00381 }
00382
00383 static inline int clip_uint8(int a)
00384 {
00385 if (a&(~255)) return (-a)>>31;
00386 else return a;
00387 }
00388
00389
00390 extern const uint8_t ff_sqrt_tab[128];
00391
00392 int64_t ff_gcd(int64_t a, int64_t b);
00393
00394 static inline int ff_sqrt(int a)
00395 {
00396 int ret=0;
00397 int s;
00398 int ret_sq=0;
00399
00400 if(a<128) return ff_sqrt_tab[a];
00401
00402 for(s=15; s>=0; s--){
00403 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
00404 if(b<=a){
00405 ret_sq=b;
00406 ret+= 1<<s;
00407 }
00408 }
00409 return ret;
00410 }
00411
00415 static inline int ff_get_fourcc(const char *s){
00416 assert( strlen(s)==4 );
00417
00418 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
00419 }
00420
00421 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
00422 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
00423
00424
00425 #if defined(ARCH_X86) || defined(ARCH_X86_64)
00426 #define MASK_ABS(mask, level)\
00427 asm volatile(\
00428 "cdq \n\t"\
00429 "xorl %1, %0 \n\t"\
00430 "subl %1, %0 \n\t"\
00431 : "+a" (level), "=&d" (mask)\
00432 );
00433 #else
00434 #define MASK_ABS(mask, level)\
00435 mask= level>>31;\
00436 level= (level^mask)-mask;
00437 #endif
00438
00439
00440 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
00441 #define COPY3_IF_LT(x,y,a,b,c,d)\
00442 asm volatile (\
00443 "cmpl %0, %3 \n\t"\
00444 "cmovl %3, %0 \n\t"\
00445 "cmovl %4, %1 \n\t"\
00446 "cmovl %5, %2 \n\t"\
00447 : "+r" (x), "+r" (a), "+r" (c)\
00448 : "r" (y), "r" (b), "r" (d)\
00449 );
00450 #else
00451 #define COPY3_IF_LT(x,y,a,b,c,d)\
00452 if((y)<(x)){\
00453 (x)=(y);\
00454 (a)=(b);\
00455 (c)=(d);\
00456 }
00457 #endif
00458
00459 #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
00460 #if defined(ARCH_X86_64)
00461 static inline uint64_t read_time(void)
00462 {
00463 uint64_t a, d;
00464 asm volatile( "rdtsc\n\t"
00465 : "=a" (a), "=d" (d)
00466 );
00467 return (d << 32) | (a & 0xffffffff);
00468 }
00469 #elif defined(ARCH_X86)
00470 static inline long long read_time(void)
00471 {
00472 long long l;
00473 asm volatile( "rdtsc\n\t"
00474 : "=A" (l)
00475 );
00476 return l;
00477 }
00478 #else //FIXME check ppc64
00479 static inline uint64_t read_time(void)
00480 {
00481 uint32_t tbu, tbl, temp;
00482
00483
00484 __asm__ __volatile__(
00485 "1:\n"
00486 "mftbu %2\n"
00487 "mftb %0\n"
00488 "mftbu %1\n"
00489 "cmpw %2,%1\n"
00490 "bne 1b\n"
00491 : "=r"(tbl), "=r"(tbu), "=r"(temp)
00492 :
00493 : "cc");
00494
00495 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
00496 }
00497 #endif
00498
00499 #define START_TIMER \
00500 uint64_t tend;\
00501 uint64_t tstart= read_time();\
00502
00503 #define STOP_TIMER(id) \
00504 tend= read_time();\
00505 {\
00506 static uint64_t tsum=0;\
00507 static int tcount=0;\
00508 static int tskip_count=0;\
00509 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
00510 tsum+= tend - tstart;\
00511 tcount++;\
00512 }else\
00513 tskip_count++;\
00514 if(256*256*256*64%(tcount+tskip_count)==0){\
00515 av_log(NULL, AV_LOG_DEBUG, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
00516 }\
00517 }
00518 #else
00519 #define START_TIMER
00520 #define STOP_TIMER(id) {}
00521 #endif
00522
00523
00524 #define malloc please_use_av_malloc
00525 #define free please_use_av_free
00526 #define realloc please_use_av_realloc
00527 #define time time_is_forbidden_due_to_security_issues
00528 #define rand rand_is_forbidden_due_to_state_trashing
00529 #define srand srand_is_forbidden_due_to_state_trashing
00530 #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
00531 #define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
00532 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
00533 #define printf please_use_av_log
00534 #define fprintf please_use_av_log
00535 #endif
00536
00537 #define CHECKED_ALLOCZ(p, size)\
00538 {\
00539 p= av_mallocz(size);\
00540 if(p==NULL && (size)!=0){\
00541 perror("malloc");\
00542 goto fail;\
00543 }\
00544 }
00545
00546 #endif
00547
00548 #endif