blob: 624bc1750b4429f98065554ae822d259580582a8 [file] [log] [blame]
Vikas Aroraa2415722012-08-09 16:18:58 -07001// Copyright 2012 Google Inc. All Rights Reserved.
2//
Vikas Arora0406ce12013-08-09 15:57:12 -07003// 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.
Vikas Aroraa2415722012-08-09 16:18:58 -07008// -----------------------------------------------------------------------------
9//
10// Utilities for building and looking up Huffman trees.
11//
12// Author: Urvang Joshi (urvang@google.com)
13
14#ifndef WEBP_UTILS_HUFFMAN_H_
15#define WEBP_UTILS_HUFFMAN_H_
16
17#include <assert.h>
James Zern9e80ee92015-03-17 18:54:21 -070018#include "../webp/format_constants.h"
19#include "../webp/types.h"
Vikas Aroraa2415722012-08-09 16:18:58 -070020
Vikas Arora8b720222014-01-02 16:48:02 -080021#ifdef __cplusplus
Vikas Aroraa2415722012-08-09 16:18:58 -070022extern "C" {
23#endif
24
25// A node of a Huffman tree.
26typedef struct {
27 int symbol_;
28 int children_; // delta offset to both children (contiguous) or 0 if leaf.
29} HuffmanTreeNode;
30
31// Huffman Tree.
Vikas Arora8b720222014-01-02 16:48:02 -080032#define HUFF_LUT_BITS 7
33#define HUFF_LUT (1U << HUFF_LUT_BITS)
Vikas Aroraa2415722012-08-09 16:18:58 -070034typedef struct HuffmanTree HuffmanTree;
35struct HuffmanTree {
Vikas Arora8b720222014-01-02 16:48:02 -080036 // Fast lookup for short bit lengths.
37 uint8_t lut_bits_[HUFF_LUT];
38 int16_t lut_symbol_[HUFF_LUT];
39 int16_t lut_jump_[HUFF_LUT];
40 // Complete tree for lookups.
Vikas Aroraa2415722012-08-09 16:18:58 -070041 HuffmanTreeNode* root_; // all the nodes, starting at root.
42 int max_nodes_; // max number of nodes
43 int num_nodes_; // number of currently occupied nodes
44};
45
Vikas Arora33f74da2014-07-25 13:53:32 -070046// Huffman Tree group.
47typedef struct HTreeGroup HTreeGroup;
48struct HTreeGroup {
49 HuffmanTree htrees_[HUFFMAN_CODES_PER_META_CODE];
50};
51
Vikas Arora8b720222014-01-02 16:48:02 -080052// Returns true if the given node is not a leaf of the Huffman tree.
53static WEBP_INLINE int HuffmanTreeNodeIsNotLeaf(
Vikas Aroraa2415722012-08-09 16:18:58 -070054 const HuffmanTreeNode* const node) {
Vikas Arora8b720222014-01-02 16:48:02 -080055 return node->children_;
Vikas Aroraa2415722012-08-09 16:18:58 -070056}
57
58// Go down one level. Most critical function. 'right_child' must be 0 or 1.
59static WEBP_INLINE const HuffmanTreeNode* HuffmanTreeNextNode(
60 const HuffmanTreeNode* node, int right_child) {
61 return node + node->children_ + right_child;
62}
63
64// Releases the nodes of the Huffman tree.
65// Note: It does NOT free 'tree' itself.
Vikas Arora33f74da2014-07-25 13:53:32 -070066void VP8LHuffmanTreeFree(HuffmanTree* const tree);
67
68// Creates the instance of HTreeGroup with specified number of tree-groups.
69HTreeGroup* VP8LHtreeGroupsNew(int num_htree_groups);
70
71// Releases the memory allocated for HTreeGroup.
72void VP8LHtreeGroupsFree(HTreeGroup* htree_groups, int num_htree_groups);
Vikas Aroraa2415722012-08-09 16:18:58 -070073
74// Builds Huffman tree assuming code lengths are implicitly in symbol order.
Vikas Arora33f74da2014-07-25 13:53:32 -070075// The 'huff_codes' and 'code_lengths' are pre-allocated temporary memory
76// buffers, used for creating the huffman tree.
Vikas Aroraa2415722012-08-09 16:18:58 -070077// Returns false in case of error (invalid tree or memory error).
Vikas Arora33f74da2014-07-25 13:53:32 -070078int VP8LHuffmanTreeBuildImplicit(HuffmanTree* const tree,
79 const int* const code_lengths,
80 int* const huff_codes,
81 int code_lengths_size);
Vikas Aroraa2415722012-08-09 16:18:58 -070082
83// Build a Huffman tree with explicitly given lists of code lengths, codes
84// and symbols. Verifies that all symbols added are smaller than max_symbol.
85// Returns false in case of an invalid symbol, invalid tree or memory error.
Vikas Arora33f74da2014-07-25 13:53:32 -070086int VP8LHuffmanTreeBuildExplicit(HuffmanTree* const tree,
87 const int* const code_lengths,
88 const int* const codes,
89 const int* const symbols, int max_symbol,
90 int num_symbols);
Vikas Aroraa2415722012-08-09 16:18:58 -070091
92// Utility: converts Huffman code lengths to corresponding Huffman codes.
93// 'huff_codes' should be pre-allocated.
94// Returns false in case of error (memory allocation, invalid codes).
Vikas Arora33f74da2014-07-25 13:53:32 -070095int VP8LHuffmanCodeLengthsToCodes(const int* const code_lengths,
96 int code_lengths_size, int* const huff_codes);
Vikas Aroraa2415722012-08-09 16:18:58 -070097
Vikas Arora8b720222014-01-02 16:48:02 -080098#ifdef __cplusplus
Vikas Aroraa2415722012-08-09 16:18:58 -070099} // extern "C"
100#endif
101
102#endif // WEBP_UTILS_HUFFMAN_H_