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
23  * common internal and external API header
24  */
25 
26 #ifndef AVUTIL_COMMON_H
27 #define AVUTIL_COMMON_H
28 
29 #if defined(__cplusplus) && !defined(__STDC_CONSTANT_MACROS) && !defined(UINT64_C)
30 #error missing -D__STDC_CONSTANT_MACROS / #define __STDC_CONSTANT_MACROS
31 #endif
32 
33 #include <errno.h>
34 
35 #if !defined(VICE_FFMPEGLIB_H)
36 #include <inttypes.h>
37 #endif
38 
39 #include <limits.h>
40 #include <math.h>
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include "attributes.h"
47 #include "version.h"
48 
49 #ifndef _MSC_VER
50 #include "libavutil/avconfig.h"
51 #endif
52 
53 #if AV_HAVE_BIGENDIAN
54 #   define AV_NE(be, le) (be)
55 #else
56 #   define AV_NE(be, le) (le)
57 #endif
58 
59 //rounded division & shift
60 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
61 /* assume b>0 */
62 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
63 /* assume a>0 and b>0 */
64 #define FF_CEIL_RSHIFT(a,b) (!av_builtin_constant_p(b) ? -((-(a)) >> (b)) \
65                                                        : ((a) + (1<<(b)) - 1) >> (b))
66 #define FFUDIV(a,b) (((a)>0 ?(a):(a)-(b)+1) / (b))
67 #define FFUMOD(a,b) ((a)-(b)*FFUDIV(a,b))
68 #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
69 #define FFSIGN(a) ((a) > 0 ? 1 : -1)
70 
71 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
72 #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
73 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
74 #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
75 
76 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
77 #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
78 #define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1))
79 
80 /* misc math functions */
81 
82 /**
83  * Reverse the order of the bits of an 8-bits unsigned integer.
84  */
85 #if FF_API_AV_REVERSE
86 extern attribute_deprecated const uint8_t av_reverse[256];
87 #endif
88 
89 #ifdef HAVE_AV_CONFIG_H
90 #   include "config.h"
91 #   include "intmath.h"
92 #endif
93 
94 /* Pull in unguarded fallback defines at the end of this file. */
95 #include "common.h"
96 
97 #ifndef av_log2
98 av_const int av_log2(unsigned v);
99 #endif
100 
101 #ifndef av_log2_16bit
102 av_const int av_log2_16bit(unsigned v);
103 #endif
104 
105 /**
106  * Clip a signed integer value into the amin-amax range.
107  * @param a value to clip
108  * @param amin minimum value of the clip range
109  * @param amax maximum value of the clip range
110  * @return clipped value
111  */
av_clip_c(int a,int amin,int amax)112 static av_always_inline av_const int av_clip_c(int a, int amin, int amax)
113 {
114 #if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
115     if (amin > amax) abort();
116 #endif
117     if      (a < amin) return amin;
118     else if (a > amax) return amax;
119     else               return a;
120 }
121 
122 /**
123  * Clip a signed 64bit integer value into the amin-amax range.
124  * @param a value to clip
125  * @param amin minimum value of the clip range
126  * @param amax maximum value of the clip range
127  * @return clipped value
128  */
av_clip64_c(int64_t a,int64_t amin,int64_t amax)129 static av_always_inline av_const int64_t av_clip64_c(int64_t a, int64_t amin, int64_t amax)
130 {
131 #if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
132     if (amin > amax) abort();
133 #endif
134     if      (a < amin) return amin;
135     else if (a > amax) return amax;
136     else               return a;
137 }
138 
139 /**
140  * Clip a signed integer value into the 0-255 range.
141  * @param a value to clip
142  * @return clipped value
143  */
av_clip_uint8_c(int a)144 static av_always_inline av_const uint8_t av_clip_uint8_c(int a)
145 {
146     if (a&(~0xFF)) return (-a)>>31;
147     else           return a;
148 }
149 
150 /**
151  * Clip a signed integer value into the -128,127 range.
152  * @param a value to clip
153  * @return clipped value
154  */
av_clip_int8_c(int a)155 static av_always_inline av_const int8_t av_clip_int8_c(int a)
156 {
157     if ((a+0x80) & ~0xFF) return (a>>31) ^ 0x7F;
158     else                  return a;
159 }
160 
161 /**
162  * Clip a signed integer value into the 0-65535 range.
163  * @param a value to clip
164  * @return clipped value
165  */
av_clip_uint16_c(int a)166 static av_always_inline av_const uint16_t av_clip_uint16_c(int a)
167 {
168     if (a&(~0xFFFF)) return (-a)>>31;
169     else             return a;
170 }
171 
172 /**
173  * Clip a signed integer value into the -32768,32767 range.
174  * @param a value to clip
175  * @return clipped value
176  */
av_clip_int16_c(int a)177 static av_always_inline av_const int16_t av_clip_int16_c(int a)
178 {
179     if ((a+0x8000) & ~0xFFFF) return (a>>31) ^ 0x7FFF;
180     else                      return a;
181 }
182 
183 /**
184  * Clip a signed 64-bit integer value into the -2147483648,2147483647 range.
185  * @param a value to clip
186  * @return clipped value
187  */
av_clipl_int32_c(int64_t a)188 static av_always_inline av_const int32_t av_clipl_int32_c(int64_t a)
189 {
190     if ((a+0x80000000u) & ~UINT64_C(0xFFFFFFFF)) return (int32_t)((a>>63) ^ 0x7FFFFFFF);
191     else                                         return (int32_t)a;
192 }
193 
194 /**
195  * Clip a signed integer to an unsigned power of two range.
196  * @param  a value to clip
197  * @param  p bit position to clip at
198  * @return clipped value
199  */
av_clip_uintp2_c(int a,int p)200 static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)
201 {
202     if (a & ~((1<<p) - 1)) return -a >> 31 & ((1<<p) - 1);
203     else                   return  a;
204 }
205 
206 /**
207  * Add two signed 32-bit values with saturation.
208  *
209  * @param  a one value
210  * @param  b another value
211  * @return sum with signed saturation
212  */
av_sat_add32_c(int a,int b)213 static av_always_inline int av_sat_add32_c(int a, int b)
214 {
215     return av_clipl_int32((int64_t)a + b);
216 }
217 
218 /**
219  * Add a doubled value to another value with saturation at both stages.
220  *
221  * @param  a first value
222  * @param  b value doubled and added to a
223  * @return sum with signed saturation
224  */
av_sat_dadd32_c(int a,int b)225 static av_always_inline int av_sat_dadd32_c(int a, int b)
226 {
227     return av_sat_add32(a, av_sat_add32(b, b));
228 }
229 
230 /**
231  * Clip a float value into the amin-amax range.
232  * @param a value to clip
233  * @param amin minimum value of the clip range
234  * @param amax maximum value of the clip range
235  * @return clipped value
236  */
av_clipf_c(float a,float amin,float amax)237 static av_always_inline av_const float av_clipf_c(float a, float amin, float amax)
238 {
239 #if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
240     if (amin > amax) abort();
241 #endif
242     if      (a < amin) return amin;
243     else if (a > amax) return amax;
244     else               return a;
245 }
246 
247 /**
248  * Clip a double value into the amin-amax range.
249  * @param a value to clip
250  * @param amin minimum value of the clip range
251  * @param amax maximum value of the clip range
252  * @return clipped value
253  */
av_clipd_c(double a,double amin,double amax)254 static av_always_inline av_const double av_clipd_c(double a, double amin, double amax)
255 {
256 #if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
257     if (amin > amax) abort();
258 #endif
259     if      (a < amin) return amin;
260     else if (a > amax) return amax;
261     else               return a;
262 }
263 
264 /** Compute ceil(log2(x)).
265  * @param x value used to compute ceil(log2(x))
266  * @return computed ceiling of log2(x)
267  */
av_ceil_log2_c(int x)268 static av_always_inline av_const int av_ceil_log2_c(int x)
269 {
270     return av_log2((x - 1) << 1);
271 }
272 
273 /**
274  * Count number of bits set to one in x
275  * @param x value to count bits of
276  * @return the number of bits set to one in x
277  */
av_popcount_c(uint32_t x)278 static av_always_inline av_const int av_popcount_c(uint32_t x)
279 {
280     x -= (x >> 1) & 0x55555555;
281     x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
282     x = (x + (x >> 4)) & 0x0F0F0F0F;
283     x += x >> 8;
284     return (x + (x >> 16)) & 0x3F;
285 }
286 
287 /**
288  * Count number of bits set to one in x
289  * @param x value to count bits of
290  * @return the number of bits set to one in x
291  */
av_popcount64_c(uint64_t x)292 static av_always_inline av_const int av_popcount64_c(uint64_t x)
293 {
294     return av_popcount((uint32_t)x) + av_popcount((uint32_t)(x >> 32));
295 }
296 
297 #define MKTAG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((unsigned)(d) << 24))
298 #define MKBETAG(a,b,c,d) ((d) | ((c) << 8) | ((b) << 16) | ((unsigned)(a) << 24))
299 
300 /**
301  * Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
302  *
303  * @param val      Output value, must be an lvalue of type uint32_t.
304  * @param GET_BYTE Expression reading one byte from the input.
305  *                 Evaluated up to 7 times (4 for the currently
306  *                 assigned Unicode range).  With a memory buffer
307  *                 input, this could be *ptr++.
308  * @param ERROR    Expression to be evaluated on invalid input,
309  *                 typically a goto statement.
310  *
311  * @warning ERROR should not contain a loop control statement which
312  * could interact with the internal while loop, and should force an
313  * exit from the macro code (e.g. through a goto or a return) in order
314  * to prevent undefined results.
315  */
316 #define GET_UTF8(val, GET_BYTE, ERROR)\
317     val= GET_BYTE;\
318     {\
319         uint32_t top = (val & 128) >> 1;\
320         if ((val & 0xc0) == 0x80 || val >= 0xFE)\
321             ERROR\
322         while (val & top) {\
323             int tmp= GET_BYTE - 128;\
324             if(tmp>>6)\
325                 ERROR\
326             val= (val<<6) + tmp;\
327             top <<= 5;\
328         }\
329         val &= (top << 1) - 1;\
330     }
331 
332 /**
333  * Convert a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form.
334  *
335  * @param val       Output value, must be an lvalue of type uint32_t.
336  * @param GET_16BIT Expression returning two bytes of UTF-16 data converted
337  *                  to native byte order.  Evaluated one or two times.
338  * @param ERROR     Expression to be evaluated on invalid input,
339  *                  typically a goto statement.
340  */
341 #define GET_UTF16(val, GET_16BIT, ERROR)\
342     val = GET_16BIT;\
343     {\
344         unsigned int hi = val - 0xD800;\
345         if (hi < 0x800) {\
346             val = GET_16BIT - 0xDC00;\
347             if (val > 0x3FFU || hi > 0x3FFU)\
348                 ERROR\
349             val += (hi<<10) + 0x10000;\
350         }\
351     }\
352 
353 /**
354  * @def PUT_UTF8(val, tmp, PUT_BYTE)
355  * Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
356  * @param val is an input-only argument and should be of type uint32_t. It holds
357  * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If
358  * val is given as a function it is executed only once.
359  * @param tmp is a temporary variable and should be of type uint8_t. It
360  * represents an intermediate value during conversion that is to be
361  * output by PUT_BYTE.
362  * @param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
363  * It could be a function or a statement, and uses tmp as the input byte.
364  * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
365  * executed up to 4 times for values in the valid UTF-8 range and up to
366  * 7 times in the general case, depending on the length of the converted
367  * Unicode character.
368  */
369 #define PUT_UTF8(val, tmp, PUT_BYTE)\
370     {\
371         int bytes, shift;\
372         uint32_t in = val;\
373         if (in < 0x80) {\
374             tmp = in;\
375             PUT_BYTE\
376         } else {\
377             bytes = (av_log2(in) + 4) / 5;\
378             shift = (bytes - 1) * 6;\
379             tmp = (256 - (256 >> bytes)) | (in >> shift);\
380             PUT_BYTE\
381             while (shift >= 6) {\
382                 shift -= 6;\
383                 tmp = 0x80 | ((in >> shift) & 0x3f);\
384                 PUT_BYTE\
385             }\
386         }\
387     }
388 
389 /**
390  * @def PUT_UTF16(val, tmp, PUT_16BIT)
391  * Convert a 32-bit Unicode character to its UTF-16 encoded form (2 or 4 bytes).
392  * @param val is an input-only argument and should be of type uint32_t. It holds
393  * a UCS-4 encoded Unicode character that is to be converted to UTF-16. If
394  * val is given as a function it is executed only once.
395  * @param tmp is a temporary variable and should be of type uint16_t. It
396  * represents an intermediate value during conversion that is to be
397  * output by PUT_16BIT.
398  * @param PUT_16BIT writes the converted UTF-16 data to any proper destination
399  * in desired endianness. It could be a function or a statement, and uses tmp
400  * as the input byte.  For example, PUT_BYTE could be "*output++ = tmp;"
401  * PUT_BYTE will be executed 1 or 2 times depending on input character.
402  */
403 #define PUT_UTF16(val, tmp, PUT_16BIT)\
404     {\
405         uint32_t in = val;\
406         if (in < 0x10000) {\
407             tmp = in;\
408             PUT_16BIT\
409         } else {\
410             tmp = 0xD800 | ((in - 0x10000) >> 10);\
411             PUT_16BIT\
412             tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\
413             PUT_16BIT\
414         }\
415     }\
416 
417 
418 
419 #include "mem.h"
420 
421 #ifdef HAVE_AV_CONFIG_H
422 #    include "internal.h"
423 #endif /* HAVE_AV_CONFIG_H */
424 
425 #endif /* AVUTIL_COMMON_H */
426 
427 /*
428  * The following definitions are outside the multiple inclusion guard
429  * to ensure they are immediately available in intmath.h.
430  */
431 
432 #ifndef av_ceil_log2
433 #   define av_ceil_log2     av_ceil_log2_c
434 #endif
435 #ifndef av_clip
436 #   define av_clip          av_clip_c
437 #endif
438 #ifndef av_clip64
439 #   define av_clip64        av_clip64_c
440 #endif
441 #ifndef av_clip_uint8
442 #   define av_clip_uint8    av_clip_uint8_c
443 #endif
444 #ifndef av_clip_int8
445 #   define av_clip_int8     av_clip_int8_c
446 #endif
447 #ifndef av_clip_uint16
448 #   define av_clip_uint16   av_clip_uint16_c
449 #endif
450 #ifndef av_clip_int16
451 #   define av_clip_int16    av_clip_int16_c
452 #endif
453 #ifndef av_clipl_int32
454 #   define av_clipl_int32   av_clipl_int32_c
455 #endif
456 #ifndef av_clip_uintp2
457 #   define av_clip_uintp2   av_clip_uintp2_c
458 #endif
459 #ifndef av_sat_add32
460 #   define av_sat_add32     av_sat_add32_c
461 #endif
462 #ifndef av_sat_dadd32
463 #   define av_sat_dadd32    av_sat_dadd32_c
464 #endif
465 #ifndef av_clipf
466 #   define av_clipf         av_clipf_c
467 #endif
468 #ifndef av_clipd
469 #   define av_clipd         av_clipd_c
470 #endif
471 #ifndef av_popcount
472 #   define av_popcount      av_popcount_c
473 #endif
474 #ifndef av_popcount64
475 #   define av_popcount64    av_popcount64_c
476 #endif
477