output/{alsa,oss}: move endian code to new library pcm_export

This commit is contained in:
Max Kellermann 2012-03-21 19:54:35 +01:00
parent a5d1444ef4
commit 170635e3a6
5 changed files with 168 additions and 61 deletions

View File

@ -399,6 +399,7 @@ libutil_a_SOURCES = \
libpcm_a_SOURCES = \
src/pcm_buffer.c src/pcm_buffer.h \
src/pcm_export.c src/pcm_export.h \
src/pcm_convert.c src/pcm_convert.h \
src/dsd2pcm/dsd2pcm.c src/dsd2pcm/dsd2pcm.h \
src/pcm_dsd.c src/pcm_dsd.h \

View File

@ -21,8 +21,7 @@
#include "alsa_output_plugin.h"
#include "output_api.h"
#include "mixer_list.h"
#include "pcm_buffer.h"
#include "pcm_byteswap.h"
#include "pcm_export.h"
#include <glib.h>
#include <alsa/asoundlib.h>
@ -47,12 +46,7 @@ typedef snd_pcm_sframes_t alsa_writei_t(snd_pcm_t * pcm, const void *buffer,
struct alsa_data {
struct audio_output base;
/**
* The buffer used to reverse the byte order.
*
* @see #reverse_endian
*/
struct pcm_buffer reverse_buffer;
struct pcm_export_state export;
/** the configured name of the ALSA device; NULL for the
default device */
@ -61,21 +55,6 @@ struct alsa_data {
/** use memory mapped I/O? */
bool use_mmap;
/**
* Does ALSA expect samples in reverse byte order? (i.e. not
* host byte order)
*
* This attribute is only valid while the device is open.
*/
bool reverse_endian;
/**
* Which sample format is being sent to the play() method?
*
* This attribute is only valid while the device is open.
*/
enum sample_format sample_format;
/** libasound's buffer_time setting (in microseconds) */
unsigned int buffer_time;
@ -196,7 +175,7 @@ alsa_output_enable(struct audio_output *ao, G_GNUC_UNUSED GError **error_r)
{
struct alsa_data *ad = (struct alsa_data *)ao;
pcm_buffer_init(&ad->reverse_buffer);
pcm_export_init(&ad->export);
return true;
}
@ -205,7 +184,7 @@ alsa_output_disable(struct audio_output *ao)
{
struct alsa_data *ad = (struct alsa_data *)ao;
pcm_buffer_deinit(&ad->reverse_buffer);
pcm_export_deinit(&ad->export);
}
static bool
@ -434,8 +413,9 @@ configure_hw:
ad->writei = snd_pcm_writei;
}
bool reverse_endian;
err = alsa_output_setup_format(ad->pcm, hwparams, audio_format,
&ad->reverse_endian);
&reverse_endian);
if (err < 0) {
g_set_error(error, alsa_output_quark(), err,
"ALSA device \"%s\" does not support format %s: %s",
@ -445,8 +425,6 @@ configure_hw:
return false;
}
ad->sample_format = audio_format->format;
err = snd_pcm_hw_params_set_channels_near(ad->pcm, hwparams,
&channels);
if (err < 0) {
@ -579,6 +557,9 @@ configure_hw:
ad->period_frames = alsa_period_size;
ad->period_position = 0;
pcm_export_open(&ad->export, audio_format->format,
reverse_endian);
return true;
error:
@ -710,9 +691,7 @@ alsa_play(struct audio_output *ao, const void *chunk, size_t size,
{
struct alsa_data *ad = (struct alsa_data *)ao;
if (ad->reverse_endian)
chunk = pcm_byteswap(&ad->reverse_buffer, ad->sample_format,
chunk, size);
chunk = pcm_export(&ad->export, chunk, size, &size);
size /= ad->frame_size;

View File

@ -52,20 +52,14 @@
#endif
#ifdef AFMT_S24_PACKED
#include "pcm_buffer.h"
#include "pcm_byteswap.h"
#include "pcm_export.h"
#endif
struct oss_data {
struct audio_output base;
#ifdef AFMT_S24_PACKED
/**
* The buffer used to reverse the byte order.
*
* @see #reverse_endian
*/
struct pcm_buffer reverse_buffer;
struct pcm_export_state export;
#endif
int fd;
@ -76,16 +70,6 @@ struct oss_data {
* the device after cancel().
*/
struct audio_format audio_format;
#ifdef AFMT_S24_PACKED
/**
* Does OSS expect samples in reverse byte order? (i.e. not
* host byte order)
*
* This attribute is only valid while the device is open.
*/
bool reverse_endian;
#endif
};
/**
@ -252,7 +236,7 @@ oss_output_enable(struct audio_output *ao, G_GNUC_UNUSED GError **error_r)
{
struct oss_data *od = (struct oss_data *)ao;
pcm_buffer_init(&od->reverse_buffer);
pcm_export_init(&od->export);
return true;
}
@ -261,7 +245,7 @@ oss_output_disable(struct audio_output *ao)
{
struct oss_data *od = (struct oss_data *)ao;
pcm_buffer_deinit(&od->reverse_buffer);
pcm_export_deinit(&od->export);
}
#endif
@ -517,7 +501,7 @@ sample_format_from_oss(int format)
static bool
oss_setup_sample_format(int fd, struct audio_format *audio_format,
#ifdef AFMT_S24_PACKED
bool *reverse_endian_r,
struct pcm_export_state *export,
#endif
GError **error_r)
{
@ -537,8 +521,9 @@ oss_setup_sample_format(int fd, struct audio_format *audio_format,
audio_format->format = mpd_format;
#ifdef AFMT_S24_PACKED
*reverse_endian_r = oss_format == AFMT_S24_PACKED &&
G_BYTE_ORDER != G_LITTLE_ENDIAN;
pcm_export_open(export, mpd_format,
oss_format == AFMT_S24_PACKED &&
G_BYTE_ORDER != G_LITTLE_ENDIAN);
#endif
return true;
@ -583,8 +568,9 @@ oss_setup_sample_format(int fd, struct audio_format *audio_format,
audio_format->format = mpd_format;
#ifdef AFMT_S24_PACKED
*reverse_endian_r = oss_format == AFMT_S24_PACKED &&
G_BYTE_ORDER != G_LITTLE_ENDIAN;
pcm_export_open(export, mpd_format,
oss_format == AFMT_S24_PACKED &&
G_BYTE_ORDER != G_LITTLE_ENDIAN);
#endif
return true;
@ -611,7 +597,7 @@ oss_setup(struct oss_data *od, struct audio_format *audio_format,
oss_setup_sample_rate(od->fd, audio_format, error_r) &&
oss_setup_sample_format(od->fd, audio_format,
#ifdef AFMT_S24_PACKED
&od->reverse_endian,
&od->export,
#endif
error_r);
}
@ -726,10 +712,7 @@ oss_output_play(struct audio_output *ao, const void *chunk, size_t size,
return 0;
#ifdef AFMT_S24_PACKED
if (od->reverse_endian)
chunk = pcm_byteswap(&od->reverse_buffer,
od->audio_format.format,
chunk, size);
chunk = pcm_export(&od->export, chunk, size, &size);
#endif
while (true) {

54
src/pcm_export.c Normal file
View File

@ -0,0 +1,54 @@
/*
* Copyright (C) 2003-2012 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 "config.h"
#include "pcm_export.h"
#include "pcm_byteswap.h"
void
pcm_export_init(struct pcm_export_state *state)
{
pcm_buffer_init(&state->reverse_buffer);
}
void pcm_export_deinit(struct pcm_export_state *state)
{
pcm_buffer_deinit(&state->reverse_buffer);
}
void
pcm_export_open(struct pcm_export_state *state,
enum sample_format sample_format,
bool reverse_endian)
{
state->sample_format = sample_format;
state->reverse_endian = reverse_endian;
}
const void *
pcm_export(struct pcm_export_state *state, const void *data, size_t size,
size_t *dest_size_r)
{
if (state->reverse_endian)
data = pcm_byteswap(&state->reverse_buffer,
state->sample_format, data, size);
*dest_size_r = size;
return data;
}

90
src/pcm_export.h Normal file
View File

@ -0,0 +1,90 @@
/*
* Copyright (C) 2003-2012 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 PCM_EXPORT_H
#define PCM_EXPORT_H
#include "check.h"
#include "pcm_buffer.h"
#include "audio_format.h"
#include <stdbool.h>
struct audio_format;
/**
* An object that handles export of PCM samples to some instance
* outside of MPD. It has a few more options to tweak the binary
* representation which are not supported by the pcm_convert library.
*/
struct pcm_export_state {
/**
* The buffer used to reverse the byte order.
*
* @see #reverse_endian
*/
struct pcm_buffer reverse_buffer;
enum sample_format sample_format;
/**
* Export the samples in reverse byte order?
*/
bool reverse_endian;
};
/**
* Initialize a #pcm_export_state object.
*/
void
pcm_export_init(struct pcm_export_state *state);
/**
* Deinitialize a #pcm_export_state object and free allocated memory.
*/
void
pcm_export_deinit(struct pcm_export_state *state);
/**
* Open the #pcm_export_state object.
*
* There is no "close" method. This function may be called multiple
* times to reuse the object, until pcm_export_deinit() is called.
*
* This function cannot fail.
*/
void
pcm_export_open(struct pcm_export_state *state,
enum sample_format sample_format,
bool reverse_endian);
/**
* Export a PCM buffer.
*
* @param state an initialized and open pcm_export_state object
* @param src the source PCM buffer
* @param src_size the size of #src in bytes
* @param dest_size_r returns the number of bytes of the destination buffer
* @return the destination buffer (may be a pointer to the source buffer)
*/
const void *
pcm_export(struct pcm_export_state *state, const void *src, size_t src_size,
size_t *dest_size_r);
#endif