remove STATS code (probably hasnt been used for years ..., and its not completely...
[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
24dd8c61
LB
21#if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
22# define PIC
23#endif
24
420b073b 25#ifdef HAVE_AV_CONFIG_H
1a565432 26/* only include the following when compiling package */
9b59c92f
MN
27# include "config.h"
28
29# include <stdlib.h>
30# include <stdio.h>
31# include <string.h>
56c4a184 32# include <ctype.h>
9ff18a70 33# include <limits.h>
9b59c92f
MN
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
1a565432 44
fe1b62fb
ZK
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 }
5d4ce457
ZK
52#define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
53 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
fe1b62fb
ZK
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 }
bec89a84 62#define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
fe1b62fb
ZK
63#define AVOPTION_END() AVOPTION_SUB(NULL)
64
44f27b3a 65#endif /* HAVE_AV_CONFIG_H */
1a565432 66
02da51ec
FH
67/* Suppress restrict if it was not defined in config.h. */
68#ifndef restrict
9b59c92f 69# define restrict
02da51ec
FH
70#endif
71
f4ae934a 72#ifndef always_inline
d200cab6 73#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
9b59c92f 74# define always_inline __attribute__((always_inline)) inline
d200cab6 75#else
9b59c92f 76# define always_inline inline
d200cab6 77#endif
f4ae934a 78#endif
d200cab6 79
f4ae934a 80#ifndef attribute_used
5c0513bd
DB
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
f4ae934a 86#endif
5c0513bd 87
88730be6
MR
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
d2a9bddd 96#ifndef EMULATE_INTTYPES
7f965c1c
CF
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 */
978844cc 113#endif /* EMULATE_INTTYPES */
7f965c1c 114
86f77a49
FR
115#ifndef PRId64
116#define PRId64 "lld"
117#endif
118
e684b35d 119#ifndef PRIu64
86f77a49
FR
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
e684b35d
FR
137#endif
138
cc044c1c
BH
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
d2fbcb3b
MR
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
cc044c1c
BH
159#ifndef INT64_MIN
160#define INT64_MIN (-0x7fffffffffffffffLL-1)
161#endif
162
9ff5f175 163#ifndef INT64_MAX
2a24fe4c 164#define INT64_MAX int64_t_C(9223372036854775807)
9ff5f175
MN
165#endif
166
caa50878
MN
167#ifndef UINT64_MAX
168#define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
169#endif
170
19d053c5 171#ifdef EMULATE_FAST_INT
19d053c5
RS
172typedef signed char int_fast8_t;
173typedef signed int int_fast16_t;
174typedef signed int int_fast32_t;
175typedef unsigned char uint_fast8_t;
176typedef unsigned int uint_fast16_t;
177typedef unsigned int uint_fast32_t;
59d8efb3 178typedef uint64_t uint_fast64_t;
19d053c5
RS
179#endif
180
0ff93477 181#ifndef INT_BIT
9ff18a70 182# if INT_MAX != 2147483647
0ff93477
MN
183# define INT_BIT 64
184# else
185# define INT_BIT 32
186# endif
187#endif
188
1a565432
FB
189#ifdef CONFIG_WIN32
190
191/* windows */
192
5c668f46 193# if !defined(__MINGW32__) && !defined(__CYGWIN__)
0c1a9eda
ZK
194# define int64_t_C(c) (c ## i64)
195# define uint64_t_C(c) (c ## i64)
1a565432 196
49e17f84
MN
197# ifdef HAVE_AV_CONFIG_H
198# define inline __inline
199# endif
1a565432 200
9b59c92f 201# else
0c1a9eda
ZK
202# define int64_t_C(c) (c ## LL)
203# define uint64_t_C(c) (c ## ULL)
9b59c92f 204# endif /* __MINGW32__ */
a74127c0 205
49e17f84
MN
206# ifdef HAVE_AV_CONFIG_H
207# ifdef _DEBUG
208# define DEBUG
209# endif
de6d9b64 210
49e17f84
MN
211# define snprintf _snprintf
212# define vsnprintf _vsnprintf
ac44871c
GB
213
214# ifdef CONFIG_WINCE
215# define perror(a)
216# endif
217
49e17f84 218# endif
1a565432 219
f3ec2d46
SG
220/* CONFIG_WIN32 end */
221#elif defined (CONFIG_OS2)
222/* OS/2 EMX */
223
0c1a9eda
ZK
224#ifndef int64_t_C
225#define int64_t_C(c) (c ## LL)
226#define uint64_t_C(c) (c ## ULL)
f3ec2d46
SG
227#endif
228
f5fe9d5f
FB
229#ifdef HAVE_AV_CONFIG_H
230
f3ec2d46
SG
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
1a565432
FB
241
242/* unix */
243
f5fe9d5f
FB
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
1a565432 250
9b59c92f
MN
251# ifdef USE_FASTMEMCPY
252# include "fastmemcpy.h"
253# endif
9b59c92f 254# endif /* HAVE_AV_CONFIG_H */
a74127c0 255
f3ec2d46 256#endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
a74127c0 257
a74127c0
FB
258#ifdef HAVE_AV_CONFIG_H
259
320d060a
DB
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
9b59c92f 267# include "bswap.h"
96707bb7 268
a2c3bf82 269// Use rip-relative addressing if compiling PIC code on x86-64.
9b59c92f 270# if defined(__MINGW32__) || defined(__CYGWIN__) || \
814b648c 271 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
a2c3bf82
DH
272# if defined(ARCH_X86_64) && defined(PIC)
273# define MANGLE(a) "_" #a"(%%rip)"
274# else
275# define MANGLE(a) "_" #a
276# endif
9b59c92f 277# else
a2c3bf82
DH
278# if defined(ARCH_X86_64) && defined(PIC)
279# define MANGLE(a) #a"(%%rip)"
5f112e1f
SH
280# elif defined(CONFIG_DARWIN)
281# define MANGLE(a) "_" #a
a2c3bf82
DH
282# else
283# define MANGLE(a) #a
284# endif
9b59c92f 285# endif
6a3d7e36 286
d503f970
FB
287/* debug stuff */
288
72468a03 289# if !defined(DEBUG) && !defined(NDEBUG)
9b59c92f
MN
290# define NDEBUG
291# endif
292# include <assert.h>
1a565432 293
a74127c0 294/* dprintf macros */
2a86d50b 295# if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
a74127c0
FB
296
297inline void dprintf(const char* fmt,...) {}
298
9b59c92f 299# else
a74127c0 300
9b59c92f 301# ifdef DEBUG
365e75f8 302# define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
9b59c92f 303# else
dc7cb06a 304# define dprintf(fmt,...)
9b59c92f 305# endif
1a565432 306
9b59c92f 307# endif /* !CONFIG_WIN32 */
ac44871c
GB
308# ifdef CONFIG_WINCE
309# define abort()
310# endif
1a565432 311
9b879566 312# define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
935cdf09 313
073b013d 314//rounded divison & shift
10f3005f 315#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
d7e9533a
MN
316/* assume b>0 */
317#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
32cd20de 318#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
75460b0c 319
b8a78f41
MN
320#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
321#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
d7e9533a 322
d4961b35
B
323extern const uint32_t inverse[256];
324
053dea12 325#if defined(ARCH_X86) || defined(ARCH_X86_64)
d4961b35
B
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
115329f1 341
de6d9b64 342/* misc math functions */
320d060a 343extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
de6d9b64 344
a822a479 345static inline int av_log2(unsigned int v)
de6d9b64
FB
346{
347 int n;
348
349 n = 0;
350 if (v & 0xffff0000) {
351 v >>= 16;
352 n += 16;
353 }
354 if (v & 0xff00) {
355 v >>= 8;
356 n += 8;
357 }
c81f0349
MN
358 n += ff_log2_tab[v];
359
360 return n;
361}
362
363static inline int av_log2_16bit(unsigned int v)
364{
365 int n;
366
367 n = 0;
368 if (v & 0xff00) {
369 v >>= 8;
370 n += 8;
de6d9b64 371 }
c81f0349
MN
372 n += ff_log2_tab[v];
373
de6d9b64
FB
374 return n;
375}
376
45870f57
MN
377/* median of 3 */
378static inline int mid_pred(int a, int b, int c)
379{
7a62e94a
MN
380#if 0
381 int t= (a-b)&((a-b)>>31);
382 a-=t;
383 b+=t;
384 b-= (b-c)&((b-c)>>31);
385 b+= (a-b)&((a-b)>>31);
386
387 return b;
388#else
389 if(a>b){
390 if(c>b){
391 if(c>a) b=a;
392 else b=c;
393 }
394 }else{
395 if(b>c){
396 if(c>a) b=c;
397 else b=a;
398 }
399 }
400 return b;
401#endif
45870f57
MN
402}
403
77177335
AJ
404/**
405 * clip a signed integer value into the amin-amax range
406 * @param a value to clip
407 * @param amin minimum value of the clip range
408 * @param amax maximum value of the clip range
409 * @return cliped value
410 */
91029be7
MN
411static inline int clip(int a, int amin, int amax)
412{
413 if (a < amin)
414 return amin;
415 else if (a > amax)
416 return amax;
417 else
418 return a;
419}
420
77177335
AJ
421/**
422 * clip a signed integer value into the 0-255 range
423 * @param a value to clip
424 * @return cliped value
425 */
3a1fda0a 426static inline uint8_t clip_uint8(int a)
3ebc7e04
MN
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
9d82b0dd
MN
482#define GET_UTF8(val, GET_BYTE, ERROR)\
483 val= GET_BYTE;\
484 {\
485 int ones= 7 - av_log2(val ^ 255);\
486 if(ones==1)\
487 ERROR\
488 val&= 127>>ones;\
489 while(--ones > 0){\
490 int tmp= GET_BYTE - 128;\
491 if(tmp>>6)\
492 ERROR\
493 val= (val<<6) + tmp;\
494 }\
495 }
2ad1516a 496
7fd08ac1
MN
497#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
498#define COPY3_IF_LT(x,y,a,b,c,d)\
499asm volatile (\
bb270c08
DB
500 "cmpl %0, %3 \n\t"\
501 "cmovl %3, %0 \n\t"\
502 "cmovl %4, %1 \n\t"\
503 "cmovl %5, %2 \n\t"\
7fd08ac1
MN
504 : "+r" (x), "+r" (a), "+r" (c)\
505 : "r" (y), "r" (b), "r" (d)\
506);
507#else
508#define COPY3_IF_LT(x,y,a,b,c,d)\
509if((y)<(x)){\
510 (x)=(y);\
511 (a)=(b);\
512 (c)=(d);\
513}
514#endif
515
0775c88f 516#if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
8230cf02 517#if defined(ARCH_X86_64)
0775c88f 518static inline uint64_t read_time(void)
8230cf02 519{
bb270c08
DB
520 uint64_t a, d;
521 asm volatile( "rdtsc\n\t"
522 : "=a" (a), "=d" (d)
523 );
524 return (d << 32) | (a & 0xffffffff);
8230cf02 525}
0775c88f
MN
526#elif defined(ARCH_X86)
527static inline long long read_time(void)
b534c7f9 528{
bb270c08
DB
529 long long l;
530 asm volatile( "rdtsc\n\t"
531 : "=A" (l)
532 );
533 return l;
b534c7f9 534}
0775c88f
MN
535#else //FIXME check ppc64
536static inline uint64_t read_time(void)
537{
538 uint32_t tbu, tbl, temp;
539
540 /* from section 2.2.1 of the 32-bit PowerPC PEM */
541 __asm__ __volatile__(
542 "1:\n"
543 "mftbu %2\n"
544 "mftb %0\n"
545 "mftbu %1\n"
546 "cmpw %2,%1\n"
547 "bne 1b\n"
548 : "=r"(tbl), "=r"(tbu), "=r"(temp)
549 :
550 : "cc");
551
552 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
553}
8230cf02 554#endif
b534c7f9
MN
555
556#define START_TIMER \
b534c7f9 557uint64_t tend;\
0775c88f 558uint64_t tstart= read_time();\
b534c7f9
MN
559
560#define STOP_TIMER(id) \
0775c88f 561tend= read_time();\
d705e4a6
MN
562{\
563 static uint64_t tsum=0;\
564 static int tcount=0;\
565 static int tskip_count=0;\
566 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
567 tsum+= tend - tstart;\
568 tcount++;\
569 }else\
570 tskip_count++;\
571 if(256*256*256*64%(tcount+tskip_count)==0){\
e852beee 572 av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
d705e4a6 573 }\
b534c7f9 574}
0187e903 575#else
115329f1 576#define START_TIMER
0187e903 577#define STOP_TIMER(id) {}
b534c7f9
MN
578#endif
579
8e1e6f31
FB
580/* avoid usage of various functions */
581#define malloc please_use_av_malloc
582#define free please_use_av_free
583#define realloc please_use_av_realloc
7d1bbcd4
MN
584#define time time_is_forbidden_due_to_security_issues
585#define rand rand_is_forbidden_due_to_state_trashing
586#define srand srand_is_forbidden_due_to_state_trashing
2fc8ea24 587#define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
1f3f9507 588#define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
d705e4a6
MN
589#if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
590#define printf please_use_av_log
591#define fprintf please_use_av_log
592#endif
8e1e6f31 593
7bc9090a
MN
594#define CHECKED_ALLOCZ(p, size)\
595{\
596 p= av_mallocz(size);\
dc939fd5 597 if(p==NULL && (size)!=0){\
7bc9090a
MN
598 perror("malloc");\
599 goto fail;\
600 }\
601}
602
7df65455
DB
603#ifndef HAVE_LRINTF
604/* XXX: add ISOC specific test to avoid specific BSD testing. */
605/* better than nothing implementation. */
606/* btw, rintf() is existing on fbsd too -- alex */
607static always_inline long int lrintf(float x)
608{
609#ifdef CONFIG_WIN32
610# ifdef ARCH_X86
611 int32_t i;
612 asm volatile(
613 "fistpl %0\n\t"
614 : "=m" (i) : "t" (x) : "st"
615 );
616 return i;
617# else
618 /* XXX: incorrect, but make it compile */
619 return (int)(x + (x < 0 ? -0.5 : 0.5));
620# endif /* ARCH_X86 */
621#else
622 return (int)(rint(x));
623#endif /* CONFIG_WIN32 */
624}
625#else
626#ifndef _ISOC9X_SOURCE
627#define _ISOC9X_SOURCE
628#endif
629#include <math.h>
630#endif /* HAVE_LRINTF */
631
96707bb7
FB
632#endif /* HAVE_AV_CONFIG_H */
633
634#endif /* COMMON_H */