QOALoader.h 2.6 KB

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