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/core/encoder_buffer.h"
16 
17 #include <cstring>  // for memcpy
18 
19 #include "draco/core/varint_encoding.h"
20 
21 namespace draco {
22 
EncoderBuffer()23 EncoderBuffer::EncoderBuffer()
24     : bit_encoder_reserved_bytes_(false), encode_bit_sequence_size_(false) {}
25 
Clear()26 void EncoderBuffer::Clear() {
27   buffer_.clear();
28   bit_encoder_reserved_bytes_ = 0;
29 }
30 
Resize(int64_t nbytes)31 void EncoderBuffer::Resize(int64_t nbytes) { buffer_.resize(nbytes); }
32 
StartBitEncoding(int64_t required_bits,bool encode_size)33 bool EncoderBuffer::StartBitEncoding(int64_t required_bits, bool encode_size) {
34   if (bit_encoder_active())
35     return false;  // Bit encoding mode already active.
36   if (required_bits <= 0)
37     return false;  // Invalid size.
38   encode_bit_sequence_size_ = encode_size;
39   const int64_t required_bytes = (required_bits + 7) / 8;
40   bit_encoder_reserved_bytes_ = required_bytes;
41   uint64_t buffer_start_size = buffer_.size();
42   if (encode_size) {
43     // Reserve memory for storing the encoded bit sequence size. It will be
44     // filled once the bit encoding ends.
45     buffer_start_size += sizeof(uint64_t);
46   }
47   // Resize buffer to fit the maximum size of encoded bit data.
48   buffer_.resize(buffer_start_size + required_bytes);
49   // Get the buffer data pointer for the bit encoder.
50   const char *const data = buffer_.data() + buffer_start_size;
51   bit_encoder_ =
52       std::unique_ptr<BitEncoder>(new BitEncoder(const_cast<char *>(data)));
53   return true;
54 }
55 
EndBitEncoding()56 void EncoderBuffer::EndBitEncoding() {
57   if (!bit_encoder_active())
58     return;
59   // Get the number of encoded bits and bytes (rounded up).
60   const uint64_t encoded_bits = bit_encoder_->Bits();
61   const uint64_t encoded_bytes = (encoded_bits + 7) / 8;
62   // Flush all cached bits that are not in the bit encoder's main buffer.
63   bit_encoder_->Flush(0);
64   // Encode size if needed.
65   if (encode_bit_sequence_size_) {
66     char *out_mem = const_cast<char *>(data() + size());
67     // Make the out_mem point to the memory reserved for storing the size.
68     out_mem = out_mem - (bit_encoder_reserved_bytes_ + sizeof(uint64_t));
69 
70     EncoderBuffer var_size_buffer;
71     EncodeVarint(encoded_bytes, &var_size_buffer);
72     const uint32_t size_len = static_cast<uint32_t>(var_size_buffer.size());
73     char *const dst = out_mem + size_len;
74     const char *const src = out_mem + sizeof(uint64_t);
75     memmove(dst, src, encoded_bytes);
76 
77     // Store the size of the encoded data.
78     memcpy(out_mem, var_size_buffer.data(), size_len);
79 
80     // We need to account for the difference between the preallocated and actual
81     // storage needed for storing the encoded length. This will be used later to
82     // compute the correct size of |buffer_|.
83     bit_encoder_reserved_bytes_ += sizeof(uint64_t) - size_len;
84   }
85   // Resize the underlying buffer to match the number of encoded bits.
86   buffer_.resize(buffer_.size() - bit_encoder_reserved_bytes_ + encoded_bytes);
87   bit_encoder_reserved_bytes_ = 0;
88 }
89 
90 }  // namespace draco
91