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 /* takes ownership of the name */
41 static GstPadTemplate*
42 ladspa_sink_factory (gchar *name)
44 return GST_PAD_TEMPLATE_NEW (
51 GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS
56 /* takes ownership of the name */
57 static GstPadTemplate*
58 ladspa_src_factory (gchar *name)
60 return GST_PAD_TEMPLATE_NEW (
67 GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS
72 static void gst_ladspa_class_init (GstLADSPAClass *klass);
73 static void gst_ladspa_base_init (GstLADSPAClass *klass);
74 static void gst_ladspa_init (GstLADSPA *ladspa);
76 static void gst_ladspa_update_int (const GValue *value, gpointer data);
77 static GstPadLinkReturn gst_ladspa_link (GstPad *pad, GstCaps *caps);
78 static void gst_ladspa_force_src_caps (GstLADSPA *ladspa, GstPad *pad);
80 static void gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
81 static void gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
83 static gboolean gst_ladspa_instantiate (GstLADSPA *ladspa);
84 static void gst_ladspa_activate (GstLADSPA *ladspa);
85 static void gst_ladspa_deactivate (GstLADSPA *ladspa);
87 static GstElementStateReturn gst_ladspa_change_state (GstElement *element);
88 static void gst_ladspa_loop (GstElement *element);
89 static void gst_ladspa_chain (GstPad *pad,GstData *_data);
90 static GstData * gst_ladspa_get (GstPad *pad);
92 static GstElementClass *parent_class = NULL;
94 static GstPlugin *ladspa_plugin;
95 static GHashTable *ladspa_descriptors;
104 GST_DEBUG_CATEGORY_STATIC (ladspa_debug);
106 GST_CAT_LEVEL_LOG (ladspa_debug, GST_LEVEL_DEBUG, NULL, __VA_ARGS__)
107 #define DEBUG_OBJ(obj,...) \
108 GST_CAT_LEVEL_LOG (ladspa_debug, GST_LEVEL_DEBUG, obj, __VA_ARGS__)
111 gst_ladspa_base_init (GstLADSPAClass *klass)
113 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
114 GstPadTemplate *templ;
115 GstElementDetails *details;
116 LADSPA_Descriptor *desc;
117 gint j, sinkcount,srccount;
119 desc = g_hash_table_lookup(ladspa_descriptors,
120 GINT_TO_POINTER(G_TYPE_FROM_CLASS(klass)));
122 desc = g_hash_table_lookup(ladspa_descriptors, GINT_TO_POINTER(0));
125 /* construct the element details struct */
126 details = g_new0(GstElementDetails,1);
127 details->longname = g_strdup(desc->Name);
128 details->klass = "Filter/Effect/Audio/LADSPA";
129 details->description = details->longname;
130 details->author = g_strdup(desc->Maker);
131 gst_element_class_set_details (element_class, details);
134 klass->numports = desc->PortCount;
135 klass->numsinkpads = 0;
136 klass->numsrcpads = 0;
137 for (j=0;j<desc->PortCount;j++) {
138 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j])) {
139 gchar *name = g_strdup((gchar *)desc->PortNames[j]);
140 g_strcanon (name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
142 /* the factories take ownership of the name */
143 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j])) {
144 templ = ladspa_sink_factory (name);
145 klass->numsinkpads++;
147 templ = ladspa_src_factory (name);
151 gst_element_class_add_pad_template (element_class, templ);
155 klass->srcpad_portnums = g_new0(gint,klass->numsrcpads);
156 klass->sinkpad_portnums = g_new0(gint,klass->numsinkpads);
160 /* walk through the ports, note the portnums for srcpads, sinkpads */
161 for (j=0; j<desc->PortCount; j++) {
162 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j])) {
163 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j]))
164 klass->sinkpad_portnums[sinkcount++] = j;
166 klass->srcpad_portnums[srccount++] = j;
170 klass->descriptor = desc;
174 gst_ladspa_class_init (GstLADSPAClass *klass)
176 GObjectClass *gobject_class;
177 GstElementClass *gstelement_class;
178 LADSPA_Descriptor *desc;
179 gint i,current_portnum,controlcount;
181 gint argtype,argperms;
182 GParamSpec *paramspec = NULL;
183 gchar *argname, *tempstr, *paren;
185 gobject_class = (GObjectClass*)klass;
186 gstelement_class = (GstElementClass*)klass;
188 gobject_class->set_property = gst_ladspa_set_property;
189 gobject_class->get_property = gst_ladspa_get_property;
191 gstelement_class->change_state = gst_ladspa_change_state;
193 /* look up and store the ladspa descriptor */
194 desc = g_hash_table_lookup(ladspa_descriptors,
195 GINT_TO_POINTER(G_TYPE_FROM_CLASS(klass)));
197 desc = g_hash_table_lookup(ladspa_descriptors, GINT_TO_POINTER(0));
200 klass->numcontrols = 0;
202 /* walk through the ports, count the input, output and control ports */
203 for (i=0; i<desc->PortCount; i++) {
204 if (!LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
205 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
206 klass->numcontrols++;
209 DEBUG ("ladspa element class: init %s with %d sink, %d src, %d control\n",
210 g_type_name (G_TYPE_FROM_CLASS (klass)),
211 klass->numsinkpads, klass->numsrcpads, klass->numcontrols);
213 klass->control_portnums = g_new0(gint,klass->numcontrols);
216 /* walk through the ports, note the portnums for control params */
217 for (i=0; i<desc->PortCount; i++) {
218 if (!LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
219 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
220 klass->control_portnums[controlcount++] = i;
223 /* now build the control info from the control ports */
224 klass->control_info = g_new0(ladspa_control_info,klass->numcontrols);
226 for (i=0;i<klass->numcontrols;i++) {
227 current_portnum = klass->control_portnums[i];
229 /* short name for hint descriptor */
230 hintdesc = desc->PortRangeHints[current_portnum].HintDescriptor;
232 /* get the various bits */
233 if (LADSPA_IS_HINT_TOGGLED(hintdesc))
234 klass->control_info[i].toggled = TRUE;
235 if (LADSPA_IS_HINT_LOGARITHMIC(hintdesc))
236 klass->control_info[i].logarithmic = TRUE;
237 if (LADSPA_IS_HINT_INTEGER(hintdesc))
238 klass->control_info[i].integer = TRUE;
240 /* figure out the argument details */
241 if (klass->control_info[i].toggled) argtype = G_TYPE_BOOLEAN;
242 else if (klass->control_info[i].integer) argtype = G_TYPE_INT;
243 else argtype = G_TYPE_FLOAT;
245 /* grab the bounds */
246 if (LADSPA_IS_HINT_BOUNDED_BELOW(hintdesc)) {
247 klass->control_info[i].lower = TRUE;
248 klass->control_info[i].lowerbound =
249 desc->PortRangeHints[current_portnum].LowerBound;
251 if (argtype==G_TYPE_INT) klass->control_info[i].lowerbound = (gfloat)G_MININT;
252 if (argtype==G_TYPE_FLOAT) klass->control_info[i].lowerbound = -G_MAXFLOAT;
255 if (LADSPA_IS_HINT_BOUNDED_ABOVE(hintdesc)) {
256 klass->control_info[i].upper = TRUE;
257 klass->control_info[i].upperbound =
258 desc->PortRangeHints[current_portnum].UpperBound;
259 if (LADSPA_IS_HINT_SAMPLE_RATE(hintdesc)) {
260 klass->control_info[i].samplerate = TRUE;
261 klass->control_info[i].upperbound *= 44100; /* FIXME? */
264 if (argtype==G_TYPE_INT) klass->control_info[i].upperbound = (gfloat)G_MAXINT;
265 if (argtype==G_TYPE_FLOAT) klass->control_info[i].upperbound = G_MAXFLOAT;
268 /* use the lowerbound as the default value */
269 klass->control_info[i].def = klass->control_info[i].lowerbound;
271 #ifdef LADSPA_IS_HINT_HAS_DEFAULT
272 /* figure out the defaults */
273 if (LADSPA_IS_HINT_HAS_DEFAULT (hintdesc)) {
274 if (LADSPA_IS_HINT_DEFAULT_MINIMUM (hintdesc))
275 klass->control_info[i].def = klass->control_info[i].lowerbound;
276 else if (LADSPA_IS_HINT_DEFAULT_LOW (hintdesc))
277 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
278 klass->control_info[i].def = exp (0.75*log(klass->control_info[i].lowerbound) +
279 0.25*log(klass->control_info[i].upperbound));
281 klass->control_info[i].def = (0.75*klass->control_info[i].lowerbound +
282 0.25*klass->control_info[i].upperbound);
283 else if (LADSPA_IS_HINT_DEFAULT_MIDDLE (hintdesc))
284 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
285 klass->control_info[i].def = exp (0.5*log(klass->control_info[i].lowerbound) +
286 0.5*log(klass->control_info[i].upperbound));
288 klass->control_info[i].def = (0.5*klass->control_info[i].lowerbound +
289 0.5*klass->control_info[i].upperbound);
290 else if (LADSPA_IS_HINT_DEFAULT_HIGH (hintdesc))
291 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
292 klass->control_info[i].def = exp (0.25*log(klass->control_info[i].lowerbound) +
293 0.75*log(klass->control_info[i].upperbound));
295 klass->control_info[i].def = (0.25*klass->control_info[i].lowerbound +
296 0.75*klass->control_info[i].upperbound);
297 else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM (hintdesc))
298 klass->control_info[i].def = klass->control_info[i].upperbound;
299 else if (LADSPA_IS_HINT_DEFAULT_0 (hintdesc))
300 klass->control_info[i].def = 0.0;
301 else if (LADSPA_IS_HINT_DEFAULT_1 (hintdesc))
302 klass->control_info[i].def = 1.0;
303 else if (LADSPA_IS_HINT_DEFAULT_100 (hintdesc))
304 klass->control_info[i].def = 100.0;
305 else if (LADSPA_IS_HINT_DEFAULT_440 (hintdesc))
306 klass->control_info[i].def = 440.0;
308 #endif /* LADSPA_IS_HINT_HAS_DEFAULT */
310 klass->control_info[i].def = CLAMP(klass->control_info[i].def,
311 klass->control_info[i].lowerbound,
312 klass->control_info[i].upperbound);
314 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[current_portnum])) {
315 argperms = G_PARAM_READWRITE;
316 klass->control_info[i].writable = TRUE;
318 argperms = G_PARAM_READABLE;
319 klass->control_info[i].writable = FALSE;
322 klass->control_info[i].name = g_strdup(desc->PortNames[current_portnum]);
323 argname = g_strdup(klass->control_info[i].name);
324 /* find out if there is a (unitname) at the end of the argname and get rid
326 paren = g_strrstr (argname, " (");
330 /* this is the same thing that param_spec_* will do */
331 g_strcanon (argname, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
332 /* satisfy glib2 (argname[0] must be [A-Za-z]) */
333 if (!((argname[0] >= 'a' && argname[0] <= 'z') || (argname[0] >= 'A' && argname[0] <= 'Z'))) {
335 argname = g_strconcat("param-", argname, NULL);
339 /* check for duplicate property names */
340 if (g_object_class_find_property(G_OBJECT_CLASS(klass), argname) != NULL){
342 gchar *numargname = g_strdup_printf("%s_%d",argname,numarg++);
343 while (g_object_class_find_property(G_OBJECT_CLASS(klass), numargname) != NULL){
345 numargname = g_strdup_printf("%s_%d",argname,numarg++);
347 argname = numargname;
350 klass->control_info[i].param_name = argname;
352 DEBUG ("adding arg %s from %s", argname, klass->control_info[i].name);
354 if (argtype==G_TYPE_BOOLEAN){
355 paramspec = g_param_spec_boolean(argname,argname,argname, FALSE, argperms);
356 } else if (argtype==G_TYPE_INT){
357 paramspec = g_param_spec_int(argname,argname,argname,
358 (gint)klass->control_info[i].lowerbound,
359 (gint)klass->control_info[i].upperbound,
360 (gint)klass->control_info[i].def, argperms);
361 } else if (klass->control_info[i].samplerate){
362 paramspec = g_param_spec_float(argname,argname,argname,
366 paramspec = g_param_spec_float(argname,argname,argname,
367 klass->control_info[i].lowerbound, klass->control_info[i].upperbound,
368 klass->control_info[i].def, argperms);
371 /* properties have an offset of 1 */
372 g_object_class_install_property(G_OBJECT_CLASS(klass), i+1, paramspec);
377 gst_ladspa_init (GstLADSPA *ladspa)
379 GstLADSPAClass *oclass;
380 ladspa_control_info cinfo;
382 LADSPA_Descriptor *desc;
383 gint i,sinkcount,srccount;
385 oclass = (GstLADSPAClass*)G_OBJECT_GET_CLASS (ladspa);
386 desc = oclass->descriptor;
387 ladspa->descriptor = oclass->descriptor;
389 /* allocate the various arrays */
390 ladspa->srcpads = g_new0(GstPad*,oclass->numsrcpads);
391 ladspa->sinkpads = g_new0(GstPad*,oclass->numsinkpads);
392 ladspa->controls = g_new(gfloat,oclass->numcontrols);
393 ladspa->dpman = gst_dpman_new ("ladspa_dpman", GST_ELEMENT(ladspa));
398 for (l=GST_ELEMENT_CLASS (oclass)->padtemplates; l; l=l->next) {
399 GstPad *pad = gst_pad_new_from_template (GST_PAD_TEMPLATE (l->data),
400 GST_PAD_TEMPLATE_NAME_TEMPLATE (l->data));
401 gst_pad_set_link_function (pad, gst_ladspa_link);
402 gst_element_add_pad ((GstElement*)ladspa, pad);
404 if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK)
405 ladspa->sinkpads[sinkcount++] = pad;
407 ladspa->srcpads[srccount++] = pad;
411 for (i=0; i<oclass->numcontrols; i++) {
412 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
413 cinfo = oclass->control_info[i];
414 ladspa->controls[i]=cinfo.def;
417 gst_dpman_add_required_dparam_callback (
419 g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
420 0, 1, (gint)(ladspa->controls[i]), G_PARAM_READWRITE),
421 "int", gst_ladspa_update_int, &(ladspa->controls[i])
424 else if (cinfo.integer){
425 gst_dpman_add_required_dparam_callback (
427 g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
428 (gint)cinfo.lowerbound, (gint)cinfo.upperbound,
429 (gint)ladspa->controls[i], G_PARAM_READWRITE),
430 "int", gst_ladspa_update_int, &(ladspa->controls[i])
433 else if (cinfo.samplerate){
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 "hertz-rate-bound", &(ladspa->controls[i])
443 gst_dpman_add_required_dparam_direct (
445 g_param_spec_float(cinfo.param_name, cinfo.name, cinfo.name,
446 cinfo.lowerbound, cinfo.upperbound,
447 ladspa->controls[i], G_PARAM_READWRITE),
448 "float", &(ladspa->controls[i])
454 /* nonzero default needed to instantiate() some plugins */
455 ladspa->samplerate = 44100;
457 ladspa->buffer_frames = 0; /* should be set with caps */
458 ladspa->activated = FALSE;
459 ladspa->bufpool = NULL;
460 ladspa->inplace_broken = LADSPA_IS_INPLACE_BROKEN(ladspa->descriptor->Properties);
462 if (sinkcount==0 && srccount == 1) {
463 /* get mode (no sink pads) */
464 DEBUG_OBJ (ladspa, "mono get mode with 1 src pad");
466 gst_pad_set_get_function (ladspa->srcpads[0], gst_ladspa_get);
467 } else if (sinkcount==1){
468 /* with one sink we can use the chain function */
469 DEBUG_OBJ (ladspa, "chain mode");
471 gst_pad_set_chain_function (ladspa->sinkpads[0], gst_ladspa_chain);
472 } else if (sinkcount > 1){
473 /* more than one sink pad needs loop mode */
474 DEBUG_OBJ (ladspa, "loop mode with %d sink pads and %d src pads", sinkcount, srccount);
476 gst_element_set_loop_function (GST_ELEMENT (ladspa), gst_ladspa_loop);
477 } else if (sinkcount==0 && srccount == 0) {
478 /* for example, a plugin with only control inputs and output -- just ignore
481 g_warning ("%d sink pads, %d src pads not yet supported", sinkcount, srccount);
484 gst_ladspa_instantiate (ladspa);
488 gst_ladspa_update_int(const GValue *value, gpointer data)
490 gfloat *target = (gfloat*) data;
491 *target = (gfloat)g_value_get_int(value);
494 static GstPadLinkReturn
495 gst_ladspa_link (GstPad *pad, GstCaps *caps)
497 GstElement *element = (GstElement*)GST_PAD_PARENT (pad);
498 GstLADSPA *ladspa = (GstLADSPA*)element;
499 const GList *l = NULL;
502 if (GST_CAPS_IS_FIXED (caps)) {
503 /* if this fails in some other plugin, the graph is left in an inconsistent
505 for (l=gst_element_get_pad_list (element); l; l=l->next)
506 if (pad != (GstPad*)l->data)
507 if (gst_pad_try_set_caps ((GstPad*)l->data, caps) <= 0)
508 return GST_PAD_LINK_REFUSED;
510 /* we assume that the ladspa plugin can handle any sample rate, so this
511 check gets put last */
512 gst_caps_get_int (caps, "rate", &rate);
513 /* have to instantiate ladspa plugin when samplerate changes (groan) */
514 if (ladspa->samplerate != rate) {
515 ladspa->samplerate = rate;
516 if (! gst_ladspa_instantiate(ladspa))
517 return GST_PAD_LINK_REFUSED;
520 gst_caps_get_int (caps, "buffer-frames", &ladspa->buffer_frames);
523 gst_buffer_pool_unref (ladspa->bufpool);
524 ladspa->bufpool = gst_buffer_pool_get_default (ladspa->buffer_frames * sizeof(gfloat),
527 return GST_PAD_LINK_OK;
530 return GST_PAD_LINK_DELAYED;
534 gst_ladspa_force_src_caps(GstLADSPA *ladspa, GstPad *pad)
536 if (!ladspa->buffer_frames) {
537 ladspa->buffer_frames = 256; /* 5 ms at 44100 kHz (just a default...) */
538 g_return_if_fail (ladspa->bufpool == NULL);
540 gst_buffer_pool_get_default (ladspa->buffer_frames * sizeof(gfloat), 3);
543 DEBUG_OBJ (ladspa, "forcing caps with rate=%d, buffer-frames=%d",
544 ladspa->samplerate, ladspa->buffer_frames);
546 gst_pad_try_set_caps (pad,
551 "width", GST_PROPS_INT (32),
552 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
553 "rate", GST_PROPS_INT (ladspa->samplerate),
554 "buffer-frames", GST_PROPS_INT (ladspa->buffer_frames),
555 "channels", GST_PROPS_INT (1),
560 gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
562 GstLADSPA *ladspa = (GstLADSPA*)object;
563 GstLADSPAClass *oclass;
564 ladspa_control_info *control_info;
566 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
568 /* remember, properties have an offset of 1 */
571 /* verify it exists */
572 g_return_if_fail (prop_id < oclass->numcontrols);
574 control_info = &(oclass->control_info[prop_id]);
575 g_return_if_fail (control_info->name != NULL);
577 /* check to see if it's writable */
578 g_return_if_fail (control_info->writable);
580 /* now see what type it is */
581 if (control_info->toggled)
582 ladspa->controls[prop_id] = g_value_get_boolean (value) ? 1.f : 0.f;
583 else if (control_info->integer)
584 ladspa->controls[prop_id] = g_value_get_int (value);
586 ladspa->controls[prop_id] = g_value_get_float (value);
588 DEBUG_OBJ (object, "set arg %s to %f", control_info->name, ladspa->controls[prop_id]);
592 gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
594 GstLADSPA *ladspa = (GstLADSPA*)object;
595 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
596 ladspa_control_info *control_info;
598 /* remember, properties have an offset of 1 */
601 /* verify it exists */
602 g_return_if_fail (prop_id < oclass->numcontrols);
604 control_info = &(oclass->control_info[prop_id]);
605 g_return_if_fail (control_info->name != NULL);
607 /* now see what type it is */
608 if (control_info->toggled)
609 g_value_set_boolean (value, ladspa->controls[prop_id] == 1.0);
610 else if (control_info->integer)
611 g_value_set_int (value, (gint)ladspa->controls[prop_id]);
613 g_value_set_float (value, ladspa->controls[prop_id]);
615 DEBUG_OBJ (object, "got arg %s as %f", control_info->name, ladspa->controls[prop_id]);
619 gst_ladspa_instantiate (GstLADSPA *ladspa)
621 LADSPA_Descriptor *desc;
623 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
624 gboolean was_activated;
626 desc = ladspa->descriptor;
628 /* check for old handle */
629 was_activated = ladspa->activated;
630 if (ladspa->handle != NULL){
631 gst_ladspa_deactivate(ladspa);
632 desc->cleanup(ladspa->handle);
635 /* instantiate the plugin */
636 DEBUG_OBJ (ladspa, "instantiating the plugin at %d Hz", ladspa->samplerate);
638 ladspa->handle = desc->instantiate(desc,ladspa->samplerate);
639 g_return_val_if_fail (ladspa->handle != NULL, FALSE);
641 /* connect the control ports */
642 for (i=0;i<oclass->numcontrols;i++)
643 desc->connect_port(ladspa->handle,
644 oclass->control_portnums[i],
645 &(ladspa->controls[i]));
647 /* reactivate if it was activated before the reinstantiation */
649 gst_ladspa_activate(ladspa);
654 static GstElementStateReturn
655 gst_ladspa_change_state (GstElement *element)
657 LADSPA_Descriptor *desc;
658 GstLADSPA *ladspa = (GstLADSPA*)element;
659 desc = ladspa->descriptor;
661 switch (GST_STATE_TRANSITION (element)) {
662 case GST_STATE_NULL_TO_READY:
663 gst_ladspa_activate(ladspa);
665 case GST_STATE_READY_TO_NULL:
666 gst_ladspa_deactivate(ladspa);
672 if (GST_ELEMENT_CLASS (parent_class)->change_state)
673 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
675 return GST_STATE_SUCCESS;
679 gst_ladspa_activate (GstLADSPA *ladspa)
681 LADSPA_Descriptor *desc;
682 desc = ladspa->descriptor;
684 if (ladspa->activated)
685 gst_ladspa_deactivate(ladspa);
687 DEBUG_OBJ (ladspa, "activating");
689 /* activate the plugin (function might be null) */
690 if (desc->activate != NULL)
691 desc->activate(ladspa->handle);
693 ladspa->activated = TRUE;
697 gst_ladspa_deactivate (GstLADSPA *ladspa)
699 LADSPA_Descriptor *desc;
700 desc = ladspa->descriptor;
702 DEBUG_OBJ (ladspa, "deactivating");
704 /* deactivate the plugin (function might be null) */
705 if (ladspa->activated && (desc->deactivate != NULL))
706 desc->deactivate(ladspa->handle);
708 ladspa->activated = FALSE;
712 gst_ladspa_loop (GstElement *element)
714 guint i, j, numsrcpads, numsinkpads;
715 guint num_processed, num_to_process;
717 LADSPA_Data **data_in, **data_out;
718 GstBuffer **buffers_in, **buffers_out;
720 GstLADSPA *ladspa = (GstLADSPA *)element;
721 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
722 LADSPA_Descriptor *desc = ladspa->descriptor;
724 numsinkpads = oclass->numsinkpads;
725 numsrcpads = oclass->numsrcpads;
727 /* fixme: these mallocs need to die */
728 data_in = g_new0(LADSPA_Data*, numsinkpads);
729 data_out = g_new0(LADSPA_Data*, numsrcpads);
730 buffers_in = g_new0(GstBuffer*, numsinkpads);
731 buffers_out = g_new0(GstBuffer*, numsrcpads);
735 /* first get all the necessary data from the input ports */
736 for (i=0 ; i<numsinkpads ; i++){
738 buffers_in[i] = GST_BUFFER (gst_pad_pull (ladspa->sinkpads[i]));
740 if (GST_IS_EVENT (buffers_in[i])) {
741 /* push it out on all pads */
742 gst_data_ref_by_count ((GstData*)buffers_in[i], numsrcpads);
743 for (j=0; j<numsrcpads; j++)
744 gst_pad_push (ladspa->srcpads[j], GST_DATA (buffers_in[i]));
745 if (GST_EVENT_TYPE (buffers_in[i]) == GST_EVENT_EOS) {
747 gst_element_set_eos (element);
754 if (largest_buffer < 0)
755 largest_buffer = GST_BUFFER_SIZE (buffers_in[i])/sizeof(gfloat);
757 largest_buffer = MIN (GST_BUFFER_SIZE (buffers_in[i])/sizeof(gfloat), largest_buffer);
758 data_in[i] = (LADSPA_Data *) GST_BUFFER_DATA(buffers_in[i]);
759 GST_BUFFER_TIMESTAMP(buffers_in[i]) = ladspa->timestamp;
762 if (!ladspa->bufpool) {
763 gst_element_error (element, "Caps were never set, bailing...");
768 if (!ladspa->inplace_broken) {
769 for (; i<numsrcpads && i<numsinkpads; i++) {
770 /* reuse input buffers */
771 buffers_out[i] = buffers_in[i];
772 data_out[i] = data_in[i];
775 for (; i<numsrcpads; i++) {
776 /* we have to make new buffers -- at least we're taking them from a pool */
777 buffers_out[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
778 GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
779 data_out[i] = (LADSPA_Data*)GST_BUFFER_DATA (buffers_out[i]);
782 GST_DPMAN_PREPROCESS(ladspa->dpman, largest_buffer, ladspa->timestamp);
785 /* split up processing of the buffer into chunks so that dparams can
786 * be updated when required.
787 * In many cases the buffer will be processed in one chunk anyway.
789 while (GST_DPMAN_PROCESS (ladspa->dpman, num_processed)) {
790 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
792 for (i=0 ; i<numsinkpads ; i++)
793 desc->connect_port (ladspa->handle, oclass->sinkpad_portnums[i], data_in[i]);
794 for (i=0 ; i<numsrcpads ; i++)
795 desc->connect_port (ladspa->handle, oclass->srcpad_portnums[i], data_out[i]);
797 desc->run(ladspa->handle, num_to_process);
799 for (i=0 ; i<numsinkpads ; i++)
800 data_in[i] += num_to_process;
801 for (i=0 ; i<numsrcpads ; i++)
802 data_out[i] += num_to_process;
804 num_processed += num_to_process;
807 for (i=0 ; i<numsinkpads ; i++) {
808 if (i >= numsrcpads || buffers_out[i] != buffers_in[i])
809 gst_buffer_unref(buffers_in[i]);
811 buffers_in[i] = NULL;
813 for (i=0 ; i<numsrcpads ; i++) {
814 DEBUG_OBJ (ladspa, "pushing buffer (%p) on src pad %d", buffers_out[i], i);
815 gst_pad_push (ladspa->srcpads[i], GST_DATA (buffers_out[i]));
818 buffers_out[i] = NULL;
821 ladspa->timestamp += ladspa->buffer_frames * GST_SECOND / ladspa->samplerate;
823 /* FIXME: move these mallocs and frees to the state-change handler */
825 g_free (buffers_out);
832 gst_ladspa_chain (GstPad *pad, GstData *_data)
834 GstBuffer *buffer_in = GST_BUFFER (_data);
835 LADSPA_Descriptor *desc;
836 LADSPA_Data *data_in, **data_out = NULL;
837 GstBuffer **buffers_out = NULL;
838 unsigned long num_samples;
839 guint num_to_process, num_processed, i, numsrcpads;
841 GstLADSPAClass *oclass;
843 ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
844 oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
845 data_in = (LADSPA_Data *) GST_BUFFER_DATA(buffer_in);
846 num_samples = GST_BUFFER_SIZE(buffer_in) / sizeof(gfloat);
847 numsrcpads = oclass->numsrcpads;
848 desc = ladspa->descriptor;
850 /* we shouldn't get events here... */
851 g_return_if_fail (GST_IS_BUFFER (buffer_in));
853 if (!ladspa->bufpool) {
854 gst_element_error ((GstElement*)ladspa, "Caps were never set, bailing...");
858 /* FIXME: this function shouldn't need to malloc() anything */
859 if (numsrcpads > 0) {
860 buffers_out = g_new(GstBuffer*, numsrcpads);
861 data_out = g_new(LADSPA_Data*, numsrcpads);
865 if (!ladspa->inplace_broken && numsrcpads) {
866 /* reuse the first (chained) buffer */
867 buffers_out[i] = buffer_in;
868 DEBUG ("reuse: %d", GST_BUFFER_SIZE (buffer_in));
869 data_out[i] = data_in;
872 for (; i<numsrcpads; i++) {
873 /* we have to make new buffers -- at least we're taking them from a pool */
874 buffers_out[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
875 /* the size of the buffer returned from the pool is the maximum size; this
876 chained buffer might be smaller */
877 GST_BUFFER_SIZE (buffers_out[i]) = GST_BUFFER_SIZE (buffer_in);
878 DEBUG ("new %d", GST_BUFFER_SIZE (buffer_in));
879 GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
880 data_out[i] = (LADSPA_Data*)GST_BUFFER_DATA (buffers_out[i]);
883 GST_DPMAN_PREPROCESS(ladspa->dpman, num_samples, GST_BUFFER_TIMESTAMP(buffer_in));
886 /* split up processing of the buffer into chunks so that dparams can
887 * be updated when required.
888 * In many cases the buffer will be processed in one chunk anyway.
890 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
891 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
893 desc->connect_port(ladspa->handle,oclass->sinkpad_portnums[0],data_in);
894 for (i=0 ; i<numsrcpads ; i++)
895 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[i],data_out[i]);
897 desc->run(ladspa->handle, num_to_process);
899 data_in += num_to_process;
900 for (i=0 ; i<numsrcpads ; i++)
901 data_out[i] += num_to_process;
903 num_processed += num_to_process;
906 if (!numsrcpads || buffers_out[0] != buffer_in)
907 gst_buffer_unref(buffer_in);
910 for (i=0; i<numsrcpads; i++) {
911 DEBUG_OBJ (ladspa, "pushing buffer (%p, length %u bytes) on src pad %d",
912 buffers_out[i], GST_BUFFER_SIZE (buffers_out[i]), i);
913 gst_pad_push (ladspa->srcpads[i], GST_DATA (buffers_out[i]));
922 gst_ladspa_get(GstPad *pad)
925 GstLADSPAClass *oclass;
928 LADSPA_Descriptor *desc;
929 guint num_to_process, num_processed;
931 ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
932 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
933 desc = ladspa->descriptor;
935 if (!ladspa->bufpool) {
936 /* capsnego hasn't happened... */
937 gst_ladspa_force_src_caps(ladspa, ladspa->srcpads[0]);
940 buf = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
941 GST_BUFFER_TIMESTAMP(buf) = ladspa->timestamp;
942 data = (LADSPA_Data *) GST_BUFFER_DATA(buf);
944 GST_DPMAN_PREPROCESS(ladspa->dpman, ladspa->buffer_frames, ladspa->timestamp);
947 /* split up processing of the buffer into chunks so that dparams can
948 * be updated when required.
949 * In many cases the buffer will be processed in one chunk anyway.
951 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
952 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
954 /* update timestamp */
955 ladspa->timestamp += num_to_process * GST_SECOND / ladspa->samplerate;
957 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);
959 desc->run(ladspa->handle, num_to_process);
961 data += num_to_process;
962 num_processed = num_to_process;
965 return GST_DATA (buf);
969 ladspa_describe_plugin(const char *pcFullFilename,
970 void *pvPluginHandle,
971 LADSPA_Descriptor_Function pfDescriptorFunction)
973 const LADSPA_Descriptor *desc;
975 GTypeInfo typeinfo = {
976 sizeof(GstLADSPAClass),
977 (GBaseInitFunc)gst_ladspa_base_init,
979 (GClassInitFunc)gst_ladspa_class_init,
984 (GInstanceInitFunc)gst_ladspa_init,
988 /* walk through all the plugins in this pluginlibrary */
990 while ((desc = pfDescriptorFunction(i++))) {
993 /* construct the type */
994 type_name = g_strdup_printf("ladspa-%s",desc->Label);
995 g_strcanon (type_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-+", '-');
996 /* if it's already registered, drop it */
997 if (g_type_from_name(type_name)) {
1002 /* base-init temp alloc */
1003 g_hash_table_insert(ladspa_descriptors,
1007 /* create the type now */
1008 type = g_type_register_static(GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
1009 if (!gst_element_register (ladspa_plugin, type_name, GST_RANK_NONE, type))
1012 /* add this plugin to the hash */
1013 g_hash_table_insert(ladspa_descriptors,
1014 GINT_TO_POINTER(type),
1018 g_hash_table_remove (ladspa_descriptors, GINT_TO_POINTER (0));
1022 plugin_init (GstPlugin *plugin)
1024 GST_DEBUG_CATEGORY_INIT (ladspa_debug, "ladspa",
1025 GST_DEBUG_FG_GREEN | GST_DEBUG_BG_BLACK | GST_DEBUG_BOLD,
1028 ladspa_descriptors = g_hash_table_new(NULL,NULL);
1029 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
1031 ladspa_plugin = plugin;
1033 LADSPAPluginSearch(ladspa_describe_plugin);
1035 /* initialize dparam support library */
1036 gst_control_init(NULL,NULL);
1045 "All LADSPA plugins",