FlacLoader.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "FlacTypes.h"
  8. #include "Loader.h"
  9. #include <AK/BitStream.h>
  10. #include <AK/Error.h>
  11. #include <AK/Span.h>
  12. #include <AK/Types.h>
  13. namespace Audio {
  14. ALWAYS_INLINE u8 frame_channel_type_to_channel_count(FlacFrameChannelType channel_type);
  15. // Sign-extend an arbitrary-size signed number to 64 bit signed
  16. ALWAYS_INLINE i64 sign_extend(u32 n, u8 size);
  17. // Decodes the sign representation method used in Rice coding.
  18. // Numbers alternate between positive and negative: 0, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5, ...
  19. ALWAYS_INLINE i32 rice_to_signed(u32 x);
  20. // decoders
  21. // read a UTF-8 encoded number, even if it is not a valid codepoint
  22. ALWAYS_INLINE ErrorOr<u64> read_utf8_char(BigEndianInputBitStream& input);
  23. // decode a single number encoded with exponential golomb encoding of the specified order
  24. ALWAYS_INLINE ErrorOr<i32> decode_unsigned_exp_golomb(u8 order, BigEndianInputBitStream& bit_input);
  25. // Loader for the Free Lossless Audio Codec (FLAC)
  26. // This loader supports all audio features of FLAC, although audio from more than two channels is discarded.
  27. // The loader currently supports the STREAMINFO, PADDING, and SEEKTABLE metadata blocks.
  28. // See: https://xiph.org/flac/documentation_format_overview.html
  29. // https://xiph.org/flac/format.html (identical to IETF draft version 2)
  30. // https://datatracker.ietf.org/doc/html/draft-ietf-cellar-flac-02 (all section numbers refer to this specification)
  31. // https://datatracker.ietf.org/doc/html/draft-ietf-cellar-flac-03 (newer IETF draft that uses incompatible numberings and names)
  32. class FlacLoaderPlugin : public LoaderPlugin {
  33. public:
  34. explicit FlacLoaderPlugin(NonnullOwnPtr<SeekableStream> stream);
  35. virtual ~FlacLoaderPlugin() override = default;
  36. static bool sniff(SeekableStream& stream);
  37. static ErrorOr<NonnullOwnPtr<LoaderPlugin>, LoaderError> create(NonnullOwnPtr<SeekableStream>);
  38. virtual ErrorOr<Vector<FixedArray<Sample>>, LoaderError> load_chunks(size_t samples_to_read_from_input) override;
  39. virtual MaybeLoaderError reset() override;
  40. virtual MaybeLoaderError seek(int sample_index) override;
  41. virtual int loaded_samples() override { return static_cast<int>(m_loaded_samples); }
  42. virtual int total_samples() override { return static_cast<int>(m_total_samples); }
  43. virtual u32 sample_rate() override { return m_sample_rate; }
  44. virtual u16 num_channels() override { return m_num_channels; }
  45. virtual ByteString format_name() override { return "FLAC (.flac)"; }
  46. virtual PcmSampleFormat pcm_format() override { return m_sample_format; }
  47. bool is_fixed_blocksize_stream() const { return m_min_block_size == m_max_block_size; }
  48. bool sample_count_unknown() const { return m_total_samples == 0; }
  49. private:
  50. MaybeLoaderError initialize();
  51. MaybeLoaderError parse_header();
  52. // Either returns the metadata block or sets error message.
  53. // Additionally, increments m_data_start_location past the read meta block.
  54. ErrorOr<FlacRawMetadataBlock, LoaderError> next_meta_block(BigEndianInputBitStream& bit_input);
  55. // Fetches and returns the next FLAC frame.
  56. LoaderSamples next_frame();
  57. // Helper of next_frame that fetches a sub frame's header
  58. ErrorOr<FlacSubframeHeader, LoaderError> next_subframe_header(BigEndianInputBitStream& bit_input, u8 channel_index);
  59. // Helper of next_frame that decompresses a subframe
  60. ErrorOr<void, LoaderError> parse_subframe(Vector<i64>& samples, FlacSubframeHeader& subframe_header, BigEndianInputBitStream& bit_input);
  61. // Subframe-internal data decoders (heavy lifting)
  62. ErrorOr<Vector<i64>, LoaderError> decode_fixed_lpc(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
  63. ErrorOr<Vector<i64>, LoaderError> decode_verbatim(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
  64. ErrorOr<void, LoaderError> decode_custom_lpc(Vector<i64>& decoded, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
  65. MaybeLoaderError decode_residual(Vector<i64>& decoded, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
  66. // decode a single rice partition that has its own rice parameter
  67. ALWAYS_INLINE ErrorOr<Vector<i64>, LoaderError> decode_rice_partition(u8 partition_type, u32 partitions, u32 partition_index, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input);
  68. MaybeLoaderError load_seektable(FlacRawMetadataBlock&);
  69. // Note that failing to read a Vorbis comment block is not treated as an error of the FLAC loader, since metadata is optional.
  70. void load_vorbis_comment(FlacRawMetadataBlock&);
  71. MaybeLoaderError load_picture(FlacRawMetadataBlock&);
  72. // Converters for special coding used in frame headers
  73. ALWAYS_INLINE ErrorOr<u32, LoaderError> convert_sample_count_code(u8 sample_count_code);
  74. ALWAYS_INLINE ErrorOr<u32, LoaderError> convert_sample_rate_code(u8 sample_rate_code);
  75. ALWAYS_INLINE ErrorOr<u8, LoaderError> convert_bit_depth_code(u8 bit_depth_code);
  76. bool should_insert_seekpoint_at(u64 sample_index) const;
  77. // Data obtained directly from the FLAC metadata: many values have specific bit counts
  78. u32 m_sample_rate { 0 }; // 20 bit
  79. u8 m_num_channels { 0 }; // 3 bit
  80. u8 m_bits_per_sample { 0 }; // 5 bits for the integer bit depth
  81. // Externally visible format; the smallest integer format that's larger than the precise bit depth.
  82. PcmSampleFormat m_sample_format;
  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. Optional<FlacFrameHeader> m_current_frame;
  95. u64 m_current_sample_or_frame { 0 };
  96. SeekTable m_seektable;
  97. // Keep around a few temporary buffers whose allocated space can be reused.
  98. // This is an empirical optimization since allocations and deallocations take a lot of time in the decoder.
  99. mutable Vector<Vector<i64>, 2> m_subframe_buffers;
  100. };
  101. }