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