Statistics
| Branch: | Revision:

ffmpeg / libavutil / common.h @ 0dd89666

History | View | Annotate | Download (7.53 KB)

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 libavutil/common.h
23
 * common internal and external API header
24
 */
25

    
26
#ifndef AVUTIL_COMMON_H
27
#define AVUTIL_COMMON_H
28

    
29
#include <ctype.h>
30
#include <errno.h>
31
#include <inttypes.h>
32
#include <limits.h>
33
#include <math.h>
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <string.h>
37

    
38
#define AV_GCC_VERSION_AT_LEAST(x,y) (defined(__GNUC__) && (__GNUC__ > x || __GNUC__ == x && __GNUC_MINOR__ >= y))
39

    
40
#ifndef av_always_inline
41
#if AV_GCC_VERSION_AT_LEAST(3,1)
42
#    define av_always_inline __attribute__((always_inline)) inline
43
#else
44
#    define av_always_inline inline
45
#endif
46
#endif
47

    
48
#ifndef av_noinline
49
#if AV_GCC_VERSION_AT_LEAST(3,1)
50
#    define av_noinline __attribute__((noinline))
51
#else
52
#    define av_noinline
53
#endif
54
#endif
55

    
56
#ifndef av_pure
57
#if AV_GCC_VERSION_AT_LEAST(3,1)
58
#    define av_pure __attribute__((pure))
59
#else
60
#    define av_pure
61
#endif
62
#endif
63

    
64
#ifndef av_const
65
#if AV_GCC_VERSION_AT_LEAST(2,6)
66
#    define av_const __attribute__((const))
67
#else
68
#    define av_const
69
#endif
70
#endif
71

    
72
#ifndef av_cold
73
#if (!defined(__ICC) || __ICC > 1100) && AV_GCC_VERSION_AT_LEAST(4,3)
74
#    define av_cold __attribute__((cold))
75
#else
76
#    define av_cold
77
#endif
78
#endif
79

    
80
#ifndef av_flatten
81
#if AV_GCC_VERSION_AT_LEAST(4,1)
82
#    define av_flatten __attribute__((flatten))
83
#else
84
#    define av_flatten
85
#endif
86
#endif
87

    
88
#ifndef attribute_deprecated
89
#if AV_GCC_VERSION_AT_LEAST(3,1)
90
#    define attribute_deprecated __attribute__((deprecated))
91
#else
92
#    define attribute_deprecated
93
#endif
94
#endif
95

    
96
#ifndef av_unused
97
#if defined(__GNUC__)
98
#    define av_unused __attribute__((unused))
99
#else
100
#    define av_unused
101
#endif
102
#endif
103

    
104
#ifndef av_uninit
105
#if defined(__GNUC__) && !defined(__ICC)
106
#    define av_uninit(x) x=x
107
#else
108
#    define av_uninit(x) x
109
#endif
110
#endif
111

    
112
//rounded division & shift
113
#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
114
/* assume b>0 */
115
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
116
#define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
117
#define FFSIGN(a) ((a) > 0 ? 1 : -1)
118

    
119
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
120
#define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
121
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
122
#define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
123

    
124
#define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
125
#define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
126

    
127
/* misc math functions */
128
extern const uint8_t ff_log2_tab[256];
129

    
130
static inline av_const int av_log2(unsigned int v)
131
{
132
    int n = 0;
133
    if (v & 0xffff0000) {
134
        v >>= 16;
135
        n += 16;
136
    }
137
    if (v & 0xff00) {
138
        v >>= 8;
139
        n += 8;
140
    }
141
    n += ff_log2_tab[v];
142

    
143
    return n;
144
}
145

    
146
static inline av_const int av_log2_16bit(unsigned int v)
147
{
148
    int n = 0;
149
    if (v & 0xff00) {
150
        v >>= 8;
151
        n += 8;
152
    }
153
    n += ff_log2_tab[v];
154

    
155
    return n;
156
}
157

    
158
/**
159
 * Clips a signed integer value into the amin-amax range.
160
 * @param a value to clip
161
 * @param amin minimum value of the clip range
162
 * @param amax maximum value of the clip range
163
 * @return clipped value
164
 */
