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