common.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 -32768,32767 range.
  169. * @param a value to clip
  170. * @return clipped value
  171. */
  172. static inline av_const int16_t av_clip_int16(int a)
  173. {
  174. if ((a+32768) & ~65535) return (a>>31) ^ 32767;
  175. else return a;
  176. }
  177. /**
  178. * Clips a float value into the amin-amax range.
  179. * @param a value to clip
  180. * @param amin minimum value of the clip range
  181. * @param amax maximum value of the clip range
  182. * @return clipped value
  183. */
  184. static inline av_const float av_clipf(float a, float amin, float amax)
  185. {
  186. if (a < amin) return amin;
  187. else if (a > amax) return amax;
  188. else return a;
  189. }
  190. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  191. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  192. /*!
  193. * \def GET_UTF8(val, GET_BYTE, ERROR)
  194. * Converts a UTF-8 character (up to 4 bytes long) to its 32-bit UCS-4 encoded form
  195. * \param val is the output and should be of type uint32_t. It holds the converted
  196. * UCS-4 character and should be a left value.
  197. * \param GET_BYTE gets UTF-8 encoded bytes from any proper source. It can be
  198. * a function or a statement whose return value or evaluated value is of type
  199. * uint8_t. It will be executed up to 4 times for values in the valid UTF-8 range,
  200. * and up to 7 times in the general case.
  201. * \param ERROR action that should be taken when an invalid UTF-8 byte is returned
  202. * from GET_BYTE. It should be a statement that jumps out of the macro,
  203. * like exit(), goto, return, break, or continue.
  204. */
  205. #define GET_UTF8(val, GET_BYTE, ERROR)\
  206. val= GET_BYTE;\
  207. {\
  208. int ones= 7 - av_log2(val ^ 255);\
  209. if(ones==1)\
  210. ERROR\
  211. val&= 127>>ones;\
  212. while(--ones > 0){\
  213. int tmp= GET_BYTE - 128;\
  214. if(tmp>>6)\
  215. ERROR\
  216. val= (val<<6) + tmp;\
  217. }\
  218. }
  219. /*!
  220. * \def PUT_UTF8(val, tmp, PUT_BYTE)
  221. * Converts a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
  222. * \param val is an input-only argument and should be of type uint32_t. It holds
  223. * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If
  224. * val is given as a function it is executed only once.
  225. * \param tmp is a temporary variable and should be of type uint8_t. It
  226. * represents an intermediate value during conversion that is to be
  227. * output by PUT_BYTE.
  228. * \param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
  229. * It could be a function or a statement, and uses tmp as the input byte.
  230. * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
  231. * executed up to 4 times for values in the valid UTF-8 range and up to
  232. * 7 times in the general case, depending on the length of the converted
  233. * Unicode character.
  234. */
  235. #define PUT_UTF8(val, tmp, PUT_BYTE)\
  236. {\
  237. int bytes, shift;\
  238. uint32_t in = val;\
  239. if (in < 0x80) {\
  240. tmp = in;\
  241. PUT_BYTE\
  242. } else {\
  243. bytes = (av_log2(in) + 4) / 5;\
  244. shift = (bytes - 1) * 6;\
  245. tmp = (256 - (256 >> bytes)) | (in >> shift);\
  246. PUT_BYTE\
  247. while (shift >= 6) {\
  248. shift -= 6;\
  249. tmp = 0x80 | ((in >> shift) & 0x3f);\
  250. PUT_BYTE\
  251. }\
  252. }\
  253. }
  254. #include "mem.h"
  255. #ifdef HAVE_AV_CONFIG_H
  256. # include "internal.h"
  257. #endif /* HAVE_AV_CONFIG_H */
  258. #endif /* AVUTIL_COMMON_H */