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.

338 lines
8.5KB

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