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>
25 #include "gstladspa.h"
26 #include <ladspa.h> /* main ladspa sdk include file */
27 #include "utils.h" /* ladspa sdk utility functions */
30 GST_PAD_TEMPLATE_FACTORY (ladspa_sink_factory,
37 "rate", GST_PROPS_INT_RANGE (4000, 96000),
38 "format", GST_PROPS_STRING ("float"),
39 "layout", GST_PROPS_STRING ("gfloat"),
40 "intercept", GST_PROPS_FLOAT(0.0),
41 "slope", GST_PROPS_FLOAT(1.0),
42 "channels", GST_PROPS_INT (1)
46 GST_PAD_TEMPLATE_FACTORY (ladspa_src_factory,
53 "rate", GST_PROPS_INT_RANGE (4000, 96000),
54 "format", GST_PROPS_STRING ("float"),
55 "layout", GST_PROPS_STRING ("gfloat"),
56 "intercept", GST_PROPS_FLOAT (0.0),
57 "slope", GST_PROPS_FLOAT (1.0),
58 "channels", GST_PROPS_INT (1)
62 static GstPadTemplate *srctempl, *sinktempl;
71 static void gst_ladspa_class_init (GstLADSPAClass *klass);
72 static void gst_ladspa_init (GstLADSPA *ladspa);
74 static void gst_ladspa_update_int(const GValue *value, gpointer data);
75 static GstPadConnectReturn gst_ladspa_connect (GstPad *pad, GstCaps *caps);
76 static GstPadConnectReturn gst_ladspa_connect_get (GstPad *pad, GstCaps *caps);
77 static void gst_ladspa_force_src_caps (GstLADSPA *ladspa, GstPad *pad);
79 static void gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
80 static void gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
82 static gboolean gst_ladspa_instantiate (GstLADSPA *ladspa);
83 static void gst_ladspa_activate (GstLADSPA *ladspa);
84 static void gst_ladspa_deactivate (GstLADSPA *ladspa);
86 static GstElementStateReturn gst_ladspa_change_state (GstElement *element);
87 static void gst_ladspa_loop (GstElement *element);
88 static void gst_ladspa_chain (GstPad *pad,GstBuffer *buf);
89 static GstBuffer * gst_ladspa_get (GstPad *pad);
91 static GstElementClass *parent_class = NULL;
92 /* static guint gst_ladspa_signals[LAST_SIGNAL] = { 0 }; */
94 static GstPlugin *ladspa_plugin;
95 static GHashTable *ladspa_descriptors;
98 gst_ladspa_get_bufferpool (GstPad *pad)
102 GstLADSPA *ladspa = (GstLADSPA *) gst_pad_get_parent (pad);
103 GstLADSPAClass *oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
105 if (oclass->numsrcpads > 0)
106 for (i=0;i<oclass->numsrcpads;i++)
107 if ((bp = gst_pad_get_bufferpool(ladspa->srcpads[i])) != NULL)
114 gst_ladspa_class_init (GstLADSPAClass *klass)
116 GObjectClass *gobject_class;
117 GstElementClass *gstelement_class;
118 LADSPA_Descriptor *desc;
119 gint i,current_portnum,sinkcount,srccount,controlcount;
121 gint argtype,argperms;
122 GParamSpec *paramspec = NULL;
123 gchar *argname, *tempstr, *paren;
125 gobject_class = (GObjectClass*)klass;
126 gstelement_class = (GstElementClass*)klass;
128 gobject_class->set_property = gst_ladspa_set_property;
129 gobject_class->get_property = gst_ladspa_get_property;
131 gstelement_class->change_state = gst_ladspa_change_state;
133 /* look up and store the ladspa descriptor */
134 klass->descriptor = g_hash_table_lookup(ladspa_descriptors,GINT_TO_POINTER(G_TYPE_FROM_CLASS(klass)));
135 desc = klass->descriptor;
137 klass->numports = desc->PortCount;
139 klass->numsinkpads = 0;
140 klass->numsrcpads = 0;
141 klass->numcontrols = 0;
143 /* walk through the ports, count the input, output and control ports */
144 for (i=0;i<desc->PortCount;i++) {
145 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
146 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])){
147 klass->numsinkpads++;
150 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
151 LADSPA_IS_PORT_OUTPUT(desc->PortDescriptors[i])){
155 if (LADSPA_IS_PORT_CONTROL(desc->PortDescriptors[i]) &&
156 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])){
157 klass->numcontrols++;
161 klass->srcpad_portnums = g_new0(gint,klass->numsrcpads);
162 klass->sinkpad_portnums = g_new0(gint,klass->numsinkpads);
163 klass->control_portnums = g_new0(gint,klass->numcontrols);
168 /* walk through the ports, note the portnums for srcpads, sinkpads and control
170 for (i=0;i<desc->PortCount;i++) {
171 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
172 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])){
173 GST_DEBUG (0, "input port %d", i);
174 klass->sinkpad_portnums[sinkcount++] = i;
177 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
178 LADSPA_IS_PORT_OUTPUT(desc->PortDescriptors[i])){
179 GST_DEBUG (0, "output port %d", i);
180 klass->srcpad_portnums[srccount++] = i;
183 if (LADSPA_IS_PORT_CONTROL(desc->PortDescriptors[i]) &&
184 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])){
185 GST_DEBUG (0, "control port %d", i);
186 klass->control_portnums[controlcount++] = i;
190 /* no sink pads - we'll use get mode and add params for samplerate and
192 if (klass->numsinkpads == 0 && klass->numsrcpads > 0){
193 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_SAMPLERATE,
194 g_param_spec_int("samplerate","samplerate","samplerate",
195 0,G_MAXINT,44100,G_PARAM_READWRITE));
196 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_BUFFERSIZE,
197 g_param_spec_int("buffersize","buffersize","buffersize",
198 0,G_MAXINT,64,G_PARAM_READWRITE));
202 /* now build the contorl info from the control ports */
203 klass->control_info = g_new0(ladspa_control_info,klass->numcontrols);
205 for (i=0;i<klass->numcontrols;i++) {
206 current_portnum = klass->control_portnums[i];
208 /* short name for hint descriptor */
209 hintdesc = desc->PortRangeHints[current_portnum].HintDescriptor;
211 /* get the various bits */
212 if (LADSPA_IS_HINT_TOGGLED(hintdesc))
213 klass->control_info[i].toggled = TRUE;
214 if (LADSPA_IS_HINT_LOGARITHMIC(hintdesc))
215 klass->control_info[i].logarithmic = TRUE;
216 if (LADSPA_IS_HINT_INTEGER(hintdesc))
217 klass->control_info[i].integer = TRUE;
219 /* figure out the argument details */
220 if (klass->control_info[i].toggled) argtype = G_TYPE_BOOLEAN;
221 else if (klass->control_info[i].integer) argtype = G_TYPE_INT;
222 else argtype = G_TYPE_FLOAT;
224 /* grab the bounds */
225 if (LADSPA_IS_HINT_BOUNDED_BELOW(hintdesc)) {
226 klass->control_info[i].lower = TRUE;
227 klass->control_info[i].lowerbound =
228 desc->PortRangeHints[current_portnum].LowerBound;
230 if (argtype==G_TYPE_INT) klass->control_info[i].lowerbound = (gfloat)G_MININT;
231 if (argtype==G_TYPE_FLOAT) klass->control_info[i].lowerbound = -G_MAXFLOAT;
234 if (LADSPA_IS_HINT_BOUNDED_ABOVE(hintdesc)) {
235 klass->control_info[i].upper = TRUE;
236 klass->control_info[i].upperbound =
237 desc->PortRangeHints[current_portnum].UpperBound;
238 if (LADSPA_IS_HINT_SAMPLE_RATE(hintdesc))
239 klass->control_info[i].samplerate = TRUE;
241 if (argtype==G_TYPE_INT) klass->control_info[i].upperbound = (gfloat)G_MAXINT;
242 if (argtype==G_TYPE_FLOAT) klass->control_info[i].upperbound = G_MAXFLOAT;
245 klass->control_info[i].def = klass->control_info[i].lowerbound;
247 #ifdef LADSPA_IS_HINT_HAS_DEFAULT
248 /* figure out the defaults */
249 if (LADSPA_IS_HINT_HAS_DEFAULT (hintdesc)) {
250 if (LADSPA_IS_HINT_DEFAULT_MINIMUM (hintdesc))
251 klass->control_info[i].def = klass->control_info[i].lowerbound;
252 else if (LADSPA_IS_HINT_DEFAULT_LOW (hintdesc))
253 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
254 klass->control_info[i].def = exp (0.75*log(klass->control_info[i].lowerbound) +
255 0.25*log(klass->control_info[i].upperbound));
257 klass->control_info[i].def = (0.75*klass->control_info[i].lowerbound +
258 0.25*klass->control_info[i].upperbound);
259 else if (LADSPA_IS_HINT_DEFAULT_MIDDLE (hintdesc))
260 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
261 klass->control_info[i].def = exp (0.5*log(klass->control_info[i].lowerbound) +
262 0.5*log(klass->control_info[i].upperbound));
264 klass->control_info[i].def = (0.5*klass->control_info[i].lowerbound +
265 0.5*klass->control_info[i].upperbound);
266 else if (LADSPA_IS_HINT_DEFAULT_HIGH (hintdesc))
267 if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
268 klass->control_info[i].def = exp (0.25*log(klass->control_info[i].lowerbound) +
269 0.75*log(klass->control_info[i].upperbound));
271 klass->control_info[i].def = (0.25*klass->control_info[i].lowerbound +
272 0.75*klass->control_info[i].upperbound);
273 else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM (hintdesc))
274 klass->control_info[i].def = klass->control_info[i].upperbound;
275 else if (LADSPA_IS_HINT_DEFAULT_0 (hintdesc))
276 klass->control_info[i].def = 0.0;
277 else if (LADSPA_IS_HINT_DEFAULT_1 (hintdesc))
278 klass->control_info[i].def = 1.0;
279 else if (LADSPA_IS_HINT_DEFAULT_100 (hintdesc))
280 klass->control_info[i].def = 100.0;
281 else if (LADSPA_IS_HINT_DEFAULT_440 (hintdesc))
282 klass->control_info[i].def = 440.0;
284 #endif /* LADSPA_IS_HINT_HAS_DEFAULT */
286 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[current_portnum])) {
287 argperms = G_PARAM_READWRITE;
288 klass->control_info[i].writable = TRUE;
290 argperms = G_PARAM_READABLE;
291 klass->control_info[i].writable = FALSE;
294 klass->control_info[i].name = g_strdup(desc->PortNames[current_portnum]);
295 argname = g_strdup(klass->control_info[i].name);
296 /* find out if there is a (unitname) at the end of the argname and get rid
298 paren = g_strrstr (argname, " (");
302 /* this is the same thing that param_spec_* will do */
303 g_strcanon (argname, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
304 /* satisfy glib2 (argname[0] must be [A-Za-z]) */
305 if (!((argname[0] >= 'a' && argname[0] <= 'z') || (argname[0] >= 'A' && argname[0] <= 'Z'))) {
307 argname = g_strconcat("param-", argname, NULL);
311 /* check for duplicate property names */
312 if (g_object_class_find_property(G_OBJECT_CLASS(klass), argname) != NULL){
314 gchar *numargname = g_strdup_printf("%s_%d",argname,numarg++);
315 while (g_object_class_find_property(G_OBJECT_CLASS(klass), numargname) != NULL){
317 numargname = g_strdup_printf("%s_%d",argname,numarg++);
319 argname = numargname;
322 klass->control_info[i].param_name = argname;
324 GST_DEBUG (0, "adding arg %s from %s",argname, klass->control_info[i].name);
326 if (argtype==G_TYPE_BOOLEAN){
327 paramspec = g_param_spec_boolean(argname,argname,argname, FALSE, argperms);
328 } else if (argtype==G_TYPE_INT){
329 paramspec = g_param_spec_int(argname,argname,argname,
330 (gint)klass->control_info[i].lowerbound,
331 (gint)klass->control_info[i].upperbound,
332 (gint)klass->control_info[i].def, argperms);
333 } else if (klass->control_info[i].samplerate){
334 paramspec = g_param_spec_float(argname,argname,argname,
338 paramspec = g_param_spec_float(argname,argname,argname,
339 klass->control_info[i].lowerbound, klass->control_info[i].upperbound,
340 klass->control_info[i].def, argperms);
343 g_object_class_install_property(G_OBJECT_CLASS(klass), i+ARG_LAST, paramspec);
348 gst_ladspa_init (GstLADSPA *ladspa)
350 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
351 ladspa_control_info cinfo;
353 LADSPA_Descriptor *desc;
354 gint i,sinkcount,srccount,controlcount;
356 desc = oclass->descriptor;
357 ladspa->descriptor = oclass->descriptor;
359 /* allocate the various arrays */
360 ladspa->srcpads = g_new0(GstPad*,oclass->numsrcpads);
361 ladspa->sinkpads = g_new0(GstPad*,oclass->numsinkpads);
362 ladspa->controls = g_new(gfloat,oclass->numcontrols);
363 ladspa->dpman = gst_dpman_new ("ladspa_dpman", GST_ELEMENT(ladspa));
365 /* walk through the ports and add all the pads */
369 for (i=0;i<desc->PortCount;i++) {
371 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i])){
372 gchar *canon_port_name = g_strdup((gchar *)desc->PortNames[i]);
373 g_strcanon (canon_port_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
374 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
375 ladspa->sinkpads[sinkcount] = gst_pad_new_from_template (sinktempl, canon_port_name);
376 gst_element_add_pad(GST_ELEMENT(ladspa),ladspa->sinkpads[sinkcount]);
379 if (LADSPA_IS_PORT_OUTPUT(desc->PortDescriptors[i])) {
380 ladspa->srcpads[srccount] = gst_pad_new_from_template (srctempl, canon_port_name);
381 gst_element_add_pad(GST_ELEMENT(ladspa),ladspa->srcpads[srccount]);
385 if (LADSPA_IS_PORT_CONTROL(desc->PortDescriptors[i]) &&
386 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
387 cinfo = oclass->control_info[controlcount];
388 /* use the lowerbound as the default value if it exists */
389 ladspa->controls[controlcount]=cinfo.def;
391 /* set up dparams for this instance */
393 gst_dpman_add_required_dparam_callback (
395 g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
396 0, 1, (gint)(ladspa->controls[controlcount]), G_PARAM_READWRITE),
397 "int", gst_ladspa_update_int, &(ladspa->controls[controlcount])
400 else if (cinfo.integer){
401 gst_dpman_add_required_dparam_callback (
403 g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
404 (gint)cinfo.lowerbound, (gint)cinfo.upperbound,
405 (gint)ladspa->controls[controlcount], G_PARAM_READWRITE),
406 "int", gst_ladspa_update_int, &(ladspa->controls[controlcount])
409 else if (cinfo.samplerate){
410 gst_dpman_add_required_dparam_direct (
412 g_param_spec_float(cinfo.param_name, cinfo.name, cinfo.name,
413 cinfo.lowerbound, cinfo.upperbound,
414 ladspa->controls[controlcount], G_PARAM_READWRITE),
415 "hertz-rate-bound", &(ladspa->controls[controlcount])
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[controlcount], G_PARAM_READWRITE),
424 "float", &(ladspa->controls[controlcount])
432 /* nonzero default needed to instantiate() some plugins */
433 ladspa->samplerate = 44100;
435 ladspa->buffersize = 64;
436 ladspa->numbuffers = 16;
437 ladspa->activated = FALSE;
438 ladspa->bufpool = NULL;
439 ladspa->inplace_broken = LADSPA_IS_INPLACE_BROKEN(ladspa->descriptor->Properties);
441 if (sinkcount==0 && srccount == 1) {
442 /* get mode (no sink pads) */
443 GST_DEBUG (0, "mono get mode with 1 src pad");
445 gst_pad_set_connect_function (ladspa->srcpads[0], gst_ladspa_connect_get);
446 gst_pad_set_get_function (ladspa->srcpads[0], gst_ladspa_get);
447 } else if (sinkcount==1){
448 /* with one sink we can use the chain function */
449 GST_DEBUG (0, "chain mode");
451 gst_pad_set_connect_function (ladspa->sinkpads[0], gst_ladspa_connect);
452 gst_pad_set_chain_function (ladspa->sinkpads[0], gst_ladspa_chain);
453 gst_pad_set_bufferpool_function (ladspa->sinkpads[0], gst_ladspa_get_bufferpool);
454 } else if (sinkcount > 1){
455 /* more than one sink pad needs loop mode */
456 GST_DEBUG (0, "loop mode with %d sink pads and %d src pads", sinkcount, srccount);
458 for (i=0;i<sinkcount;i++) {
459 gst_pad_set_connect_function (ladspa->sinkpads[i], gst_ladspa_connect);
460 gst_pad_set_bufferpool_function (ladspa->sinkpads[i], gst_ladspa_get_bufferpool);
462 gst_element_set_loop_function (GST_ELEMENT (ladspa), gst_ladspa_loop);
464 else if (sinkcount==0 && srccount == 0){
465 /* for some reason these plugins exist - we'll just ignore them */
467 g_warning("%d sink pads, %d src pads not yet supported", sinkcount, srccount);
470 gst_ladspa_instantiate (ladspa);
474 gst_ladspa_update_int(const GValue *value, gpointer data)
476 gfloat *target = (gfloat*) data;
477 *target = (gfloat)g_value_get_int(value);
480 static GstPadConnectReturn
481 gst_ladspa_connect (GstPad *pad, GstCaps *caps)
483 GstLADSPA *ladspa = (GstLADSPA *) GST_PAD_PARENT (pad);
484 GstLADSPAClass *oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
488 g_return_val_if_fail (caps != NULL, GST_PAD_CONNECT_DELAYED);
489 g_return_val_if_fail (pad != NULL, GST_PAD_CONNECT_DELAYED);
491 if (gst_caps_get_int (caps, "rate", &rate)){
492 /* have to instantiate ladspa plugin when samplerate changes (groan) */
493 if (ladspa->samplerate != rate){
494 ladspa->samplerate = rate;
496 if (! gst_ladspa_instantiate(ladspa))
497 return GST_PAD_CONNECT_REFUSED;
501 /* if the caps are fixed, we are going to try to set all srcpads using this
502 one caps object. if any of the pads barfs, we'll refuse the connection. i'm
503 not sure if this is correct. */
504 if (GST_CAPS_IS_FIXED (caps)) {
505 for (i=0;i<oclass->numsrcpads;i++) {
506 if (gst_pad_try_set_caps (ladspa->srcpads[i], caps) <= 0)
507 return GST_PAD_CONNECT_REFUSED;
511 return GST_PAD_CONNECT_OK;
514 static GstPadConnectReturn
515 gst_ladspa_connect_get (GstPad *pad, GstCaps *caps)
517 GstLADSPA *ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
520 g_return_val_if_fail (caps != NULL, GST_PAD_CONNECT_DELAYED);
521 g_return_val_if_fail (pad != NULL, GST_PAD_CONNECT_DELAYED);
523 if (gst_caps_get_int (caps, "rate", &rate)){
524 if (ladspa->samplerate != rate) {
525 ladspa->samplerate = rate;
526 if (! gst_ladspa_instantiate(ladspa))
527 return GST_PAD_CONNECT_REFUSED;
531 return GST_PAD_CONNECT_OK;
535 gst_ladspa_force_src_caps(GstLADSPA *ladspa, GstPad *pad)
537 GST_DEBUG (0, "forcing caps with rate %d", ladspa->samplerate);
538 gst_pad_try_set_caps (pad, gst_caps_new (
542 "format", GST_PROPS_STRING ("float"),
543 "layout", GST_PROPS_STRING ("gfloat"),
544 "intercept", GST_PROPS_FLOAT(0.0),
545 "slope", GST_PROPS_FLOAT(1.0),
546 "rate", GST_PROPS_INT (ladspa->samplerate),
547 "channels", GST_PROPS_INT (1),
551 ladspa->newcaps=FALSE;
555 gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
557 GstLADSPA *ladspa = (GstLADSPA*)object;
558 gint cid = prop_id - ARG_LAST;
559 GstLADSPAClass *oclass;
560 ladspa_control_info *control_info;
563 /* these are only registered in get mode */
566 ladspa->samplerate = g_value_get_int (value);
567 ladspa->newcaps=TRUE;
568 gst_ladspa_instantiate (ladspa);
571 ladspa->buffersize = g_value_get_int (value);
575 /* is it a ladspa plugin arg? */
578 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
580 /* verify it exists and is a control (not a port) */
581 g_return_if_fail(cid < oclass->numcontrols);
583 control_info = &(oclass->control_info[cid]);
584 g_return_if_fail (control_info->name != NULL);
586 /* check to see if it's writable */
587 g_return_if_fail (control_info->writable);
589 /* now see what type it is */
590 if (control_info->toggled) {
591 if (g_value_get_boolean (value))
592 ladspa->controls[cid] = 1.0;
594 ladspa->controls[cid] = 0.0;
595 } else if (control_info->integer) {
596 val = (gfloat)g_value_get_int (value);
597 ladspa->controls[cid] = val;
599 val = g_value_get_float (value);
600 ladspa->controls[cid] = val;
603 GST_DEBUG (0, "set arg %s to %f", control_info->name, ladspa->controls[cid]);
607 gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
609 GstLADSPA *ladspa = (GstLADSPA*)object;
610 gint cid = prop_id - ARG_LAST;
611 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
612 ladspa_control_info *control_info;
614 /* these are only registered in get mode */
617 g_value_set_int (value, ladspa->samplerate);
620 g_value_set_int (value, ladspa->buffersize);
626 /* verify it exists and is a control (not a port) */
627 if (cid >= oclass->numcontrols) return;
628 control_info = &(oclass->control_info[cid]);
629 if (control_info->name == NULL) return;
631 GST_DEBUG (0, "got arg %s as %f", control_info->name, ladspa->controls[cid]);
633 /* now see what type it is */
634 if (control_info->toggled) {
635 if (ladspa->controls[cid] == 1.0)
636 g_value_set_boolean (value, TRUE);
638 g_value_set_boolean (value, FALSE);
639 } else if (control_info->integer) {
640 g_value_set_int (value, (gint)ladspa->controls[cid]);
642 g_value_set_float (value, ladspa->controls[cid]);
647 gst_ladspa_instantiate (GstLADSPA *ladspa)
649 LADSPA_Descriptor *desc;
651 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
652 gboolean was_activated;
654 desc = ladspa->descriptor;
656 /* check for old handle */
657 was_activated = ladspa->activated;
658 if (ladspa->handle != NULL){
659 gst_ladspa_deactivate(ladspa);
660 desc->cleanup(ladspa->handle);
663 /* instantiate the plugin */
664 GST_DEBUG (0, "instantiating the plugin");
666 ladspa->handle = desc->instantiate(desc,ladspa->samplerate);
667 g_return_val_if_fail (ladspa->handle != NULL, FALSE);
669 /* walk through the ports and add all the arguments */
670 for (i=0;i<oclass->numcontrols;i++) {
671 /* connect the argument to the plugin */
672 GST_DEBUG (0, "added control port %d", oclass->control_portnums[i]);
673 desc->connect_port(ladspa->handle,
674 oclass->control_portnums[i],
675 &(ladspa->controls[i]));
678 /* reactivate if it was activated before the reinstantiation */
680 gst_ladspa_activate(ladspa);
685 static GstElementStateReturn
686 gst_ladspa_change_state (GstElement *element)
688 LADSPA_Descriptor *desc;
689 GstLADSPA *ladspa = (GstLADSPA*)element;
690 desc = ladspa->descriptor;
692 GST_DEBUG (0, "changing state");
693 switch (GST_STATE_TRANSITION (element)) {
694 case GST_STATE_NULL_TO_READY:
695 gst_ladspa_activate(ladspa);
697 case GST_STATE_READY_TO_NULL:
698 gst_ladspa_deactivate(ladspa);
704 if (GST_ELEMENT_CLASS (parent_class)->change_state)
705 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
707 return GST_STATE_SUCCESS;
711 gst_ladspa_activate(GstLADSPA *ladspa)
713 LADSPA_Descriptor *desc;
714 desc = ladspa->descriptor;
716 if (ladspa->activated){
717 gst_ladspa_deactivate(ladspa);
720 GST_DEBUG (0, "activating");
722 /* activate the plugin (function might be null) */
723 if (desc->activate != NULL) {
724 desc->activate(ladspa->handle);
727 ladspa->activated = TRUE;
731 gst_ladspa_deactivate(GstLADSPA *ladspa)
733 LADSPA_Descriptor *desc;
734 desc = ladspa->descriptor;
736 GST_DEBUG (0, "deactivating");
738 /* deactivate the plugin (function might be null) */
739 if (ladspa->activated && (desc->deactivate != NULL)) {
740 desc->deactivate(ladspa->handle);
743 ladspa->activated = FALSE;
747 gst_ladspa_loop(GstElement *element)
749 guint bufferbytesize, i, numsrcpads, numsinkpads, num_empty_pads;
750 guint num_processed, num_to_process;
751 GstEvent *event = NULL;
754 LADSPA_Data **data_in, **data_out;
755 GstBuffer **buffers_in, **buffers_out;
756 GstBufferPool *bufpool;
757 GstByteStream **bytestreams;
759 GstLADSPA *ladspa = (GstLADSPA *)element;
760 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
761 LADSPA_Descriptor *desc = ladspa->descriptor;
763 numsinkpads = oclass->numsinkpads;
764 numsrcpads = oclass->numsrcpads;
766 data_in = g_new0(LADSPA_Data*, numsinkpads);
767 data_out = g_new0(LADSPA_Data*, numsrcpads);
768 buffers_in = g_new0(GstBuffer*, numsinkpads);
769 buffers_out = g_new0(GstBuffer*, numsrcpads);
770 bytestreams = g_new0(GstByteStream*, numsinkpads);
772 /* find a bufferpool */
773 if (numsrcpads > 0 && (bufpool = gst_pad_get_bufferpool (ladspa->srcpads[0]))) {
774 GST_DEBUG (0, "Got bufferpool from first source pad");
776 bufferbytesize = sizeof (LADSPA_Data) * ladspa->buffersize;
777 bufpool = gst_buffer_pool_get_default (bufferbytesize, ladspa->numbuffers);
778 GST_DEBUG (0, "Created default bufferpool, %d x %d bytes", ladspa->numbuffers, bufferbytesize);
781 /* get the bytestreams for each pad */
782 for (i=0 ; i<numsinkpads ; i++){
783 bytestreams[i] = gst_bytestream_new (ladspa->sinkpads[i]);
786 /* since this is a loop element, we just loop here til things fall apart. */
788 /* we need to get the buffers_out first so we can know how many bytes to process */
789 for (i=0 ; i<numsrcpads ; i++){
790 buffers_out[i] = gst_buffer_new_from_pool (bufpool, 0, 0);
791 GST_BUFFER_TIMESTAMP(buffers_out[i]) = ladspa->timestamp;
792 data_out[i] = (LADSPA_Data *) GST_BUFFER_DATA(buffers_out[i]);
795 bufferbytesize = GST_BUFFER_SIZE (buffers_out[0]);
796 ladspa->buffersize = bufferbytesize / sizeof (LADSPA_Data);
799 /* first get all the necessary data from the input ports */
800 for (i=0 ; i<numsinkpads ; i++){
801 GST_DEBUG (0, "pulling %u bytes through channel %d's bytestream", bufferbytesize, i);
802 got_bytes = gst_bytestream_read (bytestreams[i], buffers_in + i, bufferbytesize);
804 if (got_bytes != bufferbytesize) {
805 /* we need to check for an event. */
806 gst_bytestream_get_status (bytestreams[i], &waiting, &event);
808 if (event && GST_EVENT_TYPE(event) == GST_EVENT_EOS) {
809 /* if we get an EOS event from one of our sink pads, we assume that
810 pad's finished handling data. delete the bytestream, free up the
811 pad, and free up the memory associated with the input channel. */
812 GST_DEBUG (0, "got an EOS event on sinkpad %d", i);
814 /* CHECKME should maybe check for other events and try to pull more data here */
817 data_in[i] = (LADSPA_Data *) GST_BUFFER_DATA(buffers_in[i]);
818 GST_BUFFER_TIMESTAMP(buffers_in[i]) = ladspa->timestamp;
822 if (num_empty_pads > 0){
823 if (num_empty_pads < numsinkpads){
824 /* only some pads have EOS, need to create some empty buffers */
825 for (i=0 ; i<numsinkpads ; i++){
826 if (buffers_in[i] == NULL){
829 buffers_in[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
830 GST_BUFFER_TIMESTAMP(buffers_in[i]) = ladspa->timestamp;
831 data_in[i] = data = (LADSPA_Data *) GST_BUFFER_DATA(buffers_in[i]);
832 for (x=0 ; x < ladspa->buffersize ; x++)
835 data_in[i] = (LADSPA_Data *) GST_BUFFER_DATA(buffers_in[i]);
836 GST_BUFFER_TIMESTAMP(buffers_in[i]) = ladspa->timestamp;
841 /* all pads have EOS, time to quit */
842 /* CHECKME do I have to push EOS events here? */
843 GST_DEBUG (0, "All sink pads have EOS, finishing.");
848 GST_DPMAN_PREPROCESS(ladspa->dpman, ladspa->buffersize, ladspa->timestamp);
851 /* split up processing of the buffer into chunks so that dparams can
852 * be updated when required.
853 * In many cases the buffer will be processed in one chunk anyway.
855 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
857 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
858 for (i=0 ; i<numsinkpads ; i++){
859 desc->connect_port (ladspa->handle, oclass->sinkpad_portnums[i], data_in[i]);
861 for (i=0 ; i<numsrcpads ; i++){
862 desc->connect_port (ladspa->handle, oclass->srcpad_portnums[i], data_out[i]);
864 desc->run(ladspa->handle, num_to_process);
865 for (i=0 ; i<numsinkpads ; i++){
866 data_in[i] += num_to_process;
868 for (i=0 ; i<numsrcpads ; i++){
869 data_out[i] += num_to_process;
872 num_processed += num_to_process;
875 for (i=0 ; i<numsrcpads ; i++) {
876 GST_DEBUG (0, "pushing buffer (%p) on src pad %d", buffers_out[i], i);
877 gst_pad_push (ladspa->srcpads[i], buffers_out[i]);
880 buffers_out[i] = NULL;
882 for (i=0 ; i<numsinkpads ; i++) {
883 gst_buffer_unref(buffers_in[i]);
885 buffers_in[i] = NULL;
888 ladspa->timestamp += ladspa->buffersize * 10^9 / ladspa->samplerate;
890 gst_element_yield (element);
893 gst_buffer_pool_unref(bufpool);
895 for (i=0 ; i<numsinkpads ; i++){
896 gst_bytestream_destroy (bytestreams[i]);
899 g_free (buffers_out);
903 g_free (bytestreams);
907 gst_ladspa_chain (GstPad *pad, GstBuffer *buf)
909 LADSPA_Descriptor *desc;
910 LADSPA_Data *data_in, **data_out = NULL;
911 GstBuffer **buffers_out = NULL;
913 unsigned long num_samples;
914 guint num_to_process, num_processed, i, numsrcpads;
917 GstLADSPAClass *oclass;
919 g_return_if_fail(pad != NULL);
920 g_return_if_fail(GST_IS_PAD(pad));
921 g_return_if_fail(buf != NULL);
923 ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
924 g_return_if_fail(ladspa != NULL);
926 /* this might happen if caps nego hasn't happened */
927 g_return_if_fail(ladspa->handle != NULL);
929 oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
931 if (GST_IS_EVENT (buf)) {
932 /* push the event out all the src pads */
933 for (i=0 ; i<oclass->numsrcpads ; i++){
934 gst_pad_push (ladspa->srcpads[0], buf);
939 data_in = (LADSPA_Data *) GST_BUFFER_DATA(buf);
940 num_samples = GST_BUFFER_SIZE(buf) / sizeof(gfloat);
941 numsrcpads = oclass->numsrcpads;
943 desc = ladspa->descriptor;
946 guint num_created_buffers = 0;
947 buffers_out = g_new(GstBuffer*, numsrcpads);
948 data_out = g_new(LADSPA_Data*, numsrcpads);
950 if (ladspa->inplace_broken){
951 num_created_buffers = numsrcpads;
954 /* we can share the buffer for input and output */
955 buffers_out[0] = buf;
956 data_out[0] = (LADSPA_Data *)GST_BUFFER_DATA(buf);
957 num_created_buffers = numsrcpads - 1;
960 if (num_created_buffers > 0){
961 ladspa->bufpool = gst_buffer_pool_get_default (sizeof (LADSPA_Data) * GST_BUFFER_SIZE(buf), ladspa->numbuffers);
963 for (i = numsrcpads - num_created_buffers ; i<numsrcpads ; i++){
964 buffers_out[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
965 GST_BUFFER_TIMESTAMP(buffers_out[i]) = GST_BUFFER_TIMESTAMP(buf);
966 data_out[i] = (LADSPA_Data *) GST_BUFFER_DATA(buffers_out[i]);
971 GST_DPMAN_PREPROCESS(ladspa->dpman, num_samples, GST_BUFFER_TIMESTAMP(buf));
974 /* split up processing of the buffer into chunks so that dparams can
975 * be updated when required.
976 * In many cases the buffer will be processed in one chunk anyway.
978 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
979 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
981 desc->connect_port(ladspa->handle,oclass->sinkpad_portnums[0],data_in);
982 for (i=0 ; i<numsrcpads ; i++){
983 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[i],data_out[i]);
985 desc->run(ladspa->handle, num_to_process);
987 data_in += num_to_process;
988 for (i=0 ; i<numsrcpads ; i++){
989 data_out[i] += num_to_process;
991 num_processed += num_to_process;
995 for (i=0 ; i<numsrcpads ; i++){
996 gst_pad_push (ladspa->srcpads[i], buffers_out[i]);
1003 /* if we have reached here, there are no src pads */
1004 gst_buffer_unref(buf);
1008 gst_ladspa_get(GstPad *pad)
1011 GstLADSPAClass *oclass;
1015 LADSPA_Descriptor *desc;
1017 guint num_to_process, num_processed;
1019 g_return_val_if_fail(pad != NULL, NULL);
1020 g_return_val_if_fail(GST_IS_PAD(pad), NULL);
1022 ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
1023 g_return_val_if_fail(ladspa != NULL, NULL);
1025 /* this might happen if caps nego hasn't happened */
1026 g_return_val_if_fail(ladspa->handle != NULL, NULL);
1028 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
1030 /* force src pad to set caps */
1031 if (ladspa->newcaps) {
1032 gst_ladspa_force_src_caps(ladspa, ladspa->srcpads[0]);
1035 /* get a bufferpool */
1036 if (ladspa->bufpool == NULL) {
1037 ladspa->bufpool = gst_pad_get_bufferpool (ladspa->srcpads[0]);
1038 if (ladspa->bufpool == NULL) {
1039 ladspa->bufpool = gst_buffer_pool_get_default (sizeof (LADSPA_Data) * ladspa->buffersize, ladspa->numbuffers);
1043 buf = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
1044 GST_BUFFER_TIMESTAMP(buf) = ladspa->timestamp;
1045 data = (LADSPA_Data *) GST_BUFFER_DATA(buf);
1047 desc = ladspa->descriptor;
1048 GST_DPMAN_PREPROCESS(ladspa->dpman, ladspa->buffersize, ladspa->timestamp);
1051 /* split up processing of the buffer into chunks so that dparams can
1052 * be updated when required.
1053 * In many cases the buffer will be processed in one chunk anyway.
1055 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
1056 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
1058 /* update timestamp */
1059 ladspa->timestamp += num_to_process * GST_SECOND / ladspa->samplerate;
1061 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);
1062 desc->run(ladspa->handle, num_to_process);
1064 data += num_to_process;
1065 num_processed = num_to_process;
1072 ladspa_describe_plugin(const char *pcFullFilename,
1073 void *pvPluginHandle,
1074 LADSPA_Descriptor_Function pfDescriptorFunction)
1076 const LADSPA_Descriptor *desc;
1079 GstElementDetails *details;
1080 GTypeInfo typeinfo = {
1081 sizeof(GstLADSPAClass),
1084 (GClassInitFunc)gst_ladspa_class_init,
1089 (GInstanceInitFunc)gst_ladspa_init,
1092 GstElementFactory *factory;
1094 /* walk through all the plugins in this pluginlibrary */
1096 while ((desc = pfDescriptorFunction(i++))) {
1099 /* construct the type */
1100 type_name = g_strdup_printf("ladspa_%s",desc->Label);
1101 g_strcanon (type_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_+", '-');
1102 /* if it's already registered, drop it */
1103 if (g_type_from_name(type_name)) {
1107 /* create the type now */
1108 type = g_type_register_static(GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
1110 /* construct the element details struct */
1111 details = g_new0(GstElementDetails,1);
1112 details->longname = g_strdup(desc->Name);
1113 details->klass = "Filter/Audio/LADSPA";
1114 details->description = details->longname;
1115 details->version = g_strdup_printf("%ld",desc->UniqueID);
1116 details->author = g_strdup(desc->Maker);
1117 details->copyright = g_strdup(desc->Copyright);
1119 /* register the plugin with gstreamer */
1120 factory = gst_element_factory_new(type_name,type,details);
1121 g_return_if_fail(factory != NULL);
1122 gst_plugin_add_feature (ladspa_plugin, GST_PLUGIN_FEATURE (factory));
1124 /* add this plugin to the hash */
1125 g_hash_table_insert(ladspa_descriptors,
1126 GINT_TO_POINTER(type),
1130 /* only add sink padtemplate if there are sinkpads */
1131 for (j=0;j<desc->PortCount;j++) {
1132 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j]) &&
1133 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j])) {
1134 sinktempl = ladspa_sink_factory();
1135 gst_element_factory_add_pad_template (factory, sinktempl);
1140 /* only add src padtemplate if there are srcpads */
1141 for (j=0;j<desc->PortCount;j++) {
1142 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j]) &&
1143 LADSPA_IS_PORT_OUTPUT(desc->PortDescriptors[j])) {
1144 srctempl = ladspa_src_factory();
1145 gst_element_factory_add_pad_template (factory, srctempl);
1154 plugin_init (GModule *module, GstPlugin *plugin)
1156 ladspa_descriptors = g_hash_table_new(NULL,NULL);
1157 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
1159 ladspa_plugin = plugin;
1161 LADSPAPluginSearch(ladspa_describe_plugin);
1163 if (! gst_library_load ("gstbytestream"))
1166 /* initialize dparam support library */
1167 gst_control_init(NULL,NULL);
1172 GstPluginDesc plugin_desc = {