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.

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