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.

423 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 PRIX64
  98. #define PRIX64 "llX"
  99. #endif
  100. #ifndef PRId32
  101. #define PRId32 "d"
  102. #endif
  103. #ifndef PRIdFAST16
  104. #define PRIdFAST16 PRId32
  105. #endif
  106. #ifndef PRIdFAST32
  107. #define PRIdFAST32 PRId32
  108. #endif
  109. #ifndef INT16_MIN
  110. #define INT16_MIN (-0x7fff-1)
  111. #endif
  112. #ifndef INT16_MAX
  113. #define INT16_MAX 0x7fff
  114. #endif
  115. #ifndef INT32_MIN
  116. #define INT32_MIN (-0x7fffffff-1)
  117. #endif
  118. #ifndef INT32_MAX
  119. #define INT32_MAX 0x7fffffff
  120. #endif
  121. #ifndef UINT32_MAX
  122. #define UINT32_MAX 0xffffffff
  123. #endif
  124. #ifndef INT64_MIN
  125. #define INT64_MIN (-0x7fffffffffffffffLL-1)
  126. #endif
  127. #ifndef INT64_MAX
  128. #define INT64_MAX int64_t_C(9223372036854775807)
  129. #endif
  130. #ifndef UINT64_MAX
  131. #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
  132. #endif
  133. #ifdef EMULATE_FAST_INT
  134. typedef signed char int_fast8_t;
  135. typedef signed int int_fast16_t;
  136. typedef signed int int_fast32_t;
  137. typedef unsigned char uint_fast8_t;
  138. typedef unsigned int uint_fast16_t;
  139. typedef unsigned int uint_fast32_t;
  140. typedef uint64_t uint_fast64_t;
  141. #endif
  142. #ifndef INT_BIT
  143. # if INT_MAX != 2147483647
  144. # define INT_BIT 64
  145. # else
  146. # define INT_BIT 32
  147. # endif
  148. #endif
  149. #ifndef int64_t_C
  150. #define int64_t_C(c) (c ## LL)
  151. #define uint64_t_C(c) (c ## ULL)
  152. #endif
  153. #if defined(__MINGW32__) && !defined(BUILD_AVUTIL) && defined(BUILD_SHARED_AV)
  154. # define FF_IMPORT_ATTR __declspec(dllimport)
  155. #else
  156. # define FF_IMPORT_ATTR
  157. #endif
  158. #ifdef HAVE_AV_CONFIG_H
  159. /* only include the following when compiling package */
  160. # include "internal.h"
  161. #endif
  162. //rounded divison & shift
  163. #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
  164. /* assume b>0 */
  165. #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
  166. #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
  167. #define FFSIGN(a) ((a) > 0 ? 1 : -1)
  168. #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
  169. #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
  170. #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
  171. /* misc math functions */
  172. extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
  173. static inline int av_log2(unsigned int v)
  174. {
  175. int n;
  176. n = 0;
  177. if (v & 0xffff0000) {
  178. v >>= 16;
  179. n += 16;
  180. }
  181. if (v & 0xff00) {
  182. v >>= 8;
  183. n += 8;
  184. }
  185. n += ff_log2_tab[v];
  186. return n;
  187. }
  188. static inline int av_log2_16bit(unsigned int v)
  189. {
  190. int n;
  191. n = 0;
  192. if (v & 0xff00) {
  193. v >>= 8;
  194. n += 8;
  195. }
  196. n += ff_log2_tab[v];
  197. return n;
  198. }
  199. /* median of 3 */
  200. static inline int mid_pred(int a, int b, int c)
  201. {
  202. #if HAVE_CMOV
  203. int i=b;
  204. asm volatile(
  205. "cmp %2, %1 \n\t"
  206. "cmovg %1, %0 \n\t"
  207. "cmovg %2, %1 \n\t"
  208. "cmp %3, %1 \n\t"
  209. "cmovl %3, %1 \n\t"
  210. "cmp %1, %0 \n\t"
  211. "cmovg %1, %0 \n\t"
  212. :"+&r"(i), "+&r"(a)
  213. :"r"(b), "r"(c)
  214. );
  215. return i;
  216. #elif 0
  217. int t= (a-b)&((a-b)>>31);
  218. a-=t;
  219. b+=t;
  220. b-= (b-c)&((b-c)>>31);
  221. b+= (a-b)&((a-b)>>31);
  222. return b;
  223. #else
  224. if(a>b){
  225. if(c>b){
  226. if(c>a) b=a;
  227. else b=c;
  228. }
  229. }else{
  230. if(b>c){
  231. if(c>a) b=c;
  232. else b=a;
  233. }
  234. }
  235. return b;
  236. #endif
  237. }
  238. /**
  239. * clip a signed integer value into the amin-amax range
  240. * @param a value to clip
  241. * @param amin minimum value of the clip range
  242. * @param amax maximum value of the clip range
  243. * @return cliped value
  244. */
  245. static inline int clip(int a, int amin, int amax)
  246. {
  247. if (a < amin) return amin;
  248. else if (a > amax) return amax;
  249. else return a;
  250. }
  251. /**
  252. * clip a signed integer value into the 0-255 range
  253. * @param a value to clip
  254. * @return cliped value
  255. */
  256. static inline uint8_t clip_uint8(int a)
  257. {
  258. if (a&(~255)) return (-a)>>31;
  259. else return a;
  260. }
  261. /* math */
  262. int64_t ff_gcd(int64_t a, int64_t b);
  263. /**
  264. * converts fourcc string to int
  265. */
  266. static inline int ff_get_fourcc(const char *s){
  267. #ifdef HAVE_AV_CONFIG_H
  268. assert( strlen(s)==4 );
  269. #endif
  270. return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
  271. }
  272. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  273. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  274. #define GET_UTF8(val, GET_BYTE, ERROR)\
  275. val= GET_BYTE;\
  276. {\
  277. int ones= 7 - av_log2(val ^ 255);\
  278. if(ones==1)\
  279. ERROR\
  280. val&= 127>>ones;\
  281. while(--ones > 0){\
  282. int tmp= GET_BYTE - 128;\
  283. if(tmp>>6)\
  284. ERROR\
  285. val= (val<<6) + tmp;\
  286. }\
  287. }
  288. #if defined(ARCH_X86) || defined(ARCH_POWERPC)
  289. #if defined(ARCH_X86_64)
  290. static inline uint64_t read_time(void)
  291. {
  292. uint64_t a, d;
  293. asm volatile( "rdtsc\n\t"
  294. : "=a" (a), "=d" (d)
  295. );
  296. return (d << 32) | (a & 0xffffffff);
  297. }
  298. #elif defined(ARCH_X86_32)
  299. static inline long long read_time(void)
  300. {
  301. long long l;
  302. asm volatile( "rdtsc\n\t"
  303. : "=A" (l)
  304. );
  305. return l;
  306. }
  307. #else //FIXME check ppc64
  308. static inline uint64_t read_time(void)
  309. {
  310. uint32_t tbu, tbl, temp;
  311. /* from section 2.2.1 of the 32-bit PowerPC PEM */
  312. __asm__ __volatile__(
  313. "1:\n"
  314. "mftbu %2\n"
  315. "mftb %0\n"
  316. "mftbu %1\n"
  317. "cmpw %2,%1\n"
  318. "bne 1b\n"
  319. : "=r"(tbl), "=r"(tbu), "=r"(temp)
  320. :
  321. : "cc");
  322. return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
  323. }
  324. #endif
  325. #define START_TIMER \
  326. uint64_t tend;\
  327. uint64_t tstart= read_time();\
  328. #define STOP_TIMER(id) \
  329. tend= read_time();\
  330. {\
  331. static uint64_t tsum=0;\
  332. static int tcount=0;\
  333. static int tskip_count=0;\
  334. if(tcount<2 || tend - tstart < 8*tsum/tcount){\
  335. tsum+= tend - tstart;\
  336. tcount++;\
  337. }else\
  338. tskip_count++;\
  339. if(((tcount+tskip_count)&(tcount+tskip_count-1))==0){\
  340. av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
  341. }\
  342. }
  343. #else
  344. #define START_TIMER
  345. #define STOP_TIMER(id) {}
  346. #endif
  347. /* memory */
  348. #ifdef __GNUC__
  349. #define DECLARE_ALIGNED(n,t,v) t v __attribute__ ((aligned (n)))
  350. #else
  351. #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
  352. #endif
  353. /* memory */
  354. void *av_malloc(unsigned int size);
  355. void *av_realloc(void *ptr, unsigned int size);
  356. void av_free(void *ptr);
  357. void *av_mallocz(unsigned int size);
  358. char *av_strdup(const char *s);
  359. void av_freep(void *ptr);
  360. #endif /* COMMON_H */