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_DEMIXING_INFO_PARAMETER_DATA_H_ 13 #define OBU_DEMIXING_INFO_PARAMETER_DATA_H_ 14 15 #include <cstdint> 16 17 #include "absl/status/status.h" 18 #include "iamf/common/read_bit_buffer.h" 19 #include "iamf/common/write_bit_buffer.h" 20 #include "iamf/obu/parameter_data.h" 21 22 namespace iamf_tools { 23 24 struct DownMixingParams { 25 friend bool operator==(const DownMixingParams& lhs, 26 const DownMixingParams& rhs) = default; 27 28 double alpha; 29 double beta; 30 double gamma; 31 double delta; 32 int w_idx_offset; 33 int w_idx_used; 34 double w; 35 bool in_bitstream; 36 }; 37 38 /*!\brief A 3-bit enum for the demixing info parameter. */ 39 struct DemixingInfoParameterData : public ParameterData { 40 enum DMixPMode : uint8_t { 41 kDMixPMode1 = 0, 42 kDMixPMode2 = 1, 43 kDMixPMode3 = 2, 44 kDMixPModeReserved1 = 3, 45 kDMixPMode1_n = 4, 46 kDMixPMode2_n = 5, 47 kDMixPMode3_n = 6, 48 kDMixPModeReserved2 = 7, 49 }; 50 51 enum WIdxUpdateRule { 52 kNormal = 0, 53 kFirstFrame = 1, 54 kDefault = 2, 55 }; 56 57 /*!\brief Constructor. 58 * 59 * \param input_dmixp_mode Input demixing mode. 60 * \param input_reserved Input reserved 5 bits packed in a byte. 61 */ DemixingInfoParameterDataDemixingInfoParameterData62 DemixingInfoParameterData(DMixPMode input_dmixp_mode, uint8_t input_reserved) 63 : ParameterData(), 64 dmixp_mode(input_dmixp_mode), 65 reserved(input_reserved) {} 66 DemixingInfoParameterData() = default; 67 68 /*!\brief Overridden destructor. 69 */ 70 ~DemixingInfoParameterData() override = default; 71 72 /*!\brief Fill in the input `DownMixingParams` based on the `DMixPMode`. 73 * 74 * \param dmixp_mode Input demixing mode. 75 * \param previous_w_idx Used to determine the value of `w`. Must be in the 76 * range [0, 10]. Pass in `default_w` when `w_idx_update_rule == 77 * kDefault`. 78 * \param w_idx_update_rule Rule to update `w_idx`. According to the Spec, 79 * there are two special rules: when the frame index == 0 and when 80 * the `default_w` should be used. 81 * \param down_mixing_params Output demixing parameters. 82 * \return `absl::OkStatus()` if successful. `absl::InvalidArgumentError()` if 83 * the `dmixp_mode` is unknown or `w_idx` is out of range. 84 */ 85 static absl::Status DMixPModeToDownMixingParams( 86 DMixPMode dmixp_mode, int previous_w_idx, 87 WIdxUpdateRule w_idx_update_rule, DownMixingParams& down_mixing_params); 88 89 bool friend operator==(const DemixingInfoParameterData& lhs, 90 const DemixingInfoParameterData& rhs) = default; 91 92 /*!\brief Reads and validates a `DemixingInfoParameterData` from a buffer. 93 * 94 * \param rb Buffer to read from. 95 * \return `absl::OkStatus()` if successful. A specific status on failure. 96 */ 97 absl::Status ReadAndValidate(ReadBitBuffer& rb) override; 98 99 /*!\brief Validates and writes to a buffer. 100 * 101 * \param wb Buffer to write to. 102 * \return `absl::OkStatus()` if successful. A specific status on failure. 103 */ 104 absl::Status Write(WriteBitBuffer& wb) const override; 105 106 /*!\brief Prints the demixing info parameter data. 107 */ 108 void Print() const override; 109 110 DMixPMode dmixp_mode; // 3 bits 111 uint8_t reserved; // 5 bits 112 }; 113 114 struct DefaultDemixingInfoParameterData : public DemixingInfoParameterData { 115 /*!\brief Constructor. 116 * 117 * \param input_dmixp_mode Input demixing mode. 118 * \param input_reserved Input reserved 5 bits packed in a byte. 119 * \param input_default_w Input default weight value. 120 * \param input_reserved_for_future_use Input reserved bits for future 121 * use (4 bits packed in a byte). 122 */ DefaultDemixingInfoParameterDataDefaultDemixingInfoParameterData123 DefaultDemixingInfoParameterData(DMixPMode input_dmixp_mode, 124 uint8_t input_reserved, 125 uint8_t input_default_w, 126 uint8_t input_reserved_for_future_use) 127 : DemixingInfoParameterData(input_dmixp_mode, input_reserved), 128 default_w(input_default_w), 129 reserved_for_future_use(input_reserved_for_future_use) {} 130 DefaultDemixingInfoParameterData() = default; 131 132 /*!\brief Overridden destructor.*/ 133 ~DefaultDemixingInfoParameterData() override = default; 134 135 /*!\brief Reads and validates a `DefaultDemixingInfoParameterData`. 136 * 137 * \param rb Buffer to read from. 138 * \return `absl::OkStatus()` if successful. A specific status on failure. 139 */ 140 absl::Status ReadAndValidate(ReadBitBuffer& rb) override; 141 142 /*!\brief Validates and writes to a buffer. 143 * 144 * \param wb Buffer to write to. 145 * \return `absl::OkStatus()` if successful. A specific status on failure. 146 */ 147 absl::Status Write(WriteBitBuffer& wb) const override; 148 149 /*!\brief Prints the default demixing info parameter data. 150 */ 151 void Print() const override; 152 153 bool friend operator==(const DefaultDemixingInfoParameterData& lhs, 154 const DefaultDemixingInfoParameterData& rhs) = default; 155 156 uint8_t default_w; // 4 bits. 157 uint8_t reserved_for_future_use; // 4 bits. 158 }; 159 160 } // namespace iamf_tools 161 162 #endif // OBU_DEMIXING_INFO_PARAMETER_DATA_H_ 163