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.

272 lines
7.4KB

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