1 // Copyright 2016 The Draco Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 #include "draco/compression/bit_coders/rans_bit_encoder.h"
16
17 #include "draco/compression/entropy/ans.h"
18 #include "draco/core/bit_utils.h"
19 #include "draco/core/varint_encoding.h"
20
21 namespace draco {
22
RAnsBitEncoder()23 RAnsBitEncoder::RAnsBitEncoder() : local_bits_(0), num_local_bits_(0) {}
24
~RAnsBitEncoder()25 RAnsBitEncoder::~RAnsBitEncoder() { Clear(); }
26
StartEncoding()27 void RAnsBitEncoder::StartEncoding() { Clear(); }
28
EncodeBit(bool bit)29 void RAnsBitEncoder::EncodeBit(bool bit) {
30 if (bit) {
31 bit_counts_[1]++;
32 local_bits_ |= 1 << num_local_bits_;
33 } else {
34 bit_counts_[0]++;
35 }
36 num_local_bits_++;
37
38 if (num_local_bits_ == 32) {
39 bits_.push_back(local_bits_);
40 num_local_bits_ = 0;
41 local_bits_ = 0;
42 }
43 }
44
EncodeLeastSignificantBits32(int nbits,uint32_t value)45 void RAnsBitEncoder::EncodeLeastSignificantBits32(int nbits, uint32_t value) {
46 DRACO_DCHECK_EQ(true, nbits <= 32);
47 DRACO_DCHECK_EQ(true, nbits > 0);
48
49 const uint32_t reversed = ReverseBits32(value) >> (32 - nbits);
50 const int ones = CountOneBits32(reversed);
51 bit_counts_[0] += (nbits - ones);
52 bit_counts_[1] += ones;
53
54 const int remaining = 32 - num_local_bits_;
55
56 if (nbits <= remaining) {
57 CopyBits32(&local_bits_, num_local_bits_, reversed, 0, nbits);
58 num_local_bits_ += nbits;
59 if (num_local_bits_ == 32) {
60 bits_.push_back(local_bits_);
61 local_bits_ = 0;
62 num_local_bits_ = 0;
63 }
64 } else {
65 CopyBits32(&local_bits_, num_local_bits_, reversed, 0, remaining);
66 bits_.push_back(local_bits_);
67 local_bits_ = 0;
68 CopyBits32(&local_bits_, 0, reversed, remaining, nbits - remaining);
69 num_local_bits_ = nbits - remaining;
70 }
71 }
72
EndEncoding(EncoderBuffer * target_buffer)73 void RAnsBitEncoder::EndEncoding(EncoderBuffer *target_buffer) {
74 uint64_t total = bit_counts_[1] + bit_counts_[0];
75 if (total == 0) {
76 total++;
77 }
78
79 // The probability interval [0,1] is mapped to values of [0, 256]. However,
80 // the coding scheme can not deal with probabilities of 0 or 1, which is why
81 // we must clamp the values to interval [1, 255]. Specifically 128
82 // corresponds to 0.5 exactly. And the value can be given as uint8_t.
83 const uint32_t zero_prob_raw = static_cast<uint32_t>(
84 ((bit_counts_[0] / static_cast<double>(total)) * 256.0) + 0.5);
85
86 uint8_t zero_prob = 255;
87 if (zero_prob_raw < 255) {
88 zero_prob = static_cast<uint8_t>(zero_prob_raw);
89 }
90
91 zero_prob += (zero_prob == 0);
92
93 // Space for 32 bit integer and some extra space.
94 std::vector<uint8_t> buffer((bits_.size() + 8) * 8);
95 AnsCoder ans_coder;
96 ans_write_init(&ans_coder, buffer.data());
97
98 for (int i = num_local_bits_ - 1; i >= 0; --i) {
99 const uint8_t bit = (local_bits_ >> i) & 1;
100 rabs_write(&ans_coder, bit, zero_prob);
101 }
102 for (auto it = bits_.rbegin(); it != bits_.rend(); ++it) {
103 const uint32_t bits = *it;
104 for (int i = 31; i >= 0; --i) {
105 const uint8_t bit = (bits >> i) & 1;
106 rabs_write(&ans_coder, bit, zero_prob);
107 }
108 }
109
110 const int size_in_bytes = ans_write_end(&ans_coder);
111 target_buffer->Encode(zero_prob);
112 EncodeVarint(static_cast<uint32_t>(size_in_bytes), target_buffer);
113 target_buffer->Encode(buffer.data(), size_in_bytes);
114
115 Clear();
116 }
117
Clear()118 void RAnsBitEncoder::Clear() {
119 bit_counts_.assign(2, 0);
120 bits_.clear();
121 local_bits_ = 0;
122 num_local_bits_ = 0;
123 }
124
125 } // namespace draco
126