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
29extern "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
45static 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.
55WEBP_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.
58WEBP_EXTERN void* WebPSafeCalloc(uint64_t nmemb, size_t size);
59
60// Companion deallocation function to the above allocations.
61WEBP_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) & \
68 ~(uintptr_t)WEBP_ALIGN_CST)
69
70#include <string.h>
71// memcpy() is the safe way of moving potentially unaligned 32b memory.
72static WEBP_INLINE uint32_t WebPMemToUint32(const uint8_t* const ptr) {
73 uint32_t A;
74 memcpy(&A, ptr, sizeof(A));
75 return A;
76}
77
78static WEBP_INLINE int32_t WebPMemToInt32(const uint8_t* const ptr) {
79 return (int32_t)WebPMemToUint32(ptr);
80}
81
82static WEBP_INLINE void WebPUint32ToMem(uint8_t* const ptr, uint32_t val) {
83 memcpy(ptr, &val, sizeof(val));
84}
85
86static WEBP_INLINE void WebPInt32ToMem(uint8_t* const ptr, int val) {
87 WebPUint32ToMem(ptr, (uint32_t)val);
88}
89
90//------------------------------------------------------------------------------
91// Reading/writing data.
92
93// Read 16, 24 or 32 bits stored in little-endian order.
94static WEBP_INLINE int GetLE16(const uint8_t* const data) {
95 return (int)(data[0] << 0) | (data[1] << 8);
96}
97
98static WEBP_INLINE int GetLE24(const uint8_t* const data) {
99 return GetLE16(data) | (data[2] << 16);
100}
101
102static WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) {
103 return GetLE16(data) | ((uint32_t)GetLE16(data + 2) << 16);
104}
105
106// Store 16, 24 or 32 bits in little-endian order.
107static WEBP_INLINE void PutLE16(uint8_t* const data, int val) {
108 assert(val < (1 << 16));
109 data[0] = (val >> 0) & 0xff;
110 data[1] = (val >> 8) & 0xff;
111}
112
113static WEBP_INLINE void PutLE24(uint8_t* const data, int val) {
114 assert(val < (1 << 24));
115 PutLE16(data, val & 0xffff);
116 data[2] = (val >> 16) & 0xff;
117}
118
119static WEBP_INLINE void PutLE32(uint8_t* const data, uint32_t val) {
120 PutLE16(data, (int)(val & 0xffff));
121 PutLE16(data + 2, (int)(val >> 16));
122}
123
124// use GNU builtins where available.
125#if defined(__GNUC__) && \
126 ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
127// Returns (int)floor(log2(n)). n must be > 0.
128static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
129 return 31 ^ __builtin_clz(n);
130}
131// counts the number of trailing zero
132static WEBP_INLINE int BitsCtz(uint32_t n) { return __builtin_ctz(n); }
133#elif defined(_MSC_VER) && _MSC_VER > 1310 && \
134 (defined(_M_X64) || defined(_M_IX86))
135#include <intrin.h>
136#pragma intrinsic(_BitScanReverse)
137#pragma intrinsic(_BitScanForward)
138
139static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
140 unsigned long first_set_bit; // NOLINT (runtime/int)
141 _BitScanReverse(&first_set_bit, n);
142 return first_set_bit;
143}
144static WEBP_INLINE int BitsCtz(uint32_t n) {
145 unsigned long first_set_bit; // NOLINT (runtime/int)
146 _BitScanForward(&first_set_bit, n);
147 return first_set_bit;
148}
149#else // default: use the (slow) C-version.
150#define WEBP_HAVE_SLOW_CLZ_CTZ // signal that the Clz/Ctz function are slow
151// Returns 31 ^ clz(n) = log2(n). This is the default C-implementation, either
152// based on table or not. Can be used as fallback if clz() is not available.
153#define WEBP_NEED_LOG_TABLE_8BIT
154extern const uint8_t WebPLogTable8bit[256];
155static WEBP_INLINE int WebPLog2FloorC(uint32_t n) {
156 int log_value = 0;
157 while (n >= 256) {
158 log_value += 8;
159 n >>= 8;
160 }
161 return log_value + WebPLogTable8bit[n];
162}
163
164static WEBP_INLINE int BitsLog2Floor(uint32_t n) { return WebPLog2FloorC(n); }
165
166static WEBP_INLINE int BitsCtz(uint32_t n) {
167 int i;
168 for (i = 0; i < 32; ++i, n >>= 1) {
169 if (n & 1) return i;
170 }
171 return 32;
172}
173
174#endif
175
176//------------------------------------------------------------------------------
177// Pixel copying.
178
179struct WebPPicture;
180
181// Copy width x height pixels from 'src' to 'dst' honoring the strides.
182WEBP_EXTERN void WebPCopyPlane(const uint8_t* src, int src_stride,
183 uint8_t* dst, int dst_stride,
184 int width, int height);
185
186// Copy ARGB pixels from 'src' to 'dst' honoring strides. 'src' and 'dst' are
187// assumed to be already allocated and using ARGB data.
188WEBP_EXTERN void WebPCopyPixels(const struct WebPPicture* const src,
189 struct WebPPicture* const dst);
190
191//------------------------------------------------------------------------------
192// Unique colors.
193
194// Returns count of unique colors in 'pic', assuming pic->use_argb is true.
195// If the unique color count is more than MAX_PALETTE_SIZE, returns
196// MAX_PALETTE_SIZE+1.
197// If 'palette' is not NULL and number of unique colors is less than or equal to
198// MAX_PALETTE_SIZE, also outputs the actual unique colors into 'palette'.
199// Note: 'palette' is assumed to be an array already allocated with at least
200// MAX_PALETTE_SIZE elements.
201WEBP_EXTERN int WebPGetColorPalette(const struct WebPPicture* const pic,
202 uint32_t* const palette);
203
204//------------------------------------------------------------------------------
205
206#ifdef __cplusplus
207} // extern "C"
208#endif
209
210#endif // WEBP_UTILS_UTILS_H_
211