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 /* construct the element details struct */
114 details = g_new0(GstElementDetails,1);
115 details->longname = g_strdup(desc->Name);
116 details->klass = "Filter/Audio/LADSPA";
117 details->description = details->longname;
118 details->author = g_strdup(desc->Maker);
119 gst_element_class_set_details (element_class, details);
122 klass->numports = desc->PortCount;
123 klass->numsinkpads = 0;
124 klass->numsrcpads = 0;
125 for (j=0;j<desc->PortCount;j++) {
126 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j])) {
127 gchar *name = g_strdup((gchar *)desc->PortNames[j]);
128 g_strcanon (name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
130 /* the factories take ownership of the name */
131 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j])) {
132 templ = ladspa_sink_factory (name);
133 klass->numsinkpads++;
135 templ = ladspa_src_factory (name);
139 gst_element_class_add_pad_template (element_class, templ);
143 klass->srcpad_portnums = g_new0(gint,klass->numsrcpads);
144 klass->sinkpad_portnums = g_new0(gint,klass->numsinkpads);
148 /* walk through the ports, note the portnums for srcpads, sinkpads */
149 for (j=0; j<desc->PortCount; j++) {
150 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j])) {
151 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j]))
152 klass->sinkpad_portnums[sinkcount++] = j;
154 klass->srcpad_portnums[srccount++] = j;
158 klass->descriptor = desc;
162 gst_ladspa_class_init (GstLADSPAClass *klass)
164 GObjectClass *gobject_class;
165 GstElementClass *gstelement_class;
166 LADSPA_Descriptor *desc;
167 gint i,current_portnum,controlcount;
169 gint argtype,argperms;
170 GParamSpec *paramspec = NULL;
171 gchar *argname, *tempstr, *paren;
173 gobject_class = (GObjectClass*)klass;
174 gstelement_class = (GstElementClass*)klass;
176 gobject_class->set_property = gst_ladspa_set_property;
177 gobject_class->get_property = gst_ladspa_get_property;
179 gstelement_class->change_state = gst_ladspa_change_state;
181 /* look up and store the ladspa descriptor */
182 desc = g_hash_table_lookup(ladspa_descriptors,
183 GINT_TO_POINTER(G_TYPE_FROM_CLASS(klass)));
185 klass->numcontrols = 0;
187 /* walk through the ports, count the input, output and control ports */
188 for (i=0; i<desc->PortCount; i++) {
189 if (!LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
190 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
191 klass->numcontrols++;
194 DEBUG ("ladspa element class: init %s with %d sink, %d src, %d control\n",
195 g_type_name (G_TYPE_FROM_CLASS (klass)),
196 klass->numsinkpads, klass->numsrcpads, klass->numcontrols);
198 klass->control_portnums = g_new0(gint,klass->numcontrols);
201 /* walk through the ports, note the portnums for control params */
202 for (i=0; i<desc->PortCount; i++) {
203 if (!LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
204 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
205 klass->control_portnums[controlcount++] = i;
208 /* now build the control info from the control ports */
209 klass->control_info = g_new0(ladspa_control_info,klass->numcontrols);
211 for (i=0;i<klass->numcontrols;i++) {
212 current_portnum = klass->control_portnums[i];
214 /* short name for hint descriptor */
215 hintdesc = desc->PortRangeHints[current_portnum].HintDescriptor;
217 /* get the various bits */
218 if (LADSPA_IS_HINT_TOGGLED(hintdesc))
219 klass->control_info[i].toggled = TRUE;
220 if (LADSPA_IS_HINT_LOGARITHMIC(hintdesc))
221 klass->control_info[i].logarithmic = TRUE;
222 if (LADSPA_IS_HINT_INTEGER(hintdesc))
223 klass->control_info[i].integer = TRUE;
225 /* figure out the argument details */
226 if (klass->control_info[i].toggled) argtype = G_TYPE_BOOLEAN;
227 else if (klass->control_info[i].integer) argtype = G_TYPE_INT;
228 else argtype = G_TYPE_FLOAT;
230 /* grab the bounds */
231 if (LADSPA_IS_HINT_BOUNDED_BELOW(hintdesc)) {
232 klass->control_info[i].lower = TRUE;
233 klass->control_info[i].lowerbound =
234 desc->PortRangeHints[current_portnum].LowerBound;
236 if (argtype==G_TYPE_INT) klass->control_info[i].lowerbound = (gfloat)G_MININT;
237 if (argtype==G_TYPE_FLOAT) klass->control_info[i].lowerbound = -G_MAXFLOAT;
240 if (LADSPA_IS_HINT_BOUNDED_ABOVE(hintdesc)) {
241 klass->control_info[i].upper = TRUE;
242 klass->control_info[i].upperbound =
243 desc->PortRangeHints[current_portnum].UpperBound;
244 if (LADSPA_IS_HINT_SAMPLE_RATE(hintdesc)) {
245 klass->control_info[i].samplerate = TRUE;
246 klass->control_info[i].upperbound *= 44100; /* FIXME? */
249 if (argtype==G_TYPE_INT) klass->control_info[i].upperbound = (gfloat)G_MAXINT;
250 if (argtype==G_TYPE_FLOAT) klass->control_info[i].upperbound = G_MAXFLOAT;
253 /* use the lowerbound as the default value */
254 klass->control_info[i].def = klass->control_info[i].lowerbound;
256 #ifdef LADSPA_IS_HINT_HAS_DEFAULT
257 /* figure out the defaults */
258 if (LADSPA_IS_HINT_HAS_DEFAULT (hintdesc)) {
259 if (LADSPA_IS_HINT_DEFAULT_MINIMUM (hintdesc))
260 klass->control_info[i].def = klass->control_info[i].lowerbound;
261 else if (LADSPA_IS_HINT_DEFAULT_LOW (hintdesc))
262 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
263 klass->control_info[i].def = exp (0.75*log(klass->control_info[i].lowerbound) +
264 0.25*log(klass->control_info[i].upperbound));
266 klass->control_info[i].def = (0.75*klass->control_info[i].lowerbound +
267 0.25*klass->control_info[i].upperbound);
268 else if (LADSPA_IS_HINT_DEFAULT_MIDDLE (hintdesc))
269 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
270 klass->control_info[i].def = exp (0.5*log(klass->control_info[i].lowerbound) +
271 0.5*log(klass->control_info[i].upperbound));
273 klass->control_info[i].def = (0.5*klass->control_info[i].lowerbound +
274 0.5*klass->control_info[i].upperbound);
275 else if (LADSPA_IS_HINT_DEFAULT_HIGH (hintdesc))
276 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
277 klass->control_info[i].def = exp (0.25*log(klass->control_info[i].lowerbound) +
278 0.75*log(klass->control_info[i].upperbound));
280 klass->control_info[i].def = (0.25*klass->control_info[i].lowerbound +
281 0.75*klass->control_info[i].upperbound);
282 else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM (hintdesc))
283 klass->control_info[i].def = klass->control_info[i].upperbound;
284 else if (LADSPA_IS_HINT_DEFAULT_0 (hintdesc))
285 klass->control_info[i].def = 0.0;
286 else if (LADSPA_IS_HINT_DEFAULT_1 (hintdesc))
287 klass->control_info[i].def = 1.0;
288 else if (LADSPA_IS_HINT_DEFAULT_100 (hintdesc))
289 klass->control_info[i].def = 100.0;
290 else if (LADSPA_IS_HINT_DEFAULT_440 (hintdesc))
291 klass->control_info[i].def = 440.0;
293 #endif /* LADSPA_IS_HINT_HAS_DEFAULT */
295 klass->control_info[i].def = CLAMP(klass->control_info[i].def,
296 klass->control_info[i].lowerbound,
297 klass->control_info[i].upperbound);
299 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[current_portnum])) {
300 argperms = G_PARAM_READWRITE;
301 klass->control_info[i].writable = TRUE;
303 argperms = G_PARAM_READABLE;
304 klass->control_info[i].writable = FALSE;
307 klass->control_info[i].name = g_strdup(desc->PortNames[current_portnum]);
308 argname = g_strdup(klass->control_info[i].name);
309 /* find out if there is a (unitname) at the end of the argname and get rid
311 paren = g_strrstr (argname, " (");
315 /* this is the same thing that param_spec_* will do */
316 g_strcanon (argname, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
317 /* satisfy glib2 (argname[0] must be [A-Za-z]) */
318 if (!((argname[0] >= 'a' && argname[0] <= 'z') || (argname[0] >= 'A' && argname[0] <= 'Z'))) {
320 argname = g_strconcat("param-", argname, NULL);
324 /* check for duplicate property names */
325 if (g_object_class_find_property(G_OBJECT_CLASS(klass), argname) != NULL){
327 gchar *numargname = g_strdup_printf("%s_%d",argname,numarg++);
328 while (g_object_class_find_property(G_OBJECT_CLASS(klass), numargname) != NULL){
330 numargname = g_strdup_printf("%s_%d",argname,numarg++);
332 argname = numargname;
335 klass->control_info[i].param_name = argname;
337 DEBUG ("adding arg %s from %s", argname, klass->control_info[i].name);
339 if (argtype==G_TYPE_BOOLEAN){
340 paramspec = g_param_spec_boolean(argname,argname,argname, FALSE, argperms);
341 } else if (argtype==G_TYPE_INT){
342 paramspec = g_param_spec_int(argname,argname,argname,
343 (gint)klass->control_info[i].lowerbound,
344 (gint)klass->control_info[i].upperbound,
345 (gint)klass->control_info[i].def, argperms);
346 } else if (klass->control_info[i].samplerate){
347 paramspec = g_param_spec_float(argname,argname,argname,
351 paramspec = g_param_spec_float(argname,argname,argname,
352 klass->control_info[i].lowerbound, klass->control_info[i].upperbound,
353 klass->control_info[i].def, argperms);
356 /* properties have an offset of 1 */
357 g_object_class_install_property(G_OBJECT_CLASS(klass), i+1, paramspec);
362 gst_ladspa_init (GstLADSPA *ladspa)
364 GstLADSPAClass *oclass;
365 ladspa_control_info cinfo;
367 LADSPA_Descriptor *desc;
368 gint i,sinkcount,srccount;
370 oclass = (GstLADSPAClass*)G_OBJECT_GET_CLASS (ladspa);
371 desc = oclass->descriptor;
372 ladspa->descriptor = oclass->descriptor;
374 /* allocate the various arrays */
375 ladspa->srcpads = g_new0(GstPad*,oclass->numsrcpads);
376 ladspa->sinkpads = g_new0(GstPad*,oclass->numsinkpads);
377 ladspa->controls = g_new(gfloat,oclass->numcontrols);
378 ladspa->dpman = gst_dpman_new ("ladspa_dpman", GST_ELEMENT(ladspa));
383 for (l=GST_ELEMENT_CLASS (oclass)->padtemplates; l; l=l->next) {
384 GstPad *pad = gst_pad_new_from_template (GST_PAD_TEMPLATE (l->data),
385 GST_PAD_TEMPLATE_NAME_TEMPLATE (l->data));
386 gst_pad_set_link_function (pad, gst_ladspa_link);
387 gst_element_add_pad ((GstElement*)ladspa, pad);
389 if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK)
390 ladspa->sinkpads[sinkcount++] = pad;
392 ladspa->srcpads[srccount++] = pad;
396 for (i=0; i<oclass->numcontrols; i++) {
397 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
398 cinfo = oclass->control_info[i];
399 ladspa->controls[i]=cinfo.def;
402 gst_dpman_add_required_dparam_callback (
404 g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
405 0, 1, (gint)(ladspa->controls[i]), G_PARAM_READWRITE),
406 "int", gst_ladspa_update_int, &(ladspa->controls[i])
409 else if (cinfo.integer){
410 gst_dpman_add_required_dparam_callback (
412 g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
413 (gint)cinfo.lowerbound, (gint)cinfo.upperbound,
414 (gint)ladspa->controls[i], G_PARAM_READWRITE),
415 "int", gst_ladspa_update_int, &(ladspa->controls[i])
418 else if (cinfo.samplerate){
419 gst_dpman_add_required_dparam_direct (
421 g_param_spec_float(cinfo.param_name, cinfo.name, cinfo.name,
422 cinfo.lowerbound, cinfo.upperbound,
423 ladspa->controls[i], G_PARAM_READWRITE),
424 "hertz-rate-bound", &(ladspa->controls[i])
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 "float", &(ladspa->controls[i])
439 /* nonzero default needed to instantiate() some plugins */
440 ladspa->samplerate = 44100;
442 ladspa->buffer_frames = 0; /* should be set with caps */
443 ladspa->activated = FALSE;
444 ladspa->bufpool = NULL;
445 ladspa->inplace_broken = LADSPA_IS_INPLACE_BROKEN(ladspa->descriptor->Properties);
447 if (sinkcount==0 && srccount == 1) {
448 /* get mode (no sink pads) */
449 DEBUG_OBJ (ladspa, "mono get mode with 1 src pad");
451 gst_pad_set_get_function (ladspa->srcpads[0], gst_ladspa_get);
452 } else if (sinkcount==1){
453 /* with one sink we can use the chain function */
454 DEBUG_OBJ (ladspa, "chain mode");
456 gst_pad_set_chain_function (ladspa->sinkpads[0], gst_ladspa_chain);
457 } else if (sinkcount > 1){
458 /* more than one sink pad needs loop mode */
459 DEBUG_OBJ (ladspa, "loop mode with %d sink pads and %d src pads", sinkcount, srccount);
461 gst_element_set_loop_function (GST_ELEMENT (ladspa), gst_ladspa_loop);
462 } else if (sinkcount==0 && srccount == 0) {
463 /* for example, a plugin with only control inputs and output -- just ignore
466 g_warning ("%d sink pads, %d src pads not yet supported", sinkcount, srccount);
469 gst_ladspa_instantiate (ladspa);
473 gst_ladspa_update_int(const GValue *value, gpointer data)
475 gfloat *target = (gfloat*) data;
476 *target = (gfloat)g_value_get_int(value);
479 static GstPadLinkReturn
480 gst_ladspa_link (GstPad *pad, GstCaps *caps)
482 GstElement *element = (GstElement*)GST_PAD_PARENT (pad);
483 GstLADSPA *ladspa = (GstLADSPA*)element;
484 const GList *l = NULL;
487 if (GST_CAPS_IS_FIXED (caps)) {
488 /* if this fails in some other plugin, the graph is left in an inconsistent
490 for (l=gst_element_get_pad_list (element); l; l=l->next)
491 if (pad != (GstPad*)l->data)
492 if (gst_pad_try_set_caps ((GstPad*)l->data, caps) <= 0)
493 return GST_PAD_LINK_REFUSED;
495 /* we assume that the ladspa plugin can handle any sample rate, so this
496 check gets put last */
497 gst_caps_get_int (caps, "rate", &rate);
498 /* have to instantiate ladspa plugin when samplerate changes (groan) */
499 if (ladspa->samplerate != rate) {
500 ladspa->samplerate = rate;
501 if (! gst_ladspa_instantiate(ladspa))
502 return GST_PAD_LINK_REFUSED;
505 gst_caps_get_int (caps, "buffer-frames", &ladspa->buffer_frames);
508 gst_buffer_pool_unref (ladspa->bufpool);
509 ladspa->bufpool = gst_buffer_pool_get_default (ladspa->buffer_frames * sizeof(gfloat),
512 return GST_PAD_LINK_OK;
515 return GST_PAD_LINK_DELAYED;
519 gst_ladspa_force_src_caps(GstLADSPA *ladspa, GstPad *pad)
521 if (!ladspa->buffer_frames) {
522 ladspa->buffer_frames = 256; /* 5 ms at 44100 kHz (just a default...) */
523 g_return_if_fail (ladspa->bufpool == NULL);
525 gst_buffer_pool_get_default (ladspa->buffer_frames * sizeof(gfloat), 3);
528 DEBUG_OBJ (ladspa, "forcing caps with rate=%d, buffer-frames=%d",
529 ladspa->samplerate, ladspa->buffer_frames);
531 gst_pad_try_set_caps (pad,
536 "width", GST_PROPS_INT (32),
537 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
538 "rate", GST_PROPS_INT (ladspa->samplerate),
539 "buffer-frames", GST_PROPS_INT (ladspa->buffer_frames),
540 "channels", GST_PROPS_INT (1),
545 gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
547 GstLADSPA *ladspa = (GstLADSPA*)object;
548 GstLADSPAClass *oclass;
549 ladspa_control_info *control_info;
551 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
553 /* remember, properties have an offset of 1 */
556 /* verify it exists */
557 g_return_if_fail (prop_id < oclass->numcontrols);
559 control_info = &(oclass->control_info[prop_id]);
560 g_return_if_fail (control_info->name != NULL);
562 /* check to see if it's writable */
563 g_return_if_fail (control_info->writable);
565 /* now see what type it is */
566 if (control_info->toggled)
567 ladspa->controls[prop_id] = g_value_get_boolean (value) ? 1.f : 0.f;
568 else if (control_info->integer)
569 ladspa->controls[prop_id] = g_value_get_int (value);
571 ladspa->controls[prop_id] = g_value_get_float (value);
573 DEBUG_OBJ (object, "set arg %s to %f", control_info->name, ladspa->controls[prop_id]);
577 gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
579 GstLADSPA *ladspa = (GstLADSPA*)object;
580 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
581 ladspa_control_info *control_info;
583 /* remember, properties have an offset of 1 */
586 /* verify it exists */
587 g_return_if_fail (prop_id < oclass->numcontrols);
589 control_info = &(oclass->control_info[prop_id]);
590 g_return_if_fail (control_info->name != NULL);
592 /* now see what type it is */
593 if (control_info->toggled)
594 g_value_set_boolean (value, ladspa->controls[prop_id] == 1.0);
595 else if (control_info->integer)
596 g_value_set_int (value, (gint)ladspa->controls[prop_id]);
598 g_value_set_float (value, ladspa->controls[prop_id]);
600 DEBUG_OBJ (object, "got arg %s as %f", control_info->name, ladspa->controls[prop_id]);
604 gst_ladspa_instantiate (GstLADSPA *ladspa)
606 LADSPA_Descriptor *desc;
608 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
609 gboolean was_activated;
611 desc = ladspa->descriptor;
613 /* check for old handle */
614 was_activated = ladspa->activated;
615 if (ladspa->handle != NULL){
616 gst_ladspa_deactivate(ladspa);
617 desc->cleanup(ladspa->handle);
620 /* instantiate the plugin */
621 DEBUG_OBJ (ladspa, "instantiating the plugin at %d Hz", ladspa->samplerate);
623 ladspa->handle = desc->instantiate(desc,ladspa->samplerate);
624 g_return_val_if_fail (ladspa->handle != NULL, FALSE);
626 /* connect the control ports */
627 for (i=0;i<oclass->numcontrols;i++)
628 desc->connect_port(ladspa->handle,
629 oclass->control_portnums[i],
630 &(ladspa->controls[i]));
632 /* reactivate if it was activated before the reinstantiation */
634 gst_ladspa_activate(ladspa);
639 static GstElementStateReturn
640 gst_ladspa_change_state (GstElement *element)
642 LADSPA_Descriptor *desc;
643 GstLADSPA *ladspa = (GstLADSPA*)element;
644 desc = ladspa->descriptor;
646 switch (GST_STATE_TRANSITION (element)) {
647 case GST_STATE_NULL_TO_READY:
648 gst_ladspa_activate(ladspa);
650 case GST_STATE_READY_TO_NULL:
651 gst_ladspa_deactivate(ladspa);
657 if (GST_ELEMENT_CLASS (parent_class)->change_state)
658 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
660 return GST_STATE_SUCCESS;
664 gst_ladspa_activate (GstLADSPA *ladspa)
666 LADSPA_Descriptor *desc;
667 desc = ladspa->descriptor;
669 if (ladspa->activated)
670 gst_ladspa_deactivate(ladspa);
672 DEBUG_OBJ (ladspa, "activating");
674 /* activate the plugin (function might be null) */
675 if (desc->activate != NULL)
676 desc->activate(ladspa->handle);
678 ladspa->activated = TRUE;
682 gst_ladspa_deactivate (GstLADSPA *ladspa)
684 LADSPA_Descriptor *desc;
685 desc = ladspa->descriptor;
687 DEBUG_OBJ (ladspa, "deactivating");
689 /* deactivate the plugin (function might be null) */
690 if (ladspa->activated && (desc->deactivate != NULL))
691 desc->deactivate(ladspa->handle);
693 ladspa->activated = FALSE;
697 gst_ladspa_loop (GstElement *element)
699 guint i, j, numsrcpads, numsinkpads;
700 guint num_processed, num_to_process;
702 LADSPA_Data **data_in, **data_out;
703 GstBuffer **buffers_in, **buffers_out;
705 GstLADSPA *ladspa = (GstLADSPA *)element;
706 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
707 LADSPA_Descriptor *desc = ladspa->descriptor;
709 numsinkpads = oclass->numsinkpads;
710 numsrcpads = oclass->numsrcpads;
712 /* fixme: these mallocs need to die */
713 data_in = g_new0(LADSPA_Data*, numsinkpads);
714 data_out = g_new0(LADSPA_Data*, numsrcpads);
715 buffers_in = g_new0(GstBuffer*, numsinkpads);
716 buffers_out = g_new0(GstBuffer*, numsrcpads);
720 /* first get all the necessary data from the input ports */
721 for (i=0 ; i<numsinkpads ; i++){
723 buffers_in[i] = GST_BUFFER (gst_pad_pull (ladspa->sinkpads[i]));
725 if (GST_IS_EVENT (buffers_in[i])) {
726 /* push it out on all pads */
727 gst_data_ref_by_count ((GstData*)buffers_in[i], numsrcpads);
728 for (j=0; j<numsrcpads; j++)
729 gst_pad_push (ladspa->srcpads[j], GST_DATA (buffers_in[i]));
730 if (GST_EVENT_TYPE (buffers_in[i]) == GST_EVENT_EOS) {
732 gst_element_set_eos (element);
739 if (largest_buffer < 0)
740 largest_buffer = GST_BUFFER_SIZE (buffers_in[i])/sizeof(gfloat);
742 largest_buffer = MIN (GST_BUFFER_SIZE (buffers_in[i])/sizeof(gfloat), largest_buffer);
743 data_in[i] = (LADSPA_Data *) GST_BUFFER_DATA(buffers_in[i]);
744 GST_BUFFER_TIMESTAMP(buffers_in[i]) = ladspa->timestamp;
747 if (!ladspa->bufpool) {
748 gst_element_error (element, "Caps were never set, bailing...");
753 if (!ladspa->inplace_broken) {
754 for (; i<numsrcpads && i<numsinkpads; i++) {
755 /* reuse input buffers */
756 buffers_out[i] = buffers_in[i];
757 data_out[i] = data_in[i];
760 for (; i<numsrcpads; i++) {
761 /* we have to make new buffers -- at least we're taking them from a pool */
762 buffers_out[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
763 GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
764 data_out[i] = (LADSPA_Data*)GST_BUFFER_DATA (buffers_out[i]);
767 GST_DPMAN_PREPROCESS(ladspa->dpman, largest_buffer, ladspa->timestamp);
770 /* split up processing of the buffer into chunks so that dparams can
771 * be updated when required.
772 * In many cases the buffer will be processed in one chunk anyway.
774 while (GST_DPMAN_PROCESS (ladspa->dpman, num_processed)) {
775 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
777 for (i=0 ; i<numsinkpads ; i++)
778 desc->connect_port (ladspa->handle, oclass->sinkpad_portnums[i], data_in[i]);
779 for (i=0 ; i<numsrcpads ; i++)
780 desc->connect_port (ladspa->handle, oclass->srcpad_portnums[i], data_out[i]);
782 desc->run(ladspa->handle, num_to_process);
784 for (i=0 ; i<numsinkpads ; i++)
785 data_in[i] += num_to_process;
786 for (i=0 ; i<numsrcpads ; i++)
787 data_out[i] += num_to_process;
789 num_processed += num_to_process;
792 for (i=0 ; i<numsinkpads ; i++) {
793 if (i >= numsrcpads || buffers_out[i] != buffers_in[i])
794 gst_buffer_unref(buffers_in[i]);
796 buffers_in[i] = NULL;
798 for (i=0 ; i<numsrcpads ; i++) {
799 DEBUG_OBJ (ladspa, "pushing buffer (%p) on src pad %d", buffers_out[i], i);
800 gst_pad_push (ladspa->srcpads[i], GST_DATA (buffers_out[i]));
803 buffers_out[i] = NULL;
806 ladspa->timestamp += ladspa->buffer_frames * GST_SECOND / ladspa->samplerate;
808 /* FIXME: move these mallocs and frees to the state-change handler */
810 g_free (buffers_out);
817 gst_ladspa_chain (GstPad *pad, GstData *_data)
819 GstBuffer *buffer_in = GST_BUFFER (_data);
820 LADSPA_Descriptor *desc;
821 LADSPA_Data *data_in, **data_out = NULL;
822 GstBuffer **buffers_out = NULL;
823 unsigned long num_samples;
824 guint num_to_process, num_processed, i, numsrcpads;
826 GstLADSPAClass *oclass;
828 ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
829 oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
830 data_in = (LADSPA_Data *) GST_BUFFER_DATA(buffer_in);
831 num_samples = GST_BUFFER_SIZE(buffer_in) / sizeof(gfloat);
832 numsrcpads = oclass->numsrcpads;
833 desc = ladspa->descriptor;
835 /* we shouldn't get events here... */
836 g_return_if_fail (GST_IS_BUFFER (buffer_in));
838 if (!ladspa->bufpool) {
839 gst_element_error ((GstElement*)ladspa, "Caps were never set, bailing...");
843 /* FIXME: this function shouldn't need to malloc() anything */
844 if (numsrcpads > 0) {
845 buffers_out = g_new(GstBuffer*, numsrcpads);
846 data_out = g_new(LADSPA_Data*, numsrcpads);
850 if (!ladspa->inplace_broken && numsrcpads) {
851 /* reuse the first (chained) buffer */
852 buffers_out[i] = buffer_in;
853 DEBUG ("reuse: %d", GST_BUFFER_SIZE (buffer_in));
854 data_out[i] = data_in;
857 for (; i<numsrcpads; i++) {
858 /* we have to make new buffers -- at least we're taking them from a pool */
859 buffers_out[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
860 /* the size of the buffer returned from the pool is the maximum size; this
861 chained buffer might be smaller */
862 GST_BUFFER_SIZE (buffers_out[i]) = GST_BUFFER_SIZE (buffer_in);
863 DEBUG ("new %d", GST_BUFFER_SIZE (buffer_in));
864 GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
865 data_out[i] = (LADSPA_Data*)GST_BUFFER_DATA (buffers_out[i]);
868 GST_DPMAN_PREPROCESS(ladspa->dpman, num_samples, GST_BUFFER_TIMESTAMP(buffer_in));
871 /* split up processing of the buffer into chunks so that dparams can
872 * be updated when required.
873 * In many cases the buffer will be processed in one chunk anyway.
875 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
876 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
878 desc->connect_port(ladspa->handle,oclass->sinkpad_portnums[0],data_in);
879 for (i=0 ; i<numsrcpads ; i++)
880 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[i],data_out[i]);
882 desc->run(ladspa->handle, num_to_process);
884 data_in += num_to_process;
885 for (i=0 ; i<numsrcpads ; i++)
886 data_out[i] += num_to_process;
888 num_processed += num_to_process;
891 if (!numsrcpads || buffers_out[0] != buffer_in)
892 gst_buffer_unref(buffer_in);
895 for (i=0; i<numsrcpads; i++) {
896 DEBUG_OBJ (ladspa, "pushing buffer (%p, length %u bytes) on src pad %d",
897 buffers_out[i], GST_BUFFER_SIZE (buffers_out[i]), i);
898 gst_pad_push (ladspa->srcpads[i], GST_DATA (buffers_out[i]));
907 gst_ladspa_get(GstPad *pad)
910 GstLADSPAClass *oclass;
913 LADSPA_Descriptor *desc;
914 guint num_to_process, num_processed;
916 ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
917 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
918 desc = ladspa->descriptor;
920 if (!ladspa->bufpool) {
921 /* capsnego hasn't happened... */
922 gst_ladspa_force_src_caps(ladspa, ladspa->srcpads[0]);
925 buf = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
926 GST_BUFFER_TIMESTAMP(buf) = ladspa->timestamp;
927 data = (LADSPA_Data *) GST_BUFFER_DATA(buf);
929 GST_DPMAN_PREPROCESS(ladspa->dpman, ladspa->buffer_frames, ladspa->timestamp);
932 /* split up processing of the buffer into chunks so that dparams can
933 * be updated when required.
934 * In many cases the buffer will be processed in one chunk anyway.
936 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
937 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
939 /* update timestamp */
940 ladspa->timestamp += num_to_process * GST_SECOND / ladspa->samplerate;
942 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);
944 desc->run(ladspa->handle, num_to_process);
946 data += num_to_process;
947 num_processed = num_to_process;
950 return GST_DATA (buf);
954 ladspa_describe_plugin(const char *pcFullFilename,
955 void *pvPluginHandle,
956 LADSPA_Descriptor_Function pfDescriptorFunction)
958 const LADSPA_Descriptor *desc;
960 GTypeInfo typeinfo = {
961 sizeof(GstLADSPAClass),
962 (GBaseInitFunc)gst_ladspa_base_init,
964 (GClassInitFunc)gst_ladspa_class_init,
969 (GInstanceInitFunc)gst_ladspa_init,
973 /* walk through all the plugins in this pluginlibrary */
975 while ((desc = pfDescriptorFunction(i++))) {
978 /* construct the type */
979 type_name = g_strdup_printf("ladspa-%s",desc->Label);
980 g_strcanon (type_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-+", '-');
981 /* if it's already registered, drop it */
982 if (g_type_from_name(type_name)) {
986 /* create the type now */
987 type = g_type_register_static(GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
988 if (!gst_element_register (ladspa_plugin, type_name, GST_RANK_NONE, type))
991 /* add this plugin to the hash */
992 g_hash_table_insert(ladspa_descriptors,
993 GINT_TO_POINTER(type),
999 plugin_init (GstPlugin *plugin)
1001 GST_DEBUG_CATEGORY_INIT (ladspa_debug, "ladspa",
1002 GST_DEBUG_FG_GREEN | GST_DEBUG_BG_BLACK | GST_DEBUG_BOLD,
1005 ladspa_descriptors = g_hash_table_new(NULL,NULL);
1006 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
1008 ladspa_plugin = plugin;
1010 LADSPAPluginSearch(ladspa_describe_plugin);
1012 /* initialize dparam support library */
1013 gst_control_init(NULL,NULL);
1022 "All LADSPA plugins",
1026 "(c) 2003 The LADSPA team",
1028 "http://www.ladspa.org/"