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#include "cpu.h"
41#include "dict.h"
42#include "version.h"
43
44#if ARCH_X86
45#   include "x86/emms.h"
46#endif
47
48#ifndef emms_c
49#   define emms_c()
50#endif
51
52#ifndef attribute_align_arg
53#if ARCH_X86_32 && AV_GCC_VERSION_AT_LEAST(4,2)
54#    define attribute_align_arg __attribute__((force_align_arg_pointer))
55#else
56#    define attribute_align_arg
57#endif
58#endif
59
60#if defined(_MSC_VER) && CONFIG_SHARED
61#    define av_export __declspec(dllimport)
62#else
63#    define av_export
64#endif
65
66#if HAVE_PRAGMA_DEPRECATED
67#    if defined(__ICL) || defined (__INTEL_COMPILER)
68#        define FF_DISABLE_DEPRECATION_WARNINGS __pragma(warning(push)) __pragma(warning(disable:1478))
69#        define FF_ENABLE_DEPRECATION_WARNINGS  __pragma(warning(pop))
70#    elif defined(_MSC_VER)
71#        define FF_DISABLE_DEPRECATION_WARNINGS __pragma(warning(push)) __pragma(warning(disable:4996))
72#        define FF_ENABLE_DEPRECATION_WARNINGS  __pragma(warning(pop))
73#    else
74#        define FF_DISABLE_DEPRECATION_WARNINGS _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
75#        define FF_ENABLE_DEPRECATION_WARNINGS  _Pragma("GCC diagnostic warning \"-Wdeprecated-declarations\"")
76#    endif
77#else
78#    define FF_DISABLE_DEPRECATION_WARNINGS
79#    define FF_ENABLE_DEPRECATION_WARNINGS
80#endif
81
82#ifndef INT_BIT
83#    define INT_BIT (CHAR_BIT * sizeof(int))
84#endif
85
86#define FF_MEMORY_POISON 0x2a
87
88#define MAKE_ACCESSORS(str, name, type, field) \
89    type av_##name##_get_##field(const str *s) { return s->field; } \
90    void av_##name##_set_##field(str *s, type v) { s->field = v; }
91
92// Some broken preprocessors need a second expansion
93// to be forced to tokenize __VA_ARGS__
94#define E1(x) x
95
96/* Check if the hard coded offset of a struct member still matches reality.
97 * Induce a compilation failure if not.
98 */
99#define AV_CHECK_OFFSET(s, m, o) struct check_##o {    \
100        int x_##o[offsetof(s, m) == o? 1: -1];         \
101    }
102
103#define LOCAL_ALIGNED_A(a, t, v, s, o, ...)             \
104    uint8_t la_##v[sizeof(t s o) + (a)];                \
105    t (*v) o = (void *)FFALIGN((uintptr_t)la_##v, a)
106
107#define LOCAL_ALIGNED_D(a, t, v, s, o, ...)             \
108    DECLARE_ALIGNED(a, t, la_##v) s o;                  \
109    t (*v) o = la_##v
110
111#define LOCAL_ALIGNED(a, t, v, ...) E1(LOCAL_ALIGNED_A(a, t, v, __VA_ARGS__,,))
112
113#if HAVE_LOCAL_ALIGNED_8
114#   define LOCAL_ALIGNED_8(t, v, ...) E1(LOCAL_ALIGNED_D(8, t, v, __VA_ARGS__,,))
115#else
116#   define LOCAL_ALIGNED_8(t, v, ...) LOCAL_ALIGNED(8, t, v, __VA_ARGS__)
117#endif
118
119#if HAVE_LOCAL_ALIGNED_16
120#   define LOCAL_ALIGNED_16(t, v, ...) E1(LOCAL_ALIGNED_D(16, t, v, __VA_ARGS__,,))
121#else
122#   define LOCAL_ALIGNED_16(t, v, ...) LOCAL_ALIGNED(16, t, v, __VA_ARGS__)
123#endif
124
125#if HAVE_LOCAL_ALIGNED_32
126#   define LOCAL_ALIGNED_32(t, v, ...) E1(LOCAL_ALIGNED_D(32, t, v, __VA_ARGS__,,))
127#else
128#   define LOCAL_ALIGNED_32(t, v, ...) LOCAL_ALIGNED(32, t, v, __VA_ARGS__)
129#endif
130
131#define FF_ALLOC_OR_GOTO(ctx, p, size, label)\
132{\
133    p = av_malloc(size);\
134    if (p == NULL && (size) != 0) {\
135        av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
136        goto label;\
137    }\
138}
139
140#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)\
141{\
142    p = av_mallocz(size);\
143    if (p == NULL && (size) != 0) {\
144        av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
145        goto label;\
146    }\
147}
148
149#define FF_ALLOC_ARRAY_OR_GOTO(ctx, p, nelem, elsize, label)\
150{\
151    p = av_malloc_array(nelem, elsize);\
152    if (p == NULL) {\
153        av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
154        goto label;\
155    }\
156}
157
158#define FF_ALLOCZ_ARRAY_OR_GOTO(ctx, p, nelem, elsize, label)\
159{\
160    p = av_mallocz_array(nelem, elsize);\
161    if (p == NULL) {\
162        av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
163        goto label;\
164    }\
165}
166
167#include "libm.h"
168
169#if defined(_MSC_VER)
170#pragma comment(linker, "/include:"EXTERN_PREFIX"avpriv_strtod")
171#pragma comment(linker, "/include:"EXTERN_PREFIX"avpriv_snprintf")
172#endif
173
174/**
175 * Return NULL if CONFIG_SMALL is true, otherwise the argument
176 * without modification. Used to disable the definition of strings
177 * (for example AVCodec long_names).
178 */
179#if CONFIG_SMALL
180#   define NULL_IF_CONFIG_SMALL(x) NULL
181#else
182#   define NULL_IF_CONFIG_SMALL(x) x
183#endif
184
185/**
186 * Define a function with only the non-default version specified.
187 *
188 * On systems with ELF shared libraries, all symbols exported from
189 * FFmpeg libraries are tagged with the name and major version of the
190 * library to which they belong.  If a function is moved from one
191 * library to another, a wrapper must be retained in the original
192 * location to preserve binary compatibility.
193 *
194 * Functions defined with this macro will never be used to resolve
195 * symbols by the build-time linker.
196 *
197 * @param type return type of function
198 * @param name name of function
199 * @param args argument list of function
200 * @param ver  version tag to assign function
201 */
202#if HAVE_SYMVER_ASM_LABEL
203#   define FF_SYMVER(type, name, args, ver)                     \
204    type ff_##name args __asm__ (EXTERN_PREFIX #name "@" ver);  \
205    type ff_##name args
206#elif HAVE_SYMVER_GNU_ASM
207#   define FF_SYMVER(type, name, args, ver)                             \
208    __asm__ (".symver ff_" #name "," EXTERN_PREFIX #name "@" ver);      \
209    type ff_##name args;                                                \
210    type ff_##name args
211#endif
212
213/**
214 * Return NULL if a threading library has not been enabled.
215 * Used to disable threading functions in AVCodec definitions
216 * when not needed.
217 */
218#if HAVE_THREADS
219#   define ONLY_IF_THREADS_ENABLED(x) x
220#else
221#   define ONLY_IF_THREADS_ENABLED(x) NULL
222#endif
223
224/**
225 * Log a generic warning message about a missing feature.
226 *
227 * @param[in] avc a pointer to an arbitrary struct of which the first
228 *                field is a pointer to an AVClass struct
229 * @param[in] msg string containing the name of the missing feature
230 */
231void avpriv_report_missing_feature(void *avc,
232                                   const char *msg, ...) av_printf_format(2, 3);
233
234/**
235 * Log a generic warning message about a missing feature.
236 * Additionally request that a sample showcasing the feature be uploaded.
237 *
238 * @param[in] avc a pointer to an arbitrary struct of which the first field is
239 *                a pointer to an AVClass struct
240 * @param[in] msg string containing the name of the missing feature
241 */
242void avpriv_request_sample(void *avc,
243                           const char *msg, ...) av_printf_format(2, 3);
244
245#if HAVE_LIBC_MSVCRT
246#define avpriv_open ff_open
247#define PTRDIFF_SPECIFIER "Id"
248#define SIZE_SPECIFIER "Iu"
249#else
250#define PTRDIFF_SPECIFIER "td"
251#define SIZE_SPECIFIER "zu"
252#endif
253
254/**
255 * A wrapper for open() setting O_CLOEXEC.
256 */
257int avpriv_open(const char *filename, int flags, ...);
258
259#if FF_API_GET_CHANNEL_LAYOUT_COMPAT
260uint64_t ff_get_channel_layout(const char *name, int compat);
261#endif
262
263#endif /* AVUTIL_INTERNAL_H */
264