internal.h 6.79 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 libavutil/internal.h
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's avatar
diego committed
33
#include <limits.h>
34
#include <stdint.h>
35 36
#include <stddef.h>
#include <assert.h>
37
#include "config.h"
38
#include "common.h"
39
#include "mem.h"
40
#include "timer.h"
41

42
#ifndef attribute_align_arg
43
#if (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(4,2)
44 45 46 47 48 49
#    define attribute_align_arg __attribute__((force_align_arg_pointer))
#else
#    define attribute_align_arg
#endif
#endif

50
#ifndef attribute_used
51
#if AV_GCC_VERSION_AT_LEAST(3,1)
52 53 54 55 56 57
#    define attribute_used __attribute__((used))
#else
#    define attribute_used
#endif
#endif

58 59 60 61 62 63 64 65
#ifndef av_alias
#if HAVE_ATTRIBUTE_MAY_ALIAS
#   define av_alias __attribute__((may_alias))
#else
#   define av_alias
#endif
#endif

mru's avatar
mru committed
66
#ifndef INT16_MIN
diego's avatar
diego committed
67
#define INT16_MIN       (-0x7fff - 1)
mru's avatar
mru committed
68 69 70 71 72 73 74
#endif

#ifndef INT16_MAX
#define INT16_MAX       0x7fff
#endif

#ifndef INT32_MIN
diego's avatar
diego committed
75
#define INT32_MIN       (-0x7fffffff - 1)
mru's avatar
mru committed
76 77 78 79 80 81 82 83 84 85 86
#endif

#ifndef INT32_MAX
#define INT32_MAX       0x7fffffff
#endif

#ifndef UINT32_MAX
#define UINT32_MAX      0xffffffff
#endif

#ifndef INT64_MIN
diego's avatar
diego committed
87
#define INT64_MIN       (-0x7fffffffffffffffLL - 1)
mru's avatar
mru committed
88 89 90
#endif

#ifndef INT64_MAX
91
#define INT64_MAX INT64_C(9223372036854775807)
mru's avatar
mru committed
92 93 94
#endif

#ifndef UINT64_MAX
95
#define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
mru's avatar
mru committed
96 97 98
#endif

#ifndef INT_BIT
99
#    define INT_BIT (CHAR_BIT * sizeof(int))
mru's avatar
mru committed
100 101
#endif

102
#ifndef offsetof
diego's avatar
diego committed
103
#    define offsetof(T, F) ((unsigned int)((char *)&((T *)0)->F))
104 105
#endif

106 107 108
/* Use to export labels from asm. */
#define LABEL_MANGLE(a) EXTERN_PREFIX #a

109
// Use rip-relative addressing if compiling PIC code on x86-64.
110
#if ARCH_X86_64 && defined(PIC)
111
#    define LOCAL_MANGLE(a) #a "(%%rip)"
112
#else
113
#    define LOCAL_MANGLE(a) #a
mru's avatar
mru committed
114
#endif
115

116 117
#define MANGLE(a) EXTERN_PREFIX LOCAL_MANGLE(a)

118 119 120
/* debug stuff */

/* dprintf macros */
mru's avatar
mru committed
121
#ifdef DEBUG
mbardiaux's avatar
mbardiaux committed
122
#    define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
mru's avatar
mru committed
123
#else
mbardiaux's avatar
mbardiaux committed
124
#    define dprintf(pctx, ...)
mru's avatar
mru committed
125
#endif
126

mru's avatar
mru committed
127
#define av_abort()      do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
128

mru's avatar
mru committed
129 130
/* math */

michael's avatar
michael committed
131
extern const uint8_t ff_sqrt_tab[256];
132

zuxy's avatar
zuxy committed
133
static inline av_const unsigned int ff_sqrt(unsigned int a)
134
{
michael's avatar
michael committed
135 136
    unsigned int b;

diego's avatar
diego committed
137 138
    if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4;
    else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2;
139
#if !CONFIG_SMALL
diego's avatar
diego committed
140 141
    else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1;
    else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8]   ;
michael's avatar
michael committed
142
#endif
diego's avatar
diego committed
143 144 145 146 147
    else {
        int s = av_log2_16bit(a >> 16) >> 1;
        unsigned int c = a >> (s + 2);
        b = ff_sqrt_tab[c >> (s + 8)];
        b = FASTDIV(c,b) + (b << s);
148
    }
michael's avatar
michael committed
149

diego's avatar
diego committed
150
    return b - (a < b * b);
151 152
}

153
#if ARCH_X86
154
#define MASK_ABS(mask, level)\
155
            __asm__ volatile(\
156
                "cltd                   \n\t"\
157 158 159 160 161 162
                "xorl %1, %0            \n\t"\
                "subl %1, %0            \n\t"\
                : "+a" (level), "=&d" (mask)\
            );
