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>
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.
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.
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.
27 #include <gst/control/control.h>
28 #include <gst/audio/audio.h>
30 #include "gstladspa.h"
31 #include <ladspa.h> /* main ladspa sdk include file */
32 #include "utils.h" /* ladspa sdk utility functions */
34 /* 1.0 and the 1.1 preliminary headers don't define a version, but 1.1 final
36 #ifndef LADSPA_VERSION
37 #define LADSPA_VERSION "1.0"
40 static GstStaticCaps ladspa_pad_caps =
41 GST_STATIC_CAPS (GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_CAPS);
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);
47 static void gst_ladspa_update_int (const GValue * value, gpointer data);
48 static GstPadLinkReturn gst_ladspa_link (GstPad * pad, const GstCaps * caps);
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);
55 static gboolean gst_ladspa_instantiate (GstLADSPA * ladspa);
56 static void gst_ladspa_activate (GstLADSPA * ladspa);
57 static void gst_ladspa_deactivate (GstLADSPA * ladspa);
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);
64 static GstElementClass *parent_class = NULL;
66 static GstPlugin *ladspa_plugin;
67 static GHashTable *ladspa_descriptors;
77 GST_DEBUG_CATEGORY_STATIC (ladspa_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__)
84 gst_ladspa_base_init (GstLADSPAClass * klass)
86 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
87 GstPadTemplate *templ;
88 GstElementDetails *details;
89 LADSPA_Descriptor *desc;
90 gint j, sinkcount, srccount;
92 desc = g_hash_table_lookup (ladspa_descriptors,
93 GINT_TO_POINTER (G_TYPE_FROM_CLASS (klass)));
95 desc = g_hash_table_lookup (ladspa_descriptors, GINT_TO_POINTER (0));
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]);
106 g_strcanon (name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
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++;
114 templ = gst_pad_template_new (name, GST_PAD_SRC, GST_PAD_ALWAYS,
115 gst_caps_copy (gst_static_caps_get (&ladspa_pad_caps)));
119 gst_element_class_add_pad_template (element_class, templ);
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";
135 details->klass = "Filter/Effect/Audio/LADSPA"; /* whatever this is */
136 gst_element_class_set_details (element_class, details);
138 klass->srcpad_portnums = g_new0 (gint, klass->numsrcpads);
139 klass->sinkpad_portnums = g_new0 (gint, klass->numsinkpads);
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;
149 klass->srcpad_portnums[srccount++] = j;
153 klass->descriptor = desc;
157 gst_ladspa_class_init (GstLADSPAClass * klass)
159 GObjectClass *gobject_class;
160 GstElementClass *gstelement_class;
161 LADSPA_Descriptor *desc;
162 gint i, current_portnum, controlcount;
164 gint argtype, argperms;
165 GParamSpec *paramspec = NULL;
166 gchar *argname, *tempstr, *paren;
168 gobject_class = (GObjectClass *) klass;
169 gstelement_class = (GstElementClass *) klass;
171 gobject_class->set_property = gst_ladspa_set_property;
172 gobject_class->get_property = gst_ladspa_get_property;
174 gstelement_class->change_state = gst_ladspa_change_state;
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)));
180 desc = g_hash_table_lookup (ladspa_descriptors, GINT_TO_POINTER (0));
183 klass->numcontrols = 0;
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++;
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);
196 klass->control_portnums = g_new0 (gint, klass->numcontrols);
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;
206 /* now build the control info from the control ports */
207 klass->control_info = g_new0 (ladspa_control_info, klass->numcontrols);
209 for (i = 0; i < klass->numcontrols; i++) {
210 current_portnum = klass->control_portnums[i];
212 /* short name for hint descriptor */
213 hintdesc = desc->PortRangeHints[current_portnum].HintDescriptor;
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;
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;
229 argtype = G_TYPE_FLOAT;
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;
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;
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? */
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;
258 /* use the lowerbound as the default value */
259 klass->control_info[i].def = klass->control_info[i].lowerbound;
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));
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));
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));
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;
304 #endif /* LADSPA_IS_HINT_HAS_DEFAULT */
306 klass->control_info[i].def = CLAMP (klass->control_info[i].def,
307 klass->control_info[i].lowerbound, klass->control_info[i].upperbound);
309 if (LADSPA_IS_PORT_INPUT (desc->PortDescriptors[current_portnum])) {
310 argperms = G_PARAM_READWRITE;
311 klass->control_info[i].writable = TRUE;
313 argperms = G_PARAM_READABLE;
314 klass->control_info[i].writable = FALSE;
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
321 paren = g_strrstr (argname, " (");
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'))) {
331 argname = g_strconcat ("param-", argname, NULL);
335 /* check for duplicate property names */
336 if (g_object_class_find_property (G_OBJECT_CLASS (klass), argname) != NULL) {
338 gchar *numargname = g_strdup_printf ("%s_%d", argname, numarg++);
340 while (g_object_class_find_property (G_OBJECT_CLASS (klass),
341 numargname) != NULL) {
343 numargname = g_strdup_printf ("%s_%d", argname, numarg++);
345 argname = numargname;
348 klass->control_info[i].param_name = argname;
350 DEBUG ("adding arg %s from %s", argname, klass->control_info[i].name);
352 if (argtype == G_TYPE_BOOLEAN) {
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);
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);
369 /* properties have an offset of 1 */
370 g_object_class_install_property (G_OBJECT_CLASS (klass), i + 1, paramspec);
375 gst_ladspa_init (GstLADSPA * ladspa)
377 GstLADSPAClass *oclass;
378 ladspa_control_info cinfo;
380 LADSPA_Descriptor *desc;
381 gint i, sinkcount, srccount;
383 oclass = (GstLADSPAClass *) G_OBJECT_GET_CLASS (ladspa);
384 desc = oclass->descriptor;
385 ladspa->descriptor = oclass->descriptor;
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));
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));
400 gst_pad_set_link_function (pad, gst_ladspa_link);
401 gst_element_add_pad ((GstElement *) ladspa, pad);
403 if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK)
404 ladspa->sinkpads[sinkcount++] = pad;
406 ladspa->srcpads[srccount++] = pad;
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;
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])
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])
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])
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])
446 /* nonzero default needed to instantiate() some plugins */
447 ladspa->samplerate = 44100;
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);
454 if (sinkcount == 0 && srccount == 1) {
455 /* get mode (no sink pads) */
456 DEBUG_OBJ (ladspa, "mono get mode with 1 src pad");
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");
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,
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
474 g_warning ("%d sink pads, %d src pads not yet supported", sinkcount,
478 gst_ladspa_instantiate (ladspa);
482 gst_ladspa_update_int (const GValue * value, gpointer data)
484 gfloat *target = (gfloat *) data;
486 *target = (gfloat) g_value_get_int (value);
489 static GstPadLinkReturn
490 gst_ladspa_link (GstPad * pad, const GstCaps * caps)
492 GstElement *element = (GstElement *) GST_PAD_PARENT (pad);
493 GstLADSPA *ladspa = (GstLADSPA *) element;
494 const GList *l = NULL;
496 GstStructure *structure;
498 /* if this fails in some other plugin, the graph is left in an inconsistent
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;
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;
516 gst_structure_get_int (structure, "buffer-frames", &ladspa->buffer_frames);
518 return GST_PAD_LINK_OK;
523 gst_ladspa_force_src_caps (GstLADSPA * ladspa, GstPad * pad)
525 if (!ladspa->buffer_frames) {
526 ladspa->buffer_frames = 256; /* 5 ms at 44100 kHz (just a default...) */
529 DEBUG_OBJ (ladspa, "forcing caps with rate=%d, buffer-frames=%d",
530 ladspa->samplerate, ladspa->buffer_frames);
532 gst_pad_try_set_caps (pad,
533 gst_caps_new ("ladspa_src_caps",
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)));
544 gst_ladspa_set_property (GObject * object, guint prop_id, const GValue * value,
547 GstLADSPA *ladspa = (GstLADSPA *) object;
548 GstLADSPAClass *oclass;
549 ladspa_control_info *control_info;
551 oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (object));
553 /* remember, properties have an offset of 1 */
556 /* verify it exists */
557 g_return_if_fail (prop_id < oclass->numcontrols);
559 control_info = &(oclass->control_info[prop_id]);
560 g_return_if_fail (control_info->name != NULL);
562 /* check to see if it's writable */
563 g_return_if_fail (control_info->writable);
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);
571 ladspa->controls[prop_id] = g_value_get_float (value);
573 DEBUG_OBJ (object, "set arg %s to %f", control_info->name,
574 ladspa->controls[prop_id]);
578 gst_ladspa_get_property (GObject * object, guint prop_id, GValue * value,
581 GstLADSPA *ladspa = (GstLADSPA *) object;
582 GstLADSPAClass *oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (object));
583 ladspa_control_info *control_info;
585 /* remember, properties have an offset of 1 */
588 /* verify it exists */
589 g_return_if_fail (prop_id < oclass->numcontrols);
591 control_info = &(oclass->control_info[prop_id]);
592 g_return_if_fail (control_info->name != NULL);
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]);
600 g_value_set_float (value, ladspa->controls[prop_id]);
602 DEBUG_OBJ (object, "got arg %s as %f", control_info->name,
603 ladspa->controls[prop_id]);
607 gst_ladspa_instantiate (GstLADSPA * ladspa)
609 LADSPA_Descriptor *desc;
611 GstLADSPAClass *oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
612 gboolean was_activated;
614 desc = ladspa->descriptor;
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);
623 /* instantiate the plugin */
624 DEBUG_OBJ (ladspa, "instantiating the plugin at %d Hz", ladspa->samplerate);
626 ladspa->handle = desc->instantiate (desc, ladspa->samplerate);
627 g_return_val_if_fail (ladspa->handle != NULL, FALSE);
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]));
634 /* reactivate if it was activated before the reinstantiation */
636 gst_ladspa_activate (ladspa);
641 static GstElementStateReturn
642 gst_ladspa_change_state (GstElement * element)
644 LADSPA_Descriptor *desc;
645 GstLADSPA *ladspa = (GstLADSPA *) element;
647 desc = ladspa->descriptor;
649 switch (GST_STATE_TRANSITION (element)) {
650 case GST_STATE_NULL_TO_READY:
651 gst_ladspa_activate (ladspa);
653 case GST_STATE_READY_TO_NULL:
654 gst_ladspa_deactivate (ladspa);
660 if (GST_ELEMENT_CLASS (parent_class)->change_state)
661 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
663 return GST_STATE_SUCCESS;
667 gst_ladspa_activate (GstLADSPA * ladspa)
669 LADSPA_Descriptor *desc;
671 desc = ladspa->descriptor;
673 if (ladspa->activated)
674 gst_ladspa_deactivate (ladspa);
676 DEBUG_OBJ (ladspa, "activating");
678 /* activate the plugin (function might be null) */
679 if (desc->activate != NULL)
680 desc->activate (ladspa->handle);
682 ladspa->activated = TRUE;
686 gst_ladspa_deactivate (GstLADSPA * ladspa)
688 LADSPA_Descriptor *desc;
690 desc = ladspa->descriptor;
692 DEBUG_OBJ (ladspa, "deactivating");
694 /* deactivate the plugin (function might be null) */
695 if (ladspa->activated && (desc->deactivate != NULL))
696 desc->deactivate (ladspa->handle);
698 ladspa->activated = FALSE;
702 gst_ladspa_loop (GstElement * element)
704 guint i, j, numsrcpads, numsinkpads;
705 guint num_processed, num_to_process;
707 LADSPA_Data **data_in, **data_out;
708 GstBuffer **buffers_in, **buffers_out;
710 GstLADSPA *ladspa = (GstLADSPA *) element;
711 GstLADSPAClass *oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
712 LADSPA_Descriptor *desc = ladspa->descriptor;
714 numsinkpads = oclass->numsinkpads;
715 numsrcpads = oclass->numsrcpads;
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);
725 /* first get all the necessary data from the input ports */
726 for (i = 0; i < numsinkpads; i++) {
728 buffers_in[i] = GST_BUFFER (gst_pad_pull (ladspa->sinkpads[i]));
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) {
737 gst_element_set_eos (element);
744 if (largest_buffer < 0)
745 largest_buffer = GST_BUFFER_SIZE (buffers_in[i]) / sizeof (gfloat);
748 MIN (GST_BUFFER_SIZE (buffers_in[i]) / sizeof (gfloat),
750 data_in[i] = (LADSPA_Data *) GST_BUFFER_DATA (buffers_in[i]);
751 GST_BUFFER_TIMESTAMP (buffers_in[i]) = ladspa->timestamp;
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];
762 for (; i < numsrcpads; 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]);
769 GST_DPMAN_PREPROCESS (ladspa->dpman, largest_buffer, ladspa->timestamp);
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.
776 while (GST_DPMAN_PROCESS (ladspa->dpman, num_processed)) {
777 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS (ladspa->dpman);
779 for (i = 0; i < numsinkpads; i++)
780 desc->connect_port (ladspa->handle, oclass->sinkpad_portnums[i],
782 for (i = 0; i < numsrcpads; i++)
783 desc->connect_port (ladspa->handle, oclass->srcpad_portnums[i],
786 desc->run (ladspa->handle, num_to_process);
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;
793 num_processed += num_to_process;
796 for (i = 0; i < numsinkpads; i++) {
797 if (i >= numsrcpads || buffers_out[i] != buffers_in[i])
798 gst_buffer_unref (buffers_in[i]);
800 buffers_in[i] = NULL;
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]));
807 buffers_out[i] = NULL;
810 ladspa->timestamp += ladspa->buffer_frames * GST_SECOND / ladspa->samplerate;
812 /* FIXME: move these mallocs and frees to the state-change handler */
814 g_free (buffers_out);
821 gst_ladspa_chain (GstPad * pad, GstData * _data)
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;
830 GstLADSPAClass *oclass;
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;
839 /* we shouldn't get events here... */
840 g_return_if_fail (GST_IS_BUFFER (buffer_in));
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);
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;
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]);
863 GST_DPMAN_PREPROCESS (ladspa->dpman, num_samples,
864 GST_BUFFER_TIMESTAMP (buffer_in));
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.
871 while (GST_DPMAN_PROCESS (ladspa->dpman, num_processed)) {
872 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS (ladspa->dpman);
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],
879 desc->run (ladspa->handle, num_to_process);
881 data_in += num_to_process;
882 for (i = 0; i < numsrcpads; i++)
883 data_out[i] += num_to_process;
885 num_processed += num_to_process;
888 if (!numsrcpads || buffers_out[0] != buffer_in)
889 gst_buffer_unref (buffer_in);
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]));
898 g_free (buffers_out);
904 gst_ladspa_get (GstPad * pad)
907 GstLADSPAClass *oclass;
910 LADSPA_Descriptor *desc;
911 guint num_to_process, num_processed;
913 ladspa = (GstLADSPA *) gst_pad_get_parent (pad);
914 oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
915 desc = ladspa->descriptor;
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);
922 GST_DPMAN_PREPROCESS (ladspa->dpman, ladspa->buffer_frames,
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.
930 while (GST_DPMAN_PROCESS (ladspa->dpman, num_processed)) {
931 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS (ladspa->dpman);
933 /* update timestamp */
934 ladspa->timestamp += num_to_process * GST_SECOND / ladspa->samplerate;
936 desc->connect_port (ladspa->handle, oclass->srcpad_portnums[0], data);
938 desc->run (ladspa->handle, num_to_process);
940 data += num_to_process;
941 num_processed = num_to_process;
944 return GST_DATA (buf);
948 ladspa_describe_plugin (const char *pcFullFilename,
949 void *pvPluginHandle, LADSPA_Descriptor_Function pfDescriptorFunction)
951 const LADSPA_Descriptor *desc;
953 GTypeInfo typeinfo = {
954 sizeof (GstLADSPAClass),
955 (GBaseInitFunc) gst_ladspa_base_init,
957 (GClassInitFunc) gst_ladspa_class_init,
962 (GInstanceInitFunc) gst_ladspa_init,
966 /* walk through all the plugins in this pluginlibrary */
968 while ((desc = pfDescriptorFunction (i++))) {
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)) {
980 /* base-init temp alloc */
981 g_hash_table_insert (ladspa_descriptors,
982 GINT_TO_POINTER (0), (gpointer) desc);
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))
989 /* add this plugin to the hash */
990 g_hash_table_insert (ladspa_descriptors,
991 GINT_TO_POINTER (type), (gpointer) desc);
994 g_hash_table_remove (ladspa_descriptors, GINT_TO_POINTER (0));
998 plugin_init (GstPlugin * plugin)
1000 GST_DEBUG_CATEGORY_INIT (ladspa_debug, "ladspa",
1001 GST_DEBUG_FG_GREEN | GST_DEBUG_BG_BLACK | GST_DEBUG_BOLD, "LADSPA");
1003 ladspa_descriptors = g_hash_table_new (NULL, NULL);
1004 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
1006 ladspa_plugin = plugin;
1008 LADSPAPluginSearch (ladspa_describe_plugin);
1010 /* initialize dparam support library */
1011 gst_control_init (NULL, NULL);
1016 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1019 "All LADSPA plugins",
1020 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)