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