Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(234)

Side by Side Diff: third_party/libwebp/utils/huffman.c

Issue 1546003002: libwebp: update to 0.5.0 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/libwebp/utils/huffman.h ('k') | third_party/libwebp/utils/huffman_encode.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include <assert.h> 14 #include <assert.h>
15 #include <stdlib.h> 15 #include <stdlib.h>
16 #include <string.h> 16 #include <string.h>
17 #include "./huffman.h" 17 #include "./huffman.h"
18 #include "../utils/utils.h" 18 #include "../utils/utils.h"
19 #include "../webp/format_constants.h" 19 #include "../webp/format_constants.h"
20 20
21 // Uncomment the following to use look-up table for ReverseBits()
22 // (might be faster on some platform)
23 // #define USE_LUT_REVERSE_BITS
24
25 // Huffman data read via DecodeImageStream is represented in two (red and green) 21 // Huffman data read via DecodeImageStream is represented in two (red and green)
26 // bytes. 22 // bytes.
27 #define MAX_HTREE_GROUPS 0x10000 23 #define MAX_HTREE_GROUPS 0x10000
28 #define NON_EXISTENT_SYMBOL (-1)
29
30 static void TreeNodeInit(HuffmanTreeNode* const node) {
31 node->children_ = -1; // means: 'unassigned so far'
32 }
33
34 static int NodeIsEmpty(const HuffmanTreeNode* const node) {
35 return (node->children_ < 0);
36 }
37
38 static int IsFull(const HuffmanTree* const tree) {
39 return (tree->num_nodes_ == tree->max_nodes_);
40 }
41
42 static void AssignChildren(HuffmanTree* const tree,
43 HuffmanTreeNode* const node) {
44 HuffmanTreeNode* const children = tree->root_ + tree->num_nodes_;
45 node->children_ = (int)(children - node);
46 assert(children - node == (int)(children - node));
47 tree->num_nodes_ += 2;
48 TreeNodeInit(children + 0);
49 TreeNodeInit(children + 1);
50 }
51
52 // A Huffman tree is a full binary tree; and in a full binary tree with L
53 // leaves, the total number of nodes N = 2 * L - 1.
54 static int HuffmanTreeMaxNodes(int num_leaves) {
55 return (2 * num_leaves - 1);
56 }
57
58 static int HuffmanTreeAllocate(HuffmanTree* const tree, int num_nodes) {
59 assert(tree != NULL);
60 tree->root_ =
61 (HuffmanTreeNode*)WebPSafeMalloc(num_nodes, sizeof(*tree->root_));
62 return (tree->root_ != NULL);
63 }
64
65 static int TreeInit(HuffmanTree* const tree, int num_leaves) {
66 assert(tree != NULL);
67 if (num_leaves == 0) return 0;
68 tree->max_nodes_ = HuffmanTreeMaxNodes(num_leaves);
69 assert(tree->max_nodes_ < (1 << 16)); // limit for the lut_jump_ table
70 if (!HuffmanTreeAllocate(tree, tree->max_nodes_)) return 0;
71 TreeNodeInit(tree->root_); // Initialize root.
72 tree->num_nodes_ = 1;
73 memset(tree->lut_bits_, 255, sizeof(tree->lut_bits_));
74 memset(tree->lut_jump_, 0, sizeof(tree->lut_jump_));
75 return 1;
76 }
77
78 void VP8LHuffmanTreeFree(HuffmanTree* const tree) {
79 if (tree != NULL) {
80 WebPSafeFree(tree->root_);
81 tree->root_ = NULL;
82 tree->max_nodes_ = 0;
83 tree->num_nodes_ = 0;
84 }
85 }
86 24
87 HTreeGroup* VP8LHtreeGroupsNew(int num_htree_groups) { 25 HTreeGroup* VP8LHtreeGroupsNew(int num_htree_groups) {
88 HTreeGroup* const htree_groups = 26 HTreeGroup* const htree_groups =
89 (HTreeGroup*)WebPSafeCalloc(num_htree_groups, sizeof(*htree_groups)); 27 (HTreeGroup*)WebPSafeMalloc(num_htree_groups, sizeof(*htree_groups));
90 assert(num_htree_groups <= MAX_HTREE_GROUPS);
91 if (htree_groups == NULL) { 28 if (htree_groups == NULL) {
92 return NULL; 29 return NULL;
93 } 30 }
31 assert(num_htree_groups <= MAX_HTREE_GROUPS);
94 return htree_groups; 32 return htree_groups;
95 } 33 }
96 34
97 void VP8LHtreeGroupsFree(HTreeGroup* htree_groups, int num_htree_groups) { 35 void VP8LHtreeGroupsFree(HTreeGroup* const htree_groups) {
98 if (htree_groups != NULL) { 36 if (htree_groups != NULL) {
99 int i, j;
100 for (i = 0; i < num_htree_groups; ++i) {
101 HuffmanTree* const htrees = htree_groups[i].htrees_;
102 for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
103 VP8LHuffmanTreeFree(&htrees[j]);
104 }
105 }
106 WebPSafeFree(htree_groups); 37 WebPSafeFree(htree_groups);
107 } 38 }
108 } 39 }
109 40
110 int VP8LHuffmanCodeLengthsToCodes( 41 // Returns reverse(reverse(key, len) + 1, len), where reverse(key, len) is the
111 const int* const code_lengths, int code_lengths_size, 42 // bit-wise reversal of the len least significant bits of key.
112 int* const huff_codes) { 43 static WEBP_INLINE uint32_t GetNextKey(uint32_t key, int len) {
113 int symbol; 44 uint32_t step = 1 << (len - 1);
114 int code_len; 45 while (key & step) {
115 int code_length_hist[MAX_ALLOWED_CODE_LENGTH + 1] = { 0 }; 46 step >>= 1;
116 int curr_code; 47 }
117 int next_codes[MAX_ALLOWED_CODE_LENGTH + 1] = { 0 }; 48 return (key & (step - 1)) + step;
118 int max_code_length = 0; 49 }
119 50
51 // Stores code in table[0], table[step], table[2*step], ..., table[end].
52 // Assumes that end is an integer multiple of step.
53 static WEBP_INLINE void ReplicateValue(HuffmanCode* table,
54 int step, int end,
55 HuffmanCode code) {
56 assert(end % step == 0);
57 do {
58 end -= step;
59 table[end] = code;
60 } while (end > 0);
61 }
62
63 // Returns the table width of the next 2nd level table. count is the histogram
64 // of bit lengths for the remaining symbols, len is the code length of the next
65 // processed symbol
66 static WEBP_INLINE int NextTableBitSize(const int* const count,
67 int len, int root_bits) {
68 int left = 1 << (len - root_bits);
69 while (len < MAX_ALLOWED_CODE_LENGTH) {
70 left -= count[len];
71 if (left <= 0) break;
72 ++len;
73 left <<= 1;
74 }
75 return len - root_bits;
76 }
77
78 int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
79 const int code_lengths[], int code_lengths_size) {
80 HuffmanCode* table = root_table; // next available space in table
81 int total_size = 1 << root_bits; // total size root table + 2nd level table
82 int* sorted = NULL; // symbols sorted by code length
83 int len; // current code length
84 int symbol; // symbol index in original or sorted table
85 // number of codes of each length:
86 int count[MAX_ALLOWED_CODE_LENGTH + 1] = { 0 };
87 // offsets in sorted table for each length:
88 int offset[MAX_ALLOWED_CODE_LENGTH + 1];
89
90 assert(code_lengths_size != 0);
120 assert(code_lengths != NULL); 91 assert(code_lengths != NULL);
121 assert(code_lengths_size > 0); 92 assert(root_table != NULL);
122 assert(huff_codes != NULL); 93 assert(root_bits > 0);
123 94
124 // Calculate max code length. 95 // Build histogram of code lengths.
125 for (symbol = 0; symbol < code_lengths_size; ++symbol) { 96 for (symbol = 0; symbol < code_lengths_size; ++symbol) {
126 if (code_lengths[symbol] > max_code_length) { 97 if (code_lengths[symbol] > MAX_ALLOWED_CODE_LENGTH) {
127 max_code_length = code_lengths[symbol]; 98 return 0;
128 } 99 }
129 } 100 ++count[code_lengths[symbol]];
130 if (max_code_length > MAX_ALLOWED_CODE_LENGTH) return 0;
131
132 // Calculate code length histogram.
133 for (symbol = 0; symbol < code_lengths_size; ++symbol) {
134 ++code_length_hist[code_lengths[symbol]];
135 }
136 code_length_hist[0] = 0;
137
138 // Calculate the initial values of 'next_codes' for each code length.
139 // next_codes[code_len] denotes the code to be assigned to the next symbol
140 // of code length 'code_len'.
141 curr_code = 0;
142 next_codes[0] = -1; // Unused, as code length = 0 implies code doesn't exist.
143 for (code_len = 1; code_len <= max_code_length; ++code_len) {
144 curr_code = (curr_code + code_length_hist[code_len - 1]) << 1;
145 next_codes[code_len] = curr_code;
146 } 101 }
147 102
148 // Get symbols. 103 // Error, all code lengths are zeros.
149 for (symbol = 0; symbol < code_lengths_size; ++symbol) { 104 if (count[0] == code_lengths_size) {
150 if (code_lengths[symbol] > 0) { 105 return 0;
151 huff_codes[symbol] = next_codes[code_lengths[symbol]]++;
152 } else {
153 huff_codes[symbol] = NON_EXISTENT_SYMBOL;
154 }
155 } 106 }
156 return 1;
157 }
158 107
159 #ifndef USE_LUT_REVERSE_BITS 108 // Generate offsets into sorted symbol table by code length.
160 109 offset[1] = 0;
161 static int ReverseBitsShort(int bits, int num_bits) { 110 for (len = 1; len < MAX_ALLOWED_CODE_LENGTH; ++len) {
162 int retval = 0; 111 if (count[len] > (1 << len)) {
163 int i;
164 assert(num_bits <= 8); // Not a hard requirement, just for coherency.
165 for (i = 0; i < num_bits; ++i) {
166 retval <<= 1;
167 retval |= bits & 1;
168 bits >>= 1;
169 }
170 return retval;
171 }
172
173 #else
174
175 static const uint8_t kReversedBits[16] = { // Pre-reversed 4-bit values.
176 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
177 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
178 };
179
180 static int ReverseBitsShort(int bits, int num_bits) {
181 const uint8_t v = (kReversedBits[bits & 0xf] << 4) | kReversedBits[bits >> 4];
182 assert(num_bits <= 8);
183 return v >> (8 - num_bits);
184 }
185
186 #endif
187
188 static int TreeAddSymbol(HuffmanTree* const tree,
189 int symbol, int code, int code_length) {
190 int step = HUFF_LUT_BITS;
191 int base_code;
192 HuffmanTreeNode* node = tree->root_;
193 const HuffmanTreeNode* const max_node = tree->root_ + tree->max_nodes_;
194 assert(symbol == (int16_t)symbol);
195 if (code_length <= HUFF_LUT_BITS) {
196 int i;
197 base_code = ReverseBitsShort(code, code_length);
198 for (i = 0; i < (1 << (HUFF_LUT_BITS - code_length)); ++i) {
199 const int idx = base_code | (i << code_length);
200 tree->lut_symbol_[idx] = (int16_t)symbol;
201 tree->lut_bits_[idx] = code_length;
202 }
203 } else {
204 base_code = ReverseBitsShort((code >> (code_length - HUFF_LUT_BITS)),
205 HUFF_LUT_BITS);
206 }
207 while (code_length-- > 0) {
208 if (node >= max_node) {
209 return 0; 112 return 0;
210 } 113 }
211 if (NodeIsEmpty(node)) { 114 offset[len + 1] = offset[len] + count[len];
212 if (IsFull(tree)) return 0; // error: too many symbols.
213 AssignChildren(tree, node);
214 } else if (!HuffmanTreeNodeIsNotLeaf(node)) {
215 return 0; // leaf is already occupied.
216 }
217 node += node->children_ + ((code >> code_length) & 1);
218 if (--step == 0) {
219 tree->lut_jump_[base_code] = (int16_t)(node - tree->root_);
220 }
221 } 115 }
222 if (NodeIsEmpty(node)) { 116
223 node->children_ = 0; // turn newly created node into a leaf. 117 sorted = (int*)WebPSafeMalloc(code_lengths_size, sizeof(*sorted));
224 } else if (HuffmanTreeNodeIsNotLeaf(node)) { 118 if (sorted == NULL) {
225 return 0; // trying to assign a symbol to already used code. 119 return 0;
226 } 120 }
227 node->symbol_ = symbol; // Add symbol in this node.
228 return 1;
229 }
230 121
231 int VP8LHuffmanTreeBuildImplicit(HuffmanTree* const tree, 122 // Sort symbols by length, by symbol order within each length.
232 const int* const code_lengths,
233 int* const codes,
234 int code_lengths_size) {
235 int symbol;
236 int num_symbols = 0;
237 int root_symbol = 0;
238
239 assert(tree != NULL);
240 assert(code_lengths != NULL);
241
242 // Find out number of symbols and the root symbol.
243 for (symbol = 0; symbol < code_lengths_size; ++symbol) { 123 for (symbol = 0; symbol < code_lengths_size; ++symbol) {
124 const int symbol_code_length = code_lengths[symbol];
244 if (code_lengths[symbol] > 0) { 125 if (code_lengths[symbol] > 0) {
245 // Note: code length = 0 indicates non-existent symbol. 126 sorted[offset[symbol_code_length]++] = symbol;
246 ++num_symbols;
247 root_symbol = symbol;
248 } 127 }
249 } 128 }
250 129
251 // Initialize the tree. Will fail for num_symbols = 0 130 // Special case code with only one value.
252 if (!TreeInit(tree, num_symbols)) return 0; 131 if (offset[MAX_ALLOWED_CODE_LENGTH] == 1) {
132 HuffmanCode code;
133 code.bits = 0;
134 code.value = (uint16_t)sorted[0];
135 ReplicateValue(table, 1, total_size, code);
136 WebPSafeFree(sorted);
137 return total_size;
138 }
253 139
254 // Build tree. 140 {
255 if (num_symbols == 1) { // Trivial case. 141 int step; // step size to replicate values in current table
256 const int max_symbol = code_lengths_size; 142 uint32_t low = -1; // low bits for current root entry
257 if (root_symbol < 0 || root_symbol >= max_symbol) { 143 uint32_t mask = total_size - 1; // mask for low bits
258 VP8LHuffmanTreeFree(tree); 144 uint32_t key = 0; // reversed prefix code
145 int num_nodes = 1; // number of Huffman tree nodes
146 int num_open = 1; // number of open branches in current tree level
147 int table_bits = root_bits; // key length of current table
148 int table_size = 1 << table_bits; // size of current table
149 symbol = 0;
150 // Fill in root table.
151 for (len = 1, step = 2; len <= root_bits; ++len, step <<= 1) {
152 num_open <<= 1;
153 num_nodes += num_open;
154 num_open -= count[len];
155 if (num_open < 0) {
156 WebPSafeFree(sorted);
157 return 0;
158 }
159 for (; count[len] > 0; --count[len]) {
160 HuffmanCode code;
161 code.bits = (uint8_t)len;
162 code.value = (uint16_t)sorted[symbol++];
163 ReplicateValue(&table[key], step, table_size, code);
164 key = GetNextKey(key, len);
165 }
166 }
167
168 // Fill in 2nd level tables and add pointers to root table.
169 for (len = root_bits + 1, step = 2; len <= MAX_ALLOWED_CODE_LENGTH;
170 ++len, step <<= 1) {
171 num_open <<= 1;
172 num_nodes += num_open;
173 num_open -= count[len];
174 if (num_open < 0) {
175 WebPSafeFree(sorted);
176 return 0;
177 }
178 for (; count[len] > 0; --count[len]) {
179 HuffmanCode code;
180 if ((key & mask) != low) {
181 table += table_size;
182 table_bits = NextTableBitSize(count, len, root_bits);
183 table_size = 1 << table_bits;
184 total_size += table_size;
185 low = key & mask;
186 root_table[low].bits = (uint8_t)(table_bits + root_bits);
187 root_table[low].value = (uint16_t)((table - root_table) - low);
188 }
189 code.bits = (uint8_t)(len - root_bits);
190 code.value = (uint16_t)sorted[symbol++];
191 ReplicateValue(&table[key >> root_bits], step, table_size, code);
192 key = GetNextKey(key, len);
193 }
194 }
195
196 // Check if tree is full.
197 if (num_nodes != 2 * offset[MAX_ALLOWED_CODE_LENGTH] - 1) {
198 WebPSafeFree(sorted);
259 return 0; 199 return 0;
260 } 200 }
261 return TreeAddSymbol(tree, root_symbol, 0, 0); 201 }
262 } else { // Normal case.
263 int ok = 0;
264 memset(codes, 0, code_lengths_size * sizeof(*codes));
265 202
266 if (!VP8LHuffmanCodeLengthsToCodes(code_lengths, code_lengths_size, 203 WebPSafeFree(sorted);
267 codes)) { 204 return total_size;
268 goto End;
269 }
270
271 // Add symbols one-by-one.
272 for (symbol = 0; symbol < code_lengths_size; ++symbol) {
273 if (code_lengths[symbol] > 0) {
274 if (!TreeAddSymbol(tree, symbol, codes[symbol],
275 code_lengths[symbol])) {
276 goto End;
277 }
278 }
279 }
280 ok = 1;
281 End:
282 ok = ok && IsFull(tree);
283 if (!ok) VP8LHuffmanTreeFree(tree);
284 return ok;
285 }
286 } 205 }
287
288 int VP8LHuffmanTreeBuildExplicit(HuffmanTree* const tree,
289 const int* const code_lengths,
290 const int* const codes,
291 const int* const symbols, int max_symbol,
292 int num_symbols) {
293 int ok = 0;
294 int i;
295 assert(tree != NULL);
296 assert(code_lengths != NULL);
297 assert(codes != NULL);
298 assert(symbols != NULL);
299
300 // Initialize the tree. Will fail if num_symbols = 0.
301 if (!TreeInit(tree, num_symbols)) return 0;
302
303 // Add symbols one-by-one.
304 for (i = 0; i < num_symbols; ++i) {
305 if (codes[i] != NON_EXISTENT_SYMBOL) {
306 if (symbols[i] < 0 || symbols[i] >= max_symbol) {
307 goto End;
308 }
309 if (!TreeAddSymbol(tree, symbols[i], codes[i], code_lengths[i])) {
310 goto End;
311 }
312 }
313 }
314 ok = 1;
315 End:
316 ok = ok && IsFull(tree);
317 if (!ok) VP8LHuffmanTreeFree(tree);
318 return ok;
319 }
OLDNEW
« no previous file with comments | « third_party/libwebp/utils/huffman.h ('k') | third_party/libwebp/utils/huffman_encode.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698