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