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.

549 lines
12KB

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