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
23 * common internal and external api header.
30 #define M_PI 3.14159265358979323846
33 #ifdef HAVE_AV_CONFIG_H
34 /* only include the following when compiling package */
48 #endif /* HAVE_AV_CONFIG_H */
50 /* Suppress restrict if it was not defined in config.h. */
56 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
57 # define always_inline __attribute__((always_inline)) inline
59 # define always_inline inline
63 #ifndef attribute_used
64 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
65 # define attribute_used __attribute__((used))
67 # define attribute_used
71 #ifndef attribute_unused
72 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
73 # define attribute_unused __attribute__((unused))
75 # define attribute_unused
79 #ifndef attribute_deprecated
80 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
81 # define attribute_deprecated __attribute__((deprecated))
83 # define attribute_deprecated
87 # include <inttypes.h>
110 #define PRIdFAST16 PRId32
114 #define PRIdFAST32 PRId32
118 #define INT16_MIN (-0x7fff-1)
122 #define INT16_MAX 0x7fff
126 #define INT32_MIN (-0x7fffffff-1)
130 #define INT32_MAX 0x7fffffff
134 #define UINT32_MAX 0xffffffff
138 #define INT64_MIN (-0x7fffffffffffffffLL-1)
142 #define INT64_MAX int64_t_C(9223372036854775807)
146 #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
150 # if INT_MAX != 2147483647
158 #define int64_t_C(c) (c ## LL)
159 #define uint64_t_C(c) (c ## ULL)
162 #if defined(__MINGW32__) && !defined(BUILD_AVUTIL) && defined(BUILD_SHARED_AV)
163 # define FF_IMPORT_ATTR __declspec(dllimport)
165 # define FF_IMPORT_ATTR
169 #ifdef HAVE_AV_CONFIG_H
170 /* only include the following when compiling package */
171 # include "internal.h"
174 //rounded divison & shift
175 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
177 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
178 #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
179 #define FFSIGN(a) ((a) > 0 ? 1 : -1)
181 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
182 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
184 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
186 /* misc math functions */
187 extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
189 static inline int av_log2(unsigned int v)
194 if (v & 0xffff0000) {
207 static inline int av_log2_16bit(unsigned int v)
222 static inline int mid_pred(int a, int b, int c)
239 int t= (a-b)&((a-b)>>31);
242 b-= (b-c)&((b-c)>>31);
243 b+= (a-b)&((a-b)>>31);
263 * clip a signed integer value into the amin-amax range
264 * @param a value to clip
265 * @param amin minimum value of the clip range
266 * @param amax maximum value of the clip range
267 * @return cliped value
269 static inline int clip(int a, int amin, int amax)
271 if (a < amin) return amin;
272 else if (a > amax) return amax;
277 * clip a signed integer value into the 0-255 range
278 * @param a value to clip
279 * @return cliped value
281 static inline uint8_t clip_uint8(int a)
283 if (a&(~255)) return (-a)>>31;
288 int64_t ff_gcd(int64_t a, int64_t b);
291 * converts fourcc string to int
293 static inline int ff_get_fourcc(const char *s){
294 #ifdef HAVE_AV_CONFIG_H
295 assert( strlen(s)==4 );
298 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
301 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
302 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
305 * \def GET_UTF8(val, GET_BYTE, ERROR)
306 * converts a utf-8 character (up to 4 bytes long) to its 32-bit ucs-4 encoded form
307 * \param val is the output and should be of type uint32_t. It holds the converted
308 * ucs-4 character and should be a left value.
309 * \param GET_BYTE gets utf-8 encoded bytes from any proper source. It can be
310 * a function or a statement whose return value or evaluated value is of type
311 * uint8_t. It will be execuded up to 4 times.
312 * \param ERROR action that should be taken when an invalid utf-8 byte is returned
313 * from GET_BYTE. It should be a statement that jumps out of the macro,
314 * like exit(), goto, return, break, or continue.
316 #define GET_UTF8(val, GET_BYTE, ERROR)\
319 int ones= 7 - av_log2(val ^ 255);\
324 int tmp= GET_BYTE - 128;\
327 val= (val<<6) + tmp;\
332 * \def PUT_UTF8(val, tmp, PUT_BYTE)
333 * converts a 32-bit unicode character to its utf-8 encoded form (up to 4 bytes long).
334 * \param val is an input only argument and should be of type uint32_t. It holds
335 * a ucs4 encoded unicode character that is to be converted to utf-8. If
336 * val is given as a function it's executed only once.
337 * \param tmp is a temporary variable and should be of type uint8_t. It
338 * represents an intermediate value during conversion that is to be
339 * outputted by PUT_BYTE.
340 * \param PUT_BYTE writes the converted utf-8 bytes to any proper destination.
341 * It could be a function or a statement, and uses tmp as the input byte.
342 * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
343 * executed up to 4 times, depending on the length of the converted
346 #define PUT_UTF8(val, tmp, PUT_BYTE)\
354 bytes = (av_log2(in) + 4) / 5;\
355 shift = (bytes - 1) * 6;\
356 tmp = (256 - (256 >> bytes)) | (in >> shift);\
358 while (shift >= 6) {\
360 tmp = 0x80 | ((in >> shift) & 0x3f);\
366 #if defined(ARCH_X86) || defined(ARCH_POWERPC)
367 #if defined(ARCH_X86_64)
368 static inline uint64_t read_time(void)
371 asm volatile( "rdtsc\n\t"
374 return (d << 32) | (a & 0xffffffff);
376 #elif defined(ARCH_X86_32)
377 static inline long long read_time(void)
380 asm volatile( "rdtsc\n\t"
385 #else //FIXME check ppc64
386 static inline uint64_t read_time(void)
388 uint32_t tbu, tbl, temp;
390 /* from section 2.2.1 of the 32-bit PowerPC PEM */
391 __asm__ __volatile__(
398 : "=r"(tbl), "=r"(tbu), "=r"(temp)
402 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
406 #define START_TIMER \
408 uint64_t tstart= read_time();\
410 #define STOP_TIMER(id) \
413 static uint64_t tsum=0;\
414 static int tcount=0;\
415 static int tskip_count=0;\
416 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
417 tsum+= tend - tstart;\
421 if(((tcount+tskip_count)&(tcount+tskip_count-1))==0){\
422 av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
427 #define STOP_TIMER(id) {}
433 #define DECLARE_ALIGNED(n,t,v) t v __attribute__ ((aligned (n)))
435 #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
439 void *av_malloc(unsigned int size);
440 void *av_realloc(void *ptr, unsigned int size);
441 void av_free(void *ptr);
443 void *av_mallocz(unsigned int size);
444 char *av_strdup(const char *s);
445 void av_freep(void *ptr);
447 #endif /* COMMON_H */