gst-indent
[platform/upstream/gst-plugins-good.git] / ext / ladspa / gstladspa.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *               <2001> Steve Baker <stevebaker_org@yahoo.co.uk>
4  *               2003 Andy Wingo <wingo at pobox.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <string.h>
26 #include <math.h>
27 #include <gst/control/control.h>
28 #include <gst/audio/audio.h>
29
30 #include "gstladspa.h"
31 #include <ladspa.h>             /* main ladspa sdk include file */
32 #include "utils.h"              /* ladspa sdk utility functions */
33
34 /* 1.0 and the 1.1 preliminary headers don't define a version, but 1.1 final
35    does */
36 #ifndef LADSPA_VERSION
37 #define LADSPA_VERSION "1.0"
38 #endif
39
40 static GstStaticCaps ladspa_pad_caps =
41 GST_STATIC_CAPS (GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_CAPS);
42
43 static void gst_ladspa_class_init (GstLADSPAClass * klass);
44 static void gst_ladspa_base_init (GstLADSPAClass * klass);
45 static void gst_ladspa_init (GstLADSPA * ladspa);
46
47 static void gst_ladspa_update_int (const GValue * value, gpointer data);
48 static GstPadLinkReturn gst_ladspa_link (GstPad * pad, const GstCaps * caps);
49
50 static void gst_ladspa_set_property (GObject * object, guint prop_id,
51     const GValue * value, GParamSpec * pspec);
52 static void gst_ladspa_get_property (GObject * object, guint prop_id,
53     GValue * value, GParamSpec * pspec);
54
55 static gboolean gst_ladspa_instantiate (GstLADSPA * ladspa);
56 static void gst_ladspa_activate (GstLADSPA * ladspa);
57 static void gst_ladspa_deactivate (GstLADSPA * ladspa);
58
59 static GstElementStateReturn gst_ladspa_change_state (GstElement * element);
60 static void gst_ladspa_loop (GstElement * element);
61 static void gst_ladspa_chain (GstPad * pad, GstData * _data);
62 static GstData *gst_ladspa_get (GstPad * pad);
63
64 static GstElementClass *parent_class = NULL;
65
66 static GstPlugin *ladspa_plugin;
67 static GHashTable *ladspa_descriptors;
68
69 enum
70 {
71   ARG_0,
72   ARG_SAMPLERATE,
73   ARG_BUFFERSIZE,
74   ARG_LAST,
75 };
76
77 GST_DEBUG_CATEGORY_STATIC (ladspa_debug);
78 #define DEBUG(...) \
79     GST_CAT_LEVEL_LOG (ladspa_debug, GST_LEVEL_DEBUG, NULL, __VA_ARGS__)
80 #define DEBUG_OBJ(obj,...) \
81     GST_CAT_LEVEL_LOG (ladspa_debug, GST_LEVEL_DEBUG, obj, __VA_ARGS__)
82
83 static void
84 gst_ladspa_base_init (GstLADSPAClass * klass)
85 {
86   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
87   GstPadTemplate *templ;
88   GstElementDetails *details;
89   LADSPA_Descriptor *desc;
90   gint j, sinkcount, srccount;
91
92   desc = g_hash_table_lookup (ladspa_descriptors,
93       GINT_TO_POINTER (G_TYPE_FROM_CLASS (klass)));
94   if (!desc)
95     desc = g_hash_table_lookup (ladspa_descriptors, GINT_TO_POINTER (0));
96   g_assert (desc);
97
98   /* pad templates */
99   klass->numports = desc->PortCount;
100   klass->numsinkpads = 0;
101   klass->numsrcpads = 0;
102   for (j = 0; j < desc->PortCount; j++) {
103     if (LADSPA_IS_PORT_AUDIO (desc->PortDescriptors[j])) {
104       gchar *name = g_strdup ((gchar *) desc->PortNames[j]);
105
106       g_strcanon (name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
107
108       /* the factories take ownership of the name */
109       if (LADSPA_IS_PORT_INPUT (desc->PortDescriptors[j])) {
110         templ = gst_pad_template_new (name, GST_PAD_SINK, GST_PAD_ALWAYS,
111             gst_caps_copy (gst_static_caps_get (&ladspa_pad_caps)));
112         klass->numsinkpads++;
113       } else {
114         templ = gst_pad_template_new (name, GST_PAD_SRC, GST_PAD_ALWAYS,
115             gst_caps_copy (gst_static_caps_get (&ladspa_pad_caps)));
116         klass->numsrcpads++;
117       }
118
119       gst_element_class_add_pad_template (element_class, templ);
120     }
121   }
122
123   /* construct the element details struct */
124   details = g_new0 (GstElementDetails, 1);
125   details->longname = g_strdup (desc->Name);
126   details->description = details->longname;
127   details->author = g_strdup (desc->Maker);
128   if ((klass->numsinkpads > 0) && (klass->numsrcpads > 0))
129     details->klass = "Filter/Effect/Audio/LADSPA";
130   else if ((klass->numsinkpads == 0) && (klass->numsrcpads > 0))
131     details->klass = "Source/Audio/LADSPA";
132   else if ((klass->numsinkpads > 0) && (klass->numsrcpads == 0))
133     details->klass = "Sink/Audio/LADSPA";
134   else
135     details->klass = "Filter/Effect/Audio/LADSPA";      /* whatever this is */
136   gst_element_class_set_details (element_class, details);
137
138   klass->srcpad_portnums = g_new0 (gint, klass->numsrcpads);
139   klass->sinkpad_portnums = g_new0 (gint, klass->numsinkpads);
140   sinkcount = 0;
141   srccount = 0;
142
143   /* walk through the ports, note the portnums for srcpads, sinkpads */
144   for (j = 0; j < desc->PortCount; j++) {
145     if (LADSPA_IS_PORT_AUDIO (desc->PortDescriptors[j])) {
146       if (LADSPA_IS_PORT_INPUT (desc->PortDescriptors[j]))
147         klass->sinkpad_portnums[sinkcount++] = j;
148       else
149         klass->srcpad_portnums[srccount++] = j;
150     }
151   }
152
153   klass->descriptor = desc;
154 }
155
156 static void
157 gst_ladspa_class_init (GstLADSPAClass * klass)
158 {
159   GObjectClass *gobject_class;
160   GstElementClass *gstelement_class;
161   LADSPA_Descriptor *desc;
162   gint i, current_portnum, controlcount;
163   gint hintdesc;
164   gint argtype, argperms;
165   GParamSpec *paramspec = NULL;
166   gchar *argname, *tempstr, *paren;
167
168   gobject_class = (GObjectClass *) klass;
169   gstelement_class = (GstElementClass *) klass;
170
171   gobject_class->set_property = gst_ladspa_set_property;
172   gobject_class->get_property = gst_ladspa_get_property;
173
174   gstelement_class->change_state = gst_ladspa_change_state;
175
176   /* look up and store the ladspa descriptor */
177   desc = g_hash_table_lookup (ladspa_descriptors,
178       GINT_TO_POINTER (G_TYPE_FROM_CLASS (klass)));
179   if (!desc)
180     desc = g_hash_table_lookup (ladspa_descriptors, GINT_TO_POINTER (0));
181   g_assert (desc);
182
183   klass->numcontrols = 0;
184
185   /* walk through the ports, count the input, output and control ports */
186   for (i = 0; i < desc->PortCount; i++) {
187     if (!LADSPA_IS_PORT_AUDIO (desc->PortDescriptors[i]) &&
188         LADSPA_IS_PORT_INPUT (desc->PortDescriptors[i]))
189       klass->numcontrols++;
190   }
191
192   DEBUG ("ladspa element class: init %s with %d sink, %d src, %d control\n",
193       g_type_name (G_TYPE_FROM_CLASS (klass)),
194       klass->numsinkpads, klass->numsrcpads, klass->numcontrols);
195
196   klass->control_portnums = g_new0 (gint, klass->numcontrols);
197   controlcount = 0;
198
199   /* walk through the ports, note the portnums for control params */
200   for (i = 0; i < desc->PortCount; i++) {
201     if (!LADSPA_IS_PORT_AUDIO (desc->PortDescriptors[i]) &&
202         LADSPA_IS_PORT_INPUT (desc->PortDescriptors[i]))
203       klass->control_portnums[controlcount++] = i;
204   }
205
206   /* now build the control info from the control ports */
207   klass->control_info = g_new0 (ladspa_control_info, klass->numcontrols);
208
209   for (i = 0; i < klass->numcontrols; i++) {
210     current_portnum = klass->control_portnums[i];
211
212     /* short name for hint descriptor */
213     hintdesc = desc->PortRangeHints[current_portnum].HintDescriptor;
214
215     /* get the various bits */
216     if (LADSPA_IS_HINT_TOGGLED (hintdesc))
217       klass->control_info[i].toggled = TRUE;
218     if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
219       klass->control_info[i].logarithmic = TRUE;
220     if (LADSPA_IS_HINT_INTEGER (hintdesc))
221       klass->control_info[i].integer = TRUE;
222
223     /* figure out the argument details */
224     if (klass->control_info[i].toggled)
225       argtype = G_TYPE_BOOLEAN;
226     else if (klass->control_info[i].integer)
227       argtype = G_TYPE_INT;
228     else
229       argtype = G_TYPE_FLOAT;
230
231     /* grab the bounds */
232     if (LADSPA_IS_HINT_BOUNDED_BELOW (hintdesc)) {
233       klass->control_info[i].lower = TRUE;
234       klass->control_info[i].lowerbound =
235           desc->PortRangeHints[current_portnum].LowerBound;
236     } else {
237       if (argtype == G_TYPE_INT)
238         klass->control_info[i].lowerbound = (gfloat) G_MININT;
239       if (argtype == G_TYPE_FLOAT)
240         klass->control_info[i].lowerbound = -G_MAXFLOAT;
241     }
242
243     if (LADSPA_IS_HINT_BOUNDED_ABOVE (hintdesc)) {
244       klass->control_info[i].upper = TRUE;
245       klass->control_info[i].upperbound =
246           desc->PortRangeHints[current_portnum].UpperBound;
247       if (LADSPA_IS_HINT_SAMPLE_RATE (hintdesc)) {
248         klass->control_info[i].samplerate = TRUE;
249         klass->control_info[i].upperbound *= 44100;     /* FIXME? */
250       }
251     } else {
252       if (argtype == G_TYPE_INT)
253         klass->control_info[i].upperbound = (gfloat) G_MAXINT;
254       if (argtype == G_TYPE_FLOAT)
255         klass->control_info[i].upperbound = G_MAXFLOAT;
256     }
257
258     /* use the lowerbound as the default value */
259     klass->control_info[i].def = klass->control_info[i].lowerbound;
260
261 #ifdef LADSPA_IS_HINT_HAS_DEFAULT
262     /* figure out the defaults */
263     if (LADSPA_IS_HINT_HAS_DEFAULT (hintdesc)) {
264       if (LADSPA_IS_HINT_DEFAULT_MINIMUM (hintdesc))
265         klass->control_info[i].def = klass->control_info[i].lowerbound;
266       else if (LADSPA_IS_HINT_DEFAULT_LOW (hintdesc))
267         if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
268           klass->control_info[i].def =
269               exp (0.75 * log (klass->control_info[i].lowerbound) +
270               0.25 * log (klass->control_info[i].upperbound));
271         else
272           klass->control_info[i].def =
273               (0.75 * klass->control_info[i].lowerbound +
274               0.25 * klass->control_info[i].upperbound);
275       else if (LADSPA_IS_HINT_DEFAULT_MIDDLE (hintdesc))
276         if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
277           klass->control_info[i].def =
278               exp (0.5 * log (klass->control_info[i].lowerbound) +
279               0.5 * log (klass->control_info[i].upperbound));
280         else
281           klass->control_info[i].def =
282               (0.5 * klass->control_info[i].lowerbound +
283               0.5 * klass->control_info[i].upperbound);
284       else if (LADSPA_IS_HINT_DEFAULT_HIGH (hintdesc))
285         if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
286           klass->control_info[i].def =
287               exp (0.25 * log (klass->control_info[i].lowerbound) +
288               0.75 * log (klass->control_info[i].upperbound));
289         else
290           klass->control_info[i].def =
291               (0.25 * klass->control_info[i].lowerbound +
292               0.75 * klass->control_info[i].upperbound);
293       else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM (hintdesc))
294         klass->control_info[i].def = klass->control_info[i].upperbound;
295       else if (LADSPA_IS_HINT_DEFAULT_0 (hintdesc))
296         klass->control_info[i].def = 0.0;
297       else if (LADSPA_IS_HINT_DEFAULT_1 (hintdesc))
298         klass->control_info[i].def = 1.0;
299       else if (LADSPA_IS_HINT_DEFAULT_100 (hintdesc))
300         klass->control_info[i].def = 100.0;
301       else if (LADSPA_IS_HINT_DEFAULT_440 (hintdesc))
302         klass->control_info[i].def = 440.0;
303     }
304 #endif /* LADSPA_IS_HINT_HAS_DEFAULT */
305
306     klass->control_info[i].def = CLAMP (klass->control_info[i].def,
307         klass->control_info[i].lowerbound, klass->control_info[i].upperbound);
308
309     if (LADSPA_IS_PORT_INPUT (desc->PortDescriptors[current_portnum])) {
310       argperms = G_PARAM_READWRITE;
311       klass->control_info[i].writable = TRUE;
312     } else {
313       argperms = G_PARAM_READABLE;
314       klass->control_info[i].writable = FALSE;
315     }
316
317     klass->control_info[i].name = g_strdup (desc->PortNames[current_portnum]);
318     argname = g_strdup (klass->control_info[i].name);
319     /* find out if there is a (unitname) at the end of the argname and get rid
320        of it */
321     paren = g_strrstr (argname, " (");
322     if (paren != NULL) {
323       *paren = '\0';
324     }
325     /* this is the same thing that param_spec_* will do */
326     g_strcanon (argname, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
327     /* satisfy glib2 (argname[0] must be [A-Za-z]) */
328     if (!((argname[0] >= 'a' && argname[0] <= 'z') || (argname[0] >= 'A'
329                 && argname[0] <= 'Z'))) {
330       tempstr = argname;
331       argname = g_strconcat ("param-", argname, NULL);
332       g_free (tempstr);
333     }
334
335     /* check for duplicate property names */
336     if (g_object_class_find_property (G_OBJECT_CLASS (klass), argname) != NULL) {
337       gint numarg = 1;
338       gchar *numargname = g_strdup_printf ("%s_%d", argname, numarg++);
339
340       while (g_object_class_find_property (G_OBJECT_CLASS (klass),
341               numargname) != NULL) {
342         g_free (numargname);
343         numargname = g_strdup_printf ("%s_%d", argname, numarg++);
344       }
345       argname = numargname;
346     }
347
348     klass->control_info[i].param_name = argname;
349
350     DEBUG ("adding arg %s from %s", argname, klass->control_info[i].name);
351
352     if (argtype == G_TYPE_BOOLEAN) {
353       paramspec =
354           g_param_spec_boolean (argname, argname, argname, FALSE, argperms);
355     } else if (argtype == G_TYPE_INT) {
356       paramspec = g_param_spec_int (argname, argname, argname,
357           (gint) klass->control_info[i].lowerbound,
358           (gint) klass->control_info[i].upperbound,
359           (gint) klass->control_info[i].def, argperms);
360     } else if (klass->control_info[i].samplerate) {
361       paramspec = g_param_spec_float (argname, argname, argname,
362           0.0, G_MAXFLOAT, 0.0, argperms);
363     } else {
364       paramspec = g_param_spec_float (argname, argname, argname,
365           klass->control_info[i].lowerbound, klass->control_info[i].upperbound,
366           klass->control_info[i].def, argperms);
367     }
368
369     /* properties have an offset of 1 */
370     g_object_class_install_property (G_OBJECT_CLASS (klass), i + 1, paramspec);
371   }
372 }
373
374 static void
375 gst_ladspa_init (GstLADSPA * ladspa)
376 {
377   GstLADSPAClass *oclass;
378   ladspa_control_info cinfo;
379   GList *l;
380   LADSPA_Descriptor *desc;
381   gint i, sinkcount, srccount;
382
383   oclass = (GstLADSPAClass *) G_OBJECT_GET_CLASS (ladspa);
384   desc = oclass->descriptor;
385   ladspa->descriptor = oclass->descriptor;
386
387   /* allocate the various arrays */
388   ladspa->srcpads = g_new0 (GstPad *, oclass->numsrcpads);
389   ladspa->sinkpads = g_new0 (GstPad *, oclass->numsinkpads);
390   ladspa->controls = g_new (gfloat, oclass->numcontrols);
391   ladspa->dpman = gst_dpman_new ("ladspa_dpman", GST_ELEMENT (ladspa));
392
393   /* set up pads */
394   sinkcount = 0;
395   srccount = 0;
396   for (l = GST_ELEMENT_CLASS (oclass)->padtemplates; l; l = l->next) {
397     GstPad *pad = gst_pad_new_from_template (GST_PAD_TEMPLATE (l->data),
398         GST_PAD_TEMPLATE_NAME_TEMPLATE (l->data));
399
400     gst_pad_set_link_function (pad, gst_ladspa_link);
401     gst_element_add_pad ((GstElement *) ladspa, pad);
402
403     if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK)
404       ladspa->sinkpads[sinkcount++] = pad;
405     else
406       ladspa->srcpads[srccount++] = pad;
407   }
408
409   /* set up dparams */
410   for (i = 0; i < oclass->numcontrols; i++) {
411     if (LADSPA_IS_PORT_INPUT (desc->PortDescriptors[i])) {
412       cinfo = oclass->control_info[i];
413       ladspa->controls[i] = cinfo.def;
414
415       if (cinfo.toggled) {
416         gst_dpman_add_required_dparam_callback (ladspa->dpman,
417             g_param_spec_int (cinfo.param_name, cinfo.name, cinfo.name,
418                 0, 1, (gint) (ladspa->controls[i]), G_PARAM_READWRITE),
419             "int", gst_ladspa_update_int, &(ladspa->controls[i])
420             );
421       } else if (cinfo.integer) {
422         gst_dpman_add_required_dparam_callback (ladspa->dpman,
423             g_param_spec_int (cinfo.param_name, cinfo.name, cinfo.name,
424                 (gint) cinfo.lowerbound, (gint) cinfo.upperbound,
425                 (gint) ladspa->controls[i], G_PARAM_READWRITE),
426             "int", gst_ladspa_update_int, &(ladspa->controls[i])
427             );
428       } else if (cinfo.samplerate) {
429         gst_dpman_add_required_dparam_direct (ladspa->dpman,
430             g_param_spec_float (cinfo.param_name, cinfo.name, cinfo.name,
431                 cinfo.lowerbound, cinfo.upperbound,
432                 ladspa->controls[i], G_PARAM_READWRITE),
433             "hertz-rate-bound", &(ladspa->controls[i])
434             );
435       } else {
436         gst_dpman_add_required_dparam_direct (ladspa->dpman,
437             g_param_spec_float (cinfo.param_name, cinfo.name, cinfo.name,
438                 cinfo.lowerbound, cinfo.upperbound,
439                 ladspa->controls[i], G_PARAM_READWRITE),
440             "float", &(ladspa->controls[i])
441             );
442       }
443     }
444   }
445
446   /* nonzero default needed to instantiate() some plugins */
447   ladspa->samplerate = 44100;
448
449   ladspa->buffer_frames = 0;    /* should be set with caps */
450   ladspa->activated = FALSE;
451   ladspa->inplace_broken =
452       LADSPA_IS_INPLACE_BROKEN (ladspa->descriptor->Properties);
453
454   if (sinkcount == 0 && srccount == 1) {
455     /* get mode (no sink pads) */
456     DEBUG_OBJ (ladspa, "mono get mode with 1 src pad");
457
458     gst_pad_set_get_function (ladspa->srcpads[0], gst_ladspa_get);
459   } else if (sinkcount == 1) {
460     /* with one sink we can use the chain function */
461     DEBUG_OBJ (ladspa, "chain mode");
462
463     gst_pad_set_chain_function (ladspa->sinkpads[0], gst_ladspa_chain);
464   } else if (sinkcount > 1) {
465     /* more than one sink pad needs loop mode */
466     DEBUG_OBJ (ladspa, "loop mode with %d sink pads and %d src pads", sinkcount,
467         srccount);
468
469     gst_element_set_loop_function (GST_ELEMENT (ladspa), gst_ladspa_loop);
470   } else if (sinkcount == 0 && srccount == 0) {
471     /* for example, a plugin with only control inputs and output -- just ignore
472      * it for now */
473   } else {
474     g_warning ("%d sink pads, %d src pads not yet supported", sinkcount,
475         srccount);
476   }
477
478   gst_ladspa_instantiate (ladspa);
479 }
480
481 static void
482 gst_ladspa_update_int (const GValue * value, gpointer data)
483 {
484   gfloat *target = (gfloat *) data;
485
486   *target = (gfloat) g_value_get_int (value);
487 }
488
489 static GstPadLinkReturn
490 gst_ladspa_link (GstPad * pad, const GstCaps * caps)
491 {
492   GstElement *element = (GstElement *) GST_PAD_PARENT (pad);
493   GstLADSPA *ladspa = (GstLADSPA *) element;
494   const GList *l = NULL;
495   gint rate;
496   GstStructure *structure;
497
498   /* if this fails in some other plugin, the graph is left in an inconsistent
499      state */
500   for (l = gst_element_get_pad_list (element); l; l = l->next)
501     if (pad != (GstPad *) l->data)
502       if (gst_pad_try_set_caps ((GstPad *) l->data, caps) <= 0)
503         return GST_PAD_LINK_REFUSED;
504
505   /* we assume that the ladspa plugin can handle any sample rate, so this
506      check gets put last */
507   structure = gst_caps_get_structure (caps, 0);
508   gst_structure_get_int (structure, "rate", &rate);
509   /* have to instantiate ladspa plugin when samplerate changes (groan) */
510   if (ladspa->samplerate != rate) {
511     ladspa->samplerate = rate;
512     if (!gst_ladspa_instantiate (ladspa))
513       return GST_PAD_LINK_REFUSED;
514   }
515
516   gst_structure_get_int (structure, "buffer-frames", &ladspa->buffer_frames);
517
518   return GST_PAD_LINK_OK;
519 }
520
521 #if 0
522 static void
523 gst_ladspa_force_src_caps (GstLADSPA * ladspa, GstPad * pad)
524 {
525   if (!ladspa->buffer_frames) {
526     ladspa->buffer_frames = 256;        /* 5 ms at 44100 kHz (just a default...) */
527   }
528
529   DEBUG_OBJ (ladspa, "forcing caps with rate=%d, buffer-frames=%d",
530       ladspa->samplerate, ladspa->buffer_frames);
531
532   gst_pad_try_set_caps (pad,
533       gst_caps_new ("ladspa_src_caps",
534           "audio/x-raw-float",
535           gst_props_new ("width", G_TYPE_INT (32),
536               "endianness", G_TYPE_INT (G_BYTE_ORDER),
537               "rate", G_TYPE_INT (ladspa->samplerate),
538               "buffer-frames", G_TYPE_INT (ladspa->buffer_frames),
539               "channels", G_TYPE_INT (1), NULL)));
540 }
541 #endif
542
543 static void
544 gst_ladspa_set_property (GObject * object, guint prop_id, const GValue * value,
545     GParamSpec * pspec)
546 {
547   GstLADSPA *ladspa = (GstLADSPA *) object;
548   GstLADSPAClass *oclass;
549   ladspa_control_info *control_info;
550
551   oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (object));
552
553   /* remember, properties have an offset of 1 */
554   prop_id--;
555
556   /* verify it exists */
557   g_return_if_fail (prop_id < oclass->numcontrols);
558
559   control_info = &(oclass->control_info[prop_id]);
560   g_return_if_fail (control_info->name != NULL);
561
562   /* check to see if it's writable */
563   g_return_if_fail (control_info->writable);
564
565   /* now see what type it is */
566   if (control_info->toggled)
567     ladspa->controls[prop_id] = g_value_get_boolean (value) ? 1.f : 0.f;
568   else if (control_info->integer)
569     ladspa->controls[prop_id] = g_value_get_int (value);
570   else
571     ladspa->controls[prop_id] = g_value_get_float (value);
572
573   DEBUG_OBJ (object, "set arg %s to %f", control_info->name,
574       ladspa->controls[prop_id]);
575 }
576
577 static void
578 gst_ladspa_get_property (GObject * object, guint prop_id, GValue * value,
579     GParamSpec * pspec)
580 {
581   GstLADSPA *ladspa = (GstLADSPA *) object;
582   GstLADSPAClass *oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (object));
583   ladspa_control_info *control_info;
584
585   /* remember, properties have an offset of 1 */
586   prop_id--;
587
588   /* verify it exists */
589   g_return_if_fail (prop_id < oclass->numcontrols);
590
591   control_info = &(oclass->control_info[prop_id]);
592   g_return_if_fail (control_info->name != NULL);
593
594   /* now see what type it is */
595   if (control_info->toggled)
596     g_value_set_boolean (value, ladspa->controls[prop_id] == 1.0);
597   else if (control_info->integer)
598     g_value_set_int (value, (gint) ladspa->controls[prop_id]);
599   else
600     g_value_set_float (value, ladspa->controls[prop_id]);
601
602   DEBUG_OBJ (object, "got arg %s as %f", control_info->name,
603       ladspa->controls[prop_id]);
604 }
605
606 static gboolean
607 gst_ladspa_instantiate (GstLADSPA * ladspa)
608 {
609   LADSPA_Descriptor *desc;
610   int i;
611   GstLADSPAClass *oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
612   gboolean was_activated;
613
614   desc = ladspa->descriptor;
615
616   /* check for old handle */
617   was_activated = ladspa->activated;
618   if (ladspa->handle != NULL) {
619     gst_ladspa_deactivate (ladspa);
620     desc->cleanup (ladspa->handle);
621   }
622
623   /* instantiate the plugin */
624   DEBUG_OBJ (ladspa, "instantiating the plugin at %d Hz", ladspa->samplerate);
625
626   ladspa->handle = desc->instantiate (desc, ladspa->samplerate);
627   g_return_val_if_fail (ladspa->handle != NULL, FALSE);
628
629   /* connect the control ports */
630   for (i = 0; i < oclass->numcontrols; i++)
631     desc->connect_port (ladspa->handle,
632         oclass->control_portnums[i], &(ladspa->controls[i]));
633
634   /* reactivate if it was activated before the reinstantiation */
635   if (was_activated)
636     gst_ladspa_activate (ladspa);
637
638   return TRUE;
639 }
640
641 static GstElementStateReturn
642 gst_ladspa_change_state (GstElement * element)
643 {
644   LADSPA_Descriptor *desc;
645   GstLADSPA *ladspa = (GstLADSPA *) element;
646
647   desc = ladspa->descriptor;
648
649   switch (GST_STATE_TRANSITION (element)) {
650     case GST_STATE_NULL_TO_READY:
651       gst_ladspa_activate (ladspa);
652       break;
653     case GST_STATE_READY_TO_NULL:
654       gst_ladspa_deactivate (ladspa);
655       break;
656     default:
657       break;
658   }
659
660   if (GST_ELEMENT_CLASS (parent_class)->change_state)
661     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
662
663   return GST_STATE_SUCCESS;
664 }
665
666 static void
667 gst_ladspa_activate (GstLADSPA * ladspa)
668 {
669   LADSPA_Descriptor *desc;
670
671   desc = ladspa->descriptor;
672
673   if (ladspa->activated)
674     gst_ladspa_deactivate (ladspa);
675
676   DEBUG_OBJ (ladspa, "activating");
677
678   /* activate the plugin (function might be null) */
679   if (desc->activate != NULL)
680     desc->activate (ladspa->handle);
681
682   ladspa->activated = TRUE;
683 }
684
685 static void
686 gst_ladspa_deactivate (GstLADSPA * ladspa)
687 {
688   LADSPA_Descriptor *desc;
689
690   desc = ladspa->descriptor;
691
692   DEBUG_OBJ (ladspa, "deactivating");
693
694   /* deactivate the plugin (function might be null) */
695   if (ladspa->activated && (desc->deactivate != NULL))
696     desc->deactivate (ladspa->handle);
697
698   ladspa->activated = FALSE;
699 }
700
701 static void
702 gst_ladspa_loop (GstElement * element)
703 {
704   guint i, j, numsrcpads, numsinkpads;
705   guint num_processed, num_to_process;
706   gint largest_buffer;
707   LADSPA_Data **data_in, **data_out;
708   GstBuffer **buffers_in, **buffers_out;
709
710   GstLADSPA *ladspa = (GstLADSPA *) element;
711   GstLADSPAClass *oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
712   LADSPA_Descriptor *desc = ladspa->descriptor;
713
714   numsinkpads = oclass->numsinkpads;
715   numsrcpads = oclass->numsrcpads;
716
717   /* fixme: these mallocs need to die */
718   data_in = g_new0 (LADSPA_Data *, numsinkpads);
719   data_out = g_new0 (LADSPA_Data *, numsrcpads);
720   buffers_in = g_new0 (GstBuffer *, numsinkpads);
721   buffers_out = g_new0 (GstBuffer *, numsrcpads);
722
723   largest_buffer = -1;
724
725   /* first get all the necessary data from the input ports */
726   for (i = 0; i < numsinkpads; i++) {
727   get_buffer:
728     buffers_in[i] = GST_BUFFER (gst_pad_pull (ladspa->sinkpads[i]));
729
730     if (GST_IS_EVENT (buffers_in[i])) {
731       /* push it out on all pads */
732       gst_data_ref_by_count ((GstData *) buffers_in[i], numsrcpads);
733       for (j = 0; j < numsrcpads; j++)
734         gst_pad_push (ladspa->srcpads[j], GST_DATA (buffers_in[i]));
735       if (GST_EVENT_TYPE (buffers_in[i]) == GST_EVENT_EOS) {
736         /* shut down */
737         gst_element_set_eos (element);
738         return;
739       } else {
740         goto get_buffer;
741       }
742     }
743
744     if (largest_buffer < 0)
745       largest_buffer = GST_BUFFER_SIZE (buffers_in[i]) / sizeof (gfloat);
746     else
747       largest_buffer =
748           MIN (GST_BUFFER_SIZE (buffers_in[i]) / sizeof (gfloat),
749           largest_buffer);
750     data_in[i] = (LADSPA_Data *) GST_BUFFER_DATA (buffers_in[i]);
751     GST_BUFFER_TIMESTAMP (buffers_in[i]) = ladspa->timestamp;
752   }
753
754   i = 0;
755   if (!ladspa->inplace_broken) {
756     for (; i < numsrcpads && i < numsinkpads; i++) {
757       /* reuse input buffers */
758       buffers_out[i] = buffers_in[i];
759       data_out[i] = data_in[i];
760     }
761   }
762   for (; i < numsrcpads; i++) {
763     buffers_out[i] =
764         gst_buffer_new_and_alloc (ladspa->buffer_frames * sizeof (gfloat));
765     GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
766     data_out[i] = (LADSPA_Data *) GST_BUFFER_DATA (buffers_out[i]);
767   }
768
769   GST_DPMAN_PREPROCESS (ladspa->dpman, largest_buffer, ladspa->timestamp);
770   num_processed = 0;
771
772   /* split up processing of the buffer into chunks so that dparams can
773    * be updated when required.
774    * In many cases the buffer will be processed in one chunk anyway.
775    */
776   while (GST_DPMAN_PROCESS (ladspa->dpman, num_processed)) {
777     num_to_process = GST_DPMAN_FRAMES_TO_PROCESS (ladspa->dpman);
778
779     for (i = 0; i < numsinkpads; i++)
780       desc->connect_port (ladspa->handle, oclass->sinkpad_portnums[i],
781           data_in[i]);
782     for (i = 0; i < numsrcpads; i++)
783       desc->connect_port (ladspa->handle, oclass->srcpad_portnums[i],
784           data_out[i]);
785
786     desc->run (ladspa->handle, num_to_process);
787
788     for (i = 0; i < numsinkpads; i++)
789       data_in[i] += num_to_process;
790     for (i = 0; i < numsrcpads; i++)
791       data_out[i] += num_to_process;
792
793     num_processed += num_to_process;
794   }
795
796   for (i = 0; i < numsinkpads; i++) {
797     if (i >= numsrcpads || buffers_out[i] != buffers_in[i])
798       gst_buffer_unref (buffers_in[i]);
799     data_in[i] = NULL;
800     buffers_in[i] = NULL;
801   }
802   for (i = 0; i < numsrcpads; i++) {
803     DEBUG_OBJ (ladspa, "pushing buffer (%p) on src pad %d", buffers_out[i], i);
804     gst_pad_push (ladspa->srcpads[i], GST_DATA (buffers_out[i]));
805
806     data_out[i] = NULL;
807     buffers_out[i] = NULL;
808   }
809
810   ladspa->timestamp += ladspa->buffer_frames * GST_SECOND / ladspa->samplerate;
811
812   /* FIXME: move these mallocs and frees to the state-change handler */
813
814   g_free (buffers_out);
815   g_free (buffers_in);
816   g_free (data_out);
817   g_free (data_in);
818 }
819
820 static void
821 gst_ladspa_chain (GstPad * pad, GstData * _data)
822 {
823   GstBuffer *buffer_in = GST_BUFFER (_data);
824   LADSPA_Descriptor *desc;
825   LADSPA_Data *data_in, **data_out = NULL;
826   GstBuffer **buffers_out = NULL;
827   unsigned long num_samples;
828   guint num_to_process, num_processed, i, numsrcpads;
829   GstLADSPA *ladspa;
830   GstLADSPAClass *oclass;
831
832   ladspa = (GstLADSPA *) GST_OBJECT_PARENT (pad);
833   oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
834   data_in = (LADSPA_Data *) GST_BUFFER_DATA (buffer_in);
835   num_samples = GST_BUFFER_SIZE (buffer_in) / sizeof (gfloat);
836   numsrcpads = oclass->numsrcpads;
837   desc = ladspa->descriptor;
838
839   /* we shouldn't get events here... */
840   g_return_if_fail (GST_IS_BUFFER (buffer_in));
841
842   /* FIXME: this function shouldn't need to malloc() anything */
843   if (numsrcpads > 0) {
844     buffers_out = g_new (GstBuffer *, numsrcpads);
845     data_out = g_new (LADSPA_Data *, numsrcpads);
846   }
847
848   i = 0;
849   if (!ladspa->inplace_broken && numsrcpads) {
850     /* reuse the first (chained) buffer */
851     buffers_out[i] = buffer_in;
852     DEBUG ("reuse: %d", GST_BUFFER_SIZE (buffer_in));
853     data_out[i] = data_in;
854     i++;
855   }
856   for (; i < numsrcpads; i++) {
857     buffers_out[i] = gst_buffer_new_and_alloc (GST_BUFFER_SIZE (buffer_in));
858     DEBUG ("new %d", GST_BUFFER_SIZE (buffer_in));
859     GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
860     data_out[i] = (LADSPA_Data *) GST_BUFFER_DATA (buffers_out[i]);
861   }
862
863   GST_DPMAN_PREPROCESS (ladspa->dpman, num_samples,
864       GST_BUFFER_TIMESTAMP (buffer_in));
865   num_processed = 0;
866
867   /* split up processing of the buffer into chunks so that dparams can
868    * be updated when required.
869    * In many cases the buffer will be processed in one chunk anyway.
870    */
871   while (GST_DPMAN_PROCESS (ladspa->dpman, num_processed)) {
872     num_to_process = GST_DPMAN_FRAMES_TO_PROCESS (ladspa->dpman);
873
874     desc->connect_port (ladspa->handle, oclass->sinkpad_portnums[0], data_in);
875     for (i = 0; i < numsrcpads; i++)
876       desc->connect_port (ladspa->handle, oclass->srcpad_portnums[i],
877           data_out[i]);
878
879     desc->run (ladspa->handle, num_to_process);
880
881     data_in += num_to_process;
882     for (i = 0; i < numsrcpads; i++)
883       data_out[i] += num_to_process;
884
885     num_processed += num_to_process;
886   }
887
888   if (!numsrcpads || buffers_out[0] != buffer_in)
889     gst_buffer_unref (buffer_in);
890
891   if (numsrcpads) {
892     for (i = 0; i < numsrcpads; i++) {
893       DEBUG_OBJ (ladspa, "pushing buffer (%p, length %u bytes) on src pad %d",
894           buffers_out[i], GST_BUFFER_SIZE (buffers_out[i]), i);
895       gst_pad_push (ladspa->srcpads[i], GST_DATA (buffers_out[i]));
896     }
897
898     g_free (buffers_out);
899     g_free (data_out);
900   }
901 }
902
903 static GstData *
904 gst_ladspa_get (GstPad * pad)
905 {
906   GstLADSPA *ladspa;
907   GstLADSPAClass *oclass;
908   GstBuffer *buf;
909   LADSPA_Data *data;
910   LADSPA_Descriptor *desc;
911   guint num_to_process, num_processed;
912
913   ladspa = (GstLADSPA *) gst_pad_get_parent (pad);
914   oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
915   desc = ladspa->descriptor;
916
917   /* 4096 is arbitrary */
918   buf = gst_buffer_new_and_alloc (4096);
919   GST_BUFFER_TIMESTAMP (buf) = ladspa->timestamp;
920   data = (LADSPA_Data *) GST_BUFFER_DATA (buf);
921
922   GST_DPMAN_PREPROCESS (ladspa->dpman, ladspa->buffer_frames,
923       ladspa->timestamp);
924   num_processed = 0;
925
926   /* split up processing of the buffer into chunks so that dparams can
927    * be updated when required.
928    * In many cases the buffer will be processed in one chunk anyway.
929    */
930   while (GST_DPMAN_PROCESS (ladspa->dpman, num_processed)) {
931     num_to_process = GST_DPMAN_FRAMES_TO_PROCESS (ladspa->dpman);
932
933     /* update timestamp */
934     ladspa->timestamp += num_to_process * GST_SECOND / ladspa->samplerate;
935
936     desc->connect_port (ladspa->handle, oclass->srcpad_portnums[0], data);
937
938     desc->run (ladspa->handle, num_to_process);
939
940     data += num_to_process;
941     num_processed = num_to_process;
942   }
943
944   return GST_DATA (buf);
945 }
946
947 static void
948 ladspa_describe_plugin (const char *pcFullFilename,
949     void *pvPluginHandle, LADSPA_Descriptor_Function pfDescriptorFunction)
950 {
951   const LADSPA_Descriptor *desc;
952   gint i;
953   GTypeInfo typeinfo = {
954     sizeof (GstLADSPAClass),
955     (GBaseInitFunc) gst_ladspa_base_init,
956     NULL,
957     (GClassInitFunc) gst_ladspa_class_init,
958     NULL,
959     NULL,
960     sizeof (GstLADSPA),
961     0,
962     (GInstanceInitFunc) gst_ladspa_init,
963   };
964   GType type;
965
966   /* walk through all the plugins in this pluginlibrary */
967   i = 0;
968   while ((desc = pfDescriptorFunction (i++))) {
969     gchar *type_name;
970
971     /* construct the type */
972     type_name = g_strdup_printf ("ladspa-%s", desc->Label);
973     g_strcanon (type_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-+", '-');
974     /* if it's already registered, drop it */
975     if (g_type_from_name (type_name)) {
976       g_free (type_name);
977       continue;
978     }
979
980     /* base-init temp alloc */
981     g_hash_table_insert (ladspa_descriptors,
982         GINT_TO_POINTER (0), (gpointer) desc);
983
984     /* create the type now */
985     type = g_type_register_static (GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
986     if (!gst_element_register (ladspa_plugin, type_name, GST_RANK_NONE, type))
987       continue;
988
989     /* add this plugin to the hash */
990     g_hash_table_insert (ladspa_descriptors,
991         GINT_TO_POINTER (type), (gpointer) desc);
992   }
993
994   g_hash_table_remove (ladspa_descriptors, GINT_TO_POINTER (0));
995 }
996
997 static gboolean
998 plugin_init (GstPlugin * plugin)
999 {
1000   GST_DEBUG_CATEGORY_INIT (ladspa_debug, "ladspa",
1001       GST_DEBUG_FG_GREEN | GST_DEBUG_BG_BLACK | GST_DEBUG_BOLD, "LADSPA");
1002
1003   ladspa_descriptors = g_hash_table_new (NULL, NULL);
1004   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
1005
1006   ladspa_plugin = plugin;
1007
1008   LADSPAPluginSearch (ladspa_describe_plugin);
1009
1010   /* initialize dparam support library */
1011   gst_control_init (NULL, NULL);
1012
1013   return TRUE;
1014 }
1015
1016 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1017     GST_VERSION_MINOR,
1018     "ladspa",
1019     "All LADSPA plugins",
1020     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)