common.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 libavutil/common.h
  22. * common internal and external API header
  23. */
  24. #ifndef AVUTIL_COMMON_H
  25. #define AVUTIL_COMMON_H
  26. #include <ctype.h>
  27. #include <errno.h>
  28. #include <inttypes.h>
  29. #include <limits.h>
  30. #include <math.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #ifdef HAVE_AV_CONFIG_H
  35. #include "config.h"
  36. #endif
  37. #ifdef __GNUC__
  38. # define AV_GCC_VERSION_AT_LEAST(x,y) (__GNUC__ > x || __GNUC__ == x && __GNUC_MINOR__ >= y)
  39. #else
  40. # define AV_GCC_VERSION_AT_LEAST(x,y) 0
  41. #endif
  42. #ifndef av_always_inline
  43. #if AV_GCC_VERSION_AT_LEAST(3,1)
  44. # define av_always_inline __attribute__((always_inline)) inline
  45. #else
  46. # define av_always_inline inline
  47. #endif
  48. #endif
  49. #ifndef av_noinline
  50. #if AV_GCC_VERSION_AT_LEAST(3,1)
  51. # define av_noinline __attribute__((noinline))
  52. #else
  53. # define av_noinline
  54. #endif
  55. #endif
  56. #ifndef av_pure
  57. #if AV_GCC_VERSION_AT_LEAST(3,1)
  58. # define av_pure __attribute__((pure))
  59. #else
  60. # define av_pure
  61. #endif
  62. #endif
  63. #ifndef av_const
  64. #if AV_GCC_VERSION_AT_LEAST(2,6)
  65. # define av_const __attribute__((const))
  66. #else
  67. # define av_const
  68. #endif
  69. #endif
  70. #ifndef av_cold
  71. #if (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(4,3)
  72. # define av_cold __attribute__((cold))
  73. #else
  74. # define av_cold
  75. #endif
  76. #endif
  77. #ifndef av_flatten
  78. #if (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(4,1)
  79. # define av_flatten __attribute__((flatten))
  80. #else
  81. # define av_flatten
  82. #endif
  83. #endif
  84. #ifndef attribute_deprecated
  85. #if AV_GCC_VERSION_AT_LEAST(3,1)
  86. # define attribute_deprecated __attribute__((deprecated))
  87. #else
  88. # define attribute_deprecated
  89. #endif
  90. #endif
  91. #ifndef av_unused
  92. #if defined(__GNUC__)
  93. # define av_unused __attribute__((unused))
  94. #else
  95. # define av_unused
  96. #endif
  97. #endif
  98. #ifndef av_uninit
  99. #if defined(__GNUC__) && !defined(__ICC)
  100. # define av_uninit(x) x=x
  101. #else
  102. # define av_uninit(x) x
  103. #endif
  104. #endif
  105. //rounded division & shift
  106. #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
  107. /* assume b>0 */
  108. #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
  109. #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
  110. #define FFSIGN(a) ((a) > 0 ? 1 : -1)
  111. #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
  112. #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
  113. #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
  114. #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
  115. #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
  116. #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
  117. #define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1))
  118. /* misc math functions */
  119. extern const uint8_t ff_log2_tab[256];
  120. static inline av_const int av_log2(unsigned int v)
  121. {
  122. int n = 0;
  123. if (v & 0xffff0000) {
  124. v >>= 16;
  125. n += 16;
  126. }
  127. if (v & 0xff00) {
  128. v >>= 8;
  129. n += 8;
  130. }
  131. n += ff_log2_tab[v];
  132. return n;
  133. }
  134. static inline av_const int av_log2_16bit(unsigned int v)
  135. {
  136. int n = 0;
  137. if (v & 0xff00) {
  138. v >>= 8;
  139. n += 8;
  140. }
  141. n += ff_log2_tab[v];
  142. return n;
  143. }
  144. /**
  145. * Clips a signed integer value into the amin-amax range.
  146. * @param a value to clip
  147. * @param amin minimum value of the clip range
  148. * @param amax maximum value of the clip range
  149. * @return clipped value
  150. */
  151. static inline av_const int av_clip(int a, int amin, int amax)
  152. {
  153. if (a < amin) return amin;
  154. else if (a > amax) return amax;
  155. else return a;
  156. }
  157. /**
  158. * Clips a signed integer value into the 0-255 range.
  159. * @param a value to clip
  160. * @return clipped value
  161. */
  162. static inline av_const uint8_t av_clip_uint8(int a)
  163. {
  164. if (a&(~255)) return (-a)>>31;
  165. else return a;
  166. }
  167. /**
  168. * Clips a signed integer value into the 0-65535 range.
  169. * @param a value to clip
  170. * @return clipped value
  171. */
  172. static inline av_const uint16_t av_clip_uint16(int a)
  173. {
  174. if (a&(~65535)) return (-a)>>31;
  175. else return a;
  176. }
  177. /**
  178. * Clips a signed integer value into the -32768,32767 range.
  179. * @param a value to clip
  180. * @return clipped value
  181. */
  182. static inline av_const int16_t av_clip_int16(int a)
  183. {
  184. if ((a+32768) & ~65535) return (a>>31) ^ 32767;
  185. else return a;
  186. }
  187. /**
  188. * Clips a float value into the amin-amax range.
  189. * @param a value to clip
  190. * @param amin minimum value of the clip range
  191. * @param amax maximum value of the clip range
  192. * @return clipped value
  193. */
  194. static inline av_const float av_clipf(float a, float amin, float amax)
  195. {
  196. if (a < amin) return amin;
  197. else if (a > amax) return amax;
  198. else return a;
  199. }
  200. /** Computes ceil(log2(x)).
  201. * @param x value used to compute ceil(log2(x))
  202. * @return computed ceiling of log2(x)
  203. */
  204. static inline av_const int av_ceil_log2(int x)
  205. {
  206. return av_log2((x - 1) << 1);
  207. }
  208. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  209. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  210. /*!
  211. * \def GET_UTF8(val, GET_BYTE, ERROR)
  212. * Converts a UTF-8 character (up to 4 bytes long) to its 32-bit UCS-4 encoded form
  213. * \param val is the output and should be of type uint32_t. It holds the converted
  214. * UCS-4 character and should be a left value.
  215. * \param GET_BYTE gets UTF-8 encoded bytes from any proper source. It can be
  216. * a function or a statement whose return value or evaluated value is of type
  217. * uint8_t. It will be executed up to 4 times for values in the valid UTF-8 range,
  218. * and up to 7 times in the general case.
  219. * \param ERROR action that should be taken when an invalid UTF-8 byte is returned
  220. * from GET_BYTE. It should be a statement that jumps out of the macro,
  221. * like exit(), goto, return, break, or continue.
  222. */
  223. #define GET_UTF8(val, GET_BYTE, ERROR)\
  224. val= GET_BYTE;\
  225. {\
  226. int ones= 7 - av_log2(val ^ 255);\
  227. if(ones==1)\
  228. ERROR\
  229. val&= 127>>ones;\
  230. while(--ones > 0){\
  231. int tmp= GET_BYTE - 128;\
  232. if(tmp>>6)\
  233. ERROR\
  234. val= (val<<6) + tmp;\
  235. }\
  236. }
  237. /*!
  238. * \def GET_UTF16(val, GET_16BIT, ERROR)
  239. * Converts a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form
  240. * \param val is the output and should be of type uint32_t. It holds the converted
  241. * UCS-4 character and should be a left value.
  242. * \param GET_16BIT gets two bytes of UTF-16 encoded data converted to native endianness.
  243. * It can be a function or a statement whose return value or evaluated value is of type
  244. * uint16_t. It will be executed up to 2 times.
  245. * \param ERROR action that should be taken when an invalid UTF-16 surrogate is
  246. * returned from GET_BYTE. It should be a statement that jumps out of the macro,
  247. * like exit(), goto, return, break, or continue.
  248. */
  249. #define GET_UTF16(val, GET_16BIT, ERROR)\
  250. val = GET_16BIT;\
  251. {\
  252. unsigned int hi = val - 0xD800;\
  253. if (hi < 0x800) {\
  254. val = GET_16BIT - 0xDC00;\
  255. if (val > 0x3FFU || hi > 0x3FFU)\
  256. ERROR\
  257. val += (hi<<10) + 0x10000;\
  258. }\
  259. }\
  260. /*!
  261. * \def PUT_UTF8(val, tmp, PUT_BYTE)
  262. * Converts a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
  263. * \param val is an input-only argument and should be of type uint32_t. It holds
  264. * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If
  265. * val is given as a function it is executed only once.
  266. * \param tmp is a temporary variable and should be of type uint8_t. It
  267. * represents an intermediate value during conversion that is to be
  268. * output by PUT_BYTE.
  269. * \param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
  270. * It could be a function or a statement, and uses tmp as the input byte.
  271. * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
  272. * executed up to 4 times for values in the valid UTF-8 range and up to
  273. * 7 times in the general case, depending on the length of the converted
  274. * Unicode character.
  275. */
  276. #define PUT_UTF8(val, tmp, PUT_BYTE)\
  277. {\
  278. int bytes, shift;\
  279. uint32_t in = val;\
  280. if (in < 0x80) {\
  281. tmp = in;\
  282. PUT_BYTE\
  283. } else {\
  284. bytes = (av_log2(in) + 4) / 5;\
  285. shift = (bytes - 1) * 6;\
  286. tmp = (256 - (256 >> bytes)) | (in >> shift);\
  287. PUT_BYTE\
  288. while (shift >= 6) {\
  289. shift -= 6;\
  290. tmp = 0x80 | ((in >> shift) & 0x3f);\
  291. PUT_BYTE\
  292. }\
  293. }\
  294. }
  295. #include "mem.h"
  296. #ifdef HAVE_AV_CONFIG_H
  297. # include "internal.h"
  298. #endif /* HAVE_AV_CONFIG_H */
  299. #endif /* AVUTIL_COMMON_H */