Align the input buffer in ffplay, introduce a public macro for aligned declarations
[libav.git] / libavutil / common.h
1 /**
2 * @file common.h
3 * common internal and external api header.
4 */
5
6 #ifndef COMMON_H
7 #define COMMON_H
8
9 #ifndef M_PI
10 #define M_PI 3.14159265358979323846
11 #endif
12
13 #ifdef HAVE_AV_CONFIG_H
14 /* only include the following when compiling package */
15 # include "config.h"
16
17 # include <stdlib.h>
18 # include <stdio.h>
19 # include <string.h>
20 # include <ctype.h>
21 # include <limits.h>
22 # ifndef __BEOS__
23 # include <errno.h>
24 # else
25 # include "berrno.h"
26 # endif
27 # include <math.h>
28 #endif /* HAVE_AV_CONFIG_H */
29
30 /* Suppress restrict if it was not defined in config.h. */
31 #ifndef restrict
32 # define restrict
33 #endif
34
35 #ifndef always_inline
36 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
37 # define always_inline __attribute__((always_inline)) inline
38 #else
39 # define always_inline inline
40 #endif
41 #endif
42
43 #ifndef attribute_used
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
49 #endif
50
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
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
67 #ifndef EMULATE_INTTYPES
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;
76 typedef signed long long int64_t;
77 typedef unsigned long long uint64_t;
78 #endif /* EMULATE_INTTYPES */
79
80 #ifndef PRId64
81 #define PRId64 "lld"
82 #endif
83
84 #ifndef PRIu64
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
102 #endif
103
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
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
124 #ifndef INT64_MIN
125 #define INT64_MIN (-0x7fffffffffffffffLL-1)
126 #endif
127
128 #ifndef INT64_MAX
129 #define INT64_MAX int64_t_C(9223372036854775807)
130 #endif
131
132 #ifndef UINT64_MAX
133 #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
134 #endif
135
136 #ifdef EMULATE_FAST_INT
137 typedef signed char int_fast8_t;
138 typedef signed int int_fast16_t;
139 typedef signed int int_fast32_t;
140 typedef unsigned char uint_fast8_t;
141 typedef unsigned int uint_fast16_t;
142 typedef unsigned int uint_fast32_t;
143 typedef uint64_t uint_fast64_t;
144 #endif
145
146 #ifndef INT_BIT
147 # if INT_MAX != 2147483647
148 # define INT_BIT 64
149 # else
150 # define INT_BIT 32
151 # endif
152 #endif
153
154 #ifndef int64_t_C
155 #define int64_t_C(c) (c ## LL)
156 #define uint64_t_C(c) (c ## ULL)
157 #endif
158
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
166 #ifdef HAVE_AV_CONFIG_H
167 /* only include the following when compiling package */
168 # include "internal.h"
169 #endif
170
171 //rounded divison & shift
172 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
173 /* assume b>0 */
174 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
175 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
176
177 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
178 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
179
180 #define SWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
181
182 /* misc math functions */
183 extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
184
185 static inline int av_log2(unsigned int v)
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 }
198 n += ff_log2_tab[v];
199
200 return n;
201 }
202
203 static 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;
211 }
212 n += ff_log2_tab[v];
213
214 return n;
215 }
216
217 /* median of 3 */
218 static inline int mid_pred(int a, int b, int c)
219 {
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
242 }
243
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 */
251 static inline int clip(int a, int amin, int amax)
252 {
253 if (a < amin) return amin;
254 else if (a > amax) return amax;
255 else return a;
256 }
257
258 /**
259 * clip a signed integer value into the 0-255 range
260 * @param a value to clip
261 * @return cliped value
262 */
263 static inline uint8_t clip_uint8(int a)
264 {
265 if (a&(~255)) return (-a)>>31;
266 else return a;
267 }
268
269 /* math */
270 int64_t ff_gcd(int64_t a, int64_t b);
271
272 /**
273 * converts fourcc string to int
274 */
275 static inline int ff_get_fourcc(const char *s){
276 #ifdef HAVE_AV_CONFIG_H
277 assert( strlen(s)==4 );
278 #endif
279
280 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
281 }
282
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
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 }
301
302 #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
303 #if defined(ARCH_X86_64)
304 static inline uint64_t read_time(void)
305 {
306 uint64_t a, d;
307 asm volatile( "rdtsc\n\t"
308 : "=a" (a), "=d" (d)
309 );
310 return (d << 32) | (a & 0xffffffff);
311 }
312 #elif defined(ARCH_X86)
313 static inline long long read_time(void)
314 {
315 long long l;
316 asm volatile( "rdtsc\n\t"
317 : "=A" (l)
318 );
319 return l;
320 }
321 #else //FIXME check ppc64
322 static 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 }
340 #endif
341
342 #define START_TIMER \
343 uint64_t tend;\
344 uint64_t tstart= read_time();\
345
346 #define STOP_TIMER(id) \
347 tend= read_time();\
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){\
358 av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
359 }\
360 }
361 #else
362 #define START_TIMER
363 #define STOP_TIMER(id) {}
364 #endif
365
366 /* memory */
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
374 void *av_malloc(unsigned int size);
375 void *av_realloc(void *ptr, unsigned int size);
376 void av_free(void *ptr);
377
378 #endif /* COMMON_H */