input/InputStream: pass std::span<std::byte> to Read()

This commit is contained in:
Max Kellermann
2024-05-13 10:47:57 +02:00
parent f28d10d934
commit 34f7b38f39
66 changed files with 248 additions and 274 deletions

View File

@@ -395,13 +395,12 @@ DecoderBridge::OpenUri(const char *uri)
}
size_t
DecoderBridge::Read(InputStream &is, void *buffer, size_t length) noexcept
DecoderBridge::Read(InputStream &is, std::span<std::byte> dest) noexcept
try {
assert(buffer != nullptr);
assert(dc.state == DecoderState::START ||
dc.state == DecoderState::DECODE);
if (length == 0)
if (dest.empty())
return 0;
std::unique_lock<Mutex> lock(is.mutex);
@@ -416,7 +415,7 @@ try {
dc.cond.wait(lock);
}
size_t nbytes = is.Read(lock, buffer, length);
size_t nbytes = is.Read(lock, dest);
assert(nbytes > 0 || is.IsEOF());
return nbytes;

View File

@@ -1,15 +1,16 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
#ifndef MPD_DECODER_BRIDGE_HXX
#define MPD_DECODER_BRIDGE_HXX
#pragma once
#include "Client.hxx"
#include "tag/ReplayGainInfo.hxx"
#include "MusicChunkPtr.hxx"
#include <cstddef>
#include <exception>
#include <memory>
#include <span>
class PcmConvert;
struct MusicChunk;
@@ -159,7 +160,7 @@ public:
void SeekError() noexcept override;
InputStreamPtr OpenUri(const char *uri) override;
size_t Read(InputStream &is,
void *buffer, size_t length) noexcept override;
std::span<std::byte> dest) noexcept override;
void SubmitTimestamp(FloatDuration t) noexcept override;
DecoderCommand SubmitAudio(InputStream *is,
std::span<const std::byte> audio,
@@ -191,5 +192,3 @@ private:
bool UpdateStreamTag(InputStream *is) noexcept;
};
#endif

View File

@@ -1,8 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
#ifndef MPD_DECODER_CLIENT_HXX
#define MPD_DECODER_CLIENT_HXX
#pragma once
#include "Command.hxx"
#include "Chrono.hxx"
@@ -93,7 +92,7 @@ public:
* occurs: end of file; error; command (like SEEK or STOP).
*/
virtual size_t Read(InputStream &is,
void *buffer, size_t length) noexcept = 0;
std::span<std::byte> dest) noexcept = 0;
/**
* Sets the time stamp for the next data chunk [seconds]. The MPD
@@ -171,5 +170,3 @@ public:
*/
virtual void SubmitMixRamp(MixRampInfo &&mix_ramp) noexcept = 0;
};
#endif

View File

