GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
rans_symbol_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#ifndef DRACO_COMPRESSION_ENTROPY_RANS_SYMBOL_DECODER_H_
16#define DRACO_COMPRESSION_ENTROPY_RANS_SYMBOL_DECODER_H_
17
23
24namespace draco {
25
26// A helper class for decoding symbols using the rANS algorithm (see ans.h).
27// The class can be used to decode the probability table and the data encoded
28// by the RAnsSymbolEncoder. |unique_symbols_bit_length_t| must be the same as
29// the one used for the corresponding RAnsSymbolEncoder.
30template <int unique_symbols_bit_length_t>
32 public:
33 RAnsSymbolDecoder() : num_symbols_(0) {}
34
35 // Initialize the decoder and decode the probability table.
36 bool Create(DecoderBuffer *buffer);
37
38 uint32_t num_symbols() const { return num_symbols_; }
39
40 // Starts decoding from the buffer. The buffer will be advanced past the
41 // encoded data after this call.
42 bool StartDecoding(DecoderBuffer *buffer);
43 uint32_t DecodeSymbol() { return ans_.rans_read(); }
44 void EndDecoding();
45
46 private:
47 static constexpr int rans_precision_bits_ =
49 unique_symbols_bit_length_t);
50 static constexpr int rans_precision_ = 1 << rans_precision_bits_;
51
52 std::vector<uint32_t> probability_table_;
53 uint32_t num_symbols_;
55};
56
57template <int unique_symbols_bit_length_t>
59 DecoderBuffer *buffer) {
60 // Check that the DecoderBuffer version is set.
61 if (buffer->bitstream_version() == 0) {
62 return false;
63 }
64 // Decode the number of alphabet symbols.
65#ifdef DRACO_BACKWARDS_COMPATIBILITY_SUPPORTED
66 if (buffer->bitstream_version() < DRACO_BITSTREAM_VERSION(2, 0)) {
67 if (!buffer->Decode(&num_symbols_)) {
68 return false;
69 }
70
71 } else
72#endif
73 {
74 if (!DecodeVarint(&num_symbols_, buffer)) {
75 return false;
76 }
77 }
78 // Check that decoded number of symbols is not unreasonably high. Remaining
79 // buffer size must be at least |num_symbols| / 64 bytes to contain the
80 // probability table. The |prob_data| below is one byte but it can be
81 // theoretically stored for each 64th symbol.
82 if (num_symbols_ / 64 > buffer->remaining_size()) {
83 return false;
84 }
85 probability_table_.resize(num_symbols_);
86 if (num_symbols_ == 0) {
87 return true;
88 }
89 // Decode the table.
90 for (uint32_t i = 0; i < num_symbols_; ++i) {
91 uint8_t prob_data = 0;
92 // Decode the first byte and extract the number of extra bytes we need to
93 // get, or the offset to the next symbol with non-zero probability.
94 if (!buffer->Decode(&prob_data)) {
95 return false;
96 }
97 // Token is stored in the first two bits of the first byte. Values 0-2 are
98 // used to indicate the number of extra bytes, and value 3 is a special
99 // symbol used to denote run-length coding of zero probability entries.
100 // See rans_symbol_encoder.h for more details.
101 const int token = prob_data & 3;
102 if (token == 3) {
103 const uint32_t offset = prob_data >> 2;
104 if (i + offset >= num_symbols_) {
105 return false;
106 }
107 // Set zero probability for all symbols in the specified range.
108 for (uint32_t j = 0; j < offset + 1; ++j) {
109 probability_table_[i + j] = 0;
110 }
111 i += offset;
112 } else {
113 const int extra_bytes = token;
114 uint32_t prob = prob_data >> 2;
115 for (int b = 0; b < extra_bytes; ++b) {
116 uint8_t eb;
117 if (!buffer->Decode(&eb)) {
118 return false;
119 }
120 // Shift 8 bits for each extra byte and subtract 2 for the two first
121 // bits.
122 prob |= static_cast<uint32_t>(eb) << (8 * (b + 1) - 2);
123 }
124 probability_table_[i] = prob;
125 }
126 }
127 if (!ans_.rans_build_look_up_table(&probability_table_[0], num_symbols_)) {
128 return false;
129 }
130 return true;
131}
132
133template <int unique_symbols_bit_length_t>
135 DecoderBuffer *buffer) {
136 uint64_t bytes_encoded;
137 // Decode the number of bytes encoded by the encoder.
138#ifdef DRACO_BACKWARDS_COMPATIBILITY_SUPPORTED
139 if (buffer->bitstream_version() < DRACO_BITSTREAM_VERSION(2, 0)) {
140 if (!buffer->Decode(&bytes_encoded)) {
141 return false;
142 }
143
144 } else
145#endif
146 {
147 if (!DecodeVarint<uint64_t>(&bytes_encoded, buffer)) {
148 return false;
149 }
150 }
151 if (bytes_encoded > static_cast<uint64_t>(buffer->remaining_size())) {
152 return false;
153 }
154 const uint8_t *const data_head =
155 reinterpret_cast<const uint8_t *>(buffer->data_head());
156 // Advance the buffer past the rANS data.
157 buffer->Advance(bytes_encoded);
158 if (ans_.read_init(data_head, static_cast<int>(bytes_encoded)) != 0) {
159 return false;
160 }
161 return true;
162}
163
164template <int unique_symbols_bit_length_t>
168
169} // namespace draco
170
171#endif // DRACO_COMPRESSION_ENTROPY_RANS_SYMBOL_DECODER_H_
Definition decoder_buffer.h:30
void Advance(int64_t bytes)
Definition decoder_buffer.h:105
const char * data_head() const
Definition decoder_buffer.h:114
uint16_t bitstream_version() const
Definition decoder_buffer.h:120
bool Decode(T *out_val)
Definition decoder_buffer.h:68
int64_t remaining_size() const
Definition decoder_buffer.h:115
Definition ans.h:414
uint32_t DecodeSymbol()
Definition rans_symbol_decoder.h:43
uint32_t num_symbols() const
Definition rans_symbol_decoder.h:38
RAnsSymbolDecoder()
Definition rans_symbol_decoder.h:33
void EndDecoding()
Definition rans_symbol_decoder.h:165
bool StartDecoding(DecoderBuffer *buffer)
Definition rans_symbol_decoder.h:134
bool Create(DecoderBuffer *buffer)
Definition rans_symbol_decoder.h:58
#define DRACO_BITSTREAM_VERSION(MAJOR, MINOR)
Definition macros.h:114
Definition attribute_octahedron_transform.cc:21
bool DecodeVarint(IntTypeT *out_val, DecoderBuffer *buffer)
Definition varint_decoding.h:63
constexpr int ComputeRAnsPrecisionFromUniqueSymbolsBitLength(int symbols_bit_length)
Definition rans_symbol_coding.h:32