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 "../webp/config.h"
20#endif
21
22#include <assert.h>
23
24#include "../webp/types.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30//------------------------------------------------------------------------------
31// Memory allocation
32
33// This is the maximum memory amount that libwebp will ever try to allocate.
34#define WEBP_MAX_ALLOCABLE_MEMORY (1ULL << 40)
35
36// size-checking safe malloc/calloc: verify that the requested size is not too
37// large, or return NULL. You don't need to call these for constructs like
38// malloc(sizeof(foo)), but only if there's picture-dependent size involved
39// somewhere (like: malloc(num_pixels * sizeof(*something))). That's why this
40// safe malloc() borrows the signature from calloc(), pointing at the dangerous
41// underlying multiply involved.
42WEBP_EXTERN(void*) WebPSafeMalloc(uint64_t nmemb, size_t size);
43// Note that WebPSafeCalloc() expects the second argument type to be 'size_t'
44// in order to favor the "calloc(num_foo, sizeof(foo))" pattern.
45WEBP_EXTERN(void*) WebPSafeCalloc(uint64_t nmemb, size_t size);
46
47// Companion deallocation function to the above allocations.
48WEBP_EXTERN(void) WebPSafeFree(void* const ptr);
49
50//------------------------------------------------------------------------------
51// Alignment
52
53#define WEBP_ALIGN_CST 31
54#define WEBP_ALIGN(PTR) ((uintptr_t)((PTR) + WEBP_ALIGN_CST) & ~WEBP_ALIGN_CST)
55
56#if defined(WEBP_FORCE_ALIGNED)
57#include <string.h>
58// memcpy() is the safe way of moving potentially unaligned 32b memory.
59static WEBP_INLINE uint32_t WebPMemToUint32(const uint8_t* const ptr) {
60  uint32_t A;
61  memcpy(&A, (const int*)ptr, sizeof(A));
62  return A;
63}
64static WEBP_INLINE void WebPUint32ToMem(uint8_t* const ptr, uint32_t val) {
65  memcpy(ptr, &val, sizeof(val));
66}
67#else
68static WEBP_INLINE uint32_t WebPMemToUint32(const uint8_t* const ptr) {
69  return *(const uint32_t*)ptr;
70}
71static WEBP_INLINE void WebPUint32ToMem(uint8_t* const ptr, uint32_t val) {
72  *(uint32_t*)ptr = val;
73}
74#endif
75
76//------------------------------------------------------------------------------
77// Reading/writing data.
78
79// Read 16, 24 or 32 bits stored in little-endian order.
80static WEBP_INLINE int GetLE16(const uint8_t* const data) {
81  return (int)(data[0] << 0) | (data[1] << 8);
82}
83
84static WEBP_INLINE int GetLE24(const uint8_t* const data) {
85  return GetLE16(data) | (data[2] << 16);
86}
87
88static WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) {
89  return GetLE16(data) | ((uint32_t)GetLE16(data + 2) << 16);
90}
91
92// Store 16, 24 or 32 bits in little-endian order.
93static WEBP_INLINE void PutLE16(uint8_t* const data, int val) {
94  assert(val < (1 << 16));
95  data[0] = (val >> 0);
96  data[1] = (val >> 8);
97}
98
99static WEBP_INLINE void PutLE24(uint8_t* const data, int val) {
100  assert(val < (1 << 24));
101  PutLE16(data, val & 0xffff);
102  data[2] = (val >> 16);
103}
104
105static WEBP_INLINE void PutLE32(uint8_t* const data, uint32_t val) {
106  PutLE16(data, (int)(val & 0xffff));
107  PutLE16(data + 2, (int)(val >> 16));
108}
109
110// Returns (int)floor(log2(n)). n must be > 0.
111// use GNU builtins where available.
112#if defined(__GNUC__) && \
113    ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
114static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
115  return 31 ^ __builtin_clz(n);
116}
117#elif defined(_MSC_VER) && _MSC_VER > 1310 && \
118      (defined(_M_X64) || defined(_M_IX86))
119#include <intrin.h>
120#pragma intrinsic(_BitScanReverse)
121
122static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
123  unsigned long first_set_bit;
124  _BitScanReverse(&first_set_bit, n);
125  return first_set_bit;
126}
127#else
128static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
129  int log = 0;
130  uint32_t value = n;
131  int i;
132
133  for (i = 4; i >= 0; --i) {
134    const int shift = (1 << i);
135    const uint32_t x = value >> shift;
136    if (x != 0) {
137      value = x;
138      log += shift;
139    }
140  }
141  return log;
142}
143#endif
144
145//------------------------------------------------------------------------------
146// Pixel copying.
147
148struct WebPPicture;
149
150// Copy width x height pixels from 'src' to 'dst' honoring the strides.
151WEBP_EXTERN(void) WebPCopyPlane(const uint8_t* src, int src_stride,
152                                uint8_t* dst, int dst_stride,
153                                int width, int height);
154
155// Copy ARGB pixels from 'src' to 'dst' honoring strides. 'src' and 'dst' are
156// assumed to be already allocated and using ARGB data.
157WEBP_EXTERN(void) WebPCopyPixels(const struct WebPPicture* const src,
158                                 struct WebPPicture* const dst);
159
160//------------------------------------------------------------------------------
161
162#ifdef __cplusplus
163}    // extern "C"
164#endif
165
166#endif  /* WEBP_UTILS_UTILS_H_ */
167