xref: /aosp_15_r20/external/webrtc/rtc_base/bit_buffer.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2015 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef RTC_BASE_BIT_BUFFER_H_
12*d9f75844SAndroid Build Coastguard Worker #define RTC_BASE_BIT_BUFFER_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <stddef.h>  // For size_t.
15*d9f75844SAndroid Build Coastguard Worker #include <stdint.h>  // For integer types.
16*d9f75844SAndroid Build Coastguard Worker 
17*d9f75844SAndroid Build Coastguard Worker namespace rtc {
18*d9f75844SAndroid Build Coastguard Worker 
19*d9f75844SAndroid Build Coastguard Worker // A BitBuffer API for write operations. Supports symmetric write APIs to the
20*d9f75844SAndroid Build Coastguard Worker // reading APIs of BitstreamReader.
21*d9f75844SAndroid Build Coastguard Worker // Sizes/counts specify bits/bytes, for clarity.
22*d9f75844SAndroid Build Coastguard Worker // Byte order is assumed big-endian/network.
23*d9f75844SAndroid Build Coastguard Worker class BitBufferWriter {
24*d9f75844SAndroid Build Coastguard Worker  public:
25*d9f75844SAndroid Build Coastguard Worker   // Constructs a bit buffer for the writable buffer of `bytes`.
26*d9f75844SAndroid Build Coastguard Worker   BitBufferWriter(uint8_t* bytes, size_t byte_count);
27*d9f75844SAndroid Build Coastguard Worker 
28*d9f75844SAndroid Build Coastguard Worker   BitBufferWriter(const BitBufferWriter&) = delete;
29*d9f75844SAndroid Build Coastguard Worker   BitBufferWriter& operator=(const BitBufferWriter&) = delete;
30*d9f75844SAndroid Build Coastguard Worker 
31*d9f75844SAndroid Build Coastguard Worker   // Gets the current offset, in bytes/bits, from the start of the buffer. The
32*d9f75844SAndroid Build Coastguard Worker   // bit offset is the offset into the current byte, in the range [0,7].
33*d9f75844SAndroid Build Coastguard Worker   void GetCurrentOffset(size_t* out_byte_offset, size_t* out_bit_offset);
34*d9f75844SAndroid Build Coastguard Worker 
35*d9f75844SAndroid Build Coastguard Worker   // The remaining bits in the byte buffer.
36*d9f75844SAndroid Build Coastguard Worker   uint64_t RemainingBitCount() const;
37*d9f75844SAndroid Build Coastguard Worker 
38*d9f75844SAndroid Build Coastguard Worker   // Moves current position `byte_count` bytes forward. Returns false if
39*d9f75844SAndroid Build Coastguard Worker   // there aren't enough bytes left in the buffer.
40*d9f75844SAndroid Build Coastguard Worker   bool ConsumeBytes(size_t byte_count);
41*d9f75844SAndroid Build Coastguard Worker   // Moves current position `bit_count` bits forward. Returns false if
42*d9f75844SAndroid Build Coastguard Worker   // there aren't enough bits left in the buffer.
43*d9f75844SAndroid Build Coastguard Worker   bool ConsumeBits(size_t bit_count);
44*d9f75844SAndroid Build Coastguard Worker 
45*d9f75844SAndroid Build Coastguard Worker   // Sets the current offset to the provied byte/bit offsets. The bit
46*d9f75844SAndroid Build Coastguard Worker   // offset is from the given byte, in the range [0,7].
47*d9f75844SAndroid Build Coastguard Worker   bool Seek(size_t byte_offset, size_t bit_offset);
48*d9f75844SAndroid Build Coastguard Worker 
49*d9f75844SAndroid Build Coastguard Worker   // Writes byte-sized values from the buffer. Returns false if there isn't
50*d9f75844SAndroid Build Coastguard Worker   // enough data left for the specified type.
51*d9f75844SAndroid Build Coastguard Worker   bool WriteUInt8(uint8_t val);
52*d9f75844SAndroid Build Coastguard Worker   bool WriteUInt16(uint16_t val);
53*d9f75844SAndroid Build Coastguard Worker   bool WriteUInt32(uint32_t val);
54*d9f75844SAndroid Build Coastguard Worker 
55*d9f75844SAndroid Build Coastguard Worker   // Writes bit-sized values to the buffer. Returns false if there isn't enough
56*d9f75844SAndroid Build Coastguard Worker   // room left for the specified number of bits.
57*d9f75844SAndroid Build Coastguard Worker   bool WriteBits(uint64_t val, size_t bit_count);
58*d9f75844SAndroid Build Coastguard Worker 
59*d9f75844SAndroid Build Coastguard Worker   // Writes value in range [0, num_values - 1]
60*d9f75844SAndroid Build Coastguard Worker   // See ReadNonSymmetric documentation for the format,
61*d9f75844SAndroid Build Coastguard Worker   // Call SizeNonSymmetricBits to get number of bits needed to store the value.
62*d9f75844SAndroid Build Coastguard Worker   // Returns false if there isn't enough room left for the value.
63*d9f75844SAndroid Build Coastguard Worker   bool WriteNonSymmetric(uint32_t val, uint32_t num_values);
64*d9f75844SAndroid Build Coastguard Worker   // Returns number of bits required to store `val` with NonSymmetric encoding.
65*d9f75844SAndroid Build Coastguard Worker   static size_t SizeNonSymmetricBits(uint32_t val, uint32_t num_values);
66*d9f75844SAndroid Build Coastguard Worker 
67*d9f75844SAndroid Build Coastguard Worker   // Writes the exponential golomb encoded version of the supplied value.
68*d9f75844SAndroid Build Coastguard Worker   // Returns false if there isn't enough room left for the value.
69*d9f75844SAndroid Build Coastguard Worker   bool WriteExponentialGolomb(uint32_t val);
70*d9f75844SAndroid Build Coastguard Worker   // Writes the signed exponential golomb version of the supplied value.
71*d9f75844SAndroid Build Coastguard Worker   // Signed exponential golomb values are just the unsigned values mapped to the
72*d9f75844SAndroid Build Coastguard Worker   // sequence 0, 1, -1, 2, -2, etc. in order.
73*d9f75844SAndroid Build Coastguard Worker   bool WriteSignedExponentialGolomb(int32_t val);
74*d9f75844SAndroid Build Coastguard Worker 
75*d9f75844SAndroid Build Coastguard Worker  private:
76*d9f75844SAndroid Build Coastguard Worker   // The buffer, as a writable array.
77*d9f75844SAndroid Build Coastguard Worker   uint8_t* const writable_bytes_;
78*d9f75844SAndroid Build Coastguard Worker   // The total size of `bytes_`.
79*d9f75844SAndroid Build Coastguard Worker   const size_t byte_count_;
80*d9f75844SAndroid Build Coastguard Worker   // The current offset, in bytes, from the start of `bytes_`.
81*d9f75844SAndroid Build Coastguard Worker   size_t byte_offset_;
82*d9f75844SAndroid Build Coastguard Worker   // The current offset, in bits, into the current byte.
83*d9f75844SAndroid Build Coastguard Worker   size_t bit_offset_;
84*d9f75844SAndroid Build Coastguard Worker };
85*d9f75844SAndroid Build Coastguard Worker 
86*d9f75844SAndroid Build Coastguard Worker }  // namespace rtc
87*d9f75844SAndroid Build Coastguard Worker 
88*d9f75844SAndroid Build Coastguard Worker #endif  // RTC_BASE_BIT_BUFFER_H_
89