Differential D9642 Diff 31357 extern/draco/draco/src/draco/compression/entropy/rans_symbol_decoder.h
Changeset View
Changeset View
Standalone View
Standalone View
extern/draco/draco/src/draco/compression/entropy/rans_symbol_decoder.h
- This file was moved from extern/draco/dracoenc/src/draco/compression/entropy/rans_symbol_decoder.h.
| Show All 9 Lines | |||||
| // distributed under the License is distributed on an "AS IS" BASIS, | // distributed under the License is distributed on an "AS IS" BASIS, | ||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
| // See the License for the specific language governing permissions and | // See the License for the specific language governing permissions and | ||||
| // limitations under the License. | // limitations under the License. | ||||
| // | // | ||||
| #ifndef DRACO_COMPRESSION_ENTROPY_RANS_SYMBOL_DECODER_H_ | #ifndef DRACO_COMPRESSION_ENTROPY_RANS_SYMBOL_DECODER_H_ | ||||
| #define DRACO_COMPRESSION_ENTROPY_RANS_SYMBOL_DECODER_H_ | #define DRACO_COMPRESSION_ENTROPY_RANS_SYMBOL_DECODER_H_ | ||||
| #include "draco/draco_features.h" | |||||
| #include "draco/compression/config/compression_shared.h" | #include "draco/compression/config/compression_shared.h" | ||||
| #include "draco/compression/entropy/rans_symbol_coding.h" | #include "draco/compression/entropy/rans_symbol_coding.h" | ||||
| #include "draco/core/decoder_buffer.h" | #include "draco/core/decoder_buffer.h" | ||||
| #include "draco/core/varint_decoding.h" | #include "draco/core/varint_decoding.h" | ||||
| #include "draco/draco_features.h" | |||||
| namespace draco { | namespace draco { | ||||
| // A helper class for decoding symbols using the rANS algorithm (see ans.h). | // A helper class for decoding symbols using the rANS algorithm (see ans.h). | ||||
| // The class can be used to decode the probability table and the data encoded | // The class can be used to decode the probability table and the data encoded | ||||
| // by the RAnsSymbolEncoder. |unique_symbols_bit_length_t| must be the same as | // by the RAnsSymbolEncoder. |unique_symbols_bit_length_t| must be the same as | ||||
| // the one used for the corresponding RAnsSymbolEncoder. | // the one used for the corresponding RAnsSymbolEncoder. | ||||
| template <int unique_symbols_bit_length_t> | template <int unique_symbols_bit_length_t> | ||||
| Show All 22 Lines | private: | ||||
| uint32_t num_symbols_; | uint32_t num_symbols_; | ||||
| RAnsDecoder<rans_precision_bits_> ans_; | RAnsDecoder<rans_precision_bits_> ans_; | ||||
| }; | }; | ||||
| template <int unique_symbols_bit_length_t> | template <int unique_symbols_bit_length_t> | ||||
| bool RAnsSymbolDecoder<unique_symbols_bit_length_t>::Create( | bool RAnsSymbolDecoder<unique_symbols_bit_length_t>::Create( | ||||
| DecoderBuffer *buffer) { | DecoderBuffer *buffer) { | ||||
| // Check that the DecoderBuffer version is set. | // Check that the DecoderBuffer version is set. | ||||
| if (buffer->bitstream_version() == 0) | if (buffer->bitstream_version() == 0) { | ||||
| return false; | return false; | ||||
| } | |||||
| // Decode the number of alphabet symbols. | // Decode the number of alphabet symbols. | ||||
| #ifdef DRACO_BACKWARDS_COMPATIBILITY_SUPPORTED | #ifdef DRACO_BACKWARDS_COMPATIBILITY_SUPPORTED | ||||
| if (buffer->bitstream_version() < DRACO_BITSTREAM_VERSION(2, 0)) { | if (buffer->bitstream_version() < DRACO_BITSTREAM_VERSION(2, 0)) { | ||||
| if (!buffer->Decode(&num_symbols_)) | if (!buffer->Decode(&num_symbols_)) { | ||||
| return false; | return false; | ||||
| } | |||||
| } else | } else | ||||
| #endif | #endif | ||||
| { | { | ||||
| if (!DecodeVarint(&num_symbols_, buffer)) | if (!DecodeVarint(&num_symbols_, buffer)) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| } | |||||
| probability_table_.resize(num_symbols_); | probability_table_.resize(num_symbols_); | ||||
| if (num_symbols_ == 0) | if (num_symbols_ == 0) { | ||||
| return true; | return true; | ||||
| } | |||||
| // Decode the table. | // Decode the table. | ||||
| for (uint32_t i = 0; i < num_symbols_; ++i) { | for (uint32_t i = 0; i < num_symbols_; ++i) { | ||||
| uint8_t prob_data = 0; | uint8_t prob_data = 0; | ||||
| // Decode the first byte and extract the number of extra bytes we need to | // Decode the first byte and extract the number of extra bytes we need to | ||||
| // get, or the offset to the next symbol with non-zero probability. | // get, or the offset to the next symbol with non-zero probability. | ||||
| if (!buffer->Decode(&prob_data)) | if (!buffer->Decode(&prob_data)) { | ||||
| return false; | return false; | ||||
| } | |||||
| // Token is stored in the first two bits of the first byte. Values 0-2 are | // Token is stored in the first two bits of the first byte. Values 0-2 are | ||||
| // used to indicate the number of extra bytes, and value 3 is a special | // used to indicate the number of extra bytes, and value 3 is a special | ||||
| // symbol used to denote run-length coding of zero probability entries. | // symbol used to denote run-length coding of zero probability entries. | ||||
| // See rans_symbol_encoder.h for more details. | // See rans_symbol_encoder.h for more details. | ||||
| const int token = prob_data & 3; | const int token = prob_data & 3; | ||||
| if (token == 3) { | if (token == 3) { | ||||
| const uint32_t offset = prob_data >> 2; | const uint32_t offset = prob_data >> 2; | ||||
| if (i + offset >= num_symbols_) | if (i + offset >= num_symbols_) { | ||||
| return false; | return false; | ||||
| } | |||||
| // Set zero probability for all symbols in the specified range. | // Set zero probability for all symbols in the specified range. | ||||
| for (uint32_t j = 0; j < offset + 1; ++j) { | for (uint32_t j = 0; j < offset + 1; ++j) { | ||||
| probability_table_[i + j] = 0; | probability_table_[i + j] = 0; | ||||
| } | } | ||||
| i += offset; | i += offset; | ||||
| } else { | } else { | ||||
| const int extra_bytes = token; | const int extra_bytes = token; | ||||
| uint32_t prob = prob_data >> 2; | uint32_t prob = prob_data >> 2; | ||||
| for (int b = 0; b < extra_bytes; ++b) { | for (int b = 0; b < extra_bytes; ++b) { | ||||
| uint8_t eb; | uint8_t eb; | ||||
| if (!buffer->Decode(&eb)) | if (!buffer->Decode(&eb)) { | ||||
| return false; | return false; | ||||
| } | |||||
| // Shift 8 bits for each extra byte and subtract 2 for the two first | // Shift 8 bits for each extra byte and subtract 2 for the two first | ||||
| // bits. | // bits. | ||||
| prob |= static_cast<uint32_t>(eb) << (8 * (b + 1) - 2); | prob |= static_cast<uint32_t>(eb) << (8 * (b + 1) - 2); | ||||
| } | } | ||||
| probability_table_[i] = prob; | probability_table_[i] = prob; | ||||
| } | } | ||||
| } | } | ||||
| if (!ans_.rans_build_look_up_table(&probability_table_[0], num_symbols_)) | if (!ans_.rans_build_look_up_table(&probability_table_[0], num_symbols_)) { | ||||
| return false; | return false; | ||||
| } | |||||
| return true; | return true; | ||||
| } | } | ||||
| template <int unique_symbols_bit_length_t> | template <int unique_symbols_bit_length_t> | ||||
| bool RAnsSymbolDecoder<unique_symbols_bit_length_t>::StartDecoding( | bool RAnsSymbolDecoder<unique_symbols_bit_length_t>::StartDecoding( | ||||
| DecoderBuffer *buffer) { | DecoderBuffer *buffer) { | ||||
| uint64_t bytes_encoded; | uint64_t bytes_encoded; | ||||
| // Decode the number of bytes encoded by the encoder. | // Decode the number of bytes encoded by the encoder. | ||||
| #ifdef DRACO_BACKWARDS_COMPATIBILITY_SUPPORTED | #ifdef DRACO_BACKWARDS_COMPATIBILITY_SUPPORTED | ||||
| if (buffer->bitstream_version() < DRACO_BITSTREAM_VERSION(2, 0)) { | if (buffer->bitstream_version() < DRACO_BITSTREAM_VERSION(2, 0)) { | ||||
| if (!buffer->Decode(&bytes_encoded)) | if (!buffer->Decode(&bytes_encoded)) { | ||||
| return false; | return false; | ||||
| } | |||||
| } else | } else | ||||
| #endif | #endif | ||||
| { | { | ||||
| if (!DecodeVarint<uint64_t>(&bytes_encoded, buffer)) | if (!DecodeVarint<uint64_t>(&bytes_encoded, buffer)) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| if (bytes_encoded > static_cast<uint64_t>(buffer->remaining_size())) | } | ||||
| if (bytes_encoded > static_cast<uint64_t>(buffer->remaining_size())) { | |||||
| return false; | return false; | ||||
| } | |||||
| const uint8_t *const data_head = | const uint8_t *const data_head = | ||||
| reinterpret_cast<const uint8_t *>(buffer->data_head()); | reinterpret_cast<const uint8_t *>(buffer->data_head()); | ||||
| // Advance the buffer past the rANS data. | // Advance the buffer past the rANS data. | ||||
| buffer->Advance(bytes_encoded); | buffer->Advance(bytes_encoded); | ||||
| if (ans_.read_init(data_head, static_cast<int>(bytes_encoded)) != 0) | if (ans_.read_init(data_head, static_cast<int>(bytes_encoded)) != 0) { | ||||
| return false; | return false; | ||||
| } | |||||
| return true; | return true; | ||||
| } | } | ||||
| template <int unique_symbols_bit_length_t> | template <int unique_symbols_bit_length_t> | ||||
| void RAnsSymbolDecoder<unique_symbols_bit_length_t>::EndDecoding() { | void RAnsSymbolDecoder<unique_symbols_bit_length_t>::EndDecoding() { | ||||
| ans_.read_end(); | ans_.read_end(); | ||||
| } | } | ||||
| } // namespace draco | } // namespace draco | ||||
| #endif // DRACO_COMPRESSION_ENTROPY_RANS_SYMBOL_DECODER_H_ | #endif // DRACO_COMPRESSION_ENTROPY_RANS_SYMBOL_DECODER_H_ | ||||