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