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.

397 lines
8.5KB

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