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 13 #ifndef OBU_OBU_BASE_H_ 14 #define OBU_OBU_BASE_H_ 15 16 #include <cstdint> 17 #include <vector> 18 19 #include "absl/status/status.h" 20 #include "iamf/common/read_bit_buffer.h" 21 #include "iamf/common/write_bit_buffer.h" 22 #include "iamf/obu/obu_header.h" 23 24 namespace iamf_tools { 25 26 class ObuBase { 27 public: 28 /*!\brief Constructor. 29 * 30 * \param header `ObuHeader` of the OBU. 31 * \param obu_type `obu_type` of the OBU. 32 */ ObuBase(const ObuHeader & header,ObuType obu_type)33 ObuBase(const ObuHeader& header, ObuType obu_type) : header_(header) { 34 header_.obu_type = obu_type; 35 } 36 37 /*!\brief Constructor. 38 * 39 * \param obu_type `obu_type` of the OBU. 40 */ ObuBase(ObuType obu_type)41 ObuBase(ObuType obu_type) : ObuBase(ObuHeader(), obu_type) {} 42 ObuBase() = default; 43 44 /*!\brief Destructor.*/ 45 virtual ~ObuBase() = 0; 46 47 friend bool operator==(const ObuBase& lhs, const ObuBase& rhs) = default; 48 49 /*!\brief Validates and writes an entire OBU to the buffer. 50 * 51 * \param final_wb Buffer to write to. 52 * \return `absl::OkStatus()` if the OBU is valid. A specific status on 53 * failure. 54 */ 55 absl::Status ValidateAndWriteObu(WriteBitBuffer& final_wb) const; 56 57 /*!\brief Prints logging information about the OBU.*/ 58 virtual void PrintObu() const = 0; 59 60 ObuHeader header_; 61 62 /*!\brief Bytes at the end of the OBU. 63 * 64 * Store any bytes signalled by `obu_size` but not read by 65 * `ReadAndValidatePayloadDerived`. This helps comply with section 3.2 66 * (https://aomediacodec.github.io/iamf/#obu-header-syntax): 67 * 68 * "The obu_size MAY be greater than the size needed to represent the OBU 69 * syntax. Parsers SHOULD ignore bytes past the OBU syntax that they 70 * recognize." 71 * 72 * Store the extra bytes so any components can forward them as needed. 73 */ 74 std::vector<uint8_t> footer_; 75 76 protected: 77 /*!\brief Writes the OBU payload to the buffer. 78 * 79 * \param wb Buffer to write to. 80 * \return `absl::OkStatus()` if the payload is valid. A specific status on 81 * failure. 82 */ 83 virtual absl::Status ValidateAndWritePayload(WriteBitBuffer& wb) const = 0; 84 85 /*!\brief Reads the entire OBU payload from the buffer. 86 * 87 * This includes reading in any extra bytes signalled by `obu_size`, but not 88 * known to the `ReadAndValidatePayloadDerived` implementation. 89 * 90 * \param payload_size Size of the remaining payload to read. 91 * \param rb Buffer to read from. 92 * \return `absl::OkStatus()` if the payload is valid and at least as large 93 * as the claimed size. A specific status on failure. 94 */ 95 absl::Status ReadAndValidatePayload(int64_t payload_size, ReadBitBuffer& rb); 96 97 /*!\brief Prints logging information about the OBU Header. 98 * 99 * \param payload_size Payload size of the header. 100 */ 101 void PrintHeader(int64_t payload_size) const; 102 103 private: 104 /*!\brief Reads the known OBU payload from the buffer. 105 * 106 * Implementations of this function MAY omit reading any bytes not known - 107 * even if their presence is implied by `payload_size`. Implementations are 108 * expected to read an integral number of bytes. 109 * 110 * \param payload_size Size of the obu payload in bytes. Useful to determine 111 * some fields whose presence or size are not directly signalled 112 * \param rb Buffer to read from. 113 * \return `absl::OkStatus()` if the payload is valid. A specific status on 114 * failure. 115 */ 116 virtual absl::Status ReadAndValidatePayloadDerived(int64_t payload_size, 117 ReadBitBuffer& rb) = 0; 118 }; 119 120 } // namespace iamf_tools 121 122 #endif // OBU_OBU_BASE_H_ 123