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
15 #ifndef WEBP_UTILS_UTILS_H_
16 #define WEBP_UTILS_UTILS_H_
17
18 #ifdef HAVE_CONFIG_H
19 #include "src/webp/config.h"
20 #endif
21
22 #include <assert.h>
23 #include <limits.h>
24
25 #include "src/dsp/dsp.h"
26 #include "src/webp/types.h"
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 //------------------------------------------------------------------------------
33 // Memory allocation
34
35 // This is the maximum memory amount that libwebp will ever try to allocate.
36 #ifndef WEBP_MAX_ALLOCABLE_MEMORY
37 #if SIZE_MAX > (1ULL << 34)
38 #define WEBP_MAX_ALLOCABLE_MEMORY (1ULL << 34)
39 #else
40 // For 32-bit targets keep this below INT_MAX to avoid valgrind warnings.
41 #define WEBP_MAX_ALLOCABLE_MEMORY ((1ULL << 31) - (1 << 16))
42 #endif
43 #endif // WEBP_MAX_ALLOCABLE_MEMORY
44
CheckSizeOverflow(uint64_t size)45 static WEBP_INLINE int CheckSizeOverflow(uint64_t size) {
46 return size == (size_t)size;
47 }
48
49 // size-checking safe malloc/calloc: verify that the requested size is not too
50 // large, or return NULL. You don't need to call these for constructs like
51 // malloc(sizeof(foo)), but only if there's picture-dependent size involved
52 // somewhere (like: malloc(num_pixels * sizeof(*something))). That's why this
53 // safe malloc() borrows the signature from calloc(), pointing at the dangerous
54 // underlying multiply involved.
55 WEBP_EXTERN void* WebPSafeMalloc(uint64_t nmemb, size_t size);
56 // Note that WebPSafeCalloc() expects the second argument type to be 'size_t'
57 // in order to favor the "calloc(num_foo, sizeof(foo))" pattern.
58 WEBP_EXTERN void* WebPSafeCalloc(uint64_t nmemb, size_t size);
59
60 // Companion deallocation function to the above allocations.
61 WEBP_EXTERN void WebPSafeFree(void* const ptr);
62
63 //------------------------------------------------------------------------------
64 // Alignment
65
66 #define WEBP_ALIGN_CST 31
67 #define WEBP_ALIGN(PTR) (((uintptr_t)(PTR) + WEBP_ALIGN_CST) & ~WEBP_ALIGN_CST)
68
69 #include <string.h>
70 // memcpy() is the safe way of moving potentially unaligned 32b memory.
WebPMemToUint32(const uint8_t * const ptr)71 static WEBP_INLINE uint32_t WebPMemToUint32(const uint8_t* const ptr) {
72 uint32_t A;
73 memcpy(&A, ptr, sizeof(A));
74 return A;
75 }
WebPUint32ToMem(uint8_t * const ptr,uint32_t val)76 static WEBP_INLINE void WebPUint32ToMem(uint8_t* const ptr, uint32_t val) {
77 memcpy(ptr, &val, sizeof(val));
78 }
79
80 //------------------------------------------------------------------------------
81 // Reading/writing data.
82
83 // Read 16, 24 or 32 bits stored in little-endian order.
GetLE16(const uint8_t * const data)84 static WEBP_INLINE int GetLE16(const uint8_t* const data) {
85 return (int)(data[0] << 0) | (data[1] << 8);
86 }
87
GetLE24(const uint8_t * const data)88 static WEBP_INLINE int GetLE24(const uint8_t* const data) {
89 return GetLE16(data) | (data[2] << 16);
90 }
91
GetLE32(const uint8_t * const data)92 static WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) {
93 return GetLE16(data) | ((uint32_t)GetLE16(data + 2) << 16);
94 }
95
96 // Store 16, 24 or 32 bits in little-endian order.
PutLE16(uint8_t * const data,int val)97 static WEBP_INLINE void PutLE16(uint8_t* const data, int val) {
98 assert(val < (1 << 16));
99 data[0] = (val >> 0) & 0xff;
100 data[1] = (val >> 8) & 0xff;
101 }
102
PutLE24(uint8_t * const data,int val)103 static WEBP_INLINE void PutLE24(uint8_t* const data, int val) {
104 assert(val < (1 << 24));
105 PutLE16(data, val & 0xffff);
106 data[2] = (val >> 16) & 0xff;
107 }
108
PutLE32(uint8_t * const data,uint32_t val)109 static WEBP_INLINE void PutLE32(uint8_t* const data, uint32_t val) {
110 PutLE16(data, (int)(val & 0xffff));
111 PutLE16(data + 2, (int)(val >> 16));
112 }
113
114 // use GNU builtins where available.
115 #if defined(__GNUC__) && \
116 ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
117 // Returns (int)floor(log2(n)). n must be > 0.
BitsLog2Floor(uint32_t n)118 static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
119 return 31 ^ __builtin_clz(n);
120 }
121 // counts the number of trailing zero
BitsCtz(uint32_t n)122 static WEBP_INLINE int BitsCtz(uint32_t n) { return __builtin_ctz(n); }
123 #elif defined(_MSC_VER) && _MSC_VER > 1310 && \
124 (defined(_M_X64) || defined(_M_IX86))
125 #include <intrin.h>
126 #pragma intrinsic(_BitScanReverse)
127 #pragma intrinsic(_BitScanForward)
128
BitsLog2Floor(uint32_t n)129 static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
130 unsigned long first_set_bit; // NOLINT (runtime/int)
131 _BitScanReverse(&first_set_bit, n);
132 return first_set_bit;
133 }
BitsCtz(uint32_t n)134 static WEBP_INLINE int BitsCtz(uint32_t n) {
135 unsigned long first_set_bit; // NOLINT (runtime/int)
136 _BitScanForward(&first_set_bit, n);
137 return first_set_bit;
138 }
139 #else // default: use the (slow) C-version.
140 #define WEBP_HAVE_SLOW_CLZ_CTZ // signal that the Clz/Ctz function are slow
141 // Returns 31 ^ clz(n) = log2(n). This is the default C-implementation, either
142 // based on table or not. Can be used as fallback if clz() is not available.
143 #define WEBP_NEED_LOG_TABLE_8BIT
144 extern const uint8_t WebPLogTable8bit[256];
WebPLog2FloorC(uint32_t n)145 static WEBP_INLINE int WebPLog2FloorC(uint32_t n) {
146 int log_value = 0;
147 while (n >= 256) {
148 log_value += 8;
149 n >>= 8;
150 }
151 return log_value + WebPLogTable8bit[n];
152 }
153
BitsLog2Floor(uint32_t n)154 static WEBP_INLINE int BitsLog2Floor(uint32_t n) { return WebPLog2FloorC(n); }
155
BitsCtz(uint32_t n)156 static WEBP_INLINE int BitsCtz(uint32_t n) {
157 int i;
158 for (i = 0; i < 32; ++i, n >>= 1) {
159 if (n & 1) return i;
160 }
161 return 32;
162 }
163
164 #endif
165
166 //------------------------------------------------------------------------------
167 // Pixel copying.
168
169 struct WebPPicture;
170
171 // Copy width x height pixels from 'src' to 'dst' honoring the strides.
172 WEBP_EXTERN void WebPCopyPlane(const uint8_t* src, int src_stride,
173 uint8_t* dst, int dst_stride,
174 int width, int height);
175
176 // Copy ARGB pixels from 'src' to 'dst' honoring strides. 'src' and 'dst' are
177 // assumed to be already allocated and using ARGB data.
178 WEBP_EXTERN void WebPCopyPixels(const struct WebPPicture* const src,
179 struct WebPPicture* const dst);
180
181 //------------------------------------------------------------------------------
182 // Unique colors.
183
184 // Returns count of unique colors in 'pic', assuming pic->use_argb is true.
185 // If the unique color count is more than MAX_PALETTE_SIZE, returns
186 // MAX_PALETTE_SIZE+1.
187 // If 'palette' is not NULL and number of unique colors is less than or equal to
188 // MAX_PALETTE_SIZE, also outputs the actual unique colors into 'palette'.
189 // Note: 'palette' is assumed to be an array already allocated with at least
190 // MAX_PALETTE_SIZE elements.
191 WEBP_EXTERN int WebPGetColorPalette(const struct WebPPicture* const pic,
192 uint32_t* const palette);
193
194 //------------------------------------------------------------------------------
195
196 #ifdef __cplusplus
197 } // extern "C"
198 #endif
199
200 #endif // WEBP_UTILS_UTILS_H_
201