FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/common.h
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 54 54 100.0%
Functions: 19 19 100.0%
Branches: 27 36 75.0%

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