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