2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * <2001> Steve Baker <stevebaker_org@yahoo.co.uk>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include <gst/control/control.h>
24 #include <gst/audio/audio.h>
26 #include "gstladspa.h"
27 #include <ladspa.h> /* main ladspa sdk include file */
28 #include "utils.h" /* ladspa sdk utility functions */
31 /* takes ownership of the name */
32 static GstPadTemplate*
33 ladspa_sink_factory (gchar *name)
35 return GST_PAD_TEMPLATE_NEW (
42 GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS
47 /* takes ownership of the name */
48 static GstPadTemplate*
49 ladspa_src_factory (gchar *name)
51 return GST_PAD_TEMPLATE_NEW (
58 GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS
63 static void gst_ladspa_class_init (GstLADSPAClass *klass);
64 static void gst_ladspa_base_init (GstLADSPAClass *klass);
65 static void gst_ladspa_init (GstLADSPA *ladspa);
67 static void gst_ladspa_update_int (const GValue *value, gpointer data);
68 static GstPadLinkReturn gst_ladspa_link (GstPad *pad, GstCaps *caps);
69 static void gst_ladspa_force_src_caps (GstLADSPA *ladspa, GstPad *pad);
71 static void gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
72 static void gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
74 static gboolean gst_ladspa_instantiate (GstLADSPA *ladspa);
75 static void gst_ladspa_activate (GstLADSPA *ladspa);
76 static void gst_ladspa_deactivate (GstLADSPA *ladspa);
78 static GstElementStateReturn gst_ladspa_change_state (GstElement *element);
79 static void gst_ladspa_loop (GstElement *element);
80 static void gst_ladspa_chain (GstPad *pad,GstData *_data);
81 static GstData * gst_ladspa_get (GstPad *pad);
83 static GstElementClass *parent_class = NULL;
85 static GstPlugin *ladspa_plugin;
86 static GHashTable *ladspa_descriptors;
95 GST_DEBUG_CATEGORY_STATIC (ladspa_debug);
97 GST_CAT_LEVEL_LOG (ladspa_debug, GST_LEVEL_DEBUG, NULL, __VA_ARGS__)
98 #define DEBUG_OBJ(obj,...) \
99 GST_CAT_LEVEL_LOG (ladspa_debug, GST_LEVEL_DEBUG, obj, __VA_ARGS__)
102 gst_ladspa_base_init (GstLADSPAClass *klass)
104 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
105 GstPadTemplate *templ;
106 GstElementDetails *details;
107 LADSPA_Descriptor *desc;
108 gint j, sinkcount,srccount;
110 desc = g_hash_table_lookup(ladspa_descriptors,
111 GINT_TO_POINTER(G_TYPE_FROM_CLASS(klass)));
113 desc = g_hash_table_lookup(ladspa_descriptors, GINT_TO_POINTER(0));
116 /* construct the element details struct */
117 details = g_new0(GstElementDetails,1);
118 details->longname = g_strdup(desc->Name);
119 details->klass = "Filter/Audio/LADSPA";
120 details->description = details->longname;
121 details->author = g_strdup(desc->Maker);
122 gst_element_class_set_details (element_class, details);
125 klass->numports = desc->PortCount;
126 klass->numsinkpads = 0;
127 klass->numsrcpads = 0;
128 for (j=0;j<desc->PortCount;j++) {
129 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j])) {
130 gchar *name = g_strdup((gchar *)desc->PortNames[j]);
131 g_strcanon (name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
133 /* the factories take ownership of the name */
134 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j])) {
135 templ = ladspa_sink_factory (name);
136 klass->numsinkpads++;
138 templ = ladspa_src_factory (name);
142 gst_element_class_add_pad_template (element_class, templ);
146 klass->srcpad_portnums = g_new0(gint,klass->numsrcpads);
147 klass->sinkpad_portnums = g_new0(gint,klass->numsinkpads);
151 /* walk through the ports, note the portnums for srcpads, sinkpads */
152 for (j=0; j<desc->PortCount; j++) {
153 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j])) {
154 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j]))
155 klass->sinkpad_portnums[sinkcount++] = j;
157 klass->srcpad_portnums[srccount++] = j;
161 klass->descriptor = desc;
165 gst_ladspa_class_init (GstLADSPAClass *klass)
167 GObjectClass *gobject_class;
168 GstElementClass *gstelement_class;
169 LADSPA_Descriptor *desc;
170 gint i,current_portnum,controlcount;
172 gint argtype,argperms;
173 GParamSpec *paramspec = NULL;
174 gchar *argname, *tempstr, *paren;
176 gobject_class = (GObjectClass*)klass;
177 gstelement_class = (GstElementClass*)klass;
179 gobject_class->set_property = gst_ladspa_set_property;
180 gobject_class->get_property = gst_ladspa_get_property;
182 gstelement_class->change_state = gst_ladspa_change_state;
184 /* look up and store the ladspa descriptor */
185 desc = g_hash_table_lookup(ladspa_descriptors,
186 GINT_TO_POINTER(G_TYPE_FROM_CLASS(klass)));
188 desc = g_hash_table_lookup(ladspa_descriptors, GINT_TO_POINTER(0));
191 klass->numcontrols = 0;
193 /* walk through the ports, count the input, output and control ports */
194 for (i=0; i<desc->PortCount; i++) {
195 if (!LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
196 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
197 klass->numcontrols++;
200 DEBUG ("ladspa element class: init %s with %d sink, %d src, %d control\n",
201 g_type_name (G_TYPE_FROM_CLASS (klass)),
202 klass->numsinkpads, klass->numsrcpads, klass->numcontrols);
204 klass->control_portnums = g_new0(gint,klass->numcontrols);
207 /* walk through the ports, note the portnums for control params */
208 for (i=0; i<desc->PortCount; i++) {
209 if (!LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
210 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
211 klass->control_portnums[controlcount++] = i;
214 /* now build the control info from the control ports */
215 klass->control_info = g_new0(ladspa_control_info,klass->numcontrols);
217 for (i=0;i<klass->numcontrols;i++) {
218 current_portnum = klass->control_portnums[i];
220 /* short name for hint descriptor */
221 hintdesc = desc->PortRangeHints[current_portnum].HintDescriptor;
223 /* get the various bits */
224 if (LADSPA_IS_HINT_TOGGLED(hintdesc))
225 klass->control_info[i].toggled = TRUE;
226 if (LADSPA_IS_HINT_LOGARITHMIC(hintdesc))
227 klass->control_info[i].logarithmic = TRUE;
228 if (LADSPA_IS_HINT_INTEGER(hintdesc))
229 klass->control_info[i].integer = TRUE;
231 /* figure out the argument details */
232 if (klass->control_info[i].toggled) argtype = G_TYPE_BOOLEAN;
233 else if (klass->control_info[i].integer) argtype = G_TYPE_INT;
234 else argtype = G_TYPE_FLOAT;
236 /* grab the bounds */
237 if (LADSPA_IS_HINT_BOUNDED_BELOW(hintdesc)) {
238 klass->control_info[i].lower = TRUE;
239 klass->control_info[i].lowerbound =
240 desc->PortRangeHints[current_portnum].LowerBound;
242 if (argtype==G_TYPE_INT) klass->control_info[i].lowerbound = (gfloat)G_MININT;
243 if (argtype==G_TYPE_FLOAT) klass->control_info[i].lowerbound = -G_MAXFLOAT;
246 if (LADSPA_IS_HINT_BOUNDED_ABOVE(hintdesc)) {
247 klass->control_info[i].upper = TRUE;
248 klass->control_info[i].upperbound =
249 desc->PortRangeHints[current_portnum].UpperBound;
250 if (LADSPA_IS_HINT_SAMPLE_RATE(hintdesc)) {
251 klass->control_info[i].samplerate = TRUE;
252 klass->control_info[i].upperbound *= 44100; /* FIXME? */
255 if (argtype==G_TYPE_INT) klass->control_info[i].upperbound = (gfloat)G_MAXINT;
256 if (argtype==G_TYPE_FLOAT) klass->control_info[i].upperbound = G_MAXFLOAT;
259 /* use the lowerbound as the default value */
260 klass->control_info[i].def = klass->control_info[i].lowerbound;
262 #ifdef LADSPA_IS_HINT_HAS_DEFAULT
263 /* figure out the defaults */
264 if (LADSPA_IS_HINT_HAS_DEFAULT (hintdesc)) {
265 if (LADSPA_IS_HINT_DEFAULT_MINIMUM (hintdesc))
266 klass->control_info[i].def = klass->control_info[i].lowerbound;
267 else if (LADSPA_IS_HINT_DEFAULT_LOW (hintdesc))
268 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
269 klass->control_info[i].def = exp (0.75*log(klass->control_info[i].lowerbound) +
270 0.25*log(klass->control_info[i].upperbound));
272 klass->control_info[i].def = (0.75*klass->control_info[i].lowerbound +
273 0.25*klass->control_info[i].upperbound);
274 else if (LADSPA_IS_HINT_DEFAULT_MIDDLE (hintdesc))
275 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
276 klass->control_info[i].def = exp (0.5*log(klass->control_info[i].lowerbound) +
277 0.5*log(klass->control_info[i].upperbound));
279 klass->control_info[i].def = (0.5*klass->control_info[i].lowerbound +
280 0.5*klass->control_info[i].upperbound);
281 else if (LADSPA_IS_HINT_DEFAULT_HIGH (hintdesc))
282 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
283 klass->control_info[i].def = exp (0.25*log(klass->control_info[i].lowerbound) +
284 0.75*log(klass->control_info[i].upperbound));
286 klass->control_info[i].def = (0.25*klass->control_info[i].lowerbound +
287 0.75*klass->control_info[i].upperbound);
288 else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM (hintdesc))
289 klass->control_info[i].def = klass->control_info[i].upperbound;
290 else if (LADSPA_IS_HINT_DEFAULT_0 (hintdesc))
291 klass->control_info[i].def = 0.0;
292 else if (LADSPA_IS_HINT_DEFAULT_1 (hintdesc))
293 klass->control_info[i].def = 1.0;
294 else if (LADSPA_IS_HINT_DEFAULT_100 (hintdesc))
295 klass->control_info[i].def = 100.0;
296 else if (LADSPA_IS_HINT_DEFAULT_440 (hintdesc))
297 klass->control_info[i].def = 440.0;
299 #endif /* LADSPA_IS_HINT_HAS_DEFAULT */
301 klass->control_info[i].def = CLAMP(klass->control_info[i].def,
302 klass->control_info[i].lowerbound,
303 klass->control_info[i].upperbound);
305 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[current_portnum])) {
306 argperms = G_PARAM_READWRITE;
307 klass->control_info[i].writable = TRUE;
309 argperms = G_PARAM_READABLE;
310 klass->control_info[i].writable = FALSE;
313 klass->control_info[i].name = g_strdup(desc->PortNames[current_portnum]);
314 argname = g_strdup(klass->control_info[i].name);
315 /* find out if there is a (unitname) at the end of the argname and get rid
317 paren = g_strrstr (argname, " (");
321 /* this is the same thing that param_spec_* will do */
322 g_strcanon (argname, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
323 /* satisfy glib2 (argname[0] must be [A-Za-z]) */
324 if (!((argname[0] >= 'a' && argname[0] <= 'z') || (argname[0] >= 'A' && argname[0] <= 'Z'))) {
326 argname = g_strconcat("param-", argname, NULL);
330 /* check for duplicate property names */
331 if (g_object_class_find_property(G_OBJECT_CLASS(klass), argname) != NULL){
333 gchar *numargname = g_strdup_printf("%s_%d",argname,numarg++);
334 while (g_object_class_find_property(G_OBJECT_CLASS(klass), numargname) != NULL){
336 numargname = g_strdup_printf("%s_%d",argname,numarg++);
338 argname = numargname;
341 klass->control_info[i].param_name = argname;
343 DEBUG ("adding arg %s from %s", argname, klass->control_info[i].name);
345 if (argtype==G_TYPE_BOOLEAN){
346 paramspec = g_param_spec_boolean(argname,argname,argname, FALSE, argperms);
347 } else if (argtype==G_TYPE_INT){
348 paramspec = g_param_spec_int(argname,argname,argname,
349 (gint)klass->control_info[i].lowerbound,
350 (gint)klass->control_info[i].upperbound,
351 (gint)klass->control_info[i].def, argperms);
352 } else if (klass->control_info[i].samplerate){
353 paramspec = g_param_spec_float(argname,argname,argname,
357 paramspec = g_param_spec_float(argname,argname,argname,
358 klass->control_info[i].lowerbound, klass->control_info[i].upperbound,
359 klass->control_info[i].def, argperms);
362 /* properties have an offset of 1 */
363 g_object_class_install_property(G_OBJECT_CLASS(klass), i+1, paramspec);
368 gst_ladspa_init (GstLADSPA *ladspa)
370 GstLADSPAClass *oclass;
371 ladspa_control_info cinfo;
373 LADSPA_Descriptor *desc;
374 gint i,sinkcount,srccount;
376 oclass = (GstLADSPAClass*)G_OBJECT_GET_CLASS (ladspa);
377 desc = oclass->descriptor;
378 ladspa->descriptor = oclass->descriptor;
380 /* allocate the various arrays */
381 ladspa->srcpads = g_new0(GstPad*,oclass->numsrcpads);
382 ladspa->sinkpads = g_new0(GstPad*,oclass->numsinkpads);
383 ladspa->controls = g_new(gfloat,oclass->numcontrols);
384 ladspa->dpman = gst_dpman_new ("ladspa_dpman", GST_ELEMENT(ladspa));
389 for (l=GST_ELEMENT_CLASS (oclass)->padtemplates; l; l=l->next) {
390 GstPad *pad = gst_pad_new_from_template (GST_PAD_TEMPLATE (l->data),
391 GST_PAD_TEMPLATE_NAME_TEMPLATE (l->data));
392 gst_pad_set_link_function (pad, gst_ladspa_link);
393 gst_element_add_pad ((GstElement*)ladspa, pad);
395 if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK)
396 ladspa->sinkpads[sinkcount++] = pad;
398 ladspa->srcpads[srccount++] = pad;
402 for (i=0; i<oclass->numcontrols; i++) {
403 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
404 cinfo = oclass->control_info[i];
405 ladspa->controls[i]=cinfo.def;
408 gst_dpman_add_required_dparam_callback (
410 g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
411 0, 1, (gint)(ladspa->controls[i]), G_PARAM_READWRITE),
412 "int", gst_ladspa_update_int, &(ladspa->controls[i])
415 else if (cinfo.integer){
416 gst_dpman_add_required_dparam_callback (
418 g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
419 (gint)cinfo.lowerbound, (gint)cinfo.upperbound,
420 (gint)ladspa->controls[i], G_PARAM_READWRITE),
421 "int", gst_ladspa_update_int, &(ladspa->controls[i])
424 else if (cinfo.samplerate){
425 gst_dpman_add_required_dparam_direct (
427 g_param_spec_float(cinfo.param_name, cinfo.name, cinfo.name,
428 cinfo.lowerbound, cinfo.upperbound,
429 ladspa->controls[i], G_PARAM_READWRITE),
430 "hertz-rate-bound", &(ladspa->controls[i])
434 gst_dpman_add_required_dparam_direct (
436 g_param_spec_float(cinfo.param_name, cinfo.name, cinfo.name,
437 cinfo.lowerbound, cinfo.upperbound,
438 ladspa->controls[i], G_PARAM_READWRITE),
439 "float", &(ladspa->controls[i])
445 /* nonzero default needed to instantiate() some plugins */
446 ladspa->samplerate = 44100;
448 ladspa->buffer_frames = 0; /* should be set with caps */
449 ladspa->activated = FALSE;
450 ladspa->bufpool = NULL;
451 ladspa->inplace_broken = LADSPA_IS_INPLACE_BROKEN(ladspa->descriptor->Properties);
453 if (sinkcount==0 && srccount == 1) {
454 /* get mode (no sink pads) */
455 DEBUG_OBJ (ladspa, "mono get mode with 1 src pad");
457 gst_pad_set_get_function (ladspa->srcpads[0], gst_ladspa_get);
458 } else if (sinkcount==1){
459 /* with one sink we can use the chain function */
460 DEBUG_OBJ (ladspa, "chain mode");
462 gst_pad_set_chain_function (ladspa->sinkpads[0], gst_ladspa_chain);
463 } else if (sinkcount > 1){
464 /* more than one sink pad needs loop mode */
465 DEBUG_OBJ (ladspa, "loop mode with %d sink pads and %d src pads", sinkcount, srccount);
467 gst_element_set_loop_function (GST_ELEMENT (ladspa), gst_ladspa_loop);
468 } else if (sinkcount==0 && srccount == 0) {
469 /* for example, a plugin with only control inputs and output -- just ignore
472 g_warning ("%d sink pads, %d src pads not yet supported", sinkcount, srccount);
475 gst_ladspa_instantiate (ladspa);
479 gst_ladspa_update_int(const GValue *value, gpointer data)
481 gfloat *target = (gfloat*) data;
482 *target = (gfloat)g_value_get_int(value);
485 static GstPadLinkReturn
486 gst_ladspa_link (GstPad *pad, GstCaps *caps)
488 GstElement *element = (GstElement*)GST_PAD_PARENT (pad);
489 GstLADSPA *ladspa = (GstLADSPA*)element;
490 const GList *l = NULL;
493 if (GST_CAPS_IS_FIXED (caps)) {
494 /* if this fails in some other plugin, the graph is left in an inconsistent
496 for (l=gst_element_get_pad_list (element); l; l=l->next)
497 if (pad != (GstPad*)l->data)
498 if (gst_pad_try_set_caps ((GstPad*)l->data, caps) <= 0)
499 return GST_PAD_LINK_REFUSED;
501 /* we assume that the ladspa plugin can handle any sample rate, so this
502 check gets put last */
503 gst_caps_get_int (caps, "rate", &rate);
504 /* have to instantiate ladspa plugin when samplerate changes (groan) */
505 if (ladspa->samplerate != rate) {
506 ladspa->samplerate = rate;
507 if (! gst_ladspa_instantiate(ladspa))
508 return GST_PAD_LINK_REFUSED;
511 gst_caps_get_int (caps, "buffer-frames", &ladspa->buffer_frames);
514 gst_buffer_pool_unref (ladspa->bufpool);
515 ladspa->bufpool = gst_buffer_pool_get_default (ladspa->buffer_frames * sizeof(gfloat),
518 return GST_PAD_LINK_OK;
521 return GST_PAD_LINK_DELAYED;
525 gst_ladspa_force_src_caps(GstLADSPA *ladspa, GstPad *pad)
527 if (!ladspa->buffer_frames) {
528 ladspa->buffer_frames = 256; /* 5 ms at 44100 kHz (just a default...) */
529 g_return_if_fail (ladspa->bufpool == NULL);
531 gst_buffer_pool_get_default (ladspa->buffer_frames * sizeof(gfloat), 3);
534 DEBUG_OBJ (ladspa, "forcing caps with rate=%d, buffer-frames=%d",
535 ladspa->samplerate, ladspa->buffer_frames);
537 gst_pad_try_set_caps (pad,
542 "width", GST_PROPS_INT (32),
543 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
544 "rate", GST_PROPS_INT (ladspa->samplerate),
545 "buffer-frames", GST_PROPS_INT (ladspa->buffer_frames),
546 "channels", GST_PROPS_INT (1),
551 gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
553 GstLADSPA *ladspa = (GstLADSPA*)object;
554 GstLADSPAClass *oclass;
555 ladspa_control_info *control_info;
557 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
559 /* remember, properties have an offset of 1 */
562 /* verify it exists */
563 g_return_if_fail (prop_id < oclass->numcontrols);
565 control_info = &(oclass->control_info[prop_id]);
566 g_return_if_fail (control_info->name != NULL);
568 /* check to see if it's writable */
569 g_return_if_fail (control_info->writable);
571 /* now see what type it is */
572 if (control_info->toggled)
573 ladspa->controls[prop_id] = g_value_get_boolean (value) ? 1.f : 0.f;
574 else if (control_info->integer)
575 ladspa->controls[prop_id] = g_value_get_int (value);
577 ladspa->controls[prop_id] = g_value_get_float (value);
579 DEBUG_OBJ (object, "set arg %s to %f", control_info->name, ladspa->controls[prop_id]);
583 gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
585 GstLADSPA *ladspa = (GstLADSPA*)object;
586 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
587 ladspa_control_info *control_info;
589 /* remember, properties have an offset of 1 */
592 /* verify it exists */
593 g_return_if_fail (prop_id < oclass->numcontrols);
595 control_info = &(oclass->control_info[prop_id]);
596 g_return_if_fail (control_info->name != NULL);
598 /* now see what type it is */
599 if (control_info->toggled)
600 g_value_set_boolean (value, ladspa->controls[prop_id] == 1.0);
601 else if (control_info->integer)
602 g_value_set_int (value, (gint)ladspa->controls[prop_id]);
604 g_value_set_float (value, ladspa->controls[prop_id]);
606 DEBUG_OBJ (object, "got arg %s as %f", control_info->name, ladspa->controls[prop_id]);
610 gst_ladspa_instantiate (GstLADSPA *ladspa)
612 LADSPA_Descriptor *desc;
614 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
615 gboolean was_activated;
617 desc = ladspa->descriptor;
619 /* check for old handle */
620 was_activated = ladspa->activated;
621 if (ladspa->handle != NULL){
622 gst_ladspa_deactivate(ladspa);
623 desc->cleanup(ladspa->handle);
626 /* instantiate the plugin */
627 DEBUG_OBJ (ladspa, "instantiating the plugin at %d Hz", ladspa->samplerate);
629 ladspa->handle = desc->instantiate(desc,ladspa->samplerate);
630 g_return_val_if_fail (ladspa->handle != NULL, FALSE);
632 /* connect the control ports */
633 for (i=0;i<oclass->numcontrols;i++)
634 desc->connect_port(ladspa->handle,
635 oclass->control_portnums[i],
636 &(ladspa->controls[i]));
638 /* reactivate if it was activated before the reinstantiation */
640 gst_ladspa_activate(ladspa);
645 static GstElementStateReturn
646 gst_ladspa_change_state (GstElement *element)
648 LADSPA_Descriptor *desc;
649 GstLADSPA *ladspa = (GstLADSPA*)element;
650 desc = ladspa->descriptor;
652 switch (GST_STATE_TRANSITION (element)) {
653 case GST_STATE_NULL_TO_READY:
654 gst_ladspa_activate(ladspa);
656 case GST_STATE_READY_TO_NULL:
657 gst_ladspa_deactivate(ladspa);
663 if (GST_ELEMENT_CLASS (parent_class)->change_state)
664 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
666 return GST_STATE_SUCCESS;
670 gst_ladspa_activate (GstLADSPA *ladspa)
672 LADSPA_Descriptor *desc;
673 desc = ladspa->descriptor;
675 if (ladspa->activated)
676 gst_ladspa_deactivate(ladspa);
678 DEBUG_OBJ (ladspa, "activating");
680 /* activate the plugin (function might be null) */
681 if (desc->activate != NULL)
682 desc->activate(ladspa->handle);
684 ladspa->activated = TRUE;
688 gst_ladspa_deactivate (GstLADSPA *ladspa)
690 LADSPA_Descriptor *desc;
691 desc = ladspa->descriptor;
693 DEBUG_OBJ (ladspa, "deactivating");
695 /* deactivate the plugin (function might be null) */
696 if (ladspa->activated && (desc->deactivate != NULL))
697 desc->deactivate(ladspa->handle);
699 ladspa->activated = FALSE;
703 gst_ladspa_loop (GstElement *element)
705 guint i, j, numsrcpads, numsinkpads;
706 guint num_processed, num_to_process;
708 LADSPA_Data **data_in, **data_out;
709 GstBuffer **buffers_in, **buffers_out;
711 GstLADSPA *ladspa = (GstLADSPA *)element;
712 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
713 LADSPA_Descriptor *desc = ladspa->descriptor;
715 numsinkpads = oclass->numsinkpads;
716 numsrcpads = oclass->numsrcpads;
718 /* fixme: these mallocs need to die */
719 data_in = g_new0(LADSPA_Data*, numsinkpads);
720 data_out = g_new0(LADSPA_Data*, numsrcpads);
721 buffers_in = g_new0(GstBuffer*, numsinkpads);
722 buffers_out = g_new0(GstBuffer*, numsrcpads);
726 /* first get all the necessary data from the input ports */
727 for (i=0 ; i<numsinkpads ; i++){
729 buffers_in[i] = GST_BUFFER (gst_pad_pull (ladspa->sinkpads[i]));
731 if (GST_IS_EVENT (buffers_in[i])) {
732 /* push it out on all pads */
733 gst_data_ref_by_count ((GstData*)buffers_in[i], numsrcpads);
734 for (j=0; j<numsrcpads; j++)
735 gst_pad_push (ladspa->srcpads[j], GST_DATA (buffers_in[i]));
736 if (GST_EVENT_TYPE (buffers_in[i]) == GST_EVENT_EOS) {
738 gst_element_set_eos (element);
745 if (largest_buffer < 0)
746 largest_buffer = GST_BUFFER_SIZE (buffers_in[i])/sizeof(gfloat);
748 largest_buffer = MIN (GST_BUFFER_SIZE (buffers_in[i])/sizeof(gfloat), largest_buffer);
749 data_in[i] = (LADSPA_Data *) GST_BUFFER_DATA(buffers_in[i]);
750 GST_BUFFER_TIMESTAMP(buffers_in[i]) = ladspa->timestamp;
753 if (!ladspa->bufpool) {
754 gst_element_error (element, "Caps were never set, bailing...");
759 if (!ladspa->inplace_broken) {
760 for (; i<numsrcpads && i<numsinkpads; i++) {
761 /* reuse input buffers */
762 buffers_out[i] = buffers_in[i];
763 data_out[i] = data_in[i];
766 for (; i<numsrcpads; i++) {
767 /* we have to make new buffers -- at least we're taking them from a pool */
768 buffers_out[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
769 GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
770 data_out[i] = (LADSPA_Data*)GST_BUFFER_DATA (buffers_out[i]);
773 GST_DPMAN_PREPROCESS(ladspa->dpman, largest_buffer, ladspa->timestamp);
776 /* split up processing of the buffer into chunks so that dparams can
777 * be updated when required.
778 * In many cases the buffer will be processed in one chunk anyway.
780 while (GST_DPMAN_PROCESS (ladspa->dpman, num_processed)) {
781 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
783 for (i=0 ; i<numsinkpads ; i++)
784 desc->connect_port (ladspa->handle, oclass->sinkpad_portnums[i], data_in[i]);
785 for (i=0 ; i<numsrcpads ; i++)
786 desc->connect_port (ladspa->handle, oclass->srcpad_portnums[i], data_out[i]);
788 desc->run(ladspa->handle, num_to_process);
790 for (i=0 ; i<numsinkpads ; i++)
791 data_in[i] += num_to_process;
792 for (i=0 ; i<numsrcpads ; i++)
793 data_out[i] += num_to_process;
795 num_processed += num_to_process;
798 for (i=0 ; i<numsinkpads ; i++) {
799 if (i >= numsrcpads || buffers_out[i] != buffers_in[i])
800 gst_buffer_unref(buffers_in[i]);
802 buffers_in[i] = NULL;
804 for (i=0 ; i<numsrcpads ; i++) {
805 DEBUG_OBJ (ladspa, "pushing buffer (%p) on src pad %d", buffers_out[i], i);
806 gst_pad_push (ladspa->srcpads[i], GST_DATA (buffers_out[i]));
809 buffers_out[i] = NULL;
812 ladspa->timestamp += ladspa->buffer_frames * GST_SECOND / ladspa->samplerate;
814 /* FIXME: move these mallocs and frees to the state-change handler */
816 g_free (buffers_out);
823 gst_ladspa_chain (GstPad *pad, GstData *_data)
825 GstBuffer *buffer_in = GST_BUFFER (_data);
826 LADSPA_Descriptor *desc;
827 LADSPA_Data *data_in, **data_out = NULL;
828 GstBuffer **buffers_out = NULL;
829 unsigned long num_samples;
830 guint num_to_process, num_processed, i, numsrcpads;
832 GstLADSPAClass *oclass;
834 ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
835 oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
836 data_in = (LADSPA_Data *) GST_BUFFER_DATA(buffer_in);
837 num_samples = GST_BUFFER_SIZE(buffer_in) / sizeof(gfloat);
838 numsrcpads = oclass->numsrcpads;
839 desc = ladspa->descriptor;
841 /* we shouldn't get events here... */
842 g_return_if_fail (GST_IS_BUFFER (buffer_in));
844 if (!ladspa->bufpool) {
845 gst_element_error ((GstElement*)ladspa, "Caps were never set, bailing...");
849 /* FIXME: this function shouldn't need to malloc() anything */
850 if (numsrcpads > 0) {
851 buffers_out = g_new(GstBuffer*, numsrcpads);
852 data_out = g_new(LADSPA_Data*, numsrcpads);
856 if (!ladspa->inplace_broken && numsrcpads) {
857 /* reuse the first (chained) buffer */
858 buffers_out[i] = buffer_in;
859 DEBUG ("reuse: %d", GST_BUFFER_SIZE (buffer_in));
860 data_out[i] = data_in;
863 for (; i<numsrcpads; i++) {
864 /* we have to make new buffers -- at least we're taking them from a pool */
865 buffers_out[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
866 /* the size of the buffer returned from the pool is the maximum size; this
867 chained buffer might be smaller */
868 GST_BUFFER_SIZE (buffers_out[i]) = GST_BUFFER_SIZE (buffer_in);
869 DEBUG ("new %d", GST_BUFFER_SIZE (buffer_in));
870 GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
871 data_out[i] = (LADSPA_Data*)GST_BUFFER_DATA (buffers_out[i]);
874 GST_DPMAN_PREPROCESS(ladspa->dpman, num_samples, GST_BUFFER_TIMESTAMP(buffer_in));
877 /* split up processing of the buffer into chunks so that dparams can
878 * be updated when required.
879 * In many cases the buffer will be processed in one chunk anyway.
881 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
882 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
884 desc->connect_port(ladspa->handle,oclass->sinkpad_portnums[0],data_in);
885 for (i=0 ; i<numsrcpads ; i++)
886 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[i],data_out[i]);
888 desc->run(ladspa->handle, num_to_process);
890 data_in += num_to_process;
891 for (i=0 ; i<numsrcpads ; i++)
892 data_out[i] += num_to_process;
894 num_processed += num_to_process;
897 if (!numsrcpads || buffers_out[0] != buffer_in)
898 gst_buffer_unref(buffer_in);
901 for (i=0; i<numsrcpads; i++) {
902 DEBUG_OBJ (ladspa, "pushing buffer (%p, length %u bytes) on src pad %d",
903 buffers_out[i], GST_BUFFER_SIZE (buffers_out[i]), i);
904 gst_pad_push (ladspa->srcpads[i], GST_DATA (buffers_out[i]));
913 gst_ladspa_get(GstPad *pad)
916 GstLADSPAClass *oclass;
919 LADSPA_Descriptor *desc;
920 guint num_to_process, num_processed;
922 ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
923 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
924 desc = ladspa->descriptor;
926 if (!ladspa->bufpool) {
927 /* capsnego hasn't happened... */
928 gst_ladspa_force_src_caps(ladspa, ladspa->srcpads[0]);
931 buf = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
932 GST_BUFFER_TIMESTAMP(buf) = ladspa->timestamp;
933 data = (LADSPA_Data *) GST_BUFFER_DATA(buf);
935 GST_DPMAN_PREPROCESS(ladspa->dpman, ladspa->buffer_frames, ladspa->timestamp);
938 /* split up processing of the buffer into chunks so that dparams can
939 * be updated when required.
940 * In many cases the buffer will be processed in one chunk anyway.
942 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
943 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
945 /* update timestamp */
946 ladspa->timestamp += num_to_process * GST_SECOND / ladspa->samplerate;
948 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);
950 desc->run(ladspa->handle, num_to_process);
952 data += num_to_process;
953 num_processed = num_to_process;
956 return GST_DATA (buf);
960 ladspa_describe_plugin(const char *pcFullFilename,
961 void *pvPluginHandle,
962 LADSPA_Descriptor_Function pfDescriptorFunction)
964 const LADSPA_Descriptor *desc;
966 GTypeInfo typeinfo = {
967 sizeof(GstLADSPAClass),
968 (GBaseInitFunc)gst_ladspa_base_init,
970 (GClassInitFunc)gst_ladspa_class_init,
975 (GInstanceInitFunc)gst_ladspa_init,
979 /* walk through all the plugins in this pluginlibrary */
981 while ((desc = pfDescriptorFunction(i++))) {
984 /* construct the type */
985 type_name = g_strdup_printf("ladspa-%s",desc->Label);
986 g_strcanon (type_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-+", '-');
987 /* if it's already registered, drop it */
988 if (g_type_from_name(type_name)) {
993 /* base-init temp alloc */
994 g_hash_table_insert(ladspa_descriptors,
998 /* create the type now */
999 type = g_type_register_static(GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
1000 if (!gst_element_register (ladspa_plugin, type_name, GST_RANK_NONE, type))
1003 /* add this plugin to the hash */
1004 g_hash_table_insert(ladspa_descriptors,
1005 GINT_TO_POINTER(type),
1009 g_hash_table_remove (ladspa_descriptors, GINT_TO_POINTER (0));
1013 plugin_init (GstPlugin *plugin)
1015 GST_DEBUG_CATEGORY_INIT (ladspa_debug, "ladspa",
1016 GST_DEBUG_FG_GREEN | GST_DEBUG_BG_BLACK | GST_DEBUG_BOLD,
1019 ladspa_descriptors = g_hash_table_new(NULL,NULL);
1020 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
1022 ladspa_plugin = plugin;
1024 LADSPAPluginSearch(ladspa_describe_plugin);
1026 /* initialize dparam support library */
1027 gst_control_init(NULL,NULL);
1036 "All LADSPA plugins",
1040 "(c) 2003 The LADSPA team",
1042 "http://www.ladspa.org/"