2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * @file libavutil/common.h
23 * common internal and external API header
26 #ifndef AVUTIL_COMMON_H
27 #define AVUTIL_COMMON_H
38 #ifdef HAVE_AV_CONFIG_H
43 # define AV_GCC_VERSION_AT_LEAST(x,y) (__GNUC__ > x || __GNUC__ == x && __GNUC_MINOR__ >= y)
45 # define AV_GCC_VERSION_AT_LEAST(x,y) 0
48 #ifndef av_always_inline
49 #if AV_GCC_VERSION_AT_LEAST(3,1)
50 # define av_always_inline __attribute__((always_inline)) inline
52 # define av_always_inline inline
57 #if AV_GCC_VERSION_AT_LEAST(3,1)
58 # define av_noinline __attribute__((noinline))
65 #if AV_GCC_VERSION_AT_LEAST(3,1)
66 # define av_pure __attribute__((pure))
73 #if AV_GCC_VERSION_AT_LEAST(2,6)
74 # define av_const __attribute__((const))
81 #if (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(4,3)
82 # define av_cold __attribute__((cold))
89 #if (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(4,1)
90 # define av_flatten __attribute__((flatten))
96 #ifndef attribute_deprecated
97 #if AV_GCC_VERSION_AT_LEAST(3,1)
98 # define attribute_deprecated __attribute__((deprecated))
100 # define attribute_deprecated
105 #if defined(__GNUC__)
106 # define av_unused __attribute__((unused))
113 #if defined(__GNUC__) && !defined(__ICC)
114 # define av_uninit(x) x=x
116 # define av_uninit(x) x
120 //rounded division & shift
121 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
123 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
124 #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
125 #define FFSIGN(a) ((a) > 0 ? 1 : -1)
127 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
128 #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
129 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
130 #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
132 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
133 #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
134 #define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1))
136 /* misc math functions */
137 extern const uint8_t ff_log2_tab
[256];
139 extern const uint8_t av_reverse
[256];
141 static inline av_const
int av_log2(unsigned int v
)
144 if (v
& 0xffff0000) {
157 static inline av_const
int av_log2_16bit(unsigned int v
)
170 * Clips a signed integer value into the amin-amax range.
171 * @param a value to clip
172 * @param amin minimum value of the clip range
173 * @param amax maximum value of the clip range
174 * @return clipped value
176 static inline av_const
int av_clip(int a
, int amin
, int amax
)
178 if (a
< amin
) return amin
;
179 else if (a
> amax
) return amax
;
184 * Clips a signed integer value into the 0-255 range.
185 * @param a value to clip
186 * @return clipped value
188 static inline av_const
uint8_t av_clip_uint8(int a
)
190 if (a
&(~255)) return (-a
)>>31;
195 * Clips a signed integer value into the 0-65535 range.
196 * @param a value to clip
197 * @return clipped value
199 static inline av_const
uint16_t av_clip_uint16(int a
)
201 if (a
&(~65535)) return (-a
)>>31;
206 * Clips a signed integer value into the -32768,32767 range.
207 * @param a value to clip
208 * @return clipped value
210 static inline av_const
int16_t av_clip_int16(int a
)
212 if ((a
+32768) & ~65535) return (a
>>31) ^ 32767;
217 * Clips a float value into the amin-amax range.
218 * @param a value to clip
219 * @param amin minimum value of the clip range
220 * @param amax maximum value of the clip range
221 * @return clipped value
223 static inline av_const
float av_clipf(float a
, float amin
, float amax
)
225 if (a
< amin
) return amin
;
226 else if (a
> amax
) return amax
;
230 /** Computes ceil(log2(x)).
231 * @param x value used to compute ceil(log2(x))
232 * @return computed ceiling of log2(x)
234 static inline av_const
int av_ceil_log2(int x
)
236 return av_log2((x
- 1) << 1);
239 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
240 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
243 * \def GET_UTF8(val, GET_BYTE, ERROR)
244 * Converts a UTF-8 character (up to 4 bytes long) to its 32-bit UCS-4 encoded form
245 * \param val is the output and should be of type uint32_t. It holds the converted
246 * UCS-4 character and should be a left value.
247 * \param GET_BYTE gets UTF-8 encoded bytes from any proper source. It can be
248 * a function or a statement whose return value or evaluated value is of type
249 * uint8_t. It will be executed up to 4 times for values in the valid UTF-8 range,
250 * and up to 7 times in the general case.
251 * \param ERROR action that should be taken when an invalid UTF-8 byte is returned
252 * from GET_BYTE. It should be a statement that jumps out of the macro,
253 * like exit(), goto, return, break, or continue.
255 #define GET_UTF8(val, GET_BYTE, ERROR)\
258 int ones= 7 - av_log2(val ^ 255);\
263 int tmp= GET_BYTE - 128;\
266 val= (val<<6) + tmp;\
271 * \def GET_UTF16(val, GET_16BIT, ERROR)
272 * Converts a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form
273 * \param val is the output and should be of type uint32_t. It holds the converted
274 * UCS-4 character and should be a left value.
275 * \param GET_16BIT gets two bytes of UTF-16 encoded data converted to native endianness.
276 * It can be a function or a statement whose return value or evaluated value is of type
277 * uint16_t. It will be executed up to 2 times.
278 * \param ERROR action that should be taken when an invalid UTF-16 surrogate is
279 * returned from GET_BYTE. It should be a statement that jumps out of the macro,
280 * like exit(), goto, return, break, or continue.
282 #define GET_UTF16(val, GET_16BIT, ERROR)\
285 unsigned int hi = val - 0xD800;\
287 val = GET_16BIT - 0xDC00;\
288 if (val > 0x3FFU || hi > 0x3FFU)\
290 val += (hi<<10) + 0x10000;\
295 * \def PUT_UTF8(val, tmp, PUT_BYTE)
296 * Converts a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
297 * \param val is an input-only argument and should be of type uint32_t. It holds
298 * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If
299 * val is given as a function it is executed only once.
300 * \param tmp is a temporary variable and should be of type uint8_t. It
301 * represents an intermediate value during conversion that is to be
302 * output by PUT_BYTE.
303 * \param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
304 * It could be a function or a statement, and uses tmp as the input byte.
305 * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
306 * executed up to 4 times for values in the valid UTF-8 range and up to
307 * 7 times in the general case, depending on the length of the converted
310 #define PUT_UTF8(val, tmp, PUT_BYTE)\
318 bytes = (av_log2(in) + 4) / 5;\
319 shift = (bytes - 1) * 6;\
320 tmp = (256 - (256 >> bytes)) | (in >> shift);\
322 while (shift >= 6) {\
324 tmp = 0x80 | ((in >> shift) & 0x3f);\
332 #ifdef HAVE_AV_CONFIG_H
333 # include "internal.h"
334 #endif /* HAVE_AV_CONFIG_H */
336 #endif /* AVUTIL_COMMON_H */