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.

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