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 // This can be enabled to allow detection of additional integer overflows with ubsan
34 //#define CHECKED
35 
36 #include <limits.h>
37 #include <stdint.h>
38 #include <stddef.h>
39 #include <assert.h>
40 #include "config.h"
41 #include "attributes.h"
42 #include "timer.h"
43 #include "cpu.h"
44 #include "dict.h"
45 #include "macros.h"
46 #include "mem.h"
47 #include "pixfmt.h"
48 #include "version.h"
49 
50 #if ARCH_X86
51 #   include "x86/emms.h"
52 #endif
53 
54 #ifndef emms_c
55 #   define emms_c() do {} while(0)
56 #endif
57 
58 #ifndef attribute_align_arg
59 #if ARCH_X86_32 && AV_GCC_VERSION_AT_LEAST(4,2)
60 #    define attribute_align_arg __attribute__((force_align_arg_pointer))
61 #else
62 #    define attribute_align_arg
63 #endif
64 #endif
65 
66 #if defined(_WIN32) && CONFIG_SHARED && !defined(BUILDING_avutil)
67 #    define av_export_avutil __declspec(dllimport)
68 #else
69 #    define av_export_avutil
70 #endif
71 
72 #if HAVE_PRAGMA_DEPRECATED
73 #    if defined(__ICL) || defined (__INTEL_COMPILER)
74 #        define FF_DISABLE_DEPRECATION_WARNINGS __pragma(warning(push)) __pragma(warning(disable:1478))
75 #        define FF_ENABLE_DEPRECATION_WARNINGS  __pragma(warning(pop))
76 #    elif defined(_MSC_VER)
77 #        define FF_DISABLE_DEPRECATION_WARNINGS __pragma(warning(push)) __pragma(warning(disable:4996))
78 #        define FF_ENABLE_DEPRECATION_WARNINGS  __pragma(warning(pop))
79 #    else
80 #        define FF_DISABLE_DEPRECATION_WARNINGS _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
81 #        define FF_ENABLE_DEPRECATION_WARNINGS  _Pragma("GCC diagnostic pop")
82 #    endif
83 #else
84 #    define FF_DISABLE_DEPRECATION_WARNINGS
85 #    define FF_ENABLE_DEPRECATION_WARNINGS
86 #endif
87 
88 
89 #define FF_MEMORY_POISON 0x2a
90 
91 #define MAKE_ACCESSORS(str, name, type, field) \
92     type av_##name##_get_##field(const str *s) { return s->field; } \
93     void av_##name##_set_##field(str *s, type v) { s->field = v; }
94 
95 // Some broken preprocessors need a second expansion
96 // to be forced to tokenize __VA_ARGS__
97 #define E1(x) x
98 
99 /* Check if the hard coded offset of a struct member still matches reality.
100  * Induce a compilation failure if not.
101  */
102 #define AV_CHECK_OFFSET(s, m, o) struct check_##o {    \
103         int x_##o[offsetof(s, m) == o? 1: -1];         \
104     }
105 
106 #define LOCAL_ALIGNED_A(a, t, v, s, o, ...)             \
107     uint8_t la_##v[sizeof(t s o) + (a)];                \
108     t (*v) o = (void *)FFALIGN((uintptr_t)la_##v, a)
109 
110 #define LOCAL_ALIGNED_D(a, t, v, s, o, ...)             \
111     DECLARE_ALIGNED(a, t, la_##v) s o;                  \
112     t (*v) o = la_##v
113 
114 #define LOCAL_ALIGNED(a, t, v, ...) LOCAL_ALIGNED_##a(t, v, __VA_ARGS__)
115 
116 #if HAVE_LOCAL_ALIGNED
117 #   define LOCAL_ALIGNED_4(t, v, ...) E1(LOCAL_ALIGNED_D(4, t, v, __VA_ARGS__,,))
118 #else
119 #   define LOCAL_ALIGNED_4(t, v, ...) E1(LOCAL_ALIGNED_A(4, t, v, __VA_ARGS__,,))
120 #endif
121 
122 #if HAVE_LOCAL_ALIGNED
123 #   define LOCAL_ALIGNED_8(t, v, ...) E1(LOCAL_ALIGNED_D(8, t, v, __VA_ARGS__,,))
124 #else
125 #   define LOCAL_ALIGNED_8(t, v, ...) E1(LOCAL_ALIGNED_A(8, t, v, __VA_ARGS__,,))
126 #endif
127 
128 #if HAVE_LOCAL_ALIGNED
129 #   define LOCAL_ALIGNED_16(t, v, ...) E1(LOCAL_ALIGNED_D(16, t, v, __VA_ARGS__,,))
130 #else
131 #   define LOCAL_ALIGNED_16(t, v, ...) E1(LOCAL_ALIGNED_A(16, t, v, __VA_ARGS__,,))
132 #endif
133 
134 #if HAVE_LOCAL_ALIGNED
135 #   define LOCAL_ALIGNED_32(t, v, ...) E1(LOCAL_ALIGNED_D(32, t, v, __VA_ARGS__,,))
136 #else
137 #   define LOCAL_ALIGNED_32(t, v, ...) E1(LOCAL_ALIGNED_A(32, t, v, __VA_ARGS__,,))
138 #endif
139 
140 #define FF_ALLOC_TYPED_ARRAY(p, nelem)  (p = av_malloc_array(nelem, sizeof(*p)))
141 #define FF_ALLOCZ_TYPED_ARRAY(p, nelem) (p = av_mallocz_array(nelem, sizeof(*p)))
142 
143 #include "libm.h"
144 
145 /**
146  * Return NULL if CONFIG_SMALL is true, otherwise the argument
147  * without modification. Used to disable the definition of strings
148  * (for example AVCodec long_names).
149  */
150 #if CONFIG_SMALL
151 #   define NULL_IF_CONFIG_SMALL(x) NULL
152 #else
153 #   define NULL_IF_CONFIG_SMALL(x) x
154 #endif
155 
156 /**
157  * Define a function with only the non-default version specified.
158  *
159  * On systems with ELF shared libraries, all symbols exported from
160  * FFmpeg libraries are tagged with the name and major version of the
161  * library to which they belong.  If a function is moved from one
162  * library to another, a wrapper must be retained in the original
163  * location to preserve binary compatibility.
164  *
165  * Functions defined with this macro will never be used to resolve
166  * symbols by the build-time linker.
167  *
168  * @param type return type of function
169  * @param name name of function
170  * @param args argument list of function
171  * @param ver  version tag to assign function
172  */
173 #if HAVE_SYMVER_ASM_LABEL
174 #   define FF_SYMVER(type, name, args, ver)                     \
175     type ff_##name args __asm__ (EXTERN_PREFIX #name "@" ver);  \
176     type ff_##name args
177 #elif HAVE_SYMVER_GNU_ASM
178 #   define FF_SYMVER(type, name, args, ver)                             \
179     __asm__ (".symver ff_" #name "," EXTERN_PREFIX #name "@" ver);      \
180     type ff_##name args;                                                \
181     type ff_##name args
182 #endif
183 
184 /**
185  * Return NULL if a threading library has not been enabled.
186  * Used to disable threading functions in AVCodec definitions
187  * when not needed.
188  */
189 #if HAVE_THREADS
190 #   define ONLY_IF_THREADS_ENABLED(x) x
191 #else
192 #   define ONLY_IF_THREADS_ENABLED(x) NULL
193 #endif
194 
195 /**
196  * Log a generic warning message about a missing feature.
197  *
198  * @param[in] avc a pointer to an arbitrary struct of which the first
199  *                field is a pointer to an AVClass struct
200  * @param[in] msg string containing the name of the missing feature
201  */
202 #if defined(CHROMIUM_NO_LOGGING)
203 #define avpriv_report_missing_feature(...)
204 #else
205 void avpriv_report_missing_feature(void *avc,
206                                    const char *msg, ...) av_printf_format(2, 3);
207 #endif
208 
209 /**
210  * Log a generic warning message about a missing feature.
211  * Additionally request that a sample showcasing the feature be uploaded.
212  *
213  * @param[in] avc a pointer to an arbitrary struct of which the first field is
214  *                a pointer to an AVClass struct
215  * @param[in] msg string containing the name of the missing feature
216  */
217 #if defined(CHROMIUM_NO_LOGGING)
218 #define avpriv_request_sample(...)
219 #else
220 void avpriv_request_sample(void *avc,
221                            const char *msg, ...) av_printf_format(2, 3);
222 #endif
223 
224 #if HAVE_LIBC_MSVCRT
225 #include <crtversion.h>
226 #if defined(_VC_CRT_MAJOR_VERSION) && _VC_CRT_MAJOR_VERSION < 14
227 #pragma comment(linker, "/include:" EXTERN_PREFIX "avpriv_strtod")
228 #pragma comment(linker, "/include:" EXTERN_PREFIX "avpriv_snprintf")
229 #endif
230 
231 #define avpriv_open ff_open
232 #define avpriv_tempfile ff_tempfile
233 #define PTRDIFF_SPECIFIER "Id"
234 #define SIZE_SPECIFIER "Iu"
235 #else
236 #define PTRDIFF_SPECIFIER "td"
237 #define SIZE_SPECIFIER "zu"
238 #endif
239 
240 #ifdef DEBUG
241 #   define ff_dlog(ctx, ...) av_log(ctx, AV_LOG_DEBUG, __VA_ARGS__)
242 #else
243 #   define ff_dlog(ctx, ...) do { if (0) av_log(ctx, AV_LOG_DEBUG, __VA_ARGS__); } while (0)
244 #endif
245 
246 // For debuging we use signed operations so overflows can be detected (by ubsan)
247 // For production we use unsigned so there are no undefined operations
248 #ifdef CHECKED
249 #define SUINT   int
250 #define SUINT32 int32_t
251 #else
252 #define SUINT   unsigned
253 #define SUINT32 uint32_t
254 #endif
255 
256 /**
257  * Clip and convert a double value into the long long amin-amax range.
258  * This function is needed because conversion of floating point to integers when
259  * it does not fit in the integer's representation does not necessarily saturate
260  * correctly (usually converted to a cvttsd2si on x86) which saturates numbers
261  * > INT64_MAX to INT64_MIN. The standard marks such conversions as undefined
262  * behavior, allowing this sort of mathematically bogus conversions. This provides
263  * a safe alternative that is slower obviously but assures safety and better
264  * mathematical behavior.
265  * @param a value to clip
266  * @param amin minimum value of the clip range
267  * @param amax maximum value of the clip range
268  * @return clipped value
269  */
ff_rint64_clip(double a,int64_t amin,int64_t amax)270 static av_always_inline av_const int64_t ff_rint64_clip(double a, int64_t amin, int64_t amax)
271 {
272     int64_t res;
273 #if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
274     if (amin > amax) abort();
275 #endif
276     // INT64_MAX+1,INT64_MIN are exactly representable as IEEE doubles
277     // do range checks first
278     if (a >=  9223372036854775808.0)
279         return amax;
280     if (a <= -9223372036854775808.0)
281         return amin;
282 
283     // safe to call llrint and clip accordingly
284     res = llrint(a);
285     if (res > amax)
286         return amax;
287     if (res < amin)
288         return amin;
289     return res;
290 }
291 
292 /**
293  * A wrapper for open() setting O_CLOEXEC.
294  */
295 av_warn_unused_result
296 int avpriv_open(const char *filename, int flags, ...);
297 
298 /**
299  * Wrapper to work around the lack of mkstemp() on mingw.
300  * Also, tries to create file in /tmp first, if possible.
301  * *prefix can be a character constant; *filename will be allocated internally.
302  * @return file descriptor of opened file (or negative value corresponding to an
303  * AVERROR code on error)
304  * and opened file name in **filename.
305  * @note On very old libcs it is necessary to set a secure umask before
306  *       calling this, av_tempfile() can't call umask itself as it is used in
307  *       libraries and could interfere with the calling application.
308  */
309 int avpriv_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx);
310 
311 int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt);
312 
avpriv_mirror(int x,int w)313 static av_always_inline av_const int avpriv_mirror(int x, int w)
314 {
315     if (!w)
316         return 0;
317 
318     while ((unsigned)x > (unsigned)w) {
319         x = -x;
320         if (x < 0)
321             x += 2 * w;
322     }
323     return x;
324 }
325 
326 void ff_check_pixfmt_descriptors(void);
327 
328 /**
329  * Set a dictionary value to an ISO-8601 compliant timestamp string.
330  *
331  * @param dict pointer to a pointer to a dictionary struct. If *dict is NULL
332  *             a dictionary struct is allocated and put in *dict.
333  * @param key metadata key
334  * @param timestamp unix timestamp in microseconds
335  * @return <0 on error
336  */
337 int avpriv_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp);
338 
339 // Helper macro for AV_PIX_FMT_FLAG_PSEUDOPAL deprecation. Code inside FFmpeg
340 // should always use FF_PSEUDOPAL. Once the public API flag gets removed, all
341 // code using it is dead code.
342 #if FF_API_PSEUDOPAL
343 #define FF_PSEUDOPAL AV_PIX_FMT_FLAG_PSEUDOPAL
344 #else
345 #define FF_PSEUDOPAL 0
346 #endif
347 
348 #endif /* AVUTIL_INTERNAL_H */
349