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