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.

416 lines
9.0KB

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