util/*FifoBuffer: migrate from WritableBuffer to std::span

This commit is contained in:
Max Kellermann
2022-05-10 17:26:41 +02:00
committed by Max Kellermann
parent 570755f05a
commit bb7be9a4cd
23 changed files with 114 additions and 129 deletions

View File

@@ -29,7 +29,7 @@ DecoderBuffer::Fill()
return false;
size_t nbytes = decoder_read(client, is,
w.data, w.size);
w.data(), w.size());
if (nbytes == 0)
/* end of file, I/O error or decoder command
received */
@@ -39,16 +39,16 @@ DecoderBuffer::Fill()
return true;
}
ConstBuffer<void>
std::span<const std::byte>
DecoderBuffer::Need(size_t min_size)
{
while (true) {
const auto r = Read();
if (r.size >= min_size)
if (r.size() >= min_size)
return r;
if (!Fill())
return nullptr;
return {};
}
}
@@ -56,13 +56,13 @@ bool
DecoderBuffer::Skip(size_t nbytes)
{
const auto r = buffer.Read();
if (r.size >= nbytes) {
if (r.size() >= nbytes) {
buffer.Consume(nbytes);
return true;
}
buffer.Clear();
nbytes -= r.size;
nbytes -= r.size();
return decoder_skip(client, is, nbytes);
}

View File

@@ -21,10 +21,6 @@
#define MPD_DECODER_BUFFER_HXX
#include "util/DynamicFifoBuffer.hxx"
#include "util/ConstBuffer.hxx"
#include <cstddef>
#include <cstdint>
class DecoderClient;
class InputStream;
@@ -38,7 +34,7 @@ class DecoderBuffer {
DecoderClient *const client;
InputStream &is;
DynamicFifoBuffer<uint8_t> buffer;
DynamicFifoBuffer<std::byte> buffer;
public:
/**
@@ -83,16 +79,15 @@ public:
* you have to call Consume() to do that. The returned buffer
* becomes invalid after a Fill() or a Consume() call.
*/
ConstBuffer<void> Read() const noexcept {
auto r = buffer.Read();
return { r.data, r.size };
std::span<const std::byte> Read() const noexcept {
return buffer.Read();
}
/**
* Wait until this number of bytes are available. Returns nullptr on
* error.
*/
ConstBuffer<void> Need(size_t min_size);
std::span<const std::byte> Need(size_t min_size);
/**
* Consume (delete, invalidate) a part of the buffer. The

View File

@@ -93,7 +93,7 @@ adts_find_frame(DecoderBuffer &buffer)
continue;
}
if (buffer.Need(frame_length).IsNull()) {
if (buffer.Need(frame_length).empty()) {
/* not enough data; discard this frame to
prevent a possible buffer overflow */
buffer.Clear();

View File

@@ -237,11 +237,11 @@ HybridDsdDecode(DecoderClient &client, InputStream &input)
auto w = buffer.Write();
if (!w.empty()) {
if (remaining_bytes < (1<<30ULL) &&
w.size > size_t(remaining_bytes))
w.size = remaining_bytes;
w.size() > size_t(remaining_bytes))
w = w.first(remaining_bytes);
const size_t nbytes = client.Read(input,
w.data, w.size);
w.data(), w.size());
if (nbytes == 0)
return;
@@ -251,9 +251,9 @@ HybridDsdDecode(DecoderClient &client, InputStream &input)
/* submit the buffer to our client */
auto r = buffer.Read();
auto n_frames = r.size / frame_size;
auto n_frames = r.size() / frame_size;
if (n_frames > 0) {
cmd = client.SubmitData(input, r.data,
cmd = client.SubmitData(input, r.data(),
n_frames * frame_size,
kbit_rate);
buffer.Consume(n_frames * frame_size);

View File

@@ -51,7 +51,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.data(), w.size());
if (nbytes == 0 && is.LockIsEOF())
return false;
@@ -188,25 +188,28 @@ pcm_stream_decode(DecoderClient &client, InputStream &is)
/* round down to the nearest frame size, because we
must not pass partial frames to
DecoderClient::SubmitData() */
r.size -= r.size % in_frame_size;
buffer.Consume(r.size);
r = r.first(r.size() - r.size() % in_frame_size);
buffer.Consume(r.size());
if (reverse_endian)
/* make sure we deliver samples in host byte order */
reverse_bytes_16((uint16_t *)r.data,
(uint16_t *)r.data,
(uint16_t *)(r.data + r.size));
reverse_bytes_16((uint16_t *)r.data(),
(uint16_t *)r.data(),
(uint16_t *)(r.data() + r.size()));
else if (l24) {
/* convert big-endian packed 24 bit
(audio/L24) to native-endian 24 bit (in 32
bit integers) */
pcm_unpack_24be(unpack_buffer, r.begin(), r.end());
r.data = (uint8_t *)&unpack_buffer[0];
r.size = (r.size / 3) * 4;
pcm_unpack_24be(unpack_buffer,
r.data(), r.data() + r.size());
r = {
(uint8_t *)&unpack_buffer[0],
(r.size() / 3) * 4,
};
}
cmd = !r.empty()
? client.SubmitData(is, r.data, r.size, 0)
? client.SubmitData(is, r.data(), r.size(), 0)
: client.GetCommand();
if (cmd == DecoderCommand::SEEK) {
uint64_t frame = client.GetSeekFrame();