Statistics
| Branch: | Revision:

ffmpeg / libavutil / common.h @ 75af0749

History | View | Annotate | Download (7.69 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
#ifdef HAVE_AV_CONFIG_H
39
#include "config.h"
40
#endif
41

    
42
#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

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

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

    
64
#ifndef av_pure
65
#if AV_GCC_VERSION_AT_LEAST(3,1)
66
#    define av_pure __attribute__((pure))
67
#else
68
#    define av_pure
69
#endif
70
#endif
71

    
72
#ifndef av_const
73
#if AV_GCC_VERSION_AT_LEAST(2,6)
74
#    define av_const __attribute__((const))
75
#else
76
#    define av_const
77
#endif
78
#endif
79

    
80
#ifndef av_cold
81
#if (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(4,3)
82
#    define av_cold __attribute__((cold))
83
#else
84
#    define av_cold
85
#endif
86
#endif
87

    
88
#ifndef av_flatten
89
#if (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(4,1)
90
#    define av_flatten __attribute__((flatten))
91
#else
92
#    define av_flatten
93
#endif
94
#endif
95

    
96
#ifndef attribute_deprecated
97
#if AV_GCC_VERSION_AT_LEAST(3,1)
98
#    define attribute_deprecated __attribute__((deprecated))
99
#else
100
#    define attribute_deprecated
101
#endif
102
#endif
103

    
104
#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
#ifndef av_uninit
113
#if defined(__GNUC__) && !defined(__ICC)
114
#    define av_uninit(x) x=x
115
#else
116
#    define av_uninit(x) x
117
#endif
118
#endif
119

    
120
//rounded division & shift
121
#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
122
/* assume b>0 */
123
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
124
#define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
125
#define FFSIGN(a) ((a) > 0 ? 1 : -1)
126

    
127
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
128
#define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
129
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
130
#define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
131

    
132
#define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
133
#define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
134
#define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1))
135

    
136
/* misc math functions */
137
extern const uint8_t ff_log2_tab[256];
138

    
139
static inline av_const int av_log2(unsigned int v)
140
{
141
    int n = 0;
142
    if (v & 0xffff0000) {
143
        v >>= 16;
144
        n += 16;
145
    }
146
    if (v & 0xff00) {
147
        v >>= 8;
148
        n += 8;
149
    }
150
    n += ff_log2_tab[v];
151

    
152
    return n;
153
}
154

    
155
static inline av_const int av_log2_16bit(unsigned int v)
156
{
157
    int n = 0;
158
    if (v & 0xff00) {
159
        v >>= 8;
160
        n += 8;
161
    }
162
    n += ff_log2_tab[v];
163

    
164
    return n;
165
}
166

    
167
/**
168
 * Clips a signed integer value into the amin-amax range.
169
 * @param a value to clip
170
 * @param amin minimum value of the clip range
171
 * @param amax maximum value of the clip range
172
 * @return clipped value
173
 */
174
static inline av_const int av_clip(int a, int amin, int amax)
175
{
176
    if      (a < amin) return amin;
177
    else if (a > amax) return amax;
178
    else               return a;
179
}
180

    
181
/**
182
 * Clips a signed integer value into the 0-255 range.
183
 * @param a value to clip
184
 * @return clipped value
185
 */
186
static inline av_const uint8_t av_clip_uint8(int a)
187
{
188
    if (a&(~255)) return (-a)>>31;
189
    else          return a;
190
}
191

    
192
/**
193
 * Clips a signed integer value into the -32768,32767 range.
194
 * @param a value to clip
195
 * @return clipped value
196
 */
197
static inline av_const int16_t av_clip_int16(int a)
198
{
199
    if ((a+32768) & ~65535) return (a>>31) ^ 32767;
200
    else                    return a;
201
}
202

    
203
/**
204
 * Clips a float value into the amin-amax range.
205
 * @param a value to clip
206
 * @param amin minimum value of the clip range
207
 * @param amax maximum value of the clip range
208
 * @return clipped value
209
 */
210
static inline av_const float av_clipf(float a, float amin, float amax)
211
{
212
    if      (a < amin) return amin;
213
    else if (a > amax) return amax;
214
    else               return a;
215
}
216

    
217
#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
218
#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
219

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

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

    
284
#include "mem.h"
285

    
286
#ifdef HAVE_AV_CONFIG_H
287
#    include "internal.h"
288
#endif /* HAVE_AV_CONFIG_H */
289

    
290
#endif /* AVUTIL_COMMON_H */