Coverage Report

Created: 2025-11-09 06:58

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