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_init (GstLADSPA *ladspa);
66 static void gst_ladspa_update_int (const GValue *value, gpointer data);
67 static GstPadLinkReturn gst_ladspa_link (GstPad *pad, GstCaps *caps);
68 static void gst_ladspa_force_src_caps (GstLADSPA *ladspa, GstPad *pad);
70 static void gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
71 static void gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
73 static gboolean gst_ladspa_instantiate (GstLADSPA *ladspa);
74 static void gst_ladspa_activate (GstLADSPA *ladspa);
75 static void gst_ladspa_deactivate (GstLADSPA *ladspa);
77 static GstElementStateReturn gst_ladspa_change_state (GstElement *element);
78 static void gst_ladspa_loop (GstElement *element);
79 static void gst_ladspa_chain (GstPad *pad,GstBuffer *buf);
80 static GstBuffer * gst_ladspa_get (GstPad *pad);
82 static GstElementClass *parent_class = NULL;
84 static GstPlugin *ladspa_plugin;
85 static GHashTable *ladspa_descriptors;
94 GST_DEBUG_CATEGORY_STATIC (ladspa_debug);
96 GST_CAT_LEVEL_LOG (ladspa_debug, GST_LEVEL_DEBUG, NULL, __VA_ARGS__)
97 #define DEBUG_OBJ(obj,...) \
98 GST_CAT_LEVEL_LOG (ladspa_debug, GST_LEVEL_DEBUG, obj, __VA_ARGS__)
101 gst_ladspa_class_init (GstLADSPAClass *klass)
103 GObjectClass *gobject_class;
104 GstElementClass *gstelement_class;
105 LADSPA_Descriptor *desc;
106 gint i,current_portnum,sinkcount,srccount,controlcount;
108 gint argtype,argperms;
109 GParamSpec *paramspec = NULL;
110 gchar *argname, *tempstr, *paren;
112 gobject_class = (GObjectClass*)klass;
113 gstelement_class = (GstElementClass*)klass;
115 gobject_class->set_property = gst_ladspa_set_property;
116 gobject_class->get_property = gst_ladspa_get_property;
118 gstelement_class->change_state = gst_ladspa_change_state;
120 /* look up and store the ladspa descriptor */
121 klass->descriptor = g_hash_table_lookup(ladspa_descriptors,GINT_TO_POINTER(G_TYPE_FROM_CLASS(klass)));
122 desc = klass->descriptor;
124 klass->numports = desc->PortCount;
126 klass->numsinkpads = 0;
127 klass->numsrcpads = 0;
128 klass->numcontrols = 0;
130 /* walk through the ports, count the input, output and control ports */
131 for (i=0; i<desc->PortCount; i++) {
132 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]))
133 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
134 klass->numsinkpads++;
137 else if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
138 klass->numcontrols++;
141 DEBUG ("ladspa element class: init %s with %d sink, %d src, %d control\n",
142 g_type_name (G_TYPE_FROM_CLASS (klass)),
143 klass->numsinkpads, klass->numsrcpads, klass->numcontrols);
145 klass->srcpad_portnums = g_new0(gint,klass->numsrcpads);
146 klass->sinkpad_portnums = g_new0(gint,klass->numsinkpads);
147 klass->control_portnums = g_new0(gint,klass->numcontrols);
152 /* walk through the ports, note the portnums for srcpads, sinkpads and control
154 for (i=0; i<desc->PortCount; i++) {
155 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]))
156 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
157 klass->sinkpad_portnums[sinkcount++] = i;
159 klass->srcpad_portnums[srccount++] = i;
160 else if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
161 klass->control_portnums[controlcount++] = i;
164 /* now build the control info from the control ports */
165 klass->control_info = g_new0(ladspa_control_info,klass->numcontrols);
167 for (i=0;i<klass->numcontrols;i++) {
168 current_portnum = klass->control_portnums[i];
170 /* short name for hint descriptor */
171 hintdesc = desc->PortRangeHints[current_portnum].HintDescriptor;
173 /* get the various bits */
174 if (LADSPA_IS_HINT_TOGGLED(hintdesc))
175 klass->control_info[i].toggled = TRUE;
176 if (LADSPA_IS_HINT_LOGARITHMIC(hintdesc))
177 klass->control_info[i].logarithmic = TRUE;
178 if (LADSPA_IS_HINT_INTEGER(hintdesc))
179 klass->control_info[i].integer = TRUE;
181 /* figure out the argument details */
182 if (klass->control_info[i].toggled) argtype = G_TYPE_BOOLEAN;
183 else if (klass->control_info[i].integer) argtype = G_TYPE_INT;
184 else argtype = G_TYPE_FLOAT;
186 /* grab the bounds */
187 if (LADSPA_IS_HINT_BOUNDED_BELOW(hintdesc)) {
188 klass->control_info[i].lower = TRUE;
189 klass->control_info[i].lowerbound =
190 desc->PortRangeHints[current_portnum].LowerBound;
192 if (argtype==G_TYPE_INT) klass->control_info[i].lowerbound = (gfloat)G_MININT;
193 if (argtype==G_TYPE_FLOAT) klass->control_info[i].lowerbound = -G_MAXFLOAT;
196 if (LADSPA_IS_HINT_BOUNDED_ABOVE(hintdesc)) {
197 klass->control_info[i].upper = TRUE;
198 klass->control_info[i].upperbound =
199 desc->PortRangeHints[current_portnum].UpperBound;
200 if (LADSPA_IS_HINT_SAMPLE_RATE(hintdesc)) {
201 klass->control_info[i].samplerate = TRUE;
202 klass->control_info[i].upperbound *= 44100; /* FIXME? */
205 if (argtype==G_TYPE_INT) klass->control_info[i].upperbound = (gfloat)G_MAXINT;
206 if (argtype==G_TYPE_FLOAT) klass->control_info[i].upperbound = G_MAXFLOAT;
209 /* use the lowerbound as the default value */
210 klass->control_info[i].def = klass->control_info[i].lowerbound;
212 #ifdef LADSPA_IS_HINT_HAS_DEFAULT
213 /* figure out the defaults */
214 if (LADSPA_IS_HINT_HAS_DEFAULT (hintdesc)) {
215 if (LADSPA_IS_HINT_DEFAULT_MINIMUM (hintdesc))
216 klass->control_info[i].def = klass->control_info[i].lowerbound;
217 else if (LADSPA_IS_HINT_DEFAULT_LOW (hintdesc))
218 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
219 klass->control_info[i].def = exp (0.75*log(klass->control_info[i].lowerbound) +
220 0.25*log(klass->control_info[i].upperbound));
222 klass->control_info[i].def = (0.75*klass->control_info[i].lowerbound +
223 0.25*klass->control_info[i].upperbound);
224 else if (LADSPA_IS_HINT_DEFAULT_MIDDLE (hintdesc))
225 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
226 klass->control_info[i].def = exp (0.5*log(klass->control_info[i].lowerbound) +
227 0.5*log(klass->control_info[i].upperbound));
229 klass->control_info[i].def = (0.5*klass->control_info[i].lowerbound +
230 0.5*klass->control_info[i].upperbound);
231 else if (LADSPA_IS_HINT_DEFAULT_HIGH (hintdesc))
232 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
233 klass->control_info[i].def = exp (0.25*log(klass->control_info[i].lowerbound) +
234 0.75*log(klass->control_info[i].upperbound));
236 klass->control_info[i].def = (0.25*klass->control_info[i].lowerbound +
237 0.75*klass->control_info[i].upperbound);
238 else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM (hintdesc))
239 klass->control_info[i].def = klass->control_info[i].upperbound;
240 else if (LADSPA_IS_HINT_DEFAULT_0 (hintdesc))
241 klass->control_info[i].def = 0.0;
242 else if (LADSPA_IS_HINT_DEFAULT_1 (hintdesc))
243 klass->control_info[i].def = 1.0;
244 else if (LADSPA_IS_HINT_DEFAULT_100 (hintdesc))
245 klass->control_info[i].def = 100.0;
246 else if (LADSPA_IS_HINT_DEFAULT_440 (hintdesc))
247 klass->control_info[i].def = 440.0;
249 #endif /* LADSPA_IS_HINT_HAS_DEFAULT */
251 klass->control_info[i].def = CLAMP(klass->control_info[i].def,
252 klass->control_info[i].lowerbound,
253 klass->control_info[i].upperbound);
255 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[current_portnum])) {
256 argperms = G_PARAM_READWRITE;
257 klass->control_info[i].writable = TRUE;
259 argperms = G_PARAM_READABLE;
260 klass->control_info[i].writable = FALSE;
263 klass->control_info[i].name = g_strdup(desc->PortNames[current_portnum]);
264 argname = g_strdup(klass->control_info[i].name);
265 /* find out if there is a (unitname) at the end of the argname and get rid
267 paren = g_strrstr (argname, " (");
271 /* this is the same thing that param_spec_* will do */
272 g_strcanon (argname, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
273 /* satisfy glib2 (argname[0] must be [A-Za-z]) */
274 if (!((argname[0] >= 'a' && argname[0] <= 'z') || (argname[0] >= 'A' && argname[0] <= 'Z'))) {
276 argname = g_strconcat("param-", argname, NULL);
280 /* check for duplicate property names */
281 if (g_object_class_find_property(G_OBJECT_CLASS(klass), argname) != NULL){
283 gchar *numargname = g_strdup_printf("%s_%d",argname,numarg++);
284 while (g_object_class_find_property(G_OBJECT_CLASS(klass), numargname) != NULL){
286 numargname = g_strdup_printf("%s_%d",argname,numarg++);
288 argname = numargname;
291 klass->control_info[i].param_name = argname;
293 DEBUG ("adding arg %s from %s", argname, klass->control_info[i].name);
295 if (argtype==G_TYPE_BOOLEAN){
296 paramspec = g_param_spec_boolean(argname,argname,argname, FALSE, argperms);
297 } else if (argtype==G_TYPE_INT){
298 paramspec = g_param_spec_int(argname,argname,argname,
299 (gint)klass->control_info[i].lowerbound,
300 (gint)klass->control_info[i].upperbound,
301 (gint)klass->control_info[i].def, argperms);
302 } else if (klass->control_info[i].samplerate){
303 paramspec = g_param_spec_float(argname,argname,argname,
307 paramspec = g_param_spec_float(argname,argname,argname,
308 klass->control_info[i].lowerbound, klass->control_info[i].upperbound,
309 klass->control_info[i].def, argperms);
312 /* properties have an offset of 1 */
313 g_object_class_install_property(G_OBJECT_CLASS(klass), i+1, paramspec);
318 gst_ladspa_init (GstLADSPA *ladspa)
320 GstLADSPAClass *oclass;
321 ladspa_control_info cinfo;
323 LADSPA_Descriptor *desc;
324 gint i,sinkcount,srccount;
326 oclass = (GstLADSPAClass*)G_OBJECT_GET_CLASS (ladspa);
327 desc = oclass->descriptor;
328 ladspa->descriptor = oclass->descriptor;
330 /* allocate the various arrays */
331 ladspa->srcpads = g_new0(GstPad*,oclass->numsrcpads);
332 ladspa->sinkpads = g_new0(GstPad*,oclass->numsinkpads);
333 ladspa->controls = g_new(gfloat,oclass->numcontrols);
334 ladspa->dpman = gst_dpman_new ("ladspa_dpman", GST_ELEMENT(ladspa));
339 for (l=GST_ELEMENT_CLASS (oclass)->padtemplates; l; l=l->next) {
340 GstPad *pad = gst_pad_new_from_template (GST_PAD_TEMPLATE (l->data),
341 GST_PAD_TEMPLATE_NAME_TEMPLATE (l->data));
342 gst_pad_set_link_function (pad, gst_ladspa_link);
343 gst_element_add_pad ((GstElement*)ladspa, pad);
345 if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK)
346 ladspa->sinkpads[sinkcount++] = pad;
348 ladspa->srcpads[srccount++] = pad;
352 for (i=0; i<oclass->numcontrols; i++) {
353 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
354 cinfo = oclass->control_info[i];
355 ladspa->controls[i]=cinfo.def;
358 gst_dpman_add_required_dparam_callback (
360 g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
361 0, 1, (gint)(ladspa->controls[i]), G_PARAM_READWRITE),
362 "int", gst_ladspa_update_int, &(ladspa->controls[i])
365 else if (cinfo.integer){
366 gst_dpman_add_required_dparam_callback (
368 g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
369 (gint)cinfo.lowerbound, (gint)cinfo.upperbound,
370 (gint)ladspa->controls[i], G_PARAM_READWRITE),
371 "int", gst_ladspa_update_int, &(ladspa->controls[i])
374 else if (cinfo.samplerate){
375 gst_dpman_add_required_dparam_direct (
377 g_param_spec_float(cinfo.param_name, cinfo.name, cinfo.name,
378 cinfo.lowerbound, cinfo.upperbound,
379 ladspa->controls[i], G_PARAM_READWRITE),
380 "hertz-rate-bound", &(ladspa->controls[i])
384 gst_dpman_add_required_dparam_direct (
386 g_param_spec_float(cinfo.param_name, cinfo.name, cinfo.name,
387 cinfo.lowerbound, cinfo.upperbound,
388 ladspa->controls[i], G_PARAM_READWRITE),
389 "float", &(ladspa->controls[i])
395 /* nonzero default needed to instantiate() some plugins */
396 ladspa->samplerate = 44100;
398 ladspa->buffer_frames = 0; /* should be set with caps */
399 ladspa->activated = FALSE;
400 ladspa->bufpool = NULL;
401 ladspa->inplace_broken = LADSPA_IS_INPLACE_BROKEN(ladspa->descriptor->Properties);
403 if (sinkcount==0 && srccount == 1) {
404 /* get mode (no sink pads) */
405 DEBUG_OBJ (ladspa, "mono get mode with 1 src pad");
407 gst_pad_set_get_function (ladspa->srcpads[0], gst_ladspa_get);
408 } else if (sinkcount==1){
409 /* with one sink we can use the chain function */
410 DEBUG_OBJ (ladspa, "chain mode");
412 gst_pad_set_chain_function (ladspa->sinkpads[0], gst_ladspa_chain);
413 } else if (sinkcount > 1){
414 /* more than one sink pad needs loop mode */
415 DEBUG_OBJ (ladspa, "loop mode with %d sink pads and %d src pads", sinkcount, srccount);
417 gst_element_set_loop_function (GST_ELEMENT (ladspa), gst_ladspa_loop);
418 } else if (sinkcount==0 && srccount == 0) {
419 /* for example, a plugin with only control inputs and output -- just ignore
422 g_warning ("%d sink pads, %d src pads not yet supported", sinkcount, srccount);
425 gst_ladspa_instantiate (ladspa);
429 gst_ladspa_update_int(const GValue *value, gpointer data)
431 gfloat *target = (gfloat*) data;
432 *target = (gfloat)g_value_get_int(value);
435 static GstPadLinkReturn
436 gst_ladspa_link (GstPad *pad, GstCaps *caps)
438 GstElement *element = (GstElement*)GST_PAD_PARENT (pad);
439 GstLADSPA *ladspa = (GstLADSPA*)element;
440 const GList *l = NULL;
443 if (GST_CAPS_IS_FIXED (caps)) {
444 /* if this fails in some other plugin, the graph is left in an inconsistent
446 for (l=gst_element_get_pad_list (element); l; l=l->next)
447 if (pad != (GstPad*)l->data)
448 if (gst_pad_try_set_caps ((GstPad*)l->data, caps) <= 0)
449 return GST_PAD_LINK_REFUSED;
451 /* we assume that the ladspa plugin can handle any sample rate, so this
452 check gets put last */
453 gst_caps_get_int (caps, "rate", &rate);
454 /* have to instantiate ladspa plugin when samplerate changes (groan) */
455 if (ladspa->samplerate != rate) {
456 ladspa->samplerate = rate;
457 if (! gst_ladspa_instantiate(ladspa))
458 return GST_PAD_LINK_REFUSED;
461 gst_caps_get_int (caps, "buffer-frames", &ladspa->buffer_frames);
464 gst_buffer_pool_unref (ladspa->bufpool);
465 ladspa->bufpool = gst_buffer_pool_get_default (ladspa->buffer_frames * sizeof(gfloat),
468 return GST_PAD_LINK_OK;
471 return GST_PAD_LINK_DELAYED;
475 gst_ladspa_force_src_caps(GstLADSPA *ladspa, GstPad *pad)
477 if (!ladspa->buffer_frames) {
478 ladspa->buffer_frames = 256; /* 5 ms at 44100 kHz (just a default...) */
479 g_return_if_fail (ladspa->bufpool == NULL);
481 gst_buffer_pool_get_default (ladspa->buffer_frames * sizeof(gfloat), 3);
484 DEBUG_OBJ (ladspa, "forcing caps with rate=%d, buffer-frames=%d",
485 ladspa->samplerate, ladspa->buffer_frames);
487 gst_pad_try_set_caps (pad,
492 "width", GST_PROPS_INT (32),
493 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
494 "rate", GST_PROPS_INT (ladspa->samplerate),
495 "buffer-frames", GST_PROPS_INT (ladspa->buffer_frames),
496 "channels", GST_PROPS_INT (1),
501 gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
503 GstLADSPA *ladspa = (GstLADSPA*)object;
504 GstLADSPAClass *oclass;
505 ladspa_control_info *control_info;
507 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
509 /* remember, properties have an offset of 1 */
512 /* verify it exists */
513 g_return_if_fail (prop_id < oclass->numcontrols);
515 control_info = &(oclass->control_info[prop_id]);
516 g_return_if_fail (control_info->name != NULL);
518 /* check to see if it's writable */
519 g_return_if_fail (control_info->writable);
521 /* now see what type it is */
522 if (control_info->toggled)
523 ladspa->controls[prop_id] = g_value_get_boolean (value) ? 1.f : 0.f;
524 else if (control_info->integer)
525 ladspa->controls[prop_id] = g_value_get_int (value);
527 ladspa->controls[prop_id] = g_value_get_float (value);
529 DEBUG_OBJ (object, "set arg %s to %f", control_info->name, ladspa->controls[prop_id]);
533 gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
535 GstLADSPA *ladspa = (GstLADSPA*)object;
536 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
537 ladspa_control_info *control_info;
539 /* remember, properties have an offset of 1 */
542 /* verify it exists */
543 g_return_if_fail (prop_id < oclass->numcontrols);
545 control_info = &(oclass->control_info[prop_id]);
546 g_return_if_fail (control_info->name != NULL);
548 /* now see what type it is */
549 if (control_info->toggled)
550 g_value_set_boolean (value, ladspa->controls[prop_id] == 1.0);
551 else if (control_info->integer)
552 g_value_set_int (value, (gint)ladspa->controls[prop_id]);
554 g_value_set_float (value, ladspa->controls[prop_id]);
556 DEBUG_OBJ (object, "got arg %s as %f", control_info->name, ladspa->controls[prop_id]);
560 gst_ladspa_instantiate (GstLADSPA *ladspa)
562 LADSPA_Descriptor *desc;
564 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
565 gboolean was_activated;
567 desc = ladspa->descriptor;
569 /* check for old handle */
570 was_activated = ladspa->activated;
571 if (ladspa->handle != NULL){
572 gst_ladspa_deactivate(ladspa);
573 desc->cleanup(ladspa->handle);
576 /* instantiate the plugin */
577 DEBUG_OBJ (ladspa, "instantiating the plugin at %d Hz", ladspa->samplerate);
579 ladspa->handle = desc->instantiate(desc,ladspa->samplerate);
580 g_return_val_if_fail (ladspa->handle != NULL, FALSE);
582 /* connect the control ports */
583 for (i=0;i<oclass->numcontrols;i++)
584 desc->connect_port(ladspa->handle,
585 oclass->control_portnums[i],
586 &(ladspa->controls[i]));
588 /* reactivate if it was activated before the reinstantiation */
590 gst_ladspa_activate(ladspa);
595 static GstElementStateReturn
596 gst_ladspa_change_state (GstElement *element)
598 LADSPA_Descriptor *desc;
599 GstLADSPA *ladspa = (GstLADSPA*)element;
600 desc = ladspa->descriptor;
602 switch (GST_STATE_TRANSITION (element)) {
603 case GST_STATE_NULL_TO_READY:
604 gst_ladspa_activate(ladspa);
606 case GST_STATE_READY_TO_NULL:
607 gst_ladspa_deactivate(ladspa);
613 if (GST_ELEMENT_CLASS (parent_class)->change_state)
614 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
616 return GST_STATE_SUCCESS;
620 gst_ladspa_activate (GstLADSPA *ladspa)
622 LADSPA_Descriptor *desc;
623 desc = ladspa->descriptor;
625 if (ladspa->activated)
626 gst_ladspa_deactivate(ladspa);
628 DEBUG_OBJ (ladspa, "activating");
630 /* activate the plugin (function might be null) */
631 if (desc->activate != NULL)
632 desc->activate(ladspa->handle);
634 ladspa->activated = TRUE;
638 gst_ladspa_deactivate (GstLADSPA *ladspa)
640 LADSPA_Descriptor *desc;
641 desc = ladspa->descriptor;
643 DEBUG_OBJ (ladspa, "deactivating");
645 /* deactivate the plugin (function might be null) */
646 if (ladspa->activated && (desc->deactivate != NULL))
647 desc->deactivate(ladspa->handle);
649 ladspa->activated = FALSE;
653 gst_ladspa_loop (GstElement *element)
655 guint i, j, numsrcpads, numsinkpads;
656 guint num_processed, num_to_process;
658 LADSPA_Data **data_in, **data_out;
659 GstBuffer **buffers_in, **buffers_out;
661 GstLADSPA *ladspa = (GstLADSPA *)element;
662 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
663 LADSPA_Descriptor *desc = ladspa->descriptor;
665 numsinkpads = oclass->numsinkpads;
666 numsrcpads = oclass->numsrcpads;
668 /* fixme: these mallocs need to die */
669 data_in = g_new0(LADSPA_Data*, numsinkpads);
670 data_out = g_new0(LADSPA_Data*, numsrcpads);
671 buffers_in = g_new0(GstBuffer*, numsinkpads);
672 buffers_out = g_new0(GstBuffer*, numsrcpads);
676 /* first get all the necessary data from the input ports */
677 for (i=0 ; i<numsinkpads ; i++){
679 buffers_in[i] = gst_pad_pull (ladspa->sinkpads[i]);
681 if (GST_IS_EVENT (buffers_in[i])) {
682 /* push it out on all pads */
683 gst_data_ref_by_count ((GstData*)buffers_in[i], numsrcpads);
684 for (j=0; j<numsrcpads; j++)
685 gst_pad_push (ladspa->srcpads[j], buffers_in[i]);
686 if (GST_EVENT_TYPE (buffers_in[i]) == GST_EVENT_EOS) {
688 gst_element_set_eos (element);
695 if (largest_buffer < 0)
696 largest_buffer = GST_BUFFER_SIZE (buffers_in[i])/sizeof(gfloat);
698 largest_buffer = MIN (GST_BUFFER_SIZE (buffers_in[i])/sizeof(gfloat), largest_buffer);
699 data_in[i] = (LADSPA_Data *) GST_BUFFER_DATA(buffers_in[i]);
700 GST_BUFFER_TIMESTAMP(buffers_in[i]) = ladspa->timestamp;
703 if (!ladspa->bufpool) {
704 gst_element_gerror(element, "Caps were never set, GST_ERROR_UNKNOWN,
705 g_strdup ("unconverted error, file a bug"),
706 g_strdup_printf(bailing..."));
711 if (!ladspa->inplace_broken) {
712 for (; i<numsrcpads && i<numsinkpads; i++) {
713 /* reuse input buffers */
714 buffers_out[i] = buffers_in[i];
715 data_out[i] = data_in[i];
718 for (; i<numsrcpads; i++) {
719 /* we have to make new buffers -- at least we're taking them from a pool */
720 buffers_out[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
721 GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
722 data_out[i] = (LADSPA_Data*)GST_BUFFER_DATA (buffers_out[i]);
725 GST_DPMAN_PREPROCESS(ladspa->dpman, largest_buffer, ladspa->timestamp);
728 /* split up processing of the buffer into chunks so that dparams can
729 * be updated when required.
730 * In many cases the buffer will be processed in one chunk anyway.
732 while (GST_DPMAN_PROCESS (ladspa->dpman, num_processed)) {
733 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
735 for (i=0 ; i<numsinkpads ; i++)
736 desc->connect_port (ladspa->handle, oclass->sinkpad_portnums[i], data_in[i]);
737 for (i=0 ; i<numsrcpads ; i++)
738 desc->connect_port (ladspa->handle, oclass->srcpad_portnums[i], data_out[i]);
740 desc->run(ladspa->handle, num_to_process);
742 for (i=0 ; i<numsinkpads ; i++)
743 data_in[i] += num_to_process;
744 for (i=0 ; i<numsrcpads ; i++)
745 data_out[i] += num_to_process;
747 num_processed += num_to_process;
750 for (i=0 ; i<numsinkpads ; i++) {
751 if (i >= numsrcpads || buffers_out[i] != buffers_in[i])
752 gst_buffer_unref(buffers_in[i]);
754 buffers_in[i] = NULL;
756 for (i=0 ; i<numsrcpads ; i++) {
757 DEBUG_OBJ (ladspa, "pushing buffer (%p) on src pad %d", buffers_out[i], i);
758 gst_pad_push (ladspa->srcpads[i], buffers_out[i]);
761 buffers_out[i] = NULL;
764 ladspa->timestamp += ladspa->buffer_frames * GST_SECOND / ladspa->samplerate;
766 /* FIXME: move these mallocs and frees to the state-change handler */
768 g_free (buffers_out);
775 gst_ladspa_chain (GstPad *pad, GstBuffer *buffer_in)
777 LADSPA_Descriptor *desc;
778 LADSPA_Data *data_in, **data_out = NULL;
779 GstBuffer **buffers_out = NULL;
780 unsigned long num_samples;
781 guint num_to_process, num_processed, i, numsrcpads;
783 GstLADSPAClass *oclass;
785 ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
786 oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
787 data_in = (LADSPA_Data *) GST_BUFFER_DATA(buffer_in);
788 num_samples = GST_BUFFER_SIZE(buffer_in) / sizeof(gfloat);
789 numsrcpads = oclass->numsrcpads;
790 desc = ladspa->descriptor;
792 /* we shouldn't get events here... */
793 g_return_if_fail (GST_IS_BUFFER (buffer_in));
795 if (!ladspa->bufpool) {
796 gst_element_gerror((GstElement*)ladspa, "Caps were never set, GST_ERROR_UNKNOWN,
797 g_strdup ("unconverted error, file a bug"),
798 g_strdup_printf(bailing..."));
802 /* FIXME: this function shouldn't need to malloc() anything */
803 if (numsrcpads > 0) {
804 buffers_out = g_new(GstBuffer*, numsrcpads);
805 data_out = g_new(LADSPA_Data*, numsrcpads);
809 if (!ladspa->inplace_broken && numsrcpads) {
810 /* reuse the first (chained) buffer */
811 buffers_out[i] = buffer_in;
812 DEBUG ("reuse: %d", GST_BUFFER_SIZE (buffer_in));
813 data_out[i] = data_in;
816 for (; i<numsrcpads; i++) {
817 /* we have to make new buffers -- at least we're taking them from a pool */
818 buffers_out[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
819 /* the size of the buffer returned from the pool is the maximum size; this
820 chained buffer might be smaller */
821 GST_BUFFER_SIZE (buffers_out[i]) = GST_BUFFER_SIZE (buffer_in);
822 DEBUG ("new %d", GST_BUFFER_SIZE (buffer_in));
823 GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
824 data_out[i] = (LADSPA_Data*)GST_BUFFER_DATA (buffers_out[i]);
827 GST_DPMAN_PREPROCESS(ladspa->dpman, num_samples, GST_BUFFER_TIMESTAMP(buffer_in));
830 /* split up processing of the buffer into chunks so that dparams can
831 * be updated when required.
832 * In many cases the buffer will be processed in one chunk anyway.
834 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
835 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
837 desc->connect_port(ladspa->handle,oclass->sinkpad_portnums[0],data_in);
838 for (i=0 ; i<numsrcpads ; i++)
839 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[i],data_out[i]);
841 desc->run(ladspa->handle, num_to_process);
843 data_in += num_to_process;
844 for (i=0 ; i<numsrcpads ; i++)
845 data_out[i] += num_to_process;
847 num_processed += num_to_process;
850 if (!numsrcpads || buffers_out[0] != buffer_in)
851 gst_buffer_unref(buffer_in);
854 for (i=0; i<numsrcpads; i++) {
855 DEBUG_OBJ (ladspa, "pushing buffer (%p, length %u bytes) on src pad %d",
856 buffers_out[i], GST_BUFFER_SIZE (buffers_out[i]), i);
857 gst_pad_push (ladspa->srcpads[i], buffers_out[i]);
866 gst_ladspa_get(GstPad *pad)
869 GstLADSPAClass *oclass;
872 LADSPA_Descriptor *desc;
873 guint num_to_process, num_processed;
875 ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
876 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
877 desc = ladspa->descriptor;
879 if (!ladspa->bufpool) {
880 /* capsnego hasn't happened... */
881 gst_ladspa_force_src_caps(ladspa, ladspa->srcpads[0]);
884 buf = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
885 GST_BUFFER_TIMESTAMP(buf) = ladspa->timestamp;
886 data = (LADSPA_Data *) GST_BUFFER_DATA(buf);
888 GST_DPMAN_PREPROCESS(ladspa->dpman, ladspa->buffer_frames, ladspa->timestamp);
891 /* split up processing of the buffer into chunks so that dparams can
892 * be updated when required.
893 * In many cases the buffer will be processed in one chunk anyway.
895 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
896 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
898 /* update timestamp */
899 ladspa->timestamp += num_to_process * GST_SECOND / ladspa->samplerate;
901 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);
903 desc->run(ladspa->handle, num_to_process);
905 data += num_to_process;
906 num_processed = num_to_process;
913 ladspa_describe_plugin(const char *pcFullFilename,
914 void *pvPluginHandle,
915 LADSPA_Descriptor_Function pfDescriptorFunction)
917 const LADSPA_Descriptor *desc;
920 GstElementDetails *details;
921 GTypeInfo typeinfo = {
922 sizeof(GstLADSPAClass),
925 (GClassInitFunc)gst_ladspa_class_init,
930 (GInstanceInitFunc)gst_ladspa_init,
933 GstElementFactory *factory;
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)) {
948 /* create the type now */
949 type = g_type_register_static(GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
951 /* construct the element details struct */
952 details = g_new0(GstElementDetails,1);
953 details->longname = g_strdup(desc->Name);
954 details->klass = "Filter/Audio/LADSPA";
955 details->license = g_strdup (desc->Copyright);
956 details->description = details->longname;
957 details->version = g_strdup_printf("%ld",desc->UniqueID);
958 details->author = g_strdup(desc->Maker);
959 details->copyright = g_strdup(desc->Copyright);
961 /* register the plugin with gstreamer */
962 factory = gst_element_factory_new(type_name,type,details);
963 g_return_if_fail(factory != NULL);
964 gst_plugin_add_feature (ladspa_plugin, GST_PLUGIN_FEATURE (factory));
966 /* add this plugin to the hash */
967 g_hash_table_insert(ladspa_descriptors,
968 GINT_TO_POINTER(type),
972 for (j=0;j<desc->PortCount;j++) {
973 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j])) {
974 gchar *name = g_strdup((gchar *)desc->PortNames[j]);
975 g_strcanon (name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
976 /* the factories take ownership of the name */
977 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j]))
978 gst_element_factory_add_pad_template (factory, ladspa_sink_factory (name));
980 gst_element_factory_add_pad_template (factory, ladspa_src_factory (name));
987 plugin_init (GModule *module, GstPlugin *plugin)
989 GST_DEBUG_CATEGORY_INIT (ladspa_debug, "ladspa",
990 GST_DEBUG_FG_GREEN | GST_DEBUG_BG_BLACK | GST_DEBUG_BOLD,
993 ladspa_descriptors = g_hash_table_new(NULL,NULL);
994 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
996 ladspa_plugin = plugin;
998 LADSPAPluginSearch(ladspa_describe_plugin);
1000 if (! gst_library_load ("gstbytestream"))
1003 /* initialize dparam support library */
1004 gst_control_init(NULL,NULL);
1009 GstPluginDesc plugin_desc = {