• 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_AUDIO_FRAME_H_
13 #define OBU_AUDIO_FRAME_H_
14 
15 #include <cstdint>
16 #include <vector>
17 
18 #include "absl/status/status.h"
19 #include "absl/status/statusor.h"
20 #include "absl/types/span.h"
21 #include "iamf/common/read_bit_buffer.h"
22 #include "iamf/common/write_bit_buffer.h"
23 #include "iamf/obu/obu_base.h"
24 #include "iamf/obu/obu_header.h"
25 #include "iamf/obu/types.h"
26 
27 namespace iamf_tools {
28 /*!\brief The Audio Frame OBU.
29  *
30  * The length and meaning of the `audio_frame` field depends on the associated
31  * `CodecConfigObu` and `AudioElementObu`.
32  *
33  * For IAMF-OPUS the field represents an opus packet of RFC6716.
34  * For IAMF-AAC-LC the field represents an raw_data_block() of the AAC spec.
35  * For IAMF-FLAC the field represents an FRAME of the FLAC spec.
36  * For IAMF-LPCM the field represents PCM samples. When more than one byte is
37  * used to represent a PCM sample, the byte order (i.e. its endianness) is
38  * indicated in `sample_format_flags` from the corresponding `CodecConfigObu`.
39  */
40 class AudioFrameObu : public ObuBase {
41  public:
42   /*!\brief Creates an `AudioFrameObu` from a `ReadBitBuffer`.
43    *
44    * This function is designed to be used from the perspective of the decoder.
45    * It will call `ReadAndValidatePayload` in order to read from the buffer;
46    * therefore it can fail.
47    *
48    * \param header `ObuHeader` of the OBU.
49    * \param payload_size Size of the obu payload in bytes.
50    * \param rb `ReadBitBuffer` where the `AudioFrameObu` data is stored.
51    *        Data read from the buffer is consumed.
52    * \return a `AudioFrameObu` on success. A specific status on failure.
53    */
54   static absl::StatusOr<AudioFrameObu> CreateFromBuffer(const ObuHeader& header,
55                                                         int64_t payload_size,
56                                                         ReadBitBuffer& rb);
57 
58   /*!\brief Constructor.
59    *
60    * \param header `ObuHeader` of the OBU.
61    * \param substream_id Substream ID.
62    * \param audio_frame Audio frame.
63    */
64   AudioFrameObu(const ObuHeader& header, DecodedUleb128 substream_id,
65                 absl::Span<const uint8_t> audio_frame);
66 
67   /*!\brief Destructor.*/
68   ~AudioFrameObu() = default;
69 
70   friend bool operator==(const AudioFrameObu& lhs,
71                          const AudioFrameObu& rhs) = default;
72 
73   /*!\brief Prints logging information about the OBU.*/
74   void PrintObu() const override;
75 
76   /*!\brief Gets the substream ID of the OBU.
77    * \return Substream ID.
78    */
GetSubstreamId()79   DecodedUleb128 GetSubstreamId() const { return audio_substream_id_; }
80 
81   std::vector<uint8_t> audio_frame_;
82 
83  private:
84   // This field is not serialized when in the range [0, 17].
85   DecodedUleb128 audio_substream_id_;
86 
87   // Used only by the factory create function.
AudioFrameObu(const ObuHeader & header)88   explicit AudioFrameObu(const ObuHeader& header)
89       : ObuBase(header, header.obu_type),
90         audio_frame_({}),
91         audio_substream_id_(DecodedUleb128()) {}
92   /*!\brief Writes the OBU payload to the buffer.
93    *
94    * \param wb Buffer to write to.
95    * \return `absl::OkStatus()` if the OBU is valid. A specific status on
96    *         failure.
97    */
98   absl::Status ValidateAndWritePayload(WriteBitBuffer& wb) const override;
99 
100   /*!\brief Reads the OBU payload from the buffer.
101    *
102    * \param payload_size Size of the obu payload in bytes.
103    * \param rb Buffer to read from.
104    * \return `absl::OkStatus()` if the payload is valid. A specific status on
105    *         failure.
106    */
107   absl::Status ReadAndValidatePayloadDerived(int64_t payload_size,
108                                              ReadBitBuffer& rb) override;
109 };
110 
111 }  // namespace iamf_tools
112 
113 #endif  // OBU_AUDIO_FRAME_H_
114