Coverage Report

Created: 2026-02-26 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brunsli/c/dec/arith_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
#ifndef BRUNSLI_DEC_ARITH_DECODE_H_
8
#define BRUNSLI_DEC_ARITH_DECODE_H_
9
10
#include "../common/distributions.h"
11
#include <brunsli/types.h>
12
#include "./brunsli_input.h"
13
14
namespace brunsli {
15
16
// A class used for entropy decoding a sequence of binary values.
17
// skal@ wrote the original version, szabadka@ ported it for brunsli.
18
class BinaryArithmeticDecoder {
19
 public:
20
35.2k
  BinaryArithmeticDecoder() {}
21
22
18.9k
  void Init(WordSource* in) {
23
18.9k
    low_ = 0;
24
18.9k
    high_ = ~0u;
25
18.9k
    value_ = in->GetNextWord();
26
18.9k
    value_ = (value_ << 16u) | in->GetNextWord();
27
18.9k
  }
28
29
  // Returns the next bit decoded from the bit stream, based on the given 8-bit
30
  // precision probability, i.e. P(bit = 0) = prob / 256. This probability must
31
  // be the same as the one used by the encoder.
32
143M
  int ReadBit(int prob, WordSource* in) {
33
143M
    const uint32_t diff = high_ - low_;
34
143M
    const uint32_t split = low_ + (((uint64_t)diff * prob) >> 8u);
35
143M
    int bit;
36
143M
    if (value_ > split) {
37
66.9M
      low_ = split + 1;
38
66.9M
      bit = 1;
39
76.9M
    } else {
40
76.9M
      high_ = split;
41
76.9M
      bit = 0;
42
76.9M
    }
43
143M
    if (((low_ ^ high_) >> 16u) == 0) {
44
2.59M
      value_ = (value_ << 16u) | in->GetNextWord();
45
2.59M
      low_ <<= 16u;
46
2.59M
      high_ <<= 16u;
47
2.59M
      high_ |= 0xFFFFu;
48
2.59M
    }
49
143M
    return bit;
50
143M
  }
51
52
 private:
53
  uint32_t low_;
54
  uint32_t high_;
55
  uint32_t value_;
56
};
57
58
}  // namespace brunsli
59
60
#endif  // BRUNSLI_DEC_ARITH_DECODE_H_