808
|
1 /*
|
|
2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
|
|
3 *
|
|
4 * This file is part of FFmpeg.
|
|
5 *
|
|
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.
|
|
10 *
|
|
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.
|
|
15 *
|
|
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
|
|
19 */
|
|
20
|
|
21 /**
|
|
22 * @file common.h
|
|
23 * common internal and external api header.
|
|
24 */
|
|
25
|
|
26 #ifndef COMMON_H
|
|
27 #define COMMON_H
|
|
28
|
|
29 #ifndef M_PI
|
|
30 #define M_PI 3.14159265358979323846
|
|
31 #endif
|
|
32
|
|
33 #ifdef HAVE_AV_CONFIG_H
|
|
34 /* only include the following when compiling package */
|
|
35 # include "config.h"
|
|
36
|
|
37 # include <stdlib.h>
|
|
38 # include <stdio.h>
|
|
39 # include <string.h>
|
|
40 # include <ctype.h>
|
|
41 # include <limits.h>
|
|
42 # ifndef __BEOS__
|
|
43 # include <errno.h>
|
|
44 # else
|
|
45 # include "berrno.h"
|
|
46 # endif
|
|
47 # include <math.h>
|
|
48 #endif /* HAVE_AV_CONFIG_H */
|
|
49
|
|
50 /* Suppress restrict if it was not defined in config.h. */
|
|
51 #ifndef restrict
|
|
52 # define restrict
|
|
53 #endif
|
|
54
|
|
55 #ifndef always_inline
|
|
56 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
|
|
57 # define always_inline __attribute__((always_inline)) inline
|
|
58 #else
|
|
59 # define always_inline inline
|
|
60 #endif
|
|
61 #endif
|
|
62
|
|
63 #ifndef attribute_used
|
|
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
|
|
69 #endif
|
|
70
|
|
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
|
|
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
|
|
87 #ifndef EMULATE_INTTYPES
|
|
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;
|
|
96 typedef signed long long int64_t;
|
|
97 typedef unsigned long long uint64_t;
|
|
98 #endif /* EMULATE_INTTYPES */
|
|
99
|
|
100 #ifndef PRId64
|
|
101 #define PRId64 "lld"
|
|
102 #endif
|
|
103
|
|
104 #ifndef PRIu64
|
|
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
|
|
122 #endif
|
|
123
|
|
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
|
|
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
|
|
144 #ifndef INT64_MIN
|
|
145 #define INT64_MIN (-0x7fffffffffffffffLL-1)
|
|
146 #endif
|
|
147
|
|
148 #ifndef INT64_MAX
|
|
149 #define INT64_MAX int64_t_C(9223372036854775807)
|
|
150 #endif
|
|
151
|
|
152 #ifndef UINT64_MAX
|
|
153 #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
|
|
154 #endif
|
|
155
|
|
156 #ifdef EMULATE_FAST_INT
|
|
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;
|
|
163 typedef uint64_t uint_fast64_t;
|
|
164 #endif
|
|
165
|
|
166 #ifndef INT_BIT
|
|
167 # if INT_MAX != 2147483647
|
|
168 # define INT_BIT 64
|
|
169 # else
|
|
170 # define INT_BIT 32
|
|
171 # endif
|
|
172 #endif
|
|
173
|
|
174 #ifndef int64_t_C
|
|
175 #define int64_t_C(c) (c ## LL)
|
|
176 #define uint64_t_C(c) (c ## ULL)
|
|
177 #endif
|
|
178
|
|
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
|
|
186 #ifdef HAVE_AV_CONFIG_H
|
|
187 /* only include the following when compiling package */
|
|
188 # include "internal.h"
|
|
189 #endif
|
|
190
|
|
191 //rounded divison & shift
|
|
192 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
|
|
193 /* assume b>0 */
|
|
194 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
|
|
195 #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
|
|
196 #define FFSIGN(a) ((a) > 0 ? 1 : -1)
|
|
197
|
|
198 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
|
|
199 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
|
|
200
|
818
|
201 #undef SWAP
|
808
|
202 #define SWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
|
|
203
|
|
204 /* misc math functions */
|
|
205 extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
|
|
206
|
|
207 static inline int av_log2(unsigned int v)
|
|
208 {
|
|
209 int n;
|
|
210
|
|
211 n = 0;
|
|
212 if (v & 0xffff0000) {
|
|
213 v >>= 16;
|
|
214 n += 16;
|
|
215 }
|
|
216 if (v & 0xff00) {
|
|
217 v >>= 8;
|
|
218 n += 8;
|
|
219 }
|
|
220 n += ff_log2_tab[v];
|
|
221
|
|
222 return n;
|
|
223 }
|
|
224
|
|
225 static inline int av_log2_16bit(unsigned int v)
|
|
226 {
|
|
227 int n;
|
|
228
|
|
229 n = 0;
|
|
230 if (v & 0xff00) {
|
|
231 v >>= 8;
|
|
232 n += 8;
|
|
233 }
|
|
234 n += ff_log2_tab[v];
|
|
235
|
|
236 return n;
|
|
237 }
|
|
238
|
|
239 /* median of 3 */
|
|
240 static inline int mid_pred(int a, int b, int c)
|
|
241 {
|
|
242 #if HAVE_CMOV
|
|
243 int i=b;
|
|
244 asm volatile(
|
|
245 "cmp %2, %1 \n\t"
|
|
246 "cmovg %1, %0 \n\t"
|
|
247 "cmovg %2, %1 \n\t"
|
|
248 "cmp %3, %1 \n\t"
|
|
249 "cmovl %3, %1 \n\t"
|
|
250 "cmp %1, %0 \n\t"
|
|
251 "cmovg %1, %0 \n\t"
|
|
252 :"+&r"(i), "+&r"(a)
|
|
253 :"r"(b), "r"(c)
|
|
254 );
|
|
255 return i;
|
|
256 #elif 0
|
|
257 int t= (a-b)&((a-b)>>31);
|
|
258 a-=t;
|
|
259 b+=t;
|
|
260 b-= (b-c)&((b-c)>>31);
|
|
261 b+= (a-b)&((a-b)>>31);
|
|
262
|
|
263 return b;
|
|
264 #else
|
|
265 if(a>b){
|
|
266 if(c>b){
|
|
267 if(c>a) b=a;
|
|
268 else b=c;
|
|
269 }
|
|
270 }else{
|
|
271 if(b>c){
|
|
272 if(c>a) b=c;
|
|
273 else b=a;
|
|
274 }
|
|
275 }
|
|
276 return b;
|
|
277 #endif
|
|
278 }
|
|
279
|
|
280 /**
|
|
281 * clip a signed integer value into the amin-amax range
|
|
282 * @param a value to clip
|
|
283 * @param amin minimum value of the clip range
|
|
284 * @param amax maximum value of the clip range
|
|
285 * @return cliped value
|
|
286 */
|
|
287 static inline int clip(int a, int amin, int amax)
|
|
288 {
|
|
289 if (a < amin) return amin;
|
|
290 else if (a > amax) return amax;
|
|
291 else return a;
|
|
292 }
|
|
293
|
|
294 /**
|
|
295 * clip a signed integer value into the 0-255 range
|
|
296 * @param a value to clip
|
|
297 * @return cliped value
|
|
298 */
|
|
299 static inline uint8_t clip_uint8(int a)
|
|
300 {
|
|
301 if (a&(~255)) return (-a)>>31;
|
|
302 else return a;
|
|
303 }
|
|
304
|
|
305 /* math */
|
|
306 int64_t ff_gcd(int64_t a, int64_t b);
|
|
307
|
|
308 /**
|
|
309 * converts fourcc string to int
|
|
310 */
|
|
311 static inline int ff_get_fourcc(const char *s){
|
|
312 #ifdef HAVE_AV_CONFIG_H
|
|
313 assert( strlen(s)==4 );
|
|
314 #endif
|
|
315
|
|
316 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
|
|
317 }
|
|
318
|
|
319 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
|
|
320 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
|
|
321
|
|
322
|
|
323 #define GET_UTF8(val, GET_BYTE, ERROR)\
|
|
324 val= GET_BYTE;\
|
|
325 {\
|
|
326 int ones= 7 - av_log2(val ^ 255);\
|
|
327 if(ones==1)\
|
|
328 ERROR\
|
|
329 val&= 127>>ones;\
|
|
330 while(--ones > 0){\
|
|
331 int tmp= GET_BYTE - 128;\
|
|
332 if(tmp>>6)\
|
|
333 ERROR\
|
|
334 val= (val<<6) + tmp;\
|
|
335 }\
|
|
336 }
|
|
337
|
|
338 #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
|
|
339 #if defined(ARCH_X86_64)
|
|
340 static inline uint64_t read_time(void)
|
|
341 {
|
|
342 uint64_t a, d;
|
|
343 asm volatile( "rdtsc\n\t"
|
|
344 : "=a" (a), "=d" (d)
|
|
345 );
|
|
346 return (d << 32) | (a & 0xffffffff);
|
|
347 }
|
|
348 #elif defined(ARCH_X86)
|
|
349 static inline long long read_time(void)
|
|
350 {
|
|
351 long long l;
|
|
352 asm volatile( "rdtsc\n\t"
|
|
353 : "=A" (l)
|
|
354 );
|
|
355 return l;
|
|
356 }
|
|
357 #else //FIXME check ppc64
|
|
358 static inline uint64_t read_time(void)
|
|
359 {
|
|
360 uint32_t tbu, tbl, temp;
|
|
361
|
|
362 /* from section 2.2.1 of the 32-bit PowerPC PEM */
|
|
363 __asm__ __volatile__(
|
|
364 "1:\n"
|
|
365 "mftbu %2\n"
|
|
366 "mftb %0\n"
|
|
367 "mftbu %1\n"
|
|
368 "cmpw %2,%1\n"
|
|
369 "bne 1b\n"
|
|
370 : "=r"(tbl), "=r"(tbu), "=r"(temp)
|
|
371 :
|
|
372 : "cc");
|
|
373
|
|
374 return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
|
|
375 }
|
|
376 #endif
|
|
377
|
|
378 #define START_TIMER \
|
|
379 uint64_t tend;\
|
|
380 uint64_t tstart= read_time();\
|
|
381
|
|
382 #define STOP_TIMER(id) \
|
|
383 tend= read_time();\
|
|
384 {\
|
|
385 static uint64_t tsum=0;\
|
|
386 static int tcount=0;\
|
|
387 static int tskip_count=0;\
|
|
388 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
|
|
389 tsum+= tend - tstart;\
|
|
390 tcount++;\
|
|
391 }else\
|
|
392 tskip_count++;\
|
|
393 if(((tcount+tskip_count)&(tcount+tskip_count-1))==0){\
|
|
394 av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
|
|
395 }\
|
|
396 }
|
|
397 #else
|
|
398 #define START_TIMER
|
|
399 #define STOP_TIMER(id) {}
|
|
400 #endif
|
|
401
|
|
402 /* memory */
|
|
403
|
|
404 #ifdef __GNUC__
|
|
405 #define DECLARE_ALIGNED(n,t,v) t v __attribute__ ((aligned (n)))
|
|
406 #else
|
|
407 #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
|
|
408 #endif
|
|
409
|
|
410 /* memory */
|
|
411 void *av_malloc(unsigned int size);
|
|
412 void *av_realloc(void *ptr, unsigned int size);
|
|
413 void av_free(void *ptr);
|
|
414
|
|
415 void *av_mallocz(unsigned int size);
|
|
416 char *av_strdup(const char *s);
|
|
417 void av_freep(void *ptr);
|
|
418
|
|
419 #endif /* COMMON_H */
|