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.

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