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