Split common.h in two parts, purely internal stuff (internal.h) and things
[libav.git] / libavutil / internal.h
1 /**
2 * @file internal.h
3 * common internal api header.
4 */
5
6 #ifndef INTERNAL_H
7 #define INTERNAL_H
8
9 #if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
10 # define PIC
11 #endif
12
13 # ifndef ENODATA
14 # define ENODATA 61
15 # endif
16
17 #include <stddef.h>
18 #ifndef offsetof
19 # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
20 #endif
21
22 #define AVOPTION_CODEC_BOOL(name, help, field) \
23 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
24 #define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
25 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
26 #define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
27 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
28 #define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
29 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
30 #define AVOPTION_CODEC_STRING(name, help, field, str, val) \
31 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
32 #define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
33 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
34 #define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
35 #define AVOPTION_END() AVOPTION_SUB(NULL)
36
37 #ifdef __MINGW32__
38 # ifdef _DEBUG
39 # define DEBUG
40 # endif
41
42 # define snprintf _snprintf
43 # define vsnprintf _vsnprintf
44
45 # ifdef CONFIG_WINCE
46 # define perror(a)
47 # endif
48
49 /* __MINGW32__ end */
50 #elif defined (CONFIG_OS2)
51 /* OS/2 EMX */
52
53 #include <float.h>
54
55 #endif /* !__MINGW32__ && CONFIG_OS2 */
56
57 # ifdef USE_FASTMEMCPY
58 # include "fastmemcpy.h"
59 # endif
60
61 // Use rip-relative addressing if compiling PIC code on x86-64.
62 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
63 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
64 # if defined(ARCH_X86_64) && defined(PIC)
65 # define MANGLE(a) "_" #a"(%%rip)"
66 # else
67 # define MANGLE(a) "_" #a
68 # endif
69 # else
70 # if defined(ARCH_X86_64) && defined(PIC)
71 # define MANGLE(a) #a"(%%rip)"
72 # elif defined(CONFIG_DARWIN)
73 # define MANGLE(a) "_" #a
74 # else
75 # define MANGLE(a) #a
76 # endif
77 # endif
78
79 /* debug stuff */
80
81 # if !defined(DEBUG) && !defined(NDEBUG)
82 # define NDEBUG
83 # endif
84 # include <assert.h>
85
86 /* dprintf macros */
87 # ifdef DEBUG
88 # define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
89 # else
90 # define dprintf(fmt,...)
91 # endif
92
93 # ifdef CONFIG_WINCE
94 # define abort()
95 # endif
96
97 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
98
99 extern const uint32_t inverse[256];
100
101 #if defined(ARCH_X86) || defined(ARCH_X86_64)
102 # define FASTDIV(a,b) \
103 ({\
104 int ret,dmy;\
105 asm volatile(\
106 "mull %3"\
107 :"=d"(ret),"=a"(dmy)\
108 :"1"(a),"g"(inverse[b])\
109 );\
110 ret;\
111 })
112 #elif defined(CONFIG_FASTDIV)
113 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
114 #else
115 # define FASTDIV(a,b) ((a)/(b))
116 #endif
117
118 /* math */
119 extern FF_IMPORT_ATTR const uint8_t ff_sqrt_tab[128];
120
121 static inline int ff_sqrt(int a)
122 {
123 int ret=0;
124 int s;
125 int ret_sq=0;
126
127 if(a<128) return ff_sqrt_tab[a];
128
129 for(s=15; s>=0; s--){
130 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
131 if(b<=a){
132 ret_sq=b;
133 ret+= 1<<s;
134 }
135 }
136 return ret;
137 }
138
139 #if defined(ARCH_X86) || defined(ARCH_X86_64)
140 #define MASK_ABS(mask, level)\
141 asm volatile(\
142 "cdq \n\t"\
143 "xorl %1, %0 \n\t"\
144 "subl %1, %0 \n\t"\
145 : "+a" (level), "=&d" (mask)\
146 );
147 #else
148 #define MASK_ABS(mask, level)\
149 mask= level>>31;\
150 level= (level^mask)-mask;
151 #endif
152
153 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
154 #define COPY3_IF_LT(x,y,a,b,c,d)\
155 asm volatile (\
156 "cmpl %0, %3 \n\t"\
157 "cmovl %3, %0 \n\t"\
158 "cmovl %4, %1 \n\t"\
159 "cmovl %5, %2 \n\t"\
160 : "+r" (x), "+r" (a), "+r" (c)\
161 : "r" (y), "r" (b), "r" (d)\
162 );
163 #else
164 #define COPY3_IF_LT(x,y,a,b,c,d)\
165 if((y)<(x)){\
166 (x)=(y);\
167 (a)=(b);\
168 (c)=(d);\
169 }
170 #endif
171
172 /* avoid usage of various functions */
173 #define malloc please_use_av_malloc
174 #define free please_use_av_free
175 #define realloc please_use_av_realloc
176 #define time time_is_forbidden_due_to_security_issues
177 #define rand rand_is_forbidden_due_to_state_trashing
178 #define srand srand_is_forbidden_due_to_state_trashing
179 #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
180 #define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
181 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
182 #define printf please_use_av_log
183 #define fprintf please_use_av_log
184 #endif
185
186 #define CHECKED_ALLOCZ(p, size)\
187 {\
188 p= av_mallocz(size);\
189 if(p==NULL && (size)!=0){\
190 perror("malloc");\
191 goto fail;\
192 }\
193 }
194
195 #ifndef HAVE_LRINTF
196 /* XXX: add ISOC specific test to avoid specific BSD testing. */
197 /* better than nothing implementation. */
198 /* btw, rintf() is existing on fbsd too -- alex */
199 static always_inline long int lrintf(float x)
200 {
201 #ifdef __MINGW32__
202 # ifdef ARCH_X86
203 int32_t i;
204 asm volatile(
205 "fistpl %0\n\t"
206 : "=m" (i) : "t" (x) : "st"
207 );
208 return i;
209 # else
210 /* XXX: incorrect, but make it compile */
211 return (int)(x + (x < 0 ? -0.5 : 0.5));
212 # endif /* ARCH_X86 */
213 #else
214 return (int)(rint(x));
215 #endif /* __MINGW32__ */
216 }
217 #endif /* HAVE_LRINTF */
218
219 #endif /* INTERNAL_H */