]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavutil/common.h
Rename ABS macro to FFABS.
[frescor/ffmpeg.git] / libavutil / common.h
1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file common.h
23  * common internal and external api header.
24  */
25
26 #ifndef COMMON_H
27 #define COMMON_H
28
29 #ifndef M_PI
30 #define M_PI    3.14159265358979323846
31 #endif
32
33 #ifdef HAVE_AV_CONFIG_H
34 /* only include the following when compiling package */
35 #    include "config.h"
36
37 #    include <stdlib.h>
38 #    include <stdio.h>
39 #    include <string.h>
40 #    include <ctype.h>
41 #    include <limits.h>
42 #    ifndef __BEOS__
43 #        include <errno.h>
44 #    else
45 #        include "berrno.h"
46 #    endif
47 #    include <math.h>
48 #endif /* HAVE_AV_CONFIG_H */
49
50 /* Suppress restrict if it was not defined in config.h.  */
51 #ifndef restrict
52 #    define restrict
53 #endif
54
55 #ifndef always_inline
56 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
57 #    define always_inline __attribute__((always_inline)) inline
58 #else
59 #    define always_inline inline
60 #endif
61 #endif
62
63 #ifndef attribute_used
64 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
65 #    define attribute_used __attribute__((used))
66 #else
67 #    define attribute_used
68 #endif
69 #endif
70
71 #ifndef attribute_unused
72 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
73 #    define attribute_unused __attribute__((unused))
74 #else
75 #    define attribute_unused
76 #endif
77 #endif
78
79 #ifndef attribute_deprecated
80 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
81 #    define attribute_deprecated __attribute__((deprecated))
82 #else
83 #    define attribute_deprecated
84 #endif
85 #endif
86
87 #ifndef EMULATE_INTTYPES
88 #   include <inttypes.h>
89 #else
90     typedef signed char  int8_t;
91     typedef signed short int16_t;
92     typedef signed int   int32_t;
93     typedef unsigned char  uint8_t;
94     typedef unsigned short uint16_t;
95     typedef unsigned int   uint32_t;
96     typedef signed long long   int64_t;
97     typedef unsigned long long uint64_t;
98 #endif /* EMULATE_INTTYPES */
99
100 #ifndef PRId64
101 #define PRId64 "lld"
102 #endif
103
104 #ifndef PRIu64
105 #define PRIu64 "llu"
106 #endif
107
108 #ifndef PRIx64
109 #define PRIx64 "llx"
110 #endif
111
112 #ifndef PRId32
113 #define PRId32 "d"
114 #endif
115
116 #ifndef PRIdFAST16
117 #define PRIdFAST16 PRId32
118 #endif
119
120 #ifndef PRIdFAST32
121 #define PRIdFAST32 PRId32
122 #endif
123
124 #ifndef INT16_MIN
125 #define INT16_MIN       (-0x7fff-1)
126 #endif
127
128 #ifndef INT16_MAX
129 #define INT16_MAX       0x7fff
130 #endif
131
132 #ifndef INT32_MIN
133 #define INT32_MIN       (-0x7fffffff-1)
134 #endif
135
136 #ifndef INT32_MAX
137 #define INT32_MAX       0x7fffffff
138 #endif
139
140 #ifndef UINT32_MAX
141 #define UINT32_MAX      0xffffffff
142 #endif
143
144 #ifndef INT64_MIN
145 #define INT64_MIN       (-0x7fffffffffffffffLL-1)
146 #endif
147
148 #ifndef INT64_MAX
149 #define INT64_MAX int64_t_C(9223372036854775807)
150 #endif
151
152 #ifndef UINT64_MAX
153 #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
154 #endif
155
156 #ifdef EMULATE_FAST_INT
157 typedef signed char int_fast8_t;
158 typedef signed int  int_fast16_t;
159 typedef signed int  int_fast32_t;
160 typedef unsigned char uint_fast8_t;
161 typedef unsigned int  uint_fast16_t;
162 typedef unsigned int  uint_fast32_t;
163 typedef uint64_t      uint_fast64_t;
164 #endif
165
166 #ifndef INT_BIT
167 #    if INT_MAX != 2147483647
168 #        define INT_BIT 64
169 #    else
170 #        define INT_BIT 32
171 #    endif
172 #endif
173
174 #ifndef int64_t_C
175 #define int64_t_C(c)     (c ## LL)
176 #define uint64_t_C(c)    (c ## ULL)
177 #endif
178
179 #if defined(__MINGW32__) && !defined(BUILD_AVUTIL) && defined(BUILD_SHARED_AV)
180 #  define FF_IMPORT_ATTR __declspec(dllimport)
181 #else
182 #  define FF_IMPORT_ATTR
183 #endif
184
185
186 #ifdef HAVE_AV_CONFIG_H
187 /* only include the following when compiling package */
188 #    include "internal.h"
189 #endif
190
191 //rounded divison & shift
192 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
193 /* assume b>0 */
194 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
195 #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
196 #define FFSIGN(a) ((a) > 0 ? 1 : -1)
197
198 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
199 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
200
201 #define SWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
202
203 /* misc math functions */
204 extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
205
206 static inline int av_log2(unsigned int v)
207 {
208     int n;
209
210     n = 0;
211     if (v & 0xffff0000) {
212         v >>= 16;
213         n += 16;
214     }
215     if (v & 0xff00) {
216         v >>= 8;
217         n += 8;
218     }
219     n += ff_log2_tab[v];
220
221     return n;
222 }
223
224 static inline int av_log2_16bit(unsigned int v)
225 {
226     int n;
227
228     n = 0;
229     if (v & 0xff00) {
230         v >>= 8;
231         n += 8;
232     }
233     n += ff_log2_tab[v];
234
235     return n;
236 }
237
238 /* median of 3 */
239 static inline int mid_pred(int a, int b, int c)
240 {
241 #if (defined(ARCH_X86) && __CPU__ >= 686 || defined(ARCH_X86_64)) && !defined(RUNTIME_CPUDETECT)
242     int i=b;
243     asm volatile(
244         "cmp    %2, %1 \n\t"
245         "cmovg  %1, %0 \n\t"
246         "cmovg  %2, %1 \n\t"
247         "cmp    %3, %1 \n\t"
248         "cmovl  %3, %1 \n\t"
249         "cmp    %1, %0 \n\t"
250         "cmovg  %1, %0 \n\t"
251         :"+&r"(i), "+&r"(a)
252         :"r"(b), "r"(c)
253     );
254     return i;
255 #elif 0
256     int t= (a-b)&((a-b)>>31);
257     a-=t;
258     b+=t;
259     b-= (b-c)&((b-c)>>31);
260     b+= (a-b)&((a-b)>>31);
261
262     return b;
263 #else
264     if(a>b){
265         if(c>b){
266             if(c>a) b=a;
267             else    b=c;
268         }
269     }else{
270         if(b>c){
271             if(c>a) b=c;
272             else    b=a;
273         }
274     }
275     return b;
276 #endif
277 }
278
279 /**
280  * clip a signed integer value into the amin-amax range
281  * @param a value to clip
282  * @param amin minimum value of the clip range
283  * @param amax maximum value of the clip range
284  * @return cliped value
285  */
286 static inline int clip(int a, int amin, int amax)
287 {
288     if (a < amin)      return amin;
289     else if (a > amax) return amax;
290     else               return a;
291 }
292
293 /**
294  * clip a signed integer value into the 0-255 range
295  * @param a value to clip
296  * @return cliped value
297  */
298 static inline uint8_t clip_uint8(int a)
299 {
300     if (a&(~255)) return (-a)>>31;
301     else          return a;
302 }
303
304 /* math */
305 int64_t ff_gcd(int64_t a, int64_t b);
306
307 /**
308  * converts fourcc string to int
309  */
310 static inline int ff_get_fourcc(const char *s){
311 #ifdef HAVE_AV_CONFIG_H
312     assert( strlen(s)==4 );
313 #endif
314
315     return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
316 }
317
318 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
319 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
320
321
322 #define GET_UTF8(val, GET_BYTE, ERROR)\
323     val= GET_BYTE;\
324     {\
325         int ones= 7 - av_log2(val ^ 255);\
326         if(ones==1)\
327             ERROR\
328         val&= 127>>ones;\
329         while(--ones > 0){\
330             int tmp= GET_BYTE - 128;\
331             if(tmp>>6)\
332                 ERROR\
333             val= (val<<6) + tmp;\
334         }\
335     }
336
337 #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
338 #if defined(ARCH_X86_64)
339 static inline uint64_t read_time(void)
340 {
341         uint64_t a, d;
342         asm volatile(   "rdtsc\n\t"
343                 : "=a" (a), "=d" (d)
344         );
345         return (d << 32) | (a & 0xffffffff);
346 }
347 #elif defined(ARCH_X86)
348 static inline long long read_time(void)
349 {
350         long long l;
351         asm volatile(   "rdtsc\n\t"
352                 : "=A" (l)
353         );
354         return l;
355 }
356 #else //FIXME check ppc64
357 static inline uint64_t read_time(void)
358 {
359     uint32_t tbu, tbl, temp;
360
361      /* from section 2.2.1 of the 32-bit PowerPC PEM */
362      __asm__ __volatile__(
363          "1:\n"
364          "mftbu  %2\n"
365          "mftb   %0\n"
366          "mftbu  %1\n"
367          "cmpw   %2,%1\n"
368          "bne    1b\n"
369      : "=r"(tbl), "=r"(tbu), "=r"(temp)
370      :
371      : "cc");
372
373      return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
374 }
375 #endif
376
377 #define START_TIMER \
378 uint64_t tend;\
379 uint64_t tstart= read_time();\
380
381 #define STOP_TIMER(id) \
382 tend= read_time();\
383 {\
384   static uint64_t tsum=0;\
385   static int tcount=0;\
386   static int tskip_count=0;\
387   if(tcount<2 || tend - tstart < 8*tsum/tcount){\
388       tsum+= tend - tstart;\
389       tcount++;\
390   }else\
391       tskip_count++;\
392   if(((tcount+tskip_count)&(tcount+tskip_count-1))==0){\
393       av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
394   }\
395 }
396 #else
397 #define START_TIMER
398 #define STOP_TIMER(id) {}
399 #endif
400
401 /* memory */
402
403 #ifdef __GNUC__
404   #define DECLARE_ALIGNED(n,t,v)       t v __attribute__ ((aligned (n)))
405 #else
406   #define DECLARE_ALIGNED(n,t,v)      __declspec(align(n)) t v
407 #endif
408
409 /* memory */
410 void *av_malloc(unsigned int size);
411 void *av_realloc(void *ptr, unsigned int size);
412 void av_free(void *ptr);
413
414 void *av_mallocz(unsigned int size);
415 char *av_strdup(const char *s);
416 void av_freep(void *ptr);
417
418 #endif /* COMMON_H */