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/decoder_buffer.h"
16 
17 #include "draco/core/macros.h"
18 #include "draco/core/varint_decoding.h"
19 
20 namespace draco {
21 
DecoderBuffer()22 DecoderBuffer::DecoderBuffer()
23     : data_(nullptr),
24       data_size_(0),
25       pos_(0),
26       bit_mode_(false),
27       bitstream_version_(0) {}
28 
Init(const char * data,size_t data_size)29 void DecoderBuffer::Init(const char *data, size_t data_size) {
30   Init(data, data_size, bitstream_version_);
31 }
32 
Init(const char * data,size_t data_size,uint16_t version)33 void DecoderBuffer::Init(const char *data, size_t data_size, uint16_t version) {
34   data_ = data;
35   data_size_ = data_size;
36   bitstream_version_ = version;
37   pos_ = 0;
38 }
39 
StartBitDecoding(bool decode_size,uint64_t * out_size)40 bool DecoderBuffer::StartBitDecoding(bool decode_size, uint64_t *out_size) {
41   if (decode_size) {
42 #ifdef DRACO_BACKWARDS_COMPATIBILITY_SUPPORTED
43     if (bitstream_version_ < DRACO_BITSTREAM_VERSION(2, 2)) {
44       if (!Decode(out_size)) {
45         return false;
46       }
47     } else
48 #endif
49     {
50       if (!DecodeVarint(out_size, this)) {
51         return false;
52       }
53     }
54   }
55   bit_mode_ = true;
56   bit_decoder_.reset(data_head(), remaining_size());
57   return true;
58 }
59 
EndBitDecoding()60 void DecoderBuffer::EndBitDecoding() {
61   bit_mode_ = false;
62   const uint64_t bits_decoded = bit_decoder_.BitsDecoded();
63   const uint64_t bytes_decoded = (bits_decoded + 7) / 8;
64   pos_ += bytes_decoded;
65 }
66 
BitDecoder()67 DecoderBuffer::BitDecoder::BitDecoder()
68     : bit_buffer_(nullptr), bit_buffer_end_(nullptr), bit_offset_(0) {}
69 
~BitDecoder()70 DecoderBuffer::BitDecoder::~BitDecoder() {}
71 
72 }  // namespace draco
73