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.

645 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. /* define it to include statistics code (useful only for optimizing
  271. codec efficiency */
  272. //#define STATS
  273. #ifdef STATS
  274. enum {
  275. ST_UNKNOWN,
  276. ST_DC,
  277. ST_INTRA_AC,
  278. ST_INTER_AC,
  279. ST_INTRA_MB,
  280. ST_INTER_MB,
  281. ST_MV,
  282. ST_NB,
  283. };
  284. extern int st_current_index;
  285. extern unsigned int st_bit_counts[ST_NB];
  286. extern unsigned int st_out_bit_counts[ST_NB];
  287. void print_stats(void);
  288. #endif
  289. /* misc math functions */
  290. extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
  291. static inline int av_log2(unsigned int v)
  292. {
  293. int n;
  294. n = 0;
  295. if (v & 0xffff0000) {
  296. v >>= 16;
  297. n += 16;
  298. }
  299. if (v & 0xff00) {
  300. v >>= 8;
  301. n += 8;
  302. }
  303. n += ff_log2_tab[v];
  304. return n;
  305. }
  306. static inline int av_log2_16bit(unsigned int v)
  307. {
  308. int n;
  309. n = 0;
  310. if (v & 0xff00) {
  311. v >>= 8;
  312. n += 8;
  313. }
  314. n += ff_log2_tab[v];
  315. return n;
  316. }
  317. /* median of 3 */
  318. static inline int mid_pred(int a, int b, int c)
  319. {
  320. #if 0
  321. int t= (a-b)&((a-b)>>31);
  322. a-=t;
  323. b+=t;
  324. b-= (b-c)&((b-c)>>31);
  325. b+= (a-b)&((a-b)>>31);
  326. return b;
  327. #else
  328. if(a>b){
  329. if(c>b){
  330. if(c>a) b=a;
  331. else b=c;
  332. }
  333. }else{
  334. if(b>c){
  335. if(c>a) b=c;
  336. else b=a;
  337. }
  338. }
  339. return b;
  340. #endif
  341. }
  342. /**
  343. * clip a signed integer value into the amin-amax range
  344. * @param a value to clip
  345. * @param amin minimum value of the clip range
  346. * @param amax maximum value of the clip range
  347. * @return cliped value
  348. */
  349. static inline int clip(int a, int amin, int amax)
  350. {
  351. if (a < amin)
  352. return amin;
  353. else if (a > amax)
  354. return amax;
  355. else
  356. return a;
  357. }
  358. /**
  359. * clip a signed integer value into the 0-255 range
  360. * @param a value to clip
  361. * @return cliped value
  362. */
  363. static inline uint8_t clip_uint8(int a)
  364. {
  365. if (a&(~255)) return (-a)>>31;
  366. else return a;
  367. }
  368. /* math */
  369. extern FF_IMPORT_ATTR const uint8_t ff_sqrt_tab[128];
  370. int64_t ff_gcd(int64_t a, int64_t b);
  371. static inline int ff_sqrt(int a)
  372. {
  373. int ret=0;
  374. int s;
  375. int ret_sq=0;
  376. if(a<128) return ff_sqrt_tab[a];
  377. for(s=15; s>=0; s--){
  378. int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
  379. if(b<=a){
  380. ret_sq=b;
  381. ret+= 1<<s;
  382. }
  383. }
  384. return ret;
  385. }
  386. /**
  387. * converts fourcc string to int
  388. */
  389. static inline int ff_get_fourcc(const char *s){
  390. assert( strlen(s)==4 );
  391. return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
  392. }
  393. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  394. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  395. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  396. #define MASK_ABS(mask, level)\
  397. asm volatile(\
  398. "cdq \n\t"\
  399. "xorl %1, %0 \n\t"\
  400. "subl %1, %0 \n\t"\
  401. : "+a" (level), "=&d" (mask)\
  402. );
  403. #else
  404. #define MASK_ABS(mask, level)\
  405. mask= level>>31;\
  406. level= (level^mask)-mask;
  407. #endif
  408. #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
  409. #define COPY3_IF_LT(x,y,a,b,c,d)\
  410. asm volatile (\
  411. "cmpl %0, %3 \n\t"\
  412. "cmovl %3, %0 \n\t"\
  413. "cmovl %4, %1 \n\t"\
  414. "cmovl %5, %2 \n\t"\
  415. : "+r" (x), "+r" (a), "+r" (c)\
  416. : "r" (y), "r" (b), "r" (d)\
  417. );
  418. #else
  419. #define COPY3_IF_LT(x,y,a,b,c,d)\
  420. if((y)<(x)){\
  421. (x)=(y);\
  422. (a)=(b);\
  423. (c)=(d);\
  424. }
  425. #endif
  426. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
  427. #if defined(ARCH_X86_64)
  428. static inline uint64_t read_time(void)
  429. {
  430. uint64_t a, d;
  431. asm volatile( "rdtsc\n\t"
  432. : "=a" (a), "=d" (d)
  433. );
  434. return (d << 32) | (a & 0xffffffff);
  435. }
  436. #elif defined(ARCH_X86)
  437. static inline long long read_time(void)
  438. {
  439. long long l;
  440. asm volatile( "rdtsc\n\t"
  441. : "=A" (l)
  442. );
  443. return l;
  444. }
  445. #else //FIXME check ppc64
  446. static inline uint64_t read_time(void)
  447. {
  448. uint32_t tbu, tbl, temp;
  449. /* from section 2.2.1 of the 32-bit PowerPC PEM */
  450. __asm__ __volatile__(
  451. "1:\n"
  452. "mftbu %2\n"
  453. "mftb %0\n"
  454. "mftbu %1\n"
  455. "cmpw %2,%1\n"
  456. "bne 1b\n"
  457. : "=r"(tbl), "=r"(tbu), "=r"(temp)
  458. :
  459. : "cc");
  460. return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
  461. }
  462. #endif
  463. #define START_TIMER \
  464. uint64_t tend;\
  465. uint64_t tstart= read_time();\
  466. #define STOP_TIMER(id) \
  467. tend= read_time();\
  468. {\
  469. static uint64_t tsum=0;\
  470. static int tcount=0;\
  471. static int tskip_count=0;\
  472. if(tcount<2 || tend - tstart < 8*tsum/tcount){\
  473. tsum+= tend - tstart;\
  474. tcount++;\
  475. }else\
  476. tskip_count++;\
  477. if(256*256*256*64%(tcount+tskip_count)==0){\
  478. av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
  479. }\
  480. }
  481. #else
  482. #define START_TIMER
  483. #define STOP_TIMER(id) {}
  484. #endif
  485. /* avoid usage of various functions */
  486. #define malloc please_use_av_malloc
  487. #define free please_use_av_free
  488. #define realloc please_use_av_realloc
  489. #define time time_is_forbidden_due_to_security_issues
  490. #define rand rand_is_forbidden_due_to_state_trashing
  491. #define srand srand_is_forbidden_due_to_state_trashing
  492. #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
  493. #define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
  494. #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
  495. #define printf please_use_av_log
  496. #define fprintf please_use_av_log
  497. #endif
  498. #define CHECKED_ALLOCZ(p, size)\
  499. {\
  500. p= av_mallocz(size);\
  501. if(p==NULL && (size)!=0){\
  502. perror("malloc");\
  503. goto fail;\
  504. }\
  505. }
  506. #ifndef HAVE_LRINTF
  507. /* XXX: add ISOC specific test to avoid specific BSD testing. */
  508. /* better than nothing implementation. */
  509. /* btw, rintf() is existing on fbsd too -- alex */
  510. static always_inline long int lrintf(float x)
  511. {
  512. #ifdef CONFIG_WIN32
  513. # ifdef ARCH_X86
  514. int32_t i;
  515. asm volatile(
  516. "fistpl %0\n\t"
  517. : "=m" (i) : "t" (x) : "st"
  518. );
  519. return i;
  520. # else
  521. /* XXX: incorrect, but make it compile */
  522. return (int)(x + (x < 0 ? -0.5 : 0.5));
  523. # endif /* ARCH_X86 */
  524. #else
  525. return (int)(rint(x));
  526. #endif /* CONFIG_WIN32 */
  527. }
  528. #else
  529. #ifndef _ISOC9X_SOURCE
  530. #define _ISOC9X_SOURCE
  531. #endif
  532. #include <math.h>
  533. #endif /* HAVE_LRINTF */
  534. #endif /* HAVE_AV_CONFIG_H */
  535. #endif /* COMMON_H */