Path: blob/master/dep/ffmpeg/include/libavutil/common.h
4216 views
/*1* copyright (c) 2006 Michael Niedermayer <[email protected]>2*3* This file is part of FFmpeg.4*5* FFmpeg is free software; you can redistribute it and/or6* modify it under the terms of the GNU Lesser General Public7* License as published by the Free Software Foundation; either8* version 2.1 of the License, or (at your option) any later version.9*10* FFmpeg is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU13* Lesser General Public License for more details.14*15* You should have received a copy of the GNU Lesser General Public16* License along with FFmpeg; if not, write to the Free Software17* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA18*/1920/**21* @file22* common internal and external API header23*/2425#ifndef AVUTIL_COMMON_H26#define AVUTIL_COMMON_H2728#if defined(__cplusplus) && !defined(__STDC_CONSTANT_MACROS) && !defined(UINT64_C)29#error missing -D__STDC_CONSTANT_MACROS / #define __STDC_CONSTANT_MACROS30#endif3132#include <errno.h>33#include <inttypes.h>34#include <limits.h>35#include <math.h>36#include <stdint.h>37#include <stdio.h>38#include <stdlib.h>39#include <string.h>4041#include "attributes.h"42#include "error.h"43#include "macros.h"44#include "version.h"4546#ifdef HAVE_AV_CONFIG_H47# include "config.h"48# include "intmath.h"49# include "internal.h"50#else51# include "mem.h"52#endif /* HAVE_AV_CONFIG_H */5354//rounded division & shift55#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))56/* assume b>0 */57#define ROUNDED_DIV(a,b) (((a)>=0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))58/* Fast a/(1<<b) rounded toward +inf. Assume a>=0 and b>=0 */59#define AV_CEIL_RSHIFT(a,b) (!av_builtin_constant_p(b) ? -((-(a)) >> (b)) \60: ((a) + (1<<(b)) - 1) >> (b))61/* Backwards compat. */62#define FF_CEIL_RSHIFT AV_CEIL_RSHIFT6364#define FFUDIV(a,b) (((a)>0 ?(a):(a)-(b)+1) / (b))65#define FFUMOD(a,b) ((a)-(b)*FFUDIV(a,b))6667/**68* Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they69* are not representable as absolute values of their type. This is the same70* as with *abs()71* @see FFNABS()72*/73#define FFABS(a) ((a) >= 0 ? (a) : (-(a)))74#define FFSIGN(a) ((a) > 0 ? 1 : -1)7576/**77* Negative Absolute value.78* this works for all integers of all types.79* As with many macros, this evaluates its argument twice, it thus must not have80* a sideeffect, that is FFNABS(x++) has undefined behavior.81*/82#define FFNABS(a) ((a) <= 0 ? (a) : (-(a)))8384/**85* Unsigned Absolute value.86* This takes the absolute value of a signed int and returns it as a unsigned.87* This also works with INT_MIN which would otherwise not be representable88* As with many macros, this evaluates its argument twice.89*/90#define FFABSU(a) ((a) <= 0 ? -(unsigned)(a) : (unsigned)(a))91#define FFABS64U(a) ((a) <= 0 ? -(uint64_t)(a) : (uint64_t)(a))9293/* misc math functions */9495#ifndef av_ceil_log296# define av_ceil_log2 av_ceil_log2_c97#endif98#ifndef av_clip99# define av_clip av_clip_c100#endif101#ifndef av_clip64102# define av_clip64 av_clip64_c103#endif104#ifndef av_clip_uint8105# define av_clip_uint8 av_clip_uint8_c106#endif107#ifndef av_clip_int8108# define av_clip_int8 av_clip_int8_c109#endif110#ifndef av_clip_uint16111# define av_clip_uint16 av_clip_uint16_c112#endif113#ifndef av_clip_int16114# define av_clip_int16 av_clip_int16_c115#endif116#ifndef av_clipl_int32117# define av_clipl_int32 av_clipl_int32_c118#endif119#ifndef av_clip_intp2120# define av_clip_intp2 av_clip_intp2_c121#endif122#ifndef av_clip_uintp2123# define av_clip_uintp2 av_clip_uintp2_c124#endif125#ifndef av_sat_add32126# define av_sat_add32 av_sat_add32_c127#endif128#ifndef av_sat_dadd32129# define av_sat_dadd32 av_sat_dadd32_c130#endif131#ifndef av_sat_sub32132# define av_sat_sub32 av_sat_sub32_c133#endif134#ifndef av_sat_dsub32135# define av_sat_dsub32 av_sat_dsub32_c136#endif137#ifndef av_sat_add64138# define av_sat_add64 av_sat_add64_c139#endif140#ifndef av_sat_sub64141# define av_sat_sub64 av_sat_sub64_c142#endif143#ifndef av_clipf144# define av_clipf av_clipf_c145#endif146#ifndef av_clipd147# define av_clipd av_clipd_c148#endif149#ifndef av_zero_extend150# define av_zero_extend av_zero_extend_c151#endif152#ifndef av_popcount153# define av_popcount av_popcount_c154#endif155#ifndef av_popcount64156# define av_popcount64 av_popcount64_c157#endif158#ifndef av_parity159# define av_parity av_parity_c160#endif161162#ifndef av_log2163av_const int av_log2(unsigned v);164#endif165166#ifndef av_log2_16bit167av_const int av_log2_16bit(unsigned v);168#endif169170/**171* Clip a signed integer value into the amin-amax range.172* @param a value to clip173* @param amin minimum value of the clip range174* @param amax maximum value of the clip range175* @return clipped value176*/177static av_always_inline av_const int av_clip_c(int a, int amin, int amax)178{179#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2180if (amin > amax) abort();181#endif182if (a < amin) return amin;183else if (a > amax) return amax;184else return a;185}186187/**188* Clip a signed 64bit integer value into the amin-amax range.189* @param a value to clip190* @param amin minimum value of the clip range191* @param amax maximum value of the clip range192* @return clipped value193*/194static av_always_inline av_const int64_t av_clip64_c(int64_t a, int64_t amin, int64_t amax)195{196#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2197if (amin > amax) abort();198#endif199if (a < amin) return amin;200else if (a > amax) return amax;201else return a;202}203204/**205* Clip a signed integer value into the 0-255 range.206* @param a value to clip207* @return clipped value208*/209static av_always_inline av_const uint8_t av_clip_uint8_c(int a)210{211if (a&(~0xFF)) return (~a)>>31;212else return a;213}214215/**216* Clip a signed integer value into the -128,127 range.217* @param a value to clip218* @return clipped value219*/220static av_always_inline av_const int8_t av_clip_int8_c(int a)221{222if ((a+0x80U) & ~0xFF) return (a>>31) ^ 0x7F;223else return a;224}225226/**227* Clip a signed integer value into the 0-65535 range.228* @param a value to clip229* @return clipped value230*/231static av_always_inline av_const uint16_t av_clip_uint16_c(int a)232{233if (a&(~0xFFFF)) return (~a)>>31;234else return a;235}236237/**238* Clip a signed integer value into the -32768,32767 range.239* @param a value to clip240* @return clipped value241*/242static av_always_inline av_const int16_t av_clip_int16_c(int a)243{244if ((a+0x8000U) & ~0xFFFF) return (a>>31) ^ 0x7FFF;245else return a;246}247248/**249* Clip a signed 64-bit integer value into the -2147483648,2147483647 range.250* @param a value to clip251* @return clipped value252*/253static av_always_inline av_const int32_t av_clipl_int32_c(int64_t a)254{255if ((a+UINT64_C(0x80000000)) & ~UINT64_C(0xFFFFFFFF)) return (int32_t)((a>>63) ^ 0x7FFFFFFF);256else return (int32_t)a;257}258259/**260* Clip a signed integer into the -(2^p),(2^p-1) range.261* @param a value to clip262* @param p bit position to clip at263* @return clipped value264*/265static av_always_inline av_const int av_clip_intp2_c(int a, int p)266{267if (((unsigned)a + (1U << p)) & ~((2U << p) - 1))268return (a >> 31) ^ ((1 << p) - 1);269else270return a;271}272273/**274* Clip a signed integer to an unsigned power of two range.275* @param a value to clip276* @param p bit position to clip at277* @return clipped value278*/279static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)280{281if (a & ~((1U<<p) - 1)) return (~a) >> 31 & ((1U<<p) - 1);282else return a;283}284285/**286* Clear high bits from an unsigned integer starting with specific bit position287* @param a value to clip288* @param p bit position to clip at. Must be between 0 and 31.289* @return clipped value290*/291static av_always_inline av_const unsigned av_zero_extend_c(unsigned a, unsigned p)292{293#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2294if (p > 31) abort();295#endif296return a & ((1U << p) - 1);297}298299#if FF_API_MOD_UINTP2300#ifndef av_mod_uintp2301# define av_mod_uintp2 av_mod_uintp2_c302#endif303attribute_deprecated304static av_always_inline av_const unsigned av_mod_uintp2_c(unsigned a, unsigned p)305{306return av_zero_extend_c(a, p);307}308#endif309310/**311* Add two signed 32-bit values with saturation.312*313* @param a one value314* @param b another value315* @return sum with signed saturation316*/317static av_always_inline int av_sat_add32_c(int a, int b)318{319return av_clipl_int32((int64_t)a + b);320}321322/**323* Add a doubled value to another value with saturation at both stages.324*325* @param a first value326* @param b value doubled and added to a327* @return sum sat(a + sat(2*b)) with signed saturation328*/329static av_always_inline int av_sat_dadd32_c(int a, int b)330{331return av_sat_add32(a, av_sat_add32(b, b));332}333334/**335* Subtract two signed 32-bit values with saturation.336*337* @param a one value338* @param b another value339* @return difference with signed saturation340*/341static av_always_inline int av_sat_sub32_c(int a, int b)342{343return av_clipl_int32((int64_t)a - b);344}345346/**347* Subtract a doubled value from another value with saturation at both stages.348*349* @param a first value350* @param b value doubled and subtracted from a351* @return difference sat(a - sat(2*b)) with signed saturation352*/353static av_always_inline int av_sat_dsub32_c(int a, int b)354{355return av_sat_sub32(a, av_sat_add32(b, b));356}357358/**359* Add two signed 64-bit values with saturation.360*361* @param a one value362* @param b another value363* @return sum with signed saturation364*/365static av_always_inline int64_t av_sat_add64_c(int64_t a, int64_t b) {366#if (!defined(__INTEL_COMPILER) && AV_GCC_VERSION_AT_LEAST(5,1)) || AV_HAS_BUILTIN(__builtin_add_overflow)367int64_t tmp;368return !__builtin_add_overflow(a, b, &tmp) ? tmp : (tmp < 0 ? INT64_MAX : INT64_MIN);369#else370int64_t s = a+(uint64_t)b;371if ((int64_t)(a^b | ~s^b) >= 0)372return INT64_MAX ^ (b >> 63);373return s;374#endif375}376377/**378* Subtract two signed 64-bit values with saturation.379*380* @param a one value381* @param b another value382* @return difference with signed saturation383*/384static av_always_inline int64_t av_sat_sub64_c(int64_t a, int64_t b) {385#if (!defined(__INTEL_COMPILER) && AV_GCC_VERSION_AT_LEAST(5,1)) || AV_HAS_BUILTIN(__builtin_sub_overflow)386int64_t tmp;387return !__builtin_sub_overflow(a, b, &tmp) ? tmp : (tmp < 0 ? INT64_MAX : INT64_MIN);388#else389if (b <= 0 && a >= INT64_MAX + b)390return INT64_MAX;391if (b >= 0 && a <= INT64_MIN + b)392return INT64_MIN;393return a - b;394#endif395}396397/**398* Clip a float value into the amin-amax range.399* If a is nan or -inf amin will be returned.400* If a is +inf amax will be returned.401* @param a value to clip402* @param amin minimum value of the clip range403* @param amax maximum value of the clip range404* @return clipped value405*/406static av_always_inline av_const float av_clipf_c(float a, float amin, float amax)407{408#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2409if (amin > amax) abort();410#endif411return FFMIN(FFMAX(a, amin), amax);412}413414/**415* Clip a double value into the amin-amax range.416* If a is nan or -inf amin will be returned.417* If a is +inf amax will be returned.418* @param a value to clip419* @param amin minimum value of the clip range420* @param amax maximum value of the clip range421* @return clipped value422*/423static av_always_inline av_const double av_clipd_c(double a, double amin, double amax)424{425#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2426if (amin > amax) abort();427#endif428return FFMIN(FFMAX(a, amin), amax);429}430431/** Compute ceil(log2(x)).432* @param x value used to compute ceil(log2(x))433* @return computed ceiling of log2(x)434*/435static av_always_inline av_const int av_ceil_log2_c(int x)436{437return av_log2((x - 1U) << 1);438}439440/**441* Count number of bits set to one in x442* @param x value to count bits of443* @return the number of bits set to one in x444*/445static av_always_inline av_const int av_popcount_c(uint32_t x)446{447x -= (x >> 1) & 0x55555555;448x = (x & 0x33333333) + ((x >> 2) & 0x33333333);449x = (x + (x >> 4)) & 0x0F0F0F0F;450x += x >> 8;451return (x + (x >> 16)) & 0x3F;452}453454/**455* Count number of bits set to one in x456* @param x value to count bits of457* @return the number of bits set to one in x458*/459static av_always_inline av_const int av_popcount64_c(uint64_t x)460{461return av_popcount((uint32_t)x) + av_popcount((uint32_t)(x >> 32));462}463464static av_always_inline av_const int av_parity_c(uint32_t v)465{466return av_popcount(v) & 1;467}468469/**470* Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.471*472* @param val Output value, must be an lvalue of type uint32_t.473* @param GET_BYTE Expression reading one byte from the input.474* Evaluated up to 7 times (4 for the currently475* assigned Unicode range). With a memory buffer476* input, this could be *ptr++, or if you want to make sure477* that *ptr stops at the end of a NULL terminated string then478* *ptr ? *ptr++ : 0479* @param ERROR Expression to be evaluated on invalid input,480* typically a goto statement.481*482* @warning ERROR should not contain a loop control statement which483* could interact with the internal while loop, and should force an484* exit from the macro code (e.g. through a goto or a return) in order485* to prevent undefined results.486*/487#define GET_UTF8(val, GET_BYTE, ERROR)\488val= (GET_BYTE);\489{\490uint32_t top = (val & 128) >> 1;\491if ((val & 0xc0) == 0x80 || val >= 0xFE)\492{ERROR}\493while (val & top) {\494unsigned int tmp = (GET_BYTE) - 128;\495if(tmp>>6)\496{ERROR}\497val= (val<<6) + tmp;\498top <<= 5;\499}\500val &= (top << 1) - 1;\501}502503/**504* Convert a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form.505*506* @param val Output value, must be an lvalue of type uint32_t.507* @param GET_16BIT Expression returning two bytes of UTF-16 data converted508* to native byte order. Evaluated one or two times.509* @param ERROR Expression to be evaluated on invalid input,510* typically a goto statement.511*/512#define GET_UTF16(val, GET_16BIT, ERROR)\513val = (GET_16BIT);\514{\515unsigned int hi = val - 0xD800;\516if (hi < 0x800) {\517val = (GET_16BIT) - 0xDC00;\518if (val > 0x3FFU || hi > 0x3FFU)\519{ERROR}\520val += (hi<<10) + 0x10000;\521}\522}\523524/**525* @def PUT_UTF8(val, tmp, PUT_BYTE)526* Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).527* @param val is an input-only argument and should be of type uint32_t. It holds528* a UCS-4 encoded Unicode character that is to be converted to UTF-8. If529* val is given as a function it is executed only once.530* @param tmp is a temporary variable and should be of type uint8_t. It531* represents an intermediate value during conversion that is to be532* output by PUT_BYTE.533* @param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.534* It could be a function or a statement, and uses tmp as the input byte.535* For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be536* executed up to 4 times for values in the valid UTF-8 range and up to537* 7 times in the general case, depending on the length of the converted538* Unicode character.539*/540#define PUT_UTF8(val, tmp, PUT_BYTE)\541{\542int bytes, shift;\543uint32_t in = val;\544if (in < 0x80) {\545tmp = in;\546PUT_BYTE\547} else {\548bytes = (av_log2(in) + 4) / 5;\549shift = (bytes - 1) * 6;\550tmp = (256 - (256 >> bytes)) | (in >> shift);\551PUT_BYTE\552while (shift >= 6) {\553shift -= 6;\554tmp = 0x80 | ((in >> shift) & 0x3f);\555PUT_BYTE\556}\557}\558}559560/**561* @def PUT_UTF16(val, tmp, PUT_16BIT)562* Convert a 32-bit Unicode character to its UTF-16 encoded form (2 or 4 bytes).563* @param val is an input-only argument and should be of type uint32_t. It holds564* a UCS-4 encoded Unicode character that is to be converted to UTF-16. If565* val is given as a function it is executed only once.566* @param tmp is a temporary variable and should be of type uint16_t. It567* represents an intermediate value during conversion that is to be568* output by PUT_16BIT.569* @param PUT_16BIT writes the converted UTF-16 data to any proper destination570* in desired endianness. It could be a function or a statement, and uses tmp571* as the input byte. For example, PUT_BYTE could be "*output++ = tmp;"572* PUT_BYTE will be executed 1 or 2 times depending on input character.573*/574#define PUT_UTF16(val, tmp, PUT_16BIT)\575{\576uint32_t in = val;\577if (in < 0x10000) {\578tmp = in;\579PUT_16BIT\580} else {\581tmp = 0xD800 | ((in - 0x10000) >> 10);\582PUT_16BIT\583tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\584PUT_16BIT\585}\586}\587588#endif /* AVUTIL_COMMON_H */589590591