QOALoader.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Error.h>
  8. #include <AK/NonnullOwnPtr.h>
  9. #include <AK/Span.h>
  10. #include <AK/Types.h>
  11. #include <LibAudio/Loader.h>
  12. #include <LibAudio/QOATypes.h>
  13. #include <LibAudio/SampleFormats.h>
  14. namespace Audio {
  15. // Decoder for the Quite Okay Audio (QOA) format.
  16. // NOTE: The QOA format is not finalized yet and this decoder might not be fully spec-compliant as of 2023-02-02.
  17. //
  18. // https://github.com/phoboslab/qoa/blob/master/qoa.h
  19. class QOALoaderPlugin : public LoaderPlugin {
  20. public:
  21. explicit QOALoaderPlugin(NonnullOwnPtr<AK::SeekableStream> stream);
  22. virtual ~QOALoaderPlugin() override = default;
  23. static bool sniff(SeekableStream& stream);
  24. static ErrorOr<NonnullOwnPtr<LoaderPlugin>, LoaderError> create(NonnullOwnPtr<SeekableStream>);
  25. virtual ErrorOr<Vector<FixedArray<Sample>>, LoaderError> load_chunks(size_t samples_to_read_from_input) override;
  26. virtual MaybeLoaderError reset() override;
  27. virtual MaybeLoaderError seek(int sample_index) override;
  28. virtual int loaded_samples() override { return static_cast<int>(m_loaded_samples); }
  29. virtual int total_samples() override { return static_cast<int>(m_total_samples); }
  30. virtual u32 sample_rate() override { return m_sample_rate; }
  31. virtual u16 num_channels() override { return m_num_channels; }
  32. virtual ByteString format_name() override { return "Quite Okay Audio (.qoa)"; }
  33. virtual PcmSampleFormat pcm_format() override { return PcmSampleFormat::Int16; }
  34. private:
  35. enum class IsFirstFrame : bool {
  36. Yes = true,
  37. No = false,
  38. };
  39. MaybeLoaderError initialize();
  40. MaybeLoaderError parse_header();
  41. MaybeLoaderError load_one_frame(Span<Sample>& target, IsFirstFrame is_first_frame = IsFirstFrame::No);
  42. // Updates predictor values in lms_state so the next slice can reuse the same state.
  43. MaybeLoaderError read_one_slice(QOA::LMSState& lms_state, Span<i16>& samples);
  44. static ALWAYS_INLINE QOA::UnpackedSlice unpack_slice(QOA::PackedSlice packed_slice);
  45. // QOA's division routine for scaling residuals before final quantization.
  46. static ALWAYS_INLINE i16 qoa_divide(i16 value, i16 scale_factor);
  47. // Because QOA has dynamic sample rate and channel count, we only use the sample rate and channel count from the first frame.
  48. u32 m_sample_rate { 0 };
  49. u8 m_num_channels { 0 };
  50. // If this is the case (the reference encoder even enforces it at the moment)
  51. bool m_has_uniform_channel_count { true };
  52. size_t m_loaded_samples { 0 };
  53. size_t m_total_samples { 0 };
  54. };
  55. }