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.

327 lines
8.3KB

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