3 * common internal api header.
9 #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
14 #define M_PI 3.14159265358979323846
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)
59 extern const struct AVOption avoptions_common[3 + 5];
61 extern const struct AVOption avoptions_common[3];
63 extern const struct AVOption avoptions_workaround_bug[11];
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 /* HAVE_INTTYPES_H */
116 #define INT16_MIN (-0x7fff-1)
120 #define INT16_MAX 0x7fff
124 #define INT64_MIN (-0x7fffffffffffffffLL-1)
128 #define INT64_MAX int64_t_C(9223372036854775807)
132 #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
135 #ifdef EMULATE_FAST_INT
136 /* note that we don't emulate 64bit ints */
137 typedef signed char int_fast8_t;
138 typedef signed int int_fast16_t;
139 typedef signed int int_fast32_t;
140 typedef unsigned char uint_fast8_t;
141 typedef unsigned int uint_fast16_t;
142 typedef unsigned int uint_fast32_t;
146 # if INT_MAX != 2147483647
153 #if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
154 static inline float floorf(float f) {
163 # if !defined(__MINGW32__) && !defined(__CYGWIN__)
164 # define int64_t_C(c) (c ## i64)
165 # define uint64_t_C(c) (c ## i64)
167 # ifdef HAVE_AV_CONFIG_H
168 # define inline __inline
172 # define int64_t_C(c) (c ## LL)
173 # define uint64_t_C(c) (c ## ULL)
174 # endif /* __MINGW32__ */
176 # ifdef HAVE_AV_CONFIG_H
181 # define snprintf _snprintf
182 # define vsnprintf _vsnprintf
185 /* CONFIG_WIN32 end */
186 #elif defined (CONFIG_OS2)
190 #define int64_t_C(c) (c ## LL)
191 #define uint64_t_C(c) (c ## ULL)
194 #ifdef HAVE_AV_CONFIG_H
196 #ifdef USE_FASTMEMCPY
197 #include "fastmemcpy.h"
202 #endif /* HAVE_AV_CONFIG_H */
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"
219 # endif /* HAVE_AV_CONFIG_H */
221 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
223 #ifdef HAVE_AV_CONFIG_H
227 // Use rip-relative addressing if compiling PIC code on x86-64.
228 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
229 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
230 # if defined(ARCH_X86_64) && defined(PIC)
231 # define MANGLE(a) "_" #a"(%%rip)"
233 # define MANGLE(a) "_" #a
236 # if defined(ARCH_X86_64) && defined(PIC)
237 # define MANGLE(a) #a"(%%rip)"
239 # define MANGLE(a) #a
251 # if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
253 inline void dprintf(const char* fmt,...) {}
258 # define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
260 # define dprintf(fmt,...)
263 # endif /* !CONFIG_WIN32 */
265 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
267 //rounded divison & shift
268 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
270 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
271 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
273 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
274 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
276 extern const uint32_t inverse[256];
278 #if defined(ARCH_X86) || defined(ARCH_X86_64)
279 # define FASTDIV(a,b) \
284 :"=d"(ret),"=a"(dmy)\
285 :"1"(a),"g"(inverse[b])\
289 #elif defined(CONFIG_FASTDIV)
290 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
292 # define FASTDIV(a,b) ((a)/(b))
295 /* define it to include statistics code (useful only for optimizing
312 extern int st_current_index;
313 extern unsigned int st_bit_counts[ST_NB];
314 extern unsigned int st_out_bit_counts[ST_NB];
316 void print_stats(void);
319 /* misc math functions */
320 extern const uint8_t ff_log2_tab[256];
322 static inline int av_log2(unsigned int v)
327 if (v & 0xffff0000) {
340 static inline int av_log2_16bit(unsigned int v)
355 static inline int mid_pred(int a, int b, int c)
358 int t= (a-b)&((a-b)>>31);
361 b-= (b-c)&((b-c)>>31);
362 b+= (a-b)&((a-b)>>31);
381 static inline int clip(int a, int amin, int amax)
391 static inline int clip_uint8(int a)
393 if (a&(~255)) return (-a)>>31;
398 extern const uint8_t ff_sqrt_tab[128];
400 int64_t ff_gcd(int64_t a, int64_t b);
402 static inline int ff_sqrt(int a)
408 if(a<128) return ff_sqrt_tab[a];
410 for(s=15; s>=0; s--){
411 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
421 * converts fourcc string to int
423 static inline int ff_get_fourcc(const char *s){
424 assert( strlen(s)==4 );
426 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
429 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
430 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
433 #if defined(ARCH_X86) || defined(ARCH_X86_64)
434 #define MASK_ABS(mask, level)\
439 : "+a" (level), "=&d" (mask)\
442 #define MASK_ABS(mask, level)\
444 level= (level^mask)-mask;
448 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
449 #define COPY3_IF_LT(x,y,a,b,c,d)\
455 : "+r" (x), "+r" (a), "+r" (c)\
456 : "r" (y), "r" (b), "r" (d)\
459 #define COPY3_IF_LT(x,y,a,b,c,d)\
467 #if defined(ARCH_X86) || defined(ARCH_X86_64)
468 static inline long long rdtsc(void)
471 asm volatile( "rdtsc\n\t"
477 #define START_TIMER \
479 uint64_t tstart= rdtsc();\
481 #define STOP_TIMER(id) \
484 static uint64_t tsum=0;\
485 static int tcount=0;\
486 static int tskip_count=0;\
487 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
488 tsum+= tend - tstart;\
492 if(256*256*256*64%(tcount+tskip_count)==0){\
493 av_log(NULL, AV_LOG_DEBUG, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
498 #define STOP_TIMER(id) {}
501 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
503 /* avoid usage of various functions */
504 #define malloc please_use_av_malloc
505 #define free please_use_av_free
506 #define realloc please_use_av_realloc
507 #define time time_is_forbidden_due_to_security_issues
508 #define rand rand_is_forbidden_due_to_state_trashing
509 #define srand srand_is_forbidden_due_to_state_trashing
510 #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
511 #define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
512 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
513 #define printf please_use_av_log
514 #define fprintf please_use_av_log
517 #define CHECKED_ALLOCZ(p, size)\
519 p= av_mallocz(size);\
520 if(p==NULL && (size)!=0){\
526 #endif /* HAVE_AV_CONFIG_H */
528 #endif /* COMMON_H */