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