Skip to content

Commit

Permalink
decoder/*: use std::span instead of ConstBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Jul 4, 2022
1 parent 4ce1dae commit 9675cc7
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 59 deletions.
1 change: 0 additions & 1 deletion src/decoder/Bridge.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cassert>
Expand Down
63 changes: 32 additions & 31 deletions src/decoder/plugins/FaadDecoderPlugin.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cassert>
Expand Down Expand Up @@ -65,27 +65,28 @@ static size_t
adts_find_frame(DecoderBuffer &buffer)
{
while (true) {
auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Need(8));
if (data.IsNull())
auto data = FromBytesStrict<const uint8_t>(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 */
Expand Down Expand Up @@ -123,11 +124,11 @@ adts_song_duration(DecoderBuffer &buffer)
break;

if (frames == 0) {
auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Read());
auto data = FromBytesStrict<const uint8_t>(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;
}
Expand Down Expand Up @@ -162,28 +163,28 @@ adts_song_duration(DecoderBuffer &buffer)
static SignedSongTime
faad_song_duration(DecoderBuffer &buffer, InputStream &is)
{
auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Need(5));
if (data.IsNull())
auto data = FromBytesStrict<const uint8_t>(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<uint8_t>::FromVoid(buffer.Need(5));
if (data.IsNull())
data = FromBytesStrict<const uint8_t>(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())
Expand All @@ -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)
Expand Down Expand Up @@ -251,16 +252,16 @@ static void
faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer &buffer,
AudioFormat &audio_format)
{
auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Read());
auto data = FromBytesStrict<const uint8_t>(buffer.Read());
if (data.empty())
throw std::runtime_error("Empty file");

uint8_t channels;
unsigned long sample_rate;
long nbytes = NeAACDecInit(decoder,
/* deconst hack, libfaad requires this */
const_cast<unsigned char *>(data.data),
data.size,
const_cast<unsigned char *>(data.data()),
data.size(),
&sample_rate, &channels);
if (nbytes < 0)
throw std::runtime_error("Not an AAC stream");
Expand All @@ -279,14 +280,14 @@ static const void *
faad_decoder_decode(NeAACDecHandle decoder, DecoderBuffer &buffer,
NeAACDecFrameInfo *frame_info)
{
auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Read());
auto data = FromBytesStrict<const uint8_t>(buffer.Read());
if (data.empty())
return nullptr;

return NeAACDecDecode(decoder, frame_info,
/* deconst hack, libfaad requires this */
const_cast<uint8_t *>(data.data),
data.size);
const_cast<uint8_t *>(data.data()),
data.size());
}

/**
Expand Down
14 changes: 5 additions & 9 deletions src/decoder/plugins/FfmpegDecoderPlugin.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -216,23 +215,20 @@ FfmpegSendFrame(DecoderClient &client, InputStream *is,
size_t &skip_bytes,
FfmpegBuffer &buffer)
{
ConstBuffer<void> 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);
}

Expand Down
6 changes: 4 additions & 2 deletions src/decoder/plugins/FlacCommon.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
#include "FlacInput.hxx"
#include "FlacPcm.hxx"
#include "../DecoderAPI.hxx"
#include "util/ConstBuffer.hxx"

#include <FLAC/stream_decoder.h>

#include <cstddef>
#include <span>

struct FlacDecoder : public FlacInput {
/**
* Has DecoderClient::Ready() been called yet?
Expand Down Expand Up @@ -63,7 +65,7 @@ struct FlacDecoder : public FlacInput {
* If this is non-empty, then DecoderBridge::SubmitData()
* should be called.
*/
ConstBuffer<void> chunk = nullptr;
std::span<const std::byte> chunk = {};

FlacDecoder(DecoderClient &_client, InputStream &_input_stream)
:FlacInput(_input_stream, &_client) {}
Expand Down
6 changes: 3 additions & 3 deletions src/decoder/plugins/FlacDecoderPlugin.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 3 additions & 4 deletions src/decoder/plugins/FlacPcm.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "pcm/CheckAudioFormat.hxx"
#include "lib/xiph/FlacAudioFormat.hxx"
#include "util/RuntimeError.hxx"
#include "util/ConstBuffer.hxx"

#include <cassert>

Expand Down Expand Up @@ -69,18 +68,18 @@ FlacImport(T *dest, const FLAC__int32 *const src[], size_t n_frames,
}

template<typename T>
static ConstBuffer<void>
static std::span<const std::byte>
FlacImport(PcmBuffer &buffer, const FLAC__int32 *const src[], size_t n_frames,
unsigned n_channels)
{
size_t n_samples = n_frames * n_channels;
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<void>
std::span<const std::byte>
FlacPcmImport::Import(const FLAC__int32 *const src[], size_t n_frames)
{
switch (audio_format.format) {
Expand Down
7 changes: 4 additions & 3 deletions src/decoder/plugins/FlacPcm.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

#include <FLAC/ordinals.h>

template<typename T> struct ConstBuffer;
#include <cstddef>
#include <span>

/**
* This class imports libFLAC PCM data into a PCM format supported by
Expand All @@ -47,8 +48,8 @@ public:
return audio_format;
}

ConstBuffer<void> Import(const FLAC__int32 *const src[],
size_t n_frames);
std::span<const std::byte> Import(const FLAC__int32 *const src[],
size_t n_frames);
};

#endif
8 changes: 4 additions & 4 deletions src/lib/ffmpeg/Interleave.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extern "C" {

namespace Ffmpeg {

ConstBuffer<void>
std::span<const std::byte>
InterleaveFrame(const AVFrame &frame, FfmpegBuffer &buffer)
{
assert(frame.nb_samples > 0);
Expand All @@ -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<uint8_t>(data_size);
output_buffer = buffer.GetT<std::byte>(data_size);
if (output_buffer == nullptr)
/* Not enough memory - shouldn't happen */
throw std::bad_alloc();
Expand All @@ -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 };
Expand Down
5 changes: 3 additions & 2 deletions src/lib/ffmpeg/Interleave.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
#ifndef MPD_FFMPEG_INTERLEAVE_HXX
#define MPD_FFMPEG_INTERLEAVE_HXX

#include <span>

struct AVFrame;
template<typename T> struct ConstBuffer;
class FfmpegBuffer;

namespace Ffmpeg {
Expand All @@ -32,7 +33,7 @@ namespace Ffmpeg {
*
* Throws on error.
*/
ConstBuffer<void>
std::span<const std::byte>
InterleaveFrame(const AVFrame &frame, FfmpegBuffer &buffer);

} // namespace Ffmpeg
Expand Down

0 comments on commit 9675cc7

Please sign in to comment.