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.

362 lines
7.2KB

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