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.

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