blob: b4991bc724d72a80ab54402079a9a1ffac840122 [file] [log] [blame]
Noah Richardsbbf7c862015-04-21 16:30:13 -07001/*
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_BIT_BUFFER_H_
12#define RTC_BASE_BIT_BUFFER_H_
Noah Richardsbbf7c862015-04-21 16:30:13 -070013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <stddef.h> // For size_t.
Yves Gerey665174f2018-06-19 15:03:05 +020015#include <stdint.h> // For integer types.
Noah Richards9b9f1c42015-05-12 12:20:47 -070016
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017namespace rtc {
18
Danil Chapovalov48f95252021-09-16 13:20:48 +020019// A BitBuffer API for write operations. Supports symmetric write APIs to the
20// reading APIs of BitstreamReader.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020021// Sizes/counts specify bits/bytes, for clarity.
22// Byte order is assumed big-endian/network.
Danil Chapovalov48f95252021-09-16 13:20:48 +020023class BitBufferWriter {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024 public:
Danil Chapovalov48f95252021-09-16 13:20:48 +020025 // Constructs a bit buffer for the writable buffer of `bytes`.
26 BitBufferWriter(uint8_t* bytes, size_t byte_count);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020027
Byoungchan Lee14af7622022-01-12 05:24:58 +090028 BitBufferWriter(const BitBufferWriter&) = delete;
29 BitBufferWriter& operator=(const BitBufferWriter&) = delete;
30
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031 // Gets the current offset, in bytes/bits, from the start of the buffer. The
32 // bit offset is the offset into the current byte, in the range [0,7].
33 void GetCurrentOffset(size_t* out_byte_offset, size_t* out_bit_offset);
34
35 // The remaining bits in the byte buffer.
36 uint64_t RemainingBitCount() const;
37
Artem Titov96e3b992021-07-26 16:03:14 +020038 // Moves current position `byte_count` bytes forward. Returns false if
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020039 // there aren't enough bytes left in the buffer.
40 bool ConsumeBytes(size_t byte_count);
Artem Titov96e3b992021-07-26 16:03:14 +020041 // Moves current position `bit_count` bits forward. Returns false if
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020042 // there aren't enough bits left in the buffer.
43 bool ConsumeBits(size_t bit_count);
44
45 // Sets the current offset to the provied byte/bit offsets. The bit
46 // offset is from the given byte, in the range [0,7].
47 bool Seek(size_t byte_offset, size_t bit_offset);
48
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049 // Writes byte-sized values from the buffer. Returns false if there isn't
50 // enough data left for the specified type.
51 bool WriteUInt8(uint8_t val);
52 bool WriteUInt16(uint16_t val);
53 bool WriteUInt32(uint32_t val);
54
55 // Writes bit-sized values to the buffer. Returns false if there isn't enough
56 // room left for the specified number of bits.
57 bool WriteBits(uint64_t val, size_t bit_count);
58
Danil Chapovalova2b30d82019-07-03 14:41:50 +020059 // Writes value in range [0, num_values - 1]
60 // See ReadNonSymmetric documentation for the format,
61 // Call SizeNonSymmetricBits to get number of bits needed to store the value.
62 // Returns false if there isn't enough room left for the value.
63 bool WriteNonSymmetric(uint32_t val, uint32_t num_values);
Artem Titov96e3b992021-07-26 16:03:14 +020064 // Returns number of bits required to store `val` with NonSymmetric encoding.
Danil Chapovalova2b30d82019-07-03 14:41:50 +020065 static size_t SizeNonSymmetricBits(uint32_t val, uint32_t num_values);
66
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020067 // Writes the exponential golomb encoded version of the supplied value.
68 // Returns false if there isn't enough room left for the value.
69 bool WriteExponentialGolomb(uint32_t val);
70 // Writes the signed exponential golomb version of the supplied value.
71 // Signed exponential golomb values are just the unsigned values mapped to the
72 // sequence 0, 1, -1, 2, -2, etc. in order.
73 bool WriteSignedExponentialGolomb(int32_t val);
74
75 private:
76 // The buffer, as a writable array.
77 uint8_t* const writable_bytes_;
Danil Chapovalov48f95252021-09-16 13:20:48 +020078 // The total size of `bytes_`.
79 const size_t byte_count_;
80 // The current offset, in bytes, from the start of `bytes_`.
81 size_t byte_offset_;
82 // The current offset, in bits, into the current byte.
83 size_t bit_offset_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084};
85
86} // namespace rtc
Noah Richardsbbf7c862015-04-21 16:30:13 -070087
Steve Anton10542f22019-01-11 09:11:00 -080088#endif // RTC_BASE_BIT_BUFFER_H_