Support for multichannel DSP processing in module-ladspa-sink

This commit is contained in:
Kim Therkelsen 2010-10-15 09:14:31 +02:00 committed by Colin Guthrie
parent a3dbdb0446
commit 6bd32ee2d9

View file

@ -56,13 +56,18 @@ PA_MODULE_USAGE(
"format=<sample format> "
"rate=<sample rate> "
"channels=<number of channels> "
"channel_map=<channel map> "
"channel_map=<input channel map> "
"plugin=<ladspa plugin name> "
"label=<ladspa plugin label> "
"control=<comma seperated list of input control values>"));
"control=<comma seperated list of input control values> "
"input_ladspaport_map=<comma separated list of input LADSPA port names> "
"output_ladspaport_map=<comma separated list of output LADSPA port names> "));
#define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
/* PLEASE NOTICE: The PortAudio ports and the LADSPA ports are two different concepts.
They are not related and where possible the names of the LADSPA port variables contains "ladspa" to avoid confusion */
struct userdata {
pa_module *module;
@ -70,11 +75,10 @@ struct userdata {
pa_sink_input *sink_input;
const LADSPA_Descriptor *descriptor;
unsigned channels;
LADSPA_Handle handle[PA_CHANNELS_MAX];
LADSPA_Data *input, *output;
unsigned long max_ladspaport_count, input_count, output_count, channels;
LADSPA_Data **input, **output;
size_t block_size;
unsigned long input_port, output_port;
LADSPA_Data *control;
/* This is a dummy buffer. Every port must be connected, but we don't care
@ -97,6 +101,8 @@ static const char* const valid_modargs[] = {
"plugin",
"label",
"control",
"input_ladspaport_map",
"output_ladspaport_map",
NULL
};
@ -213,7 +219,7 @@ static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk
struct userdata *u;
float *src, *dst;
size_t fs;
unsigned n, c;
unsigned n, h, c;
pa_memchunk tchunk;
pa_sink_input_assert_ref(i);
@ -248,10 +254,12 @@ static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk
src = (float*) ((uint8_t*) pa_memblock_acquire(tchunk.memblock) + tchunk.index);
dst = (float*) pa_memblock_acquire(chunk->memblock);
for (c = 0; c < u->channels; c++) {
pa_sample_clamp(PA_SAMPLE_FLOAT32NE, u->input, sizeof(float), src+c, u->channels*sizeof(float), n);
u->descriptor->run(u->handle[c], n);
pa_sample_clamp(PA_SAMPLE_FLOAT32NE, dst+c, u->channels*sizeof(float), u->output, sizeof(float), n);
for (h = 0; h < (u->channels / u->max_ladspaport_count); h++) {
for (c = 0; c < u->input_count; c++)
pa_sample_clamp(PA_SAMPLE_FLOAT32NE, u->input[c], sizeof(float), src+ h*u->max_ladspaport_count + c, u->channels*sizeof(float), n);
u->descriptor->run(u->handle[h], n);
for (c = 0; c < u->output_count; c++)
pa_sample_clamp(PA_SAMPLE_FLOAT32NE, dst + h*u->max_ladspaport_count + c, u->channels*sizeof(float), u->output[c], sizeof(float), n);
}
pa_memblock_release(tchunk.memblock);
@ -286,10 +294,10 @@ static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
/* Reset the plugin */
if (u->descriptor->deactivate)
for (c = 0; c < u->channels; c++)
for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
u->descriptor->deactivate(u->handle[c]);
if (u->descriptor->activate)
for (c = 0; c < u->channels; c++)
for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
u->descriptor->activate(u->handle[c]);
}
}
@ -471,14 +479,13 @@ int pa__init(pa_module*m) {
pa_sink *master;
pa_sink_input_new_data sink_input_data;
pa_sink_new_data sink_data;
const char *plugin, *label;
const char *plugin, *label, *input_ladspaport_map, *output_ladspaport_map;
LADSPA_Descriptor_Function descriptor_func;
unsigned long input_ladspaport[PA_CHANNELS_MAX], output_ladspaport[PA_CHANNELS_MAX];
const char *e, *cdata;
const LADSPA_Descriptor *d;
unsigned long input_port, output_port, p, j, n_control;
unsigned c;
unsigned long p, h, j, n_control, c;
pa_bool_t *use_default = NULL;
pa_memchunk silence;
pa_assert(m);
@ -512,15 +519,22 @@ int pa__init(pa_module*m) {
goto fail;
}
if (!(input_ladspaport_map = pa_modargs_get_value(ma, "input_ladspaport_map", NULL)))
pa_log_debug("Using default input ladspa port mapping");
if (!(output_ladspaport_map = pa_modargs_get_value(ma, "output_ladspaport_map", NULL)))
pa_log_debug("Using default output ladspa port mapping");
cdata = pa_modargs_get_value(ma, "control", NULL);
u = pa_xnew0(struct userdata, 1);
u->module = m;
m->userdata = u;
pa_silence_memchunk_get(&m->core->silence_cache, m->core->mempool, &silence, &ss, 0);
u->memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, pa_frame_size(&ss), 1, 1, 0, &silence);
pa_memblock_unref(silence.memblock);
u->memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, pa_frame_size(&ss), 1, 1, 0, NULL);
u->max_ladspaport_count = 1; /*to avoid division by zero etc. in pa__done when failing before this value has been set*/
u->channels = 0;
u->input = NULL;
u->output = NULL;
if (!(e = getenv("LADSPA_PATH")))
e = LADSPA_PATH;
@ -562,64 +576,127 @@ int pa__init(pa_module*m) {
pa_log_debug("Maker: %s", d->Maker);
pa_log_debug("Copyright: %s", d->Copyright);
input_port = output_port = (unsigned long) -1;
n_control = 0;
u->channels = ss.channels;
/*
* Enumerate ladspa ports
* Default mapping is in order given by the plugin
*/
for (p = 0; p < d->PortCount; p++) {
if (LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p])) {
pa_log_debug("Port %lu is input: %s", p, d->PortNames[p]);
input_ladspaport[u->input_count] = p;
u->input_count++;
} else if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
pa_log_debug("Port %lu is output: %s", p, d->PortNames[p]);
output_ladspaport[u->output_count] = p;
u->output_count++;
}
} else if (LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]) && LADSPA_IS_PORT_INPUT(d->PortDescriptors[p])) {
pa_log_debug("Port %lu is control: %s", p, d->PortNames[p]);
n_control++;
} else
pa_log_debug("Ignored port %s", d->PortNames[p]);
/* XXX: Has anyone ever seen an in-place plugin with non-equal number of input and output ports? */
/* Could be if the plugin is for up-mixing stereo to 5.1 channels */
/* Or if the plugin is down-mixing 5.1 to two channel stereo or binaural encoded signal */
if (u->input_count > u->max_ladspaport_count)
u->max_ladspaport_count = u->input_count;
else
u->max_ladspaport_count = u->output_count;
}
if (u->channels % u->max_ladspaport_count) {
pa_log("Cannot handle non-integral number of plugins required for given number of channels");
goto fail;
}
pa_log_debug("Will run %lu plugin instances", u->channels / u->max_ladspaport_count);
/* Parse data for input ladspa port map */
if (input_ladspaport_map) {
const char *state = NULL;
char *pname;
c = 0;
while ((pname = pa_split(input_ladspaport_map, ",", &state))) {
if (c == u->input_count) {
pa_log("Too many ports in input ladspa port map");
goto fail;
}
for (p = 0; p < d->PortCount; p++) {
if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
if (strcmp(d->PortNames[p], "Input") == 0) {
pa_assert(input_port == (unsigned long) -1);
input_port = p;
if (strcmp(d->PortNames[p], pname) == 0) {
if (LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p]) && LADSPA_IS_PORT_INPUT(d->PortDescriptors[p])) {
input_ladspaport[c] = p;
} else {
pa_log("Found audio input port on plugin we cannot handle: %s", d->PortNames[p]);
pa_log("Port %s is not an audio input ladspa port", pname);
pa_xfree(pname);
goto fail;
}
}
}
c++;
pa_xfree(pname);
}
}
} else if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
if (strcmp(d->PortNames[p], "Output") == 0) {
pa_assert(output_port == (unsigned long) -1);
output_port = p;
/* Parse data for output port map */
if (output_ladspaport_map) {
const char *state = NULL;
char *pname;
c = 0;
while ((pname = pa_split(output_ladspaport_map, ",", &state))) {
if (c == u->output_count) {
pa_log("Too many ports in output ladspa port map");
goto fail;
}
for (p = 0; p < d->PortCount; p++) {
if (strcmp(d->PortNames[p], pname) == 0) {
if (LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p]) && LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
output_ladspaport[c] = p;
} else {
pa_log("Found audio output port on plugin we cannot handle: %s", d->PortNames[p]);
pa_log("Port %s is not an output ladspa port", pname);
pa_xfree(pname);
goto fail;
}
} else if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
n_control++;
else {
pa_assert(LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]));
pa_log_debug("Ignored control output port \"%s\".", d->PortNames[p]);
}
}
c++;
pa_xfree(pname);
}
}
if ((input_port == (unsigned long) -1) || (output_port == (unsigned long) -1)) {
pa_log("Failed to identify input and output ports. "
"Right now this module can only deal with plugins which provide an 'Input' and an 'Output' audio port. "
"Patches welcome!");
goto fail;
}
u->block_size = pa_frame_align(pa_mempool_block_size_max(m->core->mempool), &ss);
u->input = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
if (LADSPA_IS_INPLACE_BROKEN(d->Properties))
u->output = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
else
/* Create buffers */
if (LADSPA_IS_INPLACE_BROKEN(d->Properties)) {
u->input = (LADSPA_Data**) pa_xnew(LADSPA_Data*, (unsigned) u->input_count);
for (c = 0; c < u->input_count; c++)
u->input[c] = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
u->output = (LADSPA_Data**) pa_xnew(LADSPA_Data*, (unsigned) u->output_count);
for (c = 0; c < u->output_count; c++)
u->output[c] = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
} else {
u->input = (LADSPA_Data**) pa_xnew(LADSPA_Data*, (unsigned) u->max_ladspaport_count);
for (c = 0; c < u->max_ladspaport_count; c++)
u->input[c] = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
u->output = u->input;
u->channels = ss.channels;
for (c = 0; c < ss.channels; c++) {
if (!(u->handle[c] = d->instantiate(d, ss.rate))) {
pa_log("Failed to instantiate plugin %s with label %s for channel %i", plugin, d->Label, c);
}
/* Initialize plugin instances */
for (h = 0; h < (u->channels / u->max_ladspaport_count); h++) {
if (!(u->handle[h] = d->instantiate(d, ss.rate))) {
pa_log("Failed to instantiate plugin %s with label %s", plugin, d->Label);
goto fail;
}
d->connect_port(u->handle[c], input_port, u->input);
d->connect_port(u->handle[c], output_port, u->output);
for (c = 0; c < u->input_count; c++)
d->connect_port(u->handle[h], input_ladspaport[c], u->input[c]);
for (c = 0; c < u->output_count; c++)
d->connect_port(u->handle[h], output_ladspaport[c], u->output[c]);
}
if (!cdata && n_control > 0) {
@ -630,7 +707,6 @@ int pa__init(pa_module*m) {
if (n_control > 0) {
const char *state = NULL;
char *k;
unsigned long h;
u->control = pa_xnew(LADSPA_Data, (unsigned) n_control);
use_default = pa_xnew(pa_bool_t, (unsigned) n_control);
@ -684,7 +760,7 @@ int pa__init(pa_module*m) {
continue;
if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
for (c = 0; c < ss.channels; c++)
for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
d->connect_port(u->handle[c], p, &u->control_out);
continue;
}
@ -764,7 +840,7 @@ int pa__init(pa_module*m) {
pa_log_debug("Binding %f to port %s", u->control[h], d->PortNames[p]);
for (c = 0; c < ss.channels; c++)
for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
d->connect_port(u->handle[c], p, &u->control[h]);
h++;
@ -774,7 +850,7 @@ int pa__init(pa_module*m) {
}
if (d->activate)
for (c = 0; c < u->channels; c++)
for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
d->activate(u->handle[c]);
/* Create sink */
@ -866,7 +942,6 @@ int pa__init(pa_module*m) {
pa_sink_input_put(u->sink_input);
pa_modargs_free(ma);
pa_xfree(use_default);
return 0;
@ -915,21 +990,36 @@ void pa__done(pa_module*m) {
if (u->sink)
pa_sink_unref(u->sink);
for (c = 0; c < u->channels; c++)
for (c = 0; c < (u->channels / u->max_ladspaport_count); c++) {
if (u->handle[c]) {
if (u->descriptor->deactivate)
u->descriptor->deactivate(u->handle[c]);
u->descriptor->cleanup(u->handle[c]);
}
}
if (u->output != u->input)
if (u->output == u->input) {
if (u->input != NULL) {
for (c = 0; c < u->max_ladspaport_count; c++)
pa_xfree(u->input[c]);
pa_xfree(u->input);
}
} else {
if (u->input != NULL) {
for (c = 0; c < u->input_count; c++)
pa_xfree(u->input[c]);
pa_xfree(u->input);
}
if (u->output != NULL) {
for (c = 0; c < u->output_count; c++)
pa_xfree(u->output[c]);
pa_xfree(u->output);
}
}
if (u->memblockq)
pa_memblockq_free(u->memblockq);
pa_xfree(u->input);
pa_xfree(u->control);
pa_xfree(u);
}