xref: /aosp_15_r20/external/webrtc/rtc_base/bit_buffer.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 #ifndef RTC_BASE_BIT_BUFFER_H_
12 #define RTC_BASE_BIT_BUFFER_H_
13 
14 #include <stddef.h>  // For size_t.
15 #include <stdint.h>  // For integer types.
16 
17 namespace rtc {
18 
19 // A BitBuffer API for write operations. Supports symmetric write APIs to the
20 // reading APIs of BitstreamReader.
21 // Sizes/counts specify bits/bytes, for clarity.
22 // Byte order is assumed big-endian/network.
23 class BitBufferWriter {
24  public:
25   // Constructs a bit buffer for the writable buffer of `bytes`.
26   BitBufferWriter(uint8_t* bytes, size_t byte_count);
27 
28   BitBufferWriter(const BitBufferWriter&) = delete;
29   BitBufferWriter& operator=(const BitBufferWriter&) = delete;
30 
31   // 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 
38   // Moves current position `byte_count` bytes forward. Returns false if
39   // there aren't enough bytes left in the buffer.
40   bool ConsumeBytes(size_t byte_count);
41   // Moves current position `bit_count` bits forward. Returns false if
42   // 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 
49   // 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 
59   // 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);
64   // Returns number of bits required to store `val` with NonSymmetric encoding.
65   static size_t SizeNonSymmetricBits(uint32_t val, uint32_t num_values);
66 
67   // 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_;
78   // 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_;
84 };
85 
86 }  // namespace rtc
87 
88 #endif  // RTC_BASE_BIT_BUFFER_H_
89