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.

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