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