libavutil: Utility code from libavcodec moved to a separate library.
[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
e0c53ac8
AB
13#ifndef M_PI
14#define M_PI 3.14159265358979323846
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 */
105#endif /* HAVE_INTTYPES_H */
106
cc044c1c
BH
107#ifndef INT16_MIN
108#define INT16_MIN (-0x7fff-1)
109#endif
110
111#ifndef INT16_MAX
112#define INT16_MAX 0x7fff
113#endif
114
115#ifndef INT64_MIN
116#define INT64_MIN (-0x7fffffffffffffffLL-1)
117#endif
118
9ff5f175 119#ifndef INT64_MAX
2a24fe4c 120#define INT64_MAX int64_t_C(9223372036854775807)
9ff5f175
MN
121#endif
122
caa50878
MN
123#ifndef UINT64_MAX
124#define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
125#endif
126
19d053c5 127#ifdef EMULATE_FAST_INT
19d053c5
RS
128typedef signed char int_fast8_t;
129typedef signed int int_fast16_t;
130typedef signed int int_fast32_t;
131typedef unsigned char uint_fast8_t;
132typedef unsigned int uint_fast16_t;
133typedef unsigned int uint_fast32_t;
59d8efb3 134typedef uint64_t uint_fast64_t;
19d053c5
RS
135#endif
136
0ff93477 137#ifndef INT_BIT
9ff18a70 138# if INT_MAX != 2147483647
0ff93477
MN
139# define INT_BIT 64
140# else
141# define INT_BIT 32
142# endif
143#endif
144
19d053c5
RS
145#if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
146static inline float floorf(float f) {
147 return floor(f);
148}
149#endif
150
1a565432
FB
151#ifdef CONFIG_WIN32
152
153/* windows */
154
5c668f46 155# if !defined(__MINGW32__) && !defined(__CYGWIN__)
0c1a9eda
ZK
156# define int64_t_C(c) (c ## i64)
157# define uint64_t_C(c) (c ## i64)
1a565432 158
49e17f84
MN
159# ifdef HAVE_AV_CONFIG_H
160# define inline __inline
161# endif
1a565432 162
9b59c92f 163# else
0c1a9eda
ZK
164# define int64_t_C(c) (c ## LL)
165# define uint64_t_C(c) (c ## ULL)
9b59c92f 166# endif /* __MINGW32__ */
a74127c0 167
49e17f84
MN
168# ifdef HAVE_AV_CONFIG_H
169# ifdef _DEBUG
170# define DEBUG
171# endif
de6d9b64 172
49e17f84
MN
173# define snprintf _snprintf
174# define vsnprintf _vsnprintf
175# endif
1a565432 176
f3ec2d46
SG
177/* CONFIG_WIN32 end */
178#elif defined (CONFIG_OS2)
179/* OS/2 EMX */
180
0c1a9eda
ZK
181#ifndef int64_t_C
182#define int64_t_C(c) (c ## LL)
183#define uint64_t_C(c) (c ## ULL)
f3ec2d46
SG
184#endif
185
f5fe9d5f
FB
186#ifdef HAVE_AV_CONFIG_H
187
f3ec2d46
SG
188#ifdef USE_FASTMEMCPY
189#include "fastmemcpy.h"
190#endif
191
192#include <float.h>
193
194#endif /* HAVE_AV_CONFIG_H */
195
196/* CONFIG_OS2 end */
197#else
1a565432
FB
198
199/* unix */
200
f5fe9d5f
FB
201#ifndef int64_t_C
202#define int64_t_C(c) (c ## LL)
203#define uint64_t_C(c) (c ## ULL)
204#endif
205
206#ifdef HAVE_AV_CONFIG_H
1a565432 207
9b59c92f
MN
208# ifdef USE_FASTMEMCPY
209# include "fastmemcpy.h"
210# endif
9b59c92f 211# endif /* HAVE_AV_CONFIG_H */
a74127c0 212
f3ec2d46 213#endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
a74127c0 214
a74127c0
FB
215#ifdef HAVE_AV_CONFIG_H
216
9b59c92f 217# include "bswap.h"
96707bb7 218
a2c3bf82 219// Use rip-relative addressing if compiling PIC code on x86-64.
9b59c92f 220# if defined(__MINGW32__) || defined(__CYGWIN__) || \
814b648c 221 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
a2c3bf82
DH
222# if defined(ARCH_X86_64) && defined(PIC)
223# define MANGLE(a) "_" #a"(%%rip)"
224# else
225# define MANGLE(a) "_" #a
226# endif
9b59c92f 227# else
a2c3bf82
DH
228# if defined(ARCH_X86_64) && defined(PIC)
229# define MANGLE(a) #a"(%%rip)"
230# else
231# define MANGLE(a) #a
232# endif
9b59c92f 233# endif
6a3d7e36 234
d503f970
FB
235/* debug stuff */
236
9b59c92f
MN
237# ifndef DEBUG
238# define NDEBUG
239# endif
240# include <assert.h>
1a565432 241
a74127c0 242/* dprintf macros */
2a86d50b 243# if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
a74127c0
FB
244
245inline void dprintf(const char* fmt,...) {}
246
9b59c92f 247# else
a74127c0 248
9b59c92f 249# ifdef DEBUG
365e75f8 250# define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
9b59c92f 251# else
dc7cb06a 252# define dprintf(fmt,...)
9b59c92f 253# endif
1a565432 254
9b59c92f 255# endif /* !CONFIG_WIN32 */
1a565432 256
9b879566 257# define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
935cdf09 258
073b013d 259//rounded divison & shift
10f3005f 260#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
d7e9533a
MN
261/* assume b>0 */
262#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
32cd20de 263#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
75460b0c 264
b8a78f41
MN
265#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
266#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
d7e9533a 267
d4961b35
B
268extern const uint32_t inverse[256];
269
053dea12 270#if defined(ARCH_X86) || defined(ARCH_X86_64)
d4961b35
B
271# define FASTDIV(a,b) \
272 ({\
273 int ret,dmy;\
274 asm volatile(\
275 "mull %3"\
276 :"=d"(ret),"=a"(dmy)\
277 :"1"(a),"g"(inverse[b])\
278 );\
279 ret;\
280 })
281#elif defined(CONFIG_FASTDIV)
282# define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
283#else
284# define FASTDIV(a,b) ((a)/(b))
285#endif
286
de6d9b64
FB
287/* define it to include statistics code (useful only for optimizing
288 codec efficiency */
289//#define STATS
290
291#ifdef STATS
292
293enum {
294 ST_UNKNOWN,
295 ST_DC,
296 ST_INTRA_AC,
297 ST_INTER_AC,
298 ST_INTRA_MB,
299 ST_INTER_MB,
300 ST_MV,
301 ST_NB,
302};
303
304extern int st_current_index;
305extern unsigned int st_bit_counts[ST_NB];
306extern unsigned int st_out_bit_counts[ST_NB];
307
308void print_stats(void);
309#endif
310
311/* misc math functions */
c81f0349 312extern const uint8_t ff_log2_tab[256];
de6d9b64 313
a822a479 314static inline int av_log2(unsigned int v)
de6d9b64
FB
315{
316 int n;
317
318 n = 0;
319 if (v & 0xffff0000) {
320 v >>= 16;
321 n += 16;
322 }
323 if (v & 0xff00) {
324 v >>= 8;
325 n += 8;
326 }
c81f0349
MN
327 n += ff_log2_tab[v];
328
329 return n;
330}
331
332static inline int av_log2_16bit(unsigned int v)
333{
334 int n;
335
336 n = 0;
337 if (v & 0xff00) {
338 v >>= 8;
339 n += 8;
de6d9b64 340 }
c81f0349
MN
341 n += ff_log2_tab[v];
342
de6d9b64
FB
343 return n;
344}
345
45870f57
MN
346/* median of 3 */
347static inline int mid_pred(int a, int b, int c)
348{
7a62e94a
MN
349#if 0
350 int t= (a-b)&((a-b)>>31);
351 a-=t;
352 b+=t;
353 b-= (b-c)&((b-c)>>31);
354 b+= (a-b)&((a-b)>>31);
355
356 return b;
357#else
358 if(a>b){
359 if(c>b){
360 if(c>a) b=a;
361 else b=c;
362 }
363 }else{
364 if(b>c){
365 if(c>a) b=c;
366 else b=a;
367 }
368 }
369 return b;
370#endif
45870f57
MN
371}
372
91029be7
MN
373static inline int clip(int a, int amin, int amax)
374{
375 if (a < amin)
376 return amin;
377 else if (a > amax)
378 return amax;
379 else
380 return a;
381}
382
3ebc7e04
MN
383static inline int clip_uint8(int a)
384{
385 if (a&(~255)) return (-a)>>31;
386 else return a;
387}
388
9dbcbd92 389/* math */
c81f0349 390extern const uint8_t ff_sqrt_tab[128];
f36db5df 391
14bea432 392int64_t ff_gcd(int64_t a, int64_t b);
9dbcbd92 393
7fd08ac1
MN
394static inline int ff_sqrt(int a)
395{
396 int ret=0;
397 int s;
398 int ret_sq=0;
f36db5df
MN
399
400 if(a<128) return ff_sqrt_tab[a];
401
7fd08ac1
MN
402 for(s=15; s>=0; s--){
403 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
404 if(b<=a){
405 ret_sq=b;
406 ret+= 1<<s;
407 }
408 }
409 return ret;
410}
202ef8b8
MN
411
412/**
413 * converts fourcc string to int
414 */
6a85ec8d 415static inline int ff_get_fourcc(const char *s){
202ef8b8 416 assert( strlen(s)==4 );
966df5b6 417
202ef8b8
MN
418 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
419}
420
e8750b00
FR
421#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
422#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
423
424
053dea12 425#if defined(ARCH_X86) || defined(ARCH_X86_64)
2ad1516a
MN
426#define MASK_ABS(mask, level)\
427 asm volatile(\
428 "cdq \n\t"\
429 "xorl %1, %0 \n\t"\
430 "subl %1, %0 \n\t"\
431 : "+a" (level), "=&d" (mask)\
432 );
433#else
434#define MASK_ABS(mask, level)\
435 mask= level>>31;\
436 level= (level^mask)-mask;
437#endif
438
439
7fd08ac1
MN
440#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
441#define COPY3_IF_LT(x,y,a,b,c,d)\
442asm volatile (\
443 "cmpl %0, %3 \n\t"\
444 "cmovl %3, %0 \n\t"\
445 "cmovl %4, %1 \n\t"\
446 "cmovl %5, %2 \n\t"\
447 : "+r" (x), "+r" (a), "+r" (c)\
448 : "r" (y), "r" (b), "r" (d)\
449);
450#else
451#define COPY3_IF_LT(x,y,a,b,c,d)\
452if((y)<(x)){\
453 (x)=(y);\
454 (a)=(b);\
455 (c)=(d);\
456}
457#endif
458
0775c88f 459#if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
8230cf02 460#if defined(ARCH_X86_64)
0775c88f 461static inline uint64_t read_time(void)
8230cf02
AJ
462{
463 uint64_t a, d;
464 asm volatile( "rdtsc\n\t"
465 : "=a" (a), "=d" (d)
466 );
467 return (d << 32) | (a & 0xffffffff);
468}
0775c88f
MN
469#elif defined(ARCH_X86)
470static inline long long read_time(void)
b534c7f9
MN
471{
472 long long l;
473 asm volatile( "rdtsc\n\t"
474 : "=A" (l)
475 );
476 return l;
477}
0775c88f
MN
478#else //FIXME check ppc64
479static inline uint64_t read_time(void)
480{
481 uint32_t tbu, tbl, temp;
482
483 /* from section 2.2.1 of the 32-bit PowerPC PEM */
484 __asm__ __volatile__(
485 "1:\n"
486 "mftbu %2\n"
487 "mftb %0\n"
488 "mftbu %1\n"
489 "cmpw %2,%1\n"
490 "bne 1b\n"
491 : "=r"(tbl), "=r"(tbu), "=r"(temp)
492 :
493 : "cc");
494
495 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
496}
8230cf02 497#endif
b534c7f9
MN
498
499#define START_TIMER \
b534c7f9 500uint64_t tend;\
0775c88f 501uint64_t tstart= read_time();\
b534c7f9
MN
502
503#define STOP_TIMER(id) \
0775c88f 504tend= read_time();\
d705e4a6
MN
505{\
506 static uint64_t tsum=0;\
507 static int tcount=0;\
508 static int tskip_count=0;\
509 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
510 tsum+= tend - tstart;\
511 tcount++;\
512 }else\
513 tskip_count++;\
514 if(256*256*256*64%(tcount+tskip_count)==0){\
515 av_log(NULL, AV_LOG_DEBUG, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
516 }\
b534c7f9 517}
0187e903
MN
518#else
519#define START_TIMER
520#define STOP_TIMER(id) {}
b534c7f9
MN
521#endif
522
8e1e6f31
FB
523/* avoid usage of various functions */
524#define malloc please_use_av_malloc
525#define free please_use_av_free
526#define realloc please_use_av_realloc
7d1bbcd4
MN
527#define time time_is_forbidden_due_to_security_issues
528#define rand rand_is_forbidden_due_to_state_trashing
529#define srand srand_is_forbidden_due_to_state_trashing
2fc8ea24 530#define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
1f3f9507 531#define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
d705e4a6
MN
532#if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
533#define printf please_use_av_log
534#define fprintf please_use_av_log
535#endif
8e1e6f31 536
7bc9090a
MN
537#define CHECKED_ALLOCZ(p, size)\
538{\
539 p= av_mallocz(size);\
dc939fd5 540 if(p==NULL && (size)!=0){\
7bc9090a
MN
541 perror("malloc");\
542 goto fail;\
543 }\
544}
545
96707bb7
FB
546#endif /* HAVE_AV_CONFIG_H */
547
548#endif /* COMMON_H */