2013-12-16 14:45:57 +01:00
|
|
|
/* Copyright 2013 Google Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
|
|
Utilities for building and looking up Huffman trees.
|
|
|
|
|
*/
|
2013-10-11 10:26:07 +02:00
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include "./huffman.h"
|
|
|
|
|
#include "./safe_malloc.h"
|
|
|
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#define NON_EXISTENT_SYMBOL (-1)
|
|
|
|
|
#define MAX_ALLOWED_CODE_LENGTH 15
|
|
|
|
|
|
|
|
|
|
static void TreeNodeInit(HuffmanTreeNode* const node) {
|
2013-12-16 14:45:57 +01:00
|
|
|
node->children_ = -1; /* means: 'unassigned so far' */
|
2013-10-11 10:26:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int NodeIsEmpty(const HuffmanTreeNode* const node) {
|
|
|
|
|
return (node->children_ < 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int IsFull(const HuffmanTree* const tree) {
|
|
|
|
|
return (tree->num_nodes_ == tree->max_nodes_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void AssignChildren(HuffmanTree* const tree,
|
|
|
|
|
HuffmanTreeNode* const node) {
|
|
|
|
|
HuffmanTreeNode* const children = tree->root_ + tree->num_nodes_;
|
|
|
|
|
node->children_ = (int)(children - node);
|
|
|
|
|
assert(children - node == (int)(children - node));
|
|
|
|
|
tree->num_nodes_ += 2;
|
|
|
|
|
TreeNodeInit(children + 0);
|
|
|
|
|
TreeNodeInit(children + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int TreeInit(HuffmanTree* const tree, int num_leaves) {
|
|
|
|
|
assert(tree != NULL);
|
|
|
|
|
if (num_leaves == 0) return 0;
|
2013-12-16 14:45:57 +01:00
|
|
|
/* We allocate maximum possible nodes in the tree at once. */
|
|
|
|
|
/* Note that a Huffman tree is a full binary tree; and in a full binary */
|
|
|
|
|
/* tree with L leaves, the total number of nodes N = 2 * L - 1. */
|
2013-10-11 10:26:07 +02:00
|
|
|
tree->max_nodes_ = 2 * num_leaves - 1;
|
2013-12-16 14:45:57 +01:00
|
|
|
assert(tree->max_nodes_ < (1 << 16)); /* limit for the lut_jump_ table */
|
2013-10-11 10:26:07 +02:00
|
|
|
tree->root_ = (HuffmanTreeNode*)BrotliSafeMalloc((uint64_t)tree->max_nodes_,
|
|
|
|
|
sizeof(*tree->root_));
|
|
|
|
|
if (tree->root_ == NULL) return 0;
|
2013-12-16 14:45:57 +01:00
|
|
|
TreeNodeInit(tree->root_); /* Initialize root. */
|
2013-10-11 10:26:07 +02:00
|
|
|
tree->num_nodes_ = 1;
|
|
|
|
|
memset(tree->lut_bits_, 255, sizeof(tree->lut_bits_));
|
|
|
|
|
memset(tree->lut_jump_, 0, sizeof(tree->lut_jump_));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-17 12:41:36 +02:00
|
|
|
void BrotliHuffmanTreeRelease(HuffmanTree* const tree) {
|
2013-10-11 10:26:07 +02:00
|
|
|
if (tree != NULL) {
|
|
|
|
|
free(tree->root_);
|
|
|
|
|
tree->root_ = NULL;
|
|
|
|
|
tree->max_nodes_ = 0;
|
|
|
|
|
tree->num_nodes_ = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-16 14:45:57 +01:00
|
|
|
/* Utility: converts Huffman code lengths to corresponding Huffman codes. */
|
|
|
|
|
/* 'huff_codes' should be pre-allocated. */
|
|
|
|
|
/* Returns false in case of error (memory allocation, invalid codes). */
|
2013-11-15 19:02:17 +01:00
|
|
|
static int HuffmanCodeLengthsToCodes(const uint8_t* const code_lengths,
|
2013-10-17 12:41:36 +02:00
|
|
|
int code_lengths_size,
|
|
|
|
|
int* const huff_codes) {
|
2013-10-11 10:26:07 +02:00
|
|
|
int symbol;
|
|
|
|
|
int code_len;
|
|
|
|
|
int code_length_hist[MAX_ALLOWED_CODE_LENGTH + 1] = { 0 };
|
|
|
|
|
int curr_code;
|
|
|
|
|
int next_codes[MAX_ALLOWED_CODE_LENGTH + 1] = { 0 };
|
|
|
|
|
int max_code_length = 0;
|
|
|
|
|
|
|
|
|
|
assert(code_lengths != NULL);
|
|
|
|
|
assert(code_lengths_size > 0);
|
|
|
|
|
assert(huff_codes != NULL);
|
|
|
|
|
|
2013-12-16 14:45:57 +01:00
|
|
|
/* Calculate max code length. */
|
2013-10-11 10:26:07 +02:00
|
|
|
for (symbol = 0; symbol < code_lengths_size; ++symbol) {
|
|
|
|
|
if (code_lengths[symbol] > max_code_length) {
|
|
|
|
|
max_code_length = code_lengths[symbol];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (max_code_length > MAX_ALLOWED_CODE_LENGTH) return 0;
|
|
|
|
|
|
2013-12-16 14:45:57 +01:00
|
|
|
/* Calculate code length histogram. */
|
2013-10-11 10:26:07 +02:00
|
|
|
for (symbol = 0; symbol < code_lengths_size; ++symbol) {
|
|
|
|
|
++code_length_hist[code_lengths[symbol]];
|
|
|
|
|
}
|
|
|
|
|
code_length_hist[0] = 0;
|
|
|
|
|
|
2013-12-16 14:45:57 +01:00
|
|
|
/* Calculate the initial values of 'next_codes' for each code length. */
|
|
|
|
|
/* next_codes[code_len] denotes the code to be assigned to the next symbol */
|
|
|
|
|
/* of code length 'code_len'. */
|
2013-10-11 10:26:07 +02:00
|
|
|
curr_code = 0;
|
2013-12-16 14:45:57 +01:00
|
|
|
next_codes[0] = -1; /* Unused, as code length = 0 implies */
|
|
|
|
|
/* code doesn't exist. */
|
2013-10-11 10:26:07 +02:00
|
|
|
for (code_len = 1; code_len <= max_code_length; ++code_len) {
|
|
|
|
|
curr_code = (curr_code + code_length_hist[code_len - 1]) << 1;
|
|
|
|
|
next_codes[code_len] = curr_code;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-16 14:45:57 +01:00
|
|
|
/* Get symbols. */
|
2013-10-11 10:26:07 +02:00
|
|
|
for (symbol = 0; symbol < code_lengths_size; ++symbol) {
|
|
|
|
|
if (code_lengths[symbol] > 0) {
|
|
|
|
|
huff_codes[symbol] = next_codes[code_lengths[symbol]]++;
|
|
|
|
|
} else {
|
|
|
|
|
huff_codes[symbol] = NON_EXISTENT_SYMBOL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-15 19:02:17 +01:00
|
|
|
static const uint8_t kReverse7[128] = {
|
|
|
|
|
0, 64, 32, 96, 16, 80, 48, 112, 8, 72, 40, 104, 24, 88, 56, 120,
|
|
|
|
|
4, 68, 36, 100, 20, 84, 52, 116, 12, 76, 44, 108, 28, 92, 60, 124,
|
|
|
|
|
2, 66, 34, 98, 18, 82, 50, 114, 10, 74, 42, 106, 26, 90, 58, 122,
|
|
|
|
|
6, 70, 38, 102, 22, 86, 54, 118, 14, 78, 46, 110, 30, 94, 62, 126,
|
|
|
|
|
1, 65, 33, 97, 17, 81, 49, 113, 9, 73, 41, 105, 25, 89, 57, 121,
|
|
|
|
|
5, 69, 37, 101, 21, 85, 53, 117, 13, 77, 45, 109, 29, 93, 61, 125,
|
|
|
|
|
3, 67, 35, 99, 19, 83, 51, 115, 11, 75, 43, 107, 27, 91, 59, 123,
|
|
|
|
|
7, 71, 39, 103, 23, 87, 55, 119, 15, 79, 47, 111, 31, 95, 63, 127
|
2013-10-11 10:26:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int ReverseBitsShort(int bits, int num_bits) {
|
2013-11-15 19:02:17 +01:00
|
|
|
return kReverse7[bits] >> (7 - num_bits);
|
2013-10-11 10:26:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int TreeAddSymbol(HuffmanTree* const tree,
|
|
|
|
|
int symbol, int code, int code_length) {
|
|
|
|
|
int step = HUFF_LUT_BITS;
|
|
|
|
|
int base_code;
|
|
|
|
|
HuffmanTreeNode* node = tree->root_;
|
|
|
|
|
const HuffmanTreeNode* const max_node = tree->root_ + tree->max_nodes_;
|
|
|
|
|
assert(symbol == (int16_t)symbol);
|
|
|
|
|
if (code_length <= HUFF_LUT_BITS) {
|
2013-11-15 19:02:17 +01:00
|
|
|
int i = 1 << (HUFF_LUT_BITS - code_length);
|
2013-10-11 10:26:07 +02:00
|
|
|
base_code = ReverseBitsShort(code, code_length);
|
2013-11-15 19:02:17 +01:00
|
|
|
do {
|
2013-12-13 10:39:46 +01:00
|
|
|
int idx;
|
2013-11-15 19:02:17 +01:00
|
|
|
--i;
|
2013-12-13 10:39:46 +01:00
|
|
|
idx = base_code | (i << code_length);
|
2013-10-11 10:26:07 +02:00
|
|
|
tree->lut_symbol_[idx] = (int16_t)symbol;
|
|
|
|
|
tree->lut_bits_[idx] = code_length;
|
2013-11-15 19:02:17 +01:00
|
|
|
} while (i > 0);
|
2013-10-11 10:26:07 +02:00
|
|
|
} else {
|
|
|
|
|
base_code = ReverseBitsShort((code >> (code_length - HUFF_LUT_BITS)),
|
|
|
|
|
HUFF_LUT_BITS);
|
|
|
|
|
}
|
|
|
|
|
while (code_length-- > 0) {
|
|
|
|
|
if (node >= max_node) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (NodeIsEmpty(node)) {
|
2013-12-16 14:45:57 +01:00
|
|
|
if (IsFull(tree)) return 0; /* error: too many symbols. */
|
2013-10-11 10:26:07 +02:00
|
|
|
AssignChildren(tree, node);
|
|
|
|
|
} else if (!HuffmanTreeNodeIsNotLeaf(node)) {
|
2013-12-16 14:45:57 +01:00
|
|
|
return 0; /* leaf is already occupied. */
|
2013-10-11 10:26:07 +02:00
|
|
|
}
|
|
|
|
|
node += node->children_ + ((code >> code_length) & 1);
|
|
|
|
|
if (--step == 0) {
|
|
|
|
|
tree->lut_jump_[base_code] = (int16_t)(node - tree->root_);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (NodeIsEmpty(node)) {
|
2013-12-16 14:45:57 +01:00
|
|
|
node->children_ = 0; /* turn newly created node into a leaf. */
|
2013-10-11 10:26:07 +02:00
|
|
|
} else if (HuffmanTreeNodeIsNotLeaf(node)) {
|
2013-12-16 14:45:57 +01:00
|
|
|
return 0; /* trying to assign a symbol to already used code. */
|
2013-10-11 10:26:07 +02:00
|
|
|
}
|
2013-12-16 14:45:57 +01:00
|
|
|
node->symbol_ = symbol; /* Add symbol in this node. */
|
2013-10-11 10:26:07 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-17 12:41:36 +02:00
|
|
|
int BrotliHuffmanTreeBuildImplicit(HuffmanTree* const tree,
|
2013-11-15 19:02:17 +01:00
|
|
|
const uint8_t* const code_lengths,
|
2013-10-17 12:41:36 +02:00
|
|
|
int code_lengths_size) {
|
2013-10-11 10:26:07 +02:00
|
|
|
int symbol;
|
|
|
|
|
int num_symbols = 0;
|
|
|
|
|
int root_symbol = 0;
|
|
|
|
|
|
|
|
|
|
assert(tree != NULL);
|
|
|
|
|
assert(code_lengths != NULL);
|
|
|
|
|
|
2013-12-16 14:45:57 +01:00
|
|
|
/* Find out number of symbols and the root symbol. */
|
2013-10-11 10:26:07 +02:00
|
|
|
for (symbol = 0; symbol < code_lengths_size; ++symbol) {
|
|
|
|
|
if (code_lengths[symbol] > 0) {
|
2013-12-16 14:45:57 +01:00
|
|
|
/* Note: code length = 0 indicates non-existent symbol. */
|
2013-10-11 10:26:07 +02:00
|
|
|
++num_symbols;
|
|
|
|
|
root_symbol = symbol;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-16 14:45:57 +01:00
|
|
|
/* Initialize the tree. Will fail for num_symbols = 0 */
|
2013-10-11 10:26:07 +02:00
|
|
|
if (!TreeInit(tree, num_symbols)) return 0;
|
|
|
|
|
|
2013-12-16 14:45:57 +01:00
|
|
|
/* Build tree. */
|
|
|
|
|
if (num_symbols == 1) { /* Trivial case. */
|
2013-10-11 10:26:07 +02:00
|
|
|
const int max_symbol = code_lengths_size;
|
|
|
|
|
if (root_symbol < 0 || root_symbol >= max_symbol) {
|
2013-10-17 12:41:36 +02:00
|
|
|
BrotliHuffmanTreeRelease(tree);
|
2013-10-11 10:26:07 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return TreeAddSymbol(tree, root_symbol, 0, 0);
|
2013-12-16 14:45:57 +01:00
|
|
|
} else { /* Normal case. */
|
2013-10-11 10:26:07 +02:00
|
|
|
int ok = 0;
|
|
|
|
|
|
2013-12-16 14:45:57 +01:00
|
|
|
/* Get Huffman codes from the code lengths. */
|
2013-10-11 10:26:07 +02:00
|
|
|
int* const codes =
|
|
|
|
|
(int*)BrotliSafeMalloc((uint64_t)code_lengths_size, sizeof(*codes));
|
|
|
|
|
if (codes == NULL) goto End;
|
|
|
|
|
|
|
|
|
|
if (!HuffmanCodeLengthsToCodes(code_lengths, code_lengths_size, codes)) {
|
|
|
|
|
goto End;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-16 14:45:57 +01:00
|
|
|
/* Add symbols one-by-one. */
|
2013-10-11 10:26:07 +02:00
|
|
|
for (symbol = 0; symbol < code_lengths_size; ++symbol) {
|
|
|
|
|
if (code_lengths[symbol] > 0) {
|
|
|
|
|
if (!TreeAddSymbol(tree, symbol, codes[symbol], code_lengths[symbol])) {
|
|
|
|
|
goto End;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ok = 1;
|
|
|
|
|
End:
|
|
|
|
|
free(codes);
|
|
|
|
|
ok = ok && IsFull(tree);
|
2013-10-17 12:41:36 +02:00
|
|
|
if (!ok) BrotliHuffmanTreeRelease(tree);
|
2013-10-11 10:26:07 +02:00
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
2013-12-16 14:45:57 +01:00
|
|
|
} /* extern "C" */
|
2013-10-11 10:26:07 +02:00
|
|
|
#endif
|