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.

419 lines
9.1KB

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