/src/libjxl/lib/jxl/dec_huffman.h
Line | Count | Source |
1 | | // Copyright (c) the JPEG XL Project Authors. All rights reserved. |
2 | | // |
3 | | // Use of this source code is governed by a BSD-style |
4 | | // license that can be found in the LICENSE file. |
5 | | |
6 | | #ifndef LIB_JXL_DEC_HUFFMAN_H_ |
7 | | #define LIB_JXL_DEC_HUFFMAN_H_ |
8 | | |
9 | | #include <cstddef> |
10 | | #include <cstdint> |
11 | | #include <vector> |
12 | | |
13 | | #include "lib/jxl/base/compiler_specific.h" |
14 | | #include "lib/jxl/dec_bit_reader.h" |
15 | | #include "lib/jxl/huffman_table.h" |
16 | | |
17 | | namespace jxl { |
18 | | |
19 | | static constexpr size_t kHuffmanTableBits = 8u; |
20 | | |
21 | | struct HuffmanDecodingData { |
22 | | // Decodes the Huffman code lengths from the bit-stream and fills in the |
23 | | // pre-allocated table with the corresponding 2-level Huffman decoding table. |
24 | | // Returns false if the Huffman code lengths can not be decoded. |
25 | | bool ReadFromBitStream(size_t alphabet_size, BitReader* br); |
26 | | |
27 | | // Decodes the next Huffman coded symbol from the bit-stream. |
28 | 82.3M | JXL_INLINE uint16_t ReadSymbol(BitReader* br) const { |
29 | 82.3M | size_t n_bits; |
30 | 82.3M | const HuffmanCode* table = table_.data(); |
31 | 82.3M | table += br->PeekBits(kHuffmanTableBits); |
32 | 82.3M | n_bits = table->bits; |
33 | 82.3M | if (n_bits > kHuffmanTableBits) { |
34 | 4.55k | br->Consume(kHuffmanTableBits); |
35 | 4.55k | n_bits -= kHuffmanTableBits; |
36 | 4.55k | table += table->value; |
37 | 4.55k | table += br->PeekBits(n_bits); |
38 | 4.55k | } |
39 | 82.3M | br->Consume(table->bits); |
40 | 82.3M | return table->value; |
41 | 82.3M | } |
42 | | |
43 | | std::vector<HuffmanCode> table_; |
44 | | }; |
45 | | |
46 | | } // namespace jxl |
47 | | |
48 | | #endif // LIB_JXL_DEC_HUFFMAN_H_ |