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>
|
2022-01-14 01:14:24 +01:00
|
|
|
#include <LibCore/MemoryStream.h>
|
|
|
|
#include <LibCore/Stream.h>
|
2021-06-25 13:54:14 +02:00
|
|
|
|
|
|
|
namespace Audio {
|
|
|
|
|
2021-12-16 23:18:49 +01:00
|
|
|
// Experimentally determined to be a decent buffer size on i686:
|
|
|
|
// 4K (the default) is slightly worse, and 64K is much worse.
|
|
|
|
// At sufficiently large buffer sizes, the advantage of infrequent read() calls is outweighed by the memmove() overhead.
|
|
|
|
// There was no intensive fine-tuning done to determine this value, so improvements may definitely be possible.
|
|
|
|
constexpr size_t FLAC_BUFFER_SIZE = 8 * KiB;
|
|
|
|
|
2021-06-25 13:54:14 +02:00
|
|
|
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-01-28 20:12:17 +00:00
|
|
|
static Result<NonnullOwnPtr<FlacLoaderPlugin>, LoaderError> create(StringView path);
|
|
|
|
static Result<NonnullOwnPtr<FlacLoaderPlugin>, LoaderError> create(Bytes buffer);
|
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 LoaderSamples get_more_samples(size_t max_bytes_to_read_from_input = 128 * KiB) 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; }
|
2022-12-04 18:02:33 +00:00
|
|
|
virtual DeprecatedString 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);
|
2021-12-17 18:42:36 +01:00
|
|
|
// Fetches and writes the next FLAC frame
|
|
|
|
MaybeLoaderError next_frame(Span<Sample>);
|
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
|
2022-01-14 01:14:24 +01:00
|
|
|
ErrorOr<Vector<i32>, LoaderError> parse_subframe(FlacSubframeHeader& subframe_header, BigEndianInputBitStream& bit_input);
|
2021-06-25 13:54:14 +02:00
|
|
|
// Subframe-internal data decoders (heavy lifting)
|
2022-01-14 01:14:24 +01:00
|
|
|
ErrorOr<Vector<i32>, LoaderError> decode_fixed_lpc(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
|
|
|
|
ErrorOr<Vector<i32>, LoaderError> decode_verbatim(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
|
|
|
|
ErrorOr<Vector<i32>, LoaderError> decode_custom_lpc(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
|
|
|
|
MaybeLoaderError decode_residual(Vector<i32>& 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
|
2022-01-14 01:14:24 +01:00
|
|
|
ALWAYS_INLINE ErrorOr<Vector<i32>, 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&);
|
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);
|
|
|
|
ALWAYS_INLINE ErrorOr<PcmSampleFormat, LoaderError> convert_bit_depth_code(u8 bit_depth_code);
|
2021-06-25 13:54:14 +02:00
|
|
|
|
|
|
|
// Data obtained directly from the FLAC metadata: many values have specific bit counts
|
|
|
|
u32 m_sample_rate { 0 }; // 20 bit
|
|
|
|
u8 m_num_channels { 0 }; // 3 bit
|
|
|
|
PcmSampleFormat m_sample_format; // 5 bits for the integer bit depth
|
|
|
|
// 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;
|
2021-12-17 18:42:36 +01:00
|
|
|
// Whatever the last get_more_samples() call couldn't return gets stored here.
|
|
|
|
Vector<Sample, FLAC_BUFFER_SIZE> m_unread_data;
|
2021-06-25 13:54:14 +02:00
|
|
|
u64 m_current_sample_or_frame { 0 };
|
2021-12-21 15:07:49 -08:00
|
|
|
Vector<FlacSeekPoint> m_seektable;
|
2021-06-25 13:54:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|