diff --git a/src/decoder/Bridge.cxx b/src/decoder/Bridge.cxx index 33f06adda..b4d9fd076 100644 --- a/src/decoder/Bridge.cxx +++ b/src/decoder/Bridge.cxx @@ -34,7 +34,6 @@ #include "input/cache/Manager.hxx" #include "input/cache/Stream.hxx" #include "fs/Path.hxx" -#include "util/ConstBuffer.hxx" #include "util/StringBuffer.hxx" #include diff --git a/src/decoder/plugins/FaadDecoderPlugin.cxx b/src/decoder/plugins/FaadDecoderPlugin.cxx index 0a4eb7530..357043ca0 100644 --- a/src/decoder/plugins/FaadDecoderPlugin.cxx +++ b/src/decoder/plugins/FaadDecoderPlugin.cxx @@ -24,9 +24,9 @@ #include "pcm/CheckAudioFormat.hxx" #include "tag/Handler.hxx" #include "util/ScopeExit.hxx" -#include "util/ConstBuffer.hxx" #include "util/Domain.hxx" #include "util/Math.hxx" +#include "util/SpanCast.hxx" #include "Log.hxx" #include @@ -65,27 +65,28 @@ static size_t adts_find_frame(DecoderBuffer &buffer) { while (true) { - auto data = ConstBuffer::FromVoid(buffer.Need(8)); - if (data.IsNull()) + auto data = FromBytesStrict(buffer.Need(8)); + if (data.data() == nullptr) /* failed */ return 0; /* find the 0xff marker */ - auto p = (const uint8_t *)std::memchr(data.data, 0xff, data.size); + auto p = (const uint8_t *)std::memchr(data.data(), 0xff, + data.size()); if (p == nullptr) { /* no marker - discard the buffer */ buffer.Clear(); continue; } - if (p > data.data) { + if (p > data.data()) { /* discard data before 0xff */ - buffer.Consume(p - data.data); + buffer.Consume(p - data.data()); continue; } /* is it a frame? */ - const size_t frame_length = adts_check_frame(data.data); + const size_t frame_length = adts_check_frame(data.data()); if (frame_length == 0) { /* it's just some random 0xff byte; discard it and continue searching */ @@ -123,11 +124,11 @@ adts_song_duration(DecoderBuffer &buffer) break; if (frames == 0) { - auto data = ConstBuffer::FromVoid(buffer.Read()); + auto data = FromBytesStrict(buffer.Read()); assert(!data.empty()); - assert(frame_length <= data.size); + assert(frame_length <= data.size()); - sample_rate = adts_sample_rates[(data.data[2] & 0x3c) >> 2]; + sample_rate = adts_sample_rates[(data[2] & 0x3c) >> 2]; if (sample_rate == 0) break; } @@ -162,28 +163,28 @@ adts_song_duration(DecoderBuffer &buffer) static SignedSongTime faad_song_duration(DecoderBuffer &buffer, InputStream &is) { - auto data = ConstBuffer::FromVoid(buffer.Need(5)); - if (data.IsNull()) + auto data = FromBytesStrict(buffer.Need(5)); + if (data.data() == nullptr) return SignedSongTime::Negative(); size_t tagsize = 0; - if (data.size >= 10 && !memcmp(data.data, "ID3", 3)) { + if (data.size() >= 10 && !memcmp(data.data(), "ID3", 3)) { /* skip the ID3 tag */ - tagsize = (data.data[6] << 21) | (data.data[7] << 14) | - (data.data[8] << 7) | (data.data[9] << 0); + tagsize = (data[6] << 21) | (data[7] << 14) | + (data[8] << 7) | (data[9] << 0); tagsize += 10; if (!buffer.Skip(tagsize)) return SignedSongTime::Negative(); - data = ConstBuffer::FromVoid(buffer.Need(5)); - if (data.IsNull()) + data = FromBytesStrict(buffer.Need(5)); + if (data.data() == nullptr) return SignedSongTime::Negative(); } - if (data.size >= 8 && adts_check_frame(data.data) > 0) { + if (data.size() >= 8 && adts_check_frame(data.data()) > 0) { /* obtain the duration from the ADTS header */ if (!is.IsSeekable()) @@ -199,23 +200,23 @@ faad_song_duration(DecoderBuffer &buffer, InputStream &is) buffer.Clear(); return song_length; - } else if (data.size >= 5 && memcmp(data.data, "ADIF", 4) == 0) { + } else if (data.size() >= 5 && memcmp(data.data(), "ADIF", 4) == 0) { /* obtain the duration from the ADIF header */ if (!is.KnownSize()) return SignedSongTime::Negative(); - size_t skip_size = (data.data[4] & 0x80) ? 9 : 0; + size_t skip_size = (data[4] & 0x80) ? 9 : 0; - if (8 + skip_size > data.size) + if (8 + skip_size > data.size()) /* not enough data yet; skip parsing this header */ return SignedSongTime::Negative(); - unsigned bit_rate = ((data.data[4 + skip_size] & 0x0F) << 19) | - (data.data[5 + skip_size] << 11) | - (data.data[6 + skip_size] << 3) | - (data.data[7 + skip_size] & 0xE0); + unsigned bit_rate = ((data[4 + skip_size] & 0x0F) << 19) | + (data[5 + skip_size] << 11) | + (data[6 + skip_size] << 3) | + (data[7 + skip_size] & 0xE0); const auto size = is.GetSize(); if (bit_rate == 0) @@ -251,7 +252,7 @@ static void faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer &buffer, AudioFormat &audio_format) { - auto data = ConstBuffer::FromVoid(buffer.Read()); + auto data = FromBytesStrict(buffer.Read()); if (data.empty()) throw std::runtime_error("Empty file"); @@ -259,8 +260,8 @@ faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer &buffer, unsigned long sample_rate; long nbytes = NeAACDecInit(decoder, /* deconst hack, libfaad requires this */ - const_cast(data.data), - data.size, + const_cast(data.data()), + data.size(), &sample_rate, &channels); if (nbytes < 0) throw std::runtime_error("Not an AAC stream"); @@ -279,14 +280,14 @@ static const void * faad_decoder_decode(NeAACDecHandle decoder, DecoderBuffer &buffer, NeAACDecFrameInfo *frame_info) { - auto data = ConstBuffer::FromVoid(buffer.Read()); + auto data = FromBytesStrict(buffer.Read()); if (data.empty()) return nullptr; return NeAACDecDecode(decoder, frame_info, /* deconst hack, libfaad requires this */ - const_cast(data.data), - data.size); + const_cast(data.data()), + data.size()); } /** diff --git a/src/decoder/plugins/FfmpegDecoderPlugin.cxx b/src/decoder/plugins/FfmpegDecoderPlugin.cxx index 87e80289e..384e31fcc 100644 --- a/src/decoder/plugins/FfmpegDecoderPlugin.cxx +++ b/src/decoder/plugins/FfmpegDecoderPlugin.cxx @@ -42,7 +42,6 @@ #include "input/InputStream.hxx" #include "pcm/CheckAudioFormat.hxx" #include "util/ScopeExit.hxx" -#include "util/ConstBuffer.hxx" #include "util/StringAPI.hxx" #include "Log.hxx" @@ -216,23 +215,20 @@ FfmpegSendFrame(DecoderClient &client, InputStream *is, size_t &skip_bytes, FfmpegBuffer &buffer) { - ConstBuffer output_buffer = - Ffmpeg::InterleaveFrame(frame, buffer); + auto output_buffer = Ffmpeg::InterleaveFrame(frame, buffer); if (skip_bytes > 0) { - if (skip_bytes >= output_buffer.size) { - skip_bytes -= output_buffer.size; + if (skip_bytes >= output_buffer.size()) { + skip_bytes -= output_buffer.size(); return DecoderCommand::NONE; } - output_buffer.data = - (const uint8_t *)output_buffer.data + skip_bytes; - output_buffer.size -= skip_bytes; + output_buffer = output_buffer.subspan(skip_bytes); skip_bytes = 0; } return client.SubmitData(is, - output_buffer.data, output_buffer.size, + output_buffer.data(), output_buffer.size(), codec_context.bit_rate / 1000); } diff --git a/src/decoder/plugins/FlacCommon.hxx b/src/decoder/plugins/FlacCommon.hxx index 533c38307..f5c5a5809 100644 --- a/src/decoder/plugins/FlacCommon.hxx +++ b/src/decoder/plugins/FlacCommon.hxx @@ -27,10 +27,12 @@ #include "FlacInput.hxx" #include "FlacPcm.hxx" #include "../DecoderAPI.hxx" -#include "util/ConstBuffer.hxx" #include +#include +#include + struct FlacDecoder : public FlacInput { /** * Has DecoderClient::Ready() been called yet? @@ -63,7 +65,7 @@ struct FlacDecoder : public FlacInput { * If this is non-empty, then DecoderBridge::SubmitData() * should be called. */ - ConstBuffer chunk = nullptr; + std::span chunk = {}; FlacDecoder(DecoderClient &_client, InputStream &_input_stream) :FlacInput(_input_stream, &_client) {} diff --git a/src/decoder/plugins/FlacDecoderPlugin.cxx b/src/decoder/plugins/FlacDecoderPlugin.cxx index 80d124127..d2e84b388 100644 --- a/src/decoder/plugins/FlacDecoderPlugin.cxx +++ b/src/decoder/plugins/FlacDecoderPlugin.cxx @@ -153,10 +153,10 @@ FlacSubmitToClient(DecoderClient &client, FlacDecoder &d) noexcept if (!d.chunk.empty()) { auto cmd = client.SubmitData(d.GetInputStream(), - d.chunk.data, - d.chunk.size, + d.chunk.data(), + d.chunk.size(), d.kbit_rate); - d.chunk = nullptr; + d.chunk = {}; if (cmd != DecoderCommand::NONE) return cmd; } diff --git a/src/decoder/plugins/FlacPcm.cxx b/src/decoder/plugins/FlacPcm.cxx index 94e4c2ac2..9d4d0435c 100644 --- a/src/decoder/plugins/FlacPcm.cxx +++ b/src/decoder/plugins/FlacPcm.cxx @@ -21,7 +21,6 @@ #include "pcm/CheckAudioFormat.hxx" #include "lib/xiph/FlacAudioFormat.hxx" #include "util/RuntimeError.hxx" -#include "util/ConstBuffer.hxx" #include @@ -69,7 +68,7 @@ FlacImport(T *dest, const FLAC__int32 *const src[], size_t n_frames, } template -static ConstBuffer +static std::span FlacImport(PcmBuffer &buffer, const FLAC__int32 *const src[], size_t n_frames, unsigned n_channels) { @@ -77,10 +76,10 @@ FlacImport(PcmBuffer &buffer, const FLAC__int32 *const src[], size_t n_frames, size_t dest_size = n_samples * sizeof(T); T *dest = (T *)buffer.Get(dest_size); FlacImport(dest, src, n_frames, n_channels); - return {dest, dest_size}; + return std::as_bytes(std::span{dest, n_samples}); } -ConstBuffer +std::span FlacPcmImport::Import(const FLAC__int32 *const src[], size_t n_frames) { switch (audio_format.format) { diff --git a/src/decoder/plugins/FlacPcm.hxx b/src/decoder/plugins/FlacPcm.hxx index ea0363d04..3f84a3b15 100644 --- a/src/decoder/plugins/FlacPcm.hxx +++ b/src/decoder/plugins/FlacPcm.hxx @@ -25,7 +25,8 @@ #include -template struct ConstBuffer; +#include +#include /** * This class imports libFLAC PCM data into a PCM format supported by @@ -47,8 +48,8 @@ public: return audio_format; } - ConstBuffer Import(const FLAC__int32 *const src[], - size_t n_frames); + std::span Import(const FLAC__int32 *const src[], + size_t n_frames); }; #endif diff --git a/src/lib/ffmpeg/Interleave.cxx b/src/lib/ffmpeg/Interleave.cxx index bb83081ed..f391e3f39 100644 --- a/src/lib/ffmpeg/Interleave.cxx +++ b/src/lib/ffmpeg/Interleave.cxx @@ -32,7 +32,7 @@ extern "C" { namespace Ffmpeg { -ConstBuffer +std::span InterleaveFrame(const AVFrame &frame, FfmpegBuffer &buffer) { assert(frame.nb_samples > 0); @@ -49,9 +49,9 @@ InterleaveFrame(const AVFrame &frame, FfmpegBuffer &buffer) if (data_size < 0) throw MakeFfmpegError(data_size); - void *output_buffer; + std::byte *output_buffer; if (av_sample_fmt_is_planar(format) && channels > 1) { - output_buffer = buffer.GetT(data_size); + output_buffer = buffer.GetT(data_size); if (output_buffer == nullptr) /* Not enough memory - shouldn't happen */ throw std::bad_alloc(); @@ -61,7 +61,7 @@ InterleaveFrame(const AVFrame &frame, FfmpegBuffer &buffer) n_frames, av_get_bytes_per_sample(format)); } else { - output_buffer = frame.extended_data[0]; + output_buffer = (std::byte *)frame.extended_data[0]; } return { output_buffer, (size_t)data_size }; diff --git a/src/lib/ffmpeg/Interleave.hxx b/src/lib/ffmpeg/Interleave.hxx index 33d0c0312..b2bf59e30 100644 --- a/src/lib/ffmpeg/Interleave.hxx +++ b/src/lib/ffmpeg/Interleave.hxx @@ -20,8 +20,9 @@ #ifndef MPD_FFMPEG_INTERLEAVE_HXX #define MPD_FFMPEG_INTERLEAVE_HXX +#include + struct AVFrame; -template struct ConstBuffer; class FfmpegBuffer; namespace Ffmpeg { @@ -32,7 +33,7 @@ namespace Ffmpeg { * * Throws on error. */ -ConstBuffer +std::span InterleaveFrame(const AVFrame &frame, FfmpegBuffer &buffer); } // namespace Ffmpeg