@@ -10,16 +10,14 @@
size_t
decoder_read(DecoderClient *client,
InputStream &is,
void *buffer, size_t length) noexcept
std::span<std::byte> dest) noexcept
{
assert(buffer != nullptr);
/* XXX don't allow client==nullptr */
if (client != nullptr)
return client->Read(is, buffer, length);
return client->Read(is, dest);
try {
return is.LockRead(buffer, length);
return is.LockRead(dest);
} catch (...) {
LogError(std::current_exception());
return 0;
@@ -28,20 +26,17 @@ decoder_read(DecoderClient *client,
size_t
decoder_read_much(DecoderClient *client, InputStream &is,
void *_buffer, size_t size) noexcept
std::span<std::byte> dest) noexcept
{
auto buffer = (uint8_t *)_buffer;
size_t total = 0;
while (size > 0 && !is.LockIsEOF()) {
size_t nbytes = decoder_read(client, is, buffer, size);
while (!dest.empty() && !is.LockIsEOF()) {
size_t nbytes = decoder_read(client, is, dest);
if (nbytes == 0)
return false;
dest = dest.subspan(nbytes);
total += nbytes;
buffer += nbytes;
size -= nbytes;
}
return total;
@@ -49,17 +44,14 @@ decoder_read_much(DecoderClient *client, InputStream &is,
bool
decoder_read_full(DecoderClient *client, InputStream &is,
void *_buffer, size_t size) noexcept
std::span<std::byte> dest) noexcept
{
auto buffer = (uint8_t *)_buffer;
while (size > 0) {
size_t nbytes = decoder_read(client, is, buffer, size);
while (!dest.empty()) {
size_t nbytes = decoder_read(client, is, dest);
if (nbytes == 0)
return false;
buffer += nbytes;
size -= nbytes;
dest = dest.subspan(nbytes);
}
return true;
@@ -69,9 +61,10 @@ bool
decoder_skip(DecoderClient *client, InputStream &is, size_t size) noexcept
{
while (size > 0) {
char buffer[1024];
size_t nbytes = decoder_read(client, is, buffer,
std::min(sizeof(buffer), size));
std::byte buffer[1024];
size_t nbytes = decoder_read(client, is,
std::span{buffer, std::min(sizeof(buffer), size)});
if (nbytes == 0)
return false;

View File

@@ -1,6 +1,8 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
#pragma once
/*! \file
* \brief The MPD Decoder API
*
@@ -8,9 +10,6 @@
* communicate with the mpd core.
*/
#ifndef MPD_DECODER_API_HXX
#define MPD_DECODER_API_HXX
// IWYU pragma: begin_exports
#include "Client.hxx"
@@ -26,7 +25,9 @@
// IWYU pragma: end_exports
#include <cstddef>
#include <cstdint>
#include <span>
/**
* Throw an instance of this class to stop decoding the current song
@@ -47,13 +48,13 @@ class StopDecoder {};
*/
size_t
decoder_read(DecoderClient *decoder, InputStream &is,
void *buffer, size_t length) noexcept;
std::span<std::byte> dest) noexcept;
static inline size_t
decoder_read(DecoderClient &decoder, InputStream &is,
void *buffer, size_t length) noexcept
std::span<std::byte> dest) noexcept
{
return decoder_read(&decoder, is, buffer, length);
return decoder_read(&decoder, is, dest);
}
/**
@@ -66,7 +67,7 @@ decoder_read(DecoderClient &decoder, InputStream &is,
*/
size_t
decoder_read_much(DecoderClient *decoder, InputStream &is,
void *buffer, size_t size) noexcept;
std::span<std::byte> dest) noexcept;
/**
* Blocking read from the input stream. Attempts to fill the buffer
@@ -77,7 +78,7 @@ decoder_read_much(DecoderClient *decoder, InputStream &is,
*/
bool
decoder_read_full(DecoderClient *decoder, InputStream &is,
void *buffer, size_t size) noexcept;
std::span<std::byte> dest) noexcept;
/**
* Skip data on the #InputStream.
@@ -86,5 +87,3 @@ decoder_read_full(DecoderClient *decoder, InputStream &is,
*/
bool
decoder_skip(DecoderClient *decoder, InputStream &is, size_t size) noexcept;
#endif

View File

@@ -12,8 +12,7 @@ DecoderBuffer::Fill()
/* buffer is full */
return false;
size_t nbytes = decoder_read(client, is,
w.data(), w.size());
size_t nbytes = decoder_read(client, is, w);
if (nbytes == 0)
/* end of file, I/O error or decoder command
received */

View File

@@ -7,5 +7,5 @@
std::size_t
DecoderReader::Read(std::span<std::byte> dest)
{
return decoder_read(client, is, dest.data(), dest.size());
return decoder_read(client, is, dest);
}

View File

@@ -42,7 +42,7 @@ struct AudioFileInputStream {
/* libaudiofile does not like partial reads at all,
and will abort playback; therefore always force full
reads */
return decoder_read_full(client, is, buffer, size)
return decoder_read_full(client, is, {reinterpret_cast<std::byte *>(buffer), size})
? size
: 0;
}

View File

@@ -121,7 +121,8 @@ dsdlib_tag_id3(InputStream &is, TagHandler &handler,
if (id3_buf == nullptr)
return;
if (!decoder_read_full(nullptr, is, id3_buf, count)) {
if (!decoder_read_full(nullptr, is,
{reinterpret_cast<std::byte *>(id3_buf), count})) {
delete[] id3_buf;
return;
}

View File

@@ -17,6 +17,7 @@
#include "pcm/CheckAudioFormat.hxx"
#include "util/BitReverse.hxx"
#include "util/PackedBigEndian.hxx"
#include "util/SpanCast.hxx"
#include "tag/Handler.hxx"
#include "DsdLib.hxx"
@@ -75,26 +76,26 @@ static bool
dsdiff_read_id(DecoderClient *client, InputStream &is,
DsdId &id)
{
return decoder_read_full(client, is, &id, sizeof(id));
return decoder_read_full(client, is, ReferenceAsWritableBytes(id));
}
static bool
dsdiff_read_chunk_header(DecoderClient *client, InputStream &is,
DsdiffChunkHeader &header)
{
return decoder_read_full(client, is, &header, sizeof(header));
return decoder_read_full(client, is, ReferenceAsWritableBytes(header));
}
static bool
dsdiff_read_payload(DecoderClient *client, InputStream &is,
const DsdiffChunkHeader &header,
void *data, size_t length)
std::span<std::byte> dest)
{
uint64_t size = header.GetSize();
if (size != (uint64_t)length)
if (size != (uint64_t)dest.size())
return false;
return decoder_read_full(client, is, data, length);
return decoder_read_full(client, is, dest);
}
/**
@@ -118,8 +119,7 @@ dsdiff_read_prop_snd(DecoderClient *client, InputStream &is,
if (header.id.Equals("FS ")) {
uint32_t sample_rate;
if (!dsdiff_read_payload(client, is, header,
&sample_rate,
sizeof(sample_rate)))
ReferenceAsWritableBytes(sample_rate)))
return false;
metadata.sample_rate = FromBE32(sample_rate);
@@ -127,7 +127,7 @@ dsdiff_read_prop_snd(DecoderClient *client, InputStream &is,
uint16_t channels;
if (header.GetSize() < sizeof(channels) ||
!decoder_read_full(client, is,
&channels, sizeof(channels)) ||
ReferenceAsWritableBytes(channels)) ||
!dsdlib_skip_to(client, is, chunk_end_offset))
return false;
@@ -136,7 +136,7 @@ dsdiff_read_prop_snd(DecoderClient *client, InputStream &is,
DsdId type;
if (header.GetSize() < sizeof(type) ||
!decoder_read_full(client, is,
&type, sizeof(type)) ||
ReferenceAsWritableBytes(type)) ||
!dsdlib_skip_to(client, is, chunk_end_offset))
return false;
@@ -189,7 +189,7 @@ dsdiff_handle_native_tag(DecoderClient *client, InputStream &is,
struct dsdiff_native_tag metatag;
if (!decoder_read_full(client, is, &metatag, sizeof(metatag)))
if (!decoder_read_full(client, is, ReferenceAsWritableBytes(metatag)))
return;
uint32_t length = FromBE32(metatag.size);
@@ -203,7 +203,8 @@ dsdiff_handle_native_tag(DecoderClient *client, InputStream &is,
char *label;
label = string;
if (!decoder_read_full(client, is, label, (size_t)length))
if (!decoder_read_full(client, is,
{reinterpret_cast<std::byte *>(label), (size_t)length}))
return;
handler.OnTag(type, {label, length});
@@ -304,7 +305,7 @@ dsdiff_read_metadata(DecoderClient *client, InputStream &is,
DsdiffChunkHeader &chunk_header)
{
DsdiffHeader header;
if (!decoder_read_full(client, is, &header, sizeof(header)) ||
if (!decoder_read_full(client, is, ReferenceAsWritableBytes(header)) ||
!header.id.Equals("FRM8") ||
!header.format.Equals("DSD "))
return false;
@@ -392,7 +393,8 @@ dsdiff_decode_chunk(DecoderClient &client, InputStream &is,
now_size = now_frames * frame_size;
}
if (!decoder_read_full(&client, is, buffer, now_size))
if (!decoder_read_full(&client, is,
std::span{buffer}.first(now_size)))
return false;
const size_t nbytes = now_size;

View File

@@ -18,6 +18,7 @@
#include "pcm/CheckAudioFormat.hxx"
#include "util/BitReverse.hxx"
#include "util/PackedLittleEndian.hxx"
#include "util/SpanCast.hxx"
#include "DsdLib.hxx"
#include "tag/Handler.hxx"
@@ -85,7 +86,7 @@ dsf_read_metadata(DecoderClient *client, InputStream &is,
DsfMetaData *metadata)
{
DsfHeader dsf_header;
if (!decoder_read_full(client, is, &dsf_header, sizeof(dsf_header)) ||
if (!decoder_read_full(client, is, ReferenceAsWritableBytes(dsf_header)) ||
!dsf_header.id.Equals("DSD "))
return false;
@@ -100,7 +101,7 @@ dsf_read_metadata(DecoderClient *client, InputStream &is,
/* read the 'fmt ' chunk of the DSF file */
DsfFmtChunk dsf_fmt_chunk;
if (!decoder_read_full(client, is,
&dsf_fmt_chunk, sizeof(dsf_fmt_chunk)) ||
ReferenceAsWritableBytes(dsf_fmt_chunk)) ||
!dsf_fmt_chunk.id.Equals("fmt "))
return false;
@@ -127,7 +128,7 @@ dsf_read_metadata(DecoderClient *client, InputStream &is,
/* read the 'data' chunk of the DSF file */
DsfDataChunk data_chunk;
if (!decoder_read_full(client, is, &data_chunk, sizeof(data_chunk)) ||
if (!decoder_read_full(client, is, ReferenceAsWritableBytes(data_chunk)) ||
!data_chunk.id.Equals("data"))
return false;
@@ -264,7 +265,8 @@ dsf_decode_chunk(DecoderClient &client, InputStream &is,
/* worst-case buffer size */
std::byte buffer[MAX_CHANNELS * DSF_BLOCK_SIZE];
if (!decoder_read_full(&client, is, buffer, block_size))
if (!decoder_read_full(&client, is,
std::span{buffer}.first(block_size)))
return false;
if (bitreverse)

View File

@@ -24,9 +24,9 @@ AvioStream::~AvioStream()
}
inline int
AvioStream::Read(void *dest, int size)
AvioStream::Read(std::span<std::byte> dest)
{
const auto nbytes = decoder_read(client, input, dest, size);
const auto nbytes = decoder_read(client, input, dest);
if (nbytes == 0)
return AVERROR_EOF;
@@ -74,7 +74,7 @@ AvioStream::_Read(void *opaque, uint8_t *buf, int size)
{
AvioStream &stream = *(AvioStream *)opaque;
return stream.Read(buf, size);
return stream.Read({reinterpret_cast<std::byte *>(buf), static_cast<std::size_t>(size)});
}
int64_t

View File

@@ -1,14 +1,15 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
#ifndef MPD_FFMPEG_IO_HXX
#define MPD_FFMPEG_IO_HXX
#pragma once
extern "C" {
#include "libavformat/avio.h"
}
#include <cstddef>
#include <cstdint>
#include <span>
class DecoderClient;
class InputStream;
@@ -27,11 +28,9 @@ struct AvioStream {
bool Open();
private:
int Read(void *buffer, int size);
int Read(std::span<std::byte> dest);
int64_t Seek(int64_t pos, int whence);
static int _Read(void *opaque, uint8_t *buf, int size);
static int64_t _Seek(void *opaque, int64_t pos, int whence);
};
#endif

View File

@@ -12,7 +12,8 @@
inline FLAC__StreamDecoderReadStatus
FlacInput::Read(FLAC__byte buffer[], size_t *bytes) noexcept
{
size_t r = decoder_read(client, input_stream, (void *)buffer, *bytes);
size_t r = decoder_read(client, input_stream,
{reinterpret_cast<std::byte *>(buffer), *bytes});
*bytes = r;
if (r == 0) {

View File

@@ -237,7 +237,7 @@ MadDecoder::FillBuffer() noexcept
return false;
size_t nbytes = decoder_read(client, input_stream,
dest, max_read_size);
{reinterpret_cast<std::byte *>(dest), max_read_size});
if (nbytes == 0) {
if (was_eof || max_read_size < MAD_BUFFER_GUARD)
return false;
@@ -271,7 +271,7 @@ MadDecoder::ParseId3(size_t tagsize, Tag *mpd_tag) noexcept
mad_stream_skip(&(stream), count);
if (!decoder_read_full(client, input_stream,
allocated.get() + count, tagsize - count)) {
{reinterpret_cast<std::byte *>(allocated.get() + count), tagsize - count})) {
LogDebug(mad_domain, "error parsing ID3 tag");
return;
}

View File

@@ -39,7 +39,7 @@ mod_loadfile(const Domain *domain, DecoderClient *client, InputStream &is)
std::byte *const end = p + buffer.size();
while (true) {
size_t ret = decoder_read(client, is, p, end - p);
size_t ret = decoder_read(client, is, {p, end});
if (ret == 0) {
if (is.LockIsEOF())
/* end of file */

View File

@@ -36,7 +36,8 @@ mpc_read_cb(mpc_reader *reader, void *ptr, mpc_int32_t size)
auto *data =
(struct mpc_decoder_data *)reader->data;
return decoder_read(data->client, data->is, ptr, size);
return decoder_read(data->client, data->is,
{reinterpret_cast<std::byte *>(ptr), static_cast<std::size_t>(size)});
}
static mpc_bool_t

View File

@@ -77,7 +77,8 @@ mpd_mpg123_read(void *_iohandle, void *data, size_t size) noexcept
auto &iohandle = *reinterpret_cast<mpd_mpg123_iohandle *>(_iohandle);
try {
return decoder_read_much(iohandle.client, iohandle.is, data, size);
return decoder_read_much(iohandle.client, iohandle.is,
{reinterpret_cast<std::byte *>(data), size});
} catch (...) {
LogError(std::current_exception(), "Read failed");
return -1;

View File

@@ -16,8 +16,8 @@ ogg_codec_detect(DecoderClient *client, InputStream &is)
/* oggflac detection based on code in ogg123 and this post
* http://lists.xiph.org/pipermail/flac/2004-December/000393.html
* ogg123 trunk still doesn't have this patch as of June 2005 */
unsigned char buf[41];
size_t r = decoder_read(client, is, buf, sizeof(buf));
std::byte buf[41];
size_t r = decoder_read(client, is, buf);
if (r < sizeof(buf) || memcmp(buf, "OggS", 4) != 0)
return OGG_CODEC_UNKNOWN;

View File

@@ -35,7 +35,7 @@ FillBuffer(DecoderClient &client, InputStream &is, B &buffer)
if (w.empty())
return true;
size_t nbytes = decoder_read(client, is, w.data(), w.size());
size_t nbytes = decoder_read(client, is, w);
if (nbytes == 0 && is.LockIsEOF())
return false;
@@ -157,7 +157,7 @@ pcm_stream_decode(DecoderClient &client, InputStream &is)
client.Ready(audio_format, is.IsSeekable(), total_time);
StaticFifoBuffer<uint8_t, 4096> buffer;
StaticFifoBuffer<std::byte, 4096> buffer;
/* a buffer for pcm_unpack_24be() large enough to hold the
results for a full source buffer */
@@ -185,9 +185,10 @@ pcm_stream_decode(DecoderClient &client, InputStream &is)
(audio/L24) to native-endian 24 bit (in 32
bit integers) */
pcm_unpack_24be(unpack_buffer,
r.data(), r.data() + r.size());
reinterpret_cast<const uint8_t *>(r.data()),
reinterpret_cast<const uint8_t *>(r.data() + r.size()));
r = {
(uint8_t *)&unpack_buffer[0],
(std::byte *)&unpack_buffer[0],
(r.size() / 3) * 4,
};
}

View File

@@ -30,7 +30,8 @@ struct SndfileInputStream {
size_t Read(void *buffer, size_t size) {
/* libsndfile chokes on partial reads; therefore
always force full reads */
return decoder_read_much(client, is, buffer, size);
return decoder_read_much(client, is,
{reinterpret_cast<std::byte *>(buffer), size});
}
};

View File

@@ -245,7 +245,7 @@ struct WavpackInput {
InputStream &_is) noexcept
:client(_client), is(_is), last_byte(EOF) {}
int32_t ReadBytes(void *data, size_t bcount) noexcept;
int32_t ReadBytes(std::span<std::byte> dest) noexcept;
[[nodiscard]] InputStream::offset_type GetPos() const noexcept {
return is.GetOffset();
@@ -318,34 +318,32 @@ wpin(void *id) noexcept
static int32_t
wavpack_input_read_bytes(void *id, void *data, int32_t bcount) noexcept
{
return wpin(id)->ReadBytes(data, bcount);
return wpin(id)->ReadBytes({reinterpret_cast<std::byte *>(data), static_cast<std::size_t>(bcount)});
}
int32_t
WavpackInput::ReadBytes(void *data, size_t bcount) noexcept
WavpackInput::ReadBytes(std::span<std::byte> dest) noexcept
{
auto *buf = (uint8_t *)data;
int32_t i = 0;
if (last_byte != EOF) {
*buf++ = last_byte;
dest.front() = static_cast<std::byte>(last_byte);
dest = dest.subspan(1);
last_byte = EOF;
--bcount;
++i;
}
/* wavpack fails if we return a partial read, so we just wait
until the buffer is full */
while (bcount > 0) {
size_t nbytes = decoder_read(client, is, buf, bcount);
while (!dest.empty()) {
size_t nbytes = decoder_read(client, is, dest);
if (nbytes == 0) {
/* EOF, error or a decoder command */
break;
}
i += nbytes;
bcount -= nbytes;
buf += nbytes;
dest = dest.subspan(nbytes);
}
return i;