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