VirtualBox

source: vbox/trunk/src/libs/ffmpeg-20060710/libavutil/common.h@ 9421

最後變更 在這個檔案從9421是 5776,由 vboxsync 提交於 17 年 前

ffmpeg: exported to OSE

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette