internal.h 6.39 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

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 42 43 44 45
#include "dict.h"

struct AVDictionary {
    int count;
    AVDictionaryEntry *elems;
};
46

47
#ifndef attribute_align_arg
48
#if ARCH_X86_32 && AV_GCC_VERSION_AT_LEAST(4,2)
49 50 51 52 53 54
#    define attribute_align_arg __attribute__((force_align_arg_pointer))
#else
#    define attribute_align_arg
#endif
#endif

55
#ifndef INT16_MIN
56
#define INT16_MIN       (-0x7fff - 1)
57 58 59 60 61 62 63
#endif

#ifndef INT16_MAX
#define INT16_MAX       0x7fff
#endif

#ifndef INT32_MIN
64
#define INT32_MIN       (-0x7fffffff - 1)
65 66 67 68 69 70 71 72 73 74 75
#endif

#ifndef INT32_MAX
#define INT32_MAX       0x7fffffff
#endif

#ifndef UINT32_MAX
#define UINT32_MAX      0xffffffff
#endif

#ifndef INT64_MIN
76
#define INT64_MIN       (-0x7fffffffffffffffLL - 1)
77 78 79
#endif

#ifndef INT64_MAX
80
#define INT64_MAX INT64_C(9223372036854775807)
81 82 83
#endif

#ifndef UINT64_MAX
84
#define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
85 86 87
#endif

#ifndef INT_BIT
88
#    define INT_BIT (CHAR_BIT * sizeof(int))
89 90
#endif

91
#ifndef offsetof
92
#    define offsetof(T, F) ((unsigned int)((char *)&((T *)0)->F))
93 94
#endif

95 96 97
/* Use to export labels from asm. */
#define LABEL_MANGLE(a) EXTERN_PREFIX #a

98
// Use rip-relative addressing if compiling PIC code on x86-64.
99
#if ARCH_X86_64 && defined(PIC)
100
#    define LOCAL_MANGLE(a) #a "(%%rip)"
101
#else
102
#    define LOCAL_MANGLE(a) #a
Måns Rullgård's avatar
Måns Rullgård committed
103
#endif
104

105 106
#define MANGLE(a) EXTERN_PREFIX LOCAL_MANGLE(a)

107 108
/* debug stuff */

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

Måns Rullgård's avatar
Måns Rullgård committed
111 112
/* math */

113
#if ARCH_X86
114
#define MASK_ABS(mask, level)\
115
            __asm__ volatile(\
116
                "cltd                   \n\t"\
117 118 119 120 121 122
                "xorl %1, %0            \n\t"\
                "subl %1, %0            \n\t"\
                : "+a" (level), "=&d" (mask)\
            );
#else
#define MASK_ABS(mask, level)\
123 124
            mask  = level >> 31;\
            level = (level ^ mask) - mask;
125 126
#endif

127
/* avoid usage of dangerous/inappropriate system functions */
128
#undef  malloc
129
#define malloc please_use_av_malloc
130
#undef  free
131
#define free please_use_av_free
132
#undef  realloc
133
#define realloc please_use_av_realloc
134
#undef  time
135
#define time time_is_forbidden_due_to_security_issues
136
#undef  rand
137
#define rand rand_is_forbidden_due_to_state_trashing_use_av_lfg_get
138
#undef  srand
139
#define srand srand_is_forbidden_due_to_state_trashing_use_av_lfg_init
140
#undef  random
141
#define random random_is_forbidden_due_to_state_trashing_use_av_lfg_get
142
#undef  sprintf
143
#define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
144
#undef  strcat
145
#define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
146 147
#undef  strncpy
#define strncpy strncpy_is_forbidden_due_to_security_issues_use_av_strlcpy
148
#undef  exit
149
#define exit exit_is_forbidden
150
#undef  printf
151
#define printf please_use_av_log_instead_of_printf
152
#undef  fprintf
153
#define fprintf please_use_av_log_instead_of_fprintf
154
#undef  puts
155
#define puts please_use_av_log_instead_of_puts
156 157
#undef  perror
#define perror please_use_av_log_instead_of_perror
158

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

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

177
#include "libm.h"
178

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

190 191

/**
192 193 194
 * Define a function with only the non-default version specified.
 *
 * On systems with ELF shared libraries, all symbols exported from
195
 * Libav libraries are tagged with the name and major version of the
196 197 198 199 200 201 202 203 204 205 206
 * 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
207
 */
208
#if HAVE_SYMVER_ASM_LABEL
209 210
#   define FF_SYMVER(type, name, args, ver)                     \
    type ff_##name args __asm__ (EXTERN_PREFIX #name "@" ver);  \
211 212
    type ff_##name args
#elif HAVE_SYMVER_GNU_ASM
213 214 215
#   define FF_SYMVER(type, name, args, ver)                             \
    __asm__ (".symver ff_" #name "," EXTERN_PREFIX #name "@" ver);      \
    type ff_##name args;                                                \
216 217 218
    type ff_##name args
#endif

219 220 221 222 223 224 225 226 227 228 229
/**
 * 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

230 231 232 233 234 235 236 237 238 239 240 241 242 243
#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 */

244
#endif /* AVUTIL_INTERNAL_H */