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.

412 lines
8.9KB

  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 (defined(ARCH_X86) && __CPU__ >= 686 || defined(ARCH_X86_64)) && !defined(RUNTIME_CPUDETECT)
  197. int i=a, j=a;
  198. asm volatile(
  199. "cmp %4, %2 \n\t"
  200. "cmovg %4, %0 \n\t"
  201. "cmovl %4, %1 \n\t"
  202. "cmp %4, %3 \n\t"
  203. "cmovg %3, %0 \n\t"
  204. "cmovl %3, %1 \n\t"
  205. "cmp %3, %2 \n\t"
  206. "cmovl %1, %0 \n\t"
  207. :"+&r"(i), "+&r"(j)
  208. :"r"(a), "r"(b), "r"(c)
  209. );
  210. return i;
  211. #elif 0
  212. int t= (a-b)&((a-b)>>31);
  213. a-=t;
  214. b+=t;
  215. b-= (b-c)&((b-c)>>31);
  216. b+= (a-b)&((a-b)>>31);
  217. return b;
  218. #else
  219. if(a>b){
  220. if(c>b){
  221. if(c>a) b=a;
  222. else b=c;
  223. }
  224. }else{
  225. if(b>c){
  226. if(c>a) b=c;
  227. else b=a;
  228. }
  229. }
  230. return b;
  231. #endif
  232. }
  233. /**
  234. * clip a signed integer value into the amin-amax range
  235. * @param a value to clip
  236. * @param amin minimum value of the clip range
  237. * @param amax maximum value of the clip range
  238. * @return cliped value
  239. */
  240. static inline int clip(int a, int amin, int amax)
  241. {
  242. if (a < amin) return amin;
  243. else if (a > amax) return amax;
  244. else return a;
  245. }
  246. /**
  247. * clip a signed integer value into the 0-255 range
  248. * @param a value to clip
  249. * @return cliped value
  250. */
  251. static inline uint8_t clip_uint8(int a)
  252. {
  253. if (a&(~255)) return (-a)>>31;
  254. else return a;
  255. }
  256. /* math */
  257. int64_t ff_gcd(int64_t a, int64_t b);
  258. /**
  259. * converts fourcc string to int
  260. */
  261. static inline int ff_get_fourcc(const char *s){
  262. #ifdef HAVE_AV_CONFIG_H
  263. assert( strlen(s)==4 );
  264. #endif
  265. return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
  266. }
  267. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  268. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  269. #define GET_UTF8(val, GET_BYTE, ERROR)\
  270. val= GET_BYTE;\
  271. {\
  272. int ones= 7 - av_log2(val ^ 255);\
  273. if(ones==1)\
  274. ERROR\
  275. val&= 127>>ones;\
  276. while(--ones > 0){\
  277. int tmp= GET_BYTE - 128;\
  278. if(tmp>>6)\
  279. ERROR\
  280. val= (val<<6) + tmp;\
  281. }\
  282. }
  283. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
  284. #if defined(ARCH_X86_64)
  285. static inline uint64_t read_time(void)
  286. {
  287. uint64_t a, d;
  288. asm volatile( "rdtsc\n\t"
  289. : "=a" (a), "=d" (d)
  290. );
  291. return (d << 32) | (a & 0xffffffff);
  292. }
  293. #elif defined(ARCH_X86)
  294. static inline long long read_time(void)
  295. {
  296. long long l;
  297. asm volatile( "rdtsc\n\t"
  298. : "=A" (l)
  299. );
  300. return l;
  301. }
  302. #else //FIXME check ppc64
  303. static inline uint64_t read_time(void)
  304. {
  305. uint32_t tbu, tbl, temp;
  306. /* from section 2.2.1 of the 32-bit PowerPC PEM */
  307. __asm__ __volatile__(
  308. "1:\n"
  309. "mftbu %2\n"
  310. "mftb %0\n"
  311. "mftbu %1\n"
  312. "cmpw %2,%1\n"
  313. "bne 1b\n"
  314. : "=r"(tbl), "=r"(tbu), "=r"(temp)
  315. :
  316. : "cc");
  317. return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
  318. }
  319. #endif
  320. #define START_TIMER \
  321. uint64_t tend;\
  322. uint64_t tstart= read_time();\
  323. #define STOP_TIMER(id) \
  324. tend= read_time();\
  325. {\
  326. static uint64_t tsum=0;\
  327. static int tcount=0;\
  328. static int tskip_count=0;\
  329. if(tcount<2 || tend - tstart < 8*tsum/tcount){\
  330. tsum+= tend - tstart;\
  331. tcount++;\
  332. }else\
  333. tskip_count++;\
  334. if(256*256*256*64%(tcount+tskip_count)==0){\
  335. av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
  336. }\
  337. }
  338. #else
  339. #define START_TIMER
  340. #define STOP_TIMER(id) {}
  341. #endif
  342. /* memory */
  343. #ifdef __GNUC__
  344. #define DECLARE_ALIGNED(n,t,v) t v __attribute__ ((aligned (n)))
  345. #else
  346. #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
  347. #endif
  348. void *av_malloc(unsigned int size);
  349. void *av_realloc(void *ptr, unsigned int size);
  350. void av_free(void *ptr);
  351. #endif /* COMMON_H */