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.

337 lines
9.1KB

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