common.h 10 KB

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