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.

632 lines
14KB

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