Skip to content

Commit

Permalink
Refactors common parts of various functions that add param definitions
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 672955061
  • Loading branch information
yero authored and jwcullen committed Sep 10, 2024
1 parent 00bfa6e commit 95bf210
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 85 deletions.
52 changes: 15 additions & 37 deletions iamf/cli/proto_to_obu/tests/parameter_block_generator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <cstdint>
#include <list>
#include <memory>
#include <utility>
#include <variant>
#include <vector>

Expand Down Expand Up @@ -146,19 +145,19 @@ void ValidateParameterBlocksCommon(
}

TEST(ParameterBlockGeneratorTest, GenerateTwoDemixingParameterBlocks) {
absl::flat_hash_map<uint32_t, PerIdParameterMetadata>
absl::flat_hash_map<DecodedUleb128, PerIdParameterMetadata>
parameter_id_to_metadata;
iamf_tools_cli_proto::UserMetadata user_metadata;
ConfigureDemixingParameterBlocks(user_metadata);

// Initialize pre-requisite OBUs.
absl::flat_hash_map<uint32_t, CodecConfigObu> codec_config_obus;
absl::flat_hash_map<uint32_t, AudioElementWithData> audio_elements;
absl::flat_hash_map<DecodedUleb128, CodecConfigObu> codec_config_obus;
absl::flat_hash_map<DecodedUleb128, AudioElementWithData> 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<uint32_t, const ParamDefinition*> param_definitions;
absl::flat_hash_map<DecodedUleb128, const ParamDefinition*> param_definitions;
AddDemixingParamDefinition(kParameterId, kParameterRate, kDuration,
audio_elements.begin()->second.obu,
&param_definitions);
Expand Down Expand Up @@ -380,28 +379,6 @@ void ConfigureReconGainParameterBlocks(
user_metadata.add_parameter_block_metadata()));
}

void AddReconGainParamDefinition(
AudioElementObu& audio_element_obu,
absl::flat_hash_map<DecodedUleb128, const ParamDefinition*>&
param_definitions) {
auto param_definition = std::make_unique<ReconGainParamDefinition>(
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 = {
Expand Down Expand Up @@ -450,24 +427,25 @@ IdLabeledFrameMap PrepareIdLabeledFrameMap() {
}

TEST(ParameterBlockGeneratorTest, GenerateReconGainParameterBlocks) {
absl::flat_hash_map<uint32_t, PerIdParameterMetadata>
absl::flat_hash_map<DecodedUleb128, PerIdParameterMetadata>
parameter_id_to_metadata;
iamf_tools_cli_proto::UserMetadata user_metadata;
ConfigureReconGainParameterBlocks(user_metadata);

// Initialize pre-requisite OBUs.
absl::flat_hash_map<uint32_t, CodecConfigObu> codec_config_obus;
absl::flat_hash_map<uint32_t, AudioElementWithData> audio_elements;
absl::flat_hash_map<DecodedUleb128, CodecConfigObu> codec_config_obus;
absl::flat_hash_map<DecodedUleb128, AudioElementWithData> audio_elements;
InitializePrerequisiteObus(/*substream_ids=*/{0, 1, 2, 3}, codec_config_obus,
audio_elements);

// Extra data needed to compute recon gain.
PrepareAudioElementWithDataForReconGain(audio_elements.begin()->second);

// Add a recon gain parameter definition inside the Audio Element OBU.
absl::flat_hash_map<uint32_t, const ParamDefinition*> param_definitions;
AddReconGainParamDefinition(audio_elements.begin()->second.obu,
param_definitions);
absl::flat_hash_map<DecodedUleb128, const ParamDefinition*> param_definitions;
AddReconGainParamDefinition(kParameterId, kParameterRate, kDuration,
audio_elements.begin()->second.obu,
&param_definitions);

// Construct and initialize.
ParameterBlockGenerator generator(kOverrideComputedReconGains,
Expand Down Expand Up @@ -509,19 +487,19 @@ TEST(ParameterBlockGeneratorTest, GenerateReconGainParameterBlocks) {

TEST(Initialize, FailsWhenThereAreStrayParameterBlocks) {
iamf_tools_cli_proto::UserMetadata user_metadata;
absl::flat_hash_map<uint32_t, PerIdParameterMetadata>
absl::flat_hash_map<DecodedUleb128, PerIdParameterMetadata>
parameter_id_to_metadata;
// Initialize pre-requisite OBUs.
ConfigureDemixingParameterBlocks(user_metadata);
absl::flat_hash_map<uint32_t, CodecConfigObu> codec_config_obus;
absl::flat_hash_map<uint32_t, AudioElementWithData> audio_elements;
absl::flat_hash_map<DecodedUleb128, CodecConfigObu> codec_config_obus;
absl::flat_hash_map<DecodedUleb128, AudioElementWithData> 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<uint32_t, const ParamDefinition*>
const absl::flat_hash_map<DecodedUleb128, const ParamDefinition*>
empty_param_definitions;
EXPECT_THAT(generator.Initialize(audio_elements, empty_param_definitions),
IsOk());
Expand Down
75 changes: 56 additions & 19 deletions iamf/cli/tests/cli_test_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParamDefinition> param_definition,
absl::flat_hash_map<DecodedUleb128, const ParamDefinition*>*
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(
Expand Down Expand Up @@ -231,11 +268,8 @@ void AddParamDefinitionWithMode0AndOneSubblock(
absl::flat_hash_map<DecodedUleb128, std::unique_ptr<ParamDefinition>>&
param_definitions) {
auto param_definition = std::make_unique<ParamDefinition>();
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));
}

Expand All @@ -245,27 +279,30 @@ void AddDemixingParamDefinition(
absl::flat_hash_map<DecodedUleb128, const ParamDefinition*>*
demixing_param_definitions) {
auto param_definition = std::make_unique<DemixingParamDefinition>();
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<DecodedUleb128, const ParamDefinition*>*
recon_gain_param_definitions) {
auto param_definition = std::make_unique<ReconGainParamDefinition>(
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,
Expand Down
16 changes: 16 additions & 0 deletions iamf/cli/tests/cli_test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ void AddDemixingParamDefinition(
absl::flat_hash_map<DecodedUleb128, const ParamDefinition*>*
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<DecodedUleb128, const ParamDefinition*>*
param_definitions);

/*!\brief Calls `CreateWavReader` and unwraps the `StatusOr`.
*
* \param filename Filename to forward to `CreateWavReader`.
Expand Down
39 changes: 10 additions & 29 deletions iamf/cli/tests/parameters_manager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#include <cstdint>
#include <memory>
#include <utility>
#include <vector>

#include "absl/container/flat_hash_map.h"
Expand Down Expand Up @@ -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<ReconGainParamDefinition>(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() {
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -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<ParametersManager>(audio_elements_);
ASSERT_THAT(parameters_manager_->Initialize(), IsOk());

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 95bf210

Please sign in to comment.