Commit 7244dc45 authored by Max Kellermann's avatar Max Kellermann

Filter: FilterPCM() returns ConstBuffer

API simplification. We can now avoid abusing a "size_t*" as additional return value.
parent 56f61a6d
......@@ -29,6 +29,7 @@
struct AudioFormat;
class Error;
template<typename T> struct ConstBuffer;
class Filter {
public:
......@@ -58,17 +59,13 @@ public:
*
* @param filter the filter object
* @param src the input buffer
* @param src_size the size of #src_buffer in bytes
* @param dest_size_r the size of the returned buffer
* @param error location to store the error occurring, or nullptr
* to ignore errors.
* @return the destination buffer on success (will be
* invalidated by Close() or FilterPCM()), nullptr on
* error
*/
virtual const void *FilterPCM(const void *src, size_t src_size,
size_t *dest_size_r,
Error &error) = 0;
virtual ConstBuffer<void> FilterPCM(ConstBuffer<void> src, Error &error) = 0;
};
#endif
......@@ -25,6 +25,7 @@
#include "filter/FilterRegistry.hxx"
#include "AudioFormat.hxx"
#include "config/ConfigData.hxx"
#include "util/ConstBuffer.hxx"
#include <assert.h>
......@@ -48,8 +49,7 @@ public:
virtual AudioFormat Open(AudioFormat &af, Error &error) override;
virtual void Close() override;
virtual const void *FilterPCM(const void *src, size_t src_size,
size_t *dest_size_r,
virtual ConstBuffer<void> FilterPCM(ConstBuffer<void> src,
Error &error) override;
};
......@@ -111,17 +111,16 @@ AutoConvertFilter::Close()
filter->Close();
}
const void *
AutoConvertFilter::FilterPCM(const void *src, size_t src_size,
size_t *dest_size_r, Error &error)
ConstBuffer<void>
AutoConvertFilter::FilterPCM(ConstBuffer<void> src, Error &error)
{
if (convert != nullptr) {
src = convert->FilterPCM(src, src_size, &src_size, error);
if (src == nullptr)
src = convert->FilterPCM(src, error);
if (src.IsNull())
return nullptr;
}
return filter->FilterPCM(src, src_size, dest_size_r, error);
return filter->FilterPCM(src, error);
}
Filter *
......
......@@ -25,6 +25,7 @@
#include "AudioFormat.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/ConstBuffer.hxx"
#include <list>
......@@ -54,8 +55,8 @@ public:
virtual AudioFormat Open(AudioFormat &af, Error &error) override;
virtual void Close();
virtual const void *FilterPCM(const void *src, size_t src_size,
size_t *dest_size_r, Error &error);
virtual ConstBuffer<void> FilterPCM(ConstBuffer<void> src,
Error &error);
private:
/**
......@@ -143,21 +144,18 @@ ChainFilter::Close()
child.filter->Close();
}
const void *
ChainFilter::FilterPCM(const void *src, size_t src_size,
size_t *dest_size_r, Error &error)
ConstBuffer<void>
ChainFilter::FilterPCM(ConstBuffer<void> src, Error &error)
{
for (auto &child : children) {
/* feed the output of the previous filter as input
into the current one */
src = child.filter->FilterPCM(src, src_size, &src_size,
error);
if (src == nullptr)
src = child.filter->FilterPCM(src, error);
if (src.IsNull())
return nullptr;
}
/* return the output of the last filter */
*dest_size_r = src_size;
return src;
}
......
......@@ -54,8 +54,7 @@ public:
virtual AudioFormat Open(AudioFormat &af, Error &error) override;
virtual void Close() override;
virtual const void *FilterPCM(const void *src, size_t src_size,
size_t *dest_size_r,
virtual ConstBuffer<void> FilterPCM(ConstBuffer<void> src,
Error &error) override;
};
......@@ -119,21 +118,16 @@ ConvertFilter::Close()
poison_undefined(&out_audio_format, sizeof(out_audio_format));
}
const void *
ConvertFilter::FilterPCM(const void *src, size_t src_size,
size_t *dest_size_r, Error &error)
ConstBuffer<void>
ConvertFilter::FilterPCM(ConstBuffer<void> src, Error &error)
{
assert(in_audio_format.IsValid());
if (!out_audio_format.IsValid()) {
if (!out_audio_format.IsValid())
/* optimized special case: no-op */
*dest_size_r = src_size;
return src;
}
auto result = state->Convert({src, src_size}, error);
*dest_size_r = result.size;
return result.data;
return state->Convert(src, error);
}
const struct filter_plugin convert_filter_plugin = {
......
......@@ -24,6 +24,7 @@
#include "pcm/PcmBuffer.hxx"
#include "AudioFormat.hxx"
#include "AudioCompress/compress.h"
#include "util/ConstBuffer.hxx"
#include <string.h>
......@@ -35,8 +36,8 @@ class NormalizeFilter final : public Filter {
public:
virtual AudioFormat Open(AudioFormat &af, Error &error) override;
virtual void Close();
virtual const void *FilterPCM(const void *src, size_t src_size,
size_t *dest_size_r, Error &error);
virtual ConstBuffer<void> FilterPCM(ConstBuffer<void> src,
Error &error) override;
};
static Filter *
......@@ -63,17 +64,14 @@ NormalizeFilter::Close()
Compressor_delete(compressor);
}
const void *
NormalizeFilter::FilterPCM(const void *src, size_t src_size,
size_t *dest_size_r, gcc_unused Error &error)
ConstBuffer<void>
NormalizeFilter::FilterPCM(ConstBuffer<void> src, gcc_unused Error &error)
{
int16_t *dest = (int16_t *)buffer.Get(src_size);
memcpy(dest, src, src_size);
int16_t *dest = (int16_t *)buffer.Get(src.size);
memcpy(dest, src.data, src.size);
Compressor_Process_int16(compressor, dest, src_size / 2);
*dest_size_r = src_size;
return dest;
Compressor_Process_int16(compressor, dest, src.size / 2);
return { (const void *)dest, src.size };
}
const struct filter_plugin normalize_filter_plugin = {
......
......@@ -30,6 +30,7 @@
#include "filter/FilterRegistry.hxx"
#include "AudioFormat.hxx"
#include "Compiler.h"
#include "util/ConstBuffer.hxx"
class NullFilter final : public Filter {
public:
......@@ -40,10 +41,8 @@ public:
virtual void Close() override {}
virtual const void *FilterPCM(const void *src, size_t src_size,
size_t *dest_size_r,
virtual ConstBuffer<void> FilterPCM(ConstBuffer<void> src,
gcc_unused Error &error) override {
*dest_size_r = src_size;
return src;
}
};
......
......@@ -114,8 +114,8 @@ public:
virtual AudioFormat Open(AudioFormat &af, Error &error) override;
virtual void Close();
virtual const void *FilterPCM(const void *src, size_t src_size,
size_t *dest_size_r, Error &error);
virtual ConstBuffer<void> FilterPCM(ConstBuffer<void> src,
Error &error) override;
};
void
......@@ -170,13 +170,10 @@ ReplayGainFilter::Close()
pv.Close();
}
const void *
ReplayGainFilter::FilterPCM(const void *src, size_t src_size,
size_t *dest_size_r, gcc_unused Error &error)
ConstBuffer<void>
ReplayGainFilter::FilterPCM(ConstBuffer<void> src, gcc_unused Error &error)
{
const auto dest = pv.Apply({src, src_size});
*dest_size_r = dest.size;
return dest.data;
return pv.Apply(src);
}
const struct filter_plugin replay_gain_filter_plugin = {
......
......@@ -49,6 +49,7 @@
#include "pcm/PcmBuffer.hxx"
#include "util/StringUtil.hxx"
#include "util/Error.hxx"
#include "util/ConstBuffer.hxx"
#include <algorithm>
......@@ -121,8 +122,8 @@ public:
virtual AudioFormat Open(AudioFormat &af, Error &error) override;
virtual void Close();
virtual const void *FilterPCM(const void *src, size_t src_size,
size_t *dest_size_r, Error &error);
virtual ConstBuffer<void> FilterPCM(ConstBuffer<void> src,
Error &error) override;
};
bool
......@@ -238,20 +239,19 @@ RouteFilter::Close()
output_buffer.Clear();
}
const void *
RouteFilter::FilterPCM(const void *src, size_t src_size,
size_t *dest_size_r, gcc_unused Error &error)
ConstBuffer<void>
RouteFilter::FilterPCM(ConstBuffer<void> src, gcc_unused Error &error)
{
size_t number_of_frames = src_size / input_frame_size;
size_t number_of_frames = src.size / input_frame_size;
const size_t bytes_per_frame_per_channel = input_format.GetSampleSize();
// A moving pointer that always refers to channel 0 in the input, at the currently handled frame
const uint8_t *base_source = (const uint8_t *)src;
const uint8_t *base_source = (const uint8_t *)src.data;
// Grow our reusable buffer, if needed, and set the moving pointer
*dest_size_r = number_of_frames * output_frame_size;
void *const result = output_buffer.Get(*dest_size_r);
const size_t result_size = number_of_frames * output_frame_size;
void *const result = output_buffer.Get(result_size);
// A moving pointer that always refers to the currently filled channel of the currently handled frame, in the output
uint8_t *chan_destination = (uint8_t *)result;
......@@ -287,7 +287,7 @@ RouteFilter::FilterPCM(const void *src, size_t src_size,
}
// Here it is, ladies and gentlemen! Rerouted data!
return result;
return { result, result_size };
}
const struct filter_plugin route_filter_plugin = {
......
......@@ -45,8 +45,8 @@ public:
virtual AudioFormat Open(AudioFormat &af, Error &error) override;
virtual void Close();
virtual const void *FilterPCM(const void *src, size_t src_size,
size_t *dest_size_r, Error &error);
virtual ConstBuffer<void> FilterPCM(ConstBuffer<void> src,
Error &error) override;
};
static constexpr Domain volume_domain("pcm_volume");
......@@ -73,13 +73,10 @@ VolumeFilter::Close()
pv.Close();
}
const void *
VolumeFilter::FilterPCM(const void *src, size_t src_size,
size_t *dest_size_r, gcc_unused Error &error)
ConstBuffer<void>
VolumeFilter::FilterPCM(ConstBuffer<void> src, gcc_unused Error &error)
{
const auto dest = pv.Apply({src, src_size});
*dest_size_r = dest.size;
return dest.data;
return pv.Apply(src);
}
const struct filter_plugin volume_filter_plugin = {
......
......@@ -34,6 +34,7 @@
#include "thread/Name.hxx"
#include "system/FatalError.hxx"
#include "util/Error.hxx"
#include "util/ConstBuffer.hxx"
#include "Log.hxx"
#include "Compiler.h"
......@@ -306,24 +307,22 @@ AudioOutput::WaitForDelay()
}
}
static const void *
static ConstBuffer<void>
ao_chunk_data(AudioOutput *ao, const MusicChunk *chunk,
Filter *replay_gain_filter,
unsigned *replay_gain_serial_p,
size_t *length_r)
unsigned *replay_gain_serial_p)
{
assert(chunk != nullptr);
assert(!chunk->IsEmpty());
assert(chunk->CheckFormat(ao->in_audio_format));
const void *data = chunk->data;
size_t length = chunk->length;
ConstBuffer<void> data(chunk->data, chunk->length);
(void)ao;
assert(length % ao->in_audio_format.GetFrameSize() == 0);
assert(data.size % ao->in_audio_format.GetFrameSize() == 0);
if (length > 0 && replay_gain_filter != nullptr) {
if (!data.IsEmpty() && replay_gain_filter != nullptr) {
if (chunk->replay_gain_serial != *replay_gain_serial_p) {
replay_gain_filter_set_info(replay_gain_filter,
chunk->replay_gain_serial != 0
......@@ -333,63 +332,48 @@ ao_chunk_data(AudioOutput *ao, const MusicChunk *chunk,
}
Error error;
data = replay_gain_filter->FilterPCM(data, length,
&length, error);
if (data == nullptr) {
data = replay_gain_filter->FilterPCM(data, error);
if (data.IsNull())
FormatError(error, "\"%s\" [%s] failed to filter",
ao->name, ao->plugin.name);
return nullptr;
}
}
*length_r = length;
return data;
}
static const void *
ao_filter_chunk(AudioOutput *ao, const MusicChunk *chunk,
size_t *length_r)
static ConstBuffer<void>
ao_filter_chunk(AudioOutput *ao, const MusicChunk *chunk)
{
size_t length;
const void *data = ao_chunk_data(ao, chunk, ao->replay_gain_filter,
&ao->replay_gain_serial, &length);
if (data == nullptr)
return nullptr;
if (length == 0) {
/* empty chunk, nothing to do */
*length_r = 0;
ConstBuffer<void> data =
ao_chunk_data(ao, chunk, ao->replay_gain_filter,
&ao->replay_gain_serial);
if (data.IsEmpty())
return data;
}
/* cross-fade */
if (chunk->other != nullptr) {
size_t other_length;
const void *other_data =
ConstBuffer<void> other_data =
ao_chunk_data(ao, chunk->other,
ao->other_replay_gain_filter,
&ao->other_replay_gain_serial,
&other_length);
if (other_data == nullptr)
&ao->other_replay_gain_serial);
if (other_data.IsNull())
return nullptr;
if (other_length == 0) {
*length_r = 0;
if (other_data.IsEmpty())
return data;
}
/* if the "other" chunk is longer, then that trailer
is used as-is, without mixing; it is part of the
"next" song being faded in, and if there's a rest,
it means cross-fading ends here */
if (length > other_length)
length = other_length;
if (data.size > other_data.size)
data.size = other_data.size;
void *dest = ao->cross_fade_buffer.Get(other_length);
memcpy(dest, other_data, other_length);
if (!pcm_mix(ao->cross_fade_dither, dest, data, length,
void *dest = ao->cross_fade_buffer.Get(other_data.size);
memcpy(dest, other_data.data, other_data.size);
if (!pcm_mix(ao->cross_fade_dither, dest, data.data, data.size,
ao->in_audio_format.format,
1.0 - chunk->mix_ratio)) {
FormatError(output_domain,
......@@ -398,21 +382,20 @@ ao_filter_chunk(AudioOutput *ao, const MusicChunk *chunk,
return nullptr;
}
data = dest;
length = other_length;
data.data = dest;
data.size = other_data.size;
}
/* apply filter chain */
Error error;
data = ao->filter->FilterPCM(data, length, &length, error);
if (data == nullptr) {
data = ao->filter->FilterPCM(data, error);
if (data.IsNull()) {
FormatError(error, "\"%s\" [%s] failed to filter",
ao->name, ao->plugin.name);
return nullptr;
}
*length_r = length;
return data;
}
......@@ -427,13 +410,8 @@ AudioOutput::PlayChunk(const MusicChunk *chunk)
mutex.lock();
}
size_t size;
#if GCC_CHECK_VERSION(4,7)
/* workaround -Wmaybe-uninitialized false positive */
size = 0;
#endif
const char *data = (const char *)ao_filter_chunk(this, chunk, &size);
if (data == nullptr) {
auto data = ConstBuffer<char>::FromVoid(ao_filter_chunk(this, chunk));
if (data.IsNull()) {
Close(false);
/* don't automatically reopen this device for 10
......@@ -444,14 +422,13 @@ AudioOutput::PlayChunk(const MusicChunk *chunk)
Error error;
while (size > 0 && command == AO_COMMAND_NONE) {
size_t nbytes;
while (!data.IsEmpty() && command == AO_COMMAND_NONE) {
if (!WaitForDelay())
break;
mutex.unlock();
nbytes = ao_plugin_play(this, data, size, error);
size_t nbytes = ao_plugin_play(this, data.data, data.size,
error);
mutex.lock();
if (nbytes == 0) {
/* play()==0 means failure */
......@@ -468,11 +445,11 @@ AudioOutput::PlayChunk(const MusicChunk *chunk)
return false;
}
assert(nbytes <= size);
assert(nbytes <= data.size);
assert(nbytes % out_audio_format.GetFrameSize() == 0);
data += nbytes;
size -= nbytes;
data.data += nbytes;
data.size -= nbytes;
}
return true;
......
......@@ -29,6 +29,7 @@
#include "mixer/MixerControl.hxx"
#include "stdbin.h"
#include "util/Error.hxx"
#include "util/ConstBuffer.hxx"
#include "system/FatalError.hxx"
#include "Log.hxx"
......@@ -132,23 +133,21 @@ int main(int argc, char **argv)
while (true) {
ssize_t nbytes;
size_t length;
const void *dest;
nbytes = read(0, buffer, sizeof(buffer));
if (nbytes <= 0)
break;
dest = filter->FilterPCM(buffer, (size_t)nbytes,
&length, error);
if (dest == NULL) {
auto dest = filter->FilterPCM({(const void *)buffer, (size_t)nbytes},
error);
if (dest.IsNull()) {
LogError(error, "filter/Filter failed");
filter->Close();
delete filter;
return EXIT_FAILURE;
}
nbytes = write(1, dest, length);
nbytes = write(1, dest.data, dest.size);
if (nbytes < 0) {
fprintf(stderr, "Failed to write: %s\n",
strerror(errno));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment