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 direct encoding of bits with arithmetic encoder interface.
16 #ifndef DRACO_COMPRESSION_BIT_CODERS_DIRECT_BIT_ENCODER_H_
17 #define DRACO_COMPRESSION_BIT_CODERS_DIRECT_BIT_ENCODER_H_
18 
19 #include <vector>
20 
21 #include "draco/core/encoder_buffer.h"
22 
23 namespace draco {
24 
25 class DirectBitEncoder {
26  public:
27   DirectBitEncoder();
28   ~DirectBitEncoder();
29 
30   // Must be called before any Encode* function is called.
31   void StartEncoding();
32 
33   // Encode one bit. If |bit| is true encode a 1, otherwise encode a 0.
EncodeBit(bool bit)34   void EncodeBit(bool bit) {
35     if (bit) {
36       local_bits_ |= 1 << (31 - num_local_bits_);
37     }
38     num_local_bits_++;
39     if (num_local_bits_ == 32) {
40       bits_.push_back(local_bits_);
41       num_local_bits_ = 0;
42       local_bits_ = 0;
43     }
44   }
45 
46   // Encode |nbits| of |value|, starting from the least significant bit.
47   // |nbits| must be > 0 and <= 32.
EncodeLeastSignificantBits32(int nbits,uint32_t value)48   void EncodeLeastSignificantBits32(int nbits, uint32_t value) {
49     DRACO_DCHECK_EQ(true, nbits <= 32);
50     DRACO_DCHECK_EQ(true, nbits > 0);
51 
52     const int remaining = 32 - num_local_bits_;
53 
54     // Make sure there are no leading bits that should not be encoded and
55     // start from here.
56     value = value << (32 - nbits);
57     if (nbits <= remaining) {
58       value = value >> num_local_bits_;
59       local_bits_ = local_bits_ | value;
60       num_local_bits_ += nbits;
61       if (num_local_bits_ == 32) {
62         bits_.push_back(local_bits_);
63         local_bits_ = 0;
64         num_local_bits_ = 0;
65       }
66     } else {
67       value = value >> (32 - nbits);
68       num_local_bits_ = nbits - remaining;
69       const uint32_t value_l = value >> num_local_bits_;
70       local_bits_ = local_bits_ | value_l;
71       bits_.push_back(local_bits_);
72       local_bits_ = value << (32 - num_local_bits_);
73     }
74   }
75 
76   // Ends the bit encoding and stores the result into the target_buffer.
77   void EndEncoding(EncoderBuffer *target_buffer);
78 
79  private:
80   void Clear();
81 
82   std::vector<uint32_t> bits_;
83   uint32_t local_bits_;
84   uint32_t num_local_bits_;
85 };
86 
87 }  // namespace draco
88 
89 #endif  // DRACO_COMPRESSION_BIT_CODERS_DIRECT_BIT_ENCODER_H_
90