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.

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