Statistics
| Branch: | Revision:

ffmpeg / libavutil / common.h @ 80a07f6e

History | View | Annotate | Download (9.25 KB)

1 04d7f601 Diego Biurrun
/*
2
 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3
 *
4 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7 04d7f601 Diego Biurrun
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
10 04d7f601 Diego Biurrun
 *
11 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
12 04d7f601 Diego Biurrun
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
18 04d7f601 Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21 983e3246 Michael Niedermayer
/**
22 bad5537e Diego Biurrun
 * @file libavutil/common.h
23 7ce68923 Diego Biurrun
 * common internal and external API header
24 983e3246 Michael Niedermayer
 */
25
26 98790382 Stefano Sabatini
#ifndef AVUTIL_COMMON_H
27
#define AVUTIL_COMMON_H
28 de6d9b64 Fabrice Bellard
29 8076c473 Diego Biurrun
#include <ctype.h>
30
#include <errno.h>
31 1845bf1f Måns Rullgård
#include <inttypes.h>
32 8076c473 Diego Biurrun
#include <limits.h>
33
#include <math.h>
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <string.h>
37 849f1035 Måns Rullgård
38 75af0749 Diego Biurrun
#ifdef HAVE_AV_CONFIG_H
39
#include "config.h"
40
#endif
41
42 03bc0f37 Måns Rullgård
#ifdef __GNUC__
43
#    define AV_GCC_VERSION_AT_LEAST(x,y) (__GNUC__ > x || __GNUC__ == x && __GNUC_MINOR__ >= y)
44
#else
45
#    define AV_GCC_VERSION_AT_LEAST(x,y) 0
46
#endif
47 52476c1b Carl Eugen Hoyos
48 849f1035 Måns Rullgård
#ifndef av_always_inline
49 52476c1b Carl Eugen Hoyos
#if AV_GCC_VERSION_AT_LEAST(3,1)
50 849f1035 Måns Rullgård
#    define av_always_inline __attribute__((always_inline)) inline
51
#else
52
#    define av_always_inline inline
53 b9c684a2 Aurelien Jacobs
#endif
54
#endif
55
56
#ifndef av_noinline
57 52476c1b Carl Eugen Hoyos
#if AV_GCC_VERSION_AT_LEAST(3,1)
58 b9c684a2 Aurelien Jacobs
#    define av_noinline __attribute__((noinline))
59
#else
60 410bf273 Michael Niedermayer
#    define av_noinline
61 849f1035 Måns Rullgård
#endif
62
#endif
63
64 85074d3c Zuxy Meng
#ifndef av_pure
65 52476c1b Carl Eugen Hoyos
#if AV_GCC_VERSION_AT_LEAST(3,1)
66 85074d3c Zuxy Meng
#    define av_pure __attribute__((pure))
67
#else
68
#    define av_pure
69
#endif
70
#endif
71
72
#ifndef av_const
73 52476c1b Carl Eugen Hoyos
#if AV_GCC_VERSION_AT_LEAST(2,6)
74 85074d3c Zuxy Meng
#    define av_const __attribute__((const))
75
#else
76
#    define av_const
77
#endif
78
#endif
79
80 98a6fff9 Zuxy Meng
#ifndef av_cold
81 06be9d9d Carl Eugen Hoyos
#if (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(4,3)
82 98a6fff9 Zuxy Meng
#    define av_cold __attribute__((cold))
83
#else
84
#    define av_cold
85
#endif
86
#endif
87
88 3b49f690 Michael Niedermayer
#ifndef av_flatten
89 a3a55b78 Carl Eugen Hoyos
#if (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(4,1)
90 3b49f690 Michael Niedermayer
#    define av_flatten __attribute__((flatten))
91
#else
92
#    define av_flatten
93
#endif
94
#endif
95
96 955ab9a4 Michael Niedermayer
#ifndef attribute_deprecated
97 52476c1b Carl Eugen Hoyos
#if AV_GCC_VERSION_AT_LEAST(3,1)
98 955ab9a4 Michael Niedermayer
#    define attribute_deprecated __attribute__((deprecated))
99
#else
100
#    define attribute_deprecated
101
#endif
102
#endif
103
104 154e30f6 Carl Eugen Hoyos
#ifndef av_unused
105
#if defined(__GNUC__)
106
#    define av_unused __attribute__((unused))
107
#else
108
#    define av_unused
109
#endif
110
#endif
111
112 51066987 Michael Niedermayer
#ifndef av_uninit
113 0dd89666 Carl Eugen Hoyos
#if defined(__GNUC__) && !defined(__ICC)
114 51066987 Michael Niedermayer
#    define av_uninit(x) x=x
115
#else
116
#    define av_uninit(x) x
117
#endif
118
#endif
119
120 544f5a92 Måns Rullgård
#ifdef HAVE_AV_CONFIG_H
121
#   include "intmath.h"
122
#endif
123
124 89c9ff50 Diego Biurrun
//rounded division & shift
125 10f3005f Michael Niedermayer
#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
126 d7e9533a Michael Niedermayer
/* assume b>0 */
127
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
128 c26abfa5 Diego Biurrun
#define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
129 02305ff3 Diego Biurrun
#define FFSIGN(a) ((a) > 0 ? 1 : -1)
130 75460b0c Michael Niedermayer
131 b8a78f41 Michael Niedermayer
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
132 159ef4b0 Vladimir Voroshilov
#define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
133 b8a78f41 Michael Niedermayer
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
134 b842ecbe Baptiste Coudurier
#define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
135 d7e9533a Michael Niedermayer
136 1345f4ed Diego Biurrun
#define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
137 ac809e81 Benoit Fouet
#define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
138 ef516f73 David Conrad
#define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1))
139 67eca72d Michael Niedermayer
140 de6d9b64 Fabrice Bellard
/* misc math functions */
141 a77caa4d Måns Rullgård
extern const uint8_t ff_log2_tab[256];
142 de6d9b64 Fabrice Bellard
143 91cc5d37 Francesco Lavra
extern const uint8_t av_reverse[256];
144
145 544f5a92 Måns Rullgård
#ifndef av_log2
146 85074d3c Zuxy Meng
static inline av_const int av_log2(unsigned int v)
147 de6d9b64 Fabrice Bellard
{
148 89ef2c29 Michael Niedermayer
    int n = 0;
149 de6d9b64 Fabrice Bellard
    if (v & 0xffff0000) {
150
        v >>= 16;
151
        n += 16;
152
    }
153
    if (v & 0xff00) {
154
        v >>= 8;
155
        n += 8;
156
    }
157 c81f0349 Michael Niedermayer
    n += ff_log2_tab[v];
158
159
    return n;
160
}
161 544f5a92 Måns Rullgård
#endif
162 c81f0349 Michael Niedermayer
163 544f5a92 Måns Rullgård
#ifndef av_log2_16bit
164 85074d3c Zuxy Meng
static inline av_const int av_log2_16bit(unsigned int v)
165 c81f0349 Michael Niedermayer
{
166 89ef2c29 Michael Niedermayer
    int n = 0;
167 c81f0349 Michael Niedermayer
    if (v & 0xff00) {
168
        v >>= 8;
169
        n += 8;
170 de6d9b64 Fabrice Bellard
    }
171 c81f0349 Michael Niedermayer
    n += ff_log2_tab[v];
172
173 de6d9b64 Fabrice Bellard
    return n;
174
}
175 544f5a92 Måns Rullgård
#endif
176 de6d9b64 Fabrice Bellard
177 77177335 Aurelien Jacobs
/**
178 89c9ff50 Diego Biurrun
 * Clips a signed integer value into the amin-amax range.
179 77177335 Aurelien Jacobs
 * @param a value to clip
180
 * @param amin minimum value of the clip range
181
 * @param amax maximum value of the clip range
182 c6c36725 Diego Biurrun
 * @return clipped value
183 77177335 Aurelien Jacobs
 */
