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