Statistics
| Branch: | Revision:

ffmpeg / libavutil / common.h @ 9d82b0dd

History | View | Annotate | Download (14.4 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
/* define it to include statistics code (useful only for optimizing
343
   codec efficiency */
344
//#define STATS
345

    
346
#ifdef STATS
347

    
348
enum {
349
    ST_UNKNOWN,
350
    ST_DC,
351
    ST_INTRA_AC,
352
    ST_INTER_AC,
353
    ST_INTRA_MB,
354
    ST_INTER_MB,
355
    ST_MV,
356
    ST_NB,
357
};
358

    
359
extern int st_current_index;
360
extern unsigned int st_bit_counts[ST_NB];
361
extern unsigned int st_out_bit_counts[ST_NB];
362

    
363
void print_stats(void);
364
#endif
365

    
366
/* misc math functions */
367
extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
368

    
369
static inline int av_log2(unsigned int v)
370
{
371
    int n;
372

    
373
    n = 0;
374
    if (v & 0xffff0000) {
375
        v >>= 16;
376
        n += 16;
377
    }
378
    if (v & 0xff00) {
379
        v >>= 8;
380
        n += 8;
381
    }
382
    n += ff_log2_tab[v];
383

    
384
    return n;
385
}
386

    
387
static inline int av_log2_16bit(unsigned int v)
388
{
389
    int n;
390

    
391
    n = 0;
392
    if (v & 0xff00) {
393
        v >>= 8;
394
        n += 8;
395
    }
396
    n += ff_log2_tab[v];
397

    
398
    return n;
399
}
400

    
401
/* median of 3 */
402
static inline int mid_pred(int a, int b, int c)
403
{
404
#if 0
405
    int t= (a-b)&((a-b)>>31);
406
    a-=t;
407
    b+=t;
408
    b-= (b-c)&((b-c)>>31);
409
    b+= (a-b)&((a-b)>>31);
410

411
    return b;
412
#else
413
    if(a>b){
414
        if(c>b){
415
            if(c>a) b=a;
416
            else    b=c;
417
        }
418
    }else{
419
        if(b>c){
420
            if(c>a) b=c;
421
            else    b=a;
422
        }
423
    }
424
    return b;
425
#endif
426
}
427

    
428
/**
429
 * clip a signed integer value into the amin-amax range
430
 * @param a value to clip
431
 * @param amin minimum value of the clip range
432
 * @param amax maximum value of the clip range
433
 * @return cliped value
434
 */
435
static inline int clip(int a, int amin, int amax)
436
{
437
    if (a < amin)
438
        return amin;
439
    else if (a > amax)
440
        return amax;
441
    else
442
        return a;
443
}
444

    
445
/**
446
 * clip a signed integer value into the 0-255 range
447
 * @param a value to clip
448
 * @return cliped value
449
 */
450
static inline uint8_t clip_uint8(int a)
451
{
452
    if (a&(~255)) return (-a)>>31;
453
    else          return a;
454
}
455

    
456
/* math */
457
extern FF_IMPORT_ATTR const uint8_t ff_sqrt_tab[128];
458

    
459
int64_t ff_gcd(int64_t a, int64_t b);
460

    
461
static inline int ff_sqrt(int a)
462
{
463
    int ret=0;
464
    int s;
465
    int ret_sq=0;
466

    
467
    if(a<128) return ff_sqrt_tab[a];
468

    
469
    for(s=15; s>=0; s--){
470
        int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
471
        if(b<=a){
472
            ret_sq=b;
473
            ret+= 1<<s;
474
        }
475
    }
476
    return ret;
477
}
478

    
479
/**
480
 * converts fourcc string to int
481
 */
482
static inline int ff_get_fourcc(const char *s){
483
    assert( strlen(s)==4 );
484

    
485
    return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
486
}
487

    
488
#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
489
#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
490

    
491

    
492
#if defined(ARCH_X86) || defined(ARCH_X86_64)
493
#define MASK_ABS(mask, level)\
494
            asm volatile(\
495
                "cdq                    \n\t"\
496
                "xorl %1, %0            \n\t"\
497
                "subl %1, %0            \n\t"\
498
                : "+a" (level), "=&d" (mask)\
499
            );
500
#else
501
#define MASK_ABS(mask, level)\
502
            mask= level>>31;\
503
            level= (level^mask)-mask;
504
#endif
505

    
506
#define GET_UTF8(val, GET_BYTE, ERROR)\
507
    val= GET_BYTE;\
508
    {\
509
        int ones= 7 - av_log2(val ^ 255);\
510
        if(ones==1)\
511
            ERROR\
512
        val&= 127>>ones;\
513
        while(--ones > 0){\
514
            int tmp= GET_BYTE - 128;\
515
            if(tmp>>6)\
516
                ERROR\
517
            val= (val<<6) + tmp;\
518
        }\
519
    }
520

    
521
#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
522
#define COPY3_IF_LT(x,y,a,b,c,d)\
523
asm volatile (\
524
    "cmpl %0, %3        \n\t"\
525
    "cmovl %3, %0       \n\t"\
526
    "cmovl %4, %1       \n\t"\
527
    "cmovl %5, %2       \n\t"\
528
    : "+r" (x), "+r" (a), "+r" (c)\
529
    : "r" (y), "r" (b), "r" (d)\
530
);
531
#else
532
#define COPY3_IF_LT(x,y,a,b,c,d)\
533
if((y)<(x)){\
534
     (x)=(y);\
535
     (a)=(b);\
536
     (c)=(d);\
537
}
538
#endif
539

    
540
#if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
541
#if defined(ARCH_X86_64)
542
static inline uint64_t read_time(void)
543
{
544
        uint64_t a, d;
545
        asm volatile(   "rdtsc\n\t"
546
                : "=a" (a), "=d" (d)
547
        );
548
        return (d << 32) | (a & 0xffffffff);
549
}
550
#elif defined(ARCH_X86)
551
static inline long long read_time(void)
552
{
553
        long long l;
554
        asm volatile(   "rdtsc\n\t"
555
                : "=A" (l)
556
        );
557
        return l;
558
}
559
#else //FIXME check ppc64
560
static inline uint64_t read_time(void)
561
{
562
    uint32_t tbu, tbl, temp;
563

    
564
     /* from section 2.2.1 of the 32-bit PowerPC PEM */
565
     __asm__ __volatile__(
566
         "1:\n"
567
         "mftbu  %2\n"
568
         "mftb   %0\n"
569
         "mftbu  %1\n"
570
         "cmpw   %2,%1\n"
571
         "bne    1b\n"
572
     : "=r"(tbl), "=r"(tbu), "=r"(temp)
573
     :
574
     : "cc");
575

    
576
     return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
577
}
578
#endif
579

    
580
#define START_TIMER \
581
uint64_t tend;\
582
uint64_t tstart= read_time();\
583

    
584
#define STOP_TIMER(id) \
585
tend= read_time();\
586
{\
587
  static uint64_t tsum=0;\
588
  static int tcount=0;\
589
  static int tskip_count=0;\
590
  if(tcount<2 || tend - tstart < 8*tsum/tcount){\
591
      tsum+= tend - tstart;\
592
      tcount++;\
593
  }else\
594
      tskip_count++;\
595
  if(256*256*256*64%(tcount+tskip_count)==0){\
596
      av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
597
  }\
598
}
599
#else
600
#define START_TIMER
601
#define STOP_TIMER(id) {}
602
#endif
603

    
604
/* avoid usage of various functions */
605
#define malloc please_use_av_malloc
606
#define free please_use_av_free
607
#define realloc please_use_av_realloc
608
#define time time_is_forbidden_due_to_security_issues
609
#define rand rand_is_forbidden_due_to_state_trashing
610
#define srand srand_is_forbidden_due_to_state_trashing
611
#define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
612
#define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
613
#if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
614
#define printf please_use_av_log
615
#define fprintf please_use_av_log
616
#endif
617

    
618
#define CHECKED_ALLOCZ(p, size)\
619
{\
620
    p= av_mallocz(size);\
621
    if(p==NULL && (size)!=0){\
622
        perror("malloc");\
623
        goto fail;\
624
    }\
625
}
626

    
627
#ifndef HAVE_LRINTF
628
/* XXX: add ISOC specific test to avoid specific BSD testing. */
629
/* better than nothing implementation. */
630
/* btw, rintf() is existing on fbsd too -- alex */
631
static always_inline long int lrintf(float x)
632
{
633
#ifdef CONFIG_WIN32
634
#  ifdef ARCH_X86
635
    int32_t i;
636
    asm volatile(
637
        "fistpl %0\n\t"
638
        : "=m" (i) : "t" (x) : "st"
639
    );
640
    return i;
641
#  else
642
    /* XXX: incorrect, but make it compile */
643
    return (int)(x + (x < 0 ? -0.5 : 0.5));
644
#  endif /* ARCH_X86 */
645
#else
646
    return (int)(rint(x));
647
#endif /* CONFIG_WIN32 */
648
}
649
#else
650
#ifndef _ISOC9X_SOURCE
651
#define _ISOC9X_SOURCE
652
#endif
653
#include <math.h>
654
#endif /* HAVE_LRINTF */
655

    
656
#endif /* HAVE_AV_CONFIG_H */
657

    
658
#endif /* COMMON_H */