Coverage Report

Created: 2025-06-13 07:37

/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
3.91G
  JXL_INLINE uint16_t ReadSymbol(BitReader* br) const {
29
3.91G
    size_t n_bits;
30
3.91G
    const HuffmanCode* table = table_.data();
31
3.91G
    table += br->PeekBits(kHuffmanTableBits);
32
3.91G
    n_bits = table->bits;
33
3.91G
    if (n_bits > kHuffmanTableBits) {
34
76.8M
      br->Consume(kHuffmanTableBits);
35
76.8M
      n_bits -= kHuffmanTableBits;
36
76.8M
      table += table->value;
37
76.8M
      table += br->PeekBits(n_bits);
38
76.8M
    }
39
3.91G
    br->Consume(table->bits);
40
3.91G
    return table->value;
41
3.91G
  }
42
43
  std::vector<HuffmanCode> table_;
44
};
45
46
}  // namespace jxl
47
48
#endif  // LIB_JXL_DEC_HUFFMAN_H_