]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavutil/common.h
indent
[frescor/ffmpeg.git] / libavutil / common.h
1 /**
2  * @file common.h
3  * common internal 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 #if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
14 #    define PIC
15 #endif
16
17 #ifdef HAVE_AV_CONFIG_H
18 /* only include the following when compiling package */
19 #    include "config.h"
20
21 #    include <stdlib.h>
22 #    include <stdio.h>
23 #    include <string.h>
24 #    include <ctype.h>
25 #    include <limits.h>
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
36
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 }
44 #define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
45     { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
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 }
54 #define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
55 #define AVOPTION_END() AVOPTION_SUB(NULL)
56
57 #endif /* HAVE_AV_CONFIG_H */
58
59 /* Suppress restrict if it was not defined in config.h.  */
60 #ifndef restrict
61 #    define restrict
62 #endif
63
64 #ifndef always_inline
65 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
66 #    define always_inline __attribute__((always_inline)) inline
67 #else
68 #    define always_inline inline
69 #endif
70 #endif
71
72 #ifndef attribute_used
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
78 #endif
79
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
88 #ifndef EMULATE_INTTYPES
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     typedef signed long long   int64_t;
98     typedef unsigned long long uint64_t;
99 #endif /* EMULATE_INTTYPES */
100
101 #ifndef PRId64
102 #define PRId64 "lld"
103 #endif
104
105 #ifndef PRIu64
106 #define PRIu64 "llu"
107 #endif
108
109 #ifndef PRIx64
110 #define PRIx64 "llx"
111 #endif
112
113 #ifndef PRId32
114 #define PRId32 "d"
115 #endif
116
117 #ifndef PRIdFAST16
118 #define PRIdFAST16 PRId32
119 #endif
120
121 #ifndef PRIdFAST32
122 #define PRIdFAST32 PRId32
123 #endif
124
125 #ifndef INT16_MIN
126 #define INT16_MIN       (-0x7fff-1)
127 #endif
128
129 #ifndef INT16_MAX
130 #define INT16_MAX       0x7fff
131 #endif
132
133 #ifndef INT32_MIN
134 #define INT32_MIN       (-0x7fffffff-1)
135 #endif
136
137 #ifndef INT32_MAX
138 #define INT32_MAX       0x7fffffff
139 #endif
140
141 #ifndef UINT32_MAX
142 #define UINT32_MAX      0xffffffff
143 #endif
144
145 #ifndef INT64_MIN
146 #define INT64_MIN       (-0x7fffffffffffffffLL-1)
147 #endif
148
149 #ifndef INT64_MAX
150 #define INT64_MAX int64_t_C(9223372036854775807)
151 #endif
152
153 #ifndef UINT64_MAX
154 #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
155 #endif
156
157 #ifdef EMULATE_FAST_INT
158 typedef signed char int_fast8_t;
159 typedef signed int  int_fast16_t;
160 typedef signed int  int_fast32_t;
161 typedef unsigned char uint_fast8_t;
162 typedef unsigned int  uint_fast16_t;
163 typedef unsigned int  uint_fast32_t;
164 typedef uint64_t      uint_fast64_t;
165 #endif
166
167 #ifndef INT_BIT
168 #    if INT_MAX != 2147483647
169 #        define INT_BIT 64
170 #    else
171 #        define INT_BIT 32
172 #    endif
173 #endif
174
175 #ifndef int64_t_C
176 #define int64_t_C(c)     (c ## LL)
177 #define uint64_t_C(c)    (c ## ULL)
178 #endif
179
180 #ifdef HAVE_AV_CONFIG_H
181
182 #ifdef __MINGW32__
183 #    ifdef _DEBUG
184 #        define DEBUG
185 #    endif
186
187 #    define snprintf _snprintf
188 #    define vsnprintf _vsnprintf
189
190 #    ifdef CONFIG_WINCE
191 #        define perror(a)
192 #    endif
193
194 /* __MINGW32__ end */
195 #elif defined (CONFIG_OS2)
196 /* OS/2 EMX */
197
198 #include <float.h>
199
200 #endif /* !__MINGW32__ && CONFIG_OS2 */
201
202 #    ifdef USE_FASTMEMCPY
203 #        include "fastmemcpy.h"
204 #    endif
205
206 #if defined(__MINGW32__) && !defined(BUILD_AVUTIL) && defined(BUILD_SHARED_AV)
207 #  define FF_IMPORT_ATTR __declspec(dllimport)
208 #else
209 #  define FF_IMPORT_ATTR
210 #endif
211
212
213 #    include "bswap.h"
214
215 // Use rip-relative addressing if compiling PIC code on x86-64.
216 #    if defined(__MINGW32__) || defined(__CYGWIN__) || \
217         defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
218 #        if defined(ARCH_X86_64) && defined(PIC)
219 #            define MANGLE(a) "_" #a"(%%rip)"
220 #        else
221 #            define MANGLE(a) "_" #a
222 #        endif
223 #    else
224 #        if defined(ARCH_X86_64) && defined(PIC)
225 #            define MANGLE(a) #a"(%%rip)"
226 #        elif defined(CONFIG_DARWIN)
227 #            define MANGLE(a) "_" #a
228 #        else
229 #            define MANGLE(a) #a
230 #        endif
231 #    endif
232
233 /* debug stuff */
234
235 #    if !defined(DEBUG) && !defined(NDEBUG)
236 #        define NDEBUG
237 #    endif
238 #    include <assert.h>
239
240 /* dprintf macros */
241 #    ifdef DEBUG
242 #        define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
243 #    else
244 #        define dprintf(fmt,...)
245 #    endif
246
247 #    ifdef CONFIG_WINCE
248 #            define abort()
249 #    endif
250
251 #    define av_abort()      do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
252
253 //rounded divison & shift
254 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
255 /* assume b>0 */
256 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
257 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
258
259 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
260 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
261
262 extern const uint32_t inverse[256];
263
264 #if defined(ARCH_X86) || defined(ARCH_X86_64)
265 #    define FASTDIV(a,b) \
266     ({\
267         int ret,dmy;\
268         asm volatile(\
269             "mull %3"\
270             :"=d"(ret),"=a"(dmy)\
271             :"1"(a),"g"(inverse[b])\
272             );\
273         ret;\
274     })
275 #elif defined(CONFIG_FASTDIV)
276 #    define FASTDIV(a,b)   ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
277 #else
278 #    define FASTDIV(a,b)   ((a)/(b))
279 #endif
280
281 /* misc math functions */
282 extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
283
284 static inline int av_log2(unsigned int v)
285 {
286     int n;
287
288     n = 0;
289     if (v & 0xffff0000) {
290         v >>= 16;
291         n += 16;
292     }
293     if (v & 0xff00) {
294         v >>= 8;
295         n += 8;
296     }
297     n += ff_log2_tab[v];
298
299     return n;
300 }
301
302 static inline int av_log2_16bit(unsigned int v)
303 {
304     int n;
305
306     n = 0;
307     if (v & 0xff00) {
308         v >>= 8;
309         n += 8;
310     }
311     n += ff_log2_tab[v];
312
313     return n;
314 }
315
316 /* median of 3 */
317 static inline int mid_pred(int a, int b, int c)
318 {
319 #if 0
320     int t= (a-b)&((a-b)>>31);
321     a-=t;
322     b+=t;
323     b-= (b-c)&((b-c)>>31);
324     b+= (a-b)&((a-b)>>31);
325
326     return b;
327 #else
328     if(a>b){
329         if(c>b){
330             if(c>a) b=a;
331             else    b=c;
332         }
333     }else{
334         if(b>c){
335             if(c>a) b=c;
336             else    b=a;
337         }
338     }
339     return b;
340 #endif
341 }
342
343 /**
344  * clip a signed integer value into the amin-amax range
345  * @param a value to clip
346  * @param amin minimum value of the clip range
347  * @param amax maximum value of the clip range
348  * @return cliped value
349  */
350 static inline int clip(int a, int amin, int amax)
351 {
352     if (a < amin)      return amin;
353     else if (a > amax) return amax;
354     else               return a;
355 }
356
357 /**
358  * clip a signed integer value into the 0-255 range
359  * @param a value to clip
360  * @return cliped value
361  */
362 static inline uint8_t clip_uint8(int a)
363 {
364     if (a&(~255)) return (-a)>>31;
365     else          return a;
366 }
367
368 /* math */
369 extern FF_IMPORT_ATTR const uint8_t ff_sqrt_tab[128];
370
371 int64_t ff_gcd(int64_t a, int64_t b);
372
373 static inline int ff_sqrt(int a)
374 {
375     int ret=0;
376     int s;
377     int ret_sq=0;
378
379     if(a<128) return ff_sqrt_tab[a];
380
381     for(s=15; s>=0; s--){
382         int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
383         if(b<=a){
384             ret_sq=b;
385             ret+= 1<<s;
386         }
387     }
388     return ret;
389 }
390
391 /**
392  * converts fourcc string to int
393  */
394 static inline int ff_get_fourcc(const char *s){
395     assert( strlen(s)==4 );
396
397     return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
398 }
399
400 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
401 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
402
403
404 #if defined(ARCH_X86) || defined(ARCH_X86_64)
405 #define MASK_ABS(mask, level)\
406             asm volatile(\
407                 "cdq                    \n\t"\
408                 "xorl %1, %0            \n\t"\
409                 "subl %1, %0            \n\t"\
410                 : "+a" (level), "=&d" (mask)\
411             );
412 #else
413 #define MASK_ABS(mask, level)\
414             mask= level>>31;\
415             level= (level^mask)-mask;
416 #endif
417
418 #define GET_UTF8(val, GET_BYTE, ERROR)\
419     val= GET_BYTE;\
420     {\
421         int ones= 7 - av_log2(val ^ 255);\
422         if(ones==1)\
423             ERROR\
424         val&= 127>>ones;\
425         while(--ones > 0){\
426             int tmp= GET_BYTE - 128;\
427             if(tmp>>6)\
428                 ERROR\
429             val= (val<<6) + tmp;\
430         }\
431     }
432
433 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
434 #define COPY3_IF_LT(x,y,a,b,c,d)\
435 asm volatile (\
436     "cmpl %0, %3        \n\t"\
437     "cmovl %3, %0       \n\t"\
438     "cmovl %4, %1       \n\t"\
439     "cmovl %5, %2       \n\t"\
440     : "+r" (x), "+r" (a), "+r" (c)\
441     : "r" (y), "r" (b), "r" (d)\
442 );
443 #else
444 #define COPY3_IF_LT(x,y,a,b,c,d)\
445 if((y)<(x)){\
446      (x)=(y);\
447      (a)=(b);\
448      (c)=(d);\
449 }
450 #endif
451
452 #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
453 #if defined(ARCH_X86_64)
454 static inline uint64_t read_time(void)
455 {
456         uint64_t a, d;
457         asm volatile(   "rdtsc\n\t"
458                 : "=a" (a), "=d" (d)
459         );
460         return (d << 32) | (a & 0xffffffff);
461 }
462 #elif defined(ARCH_X86)
463 static inline long long read_time(void)
464 {
465         long long l;
466         asm volatile(   "rdtsc\n\t"
467                 : "=A" (l)
468         );
469         return l;
470 }
471 #else //FIXME check ppc64
472 static inline uint64_t read_time(void)
473 {
474     uint32_t tbu, tbl, temp;
475
476      /* from section 2.2.1 of the 32-bit PowerPC PEM */
477      __asm__ __volatile__(
478          "1:\n"
479          "mftbu  %2\n"
480          "mftb   %0\n"
481          "mftbu  %1\n"
482          "cmpw   %2,%1\n"
483          "bne    1b\n"
484      : "=r"(tbl), "=r"(tbu), "=r"(temp)
485      :
486      : "cc");
487
488      return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
489 }
490 #endif
491
492 #define START_TIMER \
493 uint64_t tend;\
494 uint64_t tstart= read_time();\
495
496 #define STOP_TIMER(id) \
497 tend= read_time();\
498 {\
499   static uint64_t tsum=0;\
500   static int tcount=0;\
501   static int tskip_count=0;\
502   if(tcount<2 || tend - tstart < 8*tsum/tcount){\
503       tsum+= tend - tstart;\
504       tcount++;\
505   }else\
506       tskip_count++;\
507   if(256*256*256*64%(tcount+tskip_count)==0){\
508       av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
509   }\
510 }
511 #else
512 #define START_TIMER
513 #define STOP_TIMER(id) {}
514 #endif
515
516 /* avoid usage of various functions */
517 #define malloc please_use_av_malloc
518 #define free please_use_av_free
519 #define realloc please_use_av_realloc
520 #define time time_is_forbidden_due_to_security_issues
521 #define rand rand_is_forbidden_due_to_state_trashing
522 #define srand srand_is_forbidden_due_to_state_trashing
523 #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
524 #define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
525 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
526 #define printf please_use_av_log
527 #define fprintf please_use_av_log
528 #endif
529
530 #define CHECKED_ALLOCZ(p, size)\
531 {\
532     p= av_mallocz(size);\
533     if(p==NULL && (size)!=0){\
534         perror("malloc");\
535         goto fail;\
536     }\
537 }
538
539 #ifndef HAVE_LRINTF
540 /* XXX: add ISOC specific test to avoid specific BSD testing. */
541 /* better than nothing implementation. */
542 /* btw, rintf() is existing on fbsd too -- alex */
543 static always_inline long int lrintf(float x)
544 {
545 #ifdef __MINGW32__
546 #  ifdef ARCH_X86
547     int32_t i;
548     asm volatile(
549         "fistpl %0\n\t"
550         : "=m" (i) : "t" (x) : "st"
551     );
552     return i;
553 #  else
554     /* XXX: incorrect, but make it compile */
555     return (int)(x + (x < 0 ? -0.5 : 0.5));
556 #  endif /* ARCH_X86 */
557 #else
558     return (int)(rint(x));
559 #endif /* __MINGW32__ */
560 }
561 #endif /* HAVE_LRINTF */
562
563 #endif /* HAVE_AV_CONFIG_H */
564
565 #endif /* COMMON_H */