Statistics
| Branch: | Revision:

ffmpeg / libavutil / common.h @ a7702890

History | View | Annotate | Download (14 KB)

1
/**
2
 * @file common.h
3
 * common internal api header.
4
 */
5

    
6
#ifndef COMMON_H
7
#define COMMON_H
8

    
9
#if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
10
#    define CONFIG_WIN32
11
#endif
12

    
13
#if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(EMULATE_INTTYPES)
14
#    define EMULATE_INTTYPES
15
#endif
16

    
17
#ifndef M_PI
18
#define M_PI    3.14159265358979323846
19
#endif
20

    
21
#if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
22
#    define PIC
23
#endif
24

    
25
#ifdef HAVE_AV_CONFIG_H
26
/* only include the following when compiling package */
27
#    include "config.h"
28

    
29
#    include <stdlib.h>
30
#    include <stdio.h>
31
#    include <string.h>
32
#    include <ctype.h>
33
#    include <limits.h>
34
#    ifndef __BEOS__
35
#        include <errno.h>
36
#    else
37
#        include "berrno.h"
38
#    endif
39
#    include <math.h>
40

    
41
#    ifndef ENODATA
42
#        define ENODATA  61
43
#    endif
44

    
45
#include <stddef.h>
46
#ifndef offsetof
47
# define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
48
#endif
49

    
50
#define AVOPTION_CODEC_BOOL(name, help, field) \
51
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
52
#define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
53
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
54
#define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
55
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
56
#define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
57
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
58
#define AVOPTION_CODEC_STRING(name, help, field, str, val) \
59
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
60
#define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
61
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
62
#define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
63
#define AVOPTION_END() AVOPTION_SUB(NULL)
64

    
65
#endif /* HAVE_AV_CONFIG_H */
66

    
67
/* Suppress restrict if it was not defined in config.h.  */
68
#ifndef restrict
69
#    define restrict
70
#endif
71

    
72
#ifndef always_inline
73
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
74
#    define always_inline __attribute__((always_inline)) inline
75
#else
76
#    define always_inline inline
77
#endif
78
#endif
79

    
80
#ifndef attribute_used
81
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
82
#    define attribute_used __attribute__((used))
83
#else
84
#    define attribute_used
85
#endif
86
#endif
87

    
88
#ifndef attribute_unused
89
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
90
#    define attribute_unused __attribute__((unused))
91
#else
92
#    define attribute_unused
93
#endif
94
#endif
95

    
96
#ifndef EMULATE_INTTYPES
97
#   include <inttypes.h>
98
#else
99
    typedef signed char  int8_t;
100
    typedef signed short int16_t;
101
    typedef signed int   int32_t;
102
    typedef unsigned char  uint8_t;
103
    typedef unsigned short uint16_t;
104
    typedef unsigned int   uint32_t;
105

    
106
#   ifdef CONFIG_WIN32
107
        typedef signed __int64   int64_t;
108
        typedef unsigned __int64 uint64_t;
109
#   else /* other OS */
110
        typedef signed long long   int64_t;
111
        typedef unsigned long long uint64_t;
