Add support for CTRL_(key) on input.conf
[mplayer/kovensky.git] / libaf / af_ladspa.c
blob78a9be26d104b60bb0fa961b11494aef459bebc4
1 /*
2 * LADSPA plugin loader
4 * Written by Ivo van Poorten <ivop@euronet.nl>
5 * Copyright (C) 2004, 2005
7 * This file is part of MPlayer.
9 * MPlayer is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * MPlayer is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 /* ------------------------------------------------------------------------- */
26 /* Global Includes */
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include <inttypes.h>
33 #include <math.h>
34 #include <limits.h>
36 #include <dlfcn.h>
37 #include <ladspa.h>
39 /* ------------------------------------------------------------------------- */
41 /* Local Includes */
43 #include "af.h"
44 #include "help_mp.h"
46 /* ------------------------------------------------------------------------- */
48 /* Filter specific data */
50 typedef struct af_ladspa_s
52 int status; /**< Status of the filter.
53 * Either AF_OK or AF_ERROR
54 * Because MPlayer re-inits audio filters that
55 * _clearly_ returned AF_ERROR anyway, I use this
56 * in play() to skip the processing and return
57 * the data unchanged.
60 int activated; /**< 0 or 1. Activate LADSPA filters only once, even
61 * if the buffers get resized, to avoid a stuttering
62 * filter.
65 char *file;
66 char *label;
68 char *myname; /**< It's easy to have a concatenation of file and label */
70 void *libhandle;
71 const LADSPA_Descriptor *plugin_descriptor;
73 int nports;
75 int ninputs;
76 int *inputs;
78 int noutputs;
79 int *outputs;
81 int ninputcontrols;
82 int *inputcontrolsmap; /**< Map input port number [0-] to actual port */
83 float *inputcontrols;
85 int noutputcontrols;
86 int *outputcontrolsmap;
87 float *outputcontrols;
89 int nch; /**< number of channels */
90 int bufsize;
91 float **inbufs;
92 float **outbufs;
93 LADSPA_Handle *chhandles;
95 } af_ladspa_t;
97 /* ------------------------------------------------------------------------- */
99 static int af_open(af_instance_t *af);
100 static int af_ladspa_malloc_failed(char*);
102 /* ------------------------------------------------------------------------- */
104 /* Description */
106 af_info_t af_info_ladspa = {
107 "LADSPA plugin loader",
108 "ladspa",
109 "Ivo van Poorten",
111 AF_FLAGS_REENTRANT,
112 af_open
115 /* ------------------------------------------------------------------------- */
117 /* By lack of a better word (in my vocabulary) this is called 'parse'.
118 * Feel free to suggest an alternative.
121 /** \brief Check for inputs, outputs and controls of a given filter.
123 * This function counts and checks all input, output and control ports
124 * of the filter that was loaded. If it turns out to be a valid
125 * filter for MPlayer use, it prints out a list of all controls and
126 * the corresponding range of its value at message level MSGL_V.
128 * \param setup Current setup of the filter. Must have its
129 * plugin_descriptor set!
131 * \return Returns AF_OK if it has a valid input/output/controls
132 * configuration. Else, it returns AF_ERROR.
135 static int af_ladspa_parse_plugin(af_ladspa_t *setup) {
136 int p, i;
137 const LADSPA_Descriptor *pdes = setup->plugin_descriptor;
138 LADSPA_PortDescriptor d;
139 LADSPA_PortRangeHint hint;
141 if (!setup->libhandle)
142 return AF_ERROR; /* only call parse after a succesful load */
143 if (!setup->plugin_descriptor)
144 return AF_ERROR; /* same as above */
146 /* let's do it */
148 setup->nports = pdes->PortCount;
150 /* allocate memory for all inputs/outputs/controls */
152 setup->inputs = calloc(setup->nports, sizeof(int));
153 if (!setup->inputs) return af_ladspa_malloc_failed(setup->myname);
155 setup->outputs = calloc(setup->nports, sizeof(int));
156 if (!setup->outputs) return af_ladspa_malloc_failed(setup->myname);
158 setup->inputcontrolsmap = calloc(setup->nports, sizeof(int));
159 if (!setup->inputcontrolsmap) return af_ladspa_malloc_failed(setup->myname);
161 setup->inputcontrols = calloc(setup->nports, sizeof(float));
162 if (!setup->inputcontrols) return af_ladspa_malloc_failed(setup->myname);
164 setup->outputcontrolsmap = calloc(setup->nports, sizeof(int));
165 if (!setup->outputcontrolsmap) return af_ladspa_malloc_failed(setup->myname);
167 setup->outputcontrols = calloc(setup->nports, sizeof(float));
168 if (!setup->outputcontrols) return af_ladspa_malloc_failed(setup->myname);
170 /* set counts to zero */
172 setup->ninputs = 0;
173 setup->noutputs = 0;
174 setup->ninputcontrols = 0;
175 setup->noutputcontrols = 0;
177 /* check all ports, see what type it is and set variables according to
178 * what we have found
181 for (p=0; p<setup->nports; p++) {
182 d = pdes->PortDescriptors[p];
184 if (LADSPA_IS_PORT_AUDIO(d)) {
185 if (LADSPA_IS_PORT_INPUT(d)) {
186 setup->inputs[setup->ninputs] = p;
187 setup->ninputs++;
188 } else if (LADSPA_IS_PORT_OUTPUT(d)) {
189 setup->outputs[setup->noutputs] = p;
190 setup->noutputs++;
194 if (LADSPA_IS_PORT_CONTROL(d)) {
195 if (LADSPA_IS_PORT_INPUT(d)) {
196 setup->inputcontrolsmap[setup->ninputcontrols] = p;
197 setup->ninputcontrols++;
198 /* set control to zero. set values after reading the rest
199 * of the suboptions and check LADSPA_?_HINT's later.
201 setup->inputcontrols[p] = 0.0f;
202 } else if (LADSPA_IS_PORT_OUTPUT(d)) {
203 /* read and handle these too, otherwise filters that have them
204 * will sig11
206 setup->outputcontrolsmap[setup->noutputcontrols]=p;
207 setup->noutputcontrols++;
208 setup->outputcontrols[p] = 0.0f;
214 if (setup->ninputs == 0) {
215 mp_msg(MSGT_AFILTER, MSGL_WARN, "%s: %s\n", setup->myname,
216 _("WARNING! This LADSPA plugin has no audio inputs.\n The incoming audio signal will be lost."));
217 } else if (setup->ninputs == 1) {
218 mp_msg(MSGT_AFILTER, MSGL_V, "%s: this is a mono effect\n", setup->myname);
219 } else if (setup->ninputs == 2) {
220 mp_msg(MSGT_AFILTER, MSGL_V, "%s: this is a stereo effect\n", setup->myname);
221 } else {
222 mp_msg(MSGT_AFILTER, MSGL_V, "%s: this is a %i-channel effect, "
223 "support is experimental\n", setup->myname, setup->ninputs);
226 if (setup->noutputs == 0) {
227 mp_msg(MSGT_AFILTER, MSGL_ERR, "%s: %s\n", setup->myname,
228 _("This LADSPA plugin has no audio outputs."));
229 return AF_ERROR;
232 if (setup->noutputs != setup->ninputs ) {
233 mp_msg(MSGT_AFILTER, MSGL_ERR, "%s: %s\n", setup->myname,
234 _("The number of audio inputs and audio outputs of the LADSPA plugin differ."));
235 return AF_ERROR;
238 mp_msg(MSGT_AFILTER, MSGL_V, "%s: this plugin has %d input control(s)\n",
239 setup->myname, setup->ninputcontrols);
241 /* Print list of controls and its range of values it accepts */
243 for (i=0; i<setup->ninputcontrols; i++) {
244 p = setup->inputcontrolsmap[i];
245 hint = pdes->PortRangeHints[p];
246 mp_msg(MSGT_AFILTER, MSGL_V, " --- %d %s [", i, pdes->PortNames[p]);
248 if (LADSPA_IS_HINT_BOUNDED_BELOW(hint.HintDescriptor)) {
249 mp_msg(MSGT_AFILTER, MSGL_V, "%0.2f , ", hint.LowerBound);
250 } else {
251 mp_msg(MSGT_AFILTER, MSGL_V, "... , ");
254 if (LADSPA_IS_HINT_BOUNDED_ABOVE(hint.HintDescriptor)) {
255 mp_msg(MSGT_AFILTER, MSGL_V, "%0.2f]\n", hint.UpperBound);
256 } else {
257 mp_msg(MSGT_AFILTER, MSGL_V, "...]\n");
262 return AF_OK;
265 /* ------------------------------------------------------------------------- */
267 /* This function might "slightly" look like dlopenLADSPA in the LADSPA SDK :-)
268 * But, I changed a few things, because imho it was broken. It did not support
269 * relative paths, only absolute paths that start with a /
270 * I think ../../some/dir/foobar.so is just as valid. And if one wants to call
271 * his library '...somename...so' he's crazy, but it should be allowed.
272 * So, search the path first, try plain *filename later.
273 * Also, try adding .so first! I like the recursion the SDK did, but it's
274 * better the other way around. -af ladspa=cmt:amp_stereo:0.5 is easier to type
275 * than -af ladspa=cmt.so:amp_stereo:0.5 :-))
278 /** \brief dlopen() wrapper
280 * This is a wrapper around dlopen(). It tries various variations of the
281 * filename (with or without the addition of the .so extension) in various
282 * directories specified by the LADSPA_PATH environment variable. If all fails
283 * it tries the filename directly as an absolute path to the library.
285 * \param filename filename of the library to load.
286 * \param flag see dlopen(3) for a description of the flags.
288 * \return returns a pointer to the loaded library on success, or
289 * NULL if it fails to load.
292 static void* mydlopen(const char *filename, int flag) {
293 char *buf;
294 const char *end, *start, *ladspapath;
295 int endsinso, needslash;
296 size_t filenamelen;
297 void *result = NULL;
299 #if defined(__MINGW32__) || defined(__CYGWIN__)
300 /* For Windows there's only absolute path support.
301 * If you have a Windows machine, feel free to fix this.
302 * (path separator, shared objects extension, et cetera). */
303 mp_msg(MSGT_AFILTER, MSGL_V, "\ton windows, only absolute pathnames "
304 "are supported\n");
305 mp_msg(MSGT_AFILTER, MSGL_V, "\ttrying %s\n", filename);
306 return dlopen(filename, flag);
307 #endif
309 filenamelen = strlen(filename);
311 endsinso = 0;
312 if (filenamelen > 3)
313 endsinso = (strcmp(filename+filenamelen-3, ".so") == 0);
314 if (!endsinso) {
315 buf=malloc(filenamelen+4);
316 strcpy(buf, filename);
317 strcat(buf, ".so");
318 result=mydlopen(buf, flag);
319 free(buf);
322 if (result)
323 return result;
325 ladspapath=getenv("LADSPA_PATH");
327 if (ladspapath) {
329 start=ladspapath;
330 while (*start != '\0') {
331 end=start;
332 while ( (*end != ':') && (*end != '\0') )
333 end++;
335 buf=malloc(filenamelen + 2 + (end-start) );
336 if (end > start)
337 strncpy(buf, start, end-start);
338 needslash=0;
339 if (end > start)
340 if (*(end-1) != '/') {
341 needslash = 1;
342 buf[end-start] = '/';
344 strcpy(buf+needslash+(end-start), filename);
346 mp_msg(MSGT_AFILTER, MSGL_V, "\ttrying %s\n", buf);
347 result=dlopen(buf, flag);
349 free(buf);
350 if (result)
351 return result;
353 start = end;
354 if (*start == ':')
355 start++;
356 } /* end while there's still more in the path */
357 } /* end if there's a ladspapath */
359 /* last resort, just open it again, so the dlerror() message is correct */
360 mp_msg(MSGT_AFILTER, MSGL_V, "\ttrying %s\n", filename);
361 return dlopen(filename,flag);
364 /* ------------------------------------------------------------------------- */
366 /** \brief Load a LADSPA Plugin
368 * This function loads the LADSPA plugin specified by the file and label
369 * that are present in the setup variable. First, it loads the library.
370 * If it fails, it returns AF_ERROR. If not, it continues to look for the
371 * specified label. If it finds it, it sets the plugin_descriptor inside
372 * setup and returns AF_OK. If it doesn't, it returns AF_ERROR. Special case
373 * is a label called 'help'. In that case, it prints a list of all available
374 * labels (filters) in the library specified by file.
376 * \param setup Current setup of the filter. Contains filename and label.
378 * \return Either AF_ERROR or AF_OK, depending on the success of the operation.
381 static int af_ladspa_load_plugin(af_ladspa_t *setup) {
382 const LADSPA_Descriptor *ladspa_descriptor;
383 LADSPA_Descriptor_Function descriptor_function;
384 int i;
386 /* load library */
387 mp_msg(MSGT_AFILTER, MSGL_V, "%s: loading ladspa plugin library %s\n",
388 setup->myname, setup->file);
390 setup->libhandle = mydlopen(setup->file, RTLD_NOW);
392 if (!setup->libhandle) {
393 mp_msg(MSGT_AFILTER, MSGL_ERR, "%s: %s %s\n\t%s\n", setup->myname,
394 _("failed to load"), setup->file, dlerror() );
395 return AF_ERROR;
398 mp_msg(MSGT_AFILTER, MSGL_V, "%s: library found.\n", setup->myname);
400 /* find descriptor function */
401 dlerror();
402 descriptor_function = (LADSPA_Descriptor_Function) dlsym (setup->libhandle,
403 "ladspa_descriptor");
405 if (!descriptor_function) {
406 mp_msg(MSGT_AFILTER, MSGL_ERR, "%s: %s\n\t%s\n", setup->myname,
407 _("Couldn't find ladspa_descriptor() function in the specified library file."), dlerror());
408 return AF_ERROR;
411 /* if label == help, list all labels in library and exit */
413 if (strcmp(setup->label, "help") == 0) {
414 mp_msg(MSGT_AFILTER, MSGL_INFO, "%s: %s %s:\n", setup->myname,
415 _("available labels in"), setup->file);
416 for (i=0; ; i++) {
417 ladspa_descriptor = descriptor_function(i);
418 if (ladspa_descriptor == NULL) {
419 return AF_ERROR;
421 mp_msg(MSGT_AFILTER, MSGL_INFO, " %-16s - %s (%lu)\n",
422 ladspa_descriptor->Label,
423 ladspa_descriptor->Name,
424 ladspa_descriptor->UniqueID);
428 mp_msg(MSGT_AFILTER, MSGL_V, "%s: looking for label\n", setup->myname);
430 /* find label in library */
431 for (i=0; ; i++) {
432 ladspa_descriptor = descriptor_function(i);
433 if (ladspa_descriptor == NULL) {
434 mp_msg(MSGT_AFILTER, MSGL_ERR, "%s: %s\n", setup->myname,
435 _("Couldn't find label in plugin library."));
436 return AF_ERROR;
438 if (strcmp(ladspa_descriptor->Label, setup->label) == 0) {
439 setup->plugin_descriptor = ladspa_descriptor;
440 mp_msg(MSGT_AFILTER, MSGL_V, "%s: %s found\n", setup->myname,
441 setup->label);
442 return AF_OK;
446 return AF_OK;
449 /* ------------------------------------------------------------------------- */
451 /** \brief Print a malloc() failed error message.
453 * Generic function which can be called if a call to malloc(), calloc(),
454 * strdup(), et cetera, failed. It prints a message to the console and
455 * returns AF_ERROR.
457 * \return AF_ERROR
460 static int af_ladspa_malloc_failed(char *myname) {
461 mp_msg(MSGT_AFILTER, MSGL_ERR, "%s: %s", myname, "Memory allocation failed.\n");
462 return AF_ERROR;
465 /* ------------------------------------------------------------------------- */
467 /** \brief Controls the filter.
469 * Control the behaviour of the filter.
471 * Commands:
472 * CONTROL_REINIT Sets the af structure with proper values for number
473 * of channels, rate, format, et cetera.
474 * CONTROL_COMMAND_LINE Parses the suboptions given to this filter
475 * through arg. It first parses the filename and
476 * the label. After that, it loads the filter
477 * and finds out its proprties. Then in continues
478 * parsing the controls given on the commandline,
479 * if any are needed.
481 * \param af Audio filter instance
482 * \param cmd The command to execute
483 * \param arg Arguments to the command
485 * \return Either AF_ERROR or AF_OK, depending on the succes of the
486 * operation.
489 static int control(struct af_instance_s *af, int cmd, void *arg) {
490 af_ladspa_t *setup = (af_ladspa_t*) af->setup;
491 int i, r;
492 float val;
494 switch(cmd) {
495 case AF_CONTROL_REINIT:
496 mp_msg(MSGT_AFILTER, MSGL_V, "%s: (re)init\n", setup->myname);
498 if (!arg) return AF_ERROR;
500 /* accept FLOAT, let af_format do conversion */
502 af->data->rate = ((af_data_t*)arg)->rate;
503 af->data->nch = ((af_data_t*)arg)->nch;
504 af->data->format = AF_FORMAT_FLOAT_NE;
505 af->data->bps = 4;
507 /* arg->len is not set here yet, so init of buffers and connecting the
508 * filter, has to be done in play() :-/
511 return af_test_output(af, (af_data_t*)arg);
512 case AF_CONTROL_COMMAND_LINE: {
513 char *buf;
515 mp_msg(MSGT_AFILTER, MSGL_V, "%s: parse suboptions\n", setup->myname);
517 /* suboption parser here!
518 * format is (ladspa=)file:label:controls....
521 if (!arg) {
522 mp_msg(MSGT_AFILTER, MSGL_ERR, "%s: %s\n", setup->myname,
523 _("No suboptions specified."));
524 return AF_ERROR;
527 buf = malloc(strlen(arg)+1);
528 if (!buf) return af_ladspa_malloc_failed(setup->myname);
530 /* file... */
531 buf[0] = '\0';
532 sscanf(arg, "%[^:]", buf);
533 if (buf[0] == '\0') {
534 mp_msg(MSGT_AFILTER, MSGL_ERR, "%s: %s\n", setup->myname,
535 _("No library file specified."));
536 free(buf);
537 return AF_ERROR;
539 arg += strlen(buf);
540 setup->file = strdup(buf);
541 if (!setup->file) return af_ladspa_malloc_failed(setup->myname);
542 mp_msg(MSGT_AFILTER, MSGL_V, "%s: file --> %s\n", setup->myname,
543 setup->file);
544 if (*(char*)arg != '\0') arg++; /* read ':' */
546 /* label... */
547 buf[0] = '\0';
548 sscanf(arg, "%[^:]", buf);
549 if (buf[0] == '\0') {
550 mp_msg(MSGT_AFILTER, MSGL_ERR, "%s: %s\n", setup->myname,
551 _("No filter label specified."));
552 free(buf);
553 return AF_ERROR;
555 arg += strlen(buf);
556 setup->label = strdup(buf);
557 if (!setup->label) return af_ladspa_malloc_failed(setup->myname);
558 mp_msg(MSGT_AFILTER, MSGL_V, "%s: label --> %s\n", setup->myname,
559 setup->label);
560 /* if (*(char*)arg != '0') arg++; */ /* read ':' */
562 free(buf); /* no longer needed */
564 /* set new setup->myname */
566 if(setup->myname) free(setup->myname);
567 setup->myname = calloc(strlen(af_info_ladspa.name)+strlen(setup->file)+
568 strlen(setup->label)+6, 1);
569 snprintf(setup->myname, strlen(af_info_ladspa.name)+
570 strlen(setup->file)+strlen(setup->label)+6, "%s: (%s:%s)",
571 af_info_ladspa.name, setup->file, setup->label);
573 /* load plugin :) */
575 if ( af_ladspa_load_plugin(setup) != AF_OK )
576 return AF_ERROR;
578 /* see what inputs, outputs and controls this plugin has */
579 if ( af_ladspa_parse_plugin(setup) != AF_OK )
580 return AF_ERROR;
582 /* ninputcontrols is set by now, read control values from arg */
584 for(i=0; i<setup->ninputcontrols; i++) {
585 if (!arg || (*(char*)arg != ':') ) {
586 mp_msg(MSGT_AFILTER, MSGL_ERR, "%s: %s\n", setup->myname,
587 _("Not enough controls specified on the command line."));
588 return AF_ERROR;
590 arg++;
591 r = sscanf(arg, "%f", &val);
592 if (r!=1) {
593 mp_msg(MSGT_AFILTER, MSGL_ERR, "%s: %s\n", setup->myname,
594 _("Not enough controls specified on the command line."));
595 return AF_ERROR;
597 setup->inputcontrols[setup->inputcontrolsmap[i]] = val;
598 arg = strchr(arg, ':');
601 mp_msg(MSGT_AFILTER, MSGL_V, "%s: input controls: ", setup->myname);
602 for(i=0; i<setup->ninputcontrols; i++) {
603 mp_msg(MSGT_AFILTER, MSGL_V, "%0.4f ",
604 setup->inputcontrols[setup->inputcontrolsmap[i]]);
606 mp_msg(MSGT_AFILTER, MSGL_V, "\n");
608 /* check boundaries of inputcontrols */
610 mp_msg(MSGT_AFILTER, MSGL_V, "%s: checking boundaries of input controls\n",
611 setup->myname);
612 for(i=0; i<setup->ninputcontrols; i++) {
613 int p = setup->inputcontrolsmap[i];
614 LADSPA_PortRangeHint hint =
615 setup->plugin_descriptor->PortRangeHints[p];
616 val = setup->inputcontrols[p];
618 if (LADSPA_IS_HINT_BOUNDED_BELOW(hint.HintDescriptor) &&
619 val < hint.LowerBound) {
620 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "%s: Input control #%d is below lower boundary of %0.4f.\n",
621 setup->myname, i, hint.LowerBound);
622 return AF_ERROR;
624 if (LADSPA_IS_HINT_BOUNDED_ABOVE(hint.HintDescriptor) &&
625 val > hint.UpperBound) {
626 mp_tmsg(MSGT_AFILTER, MSGL_ERR, "%s: Input control #%d is above upper boundary of %0.4f.\n",
627 setup->myname, i, hint.UpperBound);
628 return AF_ERROR;
631 mp_msg(MSGT_AFILTER, MSGL_V, "%s: all controls have sane values\n",
632 setup->myname);
634 /* All is well! */
635 setup->status = AF_OK;
637 return AF_OK; }
640 return AF_UNKNOWN;
643 /* ------------------------------------------------------------------------- */
645 /** \brief Uninitialise the LADSPA Plugin Loader filter.
647 * This function deactivates the plugin(s), cleans up, frees all allocated
648 * memory and exits.
650 * \return No return value.
653 static void uninit(struct af_instance_s *af) {
654 int i;
656 if (af->data)
657 free(af->data);
658 if (af->setup) {
659 af_ladspa_t *setup = (af_ladspa_t*) af->setup;
660 const LADSPA_Descriptor *pdes = setup->plugin_descriptor;
662 if (setup->myname) {
663 mp_msg(MSGT_AFILTER, MSGL_V, "%s: cleaning up\n", setup->myname);
664 free(setup->myname);
667 if (setup->chhandles) {
668 for(i=0; i<setup->nch; i+=setup->ninputs) {
669 if (pdes->deactivate) pdes->deactivate(setup->chhandles[i]);
670 if (pdes->cleanup) pdes->cleanup(setup->chhandles[i]);
672 free(setup->chhandles);
675 if (setup->file)
676 free(setup->file);
677 if (setup->label)
678 free(setup->label);
679 if (setup->inputcontrolsmap)
680 free(setup->inputcontrolsmap);
681 if (setup->inputcontrols)
682 free(setup->inputcontrols);
683 if (setup->outputcontrolsmap)
684 free(setup->outputcontrolsmap);
685 if (setup->outputcontrols)
686 free(setup->outputcontrols);
687 if (setup->inputs)
688 free(setup->inputs);
689 if (setup->outputs)
690 free(setup->outputs);
692 if (setup->inbufs) {
693 for(i=0; i<setup->nch; i++) {
694 if (setup->inbufs[i])
695 free(setup->inbufs[i]);
697 free(setup->inbufs);
700 if (setup->outbufs) {
701 for(i=0; i<setup->nch; i++) {
702 if (setup->outbufs[i])
703 free(setup->outbufs[i]);
705 free(setup->outbufs);
708 if (setup->libhandle)
709 dlclose(setup->libhandle);
711 free(setup);
712 setup = NULL;
716 /* ------------------------------------------------------------------------- */
718 /** \brief Process chunk of audio data through the selected LADSPA Plugin.
720 * \param af Pointer to audio filter instance
721 * \param data Pointer to chunk of audio data
723 * \return Either AF_ERROR or AF_OK
726 static af_data_t* play(struct af_instance_s *af, af_data_t *data) {
727 af_ladspa_t *setup = af->setup;
728 const LADSPA_Descriptor *pdes = setup->plugin_descriptor;
729 float *audio = (float*)data->audio;
730 int nsamples = data->len/4; /* /4 because it's 32-bit float */
731 int nch = data->nch;
732 int rate = data->rate;
733 int i, p;
735 if (setup->status !=AF_OK)
736 return data;
738 /* See if it's the first call. If so, setup inbufs/outbufs, instantiate
739 * plugin, connect ports and activate plugin
742 /* 2004-12-07: Also check if the buffersize has to be changed!
743 * data->len is not constant per se! re-init buffers.
746 if ( (setup->bufsize != nsamples/nch) || (setup->nch != nch) ) {
748 /* if setup->nch==0, it's the first call, if not, something has
749 * changed and all previous mallocs have to be freed
752 if (setup->nch != 0) {
753 mp_msg(MSGT_AFILTER, MSGL_DBG3, "%s: bufsize change; free old buffer\n",
754 setup->myname);
756 if(setup->inbufs) {
757 for(i=0; i<setup->nch; i++) {
758 if(setup->inbufs[i])
759 free(setup->inbufs[i]);
761 free(setup->inbufs);
763 if(setup->outbufs) {
764 for(i=0; i<setup->nch; i++) {
765 if(setup->outbufs[i])
766 free(setup->outbufs[i]);
768 free(setup->outbufs);
770 } /* everything is freed */
772 setup->bufsize = nsamples/nch;
773 setup->nch = nch;
775 setup->inbufs = calloc(nch, sizeof(float*));
776 setup->outbufs = calloc(nch, sizeof(float*));
778 mp_msg(MSGT_AFILTER, MSGL_DBG3, "%s: bufsize = %d\n",
779 setup->myname, setup->bufsize);
781 for(i=0; i<nch; i++) {
782 setup->inbufs[i] = calloc(setup->bufsize, sizeof(float));
783 setup->outbufs[i] = calloc(setup->bufsize, sizeof(float));
786 /* only on the first call, there are no handles. */
788 if (!setup->chhandles) {
789 setup->chhandles = calloc(nch, sizeof(LADSPA_Handle));
791 /* create handles
792 * for stereo effects, create one handle for two channels
795 for(i=0; i<nch; i++) {
797 if (i % setup->ninputs) { /* stereo effect */
798 /* copy the handle from previous channel */
799 setup->chhandles[i] = setup->chhandles[i-1];
800 continue;
803 setup->chhandles[i] = pdes->instantiate(pdes, rate);
807 /* connect input/output ports for each channel/filter instance
809 * always (re)connect ports
812 for(i=0; i<nch; i++) {
813 pdes->connect_port(setup->chhandles[i],
814 setup->inputs[i % setup->ninputs],
815 setup->inbufs[i]);
816 pdes->connect_port(setup->chhandles[i],
817 setup->outputs[i % setup->ninputs],
818 setup->outbufs[i]);
820 /* connect (input) controls */
822 for (p=0; p<setup->nports; p++) {
823 LADSPA_PortDescriptor d = pdes->PortDescriptors[p];
824 if (LADSPA_IS_PORT_CONTROL(d)) {
825 if (LADSPA_IS_PORT_INPUT(d)) {
826 pdes->connect_port(setup->chhandles[i], p,
827 &(setup->inputcontrols[p]) );
828 } else {
829 pdes->connect_port(setup->chhandles[i], p,
830 &(setup->outputcontrols[p]) );
835 /* Activate filter (if it isn't already :) ) */
837 if ( (pdes->activate) && (setup->activated == 0) ) {
838 pdes->activate(setup->chhandles[i]);
839 setup->activated = 1;
842 } /* All channels/filters done! except for... */
844 /* Stereo effect with one channel left. Use same buffer for left
845 * and right. connect it to the second port.
848 for (p = i; p % setup->ninputs; p++) {
849 pdes->connect_port(setup->chhandles[i-1],
850 setup->inputs[p % setup->ninputs],
851 setup->inbufs[i-1]);
852 pdes->connect_port(setup->chhandles[i-1],
853 setup->outputs[p % setup->ninputs],
854 setup->outbufs[i-1]);
855 } /* done! */
857 } /* setup for first call/change of bufsize is done.
858 * normal playing routine follows...
861 /* Right now, I use a separate input and output buffer.
862 * I could change this to in-place processing (inbuf==outbuf), but some
863 * ladspa filters are broken and are not able to handle that. This seems
864 * fast enough, so unless somebody complains, it stays this way :)
867 /* Fill inbufs */
869 for (p=0; p<setup->bufsize; p++) {
870 for (i=0; i<nch; i++) {
871 setup->inbufs[i][p] = audio[p*nch + i];
875 /* Run filter(s) */
877 for (i=0; i<nch; i+=setup->ninputs) {
878 pdes->run(setup->chhandles[i], setup->bufsize);
881 /* Extract outbufs */
883 for (p=0; p<setup->bufsize; p++) {
884 for (i=0; i<nch; i++) {
885 audio[p*nch + i] = setup->outbufs[i][p];
889 /* done */
891 return data;
894 /* ------------------------------------------------------------------------- */
896 /** \brief Open LADSPA Plugin Loader Filter
898 * \param af Audio Filter instance
900 * \return Either AF_ERROR or AF_OK
903 static int af_open(af_instance_t *af) {
905 af->control=control;
906 af->uninit=uninit;
907 af->play=play;
908 af->mul=1;
910 af->data = calloc(1, sizeof(af_data_t));
911 if (af->data == NULL)
912 return af_ladspa_malloc_failed((char*)af_info_ladspa.name);
914 af->setup = calloc(1, sizeof(af_ladspa_t));
915 if (af->setup == NULL) {
916 free(af->data);
917 af->data=NULL;
918 return af_ladspa_malloc_failed((char*)af_info_ladspa.name);
921 ((af_ladspa_t*)af->setup)->status = AF_ERROR; /* will be set to AF_OK if
922 * all went OK and play()
923 * should proceed.
926 ((af_ladspa_t*)af->setup)->myname = strdup(af_info_ladspa.name);
927 if (!((af_ladspa_t*)af->setup)->myname)
928 return af_ladspa_malloc_failed((char*)af_info_ladspa.name);
930 return AF_OK;
933 /* ------------------------------------------------------------------------- */