• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 3-Clause Clear License
5  * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
6  * License was not distributed with this source code in the LICENSE file, you
7  * can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
8  * Alliance for Open Media Patent License 1.0 was not distributed with this
9  * source code in the PATENTS file, you can obtain it at
10  * www.aomedia.org/license/patent.
11  */
12 #ifndef COMMON_UTILS_BIT_BUFFER_UTIL_H_
13 #define COMMON_UTILS_BIT_BUFFER_UTIL_H_
14 
15 #include <cstdint>
16 #include <vector>
17 
18 #include "absl/status/status.h"
19 
20 namespace iamf_tools {
21 
22 /*!\brief The maximum length of an IAMF string in bytes.
23  *
24  * The spec limits the length of a string to 128 bytes including the
25  * null terminator ('\0').
26  */
27 static constexpr int kIamfMaxStringSize = 128;
28 
29 /*!\brief Confirms that `num_bits` can be written to `bit_buffer`.
30  *
31  * \param allow_resizing Whether the buffer can be resized if need be.
32  * \param num_bits Number of bits we'd like to write.
33  * \param bit_offset Bit index representing where we'd like to start writing
34  *        within `bit_buffer`.
35  * \param bit_buffer Buffer to write to.
36  *
37  * \return `absl::OkStatus()` on success. `absl::ResourceExhaustedError()` if
38  *         the `bit_buffer` does not have space to write `num_bites` and
39  * `allow_resizing` is false.
40  */
41 absl::Status CanWriteBits(bool allow_resizing, int num_bits, int64_t bit_offset,
42                           std::vector<uint8_t>& bit_buffer);
43 
44 /*!\brief Confirms that `num_bytes` can be written to `bit_buffer`.
45  *
46  * \param allow_resizing Whether the buffer can be resized if need be.
47  * \param num_bytes Number of bytes we'd like to write.
48  * \param bit_offset Bit index representing where we'd like to start writing
49  *        within `bit_buffer`.
50  * \param bit_buffer Buffer to write to.
51  *
52  * \return `absl::OkStatus()` on success. `absl::ResourceExhaustedError()` if
53  *         the `bit_buffer` does not have space to write `num_bytes` and
54  * `allow_resizing` is false.
55  */
56 absl::Status CanWriteBytes(bool allow_resizing, int num_bytes,
57                            int64_t bit_offset,
58                            std::vector<uint8_t>& bit_buffer);
59 
60 /*!\brief Write `bit` to `bit_buffer` at position `bit_offset`.
61  *
62  * \param bit Bit to write.
63  * \param bit_offset Index within `bit_buffer` where `bit` should be written to.
64  * \param bit_buffer Buffer to write to.
65  *
66  * \return `absl::OkStatus()` on success. `absl::InvalidArgumentError()` if
67  * `bit_offset` is negative.
68  */
69 absl::Status WriteBit(int bit, int64_t& bit_offset,
70                       std::vector<uint8_t>& bit_buffer);
71 
72 }  // namespace iamf_tools
73 
74 #endif  // COMMON_UTILS_BIT_BUFFER_UTIL_H_
75