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.

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