You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

617 lines
14KB

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