common.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. #ifdef HAVE_AV_CONFIG_H
  106. # include "intmath.h"
  107. #endif
  108. //rounded division & shift
  109. #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
  110. /* assume b>0 */
  111. #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
  112. #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
  113. #define FFSIGN(a) ((a) > 0 ? 1 : -1)
  114. #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
  115. #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
  116. #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
  117. #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
  118. #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
  119. #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
  120. #define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1))
  121. /* misc math functions */
  122. extern const uint8_t ff_log2_tab[256];
  123. extern const uint8_t av_reverse[256];
  124. #ifndef av_log2
  125. static inline av_const int av_log2(unsigned int v)
  126. {
  127. int n = 0;
  128. if (v & 0xffff0000) {
  129. v >>= 16;
  130. n += 16;
  131. }
  132. if (v & 0xff00) {
  133. v >>= 8;
  134. n += 8;
  135. }
  136. n += ff_log2_tab[v];
  137. return n;
  138. }
  139. #endif
  140. #ifndef av_log2_16bit
  141. static inline av_const int av_log2_16bit(unsigned int v)
  142. {
  143. int n = 0;
  144. if (v & 0xff00) {
  145. v >>= 8;
  146. n += 8;
  147. }
  148. n += ff_log2_tab[v];
  149. return n;
  150. }
  151. #endif
  152. /**
  153. * Clips a signed integer value into the amin-amax range.
  154. * @param a value to clip
  155. * @param amin minimum value of the clip range
  156. * @param amax maximum value of the clip range
  157. * @return clipped value
  158. */
  159. static inline av_const int av_clip(int a, int amin, int amax)
  160. {
  161. if (a < amin) return amin;
  162. else if (a > amax) return amax;
  163. else return a;
  164. }
  165. /**
  166. * Clips a signed integer value into the 0-255 range.
  167. * @param a value to clip
  168. * @return clipped value
  169. */
  170. static inline av_const uint8_t av_clip_uint8(int a)
  171. {
  172. if (a&(~255)) return (-a)>>31;
  173. else return a;
  174. }
  175. /**
  176. * Clips a signed integer value into the 0-65535 range.
  177. * @param a value to clip
  178. * @return clipped value
  179. */
  180. static inline av_const uint16_t av_clip_uint16(int a)
  181. {
  182. if (a&(~65535)) return (-a)>>31;
  183. else return a;
  184. }
  185. /**
  186. * Clips a signed integer value into the -32768,32767 range.
  187. * @param a value to clip
  188. * @return clipped value
  189. */
  190. static inline av_const int16_t av_clip_int16(int a)
  191. {
  192. if ((a+32768) & ~65535) return (a>>31) ^ 32767;
  193. else return a;
  194. }
  195. /**
  196. * Clips a float value into the amin-amax range.
  197. * @param a value to clip
  198. * @param amin minimum value of the clip range
  199. * @param amax maximum value of the clip range
  200. * @return clipped value
  201. */
  202. static inline av_const float av_clipf(float a, float amin, float amax)
  203. {
  204. if (a < amin) return amin;
  205. else if (a > amax) return amax;
  206. else return a;
  207. }
  208. /** Computes ceil(log2(x)).
  209. * @param x value used to compute ceil(log2(x))
  210. * @return computed ceiling of log2(x)
  211. */
  212. static inline av_const int av_ceil_log2(int x)
  213. {
  214. return av_log2((x - 1) << 1);
  215. }
  216. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  217. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  218. /*!
  219. * \def GET_UTF8(val, GET_BYTE, ERROR)
  220. * Converts a UTF-8 character (up to 4 bytes long) to its 32-bit UCS-4 encoded form
  221. * \param val is the output and should be of type uint32_t. It holds the converted
  222. * UCS-4 character and should be a left value.
  223. * \param GET_BYTE gets UTF-8 encoded bytes from any proper source. It can be
  224. * a function or a statement whose return value or evaluated value is of type
  225. * uint8_t. It will be executed up to 4 times for values in the valid UTF-8 range,
  226. * and up to 7 times in the general case.
  227. * \param ERROR action that should be taken when an invalid UTF-8 byte is returned
  228. * from GET_BYTE. It should be a statement that jumps out of the macro,
  229. * like exit(), goto, return, break, or continue.
  230. */
  231. #define GET_UTF8(val, GET_BYTE, ERROR)\
  232. val= GET_BYTE;\
  233. {\
  234. int ones= 7 - av_log2(val ^ 255);\
  235. if(ones==1)\
  236. ERROR\
  237. val&= 127>>ones;\
  238. while(--ones > 0){\
  239. int tmp= GET_BYTE - 128;\
  240. if(tmp>>6)\
  241. ERROR\
  242. val= (val<<6) + tmp;\
  243. }\
  244. }
  245. /*!
  246. * \def GET_UTF16(val, GET_16BIT, ERROR)
  247. * Converts a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form
  248. * \param val is the output and should be of type uint32_t. It holds the converted
  249. * UCS-4 character and should be a left value.
  250. * \param GET_16BIT gets two bytes of UTF-16 encoded data converted to native endianness.
  251. * It can be a function or a statement whose return value or evaluated value is of type
  252. * uint16_t. It will be executed up to 2 times.
  253. * \param ERROR action that should be taken when an invalid UTF-16 surrogate is
  254. * returned from GET_BYTE. It should be a statement that jumps out of the macro,
  255. * like exit(), goto, return, break, or continue.
  256. */
  257. #define GET_UTF16(val, GET_16BIT, ERROR)\
  258. val = GET_16BIT;\
  259. {\
  260. unsigned int hi = val - 0xD800;\
  261. if (hi < 0x800) {\
  262. val = GET_16BIT - 0xDC00;\
  263. if (val > 0x3FFU || hi > 0x3FFU)\
  264. ERROR\
  265. val += (hi<<10) + 0x10000;\
  266. }\
  267. }\
  268. /*!
  269. * \def PUT_UTF8(val, tmp, PUT_BYTE)
  270. * Converts a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
  271. * \param val is an input-only argument and should be of type uint32_t. It holds
  272. * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If
  273. * val is given as a function it is executed only once.
  274. * \param tmp is a temporary variable and should be of type uint8_t. It
  275. * represents an intermediate value during conversion that is to be
  276. * output by PUT_BYTE.
  277. * \param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
  278. * It could be a function or a statement, and uses tmp as the input byte.
  279. * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
  280. * executed up to 4 times for values in the valid UTF-8 range and up to
  281. * 7 times in the general case, depending on the length of the converted
  282. * Unicode character.
  283. */
  284. #define PUT_UTF8(val, tmp, PUT_BYTE)\
  285. {\
  286. int bytes, shift;\
  287. uint32_t in = val;\
  288. if (in < 0x80) {\
  289. tmp = in;\
  290. PUT_BYTE\
  291. } else {\
  292. bytes = (av_log2(in) + 4) / 5;\
  293. shift = (bytes - 1) * 6;\
  294. tmp = (256 - (256 >> bytes)) | (in >> shift);\
  295. PUT_BYTE\
  296. while (shift >= 6) {\
  297. shift -= 6;\
  298. tmp = 0x80 | ((in >> shift) & 0x3f);\
  299. PUT_BYTE\
  300. }\
  301. }\
  302. }
  303. #include "mem.h"
  304. #ifdef HAVE_AV_CONFIG_H
  305. # include "internal.h"
  306. #endif /* HAVE_AV_CONFIG_H */
  307. #endif /* AVUTIL_COMMON_H */