/src/brunsli/c/dec/huffman_decode.h
Line | Count | Source |
1 | | // Copyright (c) Google LLC 2019 |
2 | | // |
3 | | // Use of this source code is governed by an MIT-style |
4 | | // license that can be found in the LICENSE file or at |
5 | | // https://opensource.org/licenses/MIT. |
6 | | |
7 | | // Library to decode the Huffman code lengths from the bit-stream and build a |
8 | | // decoding table from them. |
9 | | |
10 | | #ifndef BRUNSLI_DEC_HUFFMAN_DECODE_H_ |
11 | | #define BRUNSLI_DEC_HUFFMAN_DECODE_H_ |
12 | | |
13 | | #include <memory> |
14 | | #include <vector> |
15 | | |
16 | | #include "./huffman_table.h" |
17 | | |
18 | | namespace brunsli { |
19 | | |
20 | | struct BrunsliBitReader; |
21 | | |
22 | | template<typename T> |
23 | | struct Arena { |
24 | | size_t capacity = 0; |
25 | | // TODO(eustas): use "char" storage to ensure new[] does not initialize... |
26 | | std::unique_ptr<T[]> storage; |
27 | | |
28 | 6.12k | void reserve(size_t limit) { |
29 | 6.12k | if (capacity < limit) { |
30 | 5.95k | capacity = limit; |
31 | 5.95k | storage.reset(new T[capacity]); |
32 | 5.95k | } |
33 | 6.12k | } |
34 | | |
35 | 489 | T* data() { |
36 | 489 | return storage.get(); |
37 | 489 | } |
38 | | |
39 | 4.61k | void reset() { |
40 | 4.61k | capacity = 0; |
41 | 4.61k | storage.reset(); |
42 | 4.61k | } |
43 | | }; |
44 | | |
45 | | struct HuffmanDecodingData { |
46 | | // Decodes the Huffman code lengths from the bit-stream and fills in the |
47 | | // pre-allocated table with the corresponding 2-level Huffman decoding table. |
48 | | // |arena| is used as an intermediate output for BuildHuffmanTable. |
49 | | // Returns false if the Huffman code lengths can not de decoded. |
50 | | bool ReadFromBitStream(size_t alphabet_size, BrunsliBitReader* br, |
51 | | Arena<HuffmanCode>* arena = nullptr); |
52 | | |
53 | | uint16_t ReadSymbol(BrunsliBitReader* br) const; |
54 | | |
55 | | std::vector<HuffmanCode> table_; |
56 | | }; |
57 | | |
58 | | } // namespace brunsli |
59 | | |
60 | | #endif // BRUNSLI_DEC_HUFFMAN_DECODE_H_ |