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.

469 lines
11KB

  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. #ifdef HAVE_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 /* HAVE_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. #ifndef HAVE_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. /*!
  275. * \def GET_UTF8(val, GET_BYTE, ERROR)
  276. * converts a utf-8 character (up to 4 bytes long) to its 32-bit ucs-4 encoded form
  277. * \param val is the output and should be of type uint32_t. It holds the converted
  278. * ucs-4 character and should be a left value.
  279. * \param GET_BYTE gets utf-8 encoded bytes from any proper source. It can be
  280. * a function or a statement whose return value or evaluated value is of type
  281. * uint8_t. It will be execuded up to 4 times.
  282. * \param ERROR action that should be taken when an invalid utf-8 byte is returned
  283. * from GET_BYTE. It should be a statement that jumps out of the macro,
  284. * like exit(), goto, return, break, or continue.
  285. */
  286. #define GET_UTF8(val, GET_BYTE, ERROR)\
  287. val= GET_BYTE;\
  288. {\
  289. int ones= 7 - av_log2(val ^ 255);\
  290. if(ones==1)\
  291. ERROR\
  292. val&= 127>>ones;\
  293. while(--ones > 0){\
  294. int tmp= GET_BYTE - 128;\
  295. if(tmp>>6)\
  296. ERROR\
  297. val= (val<<6) + tmp;\
  298. }\
  299. }
  300. /*!
  301. * \def PUT_UTF8(val, tmp, PUT_BYTE)
  302. * converts a 32-bit unicode character to its utf-8 encoded form (up to 4 bytes long).
  303. * \param val is an input only argument and should be of type uint32_t. It holds
  304. * a ucs4 encoded unicode character that is to be converted to utf-8. If
  305. * val is given as a function it's executed only once.
  306. * \param tmp is a temporary variable and should be of type uint8_t. It
  307. * represents an intermediate value during conversion that is to be
  308. * outputted by PUT_BYTE.
  309. * \param PUT_BYTE writes the converted utf-8 bytes to any proper destination.
  310. * It could be a function or a statement, and uses tmp as the input byte.
  311. * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
  312. * executed up to 4 times, depending on the length of the converted
  313. * unicode character.
  314. */
  315. #define PUT_UTF8(val, tmp, PUT_BYTE)\
  316. {\
  317. int bytes, shift;\
  318. uint32_t in = val;\
  319. if (in < 0x80) {\
  320. tmp = in;\
  321. PUT_BYTE\
  322. } else {\
  323. bytes = (av_log2(in) + 4) / 5;\
  324. shift = (bytes - 1) * 6;\
  325. tmp = (256 - (256 >> bytes)) | (in >> shift);\
  326. PUT_BYTE\
  327. while (shift >= 6) {\
  328. shift -= 6;\
  329. tmp = 0x80 | ((in >> shift) & 0x3f);\
  330. PUT_BYTE\
  331. }\
  332. }\
  333. }
  334. #if defined(ARCH_X86) || defined(ARCH_POWERPC)
  335. #if defined(ARCH_X86_64)
  336. static inline uint64_t read_time(void)
  337. {
  338. uint64_t a, d;
  339. asm volatile( "rdtsc\n\t"
  340. : "=a" (a), "=d" (d)
  341. );
  342. return (d << 32) | (a & 0xffffffff);
  343. }
  344. #elif defined(ARCH_X86_32)
  345. static inline long long read_time(void)
  346. {
  347. long long l;
  348. asm volatile( "rdtsc\n\t"
  349. : "=A" (l)
  350. );
  351. return l;
  352. }
  353. #else //FIXME check ppc64
  354. static inline uint64_t read_time(void)
  355. {
  356. uint32_t tbu, tbl, temp;
  357. /* from section 2.2.1 of the 32-bit PowerPC PEM */
  358. __asm__ __volatile__(
  359. "1:\n"
  360. "mftbu %2\n"
  361. "mftb %0\n"
  362. "mftbu %1\n"
  363. "cmpw %2,%1\n"
  364. "bne 1b\n"
  365. : "=r"(tbl), "=r"(tbu), "=r"(temp)
  366. :
  367. : "cc");
  368. return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
  369. }
  370. #endif
  371. #define START_TIMER \
  372. uint64_t tend;\
  373. uint64_t tstart= read_time();\
  374. #define STOP_TIMER(id) \
  375. tend= read_time();\
  376. {\
  377. static uint64_t tsum=0;\
  378. static int tcount=0;\
  379. static int tskip_count=0;\
  380. if(tcount<2 || tend - tstart < 8*tsum/tcount){\
  381. tsum+= tend - tstart;\
  382. tcount++;\
  383. }else\
  384. tskip_count++;\
  385. if(((tcount+tskip_count)&(tcount+tskip_count-1))==0){\
  386. av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
  387. }\
  388. }
  389. #else
  390. #define START_TIMER
  391. #define STOP_TIMER(id) {}
  392. #endif
  393. /* memory */
  394. #ifdef __GNUC__
  395. #define DECLARE_ALIGNED(n,t,v) t v __attribute__ ((aligned (n)))
  396. #else
  397. #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
  398. #endif
  399. /* memory */
  400. void *av_malloc(unsigned int size);
  401. void *av_realloc(void *ptr, unsigned int size);
  402. void av_free(void *ptr);
  403. void *av_mallocz(unsigned int size);
  404. char *av_strdup(const char *s);
  405. void av_freep(void *ptr);
  406. #endif /* COMMON_H */