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#include <inttypes.h>
35#include <limits.h>
36#include <math.h>
37#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41
42#include "attributes.h"
43#include "error.h"
44#include "macros.h"
45#include "mem.h"
46
47#ifdef HAVE_AV_CONFIG_H
48# include "config.h"
49# include "intmath.h"
50# include "internal.h"
51#endif /* HAVE_AV_CONFIG_H */
52
53//rounded division & shift
54#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
55/* assume b>0 */
56#define ROUNDED_DIV(a,b) (((a)>=0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
57/* Fast a/(1<<b) rounded toward +inf. Assume a>=0 and b>=0 */
58#define AV_CEIL_RSHIFT(a,b) (!av_builtin_constant_p(b) ? -((-(a)) >> (b)) \
59 : ((a) + (1<<(b)) - 1) >> (b))
60/* Backwards compat. */
61#define FF_CEIL_RSHIFT AV_CEIL_RSHIFT
62
63#define FFUDIV(a,b) (((a)>0 ?(a):(a)-(b)+1) / (b))
64#define FFUMOD(a,b) ((a)-(b)*FFUDIV(a,b))
65
66/**
67 * Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they
68 * are not representable as absolute values of their type. This is the same
69 * as with *abs()
70 * @see FFNABS()
71 */
72#define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
73#define FFSIGN(a) ((a) > 0 ? 1 : -1)
74
75/**
76 * Negative Absolute value.
77 * this works for all integers of all types.
78 * As with many macros, this evaluates its argument twice, it thus must not have
79 * a sideeffect, that is FFNABS(x++) has undefined behavior.
80 */
81#define FFNABS(a) ((a) <= 0 ? (a) : (-(a)))
82
83/**
84 * Unsigned Absolute value.
85 * This takes the absolute value of a signed int and returns it as a unsigned.
86 * This also works with INT_MIN which would otherwise not be representable
87 * As with many macros, this evaluates its argument twice.
88 */
89#define FFABSU(a) ((a) <= 0 ? -(unsigned)(a) : (unsigned)(a))
90#define FFABS64U(a) ((a) <= 0 ? -(uint64_t)(a) : (uint64_t)(a))
91
92/* misc math functions */
93
94#ifndef av_ceil_log2
95# define av_ceil_log2 av_ceil_log2_c
96#endif
97#ifndef av_clip
98# define av_clip av_clip_c
99#endif
100#ifndef av_clip64
101# define av_clip64 av_clip64_c
102#endif
103#ifndef av_clip_uint8
104# define av_clip_uint8 av_clip_uint8_c
105#endif
106#ifndef av_clip_int8
107# define av_clip_int8 av_clip_int8_c
108#endif
109#ifndef av_clip_uint16
110# define av_clip_uint16 av_clip_uint16_c
111#endif
112#ifndef av_clip_int16
113# define av_clip_int16 av_clip_int16_c
114#endif
115#ifndef av_clipl_int32
116# define av_clipl_int32 av_clipl_int32_c
117#endif
118#ifndef av_clip_intp2
119# define av_clip_intp2 av_clip_intp2_c
120#endif
121#ifndef av_clip_uintp2
122# define av_clip_uintp2 av_clip_uintp2_c
123#endif
124#ifndef av_mod_uintp2
125# define av_mod_uintp2 av_mod_uintp2_c
126#endif
127#ifndef av_sat_add32
128# define av_sat_add32 av_sat_add32_c
129#endif
130#ifndef av_sat_dadd32
131# define av_sat_dadd32 av_sat_dadd32_c
132#endif
133#ifndef av_sat_sub32
134# define av_sat_sub32 av_sat_sub32_c
135#endif
136#ifndef av_sat_dsub32
137# define av_sat_dsub32 av_sat_dsub32_c
138#endif
139#ifndef av_sat_add64
140# define av_sat_add64 av_sat_add64_c
141#endif
142#ifndef av_sat_sub64
143# define av_sat_sub64 av_sat_sub64_c
144#endif
145#ifndef av_clipf
146# define av_clipf av_clipf_c
147#endif
148#ifndef av_clipd
149# define av_clipd av_clipd_c
150#endif
151#ifndef av_popcount
152# define av_popcount av_popcount_c
153#endif
154#ifndef av_popcount64
155# define av_popcount64 av_popcount64_c
156#endif
157#ifndef av_parity
158# define av_parity av_parity_c
159#endif
160
161#ifndef av_log2
162av_const int av_log2(unsigned v);
163#endif
164
165#ifndef av_log2_16bit
166av_const int av_log2_16bit(unsigned v);
167#endif
168
169/**
170 * Clip a signed integer value into the amin-amax range.
171 * @param a value to clip
172 * @param amin minimum value of the clip range
173 * @param amax maximum value of the clip range
174 * @return clipped value
175 */
176static av_always_inline av_const int av_clip_c(int a, int amin, int amax)
177{
178#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
179 if (amin > amax) abort();
180#endif
181 if (a < amin) return amin;
182 else if (a > amax) return amax;
183 else return a;
184}
185
186/**
187 * Clip a signed 64bit integer value into the amin-amax range.
188 * @param a value to clip
189 * @param amin minimum value of the clip range
190 * @param amax maximum value of the clip range
191 * @return clipped value
192 */
193static av_always_inline av_const int64_t av_clip64_c(int64_t a, int64_t amin, int64_t amax)
194{
195#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
196 if (amin > amax) abort();
197#endif
198 if (a < amin) return amin;
199 else if (a > amax) return amax;
200 else return a;
201}
202
203/**
204 * Clip a signed integer value into the 0-255 range.
205 * @param a value to clip
206 * @return clipped value
207 */
208static av_always_inline av_const uint8_t av_clip_uint8_c(int a)
209{
210 if (a&(~0xFF)) return (~a)>>31;
211 else return a;
212}
213
214/**
215 * Clip a signed integer value into the -128,127 range.
216 * @param a value to clip
217 * @return clipped value
218 */
219static av_always_inline av_const int8_t av_clip_int8_c(int a)
220{
221 if ((a+0x80U) & ~0xFF) return (a>>31) ^ 0x7F;
222 else return a;
223}
224
225/**
226 * Clip a signed integer value into the 0-65535 range.
227 * @param a value to clip
228 * @return clipped value
229 */
230static av_always_inline av_const uint16_t av_clip_uint16_c(int a)
231{
232 if (a&(~0xFFFF)) return (~a)>>31;
233 else return a;
234}
235
236/**
237 * Clip a signed integer value into the -32768,32767 range.
238 * @param a value to clip
239 * @return clipped value
240 */
241static av_always_inline av_const int16_t av_clip_int16_c(int a)
242{
243 if ((a+0x8000U) & ~0xFFFF) return (a>>31) ^ 0x7FFF;
244 else return a;
245}
246
247/**
248 * Clip a signed 64-bit integer value into the -2147483648,2147483647 range.
249 * @param a value to clip
250 * @return clipped value
251 */
252static av_always_inline av_const int32_t av_clipl_int32_c(int64_t a)
253{
254 if ((a+0x80000000u) & ~UINT64_C(0xFFFFFFFF)) return (int32_t)((a>>63) ^ 0x7FFFFFFF);
255 else return (int32_t)a;
256}
257
258/**
259 * Clip a signed integer into the -(2^p),(2^p-1) range.
260 * @param a value to clip
261 * @param p bit position to clip at
262 * @return clipped value
263 */
264static av_always_inline av_const int av_clip_intp2_c(int a, int p)
265{
266 if (((unsigned)a + (1 << p)) & ~((2 << p) - 1))
267 return (a >> 31) ^ ((1 << p) - 1);
268 else
269 return a;
270}
271
272/**
273 * Clip a signed integer to an unsigned power of two range.
274 * @param a value to clip
275 * @param p bit position to clip at
276 * @return clipped value
277 */
278static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)
279{
280 if (a & ~((1<<p) - 1)) return (~a) >> 31 & ((1<<p) - 1);
281 else return a;
282}
283
284/**
285 * Clear high bits from an unsigned integer starting with specific bit position
286 * @param a value to clip
287 * @param p bit position to clip at
288 * @return clipped value
289 */
290static av_always_inline av_const unsigned av_mod_uintp2_c(unsigned a, unsigned p)
291{
292 return a & ((1U << p) - 1);
293}
294
295/**
296 * Add two signed 32-bit values with saturation.
297 *
298 * @param a one value
299 * @param b another value
300 * @return sum with signed saturation
301 */
302static av_always_inline int av_sat_add32_c(int a, int b)
303{
304 return av_clipl_int32(a: (int64_t)a + b);
305}
306
307/**
308 * Add a doubled value to another value with saturation at both stages.
309 *
310 * @param a first value
311 * @param b value doubled and added to a
312 * @return sum sat(a + sat(2*b)) with signed saturation
313 */
314static av_always_inline int av_sat_dadd32_c(int a, int b)
315{
316 return av_sat_add32(a, av_sat_add32(a: b, b));
317}
318
319/**
320 * Subtract two signed 32-bit values with saturation.
321 *
322 * @param a one value
323 * @param b another value
324 * @return difference with signed saturation
325 */
326static av_always_inline int av_sat_sub32_c(int a, int b)
327{
328 return av_clipl_int32(a: (int64_t)a - b);
329}
330
331/**
332 * Subtract a doubled value from another value with saturation at both stages.
333 *
334 * @param a first value
335 * @param b value doubled and subtracted from a
336 * @return difference sat(a - sat(2*b)) with signed saturation
337 */
338static av_always_inline int av_sat_dsub32_c(int a, int b)
339{
340 return av_sat_sub32(a, av_sat_add32(a: b, b));
341}
342
343/**
344 * Add two signed 64-bit values with saturation.
345 *
346 * @param a one value
347 * @param b another value
348 * @return sum with signed saturation
349 */
350static av_always_inline int64_t av_sat_add64_c(int64_t a, int64_t b) {
351#if (!defined(__INTEL_COMPILER) && AV_GCC_VERSION_AT_LEAST(5,1)) || AV_HAS_BUILTIN(__builtin_add_overflow)
352 int64_t tmp;
353 return !__builtin_add_overflow(a, b, &tmp) ? tmp : (tmp < 0 ? INT64_MAX : INT64_MIN);
354#else
355 int64_t s = a+(uint64_t)b;
356 if ((int64_t)(a^b | ~s^b) >= 0)
357 return INT64_MAX ^ (b >> 63);
358 return s;
359#endif
360}
361
362/**
363 * Subtract two signed 64-bit values with saturation.
364 *
365 * @param a one value
366 * @param b another value
367 * @return difference with signed saturation
368 */
369static av_always_inline int64_t av_sat_sub64_c(int64_t a, int64_t b) {
370#if (!defined(__INTEL_COMPILER) && AV_GCC_VERSION_AT_LEAST(5,1)) || AV_HAS_BUILTIN(__builtin_sub_overflow)
371 int64_t tmp;
372 return !__builtin_sub_overflow(a, b, &tmp) ? tmp : (tmp < 0 ? INT64_MAX : INT64_MIN);
373#else
374 if (b <= 0 && a >= INT64_MAX + b)
375 return INT64_MAX;
376 if (b >= 0 && a <= INT64_MIN + b)
377 return INT64_MIN;
378 return a - b;
379#endif
380}
381
382/**
383 * Clip a float value into the amin-amax range.
384 * If a is nan or -inf amin will be returned.
385 * If a is +inf amax will be returned.
386 * @param a value to clip
387 * @param amin minimum value of the clip range
388 * @param amax maximum value of the clip range
389 * @return clipped value
390 */
391static av_always_inline av_const float av_clipf_c(float a, float amin, float amax)
392{
393#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
394 if (amin > amax) abort();
395#endif
396 return FFMIN(FFMAX(a, amin), amax);
397}
398
399/**
400 * Clip a double value into the amin-amax range.
401 * If a is nan or -inf amin will be returned.
402 * If a is +inf amax will be returned.
403 * @param a value to clip
404 * @param amin minimum value of the clip range
405 * @param amax maximum value of the clip range
406 * @return clipped value
407 */
408static av_always_inline av_const double av_clipd_c(double a, double amin, double amax)
409{
410#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
411 if (amin > amax) abort();
412#endif
413 return FFMIN(FFMAX(a, amin), amax);
414}
415
416/** Compute ceil(log2(x)).
417 * @param x value used to compute ceil(log2(x))
418 * @return computed ceiling of log2(x)
419 */
420static av_always_inline av_const int av_ceil_log2_c(int x)
421{
422 return av_log2(v: (x - 1U) << 1);
423}
424
425/**
426 * Count number of bits set to one in x
427 * @param x value to count bits of
428 * @return the number of bits set to one in x
429 */
430static av_always_inline av_const int av_popcount_c(uint32_t x)
431{
432 x -= (x >> 1) & 0x55555555;
433 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
434 x = (x + (x >> 4)) & 0x0F0F0F0F;
435 x += x >> 8;
436 return (x + (x >> 16)) & 0x3F;
437}
438
439/**
440 * Count number of bits set to one in x
441 * @param x value to count bits of
442 * @return the number of bits set to one in x
443 */
444static av_always_inline av_const int av_popcount64_c(uint64_t x)
445{
446 return av_popcount(x: (uint32_t)x) + av_popcount(x: (uint32_t)(x >> 32));
447}
448
449static av_always_inline av_const int av_parity_c(uint32_t v)
450{
451 return av_popcount(x: v) & 1;
452}
453
454/**
455 * Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
456 *
457 * @param val Output value, must be an lvalue of type uint32_t.
458 * @param GET_BYTE Expression reading one byte from the input.
459 * Evaluated up to 7 times (4 for the currently
460 * assigned Unicode range). With a memory buffer
461 * input, this could be *ptr++, or if you want to make sure
462 * that *ptr stops at the end of a NULL terminated string then
463 * *ptr ? *ptr++ : 0
464 * @param ERROR Expression to be evaluated on invalid input,
465 * typically a goto statement.
466 *
467 * @warning ERROR should not contain a loop control statement which
468 * could interact with the internal while loop, and should force an
469 * exit from the macro code (e.g. through a goto or a return) in order
470 * to prevent undefined results.
471 */
472#define GET_UTF8(val, GET_BYTE, ERROR)\
473 val= (GET_BYTE);\
474 {\
475 uint32_t top = (val & 128) >> 1;\
476 if ((val & 0xc0) == 0x80 || val >= 0xFE)\
477 {ERROR}\
478 while (val & top) {\
479 unsigned int tmp = (GET_BYTE) - 128;\
480 if(tmp>>6)\
481 {ERROR}\
482 val= (val<<6) + tmp;\
483 top <<= 5;\
484 }\
485 val &= (top << 1) - 1;\
486 }
487
488/**
489 * Convert a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form.
490 *
491 * @param val Output value, must be an lvalue of type uint32_t.
492 * @param GET_16BIT Expression returning two bytes of UTF-16 data converted
493 * to native byte order. Evaluated one or two times.
494 * @param ERROR Expression to be evaluated on invalid input,
495 * typically a goto statement.
496 */
497#define GET_UTF16(val, GET_16BIT, ERROR)\
498 val = (GET_16BIT);\
499 {\
500 unsigned int hi = val - 0xD800;\
501 if (hi < 0x800) {\
502 val = (GET_16BIT) - 0xDC00;\
503 if (val > 0x3FFU || hi > 0x3FFU)\
504 {ERROR}\
505 val += (hi<<10) + 0x10000;\
506 }\
507 }\
508
509/**
510 * @def PUT_UTF8(val, tmp, PUT_BYTE)
511 * Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
512 * @param val is an input-only argument and should be of type uint32_t. It holds
513 * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If
514 * val is given as a function it is executed only once.
515 * @param tmp is a temporary variable and should be of type uint8_t. It
516 * represents an intermediate value during conversion that is to be
517 * output by PUT_BYTE.
518 * @param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
519 * It could be a function or a statement, and uses tmp as the input byte.
520 * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
521 * executed up to 4 times for values in the valid UTF-8 range and up to
522 * 7 times in the general case, depending on the length of the converted
523 * Unicode character.
524 */
525#define PUT_UTF8(val, tmp, PUT_BYTE)\
526 {\
527 int bytes, shift;\
528 uint32_t in = val;\
529 if (in < 0x80) {\
530 tmp = in;\
531 PUT_BYTE\
532 } else {\
533 bytes = (av_log2(in) + 4) / 5;\
534 shift = (bytes - 1) * 6;\
535 tmp = (256 - (256 >> bytes)) | (in >> shift);\
536 PUT_BYTE\
537 while (shift >= 6) {\
538 shift -= 6;\
539 tmp = 0x80 | ((in >> shift) & 0x3f);\
540 PUT_BYTE\
541 }\
542 }\
543 }
544
545/**
546 * @def PUT_UTF16(val, tmp, PUT_16BIT)
547 * Convert a 32-bit Unicode character to its UTF-16 encoded form (2 or 4 bytes).
548 * @param val is an input-only argument and should be of type uint32_t. It holds
549 * a UCS-4 encoded Unicode character that is to be converted to UTF-16. If
550 * val is given as a function it is executed only once.
551 * @param tmp is a temporary variable and should be of type uint16_t. It
552 * represents an intermediate value during conversion that is to be
553 * output by PUT_16BIT.
554 * @param PUT_16BIT writes the converted UTF-16 data to any proper destination
555 * in desired endianness. It could be a function or a statement, and uses tmp
556 * as the input byte. For example, PUT_BYTE could be "*output++ = tmp;"
557 * PUT_BYTE will be executed 1 or 2 times depending on input character.
558 */
559#define PUT_UTF16(val, tmp, PUT_16BIT)\
560 {\
561 uint32_t in = val;\
562 if (in < 0x10000) {\
563 tmp = in;\
564 PUT_16BIT\
565 } else {\
566 tmp = 0xD800 | ((in - 0x10000) >> 10);\
567 PUT_16BIT\
568 tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\
569 PUT_16BIT\
570 }\
571 }\
572
573#endif /* AVUTIL_COMMON_H */
574