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.

418 lines
9.0KB

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