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.

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