c33812e697871bd6203966062a5e8927a7812ba6
[libav.git] / libavcodec / 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 #ifndef M_PI
14 #define M_PI 3.14159265358979323846
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 struct AVOption;
58 #ifdef HAVE_MMX
59 extern const struct AVOption avoptions_common[3 + 5];
60 #else
61 extern const struct AVOption avoptions_common[3];
62 #endif
63 extern const struct AVOption avoptions_workaround_bug[11];
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 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 /* HAVE_INTTYPES_H */
106
107 #ifndef INT16_MIN
108 #define INT16_MIN (-0x7fff-1)
109 #endif
110
111 #ifndef INT16_MAX
112 #define INT16_MAX 0x7fff
113 #endif
114
115 #ifndef INT64_MIN
116 #define INT64_MIN (-0x7fffffffffffffffLL-1)
117 #endif
118
119 #ifndef INT64_MAX
120 #define INT64_MAX int64_t_C(9223372036854775807)
121 #endif
122
123 #ifndef UINT64_MAX
124 #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
125 #endif
126
127 #ifdef EMULATE_FAST_INT
128 /* note that we don't emulate 64bit ints */
129 typedef signed char int_fast8_t;
130 typedef signed int int_fast16_t;
131 typedef signed int int_fast32_t;
132 typedef unsigned char uint_fast8_t;
133 typedef unsigned int uint_fast16_t;
134 typedef unsigned int uint_fast32_t;
135 #endif
136
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
144
145 #if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
146 static inline float floorf(float f) {
147 return floor(f);
148 }
149 #endif
150
151 #ifdef CONFIG_WIN32
152
153 /* windows */
154
155 # if !defined(__MINGW32__) && !defined(__CYGWIN__)
156 # define int64_t_C(c) (c ## i64)
157 # define uint64_t_C(c) (c ## i64)
158
159 # ifdef HAVE_AV_CONFIG_H
160 # define inline __inline
161 # endif
162
163 # else
164 # define int64_t_C(c) (c ## LL)
165 # define uint64_t_C(c) (c ## ULL)
166 # endif /* __MINGW32__ */
167
168 # ifdef HAVE_AV_CONFIG_H
169 # ifdef _DEBUG
170 # define DEBUG
171 # endif
172
173 # define snprintf _snprintf
174 # define vsnprintf _vsnprintf
175 # endif
176
177 /* CONFIG_WIN32 end */
178 #elif defined (CONFIG_OS2)
179 /* OS/2 EMX */
180
181 #ifndef int64_t_C
182 #define int64_t_C(c) (c ## LL)
183 #define uint64_t_C(c) (c ## ULL)
184 #endif
185
186 #ifdef HAVE_AV_CONFIG_H
187
188 #ifdef USE_FASTMEMCPY
189 #include "fastmemcpy.h"
190 #endif
191
192 #include <float.h>
193
194 #endif /* HAVE_AV_CONFIG_H */
195
196 /* CONFIG_OS2 end */
197 #else
198
199 /* unix */
200
201 #ifndef int64_t_C
202 #define int64_t_C(c) (c ## LL)
203 #define uint64_t_C(c) (c ## ULL)
204 #endif
205
206 #ifdef HAVE_AV_CONFIG_H
207
208 # ifdef USE_FASTMEMCPY
209 # include "fastmemcpy.h"
210 # endif
211 # endif /* HAVE_AV_CONFIG_H */
212
213 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
214
215 #ifdef HAVE_AV_CONFIG_H
216
217 # include "bswap.h"
218
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
234
235 /* debug stuff */
236
237 # ifndef DEBUG
238 # define NDEBUG
239 # endif
240 # include <assert.h>
241
242 /* dprintf macros */
243 # if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
244
245 inline void dprintf(const char* fmt,...) {}
246
247 # else
248
249 # ifdef DEBUG
250 # define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
251 # else
252 # define dprintf(fmt,...)
253 # endif
254
255 # endif /* !CONFIG_WIN32 */
256
257 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
258
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)))
264
265 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
266 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
267
268 extern const uint32_t inverse[256];
269
270 #if defined(ARCH_X86) || defined(ARCH_X86_64)
271 # define FASTDIV(a,b) \
272 ({\
273 int ret,dmy;\
274 asm volatile(\
275 "mull %3"\
276 :"=d"(ret),"=a"(dmy)\
277 :"1"(a),"g"(inverse[b])\
278 );\
279 ret;\
280 })
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
286
287 /* define it to include statistics code (useful only for optimizing
288 codec efficiency */
289 //#define STATS
290
291 #ifdef STATS
292
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,
302 };
303
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];
307
308 void print_stats(void);
309 #endif
310
311 /* misc math functions */
312 extern const uint8_t ff_log2_tab[256];
313
314 static inline int av_log2(unsigned int v)
315 {
316 int n;
317
318 n = 0;
319 if (v & 0xffff0000) {
320 v >>= 16;
321 n += 16;
322 }
323 if (v & 0xff00) {
324 v >>= 8;
325 n += 8;
326 }
327 n += ff_log2_tab[v];
328
329 return n;
330 }
331
332 static inline int av_log2_16bit(unsigned int v)
333 {
334 int n;
335
336 n = 0;
337 if (v & 0xff00) {
338 v >>= 8;
339 n += 8;
340 }
341 n += ff_log2_tab[v];
342
343 return n;
344 }
345
346 /* median of 3 */
347 static inline int mid_pred(int a, int b, int c)
348 {
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);
355
356 return b;
357 #else
358 if(a>b){
359 if(c>b){
360 if(c>a) b=a;
361 else b=c;
362 }
363 }else{
364 if(b>c){
365 if(c>a) b=c;
366 else b=a;
367 }
368 }
369 return b;
370 #endif
371 }
372
373 static inline int clip(int a, int amin, int amax)
374 {
375 if (a < amin)
376 return amin;
377 else if (a > amax)
378 return amax;
379 else
380 return a;
381 }
382
383 static inline int clip_uint8(int a)
384 {
385 if (a&(~255)) return (-a)>>31;
386 else return a;
387 }
388
389 /* math */
390 extern const uint8_t ff_sqrt_tab[128];
391
392 int64_t ff_gcd(int64_t a, int64_t b);
393
394 static inline int ff_sqrt(int a)
395 {
396 int ret=0;
397 int s;
398 int ret_sq=0;
399
400 if(a<128) return ff_sqrt_tab[a];
401
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;
407 }
408 }
409 return ret;
410 }
411
412 /**
413 * converts fourcc string to int
414 */
415 static inline int ff_get_fourcc(const char *s){
416 assert( strlen(s)==4 );
417
418 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
419 }
420
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))
423
424
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)\
432 );
433 #else
434 #define MASK_ABS(mask, level)\
435 mask= level>>31;\
436 level= (level^mask)-mask;
437 #endif
438
439
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)\
449 );
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);\
456 }
457 #endif
458
459 #if defined(ARCH_X86) || defined(ARCH_X86_64)
460 static inline long long rdtsc(void)
461 {
462 long long l;
463 asm volatile( "rdtsc\n\t"
464 : "=A" (l)
465 );
466 return l;
467 }
468
469 #define START_TIMER \
470 uint64_t tend;\
471 uint64_t tstart= rdtsc();\
472
473 #define STOP_TIMER(id) \
474 tend= rdtsc();\
475 {\
476 static uint64_t tsum=0;\
477 static int tcount=0;\
478 static int tskip_count=0;\
479 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
480 tsum+= tend - tstart;\
481 tcount++;\
482 }else\
483 tskip_count++;\
484 if(256*256*256*64%(tcount+tskip_count)==0){\
485 av_log(NULL, AV_LOG_DEBUG, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
486 }\
487 }
488 #else
489 #define START_TIMER
490 #define STOP_TIMER(id) {}
491 #endif
492
493 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
494
495 /* avoid usage of various functions */
496 #define malloc please_use_av_malloc
497 #define free please_use_av_free
498 #define realloc please_use_av_realloc
499 #define time time_is_forbidden_due_to_security_issues
500 #define rand rand_is_forbidden_due_to_state_trashing
501 #define srand srand_is_forbidden_due_to_state_trashing
502 #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
503 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
504 #define printf please_use_av_log
505 #define fprintf please_use_av_log
506 #endif
507
508 #define CHECKED_ALLOCZ(p, size)\
509 {\
510 p= av_mallocz(size);\
511 if(p==NULL && (size)!=0){\
512 perror("malloc");\
513 goto fail;\
514 }\
515 }
516
517 #endif /* HAVE_AV_CONFIG_H */
518
519 #endif /* COMMON_H */