Statistics
| Branch: | Revision:

ffmpeg / libavutil / common.h @ ac44871c

History | View | Annotate | Download (13.3 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
#ifdef HAVE_AV_CONFIG_H
22
/* only include the following when compiling package */
23
#    include "config.h"
24

    
25
#    include <stdlib.h>
26
#    include <stdio.h>
27
#    include <string.h>
28
#    include <ctype.h>
29
#    include <limits.h>
30
#    ifndef __BEOS__
31
#        include <errno.h>
32
#    else
33
#        include "berrno.h"
34
#    endif
35
#    include <math.h>
36

    
37
#    ifndef ENODATA
38
#        define ENODATA  61
39
#    endif
40

    
41
#include <stddef.h>
42
#ifndef offsetof
43
# define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
44
#endif
45

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

    
61
#endif /* HAVE_AV_CONFIG_H */
62

    
63
/* Suppress restrict if it was not defined in config.h.  */
64
#ifndef restrict
65
#    define restrict
66
#endif
67

    
68
#ifndef always_inline
69
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
70
#    define always_inline __attribute__((always_inline)) inline
71
#else
72
#    define always_inline inline
73
#endif
74
#endif
75

    
76
#ifndef attribute_used
77
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
78
#    define attribute_used __attribute__((used))
79
#else
80
#    define attribute_used
81
#endif
82
#endif
83

    
84
#ifndef attribute_unused
85
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
86
#    define attribute_unused __attribute__((unused))
87
#else
88
#    define attribute_unused
89
#endif
90
#endif
91

    
92
#ifndef EMULATE_INTTYPES
93
#   include <inttypes.h>
94
#else
95
    typedef signed char  int8_t;
96
    typedef signed short int16_t;
97
    typedef signed int   int32_t;
98
    typedef unsigned char  uint8_t;
99
    typedef unsigned short uint16_t;
100
    typedef unsigned int   uint32_t;
101

    
102
#   ifdef CONFIG_WIN32
103
        typedef signed __int64   int64_t;
104
        typedef unsigned __int64 uint64_t;
105
#   else /* other OS */
106
        typedef signed long long   int64_t;
107
        typedef unsigned long long uint64_t;
108
#   endif /* other OS */
109
#endif /* EMULATE_INTTYPES */
110

    
111
#ifndef PRId64
112
#define PRId64 "lld"
113
#endif
114

    
115
#ifndef PRIu64
116
#define PRIu64 "llu"
117
#endif
118

    
119
#ifndef PRIx64
120
#define PRIx64 "llx"
121
#endif
122

    
123
#ifndef PRId32
124
#define PRId32 "d"
125
#endif
126

    
127
#ifndef PRIdFAST16
128
#define PRIdFAST16 PRId32
129
#endif
130

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

    
135
#ifndef INT16_MIN
136
#define INT16_MIN       (-0x7fff-1)
137
#endif
138

    
139
#ifndef INT16_MAX
140
#define INT16_MAX       0x7fff
141
#endif
142

    
143
#ifndef INT64_MIN
144
#define INT64_MIN       (-0x7fffffffffffffffLL-1)
145
#endif
146

    
147
#ifndef INT64_MAX
148
#define INT64_MAX int64_t_C(9223372036854775807)
149
#endif
150

    
151
#ifndef UINT64_MAX
152
#define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
153
#endif
154

    
155
#ifdef EMULATE_FAST_INT
156
typedef signed char int_fast8_t;
157
typedef signed int  int_fast16_t;
158
typedef signed int  int_fast32_t;
159
typedef unsigned char uint_fast8_t;
160
typedef unsigned int  uint_fast16_t;
161
typedef unsigned int  uint_fast32_t;
162
typedef uint64_t      uint_fast64_t;
163
#endif
164

    
165
#ifndef INT_BIT
166
#    if INT_MAX != 2147483647
167
#        define INT_BIT 64
168
#    else
169
#        define INT_BIT 32
170
#    endif
171
#endif
172

    
173
#if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
174
static inline float floorf(float f) {
175
    return floor(f);
176
}
177
#endif
178

    
179
#ifdef CONFIG_WIN32
180

    
181
/* windows */
182

    
183
#    if !defined(__MINGW32__) && !defined(__CYGWIN__)
184
#        define int64_t_C(c)     (c ## i64)
185
#        define uint64_t_C(c)    (c ## i64)
186

    
187
#    ifdef HAVE_AV_CONFIG_H
188
#            define inline __inline
189
#    endif
190

    
191
#    else
192
#        define int64_t_C(c)     (c ## LL)
193
#        define uint64_t_C(c)    (c ## ULL)
194
#    endif /* __MINGW32__ */
195

    
196
#    ifdef HAVE_AV_CONFIG_H
197
#        ifdef _DEBUG
198
#            define DEBUG
199
#        endif
200

    
201
#        define snprintf _snprintf
202
#        define vsnprintf _vsnprintf
203

    
204
#        ifdef CONFIG_WINCE
205
#            define perror(a)
206
#        endif
207

    
208
#    endif
209

    
210
/* CONFIG_WIN32 end */
211
#elif defined (CONFIG_OS2)
212
/* OS/2 EMX */
213

    
214
#ifndef int64_t_C
215
#define int64_t_C(c)     (c ## LL)
216
#define uint64_t_C(c)    (c ## ULL)
217
#endif
218

    
219
#ifdef HAVE_AV_CONFIG_H
220

    
221
#ifdef USE_FASTMEMCPY
222
#include "fastmemcpy.h"
223
#endif
224

    
225
#include <float.h>
226

    
227
#endif /* HAVE_AV_CONFIG_H */
228

    
229
/* CONFIG_OS2 end */
230
#else
231

    
232
/* unix */
233

    
234
#ifndef int64_t_C
235
#define int64_t_C(c)     (c ## LL)
236
#define uint64_t_C(c)    (c ## ULL)
237
#endif
238

    
239
#ifdef HAVE_AV_CONFIG_H
240

    
241
#        ifdef USE_FASTMEMCPY
242
#            include "fastmemcpy.h"
243
#        endif
244
#    endif /* HAVE_AV_CONFIG_H */
245

    
246
#endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
247

    
248
#ifdef HAVE_AV_CONFIG_H
249

    
250
#    include "bswap.h"
251

    
252
// Use rip-relative addressing if compiling PIC code on x86-64.
253
#    if defined(__MINGW32__) || defined(__CYGWIN__) || \
254
        defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
255
#        if defined(ARCH_X86_64) && defined(PIC)
256
#            define MANGLE(a) "_" #a"(%%rip)"
257
#        else
258
#            define MANGLE(a) "_" #a
259
#        endif
260
#    else
261
#        if defined(ARCH_X86_64) && defined(PIC)
262
#            define MANGLE(a) #a"(%%rip)"
263
#        else
264
#            define MANGLE(a) #a
265
#        endif
266
#    endif
267

    
268
/* debug stuff */
269

    
270
#    ifndef DEBUG
271
#        define NDEBUG
272
#    endif
273
#    include <assert.h>
274

    
275
/* dprintf macros */
276
#    if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
277

    
278
inline void dprintf(const char* fmt,...) {}
279

    
280
#    else
281

    
282
#        ifdef DEBUG
283
#            define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
284
#        else
285
#            define dprintf(fmt,...)
286
#        endif
287

    
288
#    endif /* !CONFIG_WIN32 */
289
#    ifdef CONFIG_WINCE
290
#            define abort()
291
#    endif
292

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

    
295
//rounded divison & shift
296
#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
297
/* assume b>0 */
298
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
299
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
300

    
301
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
302
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
303

    
304
extern const uint32_t inverse[256];
305

    
306
#if defined(ARCH_X86) || defined(ARCH_X86_64)
307
#    define FASTDIV(a,b) \
308
    ({\
309
        int ret,dmy;\
310
        asm volatile(\
311
            "mull %3"\
312
            :"=d"(ret),"=a"(dmy)\
313
            :"1"(a),"g"(inverse[b])\
314
            );\
315
        ret;\
316
    })
317
#elif defined(CONFIG_FASTDIV)
318
#    define FASTDIV(a,b)   ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
319
#else
320
#    define FASTDIV(a,b)   ((a)/(b))
321
#endif
322

    
323
/* define it to include statistics code (useful only for optimizing
324
   codec efficiency */
325
//#define STATS
326

    
327
#ifdef STATS
328

    
329
enum {
330
    ST_UNKNOWN,
331
    ST_DC,
332
    ST_INTRA_AC,
333
    ST_INTER_AC,
334
    ST_INTRA_MB,
335
    ST_INTER_MB,
336
    ST_MV,
337
    ST_NB,
338
};
339

    
340
extern int st_current_index;
341
extern unsigned int st_bit_counts[ST_NB];
342
extern unsigned int st_out_bit_counts[ST_NB];
343

    
344
void print_stats(void);
345
#endif
346

    
347
/* misc math functions */
348
extern const uint8_t ff_log2_tab[256];
349

    
350
static inline int av_log2(unsigned int v)
351
{
352
    int n;
353

    
354
    n = 0;
355
    if (v & 0xffff0000) {
356
        v >>= 16;
357
        n += 16;
358
    }
359
    if (v & 0xff00) {
360
        v >>= 8;
361
        n += 8;
362
    }
363
    n += ff_log2_tab[v];
364

    
365
    return n;
366
}
367

    
368
static inline int av_log2_16bit(unsigned int v)
369
{
370
    int n;
371

    
372
    n = 0;
373
    if (v & 0xff00) {
374
        v >>= 8;
375
        n += 8;
376
    }
377
    n += ff_log2_tab[v];
378

    
379
    return n;
380
}
381

    
382
/* median of 3 */
383
static inline int mid_pred(int a, int b, int c)
384
{
385
#if 0
386
    int t= (a-b)&((a-b)>>31);
387
    a-=t;
388
    b+=t;
389
    b-= (b-c)&((b-c)>>31);
390
    b+= (a-b)&((a-b)>>31);
391

392
    return b;
393
#else
394
    if(a>b){
395
        if(c>b){
396
            if(c>a) b=a;
397
            else    b=c;
398
        }
399
    }else{
400
        if(b>c){
401
            if(c>a) b=c;
402
            else    b=a;
403
        }
404
    }
405
    return b;
406
#endif
407
}
408

    
409
static inline int clip(int a, int amin, int amax)
410
{
411
    if (a < amin)
412
        return amin;
413
    else if (a > amax)
414
        return amax;
415
    else
416
        return a;
417
}
418

    
419
static inline int clip_uint8(int a)
420
{
421
    if (a&(~255)) return (-a)>>31;
422
    else          return a;
423
}
424

    
425
/* math */
426
extern const uint8_t ff_sqrt_tab[128];
427

    
428
int64_t ff_gcd(int64_t a, int64_t b);
429

    
430
static inline int ff_sqrt(int a)
431
{
432
    int ret=0;
433
    int s;
434
    int ret_sq=0;
435

    
436
    if(a<128) return ff_sqrt_tab[a];
437

    
438
    for(s=15; s>=0; s--){
439
        int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
440
        if(b<=a){
441
            ret_sq=b;
442
            ret+= 1<<s;
443
        }
444
    }
445
    return ret;
446
}
447

    
448
/**
449
 * converts fourcc string to int
450
 */
451
static inline int ff_get_fourcc(const char *s){
452
    assert( strlen(s)==4 );
453

    
454
    return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
455
}
456

    
457
#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
458
#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
459

    
460

    
461
#if defined(ARCH_X86) || defined(ARCH_X86_64)
462
#define MASK_ABS(mask, level)\
463
            asm volatile(\
464
                "cdq                    \n\t"\
465
                "xorl %1, %0            \n\t"\
466
                "subl %1, %0            \n\t"\
467
                : "+a" (level), "=&d" (mask)\
468
            );
469
#else
470
#define MASK_ABS(mask, level)\
471
            mask= level>>31;\
472
            level= (level^mask)-mask;
473
#endif
474

    
475

    
476
#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
477
#define COPY3_IF_LT(x,y,a,b,c,d)\
478
asm volatile (\
479
    "cmpl %0, %3        \n\t"\
480
    "cmovl %3, %0       \n\t"\
481
    "cmovl %4, %1       \n\t"\
482
    "cmovl %5, %2       \n\t"\
483
    : "+r" (x), "+r" (a), "+r" (c)\
484
    : "r" (y), "r" (b), "r" (d)\
485
);
486
#else
487
#define COPY3_IF_LT(x,y,a,b,c,d)\
488
if((y)<(x)){\
489
     (x)=(y);\
490
     (a)=(b);\
491
     (c)=(d);\
492
}
493
#endif
494

    
495
#if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
496
#if defined(ARCH_X86_64)
497
static inline uint64_t read_time(void)
498
{
499
        uint64_t a, d;
500
        asm volatile(   "rdtsc\n\t"
501
                : "=a" (a), "=d" (d)
502
        );
503
        return (d << 32) | (a & 0xffffffff);
504
}
505
#elif defined(ARCH_X86)
506
static inline long long read_time(void)
507
{
508
        long long l;
509
        asm volatile(   "rdtsc\n\t"
510
                : "=A" (l)
511
        );
512
        return l;
513
}
514
#else //FIXME check ppc64
515
static inline uint64_t read_time(void)
516
{
517
    uint32_t tbu, tbl, temp;
518

    
519
     /* from section 2.2.1 of the 32-bit PowerPC PEM */
520
     __asm__ __volatile__(
521
         "1:\n"
522
         "mftbu  %2\n"
523
         "mftb   %0\n"
524
         "mftbu  %1\n"
525
         "cmpw   %2,%1\n"
526
         "bne    1b\n"
527
     : "=r"(tbl), "=r"(tbu), "=r"(temp)
528
     :
529
     : "cc");
530

    
531
     return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
532
}
533
#endif
534

    
535
#define START_TIMER \
536
uint64_t tend;\
537
uint64_t tstart= read_time();\
538

    
539
#define STOP_TIMER(id) \
540
tend= read_time();\
541
{\
542
  static uint64_t tsum=0;\
543
  static int tcount=0;\
544
  static int tskip_count=0;\
545
  if(tcount<2 || tend - tstart < 8*tsum/tcount){\
546
      tsum+= tend - tstart;\
547
      tcount++;\
548
  }else\
549
      tskip_count++;\
550
  if(256*256*256*64%(tcount+tskip_count)==0){\
551
      av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
552
  }\
553
}
554
#else
555
#define START_TIMER
556
#define STOP_TIMER(id) {}
557
#endif
558

    
559
/* avoid usage of various functions */
560
#define malloc please_use_av_malloc
561
#define free please_use_av_free
562
#define realloc please_use_av_realloc
563
#define time time_is_forbidden_due_to_security_issues
564
#define rand rand_is_forbidden_due_to_state_trashing
565
#define srand srand_is_forbidden_due_to_state_trashing
566
#define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
567
#define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
568
#if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
569
#define printf please_use_av_log
570
#define fprintf please_use_av_log
571
#endif
572

    
573
#define CHECKED_ALLOCZ(p, size)\
574
{\
575
    p= av_mallocz(size);\
576
    if(p==NULL && (size)!=0){\
577
        perror("malloc");\
578
        goto fail;\
579
    }\
580
}
581

    
582
#ifndef HAVE_LRINTF
583
/* XXX: add ISOC specific test to avoid specific BSD testing. */
584
/* better than nothing implementation. */
585
/* btw, rintf() is existing on fbsd too -- alex */
586
static always_inline long int lrintf(float x)
587
{
588
#ifdef CONFIG_WIN32
589
#  ifdef ARCH_X86
590
    int32_t i;
591
    asm volatile(
592
        "fistpl %0\n\t"
593
        : "=m" (i) : "t" (x) : "st"
594
    );
595
    return i;
596
#  else
597
    /* XXX: incorrect, but make it compile */
598
    return (int)(x + (x < 0 ? -0.5 : 0.5));
599
#  endif /* ARCH_X86 */
600
#else
601
    return (int)(rint(x));
602
#endif /* CONFIG_WIN32 */
603
}
604
#else
605
#ifndef _ISOC9X_SOURCE
606
#define _ISOC9X_SOURCE
607
#endif
608
#include <math.h>
609
#endif /* HAVE_LRINTF */
610

    
611
#endif /* HAVE_AV_CONFIG_H */
612

    
613
#endif /* COMMON_H */