diff options
Diffstat (limited to 'drivers/webp/utils/huffman.c')
-rw-r--r-- | drivers/webp/utils/huffman.c | 72 |
1 files changed, 11 insertions, 61 deletions
diff --git a/drivers/webp/utils/huffman.c b/drivers/webp/utils/huffman.c index 8c5739f633..41529cc9da 100644 --- a/drivers/webp/utils/huffman.c +++ b/drivers/webp/utils/huffman.c @@ -1,10 +1,8 @@ // Copyright 2012 Google Inc. All Rights Reserved. // -// Use of this source code is governed by a BSD-style license -// that can be found in the COPYING file in the root of the source -// tree. An additional intellectual property rights grant can be found -// in the file PATENTS. All contributing project authors may -// be found in the AUTHORS file in the root of the source tree. +// 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/ // ----------------------------------------------------------------------------- // // Utilities for building and looking up Huffman trees. @@ -13,14 +11,13 @@ #include <assert.h> #include <stdlib.h> -#include <string.h> #include "./huffman.h" #include "../utils/utils.h" #include "../webp/format_constants.h" -// Uncomment the following to use look-up table for ReverseBits() -// (might be faster on some platform) -// #define USE_LUT_REVERSE_BITS +#if defined(__cplusplus) || defined(c_plusplus) +extern "C" { +#endif #define NON_EXISTENT_SYMBOL (-1) @@ -53,14 +50,11 @@ static int TreeInit(HuffmanTree* const tree, int num_leaves) { // 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; - assert(tree->max_nodes_ < (1 << 16)); // limit for the lut_jump_ table tree->root_ = (HuffmanTreeNode*)WebPSafeMalloc((uint64_t)tree->max_nodes_, sizeof(*tree->root_)); if (tree->root_ == NULL) return 0; TreeNodeInit(tree->root_); // Initialize root. tree->num_nodes_ = 1; - memset(tree->lut_bits_, 255, sizeof(tree->lut_bits_)); - memset(tree->lut_jump_, 0, sizeof(tree->lut_jump_)); return 1; } @@ -121,54 +115,10 @@ int HuffmanCodeLengthsToCodes(const int* const code_lengths, return 1; } -#ifndef USE_LUT_REVERSE_BITS - -static int ReverseBitsShort(int bits, int num_bits) { - int retval = 0; - int i; - assert(num_bits <= 8); // Not a hard requirement, just for coherency. - for (i = 0; i < num_bits; ++i) { - retval <<= 1; - retval |= bits & 1; - bits >>= 1; - } - return retval; -} - -#else - -static const uint8_t kReversedBits[16] = { // Pre-reversed 4-bit values. - 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe, - 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf -}; - -static int ReverseBitsShort(int bits, int num_bits) { - const uint8_t v = (kReversedBits[bits & 0xf] << 4) | kReversedBits[bits >> 4]; - assert(num_bits <= 8); - return v >> (8 - num_bits); -} - -#endif - 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) { - int i; - base_code = ReverseBitsShort(code, code_length); - for (i = 0; i < (1 << (HUFF_LUT_BITS - code_length)); ++i) { - const int idx = base_code | (i << code_length); - tree->lut_symbol_[idx] = (int16_t)symbol; - tree->lut_bits_[idx] = code_length; - } - } else { - base_code = ReverseBitsShort((code >> (code_length - HUFF_LUT_BITS)), - HUFF_LUT_BITS); - } while (code_length-- > 0) { if (node >= max_node) { return 0; @@ -176,17 +126,14 @@ static int TreeAddSymbol(HuffmanTree* const tree, if (NodeIsEmpty(node)) { if (IsFull(tree)) return 0; // error: too many symbols. AssignChildren(tree, node); - } else if (!HuffmanTreeNodeIsNotLeaf(node)) { + } else if (HuffmanTreeNodeIsLeaf(node)) { return 0; // leaf is already occupied. } node += node->children_ + ((code >> code_length) & 1); - if (--step == 0) { - tree->lut_jump_[base_code] = (int16_t)(node - tree->root_); - } } if (NodeIsEmpty(node)) { node->children_ = 0; // turn newly created node into a leaf. - } else if (HuffmanTreeNodeIsNotLeaf(node)) { + } else if (!HuffmanTreeNodeIsLeaf(node)) { return 0; // trying to assign a symbol to already used code. } node->symbol_ = symbol; // Add symbol in this node. @@ -286,3 +233,6 @@ int HuffmanTreeBuildExplicit(HuffmanTree* const tree, return ok; } +#if defined(__cplusplus) || defined(c_plusplus) +} // extern "C" +#endif |