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.
26 #include <gst/control/control.h>
27 #include <gst/audio/audio.h>
29 #include "gstladspa.h"
30 #include <ladspa.h> /* main ladspa sdk include file */
31 #include "utils.h" /* ladspa sdk utility functions */
34 /* takes ownership of the name */
35 static GstPadTemplate*
36 ladspa_sink_factory (gchar *name)
38 return GST_PAD_TEMPLATE_NEW (
45 GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS
50 /* takes ownership of the name */
51 static GstPadTemplate*
52 ladspa_src_factory (gchar *name)
54 return GST_PAD_TEMPLATE_NEW (
61 GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS
66 static void gst_ladspa_class_init (GstLADSPAClass *klass);
67 static void gst_ladspa_base_init (GstLADSPAClass *klass);
68 static void gst_ladspa_init (GstLADSPA *ladspa);
70 static void gst_ladspa_update_int (const GValue *value, gpointer data);
71 static GstPadLinkReturn gst_ladspa_link (GstPad *pad, GstCaps *caps);
72 static void gst_ladspa_force_src_caps (GstLADSPA *ladspa, GstPad *pad);
74 static void gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
75 static void gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
77 static gboolean gst_ladspa_instantiate (GstLADSPA *ladspa);
78 static void gst_ladspa_activate (GstLADSPA *ladspa);
79 static void gst_ladspa_deactivate (GstLADSPA *ladspa);
81 static GstElementStateReturn gst_ladspa_change_state (GstElement *element);
82 static void gst_ladspa_loop (GstElement *element);
83 static void gst_ladspa_chain (GstPad *pad,GstData *_data);
84 static GstData * gst_ladspa_get (GstPad *pad);
86 static GstElementClass *parent_class = NULL;
88 static GstPlugin *ladspa_plugin;
89 static GHashTable *ladspa_descriptors;
98 GST_DEBUG_CATEGORY_STATIC (ladspa_debug);
100 GST_CAT_LEVEL_LOG (ladspa_debug, GST_LEVEL_DEBUG, NULL, __VA_ARGS__)
101 #define DEBUG_OBJ(obj,...) \
102 GST_CAT_LEVEL_LOG (ladspa_debug, GST_LEVEL_DEBUG, obj, __VA_ARGS__)
105 gst_ladspa_base_init (GstLADSPAClass *klass)
107 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
108 GstPadTemplate *templ;
109 GstElementDetails *details;
110 LADSPA_Descriptor *desc;
111 gint j, sinkcount,srccount;
113 desc = g_hash_table_lookup(ladspa_descriptors,
114 GINT_TO_POINTER(G_TYPE_FROM_CLASS(klass)));
116 desc = g_hash_table_lookup(ladspa_descriptors, GINT_TO_POINTER(0));
119 /* construct the element details struct */
120 details = g_new0(GstElementDetails,1);
121 details->longname = g_strdup(desc->Name);
122 details->klass = "Filter/Effect/Audio/LADSPA";
123 details->description = details->longname;
124 details->author = g_strdup(desc->Maker);
125 gst_element_class_set_details (element_class, details);
128 klass->numports = desc->PortCount;
129 klass->numsinkpads = 0;
130 klass->numsrcpads = 0;
131 for (j=0;j<desc->PortCount;j++) {
132 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j])) {
133 gchar *name = g_strdup((gchar *)desc->PortNames[j]);
134 g_strcanon (name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
136 /* the factories take ownership of the name */
137 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j])) {
138 templ = ladspa_sink_factory (name);
139 klass->numsinkpads++;
141 templ = ladspa_src_factory (name);
145 gst_element_class_add_pad_template (element_class, templ);
149 klass->srcpad_portnums = g_new0(gint,klass->numsrcpads);
150 klass->sinkpad_portnums = g_new0(gint,klass->numsinkpads);
154 /* walk through the ports, note the portnums for srcpads, sinkpads */
155 for (j=0; j<desc->PortCount; j++) {
156 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j])) {
157 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j]))
158 klass->sinkpad_portnums[sinkcount++] = j;
160 klass->srcpad_portnums[srccount++] = j;
164 klass->descriptor = desc;
168 gst_ladspa_class_init (GstLADSPAClass *klass)
170 GObjectClass *gobject_class;
171 GstElementClass *gstelement_class;
172 LADSPA_Descriptor *desc;
173 gint i,current_portnum,controlcount;
175 gint argtype,argperms;
176 GParamSpec *paramspec = NULL;
177 gchar *argname, *tempstr, *paren;
179 gobject_class = (GObjectClass*)klass;
180 gstelement_class = (GstElementClass*)klass;
182 gobject_class->set_property = gst_ladspa_set_property;
183 gobject_class->get_property = gst_ladspa_get_property;
185 gstelement_class->change_state = gst_ladspa_change_state;
187 /* look up and store the ladspa descriptor */
188 desc = g_hash_table_lookup(ladspa_descriptors,
189 GINT_TO_POINTER(G_TYPE_FROM_CLASS(klass)));
191 desc = g_hash_table_lookup(ladspa_descriptors, GINT_TO_POINTER(0));
194 klass->numcontrols = 0;
196 /* walk through the ports, count the input, output and control ports */
197 for (i=0; i<desc->PortCount; i++) {
198 if (!LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
199 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
200 klass->numcontrols++;
203 DEBUG ("ladspa element class: init %s with %d sink, %d src, %d control\n",
204 g_type_name (G_TYPE_FROM_CLASS (klass)),
205 klass->numsinkpads, klass->numsrcpads, klass->numcontrols);
207 klass->control_portnums = g_new0(gint,klass->numcontrols);
210 /* walk through the ports, note the portnums for control params */
211 for (i=0; i<desc->PortCount; i++) {
212 if (!LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
213 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
214 klass->control_portnums[controlcount++] = i;
217 /* now build the control info from the control ports */
218 klass->control_info = g_new0(ladspa_control_info,klass->numcontrols);
220 for (i=0;i<klass->numcontrols;i++) {
221 current_portnum = klass->control_portnums[i];
223 /* short name for hint descriptor */
224 hintdesc = desc->PortRangeHints[current_portnum].HintDescriptor;
226 /* get the various bits */
227 if (LADSPA_IS_HINT_TOGGLED(hintdesc))
228 klass->control_info[i].toggled = TRUE;
229 if (LADSPA_IS_HINT_LOGARITHMIC(hintdesc))
230 klass->control_info[i].logarithmic = TRUE;
231 if (LADSPA_IS_HINT_INTEGER(hintdesc))
232 klass->control_info[i].integer = TRUE;
234 /* figure out the argument details */
235 if (klass->control_info[i].toggled) argtype = G_TYPE_BOOLEAN;
236 else if (klass->control_info[i].integer) argtype = G_TYPE_INT;
237 else argtype = G_TYPE_FLOAT;
239 /* grab the bounds */
240 if (LADSPA_IS_HINT_BOUNDED_BELOW(hintdesc)) {
241 klass->control_info[i].lower = TRUE;
242 klass->control_info[i].lowerbound =
243 desc->PortRangeHints[current_portnum].LowerBound;
245 if (argtype==G_TYPE_INT) klass->control_info[i].lowerbound = (gfloat)G_MININT;
246 if (argtype==G_TYPE_FLOAT) klass->control_info[i].lowerbound = -G_MAXFLOAT;
249 if (LADSPA_IS_HINT_BOUNDED_ABOVE(hintdesc)) {
250 klass->control_info[i].upper = TRUE;
251 klass->control_info[i].upperbound =
252 desc->PortRangeHints[current_portnum].UpperBound;
253 if (LADSPA_IS_HINT_SAMPLE_RATE(hintdesc)) {
254 klass->control_info[i].samplerate = TRUE;
255 klass->control_info[i].upperbound *= 44100; /* FIXME? */
258 if (argtype==G_TYPE_INT) klass->control_info[i].upperbound = (gfloat)G_MAXINT;
259 if (argtype==G_TYPE_FLOAT) klass->control_info[i].upperbound = G_MAXFLOAT;
262 /* use the lowerbound as the default value */
263 klass->control_info[i].def = klass->control_info[i].lowerbound;
265 #ifdef LADSPA_IS_HINT_HAS_DEFAULT
266 /* figure out the defaults */
267 if (LADSPA_IS_HINT_HAS_DEFAULT (hintdesc)) {
268 if (LADSPA_IS_HINT_DEFAULT_MINIMUM (hintdesc))
269 klass->control_info[i].def = klass->control_info[i].lowerbound;
270 else if (LADSPA_IS_HINT_DEFAULT_LOW (hintdesc))
271 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
272 klass->control_info[i].def = exp (0.75*log(klass->control_info[i].lowerbound) +
273 0.25*log(klass->control_info[i].upperbound));
275 klass->control_info[i].def = (0.75*klass->control_info[i].lowerbound +
276 0.25*klass->control_info[i].upperbound);
277 else if (LADSPA_IS_HINT_DEFAULT_MIDDLE (hintdesc))
278 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
279 klass->control_info[i].def = exp (0.5*log(klass->control_info[i].lowerbound) +
280 0.5*log(klass->control_info[i].upperbound));
282 klass->control_info[i].def = (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 = exp (0.25*log(klass->control_info[i].lowerbound) +
287 0.75*log(klass->control_info[i].upperbound));
289 klass->control_info[i].def = (0.25*klass->control_info[i].lowerbound +
290 0.75*klass->control_info[i].upperbound);
291 else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM (hintdesc))
292 klass->control_info[i].def = klass->control_info[i].upperbound;
293 else if (LADSPA_IS_HINT_DEFAULT_0 (hintdesc))
294 klass->control_info[i].def = 0.0;
295 else if (LADSPA_IS_HINT_DEFAULT_1 (hintdesc))
296 klass->control_info[i].def = 1.0;
297 else if (LADSPA_IS_HINT_DEFAULT_100 (hintdesc))
298 klass->control_info[i].def = 100.0;
299 else if (LADSPA_IS_HINT_DEFAULT_440 (hintdesc))
300 klass->control_info[i].def = 440.0;
302 #endif /* LADSPA_IS_HINT_HAS_DEFAULT */
304 klass->control_info[i].def = CLAMP(klass->control_info[i].def,
305 klass->control_info[i].lowerbound,
306 klass->control_info[i].upperbound);
308 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[current_portnum])) {
309 argperms = G_PARAM_READWRITE;
310 klass->control_info[i].writable = TRUE;
312 argperms = G_PARAM_READABLE;
313 klass->control_info[i].writable = FALSE;
316 klass->control_info[i].name = g_strdup(desc->PortNames[current_portnum]);
317 argname = g_strdup(klass->control_info[i].name);
318 /* find out if there is a (unitname) at the end of the argname and get rid
320 paren = g_strrstr (argname, " (");
324 /* this is the same thing that param_spec_* will do */
325 g_strcanon (argname, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
326 /* satisfy glib2 (argname[0] must be [A-Za-z]) */
327 if (!((argname[0] >= 'a' && argname[0] <= 'z') || (argname[0] >= 'A' && argname[0] <= 'Z'))) {
329 argname = g_strconcat("param-", argname, NULL);
333 /* check for duplicate property names */
334 if (g_object_class_find_property(G_OBJECT_CLASS(klass), argname) != NULL){
336 gchar *numargname = g_strdup_printf("%s_%d",argname,numarg++);
337 while (g_object_class_find_property(G_OBJECT_CLASS(klass), numargname) != NULL){
339 numargname = g_strdup_printf("%s_%d",argname,numarg++);
341 argname = numargname;
344 klass->control_info[i].param_name = argname;
346 DEBUG ("adding arg %s from %s", argname, klass->control_info[i].name);
348 if (argtype==G_TYPE_BOOLEAN){
349 paramspec = g_param_spec_boolean(argname,argname,argname, FALSE, argperms);
350 } else if (argtype==G_TYPE_INT){
351 paramspec = g_param_spec_int(argname,argname,argname,
352 (gint)klass->control_info[i].lowerbound,
353 (gint)klass->control_info[i].upperbound,
354 (gint)klass->control_info[i].def, argperms);
355 } else if (klass->control_info[i].samplerate){
356 paramspec = g_param_spec_float(argname,argname,argname,
360 paramspec = g_param_spec_float(argname,argname,argname,
361 klass->control_info[i].lowerbound, klass->control_info[i].upperbound,
362 klass->control_info[i].def, argperms);
365 /* properties have an offset of 1 */
366 g_object_class_install_property(G_OBJECT_CLASS(klass), i+1, paramspec);
371 gst_ladspa_init (GstLADSPA *ladspa)
373 GstLADSPAClass *oclass;
374 ladspa_control_info cinfo;
376 LADSPA_Descriptor *desc;
377 gint i,sinkcount,srccount;
379 oclass = (GstLADSPAClass*)G_OBJECT_GET_CLASS (ladspa);
380 desc = oclass->descriptor;
381 ladspa->descriptor = oclass->descriptor;
383 /* allocate the various arrays */
384 ladspa->srcpads = g_new0(GstPad*,oclass->numsrcpads);
385 ladspa->sinkpads = g_new0(GstPad*,oclass->numsinkpads);
386 ladspa->controls = g_new(gfloat,oclass->numcontrols);
387 ladspa->dpman = gst_dpman_new ("ladspa_dpman", GST_ELEMENT(ladspa));
392 for (l=GST_ELEMENT_CLASS (oclass)->padtemplates; l; l=l->next) {
393 GstPad *pad = gst_pad_new_from_template (GST_PAD_TEMPLATE (l->data),
394 GST_PAD_TEMPLATE_NAME_TEMPLATE (l->data));
395 gst_pad_set_link_function (pad, gst_ladspa_link);
396 gst_element_add_pad ((GstElement*)ladspa, pad);
398 if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK)
399 ladspa->sinkpads[sinkcount++] = pad;
401 ladspa->srcpads[srccount++] = pad;
405 for (i=0; i<oclass->numcontrols; i++) {
406 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
407 cinfo = oclass->control_info[i];
408 ladspa->controls[i]=cinfo.def;
411 gst_dpman_add_required_dparam_callback (
413 g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
414 0, 1, (gint)(ladspa->controls[i]), G_PARAM_READWRITE),
415 "int", gst_ladspa_update_int, &(ladspa->controls[i])
418 else if (cinfo.integer){
419 gst_dpman_add_required_dparam_callback (
421 g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
422 (gint)cinfo.lowerbound, (gint)cinfo.upperbound,
423 (gint)ladspa->controls[i], G_PARAM_READWRITE),
424 "int", gst_ladspa_update_int, &(ladspa->controls[i])
427 else if (cinfo.samplerate){
428 gst_dpman_add_required_dparam_direct (
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])
437 gst_dpman_add_required_dparam_direct (
439 g_param_spec_float(cinfo.param_name, cinfo.name, cinfo.name,
440 cinfo.lowerbound, cinfo.upperbound,
441 ladspa->controls[i], G_PARAM_READWRITE),
442 "float", &(ladspa->controls[i])
448 /* nonzero default needed to instantiate() some plugins */
449 ladspa->samplerate = 44100;
451 ladspa->buffer_frames = 0; /* should be set with caps */
452 ladspa->activated = FALSE;
453 ladspa->bufpool = NULL;
454 ladspa->inplace_broken = LADSPA_IS_INPLACE_BROKEN(ladspa->descriptor->Properties);
456 if (sinkcount==0 && srccount == 1) {
457 /* get mode (no sink pads) */
458 DEBUG_OBJ (ladspa, "mono get mode with 1 src pad");
460 gst_pad_set_get_function (ladspa->srcpads[0], gst_ladspa_get);
461 } else if (sinkcount==1){
462 /* with one sink we can use the chain function */
463 DEBUG_OBJ (ladspa, "chain mode");
465 gst_pad_set_chain_function (ladspa->sinkpads[0], gst_ladspa_chain);
466 } else if (sinkcount > 1){
467 /* more than one sink pad needs loop mode */
468 DEBUG_OBJ (ladspa, "loop mode with %d sink pads and %d src pads", sinkcount, srccount);
470 gst_element_set_loop_function (GST_ELEMENT (ladspa), gst_ladspa_loop);
471 } else if (sinkcount==0 && srccount == 0) {
472 /* for example, a plugin with only control inputs and output -- just ignore
475 g_warning ("%d sink pads, %d src pads not yet supported", sinkcount, srccount);
478 gst_ladspa_instantiate (ladspa);
482 gst_ladspa_update_int(const GValue *value, gpointer data)
484 gfloat *target = (gfloat*) data;
485 *target = (gfloat)g_value_get_int(value);
488 static GstPadLinkReturn
489 gst_ladspa_link (GstPad *pad, GstCaps *caps)
491 GstElement *element = (GstElement*)GST_PAD_PARENT (pad);
492 GstLADSPA *ladspa = (GstLADSPA*)element;
493 const GList *l = NULL;
496 if (GST_CAPS_IS_FIXED (caps)) {
497 /* if this fails in some other plugin, the graph is left in an inconsistent
499 for (l=gst_element_get_pad_list (element); l; l=l->next)
500 if (pad != (GstPad*)l->data)
501 if (gst_pad_try_set_caps ((GstPad*)l->data, caps) <= 0)
502 return GST_PAD_LINK_REFUSED;
504 /* we assume that the ladspa plugin can handle any sample rate, so this
505 check gets put last */
506 gst_caps_get_int (caps, "rate", &rate);
507 /* have to instantiate ladspa plugin when samplerate changes (groan) */
508 if (ladspa->samplerate != rate) {
509 ladspa->samplerate = rate;
510 if (! gst_ladspa_instantiate(ladspa))
511 return GST_PAD_LINK_REFUSED;
514 gst_caps_get_int (caps, "buffer-frames", &ladspa->buffer_frames);
517 gst_buffer_pool_unref (ladspa->bufpool);
518 ladspa->bufpool = gst_buffer_pool_get_default (ladspa->buffer_frames * sizeof(gfloat),
521 return GST_PAD_LINK_OK;
524 return GST_PAD_LINK_DELAYED;
528 gst_ladspa_force_src_caps(GstLADSPA *ladspa, GstPad *pad)
530 if (!ladspa->buffer_frames) {
531 ladspa->buffer_frames = 256; /* 5 ms at 44100 kHz (just a default...) */
532 g_return_if_fail (ladspa->bufpool == NULL);
534 gst_buffer_pool_get_default (ladspa->buffer_frames * sizeof(gfloat), 3);
537 DEBUG_OBJ (ladspa, "forcing caps with rate=%d, buffer-frames=%d",
538 ladspa->samplerate, ladspa->buffer_frames);
540 gst_pad_try_set_caps (pad,
545 "width", GST_PROPS_INT (32),
546 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
547 "rate", GST_PROPS_INT (ladspa->samplerate),
548 "buffer-frames", GST_PROPS_INT (ladspa->buffer_frames),
549 "channels", GST_PROPS_INT (1),
554 gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
556 GstLADSPA *ladspa = (GstLADSPA*)object;
557 GstLADSPAClass *oclass;
558 ladspa_control_info *control_info;
560 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
562 /* remember, properties have an offset of 1 */
565 /* verify it exists */
566 g_return_if_fail (prop_id < oclass->numcontrols);
568 control_info = &(oclass->control_info[prop_id]);
569 g_return_if_fail (control_info->name != NULL);
571 /* check to see if it's writable */
572 g_return_if_fail (control_info->writable);
574 /* now see what type it is */
575 if (control_info->toggled)
576 ladspa->controls[prop_id] = g_value_get_boolean (value) ? 1.f : 0.f;
577 else if (control_info->integer)
578 ladspa->controls[prop_id] = g_value_get_int (value);
580 ladspa->controls[prop_id] = g_value_get_float (value);
582 DEBUG_OBJ (object, "set arg %s to %f", control_info->name, ladspa->controls[prop_id]);
586 gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
588 GstLADSPA *ladspa = (GstLADSPA*)object;
589 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
590 ladspa_control_info *control_info;
592 /* remember, properties have an offset of 1 */
595 /* verify it exists */
596 g_return_if_fail (prop_id < oclass->numcontrols);
598 control_info = &(oclass->control_info[prop_id]);
599 g_return_if_fail (control_info->name != NULL);
601 /* now see what type it is */
602 if (control_info->toggled)
603 g_value_set_boolean (value, ladspa->controls[prop_id] == 1.0);
604 else if (control_info->integer)
605 g_value_set_int (value, (gint)ladspa->controls[prop_id]);
607 g_value_set_float (value, ladspa->controls[prop_id]);
609 DEBUG_OBJ (object, "got arg %s as %f", control_info->name, ladspa->controls[prop_id]);
613 gst_ladspa_instantiate (GstLADSPA *ladspa)
615 LADSPA_Descriptor *desc;
617 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
618 gboolean was_activated;
620 desc = ladspa->descriptor;
622 /* check for old handle */
623 was_activated = ladspa->activated;
624 if (ladspa->handle != NULL){
625 gst_ladspa_deactivate(ladspa);
626 desc->cleanup(ladspa->handle);
629 /* instantiate the plugin */
630 DEBUG_OBJ (ladspa, "instantiating the plugin at %d Hz", ladspa->samplerate);
632 ladspa->handle = desc->instantiate(desc,ladspa->samplerate);
633 g_return_val_if_fail (ladspa->handle != NULL, FALSE);
635 /* connect the control ports */
636 for (i=0;i<oclass->numcontrols;i++)
637 desc->connect_port(ladspa->handle,
638 oclass->control_portnums[i],
639 &(ladspa->controls[i]));
641 /* reactivate if it was activated before the reinstantiation */
643 gst_ladspa_activate(ladspa);
648 static GstElementStateReturn
649 gst_ladspa_change_state (GstElement *element)
651 LADSPA_Descriptor *desc;
652 GstLADSPA *ladspa = (GstLADSPA*)element;
653 desc = ladspa->descriptor;
655 switch (GST_STATE_TRANSITION (element)) {
656 case GST_STATE_NULL_TO_READY:
657 gst_ladspa_activate(ladspa);
659 case GST_STATE_READY_TO_NULL:
660 gst_ladspa_deactivate(ladspa);
666 if (GST_ELEMENT_CLASS (parent_class)->change_state)
667 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
669 return GST_STATE_SUCCESS;
673 gst_ladspa_activate (GstLADSPA *ladspa)
675 LADSPA_Descriptor *desc;
676 desc = ladspa->descriptor;
678 if (ladspa->activated)
679 gst_ladspa_deactivate(ladspa);
681 DEBUG_OBJ (ladspa, "activating");
683 /* activate the plugin (function might be null) */
684 if (desc->activate != NULL)
685 desc->activate(ladspa->handle);
687 ladspa->activated = TRUE;
691 gst_ladspa_deactivate (GstLADSPA *ladspa)
693 LADSPA_Descriptor *desc;
694 desc = ladspa->descriptor;
696 DEBUG_OBJ (ladspa, "deactivating");
698 /* deactivate the plugin (function might be null) */
699 if (ladspa->activated && (desc->deactivate != NULL))
700 desc->deactivate(ladspa->handle);
702 ladspa->activated = FALSE;
706 gst_ladspa_loop (GstElement *element)
708 guint i, j, numsrcpads, numsinkpads;
709 guint num_processed, num_to_process;
711 LADSPA_Data **data_in, **data_out;
712 GstBuffer **buffers_in, **buffers_out;
714 GstLADSPA *ladspa = (GstLADSPA *)element;
715 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
716 LADSPA_Descriptor *desc = ladspa->descriptor;
718 numsinkpads = oclass->numsinkpads;
719 numsrcpads = oclass->numsrcpads;
721 /* fixme: these mallocs need to die */
722 data_in = g_new0(LADSPA_Data*, numsinkpads);
723 data_out = g_new0(LADSPA_Data*, numsrcpads);
724 buffers_in = g_new0(GstBuffer*, numsinkpads);
725 buffers_out = g_new0(GstBuffer*, numsrcpads);
729 /* first get all the necessary data from the input ports */
730 for (i=0 ; i<numsinkpads ; i++){
732 buffers_in[i] = GST_BUFFER (gst_pad_pull (ladspa->sinkpads[i]));
734 if (GST_IS_EVENT (buffers_in[i])) {
735 /* push it out on all pads */
736 gst_data_ref_by_count ((GstData*)buffers_in[i], numsrcpads);
737 for (j=0; j<numsrcpads; j++)
738 gst_pad_push (ladspa->srcpads[j], GST_DATA (buffers_in[i]));
739 if (GST_EVENT_TYPE (buffers_in[i]) == GST_EVENT_EOS) {
741 gst_element_set_eos (element);
748 if (largest_buffer < 0)
749 largest_buffer = GST_BUFFER_SIZE (buffers_in[i])/sizeof(gfloat);
751 largest_buffer = MIN (GST_BUFFER_SIZE (buffers_in[i])/sizeof(gfloat), largest_buffer);
752 data_in[i] = (LADSPA_Data *) GST_BUFFER_DATA(buffers_in[i]);
753 GST_BUFFER_TIMESTAMP(buffers_in[i]) = ladspa->timestamp;
756 if (!ladspa->bufpool) {
757 gst_element_error (element, "Caps were never set, bailing...");
762 if (!ladspa->inplace_broken) {
763 for (; i<numsrcpads && i<numsinkpads; i++) {
764 /* reuse input buffers */
765 buffers_out[i] = buffers_in[i];
766 data_out[i] = data_in[i];
769 for (; i<numsrcpads; i++) {
770 /* we have to make new buffers -- at least we're taking them from a pool */
771 buffers_out[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
772 GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
773 data_out[i] = (LADSPA_Data*)GST_BUFFER_DATA (buffers_out[i]);
776 GST_DPMAN_PREPROCESS(ladspa->dpman, largest_buffer, ladspa->timestamp);
779 /* split up processing of the buffer into chunks so that dparams can
780 * be updated when required.
781 * In many cases the buffer will be processed in one chunk anyway.
783 while (GST_DPMAN_PROCESS (ladspa->dpman, num_processed)) {
784 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
786 for (i=0 ; i<numsinkpads ; i++)
787 desc->connect_port (ladspa->handle, oclass->sinkpad_portnums[i], data_in[i]);
788 for (i=0 ; i<numsrcpads ; i++)
789 desc->connect_port (ladspa->handle, oclass->srcpad_portnums[i], data_out[i]);
791 desc->run(ladspa->handle, num_to_process);
793 for (i=0 ; i<numsinkpads ; i++)
794 data_in[i] += num_to_process;
795 for (i=0 ; i<numsrcpads ; i++)
796 data_out[i] += num_to_process;
798 num_processed += num_to_process;
801 for (i=0 ; i<numsinkpads ; i++) {
802 if (i >= numsrcpads || buffers_out[i] != buffers_in[i])
803 gst_buffer_unref(buffers_in[i]);
805 buffers_in[i] = NULL;
807 for (i=0 ; i<numsrcpads ; i++) {
808 DEBUG_OBJ (ladspa, "pushing buffer (%p) on src pad %d", buffers_out[i], i);
809 gst_pad_push (ladspa->srcpads[i], GST_DATA (buffers_out[i]));
812 buffers_out[i] = NULL;
815 ladspa->timestamp += ladspa->buffer_frames * GST_SECOND / ladspa->samplerate;
817 /* FIXME: move these mallocs and frees to the state-change handler */
819 g_free (buffers_out);
826 gst_ladspa_chain (GstPad *pad, GstData *_data)
828 GstBuffer *buffer_in = GST_BUFFER (_data);
829 LADSPA_Descriptor *desc;
830 LADSPA_Data *data_in, **data_out = NULL;
831 GstBuffer **buffers_out = NULL;
832 unsigned long num_samples;
833 guint num_to_process, num_processed, i, numsrcpads;
835 GstLADSPAClass *oclass;
837 ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
838 oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
839 data_in = (LADSPA_Data *) GST_BUFFER_DATA(buffer_in);
840 num_samples = GST_BUFFER_SIZE(buffer_in) / sizeof(gfloat);
841 numsrcpads = oclass->numsrcpads;
842 desc = ladspa->descriptor;
844 /* we shouldn't get events here... */
845 g_return_if_fail (GST_IS_BUFFER (buffer_in));
847 if (!ladspa->bufpool) {
848 gst_element_error ((GstElement*)ladspa, "Caps were never set, bailing...");
852 /* FIXME: this function shouldn't need to malloc() anything */
853 if (numsrcpads > 0) {
854 buffers_out = g_new(GstBuffer*, numsrcpads);
855 data_out = g_new(LADSPA_Data*, numsrcpads);
859 if (!ladspa->inplace_broken && numsrcpads) {
860 /* reuse the first (chained) buffer */
861 buffers_out[i] = buffer_in;
862 DEBUG ("reuse: %d", GST_BUFFER_SIZE (buffer_in));
863 data_out[i] = data_in;
866 for (; i<numsrcpads; i++) {
867 /* we have to make new buffers -- at least we're taking them from a pool */
868 buffers_out[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
869 /* the size of the buffer returned from the pool is the maximum size; this
870 chained buffer might be smaller */
871 GST_BUFFER_SIZE (buffers_out[i]) = GST_BUFFER_SIZE (buffer_in);
872 DEBUG ("new %d", GST_BUFFER_SIZE (buffer_in));
873 GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
874 data_out[i] = (LADSPA_Data*)GST_BUFFER_DATA (buffers_out[i]);
877 GST_DPMAN_PREPROCESS(ladspa->dpman, num_samples, GST_BUFFER_TIMESTAMP(buffer_in));
880 /* split up processing of the buffer into chunks so that dparams can
881 * be updated when required.
882 * In many cases the buffer will be processed in one chunk anyway.
884 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
885 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
887 desc->connect_port(ladspa->handle,oclass->sinkpad_portnums[0],data_in);
888 for (i=0 ; i<numsrcpads ; i++)
889 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[i],data_out[i]);
891 desc->run(ladspa->handle, num_to_process);
893 data_in += num_to_process;
894 for (i=0 ; i<numsrcpads ; i++)
895 data_out[i] += num_to_process;
897 num_processed += num_to_process;
900 if (!numsrcpads || buffers_out[0] != buffer_in)
901 gst_buffer_unref(buffer_in);
904 for (i=0; i<numsrcpads; i++) {
905 DEBUG_OBJ (ladspa, "pushing buffer (%p, length %u bytes) on src pad %d",
906 buffers_out[i], GST_BUFFER_SIZE (buffers_out[i]), i);
907 gst_pad_push (ladspa->srcpads[i], GST_DATA (buffers_out[i]));
916 gst_ladspa_get(GstPad *pad)
919 GstLADSPAClass *oclass;
922 LADSPA_Descriptor *desc;
923 guint num_to_process, num_processed;
925 ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
926 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
927 desc = ladspa->descriptor;
929 if (!ladspa->bufpool) {
930 /* capsnego hasn't happened... */
931 gst_ladspa_force_src_caps(ladspa, ladspa->srcpads[0]);
934 buf = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
935 GST_BUFFER_TIMESTAMP(buf) = ladspa->timestamp;
936 data = (LADSPA_Data *) GST_BUFFER_DATA(buf);
938 GST_DPMAN_PREPROCESS(ladspa->dpman, ladspa->buffer_frames, ladspa->timestamp);
941 /* split up processing of the buffer into chunks so that dparams can
942 * be updated when required.
943 * In many cases the buffer will be processed in one chunk anyway.
945 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
946 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
948 /* update timestamp */
949 ladspa->timestamp += num_to_process * GST_SECOND / ladspa->samplerate;
951 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);
953 desc->run(ladspa->handle, num_to_process);
955 data += num_to_process;
956 num_processed = num_to_process;
959 return GST_DATA (buf);
963 ladspa_describe_plugin(const char *pcFullFilename,
964 void *pvPluginHandle,
965 LADSPA_Descriptor_Function pfDescriptorFunction)
967 const LADSPA_Descriptor *desc;
969 GTypeInfo typeinfo = {
970 sizeof(GstLADSPAClass),
971 (GBaseInitFunc)gst_ladspa_base_init,
973 (GClassInitFunc)gst_ladspa_class_init,
978 (GInstanceInitFunc)gst_ladspa_init,
982 /* walk through all the plugins in this pluginlibrary */
984 while ((desc = pfDescriptorFunction(i++))) {
987 /* construct the type */
988 type_name = g_strdup_printf("ladspa-%s",desc->Label);
989 g_strcanon (type_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-+", '-');
990 /* if it's already registered, drop it */
991 if (g_type_from_name(type_name)) {
996 /* base-init temp alloc */
997 g_hash_table_insert(ladspa_descriptors,
1001 /* create the type now */
1002 type = g_type_register_static(GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
1003 if (!gst_element_register (ladspa_plugin, type_name, GST_RANK_NONE, type))
1006 /* add this plugin to the hash */
1007 g_hash_table_insert(ladspa_descriptors,
1008 GINT_TO_POINTER(type),
1012 g_hash_table_remove (ladspa_descriptors, GINT_TO_POINTER (0));
1016 plugin_init (GstPlugin *plugin)
1018 GST_DEBUG_CATEGORY_INIT (ladspa_debug, "ladspa",
1019 GST_DEBUG_FG_GREEN | GST_DEBUG_BG_BLACK | GST_DEBUG_BOLD,
1022 ladspa_descriptors = g_hash_table_new(NULL,NULL);
1023 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
1025 ladspa_plugin = plugin;
1027 LADSPAPluginSearch(ladspa_describe_plugin);
1029 /* initialize dparam support library */
1030 gst_control_init(NULL,NULL);
1039 "All LADSPA plugins",
1043 "(c) 2003 The LADSPA team",
1045 "http://www.ladspa.org/"