decoder/*: use std::span instead of ConstBuffer
This commit is contained in:
parent
4ce1dae673
commit
9675cc77e2
@ -34,7 +34,6 @@
|
|||||||
#include "input/cache/Manager.hxx"
|
#include "input/cache/Manager.hxx"
|
||||||
#include "input/cache/Stream.hxx"
|
#include "input/cache/Stream.hxx"
|
||||||
#include "fs/Path.hxx"
|
#include "fs/Path.hxx"
|
||||||
#include "util/ConstBuffer.hxx"
|
|
||||||
#include "util/StringBuffer.hxx"
|
#include "util/StringBuffer.hxx"
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
@ -24,9 +24,9 @@
|
|||||||
#include "pcm/CheckAudioFormat.hxx"
|
#include "pcm/CheckAudioFormat.hxx"
|
||||||
#include "tag/Handler.hxx"
|
#include "tag/Handler.hxx"
|
||||||
#include "util/ScopeExit.hxx"
|
#include "util/ScopeExit.hxx"
|
||||||
#include "util/ConstBuffer.hxx"
|
|
||||||
#include "util/Domain.hxx"
|
#include "util/Domain.hxx"
|
||||||
#include "util/Math.hxx"
|
#include "util/Math.hxx"
|
||||||
|
#include "util/SpanCast.hxx"
|
||||||
#include "Log.hxx"
|
#include "Log.hxx"
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
@ -65,27 +65,28 @@ static size_t
|
|||||||
adts_find_frame(DecoderBuffer &buffer)
|
adts_find_frame(DecoderBuffer &buffer)
|
||||||
{
|
{
|
||||||
while (true) {
|
while (true) {
|
||||||
auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Need(8));
|
auto data = FromBytesStrict<const uint8_t>(buffer.Need(8));
|
||||||
if (data.IsNull())
|
if (data.data() == nullptr)
|
||||||
/* failed */
|
/* failed */
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* find the 0xff marker */
|
/* 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) {
|
if (p == nullptr) {
|
||||||
/* no marker - discard the buffer */
|
/* no marker - discard the buffer */
|
||||||
buffer.Clear();
|
buffer.Clear();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p > data.data) {
|
if (p > data.data()) {
|
||||||
/* discard data before 0xff */
|
/* discard data before 0xff */
|
||||||
buffer.Consume(p - data.data);
|
buffer.Consume(p - data.data());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* is it a frame? */
|
/* 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) {
|
if (frame_length == 0) {
|
||||||
/* it's just some random 0xff byte; discard it
|
/* it's just some random 0xff byte; discard it
|
||||||
and continue searching */
|
and continue searching */
|
||||||
@ -123,11 +124,11 @@ adts_song_duration(DecoderBuffer &buffer)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
if (frames == 0) {
|
if (frames == 0) {
|
||||||
auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Read());
|
auto data = FromBytesStrict<const uint8_t>(buffer.Read());
|
||||||
assert(!data.empty());
|
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)
|
if (sample_rate == 0)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -162,28 +163,28 @@ adts_song_duration(DecoderBuffer &buffer)
|
|||||||
static SignedSongTime
|
static SignedSongTime
|
||||||
faad_song_duration(DecoderBuffer &buffer, InputStream &is)
|
faad_song_duration(DecoderBuffer &buffer, InputStream &is)
|
||||||
{
|
{
|
||||||
auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Need(5));
|
auto data = FromBytesStrict<const uint8_t>(buffer.Need(5));
|
||||||
if (data.IsNull())
|
if (data.data() == nullptr)
|
||||||
return SignedSongTime::Negative();
|
return SignedSongTime::Negative();
|
||||||
|
|
||||||
size_t tagsize = 0;
|
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 */
|
/* skip the ID3 tag */
|
||||||
|
|
||||||
tagsize = (data.data[6] << 21) | (data.data[7] << 14) |
|
tagsize = (data[6] << 21) | (data[7] << 14) |
|
||||||
(data.data[8] << 7) | (data.data[9] << 0);
|
(data[8] << 7) | (data[9] << 0);
|
||||||
|
|
||||||
tagsize += 10;
|
tagsize += 10;
|
||||||
|
|
||||||
if (!buffer.Skip(tagsize))
|
if (!buffer.Skip(tagsize))
|
||||||
return SignedSongTime::Negative();
|
return SignedSongTime::Negative();
|
||||||
|
|
||||||
data = ConstBuffer<uint8_t>::FromVoid(buffer.Need(5));
|
data = FromBytesStrict<const uint8_t>(buffer.Need(5));
|
||||||
if (data.IsNull())
|
if (data.data() == nullptr)
|
||||||
return SignedSongTime::Negative();
|
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 */
|
/* obtain the duration from the ADTS header */
|
||||||
|
|
||||||
if (!is.IsSeekable())
|
if (!is.IsSeekable())
|
||||||
@ -199,23 +200,23 @@ faad_song_duration(DecoderBuffer &buffer, InputStream &is)
|
|||||||
buffer.Clear();
|
buffer.Clear();
|
||||||
|
|
||||||
return song_length;
|
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 */
|
/* obtain the duration from the ADIF header */
|
||||||
|
|
||||||
if (!is.KnownSize())
|
if (!is.KnownSize())
|
||||||
return SignedSongTime::Negative();
|
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
|
/* not enough data yet; skip parsing this
|
||||||
header */
|
header */
|
||||||
return SignedSongTime::Negative();
|
return SignedSongTime::Negative();
|
||||||
|
|
||||||
unsigned bit_rate = ((data.data[4 + skip_size] & 0x0F) << 19) |
|
unsigned bit_rate = ((data[4 + skip_size] & 0x0F) << 19) |
|
||||||
(data.data[5 + skip_size] << 11) |
|
(data[5 + skip_size] << 11) |
|
||||||
(data.data[6 + skip_size] << 3) |
|
(data[6 + skip_size] << 3) |
|
||||||
(data.data[7 + skip_size] & 0xE0);
|
(data[7 + skip_size] & 0xE0);
|
||||||
|
|
||||||
const auto size = is.GetSize();
|
const auto size = is.GetSize();
|
||||||
if (bit_rate == 0)
|
if (bit_rate == 0)
|
||||||
@ -251,7 +252,7 @@ static void
|
|||||||
faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer &buffer,
|
faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer &buffer,
|
||||||
AudioFormat &audio_format)
|
AudioFormat &audio_format)
|
||||||
{
|
{
|
||||||
auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Read());
|
auto data = FromBytesStrict<const uint8_t>(buffer.Read());
|
||||||
if (data.empty())
|
if (data.empty())
|
||||||
throw std::runtime_error("Empty file");
|
throw std::runtime_error("Empty file");
|
||||||
|
|
||||||
@ -259,8 +260,8 @@ faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer &buffer,
|
|||||||
unsigned long sample_rate;
|
unsigned long sample_rate;
|
||||||
long nbytes = NeAACDecInit(decoder,
|
long nbytes = NeAACDecInit(decoder,
|
||||||
/* deconst hack, libfaad requires this */
|
/* deconst hack, libfaad requires this */
|
||||||
const_cast<unsigned char *>(data.data),
|
const_cast<unsigned char *>(data.data()),
|
||||||
data.size,
|
data.size(),
|
||||||
&sample_rate, &channels);
|
&sample_rate, &channels);
|
||||||
if (nbytes < 0)
|
if (nbytes < 0)
|
||||||
throw std::runtime_error("Not an AAC stream");
|
throw std::runtime_error("Not an AAC stream");
|
||||||
@ -279,14 +280,14 @@ static const void *
|
|||||||
faad_decoder_decode(NeAACDecHandle decoder, DecoderBuffer &buffer,
|
faad_decoder_decode(NeAACDecHandle decoder, DecoderBuffer &buffer,
|
||||||
NeAACDecFrameInfo *frame_info)
|
NeAACDecFrameInfo *frame_info)
|
||||||
{
|
{
|
||||||
auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Read());
|
auto data = FromBytesStrict<const uint8_t>(buffer.Read());
|
||||||
if (data.empty())
|
if (data.empty())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
return NeAACDecDecode(decoder, frame_info,
|
return NeAACDecDecode(decoder, frame_info,
|
||||||
/* deconst hack, libfaad requires this */
|
/* deconst hack, libfaad requires this */
|
||||||
const_cast<uint8_t *>(data.data),
|
const_cast<uint8_t *>(data.data()),
|
||||||
data.size);
|
data.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -42,7 +42,6 @@
|
|||||||
#include "input/InputStream.hxx"
|
#include "input/InputStream.hxx"
|
||||||
#include "pcm/CheckAudioFormat.hxx"
|
#include "pcm/CheckAudioFormat.hxx"
|
||||||
#include "util/ScopeExit.hxx"
|
#include "util/ScopeExit.hxx"
|
||||||
#include "util/ConstBuffer.hxx"
|
|
||||||
#include "util/StringAPI.hxx"
|
#include "util/StringAPI.hxx"
|
||||||
#include "Log.hxx"
|
#include "Log.hxx"
|
||||||
|
|
||||||
@ -216,23 +215,20 @@ FfmpegSendFrame(DecoderClient &client, InputStream *is,
|
|||||||
size_t &skip_bytes,
|
size_t &skip_bytes,
|
||||||
FfmpegBuffer &buffer)
|
FfmpegBuffer &buffer)
|
||||||
{
|
{
|
||||||
ConstBuffer<void> output_buffer =
|
auto output_buffer = Ffmpeg::InterleaveFrame(frame, buffer);
|
||||||
Ffmpeg::InterleaveFrame(frame, buffer);
|
|
||||||
|
|
||||||
if (skip_bytes > 0) {
|
if (skip_bytes > 0) {
|
||||||
if (skip_bytes >= output_buffer.size) {
|
if (skip_bytes >= output_buffer.size()) {
|
||||||
skip_bytes -= output_buffer.size;
|
skip_bytes -= output_buffer.size();
|
||||||
return DecoderCommand::NONE;
|
return DecoderCommand::NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
output_buffer.data =
|
output_buffer = output_buffer.subspan(skip_bytes);
|
||||||
(const uint8_t *)output_buffer.data + skip_bytes;
|
|
||||||
output_buffer.size -= skip_bytes;
|
|
||||||
skip_bytes = 0;
|
skip_bytes = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return client.SubmitData(is,
|
return client.SubmitData(is,
|
||||||
output_buffer.data, output_buffer.size,
|
output_buffer.data(), output_buffer.size(),
|
||||||
codec_context.bit_rate / 1000);
|
codec_context.bit_rate / 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,10 +27,12 @@
|
|||||||
#include "FlacInput.hxx"
|
#include "FlacInput.hxx"
|
||||||
#include "FlacPcm.hxx"
|
#include "FlacPcm.hxx"
|
||||||
#include "../DecoderAPI.hxx"
|
#include "../DecoderAPI.hxx"
|
||||||
#include "util/ConstBuffer.hxx"
|
|
||||||
|
|
||||||
#include <FLAC/stream_decoder.h>
|
#include <FLAC/stream_decoder.h>
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <span>
|
||||||
|
|
||||||
struct FlacDecoder : public FlacInput {
|
struct FlacDecoder : public FlacInput {
|
||||||
/**
|
/**
|
||||||
* Has DecoderClient::Ready() been called yet?
|
* Has DecoderClient::Ready() been called yet?
|
||||||
@ -63,7 +65,7 @@ struct FlacDecoder : public FlacInput {
|
|||||||
* If this is non-empty, then DecoderBridge::SubmitData()
|
* If this is non-empty, then DecoderBridge::SubmitData()
|
||||||
* should be called.
|
* should be called.
|
||||||
*/
|
*/
|
||||||
ConstBuffer<void> chunk = nullptr;
|
std::span<const std::byte> chunk = {};
|
||||||
|
|
||||||
FlacDecoder(DecoderClient &_client, InputStream &_input_stream)
|
FlacDecoder(DecoderClient &_client, InputStream &_input_stream)
|
||||||
:FlacInput(_input_stream, &_client) {}
|
:FlacInput(_input_stream, &_client) {}
|
||||||
|
@ -153,10 +153,10 @@ FlacSubmitToClient(DecoderClient &client, FlacDecoder &d) noexcept
|
|||||||
|
|
||||||
if (!d.chunk.empty()) {
|
if (!d.chunk.empty()) {
|
||||||
auto cmd = client.SubmitData(d.GetInputStream(),
|
auto cmd = client.SubmitData(d.GetInputStream(),
|
||||||
d.chunk.data,
|
d.chunk.data(),
|
||||||
d.chunk.size,
|
d.chunk.size(),
|
||||||
d.kbit_rate);
|
d.kbit_rate);
|
||||||
d.chunk = nullptr;
|
d.chunk = {};
|
||||||
if (cmd != DecoderCommand::NONE)
|
if (cmd != DecoderCommand::NONE)
|
||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,6 @@
|
|||||||
#include "pcm/CheckAudioFormat.hxx"
|
#include "pcm/CheckAudioFormat.hxx"
|
||||||
#include "lib/xiph/FlacAudioFormat.hxx"
|
#include "lib/xiph/FlacAudioFormat.hxx"
|
||||||
#include "util/RuntimeError.hxx"
|
#include "util/RuntimeError.hxx"
|
||||||
#include "util/ConstBuffer.hxx"
|
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
@ -69,7 +68,7 @@ FlacImport(T *dest, const FLAC__int32 *const src[], size_t n_frames,
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static ConstBuffer<void>
|
static std::span<const std::byte>
|
||||||
FlacImport(PcmBuffer &buffer, const FLAC__int32 *const src[], size_t n_frames,
|
FlacImport(PcmBuffer &buffer, const FLAC__int32 *const src[], size_t n_frames,
|
||||||
unsigned n_channels)
|
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);
|
size_t dest_size = n_samples * sizeof(T);
|
||||||
T *dest = (T *)buffer.Get(dest_size);
|
T *dest = (T *)buffer.Get(dest_size);
|
||||||
FlacImport(dest, src, n_frames, n_channels);
|
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)
|
FlacPcmImport::Import(const FLAC__int32 *const src[], size_t n_frames)
|
||||||
{
|
{
|
||||||
switch (audio_format.format) {
|
switch (audio_format.format) {
|
||||||
|
@ -25,7 +25,8 @@
|
|||||||
|
|
||||||
#include <FLAC/ordinals.h>
|
#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
|
* This class imports libFLAC PCM data into a PCM format supported by
|
||||||
@ -47,7 +48,7 @@ public:
|
|||||||
return audio_format;
|
return audio_format;
|
||||||
}
|
}
|
||||||
|
|
||||||
ConstBuffer<void> Import(const FLAC__int32 *const src[],
|
std::span<const std::byte> Import(const FLAC__int32 *const src[],
|
||||||
size_t n_frames);
|
size_t n_frames);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ extern "C" {
|
|||||||
|
|
||||||
namespace Ffmpeg {
|
namespace Ffmpeg {
|
||||||
|
|
||||||
ConstBuffer<void>
|
std::span<const std::byte>
|
||||||
InterleaveFrame(const AVFrame &frame, FfmpegBuffer &buffer)
|
InterleaveFrame(const AVFrame &frame, FfmpegBuffer &buffer)
|
||||||
{
|
{
|
||||||
assert(frame.nb_samples > 0);
|
assert(frame.nb_samples > 0);
|
||||||
@ -49,9 +49,9 @@ InterleaveFrame(const AVFrame &frame, FfmpegBuffer &buffer)
|
|||||||
if (data_size < 0)
|
if (data_size < 0)
|
||||||
throw MakeFfmpegError(data_size);
|
throw MakeFfmpegError(data_size);
|
||||||
|
|
||||||
void *output_buffer;
|
std::byte *output_buffer;
|
||||||
if (av_sample_fmt_is_planar(format) && channels > 1) {
|
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)
|
if (output_buffer == nullptr)
|
||||||
/* Not enough memory - shouldn't happen */
|
/* Not enough memory - shouldn't happen */
|
||||||
throw std::bad_alloc();
|
throw std::bad_alloc();
|
||||||
@ -61,7 +61,7 @@ InterleaveFrame(const AVFrame &frame, FfmpegBuffer &buffer)
|
|||||||
n_frames,
|
n_frames,
|
||||||
av_get_bytes_per_sample(format));
|
av_get_bytes_per_sample(format));
|
||||||
} else {
|
} else {
|
||||||
output_buffer = frame.extended_data[0];
|
output_buffer = (std::byte *)frame.extended_data[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
return { output_buffer, (size_t)data_size };
|
return { output_buffer, (size_t)data_size };
|
||||||
|
@ -20,8 +20,9 @@
|
|||||||
#ifndef MPD_FFMPEG_INTERLEAVE_HXX
|
#ifndef MPD_FFMPEG_INTERLEAVE_HXX
|
||||||
#define MPD_FFMPEG_INTERLEAVE_HXX
|
#define MPD_FFMPEG_INTERLEAVE_HXX
|
||||||
|
|
||||||
|
#include <span>
|
||||||
|
|
||||||
struct AVFrame;
|
struct AVFrame;
|
||||||
template<typename T> struct ConstBuffer;
|
|
||||||
class FfmpegBuffer;
|
class FfmpegBuffer;
|
||||||
|
|
||||||
namespace Ffmpeg {
|
namespace Ffmpeg {
|
||||||
@ -32,7 +33,7 @@ namespace Ffmpeg {
|
|||||||
*
|
*
|
||||||
* Throws on error.
|
* Throws on error.
|
||||||
*/
|
*/
|
||||||
ConstBuffer<void>
|
std::span<const std::byte>
|
||||||
InterleaveFrame(const AVFrame &frame, FfmpegBuffer &buffer);
|
InterleaveFrame(const AVFrame &frame, FfmpegBuffer &buffer);
|
||||||
|
|
||||||
} // namespace Ffmpeg
|
} // namespace Ffmpeg
|
||||||
|
Loading…
Reference in New Issue
Block a user