Coverage Report

Created: 2025-10-13 06:03

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
20.7k
  BinaryArithmeticDecoder() {}
21
22
10.7k
  void Init(WordSource* in) {
23
10.7k
    low_ = 0;
24
10.7k
    high_ = ~0u;
25
10.7k
    value_ = in->GetNextWord();
26
10.7k
    value_ = (value_ << 16u) | in->GetNextWord();
27
10.7k
  }
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
109M
  int ReadBit(int prob, WordSource* in) {
33
109M
    const uint32_t diff = high_ - low_;
34
109M
    const uint32_t split = low_ + (((uint64_t)diff * prob) >> 8u);
35
109M
    int bit;
36
109M
    if (value_ > split) {
37
52.7M
      low_ = split + 1;
38
52.7M
      bit = 1;
39
56.4M
    } else {
40
56.4M
      high_ = split;
41
56.4M
      bit = 0;
42
56.4M
    }
43
109M
    if (((low_ ^ high_) >> 16u) == 0) {
44
2.62M
      value_ = (value_ << 16u) | in->GetNextWord();
45
2.62M
      low_ <<= 16u;
46
2.62M
      high_ <<= 16u;
47
2.62M
      high_ |= 0xFFFFu;
48
2.62M
    }
49
109M
    return bit;
50
109M
  }
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_