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