Split common.h in two parts, purely internal stuff (internal.h) and things
[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 EMULATE_INTTYPES
60 # include <inttypes.h>
61 #else
62 typedef signed char int8_t;
63 typedef signed short int16_t;
64 typedef signed int int32_t;
65 typedef unsigned char uint8_t;
66 typedef unsigned short uint16_t;
67 typedef unsigned int uint32_t;
68 typedef signed long long int64_t;
69 typedef unsigned long long uint64_t;
70 #endif /* EMULATE_INTTYPES */
71
72 #ifndef PRId64
73 #define PRId64 "lld"
74 #endif
75
76 #ifndef PRIu64
77 #define PRIu64 "llu"
78 #endif
79
80 #ifndef PRIx64
81 #define PRIx64 "llx"
82 #endif
83
84 #ifndef PRId32
85 #define PRId32 "d"
86 #endif
87
88 #ifndef PRIdFAST16
89 #define PRIdFAST16 PRId32
90 #endif
91
92 #ifndef PRIdFAST32
93 #define PRIdFAST32 PRId32
94 #endif
95
96 #ifndef INT16_MIN
97 #define INT16_MIN (-0x7fff-1)
98 #endif
99
100 #ifndef INT16_MAX
101 #define INT16_MAX 0x7fff
102 #endif
103
104 #ifndef INT32_MIN
105 #define INT32_MIN (-0x7fffffff-1)
106 #endif
107
108 #ifndef INT32_MAX
109 #define INT32_MAX 0x7fffffff
110 #endif
111
112 #ifndef UINT32_MAX
113 #define UINT32_MAX 0xffffffff
114 #endif
115
116 #ifndef INT64_MIN
117 #define INT64_MIN (-0x7fffffffffffffffLL-1)
118 #endif
119
120 #ifndef INT64_MAX
121 #define INT64_MAX int64_t_C(9223372036854775807)
122 #endif
123
124 #ifndef UINT64_MAX
125 #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
126 #endif
127
128 #ifdef EMULATE_FAST_INT
129 typedef signed char int_fast8_t;
130 typedef signed int int_fast16_t;
131 typedef signed int int_fast32_t;
132 typedef unsigned char uint_fast8_t;
133 typedef unsigned int uint_fast16_t;
134 typedef unsigned int uint_fast32_t;
135 typedef uint64_t uint_fast64_t;
136 #endif
137
138 #ifndef INT_BIT
139 # if INT_MAX != 2147483647
140 # define INT_BIT 64
141 # else
142 # define INT_BIT 32
143 # endif
144 #endif
145
146 #ifndef int64_t_C
147 #define int64_t_C(c) (c ## LL)
148 #define uint64_t_C(c) (c ## ULL)
149 #endif
150
151 #if defined(__MINGW32__) && !defined(BUILD_AVUTIL) && defined(BUILD_SHARED_AV)
152 # define FF_IMPORT_ATTR __declspec(dllimport)
153 #else
154 # define FF_IMPORT_ATTR
155 #endif
156
157
158 # include "bswap.h"
159
160 #ifdef HAVE_AV_CONFIG_H
161 /* only include the following when compiling package */
162 # include "internal.h"
163 #endif
164
165 //rounded divison & shift
166 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
167 /* assume b>0 */
168 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
169 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
170
171 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
172 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
173
174 /* misc math functions */
175 extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
176
177 static inline int av_log2(unsigned int v)
178 {
179 int n;
180
181 n = 0;
182 if (v & 0xffff0000) {
183 v >>= 16;
184 n += 16;
185 }
186 if (v & 0xff00) {
187 v >>= 8;
188 n += 8;
189 }
190 n += ff_log2_tab[v];
191
192 return n;
193 }
194
195 static inline int av_log2_16bit(unsigned int v)
196 {
197 int n;
198
199 n = 0;
200 if (v & 0xff00) {
201 v >>= 8;
202 n += 8;
203 }
204 n += ff_log2_tab[v];
205
206 return n;
207 }
208
209 /* median of 3 */
210 static inline int mid_pred(int a, int b, int c)
211 {
212 #if 0
213 int t= (a-b)&((a-b)>>31);
214 a-=t;
215 b+=t;
216 b-= (b-c)&((b-c)>>31);
217 b+= (a-b)&((a-b)>>31);
218
219 return b;
220 #else
221 if(a>b){
222 if(c>b){
223 if(c>a) b=a;
224 else b=c;
225 }
226 }else{
227 if(b>c){
228 if(c>a) b=c;
229 else b=a;
230 }
231 }
232 return b;
233 #endif
234 }
235
236 /**
237 * clip a signed integer value into the amin-amax range
238 * @param a value to clip
239 * @param amin minimum value of the clip range
240 * @param amax maximum value of the clip range
241 * @return cliped value
242 */
243 static inline int clip(int a, int amin, int amax)
244 {
245 if (a < amin) return amin;
246 else if (a > amax) return amax;
247 else return a;
248 }
249
250 /**
251 * clip a signed integer value into the 0-255 range
252 * @param a value to clip
253 * @return cliped value
254 */
255 static inline uint8_t clip_uint8(int a)
256 {
257 if (a&(~255)) return (-a)>>31;
258 else return a;
259 }
260
261 /* math */
262 int64_t ff_gcd(int64_t a, int64_t b);
263
264 /**
265 * converts fourcc string to int
266 */
267 static inline int ff_get_fourcc(const char *s){
268 #ifdef HAVE_AV_CONFIG_H
269 assert( strlen(s)==4 );
270 #endif
271
272 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
273 }
274
275 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
276 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
277
278
279 #define GET_UTF8(val, GET_BYTE, ERROR)\
280 val= GET_BYTE;\
281 {\
282 int ones= 7 - av_log2(val ^ 255);\
283 if(ones==1)\
284 ERROR\
285 val&= 127>>ones;\
286 while(--ones > 0){\
287 int tmp= GET_BYTE - 128;\
288 if(tmp>>6)\
289 ERROR\
290 val= (val<<6) + tmp;\
291 }\
292 }
293
294 #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
295 #if defined(ARCH_X86_64)
296 static inline uint64_t read_time(void)
297 {
298 uint64_t a, d;
299 asm volatile( "rdtsc\n\t"
300 : "=a" (a), "=d" (d)
301 );
302 return (d << 32) | (a & 0xffffffff);
303 }
304 #elif defined(ARCH_X86)
305 static inline long long read_time(void)
306 {
307 long long l;
308 asm volatile( "rdtsc\n\t"
309 : "=A" (l)
310 );
311 return l;
312 }
313 #else //FIXME check ppc64
314 static inline uint64_t read_time(void)
315 {
316 uint32_t tbu, tbl, temp;
317
318 /* from section 2.2.1 of the 32-bit PowerPC PEM */
319 __asm__ __volatile__(
320 "1:\n"
321 "mftbu %2\n"
322 "mftb %0\n"
323 "mftbu %1\n"
324 "cmpw %2,%1\n"
325 "bne 1b\n"
326 : "=r"(tbl), "=r"(tbu), "=r"(temp)
327 :
328 : "cc");
329
330 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
331 }
332 #endif
333
334 #define START_TIMER \
335 uint64_t tend;\
336 uint64_t tstart= read_time();\
337
338 #define STOP_TIMER(id) \
339 tend= read_time();\
340 {\
341 static uint64_t tsum=0;\
342 static int tcount=0;\
343 static int tskip_count=0;\
344 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
345 tsum+= tend - tstart;\
346 tcount++;\
347 }else\
348 tskip_count++;\
349 if(256*256*256*64%(tcount+tskip_count)==0){\
350 av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
351 }\
352 }
353 #else
354 #define START_TIMER
355 #define STOP_TIMER(id) {}
356 #endif
357
358 #endif /* COMMON_H */