2020-08-14 16:58:22 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef RTC_BASE_BIT_BUFFER_H_
|
|
|
|
#define RTC_BASE_BIT_BUFFER_H_
|
|
|
|
|
|
|
|
#include <stddef.h> // For size_t.
|
|
|
|
#include <stdint.h> // For integer types.
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
|
2022-03-11 16:49:54 +00:00
|
|
|
// A BitBuffer API for write operations. Supports symmetric write APIs to the
|
|
|
|
// reading APIs of BitstreamReader.
|
2020-08-14 16:58:22 +00:00
|
|
|
// Sizes/counts specify bits/bytes, for clarity.
|
|
|
|
// Byte order is assumed big-endian/network.
|
2022-03-11 16:49:54 +00:00
|
|
|
class BitBufferWriter {
|
2020-08-14 16:58:22 +00:00
|
|
|
public:
|
2022-03-11 16:49:54 +00:00
|
|
|
// Constructs a bit buffer for the writable buffer of `bytes`.
|
|
|
|
BitBufferWriter(uint8_t* bytes, size_t byte_count);
|
|
|
|
|
|
|
|
BitBufferWriter(const BitBufferWriter&) = delete;
|
|
|
|
BitBufferWriter& operator=(const BitBufferWriter&) = delete;
|
2020-08-14 16:58:22 +00:00
|
|
|
|
|
|
|
// 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_t RemainingBitCount() const;
|
|
|
|
|
|
|
|
// Reads byte-sized values from the buffer. Returns false if there isn't
|
|
|
|
// enough data left for the specified type.
|
2021-06-25 00:43:10 +00:00
|
|
|
bool ReadUInt8(uint8_t& val);
|
|
|
|
bool ReadUInt16(uint16_t& val);
|
|
|
|
bool ReadUInt32(uint32_t& val);
|
|
|
|
bool ReadUInt8(uint8_t* val) {
|
|
|
|
return val ? ReadUInt8(*val) : false;
|
|
|
|
}
|
|
|
|
bool ReadUInt16(uint16_t* val) {
|
|
|
|
return val ? ReadUInt16(*val) : false;
|
|
|
|
}
|
|
|
|
bool ReadUInt32(uint32_t* val) {
|
|
|
|
return val ? ReadUInt32(*val) : false;
|
|
|
|
}
|
2020-08-14 16:58:22 +00:00
|
|
|
|
|
|
|
// Reads bit-sized values from the buffer. Returns false if there isn't enough
|
|
|
|
// data left for the specified bit count.
|
2021-06-25 00:43:10 +00:00
|
|
|
bool ReadBits(size_t bit_count, uint32_t& val);
|
|
|
|
bool ReadBits(size_t bit_count, uint64_t& val);
|
|
|
|
bool ReadBits(uint32_t* val, size_t bit_count) {
|
|
|
|
return val ? ReadBits(bit_count, *val) : false;
|
|
|
|
}
|
2020-08-14 16:58:22 +00:00
|
|
|
|
|
|
|
// 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.
|
2021-06-25 00:43:10 +00:00
|
|
|
bool PeekBits(size_t bit_count, uint32_t& val);
|
|
|
|
bool PeekBits(size_t bit_count, uint64_t& val);
|
|
|
|
bool PeekBits(uint32_t* val, size_t bit_count) {
|
|
|
|
return val ? PeekBits(bit_count, *val) : false;
|
|
|
|
}
|
2020-08-14 16:58:22 +00:00
|
|
|
|
|
|
|
// Reads value in range [0, num_values - 1].
|
|
|
|
// This encoding is similar to ReadBits(val, Ceil(Log2(num_values)),
|
|
|
|
// but reduces wastage incurred when encoding non-power of two value ranges
|
|
|
|
// Non symmetric values are encoded as:
|
|
|
|
// 1) n = countbits(num_values)
|
|
|
|
// 2) k = (1 << n) - num_values
|
|
|
|
// Value v in range [0, k - 1] is encoded in (n-1) bits.
|
|
|
|
// Value v in range [k, num_values - 1] is encoded as (v+k) in n bits.
|
|
|
|
// https://aomediacodec.github.io/av1-spec/#nsn
|
|
|
|
// Returns false if there isn't enough data left.
|
2021-06-25 00:43:10 +00:00
|
|
|
bool ReadNonSymmetric(uint32_t num_values, uint32_t& val);
|
|
|
|
bool ReadNonSymmetric(uint32_t* val, uint32_t num_values) {
|
|
|
|
return val ? ReadNonSymmetric(num_values, *val) : false;
|
|
|
|
}
|
2020-08-14 16:58:22 +00:00
|
|
|
|
|
|
|
// Reads the exponential golomb encoded value at the current offset.
|
|
|
|
// Exponential golomb values are encoded as:
|
|
|
|
// 1) x = source val + 1
|
|
|
|
// 2) In binary, write [countbits(x) - 1] 0s, then x
|
|
|
|
// 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_t.
|
2021-06-25 00:43:10 +00:00
|
|
|
bool ReadExponentialGolomb(uint32_t& val);
|
|
|
|
bool ReadExponentialGolomb(uint32_t* val) {
|
|
|
|
return val ? ReadExponentialGolomb(*val) : false;
|
|
|
|
}
|
|
|
|
|
2020-08-14 16:58:22 +00:00
|
|
|
// Reads signed exponential golomb values at the current offset. Signed
|
|
|
|
// exponential golomb values are just the unsigned values mapped to the
|
|
|
|
// sequence 0, 1, -1, 2, -2, etc. in order.
|
2021-06-25 00:43:10 +00:00
|
|
|
bool ReadSignedExponentialGolomb(int32_t& val);
|
|
|
|
bool ReadSignedExponentialGolomb(int32_t* val) {
|
|
|
|
return val ? ReadSignedExponentialGolomb(*val) : false;
|
|
|
|
}
|
2020-08-14 16:58:22 +00:00
|
|
|
|
2022-03-11 16:49:54 +00:00
|
|
|
// Moves current position `byte_count` bytes forward. Returns false if
|
2020-08-14 16:58:22 +00:00
|
|
|
// there aren't enough bytes left in the buffer.
|
|
|
|
bool ConsumeBytes(size_t byte_count);
|
2022-03-11 16:49:54 +00:00
|
|
|
// Moves current position `bit_count` bits forward. Returns false if
|
2020-08-14 16:58:22 +00:00
|
|
|
// there aren't enough bits left in the buffer.
|
|
|
|
bool ConsumeBits(size_t bit_count);
|
|
|
|
|
|
|
|
// Sets the current offset to the provied byte/bit offsets. The bit
|
|
|
|
// offset is from the given byte, in the range [0,7].
|
|
|
|
bool Seek(size_t byte_offset, size_t bit_offset);
|
|
|
|
|
|
|
|
// Writes byte-sized values from the buffer. Returns false if there isn't
|
|
|
|
// enough data left for the specified type.
|
|
|
|
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_t val, size_t bit_count);
|
|
|
|
|
|
|
|
// Writes value in range [0, num_values - 1]
|
|
|
|
// See ReadNonSymmetric documentation for the format,
|
|
|
|
// Call SizeNonSymmetricBits to get number of bits needed to store the value.
|
|
|
|
// Returns false if there isn't enough room left for the value.
|
|
|
|
bool WriteNonSymmetric(uint32_t val, uint32_t num_values);
|
2022-03-11 16:49:54 +00:00
|
|
|
// Returns number of bits required to store `val` with NonSymmetric encoding.
|
2020-08-14 16:58:22 +00:00
|
|
|
static size_t SizeNonSymmetricBits(uint32_t val, uint32_t num_values);
|
|
|
|
|
|
|
|
// 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_t val);
|
|
|
|
// Writes the signed exponential golomb version of the supplied value.
|
|
|
|
// Signed exponential golomb values are just the unsigned values mapped to the
|
|
|
|
// sequence 0, 1, -1, 2, -2, etc. in order.
|
|
|
|
bool WriteSignedExponentialGolomb(int32_t val);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// The buffer, as a writable array.
|
|
|
|
uint8_t* const writable_bytes_;
|
2022-03-11 16:49:54 +00:00
|
|
|
// The total size of `bytes_`.
|
|
|
|
const size_t byte_count_;
|
|
|
|
// The current offset, in bytes, from the start of `bytes_`.
|
|
|
|
size_t byte_offset_;
|
|
|
|
// The current offset, in bits, into the current byte.
|
|
|
|
size_t bit_offset_;
|
2020-08-14 16:58:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace rtc
|
|
|
|
|
|
|
|
#endif // RTC_BASE_BIT_BUFFER_H_
|