GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
folded_integer_bit_encoder.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_FOLDED_INTEGER_BIT_ENCODER_H_
17#define DRACO_COMPRESSION_BIT_CODERS_FOLDED_INTEGER_BIT_ENCODER_H_
18
19#include <vector>
20
22
23namespace draco {
24
25// This coding scheme considers every bit of an (up to) 32bit integer as a
26// separate context. This can be a significant advantage when encoding numbers
27// where it is more likely that the front bits are zero.
28// The behavior is essentially the same as other arithmetic encoding schemes,
29// the only difference is that encoding and decoding of bits must be absolutely
30// symmetric, bits handed in by EncodeBit32 must be also decoded in this way.
31// This is the FoldedBit32Encoder, see also FoldedBit32Decoder.
32template <class BitEncoderT>
34 public:
37
38 // Must be called before any Encode* function is called.
40 for (int i = 0; i < 32; i++) {
41 folded_number_encoders_[i].StartEncoding();
42 }
43 bit_encoder_.StartEncoding();
44 }
45
46 // Encode one bit. If |bit| is true encode a 1, otherwise encode a 0.
47 void EncodeBit(bool bit) { bit_encoder_.EncodeBit(bit); }
48
49 // Encode |nbits| of |value|, starting from the least significant bit.
50 // |nbits| must be > 0 and <= 32.
51 void EncodeLeastSignificantBits32(int nbits, uint32_t value) {
52 uint32_t selector = 1 << (nbits - 1);
53 for (int i = 0; i < nbits; i++) {
54 const bool bit = (value & selector);
55 folded_number_encoders_[i].EncodeBit(bit);
56 selector = selector >> 1;
57 }
58 }
59
60 // Ends the bit encoding and stores the result into the target_buffer.
61 void EndEncoding(EncoderBuffer *target_buffer) {
62 for (int i = 0; i < 32; i++) {
63 folded_number_encoders_[i].EndEncoding(target_buffer);
64 }
65 bit_encoder_.EndEncoding(target_buffer);
66 }
67
68 private:
69 void Clear() {
70 for (int i = 0; i < 32; i++) {
71 folded_number_encoders_[i].Clear();
72 }
73 bit_encoder_.Clear();
74 }
75
76 std::array<BitEncoderT, 32> folded_number_encoders_;
77 BitEncoderT bit_encoder_;
78};
79
80} // namespace draco
81
82#endif // DRACO_COMPRESSION_BIT_CODERS_FOLDED_INTEGER_BIT_ENCODER_H_
Definition encoder_buffer.h:29
void EncodeLeastSignificantBits32(int nbits, uint32_t value)
Definition folded_integer_bit_encoder.h:51
void EndEncoding(EncoderBuffer *target_buffer)
Definition folded_integer_bit_encoder.h:61
FoldedBit32Encoder()
Definition folded_integer_bit_encoder.h:35
~FoldedBit32Encoder()
Definition folded_integer_bit_encoder.h:36
void EncodeBit(bool bit)
Definition folded_integer_bit_encoder.h:47
void StartEncoding()
Definition folded_integer_bit_encoder.h:39
Definition attribute_octahedron_transform.cc:21