112
#   endif /* other OS */
113
#endif /* EMULATE_INTTYPES */
114

    
115
#ifndef PRId64
116
#define PRId64 "lld"
117
#endif
118

    
119
#ifndef PRIu64
120
#define PRIu64 "llu"
121
#endif
122

    
123
#ifndef PRIx64
124
#define PRIx64 "llx"
125
#endif
126

    
127
#ifndef PRId32
128
#define PRId32 "d"
129
#endif
130

    
131
#ifndef PRIdFAST16
132
#define PRIdFAST16 PRId32
133
#endif
134

    
135
#ifndef PRIdFAST32
136
#define PRIdFAST32 PRId32
137
#endif
138

    
139
#ifndef INT16_MIN
140
#define INT16_MIN       (-0x7fff-1)
141
#endif
142

    
143
#ifndef INT16_MAX
144
#define INT16_MAX       0x7fff
145
#endif
146

    
147
#ifndef INT32_MIN
148
#define INT32_MIN       (-0x7fffffff-1)
149
#endif
150

    
151
#ifndef INT32_MAX
152
#define INT32_MAX       0x7fffffff
153
#endif
154

    
155
#ifndef UINT32_MAX
156
#define UINT32_MAX      0xffffffff
157
#endif
158

    
159
#ifndef INT64_MIN
160
#define INT64_MIN       (-0x7fffffffffffffffLL-1)
161
#endif
162

    
163
#ifndef INT64_MAX
164
#define INT64_MAX int64_t_C(9223372036854775807)
165
#endif
166

    
167
#ifndef UINT64_MAX
168
#define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
169
#endif
170

    
171
#ifdef EMULATE_FAST_INT
172
typedef signed char int_fast8_t;
173
typedef signed int  int_fast16_t;
174
typedef signed int  int_fast32_t;
175
typedef unsigned char uint_fast8_t;
176
typedef unsigned int  uint_fast16_t;
177
typedef unsigned int  uint_fast32_t;
178
typedef uint64_t      uint_fast64_t;
179
#endif
180

    
181
#ifndef INT_BIT
182
#    if INT_MAX != 2147483647
183
#        define INT_BIT 64
184
#    else
185
#        define INT_BIT 32
186
#    endif
187
#endif
188

    
189
#ifdef CONFIG_WIN32
190

    
191
/* windows */
192

    
193
#    if !defined(__MINGW32__) && !defined(__CYGWIN__)
194
#        define int64_t_C(c)     (c ## i64)
195
#        define uint64_t_C(c)    (c ## i64)
196

    
197
#    ifdef HAVE_AV_CONFIG_H
198
#            define inline __inline
199
#    endif
200

    
201
#    else
202
#        define int64_t_C(c)     (c ## LL)
203
#        define uint64_t_C(c)    (c ## ULL)
204
#    endif /* __MINGW32__ */
205

    
206
#    ifdef HAVE_AV_CONFIG_H
207
#        ifdef _DEBUG
208
#            define DEBUG
209
#        endif
210

    
211
#        define snprintf _snprintf
212
#        define vsnprintf _vsnprintf
213

    
214
#        ifdef CONFIG_WINCE
215
#            define perror(a)
216
#        endif
217

    
218
#    endif
219

    
220
/* CONFIG_WIN32 end */
221
#elif defined (CONFIG_OS2)
222
/* OS/2 EMX */
223

    
224
#ifndef int64_t_C
225
#define int64_t_C(c)     (c ## LL)
226
#define uint64_t_C(c)    (c ## ULL)
227
#endif
228

    
229
#ifdef HAVE_AV_CONFIG_H
230

    
231
#ifdef USE_FASTMEMCPY
232
#include "fastmemcpy.h"
233
#endif
234

    
235
#include <float.h>
236

    
237
#endif /* HAVE_AV_CONFIG_H */
238

    
239
/* CONFIG_OS2 end */
240
#else
241

    
242
/* unix */
243

    
244
#ifndef int64_t_C
245
#define int64_t_C(c)     (c ## LL)
246
#define uint64_t_C(c)    (c ## ULL)
247
#endif
248

    
249
#ifdef HAVE_AV_CONFIG_H
250

    
251
#        ifdef USE_FASTMEMCPY
252
#            include "fastmemcpy.h"
253
#        endif
254
#    endif /* HAVE_AV_CONFIG_H */
255

    
256
#endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
257

    
258
#ifdef HAVE_AV_CONFIG_H
259

    
260
#if defined(__MINGW32__) && !defined(BUILD_AVUTIL) && defined(BUILD_SHARED_AV)
261
#  define FF_IMPORT_ATTR __declspec(dllimport)
262
#else
263
#  define FF_IMPORT_ATTR
264
#endif
265

    
266

    
267
#    include "bswap.h"
268

    
269
// Use rip-relative addressing if compiling PIC code on x86-64.
270
#    if defined(__MINGW32__) || defined(__CYGWIN__) || \
271
        defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
272
#        if defined(ARCH_X86_64) && defined(PIC)
273
#            define MANGLE(a) "_" #a"(%%rip)"
274
#        else
275
#            define MANGLE(a) "_" #a
276
#        endif
277
#    else
278
#        if defined(ARCH_X86_64) && defined(PIC)
279
#            define MANGLE(a) #a"(%%rip)"
280
#        elif defined(CONFIG_DARWIN)
281
#            define MANGLE(a) "_" #a
282
#        else
283
#            define MANGLE(a) #a
284
#        endif
285
#    endif
286

    
287
/* debug stuff */
288

    
289
#    if !defined(DEBUG) && !defined(NDEBUG)
290
#        define NDEBUG
291
#    endif
292
#    include <assert.h>
293

    
294
/* dprintf macros */
295
#    if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
296

    
297
inline void dprintf(const char* fmt,...) {}
298

    
299
#    else
300

    
301
#        ifdef DEBUG
302
#            define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
303
#        else
304
#            define dprintf(fmt,...)
305
#        endif
306

    
307
#    endif /* !CONFIG_WIN32 */
308
#    ifdef CONFIG_WINCE
309
#            define abort()
310
#    endif
311

    
312
#    define av_abort()      do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
313

    
314
//rounded divison & shift
315
#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
316
/* assume b>0 */
317
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
318
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
319

    
320
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
321
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
322

    
323
extern const uint32_t inverse[256];
324

    
325
#if defined(ARCH_X86) || defined(ARCH_X86_64)
326
#    define FASTDIV(a,b) \
327
    ({\
328
        int ret,dmy;\
329
        asm volatile(\
330
            "mull %3"\
331
            :"=d"(ret),"=a"(dmy)\
332
            :"1"(a),"g"(inverse[b])\
333
            );\
334
        ret;\
335
    })
