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.

582 lines
13KB

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