internal.h 6.34 KB
Newer Older
1 2 3
/*
 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
 *
4
 * This file is part of Libav.
5
 *
6
 * Libav is free software; you can redistribute it and/or
7 8
 * modify it under the terms of the GNU Lesser General Public
 * 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
 * Libav is distributed in the hope that it will be useful,
12 13 14 15 16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with Libav; if not, write to the Free Software
18 19 20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

21
/**
22
 * @file
23
 * common internal API header
24 25
 */

26 27
#ifndef AVUTIL_INTERNAL_H
#define AVUTIL_INTERNAL_H
28

29 30 31 32
#if !defined(DEBUG) && !defined(NDEBUG)
#    define NDEBUG
#endif

Diego Biurrun's avatar
Diego Biurrun committed
33
#include <limits.h>
34
#include <stdint.h>
35 36
#include <stddef.h>
#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 && AV_GCC_VERSION_AT_LEAST(4,2)
43 44 45 46 47 48
#    define attribute_align_arg __attribute__((force_align_arg_pointer))
#else
#    define attribute_align_arg
#endif
#endif

49
#ifndef INT16_MIN
50
#define INT16_MIN       (-0x7fff - 1)
51 52 53 54 55 56 57
#endif

#ifndef INT16_MAX
#define INT16_MAX       0x7fff
#endif

#ifndef INT32_MIN
58
#define INT32_MIN       (-0x7fffffff - 1)
59 60 61 62 63 64 65 66 67 68 69
#endif

#ifndef INT32_MAX
#define INT32_MAX       0x7fffffff
#endif

#ifndef UINT32_MAX
#define UINT32_MAX      0xffffffff
#endif

#ifndef INT64_MIN
70
#define INT64_MIN       (-0x7fffffffffffffffLL - 1)
71 72 73
#endif

#ifndef INT64_MAX
74
#define INT64_MAX INT64_C(9223372036854775807)
75 76 77
#endif

#ifndef UINT64_MAX
78
#define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
79 80 81
#endif

#ifndef INT_BIT
82
#    define INT_BIT (CHAR_BIT * sizeof(int))
83 84
#endif

85
#ifndef offsetof
86
#    define offsetof(T, F) ((unsigned int)((char *)&((T *)0)->F))
87 88
#endif

89 90 91
/* Use to export labels from asm. */
#define LABEL_MANGLE(a) EXTERN_PREFIX #a

92
// Use rip-relative addressing if compiling PIC code on x86-64.
93
#if ARCH_X86_64 && defined(PIC)
94
#    define LOCAL_MANGLE(a) #a "(%%rip)"
95
#else
96
#    define LOCAL_MANGLE(a) #a
Måns Rullgård's avatar
Måns Rullgård committed
97
#endif
98

99 100
#define MANGLE(a) EXTERN_PREFIX LOCAL_MANGLE(a)

101 102
/* debug stuff */

Måns Rullgård's avatar
Måns Rullgård committed
103
#define av_abort()      do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
104

Måns Rullgård's avatar
Måns Rullgård committed
105 106
/* math */

107
#if ARCH_X86
108
#define MASK_ABS(mask, level)\
109
            __asm__ volatile(\
110
                "cltd                   \n\t"\
111 112 113 114 115 116
                "xorl %1, %0            \n\t"\
                "subl %1, %0            \n\t"\
                : "+a" (level), "=&d" (mask)\
            );
#else
#define MASK_ABS(mask, level)\
117 118
            mask  = level >> 31;\
            level = (level ^ mask) - mask;
119 120
#endif

