decoder/*: use std::span instead of ConstBuffer

This commit is contained in:
Max Kellermann 2022-07-04 15:14:08 +02:00
parent 4ce1dae673
commit 9675cc77e2
9 changed files with 58 additions and 59 deletions

View File

@ -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>

View File

@ -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>
@ -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 */
@ -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;
}
@ -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())
@ -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<uint8_t>::FromVoid(buffer.Read());
auto data = FromBytesStrict<const uint8_t>(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<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");
@ -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());
}
/**

View File

@ -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<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);
}

View File

@ -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?
@ -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) {}

View File

@ -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;
}

View File

@ -21,7 +21,6 @@
#include "pcm/CheckAudioFormat.hxx"
#include "lib/xiph/FlacAudioFormat.hxx"
#include "util/RuntimeError.hxx"
#include "util/ConstBuffer.hxx"
#include <cassert>
@ -69,7 +68,7 @@ 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)
{
@ -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<void>
std::span<const std::byte>
FlacPcmImport::Import(const FLAC__int32 *const src[], size_t n_frames)
{
switch (audio_format.format) {

View File

@ -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
@ -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

View File

@ -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);
@ -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();
@ -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 };

View File

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