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.

267 lines
7.2KB

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