]> git.sesse.net Git - ffmpeg/blob - libavutil/internal.h
d6a6a9107c62eaa79d41b3385eed9fbeed133670
[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 #include <stdint.h>
30 #include <assert.h>
31
32 #ifndef attribute_used
33 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
34 #    define attribute_used __attribute__((used))
35 #else
36 #    define attribute_used
37 #endif
38 #endif
39
40 #ifndef M_PI
41 #define M_PI    3.14159265358979323846
42 #endif
43
44 #ifndef INT16_MIN
45 #define INT16_MIN       (-0x7fff-1)
46 #endif
47
48 #ifndef INT16_MAX
49 #define INT16_MAX       0x7fff
50 #endif
51
52 #ifndef INT32_MIN
53 #define INT32_MIN       (-0x7fffffff-1)
54 #endif
55
56 #ifndef INT32_MAX
57 #define INT32_MAX       0x7fffffff
58 #endif
59
60 #ifndef UINT32_MAX
61 #define UINT32_MAX      0xffffffff
62 #endif
63
64 #ifndef INT64_MIN
65 #define INT64_MIN       (-0x7fffffffffffffffLL-1)
66 #endif
67
68 #ifndef INT64_MAX
69 #define INT64_MAX INT64_C(9223372036854775807)
70 #endif
71
72 #ifndef UINT64_MAX
73 #define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
74 #endif
75
76 #ifndef INT_BIT
77 #    if INT_MAX != 2147483647
78 #        define INT_BIT 64
79 #    else
80 #        define INT_BIT 32
81 #    endif
82 #endif
83
84 #if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
85 #    define PIC
86 #endif
87
88 #include "intreadwrite.h"
89 #include "bswap.h"
90
91 #include <stddef.h>
92 #ifndef offsetof
93 #    define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
94 #endif
95
96 #ifdef USE_FASTMEMCPY
97 #    include "libvo/fastmemcpy.h"
98 #    define memcpy(a,b,c) fast_memcpy(a,b,c)
99 #endif
100
101 // Use rip-relative addressing if compiling PIC code on x86-64.
102 #if defined(__MINGW32__) || defined(__CYGWIN__) || \
103     defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
104 #    if defined(ARCH_X86_64) && defined(PIC)
105 #        define MANGLE(a) "_" #a"(%%rip)"
106 #    else
107 #        define MANGLE(a) "_" #a
108 #    endif
109 #else
110 #    if defined(ARCH_X86_64) && defined(PIC)
111 #        define MANGLE(a) #a"(%%rip)"
112 #    elif defined(CONFIG_DARWIN)
113 #        define MANGLE(a) "_" #a
114 #    else
115 #        define MANGLE(a) #a
116 #    endif
117 #endif
118
119 /* debug stuff */
120
121 #if !defined(DEBUG) && !defined(NDEBUG)
122 #    define NDEBUG
123 #endif
124 #include <assert.h>
125
126 /* dprintf macros */
127 #ifdef DEBUG
128 #    define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
129 #else
130 #    define dprintf(pctx, ...)
131 #endif
132
133 #define av_abort()      do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
134
135 /* math */
136
137 extern const uint32_t ff_inverse[256];
138
139 #if defined(ARCH_X86)
140 #    define FASTDIV(a,b) \
141     ({\
142         int ret,dmy;\
143         asm volatile(\
144             "mull %3"\
145             :"=d"(ret),"=a"(dmy)\
146             :"1"(a),"g"(ff_inverse[b])\
147             );\
148         ret;\
149     })
150 #elif defined(ARCH_ARMV4L)
151 #    define FASTDIV(a,b) \
152     ({\
153         int ret,dmy;\
154         asm volatile(\
155             "umull %1, %0, %2, %3"\
156             :"=&r"(ret),"=&r"(dmy)\
157             :"r"(a),"r"(ff_inverse[b])\
158             );\
159         ret;\
160     })
161 #elif defined(CONFIG_FASTDIV)
162 #    define FASTDIV(a,b)   ((uint32_t)((((uint64_t)a)*ff_inverse[b])>>32))
163 #else
164 #    define FASTDIV(a,b)   ((a)/(b))
165 #endif
166
167 extern const uint8_t ff_sqrt_tab[128];
168
169 static inline int ff_sqrt(int a)
170 {
171     int ret=0;
172     int s, b;
173
174     if(a<128) return ff_sqrt_tab[a];
175
176     for(s=30; s>=0; s-=2){
177         ret+=ret;
178         b= (1+2*ret)<<s;
179         if(b<=a){
180             a-=b;
181             ret++;
182         }
183     }
184     return ret;
185 }
186
187 #if defined(ARCH_X86)
188 #define MASK_ABS(mask, level)\
189             asm volatile(\
190                 "cdq                    \n\t"\
191                 "xorl %1, %0            \n\t"\
192                 "subl %1, %0            \n\t"\
193                 : "+a" (level), "=&d" (mask)\
194             );
195 #else
196 #define MASK_ABS(mask, level)\
197             mask= level>>31;\
198             level= (level^mask)-mask;
199 #endif
200
201 #ifdef HAVE_CMOV
202 #define COPY3_IF_LT(x,y,a,b,c,d)\
203 asm volatile (\
204     "cmpl %0, %3        \n\t"\
205     "cmovl %3, %0       \n\t"\
206     "cmovl %4, %1       \n\t"\
207     "cmovl %5, %2       \n\t"\
208     : "+&r" (x), "+&r" (a), "+r" (c)\
209     : "r" (y), "r" (b), "r" (d)\
210 );
211 #else
212 #define COPY3_IF_LT(x,y,a,b,c,d)\
213 if((y)<(x)){\
214      (x)=(y);\
215      (a)=(b);\
216      (c)=(d);\
217 }
218 #endif
219
220 /* avoid usage of various functions */
221 #undef  malloc
222 #define malloc please_use_av_malloc
223 #undef  free
224 #define free please_use_av_free
225 #undef  realloc
226 #define realloc please_use_av_realloc
227 #undef  time
228 #define time time_is_forbidden_due_to_security_issues
229 #undef  rand
230 #define rand rand_is_forbidden_due_to_state_trashing_use_av_random
231 #undef  srand
232 #define srand srand_is_forbidden_due_to_state_trashing_use_av_init_random
233 #undef  random
234 #define random random_is_forbidden_due_to_state_trashing_use_av_random
235 #undef  sprintf
236 #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
237 #undef  strcat
238 #define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
239 #undef  exit
240 #define exit exit_is_forbidden
241 #if !(defined(LIBAVFORMAT_BUILD) || defined(FRAMEHOOK_H))
242 #undef  printf
243 #define printf please_use_av_log
244 #undef  fprintf
245 #define fprintf please_use_av_log
246 #endif
247
248 #define CHECKED_ALLOCZ(p, size)\
249 {\
250     p= av_mallocz(size);\
251     if(p==NULL && (size)!=0){\
252         perror("malloc");\
253         goto fail;\
254     }\
255 }
256
257 #ifndef HAVE_LRINTF
258 /* XXX: add ISOC specific test to avoid specific BSD testing. */
259 /* better than nothing implementation. */
260 /* btw, rintf() is existing on fbsd too -- alex */
261 static av_always_inline long int lrintf(float x)
262 {
263     return (int)(rint(x));
264 }
265 #endif /* HAVE_LRINTF */
266
267 #endif /* INTERNAL_H */