FlacLoader.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "Buffer.h"
  8. #include "FlacTypes.h"
  9. #include "Loader.h"
  10. #include <AK/Error.h>
  11. #include <AK/Span.h>
  12. #include <AK/Types.h>
  13. #include <LibCore/InputBitStream.h>
  14. #include <LibCore/MemoryStream.h>
  15. #include <LibCore/Stream.h>
  16. namespace Audio {
  17. using Core::Stream::BigEndianInputBitStream;
  18. // Experimentally determined to be a decent buffer size on i686:
  19. // 4K (the default) is slightly worse, and 64K is much worse.
  20. // At sufficiently large buffer sizes, the advantage of infrequent read() calls is outweighed by the memmove() overhead.
  21. // There was no intensive fine-tuning done to determine this value, so improvements may definitely be possible.
  22. constexpr size_t FLAC_BUFFER_SIZE = 8 * KiB;
  23. ALWAYS_INLINE u8 frame_channel_type_to_channel_count(FlacFrameChannelType channel_type);
  24. // Sign-extend an arbitrary-size signed number to 64 bit signed
  25. ALWAYS_INLINE i64 sign_extend(u32 n, u8 size);
  26. // Decodes the sign representation method used in Rice coding.
  27. // Numbers alternate between positive and negative: 0, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5, ...
  28. ALWAYS_INLINE i32 rice_to_signed(u32 x);
  29. // decoders
  30. // read a UTF-8 encoded number, even if it is not a valid codepoint
  31. ALWAYS_INLINE ErrorOr<u64> read_utf8_char(BigEndianInputBitStream& input);
  32. // decode a single number encoded with exponential golomb encoding of the specified order
  33. ALWAYS_INLINE ErrorOr<i32> decode_unsigned_exp_golomb(u8 order, BigEndianInputBitStream& bit_input);
  34. class FlacLoaderPlugin : public LoaderPlugin {
  35. public:
  36. explicit FlacLoaderPlugin(StringView path);
  37. explicit FlacLoaderPlugin(Bytes& buffer);
  38. ~FlacLoaderPlugin()
  39. {
  40. }
  41. virtual MaybeLoaderError initialize() override;
  42. virtual LoaderSamples get_more_samples(size_t max_bytes_to_read_from_input = 128 * KiB) override;
  43. virtual MaybeLoaderError reset() override;
  44. virtual MaybeLoaderError seek(int sample_index) override;
  45. virtual int loaded_samples() override { return static_cast<int>(m_loaded_samples); }
  46. virtual int total_samples() override { return static_cast<int>(m_total_samples); }
  47. virtual u32 sample_rate() override { return m_sample_rate; }
  48. virtual u16 num_channels() override { return m_num_channels; }
  49. virtual String format_name() override { return "FLAC (.flac)"; }
  50. virtual PcmSampleFormat pcm_format() override { return m_sample_format; }
  51. virtual RefPtr<Core::File> file() override { return m_file; }
  52. bool is_fixed_blocksize_stream() const { return m_min_block_size == m_max_block_size; }
  53. bool sample_count_unknown() const { return m_total_samples == 0; }
  54. private:
  55. MaybeLoaderError parse_header();
  56. // Either returns the metadata block or sets error message.
  57. // Additionally, increments m_data_start_location past the read meta block.
  58. ErrorOr<FlacRawMetadataBlock, LoaderError> next_meta_block(BigEndianInputBitStream& bit_input);
  59. // Fetches and writes the next FLAC frame
  60. MaybeLoaderError next_frame(Span<Sample>);
  61. // Helper of next_frame that fetches a sub frame's header
  62. ErrorOr<FlacSubframeHeader, LoaderError> next_subframe_header(BigEndianInputBitStream& bit_input, u8 channel_index);
  63. // Helper of next_frame that decompresses a subframe
  64. ErrorOr<Vector<i32>, LoaderError> parse_subframe(FlacSubframeHeader& subframe_header, BigEndianInputBitStream& bit_input);
  65. // Subframe-internal data decoders (heavy lifting)
  66. ErrorOr<Vector<i32>, LoaderError> decode_fixed_lpc(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
  67. ErrorOr<Vector<i32>, LoaderError> decode_verbatim(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
  68. ErrorOr<Vector<i32>, LoaderError> decode_custom_lpc(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
  69. MaybeLoaderError decode_residual(Vector<i32>& decoded, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
  70. // decode a single rice partition that has its own rice parameter
  71. ALWAYS_INLINE ErrorOr<Vector<i32>, LoaderError> decode_rice_partition(u8 partition_type, u32 partitions, u32 partition_index, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
  72. MaybeLoaderError load_seektable(FlacRawMetadataBlock&);
  73. // Converters for special coding used in frame headers
  74. ALWAYS_INLINE ErrorOr<u32, LoaderError> convert_sample_count_code(u8 sample_count_code);
  75. ALWAYS_INLINE ErrorOr<u32, LoaderError> convert_sample_rate_code(u8 sample_rate_code);
  76. ALWAYS_INLINE ErrorOr<PcmSampleFormat, LoaderError> convert_bit_depth_code(u8 bit_depth_code);
  77. RefPtr<Core::File> m_file;
  78. Optional<LoaderError> m_error {};
  79. // Data obtained directly from the FLAC metadata: many values have specific bit counts
  80. u32 m_sample_rate { 0 }; // 20 bit
  81. u8 m_num_channels { 0 }; // 3 bit
  82. PcmSampleFormat m_sample_format; // 5 bits for the integer bit depth
  83. // Blocks are units of decoded audio data
  84. u16 m_min_block_size { 0 };
  85. u16 m_max_block_size { 0 };
  86. // Frames are units of encoded audio data, both of these are 24-bit
  87. u32 m_min_frame_size { 0 }; // 24 bit
  88. u32 m_max_frame_size { 0 }; // 24 bit
  89. u64 m_total_samples { 0 }; // 36 bit
  90. u8 m_md5_checksum[128 / 8]; // 128 bit (!)
  91. size_t m_loaded_samples { 0 };
  92. // keep track of the start of the data in the FLAC stream to seek back more easily
  93. u64 m_data_start_location { 0 };
  94. OwnPtr<Core::Stream::SeekableStream> m_stream;
  95. Optional<FlacFrameHeader> m_current_frame;
  96. // Whatever the last get_more_samples() call couldn't return gets stored here.
  97. Vector<Sample, FLAC_BUFFER_SIZE> m_unread_data;
  98. u64 m_current_sample_or_frame { 0 };
  99. Vector<FlacSeekPoint> m_seektable;
  100. };
  101. }