Commit | Line | Data |
---|---|---|
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 | ||
e0c53ac8 AB |
29 | #ifndef M_PI |
30 | #define M_PI 3.14159265358979323846 | |
31 | #endif | |
32 | ||
420b073b | 33 | #ifdef HAVE_AV_CONFIG_H |
1a565432 | 34 | /* only include the following when compiling package */ |
9b59c92f MN |
35 | # include "config.h" |
36 | ||
37 | # include <stdlib.h> | |
38 | # include <stdio.h> | |
39 | # include <string.h> | |
56c4a184 | 40 | # include <ctype.h> |
9ff18a70 | 41 | # include <limits.h> |
9b59c92f MN |
42 | # ifndef __BEOS__ |
43 | # include <errno.h> | |
44 | # else | |
45 | # include "berrno.h" | |
46 | # endif | |
47 | # include <math.h> | |
44f27b3a | 48 | #endif /* HAVE_AV_CONFIG_H */ |
1a565432 | 49 | |
02da51ec FH |
50 | /* Suppress restrict if it was not defined in config.h. */ |
51 | #ifndef restrict | |
9b59c92f | 52 | # define restrict |
02da51ec FH |
53 | #endif |
54 | ||
f4ae934a | 55 | #ifndef always_inline |
d200cab6 | 56 | #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0) |
9b59c92f | 57 | # define always_inline __attribute__((always_inline)) inline |
d200cab6 | 58 | #else |
9b59c92f | 59 | # define always_inline inline |
d200cab6 | 60 | #endif |
f4ae934a | 61 | #endif |
d200cab6 | 62 | |
f4ae934a | 63 | #ifndef attribute_used |
5c0513bd DB |
64 | #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0) |
65 | # define attribute_used __attribute__((used)) | |
66 | #else | |
67 | # define attribute_used | |
68 | #endif | |
f4ae934a | 69 | #endif |
5c0513bd | 70 | |
88730be6 MR |
71 | #ifndef attribute_unused |
72 | #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0) | |
73 | # define attribute_unused __attribute__((unused)) | |
74 | #else | |
75 | # define attribute_unused | |
76 | #endif | |
77 | #endif | |
78 | ||
955ab9a4 MN |
79 | #ifndef attribute_deprecated |
80 | #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0) | |
81 | # define attribute_deprecated __attribute__((deprecated)) | |
82 | #else | |
83 | # define attribute_deprecated | |
84 | #endif | |
85 | #endif | |
86 | ||
d2a9bddd | 87 | #ifndef EMULATE_INTTYPES |
7f965c1c CF |
88 | # include <inttypes.h> |
89 | #else | |
90 | typedef signed char int8_t; | |
91 | typedef signed short int16_t; | |
92 | typedef signed int int32_t; | |
93 | typedef unsigned char uint8_t; | |
94 | typedef unsigned short uint16_t; | |
95 | typedef unsigned int uint32_t; | |
b2b48c77 MR |
96 | typedef signed long long int64_t; |
97 | typedef unsigned long long uint64_t; | |
978844cc | 98 | #endif /* EMULATE_INTTYPES */ |
7f965c1c | 99 | |
86f77a49 FR |
100 | #ifndef PRId64 |
101 | #define PRId64 "lld" | |
102 | #endif | |
103 | ||
e684b35d | 104 | #ifndef PRIu64 |
86f77a49 FR |
105 | #define PRIu64 "llu" |
106 | #endif | |
107 | ||
108 | #ifndef PRIx64 | |
109 | #define PRIx64 "llx" | |
110 | #endif | |
111 | ||
112 | #ifndef PRId32 | |
113 | #define PRId32 "d" | |
114 | #endif | |
115 | ||
116 | #ifndef PRIdFAST16 | |
117 | #define PRIdFAST16 PRId32 | |
118 | #endif | |
119 | ||
120 | #ifndef PRIdFAST32 | |
121 | #define PRIdFAST32 PRId32 | |
e684b35d FR |
122 | #endif |
123 | ||
cc044c1c BH |
124 | #ifndef INT16_MIN |
125 | #define INT16_MIN (-0x7fff-1) | |
126 | #endif | |
127 | ||
128 | #ifndef INT16_MAX | |
129 | #define INT16_MAX 0x7fff | |
130 | #endif | |
131 | ||
d2fbcb3b MR |
132 | #ifndef INT32_MIN |
133 | #define INT32_MIN (-0x7fffffff-1) | |
134 | #endif | |
135 | ||
136 | #ifndef INT32_MAX | |
137 | #define INT32_MAX 0x7fffffff | |
138 | #endif | |
139 | ||
140 | #ifndef UINT32_MAX | |
141 | #define UINT32_MAX 0xffffffff | |
142 | #endif | |
143 | ||
cc044c1c BH |
144 | #ifndef INT64_MIN |
145 | #define INT64_MIN (-0x7fffffffffffffffLL-1) | |
146 | #endif | |
147 | ||
9ff5f175 | 148 | #ifndef INT64_MAX |
2a24fe4c | 149 | #define INT64_MAX int64_t_C(9223372036854775807) |
9ff5f175 MN |
150 | #endif |
151 | ||
caa50878 MN |
152 | #ifndef UINT64_MAX |
153 | #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF) | |
154 | #endif | |
155 | ||
19d053c5 | 156 | #ifdef EMULATE_FAST_INT |
19d053c5 RS |
157 | typedef signed char int_fast8_t; |
158 | typedef signed int int_fast16_t; | |
159 | typedef signed int int_fast32_t; | |
160 | typedef unsigned char uint_fast8_t; | |
161 | typedef unsigned int uint_fast16_t; | |
162 | typedef unsigned int uint_fast32_t; | |
59d8efb3 | 163 | typedef uint64_t uint_fast64_t; |
19d053c5 RS |
164 | #endif |
165 | ||
0ff93477 | 166 | #ifndef INT_BIT |
9ff18a70 | 167 | # if INT_MAX != 2147483647 |
0ff93477 MN |
168 | # define INT_BIT 64 |
169 | # else | |
170 | # define INT_BIT 32 | |
171 | # endif | |
172 | #endif | |
173 | ||
0339e2b4 MR |
174 | #ifndef int64_t_C |
175 | #define int64_t_C(c) (c ## LL) | |
176 | #define uint64_t_C(c) (c ## ULL) | |
177 | #endif | |
1a565432 | 178 | |
320d060a DB |
179 | #if defined(__MINGW32__) && !defined(BUILD_AVUTIL) && defined(BUILD_SHARED_AV) |
180 | # define FF_IMPORT_ATTR __declspec(dllimport) | |
181 | #else | |
182 | # define FF_IMPORT_ATTR | |
183 | #endif | |
184 | ||
185 | ||
05020c89 RD |
186 | #ifdef HAVE_AV_CONFIG_H |
187 | /* only include the following when compiling package */ | |
188 | # include "internal.h" | |
189 | #endif | |
935cdf09 | 190 | |
073b013d | 191 | //rounded divison & shift |
10f3005f | 192 | #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b)) |
d7e9533a MN |
193 | /* assume b>0 */ |
194 | #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b)) | |
32cd20de | 195 | #define ABS(a) ((a) >= 0 ? (a) : (-(a))) |
a22b7322 | 196 | #define SIGN(a) ((a) > 0 ? 1 : -1) |
75460b0c | 197 | |
b8a78f41 MN |
198 | #define FFMAX(a,b) ((a) > (b) ? (a) : (b)) |
199 | #define FFMIN(a,b) ((a) > (b) ? (b) : (a)) | |
d7e9533a | 200 | |
67eca72d MN |
201 | #define SWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0) |
202 | ||
de6d9b64 | 203 | /* misc math functions */ |
320d060a | 204 | extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256]; |
de6d9b64 | 205 | |
a822a479 | 206 | static inline int av_log2(unsigned int v) |
de6d9b64 FB |
207 | { |
208 | int n; | |
209 | ||
210 | n = 0; | |
211 | if (v & 0xffff0000) { | |
212 | v >>= 16; | |
213 | n += 16; | |
214 | } | |
215 | if (v & 0xff00) { | |
216 | v >>= 8; | |
217 | n += 8; | |
218 | } | |
c81f0349 MN |
219 | n += ff_log2_tab[v]; |
220 | ||
221 | return n; | |
222 | } | |
223 | ||
224 | static inline int av_log2_16bit(unsigned int v) | |
225 | { | |
226 | int n; | |
227 | ||
228 | n = 0; | |
229 | if (v & 0xff00) { | |
230 | v >>= 8; | |
231 | n += 8; | |
de6d9b64 | 232 | } |
c81f0349 MN |
233 | n += ff_log2_tab[v]; |
234 | ||
de6d9b64 FB |
235 | return n; |
236 | } | |
237 | ||
45870f57 MN |
238 | /* median of 3 */ |
239 | static inline int mid_pred(int a, int b, int c) | |
240 | { | |
93a319f1 | 241 | #if (defined(ARCH_X86) && __CPU__ >= 686 || defined(ARCH_X86_64)) && !defined(RUNTIME_CPUDETECT) |
7e611a0e | 242 | int i=b; |
93a319f1 | 243 | asm volatile( |
7e611a0e LM |
244 | "cmp %2, %1 \n\t" |
245 | "cmovg %1, %0 \n\t" | |
246 | "cmovg %2, %1 \n\t" | |
247 | "cmp %3, %1 \n\t" | |
93a319f1 | 248 | "cmovl %3, %1 \n\t" |
7e611a0e LM |
249 | "cmp %1, %0 \n\t" |
250 | "cmovg %1, %0 \n\t" | |
251 | :"+&r"(i), "+&r"(a) | |
252 | :"r"(b), "r"(c) | |
93a319f1 LM |
253 | ); |
254 | return i; | |
255 | #elif 0 | |
7a62e94a MN |
256 | int t= (a-b)&((a-b)>>31); |
257 | a-=t; | |
258 | b+=t; | |
259 | b-= (b-c)&((b-c)>>31); | |
260 | b+= (a-b)&((a-b)>>31); | |
261 | ||
262 | return b; | |
263 | #else | |
264 | if(a>b){ | |
265 | if(c>b){ | |
266 | if(c>a) b=a; | |
267 | else b=c; | |
268 | } | |
269 | }else{ | |
270 | if(b>c){ | |
271 | if(c>a) b=c; | |
272 | else b=a; | |
273 | } | |
274 | } | |
275 | return b; | |
276 | #endif | |
45870f57 MN |
277 | } |
278 | ||
77177335 AJ |
279 | /** |
280 | * clip a signed integer value into the amin-amax range | |
281 | * @param a value to clip | |
282 | * @param amin minimum value of the clip range | |
283 | * @param amax maximum value of the clip range | |
284 | * @return cliped value | |
285 | */ | |
91029be7 MN |
286 | static inline int clip(int a, int amin, int amax) |
287 | { | |
18769c0a MN |
288 | if (a < amin) return amin; |
289 | else if (a > amax) return amax; | |
290 | else return a; | |
91029be7 MN |
291 | } |
292 | ||
77177335 AJ |
293 | /** |
294 | * clip a signed integer value into the 0-255 range | |
295 | * @param a value to clip | |
296 | * @return cliped value | |
297 | */ | |
3a1fda0a | 298 | static inline uint8_t clip_uint8(int a) |
3ebc7e04 MN |
299 | { |
300 | if (a&(~255)) return (-a)>>31; | |
301 | else return a; | |
302 | } | |
303 | ||
9dbcbd92 | 304 | /* math */ |
14bea432 | 305 | int64_t ff_gcd(int64_t a, int64_t b); |
9dbcbd92 | 306 | |
202ef8b8 MN |
307 | /** |
308 | * converts fourcc string to int | |
309 | */ | |
6a85ec8d | 310 | static inline int ff_get_fourcc(const char *s){ |
05020c89 | 311 | #ifdef HAVE_AV_CONFIG_H |
202ef8b8 | 312 | assert( strlen(s)==4 ); |
05020c89 | 313 | #endif |
966df5b6 | 314 | |
202ef8b8 MN |
315 | return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24); |
316 | } | |
317 | ||
e8750b00 FR |
318 | #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24)) |
319 | #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24)) | |
320 | ||
321 | ||
9d82b0dd MN |
322 | #define GET_UTF8(val, GET_BYTE, ERROR)\ |
323 | val= GET_BYTE;\ | |
324 | {\ | |
325 | int ones= 7 - av_log2(val ^ 255);\ | |
326 | if(ones==1)\ | |
327 | ERROR\ | |
328 | val&= 127>>ones;\ | |
329 | while(--ones > 0){\ | |
330 | int tmp= GET_BYTE - 128;\ | |
331 | if(tmp>>6)\ | |
332 | ERROR\ | |
333 | val= (val<<6) + tmp;\ | |
334 | }\ | |
335 | } | |
2ad1516a | 336 | |
0775c88f | 337 | #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC) |
8230cf02 | 338 | #if defined(ARCH_X86_64) |
0775c88f | 339 | static inline uint64_t read_time(void) |
8230cf02 | 340 | { |
bb270c08 DB |
341 | uint64_t a, d; |
342 | asm volatile( "rdtsc\n\t" | |
343 | : "=a" (a), "=d" (d) | |
344 | ); | |
345 | return (d << 32) | (a & 0xffffffff); | |
8230cf02 | 346 | } |
0775c88f MN |
347 | #elif defined(ARCH_X86) |
348 | static inline long long read_time(void) | |
b534c7f9 | 349 | { |
bb270c08 DB |
350 | long long l; |
351 | asm volatile( "rdtsc\n\t" | |
352 | : "=A" (l) | |
353 | ); | |
354 | return l; | |
b534c7f9 | 355 | } |
0775c88f MN |
356 | #else //FIXME check ppc64 |
357 | static inline uint64_t read_time(void) | |
358 | { | |
359 | uint32_t tbu, tbl, temp; | |
360 | ||
361 | /* from section 2.2.1 of the 32-bit PowerPC PEM */ | |
362 | __asm__ __volatile__( | |
363 | "1:\n" | |
364 | "mftbu %2\n" | |
365 | "mftb %0\n" | |
366 | "mftbu %1\n" | |
367 | "cmpw %2,%1\n" | |
368 | "bne 1b\n" | |
369 | : "=r"(tbl), "=r"(tbu), "=r"(temp) | |
370 | : | |
371 | : "cc"); | |
372 | ||
373 | return (((uint64_t)tbu)<<32) | (uint64_t)tbl; | |
374 | } | |
8230cf02 | 375 | #endif |
b534c7f9 MN |
376 | |
377 | #define START_TIMER \ | |
b534c7f9 | 378 | uint64_t tend;\ |
0775c88f | 379 | uint64_t tstart= read_time();\ |
b534c7f9 MN |
380 | |
381 | #define STOP_TIMER(id) \ | |
0775c88f | 382 | tend= read_time();\ |
d705e4a6 MN |
383 | {\ |
384 | static uint64_t tsum=0;\ | |
385 | static int tcount=0;\ | |
386 | static int tskip_count=0;\ | |
387 | if(tcount<2 || tend - tstart < 8*tsum/tcount){\ | |
388 | tsum+= tend - tstart;\ | |
389 | tcount++;\ | |
390 | }else\ | |
391 | tskip_count++;\ | |
8e06f20a | 392 | if(((tcount+tskip_count)&(tcount+tskip_count-1))==0){\ |
e852beee | 393 | av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\ |
d705e4a6 | 394 | }\ |
b534c7f9 | 395 | } |
0187e903 | 396 | #else |
115329f1 | 397 | #define START_TIMER |
0187e903 | 398 | #define STOP_TIMER(id) {} |
b534c7f9 MN |
399 | #endif |
400 | ||
cea8f6f3 | 401 | /* memory */ |
98145875 LB |
402 | |
403 | #ifdef __GNUC__ | |
404 | #define DECLARE_ALIGNED(n,t,v) t v __attribute__ ((aligned (n))) | |
405 | #else | |
406 | #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v | |
407 | #endif | |
408 | ||
79e47000 | 409 | /* memory */ |
cea8f6f3 LA |
410 | void *av_malloc(unsigned int size); |
411 | void *av_realloc(void *ptr, unsigned int size); | |
412 | void av_free(void *ptr); | |
413 | ||
79e47000 LB |
414 | void *av_mallocz(unsigned int size); |
415 | char *av_strdup(const char *s); | |
416 | void av_freep(void *ptr); | |
417 | ||
96707bb7 | 418 | #endif /* COMMON_H */ |