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.

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