Coverage Report

Created: 2026-10-17 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brunsli/c/dec/ans_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 ANS population counts from the bit-stream and build a
8
// decoding table from them.
9
10
#ifndef BRUNSLI_DEC_ANS_DECODE_H_
11
#define BRUNSLI_DEC_ANS_DECODE_H_
12
13
#include <brunsli/types.h>
14
15
#include <vector>
16
17
#include "../common/ans_params.h"
18
#include "./brunsli_input.h"
19
20
namespace brunsli {
21
22
typedef struct {
23
  uint16_t offset_;
24
  uint16_t freq_;
25
  uint8_t symbol_;
26
} ANSSymbolInfo;
27
28
struct ANSDecodingData {
29
95.2k
  ANSDecodingData() {}
30
31
  bool Init(const std::vector<uint32_t>& counts);
32
33
  ANSSymbolInfo map_[BRUNSLI_ANS_TAB_SIZE];
34
};
35
36
class ANSDecoder {
37
 public:
38
20.5k
  ANSDecoder() {}
39
40
10.1k
  void Init(WordSource* in) {
41
10.1k
    state_ = in->GetNextWord();
42
10.1k
    state_ = (state_ << 16u) | in->GetNextWord();
43
10.1k
  }
44
45
7.79M
  int ReadSymbol(const ANSDecodingData& code, WordSource* in) {
46
7.79M
    const uint32_t res = state_ & (BRUNSLI_ANS_TAB_SIZE - 1);
47
7.79M
    const ANSSymbolInfo& s = code.map_[res];
48
7.79M
    state_ = s.freq_ * (state_ >> BRUNSLI_ANS_LOG_TAB_SIZE) + s.offset_;
49
7.79M
    if (state_ < (1u << 16u)) {
50
639k
      state_ = (state_ << 16u) | in->GetNextWord();
51
639k
    }
52
7.79M
    return s.symbol_;
53
7.79M
  }
54
9.68k
  bool CheckCRC() const { return state_ == (BRUNSLI_ANS_SIGNATURE << 16u); }
55
56
 private:
57
  uint32_t state_;
58
};
59
60
}  // namespace brunsli
61
62
#endif  // BRUNSLI_DEC_ANS_DECODE_H_