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.

459 lines
13KB

  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
  22. * common internal and external API header
  23. */
  24. #ifndef AVUTIL_COMMON_H
  25. #define AVUTIL_COMMON_H
  26. #include <errno.h>
  27. #include <inttypes.h>
  28. #include <limits.h>
  29. #include <math.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "attributes.h"
  34. #include "version.h"
  35. #include "libavutil/avconfig.h"
  36. #if AV_HAVE_BIGENDIAN
  37. # define AV_NE(be, le) (be)
  38. #else
  39. # define AV_NE(be, le) (le)
  40. #endif
  41. //rounded division & shift
  42. #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
  43. /* assume b>0 */
  44. #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
  45. /* assume a>0 and b>0 */
  46. #define FF_CEIL_RSHIFT(a,b) (-((-(a)) >> (b)))
  47. #define FFUDIV(a,b) (((a)>0 ?(a):(a)-(b)+1) / (b))
  48. #define FFUMOD(a,b) ((a)-(b)*FFUDIV(a,b))
  49. #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
  50. #define FFSIGN(a) ((a) > 0 ? 1 : -1)
  51. #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
  52. #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
  53. #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
  54. #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
  55. #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
  56. #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
  57. #define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1))
  58. /* misc math functions */
  59. /**
  60. * Reverse the order of the bits of an 8-bits unsigned integer.
  61. */
  62. #if FF_API_AV_REVERSE
  63. extern attribute_deprecated const uint8_t av_reverse[256];
  64. #endif
  65. #ifdef HAVE_AV_CONFIG_H
  66. # include "config.h"
  67. # include "intmath.h"
  68. #endif
  69. /* Pull in unguarded fallback defines at the end of this file. */
  70. #include "common.h"
  71. #ifndef av_log2
  72. av_const int av_log2(unsigned v);
  73. #endif
  74. #ifndef av_log2_16bit
  75. av_const int av_log2_16bit(unsigned v);
  76. #endif
  77. /**
  78. * Clip a signed integer value into the amin-amax range.
  79. * @param a value to clip
  80. * @param amin minimum value of the clip range
  81. * @param amax maximum value of the clip range
  82. * @return clipped value
  83. */
  84. static av_always_inline av_const int av_clip_c(int a, int amin, int amax)
  85. {
  86. #if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
  87. if (amin > amax) abort();
  88. #endif
  89. if (a < amin) return amin;
  90. else if (a > amax) return amax;
  91. else return a;
  92. }
  93. /**
  94. * Clip a signed 64bit integer value into the amin-amax range.
  95. * @param a value to clip
  96. * @param amin minimum value of the clip range
  97. * @param amax maximum value of the clip range
  98. * @return clipped value
  99. */
  100. static av_always_inline av_const int64_t av_clip64_c(int64_t a, int64_t amin, int64_t amax)
  101. {
  102. #if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
  103. if (amin > amax) abort();
  104. #endif
  105. if (a < amin) return amin;
  106. else if (a > amax) return amax;
  107. else return a;
  108. }
  109. /**
  110. * Clip a signed integer value into the 0-255 range.
  111. * @param a value to clip
  112. * @return clipped value
  113. */
  114. static av_always_inline av_const uint8_t av_clip_uint8_c(int a)
  115. {
  116. if (a&(~0xFF)) return (-a)>>31;
  117. else return a;
  118. }
  119. /**
  120. * Clip a signed integer value into the -128,127 range.
  121. * @param a value to clip
  122. * @return clipped value
  123. */
  124. static av_always_inline av_const int8_t av_clip_int8_c(int a)
  125. {
  126. if ((a+0x80) & ~0xFF) return (a>>31) ^ 0x7F;
  127. else return a;
  128. }
  129. /**
  130. * Clip a signed integer value into the 0-65535 range.
  131. * @param a value to clip
  132. * @return clipped value
  133. */
  134. static av_always_inline av_const uint16_t av_clip_uint16_c(int a)
  135. {
  136. if (a&(~0xFFFF)) return (-a)>>31;
  137. else return a;
  138. }
  139. /**
  140. * Clip a signed integer value into the -32768,32767 range.
  141. * @param a value to clip
  142. * @return clipped value
  143. */
  144. static av_always_inline av_const int16_t av_clip_int16_c(int a)
  145. {
  146. if ((a+0x8000) & ~0xFFFF) return (a>>31) ^ 0x7FFF;
  147. else return a;
  148. }
  149. /**
  150. * Clip a signed 64-bit integer value into the -2147483648,2147483647 range.
  151. * @param a value to clip
  152. * @return clipped value
  153. */
  154. static av_always_inline av_const int32_t av_clipl_int32_c(int64_t a)
  155. {
  156. if ((a+0x80000000u) & ~UINT64_C(0xFFFFFFFF)) return (a>>63) ^ 0x7FFFFFFF;
  157. else return (int32_t)a;
  158. }
  159. /**
  160. * Clip a signed integer to an unsigned power of two range.
  161. * @param a value to clip
  162. * @param p bit position to clip at
  163. * @return clipped value
  164. */
  165. static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)
  166. {
  167. if (a & ~((1<<p) - 1)) return -a >> 31 & ((1<<p) - 1);
  168. else return a;
  169. }
  170. /**
  171. * Add two signed 32-bit values with saturation.
  172. *
  173. * @param a one value
  174. * @param b another value
  175. * @return sum with signed saturation
  176. */
  177. static av_always_inline int av_sat_add32_c(int a, int b)
  178. {
  179. return av_clipl_int32((int64_t)a + b);
  180. }
  181. /**
  182. * Add a doubled value to another value with saturation at both stages.
  183. *
  184. * @param a first value
  185. * @param b value doubled and added to a
  186. * @return sum with signed saturation
  187. */
  188. static av_always_inline int av_sat_dadd32_c(int a, int b)
  189. {
  190. return av_sat_add32(a, av_sat_add32(b, b));
  191. }
  192. /**
  193. * Clip a float value into the amin-amax range.
  194. * @param a value to clip
  195. * @param amin minimum value of the clip range
  196. * @param amax maximum value of the clip range
  197. * @return clipped value
  198. */
  199. static av_always_inline av_const float av_clipf_c(float a, float amin, float amax)
  200. {
  201. #if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
  202. if (amin > amax) abort();
  203. #endif
  204. if (a < amin) return amin;
  205. else if (a > amax) return amax;
  206. else return a;
  207. }
  208. /**
  209. * Clip a double value into the amin-amax range.
  210. * @param a value to clip
  211. * @param amin minimum value of the clip range
  212. * @param amax maximum value of the clip range
  213. * @return clipped value
  214. */
  215. static av_always_inline av_const double av_clipd_c(double a, double amin, double amax)
  216. {
  217. #if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
  218. if (amin > amax) abort();
  219. #endif
  220. if (a < amin) return amin;
  221. else if (a > amax) return amax;
  222. else return a;
  223. }
  224. /** Compute ceil(log2(x)).
  225. * @param x value used to compute ceil(log2(x))
  226. * @return computed ceiling of log2(x)
  227. */
  228. static av_always_inline av_const int av_ceil_log2_c(int x)
  229. {
  230. return av_log2((x - 1) << 1);
  231. }
  232. /**
  233. * Count number of bits set to one in x
  234. * @param x value to count bits of
  235. * @return the number of bits set to one in x
  236. */
  237. static av_always_inline av_const int av_popcount_c(uint32_t x)
  238. {
  239. x -= (x >> 1) & 0x55555555;
  240. x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
  241. x = (x + (x >> 4)) & 0x0F0F0F0F;
  242. x += x >> 8;
  243. return (x + (x >> 16)) & 0x3F;
  244. }
  245. /**
  246. * Count number of bits set to one in x
  247. * @param x value to count bits of
  248. * @return the number of bits set to one in x
  249. */
  250. static av_always_inline av_const int av_popcount64_c(uint64_t x)
  251. {
  252. return av_popcount((uint32_t)x) + av_popcount(x >> 32);
  253. }
  254. #define MKTAG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((unsigned)(d) << 24))
  255. #define MKBETAG(a,b,c,d) ((d) | ((c) << 8) | ((b) << 16) | ((unsigned)(a) << 24))
  256. /**
  257. * Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
  258. *
  259. * @param val Output value, must be an lvalue of type uint32_t.
  260. * @param GET_BYTE Expression reading one byte from the input.
  261. * Evaluated up to 7 times (4 for the currently
  262. * assigned Unicode range). With a memory buffer
  263. * input, this could be *ptr++.
  264. * @param ERROR Expression to be evaluated on invalid input,
  265. * typically a goto statement.
  266. */
  267. #define GET_UTF8(val, GET_BYTE, ERROR)\
  268. val= GET_BYTE;\
  269. {\
  270. uint32_t top = (val & 128) >> 1;\
  271. if ((val & 0xc0) == 0x80 || val >= 0xFE)\
  272. ERROR\
  273. while (val & top) {\
  274. int tmp= GET_BYTE - 128;\
  275. if(tmp>>6)\
  276. ERROR\
  277. val= (val<<6) + tmp;\
  278. top <<= 5;\
  279. }\
  280. val &= (top << 1) - 1;\
  281. }
  282. /**
  283. * Convert a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form.
  284. *
  285. * @param val Output value, must be an lvalue of type uint32_t.
  286. * @param GET_16BIT Expression returning two bytes of UTF-16 data converted
  287. * to native byte order. Evaluated one or two times.
  288. * @param ERROR Expression to be evaluated on invalid input,
  289. * typically a goto statement.
  290. */
  291. #define GET_UTF16(val, GET_16BIT, ERROR)\
  292. val = GET_16BIT;\
  293. {\
  294. unsigned int hi = val - 0xD800;\
  295. if (hi < 0x800) {\
  296. val = GET_16BIT - 0xDC00;\
  297. if (val > 0x3FFU || hi > 0x3FFU)\
  298. ERROR\
  299. val += (hi<<10) + 0x10000;\
  300. }\
  301. }\
  302. /**
  303. * @def PUT_UTF8(val, tmp, PUT_BYTE)
  304. * Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
  305. * @param val is an input-only argument and should be of type uint32_t. It holds
  306. * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If
  307. * val is given as a function it is executed only once.
  308. * @param tmp is a temporary variable and should be of type uint8_t. It
  309. * represents an intermediate value during conversion that is to be
  310. * output by PUT_BYTE.
  311. * @param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
  312. * It could be a function or a statement, and uses tmp as the input byte.
  313. * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
  314. * executed up to 4 times for values in the valid UTF-8 range and up to
  315. * 7 times in the general case, depending on the length of the converted
  316. * Unicode character.
  317. */
  318. #define PUT_UTF8(val, tmp, PUT_BYTE)\
  319. {\
  320. int bytes, shift;\
  321. uint32_t in = val;\
  322. if (in < 0x80) {\
  323. tmp = in;\
  324. PUT_BYTE\
  325. } else {\
  326. bytes = (av_log2(in) + 4) / 5;\
  327. shift = (bytes - 1) * 6;\
  328. tmp = (256 - (256 >> bytes)) | (in >> shift);\
  329. PUT_BYTE\
  330. while (shift >= 6) {\
  331. shift -= 6;\
  332. tmp = 0x80 | ((in >> shift) & 0x3f);\
  333. PUT_BYTE\
  334. }\
  335. }\
  336. }
  337. /**
  338. * @def PUT_UTF16(val, tmp, PUT_16BIT)
  339. * Convert a 32-bit Unicode character to its UTF-16 encoded form (2 or 4 bytes).
  340. * @param val is an input-only argument and should be of type uint32_t. It holds
  341. * a UCS-4 encoded Unicode character that is to be converted to UTF-16. If
  342. * val is given as a function it is executed only once.
  343. * @param tmp is a temporary variable and should be of type uint16_t. It
  344. * represents an intermediate value during conversion that is to be
  345. * output by PUT_16BIT.
  346. * @param PUT_16BIT writes the converted UTF-16 data to any proper destination
  347. * in desired endianness. It could be a function or a statement, and uses tmp
  348. * as the input byte. For example, PUT_BYTE could be "*output++ = tmp;"
  349. * PUT_BYTE will be executed 1 or 2 times depending on input character.
  350. */
  351. #define PUT_UTF16(val, tmp, PUT_16BIT)\
  352. {\
  353. uint32_t in = val;\
  354. if (in < 0x10000) {\
  355. tmp = in;\
  356. PUT_16BIT\
  357. } else {\
  358. tmp = 0xD800 | ((in - 0x10000) >> 10);\
  359. PUT_16BIT\
  360. tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\
  361. PUT_16BIT\
  362. }\
  363. }\
  364. #include "mem.h"
  365. #ifdef HAVE_AV_CONFIG_H
  366. # include "internal.h"
  367. #endif /* HAVE_AV_CONFIG_H */
  368. #endif /* AVUTIL_COMMON_H */
  369. /*
  370. * The following definitions are outside the multiple inclusion guard
  371. * to ensure they are immediately available in intmath.h.
  372. */
  373. #ifndef av_ceil_log2
  374. # define av_ceil_log2 av_ceil_log2_c
  375. #endif
  376. #ifndef av_clip
  377. # define av_clip av_clip_c
  378. #endif
  379. #ifndef av_clip64
  380. # define av_clip64 av_clip64_c
  381. #endif
  382. #ifndef av_clip_uint8
  383. # define av_clip_uint8 av_clip_uint8_c
  384. #endif
  385. #ifndef av_clip_int8
  386. # define av_clip_int8 av_clip_int8_c
  387. #endif
  388. #ifndef av_clip_uint16
  389. # define av_clip_uint16 av_clip_uint16_c
  390. #endif
  391. #ifndef av_clip_int16
  392. # define av_clip_int16 av_clip_int16_c
  393. #endif
  394. #ifndef av_clipl_int32
  395. # define av_clipl_int32 av_clipl_int32_c
  396. #endif
  397. #ifndef av_clip_uintp2
  398. # define av_clip_uintp2 av_clip_uintp2_c
  399. #endif
  400. #ifndef av_sat_add32
  401. # define av_sat_add32 av_sat_add32_c
  402. #endif
  403. #ifndef av_sat_dadd32
  404. # define av_sat_dadd32 av_sat_dadd32_c
  405. #endif
  406. #ifndef av_clipf
  407. # define av_clipf av_clipf_c
  408. #endif
  409. #ifndef av_clipd
  410. # define av_clipd av_clipd_c
  411. #endif
  412. #ifndef av_popcount
  413. # define av_popcount av_popcount_c
  414. #endif
  415. #ifndef av_popcount64
  416. # define av_popcount64 av_popcount64_c
  417. #endif