Create symbolic links for all library sonames and clean up Makefiles.
[libav.git] / libavutil / common.h
CommitLineData
983e3246
MN
1/**
2 * @file common.h
3 * common internal api header.
4 */
5
de6d9b64
FB
6#ifndef COMMON_H
7#define COMMON_H
8
4bdd9157 9#if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
9b59c92f 10# define CONFIG_WIN32
1a565432
FB
11#endif
12
765c3440
FO
13#if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(EMULATE_INTTYPES)
14# define EMULATE_INTTYPES
15#endif
16
e0c53ac8
AB
17#ifndef M_PI
18#define M_PI 3.14159265358979323846
19#endif
20
420b073b 21#ifdef HAVE_AV_CONFIG_H
1a565432 22/* only include the following when compiling package */
9b59c92f
MN
23# include "config.h"
24
25# include <stdlib.h>
26# include <stdio.h>
27# include <string.h>
56c4a184 28# include <ctype.h>
9ff18a70 29# include <limits.h>
9b59c92f
MN
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
1a565432 40
fe1b62fb
ZK
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 }
5d4ce457
ZK
48#define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
49 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
fe1b62fb
ZK
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 }
bec89a84 58#define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
fe1b62fb
ZK
59#define AVOPTION_END() AVOPTION_SUB(NULL)
60
44f27b3a 61#endif /* HAVE_AV_CONFIG_H */
1a565432 62
02da51ec
FH
63/* Suppress restrict if it was not defined in config.h. */
64#ifndef restrict
9b59c92f 65# define restrict
02da51ec
FH
66#endif
67
f4ae934a 68#ifndef always_inline
d200cab6 69#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
9b59c92f 70# define always_inline __attribute__((always_inline)) inline
d200cab6 71#else
9b59c92f 72# define always_inline inline
d200cab6 73#endif
f4ae934a 74#endif
d200cab6 75
f4ae934a 76#ifndef attribute_used
5c0513bd
DB
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
f4ae934a 82#endif
5c0513bd 83
88730be6
MR
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
d2a9bddd 92#ifndef EMULATE_INTTYPES
7f965c1c
CF
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 */
978844cc 109#endif /* EMULATE_INTTYPES */
7f965c1c 110
86f77a49
FR
111#ifndef PRId64
112#define PRId64 "lld"
113#endif
114
e684b35d 115#ifndef PRIu64
86f77a49
FR
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
e684b35d
FR
133#endif
134
cc044c1c
BH
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
9ff5f175 147#ifndef INT64_MAX
2a24fe4c 148#define INT64_MAX int64_t_C(9223372036854775807)
9ff5f175
MN
149#endif
150
caa50878
MN
151#ifndef UINT64_MAX
152#define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
153#endif
154
19d053c5 155#ifdef EMULATE_FAST_INT
19d053c5
RS
156typedef signed char int_fast8_t;
157typedef signed int int_fast16_t;
158typedef signed int int_fast32_t;
159typedef unsigned char uint_fast8_t;
160typedef unsigned int uint_fast16_t;
161typedef unsigned int uint_fast32_t;
59d8efb3 162typedef uint64_t uint_fast64_t;
19d053c5
RS
163#endif
164
0ff93477 165#ifndef INT_BIT
9ff18a70 166# if INT_MAX != 2147483647
0ff93477
MN
167# define INT_BIT 64
168# else
169# define INT_BIT 32
170# endif
171#endif
172
19d053c5 173#if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
115329f1
DB
174static inline float floorf(float f) {
175 return floor(f);
19d053c5
RS
176}
177#endif
178
1a565432
FB
179#ifdef CONFIG_WIN32
180
181/* windows */
182
5c668f46 183# if !defined(__MINGW32__) && !defined(__CYGWIN__)
0c1a9eda
ZK
184# define int64_t_C(c) (c ## i64)
185# define uint64_t_C(c) (c ## i64)
1a565432 186
49e17f84
MN
187# ifdef HAVE_AV_CONFIG_H
188# define inline __inline
189# endif
1a565432 190
9b59c92f 191# else
0c1a9eda
ZK
192# define int64_t_C(c) (c ## LL)
193# define uint64_t_C(c) (c ## ULL)
9b59c92f 194# endif /* __MINGW32__ */
a74127c0 195
49e17f84
MN
196# ifdef HAVE_AV_CONFIG_H
197# ifdef _DEBUG
198# define DEBUG
199# endif
de6d9b64 200
49e17f84
MN
201# define snprintf _snprintf
202# define vsnprintf _vsnprintf
203# endif
1a565432 204
f3ec2d46
SG
205/* CONFIG_WIN32 end */
206#elif defined (CONFIG_OS2)
207/* OS/2 EMX */
208
0c1a9eda
ZK
209#ifndef int64_t_C
210#define int64_t_C(c) (c ## LL)
211#define uint64_t_C(c) (c ## ULL)
f3ec2d46
SG
212#endif
213
f5fe9d5f
FB
214#ifdef HAVE_AV_CONFIG_H
215
f3ec2d46
SG
216#ifdef USE_FASTMEMCPY
217#include "fastmemcpy.h"
218#endif
219
220#include <float.h>
221
222#endif /* HAVE_AV_CONFIG_H */
223
224/* CONFIG_OS2 end */
225#else
1a565432
FB
226
227/* unix */
228
f5fe9d5f
FB
229#ifndef int64_t_C
230#define int64_t_C(c) (c ## LL)
231#define uint64_t_C(c) (c ## ULL)
232#endif
233
234#ifdef HAVE_AV_CONFIG_H
1a565432 235
9b59c92f
MN
236# ifdef USE_FASTMEMCPY
237# include "fastmemcpy.h"
238# endif
9b59c92f 239# endif /* HAVE_AV_CONFIG_H */
a74127c0 240
f3ec2d46 241#endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
a74127c0 242
a74127c0
FB
243#ifdef HAVE_AV_CONFIG_H
244
9b59c92f 245# include "bswap.h"
96707bb7 246
a2c3bf82 247// Use rip-relative addressing if compiling PIC code on x86-64.
9b59c92f 248# if defined(__MINGW32__) || defined(__CYGWIN__) || \
814b648c 249 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
a2c3bf82
DH
250# if defined(ARCH_X86_64) && defined(PIC)
251# define MANGLE(a) "_" #a"(%%rip)"
252# else
253# define MANGLE(a) "_" #a
254# endif
9b59c92f 255# else
a2c3bf82
DH
256# if defined(ARCH_X86_64) && defined(PIC)
257# define MANGLE(a) #a"(%%rip)"
258# else
259# define MANGLE(a) #a
260# endif
9b59c92f 261# endif
6a3d7e36 262
d503f970
FB
263/* debug stuff */
264
9b59c92f
MN
265# ifndef DEBUG
266# define NDEBUG
267# endif
268# include <assert.h>
1a565432 269
a74127c0 270/* dprintf macros */
2a86d50b 271# if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
a74127c0
FB
272
273inline void dprintf(const char* fmt,...) {}
274
9b59c92f 275# else
a74127c0 276
9b59c92f 277# ifdef DEBUG
365e75f8 278# define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
9b59c92f 279# else
dc7cb06a 280# define dprintf(fmt,...)
9b59c92f 281# endif
1a565432 282
9b59c92f 283# endif /* !CONFIG_WIN32 */
1a565432 284
9b879566 285# define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
935cdf09 286
073b013d 287//rounded divison & shift
10f3005f 288#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
d7e9533a
MN
289/* assume b>0 */
290#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
32cd20de 291#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
75460b0c 292
b8a78f41
MN
293#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
294#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
d7e9533a 295
d4961b35
B
296extern const uint32_t inverse[256];
297
053dea12 298#if defined(ARCH_X86) || defined(ARCH_X86_64)
d4961b35
B
299# define FASTDIV(a,b) \
300 ({\
301 int ret,dmy;\
302 asm volatile(\
303 "mull %3"\
304 :"=d"(ret),"=a"(dmy)\
305 :"1"(a),"g"(inverse[b])\
306 );\
307 ret;\
308 })
309#elif defined(CONFIG_FASTDIV)
310# define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
311#else
312# define FASTDIV(a,b) ((a)/(b))
313#endif
115329f1 314
de6d9b64
FB
315/* define it to include statistics code (useful only for optimizing
316 codec efficiency */
317//#define STATS
318
319#ifdef STATS
320
321enum {
322 ST_UNKNOWN,
323 ST_DC,
324 ST_INTRA_AC,
325 ST_INTER_AC,
326 ST_INTRA_MB,
327 ST_INTER_MB,
328 ST_MV,
329 ST_NB,
330};
331
332extern int st_current_index;
333extern unsigned int st_bit_counts[ST_NB];
334extern unsigned int st_out_bit_counts[ST_NB];
335
336void print_stats(void);
337#endif
338
339/* misc math functions */
c81f0349 340extern const uint8_t ff_log2_tab[256];
de6d9b64 341
a822a479 342static inline int av_log2(unsigned int v)
de6d9b64
FB
343{
344 int n;
345
346 n = 0;
347 if (v & 0xffff0000) {
348 v >>= 16;
349 n += 16;
350 }
351 if (v & 0xff00) {
352 v >>= 8;
353 n += 8;
354 }
c81f0349
MN
355 n += ff_log2_tab[v];
356
357 return n;
358}
359
360static inline int av_log2_16bit(unsigned int v)
361{
362 int n;
363
364 n = 0;
365 if (v & 0xff00) {
366 v >>= 8;
367 n += 8;
de6d9b64 368 }
c81f0349
MN
369 n += ff_log2_tab[v];
370
de6d9b64
FB
371 return n;
372}
373
45870f57
MN
374/* median of 3 */
375static inline int mid_pred(int a, int b, int c)
376{
7a62e94a
MN
377#if 0
378 int t= (a-b)&((a-b)>>31);
379 a-=t;
380 b+=t;
381 b-= (b-c)&((b-c)>>31);
382 b+= (a-b)&((a-b)>>31);
383
384 return b;
385#else
386 if(a>b){
387 if(c>b){
388 if(c>a) b=a;
389 else b=c;
390 }
391 }else{
392 if(b>c){
393 if(c>a) b=c;
394 else b=a;
395 }
396 }
397 return b;
398#endif
45870f57
MN
399}
400
91029be7
MN
401static inline int clip(int a, int amin, int amax)
402{
403 if (a < amin)
404 return amin;
405 else if (a > amax)
406 return amax;
407 else
408 return a;
409}
410
3ebc7e04
MN
411static inline int clip_uint8(int a)
412{
413 if (a&(~255)) return (-a)>>31;
414 else return a;
415}
416
9dbcbd92 417/* math */
c81f0349 418extern const uint8_t ff_sqrt_tab[128];
f36db5df 419
14bea432 420int64_t ff_gcd(int64_t a, int64_t b);
9dbcbd92 421
7fd08ac1
MN
422static inline int ff_sqrt(int a)
423{
424 int ret=0;
425 int s;
426 int ret_sq=0;
115329f1 427
f36db5df 428 if(a<128) return ff_sqrt_tab[a];
115329f1 429
7fd08ac1
MN
430 for(s=15; s>=0; s--){
431 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
432 if(b<=a){
433 ret_sq=b;
434 ret+= 1<<s;
435 }
436 }
437 return ret;
438}
202ef8b8
MN
439
440/**
441 * converts fourcc string to int
442 */
6a85ec8d 443static inline int ff_get_fourcc(const char *s){
202ef8b8 444 assert( strlen(s)==4 );
966df5b6 445
202ef8b8
MN
446 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
447}
448
e8750b00
FR
449#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
450#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
451
452
053dea12 453#if defined(ARCH_X86) || defined(ARCH_X86_64)
2ad1516a
MN
454#define MASK_ABS(mask, level)\
455 asm volatile(\
bb270c08
DB
456 "cdq \n\t"\
457 "xorl %1, %0 \n\t"\
458 "subl %1, %0 \n\t"\
459 : "+a" (level), "=&d" (mask)\
460 );
2ad1516a
MN
461#else
462#define MASK_ABS(mask, level)\
463 mask= level>>31;\
464 level= (level^mask)-mask;
465#endif
466
467
7fd08ac1
MN
468#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
469#define COPY3_IF_LT(x,y,a,b,c,d)\
470asm volatile (\
bb270c08
DB
471 "cmpl %0, %3 \n\t"\
472 "cmovl %3, %0 \n\t"\
473 "cmovl %4, %1 \n\t"\
474 "cmovl %5, %2 \n\t"\
7fd08ac1
MN
475 : "+r" (x), "+r" (a), "+r" (c)\
476 : "r" (y), "r" (b), "r" (d)\
477);
478#else
479#define COPY3_IF_LT(x,y,a,b,c,d)\
480if((y)<(x)){\
481 (x)=(y);\
482 (a)=(b);\
483 (c)=(d);\
484}
485#endif
486
0775c88f 487#if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
8230cf02 488#if defined(ARCH_X86_64)
0775c88f 489static inline uint64_t read_time(void)
8230cf02 490{
bb270c08
DB
491 uint64_t a, d;
492 asm volatile( "rdtsc\n\t"
493 : "=a" (a), "=d" (d)
494 );
495 return (d << 32) | (a & 0xffffffff);
8230cf02 496}
0775c88f
MN
497#elif defined(ARCH_X86)
498static inline long long read_time(void)
b534c7f9 499{
bb270c08
DB
500 long long l;
501 asm volatile( "rdtsc\n\t"
502 : "=A" (l)
503 );
504 return l;
b534c7f9 505}
0775c88f
MN
506#else //FIXME check ppc64
507static inline uint64_t read_time(void)
508{
509 uint32_t tbu, tbl, temp;
510
511 /* from section 2.2.1 of the 32-bit PowerPC PEM */
512 __asm__ __volatile__(
513 "1:\n"
514 "mftbu %2\n"
515 "mftb %0\n"
516 "mftbu %1\n"
517 "cmpw %2,%1\n"
518 "bne 1b\n"
519 : "=r"(tbl), "=r"(tbu), "=r"(temp)
520 :
521 : "cc");
522
523 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
524}
8230cf02 525#endif
b534c7f9
MN
526
527#define START_TIMER \
b534c7f9 528uint64_t tend;\
0775c88f 529uint64_t tstart= read_time();\
b534c7f9
MN
530
531#define STOP_TIMER(id) \
0775c88f 532tend= read_time();\
d705e4a6
MN
533{\
534 static uint64_t tsum=0;\
535 static int tcount=0;\
536 static int tskip_count=0;\
537 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
538 tsum+= tend - tstart;\
539 tcount++;\
540 }else\
541 tskip_count++;\
542 if(256*256*256*64%(tcount+tskip_count)==0){\
e852beee 543 av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
d705e4a6 544 }\
b534c7f9 545}
0187e903 546#else
115329f1 547#define START_TIMER
0187e903 548#define STOP_TIMER(id) {}
b534c7f9
MN
549#endif
550
8e1e6f31
FB
551/* avoid usage of various functions */
552#define malloc please_use_av_malloc
553#define free please_use_av_free
554#define realloc please_use_av_realloc
7d1bbcd4
MN
555#define time time_is_forbidden_due_to_security_issues
556#define rand rand_is_forbidden_due_to_state_trashing
557#define srand srand_is_forbidden_due_to_state_trashing
2fc8ea24 558#define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
1f3f9507 559#define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
d705e4a6
MN
560#if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
561#define printf please_use_av_log
562#define fprintf please_use_av_log
563#endif
8e1e6f31 564
7bc9090a
MN
565#define CHECKED_ALLOCZ(p, size)\
566{\
567 p= av_mallocz(size);\
dc939fd5 568 if(p==NULL && (size)!=0){\
7bc9090a
MN
569 perror("malloc");\
570 goto fail;\
571 }\
572}
573
7df65455
DB
574#ifndef HAVE_LRINTF
575/* XXX: add ISOC specific test to avoid specific BSD testing. */
576/* better than nothing implementation. */
577/* btw, rintf() is existing on fbsd too -- alex */
578static always_inline long int lrintf(float x)
579{
580#ifdef CONFIG_WIN32
581# ifdef ARCH_X86
582 int32_t i;
583 asm volatile(
584 "fistpl %0\n\t"
585 : "=m" (i) : "t" (x) : "st"
586 );
587 return i;
588# else
589 /* XXX: incorrect, but make it compile */
590 return (int)(x + (x < 0 ? -0.5 : 0.5));
591# endif /* ARCH_X86 */
592#else
593 return (int)(rint(x));
594#endif /* CONFIG_WIN32 */
595}
596#else
597#ifndef _ISOC9X_SOURCE
598#define _ISOC9X_SOURCE
599#endif
600#include <math.h>
601#endif /* HAVE_LRINTF */
602
96707bb7
FB
603#endif /* HAVE_AV_CONFIG_H */
604
605#endif /* COMMON_H */