PcmConvert: Convert() returns ConstBuffer

This commit is contained in:
Max Kellermann 2014-08-12 16:36:07 +02:00
parent 4d5f610029
commit 56f61a6d59
5 changed files with 19 additions and 23 deletions

View File

@ -31,6 +31,7 @@
#include "DetachedSong.hxx" #include "DetachedSong.hxx"
#include "input/InputStream.hxx" #include "input/InputStream.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/ConstBuffer.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <assert.h> #include <assert.h>
@ -472,9 +473,8 @@ decoder_data(Decoder &decoder,
assert(dc.in_audio_format != dc.out_audio_format); assert(dc.in_audio_format != dc.out_audio_format);
Error error; Error error;
data = decoder.convert->Convert(data, length, auto result = decoder.convert->Convert({data, length},
&length, error);
error);
if (data == nullptr) { if (data == nullptr) {
/* the PCM conversion has failed - stop /* the PCM conversion has failed - stop
playback, since we have no better way to playback, since we have no better way to
@ -482,6 +482,9 @@ decoder_data(Decoder &decoder,
LogError(error); LogError(error);
return DecoderCommand::STOP; return DecoderCommand::STOP;
} }
data = result.data;
length = result.size;
} else { } else {
assert(dc.in_audio_format == dc.out_audio_format); assert(dc.in_audio_format == dc.out_audio_format);
} }

View File

@ -24,6 +24,7 @@
#include "filter/FilterRegistry.hxx" #include "filter/FilterRegistry.hxx"
#include "pcm/PcmConvert.hxx" #include "pcm/PcmConvert.hxx"
#include "util/Manual.hxx" #include "util/Manual.hxx"
#include "util/ConstBuffer.hxx"
#include "AudioFormat.hxx" #include "AudioFormat.hxx"
#include "poison.h" #include "poison.h"
@ -130,8 +131,9 @@ ConvertFilter::FilterPCM(const void *src, size_t src_size,
return src; return src;
} }
return state->Convert(src, src_size, dest_size_r, auto result = state->Convert({src, src_size}, error);
error); *dest_size_r = result.size;
return result.data;
} }
const struct filter_plugin convert_filter_plugin = { const struct filter_plugin convert_filter_plugin = {

View File

@ -117,12 +117,9 @@ PcmConvert::Close()
#endif #endif
} }
const void * ConstBuffer<void>
PcmConvert::Convert(const void *src, size_t src_size, PcmConvert::Convert(ConstBuffer<void> buffer, Error &error)
size_t *dest_size_r,
Error &error)
{ {
ConstBuffer<void> buffer(src, src_size);
AudioFormat format = src_format; AudioFormat format = src_format;
if (format.format == SampleFormat::DSD) { if (format.format == SampleFormat::DSD) {
@ -164,6 +161,5 @@ PcmConvert::Convert(const void *src, size_t src_size,
format.channels = dest_format.channels; format.channels = dest_format.channels;
} }
*dest_size_r = buffer.size; return buffer;
return buffer.data;
} }

View File

@ -70,16 +70,12 @@ public:
* *
* @param src_format the source audio format * @param src_format the source audio format
* @param src the source PCM buffer * @param src the source PCM buffer
* @param src_size the size of #src in bytes
* @param dest_format the requested destination audio format * @param dest_format the requested destination audio format
* @param dest_size_r returns the number of bytes of the destination buffer
* @param error_r location to store the error occurring, or nullptr to * @param error_r location to store the error occurring, or nullptr to
* ignore errors * ignore errors
* @return the destination buffer, or nullptr on error * @return the destination buffer, or nullptr on error
*/ */
const void *Convert(const void *src, size_t src_size, ConstBuffer<void> Convert(ConstBuffer<void> src, Error &error);
size_t *dest_size_r,
Error &error);
}; };
bool bool

View File

@ -28,6 +28,7 @@
#include "AudioFormat.hxx" #include "AudioFormat.hxx"
#include "pcm/PcmConvert.hxx" #include "pcm/PcmConvert.hxx"
#include "config/ConfigGlobal.hxx" #include "config/ConfigGlobal.hxx"
#include "util/ConstBuffer.hxx"
#include "util/StaticFifoBuffer.hxx" #include "util/StaticFifoBuffer.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "Log.hxx" #include "Log.hxx"
@ -48,7 +49,6 @@ config_get_string(gcc_unused enum ConfigOption option,
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
AudioFormat in_audio_format, out_audio_format; AudioFormat in_audio_format, out_audio_format;
const void *output;
if (argc != 3) { if (argc != 3) {
fprintf(stderr, fprintf(stderr,
@ -104,16 +104,15 @@ int main(int argc, char **argv)
buffer.Consume(src.size); buffer.Consume(src.size);
size_t length; auto output = state.Convert({src.data, src.size}, error);
output = state.Convert(src.data, src.size, if (output.IsNull()) {
&length, error);
if (output == NULL) {
state.Close(); state.Close();
LogError(error, "Failed to convert"); LogError(error, "Failed to convert");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
gcc_unused ssize_t ignored = write(1, output, length); gcc_unused ssize_t ignored = write(1, output.data,
output.size);
} }
state.Close(); state.Close();