/* * Copyright 2015 The WebRTC Project Authors. All rights reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree.
*/
// Returns the highest byte of `val` in a uint8_t.
uint8_t HighestByte(uint64_t val) { returnstatic_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_t WritePartialByte(uint8_t source,
size_t source_bit_count,
uint8_t target,
size_t target_bit_offset) {
RTC_DCHECK(target_bit_offset < 8);
RTC_DCHECK(source_bit_count < 9);
RTC_DCHECK(source_bit_count <= (8 - target_bit_offset)); // Generate a mask for just the bits we're going to overwrite, so:
uint8_t mask = // The number of bits we want, in the most significant bits... static_cast<uint8_t>(0xFF << (8 - source_bit_count)) // ...shifted over to the target offset from the most signficant bit.
>> target_bit_offset;
// We want the target, with the bits we'll overwrite masked off, or'ed with // the bits from the source we want. return (target & ~mask) | (source >> target_bit_offset);
}
// For simplicity, push the bits we want to read from val to the highest bits.
val <<= (sizeof(uint64_t) * 8 - bit_count);
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 // save the bits at the end of the byte.
size_t remaining_bits_in_current_byte = 8 - bit_offset_;
size_t bits_in_first_byte =
std::min(bit_count, remaining_bits_in_current_byte);
*bytes = WritePartialByte(HighestByte(val), bits_in_first_byte, *bytes,
bit_offset_); if (bit_count <= remaining_bits_in_current_byte) { // Nothing left to write, so quit early. return ConsumeBits(total_bits);
}
// Subtract what we've written from the bit count, shift it off the value, and // write the remaining full bytes.
val <<= bits_in_first_byte;
bytes++;
bit_count -= bits_in_first_byte; while (bit_count >= 8) {
*bytes++ = HighestByte(val);
val <<= 8;
bit_count -= 8;
}
// Last byte may also be partial, so write the remaining bits from the top of // val. if (bit_count > 0) {
*bytes = WritePartialByte(HighestByte(val), bit_count, *bytes, 0);
}
// All done! Consume the bits we've written. return ConsumeBits(total_bits);
}
bool BitBufferWriter::WriteNonSymmetric(uint32_t val, uint32_t num_values) {
RTC_DCHECK_LT(val, num_values);
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 WriteBits doesn't support writing zero bits. returntrue;
}
size_t count_bits = absl::bit_width(num_values);
uint32_t num_min_bits_values = (uint32_t{1} << count_bits) - num_values;
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_t>::max()) { returnfalse;
}
uint64_t val_to_encode = static_cast<uint64_t>(val) + 1;
// We need to write bit_width(val+1) 0s and then val+1. Since val (as a // 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, absl::bit_width(val_to_encode) * 2 - 1);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.