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