common.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. /* misc math functions */
  115. extern const uint8_t ff_log2_tab[256];
  116. static inline av_const int av_log2(unsigned int v)
  117. {
  118. int n = 0;
  119. if (v & 0xffff0000) {
  120. v >>= 16;
  121. n += 16;
  122. }
  123. if (v & 0xff00) {
  124. v >>= 8;
  125. n += 8;
  126. }
  127. n += ff_log2_tab[v];
  128. return n;
  129. }
  130. static inline av_const int av_log2_16bit(unsigned int v)
  131. {
  132. int n = 0;
  133. if (v & 0xff00) {
  134. v >>= 8;
  135. n += 8;
  136. }
  137. n += ff_log2_tab[v];
  138. return n;
  139. }
  140. /**
  141. * Clips a signed integer value into the amin-amax range.
  142. * @param a value to clip
  143. * @param amin minimum value of the clip range
  144. * @param amax maximum value of the clip range
  145. * @return clipped value
  146. */
  147. static inline av_const int av_clip(int a, int amin, int amax)
  148. {
  149. if (a < amin) return amin;
  150. else if (a > amax) return amax;
  151. else return a;
  152. }
  153. /**
  154. * Clips a signed integer value into the 0-255 range.
  155. * @param a value to clip
  156. * @return clipped value
  157. */
  158. static inline av_const uint8_t av_clip_uint8(int a)
  159. {
  160. if (a&(~255)) return (-a)>>31;
  161. else return a;
  162. }
  163. /**
  164. * Clips a signed integer value into the -32768,32767 range.
  165. * @param a value to clip
  166. * @return clipped value
  167. */
  168. static inline av_const int16_t av_clip_int16(int a)
  169. {
  170. if ((a+32768) & ~65535) return (a>>31) ^ 32767;
  171. else return a;
  172. }
  173. /**
  174. * Clips a float value into the amin-amax range.
  175. * @param a value to clip
  176. * @param amin minimum value of the clip range
  177. * @param amax maximum value of the clip range
  178. * @return clipped value
  179. */
  180. static inline av_const float av_clipf(float a, float amin, float amax)
  181. {
  182. if (a < amin) return amin;
  183. else if (a > amax) return amax;
  184. else return a;
  185. }
  186. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  187. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  188. /*!
  189. * \def GET_UTF8(val, GET_BYTE, ERROR)
  190. * Converts a UTF-8 character (up to 4 bytes long) to its 32-bit UCS-4 encoded form
  191. * \param val is the output and should be of type uint32_t. It holds the converted
  192. * UCS-4 character and should be a left value.
  193. * \param GET_BYTE gets UTF-8 encoded bytes from any proper source. It can be
  194. * a function or a statement whose return value or evaluated value is of type
  195. * uint8_t. It will be executed up to 4 times for values in the valid UTF-8 range,
  196. * and up to 7 times in the general case.
  197. * \param ERROR action that should be taken when an invalid UTF-8 byte is returned
  198. * from GET_BYTE. It should be a statement that jumps out of the macro,
  199. * like exit(), goto, return, break, or continue.
  200. */
  201. #define GET_UTF8(val, GET_BYTE, ERROR)\
  202. val= GET_BYTE;\
  203. {\
  204. int ones= 7 - av_log2(val ^ 255);\
  205. if(ones==1)\
  206. ERROR\
  207. val&= 127>>ones;\
  208. while(--ones > 0){\
  209. int tmp= GET_BYTE - 128;\
  210. if(tmp>>6)\
  211. ERROR\
  212. val= (val<<6) + tmp;\
  213. }\
  214. }
  215. /*!
  216. * \def PUT_UTF8(val, tmp, PUT_BYTE)
  217. * Converts a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
  218. * \param val is an input-only argument and should be of type uint32_t. It holds
  219. * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If
  220. * val is given as a function it is executed only once.
  221. * \param tmp is a temporary variable and should be of type uint8_t. It
  222. * represents an intermediate value during conversion that is to be
  223. * output by PUT_BYTE.
  224. * \param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
  225. * It could be a function or a statement, and uses tmp as the input byte.
  226. * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
  227. * executed up to 4 times for values in the valid UTF-8 range and up to
  228. * 7 times in the general case, depending on the length of the converted
  229. * Unicode character.
  230. */
  231. #define PUT_UTF8(val, tmp, PUT_BYTE)\
  232. {\
  233. int bytes, shift;\
  234. uint32_t in = val;\
  235. if (in < 0x80) {\
  236. tmp = in;\
  237. PUT_BYTE\
  238. } else {\
  239. bytes = (av_log2(in) + 4) / 5;\
  240. shift = (bytes - 1) * 6;\
  241. tmp = (256 - (256 >> bytes)) | (in >> shift);\
  242. PUT_BYTE\
  243. while (shift >= 6) {\
  244. shift -= 6;\
  245. tmp = 0x80 | ((in >> shift) & 0x3f);\
  246. PUT_BYTE\
  247. }\
  248. }\
  249. }
  250. #include "mem.h"
  251. #ifdef HAVE_AV_CONFIG_H
  252. # include "config.h"
  253. # include "internal.h"
  254. #endif /* HAVE_AV_CONFIG_H */
  255. #endif /* AVUTIL_COMMON_H */