]> git.sesse.net Git - ffmpeg/blobdiff - libavutil/common.h
vaapi_h264: Add support for SEI messages
[ffmpeg] / libavutil / common.h
index c99d858472bbdebbdc6e4ed310dfe78783f66c79..d2ddcba9ede73d1e0d9dc4190958ac893c52959b 100644 (file)
 #ifndef AVUTIL_COMMON_H
 #define AVUTIL_COMMON_H
 
-#include <ctype.h>
 #include <errno.h>
 #include <inttypes.h>
 #include <limits.h>
 #include <math.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+
 #include "attributes.h"
+#include "macros.h"
+#include "version.h"
 #include "libavutil/avconfig.h"
 
 #if AV_HAVE_BIGENDIAN
 #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, assuming a >= 0 and b >= 0.
+ */
+#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)
 
 
 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
 #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
-#define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1))
 
 /* misc math functions */
-extern const uint8_t ff_log2_tab[256];
-
-extern const uint8_t av_reverse[256];
-
-static av_always_inline av_const int av_log2_c(unsigned int v)
-{
-    int n = 0;
-    if (v & 0xffff0000) {
-        v >>= 16;
-        n += 16;
-    }
-    if (v & 0xff00) {
-        v >>= 8;
-        n += 8;
-    }
-    n += ff_log2_tab[v];
-
-    return n;
-}
-
-static av_always_inline av_const int av_log2_16bit_c(unsigned int v)
-{
-    int n = 0;
-    if (v & 0xff00) {
-        v >>= 8;
-        n += 8;
-    }
-    n += ff_log2_tab[v];
-
-    return n;
-}
 
 #ifdef HAVE_AV_CONFIG_H
 #   include "config.h"
@@ -100,6 +79,14 @@ static av_always_inline av_const int av_log2_16bit_c(unsigned int v)
 /* Pull in unguarded fallback defines at the end of this file. */
 #include "common.h"
 
+#ifndef av_log2
+av_const int av_log2(unsigned v);
+#endif
+
+#ifndef av_log2_16bit
+av_const int av_log2_16bit(unsigned v);
+#endif
+
 /**
  * Clip a signed integer value into the amin-amax range.
  * @param a value to clip
@@ -169,6 +156,20 @@ static av_always_inline av_const int32_t av_clipl_int32_c(int64_t a)
     else                                         return a;
 }
 
+/**
+ * Clip a signed integer into the -(2^p),(2^p-1) range.
+ * @param  a value to clip
+ * @param  p bit position to clip at
+ * @return clipped value
+ */
+static av_always_inline av_const int av_clip_intp2_c(int a, int p)
+{
+    if ((a + (1 << p)) & ~((1 << (p + 1)) - 1))
+        return (a >> 31) ^ ((1 << p) - 1);
+    else
+        return a;
+}
+
 /**
  * Clip a signed integer to an unsigned power of two range.
  * @param  a value to clip
@@ -181,6 +182,30 @@ static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)
     else                   return  a;
 }
 
+/**
+ * Add two signed 32-bit values with saturation.
+ *
+ * @param  a one value
+ * @param  b another value
+ * @return sum with signed saturation
+ */
+static av_always_inline int av_sat_add32_c(int a, int b)
+{
+    return av_clipl_int32((int64_t)a + b);
+}
+
+/**
+ * Add a doubled value to another value with saturation at both stages.
+ *
+ * @param  a first value
+ * @param  b value doubled and added to a
+ * @return sum with signed saturation
+ */
+static av_always_inline int av_sat_dadd32_c(int a, int b)
+{
+    return av_sat_add32(a, av_sat_add32(b, b));
+}
+
 /**
  * Clip a float value into the amin-amax range.
  * @param a value to clip
@@ -245,16 +270,17 @@ static av_always_inline av_const int av_popcount64_c(uint64_t x)
 #define GET_UTF8(val, GET_BYTE, ERROR)\
     val= GET_BYTE;\
     {\
-        int ones= 7 - av_log2(val ^ 255);\
-        if(ones==1)\
+        uint32_t top = (val & 128) >> 1;\
+        if ((val & 0xc0) == 0x80)\
             ERROR\
-        val&= 127>>ones;\
-        while(--ones > 0){\
+        while (val & top) {\
             int tmp= GET_BYTE - 128;\
             if(tmp>>6)\
                 ERROR\
             val= (val<<6) + tmp;\
+            top <<= 5;\
         }\
+        val &= (top << 1) - 1;\
     }
 
 /**
@@ -357,12 +383,6 @@ static av_always_inline av_const int av_popcount64_c(uint64_t x)
  * to ensure they are immediately available in intmath.h.
  */
 
-#ifndef av_log2
-#   define av_log2       av_log2_c
-#endif
-#ifndef av_log2_16bit
-#   define av_log2_16bit av_log2_16bit_c
-#endif
 #ifndef av_ceil_log2
 #   define av_ceil_log2     av_ceil_log2_c
 #endif
@@ -384,9 +404,18 @@ static av_always_inline av_const int av_popcount64_c(uint64_t x)
 #ifndef av_clipl_int32
 #   define av_clipl_int32   av_clipl_int32_c
 #endif
+#ifndef av_clip_intp2
+#   define av_clip_intp2    av_clip_intp2_c
+#endif
 #ifndef av_clip_uintp2
 #   define av_clip_uintp2   av_clip_uintp2_c
 #endif
+#ifndef av_sat_add32
+#   define av_sat_add32     av_sat_add32_c
+#endif
+#ifndef av_sat_dadd32
+#   define av_sat_dadd32    av_sat_dadd32_c
+#endif
 #ifndef av_clipf
 #   define av_clipf         av_clipf_c
 #endif