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