From 05693e2d5d760e818fb7382f9bd528026f16aa51 Mon Sep 17 00:00:00 2001 From: David Woodhouse Date: Sun, 19 Jul 2009 16:42:19 +0100 Subject: Add reverse_endian field to struct audio_format and handle conversion --- src/audio_format.h | 5 ++- src/filter/convert_filter_plugin.c | 1 + src/output_thread.c | 10 +++--- src/pcm_byteswap.c | 71 ++++++++++++++++++++++++++++++++++++++ src/pcm_byteswap.h | 50 +++++++++++++++++++++++++++ src/pcm_convert.c | 19 ++++++++++ 6 files changed, 151 insertions(+), 5 deletions(-) create mode 100644 src/pcm_byteswap.c create mode 100644 src/pcm_byteswap.h (limited to 'src') diff --git a/src/audio_format.h b/src/audio_format.h index e325c1b38..54514ff93 100644 --- a/src/audio_format.h +++ b/src/audio_format.h @@ -27,6 +27,7 @@ struct audio_format { uint32_t sample_rate; uint8_t bits; uint8_t channels; + uint8_t reverse_endian; }; static inline void audio_format_clear(struct audio_format *af) @@ -34,6 +35,7 @@ static inline void audio_format_clear(struct audio_format *af) af->sample_rate = 0; af->bits = 0; af->channels = 0; + af->reverse_endian = 0; } static inline void audio_format_init(struct audio_format *af, @@ -97,7 +99,8 @@ static inline bool audio_format_equals(const struct audio_format *a, { return a->sample_rate == b->sample_rate && a->bits == b->bits && - a->channels == b->channels; + a->channels == b->channels && + a->reverse_endian == b->reverse_endian; } /** diff --git a/src/filter/convert_filter_plugin.c b/src/filter/convert_filter_plugin.c index f4d03ebef..b7f16de4f 100644 --- a/src/filter/convert_filter_plugin.c +++ b/src/filter/convert_filter_plugin.c @@ -149,6 +149,7 @@ convert_filter_set(struct filter *_filter, assert(audio_format_valid(&filter->out_audio_format)); assert(out_audio_format != NULL); assert(audio_format_valid(out_audio_format)); + assert(filter->in_audio_format.reverse_endian == 0); filter->out_audio_format = *out_audio_format; } diff --git a/src/output_thread.c b/src/output_thread.c index 2592b2456..c7bd069b1 100644 --- a/src/output_thread.c +++ b/src/output_thread.c @@ -93,18 +93,20 @@ ao_open(struct audio_output *ao) g_mutex_unlock(ao->mutex); g_debug("opened plugin=%s name=\"%s\" " - "audio_format=%u:%u:%u", + "audio_format=%u:%u:%u:%u", ao->plugin->name, ao->name, ao->out_audio_format.sample_rate, ao->out_audio_format.bits, - ao->out_audio_format.channels); + ao->out_audio_format.channels, + ao->out_audio_format.reverse_endian); if (!audio_format_equals(&ao->in_audio_format, &ao->out_audio_format)) - g_debug("converting from %u:%u:%u", + g_debug("converting from %u:%u:%u:%u", ao->in_audio_format.sample_rate, ao->in_audio_format.bits, - ao->in_audio_format.channels); + ao->in_audio_format.channels, + ao->in_audio_format.reverse_endian); } static void diff --git a/src/pcm_byteswap.c b/src/pcm_byteswap.c new file mode 100644 index 000000000..6bdec1f24 --- /dev/null +++ b/src/pcm_byteswap.c @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2003-2009 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "pcm_byteswap.h" +#include "pcm_buffer.h" + +#include + +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "pcm" + +static inline uint16_t swab16(uint16_t x) +{ + return (x << 8) | (x >> 8); +} + +const int16_t *pcm_byteswap_16(struct pcm_buffer *buffer, + const int16_t *src, size_t len) +{ + unsigned i; + int16_t *buf = pcm_buffer_get(buffer, len); + + if (!buf) + return NULL; + + for (i = 0; i < len / 2; i++) + buf[i] = swab16(src[i]); + + return buf; +} + +static inline uint32_t swab32(uint32_t x) +{ + return (x << 24) | + ((x & 0xff00) << 8) | + ((x & 0xff0000) >> 8) | + (x >> 24); +} + +const int32_t *pcm_byteswap_32(struct pcm_buffer *buffer, + const int32_t *src, size_t len) +{ + unsigned i; + int32_t *buf = pcm_buffer_get(buffer, len); + + if (!buf) + return NULL; + + for (i = 0; i < len / 4; i++) + buf[i] = swab32(src[i]); + + return buf; +} diff --git a/src/pcm_byteswap.h b/src/pcm_byteswap.h new file mode 100644 index 000000000..e1196d9b2 --- /dev/null +++ b/src/pcm_byteswap.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2003-2009 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_PCM_BYTESWAP_H +#define MPD_PCM_BYTESWAP_H + +#include +#include + +struct pcm_buffer; + +/** + * Changes the endianness of 16 bit PCM data. + * + * @param buffer the destination pcm_buffer object + * @param src the source PCM buffer + * @param src_size the number of bytes in #src + * @return the destination buffer + */ +const int16_t *pcm_byteswap_16(struct pcm_buffer *buffer, + const int16_t *src, size_t len); + +/** + * Changes the endianness of 32-bit (or 24-bit) PCM data. + * + * @param buffer the destination pcm_buffer object + * @param src the source PCM buffer + * @param src_size the number of bytes in #src + * @return the destination buffer + */ +const int32_t *pcm_byteswap_32(struct pcm_buffer *buffer, + const int32_t *src, size_t len); + +#endif diff --git a/src/pcm_convert.c b/src/pcm_convert.c index ebb4adff5..2d72628b2 100644 --- a/src/pcm_convert.c +++ b/src/pcm_convert.c @@ -20,6 +20,7 @@ #include "pcm_convert.h" #include "pcm_channels.h" #include "pcm_format.h" +#include "pcm_byteswap.h" #include "audio_format.h" #include @@ -83,6 +84,12 @@ pcm_convert_16(struct pcm_convert_state *state, dest_format->sample_rate, &len); + if (dest_format->reverse_endian) { + buf = pcm_byteswap_16(&state->format_buffer, buf, len); + if (!buf) + g_error("pcm_byteswap_16() failed"); + } + *dest_size_r = len; return buf; } @@ -120,6 +127,12 @@ pcm_convert_24(struct pcm_convert_state *state, dest_format->sample_rate, &len); + if (dest_format->reverse_endian) { + buf = pcm_byteswap_32(&state->format_buffer, buf, len); + if (!buf) + g_error("pcm_byteswap_32() failed"); + } + *dest_size_r = len; return buf; } @@ -157,6 +170,12 @@ pcm_convert_32(struct pcm_convert_state *state, dest_format->sample_rate, &len); + if (dest_format->reverse_endian) { + buf = pcm_byteswap_32(&state->format_buffer, buf, len); + if (!buf) + g_error("pcm_byteswap_32() failed"); + } + *dest_size_r = len; return buf; } -- cgit v1.2.3