libavutil/common.h
04d7f601
 /*
  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
04d7f601
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
04d7f601
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
04d7f601
  * 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
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
04d7f601
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
983e3246
 /**
bad5537e
  * @file libavutil/common.h
7ce68923
  * common internal and external API header
983e3246
  */
 
98790382
 #ifndef AVUTIL_COMMON_H
 #define AVUTIL_COMMON_H
de6d9b64
 
8076c473
 #include <ctype.h>
 #include <errno.h>
1845bf1f
 #include <inttypes.h>
8076c473
 #include <limits.h>
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
849f1035
 
03bc0f37
 #ifdef __GNUC__
 #    define AV_GCC_VERSION_AT_LEAST(x,y) (__GNUC__ > x || __GNUC__ == x && __GNUC_MINOR__ >= y)
 #else
 #    define AV_GCC_VERSION_AT_LEAST(x,y) 0
 #endif
52476c1b
 
849f1035
 #ifndef av_always_inline
52476c1b
 #if AV_GCC_VERSION_AT_LEAST(3,1)
849f1035
 #    define av_always_inline __attribute__((always_inline)) inline
 #else
 #    define av_always_inline inline
b9c684a2
 #endif
 #endif
 
 #ifndef av_noinline
52476c1b
 #if AV_GCC_VERSION_AT_LEAST(3,1)
b9c684a2
 #    define av_noinline __attribute__((noinline))
 #else
410bf273
 #    define av_noinline
849f1035
 #endif
 #endif
 
85074d3c
 #ifndef av_pure
52476c1b
 #if AV_GCC_VERSION_AT_LEAST(3,1)
85074d3c
 #    define av_pure __attribute__((pure))
 #else
 #    define av_pure
 #endif
 #endif
 
 #ifndef av_const
52476c1b
 #if AV_GCC_VERSION_AT_LEAST(2,6)
85074d3c
 #    define av_const __attribute__((const))
 #else
 #    define av_const
 #endif
 #endif
 
98a6fff9
 #ifndef av_cold
af4c0bcb
 #if (!defined(__ICC) || __ICC > 1100) && AV_GCC_VERSION_AT_LEAST(4,3)
98a6fff9
 #    define av_cold __attribute__((cold))
 #else
 #    define av_cold
 #endif
 #endif
 
3b49f690
 #ifndef av_flatten
b7d3a8c1
 #if AV_GCC_VERSION_AT_LEAST(4,1)
3b49f690
 #    define av_flatten __attribute__((flatten))
 #else
 #    define av_flatten
 #endif
 #endif
 
955ab9a4
 #ifndef attribute_deprecated
52476c1b
 #if AV_GCC_VERSION_AT_LEAST(3,1)
955ab9a4
 #    define attribute_deprecated __attribute__((deprecated))
 #else
 #    define attribute_deprecated
 #endif
 #endif
 
154e30f6
 #ifndef av_unused
 #if defined(__GNUC__)
 #    define av_unused __attribute__((unused))
 #else
 #    define av_unused
 #endif
 #endif
 
51066987
 #ifndef av_uninit
0dd89666
 #if defined(__GNUC__) && !defined(__ICC)
51066987
 #    define av_uninit(x) x=x
 #else
 #    define av_uninit(x) x
 #endif
 #endif
 
89c9ff50
 //rounded division & shift
10f3005f
 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
d7e9533a
 /* assume b>0 */
 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
c26abfa5
 #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
02305ff3
 #define FFSIGN(a) ((a) > 0 ? 1 : -1)
75460b0c
 
b8a78f41
 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
159ef4b0
 #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
b8a78f41
 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
b842ecbe
 #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
d7e9533a
 
1345f4ed
 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
ac809e81
 #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
67eca72d
 
de6d9b64
 /* misc math functions */
a77caa4d
 extern const uint8_t ff_log2_tab[256];
de6d9b64
 
85074d3c
 static inline av_const int av_log2(unsigned int v)
de6d9b64
 {
89ef2c29
     int n = 0;
de6d9b64
     if (v & 0xffff0000) {
         v >>= 16;
         n += 16;
     }
     if (v & 0xff00) {
         v >>= 8;
         n += 8;
     }
c81f0349
     n += ff_log2_tab[v];
 
     return n;
 }
 
85074d3c
 static inline av_const int av_log2_16bit(unsigned int v)
c81f0349
 {
89ef2c29
     int n = 0;
c81f0349
     if (v & 0xff00) {
         v >>= 8;
         n += 8;
de6d9b64
     }
c81f0349
     n += ff_log2_tab[v];
 
de6d9b64
     return n;
 }
 
