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.

295 lines
8.8KB

  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. #include "attributes.h"
  35. #ifdef HAVE_AV_CONFIG_H
  36. # include "config.h"
  37. # include "intmath.h"
  38. #endif
  39. //rounded division & shift
  40. #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
  41. /* assume b>0 */
  42. #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
  43. #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
  44. #define FFSIGN(a) ((a) > 0 ? 1 : -1)
  45. #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
  46. #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
  47. #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
  48. #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
  49. #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
  50. #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
  51. #define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1))
  52. /* misc math functions */
  53. extern const uint8_t ff_log2_tab[256];
  54. extern const uint8_t av_reverse[256];
  55. #ifndef av_log2
  56. static inline av_const int av_log2(unsigned int v)
  57. {
  58. int n = 0;
  59. if (v & 0xffff0000) {
  60. v >>= 16;
  61. n += 16;
  62. }
  63. if (v & 0xff00) {
  64. v >>= 8;
  65. n += 8;
  66. }
  67. n += ff_log2_tab[v];
  68. return n;
  69. }
  70. #endif
  71. #ifndef av_log2_16bit
  72. static inline av_const int av_log2_16bit(unsigned int v)
  73. {
  74. int n = 0;
  75. if (v & 0xff00) {
  76. v >>= 8;
  77. n += 8;
  78. }
  79. n += ff_log2_tab[v];
  80. return n;
  81. }
  82. #endif
  83. /**
  84. * Clips a signed integer value into the amin-amax range.
  85. * @param a value to clip
  86. * @param amin minimum value of the clip range
  87. * @param amax maximum value of the clip range
  88. * @return clipped value
  89. */
  90. static inline av_const int av_clip(int a, int amin, int amax)
  91. {
  92. if (a < amin) return amin;
  93. else if (a > amax) return amax;
  94. else return a;
  95. }
  96. /**
  97. * Clips a signed integer value into the 0-255 range.
  98. * @param a value to clip
  99. * @return clipped value
  100. */
  101. static inline av_const uint8_t av_clip_uint8(int a)
  102. {
  103. if (a&(~255)) return (-a)>>31;
  104. else return a;
  105. }
  106. /**
  107. * Clips a signed integer value into the 0-65535 range.
  108. * @param a value to clip
  109. * @return clipped value
  110. */
  111. static inline av_const uint16_t av_clip_uint16(int a)
  112. {
  113. if (a&(~65535)) return (-a)>>31;
  114. else return a;
  115. }
  116. /**
  117. * Clips a signed integer value into the -32768,32767 range.
  118. * @param a value to clip
  119. * @return clipped value
  120. */
  121. static inline av_const int16_t av_clip_int16(int a)
  122. {
  123. if ((a+32768) & ~65535) return (a>>31) ^ 32767;
  124. else return a;
  125. }
  126. /**
  127. * Clips a float value into the amin-amax range.
  128. * @param a value to clip
  129. * @param amin minimum value of the clip range
  130. * @param amax maximum value of the clip range
  131. * @return clipped value
  132. */
  133. static inline av_const float av_clipf(float a, float amin, float amax)
  134. {
  135. if (a < amin) return amin;
  136. else if (a > amax) return amax;
  137. else return a;
  138. }
  139. /** Computes ceil(log2(x)).
  140. * @param x value used to compute ceil(log2(x))
  141. * @return computed ceiling of log2(x)
  142. */
  143. static inline av_const int av_ceil_log2(int x)
  144. {
  145. return av_log2((x - 1) << 1);
  146. }
  147. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  148. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  149. /*!
  150. * \def GET_UTF8(val, GET_BYTE, ERROR)
  151. * Converts a UTF-8 character (up to 4 bytes long) to its 32-bit UCS-4 encoded form
  152. * \param val is the output and should be of type uint32_t. It holds the converted
  153. * UCS-4 character and should be a left value.
  154. * \param GET_BYTE gets UTF-8 encoded bytes from any proper source. It can be
  155. * a function or a statement whose return value or evaluated value is of type
  156. * uint8_t. It will be executed up to 4 times for values in the valid UTF-8 range,
  157. * and up to 7 times in the general case.
  158. * \param ERROR action that should be taken when an invalid UTF-8 byte is returned
  159. * from GET_BYTE. It should be a statement that jumps out of the macro,
  160. * like exit(), goto, return, break, or continue.
  161. */
  162. #define GET_UTF8(val, GET_BYTE, ERROR)\
  163. val= GET_BYTE;\
  164. {\
  165. int ones= 7 - av_log2(val ^ 255);\
  166. if(ones==1)\
  167. ERROR\
  168. val&= 127>>ones;\
  169. while(--ones > 0){\
  170. int tmp= GET_BYTE - 128;\
  171. if(tmp>>6)\
  172. ERROR\
  173. val= (val<<6) + tmp;\
  174. }\
  175. }
  176. /*!
  177. * \def GET_UTF16(val, GET_16BIT, ERROR)
  178. * Converts a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form
  179. * \param val is the output and should be of type uint32_t. It holds the converted
  180. * UCS-4 character and should be a left value.
  181. * \param GET_16BIT gets two bytes of UTF-16 encoded data converted to native endianness.
  182. * It can be a function or a statement whose return value or evaluated value is of type
  183. * uint16_t. It will be executed up to 2 times.
  184. * \param ERROR action that should be taken when an invalid UTF-16 surrogate is
  185. * returned 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_UTF16(val, GET_16BIT, ERROR)\
  189. val = GET_16BIT;\
  190. {\
  191. unsigned int hi = val - 0xD800;\
  192. if (hi < 0x800) {\
  193. val = GET_16BIT - 0xDC00;\
  194. if (val > 0x3FFU || hi > 0x3FFU)\
  195. ERROR\
  196. val += (hi<<10) + 0x10000;\
  197. }\
  198. }\
  199. /*!
  200. * \def PUT_UTF8(val, tmp, PUT_BYTE)
  201. * Converts a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
  202. * \param val is an input-only argument and should be of type uint32_t. It holds
  203. * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If
  204. * val is given as a function it is executed only once.
  205. * \param tmp is a temporary variable and should be of type uint8_t. It
  206. * represents an intermediate value during conversion that is to be
  207. * output by PUT_BYTE.
  208. * \param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
  209. * It could be a function or a statement, and uses tmp as the input byte.
  210. * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
  211. * executed up to 4 times for values in the valid UTF-8 range and up to
  212. * 7 times in the general case, depending on the length of the converted
  213. * Unicode character.
  214. */
  215. #define PUT_UTF8(val, tmp, PUT_BYTE)\
  216. {\
  217. int bytes, shift;\
  218. uint32_t in = val;\
  219. if (in < 0x80) {\
  220. tmp = in;\
  221. PUT_BYTE\
  222. } else {\
  223. bytes = (av_log2(in) + 4) / 5;\
  224. shift = (bytes - 1) * 6;\
  225. tmp = (256 - (256 >> bytes)) | (in >> shift);\
  226. PUT_BYTE\
  227. while (shift >= 6) {\
  228. shift -= 6;\
  229. tmp = 0x80 | ((in >> shift) & 0x3f);\
  230. PUT_BYTE\
  231. }\
  232. }\
  233. }
  234. /*!
  235. * \def PUT_UTF16(val, tmp, PUT_16BIT)
  236. * Converts a 32-bit Unicode character to its UTF-16 encoded form (2 or 4 bytes).
  237. * \param val is an input-only argument and should be of type uint32_t. It holds
  238. * a UCS-4 encoded Unicode character that is to be converted to UTF-16. If
  239. * val is given as a function it is executed only once.
  240. * \param tmp is a temporary variable and should be of type uint16_t. It
  241. * represents an intermediate value during conversion that is to be
  242. * output by PUT_16BIT.
  243. * \param PUT_16BIT writes the converted UTF-16 data to any proper destination
  244. * in desired endianness. It could be a function or a statement, and uses tmp
  245. * as the input byte. For example, PUT_BYTE could be "*output++ = tmp;"
  246. * PUT_BYTE will be executed 1 or 2 times depending on input character.
  247. */
  248. #define PUT_UTF16(val, tmp, PUT_16BIT)\
  249. {\
  250. uint32_t in = val;\
  251. if (in < 0x10000) {\
  252. tmp = in;\
  253. PUT_16BIT\
  254. } else {\
  255. tmp = 0xD800 | ((in - 0x10000) >> 10);\
  256. PUT_16BIT\
  257. tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\
  258. PUT_16BIT\
  259. }\
  260. }\
  261. #include "mem.h"
  262. #ifdef HAVE_AV_CONFIG_H
  263. # include "internal.h"
  264. #endif /* HAVE_AV_CONFIG_H */
  265. #endif /* AVUTIL_COMMON_H */