• 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_ARBITRARY_OBU_H_
13 #define OBU_ARBITRARY_OBU_H_
14 
15 #include <cstdint>
16 #include <list>
17 #include <optional>
18 #include <vector>
19 
20 #include "absl/status/status.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 
26 namespace iamf_tools {
27 
28 /*!\brief An arbitrary OBU.
29  *
30  * This class is useful to create edge-cases, invalid streams, or to allow
31  * encoding features that are not otherwise directly supported.
32  *
33  * Usually this class is used in a way that means any side effects of the OBU
34  * are not taken into account.
35  */
36 class ArbitraryObu : public ObuBase {
37  public:
38   /*!\brief A hook describing how the OBU will be put into the bitstream. */
39   enum InsertionHook {
40     kInsertionHookBeforeDescriptors,
41     kInsertionHookAfterDescriptors,
42     kInsertionHookAfterIaSequenceHeader,
43     kInsertionHookAfterCodecConfigs,
44     kInsertionHookAfterAudioElements,
45     kInsertionHookAfterMixPresentations,
46     kInsertionHookBeforeParameterBlocksAtTick,
47     kInsertionHookAfterParameterBlocksAtTick,
48     kInsertionHookAfterAudioFramesAtTick,
49   };
50 
51   /*!\brief Constructor.
52    *
53    * \param obu_type Type of the OBU.
54    * \param header Header of the OBU.
55    * \param payload Payload of the OBU.
56    * \param insertion_hook Hook describing when to insert the OBU.
57    * \param insertion_tick Optional hook to describe the tick to insert the OBU.
58    * \param invalidates_bitstream Whether writing the OBU invalidates the
59    *        bitstream.
60    */
61   ArbitraryObu(ObuType obu_type, const ObuHeader& header,
62                const std::vector<uint8_t>& payload,
63                InsertionHook insertion_hook,
64                const std::optional<int64_t>& insertion_tick = std::nullopt,
65                bool invalidates_bitstream = false)
ObuBase(header,obu_type)66       : ObuBase(header, obu_type),
67         payload_(payload),
68         insertion_hook_(insertion_hook),
69         insertion_tick_(insertion_tick),
70         invalidates_bitstream_(invalidates_bitstream) {}
71 
72   /*!\brief Destructor. */
73   ~ArbitraryObu() override = default;
74 
75   friend bool operator==(const ArbitraryObu& lhs,
76                          const ArbitraryObu& rhs) = default;
77 
78   /*!\brief Writes arbitrary OBUs with the specified hook.
79    *
80    * \param insertion_hook Hook of OBUs to write.
81    * \param arbitrary_obus Arbitrary OBUs to write.
82    * \param wb Write buffer to write to.
83    * \return `absl::OkStatus()` on success. A specific status if
84    *         writing any of the OBUs fail.
85    */
86   static absl::Status WriteObusWithHook(
87       InsertionHook insertion_hook,
88       const std::list<ArbitraryObu>& arbitrary_obus, WriteBitBuffer& wb);
89 
90   /*!\brief Prints logging information about the OBU.*/
91   void PrintObu() const override;
92 
93   const std::vector<uint8_t> payload_;
94 
95   // Metadata.
96   const InsertionHook insertion_hook_;
97   const std::optional<int64_t> insertion_tick_;
98   const bool invalidates_bitstream_;
99 
100  private:
101   /*!\brief Writes the OBU payload to the buffer.
102    *
103    * \param wb Buffer to write to.
104    * \return `absl::OkStatus()` if the OBU is valid. A specific error if
105    * `    invalidates_bitstream_` is true. Other specific statuses on failure.
106    */
107   absl::Status ValidateAndWritePayload(WriteBitBuffer& wb) const override;
108 
109   /*!\brief Reads the OBU payload from the buffer.
110    *
111    * \param payload_size Size of the obu payload in bytes.
112    * \param rb Buffer to read from.
113    * \return `absl::OkStatus()` if the payload is valid. A specific status on
114    *         failure.
115    */
116   absl::Status ReadAndValidatePayloadDerived(int64_t payload_size,
117                                              ReadBitBuffer& rb) override;
118 };
119 
120 }  // namespace iamf_tools
121 
122 #endif  // OBU_ARBITRARY_OBU_H_
123