Align the input buffer in ffplay, introduce a public macro for aligned declarations
[libav.git] / libavutil / common.h
CommitLineData
983e3246
MN
1/**
2 * @file common.h
05020c89 3 * common internal and external api header.
983e3246
MN
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
420b073b 13#ifdef HAVE_AV_CONFIG_H
1a565432 14/* only include the following when compiling package */
9b59c92f
MN
15# include "config.h"
16
17# include <stdlib.h>
18# include <stdio.h>
19# include <string.h>
56c4a184 20# include <ctype.h>
9ff18a70 21# include <limits.h>
9b59c92f
MN
22# ifndef __BEOS__
23# include <errno.h>
24# else
25# include "berrno.h"
26# endif
27# include <math.h>
44f27b3a 28#endif /* HAVE_AV_CONFIG_H */
1a565432 29
02da51ec
FH
30/* Suppress restrict if it was not defined in config.h. */
31#ifndef restrict
9b59c92f 32# define restrict
02da51ec
FH
33#endif
34
f4ae934a 35#ifndef always_inline
d200cab6 36#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
9b59c92f 37# define always_inline __attribute__((always_inline)) inline
d200cab6 38#else
9b59c92f 39# define always_inline inline
d200cab6 40#endif
f4ae934a 41#endif
d200cab6 42
f4ae934a 43#ifndef attribute_used
5c0513bd
DB
44#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
45# define attribute_used __attribute__((used))
46#else
47# define attribute_used
48#endif
f4ae934a 49#endif
5c0513bd 50
88730be6
MR
51#ifndef attribute_unused
52#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
53# define attribute_unused __attribute__((unused))
54#else
55# define attribute_unused
56#endif
57#endif
58
955ab9a4
MN
59#ifndef attribute_deprecated
60#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
61# define attribute_deprecated __attribute__((deprecated))
62#else
63# define attribute_deprecated
64#endif
65#endif
66
d2a9bddd 67#ifndef EMULATE_INTTYPES
7f965c1c
CF
68# include <inttypes.h>
69#else
70 typedef signed char int8_t;
71 typedef signed short int16_t;
72 typedef signed int int32_t;
73 typedef unsigned char uint8_t;
74 typedef unsigned short uint16_t;
75 typedef unsigned int uint32_t;
b2b48c77
MR
76 typedef signed long long int64_t;
77 typedef unsigned long long uint64_t;
978844cc 78#endif /* EMULATE_INTTYPES */
7f965c1c 79
86f77a49
FR
80#ifndef PRId64
81#define PRId64 "lld"
82#endif
83
e684b35d 84#ifndef PRIu64
86f77a49
FR
85#define PRIu64 "llu"
86#endif
87
88#ifndef PRIx64
89#define PRIx64 "llx"
90#endif
91
92#ifndef PRId32
93#define PRId32 "d"
94#endif
95
96#ifndef PRIdFAST16
97#define PRIdFAST16 PRId32
98#endif
99
100#ifndef PRIdFAST32
101#define PRIdFAST32 PRId32
e684b35d
FR
102#endif
103
cc044c1c
BH
104#ifndef INT16_MIN
105#define INT16_MIN (-0x7fff-1)
106#endif
107
108#ifndef INT16_MAX
109#define INT16_MAX 0x7fff
110#endif
111
d2fbcb3b
MR
112#ifndef INT32_MIN
113#define INT32_MIN (-0x7fffffff-1)
114#endif
115
116#ifndef INT32_MAX
117#define INT32_MAX 0x7fffffff
118#endif
119
120#ifndef UINT32_MAX
121#define UINT32_MAX 0xffffffff
122#endif
123
cc044c1c
BH
124#ifndef INT64_MIN
125#define INT64_MIN (-0x7fffffffffffffffLL-1)
126#endif
127
9ff5f175 128#ifndef INT64_MAX
2a24fe4c 129#define INT64_MAX int64_t_C(9223372036854775807)
9ff5f175
MN
130#endif
131
caa50878
MN
132#ifndef UINT64_MAX
133#define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
134#endif
135
19d053c5 136#ifdef EMULATE_FAST_INT
19d053c5
RS
137typedef signed char int_fast8_t;
138typedef signed int int_fast16_t;
139typedef signed int int_fast32_t;
140typedef unsigned char uint_fast8_t;
141typedef unsigned int uint_fast16_t;
142typedef unsigned int uint_fast32_t;
59d8efb3 143typedef uint64_t uint_fast64_t;
19d053c5
RS
144#endif
145
0ff93477 146#ifndef INT_BIT
9ff18a70 147# if INT_MAX != 2147483647
0ff93477
MN
148# define INT_BIT 64
149# else
150# define INT_BIT 32
151# endif
152#endif
153
0339e2b4
MR
154#ifndef int64_t_C
155#define int64_t_C(c) (c ## LL)
156#define uint64_t_C(c) (c ## ULL)
157#endif
1a565432 158
320d060a
DB
159#if defined(__MINGW32__) && !defined(BUILD_AVUTIL) && defined(BUILD_SHARED_AV)
160# define FF_IMPORT_ATTR __declspec(dllimport)
161#else
162# define FF_IMPORT_ATTR
163#endif
164
165
05020c89
RD
166#ifdef HAVE_AV_CONFIG_H
167/* only include the following when compiling package */
168# include "internal.h"
169#endif
935cdf09 170
073b013d 171//rounded divison & shift
10f3005f 172#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
d7e9533a
MN
173/* assume b>0 */
174#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
32cd20de 175#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
75460b0c 176
b8a78f41
MN
177#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
178#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
d7e9533a 179
67eca72d
MN
180#define SWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
181
de6d9b64 182/* misc math functions */
320d060a 183extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
de6d9b64 184
a822a479 185static inline int av_log2(unsigned int v)
de6d9b64
FB
186{
187 int n;
188
189 n = 0;
190 if (v & 0xffff0000) {
191 v >>= 16;
192 n += 16;
193 }
194 if (v & 0xff00) {
195 v >>= 8;
196 n += 8;
197 }
c81f0349
MN
198 n += ff_log2_tab[v];
199
200 return n;
201}
202
203static inline int av_log2_16bit(unsigned int v)
204{
205 int n;
206
207 n = 0;
208 if (v & 0xff00) {
209 v >>= 8;
210 n += 8;
de6d9b64 211 }
c81f0349
MN
212 n += ff_log2_tab[v];
213
de6d9b64
FB
214 return n;
215}
216
45870f57
MN
217/* median of 3 */
218static inline int mid_pred(int a, int b, int c)
219{
7a62e94a
MN
220#if 0
221 int t= (a-b)&((a-b)>>31);
222 a-=t;
223 b+=t;
224 b-= (b-c)&((b-c)>>31);
225 b+= (a-b)&((a-b)>>31);
226
227 return b;
228#else
229 if(a>b){
230 if(c>b){
231 if(c>a) b=a;
232 else b=c;
233 }
234 }else{
235 if(b>c){
236 if(c>a) b=c;
237 else b=a;
238 }
239 }
240 return b;
241#endif
45870f57
MN
242}
243
77177335
AJ
244/**
245 * clip a signed integer value into the amin-amax range
246 * @param a value to clip
247 * @param amin minimum value of the clip range
248 * @param amax maximum value of the clip range
249 * @return cliped value
250 */
91029be7
MN
251static inline int clip(int a, int amin, int amax)
252{
18769c0a
MN
253 if (a < amin) return amin;
254 else if (a > amax) return amax;
255 else return a;
91029be7
MN
256}
257
77177335
AJ
258/**
259 * clip a signed integer value into the 0-255 range
260 * @param a value to clip
261 * @return cliped value
262 */
3a1fda0a 263static inline uint8_t clip_uint8(int a)
3ebc7e04
MN
264{
265 if (a&(~255)) return (-a)>>31;
266 else return a;
267}
268
9dbcbd92 269/* math */
14bea432 270int64_t ff_gcd(int64_t a, int64_t b);
9dbcbd92 271
202ef8b8
MN
272/**
273 * converts fourcc string to int
274 */
6a85ec8d 275static inline int ff_get_fourcc(const char *s){
05020c89 276#ifdef HAVE_AV_CONFIG_H
202ef8b8 277 assert( strlen(s)==4 );
05020c89 278#endif
966df5b6 279
202ef8b8
MN
280 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
281}
282
e8750b00
FR
283#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
284#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
285
286
9d82b0dd
MN
287#define GET_UTF8(val, GET_BYTE, ERROR)\
288 val= GET_BYTE;\
289 {\
290 int ones= 7 - av_log2(val ^ 255);\
291 if(ones==1)\
292 ERROR\
293 val&= 127>>ones;\
294 while(--ones > 0){\
295 int tmp= GET_BYTE - 128;\
296 if(tmp>>6)\
297 ERROR\
298 val= (val<<6) + tmp;\
299 }\
300 }
2ad1516a 301
0775c88f 302#if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
8230cf02 303#if defined(ARCH_X86_64)
0775c88f 304static inline uint64_t read_time(void)
8230cf02 305{
bb270c08
DB
306 uint64_t a, d;
307 asm volatile( "rdtsc\n\t"
308 : "=a" (a), "=d" (d)
309 );
310 return (d << 32) | (a & 0xffffffff);
8230cf02 311}
0775c88f
MN
312#elif defined(ARCH_X86)
313static inline long long read_time(void)
b534c7f9 314{
bb270c08
DB
315 long long l;
316 asm volatile( "rdtsc\n\t"
317 : "=A" (l)
318 );
319 return l;
b534c7f9 320}
0775c88f
MN
321#else //FIXME check ppc64
322static inline uint64_t read_time(void)
323{
324 uint32_t tbu, tbl, temp;
325
326 /* from section 2.2.1 of the 32-bit PowerPC PEM */
327 __asm__ __volatile__(
328 "1:\n"
329 "mftbu %2\n"
330 "mftb %0\n"
331 "mftbu %1\n"
332 "cmpw %2,%1\n"
333 "bne 1b\n"
334 : "=r"(tbl), "=r"(tbu), "=r"(temp)
335 :
336 : "cc");
337
338 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
339}
8230cf02 340#endif
b534c7f9
MN
341
342#define START_TIMER \
b534c7f9 343uint64_t tend;\
0775c88f 344uint64_t tstart= read_time();\
b534c7f9
MN
345
346#define STOP_TIMER(id) \
0775c88f 347tend= read_time();\
d705e4a6
MN
348{\
349 static uint64_t tsum=0;\
350 static int tcount=0;\
351 static int tskip_count=0;\
352 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
353 tsum+= tend - tstart;\
354 tcount++;\
355 }else\
356 tskip_count++;\
357 if(256*256*256*64%(tcount+tskip_count)==0){\
e852beee 358 av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
d705e4a6 359 }\
b534c7f9 360}
0187e903 361#else
115329f1 362#define START_TIMER
0187e903 363#define STOP_TIMER(id) {}
b534c7f9
MN
364#endif
365
cea8f6f3 366/* memory */
98145875
LB
367
368#ifdef __GNUC__
369 #define DECLARE_ALIGNED(n,t,v) t v __attribute__ ((aligned (n)))
370#else
371 #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
372#endif
373
cea8f6f3
LA
374void *av_malloc(unsigned int size);
375void *av_realloc(void *ptr, unsigned int size);
376void av_free(void *ptr);
377
96707bb7 378#endif /* COMMON_H */