/* * Copyright 2023 jacqueline * * SPDX-License-Identifier: GPL-3.0-only */ #pragma once #include #include #include #include #include #include #include #include #include "result.hpp" #include "span.hpp" #include "types.hpp" namespace codecs { /* * Common interface to be implemented by all audio decoders. */ class ICodec { public: virtual ~ICodec() {} /* Errors that may be returned by codecs. */ enum class Error { // Indicates that more data is required before this codec can finish its // operation. E.g. the input buffer ends with a truncated frame. kOutOfInput, // Indicates that the data within the input buffer is fatally malformed. kMalformedData, kInternalError, }; /* * Alias for more readable return types. All codec methods, success or * failure, should also return the number of bytes they consumed. */ template using Result = std::pair>; struct OutputFormat { uint8_t num_channels; uint8_t bits_per_sample; uint32_t sample_rate_hz; std::optional duration_seconds; std::optional bits_per_second; }; /* * Decodes metadata or headers from the given input stream, and returns the * format for the samples that will be decoded from it. */ virtual auto BeginStream(cpp::span input) -> Result = 0; struct OutputInfo { std::size_t bytes_written; bool is_finished_writing; }; /* * Writes PCM samples to the given output buffer. */ virtual auto ContinueStream(cpp::span input, cpp::span output) -> Result = 0; virtual auto SeekStream(cpp::span input, std::size_t target_sample) -> Result = 0; }; auto CreateCodecForType(StreamType type) -> std::optional; } // namespace codecs