Statistics
| Branch: | Revision:

ffmpeg / libavutil / common.h @ 320d060a

History | View | Annotate | Download (13.5 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
#if defined(__MINGW32__) && !defined(BUILD_AVUTIL) && defined(BUILD_SHARED_AV)
251
#  define FF_IMPORT_ATTR __declspec(dllimport)
252
#else
253
#  define FF_IMPORT_ATTR
254
#endif
255

    
256

    
257
#    include "bswap.h"
258

    
259
// Use rip-relative addressing if compiling PIC code on x86-64.
260
#    if defined(__MINGW32__) || defined(__CYGWIN__) || \
261
        defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
262
#        if defined(ARCH_X86_64) && defined(PIC)
263
#            define MANGLE(a) "_" #a"(%%rip)"
264
#        else
265
#            define MANGLE(a) "_" #a
266
#        endif
267
#    else
268
#        if defined(ARCH_X86_64) && defined(PIC)
269
#            define MANGLE(a) #a"(%%rip)"
270
#        else
271
#            define MANGLE(a) #a
272
#        endif
273
#    endif
274

    
275
/* debug stuff */
276

    
277
#    ifndef DEBUG
278
#        define NDEBUG
279
#    endif
280
#    include <assert.h>
281

    
282
/* dprintf macros */
283
#    if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
284

    
285
inline void dprintf(const char* fmt,...) {}
286

    
287
#    else
288

    
289
#        ifdef DEBUG
290
#            define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
291
#        else
292
#            define dprintf(fmt,...)
293
#        endif
294

    
295
#    endif /* !CONFIG_WIN32 */
296
#    ifdef CONFIG_WINCE
297
#            define abort()
298
#    endif
299

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

    
302
//rounded divison & shift
303
#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
304
/* assume b>0 */
305
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
306
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
307

    
308
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
309
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
310

    
311
extern const uint32_t inverse[256];
312

    
313
#if defined(ARCH_X86) || defined(ARCH_X86_64)
314
#    define FASTDIV(a,b) \
315
    ({\
316
        int ret,dmy;\
317
        asm volatile(\
318
            "mull %3"\
319
            :"=d"(ret),"=a"(dmy)\
320
            :"1"(a),"g"(inverse[b])\
321
            );\
322
        ret;\
323
    })
324
#elif defined(CONFIG_FASTDIV)
325
#    define FASTDIV(a,b)   ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
326
#else
327
#    define FASTDIV(a,b)   ((a)/(b))
328
#endif
329

    
330
/* define it to include statistics code (useful only for optimizing
331
   codec efficiency */
332
//#define STATS
333

    
334
#ifdef STATS
335

    
336
enum {
337
    ST_UNKNOWN,
338
    ST_DC,
339
    ST_INTRA_AC,
340
    ST_INTER_AC,
341
    ST_INTRA_MB,
342
    ST_INTER_MB,
343
    ST_MV,
344
    ST_NB,
345
};
346

    
347
extern int st_current_index;
348
extern unsigned int st_bit_counts[ST_NB];
349
extern unsigned int st_out_bit_counts[ST_NB];
350

    
351
void print_stats(void);
352
#endif
353

    
354
/* misc math functions */
355
extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
356

    
357
static inline int av_log2(unsigned int v)
358
{
359
    int n;
360

    
361
    n = 0;
362
    if (v & 0xffff0000) {
363
        v >>= 16;
364
        n += 16;
365
    }
366
    if (v & 0xff00) {
367
        v >>= 8;
368
        n += 8;
369
    }
370
    n += ff_log2_tab[v];
371

    
372
    return n;
373
}
374

    
375
static inline int av_log2_16bit(unsigned int v)
376
{
377
    int n;
378

    
379
    n = 0;
380
    if (v & 0xff00) {
381
        v >>= 8;
382
        n += 8;
383
    }
384
    n += ff_log2_tab[v];
385

    
386
    return n;
387
}
388

    
389
/* median of 3 */
390
static inline int mid_pred(int a, int b, int c)
391
{
392
#if 0
393
    int t= (a-b)&((a-b)>>31);
394
    a-=t;
395
    b+=t;
396
    b-= (b-c)&((b-c)>>31);
397
    b+= (a-b)&((a-b)>>31);
398

399
    return b;
400
#else
401
    if(a>b){
402
        if(c>b){
403
            if(c>a) b=a;
404
            else    b=c;
405
        }
406
    }else{
407
        if(b>c){
408
            if(c>a) b=c;
409
            else    b=a;
410
        }
411
    }
412
    return b;
413
#endif
414
}
415

    
416
static inline int clip(int a, int amin, int amax)
417
{
418
    if (a < amin)
419
        return amin;
420
    else if (a > amax)
421
        return amax;
422
    else
423
        return a;
424
}
425

    
426
static inline int 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

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

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

    
526
     /* from section 2.2.1 of the 32-bit PowerPC PEM */
527
     __asm__ __volatile__(
528
         "1:\n"
529
         "mftbu  %2\n"
530
         "mftb   %0\n"
531
         "mftbu  %1\n"
532
         "cmpw   %2,%1\n"
533
         "bne    1b\n"
534
     : "=r"(tbl), "=r"(tbu), "=r"(temp)
535
     :
536
     : "cc");
537

    
538
     return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
539
}
540
#endif
541

    
542
#define START_TIMER \
543
uint64_t tend;\
544
uint64_t tstart= read_time();\
545

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

    
566
/* avoid usage of various functions */
567
#define malloc please_use_av_malloc
568
#define free please_use_av_free
569
#define realloc please_use_av_realloc
570
#define time time_is_forbidden_due_to_security_issues
571
#define rand rand_is_forbidden_due_to_state_trashing
572
#define srand srand_is_forbidden_due_to_state_trashing
573
#define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
574
#define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
575
#if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
576
#define printf please_use_av_log
577
#define fprintf please_use_av_log
578
#endif
579

    
580
#define CHECKED_ALLOCZ(p, size)\
581
{\
582
    p= av_mallocz(size);\
583
    if(p==NULL && (size)!=0){\
584
        perror("malloc");\
585
        goto fail;\
586
    }\
587
}
588

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

    
618
#endif /* HAVE_AV_CONFIG_H */
619

    
620
#endif /* COMMON_H */