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.

349 lines
8.8KB

  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. #include <inttypes.h>
  27. #ifdef HAVE_AV_CONFIG_H
  28. /* only include the following when compiling package */
  29. # include "config.h"
  30. # include <stdlib.h>
  31. # include <stdio.h>
  32. # include <string.h>
  33. # include <ctype.h>
  34. # include <limits.h>
  35. # ifndef __BEOS__
  36. # include <errno.h>
  37. # else
  38. # include "berrno.h"
  39. # endif
  40. # include <math.h>
  41. #endif /* HAVE_AV_CONFIG_H */
  42. #ifndef av_always_inline
  43. #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
  44. # define av_always_inline __attribute__((always_inline)) inline
  45. #else
  46. # define av_always_inline inline
  47. #endif
  48. #endif
  49. #ifdef HAVE_AV_CONFIG_H
  50. # include "internal.h"
  51. #endif /* HAVE_AV_CONFIG_H */
  52. #ifndef attribute_deprecated
  53. #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
  54. # define attribute_deprecated __attribute__((deprecated))
  55. #else
  56. # define attribute_deprecated
  57. #endif
  58. #endif
  59. #ifndef INT64_C
  60. #define INT64_C(c) (c ## LL)
  61. #define UINT64_C(c) (c ## ULL)
  62. #endif
  63. //rounded divison & shift
  64. #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
  65. /* assume b>0 */
  66. #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
  67. #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
  68. #define FFSIGN(a) ((a) > 0 ? 1 : -1)
  69. #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
  70. #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
  71. #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
  72. /* misc math functions */
  73. extern const uint8_t ff_log2_tab[256];
  74. static inline int av_log2(unsigned int v)
  75. {
  76. int n;
  77. n = 0;
  78. if (v & 0xffff0000) {
  79. v >>= 16;
  80. n += 16;
  81. }
  82. if (v & 0xff00) {
  83. v >>= 8;
  84. n += 8;
  85. }
  86. n += ff_log2_tab[v];
  87. return n;
  88. }
  89. static inline int av_log2_16bit(unsigned int v)
  90. {
  91. int n;
  92. n = 0;
  93. if (v & 0xff00) {
  94. v >>= 8;
  95. n += 8;
  96. }
  97. n += ff_log2_tab[v];
  98. return n;
  99. }
  100. /* median of 3 */
  101. static inline int mid_pred(int a, int b, int c)
  102. {
  103. #ifdef HAVE_CMOV
  104. int i=b;
  105. asm volatile(
  106. "cmp %2, %1 \n\t"
  107. "cmovg %1, %0 \n\t"
  108. "cmovg %2, %1 \n\t"
  109. "cmp %3, %1 \n\t"
  110. "cmovl %3, %1 \n\t"
  111. "cmp %1, %0 \n\t"
  112. "cmovg %1, %0 \n\t"
  113. :"+&r"(i), "+&r"(a)
  114. :"r"(b), "r"(c)
  115. );
  116. return i;
  117. #elif 0
  118. int t= (a-b)&((a-b)>>31);
  119. a-=t;
  120. b+=t;
  121. b-= (b-c)&((b-c)>>31);
  122. b+= (a-b)&((a-b)>>31);
  123. return b;
  124. #else
  125. if(a>b){
  126. if(c>b){
  127. if(c>a) b=a;
  128. else b=c;
  129. }
  130. }else{
  131. if(b>c){
  132. if(c>a) b=c;
  133. else b=a;
  134. }
  135. }
  136. return b;
  137. #endif
  138. }
  139. /**
  140. * clip a signed integer value into the amin-amax range
  141. * @param a value to clip
  142. * @param amin minimum value of the clip range
  143. * @param amax maximum value of the clip range
  144. * @return clipped value
  145. */
  146. static inline int clip(int a, int amin, int amax)
  147. {
  148. if (a < amin) return amin;
  149. else if (a > amax) return amax;
  150. else return a;
  151. }
  152. /**
  153. * clip a signed integer value into the 0-255 range
  154. * @param a value to clip
  155. * @return clipped value
  156. */
  157. static inline uint8_t clip_uint8(int a)
  158. {
  159. if (a&(~255)) return (-a)>>31;
  160. else return a;
  161. }
  162. /* math */
  163. int64_t ff_gcd(int64_t a, int64_t b);
  164. /**
  165. * converts fourcc string to int
  166. */
  167. static inline int ff_get_fourcc(const char *s){
  168. #ifdef HAVE_AV_CONFIG_H
  169. assert( strlen(s)==4 );
  170. #endif
  171. return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
  172. }
  173. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  174. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  175. /*!
  176. * \def GET_UTF8(val, GET_BYTE, ERROR)
  177. * converts a utf-8 character (up to 4 bytes long) to its 32-bit ucs-4 encoded form
  178. * \param val is the output and should be of type uint32_t. It holds the converted
  179. * ucs-4 character and should be a left value.
  180. * \param GET_BYTE gets utf-8 encoded bytes from any proper source. It can be
  181. * a function or a statement whose return value or evaluated value is of type
  182. * uint8_t. It will be executed up to 4 times for values in the valid utf-8 range,
  183. * and up to 7 times in the general case.
  184. * \param ERROR action that should be taken when an invalid utf-8 byte is returned
  185. * from GET_BYTE. It should be a statement that jumps out of the macro,
  186. * like exit(), goto, return, break, or continue.
  187. */
  188. #define GET_UTF8(val, GET_BYTE, ERROR)\
  189. val= GET_BYTE;\
  190. {\
  191. int ones= 7 - av_log2(val ^ 255);\
  192. if(ones==1)\
  193. ERROR\
  194. val&= 127>>ones;\
  195. while(--ones > 0){\
  196. int tmp= GET_BYTE - 128;\
  197. if(tmp>>6)\
  198. ERROR\
  199. val= (val<<6) + tmp;\
  200. }\
  201. }
  202. /*!
  203. * \def PUT_UTF8(val, tmp, PUT_BYTE)
  204. * converts a 32-bit unicode character to its utf-8 encoded form (up to 4 bytes long).
  205. * \param val is an input only argument and should be of type uint32_t. It holds
  206. * a ucs4 encoded unicode character that is to be converted to utf-8. If
  207. * val is given as a function it's executed only once.
  208. * \param tmp is a temporary variable and should be of type uint8_t. It
  209. * represents an intermediate value during conversion that is to be
  210. * outputted by PUT_BYTE.
  211. * \param PUT_BYTE writes the converted utf-8 bytes to any proper destination.
  212. * It could be a function or a statement, and uses tmp as the input byte.
  213. * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
  214. * executed up to 4 times for values in the valid utf-8 range and up to
  215. * 7 times in the general case, depending on the length of the converted
  216. * unicode character.
  217. */
  218. #define PUT_UTF8(val, tmp, PUT_BYTE)\
  219. {\
  220. int bytes, shift;\
  221. uint32_t in = val;\
  222. if (in < 0x80) {\
  223. tmp = in;\
  224. PUT_BYTE\
  225. } else {\
  226. bytes = (av_log2(in) + 4) / 5;\
  227. shift = (bytes - 1) * 6;\
  228. tmp = (256 - (256 >> bytes)) | (in >> shift);\
  229. PUT_BYTE\
  230. while (shift >= 6) {\
  231. shift -= 6;\
  232. tmp = 0x80 | ((in >> shift) & 0x3f);\
  233. PUT_BYTE\
  234. }\
  235. }\
  236. }
  237. #if defined(ARCH_X86) || defined(ARCH_POWERPC)
  238. #if defined(ARCH_X86_64)
  239. static inline uint64_t read_time(void)
  240. {
  241. uint64_t a, d;
  242. asm volatile( "rdtsc\n\t"
  243. : "=a" (a), "=d" (d)
  244. );
  245. return (d << 32) | (a & 0xffffffff);
  246. }
  247. #elif defined(ARCH_X86_32)
  248. static inline long long read_time(void)
  249. {
  250. long long l;
  251. asm volatile( "rdtsc\n\t"
  252. : "=A" (l)
  253. );
  254. return l;
  255. }
  256. #else //FIXME check ppc64
  257. static inline uint64_t read_time(void)
  258. {
  259. uint32_t tbu, tbl, temp;
  260. /* from section 2.2.1 of the 32-bit PowerPC PEM */
  261. __asm__ __volatile__(
  262. "1:\n"
  263. "mftbu %2\n"
  264. "mftb %0\n"
  265. "mftbu %1\n"
  266. "cmpw %2,%1\n"
  267. "bne 1b\n"
  268. : "=r"(tbl), "=r"(tbu), "=r"(temp)
  269. :
  270. : "cc");
  271. return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
  272. }
  273. #endif
  274. #define START_TIMER \
  275. uint64_t tend;\
  276. uint64_t tstart= read_time();\
  277. #define STOP_TIMER(id) \
  278. tend= read_time();\
  279. {\
  280. static uint64_t tsum=0;\
  281. static int tcount=0;\
  282. static int tskip_count=0;\
  283. if(tcount<2 || tend - tstart < 8*tsum/tcount){\
  284. tsum+= tend - tstart;\
  285. tcount++;\
  286. }else\
  287. tskip_count++;\
  288. if(((tcount+tskip_count)&(tcount+tskip_count-1))==0){\
  289. av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
  290. }\
  291. }
  292. #else
  293. #define START_TIMER
  294. #define STOP_TIMER(id) {}
  295. #endif
  296. /* memory */
  297. #ifdef __GNUC__
  298. #define DECLARE_ALIGNED(n,t,v) t v __attribute__ ((aligned (n)))
  299. #else
  300. #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
  301. #endif
  302. /* memory */
  303. void *av_malloc(unsigned int size);
  304. void *av_realloc(void *ptr, unsigned int size);
  305. void av_free(void *ptr);
  306. void *av_mallocz(unsigned int size);
  307. char *av_strdup(const char *s);
  308. void av_freep(void *ptr);
  309. #endif /* COMMON_H */