3 * common internal api header.
10 #define M_PI 3.14159265358979323846
13 #if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
17 #ifdef HAVE_AV_CONFIG_H
18 /* only include the following when compiling package */
39 # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
42 #define AVOPTION_CODEC_BOOL(name, help, field) \
43 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
44 #define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
45 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
46 #define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
47 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
48 #define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
49 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
50 #define AVOPTION_CODEC_STRING(name, help, field, str, val) \
51 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
52 #define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
53 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
54 #define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
55 #define AVOPTION_END() AVOPTION_SUB(NULL)
57 #endif /* HAVE_AV_CONFIG_H */
59 /* Suppress restrict if it was not defined in config.h. */
65 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
66 # define always_inline __attribute__((always_inline)) inline
68 # define always_inline inline
72 #ifndef attribute_used
73 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
74 # define attribute_used __attribute__((used))
76 # define attribute_used
80 #ifndef attribute_unused
81 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
82 # define attribute_unused __attribute__((unused))
84 # define attribute_unused
88 #ifndef EMULATE_INTTYPES
89 # include <inttypes.h>
91 typedef signed char int8_t;
92 typedef signed short int16_t;
93 typedef signed int int32_t;
94 typedef unsigned char uint8_t;
95 typedef unsigned short uint16_t;
96 typedef unsigned int uint32_t;
99 typedef signed __int64 int64_t;
100 typedef unsigned __int64 uint64_t;
101 # else /* other OS */
102 typedef signed long long int64_t;
103 typedef unsigned long long uint64_t;
104 # endif /* other OS */
105 #endif /* EMULATE_INTTYPES */
124 #define PRIdFAST16 PRId32
128 #define PRIdFAST32 PRId32
132 #define INT16_MIN (-0x7fff-1)
136 #define INT16_MAX 0x7fff
140 #define INT32_MIN (-0x7fffffff-1)
144 #define INT32_MAX 0x7fffffff
148 #define UINT32_MAX 0xffffffff
152 #define INT64_MIN (-0x7fffffffffffffffLL-1)
156 #define INT64_MAX int64_t_C(9223372036854775807)
160 #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
163 #ifdef EMULATE_FAST_INT
164 typedef signed char int_fast8_t;
165 typedef signed int int_fast16_t;
166 typedef signed int int_fast32_t;
167 typedef unsigned char uint_fast8_t;
168 typedef unsigned int uint_fast16_t;
169 typedef unsigned int uint_fast32_t;
170 typedef uint64_t uint_fast64_t;
174 # if INT_MAX != 2147483647
185 # if !defined(__MINGW32__) && !defined(__CYGWIN__)
186 # define int64_t_C(c) (c ## i64)
187 # define uint64_t_C(c) (c ## i64)
189 # ifdef HAVE_AV_CONFIG_H
190 # define inline __inline
194 # define int64_t_C(c) (c ## LL)
195 # define uint64_t_C(c) (c ## ULL)
196 # endif /* __MINGW32__ */
198 # ifdef HAVE_AV_CONFIG_H
203 # define snprintf _snprintf
204 # define vsnprintf _vsnprintf
212 /* CONFIG_WIN32 end */
213 #elif defined (CONFIG_OS2)
217 #define int64_t_C(c) (c ## LL)
218 #define uint64_t_C(c) (c ## ULL)
221 #ifdef HAVE_AV_CONFIG_H
223 #ifdef USE_FASTMEMCPY
224 #include "fastmemcpy.h"
229 #endif /* HAVE_AV_CONFIG_H */
237 #define int64_t_C(c) (c ## LL)
238 #define uint64_t_C(c) (c ## ULL)
241 #ifdef HAVE_AV_CONFIG_H
243 # ifdef USE_FASTMEMCPY
244 # include "fastmemcpy.h"
246 # endif /* HAVE_AV_CONFIG_H */
248 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
250 #ifdef HAVE_AV_CONFIG_H
252 #if defined(__MINGW32__) && !defined(BUILD_AVUTIL) && defined(BUILD_SHARED_AV)
253 # define FF_IMPORT_ATTR __declspec(dllimport)
255 # define FF_IMPORT_ATTR
261 // Use rip-relative addressing if compiling PIC code on x86-64.
262 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
263 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
264 # if defined(ARCH_X86_64) && defined(PIC)
265 # define MANGLE(a) "_" #a"(%%rip)"
267 # define MANGLE(a) "_" #a
270 # if defined(ARCH_X86_64) && defined(PIC)
271 # define MANGLE(a) #a"(%%rip)"
272 # elif defined(CONFIG_DARWIN)
273 # define MANGLE(a) "_" #a
275 # define MANGLE(a) #a
281 # if !defined(DEBUG) && !defined(NDEBUG)
288 # define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
290 # define dprintf(fmt,...)
297 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
299 //rounded divison & shift
300 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
302 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
303 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
305 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
306 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
308 extern const uint32_t inverse[256];
310 #if defined(ARCH_X86) || defined(ARCH_X86_64)
311 # define FASTDIV(a,b) \
316 :"=d"(ret),"=a"(dmy)\
317 :"1"(a),"g"(inverse[b])\
321 #elif defined(CONFIG_FASTDIV)
322 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
324 # define FASTDIV(a,b) ((a)/(b))
327 /* misc math functions */
328 extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
330 static inline int av_log2(unsigned int v)
335 if (v & 0xffff0000) {
348 static inline int av_log2_16bit(unsigned int v)
363 static inline int mid_pred(int a, int b, int c)
366 int t= (a-b)&((a-b)>>31);
369 b-= (b-c)&((b-c)>>31);
370 b+= (a-b)&((a-b)>>31);
390 * clip a signed integer value into the amin-amax range
391 * @param a value to clip
392 * @param amin minimum value of the clip range
393 * @param amax maximum value of the clip range
394 * @return cliped value
396 static inline int clip(int a, int amin, int amax)
398 if (a < amin) return amin;
399 else if (a > amax) return amax;
404 * clip a signed integer value into the 0-255 range
405 * @param a value to clip
406 * @return cliped value
408 static inline uint8_t clip_uint8(int a)
410 if (a&(~255)) return (-a)>>31;
415 extern FF_IMPORT_ATTR const uint8_t ff_sqrt_tab[128];
417 int64_t ff_gcd(int64_t a, int64_t b);
419 static inline int ff_sqrt(int a)
425 if(a<128) return ff_sqrt_tab[a];
427 for(s=15; s>=0; s--){
428 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
438 * converts fourcc string to int
440 static inline int ff_get_fourcc(const char *s){
441 assert( strlen(s)==4 );
443 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
446 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
447 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
450 #if defined(ARCH_X86) || defined(ARCH_X86_64)
451 #define MASK_ABS(mask, level)\
456 : "+a" (level), "=&d" (mask)\
459 #define MASK_ABS(mask, level)\
461 level= (level^mask)-mask;
464 #define GET_UTF8(val, GET_BYTE, ERROR)\
467 int ones= 7 - av_log2(val ^ 255);\
472 int tmp= GET_BYTE - 128;\
475 val= (val<<6) + tmp;\
479 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
480 #define COPY3_IF_LT(x,y,a,b,c,d)\
486 : "+r" (x), "+r" (a), "+r" (c)\
487 : "r" (y), "r" (b), "r" (d)\
490 #define COPY3_IF_LT(x,y,a,b,c,d)\
498 #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
499 #if defined(ARCH_X86_64)
500 static inline uint64_t read_time(void)
503 asm volatile( "rdtsc\n\t"
506 return (d << 32) | (a & 0xffffffff);
508 #elif defined(ARCH_X86)
509 static inline long long read_time(void)
512 asm volatile( "rdtsc\n\t"
517 #else //FIXME check ppc64
518 static inline uint64_t read_time(void)
520 uint32_t tbu, tbl, temp;
522 /* from section 2.2.1 of the 32-bit PowerPC PEM */
523 __asm__ __volatile__(
530 : "=r"(tbl), "=r"(tbu), "=r"(temp)
534 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
538 #define START_TIMER \
540 uint64_t tstart= read_time();\
542 #define STOP_TIMER(id) \
545 static uint64_t tsum=0;\
546 static int tcount=0;\
547 static int tskip_count=0;\
548 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
549 tsum+= tend - tstart;\
553 if(256*256*256*64%(tcount+tskip_count)==0){\
554 av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
559 #define STOP_TIMER(id) {}
562 /* avoid usage of various functions */
563 #define malloc please_use_av_malloc
564 #define free please_use_av_free
565 #define realloc please_use_av_realloc
566 #define time time_is_forbidden_due_to_security_issues
567 #define rand rand_is_forbidden_due_to_state_trashing
568 #define srand srand_is_forbidden_due_to_state_trashing
569 #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
570 #define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
571 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
572 #define printf please_use_av_log
573 #define fprintf please_use_av_log
576 #define CHECKED_ALLOCZ(p, size)\
578 p= av_mallocz(size);\
579 if(p==NULL && (size)!=0){\
586 /* XXX: add ISOC specific test to avoid specific BSD testing. */
587 /* better than nothing implementation. */
588 /* btw, rintf() is existing on fbsd too -- alex */
589 static always_inline long int lrintf(float x)
596 : "=m" (i) : "t" (x) : "st"
600 /* XXX: incorrect, but make it compile */
601 return (int)(x + (x < 0 ? -0.5 : 0.5));
602 # endif /* ARCH_X86 */
604 return (int)(rint(x));
605 #endif /* CONFIG_WIN32 */
608 #ifndef _ISOC9X_SOURCE
609 #define _ISOC9X_SOURCE
612 #endif /* HAVE_LRINTF */
614 #endif /* HAVE_AV_CONFIG_H */
616 #endif /* COMMON_H */