184 85074d3c Zuxy Meng
static inline av_const int av_clip(int a, int amin, int amax)
185 91029be7 Michael Niedermayer
{
186 27af15dc Diego Biurrun
    if      (a < amin) return amin;
187 18769c0a Michael Niedermayer
    else if (a > amax) return amax;
188
    else               return a;
189 91029be7 Michael Niedermayer
}
190
191 77177335 Aurelien Jacobs
/**
192 89c9ff50 Diego Biurrun
 * Clips a signed integer value into the 0-255 range.
193 77177335 Aurelien Jacobs
 * @param a value to clip
194 c6c36725 Diego Biurrun
 * @return clipped value
195 77177335 Aurelien Jacobs
 */
196 85074d3c Zuxy Meng
static inline av_const uint8_t av_clip_uint8(int a)
197 3ebc7e04 Michael Niedermayer
{
198
    if (a&(~255)) return (-a)>>31;
199
    else          return a;
200
}
201
202 ddb8ebe7 Aurelien Jacobs
/**
203 f51fb686 Ramiro Polla
 * Clips a signed integer value into the 0-65535 range.
204
 * @param a value to clip
205
 * @return clipped value
206
 */
207
static inline av_const uint16_t av_clip_uint16(int a)
208
{
209
    if (a&(~65535)) return (-a)>>31;
210
    else            return a;
211
}
212
213
/**
214 89c9ff50 Diego Biurrun
 * Clips a signed integer value into the -32768,32767 range.
215 ddb8ebe7 Aurelien Jacobs
 * @param a value to clip
216
 * @return clipped value
217
 */
