Changeset View
Changeset View
Standalone View
Standalone View
extern/draco/draco/src/draco/core/encoder_buffer.h
- This file was moved from extern/draco/dracoenc/src/draco/core/encoder_buffer.h.
| Show First 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | public: | ||||
| // End the encoding of the bit sequence and return to the default byte-aligned | // End the encoding of the bit sequence and return to the default byte-aligned | ||||
| // encoding. | // encoding. | ||||
| void EndBitEncoding(); | void EndBitEncoding(); | ||||
| // Encode up to 32 bits into the buffer. Can be called only in between | // Encode up to 32 bits into the buffer. Can be called only in between | ||||
| // StartBitEncoding and EndBitEncoding. Otherwise returns false. | // StartBitEncoding and EndBitEncoding. Otherwise returns false. | ||||
| bool EncodeLeastSignificantBits32(int nbits, uint32_t value) { | bool EncodeLeastSignificantBits32(int nbits, uint32_t value) { | ||||
| if (!bit_encoder_active()) | if (!bit_encoder_active()) { | ||||
| return false; | return false; | ||||
| } | |||||
| bit_encoder_->PutBits(value, nbits); | bit_encoder_->PutBits(value, nbits); | ||||
| return true; | return true; | ||||
| } | } | ||||
| // Encode an arbitrary data type. | // Encode an arbitrary data type. | ||||
| // Can be used only when we are not encoding a bit-sequence. | // Can be used only when we are not encoding a bit-sequence. | ||||
| // Returns false when the value couldn't be encoded. | // Returns false when the value couldn't be encoded. | ||||
| template <typename T> | template <typename T> | ||||
| bool Encode(const T &data) { | bool Encode(const T &data) { | ||||
| if (bit_encoder_active()) | if (bit_encoder_active()) { | ||||
| return false; | return false; | ||||
| } | |||||
| const uint8_t *src_data = reinterpret_cast<const uint8_t *>(&data); | const uint8_t *src_data = reinterpret_cast<const uint8_t *>(&data); | ||||
| buffer_.insert(buffer_.end(), src_data, src_data + sizeof(T)); | buffer_.insert(buffer_.end(), src_data, src_data + sizeof(T)); | ||||
| return true; | return true; | ||||
| } | } | ||||
| bool Encode(const void *data, size_t data_size) { | bool Encode(const void *data, size_t data_size) { | ||||
| if (bit_encoder_active()) | if (bit_encoder_active()) { | ||||
| return false; | return false; | ||||
| } | |||||
| const uint8_t *src_data = reinterpret_cast<const uint8_t *>(data); | const uint8_t *src_data = reinterpret_cast<const uint8_t *>(data); | ||||
| buffer_.insert(buffer_.end(), src_data, src_data + data_size); | buffer_.insert(buffer_.end(), src_data, src_data + data_size); | ||||
| return true; | return true; | ||||
| } | } | ||||
| bool bit_encoder_active() const { return bit_encoder_reserved_bytes_ > 0; } | bool bit_encoder_active() const { return bit_encoder_reserved_bytes_ > 0; } | ||||
| const char *data() const { return buffer_.data(); } | const char *data() const { return buffer_.data(); } | ||||
| size_t size() const { return buffer_.size(); } | size_t size() const { return buffer_.size(); } | ||||
| std::vector<char> *buffer() { return &buffer_; } | std::vector<char> *buffer() { return &buffer_; } | ||||
| private: | private: | ||||
| // Internal helper class to encode bits to a bit buffer. | // Internal helper class to encode bits to a bit buffer. | ||||
| class BitEncoder { | class BitEncoder { | ||||
| public: | public: | ||||
| // |data| is the buffer to write the bits into. | // |data| is the buffer to write the bits into. | ||||
| explicit BitEncoder(char *data) : bit_buffer_(data), bit_offset_(0) {} | explicit BitEncoder(char *data) : bit_buffer_(data), bit_offset_(0) {} | ||||
| // Write |nbits| of |data| into the bit buffer. | // Write |nbits| of |data| into the bit buffer. | ||||
| void PutBits(uint32_t data, int32_t nbits) { | void PutBits(uint32_t data, int32_t nbits) { | ||||
| DRACO_DCHECK_GE(nbits, 0); | DRACO_DCHECK_GE(nbits, 0); | ||||
| DRACO_DCHECK_LE(nbits, 32); | DRACO_DCHECK_LE(nbits, 32); | ||||
| for (int32_t bit = 0; bit < nbits; ++bit) | for (int32_t bit = 0; bit < nbits; ++bit) { | ||||
| PutBit((data >> bit) & 1); | PutBit((data >> bit) & 1); | ||||
| } | } | ||||
| } | |||||
| // Return number of bits encoded so far. | // Return number of bits encoded so far. | ||||
| uint64_t Bits() const { return static_cast<uint64_t>(bit_offset_); } | uint64_t Bits() const { return static_cast<uint64_t>(bit_offset_); } | ||||
| // TODO(fgalligan): Remove this function once we know we do not need the | // TODO(fgalligan): Remove this function once we know we do not need the | ||||
| // old API anymore. | // old API anymore. | ||||
| // This is a function of an old API, that currently does nothing. | // This is a function of an old API, that currently does nothing. | ||||
| void Flush(int /* left_over_bit_value */) {} | void Flush(int /* left_over_bit_value */) {} | ||||
| ▲ Show 20 Lines • Show All 48 Lines • Show Last 20 Lines | |||||