Changeset View
Changeset View
Standalone View
Standalone View
extern/draco/draco/src/draco/core/decoder_buffer.h
- This file was moved from extern/draco/dracoenc/src/draco/core/decoder_buffer.h.
| Show All 10 Lines | |||||
| // 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_CORE_DECODER_BUFFER_H_ | #ifndef DRACO_CORE_DECODER_BUFFER_H_ | ||||
| #define DRACO_CORE_DECODER_BUFFER_H_ | #define DRACO_CORE_DECODER_BUFFER_H_ | ||||
| #include <stdint.h> | #include <stdint.h> | ||||
| #include <cstring> | #include <cstring> | ||||
| #include <memory> | #include <memory> | ||||
| #include "draco/draco_features.h" | |||||
| #include "draco/core/macros.h" | #include "draco/core/macros.h" | ||||
| #include "draco/draco_features.h" | |||||
| namespace draco { | namespace draco { | ||||
| // Class is a wrapper around input data used by MeshDecoder. It provides a | // Class is a wrapper around input data used by MeshDecoder. It provides a | ||||
| // basic interface for decoding either typed or variable-bit sized data. | // basic interface for decoding either typed or variable-bit sized data. | ||||
| class DecoderBuffer { | class DecoderBuffer { | ||||
| public: | public: | ||||
| DecoderBuffer(); | DecoderBuffer(); | ||||
| Show All 17 Lines | public: | ||||
| // Ends the decoding of the bit sequence and return to the default | // Ends the decoding of the bit sequence and return to the default | ||||
| // byte-aligned decoding. | // byte-aligned decoding. | ||||
| void EndBitDecoding(); | void EndBitDecoding(); | ||||
| // Decodes up to 32 bits into out_val. Can be called only in between | // Decodes up to 32 bits into out_val. Can be called only in between | ||||
| // StartBitDecoding and EndBitDecoding. Otherwise returns false. | // StartBitDecoding and EndBitDecoding. Otherwise returns false. | ||||
| bool DecodeLeastSignificantBits32(int nbits, uint32_t *out_value) { | bool DecodeLeastSignificantBits32(int nbits, uint32_t *out_value) { | ||||
| if (!bit_decoder_active()) | if (!bit_decoder_active()) { | ||||
| return false; | return false; | ||||
| } | |||||
| bit_decoder_.GetBits(nbits, out_value); | bit_decoder_.GetBits(nbits, out_value); | ||||
| return true; | return true; | ||||
| } | } | ||||
| // Decodes an arbitrary data type. | // Decodes an arbitrary data type. | ||||
| // Can be used only when we are not decoding a bit-sequence. | // Can be used only when we are not decoding a bit-sequence. | ||||
| // Returns false on error. | // Returns false on error. | ||||
| template <typename T> | template <typename T> | ||||
| bool Decode(T *out_val) { | bool Decode(T *out_val) { | ||||
| if (!Peek(out_val)) | if (!Peek(out_val)) { | ||||
| return false; | return false; | ||||
| } | |||||
| pos_ += sizeof(T); | pos_ += sizeof(T); | ||||
| return true; | return true; | ||||
| } | } | ||||
| bool Decode(void *out_data, size_t size_to_decode) { | bool Decode(void *out_data, size_t size_to_decode) { | ||||
| if (data_size_ < static_cast<int64_t>(pos_ + size_to_decode)) | if (data_size_ < static_cast<int64_t>(pos_ + size_to_decode)) { | ||||
| return false; // Buffer overflow. | return false; // Buffer overflow. | ||||
| } | |||||
| memcpy(out_data, (data_ + pos_), size_to_decode); | memcpy(out_data, (data_ + pos_), size_to_decode); | ||||
| pos_ += size_to_decode; | pos_ += size_to_decode; | ||||
| return true; | return true; | ||||
| } | } | ||||
| // Decodes an arbitrary data, but does not advance the reading position. | // Decodes an arbitrary data, but does not advance the reading position. | ||||
| template <typename T> | template <typename T> | ||||
| bool Peek(T *out_val) { | bool Peek(T *out_val) { | ||||
| const size_t size_to_decode = sizeof(T); | const size_t size_to_decode = sizeof(T); | ||||
| if (data_size_ < static_cast<int64_t>(pos_ + size_to_decode)) | if (data_size_ < static_cast<int64_t>(pos_ + size_to_decode)) { | ||||
| return false; // Buffer overflow. | return false; // Buffer overflow. | ||||
| } | |||||
| memcpy(out_val, (data_ + pos_), size_to_decode); | memcpy(out_val, (data_ + pos_), size_to_decode); | ||||
| return true; | return true; | ||||
| } | } | ||||
| bool Peek(void *out_data, size_t size_to_peek) { | bool Peek(void *out_data, size_t size_to_peek) { | ||||
| if (data_size_ < static_cast<int64_t>(pos_ + size_to_peek)) | if (data_size_ < static_cast<int64_t>(pos_ + size_to_peek)) { | ||||
| return false; // Buffer overflow. | return false; // Buffer overflow. | ||||
| } | |||||
| memcpy(out_data, (data_ + pos_), size_to_peek); | memcpy(out_data, (data_ + pos_), size_to_peek); | ||||
| return true; | return true; | ||||
| } | } | ||||
| // Discards #bytes from the input buffer. | // Discards #bytes from the input buffer. | ||||
| void Advance(int64_t bytes) { pos_ += bytes; } | void Advance(int64_t bytes) { pos_ += bytes; } | ||||
| // Moves the parsing position to a specific offset from the beginning of the | // Moves the parsing position to a specific offset from the beginning of the | ||||
| ▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | public: | ||||
| inline void ConsumeBits(int k) { bit_offset_ += k; } | inline void ConsumeBits(int k) { bit_offset_ += k; } | ||||
| // Returns |nbits| bits in |x|. | // Returns |nbits| bits in |x|. | ||||
| inline bool GetBits(int32_t nbits, uint32_t *x) { | inline bool GetBits(int32_t nbits, uint32_t *x) { | ||||
| DRACO_DCHECK_GE(nbits, 0); | DRACO_DCHECK_GE(nbits, 0); | ||||
| DRACO_DCHECK_LE(nbits, 32); | DRACO_DCHECK_LE(nbits, 32); | ||||
| uint32_t value = 0; | uint32_t value = 0; | ||||
| for (int32_t bit = 0; bit < nbits; ++bit) | for (int32_t bit = 0; bit < nbits; ++bit) { | ||||
| value |= GetBit() << bit; | value |= GetBit() << bit; | ||||
| } | |||||
| *x = value; | *x = value; | ||||
| return true; | return true; | ||||
| } | } | ||||
| private: | private: | ||||
| // TODO(fgalligan): Add support for error reporting on range check. | // TODO(fgalligan): Add support for error reporting on range check. | ||||
| // Returns one bit from the bit buffer. | // Returns one bit from the bit buffer. | ||||
| inline int GetBit() { | inline int GetBit() { | ||||
| ▲ Show 20 Lines • Show All 41 Lines • Show Last 20 Lines | |||||