218 85074d3c Zuxy Meng
static inline av_const int16_t av_clip_int16(int a)
219 ddb8ebe7 Aurelien Jacobs
{
220
    if ((a+32768) & ~65535) return (a>>31) ^ 32767;
221
    else                    return a;
222
}
223
224 4138ad96 Vitor Sessak
/**
225 89c9ff50 Diego Biurrun
 * Clips a float value into the amin-amax range.
226 4138ad96 Vitor Sessak
 * @param a value to clip
227
 * @param amin minimum value of the clip range
228
 * @param amax maximum value of the clip range
229
 * @return clipped value
230
 */
231
static inline av_const float av_clipf(float a, float amin, float amax)
232
{
233
    if      (a < amin) return amin;
234
    else if (a > amax) return amax;
235
    else               return a;
236
}
237
238 c6dbbc45 Thilo Borgmann
/** Computes ceil(log2(x)).
239
 * @param x value used to compute ceil(log2(x))
240
 * @return computed ceiling of log2(x)
241
 */
242
static inline av_const int av_ceil_log2(int x)
243
{
244
    return av_log2((x - 1) << 1);
245
}
246
247 e8750b00 Fred Rothganger
#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
248
#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
249
250 d73427e3 Guillaume Poirier
/*!
251
 * \def GET_UTF8(val, GET_BYTE, ERROR)
252 89c9ff50 Diego Biurrun
 * Converts a UTF-8 character (up to 4 bytes long) to its 32-bit UCS-4 encoded form
253 d73427e3 Guillaume Poirier
 * \param val is the output and should be of type uint32_t. It holds the converted
254 90b5b51e Diego Biurrun
 * UCS-4 character and should be a left value.
255
 * \param GET_BYTE gets UTF-8 encoded bytes from any proper source. It can be
256 d73427e3 Guillaume Poirier
 * a function or a statement whose return value or evaluated value is of type
257 90b5b51e Diego Biurrun
 * uint8_t. It will be executed up to 4 times for values in the valid UTF-8 range,
258 40a08c7e Reimar Döffinger
 * and up to 7 times in the general case.
259 90b5b51e Diego Biurrun
 * \param ERROR action that should be taken when an invalid UTF-8 byte is returned
260 d73427e3 Guillaume Poirier
 * from GET_BYTE. It should be a statement that jumps out of the macro,
261
 * like exit(), goto, return, break, or continue.
262
 */
