diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/huffman.cpp | 71 | ||||
-rw-r--r-- | common/huffman.h | 118 | ||||
-rw-r--r-- | common/module.mk | 1 |
3 files changed, 94 insertions, 96 deletions
diff --git a/common/huffman.cpp b/common/huffman.cpp deleted file mode 100644 index 3268ddf251..0000000000 --- a/common/huffman.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -// Based on eos' Huffman code - -#include "common/huffman.h" -#include "common/util.h" -#include "common/textconsole.h" -#include "common/bitstream.h" - -namespace Common { - -Huffman::Symbol::Symbol(uint32 c, uint32 s) : code(c), symbol(s) { -} - - -Huffman::Huffman(uint8 maxLength, uint32 codeCount, const uint32 *codes, const uint8 *lengths, const uint32 *symbols) { - assert(codeCount > 0); - - assert(codes); - assert(lengths); - - if (maxLength == 0) - for (uint32 i = 0; i < codeCount; i++) - maxLength = MAX(maxLength, lengths[i]); - - assert(maxLength <= 32); - - _codes.resize(maxLength); - _symbols.resize(codeCount); - - for (uint32 i = 0; i < codeCount; i++) { - // The symbol. If none were specified, just assume it's identical to the code index - uint32 symbol = symbols ? symbols[i] : i; - - // Put the code and symbol into the correct list - _codes[lengths[i] - 1].push_back(Symbol(codes[i], symbol)); - - // And put the pointer to the symbol/code struct into the symbol list. - _symbols[i] = &_codes[lengths[i] - 1].back(); - } -} - -Huffman::~Huffman() { -} - -void Huffman::setSymbols(const uint32 *symbols) { - for (uint32 i = 0; i < _symbols.size(); i++) - _symbols[i]->symbol = symbols ? *symbols++ : i; -} - -} // End of namespace Common diff --git a/common/huffman.h b/common/huffman.h index b4de5ab483..052c7bf825 100644 --- a/common/huffman.h +++ b/common/huffman.h @@ -20,7 +20,7 @@ * */ -// Based on eos' Huffman code +// Based on xoreos' Huffman code #ifndef COMMON_HUFFMAN_H #define COMMON_HUFFMAN_H @@ -31,12 +31,22 @@ namespace Common { +inline uint32 REVERSEBITS(uint32 x) { + x = (((x & ~0x55555555) >> 1) | ((x & 0x55555555) << 1)); + x = (((x & ~0x33333333) >> 2) | ((x & 0x33333333) << 2)); + x = (((x & ~0x0F0F0F0F) >> 4) | ((x & 0x0F0F0F0F) << 4)); + x = (((x & ~0x00FF00FF) >> 8) | ((x & 0x00FF00FF) << 8)); + + return((x >> 16) | (x << 16)); +} + /** * Huffman bitstream decoding * * Used in engines: * - scumm */ +template<class BITSTREAM> class Huffman { public: /** Construct a Huffman decoder. @@ -48,47 +58,107 @@ public: * @param symbols The symbols. If 0, assume they are identical to the code indices. */ Huffman(uint8 maxLength, uint32 codeCount, const uint32 *codes, const uint8 *lengths, const uint32 *symbols = nullptr); - ~Huffman(); - - /** Modify the codes' symbols. */ - void setSymbols(const uint32 *symbols = nullptr); /** Return the next symbol in the bitstream. */ - template<class BITSTREAM> - uint32 getSymbol(BITSTREAM &bits) const { - uint32 code = 0; - - for (uint32 i = 0; i < _codes.size(); i++) { - bits.addBit(code, i); - - for (CodeList::const_iterator cCode = _codes[i].begin(); cCode != _codes[i].end(); ++cCode) - if (code == cCode->code) - return cCode->symbol; - } - - error("Unknown Huffman code"); - return 0; - } + uint32 getSymbol(BITSTREAM &bits) const; private: struct Symbol { uint32 code; uint32 symbol; - Symbol(uint32 c, uint32 s); + Symbol(uint32 c, uint32 s) : code(c), symbol(s) {} }; typedef List<Symbol> CodeList; typedef Array<CodeList> CodeLists; - typedef Array<Symbol *> SymbolList; /** Lists of codes and their symbols, sorted by code length. */ CodeLists _codes; - /** Sorted list of pointers to the symbols. */ - SymbolList _symbols; + /** Prefix lookup table used to speed up the decoding of short codes. */ + struct PrefixEntry { + uint32 symbol; + uint8 length; + + PrefixEntry() : length(0xFF) {} + }; + + static const uint8 _prefixTableBits = 8; + PrefixEntry _prefixTable[1 << _prefixTableBits]; }; +template <class BITSTREAM> +Huffman<BITSTREAM>::Huffman(uint8 maxLength, uint32 codeCount, const uint32 *codes, const uint8 *lengths, const uint32 *symbols) { + assert(codeCount > 0); + + assert(codes); + assert(lengths); + + if (maxLength == 0) + for (uint32 i = 0; i < codeCount; i++) + maxLength = MAX(maxLength, lengths[i]); + + assert(maxLength <= 32); + + // Codes that don't fit in the prefix table are stored in the _codes array + _codes.resize(MAX(maxLength - _prefixTableBits, 0)); + + for (uint i = 0; i < codeCount; i++) { + uint8 length = lengths[i]; + + // The symbol. If none were specified, just assume it's identical to the code index + uint32 symbol = symbols ? symbols[i] : i; + + if (length <= _prefixTableBits) { + // Short codes go in the prefix lookup table. Set all the entries in the table + // with an index starting with the code to the symbol value. + uint32 startIndex; + if (BITSTREAM::isMSB2LSB()) { + startIndex = codes[i] << (_prefixTableBits - length); + } else { + startIndex = REVERSEBITS(codes[i]) >> (32 - _prefixTableBits); + } + + uint32 endIndex = startIndex | ((1 << (_prefixTableBits - length)) - 1); + + for (uint32 j = startIndex; j <= endIndex; j++) { + uint32 index = BITSTREAM::isMSB2LSB() ? j : REVERSEBITS(j) >> (32 - _prefixTableBits); + _prefixTable[index].symbol = symbol; + _prefixTable[index].length = length; + } + } else { + // Put the code and symbol into the correct list for the length + _codes[lengths[i] - 1 - _prefixTableBits].push_back(Symbol(codes[i], symbol)); + } + } +} + +template <class BITSTREAM> +uint32 Huffman<BITSTREAM>::getSymbol(BITSTREAM &bits) const { + uint32 code = bits.peekBits(_prefixTableBits); + + uint8 length = _prefixTable[code].length; + + if (length != 0xFF) { + bits.skip(length); + return _prefixTable[code].symbol; + } else { + bits.skip(_prefixTableBits); + + for (uint32 i = 0; i < _codes.size(); i++) { + bits.addBit(code, i + _prefixTableBits); + + for (typename CodeList::const_iterator cCode = _codes[i].begin(); cCode != _codes[i].end(); ++cCode) + if (code == cCode->code) + return cCode->symbol; + } + } + + error("Unknown Huffman code"); + return 0; +} + } // End of namespace Common #endif // COMMON_HUFFMAN_H diff --git a/common/module.mk b/common/module.mk index 6742fef3e8..f456604ebb 100644 --- a/common/module.mk +++ b/common/module.mk @@ -49,7 +49,6 @@ MODULE_OBJS += \ cosinetables.o \ dct.o \ fft.o \ - huffman.o \ rdft.o \ sinetables.o |