utils.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2012 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Misc. common utility functions
  11. //
  12. // Authors: Skal (pascal.massimino@gmail.com)
  13. // Urvang (urvang@google.com)
  14. #ifndef WEBP_UTILS_UTILS_H_
  15. #define WEBP_UTILS_UTILS_H_
  16. #include <assert.h>
  17. #include "../webp/types.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. //------------------------------------------------------------------------------
  22. // Memory allocation
  23. // This is the maximum memory amount that libwebp will ever try to allocate.
  24. #define WEBP_MAX_ALLOCABLE_MEMORY (1ULL << 40)
  25. // size-checking safe malloc/calloc: verify that the requested size is not too
  26. // large, or return NULL. You don't need to call these for constructs like
  27. // malloc(sizeof(foo)), but only if there's picture-dependent size involved
  28. // somewhere (like: malloc(num_pixels * sizeof(*something))). That's why this
  29. // safe malloc() borrows the signature from calloc(), pointing at the dangerous
  30. // underlying multiply involved.
  31. WEBP_EXTERN(void*) WebPSafeMalloc(uint64_t nmemb, size_t size);
  32. // Note that WebPSafeCalloc() expects the second argument type to be 'size_t'
  33. // in order to favor the "calloc(num_foo, sizeof(foo))" pattern.
  34. WEBP_EXTERN(void*) WebPSafeCalloc(uint64_t nmemb, size_t size);
  35. // Companion deallocation function to the above allocations.
  36. WEBP_EXTERN(void) WebPSafeFree(void* const ptr);
  37. //------------------------------------------------------------------------------
  38. // Reading/writing data.
  39. // Read 16, 24 or 32 bits stored in little-endian order.
  40. static WEBP_INLINE int GetLE16(const uint8_t* const data) {
  41. return (int)(data[0] << 0) | (data[1] << 8);
  42. }
  43. static WEBP_INLINE int GetLE24(const uint8_t* const data) {
  44. return GetLE16(data) | (data[2] << 16);
  45. }
  46. static WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) {
  47. return (uint32_t)GetLE16(data) | (GetLE16(data + 2) << 16);
  48. }
  49. // Store 16, 24 or 32 bits in little-endian order.
  50. static WEBP_INLINE void PutLE16(uint8_t* const data, int val) {
  51. assert(val < (1 << 16));
  52. data[0] = (val >> 0);
  53. data[1] = (val >> 8);
  54. }
  55. static WEBP_INLINE void PutLE24(uint8_t* const data, int val) {
  56. assert(val < (1 << 24));
  57. PutLE16(data, val & 0xffff);
  58. data[2] = (val >> 16);
  59. }
  60. static WEBP_INLINE void PutLE32(uint8_t* const data, uint32_t val) {
  61. PutLE16(data, (int)(val & 0xffff));
  62. PutLE16(data + 2, (int)(val >> 16));
  63. }
  64. // Returns (int)floor(log2(n)). n must be > 0.
  65. // use GNU builtins where available.
  66. #if defined(__GNUC__) && \
  67. ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
  68. static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
  69. return 31 ^ __builtin_clz(n);
  70. }
  71. #elif defined(_MSC_VER) && _MSC_VER > 1310 && \
  72. (defined(_M_X64) || defined(_M_IX86))
  73. #include <intrin.h>
  74. #pragma intrinsic(_BitScanReverse)
  75. static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
  76. uint32_t first_set_bit;
  77. _BitScanReverse(&first_set_bit, n);
  78. return first_set_bit;
  79. }
  80. #else
  81. static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
  82. int log = 0;
  83. uint32_t value = n;
  84. int i;
  85. for (i = 4; i >= 0; --i) {
  86. const int shift = (1 << i);
  87. const uint32_t x = value >> shift;
  88. if (x != 0) {
  89. value = x;
  90. log += shift;
  91. }
  92. }
  93. return log;
  94. }
  95. #endif
  96. //------------------------------------------------------------------------------
  97. #ifdef __cplusplus
  98. } // extern "C"
  99. #endif
  100. #endif /* WEBP_UTILS_UTILS_H_ */