internal.h 8.22 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 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 > 1100) && 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

mru's avatar
mru committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#ifndef INT16_MIN
#define INT16_MIN       (-0x7fff-1)
#endif

#ifndef INT16_MAX
#define INT16_MAX       0x7fff
#endif

#ifndef INT32_MIN
#define INT32_MIN       (-0x7fffffff-1)
#endif

#ifndef INT32_MAX
#define INT32_MAX       0x7fffffff
#endif

#ifndef UINT32_MAX
#define UINT32_MAX      0xffffffff
#endif

#ifndef INT64_MIN
#define INT64_MIN       (-0x7fffffffffffffffLL-1)
#endif

#ifndef INT64_MAX
83
#define INT64_MAX INT64_C(9223372036854775807)
mru's avatar
mru committed
84 85 86
#endif

#ifndef UINT64_MAX
87
#define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
mru's avatar
mru committed
88 89 90 91 92 93 94 95 96 97
#endif

#ifndef INT_BIT
#    if INT_MAX != 2147483647
#        define INT_BIT 64
#    else
#        define INT_BIT 32
#    endif
#endif

98 99 100 101 102
#if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
#    define PIC
#endif

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

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

113 114
#define MANGLE(a) EXTERN_PREFIX LOCAL_MANGLE(a)

115 116 117
/* debug stuff */

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

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

mru's avatar
mru committed
126 127
/* math */

mru's avatar
mru committed
128
extern const uint32_t ff_inverse[256];
129

130
#if ARCH_X86
131 132 133
#    define FASTDIV(a,b) \
    ({\
        int ret,dmy;\
134
        __asm__ volatile(\
135 136
            "mull %3"\
            :"=d"(ret),"=a"(dmy)\
mru's avatar
mru committed
137
            :"1"(a),"g"(ff_inverse[b])\
138 139 140
            );\
        ret;\
    })
141
#elif HAVE_ARMV6
mru's avatar
mru committed
142 143
static inline av_const int FASTDIV(int a, int b)
{
mru's avatar
mru committed
144 145 146 147 148 149
    int r, t;
    __asm__ volatile("cmp     %3, #2               \n\t"
                     "ldr     %1, [%4, %3, lsl #2] \n\t"
                     "lsrle   %0, %2, #1           \n\t"
                     "smmulgt %0, %1, %2           \n\t"
                     : "=&r"(r), "=&r"(t) : "r"(a), "r"(b), "r"(ff_inverse));
mru's avatar
mru committed
150 151
    return r;
}
152
#elif ARCH_ARM
mru's avatar
mru committed
153 154 155 156 157 158 159
static inline av_const int FASTDIV(int a, int b)
{
    int r, t;
    __asm__ volatile ("umull %1, %0, %2, %3"
                      : "=&r"(r), "=&r"(t) : "r"(a), "r"(ff_inverse[b]));
    return r;
}
160
#elif CONFIG_FASTDIV
mru's avatar
mru committed
161
#    define FASTDIV(a,b)   ((uint32_t)((((uint64_t)a)*ff_inverse[b])>>32))
162 163 164 165
#else
#    define FASTDIV(a,b)   ((a)/(b))
#endif

michael's avatar
michael committed
166
extern const uint8_t ff_sqrt_tab[256];
167

zuxy's avatar
zuxy committed
168
static inline av_const unsigned int ff_sqrt(unsigned int a)
169
{
michael's avatar
michael committed
170 171 172 173
    unsigned int b;

    if(a<255) return (ff_sqrt_tab[a+1]-1)>>4;
    else if(a<(1<<12)) b= ff_sqrt_tab[a>>4 ]>>2;
174
#if !CONFIG_SMALL
michael's avatar
michael committed
175 176 177 178 179 180 181 182
    else if(a<(1<<14)) b= ff_sqrt_tab[a>>6 ]>>1;
    else if(a<(1<<16)) b= ff_sqrt_tab[a>>8 ]   ;
#endif
    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);
183
    }
michael's avatar
michael committed
184 185

    return b - (a<b*b);
186 187
}

188
#if ARCH_X86
189
#define MASK_ABS(mask, level)\
190
            __asm__ volatile(\
191
                "cltd                   \n\t"\
192 193 194 195 196 197 198 199 200 201
                "xorl %1, %0            \n\t"\
                "subl %1, %0            \n\t"\
                : "+a" (level), "=&d" (mask)\
            );
#else
#define MASK_ABS(mask, level)\
            mask= level>>31;\
            level= (level^mask)-mask;
#endif

202
#if HAVE_CMOV
203
#define COPY3_IF_LT(x,y,a,b,c,d)\
204
__asm__ volatile (\
205 206 207 208
    "cmpl %0, %3        \n\t"\
    "cmovl %3, %0       \n\t"\
    "cmovl %4, %1       \n\t"\
    "cmovl %5, %2       \n\t"\
209
    : "+&r" (x), "+&r" (a), "+r" (c)\
210 211 212 213 214 215 216 217 218 219 220
    : "r" (y), "r" (b), "r" (d)\
);
#else
#define COPY3_IF_LT(x,y,a,b,c,d)\
if((y)<(x)){\
     (x)=(y);\
     (a)=(b);\
     (c)=(d);\
}
#endif

