summaryrefslogtreecommitdiff
path: root/libavutil/common.h
diff options
context:
space:
mode:
Diffstat (limited to 'libavutil/common.h')
-rw-r--r--libavutil/common.h139
1 files changed, 121 insertions, 18 deletions
diff --git a/libavutil/common.h b/libavutil/common.h
index d2ddcba9ed..8142b31fdb 100644
--- a/libavutil/common.h
+++ b/libavutil/common.h
@@ -1,20 +1,20 @@
/*
* copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
*
- * This file is part of Libav.
+ * This file is part of FFmpeg.
*
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@@ -26,6 +26,10 @@
#ifndef AVUTIL_COMMON_H
#define AVUTIL_COMMON_H
+#if defined(__cplusplus) && !defined(__STDC_CONSTANT_MACROS) && !defined(UINT64_C)
+#error missing -D__STDC_CONSTANT_MACROS / #define __STDC_CONSTANT_MACROS
+#endif
+
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
@@ -50,17 +54,43 @@
#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
/* assume b>0 */
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
+/* Fast a/(1<<b) rounded toward +inf. Assume a>=0 and b>=0 */
+#define AV_CEIL_RSHIFT(a,b) (!av_builtin_constant_p(b) ? -((-(a)) >> (b)) \
+ : ((a) + (1<<(b)) - 1) >> (b))
+/* Backwards compat. */
+#define FF_CEIL_RSHIFT AV_CEIL_RSHIFT
+
+#define FFUDIV(a,b) (((a)>0 ?(a):(a)-(b)+1) / (b))
+#define FFUMOD(a,b) ((a)-(b)*FFUDIV(a,b))
/**
- * Fast a / (1 << b) rounded toward +inf, assuming a >= 0 and b >= 0.
+ * Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they
+ * are not representable as absolute values of their type. This is the same
+ * as with *abs()
+ * @see FFNABS()
*/
-#define AV_CEIL_RSHIFT(a, b) \
- (av_builtin_constant_p(b) ? ((a) + (1 << (b)) - 1) >> (b) \
- : -((-(a)) >> (b)))
-
#define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
#define FFSIGN(a) ((a) > 0 ? 1 : -1)
+/**
+ * Negative Absolute value.
+ * this works for all integers of all types.
+ * As with many macros, this evaluates its argument twice, it thus must not have
+ * a sideeffect, that is FFNABS(x++) has undefined behavior.
+ */
+#define FFNABS(a) ((a) <= 0 ? (a) : (-(a)))
+
+/**
+ * Comparator.
+ * For two numerical expressions x and y, gives 1 if x > y, -1 if x < y, and 0
+ * if x == y. This is useful for instance in a qsort comparator callback.
+ * Furthermore, compilers are able to optimize this to branchless code, and
+ * there is no risk of overflow with signed types.
+ * As with many macros, this evaluates its argument multiple times, it thus
+ * must not have a side-effect.
+ */
+#define FFDIFFSIGN(x,y) (((x)>(y)) - ((x)<(y)))
+
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
#define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
@@ -96,6 +126,26 @@ av_const int av_log2_16bit(unsigned v);
*/
static av_always_inline av_const int av_clip_c(int a, int amin, int amax)
{
+#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
+ if (amin > amax) abort();
+#endif
+ if (a < amin) return amin;
+ else if (a > amax) return amax;
+ else return a;
+}
+
+/**
+ * Clip a signed 64bit integer value into the amin-amax range.
+ * @param a value to clip
+ * @param amin minimum value of the clip range
+ * @param amax maximum value of the clip range
+ * @return clipped value
+ */
+static av_always_inline av_const int64_t av_clip64_c(int64_t a, int64_t amin, int64_t amax)
+{
+#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
+ if (amin > amax) abort();
+#endif
if (a < amin) return amin;
else if (a > amax) return amax;
else return a;
@@ -119,7 +169,7 @@ static av_always_inline av_const uint8_t av_clip_uint8_c(int a)
*/
static av_always_inline av_const int8_t av_clip_int8_c(int a)
{
- if ((a+0x80) & ~0xFF) return (a>>31) ^ 0x7F;
+ if ((a+0x80U) & ~0xFF) return (a>>31) ^ 0x7F;
else return a;
}
@@ -141,7 +191,7 @@ static av_always_inline av_const uint16_t av_clip_uint16_c(int a)
*/
static av_always_inline av_const int16_t av_clip_int16_c(int a)
{
- if ((a+0x8000) & ~0xFFFF) return (a>>31) ^ 0x7FFF;
+ if ((a+0x8000U) & ~0xFFFF) return (a>>31) ^ 0x7FFF;
else return a;
}
@@ -152,8 +202,8 @@ static av_always_inline av_const int16_t av_clip_int16_c(int a)
*/
static av_always_inline av_const int32_t av_clipl_int32_c(int64_t a)
{
- if ((a+0x80000000u) & ~UINT64_C(0xFFFFFFFF)) return (a>>63) ^ 0x7FFFFFFF;
- else return a;
+ if ((a+0x80000000u) & ~UINT64_C(0xFFFFFFFF)) return (int32_t)((a>>63) ^ 0x7FFFFFFF);
+ else return (int32_t)a;
}
/**
@@ -164,7 +214,7 @@ static av_always_inline av_const int32_t av_clipl_int32_c(int64_t a)
*/
static av_always_inline av_const int av_clip_intp2_c(int a, int p)
{
- if ((a + (1 << p)) & ~((1 << (p + 1)) - 1))
+ if (((unsigned)a + (1 << p)) & ~((2 << p) - 1))
return (a >> 31) ^ ((1 << p) - 1);
else
return a;
@@ -183,6 +233,17 @@ static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)
}
/**
+ * Clear high bits from an unsigned integer starting with specific bit position
+ * @param a value to clip
+ * @param p bit position to clip at
+ * @return clipped value
+ */
+static av_always_inline av_const unsigned av_mod_uintp2_c(unsigned a, unsigned p)
+{
+ return a & ((1 << p) - 1);
+}
+
+/**
* Add two signed 32-bit values with saturation.
*
* @param a one value
@@ -215,6 +276,26 @@ static av_always_inline int av_sat_dadd32_c(int a, int b)
*/
static av_always_inline av_const float av_clipf_c(float a, float amin, float amax)
{
+#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
+ if (amin > amax) abort();
+#endif
+ if (a < amin) return amin;
+ else if (a > amax) return amax;
+ else return a;
+}
+
+/**
+ * Clip a double value into the amin-amax range.
+ * @param a value to clip
+ * @param amin minimum value of the clip range
+ * @param amax maximum value of the clip range
+ * @return clipped value
+ */
+static av_always_inline av_const double av_clipd_c(double a, double amin, double amax)
+{
+#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
+ if (amin > amax) abort();
+#endif
if (a < amin) return amin;
else if (a > amax) return amax;
else return a;
@@ -250,7 +331,12 @@ static av_always_inline av_const int av_popcount_c(uint32_t x)
*/
static av_always_inline av_const int av_popcount64_c(uint64_t x)
{
- return av_popcount(x) + av_popcount(x >> 32);
+ return av_popcount((uint32_t)x) + av_popcount((uint32_t)(x >> 32));
+}
+
+static av_always_inline av_const int av_parity_c(uint32_t v)
+{
+ return av_popcount(v) & 1;
}
#define MKTAG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((unsigned)(d) << 24))
@@ -266,15 +352,20 @@ static av_always_inline av_const int av_popcount64_c(uint64_t x)
* input, this could be *ptr++.
* @param ERROR Expression to be evaluated on invalid input,
* typically a goto statement.
+ *
+ * @warning ERROR should not contain a loop control statement which
+ * could interact with the internal while loop, and should force an
+ * exit from the macro code (e.g. through a goto or a return) in order
+ * to prevent undefined results.
*/
#define GET_UTF8(val, GET_BYTE, ERROR)\
- val= GET_BYTE;\
+ val= (GET_BYTE);\
{\
uint32_t top = (val & 128) >> 1;\
- if ((val & 0xc0) == 0x80)\
+ if ((val & 0xc0) == 0x80 || val >= 0xFE)\
ERROR\
while (val & top) {\
- int tmp= GET_BYTE - 128;\
+ int tmp= (GET_BYTE) - 128;\
if(tmp>>6)\
ERROR\
val= (val<<6) + tmp;\
@@ -389,6 +480,9 @@ static av_always_inline av_const int av_popcount64_c(uint64_t x)
#ifndef av_clip
# define av_clip av_clip_c
#endif
+#ifndef av_clip64
+# define av_clip64 av_clip64_c
+#endif
#ifndef av_clip_uint8
# define av_clip_uint8 av_clip_uint8_c
#endif
@@ -410,6 +504,9 @@ static av_always_inline av_const int av_popcount64_c(uint64_t x)
#ifndef av_clip_uintp2
# define av_clip_uintp2 av_clip_uintp2_c
#endif
+#ifndef av_mod_uintp2
+# define av_mod_uintp2 av_mod_uintp2_c
+#endif
#ifndef av_sat_add32
# define av_sat_add32 av_sat_add32_c
#endif
@@ -419,9 +516,15 @@ static av_always_inline av_const int av_popcount64_c(uint64_t x)
#ifndef av_clipf
# define av_clipf av_clipf_c
#endif
+#ifndef av_clipd
+# define av_clipd av_clipd_c
+#endif
#ifndef av_popcount
# define av_popcount av_popcount_c
#endif
#ifndef av_popcount64
# define av_popcount64 av_popcount64_c
#endif
+#ifndef av_parity
+# define av_parity av_parity_c
+#endif