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.

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