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.

416 lines
11KB

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