OLD | NEW |
1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Use of this source code is governed by a BSD-style license | 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 | 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 | 5 // tree. An additional intellectual property rights grant can be found |
6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
9 // | 9 // |
10 // Utilities for building and looking up Huffman trees. | 10 // Utilities for building and looking up Huffman trees. |
11 // | 11 // |
12 // Author: Urvang Joshi (urvang@google.com) | 12 // Author: Urvang Joshi (urvang@google.com) |
13 | 13 |
14 #ifndef WEBP_UTILS_HUFFMAN_H_ | 14 #ifndef WEBP_UTILS_HUFFMAN_H_ |
15 #define WEBP_UTILS_HUFFMAN_H_ | 15 #define WEBP_UTILS_HUFFMAN_H_ |
16 | 16 |
17 #include <assert.h> | 17 #include <assert.h> |
18 #include "../webp/types.h" | 18 #include "../webp/types.h" |
19 | 19 |
20 #if defined(__cplusplus) || defined(c_plusplus) | 20 #ifdef __cplusplus |
21 extern "C" { | 21 extern "C" { |
22 #endif | 22 #endif |
23 | 23 |
24 // A node of a Huffman tree. | 24 // A node of a Huffman tree. |
25 typedef struct { | 25 typedef struct { |
26 int symbol_; | 26 int symbol_; |
27 int children_; // delta offset to both children (contiguous) or 0 if leaf. | 27 int children_; // delta offset to both children (contiguous) or 0 if leaf. |
28 } HuffmanTreeNode; | 28 } HuffmanTreeNode; |
29 | 29 |
30 // Huffman Tree. | 30 // Huffman Tree. |
| 31 #define HUFF_LUT_BITS 7 |
| 32 #define HUFF_LUT (1U << HUFF_LUT_BITS) |
31 typedef struct HuffmanTree HuffmanTree; | 33 typedef struct HuffmanTree HuffmanTree; |
32 struct HuffmanTree { | 34 struct HuffmanTree { |
| 35 // Fast lookup for short bit lengths. |
| 36 uint8_t lut_bits_[HUFF_LUT]; |
| 37 int16_t lut_symbol_[HUFF_LUT]; |
| 38 int16_t lut_jump_[HUFF_LUT]; |
| 39 // Complete tree for lookups. |
33 HuffmanTreeNode* root_; // all the nodes, starting at root. | 40 HuffmanTreeNode* root_; // all the nodes, starting at root. |
34 int max_nodes_; // max number of nodes | 41 int max_nodes_; // max number of nodes |
35 int num_nodes_; // number of currently occupied nodes | 42 int num_nodes_; // number of currently occupied nodes |
36 }; | 43 }; |
37 | 44 |
38 // Returns true if the given node is a leaf of the Huffman tree. | 45 // Returns true if the given node is not a leaf of the Huffman tree. |
39 static WEBP_INLINE int HuffmanTreeNodeIsLeaf( | 46 static WEBP_INLINE int HuffmanTreeNodeIsNotLeaf( |
40 const HuffmanTreeNode* const node) { | 47 const HuffmanTreeNode* const node) { |
41 return (node->children_ == 0); | 48 return node->children_; |
42 } | 49 } |
43 | 50 |
44 // Go down one level. Most critical function. 'right_child' must be 0 or 1. | 51 // Go down one level. Most critical function. 'right_child' must be 0 or 1. |
45 static WEBP_INLINE const HuffmanTreeNode* HuffmanTreeNextNode( | 52 static WEBP_INLINE const HuffmanTreeNode* HuffmanTreeNextNode( |
46 const HuffmanTreeNode* node, int right_child) { | 53 const HuffmanTreeNode* node, int right_child) { |
47 return node + node->children_ + right_child; | 54 return node + node->children_ + right_child; |
48 } | 55 } |
49 | 56 |
50 // Releases the nodes of the Huffman tree. | 57 // Releases the nodes of the Huffman tree. |
51 // Note: It does NOT free 'tree' itself. | 58 // Note: It does NOT free 'tree' itself. |
(...skipping 14 matching lines...) Expand all Loading... |
66 const int* const symbols, int max_symbol, | 73 const int* const symbols, int max_symbol, |
67 int num_symbols); | 74 int num_symbols); |
68 | 75 |
69 // Utility: converts Huffman code lengths to corresponding Huffman codes. | 76 // Utility: converts Huffman code lengths to corresponding Huffman codes. |
70 // 'huff_codes' should be pre-allocated. | 77 // 'huff_codes' should be pre-allocated. |
71 // Returns false in case of error (memory allocation, invalid codes). | 78 // Returns false in case of error (memory allocation, invalid codes). |
72 int HuffmanCodeLengthsToCodes(const int* const code_lengths, | 79 int HuffmanCodeLengthsToCodes(const int* const code_lengths, |
73 int code_lengths_size, int* const huff_codes); | 80 int code_lengths_size, int* const huff_codes); |
74 | 81 |
75 | 82 |
76 #if defined(__cplusplus) || defined(c_plusplus) | 83 #ifdef __cplusplus |
77 } // extern "C" | 84 } // extern "C" |
78 #endif | 85 #endif |
79 | 86 |
80 #endif // WEBP_UTILS_HUFFMAN_H_ | 87 #endif // WEBP_UTILS_HUFFMAN_H_ |
OLD | NEW |