336
#elif defined(CONFIG_FASTDIV)
337
#    define FASTDIV(a,b)   ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
338
#else
339
#    define FASTDIV(a,b)   ((a)/(b))
340
#endif
341

    
342
/* misc math functions */
343
extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
344

    
345
static inline int av_log2(unsigned int v)
346
{
347
    int n;
348

    
349
    n = 0;
350
    if (v & 0xffff0000) {
351
        v >>= 16;
352
        n += 16;
353
    }
354
    if (v & 0xff00) {
355
        v >>= 8;
356
        n += 8;
357
    }
358
    n += ff_log2_tab[v];
359

    
360
    return n;
361
}
362

    
363
static inline int av_log2_16bit(unsigned int v)
364
{
365
    int n;
366

    
367
    n = 0;
368
    if (v & 0xff00) {
369
        v >>= 8;
370
        n += 8;
371
    }
372
    n += ff_log2_tab[v];
373

    
374
    return n;
375
}
376

    
377
/* median of 3 */
378
static inline int mid_pred(int a, int b, int c)
379
{
380
#if 0
381
    int t= (a-b)&((a-b)>>31);
382
    a-=t;
383
    b+=t;
384
    b-= (b-c)&((b-c)>>31);
385
    b+= (a-b)&((a-b)>>31);
386

387
    return b;
388
#else
389
    if(a>b){
390
        if(c>b){
391
            if(c>a) b=a;
392
            else    b=c;
393
        }
394
    }else{
395
        if(b>c){
396
            if(c>a) b=c;
397
            else    b=a;
398
        }
399
    }
400
    return b;
401
#endif
402
}
403

    
404
/**
405
 * clip a signed integer value into the amin-amax range
406
 * @param a value to clip
407
 * @param amin minimum value of the clip range
408
 * @param amax maximum value of the clip range
409
 * @return cliped value
410
 */
411
static inline int clip(int a, int amin, int amax)
412
{
413
    if (a < amin)
414
        return amin;
415
    else if (a > amax)
416
        return amax;
417
    else
418
        return a;
419
}
420

    
421
/**
422
 * clip a signed integer value into the 0-255 range
423
 * @param a value to clip
424
 * @return cliped value
425
 */
426
static inline uint8_t clip_uint8(int a)
427
{
428
    if (a&(~255)) return (-a)>>31;
429
    else          return a;
430
}
431

    
432
/* math */
433
extern FF_IMPORT_ATTR const uint8_t ff_sqrt_tab[128];
434

    
435
int64_t ff_gcd(int64_t a, int64_t b);
436

    
437
static inline int ff_sqrt(int a)
438
{
439
    int ret=0;
440
    int s;
441
    int ret_sq=0;
442

    
443
    if(a<128) return ff_sqrt_tab[a];
444

    
445
    for(s=15; s>=0; s--){
446
        int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
447
        if(b<=a){
448
            ret_sq=b;
449
            ret+= 1<<s;
450
        }
451
    }
452
    return ret;
453
}
454

    
455
/**
456
 * converts fourcc string to int
457
 */
458
static inline int ff_get_fourcc(const char *s){
459
    assert( strlen(s)==4 );
460

    
461
    return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
462
}
463

    
464
#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
465
#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
466

    
467

    
468
#if defined(ARCH_X86) || defined(ARCH_X86_64)
469
#define MASK_ABS(mask, level)\
470
            asm volatile(\
471
                "cdq                    \n\t"\
472
                "xorl %1, %0            \n\t"\
473
                "subl %1, %0            \n\t"\
474
                : "+a" (level), "=&d" (mask)\
475
            );
476
#else
477
#define MASK_ABS(mask, level)\
478
            mask= level>>31;\
479
            level= (level^mask)-mask;
480
#endif
481

    
482
#define GET_UTF8(val, GET_BYTE, ERROR)\
483
    val= GET_BYTE;\
484
    {\
485
        int ones= 7 - av_log2(val ^ 255);\
486
        if(ones==1)\
487
            ERROR\
488
        val&= 127>>ones;\
489
        while(--ones > 0){\
490
            int tmp= GET_BYTE - 128;\
491
            if(tmp>>6)\
492
                ERROR\
493
            val= (val<<6) + tmp;\
494
        }\
495
    }
