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.

283 lines
7.6KB

  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. #define AV_GCC_VERSION_AT_LEAST(x,y) (defined(__GNUC__) && (__GNUC__ > x || __GNUC__ == x && __GNUC_MINOR__ >= y))
  35. #ifndef av_always_inline
  36. #if AV_GCC_VERSION_AT_LEAST(3,1)
  37. # define av_always_inline __attribute__((always_inline)) inline
  38. #else
  39. # define av_always_inline inline
  40. #endif
  41. #endif
  42. #ifndef av_noinline
  43. #if AV_GCC_VERSION_AT_LEAST(3,1)
  44. # define av_noinline __attribute__((noinline))
  45. #else
  46. # define av_noinline
  47. #endif
  48. #endif
  49. #ifndef av_pure
  50. #if AV_GCC_VERSION_AT_LEAST(3,1)
  51. # define av_pure __attribute__((pure))
  52. #else
  53. # define av_pure
  54. #endif
  55. #endif
  56. #ifndef av_const
  57. #if AV_GCC_VERSION_AT_LEAST(2,6)
  58. # define av_const __attribute__((const))
  59. #else
  60. # define av_const
  61. #endif
  62. #endif
  63. #ifndef av_cold
  64. #if (!defined(__ICC) || __ICC > 1100) && AV_GCC_VERSION_AT_LEAST(4,3)
  65. # define av_cold __attribute__((cold))
  66. #else
  67. # define av_cold
  68. #endif
  69. #endif
  70. #ifndef av_flatten
  71. #if defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ > 0)
  72. # define av_flatten __attribute__((flatten))
  73. #else
  74. # define av_flatten
  75. #endif
  76. #endif
  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. #ifndef av_uninit
  92. #if defined(__GNUC__)
  93. # define av_uninit(x) x=x
  94. #else
  95. # define av_uninit(x) x
  96. #endif
  97. #endif
  98. //rounded division & shift
  99. #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
  100. /* assume b>0 */
  101. #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
  102. #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
  103. #define FFSIGN(a) ((a) > 0 ? 1 : -1)
  104. #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
  105. #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
  106. #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
  107. #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
  108. #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
  109. #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
  110. /* misc math functions */
  111. extern const uint8_t ff_log2_tab[256];
  112. static inline av_const int av_log2(unsigned int v)
  113. {
  114. int n = 0;
  115. if (v & 0xffff0000) {
  116. v >>= 16;
  117. n += 16;
  118. }
  119. if (v & 0xff00) {
  120. v >>= 8;
  121. n += 8;
  122. }
  123. n += ff_log2_tab[v];
  124. return n;
  125. }
  126. static inline av_const int av_log2_16bit(unsigned int v)
  127. {
  128. int n = 0;
  129. if (v & 0xff00) {
  130. v >>= 8;
  131. n += 8;
  132. }
  133. n += ff_log2_tab[v];
  134. return n;
  135. }
  136. /**
  137. * Clips a signed integer value into the amin-amax range.
  138. * @param a value to clip
  139. * @param amin minimum value of the clip range
  140. * @param amax maximum value of the clip range
  141. * @return clipped value
  142. */
  143. static inline av_const int av_clip(int a, int amin, int amax)
  144. {
  145. if (a < amin) return amin;
  146. else if (a > amax) return amax;
  147. else return a;
  148. }
  149. /**
  150. * Clips a signed integer value into the 0-255 range.
  151. * @param a value to clip
  152. * @return clipped value
  153. */
  154. static inline av_const uint8_t av_clip_uint8(int a)
  155. {
  156. if (a&(~255)) return (-a)>>31;
  157. else return a;
  158. }
  159. /**
  160. * Clips a signed integer value into the -32768,32767 range.
  161. * @param a value to clip
  162. * @return clipped value
  163. */
  164. static inline av_const int16_t av_clip_int16(int a)
  165. {
  166. if ((a+32768) & ~65535) return (a>>31) ^ 32767;
  167. else return a;
  168. }
  169. /**
  170. * Clips a float 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 float av_clipf(float a, float amin, float amax)
  177. {
  178. if (a < amin) return amin;
  179. else if (a > amax) return amax;
  180. else return a;
  181. }
  182. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  183. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  184. /*!
  185. * \def GET_UTF8(val, GET_BYTE, ERROR)
  186. * Converts a UTF-8 character (up to 4 bytes long) to its 32-bit UCS-4 encoded form
  187. * \param val is the output and should be of type uint32_t. It holds the converted
  188. * UCS-4 character and should be a left value.
  189. * \param GET_BYTE gets UTF-8 encoded bytes from any proper source. It can be
  190. * a function or a statement whose return value or evaluated value is of type
  191. * uint8_t. It will be executed up to 4 times for values in the valid UTF-8 range,
  192. * and up to 7 times in the general case.
  193. * \param ERROR action that should be taken when an invalid UTF-8 byte is returned
  194. * from GET_BYTE. It should be a statement that jumps out of the macro,
  195. * like exit(), goto, return, break, or continue.
  196. */
  197. #define GET_UTF8(val, GET_BYTE, ERROR)\
  198. val= GET_BYTE;\
  199. {\
  200. int ones= 7 - av_log2(val ^ 255);\
  201. if(ones==1)\
  202. ERROR\
  203. val&= 127>>ones;\
  204. while(--ones > 0){\
  205. int tmp= GET_BYTE - 128;\
  206. if(tmp>>6)\
  207. ERROR\
  208. val= (val<<6) + tmp;\
  209. }\
  210. }
  211. /*!
  212. * \def PUT_UTF8(val, tmp, PUT_BYTE)
  213. * Converts a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
  214. * \param val is an input-only argument and should be of type uint32_t. It holds
  215. * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If
  216. * val is given as a function it is executed only once.
  217. * \param tmp is a temporary variable and should be of type uint8_t. It
  218. * represents an intermediate value during conversion that is to be
  219. * output by PUT_BYTE.
  220. * \param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
  221. * It could be a function or a statement, and uses tmp as the input byte.
  222. * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
  223. * executed up to 4 times for values in the valid UTF-8 range and up to
  224. * 7 times in the general case, depending on the length of the converted
  225. * Unicode character.
  226. */
  227. #define PUT_UTF8(val, tmp, PUT_BYTE)\
  228. {\
  229. int bytes, shift;\
  230. uint32_t in = val;\
  231. if (in < 0x80) {\
  232. tmp = in;\
  233. PUT_BYTE\
  234. } else {\
  235. bytes = (av_log2(in) + 4) / 5;\
  236. shift = (bytes - 1) * 6;\
  237. tmp = (256 - (256 >> bytes)) | (in >> shift);\
  238. PUT_BYTE\
  239. while (shift >= 6) {\
  240. shift -= 6;\
  241. tmp = 0x80 | ((in >> shift) & 0x3f);\
  242. PUT_BYTE\
  243. }\
  244. }\
  245. }
  246. #include "mem.h"
  247. #ifdef HAVE_AV_CONFIG_H
  248. # include "config.h"
  249. # include "internal.h"
  250. #endif /* HAVE_AV_CONFIG_H */
  251. #endif /* AVUTIL_COMMON_H */