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.

579 lines
17KB

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