]> git.sesse.net Git - ffmpeg/blob - libavutil/internal.h
move comment
[ffmpeg] / libavutil / internal.h
1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file internal.h
23  * common internal api header.
24  */
25
26 #ifndef INTERNAL_H
27 #define INTERNAL_H
28
29 #if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
30 #    define PIC
31 #endif
32
33 #ifndef ENODATA
34 #    define ENODATA  61
35 #endif
36
37 #include "bswap.h"
38
39 #include <stddef.h>
40 #ifndef offsetof
41 #    define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
42 #endif
43
44 #ifdef __MINGW32__
45 #    ifdef _DEBUG
46 #        define DEBUG
47 #    endif
48
49 #    define snprintf _snprintf
50 #    define vsnprintf _vsnprintf
51
52 #    ifdef CONFIG_WINCE
53 #        define perror(a)
54 #        define abort()
55 #    endif
56
57 /* __MINGW32__ end */
58 #elif defined (CONFIG_OS2)
59 /* OS/2 EMX */
60
61 #    include <float.h>
62
63 #endif /* !__MINGW32__ && CONFIG_OS2 */
64
65 #ifdef USE_FASTMEMCPY
66 #    include "libvo/fastmemcpy.h"
67 #endif
68
69 // Use rip-relative addressing if compiling PIC code on x86-64.
70 #if defined(__MINGW32__) || defined(__CYGWIN__) || \
71     defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
72 #    if defined(ARCH_X86_64) && defined(PIC)
73 #        define MANGLE(a) "_" #a"(%%rip)"
74 #    else
75 #        define MANGLE(a) "_" #a
76 #    endif
77 #else
78 #    if defined(ARCH_X86_64) && defined(PIC)
79 #        define MANGLE(a) #a"(%%rip)"
80 #    elif defined(CONFIG_DARWIN)
81 #        define MANGLE(a) "_" #a
82 #    else
83 #        define MANGLE(a) #a
84 #    endif
85 #endif
86
87 /* debug stuff */
88
89 #if !defined(DEBUG) && !defined(NDEBUG)
90 #    define NDEBUG
91 #endif
92 #include <assert.h>
93
94 /* dprintf macros */
95 #ifdef DEBUG
96 #    define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
97 #else
98 #    define dprintf(fmt,...)
99 #endif
100
101 #define av_abort()      do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
102
103 /* math */
104
105 extern const uint32_t ff_inverse[256];
106
107 #if defined(ARCH_X86)
108 #    define FASTDIV(a,b) \
109     ({\
110         int ret,dmy;\
111         asm volatile(\
112             "mull %3"\
113             :"=d"(ret),"=a"(dmy)\
114             :"1"(a),"g"(ff_inverse[b])\
115             );\
116         ret;\
117     })
118 #elif defined(ARCH_ARMV4L)
119 #    define FASTDIV(a,b) \
120     ({\
121         int ret,dmy;\
122         asm volatile(\
123             "umull %1, %0, %2, %3"\
124             :"=&r"(ret),"=&r"(dmy)\
125             :"r"(a),"r"(ff_inverse[b])\
126             );\
127         ret;\
128     })
129 #elif defined(CONFIG_FASTDIV)
130 #    define FASTDIV(a,b)   ((uint32_t)((((uint64_t)a)*ff_inverse[b])>>32))
131 #else
132 #    define FASTDIV(a,b)   ((a)/(b))
133 #endif
134
135 extern FF_IMPORT_ATTR const uint8_t ff_sqrt_tab[128];
136
137 static inline int ff_sqrt(int a)
138 {
139     int ret=0;
140     int s;
141     int ret_sq=0;
142
143     if(a<128) return ff_sqrt_tab[a];
144
145     for(s=15; s>=0; s--){
146         int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
147         if(b<=a){
148             ret_sq=b;
149             ret+= 1<<s;
150         }
151     }
152     return ret;
153 }
154
155 #if defined(ARCH_X86)
156 #define MASK_ABS(mask, level)\
157             asm volatile(\
158                 "cdq                    \n\t"\
159                 "xorl %1, %0            \n\t"\
160                 "subl %1, %0            \n\t"\
161                 : "+a" (level), "=&d" (mask)\
162             );
163 #else
164 #define MASK_ABS(mask, level)\
165             mask= level>>31;\
166             level= (level^mask)-mask;
167 #endif
168
169 #ifdef HAVE_CMOV
170 #define COPY3_IF_LT(x,y,a,b,c,d)\
171 asm volatile (\
172     "cmpl %0, %3        \n\t"\
173     "cmovl %3, %0       \n\t"\
174     "cmovl %4, %1       \n\t"\
175     "cmovl %5, %2       \n\t"\
176     : "+r" (x), "+r" (a), "+r" (c)\
177     : "r" (y), "r" (b), "r" (d)\
178 );
179 #else
180 #define COPY3_IF_LT(x,y,a,b,c,d)\
181 if((y)<(x)){\
182      (x)=(y);\
183      (a)=(b);\
184      (c)=(d);\
185 }
186 #endif
187
188 /* avoid usage of various functions */
189 #define malloc please_use_av_malloc
190 #define free please_use_av_free
191 #define realloc please_use_av_realloc
192 #define time time_is_forbidden_due_to_security_issues
193 #define rand rand_is_forbidden_due_to_state_trashing
194 #define srand srand_is_forbidden_due_to_state_trashing
195 #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
196 #define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
197 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
198 #define printf please_use_av_log
199 #define fprintf please_use_av_log
200 #endif
201
202 #define CHECKED_ALLOCZ(p, size)\
203 {\
204     p= av_mallocz(size);\
205     if(p==NULL && (size)!=0){\
206         perror("malloc");\
207         goto fail;\
208     }\
209 }
210
211 #ifndef HAVE_LRINTF
212 /* XXX: add ISOC specific test to avoid specific BSD testing. */
213 /* better than nothing implementation. */
214 /* btw, rintf() is existing on fbsd too -- alex */
215 static always_inline long int lrintf(float x)
216 {
217 #ifdef __MINGW32__
218 #  ifdef ARCH_X86_32
219     int32_t i;
220     asm volatile(
221         "fistpl %0\n\t"
222         : "=m" (i) : "t" (x) : "st"
223     );
224     return i;
225 #  else
226     /* XXX: incorrect, but make it compile */
227     return (int)(x + (x < 0 ? -0.5 : 0.5));
228 #  endif /* ARCH_X86_32 */
229 #else
230     return (int)(rint(x));
231 #endif /* __MINGW32__ */
232 }
233 #endif /* HAVE_LRINTF */
234
235 #endif /* INTERNAL_H */