r654: Initial revision
[cinelerra_cv.git] / quicktime / ffmpeg / libavcodec / common.h
blob7d5edb4a7de8e47d58079a6e1cf5b25767ae4e29
1 /**
2 * @file common.h
3 * common internal api header.
4 */
6 #ifndef COMMON_H
7 #define COMMON_H
9 #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
10 # define CONFIG_WIN32
11 #endif
13 #ifndef M_PI
14 #define M_PI 3.14159265358979323846
15 #endif
17 #ifdef HAVE_AV_CONFIG_H
18 /* only include the following when compiling package */
19 # include "config.h"
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>
33 # ifndef ENODATA
34 # define ENODATA 61
35 # endif
37 #include <stddef.h>
38 #ifndef offsetof
39 # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
40 #endif
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)
57 #endif /* HAVE_AV_CONFIG_H */
59 /* Suppress restrict if it was not defined in config.h. */
60 #ifndef restrict
61 # define restrict
62 #endif
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
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
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
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;
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 /* HAVE_INTTYPES_H */
107 #ifndef INT16_MIN
108 #define INT16_MIN (-0x7fff-1)
109 #endif
111 #ifndef INT16_MAX
112 #define INT16_MAX 0x7fff
113 #endif
115 #ifndef INT64_MIN
116 #define INT64_MIN (-0x7fffffffffffffffLL-1)
117 #endif
119 #ifndef INT64_MAX
120 #define INT64_MAX int64_t_C(9223372036854775807)
121 #endif
123 #ifndef UINT64_MAX
124 #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
125 #endif
127 #ifdef EMULATE_FAST_INT
128 typedef signed char int_fast8_t;
129 typedef signed int int_fast16_t;
130 typedef signed int int_fast32_t;
131 typedef unsigned char uint_fast8_t;
132 typedef unsigned int uint_fast16_t;
133 typedef unsigned int uint_fast32_t;
134 typedef uint64_t uint_fast64_t;
135 #endif
137 #ifndef INT_BIT
138 # if INT_MAX != 2147483647
139 # define INT_BIT 64
140 # else
141 # define INT_BIT 32
142 # endif
143 #endif
145 #if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
146 static inline float floorf(float f) {
147 return floor(f);
149 #endif
151 #ifdef CONFIG_WIN32
153 /* windows */
155 # if !defined(__MINGW32__) && !defined(__CYGWIN__)
156 # define int64_t_C(c) (c ## i64)
157 # define uint64_t_C(c) (c ## i64)
159 # ifdef HAVE_AV_CONFIG_H
160 # define inline __inline
161 # endif
163 # else
164 # define int64_t_C(c) (c ## LL)
165 # define uint64_t_C(c) (c ## ULL)
166 # endif /* __MINGW32__ */
168 # ifdef HAVE_AV_CONFIG_H
169 # ifdef _DEBUG
170 # define DEBUG
171 # endif
173 # define snprintf _snprintf
174 # define vsnprintf _vsnprintf
175 # endif
177 /* CONFIG_WIN32 end */
178 #elif defined (CONFIG_OS2)
179 /* OS/2 EMX */
181 #ifndef int64_t_C
182 #define int64_t_C(c) (c ## LL)
183 #define uint64_t_C(c) (c ## ULL)
184 #endif
186 #ifdef HAVE_AV_CONFIG_H
188 #ifdef USE_FASTMEMCPY
189 #include "fastmemcpy.h"
190 #endif
192 #include <float.h>
194 #endif /* HAVE_AV_CONFIG_H */
196 /* CONFIG_OS2 end */
197 #else
199 /* unix */
201 #ifndef int64_t_C
202 #define int64_t_C(c) (c ## LL)
203 #define uint64_t_C(c) (c ## ULL)
204 #endif
206 #ifdef HAVE_AV_CONFIG_H
208 # ifdef USE_FASTMEMCPY
209 # include "fastmemcpy.h"
210 # endif
211 # endif /* HAVE_AV_CONFIG_H */
213 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
215 #ifdef HAVE_AV_CONFIG_H
217 # include "bswap.h"
219 // Use rip-relative addressing if compiling PIC code on x86-64.
220 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
221 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
222 # if defined(ARCH_X86_64) && defined(PIC)
223 # define MANGLE(a) "_" #a"(%%rip)"
224 # else
225 # define MANGLE(a) "_" #a
226 # endif
227 # else
228 # if defined(ARCH_X86_64) && defined(PIC)
229 # define MANGLE(a) #a"(%%rip)"
230 # else
231 # define MANGLE(a) #a
232 # endif
233 # endif
235 /* debug stuff */
237 # ifndef DEBUG
238 # define NDEBUG
239 # endif
240 # include <assert.h>
242 /* dprintf macros */
243 # if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
245 inline void dprintf(const char* fmt,...) {}
247 # else
249 # ifdef DEBUG
250 # define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
251 # else
252 # define dprintf(fmt,...)
253 # endif
255 # endif /* !CONFIG_WIN32 */
257 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
259 //rounded divison & shift
260 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
261 /* assume b>0 */
262 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
263 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
265 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
266 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
268 extern const uint32_t inverse[256];
270 #if defined(ARCH_X86) || defined(ARCH_X86_64)
271 # define FASTDIV(a,b) \
273 int ret,dmy;\
274 asm volatile(\
275 "mull %3"\
276 :"=d"(ret),"=a"(dmy)\
277 :"1"(a),"g"(inverse[b])\
279 ret;\
281 #elif defined(CONFIG_FASTDIV)
282 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
283 #else
284 # define FASTDIV(a,b) ((a)/(b))
285 #endif
287 /* define it to include statistics code (useful only for optimizing
288 codec efficiency */
289 //#define STATS
291 #ifdef STATS
293 enum {
294 ST_UNKNOWN,
295 ST_DC,
296 ST_INTRA_AC,
297 ST_INTER_AC,
298 ST_INTRA_MB,
299 ST_INTER_MB,
300 ST_MV,
301 ST_NB,
304 extern int st_current_index;
305 extern unsigned int st_bit_counts[ST_NB];
306 extern unsigned int st_out_bit_counts[ST_NB];
308 void print_stats(void);
309 #endif
311 /* misc math functions */
312 extern const uint8_t ff_log2_tab[256];
314 static inline int av_log2(unsigned int v)
316 int n;
318 n = 0;
319 if (v & 0xffff0000) {
320 v >>= 16;
321 n += 16;
323 if (v & 0xff00) {
324 v >>= 8;
325 n += 8;
327 n += ff_log2_tab[v];
329 return n;
332 static inline int av_log2_16bit(unsigned int v)
334 int n;
336 n = 0;
337 if (v & 0xff00) {
338 v >>= 8;
339 n += 8;
341 n += ff_log2_tab[v];
343 return n;
346 /* median of 3 */
347 static inline int mid_pred(int a, int b, int c)
349 #if 0
350 int t= (a-b)&((a-b)>>31);
351 a-=t;
352 b+=t;
353 b-= (b-c)&((b-c)>>31);
354 b+= (a-b)&((a-b)>>31);
356 return b;
357 #else
358 if(a>b){
359 if(c>b){
360 if(c>a) b=a;
361 else b=c;
363 }else{
364 if(b>c){
365 if(c>a) b=c;
366 else b=a;
369 return b;
370 #endif
373 static inline int clip(int a, int amin, int amax)
375 if (a < amin)
376 return amin;
377 else if (a > amax)
378 return amax;
379 else
380 return a;
383 static inline int clip_uint8(int a)
385 if (a&(~255)) return (-a)>>31;
386 else return a;
389 /* math */
390 extern const uint8_t ff_sqrt_tab[128];
392 int64_t ff_gcd(int64_t a, int64_t b);
394 static inline int ff_sqrt(int a)
396 int ret=0;
397 int s;
398 int ret_sq=0;
400 if(a<128) return ff_sqrt_tab[a];
402 for(s=15; s>=0; s--){
403 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
404 if(b<=a){
405 ret_sq=b;
406 ret+= 1<<s;
409 return ret;
413 * converts fourcc string to int
415 static inline int ff_get_fourcc(const char *s){
416 assert( strlen(s)==4 );
418 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
421 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
422 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
425 #if defined(ARCH_X86) || defined(ARCH_X86_64)
426 #define MASK_ABS(mask, level)\
427 asm volatile(\
428 "cdq \n\t"\
429 "xorl %1, %0 \n\t"\
430 "subl %1, %0 \n\t"\
431 : "+a" (level), "=&d" (mask)\
433 #else
434 #define MASK_ABS(mask, level)\
435 mask= level>>31;\
436 level= (level^mask)-mask;
437 #endif
440 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
441 #define COPY3_IF_LT(x,y,a,b,c,d)\
442 asm volatile (\
443 "cmpl %0, %3 \n\t"\
444 "cmovl %3, %0 \n\t"\
445 "cmovl %4, %1 \n\t"\
446 "cmovl %5, %2 \n\t"\
447 : "+r" (x), "+r" (a), "+r" (c)\
448 : "r" (y), "r" (b), "r" (d)\
450 #else
451 #define COPY3_IF_LT(x,y,a,b,c,d)\
452 if((y)<(x)){\
453 (x)=(y);\
454 (a)=(b);\
455 (c)=(d);\
457 #endif
459 #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
460 #if defined(ARCH_X86_64)
461 static inline uint64_t read_time(void)
463 uint64_t a, d;
464 asm volatile( "rdtsc\n\t"
465 : "=a" (a), "=d" (d)
467 return (d << 32) | (a & 0xffffffff);
469 #elif defined(ARCH_X86)
470 static inline long long read_time(void)
472 long long l;
473 asm volatile( "rdtsc\n\t"
474 : "=A" (l)
476 return l;
478 #else //FIXME check ppc64
479 static inline uint64_t read_time(void)
481 uint32_t tbu, tbl, temp;
483 /* from section 2.2.1 of the 32-bit PowerPC PEM */
484 __asm__ __volatile__(
485 "1:\n"
486 "mftbu %2\n"
487 "mftb %0\n"
488 "mftbu %1\n"
489 "cmpw %2,%1\n"
490 "bne 1b\n"
491 : "=r"(tbl), "=r"(tbu), "=r"(temp)
493 : "cc");
495 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
497 #endif
499 #define START_TIMER \
500 uint64_t tend;\
501 uint64_t tstart= read_time();\
503 #define STOP_TIMER(id) \
504 tend= read_time();\
506 static uint64_t tsum=0;\
507 static int tcount=0;\
508 static int tskip_count=0;\
509 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
510 tsum+= tend - tstart;\
511 tcount++;\
512 }else\
513 tskip_count++;\
514 if(256*256*256*64%(tcount+tskip_count)==0){\
515 av_log(NULL, AV_LOG_DEBUG, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
518 #else
519 #define START_TIMER
520 #define STOP_TIMER(id) {}
521 #endif
523 /* avoid usage of various functions */
524 #define malloc please_use_av_malloc
525 #define free please_use_av_free
526 #define realloc please_use_av_realloc
527 #define time time_is_forbidden_due_to_security_issues
528 #define rand rand_is_forbidden_due_to_state_trashing
529 #define srand srand_is_forbidden_due_to_state_trashing
530 #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
531 #define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
532 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
533 #define printf please_use_av_log
534 #define fprintf please_use_av_log
535 #endif
537 #define CHECKED_ALLOCZ(p, size)\
539 p= av_mallocz(size);\
540 if(p==NULL && (size)!=0){\
541 perror("malloc");\
542 goto fail;\
546 #endif /* HAVE_AV_CONFIG_H */
548 #endif /* COMMON_H */