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.

345 lines
8.7KB

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