Add CHECKED_ALLOC macro.
[libav.git] / 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 libavutil/internal.h
23 * common internal API header
24 */
25
26 #ifndef AVUTIL_INTERNAL_H
27 #define AVUTIL_INTERNAL_H
28
29 #if !defined(DEBUG) && !defined(NDEBUG)
30 # define NDEBUG
31 #endif
32
33 #include <limits.h>
34 #include <stdint.h>
35 #include <stddef.h>
36 #include <assert.h>
37 #include "config.h"
38 #include "common.h"
39 #include "mem.h"
40 #include "timer.h"
41
42 #ifndef attribute_align_arg
43 #if (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(4,2)
44 # define attribute_align_arg __attribute__((force_align_arg_pointer))
45 #else
46 # define attribute_align_arg
47 #endif
48 #endif
49
50 #ifndef attribute_used
51 #if AV_GCC_VERSION_AT_LEAST(3,1)
52 # define attribute_used __attribute__((used))
53 #else
54 # define attribute_used
55 #endif
56 #endif
57
58 #ifndef INT16_MIN
59 #define INT16_MIN (-0x7fff-1)
60 #endif
61
62 #ifndef INT16_MAX
63 #define INT16_MAX 0x7fff
64 #endif
65
66 #ifndef INT32_MIN
67 #define INT32_MIN (-0x7fffffff-1)
68 #endif
69
70 #ifndef INT32_MAX
71 #define INT32_MAX 0x7fffffff
72 #endif
73
74 #ifndef UINT32_MAX
75 #define UINT32_MAX 0xffffffff
76 #endif
77
78 #ifndef INT64_MIN
79 #define INT64_MIN (-0x7fffffffffffffffLL-1)
80 #endif
81
82 #ifndef INT64_MAX
83 #define INT64_MAX INT64_C(9223372036854775807)
84 #endif
85
86 #ifndef UINT64_MAX
87 #define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
88 #endif
89
90 #ifndef INT_BIT
91 # define INT_BIT (CHAR_BIT * sizeof(int))
92 #endif
93
94 #if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
95 # define PIC
96 #endif
97
98 #ifndef offsetof
99 # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
100 #endif
101
102 /* Use to export labels from asm. */
103 #define LABEL_MANGLE(a) EXTERN_PREFIX #a
104
105 // Use rip-relative addressing if compiling PIC code on x86-64.
106 #if ARCH_X86_64 && defined(PIC)
107 # define LOCAL_MANGLE(a) #a "(%%rip)"
108 #else
109 # define LOCAL_MANGLE(a) #a
110 #endif
111
112 #define MANGLE(a) EXTERN_PREFIX LOCAL_MANGLE(a)
113
114 /* debug stuff */
115
116 /* dprintf macros */
117 #ifdef DEBUG
118 # define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
119 #else
120 # define dprintf(pctx, ...)
121 #endif
122
123 #define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
124
125 /* math */
126
127 extern const uint32_t ff_inverse[256];
128
129 #if ARCH_X86
130 # define FASTDIV(a,b) \
131 ({\
132 int ret,dmy;\
133 __asm__ volatile(\
134 "mull %3"\
135 :"=d"(ret),"=a"(dmy)\
136 :"1"(a),"g"(ff_inverse[b])\
137 );\
138 ret;\
139 })
140 #elif HAVE_ARMV6 && HAVE_INLINE_ASM
141 static inline av_const int FASTDIV(int a, int b)
142 {
143 int r, t;
144 __asm__ volatile("cmp %3, #2 \n\t"
145 "ldr %1, [%4, %3, lsl #2] \n\t"
146 "lsrle %0, %2, #1 \n\t"
147 "smmulgt %0, %1, %2 \n\t"
148 : "=&r"(r), "=&r"(t) : "r"(a), "r"(b), "r"(ff_inverse));
149 return r;
150 }
151 #elif ARCH_ARM && HAVE_INLINE_ASM
152 static inline av_const int FASTDIV(int a, int b)
153 {
154 int r, t;
155 __asm__ volatile ("umull %1, %0, %2, %3"
156 : "=&r"(r), "=&r"(t) : "r"(a), "r"(ff_inverse[b]));
157 return r;
158 }
159 #elif CONFIG_FASTDIV
160 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*ff_inverse[b])>>32))
161 #else
162 # define FASTDIV(a,b) ((a)/(b))
163 #endif
164
165 extern const uint8_t ff_sqrt_tab[256];
166
167 static inline av_const unsigned int ff_sqrt(unsigned int a)
168 {
169 unsigned int b;
170
171 if(a<255) return (ff_sqrt_tab[a+1]-1)>>4;
172 else if(a<(1<<12)) b= ff_sqrt_tab[a>>4 ]>>2;
173 #if !CONFIG_SMALL
174 else if(a<(1<<14)) b= ff_sqrt_tab[a>>6 ]>>1;
175 else if(a<(1<<16)) b= ff_sqrt_tab[a>>8 ] ;
176 #endif
177 else{
178 int s= av_log2_16bit(a>>16)>>1;
179 unsigned int c= a>>(s+2);
180 b= ff_sqrt_tab[c>>(s+8)];
181 b= FASTDIV(c,b) + (b<<s);
182 }
183
184 return b - (a<b*b);
185 }
186
187 #if ARCH_X86
188 #define MASK_ABS(mask, level)\
189 __asm__ volatile(\
190 "cltd \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 #if 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 dangerous/inappropriate system 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_lfg_get
231 #undef srand
232 #define srand srand_is_forbidden_due_to_state_trashing_use_av_lfg_init
233 #undef random
234 #define random random_is_forbidden_due_to_state_trashing_use_av_lfg_get
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 #ifndef LIBAVFORMAT_BUILD
242 #undef printf
243 #define printf please_use_av_log_instead_of_printf
244 #undef fprintf
245 #define fprintf please_use_av_log_instead_of_fprintf
246 #undef puts
247 #define puts please_use_av_log_instead_of_puts
248 #undef perror
249 #define perror please_use_av_log_instead_of_perror
250 #endif
251
252 #define CHECKED_ALLOC(p, size)\
253 {\
254 p= av_malloc(size);\
255 if(p==NULL && (size)!=0){\
256 av_log(NULL, AV_LOG_ERROR, "Cannot allocate memory.");\
257 goto fail;\
258 }\
259 }
260
261 #define CHECKED_ALLOCZ(p, size)\
262 {\
263 p= av_mallocz(size);\
264 if(p==NULL && (size)!=0){\
265 av_log(NULL, AV_LOG_ERROR, "Cannot allocate memory.");\
266 goto fail;\
267 }\
268 }
269
270 #if !HAVE_LLRINT
271 static av_always_inline av_const long long llrint(double x)
272 {
273 return rint(x);
274 }
275 #endif /* HAVE_LLRINT */
276
277 #if !HAVE_LOG2
278 static av_always_inline av_const double log2(double x)
279 {
280 return log(x) * 1.44269504088896340736;
281 }
282 #endif /* HAVE_LOG2 */
283
284 #if !HAVE_LRINT
285 static av_always_inline av_const long int lrint(double x)
286 {
287 return rint(x);
288 }
289 #endif /* HAVE_LRINT */
290
291 #if !HAVE_LRINTF
292 static av_always_inline av_const long int lrintf(float x)
293 {
294 return (int)(rint(x));
295 }
296 #endif /* HAVE_LRINTF */
297
298 #if !HAVE_ROUND
299 static av_always_inline av_const double round(double x)
300 {
301 return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
302 }
303 #endif /* HAVE_ROUND */
304
305 #if !HAVE_ROUNDF
306 static av_always_inline av_const float roundf(float x)
307 {
308 return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
309 }
310 #endif /* HAVE_ROUNDF */
311
312 #if !HAVE_TRUNCF
313 static av_always_inline av_const float truncf(float x)
314 {
315 return (x > 0) ? floor(x) : ceil(x);
316 }
317 #endif /* HAVE_TRUNCF */
318
319 /**
320 * Returns NULL if CONFIG_SMALL is true, otherwise the argument
321 * without modification. Used to disable the definition of strings
322 * (for example AVCodec long_names).
323 */
324 #if CONFIG_SMALL
325 # define NULL_IF_CONFIG_SMALL(x) NULL
326 #else
327 # define NULL_IF_CONFIG_SMALL(x) x
328 #endif
329
330 #endif /* AVUTIL_INTERNAL_H */