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.

566 lines
13KB

  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. typedef signed long long int64_t;
  84. typedef unsigned long long uint64_t;
  85. #endif /* EMULATE_INTTYPES */
  86. #ifndef PRId64
  87. #define PRId64 "lld"
  88. #endif
  89. #ifndef PRIu64
  90. #define PRIu64 "llu"
  91. #endif
  92. #ifndef PRIx64
  93. #define PRIx64 "llx"
  94. #endif
  95. #ifndef PRId32
  96. #define PRId32 "d"
  97. #endif
  98. #ifndef PRIdFAST16
  99. #define PRIdFAST16 PRId32
  100. #endif
  101. #ifndef PRIdFAST32
  102. #define PRIdFAST32 PRId32
  103. #endif
  104. #ifndef INT16_MIN
  105. #define INT16_MIN (-0x7fff-1)
  106. #endif
  107. #ifndef INT16_MAX
  108. #define INT16_MAX 0x7fff
  109. #endif
  110. #ifndef INT32_MIN
  111. #define INT32_MIN (-0x7fffffff-1)
  112. #endif
  113. #ifndef INT32_MAX
  114. #define INT32_MAX 0x7fffffff
  115. #endif
  116. #ifndef UINT32_MAX
  117. #define UINT32_MAX 0xffffffff
  118. #endif
  119. #ifndef INT64_MIN
  120. #define INT64_MIN (-0x7fffffffffffffffLL-1)
  121. #endif
  122. #ifndef INT64_MAX
  123. #define INT64_MAX int64_t_C(9223372036854775807)
  124. #endif
  125. #ifndef UINT64_MAX
  126. #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
  127. #endif
  128. #ifdef EMULATE_FAST_INT
  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. typedef uint64_t uint_fast64_t;
  136. #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
  144. #ifndef int64_t_C
  145. #define int64_t_C(c) (c ## LL)
  146. #define uint64_t_C(c) (c ## ULL)
  147. #endif
  148. #ifdef HAVE_AV_CONFIG_H
  149. #ifdef __MINGW32__
  150. # ifdef _DEBUG
  151. # define DEBUG
  152. # endif
  153. # define snprintf _snprintf
  154. # define vsnprintf _vsnprintf
  155. # ifdef CONFIG_WINCE
  156. # define perror(a)
  157. # endif
  158. /* __MINGW32__ end */
  159. #elif defined (CONFIG_OS2)
  160. /* OS/2 EMX */
  161. #include <float.h>
  162. #endif /* !__MINGW32__ && CONFIG_OS2 */
  163. # ifdef USE_FASTMEMCPY
  164. # include "fastmemcpy.h"
  165. # endif
  166. #if defined(__MINGW32__) && !defined(BUILD_AVUTIL) && defined(BUILD_SHARED_AV)
  167. # define FF_IMPORT_ATTR __declspec(dllimport)
  168. #else
  169. # define FF_IMPORT_ATTR
  170. #endif
  171. # include "bswap.h"
  172. // Use rip-relative addressing if compiling PIC code on x86-64.
  173. # if defined(__MINGW32__) || defined(__CYGWIN__) || \
  174. defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
  175. # if defined(ARCH_X86_64) && defined(PIC)
  176. # define MANGLE(a) "_" #a"(%%rip)"
  177. # else
  178. # define MANGLE(a) "_" #a
  179. # endif
  180. # else
  181. # if defined(ARCH_X86_64) && defined(PIC)
  182. # define MANGLE(a) #a"(%%rip)"
  183. # elif defined(CONFIG_DARWIN)
  184. # define MANGLE(a) "_" #a
  185. # else
  186. # define MANGLE(a) #a
  187. # endif
  188. # endif
  189. /* debug stuff */
  190. # if !defined(DEBUG) && !defined(NDEBUG)
  191. # define NDEBUG
  192. # endif
  193. # include <assert.h>
  194. /* dprintf macros */
  195. # ifdef DEBUG
  196. # define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
  197. # else
  198. # define dprintf(fmt,...)
  199. # endif
  200. # ifdef CONFIG_WINCE
  201. # define abort()
  202. # endif
  203. # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  204. //rounded divison & shift
  205. #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
  206. /* assume b>0 */
  207. #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
  208. #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
  209. #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
  210. #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
  211. extern const uint32_t inverse[256];
  212. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  213. # define FASTDIV(a,b) \
  214. ({\
  215. int ret,dmy;\
  216. asm volatile(\
  217. "mull %3"\
  218. :"=d"(ret),"=a"(dmy)\
  219. :"1"(a),"g"(inverse[b])\
  220. );\
  221. ret;\
  222. })
  223. #elif defined(CONFIG_FASTDIV)
  224. # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
  225. #else
  226. # define FASTDIV(a,b) ((a)/(b))
  227. #endif
  228. /* misc math functions */
  229. extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
  230. static inline int av_log2(unsigned int v)
  231. {
  232. int n;
  233. n = 0;
  234. if (v & 0xffff0000) {
  235. v >>= 16;
  236. n += 16;
  237. }
  238. if (v & 0xff00) {
  239. v >>= 8;
  240. n += 8;
  241. }
  242. n += ff_log2_tab[v];
  243. return n;
  244. }
  245. static inline int av_log2_16bit(unsigned int v)
  246. {
  247. int n;
  248. n = 0;
  249. if (v & 0xff00) {
  250. v >>= 8;
  251. n += 8;
  252. }
  253. n += ff_log2_tab[v];
  254. return n;
  255. }
  256. /* median of 3 */
  257. static inline int mid_pred(int a, int b, int c)
  258. {
  259. #if 0
  260. int t= (a-b)&((a-b)>>31);
  261. a-=t;
  262. b+=t;
  263. b-= (b-c)&((b-c)>>31);
  264. b+= (a-b)&((a-b)>>31);
  265. return b;
  266. #else
  267. if(a>b){
  268. if(c>b){
  269. if(c>a) b=a;
  270. else b=c;
  271. }
  272. }else{
  273. if(b>c){
  274. if(c>a) b=c;
  275. else b=a;
  276. }
  277. }
  278. return b;
  279. #endif
  280. }
  281. /**
  282. * clip a signed integer value into the amin-amax range
  283. * @param a value to clip
  284. * @param amin minimum value of the clip range
  285. * @param amax maximum value of the clip range
  286. * @return cliped value
  287. */
  288. static inline int clip(int a, int amin, int amax)
  289. {
  290. if (a < amin) return amin;
  291. else if (a > amax) return amax;
  292. else return a;
  293. }
  294. /**
  295. * clip a signed integer value into the 0-255 range
  296. * @param a value to clip
  297. * @return cliped value
  298. */
  299. static inline uint8_t clip_uint8(int a)
  300. {
  301. if (a&(~255)) return (-a)>>31;
  302. else return a;
  303. }
  304. /* math */
  305. extern FF_IMPORT_ATTR const uint8_t ff_sqrt_tab[128];
  306. int64_t ff_gcd(int64_t a, int64_t b);
  307. static inline int ff_sqrt(int a)
  308. {
  309. int ret=0;
  310. int s;
  311. int ret_sq=0;
  312. if(a<128) return ff_sqrt_tab[a];
  313. for(s=15; s>=0; s--){
  314. int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
  315. if(b<=a){
  316. ret_sq=b;
  317. ret+= 1<<s;
  318. }
  319. }
  320. return ret;
  321. }
  322. /**
  323. * converts fourcc string to int
  324. */
  325. static inline int ff_get_fourcc(const char *s){
  326. assert( strlen(s)==4 );
  327. return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
  328. }
  329. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  330. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  331. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  332. #define MASK_ABS(mask, level)\
  333. asm volatile(\
  334. "cdq \n\t"\
  335. "xorl %1, %0 \n\t"\
  336. "subl %1, %0 \n\t"\
  337. : "+a" (level), "=&d" (mask)\
  338. );
  339. #else
  340. #define MASK_ABS(mask, level)\
  341. mask= level>>31;\
  342. level= (level^mask)-mask;
  343. #endif
  344. #define GET_UTF8(val, GET_BYTE, ERROR)\
  345. val= GET_BYTE;\
  346. {\
  347. int ones= 7 - av_log2(val ^ 255);\
  348. if(ones==1)\
  349. ERROR\
  350. val&= 127>>ones;\
  351. while(--ones > 0){\
  352. int tmp= GET_BYTE - 128;\
  353. if(tmp>>6)\
  354. ERROR\
  355. val= (val<<6) + tmp;\
  356. }\
  357. }
  358. #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
  359. #define COPY3_IF_LT(x,y,a,b,c,d)\
  360. asm volatile (\
  361. "cmpl %0, %3 \n\t"\
  362. "cmovl %3, %0 \n\t"\
  363. "cmovl %4, %1 \n\t"\
  364. "cmovl %5, %2 \n\t"\
  365. : "+r" (x), "+r" (a), "+r" (c)\
  366. : "r" (y), "r" (b), "r" (d)\
  367. );
  368. #else
  369. #define COPY3_IF_LT(x,y,a,b,c,d)\
  370. if((y)<(x)){\
  371. (x)=(y);\
  372. (a)=(b);\
  373. (c)=(d);\
  374. }
  375. #endif
  376. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
  377. #if defined(ARCH_X86_64)
  378. static inline uint64_t read_time(void)
  379. {
  380. uint64_t a, d;
  381. asm volatile( "rdtsc\n\t"
  382. : "=a" (a), "=d" (d)
  383. );
  384. return (d << 32) | (a & 0xffffffff);
  385. }
  386. #elif defined(ARCH_X86)
  387. static inline long long read_time(void)
  388. {
  389. long long l;
  390. asm volatile( "rdtsc\n\t"
  391. : "=A" (l)
  392. );
  393. return l;
  394. }
  395. #else //FIXME check ppc64
  396. static inline uint64_t read_time(void)
  397. {
  398. uint32_t tbu, tbl, temp;
  399. /* from section 2.2.1 of the 32-bit PowerPC PEM */
  400. __asm__ __volatile__(
  401. "1:\n"
  402. "mftbu %2\n"
  403. "mftb %0\n"
  404. "mftbu %1\n"
  405. "cmpw %2,%1\n"
  406. "bne 1b\n"
  407. : "=r"(tbl), "=r"(tbu), "=r"(temp)
  408. :
  409. : "cc");
  410. return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
  411. }
  412. #endif
  413. #define START_TIMER \
  414. uint64_t tend;\
  415. uint64_t tstart= read_time();\
  416. #define STOP_TIMER(id) \
  417. tend= read_time();\
  418. {\
  419. static uint64_t tsum=0;\
  420. static int tcount=0;\
  421. static int tskip_count=0;\
  422. if(tcount<2 || tend - tstart < 8*tsum/tcount){\
  423. tsum+= tend - tstart;\
  424. tcount++;\
  425. }else\
  426. tskip_count++;\
  427. if(256*256*256*64%(tcount+tskip_count)==0){\
  428. av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
  429. }\
  430. }
  431. #else
  432. #define START_TIMER
  433. #define STOP_TIMER(id) {}
  434. #endif
  435. /* avoid usage of various functions */
  436. #define malloc please_use_av_malloc
  437. #define free please_use_av_free
  438. #define realloc please_use_av_realloc
  439. #define time time_is_forbidden_due_to_security_issues
  440. #define rand rand_is_forbidden_due_to_state_trashing
  441. #define srand srand_is_forbidden_due_to_state_trashing
  442. #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
  443. #define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
  444. #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
  445. #define printf please_use_av_log
  446. #define fprintf please_use_av_log
  447. #endif
  448. #define CHECKED_ALLOCZ(p, size)\
  449. {\
  450. p= av_mallocz(size);\
  451. if(p==NULL && (size)!=0){\
  452. perror("malloc");\
  453. goto fail;\
  454. }\
  455. }
  456. #ifndef HAVE_LRINTF
  457. /* XXX: add ISOC specific test to avoid specific BSD testing. */
  458. /* better than nothing implementation. */
  459. /* btw, rintf() is existing on fbsd too -- alex */
  460. static always_inline long int lrintf(float x)
  461. {
  462. #ifdef __MINGW32__
  463. # ifdef ARCH_X86
  464. int32_t i;
  465. asm volatile(
  466. "fistpl %0\n\t"
  467. : "=m" (i) : "t" (x) : "st"
  468. );
  469. return i;
  470. # else
  471. /* XXX: incorrect, but make it compile */
  472. return (int)(x + (x < 0 ? -0.5 : 0.5));
  473. # endif /* ARCH_X86 */
  474. #else
  475. return (int)(rint(x));
  476. #endif /* __MINGW32__ */
  477. }
  478. #endif /* HAVE_LRINTF */
  479. #endif /* HAVE_AV_CONFIG_H */
  480. #endif /* COMMON_H */