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.

450 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. # include <inttypes.h>
  77. #ifndef PRId64
  78. #define PRId64 "lld"
  79. #endif
  80. #ifndef PRIu64
  81. #define PRIu64 "llu"
  82. #endif
  83. #ifndef PRIx64
  84. #define PRIx64 "llx"
  85. #endif
  86. #ifndef PRIX64
  87. #define PRIX64 "llX"
  88. #endif
  89. #ifndef PRId32
  90. #define PRId32 "d"
  91. #endif
  92. #ifndef PRIdFAST16
  93. #define PRIdFAST16 PRId32
  94. #endif
  95. #ifndef PRIdFAST32
  96. #define PRIdFAST32 PRId32
  97. #endif
  98. #ifndef INT16_MIN
  99. #define INT16_MIN (-0x7fff-1)
  100. #endif
  101. #ifndef INT16_MAX
  102. #define INT16_MAX 0x7fff
  103. #endif
  104. #ifndef INT32_MIN
  105. #define INT32_MIN (-0x7fffffff-1)
  106. #endif
  107. #ifndef INT32_MAX
  108. #define INT32_MAX 0x7fffffff
  109. #endif
  110. #ifndef UINT32_MAX
  111. #define UINT32_MAX 0xffffffff
  112. #endif
  113. #ifndef INT64_MIN
  114. #define INT64_MIN (-0x7fffffffffffffffLL-1)
  115. #endif
  116. #ifndef INT64_MAX
  117. #define INT64_MAX int64_t_C(9223372036854775807)
  118. #endif
  119. #ifndef UINT64_MAX
  120. #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
  121. #endif
  122. #ifndef INT_BIT
  123. # if INT_MAX != 2147483647
  124. # define INT_BIT 64
  125. # else
  126. # define INT_BIT 32
  127. # endif
  128. #endif
  129. #ifndef int64_t_C
  130. #define int64_t_C(c) (c ## LL)
  131. #define uint64_t_C(c) (c ## ULL)
  132. #endif
  133. #if defined(__MINGW32__) && !defined(BUILD_AVUTIL) && defined(BUILD_SHARED_AV)
  134. # define FF_IMPORT_ATTR __declspec(dllimport)
  135. #else
  136. # define FF_IMPORT_ATTR
  137. #endif
  138. #ifdef HAVE_AV_CONFIG_H
  139. /* only include the following when compiling package */
  140. # include "internal.h"
  141. #endif
  142. //rounded divison & shift
  143. #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
  144. /* assume b>0 */
  145. #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
  146. #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
  147. #define FFSIGN(a) ((a) > 0 ? 1 : -1)
  148. #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
  149. #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
  150. #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
  151. /* misc math functions */
  152. extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
  153. static inline int av_log2(unsigned int v)
  154. {
  155. int n;
  156. n = 0;
  157. if (v & 0xffff0000) {
  158. v >>= 16;
  159. n += 16;
  160. }
  161. if (v & 0xff00) {
  162. v >>= 8;
  163. n += 8;
  164. }
  165. n += ff_log2_tab[v];
  166. return n;
  167. }
  168. static inline int av_log2_16bit(unsigned int v)
  169. {
  170. int n;
  171. n = 0;
  172. if (v & 0xff00) {
  173. v >>= 8;
  174. n += 8;
  175. }
  176. n += ff_log2_tab[v];
  177. return n;
  178. }
  179. /* median of 3 */
  180. static inline int mid_pred(int a, int b, int c)
  181. {
  182. #if HAVE_CMOV
  183. int i=b;
  184. asm volatile(
  185. "cmp %2, %1 \n\t"
  186. "cmovg %1, %0 \n\t"
  187. "cmovg %2, %1 \n\t"
  188. "cmp %3, %1 \n\t"
  189. "cmovl %3, %1 \n\t"
  190. "cmp %1, %0 \n\t"
  191. "cmovg %1, %0 \n\t"
  192. :"+&r"(i), "+&r"(a)
  193. :"r"(b), "r"(c)
  194. );
  195. return i;
  196. #elif 0
  197. int t= (a-b)&((a-b)>>31);
  198. a-=t;
  199. b+=t;
  200. b-= (b-c)&((b-c)>>31);
  201. b+= (a-b)&((a-b)>>31);
  202. return b;
  203. #else
  204. if(a>b){
  205. if(c>b){
  206. if(c>a) b=a;
  207. else b=c;
  208. }
  209. }else{
  210. if(b>c){
  211. if(c>a) b=c;
  212. else b=a;
  213. }
  214. }
  215. return b;
  216. #endif
  217. }
  218. /**
  219. * clip a signed integer value into the amin-amax range
  220. * @param a value to clip
  221. * @param amin minimum value of the clip range
  222. * @param amax maximum value of the clip range
  223. * @return cliped value
  224. */
  225. static inline int clip(int a, int amin, int amax)
  226. {
  227. if (a < amin) return amin;
  228. else if (a > amax) return amax;
  229. else return a;
  230. }
  231. /**
  232. * clip a signed integer value into the 0-255 range
  233. * @param a value to clip
  234. * @return cliped value
  235. */
  236. static inline uint8_t clip_uint8(int a)
  237. {
  238. if (a&(~255)) return (-a)>>31;
  239. else return a;
  240. }
  241. /* math */
  242. int64_t ff_gcd(int64_t a, int64_t b);
  243. /**
  244. * converts fourcc string to int
  245. */
  246. static inline int ff_get_fourcc(const char *s){
  247. #ifdef HAVE_AV_CONFIG_H
  248. assert( strlen(s)==4 );
  249. #endif
  250. return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
  251. }
  252. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  253. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  254. /*!
  255. * \def GET_UTF8(val, GET_BYTE, ERROR)
  256. * converts a utf-8 character (up to 4 bytes long) to its 32-bit ucs-4 encoded form
  257. * \param val is the output and should be of type uint32_t. It holds the converted
  258. * ucs-4 character and should be a left value.
  259. * \param GET_BYTE gets utf-8 encoded bytes from any proper source. It can be
  260. * a function or a statement whose return value or evaluated value is of type
  261. * uint8_t. It will be executed up to 4 times for values in the valid utf-8 range,
  262. * and up to 7 times in the general case.
  263. * \param ERROR action that should be taken when an invalid utf-8 byte is returned
  264. * from GET_BYTE. It should be a statement that jumps out of the macro,
  265. * like exit(), goto, return, break, or continue.
  266. */
  267. #define GET_UTF8(val, GET_BYTE, ERROR)\
  268. val= GET_BYTE;\
  269. {\
  270. int ones= 7 - av_log2(val ^ 255);\
  271. if(ones==1)\
  272. ERROR\
  273. val&= 127>>ones;\
  274. while(--ones > 0){\
  275. int tmp= GET_BYTE - 128;\
  276. if(tmp>>6)\
  277. ERROR\
  278. val= (val<<6) + tmp;\
  279. }\
  280. }
  281. /*!
  282. * \def PUT_UTF8(val, tmp, PUT_BYTE)
  283. * converts a 32-bit unicode character to its utf-8 encoded form (up to 4 bytes long).
  284. * \param val is an input only argument and should be of type uint32_t. It holds
  285. * a ucs4 encoded unicode character that is to be converted to utf-8. If
  286. * val is given as a function it's executed only once.
  287. * \param tmp is a temporary variable and should be of type uint8_t. It
  288. * represents an intermediate value during conversion that is to be
  289. * outputted by PUT_BYTE.
  290. * \param PUT_BYTE writes the converted utf-8 bytes to any proper destination.
  291. * It could be a function or a statement, and uses tmp as the input byte.
  292. * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
  293. * executed up to 4 times for values in the valid utf-8 range and up to
  294. * 7 times in the general case, depending on the length of the converted
  295. * unicode character.
  296. */
  297. #define PUT_UTF8(val, tmp, PUT_BYTE)\
  298. {\
  299. int bytes, shift;\
  300. uint32_t in = val;\
  301. if (in < 0x80) {\
  302. tmp = in;\
  303. PUT_BYTE\
  304. } else {\
  305. bytes = (av_log2(in) + 4) / 5;\
  306. shift = (bytes - 1) * 6;\
  307. tmp = (256 - (256 >> bytes)) | (in >> shift);\
  308. PUT_BYTE\
  309. while (shift >= 6) {\
  310. shift -= 6;\
  311. tmp = 0x80 | ((in >> shift) & 0x3f);\
  312. PUT_BYTE\
  313. }\
  314. }\
  315. }
  316. #if defined(ARCH_X86) || defined(ARCH_POWERPC)
  317. #if defined(ARCH_X86_64)
  318. static inline uint64_t read_time(void)
  319. {
  320. uint64_t a, d;
  321. asm volatile( "rdtsc\n\t"
  322. : "=a" (a), "=d" (d)
  323. );
  324. return (d << 32) | (a & 0xffffffff);
  325. }
  326. #elif defined(ARCH_X86_32)
  327. static inline long long read_time(void)
  328. {
  329. long long l;
  330. asm volatile( "rdtsc\n\t"
  331. : "=A" (l)
  332. );
  333. return l;
  334. }
  335. #else //FIXME check ppc64
  336. static inline uint64_t read_time(void)
  337. {
  338. uint32_t tbu, tbl, temp;
  339. /* from section 2.2.1 of the 32-bit PowerPC PEM */
  340. __asm__ __volatile__(
  341. "1:\n"
  342. "mftbu %2\n"
  343. "mftb %0\n"
  344. "mftbu %1\n"
  345. "cmpw %2,%1\n"
  346. "bne 1b\n"
  347. : "=r"(tbl), "=r"(tbu), "=r"(temp)
  348. :
  349. : "cc");
  350. return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
  351. }
  352. #endif
  353. #define START_TIMER \
  354. uint64_t tend;\
  355. uint64_t tstart= read_time();\
  356. #define STOP_TIMER(id) \
  357. tend= read_time();\
  358. {\
  359. static uint64_t tsum=0;\
  360. static int tcount=0;\
  361. static int tskip_count=0;\
  362. if(tcount<2 || tend - tstart < 8*tsum/tcount){\
  363. tsum+= tend - tstart;\
  364. tcount++;\
  365. }else\
  366. tskip_count++;\
  367. if(((tcount+tskip_count)&(tcount+tskip_count-1))==0){\
  368. av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
  369. }\
  370. }
  371. #else
  372. #define START_TIMER
  373. #define STOP_TIMER(id) {}
  374. #endif
  375. /* memory */
  376. #ifdef __GNUC__
  377. #define DECLARE_ALIGNED(n,t,v) t v __attribute__ ((aligned (n)))
  378. #else
  379. #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
  380. #endif
  381. /* memory */
  382. void *av_malloc(unsigned int size);
  383. void *av_realloc(void *ptr, unsigned int size);
  384. void av_free(void *ptr);
  385. void *av_mallocz(unsigned int size);
  386. char *av_strdup(const char *s);
  387. void av_freep(void *ptr);
  388. #endif /* COMMON_H */