]> git.sesse.net Git - ffmpeg/blob - libavutil/internal.h
748a823ac44117cc1ec544ac607eba53f4b8655d
[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
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 "attributes.h"
39 #include "timer.h"
40
41 #ifndef attribute_align_arg
42 #if ARCH_X86_32 && (!defined(__ICC) || __ICC > 1200) && AV_GCC_VERSION_AT_LEAST(4,2)
43 #    define attribute_align_arg __attribute__((force_align_arg_pointer))
44 #else
45 #    define attribute_align_arg
46 #endif
47 #endif
48
49
50 /**
51  * Mark a variable as used and prevent the compiler from optimizing it away.
52  * This is useful for asm that accesses varibles in ways that the compiler does not
53  * understand
54  */
55 #ifndef attribute_used
56 #if AV_GCC_VERSION_AT_LEAST(3,1)
57 #    define attribute_used __attribute__((used))
58 #else
59 #    define attribute_used
60 #endif
61 #endif
62
63 #ifndef INT16_MIN
64 #define INT16_MIN       (-0x7fff - 1)
65 #endif
66
67 #ifndef INT16_MAX
68 #define INT16_MAX       0x7fff
69 #endif
70
71 #ifndef INT32_MIN
72 #define INT32_MIN       (-0x7fffffff - 1)
73 #endif
74
75 #ifndef INT32_MAX
76 #define INT32_MAX       0x7fffffff
77 #endif
78
79 #ifndef UINT32_MAX
80 #define UINT32_MAX      0xffffffff
81 #endif
82
83 #ifndef INT64_MIN
84 #define INT64_MIN       (-0x7fffffffffffffffLL - 1)
85 #endif
86
87 #ifndef INT64_MAX
88 #define INT64_MAX INT64_C(9223372036854775807)
89 #endif
90
91 #ifndef UINT64_MAX
92 #define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
93 #endif
94
95 #ifndef INT_BIT
96 #    define INT_BIT (CHAR_BIT * sizeof(int))
97 #endif
98
99 #ifndef offsetof
100 #    define offsetof(T, F) ((unsigned int)((char *)&((T *)0)->F))
101 #endif
102
103 /* Use to export labels from asm. */
104 #define LABEL_MANGLE(a) EXTERN_PREFIX #a
105
106 // Use rip-relative addressing if compiling PIC code on x86-64.
107 #if ARCH_X86_64 && defined(PIC)
108 #    define LOCAL_MANGLE(a) #a "(%%rip)"
109 #else
110 #    define LOCAL_MANGLE(a) #a
111 #endif
112
113 #define MANGLE(a) EXTERN_PREFIX LOCAL_MANGLE(a)
114
115 /* debug stuff */
116
117 #define av_abort()      do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
118
119 /* math */
120
121 #if ARCH_X86
122 #define MASK_ABS(mask, level)\
123             __asm__ volatile(\
124                 "cltd                   \n\t"\
125                 "xorl %1, %0            \n\t"\
126                 "subl %1, %0            \n\t"\
127                 : "+a" (level), "=&d" (mask)\
128             );
129 #else
130 #define MASK_ABS(mask, level)\
131             mask  = level >> 31;\
132             level = (level ^ mask) - mask;
133 #endif
134
135 /* avoid usage of dangerous/inappropriate system functions */
136 #undef  malloc
137 #define malloc please_use_av_malloc
138 #undef  free
139 #define free please_use_av_free
140 #undef  realloc
141 #define realloc please_use_av_realloc
142 #undef  time
143 #define time time_is_forbidden_due_to_security_issues
144 #undef  rand
145 #define rand rand_is_forbidden_due_to_state_trashing_use_av_lfg_get
146 #undef  srand
147 #define srand srand_is_forbidden_due_to_state_trashing_use_av_lfg_init
148 #undef  random
149 #define random random_is_forbidden_due_to_state_trashing_use_av_lfg_get
150 #undef  sprintf
151 #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
152 #undef  strcat
153 #define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
154 #undef  exit
155 #define exit exit_is_forbidden
156 #ifndef LIBAVFORMAT_BUILD
157 #undef  printf
158 #define printf please_use_av_log_instead_of_printf
159 #undef  fprintf
160 #define fprintf please_use_av_log_instead_of_fprintf
161 #undef  puts
162 #define puts please_use_av_log_instead_of_puts
163 #undef  perror
164 #define perror please_use_av_log_instead_of_perror
165 #endif
166
167 #define FF_ALLOC_OR_GOTO(ctx, p, size, label)\
168 {\
169     p = av_malloc(size);\
170     if (p == NULL && (size) != 0) {\
171         av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
172         goto label;\
173     }\
174 }
175
176 #define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)\
177 {\
178     p = av_mallocz(size);\
179     if (p == NULL && (size) != 0) {\
180         av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
181         goto label;\
182     }\
183 }
184
185 #include "libm.h"
186
187 /**
188  * Return NULL if CONFIG_SMALL is true, otherwise the argument
189  * without modification. Used to disable the definition of strings
190  * (for example AVCodec long_names).
191  */
192 #if CONFIG_SMALL
193 #   define NULL_IF_CONFIG_SMALL(x) NULL
194 #else
195 #   define NULL_IF_CONFIG_SMALL(x) x
196 #endif
197
198
199 /**
200  * Define a function with only the non-default version specified.
201  *
202  * On systems with ELF shared libraries, all symbols exported from
203  * FFmpeg libraries are tagged with the name and major version of the
204  * library to which they belong.  If a function is moved from one
205  * library to another, a wrapper must be retained in the original
206  * location to preserve binary compatibility.
207  *
208  * Functions defined with this macro will never be used to resolve
209  * symbols by the build-time linker.
210  *
211  * @param type return type of function
212  * @param name name of function
213  * @param args argument list of function
214  * @param ver  version tag to assign function
215  */
216 #if HAVE_SYMVER_ASM_LABEL
217 #   define FF_SYMVER(type, name, args, ver)                     \
218     type ff_##name args __asm__ (EXTERN_PREFIX #name "@" ver);  \
219     type ff_##name args
220 #elif HAVE_SYMVER_GNU_ASM
221 #   define FF_SYMVER(type, name, args, ver)                             \
222     __asm__ (".symver ff_" #name "," EXTERN_PREFIX #name "@" ver);      \
223     type ff_##name args;                                                \
224     type ff_##name args
225 #endif
226
227 #endif /* AVUTIL_INTERNAL_H */