From 4dcc26e9402279caa2ea39785f1c82a19f9a4c4b Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Tue, 17 Jan 2023 22:34:32 +0100 Subject: [PATCH] LibAudio: Skip MP3 frames if not enough historic data is available --- Userland/Libraries/LibAudio/MP3Loader.cpp | 10 +++++----- Userland/Libraries/LibAudio/MP3Loader.h | 3 +-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibAudio/MP3Loader.cpp b/Userland/Libraries/LibAudio/MP3Loader.cpp index f1082663fc..56785cba12 100644 --- a/Userland/Libraries/LibAudio/MP3Loader.cpp +++ b/Userland/Libraries/LibAudio/MP3Loader.cpp @@ -86,7 +86,6 @@ MaybeLoaderError MP3LoaderPlugin::seek(int const position) m_synthesis_buffer = {}; m_bit_reservoir.discard_or_error(m_bit_reservoir.size()); m_bit_reservoir.handle_any_error(); - m_is_first_frame = true; m_bitstream->align_to_byte_boundary(); return {}; } @@ -108,7 +107,6 @@ LoaderSamples MP3LoaderPlugin::get_more_samples(size_t max_samples_to_read_from_ m_current_frame = maybe_frame.release_value(); if (!m_current_frame.has_value()) break; - m_is_first_frame = false; m_current_frame_read = 0; } @@ -219,11 +217,11 @@ ErrorOr MP3LoaderPlugin::read_next_frame() continue; } - return read_frame_data(header, m_is_first_frame); + return read_frame_data(header); } } -ErrorOr MP3LoaderPlugin::read_frame_data(MP3::Header const& header, bool is_first_frame) +ErrorOr MP3LoaderPlugin::read_frame_data(MP3::Header const& header) { MP3::MP3Frame frame { header }; @@ -239,8 +237,10 @@ ErrorOr MP3LoaderPlugin::read_frame_data(MP3::Header if (m_bit_reservoir.write(buffer) != header.slot_count) return LoaderError { LoaderError::Category::IO, m_loaded_samples, "Could not write frame into bit reservoir." }; - if (frame.main_data_begin > 0 && is_first_frame) + // If we don't have enough data in the reservoir to process this frame, skip it (but keep the data). + if (old_reservoir_size < static_cast(frame.main_data_begin)) return frame; + if (!m_bit_reservoir.discard_or_error(old_reservoir_size - frame.main_data_begin)) return LoaderError { LoaderError::Category::IO, m_loaded_samples, "Could not discard old frame data." }; diff --git a/Userland/Libraries/LibAudio/MP3Loader.h b/Userland/Libraries/LibAudio/MP3Loader.h index 7cf4608703..ff9d79e7b9 100644 --- a/Userland/Libraries/LibAudio/MP3Loader.h +++ b/Userland/Libraries/LibAudio/MP3Loader.h @@ -47,7 +47,7 @@ private: MaybeLoaderError build_seek_table(); ErrorOr read_header(); ErrorOr read_next_frame(); - ErrorOr read_frame_data(MP3::Header const&, bool is_first_frame); + ErrorOr read_frame_data(MP3::Header const&); MaybeLoaderError read_side_information(MP3::MP3Frame&); ErrorOr read_scale_factors(MP3::MP3Frame&, InputBitStream& reservoir, size_t granule_index, size_t channel_index); MaybeLoaderError read_huffman_data(MP3::MP3Frame&, InputBitStream& reservoir, size_t granule_index, size_t channel_index, size_t granule_bits_read); @@ -70,7 +70,6 @@ private: PcmSampleFormat m_sample_format { PcmSampleFormat::Int16 }; int m_total_samples { 0 }; size_t m_loaded_samples { 0 }; - bool m_is_first_frame { true }; AK::Optional m_current_frame; u32 m_current_frame_read;