|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, 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 | +#include "iamf/obu/element_gain_offset_config.h" |
| 13 | + |
| 14 | +#include <cstdint> |
| 15 | +#include <utility> |
| 16 | +#include <variant> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +#include "absl/log/absl_log.h" |
| 20 | +#include "absl/status/status.h" |
| 21 | +#include "absl/status/statusor.h" |
| 22 | +#include "absl/strings/string_view.h" |
| 23 | +#include "absl/types/span.h" |
| 24 | +#include "iamf/common/q_format_or_floating_point.h" |
| 25 | +#include "iamf/common/read_bit_buffer.h" |
| 26 | +#include "iamf/common/utils/macros.h" |
| 27 | +#include "iamf/common/utils/validation_utils.h" |
| 28 | +#include "iamf/common/write_bit_buffer.h" |
| 29 | + |
| 30 | +namespace iamf_tools { |
| 31 | + |
| 32 | +using ::absl::MakeConstSpan; |
| 33 | +using ::absl::MakeSpan; |
| 34 | + |
| 35 | +namespace { |
| 36 | + |
| 37 | +enum class ElementGainOffsetConfigType : uint8_t { |
| 38 | + kValueType = 0, |
| 39 | + kRangeType = 1 |
| 40 | +}; |
| 41 | + |
| 42 | +} // namespace |
| 43 | + |
| 44 | +absl::Status ElementGainOffsetConfig::ValueType::Write( |
| 45 | + WriteBitBuffer& wb) const { |
| 46 | + RETURN_IF_NOT_OK(wb.WriteUnsignedLiteral( |
| 47 | + static_cast<uint32_t>(ElementGainOffsetConfigType::kValueType), 8)); |
| 48 | + return wb.WriteSigned16(element_gain_offset.GetQ7_8()); |
| 49 | +} |
| 50 | + |
| 51 | +absl::Status ElementGainOffsetConfig::RangeType::Write( |
| 52 | + WriteBitBuffer& wb) const { |
| 53 | + RETURN_IF_NOT_OK(wb.WriteUnsignedLiteral( |
| 54 | + static_cast<uint32_t>(ElementGainOffsetConfigType::kRangeType), 8)); |
| 55 | + RETURN_IF_NOT_OK(wb.WriteSigned16(default_element_gain_offset.GetQ7_8())); |
| 56 | + RETURN_IF_NOT_OK(wb.WriteSigned16(min_element_gain_offset.GetQ7_8())); |
| 57 | + return wb.WriteSigned16(max_element_gain_offset.GetQ7_8()); |
| 58 | +} |
| 59 | + |
| 60 | +absl::Status ElementGainOffsetConfig::ExtensionType::Write( |
| 61 | + WriteBitBuffer& wb) const { |
| 62 | + RETURN_IF_NOT_OK(wb.WriteUnsignedLiteral(element_gain_offset_config_type, 8)); |
| 63 | + RETURN_IF_NOT_OK(wb.WriteUleb128(element_gain_offset_bytes.size())); |
| 64 | + return wb.WriteUint8Span(MakeConstSpan(element_gain_offset_bytes)); |
| 65 | +} |
| 66 | + |
| 67 | +void ElementGainOffsetConfig::ValueType::Print() const { |
| 68 | + ABSL_LOG(INFO) << "value_type: " << element_gain_offset; |
| 69 | +} |
| 70 | + |
| 71 | +void ElementGainOffsetConfig::RangeType::Print() const { |
| 72 | + ABSL_LOG(INFO) << "range_type: "; |
| 73 | + ABSL_LOG(INFO) << " default_element_gain_offset= " |
| 74 | + << default_element_gain_offset; |
| 75 | + ABSL_LOG(INFO) << " min_element_gain_offset= " << min_element_gain_offset; |
| 76 | + ABSL_LOG(INFO) << " max_element_gain_offset= " << max_element_gain_offset; |
| 77 | +} |
| 78 | + |
| 79 | +void ElementGainOffsetConfig::ExtensionType::Print() const { |
| 80 | + ABSL_LOG(INFO) << "element_gain_offset_config_type: " |
| 81 | + << element_gain_offset_config_type; |
| 82 | + ABSL_LOG(INFO) << " element_gain_offset_bytes size: " |
| 83 | + << element_gain_offset_bytes.size(); |
| 84 | + ABSL_LOG(INFO) << " (element_gain_offset_bytes omitted)"; |
| 85 | +} |
| 86 | + |
| 87 | +ElementGainOffsetConfig ElementGainOffsetConfig::MakeValueType( |
| 88 | + QFormatOrFloatingPoint element_gain_offset) { |
| 89 | + // Nothing can fail here. |
| 90 | + return ElementGainOffsetConfig( |
| 91 | + ValueType{.element_gain_offset = element_gain_offset}); |
| 92 | +} |
| 93 | + |
| 94 | +absl::StatusOr<ElementGainOffsetConfig> |
| 95 | +ElementGainOffsetConfig::CreateRangeType( |
| 96 | + QFormatOrFloatingPoint default_element_gain_offset, |
| 97 | + QFormatOrFloatingPoint min_element_gain_offset, |
| 98 | + QFormatOrFloatingPoint max_element_gain_offset) { |
| 99 | + // Check that the range is valid, and the default is within the range. |
| 100 | + RETURN_IF_NOT_OK(ValidateInRange( |
| 101 | + default_element_gain_offset.GetQ7_8(), |
| 102 | + {min_element_gain_offset.GetQ7_8(), max_element_gain_offset.GetQ7_8()}, |
| 103 | + "default_element_gain_offset")); |
| 104 | + |
| 105 | + return ElementGainOffsetConfig(RangeType{ |
| 106 | + .default_element_gain_offset = default_element_gain_offset, |
| 107 | + .min_element_gain_offset = min_element_gain_offset, |
| 108 | + .max_element_gain_offset = max_element_gain_offset, |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +absl::StatusOr<ElementGainOffsetConfig> |
| 113 | +ElementGainOffsetConfig::CreateExtensionType( |
| 114 | + uint8_t element_gain_offset_config_type, |
| 115 | + absl::Span<const uint8_t> element_gain_offset_bytes) { |
| 116 | + if (element_gain_offset_config_type == |
| 117 | + static_cast<uint8_t>(ElementGainOffsetConfigType::kValueType) || |
| 118 | + element_gain_offset_config_type == |
| 119 | + static_cast<uint8_t>(ElementGainOffsetConfigType::kRangeType)) { |
| 120 | + return absl::InvalidArgumentError( |
| 121 | + "Call the specific factory function for value and range types."); |
| 122 | + } |
| 123 | + |
| 124 | + return ElementGainOffsetConfig(ExtensionType{ |
| 125 | + .element_gain_offset_config_type = element_gain_offset_config_type, |
| 126 | + .element_gain_offset_bytes = std::vector<uint8_t>( |
| 127 | + element_gain_offset_bytes.begin(), element_gain_offset_bytes.end()), |
| 128 | + }); |
| 129 | +} |
| 130 | + |
| 131 | +absl::StatusOr<ElementGainOffsetConfig> |
| 132 | +ElementGainOffsetConfig::CreateFromBuffer(ReadBitBuffer& rb) { |
| 133 | + uint8_t element_gain_offset_config_type; |
| 134 | + RETURN_IF_NOT_OK(rb.ReadUnsignedLiteral(8, element_gain_offset_config_type)); |
| 135 | + if (element_gain_offset_config_type == |
| 136 | + static_cast<uint8_t>(ElementGainOffsetConfigType::kValueType)) { |
| 137 | + int16_t element_gain_offset_q78; |
| 138 | + RETURN_IF_NOT_OK(rb.ReadSigned16(element_gain_offset_q78)); |
| 139 | + return MakeValueType( |
| 140 | + QFormatOrFloatingPoint::MakeFromQ7_8(element_gain_offset_q78)); |
| 141 | + } else if (element_gain_offset_config_type == |
| 142 | + static_cast<uint8_t>(ElementGainOffsetConfigType::kRangeType)) { |
| 143 | + int16_t default_element_gain_offset_q78; |
| 144 | + int16_t min_element_gain_offset_q78; |
| 145 | + int16_t max_element_gain_offset_q78; |
| 146 | + RETURN_IF_NOT_OK(rb.ReadSigned16(default_element_gain_offset_q78)); |
| 147 | + RETURN_IF_NOT_OK(rb.ReadSigned16(min_element_gain_offset_q78)); |
| 148 | + RETURN_IF_NOT_OK(rb.ReadSigned16(max_element_gain_offset_q78)); |
| 149 | + return CreateRangeType( |
| 150 | + QFormatOrFloatingPoint::MakeFromQ7_8(default_element_gain_offset_q78), |
| 151 | + QFormatOrFloatingPoint::MakeFromQ7_8(min_element_gain_offset_q78), |
| 152 | + QFormatOrFloatingPoint::MakeFromQ7_8(max_element_gain_offset_q78)); |
| 153 | + } |
| 154 | + |
| 155 | + // Else it is an extension type. |
| 156 | + uint32_t element_gain_offset_size; |
| 157 | + RETURN_IF_NOT_OK(rb.ReadULeb128(element_gain_offset_size)); |
| 158 | + std::vector<uint8_t> element_gain_offset_bytes(element_gain_offset_size); |
| 159 | + RETURN_IF_NOT_OK(rb.ReadUint8Span(MakeSpan(element_gain_offset_bytes))); |
| 160 | + return CreateExtensionType(element_gain_offset_config_type, |
| 161 | + element_gain_offset_bytes); |
| 162 | +} |
| 163 | + |
| 164 | +absl::Status ElementGainOffsetConfig::Write(WriteBitBuffer& wb) const { |
| 165 | + return std::visit( |
| 166 | + [&wb](const auto& data) -> absl::Status { return data.Write(wb); }, |
| 167 | + element_gain_offset_config_data_); |
| 168 | +} |
| 169 | + |
| 170 | +void ElementGainOffsetConfig::Print() const { |
| 171 | + std::visit([](const auto& data) { data.Print(); }, |
| 172 | + element_gain_offset_config_data_); |
| 173 | +} |
| 174 | + |
| 175 | +} // namespace iamf_tools |
0 commit comments