Update BitBuffer methods to style guide
Specifically, use reference instead of pointer for out parameter and place the out parameter last, for the following methods ReadUInt8 ReadUInt16 ReadUInt32 ReadBits PeekBits ReadNonSymmetric ReadSignedExponentialGolomb ReadExponentialGolomb Bug: webrtc:11933 Change-Id: I3f1efe3e29155985277b0cd18700ddea25fe7914 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/218504 Reviewed-by: Harald Alvestrand <hta@webrtc.org> Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> Commit-Queue: Björn Terelius <terelius@webrtc.org> Cr-Commit-Position: refs/heads/master@{#34037}
This commit is contained in:
committed by
WebRTC LUCI CQ
parent
7c286c062c
commit
a77e16ca2c
@ -83,36 +83,36 @@ uint64_t BitBuffer::RemainingBitCount() const {
|
||||
return (static_cast<uint64_t>(byte_count_) - byte_offset_) * 8 - bit_offset_;
|
||||
}
|
||||
|
||||
bool BitBuffer::ReadUInt8(uint8_t* val) {
|
||||
bool BitBuffer::ReadUInt8(uint8_t& val) {
|
||||
uint32_t bit_val;
|
||||
if (!ReadBits(&bit_val, sizeof(uint8_t) * 8)) {
|
||||
if (!ReadBits(sizeof(uint8_t) * 8, bit_val)) {
|
||||
return false;
|
||||
}
|
||||
RTC_DCHECK(bit_val <= std::numeric_limits<uint8_t>::max());
|
||||
*val = static_cast<uint8_t>(bit_val);
|
||||
val = static_cast<uint8_t>(bit_val);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BitBuffer::ReadUInt16(uint16_t* val) {
|
||||
bool BitBuffer::ReadUInt16(uint16_t& val) {
|
||||
uint32_t bit_val;
|
||||
if (!ReadBits(&bit_val, sizeof(uint16_t) * 8)) {
|
||||
if (!ReadBits(sizeof(uint16_t) * 8, bit_val)) {
|
||||
return false;
|
||||
}
|
||||
RTC_DCHECK(bit_val <= std::numeric_limits<uint16_t>::max());
|
||||
*val = static_cast<uint16_t>(bit_val);
|
||||
val = static_cast<uint16_t>(bit_val);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BitBuffer::ReadUInt32(uint32_t* val) {
|
||||
return ReadBits(val, sizeof(uint32_t) * 8);
|
||||
bool BitBuffer::ReadUInt32(uint32_t& val) {
|
||||
return ReadBits(sizeof(uint32_t) * 8, val);
|
||||
}
|
||||
|
||||
bool BitBuffer::PeekBits(uint32_t* val, size_t bit_count) {
|
||||
bool BitBuffer::PeekBits(size_t bit_count, uint32_t& val) {
|
||||
// TODO(nisse): Could allow bit_count == 0 and always return success. But
|
||||
// current code reads one byte beyond end of buffer in the case that
|
||||
// RemainingBitCount() == 0 and bit_count == 0.
|
||||
RTC_DCHECK(bit_count > 0);
|
||||
if (!val || bit_count > RemainingBitCount() || bit_count > 32) {
|
||||
if (bit_count > RemainingBitCount() || bit_count > 32) {
|
||||
return false;
|
||||
}
|
||||
const uint8_t* bytes = bytes_ + byte_offset_;
|
||||
@ -121,7 +121,7 @@ bool BitBuffer::PeekBits(uint32_t* val, size_t bit_count) {
|
||||
// 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) {
|
||||
*val = HighestBits(bits, bit_offset_ + bit_count);
|
||||
val = HighestBits(bits, bit_offset_ + bit_count);
|
||||
return true;
|
||||
}
|
||||
// Otherwise, subtract what we've read from the bit count and read as many
|
||||
@ -137,16 +137,16 @@ bool BitBuffer::PeekBits(uint32_t* val, size_t bit_count) {
|
||||
bits <<= bit_count;
|
||||
bits |= HighestBits(*bytes, bit_count);
|
||||
}
|
||||
*val = bits;
|
||||
val = bits;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BitBuffer::PeekBits(uint64_t* val, size_t bit_count) {
|
||||
bool BitBuffer::PeekBits(size_t bit_count, uint64_t& val) {
|
||||
// TODO(nisse): Could allow bit_count == 0 and always return success. But
|
||||
// current code reads one byte beyond end of buffer in the case that
|
||||
// RemainingBitCount() == 0 and bit_count == 0.
|
||||
RTC_DCHECK(bit_count > 0);
|
||||
if (!val || bit_count > RemainingBitCount() || bit_count > 64) {
|
||||
if (bit_count > RemainingBitCount() || bit_count > 64) {
|
||||
return false;
|
||||
}
|
||||
const uint8_t* bytes = bytes_ + byte_offset_;
|
||||
@ -155,7 +155,7 @@ bool BitBuffer::PeekBits(uint64_t* val, size_t bit_count) {
|
||||
// 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) {
|
||||
*val = HighestBits(bits, bit_offset_ + bit_count);
|
||||
val = HighestBits(bits, bit_offset_ + bit_count);
|
||||
return true;
|
||||
}
|
||||
// Otherwise, subtract what we've read from the bit count and read as many
|
||||
@ -171,16 +171,16 @@ bool BitBuffer::PeekBits(uint64_t* val, size_t bit_count) {
|
||||
bits <<= bit_count;
|
||||
bits |= HighestBits(*bytes, bit_count);
|
||||
}
|
||||
*val = bits;
|
||||
val = bits;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BitBuffer::ReadBits(uint32_t* val, size_t bit_count) {
|
||||
return PeekBits(val, bit_count) && ConsumeBits(bit_count);
|
||||
bool BitBuffer::ReadBits(size_t bit_count, uint32_t& val) {
|
||||
return PeekBits(bit_count, val) && ConsumeBits(bit_count);
|
||||
}
|
||||
|
||||
bool BitBuffer::ReadBits(uint64_t* val, size_t bit_count) {
|
||||
return PeekBits(val, bit_count) && ConsumeBits(bit_count);
|
||||
bool BitBuffer::ReadBits(size_t bit_count, uint64_t& val) {
|
||||
return PeekBits(bit_count, val) && ConsumeBits(bit_count);
|
||||
}
|
||||
|
||||
bool BitBuffer::ConsumeBytes(size_t byte_count) {
|
||||
@ -197,39 +197,36 @@ bool BitBuffer::ConsumeBits(size_t bit_count) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BitBuffer::ReadNonSymmetric(uint32_t* val, uint32_t num_values) {
|
||||
bool BitBuffer::ReadNonSymmetric(uint32_t num_values, uint32_t& val) {
|
||||
RTC_DCHECK_GT(num_values, 0);
|
||||
RTC_DCHECK_LE(num_values, uint32_t{1} << 31);
|
||||
if (num_values == 1) {
|
||||
// When there is only one possible value, it requires zero bits to store it.
|
||||
// But ReadBits doesn't support reading zero bits.
|
||||
*val = 0;
|
||||
val = 0;
|
||||
return true;
|
||||
}
|
||||
size_t count_bits = CountBits(num_values);
|
||||
uint32_t num_min_bits_values = (uint32_t{1} << count_bits) - num_values;
|
||||
|
||||
if (!ReadBits(val, count_bits - 1)) {
|
||||
if (!ReadBits(count_bits - 1, val)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (*val < num_min_bits_values) {
|
||||
if (val < num_min_bits_values) {
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t extra_bit;
|
||||
if (!ReadBits(&extra_bit, /*bit_count=*/1)) {
|
||||
if (!ReadBits(/*bit_count=*/1, extra_bit)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*val = (*val << 1) + extra_bit - num_min_bits_values;
|
||||
val = (val << 1) + extra_bit - num_min_bits_values;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BitBuffer::ReadExponentialGolomb(uint32_t* val) {
|
||||
if (!val) {
|
||||
return false;
|
||||
}
|
||||
bool BitBuffer::ReadExponentialGolomb(uint32_t& val) {
|
||||
// Store off the current byte/bit offset, in case we want to restore them due
|
||||
// to a failed parse.
|
||||
size_t original_byte_offset = byte_offset_;
|
||||
@ -238,35 +235,35 @@ bool BitBuffer::ReadExponentialGolomb(uint32_t* val) {
|
||||
// Count the number of leading 0 bits by peeking/consuming them one at a time.
|
||||
size_t zero_bit_count = 0;
|
||||
uint32_t peeked_bit;
|
||||
while (PeekBits(&peeked_bit, 1) && peeked_bit == 0) {
|
||||
while (PeekBits(1, peeked_bit) && peeked_bit == 0) {
|
||||
zero_bit_count++;
|
||||
ConsumeBits(1);
|
||||
}
|
||||
|
||||
// We should either be at the end of the stream, or the next bit should be 1.
|
||||
RTC_DCHECK(!PeekBits(&peeked_bit, 1) || peeked_bit == 1);
|
||||
RTC_DCHECK(!PeekBits(1, peeked_bit) || peeked_bit == 1);
|
||||
|
||||
// The bit count of the value is the number of zeros + 1. Make sure that many
|
||||
// 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)) {
|
||||
if (value_bit_count > 32 || !ReadBits(value_bit_count, val)) {
|
||||
RTC_CHECK(Seek(original_byte_offset, original_bit_offset));
|
||||
return false;
|
||||
}
|
||||
*val -= 1;
|
||||
val -= 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BitBuffer::ReadSignedExponentialGolomb(int32_t* val) {
|
||||
bool BitBuffer::ReadSignedExponentialGolomb(int32_t& val) {
|
||||
uint32_t unsigned_val;
|
||||
if (!ReadExponentialGolomb(&unsigned_val)) {
|
||||
if (!ReadExponentialGolomb(unsigned_val)) {
|
||||
return false;
|
||||
}
|
||||
if ((unsigned_val & 1) == 0) {
|
||||
*val = -static_cast<int32_t>(unsigned_val / 2);
|
||||
val = -static_cast<int32_t>(unsigned_val / 2);
|
||||
} else {
|
||||
*val = (unsigned_val + 1) / 2;
|
||||
val = (unsigned_val + 1) / 2;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user