This fixes error handling for BeOS, removing the need for some ifdefs.
[libav.git] / libavutil / common.h
CommitLineData
04d7f601
DB
1/*
2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3 *
b78e7197
DB
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
04d7f601
DB
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
b78e7197 9 * version 2.1 of the License, or (at your option) any later version.
04d7f601 10 *
b78e7197 11 * FFmpeg is distributed in the hope that it will be useful,
04d7f601
DB
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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
b78e7197 17 * License along with FFmpeg; if not, write to the Free Software
04d7f601
DB
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
983e3246
MN
21/**
22 * @file common.h
05020c89 23 * common internal and external api header.
983e3246
MN
24 */
25
de6d9b64
FB
26#ifndef COMMON_H
27#define COMMON_H
28
1845bf1f
MR
29#include <inttypes.h>
30
420b073b 31#ifdef HAVE_AV_CONFIG_H
1a565432 32/* only include the following when compiling package */
9b59c92f
MN
33# include "config.h"
34
35# include <stdlib.h>
36# include <stdio.h>
37# include <string.h>
56c4a184 38# include <ctype.h>
9ff18a70 39# include <limits.h>
8fa36ae0 40# include <errno.h>
9b59c92f 41# include <math.h>
849f1035
MR
42#endif /* HAVE_AV_CONFIG_H */
43
44#ifndef av_always_inline
45#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
46# define av_always_inline __attribute__((always_inline)) inline
47#else
48# define av_always_inline inline
49#endif
50#endif
51
52#ifdef HAVE_AV_CONFIG_H
1845bf1f 53# include "internal.h"
44f27b3a 54#endif /* HAVE_AV_CONFIG_H */
1a565432 55
955ab9a4
MN
56#ifndef attribute_deprecated
57#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
58# define attribute_deprecated __attribute__((deprecated))
59#else
60# define attribute_deprecated
61#endif
62#endif
63
8da9266c
MR
64#ifndef INT64_C
65#define INT64_C(c) (c ## LL)
66#define UINT64_C(c) (c ## ULL)
0339e2b4 67#endif
1a565432 68
073b013d 69//rounded divison & shift
10f3005f 70#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
d7e9533a
MN
71/* assume b>0 */
72#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
c26abfa5 73#define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
02305ff3 74#define FFSIGN(a) ((a) > 0 ? 1 : -1)
75460b0c 75
b8a78f41
MN
76#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
77#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
d7e9533a 78
1345f4ed 79#define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
67eca72d 80
de6d9b64 81/* misc math functions */
a77caa4d 82extern const uint8_t ff_log2_tab[256];
de6d9b64 83
a822a479 84static inline int av_log2(unsigned int v)
de6d9b64
FB
85{
86 int n;
87
88 n = 0;
89 if (v & 0xffff0000) {
90 v >>= 16;
91 n += 16;
92 }
93 if (v & 0xff00) {
94 v >>= 8;
95 n += 8;
96 }
c81f0349
MN
97 n += ff_log2_tab[v];
98
99 return n;
100}
101
102static inline int av_log2_16bit(unsigned int v)
103{
104 int n;
105
106 n = 0;
107 if (v & 0xff00) {
108 v >>= 8;
109 n += 8;
de6d9b64 110 }
c81f0349
MN
111 n += ff_log2_tab[v];
112
de6d9b64
FB
113 return n;
114}
115
45870f57
MN
116/* median of 3 */
117static inline int mid_pred(int a, int b, int c)
118{
d0b456ba 119#ifdef HAVE_CMOV
7e611a0e 120 int i=b;
93a319f1 121 asm volatile(
7e611a0e
LM
122 "cmp %2, %1 \n\t"
123 "cmovg %1, %0 \n\t"
124 "cmovg %2, %1 \n\t"
125 "cmp %3, %1 \n\t"
93a319f1 126 "cmovl %3, %1 \n\t"
7e611a0e
LM
127 "cmp %1, %0 \n\t"
128 "cmovg %1, %0 \n\t"
129 :"+&r"(i), "+&r"(a)
130 :"r"(b), "r"(c)
93a319f1
LM
131 );
132 return i;
133#elif 0
7a62e94a
MN
134 int t= (a-b)&((a-b)>>31);
135 a-=t;
136 b+=t;
137 b-= (b-c)&((b-c)>>31);
138 b+= (a-b)&((a-b)>>31);
139
140 return b;
141#else
142 if(a>b){
143 if(c>b){
144 if(c>a) b=a;
145 else b=c;
146 }
147 }else{
148 if(b>c){
149 if(c>a) b=c;
150 else b=a;
151 }
152 }
153 return b;
154#endif
45870f57
MN
155}
156
77177335
AJ
157/**
158 * clip a signed integer value into the amin-amax range
159 * @param a value to clip
160 * @param amin minimum value of the clip range
161 * @param amax maximum value of the clip range
c6c36725 162 * @return clipped value
77177335 163 */
91029be7
MN
164static inline int clip(int a, int amin, int amax)
165{
18769c0a
MN
166 if (a < amin) return amin;
167 else if (a > amax) return amax;
168 else return a;
91029be7
MN
169}
170
77177335
AJ
171/**
172 * clip a signed integer value into the 0-255 range
173 * @param a value to clip
c6c36725 174 * @return clipped value
77177335 175 */
3a1fda0a 176static inline uint8_t clip_uint8(int a)
3ebc7e04
MN
177{
178 if (a&(~255)) return (-a)>>31;
179 else return a;
180}
181
9dbcbd92 182/* math */
14bea432 183int64_t ff_gcd(int64_t a, int64_t b);
9dbcbd92 184
202ef8b8
MN
185/**
186 * converts fourcc string to int
187 */
6a85ec8d 188static inline int ff_get_fourcc(const char *s){
05020c89 189#ifdef HAVE_AV_CONFIG_H
202ef8b8 190 assert( strlen(s)==4 );
05020c89 191#endif
966df5b6 192
202ef8b8
MN
193 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
194}
195
e8750b00
FR
196#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
197#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
198
d73427e3
GP
199/*!
200 * \def GET_UTF8(val, GET_BYTE, ERROR)
201 * converts a utf-8 character (up to 4 bytes long) to its 32-bit ucs-4 encoded form
202 * \param val is the output and should be of type uint32_t. It holds the converted
203 * ucs-4 character and should be a left value.
204 * \param GET_BYTE gets utf-8 encoded bytes from any proper source. It can be
205 * a function or a statement whose return value or evaluated value is of type
40a08c7e
RD
206 * uint8_t. It will be executed up to 4 times for values in the valid utf-8 range,
207 * and up to 7 times in the general case.
d73427e3
GP
208 * \param ERROR action that should be taken when an invalid utf-8 byte is returned
209 * from GET_BYTE. It should be a statement that jumps out of the macro,
210 * like exit(), goto, return, break, or continue.
211 */
9d82b0dd
MN
212#define GET_UTF8(val, GET_BYTE, ERROR)\
213 val= GET_BYTE;\
214 {\
215 int ones= 7 - av_log2(val ^ 255);\
216 if(ones==1)\
217 ERROR\
218 val&= 127>>ones;\
219 while(--ones > 0){\
220 int tmp= GET_BYTE - 128;\
221 if(tmp>>6)\
222 ERROR\
223 val= (val<<6) + tmp;\
224 }\
225 }
2ad1516a 226
0e8c148b 227/*!
34d33769 228 * \def PUT_UTF8(val, tmp, PUT_BYTE)
d73427e3 229 * converts a 32-bit unicode character to its utf-8 encoded form (up to 4 bytes long).
0e8c148b
GP
230 * \param val is an input only argument and should be of type uint32_t. It holds
231 * a ucs4 encoded unicode character that is to be converted to utf-8. If
232 * val is given as a function it's executed only once.
233 * \param tmp is a temporary variable and should be of type uint8_t. It
234 * represents an intermediate value during conversion that is to be
235 * outputted by PUT_BYTE.
236 * \param PUT_BYTE writes the converted utf-8 bytes to any proper destination.
237 * It could be a function or a statement, and uses tmp as the input byte.
238 * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
40a08c7e
RD
239 * executed up to 4 times for values in the valid utf-8 range and up to
240 * 7 times in the general case, depending on the length of the converted
0e8c148b
GP
241 * unicode character.
242 */
360932f7
ZM
243#define PUT_UTF8(val, tmp, PUT_BYTE)\
244 {\
245 int bytes, shift;\
246 uint32_t in = val;\
247 if (in < 0x80) {\
248 tmp = in;\
249 PUT_BYTE\
250 } else {\
251 bytes = (av_log2(in) + 4) / 5;\
252 shift = (bytes - 1) * 6;\
253 tmp = (256 - (256 >> bytes)) | (in >> shift);\
254 PUT_BYTE\
255 while (shift >= 6) {\
256 shift -= 6;\
257 tmp = 0x80 | ((in >> shift) & 0x3f);\
258 PUT_BYTE\
259 }\
260 }\
261 }
262
3cd52279 263#if defined(ARCH_X86) || defined(ARCH_POWERPC)
8230cf02 264#if defined(ARCH_X86_64)
0775c88f 265static inline uint64_t read_time(void)
8230cf02 266{
bb270c08
DB
267 uint64_t a, d;
268 asm volatile( "rdtsc\n\t"
269 : "=a" (a), "=d" (d)
270 );
271 return (d << 32) | (a & 0xffffffff);
8230cf02 272}
419b8784 273#elif defined(ARCH_X86_32)
0775c88f 274static inline long long read_time(void)
b534c7f9 275{
bb270c08
DB
276 long long l;
277 asm volatile( "rdtsc\n\t"
278 : "=A" (l)
279 );
280 return l;
b534c7f9 281}
0775c88f
MN
282#else //FIXME check ppc64
283static inline uint64_t read_time(void)
284{
285 uint32_t tbu, tbl, temp;
286
287 /* from section 2.2.1 of the 32-bit PowerPC PEM */
288 __asm__ __volatile__(
289 "1:\n"
290 "mftbu %2\n"
291 "mftb %0\n"
292 "mftbu %1\n"
293 "cmpw %2,%1\n"
294 "bne 1b\n"
295 : "=r"(tbl), "=r"(tbu), "=r"(temp)
296 :
297 : "cc");
298
299 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
300}
8230cf02 301#endif
b534c7f9
MN
302
303#define START_TIMER \
b534c7f9 304uint64_t tend;\
0775c88f 305uint64_t tstart= read_time();\
b534c7f9
MN
306
307#define STOP_TIMER(id) \
0775c88f 308tend= read_time();\
d705e4a6
MN
309{\
310 static uint64_t tsum=0;\
311 static int tcount=0;\
312 static int tskip_count=0;\
9b0dfb27 313 if(tcount<2 || tend - tstart < FFMAX(8*tsum/tcount, 2000)){\
d705e4a6
MN
314 tsum+= tend - tstart;\
315 tcount++;\
316 }else\
317 tskip_count++;\
8e06f20a 318 if(((tcount+tskip_count)&(tcount+tskip_count-1))==0){\
e852beee 319 av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
d705e4a6 320 }\
b534c7f9 321}
0187e903 322#else
115329f1 323#define START_TIMER
0187e903 324#define STOP_TIMER(id) {}
b534c7f9
MN
325#endif
326
cea8f6f3 327/* memory */
98145875
LB
328
329#ifdef __GNUC__
330 #define DECLARE_ALIGNED(n,t,v) t v __attribute__ ((aligned (n)))
331#else
332 #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
333#endif
334
79e47000 335/* memory */
cea8f6f3
LA
336void *av_malloc(unsigned int size);
337void *av_realloc(void *ptr, unsigned int size);
338void av_free(void *ptr);
339
79e47000
LB
340void *av_mallocz(unsigned int size);
341char *av_strdup(const char *s);
342void av_freep(void *ptr);
343
96707bb7 344#endif /* COMMON_H */