| 1 | /* |
| 2 | Copyright 2015 - 2026 Esri |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | |
| 16 | A local copy of the license and additional notices are located with the |
| 17 | source distribution at: |
| 18 | |
| 19 | http://github.com/Esri/lerc/ |
| 20 | |
| 21 | Contributors: Thomas Maurer |
| 22 | */ |
| 23 | |
| 24 | #ifndef HUFFMAN_H |
| 25 | #define HUFFMAN_H |
| 26 | |
| 27 | #include <vector> |
| 28 | #include <cstring> |
| 29 | #include <utility> |
| 30 | #include "Defines.h" |
| 31 | |
| 32 | NAMESPACE_LERC_START |
| 33 | |
| 34 | class Huffman |
| 35 | { |
| 36 | public: |
| 37 | Huffman() : m_maxHistoSize(1 << 15), m_maxNumBitsLUT(12), m_numBitsToSkipInTree(0), m_root(nullptr) {} |
| 38 | ~Huffman() { Clear(); } |
| 39 | |
| 40 | // Limitation: We limit the max Huffman code length to 32 bit. If this happens, the function ComputeCodes() |
| 41 | // returns false. In that case don't use Huffman coding but Lerc only instead. |
| 42 | // This won't happen easily. For the worst case input maximizing the Huffman code length the counts in the |
| 43 | // histogram have to follow the Fibonacci sequence. Even then, for < 9,227,465 data values, 32 bit is |
| 44 | // the max Huffman code length possible. |
| 45 | |
| 46 | bool ComputeCodes(const std::vector<int>& histo); // input histogram, size < 2^15 |
| 47 | |
| 48 | bool ComputeCompressedSize(const std::vector<int>& histo, int& numBytes, double& avgBpp) const; |
| 49 | |
| 50 | // LUT of same size as histogram, each entry has length of Huffman bit code, and the bit code |
| 51 | const std::vector<std::pair<unsigned short, unsigned int> >& GetCodes() const { return m_codeTable; } |
| 52 | bool SetCodes(const std::vector<std::pair<unsigned short, unsigned int> >& codeTable); |
| 53 | |
| 54 | bool WriteCodeTable(Byte** ppByte, int lerc2Version) const; |
| 55 | bool ReadCodeTable(const Byte** ppByte, size_t& nBytesRemaining, int lerc2Version); |
| 56 | |
| 57 | bool BuildTreeFromCodes(int& numBitsLUT); |
| 58 | inline bool DecodeOneValue(const Byte** ppSrc, size_t& nBytesRemaining, int& bitPos, int numBitsLUT, int& value) const; |
| 59 | inline static bool PushValue(Byte** ppByte, int& bitPos, unsigned int value, int len); |
| 60 | void Clear(); |
| 61 | |
| 62 | private: |
| 63 | |
| 64 | struct Node |
| 65 | { |
| 66 | int weight; |
| 67 | short value; |
| 68 | Node *child0, *child1; |
| 69 | |
| 70 | Node(short val, int cnt) // new leaf node for val |
| 71 | { |
| 72 | value = val; |
| 73 | weight = -cnt; |
| 74 | child0 = child1 = nullptr; |
| 75 | } |
| 76 | |
| 77 | Node(Node* c0, Node* c1) // new internal node from children c0 and c1 |
| 78 | { |
| 79 | value = -1; |
| 80 | weight = c0->weight + c1->weight; |
| 81 | child0 = c0; |
| 82 | child1 = c1; |
| 83 | } |
| 84 | |
| 85 | bool operator < (const Node& other) const { return weight < other.weight; } |
| 86 | |
| 87 | bool TreeToLUT(unsigned short numBits, unsigned int bits, std::vector<std::pair<unsigned short, unsigned int> >& luTable) const |
| 88 | { |
| 89 | if (child0) |
| 90 | { |
| 91 | if (numBits == 32 // the max huffman code length we allow |
| 92 | || !child0->TreeToLUT(numBits + 1, (bits << 1) + 0, luTable) |
| 93 | || !child1->TreeToLUT(numBits + 1, (bits << 1) + 1, luTable)) |
| 94 | { |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | else |
| 99 | luTable[value] = std::pair<unsigned short, unsigned int>(numBits, bits); |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | void FreeTree(int& n) |
| 105 | { |
| 106 | if (child0) |
| 107 | { |
| 108 | child0->FreeTree(n); |
| 109 | delete child0; |
| 110 | child0 = nullptr; |
| 111 | n--; |
| 112 | } |
| 113 | if (child1) |
| 114 | { |
| 115 | child1->FreeTree(n); |
| 116 | delete child1; |
| 117 | child1 = nullptr; |
| 118 | n--; |
| 119 | } |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | private: |
| 124 | |
| 125 | size_t m_maxHistoSize; |
| 126 | std::vector<std::pair<unsigned short, unsigned int> > m_codeTable; |
| 127 | std::vector<std::pair<short, short> > m_decodeLUT; |
| 128 | int m_maxNumBitsLUT; |
| 129 | int m_numBitsToSkipInTree; |
| 130 | Node* m_root; |
| 131 | |
| 132 | static int GetIndexWrapAround(int i, int size) { return i - (i < size ? 0 : size); } |
| 133 | |
| 134 | bool ComputeNumBytesCodeTable(int& numBytes) const; |
| 135 | bool GetRange(int& i0, int& i1, int& maxCodeLength) const; |
| 136 | bool BitStuffCodes(Byte** ppByte, int i0, int i1) const; |
| 137 | bool BitUnStuffCodes(const Byte** ppByte, size_t& nBytesRemaining, int i0, int i1); |
| 138 | bool ConvertCodesToCanonical(); |
| 139 | void ClearTree(); |
| 140 | }; |
| 141 | |
| 142 | // -------------------------------------------------------------------------- ; |
| 143 | |
| 144 | inline bool Huffman::DecodeOneValue(const Byte** ppSrc, size_t& nBytesRemaining, int& bitPos, int numBitsLUT, int& value) const |
| 145 | { |
| 146 | const size_t s4 = sizeof(unsigned int); |
| 147 | |
| 148 | if (!ppSrc || !(*ppSrc) || bitPos < 0 || bitPos >= 32 || nBytesRemaining < s4) |
| 149 | return false; |
| 150 | |
| 151 | // first get the next (up to) 12 bits as a copy |
| 152 | unsigned int temp(0); |
| 153 | memcpy(&temp, *ppSrc, s4); |
| 154 | int valTmp = (temp << bitPos) >> (32 - numBitsLUT); |
| 155 | |
| 156 | if (32 - bitPos < numBitsLUT) |
| 157 | { |
| 158 | if (nBytesRemaining < 2 * s4) |
| 159 | return false; |
| 160 | |
| 161 | memcpy(&temp, *ppSrc + s4, s4); |
| 162 | valTmp |= temp >> (64 - bitPos - numBitsLUT); |
| 163 | } |
| 164 | |
| 165 | if (m_decodeLUT[valTmp].first >= 0) // if there, move the correct number of bits and done |
| 166 | { |
| 167 | value = m_decodeLUT[valTmp].second; |
| 168 | bitPos += m_decodeLUT[valTmp].first; |
| 169 | if (bitPos >= 32) |
| 170 | { |
| 171 | bitPos -= 32; |
| 172 | *ppSrc += s4; |
| 173 | nBytesRemaining -= s4; |
| 174 | } |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | // if not there, go through the tree (slower) |
| 179 | if (!m_root) |
| 180 | return false; |
| 181 | |
| 182 | // skip leading 0 bits before entering the tree |
| 183 | bitPos += m_numBitsToSkipInTree; |
| 184 | if (bitPos >= 32) |
| 185 | { |
| 186 | bitPos -= 32; |
| 187 | *ppSrc += s4; |
| 188 | nBytesRemaining -= s4; |
| 189 | } |
| 190 | |
| 191 | const Node* node = m_root; |
| 192 | value = -1; |
| 193 | while (value < 0 && nBytesRemaining >= s4) |
| 194 | { |
| 195 | memcpy(&temp, *ppSrc, s4); |
| 196 | int bit = (temp << bitPos) >> 31; |
| 197 | bitPos++; |
| 198 | if (bitPos == 32) |
| 199 | { |
| 200 | bitPos = 0; |
| 201 | *ppSrc += s4; |
| 202 | nBytesRemaining -= s4; |
| 203 | } |
| 204 | |
| 205 | node = bit ? node->child1 : node->child0; |
| 206 | if (!node) |
| 207 | return false; |
| 208 | |
| 209 | if (node->value >= 0) // reached a leaf node |
| 210 | value = node->value; |
| 211 | } |
| 212 | |
| 213 | return (value >= 0); |
| 214 | } |
| 215 | |
| 216 | // -------------------------------------------------------------------------- ; |
| 217 | |
| 218 | inline bool Huffman::PushValue(Byte** ppByte, int& bitPos, unsigned int value, int len) |
| 219 | { |
| 220 | const size_t s4 = sizeof(unsigned int); |
| 221 | |
| 222 | if (!ppByte || !(*ppByte) || bitPos < 0 || bitPos >= 32 || len <= 0 || len > 32) |
| 223 | return false; |
| 224 | |
| 225 | if (32 - bitPos >= len) |
| 226 | { |
| 227 | if (bitPos == 0) |
| 228 | memset(*ppByte, 0, s4); |
| 229 | |
| 230 | unsigned int temp(0); |
| 231 | memcpy(&temp, *ppByte, s4); |
| 232 | temp |= value << (32 - bitPos - len); |
| 233 | memcpy(*ppByte, &temp, s4); |
| 234 | |
| 235 | bitPos += len; |
| 236 | if (bitPos == 32) |
| 237 | { |
| 238 | bitPos = 0; |
| 239 | *ppByte += s4; |
| 240 | } |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | bitPos += len - 32; |
| 245 | unsigned int temp(0); |
| 246 | memcpy(&temp, *ppByte, s4); |
| 247 | temp |= value >> bitPos; |
| 248 | memcpy(*ppByte, &temp, s4); |
| 249 | *ppByte += s4; |
| 250 | temp = value << (32 - bitPos); |
| 251 | memcpy(*ppByte, &temp, s4); |
| 252 | } |
| 253 | |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | // -------------------------------------------------------------------------- ; |
| 258 | |
| 259 | NAMESPACE_LERC_END |
| 260 | #endif |