121
/* avoid usage of dangerous/inappropriate system functions */
122
#undef  malloc
123
#define malloc please_use_av_malloc
124
#undef  free
125
#define free please_use_av_free
126
#undef  realloc
127
#define realloc please_use_av_realloc
128
#undef  time
129
#define time time_is_forbidden_due_to_security_issues
130
#undef  rand
131
#define rand rand_is_forbidden_due_to_state_trashing_use_av_lfg_get
132
#undef  srand
133
#define srand srand_is_forbidden_due_to_state_trashing_use_av_lfg_init
134
#undef  random
135
#define random random_is_forbidden_due_to_state_trashing_use_av_lfg_get
136
#undef  sprintf
137
#define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
138
#undef  strcat
Reimar Döffinger's avatar
Reimar Döffinger committed
139
#define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
140 141
#undef  strncpy
#define strncpy strncpy_is_forbidden_due_to_security_issues_use_av_strlcpy
142
#undef  exit
Michael Niedermayer's avatar
Michael Niedermayer committed
143
#define exit exit_is_forbidden
144
#ifndef LIBAVFORMAT_BUILD
145
#undef  printf
146
#define printf please_use_av_log_instead_of_printf
147
#undef  fprintf
148
#define fprintf please_use_av_log_instead_of_fprintf
149
#undef  puts
150
#define puts please_use_av_log_instead_of_puts
151 152
#undef  perror
#define perror please_use_av_log_instead_of_perror
153 154
#endif

155
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)\
Ramiro Polla's avatar
Ramiro Polla committed
156
{\
157 158
    p = av_malloc(size);\
    if (p == NULL && (size) != 0) {\
159 160
        av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
        goto label;\
Ramiro Polla's avatar
Ramiro Polla committed
161 162 163
    }\
}

164
#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)\
165
{\
166 167
    p = av_mallocz(size);\
    if (p == NULL && (size) != 0) {\
168 169
        av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
        goto label;\
170 171 172
    }\
}

173
#include "libm.h"
174

175
/**
176
 * Return NULL if CONFIG_SMALL is true, otherwise the argument
177
 * without modification. Used to disable the definition of strings
178 179 180 181 182 183 184 185
 * (for example AVCodec long_names).
 */
#if CONFIG_SMALL
#   define NULL_IF_CONFIG_SMALL(x) NULL
#else
#   define NULL_IF_CONFIG_SMALL(x) x
#endif

186 187

/**
188 189 190
 * Define a function with only the non-default version specified.
 *
 * On systems with ELF shared libraries, all symbols exported from
191
 * Libav libraries are tagged with the name and major version of the
192 193 194 195 196 197 198 199 200 201 202
 * library to which they belong.  If a function is moved from one
 * library to another, a wrapper must be retained in the original
 * location to preserve binary compatibility.
 *
 * Functions defined with this macro will never be used to resolve
 * symbols by the build-time linker.
 *
 * @param type return type of function
 * @param name name of function
 * @param args argument list of function
 * @param ver  version tag to assign function
203
 */
204
#if HAVE_SYMVER_ASM_LABEL
205 206
#   define FF_SYMVER(type, name, args, ver)                     \
    type ff_##name args __asm__ (EXTERN_PREFIX #name "@" ver);  \
207 208
    type ff_##name args
#elif HAVE_SYMVER_GNU_ASM
209 210 211
#   define FF_SYMVER(type, name, args, ver)                             \
    __asm__ (".symver ff_" #name "," EXTERN_PREFIX #name "@" ver);      \
    type ff_##name args;                                                \
212 213 214
    type ff_##name args
#endif

215 216 217 218 219 220 221 222 223 224 225
/**
 * Returns NULL if a threading library has not been enabled.
 * Used to disable threading functions in AVCodec definitions
 * when not needed.
 */
#if HAVE_THREADS
#   define ONLY_IF_THREADS_ENABLED(x) x
#else
#   define ONLY_IF_THREADS_ENABLED(x) NULL
#endif

226 227 228 229 230 231 232 233 234 235 236 237 238 239
#if HAVE_MMX
/**
 * Empty mmx state.
 * this must be called between any dsp function and float/double code.
 * for example sin(); dsp->idct_put(); emms_c(); cos()
 */
static av_always_inline void emms_c(void)
{
    __asm__ volatile ("emms" ::: "memory");
}
#else /* HAVE_MMX */
#define emms_c()
#endif /* HAVE_MMX */

240
#endif /* AVUTIL_INTERNAL_H */