Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libwebp/src/utils/huffman_utils.c
20959 views
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
// Utilities for building and looking up Huffman trees.
11
//
12
// Author: Urvang Joshi ([email protected])
13
14
#include <assert.h>
15
#include <stdlib.h>
16
#include <string.h>
17
18
#include "src/utils/huffman_utils.h"
19
#include "src/utils/utils.h"
20
#include "src/webp/format_constants.h"
21
#include "src/webp/types.h"
22
23
// Huffman data read via DecodeImageStream is represented in two (red and green)
24
// bytes.
25
#define MAX_HTREE_GROUPS 0x10000
26
27
HTreeGroup* VP8LHtreeGroupsNew(int num_htree_groups) {
28
HTreeGroup* const htree_groups =
29
(HTreeGroup*)WebPSafeMalloc(num_htree_groups, sizeof(*htree_groups));
30
if (htree_groups == NULL) {
31
return NULL;
32
}
33
assert(num_htree_groups <= MAX_HTREE_GROUPS);
34
return htree_groups;
35
}
36
37
void VP8LHtreeGroupsFree(HTreeGroup* const htree_groups) {
38
if (htree_groups != NULL) {
39
WebPSafeFree(htree_groups);
40
}
41
}
42
43
// Returns reverse(reverse(key, len) + 1, len), where reverse(key, len) is the
44
// bit-wise reversal of the len least significant bits of key.
45
static WEBP_INLINE uint32_t GetNextKey(uint32_t key, int len) {
46
uint32_t step = 1 << (len - 1);
47
while (key & step) {
48
step >>= 1;
49
}
50
return step ? (key & (step - 1)) + step : key;
51
}
52
53
// Stores code in table[0], table[step], table[2*step], ..., table[end].
54
// Assumes that end is an integer multiple of step.
55
static WEBP_INLINE void ReplicateValue(HuffmanCode* table,
56
int step, int end,
57
HuffmanCode code) {
58
assert(end % step == 0);
59
do {
60
end -= step;
61
table[end] = code;
62
} while (end > 0);
63
}
64
65
// Returns the table width of the next 2nd level table. count is the histogram
66
// of bit lengths for the remaining symbols, len is the code length of the next
67
// processed symbol
68
static WEBP_INLINE int NextTableBitSize(const int* const count,
69
int len, int root_bits) {
70
int left = 1 << (len - root_bits);
71
while (len < MAX_ALLOWED_CODE_LENGTH) {
72
left -= count[len];
73
if (left <= 0) break;
74
++len;
75
left <<= 1;
76
}
77
return len - root_bits;
78
}
79
80
// sorted[code_lengths_size] is a pre-allocated array for sorting symbols
81
// by code length.
82
static int BuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
83
const int code_lengths[], int code_lengths_size,
84
uint16_t sorted[]) {
85
HuffmanCode* table = root_table; // next available space in table
86
int total_size = 1 << root_bits; // total size root table + 2nd level table
87
int len; // current code length
88
int symbol; // symbol index in original or sorted table
89
// number of codes of each length:
90
int count[MAX_ALLOWED_CODE_LENGTH + 1] = { 0 };
91
// offsets in sorted table for each length:
92
int offset[MAX_ALLOWED_CODE_LENGTH + 1];
93
94
assert(code_lengths_size != 0);
95
assert(code_lengths != NULL);
96
assert((root_table != NULL && sorted != NULL) ||
97
(root_table == NULL && sorted == NULL));
98
assert(root_bits > 0);
99
100
// Build histogram of code lengths.
101
for (symbol = 0; symbol < code_lengths_size; ++symbol) {
102
if (code_lengths[symbol] > MAX_ALLOWED_CODE_LENGTH) {
103
return 0;
104
}
105
++count[code_lengths[symbol]];
106
}
107
108
// Error, all code lengths are zeros.
109
if (count[0] == code_lengths_size) {
110
return 0;
111
}
112
113
// Generate offsets into sorted symbol table by code length.
114
offset[1] = 0;
115
for (len = 1; len < MAX_ALLOWED_CODE_LENGTH; ++len) {
116
if (count[len] > (1 << len)) {
117
return 0;
118
}
119
offset[len + 1] = offset[len] + count[len];
120
}
121
122
// Sort symbols by length, by symbol order within each length.
123
for (symbol = 0; symbol < code_lengths_size; ++symbol) {
124
const int symbol_code_length = code_lengths[symbol];
125
if (code_lengths[symbol] > 0) {
126
if (sorted != NULL) {
127
if(offset[symbol_code_length] >= code_lengths_size) {
128
return 0;
129
}
130
sorted[offset[symbol_code_length]++] = symbol;
131
} else {
132
offset[symbol_code_length]++;
133
}
134
}
135
}
136
137
// Special case code with only one value.
138
if (offset[MAX_ALLOWED_CODE_LENGTH] == 1) {
139
if (sorted != NULL) {
140
HuffmanCode code;
141
code.bits = 0;
142
code.value = (uint16_t)sorted[0];
143
ReplicateValue(table, 1, total_size, code);
144
}
145
return total_size;
146
}
147
148
{
149
int step; // step size to replicate values in current table
150
uint32_t low = 0xffffffffu; // low bits for current root entry
151
uint32_t mask = total_size - 1; // mask for low bits
152
uint32_t key = 0; // reversed prefix code
153
int num_nodes = 1; // number of Huffman tree nodes
154
int num_open = 1; // number of open branches in current tree level
155
int table_bits = root_bits; // key length of current table
156
int table_size = 1 << table_bits; // size of current table
157
symbol = 0;
158
// Fill in root table.
159
for (len = 1, step = 2; len <= root_bits; ++len, step <<= 1) {
160
num_open <<= 1;
161
num_nodes += num_open;
162
num_open -= count[len];
163
if (num_open < 0) {
164
return 0;
165
}
166
if (root_table == NULL) continue;
167
for (; count[len] > 0; --count[len]) {
168
HuffmanCode code;
169
code.bits = (uint8_t)len;
170
code.value = (uint16_t)sorted[symbol++];
171
ReplicateValue(&table[key], step, table_size, code);
172
key = GetNextKey(key, len);
173
}
174
}
175
176
// Fill in 2nd level tables and add pointers to root table.
177
for (len = root_bits + 1, step = 2; len <= MAX_ALLOWED_CODE_LENGTH;
178
++len, step <<= 1) {
179
num_open <<= 1;
180
num_nodes += num_open;
181
num_open -= count[len];
182
if (num_open < 0) {
183
return 0;
184
}
185
for (; count[len] > 0; --count[len]) {
186
HuffmanCode code;
187
if ((key & mask) != low) {
188
if (root_table != NULL) table += table_size;
189
table_bits = NextTableBitSize(count, len, root_bits);
190
table_size = 1 << table_bits;
191
total_size += table_size;
192
low = key & mask;
193
if (root_table != NULL) {
194
root_table[low].bits = (uint8_t)(table_bits + root_bits);
195
root_table[low].value = (uint16_t)((table - root_table) - low);
196
}
197
}
198
if (root_table != NULL) {
199
code.bits = (uint8_t)(len - root_bits);
200
code.value = (uint16_t)sorted[symbol++];
201
ReplicateValue(&table[key >> root_bits], step, table_size, code);
202
}
203
key = GetNextKey(key, len);
204
}
205
}
206
207
// Check if tree is full.
208
if (num_nodes != 2 * offset[MAX_ALLOWED_CODE_LENGTH] - 1) {
209
return 0;
210
}
211
}
212
213
return total_size;
214
}
215
216
// Maximum code_lengths_size is 2328 (reached for 11-bit color_cache_bits).
217
// More commonly, the value is around ~280.
218
#define MAX_CODE_LENGTHS_SIZE \
219
((1 << MAX_CACHE_BITS) + NUM_LITERAL_CODES + NUM_LENGTH_CODES)
220
// Cut-off value for switching between heap and stack allocation.
221
#define SORTED_SIZE_CUTOFF 512
222
int VP8LBuildHuffmanTable(HuffmanTables* const root_table, int root_bits,
223
const int code_lengths[], int code_lengths_size) {
224
const int total_size =
225
BuildHuffmanTable(NULL, root_bits, code_lengths, code_lengths_size, NULL);
226
assert(code_lengths_size <= MAX_CODE_LENGTHS_SIZE);
227
if (total_size == 0 || root_table == NULL) return total_size;
228
229
if (root_table->curr_segment->curr_table + total_size >=
230
root_table->curr_segment->start + root_table->curr_segment->size) {
231
// If 'root_table' does not have enough memory, allocate a new segment.
232
// The available part of root_table->curr_segment is left unused because we
233
// need a contiguous buffer.
234
const int segment_size = root_table->curr_segment->size;
235
struct HuffmanTablesSegment* next =
236
(HuffmanTablesSegment*)WebPSafeMalloc(1, sizeof(*next));
237
if (next == NULL) return 0;
238
// Fill the new segment.
239
// We need at least 'total_size' but if that value is small, it is better to
240
// allocate a big chunk to prevent more allocations later. 'segment_size' is
241
// therefore chosen (any other arbitrary value could be chosen).
242
next->size = total_size > segment_size ? total_size : segment_size;
243
next->start =
244
(HuffmanCode*)WebPSafeMalloc(next->size, sizeof(*next->start));
245
if (next->start == NULL) {
246
WebPSafeFree(next);
247
return 0;
248
}
249
next->curr_table = next->start;
250
next->next = NULL;
251
// Point to the new segment.
252
root_table->curr_segment->next = next;
253
root_table->curr_segment = next;
254
}
255
if (code_lengths_size <= SORTED_SIZE_CUTOFF) {
256
// use local stack-allocated array.
257
uint16_t sorted[SORTED_SIZE_CUTOFF];
258
BuildHuffmanTable(root_table->curr_segment->curr_table, root_bits,
259
code_lengths, code_lengths_size, sorted);
260
} else { // rare case. Use heap allocation.
261
uint16_t* const sorted =
262
(uint16_t*)WebPSafeMalloc(code_lengths_size, sizeof(*sorted));
263
if (sorted == NULL) return 0;
264
BuildHuffmanTable(root_table->curr_segment->curr_table, root_bits,
265
code_lengths, code_lengths_size, sorted);
266
WebPSafeFree(sorted);
267
}
268
return total_size;
269
}
270
271
int VP8LHuffmanTablesAllocate(int size, HuffmanTables* huffman_tables) {
272
// Have 'segment' point to the first segment for now, 'root'.
273
HuffmanTablesSegment* const root = &huffman_tables->root;
274
huffman_tables->curr_segment = root;
275
root->next = NULL;
276
// Allocate root.
277
root->start = (HuffmanCode*)WebPSafeMalloc(size, sizeof(*root->start));
278
if (root->start == NULL) return 0;
279
root->curr_table = root->start;
280
root->size = size;
281
return 1;
282
}
283
284
void VP8LHuffmanTablesDeallocate(HuffmanTables* const huffman_tables) {
285
HuffmanTablesSegment *current, *next;
286
if (huffman_tables == NULL) return;
287
// Free the root node.
288
current = &huffman_tables->root;
289
next = current->next;
290
WebPSafeFree(current->start);
291
current->start = NULL;
292
current->next = NULL;
293
current = next;
294
// Free the following nodes.
295
while (current != NULL) {
296
next = current->next;
297
WebPSafeFree(current->start);
298
WebPSafeFree(current);
299
current = next;
300
}
301
}
302
303