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 #ifdef HAVE_AV_CONFIG_H
22 /* only include the following when compiling package */
43 # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
46 #define AVOPTION_CODEC_BOOL(name, help, field) \
47 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
48 #define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
49 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
50 #define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
51 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
52 #define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
53 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
54 #define AVOPTION_CODEC_STRING(name, help, field, str, val) \
55 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
56 #define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
57 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
58 #define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
59 #define AVOPTION_END() AVOPTION_SUB(NULL)
61 #endif /* HAVE_AV_CONFIG_H */
63 /* Suppress restrict if it was not defined in config.h. */
69 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
70 # define always_inline __attribute__((always_inline)) inline
72 # define always_inline inline
76 #ifndef attribute_used
77 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
78 # define attribute_used __attribute__((used))
80 # define attribute_used
84 #ifndef attribute_unused
85 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
86 # define attribute_unused __attribute__((unused))
88 # define attribute_unused
92 #ifndef EMULATE_INTTYPES
93 # include <inttypes.h>
95 typedef signed char int8_t;
96 typedef signed short int16_t;
97 typedef signed int int32_t;
98 typedef unsigned char uint8_t;
99 typedef unsigned short uint16_t;
100 typedef unsigned int uint32_t;
103 typedef signed __int64 int64_t;
104 typedef unsigned __int64 uint64_t;
105 # else /* other OS */
106 typedef signed long long int64_t;
107 typedef unsigned long long uint64_t;
108 # endif /* other OS */
109 #endif /* EMULATE_INTTYPES */
128 #define PRIdFAST16 PRId32
132 #define PRIdFAST32 PRId32
136 #define INT16_MIN (-0x7fff-1)
140 #define INT16_MAX 0x7fff
144 #define INT64_MIN (-0x7fffffffffffffffLL-1)
148 #define INT64_MAX int64_t_C(9223372036854775807)
152 #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
155 #ifdef EMULATE_FAST_INT
156 typedef signed char int_fast8_t;
157 typedef signed int int_fast16_t;
158 typedef signed int int_fast32_t;
159 typedef unsigned char uint_fast8_t;
160 typedef unsigned int uint_fast16_t;
161 typedef unsigned int uint_fast32_t;
162 typedef uint64_t uint_fast64_t;
166 # if INT_MAX != 2147483647
173 #if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
174 static inline float floorf(float f) {
183 # if !defined(__MINGW32__) && !defined(__CYGWIN__)
184 # define int64_t_C(c) (c ## i64)
185 # define uint64_t_C(c) (c ## i64)
187 # ifdef HAVE_AV_CONFIG_H
188 # define inline __inline
192 # define int64_t_C(c) (c ## LL)
193 # define uint64_t_C(c) (c ## ULL)
194 # endif /* __MINGW32__ */
196 # ifdef HAVE_AV_CONFIG_H
201 # define snprintf _snprintf
202 # define vsnprintf _vsnprintf
205 /* CONFIG_WIN32 end */
206 #elif defined (CONFIG_OS2)
210 #define int64_t_C(c) (c ## LL)
211 #define uint64_t_C(c) (c ## ULL)
214 #ifdef HAVE_AV_CONFIG_H
216 #ifdef USE_FASTMEMCPY
217 #include "fastmemcpy.h"
222 #endif /* HAVE_AV_CONFIG_H */
230 #define int64_t_C(c) (c ## LL)
231 #define uint64_t_C(c) (c ## ULL)
234 #ifdef HAVE_AV_CONFIG_H
236 # ifdef USE_FASTMEMCPY
237 # include "fastmemcpy.h"
239 # endif /* HAVE_AV_CONFIG_H */
241 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
243 #ifdef HAVE_AV_CONFIG_H
247 // Use rip-relative addressing if compiling PIC code on x86-64.
248 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
249 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
250 # if defined(ARCH_X86_64) && defined(PIC)
251 # define MANGLE(a) "_" #a"(%%rip)"
253 # define MANGLE(a) "_" #a
256 # if defined(ARCH_X86_64) && defined(PIC)
257 # define MANGLE(a) #a"(%%rip)"
259 # define MANGLE(a) #a
271 # if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
273 inline void dprintf(const char* fmt,...) {}
278 # define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
280 # define dprintf(fmt,...)
283 # endif /* !CONFIG_WIN32 */
285 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
287 //rounded divison & shift
288 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
290 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
291 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
293 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
294 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
296 extern const uint32_t inverse[256];
298 #if defined(ARCH_X86) || defined(ARCH_X86_64)
299 # define FASTDIV(a,b) \
304 :"=d"(ret),"=a"(dmy)\
305 :"1"(a),"g"(inverse[b])\
309 #elif defined(CONFIG_FASTDIV)
310 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
312 # define FASTDIV(a,b) ((a)/(b))
315 /* define it to include statistics code (useful only for optimizing
332 extern int st_current_index;
333 extern unsigned int st_bit_counts[ST_NB];
334 extern unsigned int st_out_bit_counts[ST_NB];
336 void print_stats(void);
339 /* misc math functions */
340 extern const uint8_t ff_log2_tab[256];
342 static inline int av_log2(unsigned int v)
347 if (v & 0xffff0000) {
360 static inline int av_log2_16bit(unsigned int v)
375 static inline int mid_pred(int a, int b, int c)
378 int t= (a-b)&((a-b)>>31);
381 b-= (b-c)&((b-c)>>31);
382 b+= (a-b)&((a-b)>>31);
401 static inline int clip(int a, int amin, int amax)
411 static inline int clip_uint8(int a)
413 if (a&(~255)) return (-a)>>31;
418 extern const uint8_t ff_sqrt_tab[128];
420 int64_t ff_gcd(int64_t a, int64_t b);
422 static inline int ff_sqrt(int a)
428 if(a<128) return ff_sqrt_tab[a];
430 for(s=15; s>=0; s--){
431 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
441 * converts fourcc string to int
443 static inline int ff_get_fourcc(const char *s){
444 assert( strlen(s)==4 );
446 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
449 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
450 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
453 #if defined(ARCH_X86) || defined(ARCH_X86_64)
454 #define MASK_ABS(mask, level)\
459 : "+a" (level), "=&d" (mask)\
462 #define MASK_ABS(mask, level)\
464 level= (level^mask)-mask;
468 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
469 #define COPY3_IF_LT(x,y,a,b,c,d)\
475 : "+r" (x), "+r" (a), "+r" (c)\
476 : "r" (y), "r" (b), "r" (d)\
479 #define COPY3_IF_LT(x,y,a,b,c,d)\
487 #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
488 #if defined(ARCH_X86_64)
489 static inline uint64_t read_time(void)
492 asm volatile( "rdtsc\n\t"
495 return (d << 32) | (a & 0xffffffff);
497 #elif defined(ARCH_X86)
498 static inline long long read_time(void)
501 asm volatile( "rdtsc\n\t"
506 #else //FIXME check ppc64
507 static inline uint64_t read_time(void)
509 uint32_t tbu, tbl, temp;
511 /* from section 2.2.1 of the 32-bit PowerPC PEM */
512 __asm__ __volatile__(
519 : "=r"(tbl), "=r"(tbu), "=r"(temp)
523 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
527 #define START_TIMER \
529 uint64_t tstart= read_time();\
531 #define STOP_TIMER(id) \
534 static uint64_t tsum=0;\
535 static int tcount=0;\
536 static int tskip_count=0;\
537 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
538 tsum+= tend - tstart;\
542 if(256*256*256*64%(tcount+tskip_count)==0){\
543 av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
548 #define STOP_TIMER(id) {}
551 /* avoid usage of various functions */
552 #define malloc please_use_av_malloc
553 #define free please_use_av_free
554 #define realloc please_use_av_realloc
555 #define time time_is_forbidden_due_to_security_issues
556 #define rand rand_is_forbidden_due_to_state_trashing
557 #define srand srand_is_forbidden_due_to_state_trashing
558 #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
559 #define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
560 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
561 #define printf please_use_av_log
562 #define fprintf please_use_av_log
565 #define CHECKED_ALLOCZ(p, size)\
567 p= av_mallocz(size);\
568 if(p==NULL && (size)!=0){\
575 /* XXX: add ISOC specific test to avoid specific BSD testing. */
576 /* better than nothing implementation. */
577 /* btw, rintf() is existing on fbsd too -- alex */
578 static always_inline long int lrintf(float x)
585 : "=m" (i) : "t" (x) : "st"
589 /* XXX: incorrect, but make it compile */
590 return (int)(x + (x < 0 ? -0.5 : 0.5));
591 # endif /* ARCH_X86 */
593 return (int)(rint(x));
594 #endif /* CONFIG_WIN32 */
597 #ifndef _ISOC9X_SOURCE
598 #define _ISOC9X_SOURCE
601 #endif /* HAVE_LRINTF */
603 #endif /* HAVE_AV_CONFIG_H */
605 #endif /* COMMON_H */