2023-02-02 19:28:57 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Error.h>
|
2023-06-24 18:04:38 +03:00
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2023-02-02 19:28:57 +03:00
|
|
|
#include <AK/Span.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <LibAudio/Loader.h>
|
|
|
|
#include <LibAudio/QOATypes.h>
|
|
|
|
#include <LibAudio/SampleFormats.h>
|
|
|
|
|
|
|
|
namespace Audio {
|
|
|
|
|
|
|
|
// Decoder for the Quite Okay Audio (QOA) format.
|
|
|
|
// NOTE: The QOA format is not finalized yet and this decoder might not be fully spec-compliant as of 2023-02-02.
|
|
|
|
//
|
|
|
|
// https://github.com/phoboslab/qoa/blob/master/qoa.h
|
|
|
|
class QOALoaderPlugin : public LoaderPlugin {
|
|
|
|
public:
|
|
|
|
explicit QOALoaderPlugin(NonnullOwnPtr<AK::SeekableStream> stream);
|
|
|
|
virtual ~QOALoaderPlugin() override = default;
|
|
|
|
|
2023-06-24 18:04:38 +03:00
|
|
|
static bool sniff(SeekableStream& stream);
|
|
|
|
static ErrorOr<NonnullOwnPtr<LoaderPlugin>, LoaderError> create(NonnullOwnPtr<SeekableStream>);
|
2023-02-02 19:28:57 +03:00
|
|
|
|
LibAudio: Move audio stream buffering into the loader
Before, some loader plugins implemented their own buffering (FLAC&MP3),
some didn't require any (WAV), and some didn't buffer at all (QOA). This
meant that in practice, while you could load arbitrary amounts of
samples from some loader plugins, you couldn't do that with some others.
Also, it was ill-defined how many samples you would actually get back
from a get_more_samples call.
This commit fixes that by introducing a layer of abstraction between the
loader and its plugins (because that's the whole point of having the
extra class!). The plugins now only implement a load_chunks() function,
which is much simpler to implement and allows plugins to play fast and
loose with what they actually return. Basically, they can return many
chunks of samples, where one chunk is simply a convenient block of
samples to load. In fact, some loaders such as FLAC and QOA have
separate internal functions for loading exactly one chunk. The loaders
*should* load as many chunks as necessary for the sample count to be
reached or surpassed (the latter simplifies loading loops in the
implementations, since you don't need to know how large your next chunk
is going to be; a problem for e.g. FLAC). If a plugin has no problems
returning data of arbitrary size (currently WAV), it can return a single
chunk that exactly (or roughly) matches the requested sample count. If a
plugin is at the stream end, it can also return less samples than was
requested! The loader can handle all of these cases and may call into
load_chunk multiple times. If the plugin returns an empty chunk list (or
only empty chunks; again, they can play fast and loose), the loader
takes that as a stream end signal. Otherwise, the loader will always
return exactly as many samples as the user requested. Buffering is
handled by the loader, allowing any underlying plugin to deal with any
weird sample count requirement the user throws at it (looking at you,
SoundPlayer!).
This (not accidentally!) makes QOA work in SoundPlayer.
2023-02-27 02:05:14 +03:00
|
|
|
virtual ErrorOr<Vector<FixedArray<Sample>>, LoaderError> load_chunks(size_t samples_to_read_from_input) override;
|
2023-02-02 19:28:57 +03:00
|
|
|
|
|
|
|
virtual MaybeLoaderError reset() override;
|
|
|
|
virtual MaybeLoaderError seek(int sample_index) override;
|
|
|
|
|
|
|
|
virtual int loaded_samples() override { return static_cast<int>(m_loaded_samples); }
|
|
|
|
virtual int total_samples() override { return static_cast<int>(m_total_samples); }
|
|
|
|
virtual u32 sample_rate() override { return m_sample_rate; }
|
|
|
|
virtual u16 num_channels() override { return m_num_channels; }
|
2023-12-16 17:19:34 +03:00
|
|
|
virtual ByteString format_name() override { return "Quite Okay Audio (.qoa)"; }
|
2023-02-02 19:28:57 +03:00
|
|
|
virtual PcmSampleFormat pcm_format() override { return PcmSampleFormat::Int16; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum class IsFirstFrame : bool {
|
|
|
|
Yes = true,
|
|
|
|
No = false,
|
|
|
|
};
|
|
|
|
|
|
|
|
MaybeLoaderError initialize();
|
|
|
|
MaybeLoaderError parse_header();
|
|
|
|
|
|
|
|
MaybeLoaderError load_one_frame(Span<Sample>& target, IsFirstFrame is_first_frame = IsFirstFrame::No);
|
|
|
|
// Updates predictor values in lms_state so the next slice can reuse the same state.
|
|
|
|
MaybeLoaderError read_one_slice(QOA::LMSState& lms_state, Span<i16>& samples);
|
|
|
|
static ALWAYS_INLINE QOA::UnpackedSlice unpack_slice(QOA::PackedSlice packed_slice);
|
|
|
|
|
|
|
|
// QOA's division routine for scaling residuals before final quantization.
|
|
|
|
static ALWAYS_INLINE i16 qoa_divide(i16 value, i16 scale_factor);
|
|
|
|
|
|
|
|
// Because QOA has dynamic sample rate and channel count, we only use the sample rate and channel count from the first frame.
|
|
|
|
u32 m_sample_rate { 0 };
|
|
|
|
u8 m_num_channels { 0 };
|
|
|
|
// If this is the case (the reference encoder even enforces it at the moment)
|
|
|
|
bool m_has_uniform_channel_count { true };
|
|
|
|
|
|
|
|
size_t m_loaded_samples { 0 };
|
|
|
|
size_t m_total_samples { 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|