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