diff --git a/iamf/cli/parameters_manager.cc b/iamf/cli/parameters_manager.cc index d99bfbc3..de68c7b7 100644 --- a/iamf/cli/parameters_manager.cc +++ b/iamf/cli/parameters_manager.cc @@ -210,4 +210,45 @@ absl::Status ParametersManager::UpdateDemixingState( return absl::OkStatus(); } +// TODO(b/356393945): Refactor to use a template function. +absl::Status ParametersManager::UpdateReconGainState( + DecodedUleb128 audio_element_id, int32_t expected_timestamp) { + const auto recon_gain_states_iter = recon_gain_states_.find(audio_element_id); + if (recon_gain_states_iter == recon_gain_states_.end()) { + // No recon gain parameter definition found for the audio element ID, so + // nothing to update. + return absl::OkStatus(); + } + + // Validate the timestamps before updating. + auto& recon_gain_state = recon_gain_states_iter->second; + + // Using `.at()` here is safe because if the recon gain state exists for the + // `audio_element_id`, an entry in `recon_gain_parameter_blocks_` with the key + // `recon_gain_state.param_definition->parameter_id_` has already been + // created during `Initialize()`. + auto& parameter_block = recon_gain_parameter_blocks_.at( + recon_gain_state.param_definition->parameter_id_); + if (parameter_block == nullptr) { + // No parameter block found for this ID. Do not validate the timestamp + // or update anything else. + return absl::OkStatus(); + } + + if (expected_timestamp != recon_gain_state.next_timestamp) { + return absl::InvalidArgumentError(absl::StrCat( + "Mismatching timestamps for recon gain parameters: (", + recon_gain_state.next_timestamp, " vs ", expected_timestamp, ")")); + } + + // Update the next timestamp for the next frame. + recon_gain_state.next_timestamp = parameter_block->end_timestamp; + + // Clear out the parameter block, which should not be used before a new + // one is added via `AddReconGainParameterBlock()`. + parameter_block = nullptr; + + return absl::OkStatus(); +} + } // namespace iamf_tools diff --git a/iamf/cli/parameters_manager.h b/iamf/cli/parameters_manager.h index 28b4a66a..2ea0377f 100644 --- a/iamf/cli/parameters_manager.h +++ b/iamf/cli/parameters_manager.h @@ -98,6 +98,19 @@ class ParametersManager { absl::Status UpdateDemixingState(DecodedUleb128 audio_element_id, int32_t expected_timestamp); + /*!\brief Updates the state of recon gain parameters for an audio element. + * + * Also validates the timestamp is as expected. + * + * \param audio_element_id Audio Element ID whose corresponding recon gain + * state are to be updated. + * \param expected_timestamp Expected timestamp of the next set of + * recon gain parameter blocks. + * \return `absl::OkStatus()` on success. A specific status on failure. + */ + absl::Status UpdateReconGainState(DecodedUleb128 audio_element_id, + int32_t expected_timestamp); + private: // State used when generating demixing parameters for an audio element. struct DemixingState {