• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023, 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 OBU_OBU_HEADER_H_
13 #define OBU_OBU_HEADER_H_
14 
15 #include <cstdint>
16 #include <vector>
17 
18 #include "absl/status/status.h"
19 #include "absl/status/statusor.h"
20 #include "iamf/common/leb_generator.h"
21 #include "iamf/common/read_bit_buffer.h"
22 #include "iamf/common/write_bit_buffer.h"
23 #include "iamf/obu/types.h"
24 
25 namespace iamf_tools {
26 
27 /*!\brief A 5-bit enum for the type of OBU. */
28 enum ObuType : uint8_t {
29   kObuIaCodecConfig = 0,
30   kObuIaAudioElement = 1,
31   kObuIaMixPresentation = 2,
32   kObuIaParameterBlock = 3,
33   kObuIaTemporalDelimiter = 4,
34   kObuIaAudioFrame = 5,
35   kObuIaAudioFrameId0 = 6,
36   kObuIaAudioFrameId1 = 7,
37   kObuIaAudioFrameId2 = 8,
38   kObuIaAudioFrameId3 = 9,
39   kObuIaAudioFrameId4 = 10,
40   kObuIaAudioFrameId5 = 11,
41   kObuIaAudioFrameId6 = 12,
42   kObuIaAudioFrameId7 = 13,
43   kObuIaAudioFrameId8 = 14,
44   kObuIaAudioFrameId9 = 15,
45   kObuIaAudioFrameId10 = 16,
46   kObuIaAudioFrameId11 = 17,
47   kObuIaAudioFrameId12 = 18,
48   kObuIaAudioFrameId13 = 19,
49   kObuIaAudioFrameId14 = 20,
50   kObuIaAudioFrameId15 = 21,
51   kObuIaAudioFrameId16 = 22,
52   kObuIaAudioFrameId17 = 23,
53   kObuIaReserved24 = 24,
54   kObuIaReserved25 = 25,
55   kObuIaReserved26 = 26,
56   kObuIaReserved27 = 27,
57   kObuIaReserved28 = 28,
58   kObuIaReserved29 = 29,
59   kObuIaReserved30 = 30,
60   kObuIaSequenceHeader = 31,
61 };
62 
63 struct HeaderMetadata {
64   ObuType obu_type;
65   int64_t total_obu_size;
66 };
67 
68 struct ObuHeader {
69   friend bool operator==(const ObuHeader& lhs, const ObuHeader& rhs) = default;
70 
71   /*!\brief Validates and writes an `ObuHeader`.
72    *
73    * \param payload_serialized_size `payload_serialized_size` of the output OBU.
74    *        The value MUST be able to be cast to `uint32_t` without losing data.
75    * \param wb Buffer to write to.
76    * \return `absl::OkStatus()` if successful. `absl::InvalidArgumentError()`
77    *         if the fields are invalid or inconsistent or if writing the
78    *         `Leb128` representation of `obu_size` fails.
79    *         `absl::InvalidArgumentError()` if fields are set inconsistent with
80    *         the IAMF specification or if the calculated `obu_size_` larger
81    *         than IAMF limitations. Or a specific status if the write fails.
82    */
83   absl::Status ValidateAndWrite(int64_t payload_serialized_size,
84                                 WriteBitBuffer& wb) const;
85 
86   /*!\brief Validates and reads an `ObuHeader`.
87    *
88    * \param rb Buffer to read from.
89 
90    * \param output_payload_serialized_size `output_payload_serialized_size` Size
91    *        of the payload of the OBU.
92    * \return `absl::OkStatus()` if successful. `absl::InvalidArgumentError()` if
93    *         the fields are invalid or set in a manner that is inconsistent with
94    *         the IAMF specification.
95    */
96   absl::Status ReadAndValidate(ReadBitBuffer& rb,
97                                int64_t& output_payload_serialized_size);
98 
99   /*!\brief Prints logging information about an `ObuHeader`.
100    *
101    * \param leb_generator `LebGenerator` to use when calculating `obu_size_`.
102    * \param payload_serialized_size `payload_serialized_size` of the output OBU.
103    *        The value MUST be able to be cast to `uint32_t` without losing data.
104    */
105   void Print(const LebGenerator& leb_generator,
106              int64_t payload_serialized_size) const;
107 
108   /*!\brief Peeks the type and total OBU size from the bitstream.
109    *
110    * This function does not consume any data from the bitstream.
111    *
112    * \param rb Buffer to read from.
113    * \return `HeaderMetadata` containing the OBU type and total OBU size if
114    *         successful. Returns an absl::ResourceExhaustedError if there is not
115    *         enough data to read the obu_type and obu_size. Returns other errors
116    *         if the bitstream is invalid.
117    */
118   static absl::StatusOr<HeaderMetadata> PeekObuTypeAndTotalObuSize(
119       ReadBitBuffer& rb);
120 
121   static bool IsTemporalUnitObuType(ObuType obu_type);
122 
123   ObuType obu_type;
124   // `obu_size` is inserted automatically.
125   bool obu_redundant_copy = false;
126   bool obu_trimming_status_flag = false;
127   bool obu_extension_flag = false;
128   DecodedUleb128 num_samples_to_trim_at_end = 0;
129   DecodedUleb128 num_samples_to_trim_at_start = 0;
130   DecodedUleb128 extension_header_size = 0;
131   std::vector<uint8_t> extension_header_bytes = {};
132 };
133 
134 }  // namespace iamf_tools
135 
136 #endif  // OBU_OBU_HEADER_H_
137