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, const GValue *value, GParamSpec *pspec);
51 static void gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
53 static gboolean gst_ladspa_instantiate (GstLADSPA *ladspa);
54 static void gst_ladspa_activate (GstLADSPA *ladspa);
55 static void gst_ladspa_deactivate (GstLADSPA *ladspa);
57 static GstElementStateReturn gst_ladspa_change_state (GstElement *element);
58 static void gst_ladspa_loop (GstElement *element);
59 static void gst_ladspa_chain (GstPad *pad,GstData *_data);
60 static GstData * gst_ladspa_get (GstPad *pad);
62 static GstElementClass *parent_class = NULL;
64 static GstPlugin *ladspa_plugin;
65 static GHashTable *ladspa_descriptors;
74 GST_DEBUG_CATEGORY_STATIC (ladspa_debug);
76 GST_CAT_LEVEL_LOG (ladspa_debug, GST_LEVEL_DEBUG, NULL, __VA_ARGS__)
77 #define DEBUG_OBJ(obj,...) \
78 GST_CAT_LEVEL_LOG (ladspa_debug, GST_LEVEL_DEBUG, obj, __VA_ARGS__)
81 gst_ladspa_base_init (GstLADSPAClass *klass)
83 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
84 GstPadTemplate *templ;
85 GstElementDetails *details;
86 LADSPA_Descriptor *desc;
87 gint j, sinkcount,srccount;
89 desc = g_hash_table_lookup(ladspa_descriptors,
90 GINT_TO_POINTER(G_TYPE_FROM_CLASS(klass)));
92 desc = g_hash_table_lookup(ladspa_descriptors, GINT_TO_POINTER(0));
96 klass->numports = desc->PortCount;
97 klass->numsinkpads = 0;
98 klass->numsrcpads = 0;
99 for (j=0;j<desc->PortCount;j++) {
100 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j])) {
101 gchar *name = g_strdup((gchar *)desc->PortNames[j]);
102 g_strcanon (name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
104 /* the factories take ownership of the name */
105 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j])) {
106 templ = gst_pad_template_new (name, GST_PAD_SINK, GST_PAD_ALWAYS,
107 gst_caps_copy (gst_static_caps_get (&ladspa_pad_caps)));
108 klass->numsinkpads++;
110 templ = gst_pad_template_new (name, GST_PAD_SRC, GST_PAD_ALWAYS,
111 gst_caps_copy (gst_static_caps_get (&ladspa_pad_caps)));
115 gst_element_class_add_pad_template (element_class, templ);
119 /* construct the element details struct */
120 details = g_new0(GstElementDetails,1);
121 details->longname = g_strdup(desc->Name);
122 details->description = details->longname;
123 details->author = g_strdup(desc->Maker);
124 if ((klass->numsinkpads >0) && (klass->numsrcpads >0)) details->klass = "Filter/Effect/Audio/LADSPA";
125 else if((klass->numsinkpads==0) && (klass->numsrcpads >0)) details->klass = "Source/Audio/LADSPA";
126 else if((klass->numsinkpads >0) && (klass->numsrcpads==0)) details->klass = "Sink/Audio/LADSPA";
127 else details->klass = "Filter/Effect/Audio/LADSPA"; /* whatever this is */
128 gst_element_class_set_details (element_class, details);
130 klass->srcpad_portnums = g_new0(gint,klass->numsrcpads);
131 klass->sinkpad_portnums = g_new0(gint,klass->numsinkpads);
135 /* walk through the ports, note the portnums for srcpads, sinkpads */
136 for (j=0; j<desc->PortCount; j++) {
137 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j])) {
138 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j]))
139 klass->sinkpad_portnums[sinkcount++] = j;
141 klass->srcpad_portnums[srccount++] = j;
145 klass->descriptor = desc;
149 gst_ladspa_class_init (GstLADSPAClass *klass)
151 GObjectClass *gobject_class;
152 GstElementClass *gstelement_class;
153 LADSPA_Descriptor *desc;
154 gint i,current_portnum,controlcount;
156 gint argtype,argperms;
157 GParamSpec *paramspec = NULL;
158 gchar *argname, *tempstr, *paren;
160 gobject_class = (GObjectClass*)klass;
161 gstelement_class = (GstElementClass*)klass;
163 gobject_class->set_property = gst_ladspa_set_property;
164 gobject_class->get_property = gst_ladspa_get_property;
166 gstelement_class->change_state = gst_ladspa_change_state;
168 /* look up and store the ladspa descriptor */
169 desc = g_hash_table_lookup(ladspa_descriptors,
170 GINT_TO_POINTER(G_TYPE_FROM_CLASS(klass)));
172 desc = g_hash_table_lookup(ladspa_descriptors, GINT_TO_POINTER(0));
175 klass->numcontrols = 0;
177 /* walk through the ports, count the input, output and control ports */
178 for (i=0; i<desc->PortCount; i++) {
179 if (!LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
180 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
181 klass->numcontrols++;
184 DEBUG ("ladspa element class: init %s with %d sink, %d src, %d control\n",
185 g_type_name (G_TYPE_FROM_CLASS (klass)),
186 klass->numsinkpads, klass->numsrcpads, klass->numcontrols);
188 klass->control_portnums = g_new0(gint,klass->numcontrols);
191 /* walk through the ports, note the portnums for control params */
192 for (i=0; i<desc->PortCount; i++) {
193 if (!LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
194 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
195 klass->control_portnums[controlcount++] = i;
198 /* now build the control info from the control ports */
199 klass->control_info = g_new0(ladspa_control_info,klass->numcontrols);
201 for (i=0;i<klass->numcontrols;i++) {
202 current_portnum = klass->control_portnums[i];
204 /* short name for hint descriptor */
205 hintdesc = desc->PortRangeHints[current_portnum].HintDescriptor;
207 /* get the various bits */
208 if (LADSPA_IS_HINT_TOGGLED(hintdesc))
209 klass->control_info[i].toggled = TRUE;
210 if (LADSPA_IS_HINT_LOGARITHMIC(hintdesc))
211 klass->control_info[i].logarithmic = TRUE;
212 if (LADSPA_IS_HINT_INTEGER(hintdesc))
213 klass->control_info[i].integer = TRUE;
215 /* figure out the argument details */
216 if (klass->control_info[i].toggled) argtype = G_TYPE_BOOLEAN;
217 else if (klass->control_info[i].integer) argtype = G_TYPE_INT;
218 else argtype = G_TYPE_FLOAT;
220 /* grab the bounds */
221 if (LADSPA_IS_HINT_BOUNDED_BELOW(hintdesc)) {
222 klass->control_info[i].lower = TRUE;
223 klass->control_info[i].lowerbound =
224 desc->PortRangeHints[current_portnum].LowerBound;
226 if (argtype==G_TYPE_INT) klass->control_info[i].lowerbound = (gfloat)G_MININT;
227 if (argtype==G_TYPE_FLOAT) klass->control_info[i].lowerbound = -G_MAXFLOAT;
230 if (LADSPA_IS_HINT_BOUNDED_ABOVE(hintdesc)) {
231 klass->control_info[i].upper = TRUE;
232 klass->control_info[i].upperbound =
233 desc->PortRangeHints[current_portnum].UpperBound;
234 if (LADSPA_IS_HINT_SAMPLE_RATE(hintdesc)) {
235 klass->control_info[i].samplerate = TRUE;
236 klass->control_info[i].upperbound *= 44100; /* FIXME? */
239 if (argtype==G_TYPE_INT) klass->control_info[i].upperbound = (gfloat)G_MAXINT;
240 if (argtype==G_TYPE_FLOAT) klass->control_info[i].upperbound = G_MAXFLOAT;
243 /* use the lowerbound as the default value */
244 klass->control_info[i].def = klass->control_info[i].lowerbound;
246 #ifdef LADSPA_IS_HINT_HAS_DEFAULT
247 /* figure out the defaults */
248 if (LADSPA_IS_HINT_HAS_DEFAULT (hintdesc)) {
249 if (LADSPA_IS_HINT_DEFAULT_MINIMUM (hintdesc))
250 klass->control_info[i].def = klass->control_info[i].lowerbound;
251 else if (LADSPA_IS_HINT_DEFAULT_LOW (hintdesc))
252 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
253 klass->control_info[i].def = exp (0.75*log(klass->control_info[i].lowerbound) +
254 0.25*log(klass->control_info[i].upperbound));
256 klass->control_info[i].def = (0.75*klass->control_info[i].lowerbound +
257 0.25*klass->control_info[i].upperbound);
258 else if (LADSPA_IS_HINT_DEFAULT_MIDDLE (hintdesc))
259 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
260 klass->control_info[i].def = exp (0.5*log(klass->control_info[i].lowerbound) +
261 0.5*log(klass->control_info[i].upperbound));
263 klass->control_info[i].def = (0.5*klass->control_info[i].lowerbound +
264 0.5*klass->control_info[i].upperbound);
265 else if (LADSPA_IS_HINT_DEFAULT_HIGH (hintdesc))
266 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
267 klass->control_info[i].def = exp (0.25*log(klass->control_info[i].lowerbound) +
268 0.75*log(klass->control_info[i].upperbound));
270 klass->control_info[i].def = (0.25*klass->control_info[i].lowerbound +
271 0.75*klass->control_info[i].upperbound);
272 else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM (hintdesc))
273 klass->control_info[i].def = klass->control_info[i].upperbound;
274 else if (LADSPA_IS_HINT_DEFAULT_0 (hintdesc))
275 klass->control_info[i].def = 0.0;
276 else if (LADSPA_IS_HINT_DEFAULT_1 (hintdesc))
277 klass->control_info[i].def = 1.0;
278 else if (LADSPA_IS_HINT_DEFAULT_100 (hintdesc))
279 klass->control_info[i].def = 100.0;
280 else if (LADSPA_IS_HINT_DEFAULT_440 (hintdesc))
281 klass->control_info[i].def = 440.0;
283 #endif /* LADSPA_IS_HINT_HAS_DEFAULT */
285 klass->control_info[i].def = CLAMP(klass->control_info[i].def,
286 klass->control_info[i].lowerbound,
287 klass->control_info[i].upperbound);
289 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[current_portnum])) {
290 argperms = G_PARAM_READWRITE;
291 klass->control_info[i].writable = TRUE;
293 argperms = G_PARAM_READABLE;
294 klass->control_info[i].writable = FALSE;
297 klass->control_info[i].name = g_strdup(desc->PortNames[current_portnum]);
298 argname = g_strdup(klass->control_info[i].name);
299 /* find out if there is a (unitname) at the end of the argname and get rid
301 paren = g_strrstr (argname, " (");
305 /* this is the same thing that param_spec_* will do */
306 g_strcanon (argname, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
307 /* satisfy glib2 (argname[0] must be [A-Za-z]) */
308 if (!((argname[0] >= 'a' && argname[0] <= 'z') || (argname[0] >= 'A' && argname[0] <= 'Z'))) {
310 argname = g_strconcat("param-", argname, NULL);
314 /* check for duplicate property names */
315 if (g_object_class_find_property(G_OBJECT_CLASS(klass), argname) != NULL){
317 gchar *numargname = g_strdup_printf("%s_%d",argname,numarg++);
318 while (g_object_class_find_property(G_OBJECT_CLASS(klass), numargname) != NULL){
320 numargname = g_strdup_printf("%s_%d",argname,numarg++);
322 argname = numargname;
325 klass->control_info[i].param_name = argname;
327 DEBUG ("adding arg %s from %s", argname, klass->control_info[i].name);
329 if (argtype==G_TYPE_BOOLEAN){
330 paramspec = g_param_spec_boolean(argname,argname,argname, FALSE, argperms);
331 } else if (argtype==G_TYPE_INT){
332 paramspec = g_param_spec_int(argname,argname,argname,
333 (gint)klass->control_info[i].lowerbound,
334 (gint)klass->control_info[i].upperbound,
335 (gint)klass->control_info[i].def, argperms);
336 } else if (klass->control_info[i].samplerate){
337 paramspec = g_param_spec_float(argname,argname,argname,
341 paramspec = g_param_spec_float(argname,argname,argname,
342 klass->control_info[i].lowerbound, klass->control_info[i].upperbound,
343 klass->control_info[i].def, argperms);
346 /* properties have an offset of 1 */
347 g_object_class_install_property(G_OBJECT_CLASS(klass), i+1, paramspec);
352 gst_ladspa_init (GstLADSPA *ladspa)
354 GstLADSPAClass *oclass;
355 ladspa_control_info cinfo;
357 LADSPA_Descriptor *desc;
358 gint i,sinkcount,srccount;
360 oclass = (GstLADSPAClass*)G_OBJECT_GET_CLASS (ladspa);
361 desc = oclass->descriptor;
362 ladspa->descriptor = oclass->descriptor;
364 /* allocate the various arrays */
365 ladspa->srcpads = g_new0(GstPad*,oclass->numsrcpads);
366 ladspa->sinkpads = g_new0(GstPad*,oclass->numsinkpads);
367 ladspa->controls = g_new(gfloat,oclass->numcontrols);
368 ladspa->dpman = gst_dpman_new ("ladspa_dpman", GST_ELEMENT(ladspa));
373 for (l=GST_ELEMENT_CLASS (oclass)->padtemplates; l; l=l->next) {
374 GstPad *pad = gst_pad_new_from_template (GST_PAD_TEMPLATE (l->data),
375 GST_PAD_TEMPLATE_NAME_TEMPLATE (l->data));
376 gst_pad_set_link_function (pad, gst_ladspa_link);
377 gst_element_add_pad ((GstElement*)ladspa, pad);
379 if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK)
380 ladspa->sinkpads[sinkcount++] = pad;
382 ladspa->srcpads[srccount++] = pad;
386 for (i=0; i<oclass->numcontrols; i++) {
387 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
388 cinfo = oclass->control_info[i];
389 ladspa->controls[i]=cinfo.def;
392 gst_dpman_add_required_dparam_callback (
394 g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
395 0, 1, (gint)(ladspa->controls[i]), G_PARAM_READWRITE),
396 "int", gst_ladspa_update_int, &(ladspa->controls[i])
399 else if (cinfo.integer){
400 gst_dpman_add_required_dparam_callback (
402 g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
403 (gint)cinfo.lowerbound, (gint)cinfo.upperbound,
404 (gint)ladspa->controls[i], G_PARAM_READWRITE),
405 "int", gst_ladspa_update_int, &(ladspa->controls[i])
408 else if (cinfo.samplerate){
409 gst_dpman_add_required_dparam_direct (
411 g_param_spec_float(cinfo.param_name, cinfo.name, cinfo.name,
412 cinfo.lowerbound, cinfo.upperbound,
413 ladspa->controls[i], G_PARAM_READWRITE),
414 "hertz-rate-bound", &(ladspa->controls[i])
418 gst_dpman_add_required_dparam_direct (
420 g_param_spec_float(cinfo.param_name, cinfo.name, cinfo.name,
421 cinfo.lowerbound, cinfo.upperbound,
422 ladspa->controls[i], G_PARAM_READWRITE),
423 "float", &(ladspa->controls[i])
429 /* nonzero default needed to instantiate() some plugins */
430 ladspa->samplerate = 44100;
432 ladspa->buffer_frames = 0; /* should be set with caps */
433 ladspa->activated = FALSE;
434 ladspa->inplace_broken = LADSPA_IS_INPLACE_BROKEN(ladspa->descriptor->Properties);
436 if (sinkcount==0 && srccount == 1) {
437 /* get mode (no sink pads) */
438 DEBUG_OBJ (ladspa, "mono get mode with 1 src pad");
440 gst_pad_set_get_function (ladspa->srcpads[0], gst_ladspa_get);
441 } else if (sinkcount==1){
442 /* with one sink we can use the chain function */
443 DEBUG_OBJ (ladspa, "chain mode");
445 gst_pad_set_chain_function (ladspa->sinkpads[0], gst_ladspa_chain);
446 } else if (sinkcount > 1){
447 /* more than one sink pad needs loop mode */
448 DEBUG_OBJ (ladspa, "loop mode with %d sink pads and %d src pads", sinkcount, srccount);
450 gst_element_set_loop_function (GST_ELEMENT (ladspa), gst_ladspa_loop);
451 } else if (sinkcount==0 && srccount == 0) {
452 /* for example, a plugin with only control inputs and output -- just ignore
455 g_warning ("%d sink pads, %d src pads not yet supported", sinkcount, srccount);
458 gst_ladspa_instantiate (ladspa);
462 gst_ladspa_update_int(const GValue *value, gpointer data)
464 gfloat *target = (gfloat*) data;
465 *target = (gfloat)g_value_get_int(value);
468 static GstPadLinkReturn
469 gst_ladspa_link (GstPad *pad, const GstCaps *caps)
471 GstElement *element = (GstElement*)GST_PAD_PARENT (pad);
472 GstLADSPA *ladspa = (GstLADSPA*)element;
473 const GList *l = NULL;
475 GstStructure *structure;
477 /* if this fails in some other plugin, the graph is left in an inconsistent
479 for (l=gst_element_get_pad_list (element); l; l=l->next)
480 if (pad != (GstPad*)l->data)
481 if (gst_pad_try_set_caps ((GstPad*)l->data, caps) <= 0)
482 return GST_PAD_LINK_REFUSED;
484 /* we assume that the ladspa plugin can handle any sample rate, so this
485 check gets put last */
486 structure = gst_caps_get_structure (caps, 0);
487 gst_structure_get_int (structure, "rate", &rate);
488 /* have to instantiate ladspa plugin when samplerate changes (groan) */
489 if (ladspa->samplerate != rate) {
490 ladspa->samplerate = rate;
491 if (! gst_ladspa_instantiate(ladspa))
492 return GST_PAD_LINK_REFUSED;
495 gst_structure_get_int (structure, "buffer-frames", &ladspa->buffer_frames);
497 return GST_PAD_LINK_OK;
502 gst_ladspa_force_src_caps(GstLADSPA *ladspa, GstPad *pad)
504 if (!ladspa->buffer_frames) {
505 ladspa->buffer_frames = 256; /* 5 ms at 44100 kHz (just a default...) */
508 DEBUG_OBJ (ladspa, "forcing caps with rate=%d, buffer-frames=%d",
509 ladspa->samplerate, ladspa->buffer_frames);
511 gst_pad_try_set_caps (pad,
516 "width", G_TYPE_INT (32),
517 "endianness", G_TYPE_INT (G_BYTE_ORDER),
518 "rate", G_TYPE_INT (ladspa->samplerate),
519 "buffer-frames", G_TYPE_INT (ladspa->buffer_frames),
520 "channels", G_TYPE_INT (1),
526 gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
528 GstLADSPA *ladspa = (GstLADSPA*)object;
529 GstLADSPAClass *oclass;
530 ladspa_control_info *control_info;
532 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
534 /* remember, properties have an offset of 1 */
537 /* verify it exists */
538 g_return_if_fail (prop_id < oclass->numcontrols);
540 control_info = &(oclass->control_info[prop_id]);
541 g_return_if_fail (control_info->name != NULL);
543 /* check to see if it's writable */
544 g_return_if_fail (control_info->writable);
546 /* now see what type it is */
547 if (control_info->toggled)
548 ladspa->controls[prop_id] = g_value_get_boolean (value) ? 1.f : 0.f;
549 else if (control_info->integer)
550 ladspa->controls[prop_id] = g_value_get_int (value);
552 ladspa->controls[prop_id] = g_value_get_float (value);
554 DEBUG_OBJ (object, "set arg %s to %f", control_info->name, ladspa->controls[prop_id]);
558 gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
560 GstLADSPA *ladspa = (GstLADSPA*)object;
561 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
562 ladspa_control_info *control_info;
564 /* remember, properties have an offset of 1 */
567 /* verify it exists */
568 g_return_if_fail (prop_id < oclass->numcontrols);
570 control_info = &(oclass->control_info[prop_id]);
571 g_return_if_fail (control_info->name != NULL);
573 /* now see what type it is */
574 if (control_info->toggled)
575 g_value_set_boolean (value, ladspa->controls[prop_id] == 1.0);
576 else if (control_info->integer)
577 g_value_set_int (value, (gint)ladspa->controls[prop_id]);
579 g_value_set_float (value, ladspa->controls[prop_id]);
581 DEBUG_OBJ (object, "got arg %s as %f", control_info->name, ladspa->controls[prop_id]);
585 gst_ladspa_instantiate (GstLADSPA *ladspa)
587 LADSPA_Descriptor *desc;
589 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
590 gboolean was_activated;
592 desc = ladspa->descriptor;
594 /* check for old handle */
595 was_activated = ladspa->activated;
596 if (ladspa->handle != NULL){
597 gst_ladspa_deactivate(ladspa);
598 desc->cleanup(ladspa->handle);
601 /* instantiate the plugin */
602 DEBUG_OBJ (ladspa, "instantiating the plugin at %d Hz", ladspa->samplerate);
604 ladspa->handle = desc->instantiate(desc,ladspa->samplerate);
605 g_return_val_if_fail (ladspa->handle != NULL, FALSE);
607 /* connect the control ports */
608 for (i=0;i<oclass->numcontrols;i++)
609 desc->connect_port(ladspa->handle,
610 oclass->control_portnums[i],
611 &(ladspa->controls[i]));
613 /* reactivate if it was activated before the reinstantiation */
615 gst_ladspa_activate(ladspa);
620 static GstElementStateReturn
621 gst_ladspa_change_state (GstElement *element)
623 LADSPA_Descriptor *desc;
624 GstLADSPA *ladspa = (GstLADSPA*)element;
625 desc = ladspa->descriptor;
627 switch (GST_STATE_TRANSITION (element)) {
628 case GST_STATE_NULL_TO_READY:
629 gst_ladspa_activate(ladspa);
631 case GST_STATE_READY_TO_NULL:
632 gst_ladspa_deactivate(ladspa);
638 if (GST_ELEMENT_CLASS (parent_class)->change_state)
639 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
641 return GST_STATE_SUCCESS;
645 gst_ladspa_activate (GstLADSPA *ladspa)
647 LADSPA_Descriptor *desc;
648 desc = ladspa->descriptor;
650 if (ladspa->activated)
651 gst_ladspa_deactivate(ladspa);
653 DEBUG_OBJ (ladspa, "activating");
655 /* activate the plugin (function might be null) */
656 if (desc->activate != NULL)
657 desc->activate(ladspa->handle);
659 ladspa->activated = TRUE;
663 gst_ladspa_deactivate (GstLADSPA *ladspa)
665 LADSPA_Descriptor *desc;
666 desc = ladspa->descriptor;
668 DEBUG_OBJ (ladspa, "deactivating");
670 /* deactivate the plugin (function might be null) */
671 if (ladspa->activated && (desc->deactivate != NULL))
672 desc->deactivate(ladspa->handle);
674 ladspa->activated = FALSE;
678 gst_ladspa_loop (GstElement *element)
680 guint i, j, numsrcpads, numsinkpads;
681 guint num_processed, num_to_process;
683 LADSPA_Data **data_in, **data_out;
684 GstBuffer **buffers_in, **buffers_out;
686 GstLADSPA *ladspa = (GstLADSPA *)element;
687 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
688 LADSPA_Descriptor *desc = ladspa->descriptor;
690 numsinkpads = oclass->numsinkpads;
691 numsrcpads = oclass->numsrcpads;
693 /* fixme: these mallocs need to die */
694 data_in = g_new0(LADSPA_Data*, numsinkpads);
695 data_out = g_new0(LADSPA_Data*, numsrcpads);
696 buffers_in = g_new0(GstBuffer*, numsinkpads);
697 buffers_out = g_new0(GstBuffer*, numsrcpads);
701 /* first get all the necessary data from the input ports */
702 for (i=0 ; i<numsinkpads ; i++){
704 buffers_in[i] = GST_BUFFER (gst_pad_pull (ladspa->sinkpads[i]));
706 if (GST_IS_EVENT (buffers_in[i])) {
707 /* push it out on all pads */
708 gst_data_ref_by_count ((GstData*)buffers_in[i], numsrcpads);
709 for (j=0; j<numsrcpads; j++)
710 gst_pad_push (ladspa->srcpads[j], GST_DATA (buffers_in[i]));
711 if (GST_EVENT_TYPE (buffers_in[i]) == GST_EVENT_EOS) {
713 gst_element_set_eos (element);
720 if (largest_buffer < 0)
721 largest_buffer = GST_BUFFER_SIZE (buffers_in[i])/sizeof(gfloat);
723 largest_buffer = MIN (GST_BUFFER_SIZE (buffers_in[i])/sizeof(gfloat), largest_buffer);
724 data_in[i] = (LADSPA_Data *) GST_BUFFER_DATA(buffers_in[i]);
725 GST_BUFFER_TIMESTAMP(buffers_in[i]) = ladspa->timestamp;
729 if (!ladspa->inplace_broken) {
730 for (; i<numsrcpads && i<numsinkpads; i++) {
731 /* reuse input buffers */
732 buffers_out[i] = buffers_in[i];
733 data_out[i] = data_in[i];
736 for (; i<numsrcpads; i++) {
737 buffers_out[i] = gst_buffer_new_and_alloc (ladspa->buffer_frames * sizeof(gfloat));
738 GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
739 data_out[i] = (LADSPA_Data*)GST_BUFFER_DATA (buffers_out[i]);
742 GST_DPMAN_PREPROCESS(ladspa->dpman, largest_buffer, ladspa->timestamp);
745 /* split up processing of the buffer into chunks so that dparams can
746 * be updated when required.
747 * In many cases the buffer will be processed in one chunk anyway.
749 while (GST_DPMAN_PROCESS (ladspa->dpman, num_processed)) {
750 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
752 for (i=0 ; i<numsinkpads ; i++)
753 desc->connect_port (ladspa->handle, oclass->sinkpad_portnums[i], data_in[i]);
754 for (i=0 ; i<numsrcpads ; i++)
755 desc->connect_port (ladspa->handle, oclass->srcpad_portnums[i], data_out[i]);
757 desc->run(ladspa->handle, num_to_process);
759 for (i=0 ; i<numsinkpads ; i++)
760 data_in[i] += num_to_process;
761 for (i=0 ; i<numsrcpads ; i++)
762 data_out[i] += num_to_process;
764 num_processed += num_to_process;
767 for (i=0 ; i<numsinkpads ; i++) {
768 if (i >= numsrcpads || buffers_out[i] != buffers_in[i])
769 gst_buffer_unref(buffers_in[i]);
771 buffers_in[i] = NULL;
773 for (i=0 ; i<numsrcpads ; i++) {
774 DEBUG_OBJ (ladspa, "pushing buffer (%p) on src pad %d", buffers_out[i], i);
775 gst_pad_push (ladspa->srcpads[i], GST_DATA (buffers_out[i]));
778 buffers_out[i] = NULL;
781 ladspa->timestamp += ladspa->buffer_frames * GST_SECOND / ladspa->samplerate;
783 /* FIXME: move these mallocs and frees to the state-change handler */
785 g_free (buffers_out);
792 gst_ladspa_chain (GstPad *pad, GstData *_data)
794 GstBuffer *buffer_in = GST_BUFFER (_data);
795 LADSPA_Descriptor *desc;
796 LADSPA_Data *data_in, **data_out = NULL;
797 GstBuffer **buffers_out = NULL;
798 unsigned long num_samples;
799 guint num_to_process, num_processed, i, numsrcpads;
801 GstLADSPAClass *oclass;
803 ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
804 oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
805 data_in = (LADSPA_Data *) GST_BUFFER_DATA(buffer_in);
806 num_samples = GST_BUFFER_SIZE(buffer_in) / sizeof(gfloat);
807 numsrcpads = oclass->numsrcpads;
808 desc = ladspa->descriptor;
810 /* we shouldn't get events here... */
811 g_return_if_fail (GST_IS_BUFFER (buffer_in));
813 /* FIXME: this function shouldn't need to malloc() anything */
814 if (numsrcpads > 0) {
815 buffers_out = g_new(GstBuffer*, numsrcpads);
816 data_out = g_new(LADSPA_Data*, numsrcpads);
820 if (!ladspa->inplace_broken && numsrcpads) {
821 /* reuse the first (chained) buffer */
822 buffers_out[i] = buffer_in;
823 DEBUG ("reuse: %d", GST_BUFFER_SIZE (buffer_in));
824 data_out[i] = data_in;
827 for (; i<numsrcpads; i++) {
828 buffers_out[i] = gst_buffer_new_and_alloc (GST_BUFFER_SIZE(buffer_in));
829 DEBUG ("new %d", GST_BUFFER_SIZE (buffer_in));
830 GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
831 data_out[i] = (LADSPA_Data*)GST_BUFFER_DATA (buffers_out[i]);
834 GST_DPMAN_PREPROCESS(ladspa->dpman, num_samples, GST_BUFFER_TIMESTAMP(buffer_in));
837 /* split up processing of the buffer into chunks so that dparams can
838 * be updated when required.
839 * In many cases the buffer will be processed in one chunk anyway.
841 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
842 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
844 desc->connect_port(ladspa->handle,oclass->sinkpad_portnums[0],data_in);
845 for (i=0 ; i<numsrcpads ; i++)
846 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[i],data_out[i]);
848 desc->run(ladspa->handle, num_to_process);
850 data_in += num_to_process;
851 for (i=0 ; i<numsrcpads ; i++)
852 data_out[i] += num_to_process;
854 num_processed += num_to_process;
857 if (!numsrcpads || buffers_out[0] != buffer_in)
858 gst_buffer_unref(buffer_in);
861 for (i=0; i<numsrcpads; i++) {
862 DEBUG_OBJ (ladspa, "pushing buffer (%p, length %u bytes) on src pad %d",
863 buffers_out[i], GST_BUFFER_SIZE (buffers_out[i]), i);
864 gst_pad_push (ladspa->srcpads[i], GST_DATA (buffers_out[i]));
873 gst_ladspa_get(GstPad *pad)
876 GstLADSPAClass *oclass;
879 LADSPA_Descriptor *desc;
880 guint num_to_process, num_processed;
882 ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
883 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
884 desc = ladspa->descriptor;
886 /* 4096 is arbitrary */
887 buf = gst_buffer_new_and_alloc (4096);
888 GST_BUFFER_TIMESTAMP(buf) = ladspa->timestamp;
889 data = (LADSPA_Data *) GST_BUFFER_DATA(buf);
891 GST_DPMAN_PREPROCESS(ladspa->dpman, ladspa->buffer_frames, ladspa->timestamp);
894 /* split up processing of the buffer into chunks so that dparams can
895 * be updated when required.
896 * In many cases the buffer will be processed in one chunk anyway.
898 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
899 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
901 /* update timestamp */
902 ladspa->timestamp += num_to_process * GST_SECOND / ladspa->samplerate;
904 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);
906 desc->run(ladspa->handle, num_to_process);
908 data += num_to_process;
909 num_processed = num_to_process;
912 return GST_DATA (buf);
916 ladspa_describe_plugin(const char *pcFullFilename,
917 void *pvPluginHandle,
918 LADSPA_Descriptor_Function pfDescriptorFunction)
920 const LADSPA_Descriptor *desc;
922 GTypeInfo typeinfo = {
923 sizeof(GstLADSPAClass),
924 (GBaseInitFunc)gst_ladspa_base_init,
926 (GClassInitFunc)gst_ladspa_class_init,
931 (GInstanceInitFunc)gst_ladspa_init,
935 /* walk through all the plugins in this pluginlibrary */
937 while ((desc = pfDescriptorFunction(i++))) {
940 /* construct the type */
941 type_name = g_strdup_printf("ladspa-%s",desc->Label);
942 g_strcanon (type_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-+", '-');
943 /* if it's already registered, drop it */
944 if (g_type_from_name(type_name)) {
949 /* base-init temp alloc */
950 g_hash_table_insert(ladspa_descriptors,
954 /* create the type now */
955 type = g_type_register_static(GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
956 if (!gst_element_register (ladspa_plugin, type_name, GST_RANK_NONE, type))
959 /* add this plugin to the hash */
960 g_hash_table_insert(ladspa_descriptors,
961 GINT_TO_POINTER(type),
965 g_hash_table_remove (ladspa_descriptors, GINT_TO_POINTER (0));
969 plugin_init (GstPlugin *plugin)
971 GST_DEBUG_CATEGORY_INIT (ladspa_debug, "ladspa",
972 GST_DEBUG_FG_GREEN | GST_DEBUG_BG_BLACK | GST_DEBUG_BOLD,
975 ladspa_descriptors = g_hash_table_new(NULL,NULL);
976 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
978 ladspa_plugin = plugin;
980 LADSPAPluginSearch(ladspa_describe_plugin);
982 /* initialize dparam support library */
983 gst_control_init(NULL,NULL);
992 "All LADSPA plugins",