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.

372 lines
7.5KB

  1. /**
  2. * @file common.h
  3. * common internal and external api header.
  4. */
  5. #ifndef COMMON_H
  6. #define COMMON_H
  7. #ifndef M_PI
  8. #define M_PI 3.14159265358979323846
  9. #endif
  10. #ifdef HAVE_AV_CONFIG_H
  11. /* only include the following when compiling package */
  12. # include "config.h"
  13. # include <stdlib.h>
  14. # include <stdio.h>
  15. # include <string.h>
  16. # include <ctype.h>
  17. # include <limits.h>
  18. # ifndef __BEOS__
  19. # include <errno.h>
  20. # else
  21. # include "berrno.h"
  22. # endif
  23. # include <math.h>
  24. #endif /* HAVE_AV_CONFIG_H */
  25. /* Suppress restrict if it was not defined in config.h. */
  26. #ifndef restrict
  27. # define restrict
  28. #endif
  29. #ifndef always_inline
  30. #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
  31. # define always_inline __attribute__((always_inline)) inline
  32. #else
  33. # define always_inline inline
  34. #endif
  35. #endif
  36. #ifndef attribute_used
  37. #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
  38. # define attribute_used __attribute__((used))
  39. #else
  40. # define attribute_used
  41. #endif
  42. #endif
  43. #ifndef attribute_unused
  44. #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
  45. # define attribute_unused __attribute__((unused))
  46. #else
  47. # define attribute_unused
  48. #endif
  49. #endif
  50. #ifndef attribute_deprecated
  51. #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
  52. # define attribute_deprecated __attribute__((deprecated))
  53. #else
  54. # define attribute_deprecated
  55. #endif
  56. #endif
  57. #ifndef EMULATE_INTTYPES
  58. # include <inttypes.h>
  59. #else
  60. typedef signed char int8_t;
  61. typedef signed short int16_t;
  62. typedef signed int int32_t;
  63. typedef unsigned char uint8_t;
  64. typedef unsigned short uint16_t;
  65. typedef unsigned int uint32_t;
  66. typedef signed long long int64_t;
  67. typedef unsigned long long uint64_t;
  68. #endif /* EMULATE_INTTYPES */
  69. #ifndef PRId64
  70. #define PRId64 "lld"
  71. #endif
  72. #ifndef PRIu64
  73. #define PRIu64 "llu"
  74. #endif
  75. #ifndef PRIx64
  76. #define PRIx64 "llx"
  77. #endif
  78. #ifndef PRId32
  79. #define PRId32 "d"
  80. #endif
  81. #ifndef PRIdFAST16
  82. #define PRIdFAST16 PRId32
  83. #endif
  84. #ifndef PRIdFAST32
  85. #define PRIdFAST32 PRId32
  86. #endif
  87. #ifndef INT16_MIN
  88. #define INT16_MIN (-0x7fff-1)
  89. #endif
  90. #ifndef INT16_MAX
  91. #define INT16_MAX 0x7fff
  92. #endif
  93. #ifndef INT32_MIN
  94. #define INT32_MIN (-0x7fffffff-1)
  95. #endif
  96. #ifndef INT32_MAX
  97. #define INT32_MAX 0x7fffffff
  98. #endif
  99. #ifndef UINT32_MAX
  100. #define UINT32_MAX 0xffffffff
  101. #endif
  102. #ifndef INT64_MIN
  103. #define INT64_MIN (-0x7fffffffffffffffLL-1)
  104. #endif
  105. #ifndef INT64_MAX
  106. #define INT64_MAX int64_t_C(9223372036854775807)
  107. #endif
  108. #ifndef UINT64_MAX
  109. #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
  110. #endif
  111. #ifdef EMULATE_FAST_INT
  112. typedef signed char int_fast8_t;
  113. typedef signed int int_fast16_t;
  114. typedef signed int int_fast32_t;
  115. typedef unsigned char uint_fast8_t;
  116. typedef unsigned int uint_fast16_t;
  117. typedef unsigned int uint_fast32_t;
  118. typedef uint64_t uint_fast64_t;
  119. #endif
  120. #ifndef INT_BIT
  121. # if INT_MAX != 2147483647
  122. # define INT_BIT 64
  123. # else
  124. # define INT_BIT 32
  125. # endif
  126. #endif
  127. #ifndef int64_t_C
  128. #define int64_t_C(c) (c ## LL)
  129. #define uint64_t_C(c) (c ## ULL)
  130. #endif
  131. #if defined(__MINGW32__) && !defined(BUILD_AVUTIL) && defined(BUILD_SHARED_AV)
  132. # define FF_IMPORT_ATTR __declspec(dllimport)
  133. #else
  134. # define FF_IMPORT_ATTR
  135. #endif
  136. #ifdef HAVE_AV_CONFIG_H
  137. /* only include the following when compiling package */
  138. # include "internal.h"
  139. #endif
  140. //rounded divison & shift
  141. #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
  142. /* assume b>0 */
  143. #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
  144. #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
  145. #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
  146. #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
  147. #define SWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
  148. /* misc math functions */
  149. extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
  150. static inline int av_log2(unsigned int v)
  151. {
  152. int n;
  153. n = 0;
  154. if (v & 0xffff0000) {
  155. v >>= 16;
  156. n += 16;
  157. }
  158. if (v & 0xff00) {
  159. v >>= 8;
  160. n += 8;
  161. }
  162. n += ff_log2_tab[v];
  163. return n;
  164. }
  165. static inline int av_log2_16bit(unsigned int v)
  166. {
  167. int n;
  168. n = 0;
  169. if (v & 0xff00) {
  170. v >>= 8;
  171. n += 8;
  172. }
  173. n += ff_log2_tab[v];
  174. return n;
  175. }
  176. /* median of 3 */
  177. static inline int mid_pred(int a, int b, int c)
  178. {
  179. #if 0
  180. int t= (a-b)&((a-b)>>31);
  181. a-=t;
  182. b+=t;
  183. b-= (b-c)&((b-c)>>31);
  184. b+= (a-b)&((a-b)>>31);
  185. return b;
  186. #else
  187. if(a>b){
  188. if(c>b){
  189. if(c>a) b=a;
  190. else b=c;
  191. }
  192. }else{
  193. if(b>c){
  194. if(c>a) b=c;
  195. else b=a;
  196. }
  197. }
  198. return b;
  199. #endif
  200. }
  201. /**
  202. * clip a signed integer value into the amin-amax range
  203. * @param a value to clip
  204. * @param amin minimum value of the clip range
  205. * @param amax maximum value of the clip range
  206. * @return cliped value
  207. */
  208. static inline int clip(int a, int amin, int amax)
  209. {
  210. if (a < amin) return amin;
  211. else if (a > amax) return amax;
  212. else return a;
  213. }
  214. /**
  215. * clip a signed integer value into the 0-255 range
  216. * @param a value to clip
  217. * @return cliped value
  218. */
  219. static inline uint8_t clip_uint8(int a)
  220. {
  221. if (a&(~255)) return (-a)>>31;
  222. else return a;
  223. }
  224. /* math */
  225. int64_t ff_gcd(int64_t a, int64_t b);
  226. /**
  227. * converts fourcc string to int
  228. */
  229. static inline int ff_get_fourcc(const char *s){
  230. #ifdef HAVE_AV_CONFIG_H
  231. assert( strlen(s)==4 );
  232. #endif
  233. return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
  234. }
  235. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  236. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  237. #define GET_UTF8(val, GET_BYTE, ERROR)\
  238. val= GET_BYTE;\
  239. {\
  240. int ones= 7 - av_log2(val ^ 255);\
  241. if(ones==1)\
  242. ERROR\
  243. val&= 127>>ones;\
  244. while(--ones > 0){\
  245. int tmp= GET_BYTE - 128;\
  246. if(tmp>>6)\
  247. ERROR\
  248. val= (val<<6) + tmp;\
  249. }\
  250. }
  251. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
  252. #if defined(ARCH_X86_64)
  253. static inline uint64_t read_time(void)
  254. {
  255. uint64_t a, d;
  256. asm volatile( "rdtsc\n\t"
  257. : "=a" (a), "=d" (d)
  258. );
  259. return (d << 32) | (a & 0xffffffff);
  260. }
  261. #elif defined(ARCH_X86)
  262. static inline long long read_time(void)
  263. {
  264. long long l;
  265. asm volatile( "rdtsc\n\t"
  266. : "=A" (l)
  267. );
  268. return l;
  269. }
  270. #else //FIXME check ppc64
  271. static inline uint64_t read_time(void)
  272. {
  273. uint32_t tbu, tbl, temp;
  274. /* from section 2.2.1 of the 32-bit PowerPC PEM */
  275. __asm__ __volatile__(
  276. "1:\n"
  277. "mftbu %2\n"
  278. "mftb %0\n"
  279. "mftbu %1\n"
  280. "cmpw %2,%1\n"
  281. "bne 1b\n"
  282. : "=r"(tbl), "=r"(tbu), "=r"(temp)
  283. :
  284. : "cc");
  285. return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
  286. }
  287. #endif
  288. #define START_TIMER \
  289. uint64_t tend;\
  290. uint64_t tstart= read_time();\
  291. #define STOP_TIMER(id) \
  292. tend= read_time();\
  293. {\
  294. static uint64_t tsum=0;\
  295. static int tcount=0;\
  296. static int tskip_count=0;\
  297. if(tcount<2 || tend - tstart < 8*tsum/tcount){\
  298. tsum+= tend - tstart;\
  299. tcount++;\
  300. }else\
  301. tskip_count++;\
  302. if(256*256*256*64%(tcount+tskip_count)==0){\
  303. av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
  304. }\
  305. }
  306. #else
  307. #define START_TIMER
  308. #define STOP_TIMER(id) {}
  309. #endif
  310. /* memory */
  311. void *av_malloc(unsigned int size);
  312. void *av_realloc(void *ptr, unsigned int size);
  313. void av_free(void *ptr);
  314. #endif /* COMMON_H */