Skip to content

Truly independent web browser

LibMedia+LibMediaCodecs: Refuse sample rates outside 3000 to 768000 Hz

Zaggy10242abe68fefd4e
diff --git a/Libraries/LibMedia/Codecs/FLAC.cpp b/Libraries/LibMedia/Codecs/FLAC.cpp
index 815f79b3a7c..e16fc449e6a 100644
--- a/Libraries/LibMedia/Codecs/FLAC.cpp
+++ b/Libraries/LibMedia/Codecs/FLAC.cpp
@@ -9,6 +9,7 @@
#include <LibMedia/Codecs/FLAC.h>
#include <LibMedia/MediaStream.h>
#include <LibMediaCodecs/BitReader.h>
+#include <LibMediaCodecs/Limits.h>
namespace Media::Codecs {
@@ -149,6 +150,8 @@ DecoderErrorOr<FLAC::StreamInfo> FLAC::parse_stream_info(ReadonlyBytes bytes)
stream_info.total_sample_count = reader.read_bits<u64>(36);
if (stream_info.sample_rate == 0 || stream_info.maximum_block_size == 0)
return DecoderError::corrupted("FLAC STREAMINFO declares no sample rate or block size"sv);
+ if (audio_sample_rate_is_outside_limits(stream_info.sample_rate))
+ return DecoderError::format(DecoderErrorCategory::NotImplemented, "FLAC sample rate of {} Hz is not supported", stream_info.sample_rate);
return stream_info;
}
diff --git a/Libraries/LibMedia/Containers/Ogg/Reader.cpp b/Libraries/LibMedia/Containers/Ogg/Reader.cpp
index d76cd74a3f5..986773cf970 100644
--- a/Libraries/LibMedia/Containers/Ogg/Reader.cpp
+++ b/Libraries/LibMedia/Containers/Ogg/Reader.cpp
@@ -8,6 +8,7 @@
#include <AK/Checked.h>
#include <LibMedia/Codecs/FLAC.h>
#include <LibMedia/Containers/Ogg/Reader.h>
+#include <LibMediaCodecs/Limits.h>
namespace Media::Ogg {
@@ -366,6 +367,8 @@ static DecoderErrorOr<StreamIdentification> identify_vorbis(ReadonlyBytes packet
identification.sample_rate = load_little_endian<u32>(packet, 12);
if (identification.channel_count == 0 || identification.sample_rate == 0)
return DecoderError::corrupted("Vorbis identification header is invalid"sv);
+ if (audio_sample_rate_is_outside_limits(identification.sample_rate))
+ return DecoderError::format(DecoderErrorCategory::NotImplemented, "Vorbis sample rate of {} Hz is not supported", identification.sample_rate);
return identification;
}
@@ -393,16 +396,14 @@ static DecoderErrorOr<StreamIdentification> identify_flac(ReadonlyBytes packet)
if (packet.size() < OGG_FLAC_STREAM_INFO_OFFSET + Codecs::FLAC::STREAM_INFO_SIZE || packet[5] != 1
|| !packet.slice(9).starts_with("fLaC"sv.bytes()) || (packet[13] & 0x7F) != 0)
return DecoderError::corrupted("FLAC identification header is invalid"sv);
- auto stream_info = packet.slice(OGG_FLAC_STREAM_INFO_OFFSET, Codecs::FLAC::STREAM_INFO_SIZE);
+ auto stream_info = TRY(Codecs::FLAC::parse_stream_info(packet.slice(OGG_FLAC_STREAM_INFO_OFFSET, Codecs::FLAC::STREAM_INFO_SIZE)));
StreamIdentification identification;
identification.codec_id = CodecID::FLAC;
auto following_header_count = load_big_endian<u16>(packet, 7);
if (following_header_count != 0)
identification.header_packet_count = following_header_count + 1;
- identification.sample_rate = (static_cast<u32>(stream_info[10]) << 12) | (static_cast<u32>(stream_info[11]) << 4) | (stream_info[12] >> 4);
- identification.channel_count = ((stream_info[12] >> 1) & 0x07) + 1;
- if (identification.sample_rate == 0)
- return DecoderError::corrupted("FLAC identification header is invalid"sv);
+ identification.sample_rate = stream_info.sample_rate;
+ identification.channel_count = stream_info.channel_count;
return identification;
}
diff --git a/Libraries/LibMedia/Containers/WAV/Reader.cpp b/Libraries/LibMedia/Containers/WAV/Reader.cpp
index 212d4edef70..9520e803509 100644
--- a/Libraries/LibMedia/Containers/WAV/Reader.cpp
+++ b/Libraries/LibMedia/Containers/WAV/Reader.cpp
@@ -8,6 +8,7 @@
#include <AK/Math.h>
#include <AK/Vector.h>
#include <LibMedia/Containers/WAV/Reader.h>
+#include <LibMediaCodecs/Limits.h>
namespace Media::WAV {
@@ -114,6 +115,8 @@ static DecoderErrorOr<SampleFormat> parse_format_chunk(MediaStreamCursor& cursor
if (channel_count == 0 || sample_rate == 0)
return DecoderError::corrupted("WAVE fmt chunk declares no channels or no sample rate"sv);
+ if (audio_sample_rate_is_outside_limits(sample_rate))
+ return DecoderError::format(DecoderErrorCategory::NotImplemented, "WAVE sample rate of {} Hz is not supported", sample_rate);
if (channel_count > Audio::ChannelMap::capacity())
return DecoderError::format(DecoderErrorCategory::NotImplemented, "WAVE stream has {} channels", channel_count);
diff --git a/Libraries/LibMedia/FFmpeg/FFmpegAudioConverter.cpp b/Libraries/LibMedia/FFmpeg/FFmpegAudioConverter.cpp
index f0b5128b7b4..d5b2595499c 100644
--- a/Libraries/LibMedia/FFmpeg/FFmpegAudioConverter.cpp
+++ b/Libraries/LibMedia/FFmpeg/FFmpegAudioConverter.cpp
@@ -10,6 +10,7 @@
#include <LibMedia/FFmpeg/FFmpegAudioConverter.h>
#include <LibMedia/FFmpeg/FFmpegChannelLayout.h>
#include <LibMediaCodecs/FFmpeg/FFmpegHelpers.h>
+#include <LibMediaCodecs/Limits.h>
extern "C" {
#include <libswresample/swresample.h>
@@ -26,11 +27,15 @@ ErrorOr<NonnullOwnPtr<FFmpegAudioConverter>> FFmpegAudioConverter::try_create()
ErrorOr<void> FFmpegAudioConverter::set_output_sample_specification(Audio::SampleSpecification specification)
{
+ if (audio_sample_rate_is_outside_limits(specification.sample_rate()))
+ return Error::from_string_literal("Audio sample rate is outside the supported range");
return set_sample_specifications(m_input_sample_specification, specification);
}
ErrorOr<void> FFmpegAudioConverter::set_input_sample_specification(Audio::SampleSpecification specification)
{
+ if (audio_sample_rate_is_outside_limits(specification.sample_rate()))
+ return Error::from_string_literal("Audio sample rate is outside the supported range");
return set_sample_specifications(specification, m_output_sample_specification);
}
diff --git a/Libraries/LibMediaCodecs/Codecs/AAC.cpp b/Libraries/LibMediaCodecs/Codecs/AAC.cpp
index 57f2bfbf551..254c45e47d3 100644
--- a/Libraries/LibMediaCodecs/Codecs/AAC.cpp
+++ b/Libraries/LibMediaCodecs/Codecs/AAC.cpp
@@ -10,6 +10,7 @@
#include <AK/MemoryStream.h>
#include <LibMediaCodecs/BitReader.h>
#include <LibMediaCodecs/Codecs/AAC.h>
+#include <LibMediaCodecs/Limits.h>
namespace Media::Codecs {
@@ -167,6 +168,11 @@ DecoderErrorOr<FixedArray<u8>> AAC::create_configuration_record(u32 audio_object
VERIFY(audio_object_type < ESCAPED_AUDIO_OBJECT_TYPE);
+ if (audio_sample_rate_is_outside_limits(sample_rate))
+ return DecoderError::format(DecoderErrorCategory::NotImplemented, "AAC sample rate of {} Hz is not supported", sample_rate);
+ if (spectral_band_replication_sample_rate.has_value() && audio_sample_rate_is_outside_limits(spectral_band_replication_sample_rate.value()))
+ return DecoderError::format(DecoderErrorCategory::NotImplemented, "AAC SBR sample rate of {} Hz is not supported", spectral_band_replication_sample_rate.value());
+
auto channels = channel_configuration(channel_count);
if (!channels.has_value())
return DecoderError::format(DecoderErrorCategory::NotImplemented, "No AAC channel configuration describes {} channels", channel_count);
diff --git a/Libraries/LibMediaCodecs/Limits.h b/Libraries/LibMediaCodecs/Limits.h
index 22e4f247d99..84f64c08e51 100644
--- a/Libraries/LibMediaCodecs/Limits.h
+++ b/Libraries/LibMediaCodecs/Limits.h
@@ -18,4 +18,12 @@ constexpr bool video_frame_exceeds_limits(u64 width, u64 height)
return width > MAX_VIDEO_FRAME_DIMENSION || height > MAX_VIDEO_FRAME_DIMENSION || width * height > MAX_VIDEO_FRAME_AREA;
}
+static constexpr u32 MIN_AUDIO_SAMPLE_RATE = 3000;
+static constexpr u32 MAX_AUDIO_SAMPLE_RATE = 768000;
+
+constexpr bool audio_sample_rate_is_outside_limits(u64 sample_rate)
+{
+ return sample_rate < MIN_AUDIO_SAMPLE_RATE || sample_rate > MAX_AUDIO_SAMPLE_RATE;
+}
+
}
diff --git a/Tests/LibMedia/CMakeLists.txt b/Tests/LibMedia/CMakeLists.txt
index 3e50256ff44..35b6150a86a 100644
--- a/Tests/LibMedia/CMakeLists.txt
+++ b/Tests/LibMedia/CMakeLists.txt
@@ -18,6 +18,7 @@ set(TEST_SOURCES
TestDecoderRegistry.cpp
TestDemuxerRegistry.cpp
TestDisplayingVideoSink.cpp
+ TestFFmpegAudioConverter.cpp
TestFFmpegAudioNormalization.cpp
TestFLACDemuxer.cpp
TestFrameScanTimeline.cpp
diff --git a/Tests/LibMedia/TestCodecConfigurations.cpp b/Tests/LibMedia/TestCodecConfigurations.cpp
index c6c12e1ac03..4bf075b5bba 100644
--- a/Tests/LibMedia/TestCodecConfigurations.cpp
+++ b/Tests/LibMedia/TestCodecConfigurations.cpp
@@ -14,6 +14,7 @@
#include <LibMediaCodecs/Codecs/H265.h>
#include <LibMediaCodecs/Codecs/NALUnit.h>
#include <LibMediaCodecs/Codecs/VP9.h>
+#include <LibMediaCodecs/Limits.h>
#include <LibTest/TestCase.h>
TEST_CASE(opus_isobmff_configuration_is_normalized_to_opus_head)
@@ -1238,3 +1239,18 @@ TEST_CASE(h264_max_num_reorder_frames_infers_zero_from_profile_constraints)
check_inference(without_vui);
check_inference(without_bitstream_restriction);
}
+
+TEST_CASE(aac_configuration_record_refuses_sample_rates_outside_the_supported_range)
+{
+ auto create = [](u32 sample_rate, Optional<u32> spectral_band_replication_sample_rate = {}) {
+ return Media::Codecs::AAC::create_configuration_record(Media::Codecs::AAC::LOW_COMPLEXITY_AUDIO_OBJECT_TYPE, sample_rate, 2, spectral_band_replication_sample_rate);
+ };
+
+ EXPECT(!create(Media::MIN_AUDIO_SAMPLE_RATE).is_error());
+ EXPECT(!create(Media::MAX_AUDIO_SAMPLE_RATE / 2, Media::MAX_AUDIO_SAMPLE_RATE).is_error());
+
+ EXPECT_EQ(create(Media::MIN_AUDIO_SAMPLE_RATE - 1).error().category(), Media::DecoderErrorCategory::NotImplemented);
+ EXPECT_EQ(create(Media::MAX_AUDIO_SAMPLE_RATE + 1).error().category(), Media::DecoderErrorCategory::NotImplemented);
+ EXPECT_EQ(create(Media::MAX_AUDIO_SAMPLE_RATE / 2, Media::MAX_AUDIO_SAMPLE_RATE + 2).error().category(), Media::DecoderErrorCategory::NotImplemented);
+ EXPECT_EQ(create(Media::MIN_AUDIO_SAMPLE_RATE - 1, (Media::MIN_AUDIO_SAMPLE_RATE - 1) * 2).error().category(), Media::DecoderErrorCategory::NotImplemented);
+}
diff --git a/Tests/LibMedia/TestFFmpegAudioConverter.cpp b/Tests/LibMedia/TestFFmpegAudioConverter.cpp
new file mode 100644
index 00000000000..1c27f94607c
--- /dev/null
+++ b/Tests/LibMedia/TestFFmpegAudioConverter.cpp
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/NonnullOwnPtr.h>
+#include <LibAudio/SampleSpecification.h>
+#include <LibMedia/FFmpeg/FFmpegAudioConverter.h>
+#include <LibMediaCodecs/AudioBlock.h>
+#include <LibMediaCodecs/Limits.h>
+#include <LibTest/TestCase.h>
+
+TEST_CASE(blocks_outside_the_sample_rate_range_are_refused)
+{
+ auto push_block_at_sample_rate = [](u32 sample_rate) {
+ auto converter = MUST(Media::FFmpeg::FFmpegAudioConverter::try_create());
+ MUST(converter->set_output_sample_specification(Audio::SampleSpecification { 48'000, Audio::ChannelMap::stereo() }));
+ Media::AudioBlock block;
+ block.initialize(Audio::SampleSpecification { sample_rate, Audio::ChannelMap::stereo() }, 0, 1024);
+ return converter->push_block(block);
+ };
+
+ EXPECT(!push_block_at_sample_rate(Media::MIN_AUDIO_SAMPLE_RATE).is_error());
+ EXPECT(!push_block_at_sample_rate(Media::MAX_AUDIO_SAMPLE_RATE).is_error());
+ EXPECT(push_block_at_sample_rate(1).is_error());
+ EXPECT(push_block_at_sample_rate(Media::MAX_AUDIO_SAMPLE_RATE + 1).is_error());
+}
+
+TEST_CASE(output_specifications_outside_the_sample_rate_range_are_refused)
+{
+ auto set_output_sample_rate = [](u32 sample_rate) {
+ auto converter = MUST(Media::FFmpeg::FFmpegAudioConverter::try_create());
+ return converter->set_output_sample_specification(Audio::SampleSpecification { sample_rate, Audio::ChannelMap::stereo() });
+ };
+
+ EXPECT(!set_output_sample_rate(Media::MIN_AUDIO_SAMPLE_RATE).is_error());
+ EXPECT(!set_output_sample_rate(Media::MAX_AUDIO_SAMPLE_RATE).is_error());
+ EXPECT(set_output_sample_rate(1).is_error());
+ EXPECT(set_output_sample_rate(Media::MAX_AUDIO_SAMPLE_RATE + 1).is_error());
+}
diff --git a/Tests/LibMedia/TestFLACDemuxer.cpp b/Tests/LibMedia/TestFLACDemuxer.cpp
index b1e755b753a..84f08249508 100644
--- a/Tests/LibMedia/TestFLACDemuxer.cpp
+++ b/Tests/LibMedia/TestFLACDemuxer.cpp
@@ -12,6 +12,7 @@
#include <LibMedia/DecoderRegistrations.h>
#include <LibMedia/IncrementallyPopulatedStream.h>
#include <LibMediaCodecs/CodedFrame.h>
+#include <LibMediaCodecs/Limits.h>
#include <LibTest/TestCase.h>
#include <LibThreading/Thread.h>
@@ -349,6 +350,27 @@ TEST_CASE(demuxer_declines_what_it_cannot_read)
EXPECT(demuxer_for(marker_only).is_error());
}
+TEST_CASE(demuxer_refuses_sample_rates_outside_the_supported_range)
+{
+ auto with_sample_rate = [](u32 sample_rate) {
+ // The sample rate is the first 20 bits after STREAMINFO's block and frame sizes.
+ static constexpr size_t sample_rate_offset = 8 + 10;
+ auto data = read_fixture("buffered-ranges/tone.flac"sv);
+ data[sample_rate_offset] = static_cast<u8>(sample_rate >> 12);
+ data[sample_rate_offset + 1] = static_cast<u8>(sample_rate >> 4);
+ data[sample_rate_offset + 2] = static_cast<u8>(((sample_rate & 0xF) << 4) | (data[sample_rate_offset + 2] & 0xF));
+ return data;
+ };
+
+ EXPECT(!demuxer_for(with_sample_rate(Media::MIN_AUDIO_SAMPLE_RATE)).is_error());
+ EXPECT(!demuxer_for(with_sample_rate(Media::MAX_AUDIO_SAMPLE_RATE)).is_error());
+
+ auto too_slow = demuxer_for(with_sample_rate(Media::MIN_AUDIO_SAMPLE_RATE - 1));
+ EXPECT_EQ(too_slow.error().category(), Media::DecoderErrorCategory::NotImplemented);
+ auto too_fast = demuxer_for(with_sample_rate(Media::MAX_AUDIO_SAMPLE_RATE + 1));
+ EXPECT_EQ(too_fast.error().category(), Media::DecoderErrorCategory::NotImplemented);
+}
+
TEST_CASE(demuxer_grows_the_duration_of_a_stream_that_does_not_state_its_length)
{
auto file_data = read_fixture("buffered-ranges/tone.flac"sv);
diff --git a/Tests/LibMedia/TestOggDemuxer.cpp b/Tests/LibMedia/TestOggDemuxer.cpp
index 92c0697038a..8a4e787e990 100644
--- a/Tests/LibMedia/TestOggDemuxer.cpp
+++ b/Tests/LibMedia/TestOggDemuxer.cpp
@@ -14,6 +14,7 @@
#include <LibMedia/IncrementallyPopulatedStream.h>
#include <LibMedia/Producers/DecodedVideoProducer.h>
#include <LibMediaCodecs/CodedFrame.h>
+#include <LibMediaCodecs/Limits.h>
#include <LibMediaCodecs/VideoFrame.h>
#include <LibTest/TestCase.h>
#include <LibThreading/Thread.h>
@@ -581,6 +582,38 @@ TEST_CASE(demuxer_declines_what_it_cannot_read)
EXPECT_EQ(not_ogg.error().category(), Media::DecoderErrorCategory::UnrecognizedFormat);
}
+TEST_CASE(identification_refuses_sample_rates_outside_the_supported_range)
+{
+ auto vorbis_with_sample_rate = [](u32 sample_rate) {
+ Array<u8, 30> packet {};
+ "\x01vorbis"sv.bytes().copy_to(packet.span());
+ packet[11] = 1;
+ for (size_t i = 0; i < sizeof(sample_rate); i++)
+ packet[12 + i] = static_cast<u8>(sample_rate >> (i * 8));
+ return Media::Ogg::identify_stream(packet.span());
+ };
+ auto flac_with_sample_rate = [](u32 sample_rate) {
+ Array<u8, 17 + 34> packet {};
+ packet[0] = 0x7F;
+ "FLAC"sv.bytes().copy_to(packet.span().slice(1));
+ packet[5] = 1;
+ "fLaC"sv.bytes().copy_to(packet.span().slice(9));
+ // Block sizes of 4096.
+ packet[17 + 0] = packet[17 + 2] = 0x10;
+ packet[17 + 10] = static_cast<u8>(sample_rate >> 12);
+ packet[17 + 11] = static_cast<u8>(sample_rate >> 4);
+ packet[17 + 12] = static_cast<u8>((sample_rate & 0xF) << 4);
+ return Media::Ogg::identify_stream(packet.span());
+ };
+
+ for (auto identify : { +vorbis_with_sample_rate, +flac_with_sample_rate }) {
+ EXPECT(!identify(Media::MIN_AUDIO_SAMPLE_RATE).is_error());
+ EXPECT(!identify(Media::MAX_AUDIO_SAMPLE_RATE).is_error());
+ EXPECT_EQ(identify(Media::MIN_AUDIO_SAMPLE_RATE - 1).error().category(), Media::DecoderErrorCategory::NotImplemented);
+ EXPECT_EQ(identify(Media::MAX_AUDIO_SAMPLE_RATE + 1).error().category(), Media::DecoderErrorCategory::NotImplemented);
+ }
+}
+
// A seek into data that is not there yet asks for little more than the pages its probes land on.
TEST_CASE(demuxer_seeks_into_unbuffered_data_with_few_requests)
{
diff --git a/Tests/LibMedia/TestWAVDemuxer.cpp b/Tests/LibMedia/TestWAVDemuxer.cpp
index 0088f5d2653..42b2352456e 100644
--- a/Tests/LibMedia/TestWAVDemuxer.cpp
+++ b/Tests/LibMedia/TestWAVDemuxer.cpp
@@ -11,6 +11,7 @@
#include <LibMedia/Containers/WAV/WAVDemuxer.h>
#include <LibMedia/IncrementallyPopulatedStream.h>
#include <LibMediaCodecs/CodedFrame.h>
+#include <LibMediaCodecs/Limits.h>
#include <LibTest/TestCase.h>
#include <LibThreading/Thread.h>
@@ -299,6 +300,17 @@ TEST_CASE(demuxer_declines_what_it_cannot_read)
EXPECT_EQ(no_data_chunk.error().category(), Media::DecoderErrorCategory::Corrupted);
}
+TEST_CASE(demuxer_refuses_sample_rates_outside_the_supported_range)
+{
+ EXPECT(!demuxer_for(make_wave({ .sample_rate = Media::MIN_AUDIO_SAMPLE_RATE })).is_error());
+ EXPECT(!demuxer_for(make_wave({ .sample_rate = Media::MAX_AUDIO_SAMPLE_RATE })).is_error());
+
+ auto too_slow = demuxer_for(make_wave({ .sample_rate = Media::MIN_AUDIO_SAMPLE_RATE - 1 }));
+ EXPECT_EQ(too_slow.error().category(), Media::DecoderErrorCategory::NotImplemented);
+ auto too_fast = demuxer_for(make_wave({ .sample_rate = Media::MAX_AUDIO_SAMPLE_RATE + 1 }));
+ EXPECT_EQ(too_fast.error().category(), Media::DecoderErrorCategory::NotImplemented);
+}
+
TEST_CASE(demuxer_reads_to_the_end_of_a_stream_whose_data_size_is_left_open)
{
constexpr u32 frame_count = 2500;