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.

359 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. # include "bswap.h"
  130. #ifdef HAVE_AV_CONFIG_H
  131. /* only include the following when compiling package */
  132. # include "internal.h"
  133. #endif
  134. //rounded divison & shift
  135. #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
  136. /* assume b>0 */
  137. #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
  138. #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
  139. #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
  140. #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
  141. /* misc math functions */
  142. extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
  143. static inline int av_log2(unsigned int v)
  144. {
  145. int n;
  146. n = 0;
  147. if (v & 0xffff0000) {
  148. v >>= 16;
  149. n += 16;
  150. }
  151. if (v & 0xff00) {
  152. v >>= 8;
  153. n += 8;
  154. }
  155. n += ff_log2_tab[v];
  156. return n;
  157. }
  158. static inline int av_log2_16bit(unsigned int v)
  159. {
  160. int n;
  161. n = 0;
  162. if (v & 0xff00) {
  163. v >>= 8;
  164. n += 8;
  165. }
  166. n += ff_log2_tab[v];
  167. return n;
  168. }
  169. /* median of 3 */
  170. static inline int mid_pred(int a, int b, int c)
  171. {
  172. #if 0
  173. int t= (a-b)&((a-b)>>31);
  174. a-=t;
  175. b+=t;
  176. b-= (b-c)&((b-c)>>31);
  177. b+= (a-b)&((a-b)>>31);
  178. return b;
  179. #else
  180. if(a>b){
  181. if(c>b){
  182. if(c>a) b=a;
  183. else b=c;
  184. }
  185. }else{
  186. if(b>c){
  187. if(c>a) b=c;
  188. else b=a;
  189. }
  190. }
  191. return b;
  192. #endif
  193. }
  194. /**
  195. * clip a signed integer value into the amin-amax range
  196. * @param a value to clip
  197. * @param amin minimum value of the clip range
  198. * @param amax maximum value of the clip range
  199. * @return cliped value
  200. */
  201. static inline int clip(int a, int amin, int amax)
  202. {
  203. if (a < amin) return amin;
  204. else if (a > amax) return amax;
  205. else return a;
  206. }
  207. /**
  208. * clip a signed integer value into the 0-255 range
  209. * @param a value to clip
  210. * @return cliped value
  211. */
  212. static inline uint8_t clip_uint8(int a)
  213. {
  214. if (a&(~255)) return (-a)>>31;
  215. else return a;
  216. }
  217. /* math */
  218. int64_t ff_gcd(int64_t a, int64_t b);
  219. /**
  220. * converts fourcc string to int
  221. */
  222. static inline int ff_get_fourcc(const char *s){
  223. #ifdef HAVE_AV_CONFIG_H
  224. assert( strlen(s)==4 );
  225. #endif
  226. return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
  227. }
  228. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  229. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  230. #define GET_UTF8(val, GET_BYTE, ERROR)\
  231. val= GET_BYTE;\
  232. {\
  233. int ones= 7 - av_log2(val ^ 255);\
  234. if(ones==1)\
  235. ERROR\
  236. val&= 127>>ones;\
  237. while(--ones > 0){\
  238. int tmp= GET_BYTE - 128;\
  239. if(tmp>>6)\
  240. ERROR\
  241. val= (val<<6) + tmp;\
  242. }\
  243. }
  244. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
  245. #if defined(ARCH_X86_64)
  246. static inline uint64_t read_time(void)
  247. {
  248. uint64_t a, d;
  249. asm volatile( "rdtsc\n\t"
  250. : "=a" (a), "=d" (d)
  251. );
  252. return (d << 32) | (a & 0xffffffff);
  253. }
  254. #elif defined(ARCH_X86)
  255. static inline long long read_time(void)
  256. {
  257. long long l;
  258. asm volatile( "rdtsc\n\t"
  259. : "=A" (l)
  260. );
  261. return l;
  262. }
  263. #else //FIXME check ppc64
  264. static inline uint64_t read_time(void)
  265. {
  266. uint32_t tbu, tbl, temp;
  267. /* from section 2.2.1 of the 32-bit PowerPC PEM */
  268. __asm__ __volatile__(
  269. "1:\n"
  270. "mftbu %2\n"
  271. "mftb %0\n"
  272. "mftbu %1\n"
  273. "cmpw %2,%1\n"
  274. "bne 1b\n"
  275. : "=r"(tbl), "=r"(tbu), "=r"(temp)
  276. :
  277. : "cc");
  278. return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
  279. }
  280. #endif
  281. #define START_TIMER \
  282. uint64_t tend;\
  283. uint64_t tstart= read_time();\
  284. #define STOP_TIMER(id) \
  285. tend= read_time();\
  286. {\
  287. static uint64_t tsum=0;\
  288. static int tcount=0;\
  289. static int tskip_count=0;\
  290. if(tcount<2 || tend - tstart < 8*tsum/tcount){\
  291. tsum+= tend - tstart;\
  292. tcount++;\
  293. }else\
  294. tskip_count++;\
  295. if(256*256*256*64%(tcount+tskip_count)==0){\
  296. av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
  297. }\
  298. }
  299. #else
  300. #define START_TIMER
  301. #define STOP_TIMER(id) {}
  302. #endif
  303. #endif /* COMMON_H */