ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

huffman.h

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 // Based on xoreos' Huffman code
00024 
00025 #ifndef COMMON_HUFFMAN_H
00026 #define COMMON_HUFFMAN_H
00027 
00028 #include "common/array.h"
00029 #include "common/list.h"
00030 #include "common/types.h"
00031 
00032 namespace Common {
00033 
00034 inline uint32 REVERSEBITS(uint32 x) {
00035     x = (((x & ~0x55555555) >> 1) | ((x & 0x55555555) << 1));
00036     x = (((x & ~0x33333333) >> 2) | ((x & 0x33333333) << 2));
00037     x = (((x & ~0x0F0F0F0F) >> 4) | ((x & 0x0F0F0F0F) << 4));
00038     x = (((x & ~0x00FF00FF) >> 8) | ((x & 0x00FF00FF) << 8));
00039 
00040     return((x >> 16) | (x << 16));
00041 }
00042 
00049 template<class BITSTREAM>
00050 class Huffman {
00051 public:
00060     Huffman(uint8 maxLength, uint32 codeCount, const uint32 *codes, const uint8 *lengths, const uint32 *symbols = nullptr);
00061 
00063     uint32 getSymbol(BITSTREAM &bits) const;
00064 
00065 private:
00066     struct Symbol {
00067         uint32 code;
00068         uint32 symbol;
00069 
00070         Symbol(uint32 c, uint32 s) : code(c), symbol(s) {}
00071     };
00072 
00073     typedef List<Symbol> CodeList;
00074     typedef Array<CodeList> CodeLists;
00075 
00077     CodeLists _codes;
00078 
00080     struct PrefixEntry {
00081         uint32 symbol;
00082         uint8  length;
00083 
00084         PrefixEntry() : length(0xFF) {}
00085     };
00086 
00087     static const uint8 _prefixTableBits = 8;
00088     PrefixEntry _prefixTable[1 << _prefixTableBits];
00089 };
00090 
00091 template <class BITSTREAM>
00092 Huffman<BITSTREAM>::Huffman(uint8 maxLength, uint32 codeCount, const uint32 *codes, const uint8 *lengths, const uint32 *symbols) {
00093     assert(codeCount > 0);
00094 
00095     assert(codes);
00096     assert(lengths);
00097 
00098     if (maxLength == 0)
00099         for (uint32 i = 0; i < codeCount; i++)
00100             maxLength = MAX(maxLength, lengths[i]);
00101 
00102     assert(maxLength <= 32);
00103 
00104     // Codes that don't fit in the prefix table are stored in the _codes array
00105     _codes.resize(MAX(maxLength - _prefixTableBits, 0));
00106 
00107     for (uint i = 0; i < codeCount; i++) {
00108         uint8 length = lengths[i];
00109 
00110         // The symbol. If none were specified, just assume it's identical to the code index
00111         uint32 symbol = symbols ? symbols[i] : i;
00112 
00113         if (length <= _prefixTableBits) {
00114             // Short codes go in the prefix lookup table. Set all the entries in the table
00115             // with an index starting with the code to the symbol value.
00116             uint32 startIndex;
00117             if (BITSTREAM::isMSB2LSB()) {
00118                 startIndex = codes[i] << (_prefixTableBits - length);
00119             } else {
00120                 startIndex = REVERSEBITS(codes[i]) >> (32 - _prefixTableBits);
00121             }
00122 
00123             uint32 endIndex = startIndex | ((1 << (_prefixTableBits - length)) - 1);
00124 
00125             for (uint32 j = startIndex; j <= endIndex; j++) {
00126                 uint32 index = BITSTREAM::isMSB2LSB() ? j : REVERSEBITS(j) >> (32 - _prefixTableBits);
00127                 _prefixTable[index].symbol = symbol;
00128                 _prefixTable[index].length = length;
00129             }
00130         } else {
00131             // Put the code and symbol into the correct list for the length
00132             _codes[lengths[i] - 1 - _prefixTableBits].push_back(Symbol(codes[i], symbol));
00133         }
00134     }
00135 }
00136 
00137 template <class BITSTREAM>
00138 uint32 Huffman<BITSTREAM>::getSymbol(BITSTREAM &bits) const {
00139     uint32 code = bits.peekBits(_prefixTableBits);
00140 
00141     uint8 length = _prefixTable[code].length;
00142 
00143     if (length != 0xFF) {
00144         bits.skip(length);
00145         return _prefixTable[code].symbol;
00146     } else {
00147         bits.skip(_prefixTableBits);
00148 
00149         for (uint32 i = 0; i < _codes.size(); i++) {
00150             bits.addBit(code, i + _prefixTableBits);
00151 
00152             for (typename CodeList::const_iterator cCode = _codes[i].begin(); cCode != _codes[i].end(); ++cCode)
00153                 if (code == cCode->code)
00154                     return cCode->symbol;
00155         }
00156     }
00157 
00158     error("Unknown Huffman code");
00159     return 0;
00160 }
00161 
00162 } // End of namespace Common
00163 
00164 #endif // COMMON_HUFFMAN_H


Generated on Sat Jul 20 2019 05:00:47 for ResidualVM by doxygen 1.7.1
curved edge   curved edge