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.

516 lines
15KB

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