mirror of
https://github.com/webmproject/libwebp.git
synced 2024-12-28 14:38:21 +01:00
add utils/huffman.[hc] from experimental
Pulled from the current HEAD (218c32e). The history of this and related files is a bit entangled so rather trying to split the changes and introduce some noise in master's history we'll start with a fresh snapshot. The file progression is still available in the experimental branch. Change-Id: Ie57be21bf50ad83808c72aeb5fc706d9954d01d8
This commit is contained in:
parent
337914a036
commit
9c67291d67
227
src/utils/huffman.c
Normal file
227
src/utils/huffman.c
Normal file
@ -0,0 +1,227 @@
|
|||||||
|
// Copyright 2012 Google Inc. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// This code is licensed under the same terms as WebM:
|
||||||
|
// Software License Agreement: http://www.webmproject.org/license/software/
|
||||||
|
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
//
|
||||||
|
// A utility for reading Canonical Huffman Codes.
|
||||||
|
//
|
||||||
|
// Author: urvang@google.com (Urvang Joshi)
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "./huffman.h"
|
||||||
|
|
||||||
|
#if defined(__cplusplus) || defined(c_plusplus)
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define MAX_ALLOWED_CODE_LENGTH 15
|
||||||
|
#define NON_EXISTENT_SYMBOL (-1)
|
||||||
|
|
||||||
|
static void TreeNodeInit(HuffmanTreeNode* const node) {
|
||||||
|
node->children_ = -1; // means: 'unassigned so far'
|
||||||
|
}
|
||||||
|
|
||||||
|
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_ = 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;
|
||||||
|
// 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.
|
||||||
|
tree->max_nodes_ = 2 * num_leaves - 1;
|
||||||
|
tree->root_ =
|
||||||
|
(HuffmanTreeNode*)malloc(tree->max_nodes_ * sizeof(*tree->root_));
|
||||||
|
if (tree->root_ == NULL) return 0;
|
||||||
|
TreeNodeInit(tree->root_); // Initialize root.
|
||||||
|
tree->num_nodes_ = 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void HuffmanTreeRelease(HuffmanTree* const tree) {
|
||||||
|
if (tree != NULL) {
|
||||||
|
free(tree->root_);
|
||||||
|
tree->root_ = NULL;
|
||||||
|
tree->max_nodes_ = 0;
|
||||||
|
tree->num_nodes_ = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int HuffmanCodeLengthsToCodes(const int* const code_lengths,
|
||||||
|
int code_lengths_size, int* const huff_codes) {
|
||||||
|
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);
|
||||||
|
|
||||||
|
// Calculate max code length.
|
||||||
|
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;
|
||||||
|
|
||||||
|
// Calculate code length histogram.
|
||||||
|
for (symbol = 0; symbol < code_lengths_size; ++symbol) {
|
||||||
|
++code_length_hist[code_lengths[symbol]];
|
||||||
|
}
|
||||||
|
code_length_hist[0] = 0;
|
||||||
|
|
||||||
|
// 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'.
|
||||||
|
curr_code = 0;
|
||||||
|
next_codes[0] = -1; // Unused, as code length = 0 implies code doesn't exist.
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get symbols.
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int TreeAddSymbol(HuffmanTree* const tree,
|
||||||
|
int symbol, int code, int code_length) {
|
||||||
|
HuffmanTreeNode* node = tree->root_;
|
||||||
|
const HuffmanTreeNode* const max_node = tree->root_ + tree->max_nodes_;
|
||||||
|
while (code_length-- > 0) {
|
||||||
|
if (node >= max_node) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (NodeIsEmpty(node)) {
|
||||||
|
if (IsFull(tree)) return 0; // error: too many symbols.
|
||||||
|
AssignChildren(tree, node);
|
||||||
|
} else if (HuffmanTreeNodeIsLeaf(node)) {
|
||||||
|
return 0; // leaf is already occupied.
|
||||||
|
}
|
||||||
|
node += node->children_ + ((code >> code_length) & 1);
|
||||||
|
}
|
||||||
|
if (NodeIsEmpty(node)) {
|
||||||
|
node->children_ = 0; // turn newly created node into a leaf.
|
||||||
|
} else if (!HuffmanTreeNodeIsLeaf(node)) {
|
||||||
|
return 0; // trying to assign a symbol to already used code.
|
||||||
|
}
|
||||||
|
node->symbol_ = symbol; // Add symbol in this node.
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int HuffmanTreeBuildImplicit(HuffmanTree* const tree,
|
||||||
|
const int* const code_lengths,
|
||||||
|
int code_lengths_size) {
|
||||||
|
int symbol;
|
||||||
|
int num_symbols = 0;
|
||||||
|
int root_symbol = 0;
|
||||||
|
|
||||||
|
assert(tree != NULL);
|
||||||
|
assert(code_lengths != NULL);
|
||||||
|
|
||||||
|
// Find out number of symbols and the root symbol.
|
||||||
|
for (symbol = 0; symbol < code_lengths_size; ++symbol) {
|
||||||
|
if (code_lengths[symbol] > 0) {
|
||||||
|
// Note: code length = 0 indicates non-existent symbol.
|
||||||
|
++num_symbols;
|
||||||
|
root_symbol = symbol;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize the tree. Will fail for num_symbols = 0
|
||||||
|
if (!TreeInit(tree, num_symbols)) return 0;
|
||||||
|
|
||||||
|
// Build tree.
|
||||||
|
if (num_symbols == 1) { // Trivial case.
|
||||||
|
return TreeAddSymbol(tree, root_symbol, 0, 0);
|
||||||
|
} else { // Normal case.
|
||||||
|
int ok = 0;
|
||||||
|
|
||||||
|
// Get Huffman codes from the code lengths.
|
||||||
|
int* const codes = (int*)malloc(code_lengths_size * sizeof(*codes));
|
||||||
|
if (codes == NULL) goto End;
|
||||||
|
|
||||||
|
if (!HuffmanCodeLengthsToCodes(code_lengths, code_lengths_size, codes)) {
|
||||||
|
goto End;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add symbols one-by-one.
|
||||||
|
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);
|
||||||
|
if (!ok) HuffmanTreeRelease(tree);
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int HuffmanTreeBuildExplicit(HuffmanTree* const tree,
|
||||||
|
const int* const code_lengths,
|
||||||
|
const int* const codes,
|
||||||
|
const int* const symbols,
|
||||||
|
int num_symbols) {
|
||||||
|
int ok = 0;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
assert(tree != NULL);
|
||||||
|
assert(code_lengths != NULL);
|
||||||
|
assert(codes != NULL);
|
||||||
|
assert(symbols != NULL);
|
||||||
|
|
||||||
|
// Initialize the tree. Will fail if num_symbols = 0.
|
||||||
|
if (!TreeInit(tree, num_symbols)) return 0;
|
||||||
|
|
||||||
|
// Add symbols one-by-one.
|
||||||
|
for (i = 0; i < num_symbols; ++i) {
|
||||||
|
if (codes[i] != NON_EXISTENT_SYMBOL) {
|
||||||
|
if (!TreeAddSymbol(tree, symbols[i], codes[i], code_lengths[i])) {
|
||||||
|
goto End;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ok = 1;
|
||||||
|
End:
|
||||||
|
ok = ok && IsFull(tree);
|
||||||
|
if (!ok) HuffmanTreeRelease(tree);
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(__cplusplus) || defined(c_plusplus)
|
||||||
|
} // extern "C"
|
||||||
|
#endif
|
79
src/utils/huffman.h
Normal file
79
src/utils/huffman.h
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
// Copyright 2012 Google Inc. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// This code is licensed under the same terms as WebM:
|
||||||
|
// Software License Agreement: http://www.webmproject.org/license/software/
|
||||||
|
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
//
|
||||||
|
// A utility for building and reading looking up trees for Huffman coding.
|
||||||
|
//
|
||||||
|
// Author: urvang@google.com (Urvang Joshi)
|
||||||
|
|
||||||
|
#ifndef WEBP_UTILS_HUFFMAN_H_
|
||||||
|
#define WEBP_UTILS_HUFFMAN_H_
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include "../webp/types.h"
|
||||||
|
|
||||||
|
#if defined(__cplusplus) || defined(c_plusplus)
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// A node of a Huffman tree.
|
||||||
|
typedef struct {
|
||||||
|
int symbol_;
|
||||||
|
int children_; // delta offset to both children (contiguous) or 0 if leaf.
|
||||||
|
} HuffmanTreeNode;
|
||||||
|
|
||||||
|
// Huffman Tree.
|
||||||
|
typedef struct HuffmanTree HuffmanTree;
|
||||||
|
struct HuffmanTree {
|
||||||
|
HuffmanTreeNode* root_; // all the nodes, starting at root.
|
||||||
|
int max_nodes_; // max number of nodes
|
||||||
|
int num_nodes_; // number of currently occupied nodes
|
||||||
|
};
|
||||||
|
|
||||||
|
// Returns true if the given node is a leaf of the Huffman tree.
|
||||||
|
static WEBP_INLINE int HuffmanTreeNodeIsLeaf(
|
||||||
|
const HuffmanTreeNode* const node) {
|
||||||
|
return (node->children_ == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Go down one level. Most critical function. 'right_child' must be 0 or 1.
|
||||||
|
static WEBP_INLINE const HuffmanTreeNode* HuffmanTreeNextNode(
|
||||||
|
const HuffmanTreeNode* node, int right_child) {
|
||||||
|
return node + node->children_ + right_child;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Releases the nodes of the Huffman tree.
|
||||||
|
// Note: It does NOT free 'tree' itself.
|
||||||
|
void HuffmanTreeRelease(HuffmanTree* const tree);
|
||||||
|
|
||||||
|
// Builds Huffman tree assuming code lengths are implicitly in symbol order.
|
||||||
|
// Returns false in case of error (invalid tree or memory error).
|
||||||
|
int HuffmanTreeBuildImplicit(HuffmanTree* const tree,
|
||||||
|
const int* const code_lengths,
|
||||||
|
int code_lengths_size);
|
||||||
|
|
||||||
|
// Build a Huffman tree with explicitly given lists of code lengths, codes
|
||||||
|
// and symbols.
|
||||||
|
// Returns false in case of error (invalid tree or memory error).
|
||||||
|
int HuffmanTreeBuildExplicit(HuffmanTree* const tree,
|
||||||
|
const int* const code_lengths,
|
||||||
|
const int* const codes,
|
||||||
|
const int* const symbols,
|
||||||
|
int num_symbols);
|
||||||
|
|
||||||
|
// 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).
|
||||||
|
int HuffmanCodeLengthsToCodes(const int* const code_lengths,
|
||||||
|
int code_lengths_size, int* const huff_codes);
|
||||||
|
|
||||||
|
|
||||||
|
#if defined(__cplusplus) || defined(c_plusplus)
|
||||||
|
} // extern "C"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // WEBP_UTILS_HUFFMAN_H_
|
Loading…
Reference in New Issue
Block a user