2021-06-25 13:54:14 +02:00
|
|
|
/*
|
2022-01-13 12:07:00 +01:00
|
|
|
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
|
2021-06-25 13:54:14 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "FlacTypes.h"
|
|
|
|
|
#include "Loader.h"
|
2023-01-25 20:06:16 +01:00
|
|
|
#include <AK/BitStream.h>
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
#include <AK/Error.h>
|
2022-01-14 01:14:24 +01:00
|
|
|
#include <AK/Span.h>
|
2021-06-25 13:54:14 +02:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
|
|
namespace Audio {
|
|
|
|
|
|
|
|
|
|
ALWAYS_INLINE u8 frame_channel_type_to_channel_count(FlacFrameChannelType channel_type);
|
|
|
|
|
// Sign-extend an arbitrary-size signed number to 64 bit signed
|
|
|
|
|
ALWAYS_INLINE i64 sign_extend(u32 n, u8 size);
|
|
|
|
|
// Decodes the sign representation method used in Rice coding.
|
|
|
|
|
// Numbers alternate between positive and negative: 0, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5, ...
|
|
|
|
|
ALWAYS_INLINE i32 rice_to_signed(u32 x);
|
|
|
|
|
|
|
|
|
|
// decoders
|
|
|
|
|
// read a UTF-8 encoded number, even if it is not a valid codepoint
|
2022-01-14 01:14:24 +01:00
|
|
|
ALWAYS_INLINE ErrorOr<u64> read_utf8_char(BigEndianInputBitStream& input);
|
2021-06-25 13:54:14 +02:00
|
|
|
// decode a single number encoded with exponential golomb encoding of the specified order
|
2022-01-14 01:14:24 +01:00
|
|
|
ALWAYS_INLINE ErrorOr<i32> decode_unsigned_exp_golomb(u8 order, BigEndianInputBitStream& bit_input);
|
2021-06-25 13:54:14 +02:00
|
|
|
|
2022-06-16 21:23:31 +02:00
|
|
|
// Loader for the Free Lossless Audio Codec (FLAC)
|
|
|
|
|
// This loader supports all audio features of FLAC, although audio from more than two channels is discarded.
|
|
|
|
|
// The loader currently supports the STREAMINFO, PADDING, and SEEKTABLE metadata blocks.
|
|
|
|
|
// See: https://xiph.org/flac/documentation_format_overview.html
|
|
|
|
|
// https://xiph.org/flac/format.html (identical to IETF draft version 2)
|
|
|
|
|
// https://datatracker.ietf.org/doc/html/draft-ietf-cellar-flac-02 (all section numbers refer to this specification)
|
|
|
|
|
// https://datatracker.ietf.org/doc/html/draft-ietf-cellar-flac-03 (newer IETF draft that uses incompatible numberings and names)
|
2021-06-25 13:54:14 +02:00
|
|
|
class FlacLoaderPlugin : public LoaderPlugin {
|
|
|
|
|
public:
|
2023-01-22 05:09:11 +01:00
|
|
|
explicit FlacLoaderPlugin(NonnullOwnPtr<SeekableStream> stream);
|
2022-10-03 20:28:11 +02:00
|
|
|
virtual ~FlacLoaderPlugin() override = default;
|
2021-06-25 13:54:14 +02:00
|
|
|
|
2023-06-24 17:04:38 +02:00
|
|
|
static bool sniff(SeekableStream& stream);
|
|
|
|
|
static ErrorOr<NonnullOwnPtr<LoaderPlugin>, LoaderError> create(NonnullOwnPtr<SeekableStream>);
|
2021-06-25 13:54:14 +02: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 00:05:14 +01:00
|
|
|
virtual ErrorOr<Vector<FixedArray<Sample>>, LoaderError> load_chunks(size_t samples_to_read_from_input) override;
|
2021-06-25 13:54:14 +02:00
|
|
|
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
virtual MaybeLoaderError reset() override;
|
2021-12-21 15:43:18 -08:00
|
|
|
virtual MaybeLoaderError seek(int sample_index) override;
|
2021-06-25 13:54:14 +02:00
|
|
|
|
2021-11-15 22:27:28 +01:00
|
|
|
virtual int loaded_samples() override { return static_cast<int>(m_loaded_samples); }
|
|
|
|
|
virtual int total_samples() override { return static_cast<int>(m_total_samples); }
|
2021-06-25 13:54:14 +02:00
|
|
|
virtual u32 sample_rate() override { return m_sample_rate; }
|
|
|
|
|
virtual u16 num_channels() override { return m_num_channels; }
|
2023-12-16 17:49:34 +03:30
|
|
|
virtual ByteString format_name() override { return "FLAC (.flac)"; }
|
2021-06-25 13:54:14 +02:00
|
|
|
virtual PcmSampleFormat pcm_format() override { return m_sample_format; }
|
|
|
|
|
|
|
|
|
|
bool is_fixed_blocksize_stream() const { return m_min_block_size == m_max_block_size; }
|
|
|
|
|
bool sample_count_unknown() const { return m_total_samples == 0; }
|
|
|
|
|
|
|
|
|
|
private:
|
2022-12-05 00:41:23 +01:00
|
|
|
MaybeLoaderError initialize();
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
MaybeLoaderError parse_header();
|
2021-06-25 13:54:14 +02:00
|
|
|
// Either returns the metadata block or sets error message.
|
|
|
|
|
// Additionally, increments m_data_start_location past the read meta block.
|
2022-01-14 01:14:24 +01:00
|
|
|
ErrorOr<FlacRawMetadataBlock, LoaderError> next_meta_block(BigEndianInputBitStream& bit_input);
|
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 00:05:14 +01:00
|
|
|
// Fetches and returns the next FLAC frame.
|
|
|
|
|
LoaderSamples next_frame();
|
2021-06-25 13:54:14 +02:00
|
|
|
// Helper of next_frame that fetches a sub frame's header
|
2022-01-14 01:14:24 +01:00
|
|
|
ErrorOr<FlacSubframeHeader, LoaderError> next_subframe_header(BigEndianInputBitStream& bit_input, u8 channel_index);
|
2021-06-25 13:54:14 +02:00
|
|
|
// Helper of next_frame that decompresses a subframe
|
2023-07-06 20:10:17 +02:00
|
|
|
ErrorOr<void, LoaderError> parse_subframe(Vector<i64>& samples, FlacSubframeHeader& subframe_header, BigEndianInputBitStream& bit_input);
|
2021-06-25 13:54:14 +02:00
|
|
|
// Subframe-internal data decoders (heavy lifting)
|
2023-06-26 15:12:20 +02:00
|
|
|
ErrorOr<Vector<i64>, LoaderError> decode_fixed_lpc(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
|
|
|
|
|
ErrorOr<Vector<i64>, LoaderError> decode_verbatim(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
|
2023-07-06 20:10:17 +02:00
|
|
|
ErrorOr<void, LoaderError> decode_custom_lpc(Vector<i64>& decoded, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
|
2023-06-26 15:12:20 +02:00
|
|
|
MaybeLoaderError decode_residual(Vector<i64>& decoded, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
|
2021-06-25 13:54:14 +02:00
|
|
|
// decode a single rice partition that has its own rice parameter
|
2023-06-26 15:12:20 +02:00
|
|
|
ALWAYS_INLINE ErrorOr<Vector<i64>, LoaderError> decode_rice_partition(u8 partition_type, u32 partitions, u32 partition_index, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
|
2021-12-21 15:07:49 -08:00
|
|
|
MaybeLoaderError load_seektable(FlacRawMetadataBlock&);
|
2023-03-07 16:58:01 +01:00
|
|
|
// Note that failing to read a Vorbis comment block is not treated as an error of the FLAC loader, since metadata is optional.
|
|
|
|
|
void load_vorbis_comment(FlacRawMetadataBlock&);
|
2022-10-02 18:48:38 +02:00
|
|
|
MaybeLoaderError load_picture(FlacRawMetadataBlock&);
|
2021-06-25 13:54:14 +02:00
|
|
|
|
|
|
|
|
// Converters for special coding used in frame headers
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
ALWAYS_INLINE ErrorOr<u32, LoaderError> convert_sample_count_code(u8 sample_count_code);
|
|
|
|
|
ALWAYS_INLINE ErrorOr<u32, LoaderError> convert_sample_rate_code(u8 sample_rate_code);
|
2023-03-14 19:24:18 +01:00
|
|
|
ALWAYS_INLINE ErrorOr<u8, LoaderError> convert_bit_depth_code(u8 bit_depth_code);
|
2021-06-25 13:54:14 +02:00
|
|
|
|
2023-03-18 14:12:25 +01:00
|
|
|
bool should_insert_seekpoint_at(u64 sample_index) const;
|
|
|
|
|
|
2021-06-25 13:54:14 +02:00
|
|
|
// Data obtained directly from the FLAC metadata: many values have specific bit counts
|
2023-03-14 19:24:18 +01:00
|
|
|
u32 m_sample_rate { 0 }; // 20 bit
|
|
|
|
|
u8 m_num_channels { 0 }; // 3 bit
|
|
|
|
|
u8 m_bits_per_sample { 0 }; // 5 bits for the integer bit depth
|
|
|
|
|
// Externally visible format; the smallest integer format that's larger than the precise bit depth.
|
|
|
|
|
PcmSampleFormat m_sample_format;
|
2021-06-25 13:54:14 +02:00
|
|
|
// Blocks are units of decoded audio data
|
|
|
|
|
u16 m_min_block_size { 0 };
|
|
|
|
|
u16 m_max_block_size { 0 };
|
|
|
|
|
// Frames are units of encoded audio data, both of these are 24-bit
|
2022-04-01 20:58:27 +03:00
|
|
|
u32 m_min_frame_size { 0 }; // 24 bit
|
2021-06-25 13:54:14 +02:00
|
|
|
u32 m_max_frame_size { 0 }; // 24 bit
|
|
|
|
|
u64 m_total_samples { 0 }; // 36 bit
|
|
|
|
|
u8 m_md5_checksum[128 / 8]; // 128 bit (!)
|
2021-07-22 15:41:18 +02:00
|
|
|
size_t m_loaded_samples { 0 };
|
2021-06-25 13:54:14 +02:00
|
|
|
|
|
|
|
|
// keep track of the start of the data in the FLAC stream to seek back more easily
|
|
|
|
|
u64 m_data_start_location { 0 };
|
|
|
|
|
Optional<FlacFrameHeader> m_current_frame;
|
|
|
|
|
u64 m_current_sample_or_frame { 0 };
|
2023-03-18 14:12:25 +01:00
|
|
|
SeekTable m_seektable;
|
2023-07-06 20:10:17 +02:00
|
|
|
|
|
|
|
|
// Keep around a few temporary buffers whose allocated space can be reused.
|
|
|
|
|
// This is an empirical optimization since allocations and deallocations take a lot of time in the decoder.
|
|
|
|
|
mutable Vector<Vector<i64>, 2> m_subframe_buffers;
|
2021-06-25 13:54:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|