GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
direct_bit_decoder.h
Go to the documentation of this file.
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_DECODER_H_
17#define DRACO_COMPRESSION_BIT_CODERS_DIRECT_BIT_DECODER_H_
18
19#include <vector>
20
22
23namespace draco {
24
26 public:
29
30 // Sets |source_buffer| as the buffer to decode bits from.
31 bool StartDecoding(DecoderBuffer *source_buffer);
32
33 // Decode one bit. Returns true if the bit is a 1, otherwise false.
35 const uint32_t selector = 1 << (31 - num_used_bits_);
36 if (pos_ == bits_.end()) {
37 return false;
38 }
39 const bool bit = *pos_ & selector;
40 ++num_used_bits_;
41 if (num_used_bits_ == 32) {
42 ++pos_;
43 num_used_bits_ = 0;
44 }
45 return bit;
46 }
47
48 // Decode the next |nbits| and return the sequence in |value|. |nbits| must be
49 // > 0 and <= 32.
50 bool DecodeLeastSignificantBits32(int nbits, uint32_t *value) {
51 DRACO_DCHECK_EQ(true, nbits <= 32);
52 DRACO_DCHECK_EQ(true, nbits > 0);
53 const int remaining = 32 - num_used_bits_;
54 if (nbits <= remaining) {
55 if (pos_ == bits_.end()) {
56 return false;
57 }
58 *value = (*pos_ << num_used_bits_) >> (32 - nbits);
59 num_used_bits_ += nbits;
60 if (num_used_bits_ == 32) {
61 ++pos_;
62 num_used_bits_ = 0;
63 }
64 } else {
65 if (pos_ + 1 == bits_.end()) {
66 return false;
67 }
68 const uint32_t value_l = ((*pos_) << num_used_bits_);
69 num_used_bits_ = nbits - remaining;
70 ++pos_;
71 const uint32_t value_r = (*pos_) >> (32 - num_used_bits_);
72 *value = (value_l >> (32 - num_used_bits_ - remaining)) | value_r;
73 }
74 return true;
75 }
76
77 void EndDecoding() {}
78
79 private:
80 void Clear();
81
82 std::vector<uint32_t> bits_;
83 std::vector<uint32_t>::const_iterator pos_;
84 uint32_t num_used_bits_;
85};
86
87} // namespace draco
88
89#endif // DRACO_COMPRESSION_BIT_CODERS_DIRECT_BIT_DECODER_H_
Definition decoder_buffer.h:30
~DirectBitDecoder()
Definition direct_bit_decoder.cc:23
bool DecodeLeastSignificantBits32(int nbits, uint32_t *value)
Definition direct_bit_decoder.h:50
bool DecodeNextBit()
Definition direct_bit_decoder.h:34
DirectBitDecoder()
Definition direct_bit_decoder.cc:21
void EndDecoding()
Definition direct_bit_decoder.h:77
bool StartDecoding(DecoderBuffer *source_buffer)
Definition direct_bit_decoder.cc:25
#define DRACO_DCHECK_EQ(a, b)
Definition macros.h:81
Definition attribute_octahedron_transform.cc:21