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