blob: 537555060871045941905f98ecae9f11dadcd101 [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
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/constructor_magic.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018
19namespace rtc {
20
Danil Chapovalov48f95252021-09-16 13:20:48 +020021// A BitBuffer API for write operations. Supports symmetric write APIs to the
22// reading APIs of BitstreamReader.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023// Sizes/counts specify bits/bytes, for clarity.
24// Byte order is assumed big-endian/network.
Danil Chapovalov48f95252021-09-16 13:20:48 +020025class BitBufferWriter {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020026 public:
Danil Chapovalov48f95252021-09-16 13:20:48 +020027 // Constructs a bit buffer for the writable buffer of `bytes`.
28 BitBufferWriter(uint8_t* bytes, size_t byte_count);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029
30 // Gets the current offset, in bytes/bits, from the start of the buffer. The
31 // bit offset is the offset into the current byte, in the range [0,7].
32 void GetCurrentOffset(size_t* out_byte_offset, size_t* out_bit_offset);
33
34 // The remaining bits in the byte buffer.
35 uint64_t RemainingBitCount() const;
36
Artem Titov96e3b992021-07-26 16:03:14 +020037 // Moves current position `byte_count` bytes forward. Returns false if
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020038 // there aren't enough bytes left in the buffer.
39 bool ConsumeBytes(size_t byte_count);
Artem Titov96e3b992021-07-26 16:03:14 +020040 // Moves current position `bit_count` bits forward. Returns false if
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041 // there aren't enough bits left in the buffer.
42 bool ConsumeBits(size_t bit_count);
43
44 // Sets the current offset to the provied byte/bit offsets. The bit
45 // offset is from the given byte, in the range [0,7].
46 bool Seek(size_t byte_offset, size_t bit_offset);
47
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020048 // Writes byte-sized values from the buffer. Returns false if there isn't
49 // enough data left for the specified type.
50 bool WriteUInt8(uint8_t val);
51 bool WriteUInt16(uint16_t val);
52 bool WriteUInt32(uint32_t val);
53
54 // Writes bit-sized values to the buffer. Returns false if there isn't enough
55 // room left for the specified number of bits.
56 bool WriteBits(uint64_t val, size_t bit_count);
57
Danil Chapovalova2b30d82019-07-03 14:41:50 +020058 // Writes value in range [0, num_values - 1]
59 // See ReadNonSymmetric documentation for the format,
60 // Call SizeNonSymmetricBits to get number of bits needed to store the value.
61 // Returns false if there isn't enough room left for the value.
62 bool WriteNonSymmetric(uint32_t val, uint32_t num_values);
Artem Titov96e3b992021-07-26 16:03:14 +020063 // Returns number of bits required to store `val` with NonSymmetric encoding.
Danil Chapovalova2b30d82019-07-03 14:41:50 +020064 static size_t SizeNonSymmetricBits(uint32_t val, uint32_t num_values);
65
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020066 // Writes the exponential golomb encoded version of the supplied value.
67 // Returns false if there isn't enough room left for the value.
68 bool WriteExponentialGolomb(uint32_t val);
69 // Writes the signed exponential golomb version of the supplied value.
70 // Signed exponential golomb values are just the unsigned values mapped to the
71 // sequence 0, 1, -1, 2, -2, etc. in order.
72 bool WriteSignedExponentialGolomb(int32_t val);
73
74 private:
75 // The buffer, as a writable array.
76 uint8_t* const writable_bytes_;
Danil Chapovalov48f95252021-09-16 13:20:48 +020077 // The total size of `bytes_`.
78 const size_t byte_count_;
79 // The current offset, in bytes, from the start of `bytes_`.
80 size_t byte_offset_;
81 // The current offset, in bits, into the current byte.
82 size_t bit_offset_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020083
84 RTC_DISALLOW_COPY_AND_ASSIGN(BitBufferWriter);
85};
86
87} // namespace rtc
Noah Richardsbbf7c862015-04-21 16:30:13 -070088
Steve Anton10542f22019-01-11 09:11:00 -080089#endif // RTC_BASE_BIT_BUFFER_H_