263 9d82b0dd Michael Niedermayer
#define GET_UTF8(val, GET_BYTE, ERROR)\
264
    val= GET_BYTE;\
265
    {\
266
        int ones= 7 - av_log2(val ^ 255);\
267
        if(ones==1)\
268
            ERROR\
269
        val&= 127>>ones;\
270
        while(--ones > 0){\
271
            int tmp= GET_BYTE - 128;\
272
            if(tmp>>6)\
273
                ERROR\
274
            val= (val<<6) + tmp;\
275
        }\
276
    }
277 2ad1516a Michael Niedermayer
278 0e8c148b Guillaume Poirier
/*!
279 20c68378 Anton Khirnov
 * \def GET_UTF16(val, GET_16BIT, ERROR)
280
 * Converts a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form
281
 * \param val is the output and should be of type uint32_t. It holds the converted
282
 * UCS-4 character and should be a left value.
283
 * \param GET_16BIT gets two bytes of UTF-16 encoded data converted to native endianness.
284
 * It can be a function or a statement whose return value or evaluated value is of type
285
 * uint16_t. It will be executed up to 2 times.
286
 * \param ERROR action that should be taken when an invalid UTF-16 surrogate is
287
 * returned from GET_BYTE. It should be a statement that jumps out of the macro,
288
 * like exit(), goto, return, break, or continue.
289
 */
290
#define GET_UTF16(val, GET_16BIT, ERROR)\
291
    val = GET_16BIT;\
292
    {\
293
        unsigned int hi = val - 0xD800;\
294
        if (hi < 0x800) {\
295
            val = GET_16BIT - 0xDC00;\
296
            if (val > 0x3FFU || hi > 0x3FFU)\
297
                ERROR\
298
            val += (hi<<10) + 0x10000;\
299
        }\
300
    }\
301
302
/*!
303 34d33769 Guillaume Poirier
 * \def PUT_UTF8(val, tmp, PUT_BYTE)
304 89c9ff50 Diego Biurrun
 * Converts a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
305
 * \param val is an input-only argument and should be of type uint32_t. It holds
306
 * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If
307
 * val is given as a function it is executed only once.
308 0e8c148b Guillaume Poirier
 * \param tmp is a temporary variable and should be of type uint8_t. It
309
 * represents an intermediate value during conversion that is to be
310 89c9ff50 Diego Biurrun
 * output by PUT_BYTE.
311 90b5b51e Diego Biurrun
 * \param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
312 0e8c148b Guillaume Poirier
 * It could be a function or a statement, and uses tmp as the input byte.
313
 * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
314 90b5b51e Diego Biurrun
 * executed up to 4 times for values in the valid UTF-8 range and up to
315 40a08c7e Reimar Döffinger
 * 7 times in the general case, depending on the length of the converted
316 89c9ff50 Diego Biurrun
 * Unicode character.
317 0e8c148b Guillaume Poirier
 */
318 360932f7 Zuxy Meng
#define PUT_UTF8(val, tmp, PUT_BYTE)\
319
    {\
320
        int bytes, shift;\
321
        uint32_t in = val;\
322
        if (in < 0x80) {\
323
            tmp = in;\
324
            PUT_BYTE\
325
        } else {\
326
            bytes = (av_log2(in) + 4) / 5;\
327
            shift = (bytes - 1) * 6;\
328
            tmp = (256 - (256 >> bytes)) | (in >> shift);\
329
            PUT_BYTE\
330
            while (shift >= 6) {\
331
                shift -= 6;\
332
                tmp = 0x80 | ((in >> shift) & 0x3f);\
333
                PUT_BYTE\
334
            }\
335
        }\
336
    }
337
338 8076c473 Diego Biurrun
#include "mem.h"
339
340 6b8b7258 Diego Biurrun
#ifdef HAVE_AV_CONFIG_H
341
#    include "internal.h"
342
#endif /* HAVE_AV_CONFIG_H */
343
344 98790382 Stefano Sabatini
#endif /* AVUTIL_COMMON_H */