221
/* avoid usage of dangerous/inappropriate system functions */
222
#undef  malloc
223
#define malloc please_use_av_malloc
224
#undef  free
225
#define free please_use_av_free
226
#undef  realloc
227
#define realloc please_use_av_realloc
228
#undef  time
229
#define time time_is_forbidden_due_to_security_issues
230
#undef  rand
231
#define rand rand_is_forbidden_due_to_state_trashing_use_av_random
232
#undef  srand
233
#define srand srand_is_forbidden_due_to_state_trashing_use_av_random_init
234
#undef  random
235
#define random random_is_forbidden_due_to_state_trashing_use_av_random
236
#undef  sprintf
237
#define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
238
#undef  strcat
reimar's avatar
reimar committed
239
#define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
240
#undef  exit
michael's avatar
michael committed
241
#define exit exit_is_forbidden
242
#ifndef LIBAVFORMAT_BUILD
243
#undef  printf
244
#define printf please_use_av_log_instead_of_printf
245
#undef  fprintf
246
#define fprintf please_use_av_log_instead_of_fprintf
247
#undef  puts
248
#define puts please_use_av_log_instead_of_puts
249 250
#undef  perror
#define perror please_use_av_log_instead_of_perror
251 252 253 254 255 256
#endif

#define CHECKED_ALLOCZ(p, size)\
{\
    p= av_mallocz(size);\
    if(p==NULL && (size)!=0){\
257
        av_log(NULL, AV_LOG_ERROR, "Cannot allocate memory.");\
258 259 260 261
        goto fail;\
    }\
}

262 263 264 265 266 267
#if defined(__ICC) || defined(__SUNPRO_C)
    #define DECLARE_ALIGNED(n,t,v)      t v __attribute__ ((aligned (n)))
    #define DECLARE_ASM_CONST(n,t,v)    const t __attribute__ ((aligned (n))) v
#elif defined(__GNUC__)
    #define DECLARE_ALIGNED(n,t,v)      t v __attribute__ ((aligned (n)))
    #define DECLARE_ASM_CONST(n,t,v)    static const t v attribute_used __attribute__ ((aligned (n)))
268 269 270
#elif defined(_MSC_VER)
    #define DECLARE_ALIGNED(n,t,v)      __declspec(align(n)) t v
    #define DECLARE_ASM_CONST(n,t,v)    __declspec(align(n)) static const t v
271 272 273 274 275 276 277 278
#elif HAVE_INLINE_ASM
    #error The asm code needs alignment, but we do not know how to do it for this compiler.
#else
    #define DECLARE_ALIGNED(n,t,v)      t v
    #define DECLARE_ASM_CONST(n,t,v)    static const t v
#endif


279
#if !HAVE_LLRINT
zuxy's avatar
zuxy committed
280
static av_always_inline av_const long long llrint(double x)
281 282 283 284 285
{
    return rint(x);
}
#endif /* HAVE_LLRINT */

286
#if !HAVE_LRINT
zuxy's avatar
zuxy committed
287
static av_always_inline av_const long int lrint(double x)
288 289 290 291 292
{
    return rint(x);
}
#endif /* HAVE_LRINT */

293
#if !HAVE_LRINTF
zuxy's avatar
zuxy committed
294
static av_always_inline av_const long int lrintf(float x)
295 296 297 298 299
{
    return (int)(rint(x));
}
#endif /* HAVE_LRINTF */

300
#if !HAVE_ROUND
zuxy's avatar
zuxy committed
301
static av_always_inline av_const double round(double x)
302 303 304 305 306
{
    return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
}
#endif /* HAVE_ROUND */

307
#if !HAVE_ROUNDF
zuxy's avatar
zuxy committed
308
static av_always_inline av_const float roundf(float x)
309 310 311 312 313
{
    return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
}
#endif /* HAVE_ROUNDF */

314
#if !HAVE_TRUNCF
ramiro's avatar
ramiro committed
315 316 317 318 319 320
static av_always_inline av_const float truncf(float x)
{
    return (x > 0) ? floor(x) : ceil(x);
}
#endif /* HAVE_TRUNCF */

321
/**
322 323
 * Returns NULL if CONFIG_SMALL is true, otherwise the argument
 * without modification. Used to disable the definition of strings
324 325 326 327 328 329 330 331
 * (for example AVCodec long_names).
 */
#if CONFIG_SMALL
#   define NULL_IF_CONFIG_SMALL(x) NULL
#else
#   define NULL_IF_CONFIG_SMALL(x) x
#endif

332
#endif /* AVUTIL_INTERNAL_H */