Bring the plugins in sync with the new core capsnego system.
[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
24 //#define DEBUG_ENABLED
25 #include "gstladspa.h"
26 #include "ladspa.h"
27 #include "search.h"
28 #include "utils.h"
29
30
31 static GstPadTemplate* 
32 ladspa_src_factory (void)
33 {
34   return 
35     gst_padtemplate_new (
36     "src",
37     GST_PAD_SRC,
38     GST_PAD_ALWAYS,
39     gst_caps_new (
40     "ladspa_src",
41       "audio/raw",
42       gst_props_new (
43         "format",     GST_PROPS_STRING ("float"),
44         "layout",     GST_PROPS_STRING ("gfloat"),
45         "intercept",  GST_PROPS_FLOAT(0.0),
46         "slope",      GST_PROPS_FLOAT(1.0),
47         "channels",   GST_PROPS_INT (1),
48         "rate",       GST_PROPS_INT_RANGE (0,G_MAXINT),
49       NULL)),
50     NULL);
51 }
52
53 static GstPadTemplate* 
54 ladspa_sink_factory (void) 
55 {
56   return 
57     gst_padtemplate_new (
58       "sink",
59       GST_PAD_SINK,
60       GST_PAD_ALWAYS,
61       gst_caps_new (
62       "float2int_sink",
63         "audio/raw",
64         gst_props_new (
65           "format",     GST_PROPS_STRING ("float"),
66           "layout",     GST_PROPS_STRING ("gfloat"),
67           "intercept",  GST_PROPS_FLOAT(0.0),
68           "slope",      GST_PROPS_FLOAT(1.0),
69           "channels",   GST_PROPS_INT (1),
70           "rate",       GST_PROPS_INT_RANGE (0,G_MAXINT),
71       NULL)),
72     NULL);
73 }
74
75 enum {
76   ARG_0,
77   ARG_LOOP_BASED,
78   ARG_SAMPLERATE,
79   ARG_BUFFERSIZE,
80   ARG_LAST,
81 };
82
83 static GstPadTemplate *srctempl, *sinktempl;
84
85 static void                     gst_ladspa_class_init           (GstLADSPAClass *klass);
86 static void                     gst_ladspa_init                 (GstLADSPA *ladspa);
87
88 //static GstPadNegotiateReturn gst_ladspa_negotiate_sink_mono (GstPad *pad, GstCaps **caps, gpointer *data);
89 //static GstPadNegotiateReturn gst_ladspa_negotiate_src_mono (GstPad *pad, GstCaps **caps, gpointer *data);
90 //static GstPadNegotiateReturn gst_ladspa_negotiate_src_get_mono (GstPad *pad, GstCaps **caps, gpointer *data);
91 static void gst_ladspa_force_caps(GstLADSPA *ladspa, GstPad *pad);
92
93 static void                     gst_ladspa_set_property         (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
94 static void                     gst_ladspa_get_property         (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
95
96 static gboolean gst_ladspa_instantiate (GstLADSPA *ladspa);
97 static void gst_ladspa_activate(GstLADSPA *ladspa);
98 static void gst_ladspa_deactivate(GstLADSPA *ladspa);
99
100 static GstElementStateReturn    gst_ladspa_change_state         (GstElement *element);
101 static void                     gst_ladspa_loop                 (GstElement *element);
102 static void      gst_ladspa_chain_inplace_mono (GstPad *pad,GstBuffer *buf);
103 static GstBuffer * gst_ladspa_get_mono(GstPad *pad);
104 static GstBuffer * gst_ladspa_get(GstPad *pad);
105
106 static GstElementClass *parent_class = NULL;
107 //static guint gst_ladspa_signals[LAST_SIGNAL] = { 0 };
108
109 static GstPlugin *ladspa_plugin;
110 static GHashTable *ladspa_descriptors;
111
112 static void
113 gst_ladspa_class_init (GstLADSPAClass *klass)
114 {
115   GObjectClass *gobject_class;
116   GstElementClass *gstelement_class;
117   LADSPA_Descriptor *desc;
118   gint i,current_portnum,sinkcount,srccount,controlcount;
119   gint hintdesc;
120   gint argtype,argperms;
121   GParamSpec *paramspec = NULL;
122   gchar *argname, *tempstr;
123
124   gobject_class = (GObjectClass*)klass;
125   gstelement_class = (GstElementClass*)klass;
126
127   gobject_class->set_property = gst_ladspa_set_property;
128   gobject_class->get_property = gst_ladspa_get_property;
129
130   gstelement_class->change_state = gst_ladspa_change_state;
131
132   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LOOP_BASED,
133     g_param_spec_boolean("loop-based","loop-based","loop-based",
134                          FALSE,G_PARAM_READWRITE));
135
136   // look up and store the ladspa descriptor
137   klass->descriptor = g_hash_table_lookup(ladspa_descriptors,GINT_TO_POINTER(G_TYPE_FROM_CLASS(klass)));
138   desc = klass->descriptor;
139
140   klass->numports = desc->PortCount;
141
142   klass->numsinkpads = 0;
143   klass->numsrcpads = 0;
144   klass->numcontrols = 0;
145
146   // walk through the ports, count the input, output and control ports
147   for (i=0;i<desc->PortCount;i++) {
148     if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) && 
149         LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])){
150       klass->numsinkpads++;
151     }
152       
153     if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) && 
154         LADSPA_IS_PORT_OUTPUT(desc->PortDescriptors[i])){
155       klass->numsrcpads++;
156     }
157       
158     if (LADSPA_IS_PORT_CONTROL(desc->PortDescriptors[i]) && 
159         LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])){
160       klass->numcontrols++;
161     }
162   }
163
164   klass->srcpad_portnums = g_new0(gint,klass->numsrcpads);
165   klass->sinkpad_portnums = g_new0(gint,klass->numsinkpads);
166   klass->control_portnums = g_new0(gint,klass->numcontrols);
167   sinkcount = 0;
168   srccount = 0;
169   controlcount = 0;
170
171   // walk through the ports, note the portnums for srcpads, sinkpads and control params
172   for (i=0;i<desc->PortCount;i++) {
173     if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) && 
174         LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])){
175       //g_print("input port %d\n", i);
176       klass->sinkpad_portnums[sinkcount++] = i;
177     }
178       
179     if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) && 
180         LADSPA_IS_PORT_OUTPUT(desc->PortDescriptors[i])){
181       //g_print("output port %d\n", i);
182       klass->srcpad_portnums[srccount++] = i;
183     }
184       
185     if (LADSPA_IS_PORT_CONTROL(desc->PortDescriptors[i]) && 
186         LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])){
187       //g_print("control port %d\n", i);
188       klass->control_portnums[controlcount++] = i;
189     }
190   }
191
192   // no sink pads - we'll use get mode and add params for samplerate and buffersize
193   if (klass->numsinkpads == 0 && klass->numsrcpads > 0){
194     g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_SAMPLERATE,
195       g_param_spec_int("samplerate","samplerate","samplerate",
196                       0,G_MAXINT,44100,G_PARAM_READWRITE));
197     g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_BUFFERSIZE,
198       g_param_spec_int("buffersize","buffersize","buffersize",
199                       0,G_MAXINT,64,G_PARAM_READWRITE));
200
201   }
202   
203   // now build the contorl info from the control ports
204   klass->control_info = g_new0(ladspa_control_info,klass->numcontrols);
205     
206   for (i=0;i<klass->numcontrols;i++) {
207
208     current_portnum = klass->control_portnums[i];
209     
210     // short name for hint descriptor
211     hintdesc = desc->PortRangeHints[current_portnum].HintDescriptor;
212
213     // get the various bits
214     if (LADSPA_IS_HINT_TOGGLED(hintdesc))
215       klass->control_info[i].toggled = TRUE;
216     if (LADSPA_IS_HINT_LOGARITHMIC(hintdesc))
217       klass->control_info[i].logarithmic = TRUE;
218     if (LADSPA_IS_HINT_INTEGER(hintdesc))
219       klass->control_info[i].integer = TRUE;
220
221     // figure out the argument details
222     if (klass->control_info[i].toggled) argtype = G_TYPE_BOOLEAN;
223     else if (klass->control_info[i].integer) argtype = G_TYPE_INT;
224     else argtype = G_TYPE_FLOAT;
225
226     // grab the bounds
227     if (LADSPA_IS_HINT_BOUNDED_BELOW(hintdesc)) {
228       klass->control_info[i].lower = TRUE;
229       klass->control_info[i].lowerbound =
230         desc->PortRangeHints[current_portnum].LowerBound;
231     } else {
232       if (argtype==G_TYPE_INT) klass->control_info[i].lowerbound = (gfloat)G_MININT;
233       if (argtype==G_TYPE_FLOAT) klass->control_info[i].lowerbound = G_MINFLOAT;
234     }
235     
236     if (LADSPA_IS_HINT_BOUNDED_ABOVE(hintdesc)) {
237       klass->control_info[i].upper = TRUE;
238       klass->control_info[i].upperbound =
239         desc->PortRangeHints[current_portnum].UpperBound;
240       if (LADSPA_IS_HINT_SAMPLE_RATE(hintdesc))
241         klass->control_info[i].samplerate = TRUE;
242     } else {
243       if (argtype==G_TYPE_INT) klass->control_info[i].upperbound = (gfloat)G_MAXINT;
244       if (argtype==G_TYPE_FLOAT) klass->control_info[i].upperbound = G_MAXFLOAT;
245     }
246
247     if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[current_portnum])) {
248       argperms = G_PARAM_READWRITE;
249       klass->control_info[i].writable = TRUE;
250     } else {
251       argperms = G_PARAM_READABLE;
252       klass->control_info[i].writable = FALSE;
253     }
254
255     klass->control_info[i].name = g_strdup(desc->PortNames[current_portnum]);
256     argname = g_strdup(klass->control_info[i].name);
257     /* this is the same thing that param_spec_* will do */
258     g_strcanon (argname, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
259     /* satisfy glib2 (argname[0] must be [A-Za-z]) */
260     if (!((argname[0] >= 'a' && argname[0] <= 'z') || (argname[0] >= 'A' && argname[0] <= 'Z'))) {
261       tempstr = argname;
262       argname = g_strconcat("param-", argname, NULL);
263       g_free (tempstr);
264     }
265     
266     /* check for duplicate property names */
267     if (g_object_class_find_property(G_OBJECT_CLASS(klass), argname) != NULL){
268       gint numarg=1;
269       gchar *numargname = g_strdup_printf("%s_%d",argname,numarg++);
270       while (g_object_class_find_property(G_OBJECT_CLASS(klass), numargname) != NULL){
271         g_free(numargname);
272         numargname = g_strdup_printf("%s_%d",argname,numarg++);
273       }
274       argname = numargname;
275     }
276     
277     g_print("adding arg %s from %s\n",argname, klass->control_info[i].name);
278     
279     if (argtype==G_TYPE_BOOLEAN){
280       paramspec = g_param_spec_boolean(argname,argname,argname, FALSE, argperms);
281     } else if (argtype==G_TYPE_INT){      
282       paramspec = g_param_spec_int(argname,argname,argname, 
283         (gint)klass->control_info[i].lowerbound, (gint)klass->control_info[i].upperbound, 0, argperms);
284     } else {
285       paramspec = g_param_spec_float(argname,argname,argname, 
286         klass->control_info[i].lowerbound, klass->control_info[i].upperbound, 
287         (klass->control_info[i].lowerbound + klass->control_info[i].upperbound) / 2.0f, argperms);
288     }
289     
290     g_object_class_install_property(G_OBJECT_CLASS(klass), i+ARG_LAST, paramspec);
291   }
292 }
293
294 static void
295 gst_ladspa_init (GstLADSPA *ladspa)
296 {
297   GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
298   
299   LADSPA_Descriptor *desc;
300   gint i,sinkcount,srccount,controlcount;
301
302   desc = oclass->descriptor;
303   ladspa->descriptor = oclass->descriptor;
304   
305   // allocate the various arrays
306   ladspa->srcpads = g_new0(GstPad*,oclass->numsrcpads);
307   ladspa->sinkpads = g_new0(GstPad*,oclass->numsinkpads);
308   ladspa->controls = g_new(gfloat,oclass->numcontrols);
309
310   // walk through the ports and add all the pads
311   sinkcount = 0;
312   srccount = 0;
313   controlcount = 0;
314   for (i=0;i<desc->PortCount;i++) {
315     if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
316         LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
317       ladspa->sinkpads[sinkcount] = gst_pad_new_from_template 
318       (sinktempl, (gchar *)desc->PortNames[i]);
319       gst_element_add_pad(GST_ELEMENT(ladspa),ladspa->sinkpads[sinkcount]);
320       sinkcount++;
321     }
322     if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
323         LADSPA_IS_PORT_OUTPUT(desc->PortDescriptors[i])) {
324       ladspa->srcpads[srccount] = gst_pad_new_from_template 
325         (srctempl, (gchar *)desc->PortNames[i]);
326       gst_element_add_pad(GST_ELEMENT(ladspa),ladspa->srcpads[srccount]);
327       srccount++;
328     }
329     if (LADSPA_IS_PORT_CONTROL(desc->PortDescriptors[i]) &&
330         LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
331           
332       // use the lowerbound as the default value if it exists
333       if (oclass->control_info[controlcount].lower){
334         ladspa->controls[controlcount]=oclass->control_info[controlcount].lowerbound;
335       } else {
336         ladspa->controls[controlcount] = 0.0;
337       }
338       controlcount++;
339     }
340   }
341
342   ladspa->samplerate = 0;
343   ladspa->buffersize = 0;
344   ladspa->newcaps = FALSE;
345   ladspa->activated = FALSE;
346   
347   // mono chain
348   if (sinkcount==1 && srccount==1){
349     //g_print("inplace mono chain mode\n");
350     //gst_pad_set_negotiate_function (ladspa->sinkpads[0], gst_ladspa_negotiate_sink_mono);
351     gst_pad_set_chain_function(ladspa->sinkpads[0],gst_ladspa_chain_inplace_mono);
352     //gst_pad_set_negotiate_function (ladspa->srcpads[0], gst_ladspa_negotiate_src_mono);
353   }
354   
355   // mono get (no sink pads)
356   if (sinkcount==0 && srccount == 1){
357     //g_print("get mode\n");
358     ladspa->newcaps = TRUE;
359     ladspa->samplerate = 44100;
360     ladspa->buffersize = 64;
361     gst_pad_set_get_function(ladspa->srcpads[0],gst_ladspa_get_mono);
362     //gst_pad_set_negotiate_function (ladspa->srcpads[0], gst_ladspa_negotiate_src_get_mono);
363     gst_ladspa_instantiate(ladspa);
364   }
365   
366   // multi srcpad get
367   if (sinkcount==0 && srccount > 1){
368     //g_print("multi get mode\n");
369     ladspa->newcaps = TRUE;
370     ladspa->samplerate = 44100;
371     ladspa->buffersize = 64;
372     gst_pad_set_get_function(ladspa->srcpads[0],gst_ladspa_get);
373     //gst_pad_set_negotiate_function (ladspa->srcpads[0], gst_ladspa_negotiate_src_get_mono);
374     gst_ladspa_instantiate(ladspa);
375     ladspa->buffers = g_new0(GstBuffer*,oclass->numsrcpads);
376   }
377 }
378
379 #if 0
380 static GstPadNegotiateReturn
381 gst_ladspa_negotiate_src_mono (GstPad *pad, GstCaps **caps, gpointer *data)
382 {
383   GstLADSPA *ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
384   
385   // have to instantiate ladspa plugin when samplerate changes (groan)
386   if (ladspa->samplerate != gst_caps_get_int (*caps, "rate")){
387     ladspa->samplerate = gst_caps_get_int (*caps, "rate");
388     if (!gst_ladspa_instantiate(ladspa)) return GST_PAD_NEGOTIATE_FAIL;
389   }
390   return gst_pad_negotiate_proxy (pad, ladspa->sinkpads[0], caps);
391 }
392
393 static GstPadNegotiateReturn
394 gst_ladspa_negotiate_sink_mono (GstPad *pad, GstCaps **caps, gpointer *data)
395 {
396   GstLADSPA *ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
397   
398   // have to instantiate ladspa plugin when samplerate changes (groan)
399   if (ladspa->samplerate != gst_caps_get_int (*caps, "rate")){
400     ladspa->samplerate = gst_caps_get_int (*caps, "rate");
401     if (!gst_ladspa_instantiate(ladspa)) return GST_PAD_NEGOTIATE_FAIL;
402   }
403   return gst_pad_negotiate_proxy (pad, ladspa->srcpads[0], caps);
404 }
405
406 static GstPadNegotiateReturn 
407 gst_ladspa_negotiate_src_get_mono (GstPad *pad, GstCaps **caps, gpointer *data) 
408 {
409   GstLADSPA *ladspa;
410   //g_print("gst_ladspa_negotiate_src_get_mono\n");
411   if (*caps) {
412     g_return_val_if_fail (pad != NULL, GST_PAD_NEGOTIATE_FAIL);
413     ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
414     ladspa->samplerate = gst_caps_get_int (*caps, "rate");
415     if (!gst_ladspa_instantiate(ladspa)) return GST_PAD_NEGOTIATE_FAIL;
416     return GST_PAD_NEGOTIATE_AGREE;
417   }
418   return GST_PAD_NEGOTIATE_FAIL;
419 }
420 #endif
421
422 static void
423 gst_ladspa_force_caps(GstLADSPA *ladspa, GstPad *pad) {
424   
425   // g_print("forcing caps\n");
426   gst_pad_try_set_caps (pad, gst_caps_new (
427     "ladspa_src_caps",
428     "audio/raw",
429     gst_props_new (
430       "format",     GST_PROPS_STRING ("float"),
431       "layout",     GST_PROPS_STRING ("gfloat"),
432       "intercept",  GST_PROPS_FLOAT(0.0),
433       "slope",      GST_PROPS_FLOAT(1.0),
434       "rate",       GST_PROPS_INT (ladspa->samplerate),
435       "channels",   GST_PROPS_INT (1),
436       NULL
437     )
438   ));
439   ladspa->newcaps=FALSE;
440 }
441
442 static void
443 gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
444 {
445   GstLADSPA *ladspa = (GstLADSPA*)object;
446   gint cid = prop_id - ARG_LAST;
447   GstLADSPAClass *oclass;
448   ladspa_control_info *control_info;
449   gfloat val=0.0;
450     
451   // these are only registered in get mode
452   switch (prop_id) {
453     case ARG_SAMPLERATE:
454       ladspa->samplerate = g_value_get_int (value);
455       ladspa->newcaps=TRUE;
456       break;
457     case ARG_BUFFERSIZE:
458       ladspa->buffersize = g_value_get_int (value);
459       break;
460   }
461   
462   // is it a ladspa plugin arg?
463   if (cid<0) return;
464
465 /*
466   if (id == ARG_LOOP_BASED) {
467     // we can only do this in NULL state
468     g_return_if_fail (GST_STATE(object) != GST_STATE_NULL);
469     ladspa->loopbased = g_value_get_boolean (value);
470     if (ladspa->loopbased) {
471       gst_element_set_loop_function (GST_ELEMENT (ladspa), gst_ladspa_loop);
472     } else {
473       gst_element_set_loop_function (GST_ELEMENT (ladspa), NULL);
474     }
475   }
476 */
477
478   oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
479
480   // verify it exists and is a control (not a port)
481   g_return_if_fail(cid < oclass->numcontrols);
482   
483   control_info = &(oclass->control_info[cid]);
484   g_return_if_fail (control_info->name != NULL);
485
486   // check to see if it's writable
487   g_return_if_fail (control_info->writable);
488
489   // g_print("set arg %s to %f\n",control_info->name,ladspa->controls[cid]);
490
491   // now see what type it is
492   if (control_info->toggled) {
493     if (g_value_get_boolean (value))
494       ladspa->controls[cid] = 1.0;
495     else
496       ladspa->controls[cid] = 0.0;
497   } else if (control_info->integer) {
498     val = (gfloat)g_value_get_int (value);
499     ladspa->controls[cid] = val;
500   } else {
501     val = g_value_get_float (value);
502     ladspa->controls[cid] = val;
503   }    
504 }
505
506 static void
507 gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
508 {
509   GstLADSPA *ladspa = (GstLADSPA*)object;
510   gint cid = prop_id - ARG_LAST;
511   GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
512   ladspa_control_info *control_info;
513
514   // these are only registered in get mode
515   switch (prop_id){
516     case ARG_SAMPLERATE:
517       g_value_set_int (value, ladspa->samplerate);
518       break;
519     case ARG_BUFFERSIZE:
520       g_value_set_int (value, ladspa->buffersize);
521       break;
522   }
523     
524   if (cid<0) return;
525   // verify it exists and is a control (not a port)
526   if (cid >= oclass->numcontrols) return;
527   control_info = &(oclass->control_info[cid]);
528   if (control_info->name == NULL) return;
529
530   //g_print("got arg %s as %f\n",control_info->name,ladspa->controls[cid]);
531
532   // now see what type it is
533   if (control_info->toggled) {
534     if (ladspa->controls[cid] == 1.0)
535       g_value_set_boolean (value, TRUE);
536     else
537       g_value_set_boolean (value, FALSE);
538   } else if (control_info->integer) {
539     g_value_set_int (value, (gint)ladspa->controls[cid]);
540   } else {
541     g_value_set_float (value, ladspa->controls[cid]);
542   }
543 }
544
545 static gboolean
546 gst_ladspa_instantiate (GstLADSPA *ladspa)
547 {
548   LADSPA_Descriptor *desc;
549   int i;
550   GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
551   gboolean was_activated;
552   
553   desc = ladspa->descriptor;
554   
555   // check for old handle
556   was_activated = ladspa->activated;
557   if (ladspa->handle != NULL){
558     gst_ladspa_deactivate(ladspa);
559     desc->cleanup(ladspa->handle);
560   }
561         
562   // instantiate the plugin  
563   ladspa->handle = desc->instantiate(desc,ladspa->samplerate);
564   g_return_val_if_fail (ladspa->handle != NULL, FALSE);
565
566   // walk through the ports and add all the arguments
567   for (i=0;i<oclass->numcontrols;i++) {
568     // connect the argument to the plugin
569     //g_print("added control port %d\n", oclass->control_portnums[i]);
570     desc->connect_port(ladspa->handle,
571                        oclass->control_portnums[i],
572                        &(ladspa->controls[i]));
573   }
574
575   // reactivate if it was activated before the reinstantiation
576   if (was_activated){
577     gst_ladspa_activate(ladspa);
578   }
579   return TRUE;
580 }
581
582 static GstElementStateReturn
583 gst_ladspa_change_state (GstElement *element)
584 {
585   LADSPA_Descriptor *desc;
586   GstLADSPA *ladspa = (GstLADSPA*)element;
587 //  GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT(ladspa)->klass);
588   desc = ladspa->descriptor;
589
590   //g_print("changing state\n");
591   switch (GST_STATE_TRANSITION (element)) {
592     case GST_STATE_NULL_TO_READY:
593       gst_ladspa_activate(ladspa);
594       break;
595     case GST_STATE_READY_TO_NULL:
596       gst_ladspa_deactivate(ladspa);
597       break;
598     default:
599       break;
600   }
601
602   if (GST_ELEMENT_CLASS (parent_class)->change_state)
603     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
604
605   return GST_STATE_SUCCESS;
606 }
607
608 static void
609 gst_ladspa_activate(GstLADSPA *ladspa)
610 {
611   LADSPA_Descriptor *desc;
612   desc = ladspa->descriptor;
613   
614   if (ladspa->activated){
615     gst_ladspa_deactivate(ladspa);
616   }
617   
618   //g_print("activating\n");
619
620   // activate the plugin (function might be null)
621   if (desc->activate != NULL){
622     desc->activate(ladspa->handle);
623   }
624   
625   ladspa->activated = TRUE;
626 }
627
628 static void
629 gst_ladspa_deactivate(GstLADSPA *ladspa)
630 {
631   LADSPA_Descriptor *desc;
632   desc = ladspa->descriptor;
633
634   //g_print("deactivating\n");
635
636   // deactivate the plugin (function might be null)  
637   if (ladspa->activated && desc->deactivate != NULL){
638     desc->deactivate(ladspa->handle);
639   }
640   ladspa->activated = FALSE;
641 }
642
643 static void
644 gst_ladspa_loop (GstElement *element)
645 {
646   int i;
647   GstLADSPA *ladspa = (GstLADSPA *)element;
648   GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
649   LADSPA_Descriptor *desc;
650
651   desc = ladspa->descriptor;
652   printf("looping something\n");
653
654   // first get all the necessary data from the input ports
655   for (i=0;i<oclass->numsinkpads;i++){  
656     ladspa->buffers[i] = gst_pad_pull(ladspa->sinkpads[i]);
657     printf("pulling buffer %d\n", i);
658   }
659     
660   for (i=0;i<oclass->numsinkpads;i++) {
661 //      desc->connect_port(ladspa->handle,i,&(ladspa->controls[i]));
662   }
663
664   for (i=0;i<oclass->numsrcpads && i<oclass->numsinkpads;i++){
665     printf("pushing buffer %d\n", i);
666     gst_pad_push (ladspa->srcpads[i], ladspa->buffers[i]);
667     ladspa->buffers[i] = NULL;
668   }
669 }
670
671 static void
672 gst_ladspa_chain_inplace_mono (GstPad *pad,GstBuffer *buf)
673 {
674   LADSPA_Descriptor *desc;
675   LADSPA_Data *data;
676   unsigned long num_samples;
677   
678   GstLADSPA *ladspa;
679   GstLADSPAClass *oclass;
680
681   g_return_if_fail(pad != NULL);
682   g_return_if_fail(GST_IS_PAD(pad));
683   g_return_if_fail(buf != NULL);
684
685   ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
686   g_return_if_fail(ladspa != NULL);
687
688   // this might happen if caps nego hasn't happened
689   g_return_if_fail(ladspa->handle != NULL);
690
691   oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
692   data=(LADSPA_Data*)GST_BUFFER_DATA(buf);
693   num_samples = GST_BUFFER_SIZE(buf) / sizeof(gfloat);
694   
695   desc = ladspa->descriptor;
696
697   desc->connect_port(ladspa->handle,oclass->sinkpad_portnums[0],data);
698   desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);
699
700   desc->run(ladspa->handle,num_samples);
701   
702   desc->connect_port(ladspa->handle,oclass->sinkpad_portnums[0],NULL);
703   desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],NULL);
704
705   gst_pad_push (ladspa->srcpads[0], buf);  
706 }
707
708 static GstBuffer *
709 gst_ladspa_get_mono(GstPad *pad)
710 {
711   LADSPA_Descriptor *desc;
712   LADSPA_Data *data;
713   
714   GstLADSPA *ladspa;
715   GstLADSPAClass *oclass;
716   GstBuffer *buf;
717
718   g_return_val_if_fail(pad != NULL, NULL);
719   g_return_val_if_fail(GST_IS_PAD(pad), NULL);
720
721   ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
722   g_return_val_if_fail(ladspa != NULL, NULL);
723
724   // this might happen if caps nego hasn't happened
725   g_return_val_if_fail(ladspa->handle != NULL, NULL);
726
727   oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
728   
729   if (ladspa->newcaps) {
730     gst_ladspa_force_caps(ladspa, ladspa->srcpads[0]);
731   }
732   
733   buf = gst_buffer_new();
734   g_return_val_if_fail (buf, NULL);
735   data = g_new(LADSPA_Data, ladspa->buffersize);
736   GST_BUFFER_DATA(buf) = (gpointer) data;
737   GST_BUFFER_SIZE(buf) = sizeof(LADSPA_Data) * ladspa->buffersize;
738   GST_BUFFER_TIMESTAMP(buf) = ladspa->timestamp;
739
740   desc = ladspa->descriptor;
741   desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);
742   desc->run(ladspa->handle,(unsigned long)ladspa->buffersize);  
743   desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],NULL);
744   
745   return buf;
746 }
747
748 static GstBuffer *
749 gst_ladspa_get(GstPad *pad)
750 {
751   LADSPA_Descriptor *desc;
752   LADSPA_Data *data;
753   
754   GstLADSPA *ladspa;
755   GstLADSPAClass *oclass;
756   GstBuffer *buf;
757
758   g_return_val_if_fail(pad != NULL, NULL);
759   g_return_val_if_fail(GST_IS_PAD(pad), NULL);
760
761   ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
762   g_return_val_if_fail(ladspa != NULL, NULL);
763
764   // this might happen if caps nego hasn't happened
765   g_return_val_if_fail(ladspa->handle != NULL, NULL);
766
767   oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
768   
769   if (ladspa->newcaps) {
770     gst_ladspa_force_caps(ladspa, ladspa->srcpads[0]);
771   }
772   
773   buf = gst_buffer_new();
774   g_return_val_if_fail (buf, NULL);
775   data = g_new(LADSPA_Data, ladspa->buffersize);
776   GST_BUFFER_DATA(buf) = (gpointer) data;
777   GST_BUFFER_SIZE(buf) = sizeof(LADSPA_Data) * ladspa->buffersize;
778   GST_BUFFER_TIMESTAMP(buf) = ladspa->timestamp;
779   ladspa->timestamp+= ladspa->buffersize * ladspa->samplerate * 10^9;
780
781   desc = ladspa->descriptor;
782   desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);
783   desc->run(ladspa->handle,(unsigned long)ladspa->buffersize);  
784   desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],NULL);
785   
786   return buf;
787 }
788
789 static void
790 ladspa_describe_plugin(const char *pcFullFilename,
791                        void *pvPluginHandle,
792                        LADSPA_Descriptor_Function pfDescriptorFunction)
793 {
794   const LADSPA_Descriptor *desc;
795   int i,j;
796   
797   GstElementDetails *details;
798   GTypeInfo typeinfo = {
799       sizeof(GstLADSPAClass),      NULL,
800       NULL,
801       (GClassInitFunc)gst_ladspa_class_init,
802       NULL,
803       NULL,
804       sizeof(GstLADSPA),
805       0,
806       (GInstanceInitFunc)gst_ladspa_init,
807   };
808   GType type;
809   GstElementFactory *factory;
810
811   // walk through all the plugins in this pluginlibrary
812   i = 0;
813   while ((desc = pfDescriptorFunction(i++))) {
814     gchar *type_name;
815
816     // construct the type
817     type_name = g_strdup_printf("ladspa_%s",desc->Label);
818     g_strcanon (type_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_+", '-');
819     // if it's already registered, drop it
820     if (g_type_from_name(type_name)) {
821       g_free(type_name);
822       continue;
823     }
824     // create the type now
825     type = g_type_register_static(GST_TYPE_ELEMENT, type_name , &typeinfo, 0);
826
827     // construct the element details struct
828     details = g_new0(GstElementDetails,1);
829     details->longname = g_strdup(desc->Name);
830     details->klass = "Filter/LADSPA";
831     details->description = details->longname;
832     details->version = g_strdup_printf("%ld",desc->UniqueID);
833     details->author = g_strdup(desc->Maker);
834     details->copyright = g_strdup(desc->Copyright);
835
836     // register the plugin with gstreamer
837     factory = gst_elementfactory_new(type_name,type,details);
838     g_return_if_fail(factory != NULL);
839     gst_plugin_add_feature (ladspa_plugin, GST_PLUGIN_FEATURE (factory));
840
841     // add this plugin to the hash
842     g_hash_table_insert(ladspa_descriptors,
843                         GINT_TO_POINTER(type),
844                         (gpointer)desc);
845     
846
847     // only add sink padtemplate if there are sinkpads                        
848     for (j=0;j<desc->PortCount;j++) {
849       if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j]) &&
850           LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j])) {
851         sinktempl = ladspa_sink_factory();
852         gst_elementfactory_add_padtemplate (factory, sinktempl);
853         break;
854       }
855     }
856   
857     srctempl = ladspa_src_factory();
858     gst_elementfactory_add_padtemplate (factory, srctempl);
859
860   }
861 }
862
863 static gboolean
864 plugin_init (GModule *module, GstPlugin *plugin)
865 {
866   
867   ladspa_descriptors = g_hash_table_new(NULL,NULL);
868   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
869
870   ladspa_plugin = plugin;
871
872   LADSPAPluginSearch(ladspa_describe_plugin);
873
874   return TRUE;
875 }
876
877 GstPluginDesc plugin_desc = {
878   GST_VERSION_MAJOR,
879   GST_VERSION_MINOR,
880   "ladspa",
881   plugin_init
882 };
883