move more macros to internal.h
[libav.git] / libavutil / internal.h
CommitLineData
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
5403f857
MR
29#ifndef always_inline
30#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
31# define always_inline __attribute__((always_inline)) inline
32#else
33# define always_inline inline
34#endif
35#endif
36
37#ifndef attribute_used
38#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
39# define attribute_used __attribute__((used))
40#else
41# define attribute_used
42#endif
43#endif
44
45#ifndef attribute_unused
46#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
47# define attribute_unused __attribute__((unused))
48#else
49# define attribute_unused
50#endif
51#endif
52
cd107896
MR
53#ifndef M_PI
54#define M_PI 3.14159265358979323846
55#endif
56
57#ifndef PRId64
58#define PRId64 "lld"
59#endif
60
61#ifndef PRIu64
62#define PRIu64 "llu"
63#endif
64
65#ifndef PRIx64
66#define PRIx64 "llx"
67#endif
68
69#ifndef PRIX64
70#define PRIX64 "llX"
71#endif
72
73#ifndef PRId32
74#define PRId32 "d"
75#endif
76
77#ifndef PRIdFAST16
78#define PRIdFAST16 PRId32
79#endif
80
81#ifndef PRIdFAST32
82#define PRIdFAST32 PRId32
83#endif
84
85#ifndef INT16_MIN
86#define INT16_MIN (-0x7fff-1)
87#endif
88
89#ifndef INT16_MAX
90#define INT16_MAX 0x7fff
91#endif
92
93#ifndef INT32_MIN
94#define INT32_MIN (-0x7fffffff-1)
95#endif
96
97#ifndef INT32_MAX
98#define INT32_MAX 0x7fffffff
99#endif
100
101#ifndef UINT32_MAX
102#define UINT32_MAX 0xffffffff
103#endif
104
105#ifndef INT64_MIN
106#define INT64_MIN (-0x7fffffffffffffffLL-1)
107#endif
108
109#ifndef INT64_MAX
110#define INT64_MAX int64_t_C(9223372036854775807)
111#endif
112
113#ifndef UINT64_MAX
114#define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
115#endif
116
117#ifndef INT_BIT
118# if INT_MAX != 2147483647
119# define INT_BIT 64
120# else
121# define INT_BIT 32
122# endif
123#endif
124
05020c89
RD
125#if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
126# define PIC
127#endif
128
635eb0cc
MR
129#ifndef ENODATA
130# define ENODATA 61
131#endif
05020c89 132
cf1e119b 133#include "intreadwrite.h"
aab77159
RD
134#include "bswap.h"
135
05020c89
RD
136#include <stddef.h>
137#ifndef offsetof
635eb0cc 138# define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
05020c89
RD
139#endif
140
05020c89
RD
141#ifdef __MINGW32__
142# ifdef _DEBUG
143# define DEBUG
144# endif
145
146# define snprintf _snprintf
147# define vsnprintf _vsnprintf
148
149# ifdef CONFIG_WINCE
150# define perror(a)
f940f7c9 151# define abort()
05020c89
RD
152# endif
153
154/* __MINGW32__ end */
155#elif defined (CONFIG_OS2)
156/* OS/2 EMX */
157
635eb0cc 158# include <float.h>
05020c89
RD
159
160#endif /* !__MINGW32__ && CONFIG_OS2 */
161
635eb0cc
MR
162#ifdef USE_FASTMEMCPY
163# include "libvo/fastmemcpy.h"
164#endif
05020c89
RD
165
166// Use rip-relative addressing if compiling PIC code on x86-64.
635eb0cc
MR
167#if defined(__MINGW32__) || defined(__CYGWIN__) || \
168 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
169# if defined(ARCH_X86_64) && defined(PIC)
170# define MANGLE(a) "_" #a"(%%rip)"
05020c89 171# else
635eb0cc 172# define MANGLE(a) "_" #a
05020c89 173# endif
635eb0cc
MR
174#else
175# if defined(ARCH_X86_64) && defined(PIC)
176# define MANGLE(a) #a"(%%rip)"
177# elif defined(CONFIG_DARWIN)
178# define MANGLE(a) "_" #a
179# else
180# define MANGLE(a) #a
181# endif
182#endif
05020c89
RD
183
184/* debug stuff */
185
635eb0cc
MR
186#if !defined(DEBUG) && !defined(NDEBUG)
187# define NDEBUG
188#endif
189#include <assert.h>
05020c89
RD
190
191/* dprintf macros */
635eb0cc
MR
192#ifdef DEBUG
193# define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
194#else
195# define dprintf(fmt,...)
196#endif
05020c89 197
635eb0cc 198#define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
05020c89 199
7e5f82dc
MR
200/* math */
201
36cd3069 202extern const uint32_t ff_inverse[256];
05020c89 203
3cd52279 204#if defined(ARCH_X86)
05020c89
RD
205# define FASTDIV(a,b) \
206 ({\
207 int ret,dmy;\
208 asm volatile(\
209 "mull %3"\
210 :"=d"(ret),"=a"(dmy)\
36cd3069 211 :"1"(a),"g"(ff_inverse[b])\
05020c89
RD
212 );\
213 ret;\
214 })
eeebe6ad
SS
215#elif defined(ARCH_ARMV4L)
216# define FASTDIV(a,b) \
217 ({\
218 int ret,dmy;\
219 asm volatile(\
220 "umull %1, %0, %2, %3"\
221 :"=&r"(ret),"=&r"(dmy)\
36cd3069 222 :"r"(a),"r"(ff_inverse[b])\
eeebe6ad
SS
223 );\
224 ret;\
225 })
05020c89 226#elif defined(CONFIG_FASTDIV)
36cd3069 227# define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*ff_inverse[b])>>32))
05020c89
RD
228#else
229# define FASTDIV(a,b) ((a)/(b))
230#endif
231
05020c89
RD
232extern FF_IMPORT_ATTR const uint8_t ff_sqrt_tab[128];
233
234static inline int ff_sqrt(int a)
235{
236 int ret=0;
237 int s;
238 int ret_sq=0;
239
240 if(a<128) return ff_sqrt_tab[a];
241
242 for(s=15; s>=0; s--){
243 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
244 if(b<=a){
245 ret_sq=b;
246 ret+= 1<<s;
247 }
248 }
249 return ret;
250}
251
3cd52279 252#if defined(ARCH_X86)
05020c89
RD
253#define MASK_ABS(mask, level)\
254 asm volatile(\
255 "cdq \n\t"\
256 "xorl %1, %0 \n\t"\
257 "subl %1, %0 \n\t"\
258 : "+a" (level), "=&d" (mask)\
259 );
260#else
261#define MASK_ABS(mask, level)\
262 mask= level>>31;\
263 level= (level^mask)-mask;
264#endif
265
94e4c3a3 266#ifdef HAVE_CMOV
05020c89
RD
267#define COPY3_IF_LT(x,y,a,b,c,d)\
268asm volatile (\
269 "cmpl %0, %3 \n\t"\
270 "cmovl %3, %0 \n\t"\
271 "cmovl %4, %1 \n\t"\
272 "cmovl %5, %2 \n\t"\
273 : "+r" (x), "+r" (a), "+r" (c)\
274 : "r" (y), "r" (b), "r" (d)\
275);
276#else
277#define COPY3_IF_LT(x,y,a,b,c,d)\
278if((y)<(x)){\
279 (x)=(y);\
280 (a)=(b);\
281 (c)=(d);\
282}
283#endif
284
285/* avoid usage of various functions */
286#define malloc please_use_av_malloc
287#define free please_use_av_free
288#define realloc please_use_av_realloc
289#define time time_is_forbidden_due_to_security_issues
290#define rand rand_is_forbidden_due_to_state_trashing
291#define srand srand_is_forbidden_due_to_state_trashing
292#define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
293#define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
294#if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
295#define printf please_use_av_log
296#define fprintf please_use_av_log
297#endif
298
299#define CHECKED_ALLOCZ(p, size)\
300{\
301 p= av_mallocz(size);\
302 if(p==NULL && (size)!=0){\
303 perror("malloc");\
304 goto fail;\
305 }\
306}
307
308#ifndef HAVE_LRINTF
309/* XXX: add ISOC specific test to avoid specific BSD testing. */
310/* better than nothing implementation. */
311/* btw, rintf() is existing on fbsd too -- alex */
312static always_inline long int lrintf(float x)
313{
314#ifdef __MINGW32__
419b8784 315# ifdef ARCH_X86_32
05020c89
RD
316 int32_t i;
317 asm volatile(
318 "fistpl %0\n\t"
319 : "=m" (i) : "t" (x) : "st"
320 );
321 return i;
322# else
323 /* XXX: incorrect, but make it compile */
324 return (int)(x + (x < 0 ? -0.5 : 0.5));
419b8784 325# endif /* ARCH_X86_32 */
05020c89
RD
326#else
327 return (int)(rint(x));
328#endif /* __MINGW32__ */
329}
330#endif /* HAVE_LRINTF */
331
332#endif /* INTERNAL_H */