]> git.sesse.net Git - ffmpeg/blob - libavutil/common.h
Correct GET/PUT_UTF8 comment: the get/put functions might be called up
[ffmpeg] / libavutil / common.h
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 #   include <inttypes.h>
88
89 #ifndef PRId64
90 #define PRId64 "lld"
91 #endif
92
93 #ifndef PRIu64
94 #define PRIu64 "llu"
95 #endif
96
97 #ifndef PRIx64
98 #define PRIx64 "llx"
99 #endif
100
101 #ifndef PRIX64
102 #define PRIX64 "llX"
103 #endif
104
105 #ifndef PRId32
106 #define PRId32 "d"
107 #endif
108
109 #ifndef PRIdFAST16
110 #define PRIdFAST16 PRId32
111 #endif
112
113 #ifndef PRIdFAST32
114 #define PRIdFAST32 PRId32
115 #endif
116
117 #ifndef INT16_MIN
118 #define INT16_MIN       (-0x7fff-1)
119 #endif
120
121 #ifndef INT16_MAX
122 #define INT16_MAX       0x7fff
123 #endif
124
125 #ifndef INT32_MIN
126 #define INT32_MIN       (-0x7fffffff-1)
127 #endif
128
129 #ifndef INT32_MAX
130 #define INT32_MAX       0x7fffffff
131 #endif
132
133 #ifndef UINT32_MAX
134 #define UINT32_MAX      0xffffffff
135 #endif
136
137 #ifndef INT64_MIN
138 #define INT64_MIN       (-0x7fffffffffffffffLL-1)
139 #endif
140
141 #ifndef INT64_MAX
142 #define INT64_MAX int64_t_C(9223372036854775807)
143 #endif
144
145 #ifndef UINT64_MAX
146 #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
147 #endif
148
149 #ifndef INT_BIT
150 #    if INT_MAX != 2147483647
151 #        define INT_BIT 64
152 #    else
153 #        define INT_BIT 32
154 #    endif
155 #endif
156
157 #ifndef int64_t_C
158 #define int64_t_C(c)     (c ## LL)
159 #define uint64_t_C(c)    (c ## ULL)
160 #endif
161
162 #if defined(__MINGW32__) && !defined(BUILD_AVUTIL) && defined(BUILD_SHARED_AV)
163 #  define FF_IMPORT_ATTR __declspec(dllimport)
164 #else
165 #  define FF_IMPORT_ATTR
166 #endif
167
168
169 #ifdef HAVE_AV_CONFIG_H
170 /* only include the following when compiling package */
171 #    include "internal.h"
172 #endif
173
174 //rounded divison & shift
175 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
176 /* assume b>0 */
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)
180
181 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
182 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
183
184 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
185
186 /* misc math functions */
187 extern FF_IMPORT_ATTR const uint8_t ff_log2_tab[256];
188
189 static inline int av_log2(unsigned int v)
190 {
191     int n;
192
193     n = 0;
194     if (v & 0xffff0000) {
195         v >>= 16;
196         n += 16;
197     }
198     if (v & 0xff00) {
199         v >>= 8;
200         n += 8;
201     }
202     n += ff_log2_tab[v];
203
204     return n;
205 }
206
207 static inline int av_log2_16bit(unsigned int v)
208 {
209     int n;
210
211     n = 0;
212     if (v & 0xff00) {
213         v >>= 8;
214         n += 8;
215     }
216     n += ff_log2_tab[v];
217
218     return n;
219 }
220
221 /* median of 3 */
222 static inline int mid_pred(int a, int b, int c)
223 {
224 #if HAVE_CMOV
225     int i=b;
226     asm volatile(
227         "cmp    %2, %1 \n\t"
228         "cmovg  %1, %0 \n\t"
229         "cmovg  %2, %1 \n\t"
230         "cmp    %3, %1 \n\t"
231         "cmovl  %3, %1 \n\t"
232         "cmp    %1, %0 \n\t"
233         "cmovg  %1, %0 \n\t"
234         :"+&r"(i), "+&r"(a)
235         :"r"(b), "r"(c)
236     );
237     return i;
238 #elif 0
239     int t= (a-b)&((a-b)>>31);
240     a-=t;
241     b+=t;
242     b-= (b-c)&((b-c)>>31);
243     b+= (a-b)&((a-b)>>31);
244
245     return b;
246 #else
247     if(a>b){
248         if(c>b){
249             if(c>a) b=a;
250             else    b=c;
251         }
252     }else{
253         if(b>c){
254             if(c>a) b=c;
255             else    b=a;
256         }
257     }
258     return b;
259 #endif
260 }
261
262 /**
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
268  */
269 static inline int clip(int a, int amin, int amax)
270 {
271     if (a < amin)      return amin;
272     else if (a > amax) return amax;
273     else               return a;
274 }
275
276 /**
277  * clip a signed integer value into the 0-255 range
278  * @param a value to clip
279  * @return cliped value
280  */
281 static inline uint8_t clip_uint8(int a)
282 {
283     if (a&(~255)) return (-a)>>31;
284     else          return a;
285 }
286
287 /* math */
288 int64_t ff_gcd(int64_t a, int64_t b);
289
290 /**
291  * converts fourcc string to int
292  */
293 static inline int ff_get_fourcc(const char *s){
294 #ifdef HAVE_AV_CONFIG_H
295     assert( strlen(s)==4 );
296 #endif
297
298     return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
299 }
300
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))
303
304 /*!
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 executed up to 4 times for values in the valid utf-8 range,
312  * and up to 7 times in the general case.
313  * \param ERROR action that should be taken when an invalid utf-8 byte is returned
314  * from GET_BYTE. It should be a statement that jumps out of the macro,
315  * like exit(), goto, return, break, or continue.
316  */
317 #define GET_UTF8(val, GET_BYTE, ERROR)\
318     val= GET_BYTE;\
319     {\
320         int ones= 7 - av_log2(val ^ 255);\
321         if(ones==1)\
322             ERROR\
323         val&= 127>>ones;\
324         while(--ones > 0){\
325             int tmp= GET_BYTE - 128;\
326             if(tmp>>6)\
327                 ERROR\
328             val= (val<<6) + tmp;\
329         }\
330     }
331
332 /*!
333  * \def PUT_UTF8(val, tmp, PUT_BYTE)
334  * converts a 32-bit unicode character to its utf-8 encoded form (up to 4 bytes long).
335  * \param val is an input only argument and should be of type uint32_t. It holds
336  * a ucs4 encoded unicode character that is to be converted to utf-8. If
337  * val is given as a function it's executed only once.
338  * \param tmp is a temporary variable and should be of type uint8_t. It
339  * represents an intermediate value during conversion that is to be
340  * outputted by PUT_BYTE.
341  * \param PUT_BYTE writes the converted utf-8 bytes to any proper destination.
342  * It could be a function or a statement, and uses tmp as the input byte.
343  * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
344  * executed up to 4 times for values in the valid utf-8 range and up to
345  * 7 times in the general case, depending on the length of the converted
346  * unicode character.
347  */
348 #define PUT_UTF8(val, tmp, PUT_BYTE)\
349     {\
350         int bytes, shift;\
351         uint32_t in = val;\
352         if (in < 0x80) {\
353             tmp = in;\
354             PUT_BYTE\
355         } else {\
356             bytes = (av_log2(in) + 4) / 5;\
357             shift = (bytes - 1) * 6;\
358             tmp = (256 - (256 >> bytes)) | (in >> shift);\
359             PUT_BYTE\
360             while (shift >= 6) {\
361                 shift -= 6;\
362                 tmp = 0x80 | ((in >> shift) & 0x3f);\
363                 PUT_BYTE\
364             }\
365         }\
366     }
367
368 #if defined(ARCH_X86) || defined(ARCH_POWERPC)
369 #if defined(ARCH_X86_64)
370 static inline uint64_t read_time(void)
371 {
372         uint64_t a, d;
373         asm volatile(   "rdtsc\n\t"
374                 : "=a" (a), "=d" (d)
375         );
376         return (d << 32) | (a & 0xffffffff);
377 }
378 #elif defined(ARCH_X86_32)
379 static inline long long read_time(void)
380 {
381         long long l;
382         asm volatile(   "rdtsc\n\t"
383                 : "=A" (l)
384         );
385         return l;
386 }
387 #else //FIXME check ppc64
388 static inline uint64_t read_time(void)
389 {
390     uint32_t tbu, tbl, temp;
391
392      /* from section 2.2.1 of the 32-bit PowerPC PEM */
393      __asm__ __volatile__(
394          "1:\n"
395          "mftbu  %2\n"
396          "mftb   %0\n"
397          "mftbu  %1\n"
398          "cmpw   %2,%1\n"
399          "bne    1b\n"
400      : "=r"(tbl), "=r"(tbu), "=r"(temp)
401      :
402      : "cc");
403
404      return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
405 }
406 #endif
407
408 #define START_TIMER \
409 uint64_t tend;\
410 uint64_t tstart= read_time();\
411
412 #define STOP_TIMER(id) \
413 tend= read_time();\
414 {\
415   static uint64_t tsum=0;\
416   static int tcount=0;\
417   static int tskip_count=0;\
418   if(tcount<2 || tend - tstart < 8*tsum/tcount){\
419       tsum+= tend - tstart;\
420       tcount++;\
421   }else\
422       tskip_count++;\
423   if(((tcount+tskip_count)&(tcount+tskip_count-1))==0){\
424       av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
425   }\
426 }
427 #else
428 #define START_TIMER
429 #define STOP_TIMER(id) {}
430 #endif
431
432 /* memory */
433
434 #ifdef __GNUC__
435   #define DECLARE_ALIGNED(n,t,v)       t v __attribute__ ((aligned (n)))
436 #else
437   #define DECLARE_ALIGNED(n,t,v)      __declspec(align(n)) t v
438 #endif
439
440 /* memory */
441 void *av_malloc(unsigned int size);
442 void *av_realloc(void *ptr, unsigned int size);
443 void av_free(void *ptr);
444
445 void *av_mallocz(unsigned int size);
446 char *av_strdup(const char *s);
447 void av_freep(void *ptr);
448
449 #endif /* COMMON_H */