77177335
 /**
89c9ff50
  * Clips a signed integer value into the amin-amax range.
77177335
  * @param a value to clip
  * @param amin minimum value of the clip range
  * @param amax maximum value of the clip range
c6c36725
  * @return clipped value
77177335
  */
85074d3c
 static inline av_const int av_clip(int a, int amin, int amax)
91029be7
 {
27af15dc
     if      (a < amin) return amin;
18769c0a
     else if (a > amax) return amax;
     else               return a;
91029be7
 }
 
77177335
 /**
89c9ff50
  * Clips a signed integer value into the 0-255 range.
77177335
  * @param a value to clip
c6c36725
  * @return clipped value
77177335
  */
85074d3c
 static inline av_const uint8_t av_clip_uint8(int a)
3ebc7e04
 {
     if (a&(~255)) return (-a)>>31;
     else          return a;
 }
 
ddb8ebe7
 /**
89c9ff50
  * Clips a signed integer value into the -32768,32767 range.
ddb8ebe7
  * @param a value to clip
  * @return clipped value
  */
85074d3c
 static inline av_const int16_t av_clip_int16(int a)
ddb8ebe7
 {
     if ((a+32768) & ~65535) return (a>>31) ^ 32767;
     else                    return a;
 }
 
4138ad96
 /**
89c9ff50
  * Clips a float value into the amin-amax range.
4138ad96
  * @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 inline av_const float av_clipf(float a, float amin, float amax)
 {
     if      (a < amin) return amin;
     else if (a > amax) return amax;
     else               return a;
 }
 
e8750b00
 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
 
d73427e3
 /*!
  * \def GET_UTF8(val, GET_BYTE, ERROR)
89c9ff50
  * Converts a UTF-8 character (up to 4 bytes long) to its 32-bit UCS-4 encoded form
d73427e3
  * \param val is the output and should be of type uint32_t. It holds the converted
90b5b51e
  * UCS-4 character and should be a left value.
  * \param GET_BYTE gets UTF-8 encoded bytes from any proper source. It can be
d73427e3
  * a function or a statement whose return value or evaluated value is of type
90b5b51e
  * uint8_t. It will be executed up to 4 times for values in the valid UTF-8 range,
40a08c7e
  * and up to 7 times in the general case.
90b5b51e
  * \param ERROR action that should be taken when an invalid UTF-8 byte is returned
d73427e3
  * from GET_BYTE. It should be a statement that jumps out of the macro,
  * like exit(), goto, return, break, or continue.
  */
9d82b0dd
 #define GET_UTF8(val, GET_BYTE, ERROR)\
     val= GET_BYTE;\
     {\
         int ones= 7 - av_log2(val ^ 255);\
         if(ones==1)\
             ERROR\
         val&= 127>>ones;\
         while(--ones > 0){\
             int tmp= GET_BYTE - 128;\
             if(tmp>>6)\
                 ERROR\
             val= (val<<6) + tmp;\
         }\
     }
2ad1516a
 
0e8c148b
 /*!
34d33769
  * \def PUT_UTF8(val, tmp, PUT_BYTE)
89c9ff50
  * Converts a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
  * \param val is an input-only argument and should be of type uint32_t. It holds
  * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If
  * val is given as a function it is executed only once.
0e8c148b
  * \param tmp is a temporary variable and should be of type uint8_t. It
  * represents an intermediate value during conversion that is to be
89c9ff50
  * output by PUT_BYTE.
90b5b51e
  * \param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
0e8c148b
  * It could be a function or a statement, and uses tmp as the input byte.
  * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
90b5b51e
  * executed up to 4 times for values in the valid UTF-8 range and up to
40a08c7e
  * 7 times in the general case, depending on the length of the converted
89c9ff50
  * Unicode character.
0e8c148b
  */
360932f7
 #define PUT_UTF8(val, tmp, PUT_BYTE)\
     {\
         int bytes, shift;\
         uint32_t in = val;\
         if (in < 0x80) {\
             tmp = in;\
             PUT_BYTE\
         } else {\
             bytes = (av_log2(in) + 4) / 5;\
             shift = (bytes - 1) * 6;\
             tmp = (256 - (256 >> bytes)) | (in >> shift);\
             PUT_BYTE\
             while (shift >= 6) {\
                 shift -= 6;\
                 tmp = 0x80 | ((in >> shift) & 0x3f);\
                 PUT_BYTE\
             }\
         }\
     }
 
8076c473
 #include "mem.h"
 
6b8b7258
 #ifdef HAVE_AV_CONFIG_H
8076c473
 #    include "config.h"
6b8b7258
 #    include "internal.h"
 #endif /* HAVE_AV_CONFIG_H */
 
98790382
 #endif /* AVUTIL_COMMON_H */