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
210 /* CONFIG_WIN32 end */
211 #elif defined (CONFIG_OS2)
215 #define int64_t_C(c) (c ## LL)
216 #define uint64_t_C(c) (c ## ULL)
219 #ifdef HAVE_AV_CONFIG_H
221 #ifdef USE_FASTMEMCPY
222 #include "fastmemcpy.h"
227 #endif /* HAVE_AV_CONFIG_H */
235 #define int64_t_C(c) (c ## LL)
236 #define uint64_t_C(c) (c ## ULL)
239 #ifdef HAVE_AV_CONFIG_H
241 # ifdef USE_FASTMEMCPY
242 # include "fastmemcpy.h"
244 # endif /* HAVE_AV_CONFIG_H */
246 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
248 #ifdef HAVE_AV_CONFIG_H
250 #if defined(__MINGW32__) && !defined(BUILD_AVUTIL) && defined(BUILD_SHARED_AV)
251 # define FF_IMPORT_ATTR __declspec(dllimport)
253 # define FF_IMPORT_ATTR
259 // Use rip-relative addressing if compiling PIC code on x86-64.
260 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
261 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
262 # if defined(ARCH_X86_64) && defined(PIC)
263 # define MANGLE(a) "_" #a"(%%rip)"
265 # define MANGLE(a) "_" #a
268 # if defined(ARCH_X86_64) && defined(PIC)
269 # define MANGLE(a) #a"(%%rip)"
271 # define MANGLE(a) #a
283 # if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
285 inline void dprintf(const char* fmt,...) {}
290 # define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
292 # define dprintf(fmt,...)
295 # endif /* !CONFIG_WIN32 */
300 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
302 //rounded divison & shift
303 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
305 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
306 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
308 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
309 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
311 extern const uint32_t inverse[256];
313 #if defined(ARCH_X86) || defined(ARCH_X86_64)
314 # define FASTDIV(a,b) \
319 :"=d"(ret),"=a"(dmy)\
320 :"1"(a),"g"(inverse[b])\
324 #elif defined(CONFIG_FASTDIV)
325 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
327 # define FASTDIV(a,b) ((a)/(b))
330 /* define it to include statistics code (useful only for optimizing
347 extern int st_current_index;
348 extern unsigned int st_bit_counts[ST_NB];
349 extern unsigned int st_out_bit_counts[ST_NB];
351 void print_stats(void);
354 /* misc math functions */
355 extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
357 static inline int av_log2(unsigned int v)
362 if (v & 0xffff0000) {
375 static inline int av_log2_16bit(unsigned int v)
390 static inline int mid_pred(int a, int b, int c)
393 int t= (a-b)&((a-b)>>31);
396 b-= (b-c)&((b-c)>>31);
397 b+= (a-b)&((a-b)>>31);
416 static inline int clip(int a, int amin, int amax)
426 static inline int 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;
483 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
484 #define COPY3_IF_LT(x,y,a,b,c,d)\
490 : "+r" (x), "+r" (a), "+r" (c)\
491 : "r" (y), "r" (b), "r" (d)\
494 #define COPY3_IF_LT(x,y,a,b,c,d)\
502 #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
503 #if defined(ARCH_X86_64)
504 static inline uint64_t read_time(void)
507 asm volatile( "rdtsc\n\t"
510 return (d << 32) | (a & 0xffffffff);
512 #elif defined(ARCH_X86)
513 static inline long long read_time(void)
516 asm volatile( "rdtsc\n\t"
521 #else //FIXME check ppc64
522 static inline uint64_t read_time(void)
524 uint32_t tbu, tbl, temp;
526 /* from section 2.2.1 of the 32-bit PowerPC PEM */
527 __asm__ __volatile__(
534 : "=r"(tbl), "=r"(tbu), "=r"(temp)
538 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
542 #define START_TIMER \
544 uint64_t tstart= read_time();\
546 #define STOP_TIMER(id) \
549 static uint64_t tsum=0;\
550 static int tcount=0;\
551 static int tskip_count=0;\
552 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
553 tsum+= tend - tstart;\
557 if(256*256*256*64%(tcount+tskip_count)==0){\
558 av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
563 #define STOP_TIMER(id) {}
566 /* avoid usage of various functions */
567 #define malloc please_use_av_malloc
568 #define free please_use_av_free
569 #define realloc please_use_av_realloc
570 #define time time_is_forbidden_due_to_security_issues
571 #define rand rand_is_forbidden_due_to_state_trashing
572 #define srand srand_is_forbidden_due_to_state_trashing
573 #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
574 #define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
575 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
576 #define printf please_use_av_log
577 #define fprintf please_use_av_log
580 #define CHECKED_ALLOCZ(p, size)\
582 p= av_mallocz(size);\
583 if(p==NULL && (size)!=0){\
590 /* XXX: add ISOC specific test to avoid specific BSD testing. */
591 /* better than nothing implementation. */
592 /* btw, rintf() is existing on fbsd too -- alex */
593 static always_inline long int lrintf(float x)
600 : "=m" (i) : "t" (x) : "st"
604 /* XXX: incorrect, but make it compile */
605 return (int)(x + (x < 0 ? -0.5 : 0.5));
606 # endif /* ARCH_X86 */
608 return (int)(rint(x));
609 #endif /* CONFIG_WIN32 */
612 #ifndef _ISOC9X_SOURCE
613 #define _ISOC9X_SOURCE
616 #endif /* HAVE_LRINTF */
618 #endif /* HAVE_AV_CONFIG_H */
620 #endif /* COMMON_H */