Path: blob/a-new-beginning/libavutil.xcframework/macos-arm64/libavutil.framework/Versions/A/Headers/common.h
2 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 "macros.h"4344//rounded division & shift45#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))46/* assume b>0 */47#define ROUNDED_DIV(a,b) (((a)>=0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))48/* Fast a/(1<<b) rounded toward +inf. Assume a>=0 and b>=0 */49#define AV_CEIL_RSHIFT(a,b) (!av_builtin_constant_p(b) ? -((-(a)) >> (b)) \50: ((a) + (1<<(b)) - 1) >> (b))51/* Backwards compat. */52#define FF_CEIL_RSHIFT AV_CEIL_RSHIFT5354#define FFUDIV(a,b) (((a)>0 ?(a):(a)-(b)+1) / (b))55#define FFUMOD(a,b) ((a)-(b)*FFUDIV(a,b))5657/**58* Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they59* are not representable as absolute values of their type. This is the same60* as with *abs()61* @see FFNABS()62*/63#define FFABS(a) ((a) >= 0 ? (a) : (-(a)))64#define FFSIGN(a) ((a) > 0 ? 1 : -1)6566/**67* Negative Absolute value.68* this works for all integers of all types.69* As with many macros, this evaluates its argument twice, it thus must not have70* a sideeffect, that is FFNABS(x++) has undefined behavior.71*/72#define FFNABS(a) ((a) <= 0 ? (a) : (-(a)))7374/**75* Unsigned Absolute value.76* This takes the absolute value of a signed int and returns it as a unsigned.77* This also works with INT_MIN which would otherwise not be representable78* As with many macros, this evaluates its argument twice.79*/80#define FFABSU(a) ((a) <= 0 ? -(unsigned)(a) : (unsigned)(a))81#define FFABS64U(a) ((a) <= 0 ? -(uint64_t)(a) : (uint64_t)(a))8283/* misc math functions */8485#ifdef HAVE_AV_CONFIG_H86# include "config.h"87# include "intmath.h"88#endif8990#ifndef av_ceil_log291# define av_ceil_log2 av_ceil_log2_c92#endif93#ifndef av_clip94# define av_clip av_clip_c95#endif96#ifndef av_clip6497# define av_clip64 av_clip64_c98#endif99#ifndef av_clip_uint8100# define av_clip_uint8 av_clip_uint8_c101#endif102#ifndef av_clip_int8103# define av_clip_int8 av_clip_int8_c104#endif105#ifndef av_clip_uint16106# define av_clip_uint16 av_clip_uint16_c107#endif108#ifndef av_clip_int16109# define av_clip_int16 av_clip_int16_c110#endif111#ifndef av_clipl_int32112# define av_clipl_int32 av_clipl_int32_c113#endif114#ifndef av_clip_intp2115# define av_clip_intp2 av_clip_intp2_c116#endif117#ifndef av_clip_uintp2118# define av_clip_uintp2 av_clip_uintp2_c119#endif120#ifndef av_mod_uintp2121# define av_mod_uintp2 av_mod_uintp2_c122#endif123#ifndef av_sat_add32124# define av_sat_add32 av_sat_add32_c125#endif126#ifndef av_sat_dadd32127# define av_sat_dadd32 av_sat_dadd32_c128#endif129#ifndef av_sat_sub32130# define av_sat_sub32 av_sat_sub32_c131#endif132#ifndef av_sat_dsub32133# define av_sat_dsub32 av_sat_dsub32_c134#endif135#ifndef av_sat_add64136# define av_sat_add64 av_sat_add64_c137#endif138#ifndef av_sat_sub64139# define av_sat_sub64 av_sat_sub64_c140#endif141#ifndef av_clipf142# define av_clipf av_clipf_c143#endif144#ifndef av_clipd145# define av_clipd av_clipd_c146#endif147#ifndef av_popcount148# define av_popcount av_popcount_c149#endif150#ifndef av_popcount64151# define av_popcount64 av_popcount64_c152#endif153#ifndef av_parity154# define av_parity av_parity_c155#endif156157#ifndef av_log2158av_const int av_log2(unsigned v);159#endif160161#ifndef av_log2_16bit162av_const int av_log2_16bit(unsigned v);163#endif164165/**166* Clip a signed integer value into the amin-amax range.167* @param a value to clip168* @param amin minimum value of the clip range169* @param amax maximum value of the clip range170* @return clipped value171*/172static av_always_inline av_const int av_clip_c(int a, int amin, int amax)173{174#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2175if (amin > amax) abort();176#endif177if (a < amin) return amin;178else if (a > amax) return amax;179else return a;180}181182/**183* Clip a signed 64bit integer value into the amin-amax range.184* @param a value to clip185* @param amin minimum value of the clip range186* @param amax maximum value of the clip range187* @return clipped value188*/189static av_always_inline av_const int64_t av_clip64_c(int64_t a, int64_t amin, int64_t amax)190{191#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2192if (amin > amax) abort();193#endif194if (a < amin) return amin;195else if (a > amax) return amax;196else return a;197}198199/**200* Clip a signed integer value into the 0-255 range.201* @param a value to clip202* @return clipped value203*/204static av_always_inline av_const uint8_t av_clip_uint8_c(int a)205{206if (a&(~0xFF)) return (~a)>>31;207else return a;208}209210/**211* Clip a signed integer value into the -128,127 range.212* @param a value to clip213* @return clipped value214*/215static av_always_inline av_const int8_t av_clip_int8_c(int a)216{217if ((a+0x80U) & ~0xFF) return (a>>31) ^ 0x7F;218else return a;219}220221/**222* Clip a signed integer value into the 0-65535 range.223* @param a value to clip224* @return clipped value225*/226static av_always_inline av_const uint16_t av_clip_uint16_c(int a)227{228if (a&(~0xFFFF)) return (~a)>>31;229else return a;230}231232/**233* Clip a signed integer value into the -32768,32767 range.234* @param a value to clip235* @return clipped value236*/237static av_always_inline av_const int16_t av_clip_int16_c(int a)238{239if ((a+0x8000U) & ~0xFFFF) return (a>>31) ^ 0x7FFF;240else return a;241}242243/**244* Clip a signed 64-bit integer value into the -2147483648,2147483647 range.245* @param a value to clip246* @return clipped value247*/248static av_always_inline av_const int32_t av_clipl_int32_c(int64_t a)249{250if ((a+0x80000000u) & ~UINT64_C(0xFFFFFFFF)) return (int32_t)((a>>63) ^ 0x7FFFFFFF);251else return (int32_t)a;252}253254/**255* Clip a signed integer into the -(2^p),(2^p-1) range.256* @param a value to clip257* @param p bit position to clip at258* @return clipped value259*/260static av_always_inline av_const int av_clip_intp2_c(int a, int p)261{262if (((unsigned)a + (1 << p)) & ~((2 << p) - 1))263return (a >> 31) ^ ((1 << p) - 1);264else265return a;266}267268/**269* Clip a signed integer to an unsigned power of two range.270* @param a value to clip271* @param p bit position to clip at272* @return clipped value273*/274static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)275{276if (a & ~((1<<p) - 1)) return (~a) >> 31 & ((1<<p) - 1);277else return a;278}279280/**281* Clear high bits from an unsigned integer starting with specific bit position282* @param a value to clip283* @param p bit position to clip at284* @return clipped value285*/286static av_always_inline av_const unsigned av_mod_uintp2_c(unsigned a, unsigned p)287{288return a & ((1U << p) - 1);289}290291/**292* Add two signed 32-bit values with saturation.293*294* @param a one value295* @param b another value296* @return sum with signed saturation297*/298static av_always_inline int av_sat_add32_c(int a, int b)299{300return av_clipl_int32((int64_t)a + b);301}302303/**304* Add a doubled value to another value with saturation at both stages.305*306* @param a first value307* @param b value doubled and added to a308* @return sum sat(a + sat(2*b)) with signed saturation309*/310static av_always_inline int av_sat_dadd32_c(int a, int b)311{312return av_sat_add32(a, av_sat_add32(b, b));313}314315/**316* Subtract two signed 32-bit values with saturation.317*318* @param a one value319* @param b another value320* @return difference with signed saturation321*/322static av_always_inline int av_sat_sub32_c(int a, int b)323{324return av_clipl_int32((int64_t)a - b);325}326327/**328* Subtract a doubled value from another value with saturation at both stages.329*330* @param a first value331* @param b value doubled and subtracted from a332* @return difference sat(a - sat(2*b)) with signed saturation333*/334static av_always_inline int av_sat_dsub32_c(int a, int b)335{336return av_sat_sub32(a, av_sat_add32(b, b));337}338339/**340* Add two signed 64-bit values with saturation.341*342* @param a one value343* @param b another value344* @return sum with signed saturation345*/346static av_always_inline int64_t av_sat_add64_c(int64_t a, int64_t b) {347#if (!defined(__INTEL_COMPILER) && AV_GCC_VERSION_AT_LEAST(5,1)) || AV_HAS_BUILTIN(__builtin_add_overflow)348int64_t tmp;349return !__builtin_add_overflow(a, b, &tmp) ? tmp : (tmp < 0 ? INT64_MAX : INT64_MIN);350#else351int64_t s = a+(uint64_t)b;352if ((int64_t)(a^b | ~s^b) >= 0)353return INT64_MAX ^ (b >> 63);354return s;355#endif356}357358/**359* Subtract two signed 64-bit values with saturation.360*361* @param a one value362* @param b another value363* @return difference with signed saturation364*/365static av_always_inline int64_t av_sat_sub64_c(int64_t a, int64_t b) {366#if (!defined(__INTEL_COMPILER) && AV_GCC_VERSION_AT_LEAST(5,1)) || AV_HAS_BUILTIN(__builtin_sub_overflow)367int64_t tmp;368return !__builtin_sub_overflow(a, b, &tmp) ? tmp : (tmp < 0 ? INT64_MAX : INT64_MIN);369#else370if (b <= 0 && a >= INT64_MAX + b)371return INT64_MAX;372if (b >= 0 && a <= INT64_MIN + b)373return INT64_MIN;374return a - b;375#endif376}377378/**379* Clip a float value into the amin-amax range.380* If a is nan or -inf amin will be returned.381* If a is +inf amax will be returned.382* @param a value to clip383* @param amin minimum value of the clip range384* @param amax maximum value of the clip range385* @return clipped value386*/387static av_always_inline av_const float av_clipf_c(float a, float amin, float amax)388{389#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2390if (amin > amax) abort();391#endif392return FFMIN(FFMAX(a, amin), amax);393}394395/**396* Clip a double value into the amin-amax range.397* If a is nan or -inf amin will be returned.398* If a is +inf amax will be returned.399* @param a value to clip400* @param amin minimum value of the clip range401* @param amax maximum value of the clip range402* @return clipped value403*/404static av_always_inline av_const double av_clipd_c(double a, double amin, double amax)405{406#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2407if (amin > amax) abort();408#endif409return FFMIN(FFMAX(a, amin), amax);410}411412/** Compute ceil(log2(x)).413* @param x value used to compute ceil(log2(x))414* @return computed ceiling of log2(x)415*/416static av_always_inline av_const int av_ceil_log2_c(int x)417{418return av_log2((x - 1U) << 1);419}420421/**422* Count number of bits set to one in x423* @param x value to count bits of424* @return the number of bits set to one in x425*/426static av_always_inline av_const int av_popcount_c(uint32_t x)427{428x -= (x >> 1) & 0x55555555;429x = (x & 0x33333333) + ((x >> 2) & 0x33333333);430x = (x + (x >> 4)) & 0x0F0F0F0F;431x += x >> 8;432return (x + (x >> 16)) & 0x3F;433}434435/**436* Count number of bits set to one in x437* @param x value to count bits of438* @return the number of bits set to one in x439*/440static av_always_inline av_const int av_popcount64_c(uint64_t x)441{442return av_popcount((uint32_t)x) + av_popcount((uint32_t)(x >> 32));443}444445static av_always_inline av_const int av_parity_c(uint32_t v)446{447return av_popcount(v) & 1;448}449450/**451* Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.452*453* @param val Output value, must be an lvalue of type uint32_t.454* @param GET_BYTE Expression reading one byte from the input.455* Evaluated up to 7 times (4 for the currently456* assigned Unicode range). With a memory buffer457* input, this could be *ptr++, or if you want to make sure458* that *ptr stops at the end of a NULL terminated string then459* *ptr ? *ptr++ : 0460* @param ERROR Expression to be evaluated on invalid input,461* typically a goto statement.462*463* @warning ERROR should not contain a loop control statement which464* could interact with the internal while loop, and should force an465* exit from the macro code (e.g. through a goto or a return) in order466* to prevent undefined results.467*/468#define GET_UTF8(val, GET_BYTE, ERROR)\469val= (GET_BYTE);\470{\471uint32_t top = (val & 128) >> 1;\472if ((val & 0xc0) == 0x80 || val >= 0xFE)\473{ERROR}\474while (val & top) {\475unsigned int tmp = (GET_BYTE) - 128;\476if(tmp>>6)\477{ERROR}\478val= (val<<6) + tmp;\479top <<= 5;\480}\481val &= (top << 1) - 1;\482}483484/**485* Convert a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form.486*487* @param val Output value, must be an lvalue of type uint32_t.488* @param GET_16BIT Expression returning two bytes of UTF-16 data converted489* to native byte order. Evaluated one or two times.490* @param ERROR Expression to be evaluated on invalid input,491* typically a goto statement.492*/493#define GET_UTF16(val, GET_16BIT, ERROR)\494val = (GET_16BIT);\495{\496unsigned int hi = val - 0xD800;\497if (hi < 0x800) {\498val = (GET_16BIT) - 0xDC00;\499if (val > 0x3FFU || hi > 0x3FFU)\500{ERROR}\501val += (hi<<10) + 0x10000;\502}\503}\504505/**506* @def PUT_UTF8(val, tmp, PUT_BYTE)507* Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).508* @param val is an input-only argument and should be of type uint32_t. It holds509* a UCS-4 encoded Unicode character that is to be converted to UTF-8. If510* val is given as a function it is executed only once.511* @param tmp is a temporary variable and should be of type uint8_t. It512* represents an intermediate value during conversion that is to be513* output by PUT_BYTE.514* @param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.515* It could be a function or a statement, and uses tmp as the input byte.516* For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be517* executed up to 4 times for values in the valid UTF-8 range and up to518* 7 times in the general case, depending on the length of the converted519* Unicode character.520*/521#define PUT_UTF8(val, tmp, PUT_BYTE)\522{\523int bytes, shift;\524uint32_t in = val;\525if (in < 0x80) {\526tmp = in;\527PUT_BYTE\528} else {\529bytes = (av_log2(in) + 4) / 5;\530shift = (bytes - 1) * 6;\531tmp = (256 - (256 >> bytes)) | (in >> shift);\532PUT_BYTE\533while (shift >= 6) {\534shift -= 6;\535tmp = 0x80 | ((in >> shift) & 0x3f);\536PUT_BYTE\537}\538}\539}540541/**542* @def PUT_UTF16(val, tmp, PUT_16BIT)543* Convert a 32-bit Unicode character to its UTF-16 encoded form (2 or 4 bytes).544* @param val is an input-only argument and should be of type uint32_t. It holds545* a UCS-4 encoded Unicode character that is to be converted to UTF-16. If546* val is given as a function it is executed only once.547* @param tmp is a temporary variable and should be of type uint16_t. It548* represents an intermediate value during conversion that is to be549* output by PUT_16BIT.550* @param PUT_16BIT writes the converted UTF-16 data to any proper destination551* in desired endianness. It could be a function or a statement, and uses tmp552* as the input byte. For example, PUT_BYTE could be "*output++ = tmp;"553* PUT_BYTE will be executed 1 or 2 times depending on input character.554*/555#define PUT_UTF16(val, tmp, PUT_16BIT)\556{\557uint32_t in = val;\558if (in < 0x10000) {\559tmp = in;\560PUT_16BIT\561} else {\562tmp = 0xD800 | ((in - 0x10000) >> 10);\563PUT_16BIT\564tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\565PUT_16BIT\566}\567}\568569570571#include "mem.h"572573#ifdef HAVE_AV_CONFIG_H574# include "internal.h"575#endif /* HAVE_AV_CONFIG_H */576577#endif /* AVUTIL_COMMON_H */578579580