3 * common internal api header.
9 #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
13 #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(EMULATE_INTTYPES)
14 # define EMULATE_INTTYPES
18 #define M_PI 3.14159265358979323846
21 #if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
25 #ifdef HAVE_AV_CONFIG_H
26 /* only include the following when compiling package */
47 # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
50 #define AVOPTION_CODEC_BOOL(name, help, field) \
51 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
52 #define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
53 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
54 #define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
55 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
56 #define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
57 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
58 #define AVOPTION_CODEC_STRING(name, help, field, str, val) \
59 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
60 #define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
61 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
62 #define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
63 #define AVOPTION_END() AVOPTION_SUB(NULL)
65 #endif /* HAVE_AV_CONFIG_H */
67 /* Suppress restrict if it was not defined in config.h. */
73 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
74 # define always_inline __attribute__((always_inline)) inline
76 # define always_inline inline
80 #ifndef attribute_used
81 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
82 # define attribute_used __attribute__((used))
84 # define attribute_used
88 #ifndef attribute_unused
89 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
90 # define attribute_unused __attribute__((unused))
92 # define attribute_unused
96 #ifndef EMULATE_INTTYPES
97 # include <inttypes.h>
99 typedef signed char int8_t;
100 typedef signed short int16_t;
101 typedef signed int int32_t;
102 typedef unsigned char uint8_t;
103 typedef unsigned short uint16_t;
104 typedef unsigned int uint32_t;
107 typedef signed __int64 int64_t;
108 typedef unsigned __int64 uint64_t;
109 # else /* other OS */
110 typedef signed long long int64_t;
111 typedef unsigned long long uint64_t;
112 # endif /* other OS */
113 #endif /* EMULATE_INTTYPES */
132 #define PRIdFAST16 PRId32
136 #define PRIdFAST32 PRId32
140 #define INT16_MIN (-0x7fff-1)
144 #define INT16_MAX 0x7fff
148 #define INT32_MIN (-0x7fffffff-1)
152 #define INT32_MAX 0x7fffffff
156 #define UINT32_MAX 0xffffffff
160 #define INT64_MIN (-0x7fffffffffffffffLL-1)
164 #define INT64_MAX int64_t_C(9223372036854775807)
168 #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
171 #ifdef EMULATE_FAST_INT
172 typedef signed char int_fast8_t;
173 typedef signed int int_fast16_t;
174 typedef signed int int_fast32_t;
175 typedef unsigned char uint_fast8_t;
176 typedef unsigned int uint_fast16_t;
177 typedef unsigned int uint_fast32_t;
178 typedef uint64_t uint_fast64_t;
182 # if INT_MAX != 2147483647
193 # if !defined(__MINGW32__) && !defined(__CYGWIN__)
194 # define int64_t_C(c) (c ## i64)
195 # define uint64_t_C(c) (c ## i64)
197 # ifdef HAVE_AV_CONFIG_H
198 # define inline __inline
202 # define int64_t_C(c) (c ## LL)
203 # define uint64_t_C(c) (c ## ULL)
204 # endif /* __MINGW32__ */
206 # ifdef HAVE_AV_CONFIG_H
211 # define snprintf _snprintf
212 # define vsnprintf _vsnprintf
220 /* CONFIG_WIN32 end */
221 #elif defined (CONFIG_OS2)
225 #define int64_t_C(c) (c ## LL)
226 #define uint64_t_C(c) (c ## ULL)
229 #ifdef HAVE_AV_CONFIG_H
231 #ifdef USE_FASTMEMCPY
232 #include "fastmemcpy.h"
237 #endif /* HAVE_AV_CONFIG_H */
245 #define int64_t_C(c) (c ## LL)
246 #define uint64_t_C(c) (c ## ULL)
249 #ifdef HAVE_AV_CONFIG_H
251 # ifdef USE_FASTMEMCPY
252 # include "fastmemcpy.h"
254 # endif /* HAVE_AV_CONFIG_H */
256 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
258 #ifdef HAVE_AV_CONFIG_H
260 #if defined(__MINGW32__) && !defined(BUILD_AVUTIL) && defined(BUILD_SHARED_AV)
261 # define FF_IMPORT_ATTR __declspec(dllimport)
263 # define FF_IMPORT_ATTR
269 // Use rip-relative addressing if compiling PIC code on x86-64.
270 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
271 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
272 # if defined(ARCH_X86_64) && defined(PIC)
273 # define MANGLE(a) "_" #a"(%%rip)"
275 # define MANGLE(a) "_" #a
278 # if defined(ARCH_X86_64) && defined(PIC)
279 # define MANGLE(a) #a"(%%rip)"
280 # elif defined(CONFIG_DARWIN)
281 # define MANGLE(a) "_" #a
283 # define MANGLE(a) #a
289 # if !defined(DEBUG) && !defined(NDEBUG)
295 # if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
297 inline void dprintf(const char* fmt,...) {}
302 # define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
304 # define dprintf(fmt,...)
307 # endif /* !CONFIG_WIN32 */
312 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
314 //rounded divison & shift
315 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
317 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
318 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
320 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
321 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
323 extern const uint32_t inverse[256];
325 #if defined(ARCH_X86) || defined(ARCH_X86_64)
326 # define FASTDIV(a,b) \
331 :"=d"(ret),"=a"(dmy)\
332 :"1"(a),"g"(inverse[b])\
336 #elif defined(CONFIG_FASTDIV)
337 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
339 # define FASTDIV(a,b) ((a)/(b))
342 /* misc math functions */
343 extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
345 static inline int av_log2(unsigned int v)
350 if (v & 0xffff0000) {
363 static inline int av_log2_16bit(unsigned int v)
378 static inline int mid_pred(int a, int b, int c)
381 int t= (a-b)&((a-b)>>31);
384 b-= (b-c)&((b-c)>>31);
385 b+= (a-b)&((a-b)>>31);
405 * clip a signed integer value into the amin-amax range
406 * @param a value to clip
407 * @param amin minimum value of the clip range
408 * @param amax maximum value of the clip range
409 * @return cliped value
411 static inline int clip(int a, int amin, int amax)
422 * clip a signed integer value into the 0-255 range
423 * @param a value to clip
424 * @return cliped value
426 static inline uint8_t clip_uint8(int a)
428 if (a&(~255)) return (-a)>>31;
433 extern FF_IMPORT_ATTR const uint8_t ff_sqrt_tab[128];
435 int64_t ff_gcd(int64_t a, int64_t b);
437 static inline int ff_sqrt(int a)
443 if(a<128) return ff_sqrt_tab[a];
445 for(s=15; s>=0; s--){
446 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
456 * converts fourcc string to int
458 static inline int ff_get_fourcc(const char *s){
459 assert( strlen(s)==4 );
461 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
464 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
465 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
468 #if defined(ARCH_X86) || defined(ARCH_X86_64)
469 #define MASK_ABS(mask, level)\
474 : "+a" (level), "=&d" (mask)\
477 #define MASK_ABS(mask, level)\
479 level= (level^mask)-mask;
482 #define GET_UTF8(val, GET_BYTE, ERROR)\
485 int ones= 7 - av_log2(val ^ 255);\
490 int tmp= GET_BYTE - 128;\
493 val= (val<<6) + tmp;\
497 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
498 #define COPY3_IF_LT(x,y,a,b,c,d)\
504 : "+r" (x), "+r" (a), "+r" (c)\
505 : "r" (y), "r" (b), "r" (d)\
508 #define COPY3_IF_LT(x,y,a,b,c,d)\
516 #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
517 #if defined(ARCH_X86_64)
518 static inline uint64_t read_time(void)
521 asm volatile( "rdtsc\n\t"
524 return (d << 32) | (a & 0xffffffff);
526 #elif defined(ARCH_X86)
527 static inline long long read_time(void)
530 asm volatile( "rdtsc\n\t"
535 #else //FIXME check ppc64
536 static inline uint64_t read_time(void)
538 uint32_t tbu, tbl, temp;
540 /* from section 2.2.1 of the 32-bit PowerPC PEM */
541 __asm__ __volatile__(
548 : "=r"(tbl), "=r"(tbu), "=r"(temp)
552 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
556 #define START_TIMER \
558 uint64_t tstart= read_time();\
560 #define STOP_TIMER(id) \
563 static uint64_t tsum=0;\
564 static int tcount=0;\
565 static int tskip_count=0;\
566 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
567 tsum+= tend - tstart;\
571 if(256*256*256*64%(tcount+tskip_count)==0){\
572 av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
577 #define STOP_TIMER(id) {}
580 /* avoid usage of various functions */
581 #define malloc please_use_av_malloc
582 #define free please_use_av_free
583 #define realloc please_use_av_realloc
584 #define time time_is_forbidden_due_to_security_issues
585 #define rand rand_is_forbidden_due_to_state_trashing
586 #define srand srand_is_forbidden_due_to_state_trashing
587 #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
588 #define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
589 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
590 #define printf please_use_av_log
591 #define fprintf please_use_av_log
594 #define CHECKED_ALLOCZ(p, size)\
596 p= av_mallocz(size);\
597 if(p==NULL && (size)!=0){\
604 /* XXX: add ISOC specific test to avoid specific BSD testing. */
605 /* better than nothing implementation. */
606 /* btw, rintf() is existing on fbsd too -- alex */
607 static always_inline long int lrintf(float x)
614 : "=m" (i) : "t" (x) : "st"
618 /* XXX: incorrect, but make it compile */
619 return (int)(x + (x < 0 ? -0.5 : 0.5));
620 # endif /* ARCH_X86 */
622 return (int)(rint(x));
623 #endif /* CONFIG_WIN32 */
626 #ifndef _ISOC9X_SOURCE
627 #define _ISOC9X_SOURCE
630 #endif /* HAVE_LRINTF */
632 #endif /* HAVE_AV_CONFIG_H */
634 #endif /* COMMON_H */