internal.h 6.59 KB
Newer Older
1 2 3
/*
 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
 *
4 5 6
 * This file is part of FFmpeg.
 *
 * FFmpeg 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
 * FFmpeg 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 FFmpeg; 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
#include "cpu.h"
41 42 43 44 45 46
#include "dict.h"

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

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

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

#ifndef INT16_MAX
#define INT16_MAX       0x7fff
#endif

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

#ifndef INT32_MAX
#define INT32_MAX       0x7fffffff
#endif

#ifndef UINT32_MAX
#define UINT32_MAX      0xffffffff
#endif

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

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

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

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

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

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

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

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

108 109
/* debug stuff */

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

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

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

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

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

173
#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)\
174
{\
175 176
    p = av_mallocz(size);\
    if (p == NULL && (size) != 0) {\
177 178
        av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
        goto label;\
179 180 181
    }\
}

182
#include "libm.h"
183

184
/**
185
 * Return NULL if CONFIG_SMALL is true, otherwise the argument
186
 * without modification. Used to disable the definition of strings
187 188 189 190 191 192 193 194
 * (for example AVCodec long_names).
 */
#if CONFIG_SMALL
#   define NULL_IF_CONFIG_SMALL(x) NULL
#else
#   define NULL_IF_CONFIG_SMALL(x) x
#endif

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

223
/**
224
 * Return NULL if a threading library has not been enabled.
225 226 227 228 229 230 231 232 233
 * 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

234 235 236 237 238 239 240 241
#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)
{
242 243
    if(av_get_cpu_flags() & AV_CPU_FLAG_MMX)
        __asm__ volatile ("emms" ::: "memory");
244 245 246 247 248
}
#else /* HAVE_MMX */
#define emms_c()
#endif /* HAVE_MMX */

249
#endif /* AVUTIL_INTERNAL_H */