ScummVM API documentation
huffman.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 // Based on xoreos' Huffman code
23 
24 #ifndef COMMON_HUFFMAN_H
25 #define COMMON_HUFFMAN_H
26 
27 #include "common/array.h"
28 #include "common/list.h"
29 #include "common/types.h"
30 
31 namespace Common {
32 
45 inline uint32 REVERSEBITS(uint32 x) {
46  x = (((x & ~0x55555555) >> 1) | ((x & 0x55555555) << 1));
47  x = (((x & ~0x33333333) >> 2) | ((x & 0x33333333) << 2));
48  x = (((x & ~0x0F0F0F0F) >> 4) | ((x & 0x0F0F0F0F) << 4));
49  x = (((x & ~0x00FF00FF) >> 8) | ((x & 0x00FF00FF) << 8));
50 
51  return((x >> 16) | (x << 16));
52 }
53 
58 template<class BITSTREAM>
59 class Huffman {
60 public:
69  Huffman(uint8 maxLength, uint32 codeCount, const uint32 *codes, const uint8 *lengths, const uint32 *symbols = nullptr);
70 
72  uint32 getSymbol(BITSTREAM &bits) const;
73 
74 private:
75  struct Symbol {
76  uint32 code;
77  uint32 symbol;
78 
79  Symbol(uint32 c, uint32 s) : code(c), symbol(s) {}
80  };
81 
82  typedef List<Symbol> CodeList;
83  typedef Array<CodeList> CodeLists;
84 
86  CodeLists _codes;
87 
89  struct PrefixEntry {
90  uint32 symbol;
91  uint8 length;
92 
93  PrefixEntry() : length(0xFF) {}
94  };
95 
96  static const uint8 _prefixTableBits = 8;
97  PrefixEntry _prefixTable[1 << _prefixTableBits];
98 };
99 
100 template <class BITSTREAM>
101 Huffman<BITSTREAM>::Huffman(uint8 maxLength, uint32 codeCount, const uint32 *codes, const uint8 *lengths, const uint32 *symbols) {
102  assert(codeCount > 0);
103 
104  assert(codes);
105  assert(lengths);
106 
107  if (maxLength == 0)
108  for (uint32 i = 0; i < codeCount; i++)
109  maxLength = MAX(maxLength, lengths[i]);
110 
111  assert(maxLength <= 32);
112 
113  // Codes that do not fit in the prefix table are stored in the _codes array.
114  _codes.resize(MAX(maxLength - _prefixTableBits, 0));
115 
116  for (uint i = 0; i < codeCount; i++) {
117  uint8 length = lengths[i];
118 
119  // The symbol. If none was specified, assume it is identical to the code index.
120  uint32 symbol = symbols ? symbols[i] : i;
121 
122  if (length <= _prefixTableBits) {
123  // Short codes go in the prefix lookup table. Set all the entries in the table
124  // with an index starting with the code to the symbol value.
125  uint32 startIndex;
126  if (BITSTREAM::isMSB2LSB()) {
127  startIndex = codes[i] << (_prefixTableBits - length);
128  } else {
129  startIndex = REVERSEBITS(codes[i]) >> (32 - _prefixTableBits);
130  }
131 
132  uint32 endIndex = startIndex | ((1 << (_prefixTableBits - length)) - 1);
133 
134  for (uint32 j = startIndex; j <= endIndex; j++) {
135  uint32 index = BITSTREAM::isMSB2LSB() ? j : REVERSEBITS(j) >> (32 - _prefixTableBits);
136  _prefixTable[index].symbol = symbol;
137  _prefixTable[index].length = length;
138  }
139  } else {
140  // Put the code and symbol into the correct list for the length.
141  _codes[lengths[i] - 1 - _prefixTableBits].push_back(Symbol(codes[i], symbol));
142  }
143  }
144 }
145 
146 template <class BITSTREAM>
147 uint32 Huffman<BITSTREAM>::getSymbol(BITSTREAM &bits) const {
148  uint32 code = bits.peekBits(_prefixTableBits);
149 
150  uint8 length = _prefixTable[code].length;
151 
152  if (length != 0xFF) {
153  bits.skip(length);
154  return _prefixTable[code].symbol;
155  } else {
156  bits.skip(_prefixTableBits);
157 
158  for (uint32 i = 0; i < _codes.size(); i++) {
159  bits.addBit(code, i + _prefixTableBits);
160 
161  for (typename CodeList::const_iterator cCode = _codes[i].begin(); cCode != _codes[i].end(); ++cCode)
162  if (code == cCode->code)
163  return cCode->symbol;
164  }
165  }
166 
167  error("Unknown Huffman code");
168  return 0;
169 }
170 
173 } // End of namespace Common
174 
175 #endif // COMMON_HUFFMAN_H
Definition: list.h:44
uint32 getSymbol(BITSTREAM &bits) const
Definition: huffman.h:147
Huffman(uint8 maxLength, uint32 codeCount, const uint32 *codes, const uint8 *lengths, const uint32 *symbols=nullptr)
Definition: huffman.h:101
Definition: huffman.h:59
Definition: algorithm.h:29
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: list_intern.h:48
T MAX(T a, T b)
Definition: util.h:62