/*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 "macros.h"43#include "version.h"44#include "libavutil/avconfig.h"4546#if AV_HAVE_BIGENDIAN47# define AV_NE(be, le) (be)48#else49# define AV_NE(be, le) (le)50#endif5152//rounded division & shift53#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))54/* assume b>0 */55#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))56/* Fast a/(1<<b) rounded toward +inf. Assume a>=0 and b>=0 */57#define AV_CEIL_RSHIFT(a,b) (!av_builtin_constant_p(b) ? -((-(a)) >> (b)) \58: ((a) + (1<<(b)) - 1) >> (b))59/* Backwards compat. */60#define FF_CEIL_RSHIFT AV_CEIL_RSHIFT6162#define FFUDIV(a,b) (((a)>0 ?(a):(a)-(b)+1) / (b))63#define FFUMOD(a,b) ((a)-(b)*FFUDIV(a,b))6465/**66* Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they67* are not representable as absolute values of their type. This is the same68* as with *abs()69* @see FFNABS()70*/71#define FFABS(a) ((a) >= 0 ? (a) : (-(a)))72#define FFSIGN(a) ((a) > 0 ? 1 : -1)7374/**75* Negative Absolute value.76* this works for all integers of all types.77* As with many macros, this evaluates its argument twice, it thus must not have78* a sideeffect, that is FFNABS(x++) has undefined behavior.79*/80#define FFNABS(a) ((a) <= 0 ? (a) : (-(a)))8182/**83* Comparator.84* For two numerical expressions x and y, gives 1 if x > y, -1 if x < y, and 085* if x == y. This is useful for instance in a qsort comparator callback.86* Furthermore, compilers are able to optimize this to branchless code, and87* there is no risk of overflow with signed types.88* As with many macros, this evaluates its argument multiple times, it thus89* must not have a side-effect.90*/91#define FFDIFFSIGN(x,y) (((x)>(y)) - ((x)<(y)))9293#define FFMAX(a,b) ((a) > (b) ? (a) : (b))94#define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)95#define FFMIN(a,b) ((a) > (b) ? (b) : (a))96#define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)9798#define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)99#define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))100101/* misc math functions */102103#ifdef HAVE_AV_CONFIG_H104# include "config.h"105# include "intmath.h"106#endif107108/* Pull in unguarded fallback defines at the end of this file. */109#include "common.h"110111#ifndef av_log2112av_const int av_log2(unsigned v);113#endif114115#ifndef av_log2_16bit116av_const int av_log2_16bit(unsigned v);117#endif118119/**120* Clip a signed integer value into the amin-amax range.121* @param a value to clip122* @param amin minimum value of the clip range123* @param amax maximum value of the clip range124* @return clipped value125*/126static av_always_inline av_const int av_clip_c(int a, int amin, int amax)127{128#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2129if (amin > amax) abort();130#endif131if (a < amin) return amin;132else if (a > amax) return amax;133else return a;134}135136/**137* Clip a signed 64bit integer value into the amin-amax range.138* @param a value to clip139* @param amin minimum value of the clip range140* @param amax maximum value of the clip range141* @return clipped value142*/143static av_always_inline av_const int64_t av_clip64_c(int64_t a, int64_t amin, int64_t amax)144{145#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2146if (amin > amax) abort();147#endif148if (a < amin) return amin;149else if (a > amax) return amax;150else return a;151}152153/**154* Clip a signed integer value into the 0-255 range.155* @param a value to clip156* @return clipped value157*/158static av_always_inline av_const uint8_t av_clip_uint8_c(int a)159{160if (a&(~0xFF)) return (-a)>>31;161else return a;162}163164/**165* Clip a signed integer value into the -128,127 range.166* @param a value to clip167* @return clipped value168*/169static av_always_inline av_const int8_t av_clip_int8_c(int a)170{171if ((a+0x80U) & ~0xFF) return (a>>31) ^ 0x7F;172else return a;173}174175/**176* Clip a signed integer value into the 0-65535 range.177* @param a value to clip178* @return clipped value179*/180static av_always_inline av_const uint16_t av_clip_uint16_c(int a)181{182if (a&(~0xFFFF)) return (-a)>>31;183else return a;184}185186/**187* Clip a signed integer value into the -32768,32767 range.188* @param a value to clip189* @return clipped value190*/191static av_always_inline av_const int16_t av_clip_int16_c(int a)192{193if ((a+0x8000U) & ~0xFFFF) return (a>>31) ^ 0x7FFF;194else return a;195}196197/**198* Clip a signed 64-bit integer value into the -2147483648,2147483647 range.199* @param a value to clip200* @return clipped value201*/202static av_always_inline av_const int32_t av_clipl_int32_c(int64_t a)203{204if ((a+0x80000000u) & ~UINT64_C(0xFFFFFFFF)) return (int32_t)((a>>63) ^ 0x7FFFFFFF);205else return (int32_t)a;206}207208/**209* Clip a signed integer into the -(2^p),(2^p-1) range.210* @param a value to clip211* @param p bit position to clip at212* @return clipped value213*/214static av_always_inline av_const int av_clip_intp2_c(int a, int p)215{216if (((unsigned)a + (1 << p)) & ~((2 << p) - 1))217return (a >> 31) ^ ((1 << p) - 1);218else219return a;220}221222/**223* Clip a signed integer to an unsigned power of two range.224* @param a value to clip225* @param p bit position to clip at226* @return clipped value227*/228static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)229{230if (a & ~((1<<p) - 1)) return -a >> 31 & ((1<<p) - 1);231else return a;232}233234/**235* Clear high bits from an unsigned integer starting with specific bit position236* @param a value to clip237* @param p bit position to clip at238* @return clipped value239*/240static av_always_inline av_const unsigned av_mod_uintp2_c(unsigned a, unsigned p)241{242return a & ((1 << p) - 1);243}244245/**246* Add two signed 32-bit values with saturation.247*248* @param a one value249* @param b another value250* @return sum with signed saturation251*/252static av_always_inline int av_sat_add32_c(int a, int b)253{254return av_clipl_int32((int64_t)a + b);255}256257/**258* Add a doubled value to another value with saturation at both stages.259*260* @param a first value261* @param b value doubled and added to a262* @return sum with signed saturation263*/264static av_always_inline int av_sat_dadd32_c(int a, int b)265{266return av_sat_add32(a, av_sat_add32(b, b));267}268269/**270* Clip a float value into the amin-amax range.271* @param a value to clip272* @param amin minimum value of the clip range273* @param amax maximum value of the clip range274* @return clipped value275*/276static av_always_inline av_const float av_clipf_c(float a, float amin, float amax)277{278#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2279if (amin > amax) abort();280#endif281if (a < amin) return amin;282else if (a > amax) return amax;283else return a;284}285286/**287* Clip a double value into the amin-amax range.288* @param a value to clip289* @param amin minimum value of the clip range290* @param amax maximum value of the clip range291* @return clipped value292*/293static av_always_inline av_const double av_clipd_c(double a, double amin, double amax)294{295#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2296if (amin > amax) abort();297#endif298if (a < amin) return amin;299else if (a > amax) return amax;300else return a;301}302303/** Compute ceil(log2(x)).304* @param x value used to compute ceil(log2(x))305* @return computed ceiling of log2(x)306*/307static av_always_inline av_const int av_ceil_log2_c(int x)308{309return av_log2((x - 1) << 1);310}311312/**313* Count number of bits set to one in x314* @param x value to count bits of315* @return the number of bits set to one in x316*/317static av_always_inline av_const int av_popcount_c(uint32_t x)318{319x -= (x >> 1) & 0x55555555;320x = (x & 0x33333333) + ((x >> 2) & 0x33333333);321x = (x + (x >> 4)) & 0x0F0F0F0F;322x += x >> 8;323return (x + (x >> 16)) & 0x3F;324}325326/**327* Count number of bits set to one in x328* @param x value to count bits of329* @return the number of bits set to one in x330*/331static av_always_inline av_const int av_popcount64_c(uint64_t x)332{333return av_popcount((uint32_t)x) + av_popcount((uint32_t)(x >> 32));334}335336static av_always_inline av_const int av_parity_c(uint32_t v)337{338return av_popcount(v) & 1;339}340341#define MKTAG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((unsigned)(d) << 24))342#define MKBETAG(a,b,c,d) ((d) | ((c) << 8) | ((b) << 16) | ((unsigned)(a) << 24))343344/**345* Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.346*347* @param val Output value, must be an lvalue of type uint32_t.348* @param GET_BYTE Expression reading one byte from the input.349* Evaluated up to 7 times (4 for the currently350* assigned Unicode range). With a memory buffer351* input, this could be *ptr++.352* @param ERROR Expression to be evaluated on invalid input,353* typically a goto statement.354*355* @warning ERROR should not contain a loop control statement which356* could interact with the internal while loop, and should force an357* exit from the macro code (e.g. through a goto or a return) in order358* to prevent undefined results.359*/360#define GET_UTF8(val, GET_BYTE, ERROR)\361val= (GET_BYTE);\362{\363uint32_t top = (val & 128) >> 1;\364if ((val & 0xc0) == 0x80 || val >= 0xFE)\365ERROR\366while (val & top) {\367int tmp= (GET_BYTE) - 128;\368if(tmp>>6)\369ERROR\370val= (val<<6) + tmp;\371top <<= 5;\372}\373val &= (top << 1) - 1;\374}375376/**377* Convert a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form.378*379* @param val Output value, must be an lvalue of type uint32_t.380* @param GET_16BIT Expression returning two bytes of UTF-16 data converted381* to native byte order. Evaluated one or two times.382* @param ERROR Expression to be evaluated on invalid input,383* typically a goto statement.384*/385#define GET_UTF16(val, GET_16BIT, ERROR)\386val = GET_16BIT;\387{\388unsigned int hi = val - 0xD800;\389if (hi < 0x800) {\390val = GET_16BIT - 0xDC00;\391if (val > 0x3FFU || hi > 0x3FFU)\392ERROR\393val += (hi<<10) + 0x10000;\394}\395}\396397/**398* @def PUT_UTF8(val, tmp, PUT_BYTE)399* Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).400* @param val is an input-only argument and should be of type uint32_t. It holds401* a UCS-4 encoded Unicode character that is to be converted to UTF-8. If402* val is given as a function it is executed only once.403* @param tmp is a temporary variable and should be of type uint8_t. It404* represents an intermediate value during conversion that is to be405* output by PUT_BYTE.406* @param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.407* It could be a function or a statement, and uses tmp as the input byte.408* For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be409* executed up to 4 times for values in the valid UTF-8 range and up to410* 7 times in the general case, depending on the length of the converted411* Unicode character.412*/413#define PUT_UTF8(val, tmp, PUT_BYTE)\414{\415int bytes, shift;\416uint32_t in = val;\417if (in < 0x80) {\418tmp = in;\419PUT_BYTE\420} else {\421bytes = (av_log2(in) + 4) / 5;\422shift = (bytes - 1) * 6;\423tmp = (256 - (256 >> bytes)) | (in >> shift);\424PUT_BYTE\425while (shift >= 6) {\426shift -= 6;\427tmp = 0x80 | ((in >> shift) & 0x3f);\428PUT_BYTE\429}\430}\431}432433/**434* @def PUT_UTF16(val, tmp, PUT_16BIT)435* Convert a 32-bit Unicode character to its UTF-16 encoded form (2 or 4 bytes).436* @param val is an input-only argument and should be of type uint32_t. It holds437* a UCS-4 encoded Unicode character that is to be converted to UTF-16. If438* val is given as a function it is executed only once.439* @param tmp is a temporary variable and should be of type uint16_t. It440* represents an intermediate value during conversion that is to be441* output by PUT_16BIT.442* @param PUT_16BIT writes the converted UTF-16 data to any proper destination443* in desired endianness. It could be a function or a statement, and uses tmp444* as the input byte. For example, PUT_BYTE could be "*output++ = tmp;"445* PUT_BYTE will be executed 1 or 2 times depending on input character.446*/447#define PUT_UTF16(val, tmp, PUT_16BIT)\448{\449uint32_t in = val;\450if (in < 0x10000) {\451tmp = in;\452PUT_16BIT\453} else {\454tmp = 0xD800 | ((in - 0x10000) >> 10);\455PUT_16BIT\456tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\457PUT_16BIT\458}\459}\460461462463#include "mem.h"464465#ifdef HAVE_AV_CONFIG_H466# include "internal.h"467#endif /* HAVE_AV_CONFIG_H */468469#endif /* AVUTIL_COMMON_H */470471/*472* The following definitions are outside the multiple inclusion guard473* to ensure they are immediately available in intmath.h.474*/475476#ifndef av_ceil_log2477# define av_ceil_log2 av_ceil_log2_c478#endif479#ifndef av_clip480# define av_clip av_clip_c481#endif482#ifndef av_clip64483# define av_clip64 av_clip64_c484#endif485#ifndef av_clip_uint8486# define av_clip_uint8 av_clip_uint8_c487#endif488#ifndef av_clip_int8489# define av_clip_int8 av_clip_int8_c490#endif491#ifndef av_clip_uint16492# define av_clip_uint16 av_clip_uint16_c493#endif494#ifndef av_clip_int16495# define av_clip_int16 av_clip_int16_c496#endif497#ifndef av_clipl_int32498# define av_clipl_int32 av_clipl_int32_c499#endif500#ifndef av_clip_intp2501# define av_clip_intp2 av_clip_intp2_c502#endif503#ifndef av_clip_uintp2504# define av_clip_uintp2 av_clip_uintp2_c505#endif506#ifndef av_mod_uintp2507# define av_mod_uintp2 av_mod_uintp2_c508#endif509#ifndef av_sat_add32510# define av_sat_add32 av_sat_add32_c511#endif512#ifndef av_sat_dadd32513# define av_sat_dadd32 av_sat_dadd32_c514#endif515#ifndef av_clipf516# define av_clipf av_clipf_c517#endif518#ifndef av_clipd519# define av_clipd av_clipd_c520#endif521#ifndef av_popcount522# define av_popcount av_popcount_c523#endif524#ifndef av_popcount64525# define av_popcount64 av_popcount64_c526#endif527#ifndef av_parity528# define av_parity av_parity_c529#endif530531532