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 */
285 if(klass->control_info[i].def < klass->control_info[i].lowerbound)
286 klass->control_info[i].def = klass->control_info[i].lowerbound;
287 if(klass->control_info[i].def > klass->control_info[i].upperbound)
288 klass->control_info[i].def = klass->control_info[i].upperbound;
290 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[current_portnum])) {
291 argperms = G_PARAM_READWRITE;
292 klass->control_info[i].writable = TRUE;
294 argperms = G_PARAM_READABLE;
295 klass->control_info[i].writable = FALSE;
298 klass->control_info[i].name = g_strdup(desc->PortNames[current_portnum]);
299 argname = g_strdup(klass->control_info[i].name);
300 /* find out if there is a (unitname) at the end of the argname and get rid
302 paren = g_strrstr (argname, " (");
306 /* this is the same thing that param_spec_* will do */
307 g_strcanon (argname, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
308 /* satisfy glib2 (argname[0] must be [A-Za-z]) */
309 if (!((argname[0] >= 'a' && argname[0] <= 'z') || (argname[0] >= 'A' && argname[0] <= 'Z'))) {
311 argname = g_strconcat("param-", argname, NULL);
315 /* check for duplicate property names */
316 if (g_object_class_find_property(G_OBJECT_CLASS(klass), argname) != NULL){
318 gchar *numargname = g_strdup_printf("%s_%d",argname,numarg++);
319 while (g_object_class_find_property(G_OBJECT_CLASS(klass), numargname) != NULL){
321 numargname = g_strdup_printf("%s_%d",argname,numarg++);
323 argname = numargname;
326 klass->control_info[i].param_name = argname;
328 GST_DEBUG (0, "adding arg %s from %s",argname, klass->control_info[i].name);
330 if (argtype==G_TYPE_BOOLEAN){
331 paramspec = g_param_spec_boolean(argname,argname,argname, FALSE, argperms);
332 } else if (argtype==G_TYPE_INT){
333 paramspec = g_param_spec_int(argname,argname,argname,
334 (gint)klass->control_info[i].lowerbound,
335 (gint)klass->control_info[i].upperbound,
336 (gint)klass->control_info[i].def, argperms);
337 } else if (klass->control_info[i].samplerate){
338 paramspec = g_param_spec_float(argname,argname,argname,
342 paramspec = g_param_spec_float(argname,argname,argname,
343 klass->control_info[i].lowerbound, klass->control_info[i].upperbound,
344 klass->control_info[i].def, argperms);
347 g_object_class_install_property(G_OBJECT_CLASS(klass), i+ARG_LAST, paramspec);
352 gst_ladspa_init (GstLADSPA *ladspa)
354 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
355 ladspa_control_info cinfo;
357 LADSPA_Descriptor *desc;
358 gint i,sinkcount,srccount,controlcount;
360 desc = oclass->descriptor;
361 ladspa->descriptor = oclass->descriptor;
363 /* allocate the various arrays */
364 ladspa->srcpads = g_new0(GstPad*,oclass->numsrcpads);
365 ladspa->sinkpads = g_new0(GstPad*,oclass->numsinkpads);
366 ladspa->controls = g_new(gfloat,oclass->numcontrols);
367 ladspa->dpman = gst_dpman_new ("ladspa_dpman", GST_ELEMENT(ladspa));
369 /* walk through the ports and add all the pads */
373 for (i=0;i<desc->PortCount;i++) {
375 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i])){
376 gchar *canon_port_name = g_strdup((gchar *)desc->PortNames[i]);
377 g_strcanon (canon_port_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
378 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
379 ladspa->sinkpads[sinkcount] = gst_pad_new_from_template (sinktempl, canon_port_name);
380 gst_element_add_pad(GST_ELEMENT(ladspa),ladspa->sinkpads[sinkcount]);
383 if (LADSPA_IS_PORT_OUTPUT(desc->PortDescriptors[i])) {
384 ladspa->srcpads[srccount] = gst_pad_new_from_template (srctempl, canon_port_name);
385 gst_element_add_pad(GST_ELEMENT(ladspa),ladspa->srcpads[srccount]);
389 if (LADSPA_IS_PORT_CONTROL(desc->PortDescriptors[i]) &&
390 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
391 cinfo = oclass->control_info[controlcount];
392 /* use the lowerbound as the default value if it exists */
393 ladspa->controls[controlcount]=cinfo.def;
395 /* set up dparams for this instance */
397 gst_dpman_add_required_dparam_callback (
399 g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
400 0, 1, (gint)(ladspa->controls[controlcount]), G_PARAM_READWRITE),
401 "int", gst_ladspa_update_int, &(ladspa->controls[controlcount])
404 else if (cinfo.integer){
405 gst_dpman_add_required_dparam_callback (
407 g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
408 (gint)cinfo.lowerbound, (gint)cinfo.upperbound,
409 (gint)ladspa->controls[controlcount], G_PARAM_READWRITE),
410 "int", gst_ladspa_update_int, &(ladspa->controls[controlcount])
413 else if (cinfo.samplerate){
414 gst_dpman_add_required_dparam_direct (
416 g_param_spec_float(cinfo.param_name, cinfo.name, cinfo.name,
417 cinfo.lowerbound, cinfo.upperbound,
418 ladspa->controls[controlcount], G_PARAM_READWRITE),
419 "hertz-rate-bound", &(ladspa->controls[controlcount])
423 gst_dpman_add_required_dparam_direct (
425 g_param_spec_float(cinfo.param_name, cinfo.name, cinfo.name,
426 cinfo.lowerbound, cinfo.upperbound,
427 ladspa->controls[controlcount], G_PARAM_READWRITE),
428 "float", &(ladspa->controls[controlcount])
436 /* nonzero default needed to instantiate() some plugins */
437 ladspa->samplerate = 44100;
439 ladspa->buffersize = 64;
440 ladspa->numbuffers = 16;
441 ladspa->activated = FALSE;
442 ladspa->bufpool = NULL;
443 ladspa->inplace_broken = LADSPA_IS_INPLACE_BROKEN(ladspa->descriptor->Properties);
445 if (sinkcount==0 && srccount == 1) {
446 /* get mode (no sink pads) */
447 GST_DEBUG (0, "mono get mode with 1 src pad");
449 ladspa->newcaps = TRUE;
451 gst_pad_set_connect_function (ladspa->srcpads[0], gst_ladspa_connect_get);
452 gst_pad_set_get_function (ladspa->srcpads[0], gst_ladspa_get);
453 } else if (sinkcount==1){
454 /* with one sink we can use the chain function */
455 GST_DEBUG (0, "chain mode");
457 gst_pad_set_connect_function (ladspa->sinkpads[0], gst_ladspa_connect);
458 gst_pad_set_chain_function (ladspa->sinkpads[0], gst_ladspa_chain);
459 gst_pad_set_bufferpool_function (ladspa->sinkpads[0], gst_ladspa_get_bufferpool);
460 } else if (sinkcount > 1){
461 /* more than one sink pad needs loop mode */
462 GST_DEBUG (0, "loop mode with %d sink pads and %d src pads", sinkcount, srccount);
464 for (i=0;i<sinkcount;i++) {
465 gst_pad_set_connect_function (ladspa->sinkpads[i], gst_ladspa_connect);
466 gst_pad_set_bufferpool_function (ladspa->sinkpads[i], gst_ladspa_get_bufferpool);
468 gst_element_set_loop_function (GST_ELEMENT (ladspa), gst_ladspa_loop);
470 else if (sinkcount==0 && srccount == 0){
471 /* for some reason these plugins exist - we'll just ignore them */
473 g_warning("%d sink pads, %d src pads not yet supported", sinkcount, srccount);
476 gst_ladspa_instantiate (ladspa);
480 gst_ladspa_update_int(const GValue *value, gpointer data)
482 gfloat *target = (gfloat*) data;
483 *target = (gfloat)g_value_get_int(value);
486 static GstPadConnectReturn
487 gst_ladspa_connect (GstPad *pad, GstCaps *caps)
489 GstLADSPA *ladspa = (GstLADSPA *) GST_PAD_PARENT (pad);
490 GstLADSPAClass *oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
494 g_return_val_if_fail (caps != NULL, GST_PAD_CONNECT_DELAYED);
495 g_return_val_if_fail (pad != NULL, GST_PAD_CONNECT_DELAYED);
497 if (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;
502 if (! gst_ladspa_instantiate(ladspa))
503 return GST_PAD_CONNECT_REFUSED;
507 /* if the caps are fixed, we are going to try to set all srcpads using this
508 one caps object. if any of the pads barfs, we'll refuse the connection. i'm
509 not sure if this is correct. */
510 if (GST_CAPS_IS_FIXED (caps)) {
511 for (i=0;i<oclass->numsrcpads;i++) {
512 if (gst_pad_try_set_caps (ladspa->srcpads[i], caps) <= 0)
513 return GST_PAD_CONNECT_REFUSED;
517 return GST_PAD_CONNECT_OK;
520 static GstPadConnectReturn
521 gst_ladspa_connect_get (GstPad *pad, GstCaps *caps)
523 GstLADSPA *ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
526 g_return_val_if_fail (caps != NULL, GST_PAD_CONNECT_DELAYED);
527 g_return_val_if_fail (pad != NULL, GST_PAD_CONNECT_DELAYED);
529 if (gst_caps_get_int (caps, "rate", &rate)){
530 if (ladspa->samplerate != rate) {
531 ladspa->samplerate = rate;
532 if (! gst_ladspa_instantiate(ladspa))
533 return GST_PAD_CONNECT_REFUSED;
537 return GST_PAD_CONNECT_OK;
541 gst_ladspa_force_src_caps(GstLADSPA *ladspa, GstPad *pad)
543 GST_DEBUG (0, "forcing caps with rate %d", ladspa->samplerate);
544 gst_pad_try_set_caps (pad, gst_caps_new (
548 "format", GST_PROPS_STRING ("float"),
549 "layout", GST_PROPS_STRING ("gfloat"),
550 "intercept", GST_PROPS_FLOAT(0.0),
551 "slope", GST_PROPS_FLOAT(1.0),
552 "rate", GST_PROPS_INT (ladspa->samplerate),
553 "channels", GST_PROPS_INT (1),
557 ladspa->newcaps=FALSE;
561 gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
563 GstLADSPA *ladspa = (GstLADSPA*)object;
564 gint cid = prop_id - ARG_LAST;
565 GstLADSPAClass *oclass;
566 ladspa_control_info *control_info;
569 /* these are only registered in get mode */
572 ladspa->samplerate = g_value_get_int (value);
573 ladspa->newcaps=TRUE;
574 gst_ladspa_instantiate (ladspa);
577 ladspa->buffersize = g_value_get_int (value);
581 /* is it a ladspa plugin arg? */
584 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
586 /* verify it exists and is a control (not a port) */
587 g_return_if_fail(cid < oclass->numcontrols);
589 control_info = &(oclass->control_info[cid]);
590 g_return_if_fail (control_info->name != NULL);
592 /* check to see if it's writable */
593 g_return_if_fail (control_info->writable);
595 /* now see what type it is */
596 if (control_info->toggled) {
597 if (g_value_get_boolean (value))
598 ladspa->controls[cid] = 1.0;
600 ladspa->controls[cid] = 0.0;
601 } else if (control_info->integer) {
602 val = (gfloat)g_value_get_int (value);
603 ladspa->controls[cid] = val;
605 val = g_value_get_float (value);
606 ladspa->controls[cid] = val;
609 GST_DEBUG (0, "set arg %s to %f", control_info->name, ladspa->controls[cid]);
613 gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
615 GstLADSPA *ladspa = (GstLADSPA*)object;
616 gint cid = prop_id - ARG_LAST;
617 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
618 ladspa_control_info *control_info;
620 /* these are only registered in get mode */
623 g_value_set_int (value, ladspa->samplerate);
626 g_value_set_int (value, ladspa->buffersize);
632 /* verify it exists and is a control (not a port) */
633 if (cid >= oclass->numcontrols) return;
634 control_info = &(oclass->control_info[cid]);
635 if (control_info->name == NULL) return;
637 GST_DEBUG (0, "got arg %s as %f", control_info->name, ladspa->controls[cid]);
639 /* now see what type it is */
640 if (control_info->toggled) {
641 if (ladspa->controls[cid] == 1.0)
642 g_value_set_boolean (value, TRUE);
644 g_value_set_boolean (value, FALSE);
645 } else if (control_info->integer) {
646 g_value_set_int (value, (gint)ladspa->controls[cid]);
648 g_value_set_float (value, ladspa->controls[cid]);
653 gst_ladspa_instantiate (GstLADSPA *ladspa)
655 LADSPA_Descriptor *desc;
657 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
658 gboolean was_activated;
660 desc = ladspa->descriptor;
662 /* check for old handle */
663 was_activated = ladspa->activated;
664 if (ladspa->handle != NULL){
665 gst_ladspa_deactivate(ladspa);
666 desc->cleanup(ladspa->handle);
669 /* instantiate the plugin */
670 GST_DEBUG (0, "instantiating the plugin");
672 ladspa->handle = desc->instantiate(desc,ladspa->samplerate);
673 g_return_val_if_fail (ladspa->handle != NULL, FALSE);
675 /* walk through the ports and add all the arguments */
676 for (i=0;i<oclass->numcontrols;i++) {
677 /* connect the argument to the plugin */
678 GST_DEBUG (0, "added control port %d", oclass->control_portnums[i]);
679 desc->connect_port(ladspa->handle,
680 oclass->control_portnums[i],
681 &(ladspa->controls[i]));
684 /* reactivate if it was activated before the reinstantiation */
686 gst_ladspa_activate(ladspa);
691 static GstElementStateReturn
692 gst_ladspa_change_state (GstElement *element)
694 LADSPA_Descriptor *desc;
695 GstLADSPA *ladspa = (GstLADSPA*)element;
696 desc = ladspa->descriptor;
698 GST_DEBUG (0, "changing state");
699 switch (GST_STATE_TRANSITION (element)) {
700 case GST_STATE_NULL_TO_READY:
701 gst_ladspa_activate(ladspa);
703 case GST_STATE_READY_TO_NULL:
704 gst_ladspa_deactivate(ladspa);
710 if (GST_ELEMENT_CLASS (parent_class)->change_state)
711 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
713 return GST_STATE_SUCCESS;
717 gst_ladspa_activate(GstLADSPA *ladspa)
719 LADSPA_Descriptor *desc;
720 desc = ladspa->descriptor;
722 if (ladspa->activated){
723 gst_ladspa_deactivate(ladspa);
726 GST_DEBUG (0, "activating");
728 /* activate the plugin (function might be null) */
729 if (desc->activate != NULL) {
730 desc->activate(ladspa->handle);
733 ladspa->activated = TRUE;
737 gst_ladspa_deactivate(GstLADSPA *ladspa)
739 LADSPA_Descriptor *desc;
740 desc = ladspa->descriptor;
742 GST_DEBUG (0, "deactivating");
744 /* deactivate the plugin (function might be null) */
745 if (ladspa->activated && (desc->deactivate != NULL)) {
746 desc->deactivate(ladspa->handle);
749 ladspa->activated = FALSE;
753 gst_ladspa_loop(GstElement *element)
755 guint bufferbytesize, i, numsrcpads, numsinkpads, num_empty_pads;
756 guint num_processed, num_to_process;
757 GstEvent *event = NULL;
760 LADSPA_Data **data_in, **data_out;
761 GstBuffer **buffers_in, **buffers_out;
762 GstBufferPool *bufpool;
763 GstByteStream **bytestreams;
765 GstLADSPA *ladspa = (GstLADSPA *)element;
766 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
767 LADSPA_Descriptor *desc = ladspa->descriptor;
769 numsinkpads = oclass->numsinkpads;
770 numsrcpads = oclass->numsrcpads;
772 data_in = g_new0(LADSPA_Data*, numsinkpads);
773 data_out = g_new0(LADSPA_Data*, numsrcpads);
774 buffers_in = g_new0(GstBuffer*, numsinkpads);
775 buffers_out = g_new0(GstBuffer*, numsrcpads);
776 bytestreams = g_new0(GstByteStream*, numsinkpads);
778 /* find a bufferpool */
779 if (numsrcpads > 0 && (bufpool = gst_pad_get_bufferpool (ladspa->srcpads[0]))) {
780 GST_DEBUG (0, "Got bufferpool from first source pad");
782 bufferbytesize = sizeof (LADSPA_Data) * ladspa->buffersize;
783 bufpool = gst_buffer_pool_get_default (bufferbytesize, ladspa->numbuffers);
784 GST_DEBUG (0, "Created default bufferpool, %d x %d bytes", ladspa->numbuffers, bufferbytesize);
787 /* get the bytestreams for each pad */
788 for (i=0 ; i<numsinkpads ; i++){
789 bytestreams[i] = gst_bytestream_new (ladspa->sinkpads[i]);
792 /* since this is a loop element, we just loop here til things fall apart. */
794 /* we need to get the buffers_out first so we can know how many bytes to process */
795 for (i=0 ; i<numsrcpads ; i++){
796 buffers_out[i] = gst_buffer_new_from_pool (bufpool, 0, 0);
797 GST_BUFFER_TIMESTAMP(buffers_out[i]) = ladspa->timestamp;
798 data_out[i] = (LADSPA_Data *) GST_BUFFER_DATA(buffers_out[i]);
801 bufferbytesize = GST_BUFFER_SIZE (buffers_out[0]);
802 ladspa->buffersize = bufferbytesize / sizeof (LADSPA_Data);
805 /* first get all the necessary data from the input ports */
806 for (i=0 ; i<numsinkpads ; i++){
807 GST_DEBUG (0, "pulling %u bytes through channel %d's bytestream", bufferbytesize, i);
808 got_bytes = gst_bytestream_read (bytestreams[i], buffers_in + i, bufferbytesize);
810 if (got_bytes != bufferbytesize) {
811 /* we need to check for an event. */
812 gst_bytestream_get_status (bytestreams[i], &waiting, &event);
814 if (event && GST_EVENT_TYPE(event) == GST_EVENT_EOS) {
815 /* if we get an EOS event from one of our sink pads, we assume that
816 pad's finished handling data. delete the bytestream, free up the
817 pad, and free up the memory associated with the input channel. */
818 GST_DEBUG (0, "got an EOS event on sinkpad %d", i);
820 /* CHECKME should maybe check for other events and try to pull more data here */
823 data_in[i] = (LADSPA_Data *) GST_BUFFER_DATA(buffers_in[i]);
824 GST_BUFFER_TIMESTAMP(buffers_in[i]) = ladspa->timestamp;
828 if (num_empty_pads > 0){
829 if (num_empty_pads < numsinkpads){
830 /* only some pads have EOS, need to create some empty buffers */
831 for (i=0 ; i<numsinkpads ; i++){
832 if (buffers_in[i] == NULL){
835 buffers_in[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
836 GST_BUFFER_TIMESTAMP(buffers_in[i]) = ladspa->timestamp;
837 data_in[i] = data = (LADSPA_Data *) GST_BUFFER_DATA(buffers_in[i]);
838 for (x=0 ; x < ladspa->buffersize ; x++)
841 data_in[i] = (LADSPA_Data *) GST_BUFFER_DATA(buffers_in[i]);
842 GST_BUFFER_TIMESTAMP(buffers_in[i]) = ladspa->timestamp;
847 /* all pads have EOS, time to quit */
848 /* CHECKME do I have to push EOS events here? */
849 GST_DEBUG (0, "All sink pads have EOS, finishing.");
854 GST_DPMAN_PREPROCESS(ladspa->dpman, ladspa->buffersize, ladspa->timestamp);
857 /* split up processing of the buffer into chunks so that dparams can
858 * be updated when required.
859 * In many cases the buffer will be processed in one chunk anyway.
861 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
863 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
864 for (i=0 ; i<numsinkpads ; i++){
865 desc->connect_port (ladspa->handle, oclass->sinkpad_portnums[i], data_in[i]);
867 for (i=0 ; i<numsrcpads ; i++){
868 desc->connect_port (ladspa->handle, oclass->srcpad_portnums[i], data_out[i]);
870 desc->run(ladspa->handle, num_to_process);
871 for (i=0 ; i<numsinkpads ; i++){
872 data_in[i] += num_to_process;
874 for (i=0 ; i<numsrcpads ; i++){
875 data_out[i] += num_to_process;
878 num_processed += num_to_process;
881 for (i=0 ; i<numsrcpads ; i++) {
882 GST_DEBUG (0, "pushing buffer (%p) on src pad %d", buffers_out[i], i);
883 gst_pad_push (ladspa->srcpads[i], buffers_out[i]);
886 buffers_out[i] = NULL;
888 for (i=0 ; i<numsinkpads ; i++) {
889 gst_buffer_unref(buffers_in[i]);
891 buffers_in[i] = NULL;
894 ladspa->timestamp += ladspa->buffersize * 10^9 / ladspa->samplerate;
896 gst_element_yield (element);
899 gst_buffer_pool_unref(bufpool);
901 for (i=0 ; i<numsinkpads ; i++){
902 gst_bytestream_destroy (bytestreams[i]);
905 g_free (buffers_out);
909 g_free (bytestreams);
913 gst_ladspa_chain (GstPad *pad, GstBuffer *buf)
915 LADSPA_Descriptor *desc;
916 LADSPA_Data *data_in, **data_out = NULL;
917 GstBuffer **buffers_out = NULL;
919 unsigned long num_samples;
920 guint num_to_process, num_processed, i, numsrcpads;
923 GstLADSPAClass *oclass;
925 g_return_if_fail(pad != NULL);
926 g_return_if_fail(GST_IS_PAD(pad));
927 g_return_if_fail(buf != NULL);
929 ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
930 g_return_if_fail(ladspa != NULL);
932 /* this might happen if caps nego hasn't happened */
933 g_return_if_fail(ladspa->handle != NULL);
935 oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
937 if (GST_IS_EVENT (buf)) {
938 /* push the event out all the src pads */
939 for (i=0 ; i<oclass->numsrcpads ; i++){
940 gst_pad_push (ladspa->srcpads[0], buf);
945 data_in = (LADSPA_Data *) GST_BUFFER_DATA(buf);
946 num_samples = GST_BUFFER_SIZE(buf) / sizeof(gfloat);
947 numsrcpads = oclass->numsrcpads;
949 desc = ladspa->descriptor;
952 guint num_created_buffers = 0;
953 buffers_out = g_new(GstBuffer*, numsrcpads);
954 data_out = g_new(LADSPA_Data*, numsrcpads);
956 if (ladspa->inplace_broken){
957 num_created_buffers = numsrcpads;
960 /* we can share the buffer for input and output */
961 buffers_out[0] = buf;
962 data_out[0] = (LADSPA_Data *)GST_BUFFER_DATA(buf);
963 num_created_buffers = numsrcpads - 1;
966 if (num_created_buffers > 0){
967 ladspa->bufpool = gst_buffer_pool_get_default (sizeof (LADSPA_Data) * GST_BUFFER_SIZE(buf), ladspa->numbuffers);
969 for (i = numsrcpads - num_created_buffers ; i<numsrcpads ; i++){
970 buffers_out[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
971 GST_BUFFER_TIMESTAMP(buffers_out[i]) = GST_BUFFER_TIMESTAMP(buf);
972 data_out[i] = (LADSPA_Data *) GST_BUFFER_DATA(buffers_out[i]);
977 GST_DPMAN_PREPROCESS(ladspa->dpman, num_samples, GST_BUFFER_TIMESTAMP(buf));
980 /* split up processing of the buffer into chunks so that dparams can
981 * be updated when required.
982 * In many cases the buffer will be processed in one chunk anyway.
984 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
985 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
987 desc->connect_port(ladspa->handle,oclass->sinkpad_portnums[0],data_in);
988 for (i=0 ; i<numsrcpads ; i++){
989 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[i],data_out[i]);
991 desc->run(ladspa->handle, num_to_process);
993 data_in += num_to_process;
994 for (i=0 ; i<numsrcpads ; i++){
995 data_out[i] += num_to_process;
997 num_processed += num_to_process;
1000 if (numsrcpads > 0){
1001 for (i=0 ; i<numsrcpads ; i++){
1002 gst_pad_push (ladspa->srcpads[i], buffers_out[i]);
1004 g_free(buffers_out);
1009 /* if we have reached here, there are no src pads */
1010 gst_buffer_unref(buf);
1014 gst_ladspa_get(GstPad *pad)
1017 GstLADSPAClass *oclass;
1021 LADSPA_Descriptor *desc;
1023 guint num_to_process, num_processed;
1025 g_return_val_if_fail(pad != NULL, NULL);
1026 g_return_val_if_fail(GST_IS_PAD(pad), NULL);
1028 ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
1029 g_return_val_if_fail(ladspa != NULL, NULL);
1031 /* this might happen if caps nego hasn't happened */
1032 g_return_val_if_fail(ladspa->handle != NULL, NULL);
1034 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
1036 /* force src pad to set caps */
1037 if (ladspa->newcaps) {
1038 gst_ladspa_force_src_caps(ladspa, ladspa->srcpads[0]);
1041 /* get a bufferpool */
1042 if (ladspa->bufpool == NULL) {
1043 ladspa->bufpool = gst_pad_get_bufferpool (ladspa->srcpads[0]);
1044 if (ladspa->bufpool == NULL) {
1045 ladspa->bufpool = gst_buffer_pool_get_default (sizeof (LADSPA_Data) * ladspa->buffersize, ladspa->numbuffers);
1049 buf = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
1050 GST_BUFFER_TIMESTAMP(buf) = ladspa->timestamp;
1051 data = (LADSPA_Data *) GST_BUFFER_DATA(buf);
1053 desc = ladspa->descriptor;
1054 GST_DPMAN_PREPROCESS(ladspa->dpman, GST_BUFFER_SIZE (buf) / sizeof (gfloat), ladspa->timestamp);
1057 /* split up processing of the buffer into chunks so that dparams can
1058 * be updated when required.
1059 * In many cases the buffer will be processed in one chunk anyway.
1061 while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
1062 num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
1064 /* update timestamp */
1065 ladspa->timestamp += num_to_process * GST_SECOND / ladspa->samplerate;
1067 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);
1068 desc->run(ladspa->handle, num_to_process);
1070 data += num_to_process;
1071 num_processed = num_to_process;
1078 ladspa_describe_plugin(const char *pcFullFilename,
1079 void *pvPluginHandle,
1080 LADSPA_Descriptor_Function pfDescriptorFunction)
1082 const LADSPA_Descriptor *desc;
1085 GstElementDetails *details;
1086 GTypeInfo typeinfo = {
1087 sizeof(GstLADSPAClass),
1090 (GClassInitFunc)gst_ladspa_class_init,
1095 (GInstanceInitFunc)gst_ladspa_init,
1098 GstElementFactory *factory;
1100 /* walk through all the plugins in this pluginlibrary */
1102 while ((desc = pfDescriptorFunction(i++))) {
1105 /* construct the type */
1106 type_name = g_strdup_printf("ladspa_%s",desc->Label);
1107 g_strcanon (type_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_+", '-');
1108 /* if it's already registered, drop it */
1109 if (g_type_from_name(type_name)) {
1113 /* create the type now */
1114 type = g_type_register_static(GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
1116 /* construct the element details struct */
1117 details = g_new0(GstElementDetails,1);
1118 details->longname = g_strdup(desc->Name);
1119 details->klass = "Filter/Audio/LADSPA";
1120 details->license = "LGPL";
1121 details->description = details->longname;
1122 details->version = g_strdup_printf("%ld",desc->UniqueID);
1123 details->author = g_strdup(desc->Maker);
1124 details->copyright = g_strdup(desc->Copyright);
1126 /* register the plugin with gstreamer */
1127 factory = gst_element_factory_new(type_name,type,details);
1128 g_return_if_fail(factory != NULL);
1129 gst_plugin_add_feature (ladspa_plugin, GST_PLUGIN_FEATURE (factory));
1131 /* add this plugin to the hash */
1132 g_hash_table_insert(ladspa_descriptors,
1133 GINT_TO_POINTER(type),
1137 /* only add sink padtemplate if there are sinkpads */
1138 for (j=0;j<desc->PortCount;j++) {
1139 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j]) &&
1140 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j])) {
1141 sinktempl = ladspa_sink_factory();
1142 gst_element_factory_add_pad_template (factory, sinktempl);
1147 /* only add src padtemplate if there are srcpads */
1148 for (j=0;j<desc->PortCount;j++) {
1149 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j]) &&
1150 LADSPA_IS_PORT_OUTPUT(desc->PortDescriptors[j])) {
1151 srctempl = ladspa_src_factory();
1152 gst_element_factory_add_pad_template (factory, srctempl);
1161 plugin_init (GModule *module, GstPlugin *plugin)
1163 ladspa_descriptors = g_hash_table_new(NULL,NULL);
1164 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
1166 ladspa_plugin = plugin;
1168 LADSPAPluginSearch(ladspa_describe_plugin);
1170 if (! gst_library_load ("gstbytestream"))
1173 /* initialize dparam support library */
1174 gst_control_init(NULL,NULL);
1179 GstPluginDesc plugin_desc = {