Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libwebp/src/utils/huffman_encode_utils.c
20997 views
1
// Copyright 2011 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
// Author: Jyrki Alakuijala ([email protected])
11
//
12
// Entropy encoding (Huffman) for webp lossless.
13
14
#include <assert.h>
15
#include <stdlib.h>
16
#include <string.h>
17
18
#include "src/utils/huffman_encode_utils.h"
19
#include "src/webp/types.h"
20
#include "src/utils/utils.h"
21
#include "src/webp/format_constants.h"
22
23
// -----------------------------------------------------------------------------
24
// Util function to optimize the symbol map for RLE coding
25
26
// Heuristics for selecting the stride ranges to collapse.
27
static int ValuesShouldBeCollapsedToStrideAverage(int a, int b) {
28
return abs(a - b) < 4;
29
}
30
31
// Change the population counts in a way that the consequent
32
// Huffman tree compression, especially its RLE-part, give smaller output.
33
static void OptimizeHuffmanForRle(int length, uint8_t* const good_for_rle,
34
uint32_t* const counts) {
35
// 1) Let's make the Huffman code more compatible with rle encoding.
36
int i;
37
for (; length >= 0; --length) {
38
if (length == 0) {
39
return; // All zeros.
40
}
41
if (counts[length - 1] != 0) {
42
// Now counts[0..length - 1] does not have trailing zeros.
43
break;
44
}
45
}
46
// 2) Let's mark all population counts that already can be encoded
47
// with an rle code.
48
{
49
// Let's not spoil any of the existing good rle codes.
50
// Mark any seq of 0's that is longer as 5 as a good_for_rle.
51
// Mark any seq of non-0's that is longer as 7 as a good_for_rle.
52
uint32_t symbol = counts[0];
53
int stride = 0;
54
for (i = 0; i < length + 1; ++i) {
55
if (i == length || counts[i] != symbol) {
56
if ((symbol == 0 && stride >= 5) ||
57
(symbol != 0 && stride >= 7)) {
58
int k;
59
for (k = 0; k < stride; ++k) {
60
good_for_rle[i - k - 1] = 1;
61
}
62
}
63
stride = 1;
64
if (i != length) {
65
symbol = counts[i];
66
}
67
} else {
68
++stride;
69
}
70
}
71
}
72
// 3) Let's replace those population counts that lead to more rle codes.
73
{
74
uint32_t stride = 0;
75
uint32_t limit = counts[0];
76
uint32_t sum = 0;
77
for (i = 0; i < length + 1; ++i) {
78
if (i == length || good_for_rle[i] ||
79
(i != 0 && good_for_rle[i - 1]) ||
80
!ValuesShouldBeCollapsedToStrideAverage(counts[i], limit)) {
81
if (stride >= 4 || (stride >= 3 && sum == 0)) {
82
uint32_t k;
83
// The stride must end, collapse what we have, if we have enough (4).
84
uint32_t count = (sum + stride / 2) / stride;
85
if (count < 1) {
86
count = 1;
87
}
88
if (sum == 0) {
89
// Don't make an all zeros stride to be upgraded to ones.
90
count = 0;
91
}
92
for (k = 0; k < stride; ++k) {
93
// We don't want to change value at counts[i],
94
// that is already belonging to the next stride. Thus - 1.
95
counts[i - k - 1] = count;
96
}
97
}
98
stride = 0;
99
sum = 0;
100
if (i < length - 3) {
101
// All interesting strides have a count of at least 4,
102
// at least when non-zeros.
103
limit = (counts[i] + counts[i + 1] +
104
counts[i + 2] + counts[i + 3] + 2) / 4;
105
} else if (i < length) {
106
limit = counts[i];
107
} else {
108
limit = 0;
109
}
110
}
111
++stride;
112
if (i != length) {
113
sum += counts[i];
114
if (stride >= 4) {
115
limit = (sum + stride / 2) / stride;
116
}
117
}
118
}
119
}
120
}
121
122
// A comparer function for two Huffman trees: sorts first by 'total count'
123
// (more comes first), and then by 'value' (more comes first).
124
static int CompareHuffmanTrees(const void* ptr1, const void* ptr2) {
125
const HuffmanTree* const t1 = (const HuffmanTree*)ptr1;
126
const HuffmanTree* const t2 = (const HuffmanTree*)ptr2;
127
if (t1->total_count > t2->total_count) {
128
return -1;
129
} else if (t1->total_count < t2->total_count) {
130
return 1;
131
} else {
132
assert(t1->value != t2->value);
133
return (t1->value < t2->value) ? -1 : 1;
134
}
135
}
136
137
static void SetBitDepths(const HuffmanTree* const tree,
138
const HuffmanTree* const pool,
139
uint8_t* const bit_depths, int level) {
140
if (tree->pool_index_left >= 0) {
141
SetBitDepths(&pool[tree->pool_index_left], pool, bit_depths, level + 1);
142
SetBitDepths(&pool[tree->pool_index_right], pool, bit_depths, level + 1);
143
} else {
144
bit_depths[tree->value] = level;
145
}
146
}
147
148
// Create an optimal Huffman tree.
149
//
150
// (data,length): population counts.
151
// tree_limit: maximum bit depth (inclusive) of the codes.
152
// bit_depths[]: how many bits are used for the symbol.
153
//
154
// Returns 0 when an error has occurred.
155
//
156
// The catch here is that the tree cannot be arbitrarily deep
157
//
158
// count_limit is the value that is to be faked as the minimum value
159
// and this minimum value is raised until the tree matches the
160
// maximum length requirement.
161
//
162
// This algorithm is not of excellent performance for very long data blocks,
163
// especially when population counts are longer than 2**tree_limit, but
164
// we are not planning to use this with extremely long blocks.
165
//
166
// See https://en.wikipedia.org/wiki/Huffman_coding
167
static void GenerateOptimalTree(const uint32_t* const histogram,
168
int histogram_size,
169
HuffmanTree* tree, int tree_depth_limit,
170
uint8_t* const bit_depths) {
171
uint32_t count_min;
172
HuffmanTree* tree_pool;
173
int tree_size_orig = 0;
174
int i;
175
176
for (i = 0; i < histogram_size; ++i) {
177
if (histogram[i] != 0) {
178
++tree_size_orig;
179
}
180
}
181
182
if (tree_size_orig == 0) { // pretty optimal already!
183
return;
184
}
185
186
tree_pool = tree + tree_size_orig;
187
188
// For block sizes with less than 64k symbols we never need to do a
189
// second iteration of this loop.
190
// If we actually start running inside this loop a lot, we would perhaps
191
// be better off with the Katajainen algorithm.
192
assert(tree_size_orig <= (1 << (tree_depth_limit - 1)));
193
for (count_min = 1; ; count_min *= 2) {
194
int tree_size = tree_size_orig;
195
// We need to pack the Huffman tree in tree_depth_limit bits.
196
// So, we try by faking histogram entries to be at least 'count_min'.
197
int idx = 0;
198
int j;
199
for (j = 0; j < histogram_size; ++j) {
200
if (histogram[j] != 0) {
201
const uint32_t count =
202
(histogram[j] < count_min) ? count_min : histogram[j];
203
tree[idx].total_count = count;
204
tree[idx].value = j;
205
tree[idx].pool_index_left = -1;
206
tree[idx].pool_index_right = -1;
207
++idx;
208
}
209
}
210
211
// Build the Huffman tree.
212
qsort(tree, tree_size, sizeof(*tree), CompareHuffmanTrees);
213
214
if (tree_size > 1) { // Normal case.
215
int tree_pool_size = 0;
216
while (tree_size > 1) { // Finish when we have only one root.
217
uint32_t count;
218
tree_pool[tree_pool_size++] = tree[tree_size - 1];
219
tree_pool[tree_pool_size++] = tree[tree_size - 2];
220
count = tree_pool[tree_pool_size - 1].total_count +
221
tree_pool[tree_pool_size - 2].total_count;
222
tree_size -= 2;
223
{
224
// Search for the insertion point.
225
int k;
226
for (k = 0; k < tree_size; ++k) {
227
if (tree[k].total_count <= count) {
228
break;
229
}
230
}
231
memmove(tree + (k + 1), tree + k, (tree_size - k) * sizeof(*tree));
232
tree[k].total_count = count;
233
tree[k].value = -1;
234
235
tree[k].pool_index_left = tree_pool_size - 1;
236
tree[k].pool_index_right = tree_pool_size - 2;
237
tree_size = tree_size + 1;
238
}
239
}
240
SetBitDepths(&tree[0], tree_pool, bit_depths, 0);
241
} else if (tree_size == 1) { // Trivial case: only one element.
242
bit_depths[tree[0].value] = 1;
243
}
244
245
{
246
// Test if this Huffman tree satisfies our 'tree_depth_limit' criteria.
247
int max_depth = bit_depths[0];
248
for (j = 1; j < histogram_size; ++j) {
249
if (max_depth < bit_depths[j]) {
250
max_depth = bit_depths[j];
251
}
252
}
253
if (max_depth <= tree_depth_limit) {
254
break;
255
}
256
}
257
}
258
}
259
260
// -----------------------------------------------------------------------------
261
// Coding of the Huffman tree values
262
263
static HuffmanTreeToken* CodeRepeatedValues(int repetitions,
264
HuffmanTreeToken* tokens,
265
int value, int prev_value) {
266
assert(value <= MAX_ALLOWED_CODE_LENGTH);
267
if (value != prev_value) {
268
tokens->code = value;
269
tokens->extra_bits = 0;
270
++tokens;
271
--repetitions;
272
}
273
while (repetitions >= 1) {
274
if (repetitions < 3) {
275
int i;
276
for (i = 0; i < repetitions; ++i) {
277
tokens->code = value;
278
tokens->extra_bits = 0;
279
++tokens;
280
}
281
break;
282
} else if (repetitions < 7) {
283
tokens->code = 16;
284
tokens->extra_bits = repetitions - 3;
285
++tokens;
286
break;
287
} else {
288
tokens->code = 16;
289
tokens->extra_bits = 3;
290
++tokens;
291
repetitions -= 6;
292
}
293
}
294
return tokens;
295
}
296
297
static HuffmanTreeToken* CodeRepeatedZeros(int repetitions,
298
HuffmanTreeToken* tokens) {
299
while (repetitions >= 1) {
300
if (repetitions < 3) {
301
int i;
302
for (i = 0; i < repetitions; ++i) {
303
tokens->code = 0; // 0-value
304
tokens->extra_bits = 0;
305
++tokens;
306
}
307
break;
308
} else if (repetitions < 11) {
309
tokens->code = 17;
310
tokens->extra_bits = repetitions - 3;
311
++tokens;
312
break;
313
} else if (repetitions < 139) {
314
tokens->code = 18;
315
tokens->extra_bits = repetitions - 11;
316
++tokens;
317
break;
318
} else {
319
tokens->code = 18;
320
tokens->extra_bits = 0x7f; // 138 repeated 0s
321
++tokens;
322
repetitions -= 138;
323
}
324
}
325
return tokens;
326
}
327
328
int VP8LCreateCompressedHuffmanTree(const HuffmanTreeCode* const tree,
329
HuffmanTreeToken* tokens, int max_tokens) {
330
HuffmanTreeToken* const starting_token = tokens;
331
HuffmanTreeToken* const ending_token = tokens + max_tokens;
332
const int depth_size = tree->num_symbols;
333
int prev_value = 8; // 8 is the initial value for rle.
334
int i = 0;
335
assert(tokens != NULL);
336
while (i < depth_size) {
337
const int value = tree->code_lengths[i];
338
int k = i + 1;
339
int runs;
340
while (k < depth_size && tree->code_lengths[k] == value) ++k;
341
runs = k - i;
342
if (value == 0) {
343
tokens = CodeRepeatedZeros(runs, tokens);
344
} else {
345
tokens = CodeRepeatedValues(runs, tokens, value, prev_value);
346
prev_value = value;
347
}
348
i += runs;
349
assert(tokens <= ending_token);
350
}
351
(void)ending_token; // suppress 'unused variable' warning
352
return (int)(tokens - starting_token);
353
}
354
355
// -----------------------------------------------------------------------------
356
357
// Pre-reversed 4-bit values.
358
static const uint8_t kReversedBits[16] = {
359
0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
360
0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
361
};
362
363
static uint32_t ReverseBits(int num_bits, uint32_t bits) {
364
uint32_t retval = 0;
365
int i = 0;
366
while (i < num_bits) {
367
i += 4;
368
retval |= kReversedBits[bits & 0xf] << (MAX_ALLOWED_CODE_LENGTH + 1 - i);
369
bits >>= 4;
370
}
371
retval >>= (MAX_ALLOWED_CODE_LENGTH + 1 - num_bits);
372
return retval;
373
}
374
375
// Get the actual bit values for a tree of bit depths.
376
static void ConvertBitDepthsToSymbols(HuffmanTreeCode* const tree) {
377
// 0 bit-depth means that the symbol does not exist.
378
int i;
379
int len;
380
uint32_t next_code[MAX_ALLOWED_CODE_LENGTH + 1];
381
int depth_count[MAX_ALLOWED_CODE_LENGTH + 1] = { 0 };
382
383
assert(tree != NULL);
384
len = tree->num_symbols;
385
for (i = 0; i < len; ++i) {
386
const int code_length = tree->code_lengths[i];
387
assert(code_length <= MAX_ALLOWED_CODE_LENGTH);
388
++depth_count[code_length];
389
}
390
depth_count[0] = 0; // ignore unused symbol
391
next_code[0] = 0;
392
{
393
uint32_t code = 0;
394
for (i = 1; i <= MAX_ALLOWED_CODE_LENGTH; ++i) {
395
code = (code + depth_count[i - 1]) << 1;
396
next_code[i] = code;
397
}
398
}
399
for (i = 0; i < len; ++i) {
400
const int code_length = tree->code_lengths[i];
401
tree->codes[i] = ReverseBits(code_length, next_code[code_length]++);
402
}
403
}
404
405
// -----------------------------------------------------------------------------
406
// Main entry point
407
408
void VP8LCreateHuffmanTree(uint32_t* const histogram, int tree_depth_limit,
409
uint8_t* const buf_rle, HuffmanTree* const huff_tree,
410
HuffmanTreeCode* const huff_code) {
411
const int num_symbols = huff_code->num_symbols;
412
memset(buf_rle, 0, num_symbols * sizeof(*buf_rle));
413
OptimizeHuffmanForRle(num_symbols, buf_rle, histogram);
414
GenerateOptimalTree(histogram, num_symbols, huff_tree, tree_depth_limit,
415
huff_code->code_lengths);
416
// Create the actual bit codes for the bit lengths.
417
ConvertBitDepthsToSymbols(huff_code);
418
}
419
420