add missing #includes
[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
ac44871c
GB
203
204# ifdef CONFIG_WINCE
205# define perror(a)
206# endif
207
49e17f84 208# endif
1a565432 209
f3ec2d46
SG
210/* CONFIG_WIN32 end */
211#elif defined (CONFIG_OS2)
212/* OS/2 EMX */
213
0c1a9eda
ZK
214#ifndef int64_t_C
215#define int64_t_C(c) (c ## LL)
216#define uint64_t_C(c) (c ## ULL)
f3ec2d46
SG
217#endif
218
f5fe9d5f
FB
219#ifdef HAVE_AV_CONFIG_H
220
f3ec2d46
SG
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
1a565432
FB
231
232/* unix */
233
f5fe9d5f
FB
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
1a565432 240
9b59c92f
MN
241# ifdef USE_FASTMEMCPY
242# include "fastmemcpy.h"
243# endif
9b59c92f 244# endif /* HAVE_AV_CONFIG_H */
a74127c0 245
f3ec2d46 246#endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
a74127c0 247
a74127c0
FB
248#ifdef HAVE_AV_CONFIG_H
249
320d060a
DB
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
9b59c92f 257# include "bswap.h"
96707bb7 258
a2c3bf82 259// Use rip-relative addressing if compiling PIC code on x86-64.
9b59c92f 260# if defined(__MINGW32__) || defined(__CYGWIN__) || \
814b648c 261 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
a2c3bf82
DH
262# if defined(ARCH_X86_64) && defined(PIC)
263# define MANGLE(a) "_" #a"(%%rip)"
264# else
265# define MANGLE(a) "_" #a
266# endif
9b59c92f 267# else
a2c3bf82
DH
268# if defined(ARCH_X86_64) && defined(PIC)
269# define MANGLE(a) #a"(%%rip)"
270# else
271# define MANGLE(a) #a
272# endif
9b59c92f 273# endif
6a3d7e36 274
d503f970
FB
275/* debug stuff */
276
9b59c92f
MN
277# ifndef DEBUG
278# define NDEBUG
279# endif
280# include <assert.h>
1a565432 281
a74127c0 282/* dprintf macros */
2a86d50b 283# if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
a74127c0
FB
284
285inline void dprintf(const char* fmt,...) {}
286
9b59c92f 287# else
a74127c0 288
9b59c92f 289# ifdef DEBUG
365e75f8 290# define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
9b59c92f 291# else
dc7cb06a 292# define dprintf(fmt,...)
9b59c92f 293# endif
1a565432 294
9b59c92f 295# endif /* !CONFIG_WIN32 */
ac44871c
GB
296# ifdef CONFIG_WINCE
297# define abort()
298# endif
1a565432 299
9b879566 300# define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
935cdf09 301
073b013d 302//rounded divison & shift
10f3005f 303#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
d7e9533a
MN
304/* assume b>0 */
305#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
32cd20de 306#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
75460b0c 307
b8a78f41
MN
308#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
309#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
d7e9533a 310
d4961b35
B
311extern const uint32_t inverse[256];
312
053dea12 313#if defined(ARCH_X86) || defined(ARCH_X86_64)
d4961b35
B
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
115329f1 329
de6d9b64
FB
330/* define it to include statistics code (useful only for optimizing
331 codec efficiency */
332//#define STATS
333
334#ifdef STATS
335
336enum {
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
347extern int st_current_index;
348extern unsigned int st_bit_counts[ST_NB];
349extern unsigned int st_out_bit_counts[ST_NB];
350
351void print_stats(void);
352#endif
353
354/* misc math functions */
320d060a 355extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
de6d9b64 356
a822a479 357static inline int av_log2(unsigned int v)
de6d9b64
FB
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 }
c81f0349
MN
370 n += ff_log2_tab[v];
371
372 return n;
373}
374
375static 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;
de6d9b64 383 }
c81f0349
MN
384 n += ff_log2_tab[v];
385
de6d9b64
FB
386 return n;
387}
388
45870f57
MN
389/* median of 3 */
390static inline int mid_pred(int a, int b, int c)
391{
7a62e94a
MN
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
45870f57
MN
414}
415
91029be7
MN
416static 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
3ebc7e04
MN
426static inline int clip_uint8(int a)
427{
428 if (a&(~255)) return (-a)>>31;
429 else return a;
430}
431
9dbcbd92 432/* math */
320d060a 433extern FF_IMPORT_ATTR const uint8_t ff_sqrt_tab[128];
f36db5df 434
14bea432 435int64_t ff_gcd(int64_t a, int64_t b);
9dbcbd92 436
7fd08ac1
MN
437static inline int ff_sqrt(int a)
438{
439 int ret=0;
440 int s;
441 int ret_sq=0;
115329f1 442
f36db5df 443 if(a<128) return ff_sqrt_tab[a];
115329f1 444
7fd08ac1
MN
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}
202ef8b8
MN
454
455/**
456 * converts fourcc string to int
457 */
6a85ec8d 458static inline int ff_get_fourcc(const char *s){
202ef8b8 459 assert( strlen(s)==4 );
966df5b6 460
202ef8b8
MN
461 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
462}
463
e8750b00
FR
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
053dea12 468#if defined(ARCH_X86) || defined(ARCH_X86_64)
2ad1516a
MN
469#define MASK_ABS(mask, level)\
470 asm volatile(\
bb270c08
DB
471 "cdq \n\t"\
472 "xorl %1, %0 \n\t"\
473 "subl %1, %0 \n\t"\
474 : "+a" (level), "=&d" (mask)\
475 );
2ad1516a
MN
476#else
477#define MASK_ABS(mask, level)\
478 mask= level>>31;\
479 level= (level^mask)-mask;
480#endif
481
482
7fd08ac1
MN
483#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
484#define COPY3_IF_LT(x,y,a,b,c,d)\
485asm volatile (\
bb270c08
DB
486 "cmpl %0, %3 \n\t"\
487 "cmovl %3, %0 \n\t"\
488 "cmovl %4, %1 \n\t"\
489 "cmovl %5, %2 \n\t"\
7fd08ac1
MN
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)\
495if((y)<(x)){\
496 (x)=(y);\
497 (a)=(b);\
498 (c)=(d);\
499}
500#endif
501
0775c88f 502#if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
8230cf02 503#if defined(ARCH_X86_64)
0775c88f 504static inline uint64_t read_time(void)
8230cf02 505{
bb270c08
DB
506 uint64_t a, d;
507 asm volatile( "rdtsc\n\t"
508 : "=a" (a), "=d" (d)
509 );
510 return (d << 32) | (a & 0xffffffff);
8230cf02 511}
0775c88f
MN
512#elif defined(ARCH_X86)
513static inline long long read_time(void)
b534c7f9 514{
bb270c08
DB
515 long long l;
516 asm volatile( "rdtsc\n\t"
517 : "=A" (l)
518 );
519 return l;
b534c7f9 520}
0775c88f
MN
521#else //FIXME check ppc64
522static 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}
8230cf02 540#endif
b534c7f9
MN
541
542#define START_TIMER \
b534c7f9 543uint64_t tend;\
0775c88f 544uint64_t tstart= read_time();\
b534c7f9
MN
545
546#define STOP_TIMER(id) \
0775c88f 547tend= read_time();\
d705e4a6
MN
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){\
e852beee 558 av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
d705e4a6 559 }\
b534c7f9 560}
0187e903 561#else
115329f1 562#define START_TIMER
0187e903 563#define STOP_TIMER(id) {}
b534c7f9
MN
564#endif
565
8e1e6f31
FB
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
7d1bbcd4
MN
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
2fc8ea24 573#define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
1f3f9507 574#define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
d705e4a6
MN
575#if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
576#define printf please_use_av_log
577#define fprintf please_use_av_log
578#endif
8e1e6f31 579
7bc9090a
MN
580#define CHECKED_ALLOCZ(p, size)\
581{\
582 p= av_mallocz(size);\
dc939fd5 583 if(p==NULL && (size)!=0){\
7bc9090a
MN
584 perror("malloc");\
585 goto fail;\
586 }\
587}
588
7df65455
DB
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 */
593static 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
96707bb7
FB
618#endif /* HAVE_AV_CONFIG_H */
619
620#endif /* COMMON_H */