nengel@2: /* nengel@2: * copyright (c) 2006 Michael Niedermayer nengel@2: * nengel@2: * This file is part of FFmpeg. nengel@2: * nengel@2: * FFmpeg is free software; you can redistribute it and/or nengel@2: * modify it under the terms of the GNU Lesser General Public nengel@2: * License as published by the Free Software Foundation; either nengel@2: * version 2.1 of the License, or (at your option) any later version. nengel@2: * nengel@2: * FFmpeg is distributed in the hope that it will be useful, nengel@2: * but WITHOUT ANY WARRANTY; without even the implied warranty of nengel@2: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU nengel@2: * Lesser General Public License for more details. nengel@2: * nengel@2: * You should have received a copy of the GNU Lesser General Public nengel@2: * License along with FFmpeg; if not, write to the Free Software nengel@2: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA nengel@2: */ nengel@2: nengel@2: /** nengel@2: * @file nengel@2: * common internal and external API header nengel@2: */ nengel@2: nengel@2: #ifndef AVUTIL_COMMON_H nengel@2: #define AVUTIL_COMMON_H nengel@2: nengel@2: #include nengel@2: #include nengel@2: #include nengel@2: #include nengel@2: #include nengel@2: #include nengel@2: #include nengel@2: #include nengel@2: #include "attributes.h" nengel@2: nengel@2: //rounded division & shift nengel@2: #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b)) nengel@2: /* assume b>0 */ nengel@2: #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b)) nengel@2: #define FFABS(a) ((a) >= 0 ? (a) : (-(a))) nengel@2: #define FFSIGN(a) ((a) > 0 ? 1 : -1) nengel@2: nengel@2: #define FFMAX(a,b) ((a) > (b) ? (a) : (b)) nengel@2: #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c) nengel@2: #define FFMIN(a,b) ((a) > (b) ? (b) : (a)) nengel@2: #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c) nengel@2: nengel@2: #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0) nengel@2: #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0])) nengel@2: #define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1)) nengel@2: nengel@2: /* misc math functions */ nengel@2: extern const uint8_t ff_log2_tab[256]; nengel@2: nengel@2: static inline av_const int av_log2_c(unsigned int v) nengel@2: { nengel@2: int n = 0; nengel@2: if (v & 0xffff0000) { nengel@2: v >>= 16; nengel@2: n += 16; nengel@2: } nengel@2: if (v & 0xff00) { nengel@2: v >>= 8; nengel@2: n += 8; nengel@2: } nengel@2: n += ff_log2_tab[v]; nengel@2: nengel@2: return n; nengel@2: } nengel@2: nengel@2: static inline av_const int av_log2_16bit_c(unsigned int v) nengel@2: { nengel@2: int n = 0; nengel@2: if (v & 0xff00) { nengel@2: v >>= 8; nengel@2: n += 8; nengel@2: } nengel@2: n += ff_log2_tab[v]; nengel@2: nengel@2: return n; nengel@2: } nengel@2: nengel@2: #ifdef HAVE_AV_CONFIG_H nengel@2: # include "config.h" nengel@2: #endif nengel@2: nengel@2: /** nengel@2: * Clips a signed integer value into the amin-amax range. nengel@2: * @param a value to clip nengel@2: * @param amin minimum value of the clip range nengel@2: * @param amax maximum value of the clip range nengel@2: * @return clipped value nengel@2: */ nengel@2: static inline av_const int av_clip(int a, int amin, int amax) nengel@2: { nengel@2: if (a < amin) return amin; nengel@2: else if (a > amax) return amax; nengel@2: else return a; nengel@2: } nengel@2: nengel@2: /** nengel@2: * Clips a signed integer value into the 0-255 range. nengel@2: * @param a value to clip nengel@2: * @return clipped value nengel@2: */ nengel@2: static inline av_const uint8_t av_clip_uint8(int a) nengel@2: { nengel@2: if (a&(~0xFF)) return (-a)>>31; nengel@2: else return a; nengel@2: } nengel@2: nengel@2: /** nengel@2: * Clips a signed integer value into the 0-65535 range. nengel@2: * @param a value to clip nengel@2: * @return clipped value nengel@2: */ nengel@2: static inline av_const uint16_t av_clip_uint16(int a) nengel@2: { nengel@2: if (a&(~0xFFFF)) return (-a)>>31; nengel@2: else return a; nengel@2: } nengel@2: nengel@2: /** nengel@2: * Clips a signed integer value into the -32768,32767 range. nengel@2: * @param a value to clip nengel@2: * @return clipped value nengel@2: */ nengel@2: static inline av_const int16_t av_clip_int16(int a) nengel@2: { nengel@2: if ((a+0x8000) & ~0xFFFF) return (a>>31) ^ 0x7FFF; nengel@2: else return a; nengel@2: } nengel@2: nengel@2: /** nengel@2: * Clips a signed 64-bit integer value into the -2147483648,2147483647 range. nengel@2: * @param a value to clip nengel@2: * @return clipped value nengel@2: */ nengel@2: static inline av_const int32_t av_clipl_int32(int64_t a) nengel@2: { nengel@2: if ((a+0x80000000u) & ~UINT64_C(0xFFFFFFFF)) return (a>>63) ^ 0x7FFFFFFF; nengel@2: else return a; nengel@2: } nengel@2: nengel@2: /** nengel@2: * Clips a float value into the amin-amax range. nengel@2: * @param a value to clip nengel@2: * @param amin minimum value of the clip range nengel@2: * @param amax maximum value of the clip range nengel@2: * @return clipped value nengel@2: */ nengel@2: static inline av_const float av_clipf(float a, float amin, float amax) nengel@2: { nengel@2: if (a < amin) return amin; nengel@2: else if (a > amax) return amax; nengel@2: else return a; nengel@2: } nengel@2: nengel@2: /** Computes ceil(log2(x)). nengel@2: * @param x value used to compute ceil(log2(x)) nengel@2: * @return computed ceiling of log2(x) nengel@2: */ nengel@2: static inline av_const int av_ceil_log2(int x) nengel@2: { nengel@2: return av_log2_c((x - 1) << 1); nengel@2: } nengel@2: nengel@2: #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24)) nengel@2: #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24)) nengel@2: nengel@2: /*! nengel@2: * \def GET_UTF8(val, GET_BYTE, ERROR) nengel@2: * Converts a UTF-8 character (up to 4 bytes long) to its 32-bit UCS-4 encoded form nengel@2: * \param val is the output and should be of type uint32_t. It holds the converted nengel@2: * UCS-4 character and should be a left value. nengel@2: * \param GET_BYTE gets UTF-8 encoded bytes from any proper source. It can be nengel@2: * a function or a statement whose return value or evaluated value is of type nengel@2: * uint8_t. It will be executed up to 4 times for values in the valid UTF-8 range, nengel@2: * and up to 7 times in the general case. nengel@2: * \param ERROR action that should be taken when an invalid UTF-8 byte is returned nengel@2: * from GET_BYTE. It should be a statement that jumps out of the macro, nengel@2: * like exit(), goto, return, break, or continue. nengel@2: */ nengel@2: #define GET_UTF8(val, GET_BYTE, ERROR)\ nengel@2: val= GET_BYTE;\ nengel@2: {\ nengel@2: int ones= 7 - av_log2(val ^ 255);\ nengel@2: if(ones==1)\ nengel@2: ERROR\ nengel@2: val&= 127>>ones;\ nengel@2: while(--ones > 0){\ nengel@2: int tmp= GET_BYTE - 128;\ nengel@2: if(tmp>>6)\ nengel@2: ERROR\ nengel@2: val= (val<<6) + tmp;\ nengel@2: }\ nengel@2: } nengel@2: nengel@2: /*! nengel@2: * \def GET_UTF16(val, GET_16BIT, ERROR) nengel@2: * Converts a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form nengel@2: * \param val is the output and should be of type uint32_t. It holds the converted nengel@2: * UCS-4 character and should be a left value. nengel@2: * \param GET_16BIT gets two bytes of UTF-16 encoded data converted to native endianness. nengel@2: * It can be a function or a statement whose return value or evaluated value is of type nengel@2: * uint16_t. It will be executed up to 2 times. nengel@2: * \param ERROR action that should be taken when an invalid UTF-16 surrogate is nengel@2: * returned from GET_BYTE. It should be a statement that jumps out of the macro, nengel@2: * like exit(), goto, return, break, or continue. nengel@2: */ nengel@2: #define GET_UTF16(val, GET_16BIT, ERROR)\ nengel@2: val = GET_16BIT;\ nengel@2: {\ nengel@2: unsigned int hi = val - 0xD800;\ nengel@2: if (hi < 0x800) {\ nengel@2: val = GET_16BIT - 0xDC00;\ nengel@2: if (val > 0x3FFU || hi > 0x3FFU)\ nengel@2: ERROR\ nengel@2: val += (hi<<10) + 0x10000;\ nengel@2: }\ nengel@2: }\ nengel@2: nengel@2: /*! nengel@2: * \def PUT_UTF8(val, tmp, PUT_BYTE) nengel@2: * Converts a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long). nengel@2: * \param val is an input-only argument and should be of type uint32_t. It holds nengel@2: * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If nengel@2: * val is given as a function it is executed only once. nengel@2: * \param tmp is a temporary variable and should be of type uint8_t. It nengel@2: * represents an intermediate value during conversion that is to be nengel@2: * output by PUT_BYTE. nengel@2: * \param PUT_BYTE writes the converted UTF-8 bytes to any proper destination. nengel@2: * It could be a function or a statement, and uses tmp as the input byte. nengel@2: * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be nengel@2: * executed up to 4 times for values in the valid UTF-8 range and up to nengel@2: * 7 times in the general case, depending on the length of the converted nengel@2: * Unicode character. nengel@2: */ nengel@2: #define PUT_UTF8(val, tmp, PUT_BYTE)\ nengel@2: {\ nengel@2: int bytes, shift;\ nengel@2: uint32_t in = val;\ nengel@2: if (in < 0x80) {\ nengel@2: tmp = in;\ nengel@2: PUT_BYTE\ nengel@2: } else {\ nengel@2: bytes = (av_log2(in) + 4) / 5;\ nengel@2: shift = (bytes - 1) * 6;\ nengel@2: tmp = (256 - (256 >> bytes)) | (in >> shift);\ nengel@2: PUT_BYTE\ nengel@2: while (shift >= 6) {\ nengel@2: shift -= 6;\ nengel@2: tmp = 0x80 | ((in >> shift) & 0x3f);\ nengel@2: PUT_BYTE\ nengel@2: }\ nengel@2: }\ nengel@2: } nengel@2: nengel@2: /*! nengel@2: * \def PUT_UTF16(val, tmp, PUT_16BIT) nengel@2: * Converts a 32-bit Unicode character to its UTF-16 encoded form (2 or 4 bytes). nengel@2: * \param val is an input-only argument and should be of type uint32_t. It holds nengel@2: * a UCS-4 encoded Unicode character that is to be converted to UTF-16. If nengel@2: * val is given as a function it is executed only once. nengel@2: * \param tmp is a temporary variable and should be of type uint16_t. It nengel@2: * represents an intermediate value during conversion that is to be nengel@2: * output by PUT_16BIT. nengel@2: * \param PUT_16BIT writes the converted UTF-16 data to any proper destination nengel@2: * in desired endianness. It could be a function or a statement, and uses tmp nengel@2: * as the input byte. For example, PUT_BYTE could be "*output++ = tmp;" nengel@2: * PUT_BYTE will be executed 1 or 2 times depending on input character. nengel@2: */ nengel@2: #define PUT_UTF16(val, tmp, PUT_16BIT)\ nengel@2: {\ nengel@2: uint32_t in = val;\ nengel@2: if (in < 0x10000) {\ nengel@2: tmp = in;\ nengel@2: PUT_16BIT\ nengel@2: } else {\ nengel@2: tmp = 0xD800 | ((in - 0x10000) >> 10);\ nengel@2: PUT_16BIT\ nengel@2: tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\ nengel@2: PUT_16BIT\ nengel@2: }\ nengel@2: }\ nengel@2: nengel@2: nengel@2: nengel@2: #include "mem.h" nengel@2: nengel@2: #ifdef HAVE_AV_CONFIG_H nengel@2: # include "internal.h" nengel@2: #endif /* HAVE_AV_CONFIG_H */ nengel@2: nengel@2: #endif /* AVUTIL_COMMON_H */