165
static inline av_const int av_clip(int a, int amin, int amax)
166
{
167
    if      (a < amin) return amin;
168
    else if (a > amax) return amax;
169
    else               return a;
170
}
171

    
172
/**
173
 * Clips a signed integer value into the 0-255 range.
174
 * @param a value to clip
175
 * @return clipped value
176
 */
177
static inline av_const uint8_t av_clip_uint8(int a)
178
{
179
    if (a&(~255)) return (-a)>>31;
180
    else          return a;
181
}
182

    
183
/**
184
 * Clips a signed integer value into the -32768,32767 range.
185
 * @param a value to clip
186
 * @return clipped value
187
 */
188
static inline av_const int16_t av_clip_int16(int a)
189
{
190
    if ((a+32768) & ~65535) return (a>>31) ^ 32767;
191
    else                    return a;
192
}
193

    
194
/**
195
 * Clips a float value into the amin-amax range.
196
 * @param a value to clip
197
 * @param amin minimum value of the clip range
198
 * @param amax maximum value of the clip range
199
 * @return clipped value
200
 */
201
static inline av_const float av_clipf(float a, float amin, float amax)
202
{
203
    if      (a < amin) return amin;
204
    else if (a > amax) return amax;
205
    else               return a;
206
}
207

    
208
#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
209
#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
210

    
211
/*!
212
 * \def GET_UTF8(val, GET_BYTE, ERROR)
213
 * Converts a UTF-8 character (up to 4 bytes long) to its 32-bit UCS-4 encoded form
214
 * \param val is the output and should be of type uint32_t. It holds the converted
215
 * UCS-4 character and should be a left value.
216
 * \param GET_BYTE gets UTF-8 encoded bytes from any proper source. It can be
217
 * a function or a statement whose return value or evaluated value is of type
218
 * uint8_t. It will be executed up to 4 times for values in the valid UTF-8 range,
219
 * and up to 7 times in the general case.
220
 * \param ERROR action that should be taken when an invalid UTF-8 byte is returned
221
 * from GET_BYTE. It should be a statement that jumps out of the macro,
222
 * like exit(), goto, return, break, or continue.
223
 */
224
#define GET_UTF8(val, GET_BYTE, ERROR)\
225
    val= GET_BYTE;\
226
    {\
227
        int ones= 7 - av_log2(val ^ 255);\
228
        if(ones==1)\
229
            ERROR\
230
        val&= 127>>ones;\
231
        while(--ones > 0){\
232
            int tmp= GET_BYTE - 128;\
233
            if(tmp>>6)\
234
                ERROR\
235
            val= (val<<6) + tmp;\
236
        }\
237
    }
238

    
239
/*!
240
 * \def PUT_UTF8(val, tmp, PUT_BYTE)
241
 * Converts a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
242
 * \param val is an input-only argument and should be of type uint32_t. It holds
243
 * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If
244
 * val is given as a function it is executed only once.
245
 * \param tmp is a temporary variable and should be of type uint8_t. It
246
 * represents an intermediate value during conversion that is to be
247
 * output by PUT_BYTE.
248
 * \param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
249
 * It could be a function or a statement, and uses tmp as the input byte.
250
 * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
251
 * executed up to 4 times for values in the valid UTF-8 range and up to
252
 * 7 times in the general case, depending on the length of the converted
253
 * Unicode character.
254
 */
255
#define PUT_UTF8(val, tmp, PUT_BYTE)\
256
    {\
257
        int bytes, shift;\
258
        uint32_t in = val;\
259
        if (in < 0x80) {\
260
            tmp = in;\
261
            PUT_BYTE\
262
        } else {\
263
            bytes = (av_log2(in) + 4) / 5;\
264
            shift = (bytes - 1) * 6;\
265
            tmp = (256 - (256 >> bytes)) | (in >> shift);\
266
            PUT_BYTE\
267
            while (shift >= 6) {\
268
                shift -= 6;\
269
                tmp = 0x80 | ((in >> shift) & 0x3f);\
270
                PUT_BYTE\
271
            }\
272
        }\
273
    }
274

    
275
#include "mem.h"
276

    
277
#ifdef HAVE_AV_CONFIG_H
278
#    include "config.h"
279
#    include "internal.h"
280
#endif /* HAVE_AV_CONFIG_H */
281

    
282
#endif /* AVUTIL_COMMON_H */