diff --git a/iamf/cli/proto_to_obu/tests/parameter_block_generator_test.cc b/iamf/cli/proto_to_obu/tests/parameter_block_generator_test.cc index 0138518..a7f76bc 100644 --- a/iamf/cli/proto_to_obu/tests/parameter_block_generator_test.cc +++ b/iamf/cli/proto_to_obu/tests/parameter_block_generator_test.cc @@ -14,7 +14,6 @@ #include #include #include -#include #include #include @@ -146,19 +145,19 @@ void ValidateParameterBlocksCommon( } TEST(ParameterBlockGeneratorTest, GenerateTwoDemixingParameterBlocks) { - absl::flat_hash_map + absl::flat_hash_map parameter_id_to_metadata; iamf_tools_cli_proto::UserMetadata user_metadata; ConfigureDemixingParameterBlocks(user_metadata); // Initialize pre-requisite OBUs. - absl::flat_hash_map codec_config_obus; - absl::flat_hash_map audio_elements; + absl::flat_hash_map codec_config_obus; + absl::flat_hash_map audio_elements; InitializePrerequisiteObus(/*substream_ids=*/{0}, codec_config_obus, audio_elements); // Add a demixing parameter definition inside the Audio Element OBU. - absl::flat_hash_map param_definitions; + absl::flat_hash_map param_definitions; AddDemixingParamDefinition(kParameterId, kParameterRate, kDuration, audio_elements.begin()->second.obu, ¶m_definitions); @@ -380,28 +379,6 @@ void ConfigureReconGainParameterBlocks( user_metadata.add_parameter_block_metadata())); } -void AddReconGainParamDefinition( - AudioElementObu& audio_element_obu, - absl::flat_hash_map& - param_definitions) { - auto param_definition = std::make_unique( - audio_element_obu.GetAudioElementId()); - param_definitions.insert({kParameterId, param_definition.get()}); - - param_definition->parameter_id_ = kParameterId; - param_definition->parameter_rate_ = 48000; - param_definition->param_definition_mode_ = 0; - param_definition->reserved_ = 0; - param_definition->duration_ = 8; - param_definition->constant_subblock_duration_ = 8; - - // Add to the Audio Element OBU. - audio_element_obu.InitializeParams(1); - audio_element_obu.audio_element_params_[0] = AudioElementParam{ - .param_definition_type = ParamDefinition::kParameterDefinitionReconGain, - .param_definition = std::move(param_definition)}; -} - void PrepareAudioElementWithDataForReconGain( AudioElementWithData& audio_element_with_data) { audio_element_with_data.channel_numbers_for_layers = { @@ -450,14 +427,14 @@ IdLabeledFrameMap PrepareIdLabeledFrameMap() { } TEST(ParameterBlockGeneratorTest, GenerateReconGainParameterBlocks) { - absl::flat_hash_map + absl::flat_hash_map parameter_id_to_metadata; iamf_tools_cli_proto::UserMetadata user_metadata; ConfigureReconGainParameterBlocks(user_metadata); // Initialize pre-requisite OBUs. - absl::flat_hash_map codec_config_obus; - absl::flat_hash_map audio_elements; + absl::flat_hash_map codec_config_obus; + absl::flat_hash_map audio_elements; InitializePrerequisiteObus(/*substream_ids=*/{0, 1, 2, 3}, codec_config_obus, audio_elements); @@ -465,9 +442,10 @@ TEST(ParameterBlockGeneratorTest, GenerateReconGainParameterBlocks) { PrepareAudioElementWithDataForReconGain(audio_elements.begin()->second); // Add a recon gain parameter definition inside the Audio Element OBU. - absl::flat_hash_map param_definitions; - AddReconGainParamDefinition(audio_elements.begin()->second.obu, - param_definitions); + absl::flat_hash_map param_definitions; + AddReconGainParamDefinition(kParameterId, kParameterRate, kDuration, + audio_elements.begin()->second.obu, + ¶m_definitions); // Construct and initialize. ParameterBlockGenerator generator(kOverrideComputedReconGains, @@ -509,19 +487,19 @@ TEST(ParameterBlockGeneratorTest, GenerateReconGainParameterBlocks) { TEST(Initialize, FailsWhenThereAreStrayParameterBlocks) { iamf_tools_cli_proto::UserMetadata user_metadata; - absl::flat_hash_map + absl::flat_hash_map parameter_id_to_metadata; // Initialize pre-requisite OBUs. ConfigureDemixingParameterBlocks(user_metadata); - absl::flat_hash_map codec_config_obus; - absl::flat_hash_map audio_elements; + absl::flat_hash_map codec_config_obus; + absl::flat_hash_map audio_elements; InitializePrerequisiteObus(/*substream_ids=*/{0, 1, 2, 3}, codec_config_obus, audio_elements); // Construct and initialize. ParameterBlockGenerator generator(kOverrideComputedReconGains, parameter_id_to_metadata); - const absl::flat_hash_map + const absl::flat_hash_map empty_param_definitions; EXPECT_THAT(generator.Initialize(audio_elements, empty_param_definitions), IsOk()); diff --git a/iamf/cli/tests/cli_test_utils.cc b/iamf/cli/tests/cli_test_utils.cc index 3a56630..233f28c 100644 --- a/iamf/cli/tests/cli_test_utils.cc +++ b/iamf/cli/tests/cli_test_utils.cc @@ -46,6 +46,43 @@ namespace iamf_tools { +namespace { + +void SetParamDefinitionCommonFields(DecodedUleb128 parameter_id, + DecodedUleb128 parameter_rate, + DecodedUleb128 duration, + ParamDefinition* param_definition) { + param_definition->parameter_id_ = parameter_id; + param_definition->parameter_rate_ = parameter_rate; + param_definition->param_definition_mode_ = 0; + param_definition->reserved_ = 0; + param_definition->duration_ = duration; + param_definition->constant_subblock_duration_ = duration; +} + +void AddParamDefinition( + ParamDefinition::ParameterDefinitionType param_definition_type, + DecodedUleb128 parameter_id, DecodedUleb128 parameter_rate, + DecodedUleb128 duration, AudioElementObu& audio_element_obu, + std::unique_ptr param_definition, + absl::flat_hash_map* + param_definitions) { + SetParamDefinitionCommonFields(parameter_id, parameter_rate, duration, + param_definition.get()); + + if (param_definitions != nullptr) { + param_definitions->insert({parameter_id, param_definition.get()}); + } + + // Add to the Audio Element OBU. + audio_element_obu.InitializeParams(audio_element_obu.num_parameters_ + 1); + audio_element_obu.audio_element_params_.back() = + AudioElementParam{.param_definition_type = param_definition_type, + .param_definition = std::move(param_definition)}; +} + +} // namespace + using ::absl_testing::IsOk; void AddLpcmCodecConfigWithIdAndSampleRate( @@ -231,11 +268,8 @@ void AddParamDefinitionWithMode0AndOneSubblock( absl::flat_hash_map>& param_definitions) { auto param_definition = std::make_unique(); - param_definition->parameter_id_ = parameter_id; - param_definition->parameter_rate_ = parameter_rate; - param_definition->param_definition_mode_ = 0; - param_definition->duration_ = duration; - param_definition->constant_subblock_duration_ = duration; + SetParamDefinitionCommonFields(parameter_id, parameter_rate, duration, + param_definition.get()); param_definitions.emplace(parameter_id, std::move(param_definition)); } @@ -245,27 +279,30 @@ void AddDemixingParamDefinition( absl::flat_hash_map* demixing_param_definitions) { auto param_definition = std::make_unique(); - param_definition->parameter_id_ = parameter_id; - param_definition->parameter_rate_ = parameter_rate; - param_definition->param_definition_mode_ = 0; - param_definition->reserved_ = 0; - param_definition->duration_ = duration; - param_definition->constant_subblock_duration_ = duration; + + // Specific fields of demixing param definitions. param_definition->default_demixing_info_parameter_data_.dmixp_mode = DemixingInfoParameterData::kDMixPMode1; param_definition->default_demixing_info_parameter_data_.reserved = 0; param_definition->default_demixing_info_parameter_data_.default_w = 10; param_definition->default_demixing_info_parameter_data_.reserved_default = 0; - if (demixing_param_definitions != nullptr) { - demixing_param_definitions->insert({parameter_id, param_definition.get()}); - } + AddParamDefinition(ParamDefinition::kParameterDefinitionDemixing, + parameter_id, parameter_rate, duration, audio_element_obu, + std::move(param_definition), demixing_param_definitions); +} - // Add to the Audio Element OBU. - audio_element_obu.InitializeParams(audio_element_obu.num_parameters_ + 1); - audio_element_obu.audio_element_params_.back() = AudioElementParam{ - .param_definition_type = ParamDefinition::kParameterDefinitionDemixing, - .param_definition = std::move(param_definition)}; +void AddReconGainParamDefinition( + DecodedUleb128 parameter_id, DecodedUleb128 parameter_rate, + DecodedUleb128 duration, AudioElementObu& audio_element_obu, + absl::flat_hash_map* + recon_gain_param_definitions) { + auto param_definition = std::make_unique( + audio_element_obu.GetAudioElementId()); + + AddParamDefinition(ParamDefinition::kParameterDefinitionReconGain, + parameter_id, parameter_rate, duration, audio_element_obu, + std::move(param_definition), recon_gain_param_definitions); } WavReader CreateWavReaderExpectOk(const std::string& filename, diff --git a/iamf/cli/tests/cli_test_utils.h b/iamf/cli/tests/cli_test_utils.h index dc1d99f..b57fc0c 100644 --- a/iamf/cli/tests/cli_test_utils.h +++ b/iamf/cli/tests/cli_test_utils.h @@ -135,6 +135,22 @@ void AddDemixingParamDefinition( absl::flat_hash_map* param_definitions); +/*!\brief Adds a recon gain parameter definition to an Audio Element OBU. + * + * \param parameter_id `parameter_id` of the `ParamDefinition` to add. + * \param parameter_rate `parameter_rate` of the `ParamDefinition` to add. + * \param duration `duration` and `constant_subblock_duration` of the + * `ParamDefinition` to add. + * \param audio_element_obu Audio Element OBU to add the `ParamDefinition` to. + * \param param_definitions Output pointer to the map to add the + * `ParamDefinition*` to keyed by `parameter_id`. + */ +void AddReconGainParamDefinition( + DecodedUleb128 parameter_id, DecodedUleb128 parameter_rate, + DecodedUleb128 duration, AudioElementObu& audio_element_obu, + absl::flat_hash_map* + param_definitions); + /*!\brief Calls `CreateWavReader` and unwraps the `StatusOr`. * * \param filename Filename to forward to `CreateWavReader`. diff --git a/iamf/cli/tests/parameters_manager_test.cc b/iamf/cli/tests/parameters_manager_test.cc index fba89d5..c7bbdb0 100644 --- a/iamf/cli/tests/parameters_manager_test.cc +++ b/iamf/cli/tests/parameters_manager_test.cc @@ -13,7 +13,6 @@ #include #include -#include #include #include "absl/container/flat_hash_map.h" @@ -101,29 +100,6 @@ absl::Status AddOneReconGainParameterBlock( return status; } -// TODO(b/355436892): Refactor common parts of this and -// `AddDemixingParamDefinition()` into a helper function. -void AddReconGainParamDefinition(DecodedUleb128 parameter_id, - DecodedUleb128 parameter_rate, - DecodedUleb128 duration, - AudioElementObu& audio_element_obu) { - const auto audio_element_id = audio_element_obu.GetAudioElementId(); - auto param_definition = - std::make_unique(audio_element_id); - param_definition->parameter_id_ = parameter_id; - param_definition->parameter_rate_ = parameter_rate; - param_definition->param_definition_mode_ = 0; - param_definition->reserved_ = 0; - param_definition->duration_ = duration; - param_definition->constant_subblock_duration_ = duration; - - // Add to the Audio Element OBU. - audio_element_obu.InitializeParams(audio_element_obu.num_parameters_ + 1); - audio_element_obu.audio_element_params_.back() = AudioElementParam{ - .param_definition_type = ParamDefinition::kParameterDefinitionReconGain, - .param_definition = std::move(param_definition)}; -} - class ParametersManagerTest : public testing::Test { public: ParametersManagerTest() { @@ -172,7 +148,8 @@ TEST_F(ParametersManagerTest, InitializeWithTwoDemixingParametersFails) { TEST_F(ParametersManagerTest, InitializeWithReconGainParameterSucceeds) { AddReconGainParamDefinition(kSecondParameterId, kSampleRate, kDuration, - audio_elements_.at(kAudioElementId).obu); + audio_elements_.at(kAudioElementId).obu, + /*param_definitions=*/nullptr); EXPECT_THAT( AddOneReconGainParameterBlock(*audio_elements_.at(kAudioElementId) .obu.audio_element_params_[0] @@ -215,7 +192,8 @@ TEST_F(ParametersManagerTest, GetDownMixingParametersSucceeds) { TEST_F(ParametersManagerTest, GetReconGainParametersSucceeds) { AddReconGainParamDefinition(kSecondParameterId, kSampleRate, kDuration, - audio_elements_.at(kAudioElementId).obu); + audio_elements_.at(kAudioElementId).obu, + /*param_definitions=*/nullptr); ASSERT_THAT( AddOneReconGainParameterBlock(*audio_elements_.at(kAudioElementId) .obu.audio_element_params_[1] @@ -242,7 +220,8 @@ TEST_F(ParametersManagerTest, GetReconGainParametersSucceeds) { TEST_F(ParametersManagerTest, GetReconGainParametersSucceedsWithNoParameterBlocks) { AddReconGainParamDefinition(kSecondParameterId, kSampleRate, kDuration, - audio_elements_.at(kAudioElementId).obu); + audio_elements_.at(kAudioElementId).obu, + /*param_definitions=*/nullptr); parameters_manager_ = std::make_unique(audio_elements_); ASSERT_THAT(parameters_manager_->Initialize(), IsOk()); @@ -278,7 +257,8 @@ TEST_F(ParametersManagerTest, GetMultipleReconGainParametersSucceeds) { // are multiple recon gain parameter blocks within the same substream, with // consecutive timestamps. AddReconGainParamDefinition(kSecondParameterId, kSampleRate, kDuration, - audio_elements_.at(kAudioElementId).obu); + audio_elements_.at(kAudioElementId).obu, + /*param_definitions=*/nullptr); ASSERT_THAT( AddOneReconGainParameterBlock(*audio_elements_.at(kAudioElementId) .obu.audio_element_params_[1] @@ -335,7 +315,8 @@ TEST_F(ParametersManagerTest, GetMultipleReconGainParametersSucceeds) { TEST_F(ParametersManagerTest, GetMultipleReconGainParametersFailsWithoutUpdatingState) { AddReconGainParamDefinition(kSecondParameterId, kSampleRate, kDuration, - audio_elements_.at(kAudioElementId).obu); + audio_elements_.at(kAudioElementId).obu, + /*param_definitions=*/nullptr); ASSERT_THAT( AddOneReconGainParameterBlock(*audio_elements_.at(kAudioElementId) .obu.audio_element_params_[1]