summaryrefslogtreecommitdiff
path: root/audio_codec/libflac/common.h (plain)
blob: 7c5497e1315818188898966442f1c7e5cabced4f
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 <string.h>
30#ifdef __MW__
31#include "types.h"
32#endif
33
34#ifndef __MW__
35#ifdef __GNUC__
36# define AV_GCC_VERSION_AT_LEAST(x,y) (__GNUC__ > x || __GNUC__ == x && __GNUC_MINOR__ >= y)
37#else
38# define AV_GCC_VERSION_AT_LEAST(x,y) 0
39#endif
40#else
41# define AV_GCC_VERSION_AT_LEAST(x,y) 0
42#endif
43#ifndef av_always_inline
44#if AV_GCC_VERSION_AT_LEAST(3,1)
45# define av_always_inline __attribute__((always_inline)) inline
46#else
47# define av_always_inline inline
48#endif
49#endif
50
51#ifndef av_noinline
52#if AV_GCC_VERSION_AT_LEAST(3,1)
53# define av_noinline __attribute__((noinline))
54#else
55# define av_noinline
56#endif
57#endif
58
59#ifndef av_pure
60#if AV_GCC_VERSION_AT_LEAST(3,1)
61# define av_pure __attribute__((pure))
62#else
63# define av_pure
64#endif
65#endif
66
67#ifndef av_const
68#if AV_GCC_VERSION_AT_LEAST(2,6)
69# define av_const __attribute__((const))
70#else
71# define av_const
72#endif
73#endif
74
75#ifndef av_cold
76#if (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(4,3)
77# define av_cold __attribute__((cold))
78#else
79# define av_cold
80#endif
81#endif
82
83#ifndef av_flatten
84#if (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(4,1)
85# define av_flatten __attribute__((flatten))
86#else
87# define av_flatten
88#endif
89#endif
90
91#ifndef attribute_deprecated
92#if AV_GCC_VERSION_AT_LEAST(3,1)
93# define attribute_deprecated __attribute__((deprecated))
94#else
95# define attribute_deprecated
96#endif
97#endif
98
99#ifndef av_unused
100#if defined(__GNUC__)
101# define av_unused __attribute__((unused))
102#else
103# define av_unused
104#endif
105#endif
106
107#ifndef av_uninit
108#if defined(__GNUC__) && !defined(__ICC)
109# define av_uninit(x) x=x
110#else
111# define av_uninit(x) x
112#endif
113#endif
114#ifndef inline
115#define inline
116#endif
117//rounded division & shift
118#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
119/* assume b>0 */
120#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
121#define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
122#define FFSIGN(a) ((a) > 0 ? 1 : -1)
123
124#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
125#define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
126#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
127#define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
128
129#define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
130#define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
131#define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1))
132
133/* misc math functions */
134//extern const uint8_t ff_log2_tab[256];
135
136extern const uint8_t av_reverse[256];
137static uint8_t ff_log2_tab[256] = {
138 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
139 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
140 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
141 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
142 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
143 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
144 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
145 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
146};
147
148static inline av_const int av_log2(unsigned int v)
149{
150 int n = 0;
151 if (v & 0xffff0000) {
152 v >>= 16;
153 n += 16;
154 }
155 if (v & 0xff00) {
156 v >>= 8;
157 n += 8;
158 }
159 n += ff_log2_tab[v];
160
161 return n;
162}
163
164static inline av_const int av_log2_16bit(unsigned int v)
165{
166 int n = 0;
167 if (v & 0xff00) {
168 v >>= 8;
169 n += 8;
170 }
171 n += ff_log2_tab[v];
172
173 return n;
174}
175
176/**
177 * Clips a signed integer value into the amin-amax range.
178 * @param a value to clip
179 * @param amin minimum value of the clip range
180 * @param amax maximum value of the clip range
181 * @return clipped value
182 */
183static inline av_const int av_clip(int a, int amin, int amax)
184{
185 if (a < amin) {
186 return amin;
187 } else if (a > amax) {
188 return amax;
189 } else {
190 return a;
191 }
192}
193
194/**
195 * Clips a signed integer value into the 0-255 range.
196 * @param a value to clip
197 * @return clipped value
198 */
199static inline av_const uint8_t av_clip_uint8(int a)
200{
201 if (a & (~255)) {
202 return (-a) >> 31;
203 } else {
204 return a;
205 }
206}
207
208/**
209 * Clips a signed integer value into the 0-65535 range.
210 * @param a value to clip
211 * @return clipped value
212 */
213static inline av_const uint16_t av_clip_uint16(int a)
214{
215 if (a & (~65535)) {
216 return (-a) >> 31;
217 } else {
218 return a;
219 }
220}
221
222/**
223 * Clips a signed integer value into the -32768,32767 range.
224 * @param a value to clip
225 * @return clipped value
226 */
227static inline av_const int16_t av_clip_int16(int a)
228{
229 if ((a + 32768) & ~65535) {
230 return (a >> 31) ^ 32767;
231 } else {
232 return a;
233 }
234}
235
236/**
237 * Clips a float value into the amin-amax range.
238 * @param a value to clip
239 * @param amin minimum value of the clip range
240 * @param amax maximum value of the clip range
241 * @return clipped value
242 */
243static inline av_const float av_clipf(float a, float amin, float amax)
244{
245 if (a < amin) {
246 return amin;
247 } else if (a > amax) {
248 return amax;
249 } else {
250 return a;
251 }
252}
253
254/** Computes ceil(log2(x)).
255 * @param x value used to compute ceil(log2(x))
256 * @return computed ceiling of log2(x)
257 */
258static inline av_const int av_ceil_log2(int x)
259{
260 return av_log2((x - 1) << 1);
261}
262
263#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
264#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
265
266/*!
267 * \def GET_UTF8(val, GET_BYTE, ERROR)
268 * Converts a UTF-8 character (up to 4 bytes long) to its 32-bit UCS-4 encoded form
269 * \param val is the output and should be of type uint32_t. It holds the converted
270 * UCS-4 character and should be a left value.
271 * \param GET_BYTE gets UTF-8 encoded bytes from any proper source. It can be
272 * a function or a statement whose return value or evaluated value is of type
273 * uint8_t. It will be executed up to 4 times for values in the valid UTF-8 range,
274 * and up to 7 times in the general case.
275 * \param ERROR action that should be taken when an invalid UTF-8 byte is returned
276 * from GET_BYTE. It should be a statement that jumps out of the macro,
277 * like exit(), goto, return, break, or continue.
278 */
279#define GET_UTF8(val, GET_BYTE, ERROR)\
280 val= GET_BYTE;\
281 {\
282 int ones= 7 - av_log2(val ^ 255);\
283 if(ones==1)\
284 ERROR\
285 val&= 127>>ones;\
286 while(--ones > 0){\
287 int tmp= GET_BYTE - 128;\
288 if(tmp>>6)\
289 ERROR\
290 val= (val<<6) + tmp;\
291 }\
292 }
293
294/*!
295 * \def GET_UTF16(val, GET_16BIT, ERROR)
296 * Converts a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form
297 * \param val is the output and should be of type uint32_t. It holds the converted
298 * UCS-4 character and should be a left value.
299 * \param GET_16BIT gets two bytes of UTF-16 encoded data converted to native endianness.
300 * It can be a function or a statement whose return value or evaluated value is of type
301 * uint16_t. It will be executed up to 2 times.
302 * \param ERROR action that should be taken when an invalid UTF-16 surrogate is
303 * returned from GET_BYTE. It should be a statement that jumps out of the macro,
304 * like exit(), goto, return, break, or continue.
305 */
306#define GET_UTF16(val, GET_16BIT, ERROR)\
307 val = GET_16BIT;\
308 {\
309 unsigned int hi = val - 0xD800;\
310 if (hi < 0x800) {\
311 val = GET_16BIT - 0xDC00;\
312 if (val > 0x3FFU || hi > 0x3FFU)\
313 ERROR\
314 val += (hi<<10) + 0x10000;\
315 }\
316 }\
317
318/*!
319 * \def PUT_UTF8(val, tmp, PUT_BYTE)
320 * Converts a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
321 * \param val is an input-only argument and should be of type uint32_t. It holds
322 * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If
323 * val is given as a function it is executed only once.
324 * \param tmp is a temporary variable and should be of type uint8_t. It
325 * represents an intermediate value during conversion that is to be
326 * output by PUT_BYTE.
327 * \param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
328 * It could be a function or a statement, and uses tmp as the input byte.
329 * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
330 * executed up to 4 times for values in the valid UTF-8 range and up to
331 * 7 times in the general case, depending on the length of the converted
332 * Unicode character.
333 */
334#define PUT_UTF8(val, tmp, PUT_BYTE)\
335 {\
336 int bytes, shift;\
337 uint32_t in = val;\
338 if (in < 0x80) {\
339 tmp = in;\
340 PUT_BYTE\
341 } else {\
342 bytes = (av_log2(in) + 4) / 5;\
343 shift = (bytes - 1) * 6;\
344 tmp = (256 - (256 >> bytes)) | (in >> shift);\
345 PUT_BYTE\
346 while (shift >= 6) {\
347 shift -= 6;\
348 tmp = 0x80 | ((in >> shift) & 0x3f);\
349 PUT_BYTE\
350 }\
351 }\
352 }
353
354//#include "mem.h"
355
356#endif /* AVUTIL_COMMON_H */
357