Remove basictypes.h dependency from bitbuffer.
This reduces the types exported in webrtc proper, which can cause other issues (since it doesn't generally use webrtc/base/basictypes.h). basictypes.h integral types (e.g. uint8) have been replaced by the stdint counterparts (e.g. uint8_t), which matches general webrtc style. The include for common.h has been replaced by constructormagic.h, which was the only part used. BUG= R=pthatcher@webrtc.org Review URL: https://webrtc-codereview.appspot.com/50859004 Cr-Commit-Position: refs/heads/master@{#9181}
This commit is contained in:
parent
e2357149eb
commit
9b9f1c4562
@ -18,37 +18,39 @@
|
||||
namespace {
|
||||
|
||||
// Returns the lowest (right-most) |bit_count| bits in |byte|.
|
||||
uint8 LowestBits(uint8 byte, size_t bit_count) {
|
||||
uint8_t LowestBits(uint8_t byte, size_t bit_count) {
|
||||
DCHECK_LE(bit_count, 8u);
|
||||
return byte & ((1 << bit_count) - 1);
|
||||
}
|
||||
|
||||
// Returns the highest (left-most) |bit_count| bits in |byte|, shifted to the
|
||||
// lowest bits (to the right).
|
||||
uint8 HighestBits(uint8 byte, size_t bit_count) {
|
||||
uint8_t HighestBits(uint8_t byte, size_t bit_count) {
|
||||
DCHECK_LE(bit_count, 8u);
|
||||
uint8 shift = 8 - static_cast<uint8>(bit_count);
|
||||
uint8 mask = 0xFF << shift;
|
||||
uint8_t shift = 8 - static_cast<uint8_t>(bit_count);
|
||||
uint8_t mask = 0xFF << shift;
|
||||
return (byte & mask) >> shift;
|
||||
}
|
||||
|
||||
// Returns the highest byte of |val| in a uint8.
|
||||
uint8 HighestByte(uint64 val) {
|
||||
return static_cast<uint8>(val >> 56);
|
||||
// Returns the highest byte of |val| in a uint8_t.
|
||||
uint8_t HighestByte(uint64_t val) {
|
||||
return static_cast<uint8_t>(val >> 56);
|
||||
}
|
||||
|
||||
// Returns the result of writing partial data from |source|, of
|
||||
// |source_bit_count| size in the highest bits, to |target| at
|
||||
// |target_bit_offset| from the highest bit.
|
||||
uint8 WritePartialByte(uint8 source, size_t source_bit_count,
|
||||
uint8 target, size_t target_bit_offset) {
|
||||
uint8_t WritePartialByte(uint8_t source,
|
||||
size_t source_bit_count,
|
||||
uint8_t target,
|
||||
size_t target_bit_offset) {
|
||||
DCHECK(target_bit_offset < 8);
|
||||
DCHECK(source_bit_count < 9);
|
||||
DCHECK(source_bit_count <= (8 - target_bit_offset));
|
||||
// Generate a mask for just the bits we're going to overwrite, so:
|
||||
uint8 mask =
|
||||
uint8_t mask =
|
||||
// The number of bits we want, in the most significant bits...
|
||||
static_cast<uint8>(0xFF << (8 - source_bit_count))
|
||||
static_cast<uint8_t>(0xFF << (8 - source_bit_count))
|
||||
// ...shifted over to the target offset from the most signficant bit.
|
||||
>> target_bit_offset;
|
||||
|
||||
@ -58,7 +60,7 @@ uint8 WritePartialByte(uint8 source, size_t source_bit_count,
|
||||
}
|
||||
|
||||
// Counts the number of bits used in the binary representation of val.
|
||||
size_t CountBits(uint64 val) {
|
||||
size_t CountBits(uint64_t val) {
|
||||
size_t bit_count = 0;
|
||||
while (val != 0) {
|
||||
bit_count++;
|
||||
@ -71,47 +73,47 @@ size_t CountBits(uint64 val) {
|
||||
|
||||
namespace rtc {
|
||||
|
||||
BitBuffer::BitBuffer(const uint8* bytes, size_t byte_count)
|
||||
BitBuffer::BitBuffer(const uint8_t* bytes, size_t byte_count)
|
||||
: bytes_(bytes), byte_count_(byte_count), byte_offset_(), bit_offset_() {
|
||||
DCHECK(static_cast<uint64>(byte_count_) <=
|
||||
std::numeric_limits<uint32>::max());
|
||||
DCHECK(static_cast<uint64_t>(byte_count_) <=
|
||||
std::numeric_limits<uint32_t>::max());
|
||||
}
|
||||
|
||||
uint64 BitBuffer::RemainingBitCount() const {
|
||||
return (static_cast<uint64>(byte_count_) - byte_offset_) * 8 - bit_offset_;
|
||||
uint64_t BitBuffer::RemainingBitCount() const {
|
||||
return (static_cast<uint64_t>(byte_count_) - byte_offset_) * 8 - bit_offset_;
|
||||
}
|
||||
|
||||
bool BitBuffer::ReadUInt8(uint8* val) {
|
||||
uint32 bit_val;
|
||||
if (!ReadBits(&bit_val, sizeof(uint8) * 8)) {
|
||||
bool BitBuffer::ReadUInt8(uint8_t* val) {
|
||||
uint32_t bit_val;
|
||||
if (!ReadBits(&bit_val, sizeof(uint8_t) * 8)) {
|
||||
return false;
|
||||
}
|
||||
DCHECK(bit_val <= std::numeric_limits<uint8>::max());
|
||||
*val = static_cast<uint8>(bit_val);
|
||||
DCHECK(bit_val <= std::numeric_limits<uint8_t>::max());
|
||||
*val = static_cast<uint8_t>(bit_val);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BitBuffer::ReadUInt16(uint16* val) {
|
||||
uint32 bit_val;
|
||||
if (!ReadBits(&bit_val, sizeof(uint16) * 8)) {
|
||||
bool BitBuffer::ReadUInt16(uint16_t* val) {
|
||||
uint32_t bit_val;
|
||||
if (!ReadBits(&bit_val, sizeof(uint16_t) * 8)) {
|
||||
return false;
|
||||
}
|
||||
DCHECK(bit_val <= std::numeric_limits<uint16>::max());
|
||||
*val = static_cast<uint16>(bit_val);
|
||||
DCHECK(bit_val <= std::numeric_limits<uint16_t>::max());
|
||||
*val = static_cast<uint16_t>(bit_val);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BitBuffer::ReadUInt32(uint32* val) {
|
||||
return ReadBits(val, sizeof(uint32) * 8);
|
||||
bool BitBuffer::ReadUInt32(uint32_t* val) {
|
||||
return ReadBits(val, sizeof(uint32_t) * 8);
|
||||
}
|
||||
|
||||
bool BitBuffer::PeekBits(uint32* val, size_t bit_count) {
|
||||
bool BitBuffer::PeekBits(uint32_t* val, size_t bit_count) {
|
||||
if (!val || bit_count > RemainingBitCount() || bit_count > 32) {
|
||||
return false;
|
||||
}
|
||||
const uint8* bytes = bytes_ + byte_offset_;
|
||||
const uint8_t* bytes = bytes_ + byte_offset_;
|
||||
size_t remaining_bits_in_current_byte = 8 - bit_offset_;
|
||||
uint32 bits = LowestBits(*bytes++, remaining_bits_in_current_byte);
|
||||
uint32_t bits = LowestBits(*bytes++, remaining_bits_in_current_byte);
|
||||
// If we're reading fewer bits than what's left in the current byte, just
|
||||
// return the portion of this byte that we need.
|
||||
if (bit_count < remaining_bits_in_current_byte) {
|
||||
@ -135,7 +137,7 @@ bool BitBuffer::PeekBits(uint32* val, size_t bit_count) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BitBuffer::ReadBits(uint32* val, size_t bit_count) {
|
||||
bool BitBuffer::ReadBits(uint32_t* val, size_t bit_count) {
|
||||
return PeekBits(val, bit_count) && ConsumeBits(bit_count);
|
||||
}
|
||||
|
||||
@ -153,7 +155,7 @@ bool BitBuffer::ConsumeBits(size_t bit_count) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BitBuffer::ReadExponentialGolomb(uint32* val) {
|
||||
bool BitBuffer::ReadExponentialGolomb(uint32_t* val) {
|
||||
if (!val) {
|
||||
return false;
|
||||
}
|
||||
@ -164,7 +166,7 @@ bool BitBuffer::ReadExponentialGolomb(uint32* val) {
|
||||
|
||||
// Count the number of leading 0 bits by peeking/consuming them one at a time.
|
||||
size_t zero_bit_count = 0;
|
||||
uint32 peeked_bit;
|
||||
uint32_t peeked_bit;
|
||||
while (PeekBits(&peeked_bit, 1) && peeked_bit == 0) {
|
||||
zero_bit_count++;
|
||||
ConsumeBits(1);
|
||||
@ -174,7 +176,7 @@ bool BitBuffer::ReadExponentialGolomb(uint32* val) {
|
||||
DCHECK(!PeekBits(&peeked_bit, 1) || peeked_bit == 1);
|
||||
|
||||
// The bit count of the value is the number of zeros + 1. Make sure that many
|
||||
// bits fits in a uint32 and that we have enough bits left for it, and then
|
||||
// bits fits in a uint32_t and that we have enough bits left for it, and then
|
||||
// read the value.
|
||||
size_t value_bit_count = zero_bit_count + 1;
|
||||
if (value_bit_count > 32 || !ReadBits(val, value_bit_count)) {
|
||||
@ -203,32 +205,32 @@ bool BitBuffer::Seek(size_t byte_offset, size_t bit_offset) {
|
||||
return true;
|
||||
}
|
||||
|
||||
BitBufferWriter::BitBufferWriter(uint8* bytes, size_t byte_count)
|
||||
: BitBuffer(bytes, byte_count), writable_bytes_(bytes) {
|
||||
BitBufferWriter::BitBufferWriter(uint8_t* bytes, size_t byte_count)
|
||||
: BitBuffer(bytes, byte_count), writable_bytes_(bytes) {
|
||||
}
|
||||
|
||||
bool BitBufferWriter::WriteUInt8(uint8 val) {
|
||||
return WriteBits(val, sizeof(uint8) * 8);
|
||||
bool BitBufferWriter::WriteUInt8(uint8_t val) {
|
||||
return WriteBits(val, sizeof(uint8_t) * 8);
|
||||
}
|
||||
|
||||
bool BitBufferWriter::WriteUInt16(uint16 val) {
|
||||
return WriteBits(val, sizeof(uint16) * 8);
|
||||
bool BitBufferWriter::WriteUInt16(uint16_t val) {
|
||||
return WriteBits(val, sizeof(uint16_t) * 8);
|
||||
}
|
||||
|
||||
bool BitBufferWriter::WriteUInt32(uint32 val) {
|
||||
return WriteBits(val, sizeof(uint32) * 8);
|
||||
bool BitBufferWriter::WriteUInt32(uint32_t val) {
|
||||
return WriteBits(val, sizeof(uint32_t) * 8);
|
||||
}
|
||||
|
||||
bool BitBufferWriter::WriteBits(uint64 val, size_t bit_count) {
|
||||
bool BitBufferWriter::WriteBits(uint64_t val, size_t bit_count) {
|
||||
if (bit_count > RemainingBitCount()) {
|
||||
return false;
|
||||
}
|
||||
size_t total_bits = bit_count;
|
||||
|
||||
// For simplicity, push the bits we want to read from val to the highest bits.
|
||||
val <<= (sizeof(uint64) * 8 - bit_count);
|
||||
val <<= (sizeof(uint64_t) * 8 - bit_count);
|
||||
|
||||
uint8* bytes = writable_bytes_ + byte_offset_;
|
||||
uint8_t* bytes = writable_bytes_ + byte_offset_;
|
||||
|
||||
// The first byte is relatively special; the bit offset to write to may put us
|
||||
// in the middle of the byte, and the total bit count to write may require we
|
||||
@ -264,17 +266,17 @@ bool BitBufferWriter::WriteBits(uint64 val, size_t bit_count) {
|
||||
return ConsumeBits(total_bits);
|
||||
}
|
||||
|
||||
bool BitBufferWriter::WriteExponentialGolomb(uint32 val) {
|
||||
// We don't support reading UINT32_MAX, because it doesn't fit in a uint32
|
||||
bool BitBufferWriter::WriteExponentialGolomb(uint32_t val) {
|
||||
// We don't support reading UINT32_MAX, because it doesn't fit in a uint32_t
|
||||
// when encoded, so don't support writing it either.
|
||||
if (val == std::numeric_limits<uint32>::max()) {
|
||||
if (val == std::numeric_limits<uint32_t>::max()) {
|
||||
return false;
|
||||
}
|
||||
uint64 val_to_encode = static_cast<uint64>(val) + 1;
|
||||
uint64_t val_to_encode = static_cast<uint64_t>(val) + 1;
|
||||
|
||||
// We need to write CountBits(val+1) 0s and then val+1. Since val (as a
|
||||
// uint64) has leading zeros, we can just write the total golomb encoded size
|
||||
// worth of bits, knowing the value will appear last.
|
||||
// uint64_t) has leading zeros, we can just write the total golomb encoded
|
||||
// size worth of bits, knowing the value will appear last.
|
||||
return WriteBits(val_to_encode, CountBits(val_to_encode) * 2 - 1);
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,10 @@
|
||||
#ifndef WEBRTC_BASE_BITBUFFER_H_
|
||||
#define WEBRTC_BASE_BITBUFFER_H_
|
||||
|
||||
#include "webrtc/base/common.h"
|
||||
#include <stdint.h> // For integer types.
|
||||
#include <stddef.h> // For size_t.
|
||||
|
||||
#include "webrtc/base/constructormagic.h"
|
||||
|
||||
namespace rtc {
|
||||
|
||||
@ -24,29 +27,29 @@ namespace rtc {
|
||||
// Byte order is assumed big-endian/network.
|
||||
class BitBuffer {
|
||||
public:
|
||||
BitBuffer(const uint8* bytes, size_t byte_count);
|
||||
BitBuffer(const uint8_t* bytes, size_t byte_count);
|
||||
|
||||
// Gets the current offset, in bytes/bits, from the start of the buffer. The
|
||||
// bit offset is the offset into the current byte, in the range [0,7].
|
||||
void GetCurrentOffset(size_t* out_byte_offset, size_t* out_bit_offset);
|
||||
|
||||
// The remaining bits in the byte buffer.
|
||||
uint64 RemainingBitCount() const;
|
||||
uint64_t RemainingBitCount() const;
|
||||
|
||||
// Reads byte-sized values from the buffer. Returns false if there isn't
|
||||
// enough data left for the specified type.
|
||||
bool ReadUInt8(uint8* val);
|
||||
bool ReadUInt16(uint16* val);
|
||||
bool ReadUInt32(uint32* val);
|
||||
bool ReadUInt8(uint8_t* val);
|
||||
bool ReadUInt16(uint16_t* val);
|
||||
bool ReadUInt32(uint32_t* val);
|
||||
|
||||
// Reads bit-sized values from the buffer. Returns false if there isn't enough
|
||||
// data left for the specified bit count..
|
||||
bool ReadBits(uint32* val, size_t bit_count);
|
||||
bool ReadBits(uint32_t* val, size_t bit_count);
|
||||
|
||||
// Peeks bit-sized values from the buffer. Returns false if there isn't enough
|
||||
// data left for the specified number of bits. Doesn't move the current
|
||||
// offset.
|
||||
bool PeekBits(uint32* val, size_t bit_count);
|
||||
bool PeekBits(uint32_t* val, size_t bit_count);
|
||||
|
||||
// Reads the exponential golomb encoded value at the current offset.
|
||||
// Exponential golomb values are encoded as:
|
||||
@ -55,8 +58,8 @@ class BitBuffer {
|
||||
// To decode, we count the number of leading 0 bits, read that many + 1 bits,
|
||||
// and increment the result by 1.
|
||||
// Returns false if there isn't enough data left for the specified type, or if
|
||||
// the value wouldn't fit in a uint32.
|
||||
bool ReadExponentialGolomb(uint32* val);
|
||||
// the value wouldn't fit in a uint32_t.
|
||||
bool ReadExponentialGolomb(uint32_t* val);
|
||||
|
||||
// Moves current position |byte_count| bytes forward. Returns false if
|
||||
// there aren't enough bytes left in the buffer.
|
||||
@ -70,7 +73,7 @@ class BitBuffer {
|
||||
bool Seek(size_t byte_offset, size_t bit_offset);
|
||||
|
||||
protected:
|
||||
const uint8* const bytes_;
|
||||
const uint8_t* const bytes_;
|
||||
// The total size of |bytes_|.
|
||||
size_t byte_count_;
|
||||
// The current offset, in bytes, from the start of |bytes_|.
|
||||
@ -87,25 +90,25 @@ class BitBuffer {
|
||||
class BitBufferWriter : public BitBuffer {
|
||||
public:
|
||||
// Constructs a bit buffer for the writable buffer of |bytes|.
|
||||
BitBufferWriter(uint8* bytes, size_t byte_count);
|
||||
BitBufferWriter(uint8_t* bytes, size_t byte_count);
|
||||
|
||||
// Writes byte-sized values from the buffer. Returns false if there isn't
|
||||
// enough data left for the specified type.
|
||||
bool WriteUInt8(uint8 val);
|
||||
bool WriteUInt16(uint16 val);
|
||||
bool WriteUInt32(uint32 val);
|
||||
bool WriteUInt8(uint8_t val);
|
||||
bool WriteUInt16(uint16_t val);
|
||||
bool WriteUInt32(uint32_t val);
|
||||
|
||||
// Writes bit-sized values to the buffer. Returns false if there isn't enough
|
||||
// room left for the specified number of bits.
|
||||
bool WriteBits(uint64 val, size_t bit_count);
|
||||
bool WriteBits(uint64_t val, size_t bit_count);
|
||||
|
||||
// Writes the exponential golomb encoded version of the supplied value.
|
||||
// Returns false if there isn't enough room left for the value.
|
||||
bool WriteExponentialGolomb(uint32 val);
|
||||
bool WriteExponentialGolomb(uint32_t val);
|
||||
|
||||
private:
|
||||
// The buffer, as a writable array.
|
||||
uint8* const writable_bytes_;
|
||||
uint8_t* const writable_bytes_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(BitBufferWriter);
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user