#else
#define MASK_ABS(mask, level)\
diego's avatar
diego committed
163 164
            mask  = level >> 31;\
            level = (level ^ mask) - mask;
165 166
#endif

167
/* avoid usage of dangerous/inappropriate system functions */
168
#undef  malloc
169
#define malloc please_use_av_malloc
170
#undef  free
171
#define free please_use_av_free
172
#undef  realloc
173
#define realloc please_use_av_realloc
174
#undef  time
175
#define time time_is_forbidden_due_to_security_issues
176
#undef  rand
177
#define rand rand_is_forbidden_due_to_state_trashing_use_av_lfg_get
178
#undef  srand
179
#define srand srand_is_forbidden_due_to_state_trashing_use_av_lfg_init
180
#undef  random
181
#define random random_is_forbidden_due_to_state_trashing_use_av_lfg_get
182
#undef  sprintf
183
#define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
184
#undef  strcat
reimar's avatar
reimar committed
185
#define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
186
#undef  exit
michael's avatar
michael committed
187
#define exit exit_is_forbidden
188
#ifndef LIBAVFORMAT_BUILD
189
#undef  printf
190
#define printf please_use_av_log_instead_of_printf
191
#undef  fprintf
192
#define fprintf please_use_av_log_instead_of_fprintf
193
#undef  puts
194
#define puts please_use_av_log_instead_of_puts
195 196
#undef  perror
#define perror please_use_av_log_instead_of_perror
197 198
#endif

199
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)\
ramiro's avatar
ramiro committed
200
{\
diego's avatar
diego committed
201 202
    p = av_malloc(size);\
    if (p == NULL && (size) != 0) {\
203 204
        av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
        goto label;\
ramiro's avatar
ramiro committed
205 206 207
    }\
}

208
#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)\
209
{\
diego's avatar
diego committed
210 211
    p = av_mallocz(size);\
    if (p == NULL && (size) != 0) {\
212 213
        av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
        goto label;\
214 215 216
    }\
}

217
#if !HAVE_EXP2
218 219
#undef exp2
#define exp2(x) exp((x) * 0.693147180559945)
220 221 222
#endif /* HAVE_EXP2 */

#if !HAVE_EXP2F
223 224
#undef exp2f
#define exp2f(x) exp2(x)
225 226
#endif /* HAVE_EXP2F */

227
#if !HAVE_LLRINT
zuxy's avatar
zuxy committed
228
static av_always_inline av_const long long llrint(double x)
229 230 231 232
{
    return rint(x);
}
#endif /* HAVE_LLRINT */
233 234

#if !HAVE_LOG2
235 236
#undef log2
#define log2(x) (log(x) * 1.44269504088896340736)
237
#endif /* HAVE_LOG2 */
238 239

#if !HAVE_LOG2F
240 241
#undef log2f
#define log2f(x) log2(x)
242
#endif /* HAVE_LOG2F */
243

244
#if !HAVE_LRINT
zuxy's avatar
zuxy committed
245
static av_always_inline av_const long int lrint(double x)
246 247 248 249 250
{
    return rint(x);
}
#endif /* HAVE_LRINT */

251
#if !HAVE_LRINTF
zuxy's avatar
zuxy committed
252
static av_always_inline av_const long int lrintf(float x)
253 254 255 256 257
{
    return (int)(rint(x));
}
#endif /* HAVE_LRINTF */

258
#if !HAVE_ROUND
zuxy's avatar
zuxy committed
259
static av_always_inline av_const double round(double x)
260 261 262 263 264
{
    return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
}
#endif /* HAVE_ROUND */

265
#if !HAVE_ROUNDF
zuxy's avatar
zuxy committed
266
static av_always_inline av_const float roundf(float x)
267 268 269 270 271
{
    return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
}
#endif /* HAVE_ROUNDF */

272
#if !HAVE_TRUNCF
ramiro's avatar
ramiro committed
273 274 275 276 277 278
static av_always_inline av_const float truncf(float x)
{
    return (x > 0) ? floor(x) : ceil(x);
}
#endif /* HAVE_TRUNCF */

279
/**
280 281
 * Returns NULL if CONFIG_SMALL is true, otherwise the argument
 * without modification. Used to disable the definition of strings
282 283 284 285 286 287 288 289
 * (for example AVCodec long_names).
 */
#if CONFIG_SMALL
#   define NULL_IF_CONFIG_SMALL(x) NULL
#else
#   define NULL_IF_CONFIG_SMALL(x) x
#endif

290
#endif /* AVUTIL_INTERNAL_H */