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