496

    
497
#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
498
#define COPY3_IF_LT(x,y,a,b,c,d)\
499
asm volatile (\
500
    "cmpl %0, %3        \n\t"\
501
    "cmovl %3, %0       \n\t"\
502
    "cmovl %4, %1       \n\t"\
503
    "cmovl %5, %2       \n\t"\
504
    : "+r" (x), "+r" (a), "+r" (c)\
505
    : "r" (y), "r" (b), "r" (d)\
506
);
507
#else
508
#define COPY3_IF_LT(x,y,a,b,c,d)\
509
if((y)<(x)){\
510
     (x)=(y);\
511
     (a)=(b);\
512
     (c)=(d);\
513
}
514
#endif
515

    
516
#if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
517
#if defined(ARCH_X86_64)
518
static inline uint64_t read_time(void)
519
{
520
        uint64_t a, d;
521
        asm volatile(   "rdtsc\n\t"
522
                : "=a" (a), "=d" (d)
523
        );
524
        return (d << 32) | (a & 0xffffffff);
525
}
526
#elif defined(ARCH_X86)
527
static inline long long read_time(void)
528
{
529
        long long l;
530
        asm volatile(   "rdtsc\n\t"
531
                : "=A" (l)
532
        );
533
        return l;
534
}
535
#else //FIXME check ppc64
536
static inline uint64_t read_time(void)
537
{
538
    uint32_t tbu, tbl, temp;
539

    
540
     /* from section 2.2.1 of the 32-bit PowerPC PEM */
541
     __asm__ __volatile__(
542
         "1:\n"
543
         "mftbu  %2\n"
544
         "mftb   %0\n"
545
         "mftbu  %1\n"
546
         "cmpw   %2,%1\n"
547
         "bne    1b\n"
548
     : "=r"(tbl), "=r"(tbu), "=r"(temp)
549
     :
550
     : "cc");
551

    
552
     return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
553
}
554
#endif
555

    
556
#define START_TIMER \
557
uint64_t tend;\
558
uint64_t tstart= read_time();\
559

    
560
#define STOP_TIMER(id) \
561
tend= read_time();\
562
{\
563
  static uint64_t tsum=0;\
564
  static int tcount=0;\
565
  static int tskip_count=0;\
566
  if(tcount<2 || tend - tstart < 8*tsum/tcount){\
567
      tsum+= tend - tstart;\
568
      tcount++;\
569
  }else\
570
      tskip_count++;\
571
  if(256*256*256*64%(tcount+tskip_count)==0){\
572
      av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
573
  }\
574
}
575
#else
576
#define START_TIMER
577
#define STOP_TIMER(id) {}
578
#endif
579

    
580
/* avoid usage of various functions */
581
#define malloc please_use_av_malloc
582
#define free please_use_av_free
583
#define realloc please_use_av_realloc
584
#define time time_is_forbidden_due_to_security_issues
585
#define rand rand_is_forbidden_due_to_state_trashing
586
#define srand srand_is_forbidden_due_to_state_trashing
587
#define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
588
#define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
589
#if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
590
#define printf please_use_av_log
591
#define fprintf please_use_av_log
592
#endif
593

    
594
#define CHECKED_ALLOCZ(p, size)\
595
{\
596
    p= av_mallocz(size);\
597
    if(p==NULL && (size)!=0){\
598
        perror("malloc");\
599
        goto fail;\
600
    }\
601
}
602

    
603
#ifndef HAVE_LRINTF
604
/* XXX: add ISOC specific test to avoid specific BSD testing. */
605
/* better than nothing implementation. */
606
/* btw, rintf() is existing on fbsd too -- alex */
607
static always_inline long int lrintf(float x)
608
{
609
#ifdef CONFIG_WIN32
610
#  ifdef ARCH_X86
611
    int32_t i;
612
    asm volatile(
613
        "fistpl %0\n\t"
614
        : "=m" (i) : "t" (x) : "st"
615
    );
616
    return i;
617
#  else
618
    /* XXX: incorrect, but make it compile */
619
    return (int)(x + (x < 0 ? -0.5 : 0.5));
620
#  endif /* ARCH_X86 */
621
#else
622
    return (int)(rint(x));
623
#endif /* CONFIG_WIN32 */
624
}
625
#else
626
#ifndef _ISOC9X_SOURCE
627
#define _ISOC9X_SOURCE
628
#endif
629
#include <math.h>
630
#endif /* HAVE_LRINTF */
631

    
632
#endif /* HAVE_AV_CONFIG_H */
633

    
634
#endif /* COMMON_H */