aboutsummaryrefslogtreecommitdiffstats
path: root/src/pcm_convert.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pcm_convert.c')
-rw-r--r--src/pcm_convert.c136
1 files changed, 134 insertions, 2 deletions
diff --git a/src/pcm_convert.c b/src/pcm_convert.c
index 7bd4d7215..daf1cd234 100644
--- a/src/pcm_convert.c
+++ b/src/pcm_convert.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2003-2010 The Music Player Daemon Project
+ * Copyright (C) 2003-2011 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
@@ -24,6 +24,7 @@
#include "pcm_byteswap.h"
#include "pcm_pack.h"
#include "audio_format.h"
+#include "glib_compat.h"
#include <assert.h>
#include <string.h>
@@ -56,6 +57,55 @@ void pcm_convert_deinit(struct pcm_convert_state *state)
pcm_buffer_deinit(&state->byteswap_buffer);
}
+static const void *
+pcm_convert_channels(struct pcm_buffer *buffer, enum sample_format format,
+ uint8_t dest_channels,
+ uint8_t src_channels, const void *src,
+ size_t src_size, size_t *dest_size_r,
+ GError **error_r)
+{
+ const void *dest = NULL;
+
+ switch (format) {
+ case SAMPLE_FORMAT_UNDEFINED:
+ case SAMPLE_FORMAT_S8:
+ case SAMPLE_FORMAT_S24:
+ case SAMPLE_FORMAT_FLOAT:
+ g_set_error(error_r, pcm_convert_quark(), 0,
+ "Channel conversion not implemented for format '%s'",
+ sample_format_to_string(format));
+ return NULL;
+
+ case SAMPLE_FORMAT_S16:
+ dest = pcm_convert_channels_16(buffer, dest_channels,
+ src_channels, src,
+ src_size, dest_size_r);
+ break;
+
+ case SAMPLE_FORMAT_S24_P32:
+ dest = pcm_convert_channels_24(buffer, dest_channels,
+ src_channels, src,
+ src_size, dest_size_r);
+ break;
+
+ case SAMPLE_FORMAT_S32:
+ dest = pcm_convert_channels_32(buffer, dest_channels,
+ src_channels, src,
+ src_size, dest_size_r);
+ break;
+ }
+
+ if (dest == NULL) {
+ g_set_error(error_r, pcm_convert_quark(), 0,
+ "Conversion from %u to %u channels "
+ "is not implemented",
+ src_channels, dest_channels);
+ return NULL;
+ }
+
+ return dest;
+}
+
static const int16_t *
pcm_convert_16(struct pcm_convert_state *state,
const struct audio_format *src_format,
@@ -200,7 +250,8 @@ pcm_convert_24_packed(struct pcm_convert_state *state,
size_t dest_size = num_samples * 3;
uint8_t *dest = pcm_buffer_get(&state->pack_buffer, dest_size);
- pcm_pack_24(dest, buffer, num_samples, dest_format->reverse_endian);
+ pcm_pack_24(dest, buffer, buffer + num_samples,
+ dest_format->reverse_endian);
*dest_size_r = dest_size;
return dest;
@@ -261,6 +312,67 @@ pcm_convert_32(struct pcm_convert_state *state,
return buf;
}
+static const float *
+pcm_convert_float(struct pcm_convert_state *state,
+ const struct audio_format *src_format,
+ const void *src_buffer, size_t src_size,
+ const struct audio_format *dest_format, size_t *dest_size_r,
+ GError **error_r)
+{
+ const float *buffer = src_buffer;
+ size_t size = src_size;
+
+ assert(dest_format->format == SAMPLE_FORMAT_FLOAT);
+
+ if (src_format->reverse_endian || dest_format->reverse_endian) {
+ g_set_error_literal(error_r, pcm_convert_quark(), 0,
+ "Reverse endian not supported");
+ return NULL;
+ }
+
+ /* convert channels first, hoping the source format is
+ supported (float is not) */
+
+ if (dest_format->channels != src_format->channels) {
+ buffer = pcm_convert_channels(&state->channels_buffer,
+ src_format->format,
+ dest_format->channels,
+ src_format->channels,
+ buffer, size, &size, error_r);
+ if (buffer == NULL)
+ return NULL;
+ }
+
+ /* convert to float now */
+
+ buffer = pcm_convert_to_float(&state->format_buffer,
+ src_format->format,
+ buffer, size, &size);
+ if (buffer == NULL) {
+ g_set_error(error_r, pcm_convert_quark(), 0,
+ "Conversion from %s to float is not implemented",
+ sample_format_to_string(src_format->format));
+ return NULL;
+ }
+
+ /* resample with float, because this is the best format for
+ libsamplerate */
+
+ if (src_format->sample_rate != dest_format->sample_rate) {
+ buffer = pcm_resample_float(&state->resample,
+ dest_format->channels,
+ src_format->sample_rate,
+ buffer, size,
+ dest_format->sample_rate, &size,
+ error_r);
+ if (buffer == NULL)
+ return NULL;
+ }
+
+ *dest_size_r = size;
+ return buffer;
+}
+
const void *
pcm_convert(struct pcm_convert_state *state,
const struct audio_format *src_format,
@@ -269,6 +381,20 @@ pcm_convert(struct pcm_convert_state *state,
size_t *dest_size_r,
GError **error_r)
{
+ if (src_format->reverse_endian) {
+ /* convert to host byte order, because all of our
+ conversion libraries assume host byte order */
+
+ src = pcm_byteswap(&state->byteswap_buffer, src_format->format,
+ src, src_size);
+ if (src == NULL) {
+ g_set_error(error_r, pcm_convert_quark(), 0,
+ "PCM byte order change of format '%s' is not implemented",
+ sample_format_to_string(src_format->format));
+ return NULL;
+ }
+ }
+
switch (dest_format->format) {
case SAMPLE_FORMAT_S16:
return pcm_convert_16(state,
@@ -294,6 +420,12 @@ pcm_convert(struct pcm_convert_state *state,
dest_format, dest_size_r,
error_r);
+ case SAMPLE_FORMAT_FLOAT:
+ return pcm_convert_float(state,
+ src_format, src, src_size,
+ dest_format, dest_size_r,
+ error_r);
+
default:
g_set_error(error_r, pcm_convert_quark(), 0,
"PCM conversion to %s is not implemented",