diff options
author | 2021-10-05 03:34:45 +1100 | |
---|---|---|
committer | 2021-10-05 03:34:45 +1100 | |
commit | aeb9eae87469e67bc6a91caf3840c34c11d959fc (patch) | |
tree | 45bffedf374dbce47caca633ad7bda547592e6c9 /src/lib/util/flac.cpp | |
parent | 33723892a3f06e678d817b36aef84364c32848ec (diff) |
util: Further API cleanups: (#8661)
* Turned `core_file` into an implementation of `random_read_write`.
* Turned PNG errors into a standard error category.
* Added a helper for generating what look like derived classes on-the-fly.
Diffstat (limited to 'src/lib/util/flac.cpp')
-rw-r--r-- | src/lib/util/flac.cpp | 48 |
1 files changed, 29 insertions, 19 deletions
diff --git a/src/lib/util/flac.cpp b/src/lib/util/flac.cpp index 13bf7aed8eb..9fb0c4c46dc 100644 --- a/src/lib/util/flac.cpp +++ b/src/lib/util/flac.cpp @@ -10,9 +10,10 @@ #include "flac.h" -#include "corefile.h" +#include "ioprocs.h" #include <cassert> +#include <cstring> #include <new> @@ -37,7 +38,7 @@ flac_encoder::flac_encoder(void *buffer, uint32_t buflength) } -flac_encoder::flac_encoder(util::core_file &file) +flac_encoder::flac_encoder(util::random_write &file) { init_common(); reset(file); @@ -102,7 +103,7 @@ bool flac_encoder::reset(void *buffer, uint32_t buflength) // reset - reset state with new file parameters //------------------------------------------------- -bool flac_encoder::reset(util::core_file &file) +bool flac_encoder::reset(util::random_write &file) { // configure the output m_compressed_start = nullptr; @@ -187,7 +188,16 @@ uint32_t flac_encoder::finish() { // process the data and return the amount written FLAC__stream_encoder_finish(m_encoder); - return (m_file != nullptr) ? m_file->tell() : m_compressed_offset; + if (m_file) + { + std::uint64_t result = 0; + m_file->tell(result); // TODO: check for error result, consider this may be too big for uint32_t + return result; + } + else + { + return m_compressed_offset; + } } @@ -232,29 +242,30 @@ FLAC__StreamEncoderWriteStatus flac_encoder::write_callback(const FLAC__byte buf size_t offset = 0; while (offset < bytes) { - // if we're ignoring, continue to do so if (m_ignore_bytes != 0) { + // if we're ignoring, continue to do so size_t ignore = std::min(bytes - offset, size_t(m_ignore_bytes)); offset += ignore; m_ignore_bytes -= ignore; } - - // if we haven't hit the end of metadata, process a new piece else if (!m_found_audio) { + // if we haven't hit the end of metadata, process a new piece assert(bytes - offset >= 4); m_found_audio = ((buffer[offset] & 0x80) != 0); m_ignore_bytes = (buffer[offset + 1] << 16) | (buffer[offset + 2] << 8) | buffer[offset + 3]; offset += 4; } - - // otherwise process as audio data and copy to the output else { + // otherwise process as audio data and copy to the output int count = bytes - offset; - if (m_file != nullptr) - m_file->write(buffer, count); + if (m_file) + { + size_t actual; + m_file->write(buffer, count, actual); // TODO: check for errors + } else { if (m_compressed_offset + count <= m_compressed_length) @@ -316,7 +327,7 @@ flac_decoder::flac_decoder(const void *buffer, uint32_t length, const void *buff // flac_decoder - constructor //------------------------------------------------- -flac_decoder::flac_decoder(util::core_file &file) +flac_decoder::flac_decoder(util::read_stream &file) : m_decoder(FLAC__stream_decoder_new()), m_file(&file), m_compressed_offset(0), @@ -422,7 +433,7 @@ bool flac_decoder::reset(uint32_t sample_rate, uint8_t num_channels, uint32_t bl // reset - reset state with new file parameter //------------------------------------------------- -bool flac_decoder::reset(util::core_file &file) +bool flac_decoder::reset(util::read_stream &file) { m_file = &file; m_compressed_start = nullptr; @@ -517,12 +528,11 @@ FLAC__StreamDecoderReadStatus flac_decoder::read_callback(FLAC__byte buffer[], s { uint32_t expected = *bytes; - // if a file, just read - if (m_file != nullptr) - *bytes = m_file->read(buffer, expected); - - // otherwise, copy from memory - else + if (m_file) // if a file, just read + { + m_file->read(buffer, expected, *bytes); // TODO: check for errors + } + else // otherwise, copy from memory { // copy from primary buffer first uint32_t outputpos = 0; |