Skip to content

Commit

Permalink
WavSampleProvider: Fail initialization when channel_ids is incons…
Browse files Browse the repository at this point in the history
…istent with the file.

PiperOrigin-RevId: 672592698
  • Loading branch information
jwcullen committed Sep 9, 2024
1 parent 1b96278 commit 166c8de
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions iamf/cli/tests/wav_sample_provider_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ TEST(Initialize, FailsForUnknownLabels) {
wav_sample_provider.Initialize(GetInputWavDir(), audio_elements).ok());
}

TEST(Initialize, FailsForChannelIdTooLarge) {
iamf_tools_cli_proto::UserMetadata user_metadata;
absl::flat_hash_map<DecodedUleb128, AudioElementWithData> audio_elements;
InitializeTestData(kSampleRate, user_metadata, audio_elements);
// Channel IDs are indexed from zero; a stereo wav file must not have a
// channel ID greater than 1.
constexpr auto kFirstChannelIndex = 0;
constexpr uint32_t kChannelIdTooLargeForStereoWavFile = 2;
user_metadata.mutable_audio_frame_metadata(0)->mutable_channel_ids()->Set(
kFirstChannelIndex, kChannelIdTooLargeForStereoWavFile);
WavSampleProvider wav_sample_provider(user_metadata.audio_frame_metadata());

EXPECT_FALSE(
wav_sample_provider.Initialize(GetInputWavDir(), audio_elements).ok());
}

TEST(WavSampleProviderTest, MismatchingChannelIdsAndLabels) {
iamf_tools_cli_proto::UserMetadata user_metadata;
absl::flat_hash_map<DecodedUleb128, AudioElementWithData> audio_elements;
Expand Down
12 changes: 12 additions & 0 deletions iamf/cli/wav_sample_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ absl::Status WavSampleProvider::Initialize(
" vs ", decoder_output_sample_rate, ")"));
}

// To prevent indexing out of bounds after the `WavSampleProvider` is
// created, we ensure all user-specified channel IDs are in range of the
// number of channels in the input file.
for (const uint32_t channel_id : audio_frame_metadata.channel_ids()) {
if (channel_id >= wav_reader->num_channels()) {
return absl::InvalidArgumentError(
absl::StrCat("WAV (", wav_filename.string(),
") has num_channels= ", wav_reader->num_channels(),
". channel_id= ", channel_id, " is out of bounds."));
}
}

wav_readers_.emplace(audio_element_id, std::move(*wav_reader));
}

Expand Down

0 comments on commit 166c8de

Please sign in to comment.