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 // File provides basic classes and functions for rANS coding.
16 #ifndef DRACO_COMPRESSION_BIT_CODERS_RANS_BIT_ENCODER_H_
17 #define DRACO_COMPRESSION_BIT_CODERS_RANS_BIT_ENCODER_H_
18 
19 #include <vector>
20 
21 #include "draco/core/encoder_buffer.h"
22 
23 namespace draco {
24 
25 // Class for encoding a sequence of bits using rANS. The probability table used
26 // to encode the bits is based off the total counts of bits.
27 // TODO(fgalligan): Investigate using an adaptive table for more compression.
28 class RAnsBitEncoder {
29  public:
30   RAnsBitEncoder();
31   ~RAnsBitEncoder();
32 
33   // Must be called before any Encode* function is called.
34   void StartEncoding();
35 
36   // Encode one bit. If |bit| is true encode a 1, otherwise encode a 0.
37   void EncodeBit(bool bit);
38 
39   // Encode |nbits| of |value|, starting from the least significant bit.
40   // |nbits| must be > 0 and <= 32.
41   void EncodeLeastSignificantBits32(int nbits, uint32_t value);
42 
43   // Ends the bit encoding and stores the result into the target_buffer.
44   void EndEncoding(EncoderBuffer *target_buffer);
45 
46  private:
47   void Clear();
48 
49   std::vector<uint64_t> bit_counts_;
50   std::vector<uint32_t> bits_;
51   uint32_t local_bits_;
52   uint32_t num_local_bits_;
53 };
54 
55 }  // namespace draco
56 
57 #endif  // DRACO_COMPRESSION_BIT_CODERS_RANS_BIT_ENCODER_H_
58