Commit | Line | Data |
---|---|---|
04d7f601 DB |
1 | /* |
2 | * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at> | |
3 | * | |
b78e7197 DB |
4 | * This file is part of FFmpeg. |
5 | * | |
6 | * FFmpeg is free software; you can redistribute it and/or | |
04d7f601 DB |
7 | * modify it under the terms of the GNU Lesser General Public |
8 | * License as published by the Free Software Foundation; either | |
b78e7197 | 9 | * version 2.1 of the License, or (at your option) any later version. |
04d7f601 | 10 | * |
b78e7197 | 11 | * FFmpeg is distributed in the hope that it will be useful, |
04d7f601 DB |
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 | |
b78e7197 | 17 | * License along with FFmpeg; if not, write to the Free Software |
04d7f601 DB |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | */ | |
20 | ||
05020c89 RD |
21 | /** |
22 | * @file internal.h | |
23 | * common internal api header. | |
24 | */ | |
25 | ||
26 | #ifndef INTERNAL_H | |
27 | #define INTERNAL_H | |
28 | ||
99545457 | 29 | #include <stdint.h> |
99545457 | 30 | |
5403f857 MR |
31 | #ifndef attribute_used |
32 | #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0) | |
33 | # define attribute_used __attribute__((used)) | |
34 | #else | |
35 | # define attribute_used | |
36 | #endif | |
37 | #endif | |
38 | ||
cd107896 MR |
39 | #ifndef M_PI |
40 | #define M_PI 3.14159265358979323846 | |
41 | #endif | |
42 | ||
cd107896 MR |
43 | #ifndef INT16_MIN |
44 | #define INT16_MIN (-0x7fff-1) | |
45 | #endif | |
46 | ||
47 | #ifndef INT16_MAX | |
48 | #define INT16_MAX 0x7fff | |
49 | #endif | |
50 | ||
51 | #ifndef INT32_MIN | |
52 | #define INT32_MIN (-0x7fffffff-1) | |
53 | #endif | |
54 | ||
55 | #ifndef INT32_MAX | |
56 | #define INT32_MAX 0x7fffffff | |
57 | #endif | |
58 | ||
59 | #ifndef UINT32_MAX | |
60 | #define UINT32_MAX 0xffffffff | |
61 | #endif | |
62 | ||
63 | #ifndef INT64_MIN | |
64 | #define INT64_MIN (-0x7fffffffffffffffLL-1) | |
65 | #endif | |
66 | ||
67 | #ifndef INT64_MAX | |
8da9266c | 68 | #define INT64_MAX INT64_C(9223372036854775807) |
cd107896 MR |
69 | #endif |
70 | ||
71 | #ifndef UINT64_MAX | |
8da9266c | 72 | #define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF) |
cd107896 MR |
73 | #endif |
74 | ||
75 | #ifndef INT_BIT | |
76 | # if INT_MAX != 2147483647 | |
77 | # define INT_BIT 64 | |
78 | # else | |
79 | # define INT_BIT 32 | |
80 | # endif | |
81 | #endif | |
82 | ||
05020c89 RD |
83 | #if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC) |
84 | # define PIC | |
85 | #endif | |
86 | ||
cf1e119b | 87 | #include "intreadwrite.h" |
aab77159 RD |
88 | #include "bswap.h" |
89 | ||
05020c89 RD |
90 | #include <stddef.h> |
91 | #ifndef offsetof | |
635eb0cc | 92 | # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F)) |
05020c89 RD |
93 | #endif |
94 | ||
635eb0cc MR |
95 | #ifdef USE_FASTMEMCPY |
96 | # include "libvo/fastmemcpy.h" | |
6f74b71e | 97 | # define memcpy(a,b,c) fast_memcpy(a,b,c) |
635eb0cc | 98 | #endif |
05020c89 RD |
99 | |
100 | // Use rip-relative addressing if compiling PIC code on x86-64. | |
635eb0cc MR |
101 | #if defined(__MINGW32__) || defined(__CYGWIN__) || \ |
102 | defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__)) | |
103 | # if defined(ARCH_X86_64) && defined(PIC) | |
104 | # define MANGLE(a) "_" #a"(%%rip)" | |
05020c89 | 105 | # else |
635eb0cc | 106 | # define MANGLE(a) "_" #a |
05020c89 | 107 | # endif |
635eb0cc MR |
108 | #else |
109 | # if defined(ARCH_X86_64) && defined(PIC) | |
110 | # define MANGLE(a) #a"(%%rip)" | |
111 | # elif defined(CONFIG_DARWIN) | |
112 | # define MANGLE(a) "_" #a | |
113 | # else | |
114 | # define MANGLE(a) #a | |
115 | # endif | |
116 | #endif | |
05020c89 RD |
117 | |
118 | /* debug stuff */ | |
119 | ||
635eb0cc MR |
120 | #if !defined(DEBUG) && !defined(NDEBUG) |
121 | # define NDEBUG | |
122 | #endif | |
123 | #include <assert.h> | |
05020c89 RD |
124 | |
125 | /* dprintf macros */ | |
635eb0cc | 126 | #ifdef DEBUG |
318c5e05 | 127 | # define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__) |
635eb0cc | 128 | #else |
318c5e05 | 129 | # define dprintf(pctx, ...) |
635eb0cc | 130 | #endif |
05020c89 | 131 | |
635eb0cc | 132 | #define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0) |
05020c89 | 133 | |
7e5f82dc MR |
134 | /* math */ |
135 | ||
36cd3069 | 136 | extern const uint32_t ff_inverse[256]; |
05020c89 | 137 | |
3cd52279 | 138 | #if defined(ARCH_X86) |
05020c89 RD |
139 | # define FASTDIV(a,b) \ |
140 | ({\ | |
141 | int ret,dmy;\ | |
142 | asm volatile(\ | |
143 | "mull %3"\ | |
144 | :"=d"(ret),"=a"(dmy)\ | |
36cd3069 | 145 | :"1"(a),"g"(ff_inverse[b])\ |
05020c89 RD |
146 | );\ |
147 | ret;\ | |
148 | }) | |
eeebe6ad SS |
149 | #elif defined(ARCH_ARMV4L) |
150 | # define FASTDIV(a,b) \ | |
151 | ({\ | |
152 | int ret,dmy;\ | |
153 | asm volatile(\ | |
154 | "umull %1, %0, %2, %3"\ | |
155 | :"=&r"(ret),"=&r"(dmy)\ | |
36cd3069 | 156 | :"r"(a),"r"(ff_inverse[b])\ |
eeebe6ad SS |
157 | );\ |
158 | ret;\ | |
159 | }) | |
05020c89 | 160 | #elif defined(CONFIG_FASTDIV) |
36cd3069 | 161 | # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*ff_inverse[b])>>32)) |
05020c89 RD |
162 | #else |
163 | # define FASTDIV(a,b) ((a)/(b)) | |
164 | #endif | |
165 | ||
a77caa4d | 166 | extern const uint8_t ff_sqrt_tab[128]; |
05020c89 RD |
167 | |
168 | static inline int ff_sqrt(int a) | |
169 | { | |
170 | int ret=0; | |
45221f7f | 171 | int s, b; |
05020c89 RD |
172 | |
173 | if(a<128) return ff_sqrt_tab[a]; | |
174 | ||
45221f7f MN |
175 | for(s=30; s>=0; s-=2){ |
176 | ret+=ret; | |
177 | b= (1+2*ret)<<s; | |
05020c89 | 178 | if(b<=a){ |
fd735e4b | 179 | a-=b; |
45221f7f | 180 | ret++; |
05020c89 RD |
181 | } |
182 | } | |
183 | return ret; | |
184 | } | |
185 | ||
3cd52279 | 186 | #if defined(ARCH_X86) |
05020c89 RD |
187 | #define MASK_ABS(mask, level)\ |
188 | asm volatile(\ | |
189 | "cdq \n\t"\ | |
190 | "xorl %1, %0 \n\t"\ | |
191 | "subl %1, %0 \n\t"\ | |
192 | : "+a" (level), "=&d" (mask)\ | |
193 | ); | |
194 | #else | |
195 | #define MASK_ABS(mask, level)\ | |
196 | mask= level>>31;\ | |
197 | level= (level^mask)-mask; | |
198 | #endif | |
199 | ||
94e4c3a3 | 200 | #ifdef HAVE_CMOV |
05020c89 RD |
201 | #define COPY3_IF_LT(x,y,a,b,c,d)\ |
202 | asm volatile (\ | |
203 | "cmpl %0, %3 \n\t"\ | |
204 | "cmovl %3, %0 \n\t"\ | |
205 | "cmovl %4, %1 \n\t"\ | |
206 | "cmovl %5, %2 \n\t"\ | |
8c2e2040 | 207 | : "+&r" (x), "+&r" (a), "+r" (c)\ |
05020c89 RD |
208 | : "r" (y), "r" (b), "r" (d)\ |
209 | ); | |
210 | #else | |
211 | #define COPY3_IF_LT(x,y,a,b,c,d)\ | |
212 | if((y)<(x)){\ | |
213 | (x)=(y);\ | |
214 | (a)=(b);\ | |
215 | (c)=(d);\ | |
216 | } | |
217 | #endif | |
218 | ||
219 | /* avoid usage of various functions */ | |
84662c01 | 220 | #undef malloc |
05020c89 | 221 | #define malloc please_use_av_malloc |
84662c01 | 222 | #undef free |
05020c89 | 223 | #define free please_use_av_free |
84662c01 | 224 | #undef realloc |
05020c89 | 225 | #define realloc please_use_av_realloc |
84662c01 | 226 | #undef time |
05020c89 | 227 | #define time time_is_forbidden_due_to_security_issues |
84662c01 | 228 | #undef rand |
3299fb45 | 229 | #define rand rand_is_forbidden_due_to_state_trashing_use_av_random |
84662c01 | 230 | #undef srand |
3299fb45 | 231 | #define srand srand_is_forbidden_due_to_state_trashing_use_av_init_random |
84662c01 | 232 | #undef random |
3299fb45 | 233 | #define random random_is_forbidden_due_to_state_trashing_use_av_random |
84662c01 | 234 | #undef sprintf |
05020c89 | 235 | #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf |
84662c01 | 236 | #undef strcat |
272605c7 | 237 | #define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat |
84662c01 | 238 | #undef exit |
c367d067 | 239 | #define exit exit_is_forbidden |
22490641 | 240 | #if !(defined(LIBAVFORMAT_BUILD) || defined(FRAMEHOOK_H)) |
84662c01 | 241 | #undef printf |
05020c89 | 242 | #define printf please_use_av_log |
84662c01 | 243 | #undef fprintf |
05020c89 RD |
244 | #define fprintf please_use_av_log |
245 | #endif | |
246 | ||
247 | #define CHECKED_ALLOCZ(p, size)\ | |
248 | {\ | |
249 | p= av_mallocz(size);\ | |
250 | if(p==NULL && (size)!=0){\ | |
251 | perror("malloc");\ | |
252 | goto fail;\ | |
253 | }\ | |
254 | } | |
255 | ||
256 | #ifndef HAVE_LRINTF | |
257 | /* XXX: add ISOC specific test to avoid specific BSD testing. */ | |
258 | /* better than nothing implementation. */ | |
259 | /* btw, rintf() is existing on fbsd too -- alex */ | |
849f1035 | 260 | static av_always_inline long int lrintf(float x) |
05020c89 | 261 | { |
05020c89 | 262 | return (int)(rint(x)); |
05020c89 RD |
263 | } |
264 | #endif /* HAVE_LRINTF */ | |
265 | ||
266 | #endif /* INTERNAL_H */ |