actually recurse into sndfile if we are able big ladspa cleanups, mainly to comply...
[platform/upstream/gst-plugins-good.git] / ext / ladspa / gstladspa.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *               <2001> Steve Baker <stevebaker_org@yahoo.co.uk>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include <string.h>
22 #include <math.h>
23 #include <gst/control/control.h>
24
25 #include "gstladspa.h"
26 #include <ladspa.h>     /* main ladspa sdk include file */
27 #include "utils.h"      /* ladspa sdk utility functions */
28
29
30 /* takes ownership of the name */
31 static GstPadTemplate*
32 ladspa_sink_factory (gchar *name)
33 {
34   return GST_PAD_TEMPLATE_NEW (
35   name,
36   GST_PAD_SINK,
37   GST_PAD_ALWAYS,
38   GST_CAPS_NEW (
39     "ladspa_sink",
40     "audio/x-raw-float",
41     "width",            GST_PROPS_INT (32),
42     "endianness",       GST_PROPS_INT (G_BYTE_ORDER),
43     "rate",             GST_PROPS_INT_RANGE (4000, 96000),
44     "buffer-frames",    GST_PROPS_INT_RANGE (1, G_MAXINT),
45     "channels",         GST_PROPS_INT (1)
46     )
47   );
48 }
49
50 /* takes ownership of the name */
51 static GstPadTemplate*
52 ladspa_src_factory (gchar *name)
53 {
54   return GST_PAD_TEMPLATE_NEW (
55   name,
56   GST_PAD_SRC,
57   GST_PAD_ALWAYS,
58   GST_CAPS_NEW (
59     "ladspa_src",
60     "audio/x-raw-float",
61     "width",            GST_PROPS_INT (32),
62     "endianness",       GST_PROPS_INT (G_BYTE_ORDER),
63     "rate",             GST_PROPS_INT_RANGE (4000, 96000),
64     "buffer-frames",    GST_PROPS_INT_RANGE (1, G_MAXINT),
65     "channels",         GST_PROPS_INT (1)
66     )
67   );
68 }
69
70 static void                     gst_ladspa_class_init           (GstLADSPAClass *klass);
71 static void                     gst_ladspa_init                 (GstLADSPA *ladspa);
72
73 static void                     gst_ladspa_update_int           (const GValue *value, gpointer data);
74 static GstPadLinkReturn         gst_ladspa_link                 (GstPad *pad, GstCaps *caps);
75 static void                     gst_ladspa_force_src_caps       (GstLADSPA *ladspa, GstPad *pad);
76
77 static void                     gst_ladspa_set_property         (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
78 static void                     gst_ladspa_get_property         (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
79
80 static gboolean                 gst_ladspa_instantiate          (GstLADSPA *ladspa);
81 static void                     gst_ladspa_activate             (GstLADSPA *ladspa);
82 static void                     gst_ladspa_deactivate           (GstLADSPA *ladspa);
83
84 static GstElementStateReturn    gst_ladspa_change_state         (GstElement *element);
85 static void                     gst_ladspa_loop                 (GstElement *element);
86 static void                     gst_ladspa_chain                (GstPad *pad,GstBuffer *buf);
87 static GstBuffer *              gst_ladspa_get                  (GstPad *pad);
88
89 static GstElementClass *parent_class = NULL;
90
91 static GstPlugin *ladspa_plugin;
92 static GHashTable *ladspa_descriptors;
93
94 GST_DEBUG_CATEGORY_STATIC (ladspa_debug);
95 #define DEBUG(...) \
96     GST_CAT_LEVEL_LOG (ladspa_debug, GST_LEVEL_DEBUG, NULL, __VA_ARGS__)
97 #define DEBUG_OBJ(obj,...) \
98     GST_CAT_LEVEL_LOG (ladspa_debug, GST_LEVEL_DEBUG, obj, __VA_ARGS__)
99
100 static void
101 gst_ladspa_class_init (GstLADSPAClass *klass)
102 {
103   GObjectClass *gobject_class;
104   GstElementClass *gstelement_class;
105   LADSPA_Descriptor *desc;
106   gint i,current_portnum,sinkcount,srccount,controlcount;
107   gint hintdesc;
108   gint argtype,argperms;
109   GParamSpec *paramspec = NULL;
110   gchar *argname, *tempstr, *paren;
111
112   gobject_class = (GObjectClass*)klass;
113   gstelement_class = (GstElementClass*)klass;
114
115   gobject_class->set_property = gst_ladspa_set_property;
116   gobject_class->get_property = gst_ladspa_get_property;
117
118   gstelement_class->change_state = gst_ladspa_change_state;
119
120   /* look up and store the ladspa descriptor */
121   klass->descriptor = g_hash_table_lookup(ladspa_descriptors,GINT_TO_POINTER(G_TYPE_FROM_CLASS(klass)));
122   desc = klass->descriptor;
123
124   klass->numports = desc->PortCount;
125
126   klass->numsinkpads = 0;
127   klass->numsrcpads = 0;
128   klass->numcontrols = 0;
129
130   /* walk through the ports, count the input, output and control ports */
131   for (i=0; i<desc->PortCount; i++) {
132     if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]))
133       if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
134         klass->numsinkpads++;
135       else
136         klass->numsrcpads++;
137     else if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
138       klass->numcontrols++;
139   }
140
141   DEBUG ("ladspa element class: init %s with %d sink, %d src, %d control\n",
142          g_type_name (G_TYPE_FROM_CLASS (klass)),
143          klass->numsinkpads, klass->numsrcpads, klass->numcontrols);
144
145   klass->srcpad_portnums = g_new0(gint,klass->numsrcpads);
146   klass->sinkpad_portnums = g_new0(gint,klass->numsinkpads);
147   klass->control_portnums = g_new0(gint,klass->numcontrols);
148   sinkcount = 0;
149   srccount = 0;
150   controlcount = 0;
151
152   /* walk through the ports, note the portnums for srcpads, sinkpads and control
153      params */
154   for (i=0; i<desc->PortCount; i++) {
155     if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]))
156       if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
157         klass->sinkpad_portnums[sinkcount++] = i;
158       else
159         klass->srcpad_portnums[srccount++] = i;
160     else if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
161       klass->control_portnums[controlcount++] = i;
162   }
163
164   /* now build the control info from the control ports */
165   klass->control_info = g_new0(ladspa_control_info,klass->numcontrols);
166     
167   for (i=0;i<klass->numcontrols;i++) {
168     current_portnum = klass->control_portnums[i];
169     
170     /* short name for hint descriptor */
171     hintdesc = desc->PortRangeHints[current_portnum].HintDescriptor;
172
173     /* get the various bits */
174     if (LADSPA_IS_HINT_TOGGLED(hintdesc))
175       klass->control_info[i].toggled = TRUE;
176     if (LADSPA_IS_HINT_LOGARITHMIC(hintdesc))
177       klass->control_info[i].logarithmic = TRUE;
178     if (LADSPA_IS_HINT_INTEGER(hintdesc))
179       klass->control_info[i].integer = TRUE;
180
181     /* figure out the argument details */
182     if (klass->control_info[i].toggled) argtype = G_TYPE_BOOLEAN;
183     else if (klass->control_info[i].integer) argtype = G_TYPE_INT;
184     else argtype = G_TYPE_FLOAT;
185
186     /* grab the bounds */
187     if (LADSPA_IS_HINT_BOUNDED_BELOW(hintdesc)) {
188       klass->control_info[i].lower = TRUE;
189       klass->control_info[i].lowerbound =
190         desc->PortRangeHints[current_portnum].LowerBound;
191     } else {
192       if (argtype==G_TYPE_INT) klass->control_info[i].lowerbound = (gfloat)G_MININT;
193       if (argtype==G_TYPE_FLOAT) klass->control_info[i].lowerbound = -G_MAXFLOAT;
194     }
195     
196     if (LADSPA_IS_HINT_BOUNDED_ABOVE(hintdesc)) {
197       klass->control_info[i].upper = TRUE;
198       klass->control_info[i].upperbound =
199         desc->PortRangeHints[current_portnum].UpperBound;
200       if (LADSPA_IS_HINT_SAMPLE_RATE(hintdesc)) {
201         klass->control_info[i].samplerate = TRUE;
202         klass->control_info[i].upperbound *= 44100; /* FIXME? */
203       }
204     } else {
205       if (argtype==G_TYPE_INT) klass->control_info[i].upperbound = (gfloat)G_MAXINT;
206       if (argtype==G_TYPE_FLOAT) klass->control_info[i].upperbound = G_MAXFLOAT;
207     }
208
209     /* use the lowerbound as the default value */
210     klass->control_info[i].def = klass->control_info[i].lowerbound;
211
212 #ifdef LADSPA_IS_HINT_HAS_DEFAULT
213     /* figure out the defaults */
214     if (LADSPA_IS_HINT_HAS_DEFAULT (hintdesc)) {
215       if (LADSPA_IS_HINT_DEFAULT_MINIMUM (hintdesc))
216         klass->control_info[i].def = klass->control_info[i].lowerbound;
217       else if (LADSPA_IS_HINT_DEFAULT_LOW (hintdesc))
218         if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
219           klass->control_info[i].def = exp (0.75*log(klass->control_info[i].lowerbound) +
220                                                 0.25*log(klass->control_info[i].upperbound));
221         else
222           klass->control_info[i].def = (0.75*klass->control_info[i].lowerbound +
223                                             0.25*klass->control_info[i].upperbound);
224       else if (LADSPA_IS_HINT_DEFAULT_MIDDLE (hintdesc))
225         if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
226           klass->control_info[i].def = exp (0.5*log(klass->control_info[i].lowerbound) +
227                                                 0.5*log(klass->control_info[i].upperbound));
228         else
229           klass->control_info[i].def = (0.5*klass->control_info[i].lowerbound +
230                                             0.5*klass->control_info[i].upperbound);
231       else if (LADSPA_IS_HINT_DEFAULT_HIGH (hintdesc))
232         if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
233           klass->control_info[i].def = exp (0.25*log(klass->control_info[i].lowerbound) +
234                                                 0.75*log(klass->control_info[i].upperbound));
235         else
236           klass->control_info[i].def = (0.25*klass->control_info[i].lowerbound +
237                                             0.75*klass->control_info[i].upperbound);
238       else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM (hintdesc))
239         klass->control_info[i].def = klass->control_info[i].upperbound;
240       else if (LADSPA_IS_HINT_DEFAULT_0 (hintdesc))
241         klass->control_info[i].def = 0.0;
242       else if (LADSPA_IS_HINT_DEFAULT_1 (hintdesc))
243         klass->control_info[i].def = 1.0;
244       else if (LADSPA_IS_HINT_DEFAULT_100 (hintdesc))
245         klass->control_info[i].def = 100.0;
246       else if (LADSPA_IS_HINT_DEFAULT_440 (hintdesc))
247         klass->control_info[i].def = 440.0;
248     }
249 #endif /* LADSPA_IS_HINT_HAS_DEFAULT */
250
251     klass->control_info[i].def = CLAMP(klass->control_info[i].def,
252                                        klass->control_info[i].lowerbound,
253                                        klass->control_info[i].upperbound);
254     
255     if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[current_portnum])) {
256       argperms = G_PARAM_READWRITE;
257       klass->control_info[i].writable = TRUE;
258     } else {
259       argperms = G_PARAM_READABLE;
260       klass->control_info[i].writable = FALSE;
261     }
262
263     klass->control_info[i].name = g_strdup(desc->PortNames[current_portnum]);
264     argname = g_strdup(klass->control_info[i].name);
265     /* find out if there is a (unitname) at the end of the argname and get rid
266        of it */
267     paren = g_strrstr (argname, " (");
268     if (paren != NULL) {
269       *paren = '\0';
270     }
271     /* this is the same thing that param_spec_* will do */
272     g_strcanon (argname, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
273     /* satisfy glib2 (argname[0] must be [A-Za-z]) */
274     if (!((argname[0] >= 'a' && argname[0] <= 'z') || (argname[0] >= 'A' && argname[0] <= 'Z'))) {
275       tempstr = argname;
276       argname = g_strconcat("param-", argname, NULL);
277       g_free (tempstr);
278     }
279     
280     /* check for duplicate property names */
281     if (g_object_class_find_property(G_OBJECT_CLASS(klass), argname) != NULL){
282       gint numarg=1;
283       gchar *numargname = g_strdup_printf("%s_%d",argname,numarg++);
284       while (g_object_class_find_property(G_OBJECT_CLASS(klass), numargname) != NULL){
285         g_free(numargname);
286         numargname = g_strdup_printf("%s_%d",argname,numarg++);
287       }
288       argname = numargname;
289     }
290     
291     klass->control_info[i].param_name = argname;
292     
293     DEBUG ("adding arg %s from %s", argname, klass->control_info[i].name);
294     
295     if (argtype==G_TYPE_BOOLEAN){
296       paramspec = g_param_spec_boolean(argname,argname,argname, FALSE, argperms);
297     } else if (argtype==G_TYPE_INT){      
298       paramspec = g_param_spec_int(argname,argname,argname, 
299         (gint)klass->control_info[i].lowerbound, 
300         (gint)klass->control_info[i].upperbound, 
301         (gint)klass->control_info[i].def, argperms);
302     } else if (klass->control_info[i].samplerate){
303       paramspec = g_param_spec_float(argname,argname,argname, 
304         0.0, G_MAXFLOAT, 
305         0.0, argperms);
306     } else {
307       paramspec = g_param_spec_float(argname,argname,argname, 
308         klass->control_info[i].lowerbound, klass->control_info[i].upperbound, 
309         klass->control_info[i].def, argperms);
310     }
311     
312     /* properties have an offset of 1 */
313     g_object_class_install_property(G_OBJECT_CLASS(klass), i+1, paramspec);
314   }
315 }
316
317 static void
318 gst_ladspa_init (GstLADSPA *ladspa)
319 {
320   GstLADSPAClass *oclass;
321   ladspa_control_info cinfo;
322   GList *l;
323   LADSPA_Descriptor *desc;
324   gint i,sinkcount,srccount;
325
326   oclass = (GstLADSPAClass*)G_OBJECT_GET_CLASS (ladspa);
327   desc = oclass->descriptor;
328   ladspa->descriptor = oclass->descriptor;
329   
330   /* allocate the various arrays */
331   ladspa->srcpads = g_new0(GstPad*,oclass->numsrcpads);
332   ladspa->sinkpads = g_new0(GstPad*,oclass->numsinkpads);
333   ladspa->controls = g_new(gfloat,oclass->numcontrols);
334   ladspa->dpman = gst_dpman_new ("ladspa_dpman", GST_ELEMENT(ladspa));
335   
336   /* set up pads */
337   sinkcount = 0;
338   srccount = 0;
339   for (l=GST_ELEMENT_CLASS (oclass)->padtemplates; l; l=l->next) {
340     GstPad *pad = gst_pad_new_from_template (GST_PAD_TEMPLATE (l->data),
341                                              GST_PAD_TEMPLATE_NAME_TEMPLATE (l->data));
342     gst_pad_set_link_function (pad, gst_ladspa_link);
343     gst_element_add_pad ((GstElement*)ladspa, pad);
344
345     if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK)
346       ladspa->sinkpads[sinkcount++] = pad;
347     else
348       ladspa->srcpads[srccount++] = pad;
349   }
350   
351   /* set up dparams */
352   for (i=0; i<oclass->numcontrols; i++) {
353     if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
354       cinfo = oclass->control_info[i];
355       ladspa->controls[i]=cinfo.def;
356       
357       if (cinfo.toggled){
358         gst_dpman_add_required_dparam_callback (
359           ladspa->dpman, 
360           g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
361                            0, 1, (gint)(ladspa->controls[i]), G_PARAM_READWRITE),
362           "int", gst_ladspa_update_int, &(ladspa->controls[i])
363         );
364       }
365       else if (cinfo.integer){
366         gst_dpman_add_required_dparam_callback (
367           ladspa->dpman, 
368           g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
369                            (gint)cinfo.lowerbound, (gint)cinfo.upperbound,
370                            (gint)ladspa->controls[i], G_PARAM_READWRITE),
371           "int", gst_ladspa_update_int, &(ladspa->controls[i])
372         );
373       }
374       else if (cinfo.samplerate){
375         gst_dpman_add_required_dparam_direct (
376           ladspa->dpman, 
377           g_param_spec_float(cinfo.param_name, cinfo.name, cinfo.name,
378                            cinfo.lowerbound, cinfo.upperbound,
379                            ladspa->controls[i], G_PARAM_READWRITE),
380           "hertz-rate-bound", &(ladspa->controls[i])
381         );
382       }
383       else {
384         gst_dpman_add_required_dparam_direct (
385           ladspa->dpman, 
386           g_param_spec_float(cinfo.param_name, cinfo.name, cinfo.name,
387                            cinfo.lowerbound, cinfo.upperbound,
388                            ladspa->controls[i], G_PARAM_READWRITE),
389           "float", &(ladspa->controls[i])
390         );
391       }
392     }
393   }
394
395   /* nonzero default needed to instantiate() some plugins */
396   ladspa->samplerate = 44100;
397
398   ladspa->buffer_frames = 0; /* should be set with caps */
399   ladspa->activated = FALSE;
400   ladspa->bufpool = NULL;
401   ladspa->inplace_broken = LADSPA_IS_INPLACE_BROKEN(ladspa->descriptor->Properties);
402
403   if (sinkcount==0 && srccount == 1) {
404     /* get mode (no sink pads) */
405     DEBUG_OBJ (ladspa, "mono get mode with 1 src pad");
406
407     gst_pad_set_get_function (ladspa->srcpads[0], gst_ladspa_get);
408   } else if (sinkcount==1){
409     /* with one sink we can use the chain function */
410     DEBUG_OBJ (ladspa, "chain mode");
411
412     gst_pad_set_chain_function (ladspa->sinkpads[0], gst_ladspa_chain);
413   } else if (sinkcount > 1){
414     /* more than one sink pad needs loop mode */
415     DEBUG_OBJ (ladspa, "loop mode with %d sink pads and %d src pads", sinkcount, srccount);
416
417     gst_element_set_loop_function (GST_ELEMENT (ladspa), gst_ladspa_loop);
418   } else if (sinkcount==0 && srccount == 0) {
419     /* for example, a plugin with only control inputs and output -- just ignore
420      * it for now */
421   } else {
422     g_warning ("%d sink pads, %d src pads not yet supported", sinkcount, srccount);
423   }
424
425   gst_ladspa_instantiate (ladspa);
426 }
427
428 static void
429 gst_ladspa_update_int(const GValue *value, gpointer data)
430 {
431   gfloat *target = (gfloat*) data;
432   *target = (gfloat)g_value_get_int(value);
433 }
434
435 static GstPadLinkReturn
436 gst_ladspa_link (GstPad *pad, GstCaps *caps)
437 {
438   GstElement *element = (GstElement*)GST_PAD_PARENT (pad);
439   GstLADSPA *ladspa = (GstLADSPA*)element;
440   const GList *l = NULL;
441   gint rate;
442
443   if (GST_CAPS_IS_FIXED (caps)) {
444     /* if this fails in some other plugin, the graph is left in an inconsistent
445        state */
446     for (l=gst_element_get_pad_list (element); l; l=l->next)
447       if (pad != (GstPad*)l->data)
448         if (gst_pad_try_set_caps ((GstPad*)l->data, caps) <= 0)
449           return GST_PAD_LINK_REFUSED;
450     
451     /* we assume that the ladspa plugin can handle any sample rate, so this
452        check gets put last */
453     gst_caps_get_int (caps, "rate", &rate);
454     /* have to instantiate ladspa plugin when samplerate changes (groan) */
455     if (ladspa->samplerate != rate) {
456       ladspa->samplerate = rate;
457       if (! gst_ladspa_instantiate(ladspa))
458         return GST_PAD_LINK_REFUSED;
459     }
460     
461     gst_caps_get_int (caps, "buffer-frames", &ladspa->buffer_frames);
462     
463     if (ladspa->bufpool)
464       gst_buffer_pool_unref (ladspa->bufpool);
465     ladspa->bufpool = gst_buffer_pool_get_default (ladspa->buffer_frames * sizeof(gfloat),
466                                                    3);
467     
468     return GST_PAD_LINK_OK;
469   }
470   
471   return GST_PAD_LINK_DELAYED;
472 }
473
474 static void
475 gst_ladspa_force_src_caps(GstLADSPA *ladspa, GstPad *pad)
476 {
477   if (!ladspa->buffer_frames) {
478     ladspa->buffer_frames = 256; /* 5 ms at 44100 kHz (just a default...) */
479     g_return_if_fail (ladspa->bufpool == NULL);
480     ladspa->bufpool = gst_buffer_pool_get_default (ladspa->buffer_frames * sizeof(gfloat),
481                                                    3);
482   }
483
484   DEBUG_OBJ (ladspa, "forcing caps with rate=%d, buffer-frames=%d",
485              ladspa->samplerate, ladspa->buffer_frames);
486
487   gst_pad_try_set_caps (pad, gst_caps_new (
488     "ladspa_src_caps",
489     "audio/x-raw-float",
490     gst_props_new (
491       "width",          GST_PROPS_INT (32),
492       "endianness",     GST_PROPS_INT (G_BYTE_ORDER),
493       "buffer-frames",  GST_PROPS_INT (ladspa->buffer_frames),
494       "rate",           GST_PROPS_INT (ladspa->samplerate),
495       "channels",       GST_PROPS_INT (1),
496       NULL
497     )
498   ));
499 }
500
501 static void
502 gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
503 {
504   GstLADSPA *ladspa = (GstLADSPA*)object;
505   GstLADSPAClass *oclass;
506   ladspa_control_info *control_info;
507     
508   oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
509
510   /* remember, properties have an offset of 1 */
511   prop_id--;
512
513   /* verify it exists */
514   g_return_if_fail (prop_id < oclass->numcontrols);
515   
516   control_info = &(oclass->control_info[prop_id]);
517   g_return_if_fail (control_info->name != NULL);
518
519   /* check to see if it's writable */
520   g_return_if_fail (control_info->writable);
521
522   /* now see what type it is */
523   if (control_info->toggled)
524     ladspa->controls[prop_id] = g_value_get_boolean (value) ? 1.f : 0.f;
525   else if (control_info->integer)
526     ladspa->controls[prop_id] = g_value_get_int (value);
527   else
528     ladspa->controls[prop_id] = g_value_get_float (value);
529
530   DEBUG_OBJ (object, "set arg %s to %f", control_info->name, ladspa->controls[prop_id]);
531 }
532
533 static void
534 gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
535 {
536   GstLADSPA *ladspa = (GstLADSPA*)object;
537   GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
538   ladspa_control_info *control_info;
539
540   /* remember, properties have an offset of 1 */
541   prop_id--;
542
543   /* verify it exists */
544   g_return_if_fail (prop_id < oclass->numcontrols);
545
546   control_info = &(oclass->control_info[prop_id]);
547   g_return_if_fail (control_info->name != NULL);
548
549   /* now see what type it is */
550   if (control_info->toggled)
551     g_value_set_boolean (value, ladspa->controls[prop_id] == 1.0);
552   else if (control_info->integer)
553     g_value_set_int (value, (gint)ladspa->controls[prop_id]);
554   else
555     g_value_set_float (value, ladspa->controls[prop_id]);
556
557   DEBUG_OBJ (object, "got arg %s as %f", control_info->name, ladspa->controls[prop_id]);
558 }
559
560 static gboolean
561 gst_ladspa_instantiate (GstLADSPA *ladspa)
562 {
563   LADSPA_Descriptor *desc;
564   int i;
565   GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
566   gboolean was_activated;
567   
568   desc = ladspa->descriptor;
569   
570   /* check for old handle */
571   was_activated = ladspa->activated;
572   if (ladspa->handle != NULL){
573     gst_ladspa_deactivate(ladspa);
574     desc->cleanup(ladspa->handle);
575   }
576         
577   /* instantiate the plugin */ 
578   DEBUG_OBJ (ladspa, "instantiating the plugin at %d Hz", ladspa->samplerate);
579   
580   ladspa->handle = desc->instantiate(desc,ladspa->samplerate);
581   g_return_val_if_fail (ladspa->handle != NULL, FALSE);
582
583   /* connect the control ports */
584   for (i=0;i<oclass->numcontrols;i++)
585     desc->connect_port(ladspa->handle,
586                        oclass->control_portnums[i],
587                        &(ladspa->controls[i]));
588
589   /* reactivate if it was activated before the reinstantiation */
590   if (was_activated)
591     gst_ladspa_activate(ladspa);
592
593   return TRUE;
594 }
595
596 static GstElementStateReturn
597 gst_ladspa_change_state (GstElement *element)
598 {
599   LADSPA_Descriptor *desc;
600   GstLADSPA *ladspa = (GstLADSPA*)element;
601   desc = ladspa->descriptor;
602
603   switch (GST_STATE_TRANSITION (element)) {
604     case GST_STATE_NULL_TO_READY:
605       gst_ladspa_activate(ladspa);
606       break;
607     case GST_STATE_READY_TO_NULL:
608       gst_ladspa_deactivate(ladspa);
609       break;
610     default:
611       break;
612   }
613
614   if (GST_ELEMENT_CLASS (parent_class)->change_state)
615     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
616
617   return GST_STATE_SUCCESS;
618 }
619
620 static void
621 gst_ladspa_activate (GstLADSPA *ladspa)
622 {
623   LADSPA_Descriptor *desc;
624   desc = ladspa->descriptor;
625   
626   if (ladspa->activated)
627     gst_ladspa_deactivate(ladspa);
628   
629   DEBUG_OBJ (ladspa, "activating");
630
631   /* activate the plugin (function might be null) */
632   if (desc->activate != NULL)
633     desc->activate(ladspa->handle);
634
635   ladspa->activated = TRUE;
636 }
637
638 static void
639 gst_ladspa_deactivate (GstLADSPA *ladspa)
640 {
641   LADSPA_Descriptor *desc;
642   desc = ladspa->descriptor;
643
644   DEBUG_OBJ (ladspa, "deactivating");
645
646   /* deactivate the plugin (function might be null) */
647   if (ladspa->activated && (desc->deactivate != NULL))
648     desc->deactivate(ladspa->handle);
649
650   ladspa->activated = FALSE;
651 }
652
653 static void
654 gst_ladspa_loop (GstElement *element)
655 {
656   guint        i, j, numsrcpads, numsinkpads;
657   guint        num_processed, num_to_process;
658   gint         largest_buffer;
659   LADSPA_Data  **data_in, **data_out;
660   GstBuffer    **buffers_in, **buffers_out;
661  
662   GstLADSPA       *ladspa = (GstLADSPA *)element;
663   GstLADSPAClass  *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
664   LADSPA_Descriptor *desc = ladspa->descriptor;
665
666   numsinkpads = oclass->numsinkpads;
667   numsrcpads = oclass->numsrcpads;
668   
669   /* fixme: these mallocs need to die */
670   data_in = g_new0(LADSPA_Data*, numsinkpads);
671   data_out = g_new0(LADSPA_Data*, numsrcpads);
672   buffers_in = g_new0(GstBuffer*, numsinkpads);
673   buffers_out = g_new0(GstBuffer*, numsrcpads);
674   
675   largest_buffer = -1;
676
677   /* first get all the necessary data from the input ports */
678   for (i=0 ; i<numsinkpads ; i++){  
679   get_buffer:
680     buffers_in[i] = gst_pad_pull (ladspa->sinkpads[i]);
681     
682     if (GST_IS_EVENT (buffers_in[i])) {
683       /* push it out on all pads */
684       gst_data_ref_by_count ((GstData*)buffers_in[i], numsrcpads);
685       for (j=0; j<numsrcpads; j++)
686         gst_pad_push (ladspa->srcpads[j], buffers_in[i]);
687       if (GST_EVENT_TYPE (buffers_in[i]) == GST_EVENT_EOS) {
688         /* shut down */
689         gst_element_set_eos (element);
690         return;
691       } else {
692         goto get_buffer;
693       }
694     }
695
696     if (largest_buffer < 0)
697       largest_buffer = GST_BUFFER_SIZE (buffers_in[i])/sizeof(gfloat);
698     else
699       largest_buffer = MIN (GST_BUFFER_SIZE (buffers_in[i])/sizeof(gfloat), largest_buffer);
700     data_in[i] = (LADSPA_Data *) GST_BUFFER_DATA(buffers_in[i]);
701     GST_BUFFER_TIMESTAMP(buffers_in[i]) = ladspa->timestamp;
702   }
703
704   if (!ladspa->bufpool) {
705     gst_element_error (element, "Caps were never set, bailing...");
706     return;
707   }
708
709   i=0;
710   if (!ladspa->inplace_broken) {
711     for (; i<numsrcpads && i<numsinkpads; i++) {
712       /* reuse input buffers */
713       buffers_out[i] = buffers_in[i];
714       data_out[i] = data_in[i];
715     }
716   }
717   for (; i<numsrcpads; i++) {
718     /* we have to make new buffers -- at least we're taking them from a pool */
719     buffers_out[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
720     GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
721     data_out[i] = (LADSPA_Data*)GST_BUFFER_DATA (buffers_out[i]);
722   }
723   
724   GST_DPMAN_PREPROCESS(ladspa->dpman, largest_buffer, ladspa->timestamp);
725   num_processed = 0;
726
727   /* split up processing of the buffer into chunks so that dparams can
728    * be updated when required.
729    * In many cases the buffer will be processed in one chunk anyway.
730    */
731   while (GST_DPMAN_PROCESS (ladspa->dpman, num_processed)) {
732     num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
733
734     for (i=0 ; i<numsinkpads ; i++)
735       desc->connect_port (ladspa->handle, oclass->sinkpad_portnums[i], data_in[i]);
736     for (i=0 ; i<numsrcpads ; i++)
737       desc->connect_port (ladspa->handle, oclass->srcpad_portnums[i], data_out[i]);
738
739     desc->run(ladspa->handle, num_to_process);
740
741     for (i=0 ; i<numsinkpads ; i++)
742       data_in[i] += num_to_process;
743     for (i=0 ; i<numsrcpads ; i++)
744       data_out[i] += num_to_process;
745     
746     num_processed += num_to_process;
747   }
748     
749   for (i=0 ; i<numsinkpads ; i++) {
750     if (i >= numsrcpads || buffers_out[i] != buffers_in[i])
751       gst_buffer_unref(buffers_in[i]);
752     data_in[i] = NULL;
753     buffers_in[i] = NULL;
754   }      
755   for (i=0 ; i<numsrcpads ; i++) {
756     DEBUG_OBJ (ladspa, "pushing buffer (%p) on src pad %d", buffers_out[i], i);
757     gst_pad_push (ladspa->srcpads[i], buffers_out[i]);
758     
759     data_out[i] = NULL;
760     buffers_out[i] = NULL;
761   }
762   
763   ladspa->timestamp += ladspa->buffer_frames * GST_SECOND / ladspa->samplerate;
764
765   /* FIXME: move these mallocs and frees to the state-change handler */
766
767   g_free (buffers_out);
768   g_free (buffers_in);
769   g_free (data_out);
770   g_free (data_in);
771 }
772
773 static void
774 gst_ladspa_chain (GstPad *pad, GstBuffer *buffer_in)
775 {
776   LADSPA_Descriptor *desc;
777   LADSPA_Data *data_in, **data_out = NULL;
778   GstBuffer **buffers_out = NULL;
779   unsigned long num_samples;
780   guint num_to_process, num_processed, i, numsrcpads;
781   GstLADSPA *ladspa;
782   GstLADSPAClass *oclass;
783
784   ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
785   oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
786   data_in = (LADSPA_Data *) GST_BUFFER_DATA(buffer_in);
787   num_samples = GST_BUFFER_SIZE(buffer_in) / sizeof(gfloat);
788   numsrcpads = oclass->numsrcpads;
789   desc = ladspa->descriptor;
790
791   /* we shouldn't get events here... */
792   g_return_if_fail (GST_IS_BUFFER (buffer_in));
793   
794   if (!ladspa->bufpool) {
795     gst_element_error ((GstElement*)ladspa, "Caps were never set, bailing...");
796     return;
797   }
798
799   /* FIXME: this function shouldn't need to malloc() anything */
800   if (numsrcpads > 0) {
801     buffers_out = g_new(GstBuffer*, numsrcpads);
802     data_out = g_new(LADSPA_Data*, numsrcpads);
803   }
804
805   i=0;
806   if (!ladspa->inplace_broken && numsrcpads) {
807     /* reuse the first (chained) buffer */
808     buffers_out[i] = buffer_in;
809     DEBUG ("reuse: %d", GST_BUFFER_SIZE (buffer_in));
810     data_out[i] = data_in;
811     i++;
812   }
813   for (; i<numsrcpads; i++) {
814     /* we have to make new buffers -- at least we're taking them from a pool */
815     buffers_out[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
816     /* the size of the buffer returned from the pool is the maximum size; this
817        chained buffer might be smaller */
818     GST_BUFFER_SIZE (buffers_out[i]) = GST_BUFFER_SIZE (buffer_in);
819     DEBUG ("new %d", GST_BUFFER_SIZE (buffer_in));
820     GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
821     data_out[i] = (LADSPA_Data*)GST_BUFFER_DATA (buffers_out[i]);
822   }
823
824   GST_DPMAN_PREPROCESS(ladspa->dpman, num_samples, GST_BUFFER_TIMESTAMP(buffer_in));
825   num_processed = 0;
826
827   /* split up processing of the buffer into chunks so that dparams can
828    * be updated when required.
829    * In many cases the buffer will be processed in one chunk anyway.
830    */
831   while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
832     num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
833
834     desc->connect_port(ladspa->handle,oclass->sinkpad_portnums[0],data_in);  
835     for (i=0 ; i<numsrcpads ; i++)
836       desc->connect_port(ladspa->handle,oclass->srcpad_portnums[i],data_out[i]);
837
838     desc->run(ladspa->handle, num_to_process);
839     
840     data_in += num_to_process;
841     for (i=0 ; i<numsrcpads ; i++)
842       data_out[i] += num_to_process;
843
844     num_processed += num_to_process;
845   }
846
847   if (!numsrcpads || buffers_out[0] != buffer_in)
848     gst_buffer_unref(buffer_in);
849
850   if (numsrcpads) {
851     for (i=0; i<numsrcpads; i++) {
852       DEBUG_OBJ (ladspa, "pushing buffer (%p, length %d) on src pad %d",
853                  buffers_out[i], GST_BUFFER_SIZE (buffers_out[i])/sizeof(float), i);
854       gst_pad_push (ladspa->srcpads[i], buffers_out[i]);
855     }
856
857     g_free(buffers_out);
858     g_free(data_out);
859   }
860 }
861
862 static GstBuffer *
863 gst_ladspa_get(GstPad *pad)
864 {  
865   GstLADSPA *ladspa;
866   GstLADSPAClass *oclass;
867   GstBuffer *buf;
868   LADSPA_Data *data;
869   LADSPA_Descriptor *desc;
870   guint num_to_process, num_processed;
871
872   ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
873   oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
874   desc = ladspa->descriptor;
875
876   if (!ladspa->bufpool) {
877     /* capsnego hasn't happened... */
878     gst_ladspa_force_src_caps(ladspa, ladspa->srcpads[0]);
879   }
880
881   buf = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
882   GST_BUFFER_TIMESTAMP(buf) = ladspa->timestamp;
883   data = (LADSPA_Data *) GST_BUFFER_DATA(buf);  
884
885   GST_DPMAN_PREPROCESS(ladspa->dpman, ladspa->buffer_frames, ladspa->timestamp);
886   num_processed = 0;
887
888   /* split up processing of the buffer into chunks so that dparams can
889    * be updated when required.
890    * In many cases the buffer will be processed in one chunk anyway.
891    */
892   while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
893     num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
894
895     /* update timestamp */  
896     ladspa->timestamp += num_to_process * GST_SECOND / ladspa->samplerate;
897
898     desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);  
899
900     desc->run(ladspa->handle, num_to_process);
901     
902     data += num_to_process;
903     num_processed = num_to_process;
904   }
905   
906   return buf;
907 }
908
909 static void
910 ladspa_describe_plugin(const char *pcFullFilename,
911                        void *pvPluginHandle,
912                        LADSPA_Descriptor_Function pfDescriptorFunction)
913 {
914   const LADSPA_Descriptor *desc;
915   int i,j;
916   
917   GstElementDetails *details;
918   GTypeInfo typeinfo = {
919       sizeof(GstLADSPAClass),
920       NULL,
921       NULL,
922       (GClassInitFunc)gst_ladspa_class_init,
923       NULL,
924       NULL,
925       sizeof(GstLADSPA),
926       0,
927       (GInstanceInitFunc)gst_ladspa_init,
928   };
929   GType type;
930   GstElementFactory *factory;
931
932   /* walk through all the plugins in this pluginlibrary */
933   i = 0;
934   while ((desc = pfDescriptorFunction(i++))) {
935     gchar *type_name;
936
937     /* construct the type */
938     type_name = g_strdup_printf("ladspa-%s",desc->Label);
939     g_strcanon (type_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-+", '-');
940     /* if it's already registered, drop it */
941     if (g_type_from_name(type_name)) {
942       g_free(type_name);
943       continue;
944     }
945     /* create the type now */
946     type = g_type_register_static(GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
947
948     /* construct the element details struct */
949     details = g_new0(GstElementDetails,1);
950     details->longname = g_strdup(desc->Name);
951     details->klass = "Filter/Audio/LADSPA";
952     details->license = g_strdup (desc->Copyright);
953     details->description = details->longname;
954     details->version = g_strdup_printf("%ld",desc->UniqueID);
955     details->author = g_strdup(desc->Maker);
956     details->copyright = g_strdup(desc->Copyright);
957
958     /* register the plugin with gstreamer */
959     factory = gst_element_factory_new(type_name,type,details);
960     g_return_if_fail(factory != NULL);
961     gst_plugin_add_feature (ladspa_plugin, GST_PLUGIN_FEATURE (factory));
962
963     /* add this plugin to the hash */
964     g_hash_table_insert(ladspa_descriptors,
965                         GINT_TO_POINTER(type),
966                         (gpointer)desc);
967     
968
969     for (j=0;j<desc->PortCount;j++) {
970       if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j])) {
971         gchar *name = g_strdup((gchar *)desc->PortNames[j]);
972         g_strcanon (name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
973         /* the factories take ownership of the name */
974         if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j]))
975           gst_element_factory_add_pad_template (factory, ladspa_sink_factory (name));
976         else
977           gst_element_factory_add_pad_template (factory, ladspa_src_factory (name));
978       }
979     }
980   }
981 }
982
983 static gboolean
984 plugin_init (GModule *module, GstPlugin *plugin)
985 {
986   GST_DEBUG_CATEGORY_INIT (ladspa_debug, "ladspa",
987                            GST_DEBUG_FG_GREEN | GST_DEBUG_BG_BLACK | GST_DEBUG_BOLD,
988                            "LADSPA");
989
990   ladspa_descriptors = g_hash_table_new(NULL,NULL);
991   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
992
993   ladspa_plugin = plugin;
994
995   LADSPAPluginSearch(ladspa_describe_plugin);
996
997   if (! gst_library_load ("gstbytestream"))
998     return FALSE;
999   
1000   /* initialize dparam support library */
1001   gst_control_init(NULL,NULL);
1002   
1003   return TRUE;
1004 }
1005
1006 GstPluginDesc plugin_desc = {
1007   GST_VERSION_MAJOR,
1008   GST_VERSION_MINOR,
1009   "ladspa",
1010   plugin_init
1011 };