remove copyright field from plugins
[platform/upstream/gst-plugins-good.git] / ext / ladspa / gstladspa.c
index 53f1d5e..6ecb3ce 100644 (file)
@@ -1,6 +1,7 @@
 /* GStreamer
  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
  *               <2001> Steve Baker <stevebaker_org@yahoo.co.uk>
+ *               2003 Andy Wingo <wingo at pobox.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  * Boston, MA 02111-1307, USA.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 #include <string.h>
 #include <math.h>
+#include <gst/control/control.h>
+#include <gst/audio/audio.h>
 
-//#define DEBUG_ENABLED
 #include "gstladspa.h"
-#include "ladspa.h"
-#include "search.h"
-#include "utils.h"
+#include <ladspa.h>     /* main ladspa sdk include file */
+#include "utils.h"      /* ladspa sdk utility functions */
 
+/* 1.0 and the 1.1 preliminary headers don't define a version, but 1.1 final
+   does */
+#ifndef LADSPA_VERSION
+#define LADSPA_VERSION "1.0"
+#endif
 
-static GstPadTemplate* 
-ladspa_src_factory (void)
+/* takes ownership of the name */
+static GstPadTemplate*
+ladspa_sink_factory (gchar *name)
 {
-  return 
-    gst_padtemplate_new (
-    "src",
-    GST_PAD_SRC,
-    GST_PAD_ALWAYS,
-    gst_caps_new (
-    "ladspa_src",
-      "audio/raw",
-      gst_props_new (
-        "format",     GST_PROPS_STRING ("float"),
-        "layout",     GST_PROPS_STRING ("gfloat"),
-        "intercept",  GST_PROPS_FLOAT(0.0),
-        "slope",      GST_PROPS_FLOAT(1.0),
-        "channels",   GST_PROPS_INT (1),
-        "rate",       GST_PROPS_INT_RANGE (0,G_MAXINT),
-      NULL)),
-    NULL);
+  return GST_PAD_TEMPLATE_NEW (
+  name,
+  GST_PAD_SINK,
+  GST_PAD_ALWAYS,
+  gst_caps_new (
+    "ladspa_sink",
+    "audio/x-raw-float",
+    GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS
+    )
+  );
 }
 
-static GstPadTemplate* 
-ladspa_sink_factory (void) 
+/* takes ownership of the name */
+static GstPadTemplate*
+ladspa_src_factory (gchar *name)
 {
-  return 
-    gst_padtemplate_new (
-      "sink",
-      GST_PAD_SINK,
-      GST_PAD_ALWAYS,
-      gst_caps_new (
-      "float2int_sink",
-        "audio/raw",
-        gst_props_new (
-          "format",     GST_PROPS_STRING ("float"),
-          "layout",     GST_PROPS_STRING ("gfloat"),
-          "intercept",  GST_PROPS_FLOAT(0.0),
-          "slope",      GST_PROPS_FLOAT(1.0),
-          "channels",   GST_PROPS_INT (1),
-          "rate",       GST_PROPS_INT_RANGE (0,G_MAXINT),
-      NULL)),
-    NULL);
+  return GST_PAD_TEMPLATE_NEW (
+  name,
+  GST_PAD_SRC,
+  GST_PAD_ALWAYS,
+  gst_caps_new (
+    "ladspa_src",
+    "audio/x-raw-float",
+    GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS
+    )
+  );
 }
 
-enum {
-  ARG_0,
-  ARG_LOOP_BASED,
-  ARG_SAMPLERATE,
-  ARG_BUFFERSIZE,
-  ARG_LAST,
-};
-
-static GstPadTemplate *srctempl, *sinktempl;
-
 static void                    gst_ladspa_class_init           (GstLADSPAClass *klass);
+static void                    gst_ladspa_base_init            (GstLADSPAClass *klass);
 static void                    gst_ladspa_init                 (GstLADSPA *ladspa);
 
-//static GstPadNegotiateReturn gst_ladspa_negotiate_sink_mono (GstPad *pad, GstCaps **caps, gpointer *data);
-//static GstPadNegotiateReturn gst_ladspa_negotiate_src_mono (GstPad *pad, GstCaps **caps, gpointer *data);
-//static GstPadNegotiateReturn gst_ladspa_negotiate_src_get_mono (GstPad *pad, GstCaps **caps, gpointer *data);
-static void gst_ladspa_force_caps(GstLADSPA *ladspa, GstPad *pad);
+static void                    gst_ladspa_update_int           (const GValue *value, gpointer data);
+static GstPadLinkReturn                gst_ladspa_link                 (GstPad *pad, GstCaps *caps);
+static void                    gst_ladspa_force_src_caps       (GstLADSPA *ladspa, GstPad *pad);
 
 static void                    gst_ladspa_set_property         (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
 static void                    gst_ladspa_get_property         (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
 
-static gboolean gst_ladspa_instantiate (GstLADSPA *ladspa);
-static void gst_ladspa_activate(GstLADSPA *ladspa);
-static void gst_ladspa_deactivate(GstLADSPA *ladspa);
+static gboolean                        gst_ladspa_instantiate          (GstLADSPA *ladspa);
+static void                    gst_ladspa_activate             (GstLADSPA *ladspa);
+static void                    gst_ladspa_deactivate           (GstLADSPA *ladspa);
 
 static GstElementStateReturn   gst_ladspa_change_state         (GstElement *element);
 static void                    gst_ladspa_loop                 (GstElement *element);
-static void      gst_ladspa_chain_inplace_mono (GstPad *pad,GstBuffer *buf);
-static GstBuffer * gst_ladspa_get_mono(GstPad *pad);
-static GstBuffer * gst_ladspa_get(GstPad *pad);
+static void                    gst_ladspa_chain                (GstPad *pad,GstData *_data);
+static GstData *               gst_ladspa_get                  (GstPad *pad);
 
 static GstElementClass *parent_class = NULL;
-//static guint gst_ladspa_signals[LAST_SIGNAL] = { 0 };
 
 static GstPlugin *ladspa_plugin;
 static GHashTable *ladspa_descriptors;
 
+enum {
+  ARG_0,
+  ARG_SAMPLERATE,
+  ARG_BUFFERSIZE,
+  ARG_LAST,
+};
+
+GST_DEBUG_CATEGORY_STATIC (ladspa_debug);
+#define DEBUG(...) \
+    GST_CAT_LEVEL_LOG (ladspa_debug, GST_LEVEL_DEBUG, NULL, __VA_ARGS__)
+#define DEBUG_OBJ(obj,...) \
+    GST_CAT_LEVEL_LOG (ladspa_debug, GST_LEVEL_DEBUG, obj, __VA_ARGS__)
+
+static void
+gst_ladspa_base_init (GstLADSPAClass *klass)
+{
+  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
+  GstPadTemplate *templ;
+  GstElementDetails *details;
+  LADSPA_Descriptor *desc;
+  gint j, sinkcount,srccount;
+
+  desc = g_hash_table_lookup(ladspa_descriptors,
+               GINT_TO_POINTER(G_TYPE_FROM_CLASS(klass)));
+  if (!desc)
+    desc = g_hash_table_lookup(ladspa_descriptors, GINT_TO_POINTER(0));
+  g_assert (desc);
+
+  /* construct the element details struct */
+  details = g_new0(GstElementDetails,1);
+  details->longname = g_strdup(desc->Name);
+  details->klass = "Filter/Effect/Audio/LADSPA";
+  details->description = details->longname;
+  details->author = g_strdup(desc->Maker);
+  gst_element_class_set_details (element_class, details);
+
+  /* pad templates */
+  klass->numports = desc->PortCount;
+  klass->numsinkpads = 0;
+  klass->numsrcpads = 0;
+  for (j=0;j<desc->PortCount;j++) {
+    if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j])) {
+      gchar *name = g_strdup((gchar *)desc->PortNames[j]);
+      g_strcanon (name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
+
+      /* the factories take ownership of the name */
+      if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j])) {
+        templ = ladspa_sink_factory (name);
+        klass->numsinkpads++;
+      } else {
+        templ = ladspa_src_factory (name);
+        klass->numsrcpads++;
+      }
+
+      gst_element_class_add_pad_template (element_class, templ);
+    }
+  }
+
+  klass->srcpad_portnums = g_new0(gint,klass->numsrcpads);
+  klass->sinkpad_portnums = g_new0(gint,klass->numsinkpads);
+  sinkcount = 0;
+  srccount = 0;
+
+  /* walk through the ports, note the portnums for srcpads, sinkpads */
+  for (j=0; j<desc->PortCount; j++) {
+    if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j])) {
+      if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j]))
+        klass->sinkpad_portnums[sinkcount++] = j;
+      else
+        klass->srcpad_portnums[srccount++] = j;
+    }
+  }
+
+  klass->descriptor = desc;
+}
+
 static void
 gst_ladspa_class_init (GstLADSPAClass *klass)
 {
   GObjectClass *gobject_class;
   GstElementClass *gstelement_class;
   LADSPA_Descriptor *desc;
-  gint i,current_portnum,sinkcount,srccount,controlcount;
+  gint i,current_portnum,controlcount;
   gint hintdesc;
   gint argtype,argperms;
   GParamSpec *paramspec = NULL;
-  gchar *argname, *tempstr;
+  gchar *argname, *tempstr, *paren;
 
   gobject_class = (GObjectClass*)klass;
   gstelement_class = (GstElementClass*)klass;
@@ -129,88 +190,46 @@ gst_ladspa_class_init (GstLADSPAClass *klass)
 
   gstelement_class->change_state = gst_ladspa_change_state;
 
-  g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LOOP_BASED,
-    g_param_spec_boolean("loop-based","loop-based","loop-based",
-                         FALSE,G_PARAM_READWRITE));
+  /* look up and store the ladspa descriptor */
+  desc = g_hash_table_lookup(ladspa_descriptors,
+               GINT_TO_POINTER(G_TYPE_FROM_CLASS(klass)));
+  if (!desc)
+    desc = g_hash_table_lookup(ladspa_descriptors, GINT_TO_POINTER(0));
+  g_assert (desc);
 
-  // look up and store the ladspa descriptor
-  klass->descriptor = g_hash_table_lookup(ladspa_descriptors,GINT_TO_POINTER(G_TYPE_FROM_CLASS(klass)));
-  desc = klass->descriptor;
-
-  klass->numports = desc->PortCount;
-
-  klass->numsinkpads = 0;
-  klass->numsrcpads = 0;
   klass->numcontrols = 0;
 
-  // walk through the ports, count the input, output and control ports
-  for (i=0;i<desc->PortCount;i++) {
-    if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) && 
-        LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])){
-      klass->numsinkpads++;
-    }
-      
-    if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) && 
-        LADSPA_IS_PORT_OUTPUT(desc->PortDescriptors[i])){
-      klass->numsrcpads++;
-    }
-      
-    if (LADSPA_IS_PORT_CONTROL(desc->PortDescriptors[i]) && 
-        LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])){
+  /* walk through the ports, count the input, output and control ports */
+  for (i=0; i<desc->PortCount; i++) {
+    if (!LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
+        LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
       klass->numcontrols++;
-    }
   }
 
-  klass->srcpad_portnums = g_new0(gint,klass->numsrcpads);
-  klass->sinkpad_portnums = g_new0(gint,klass->numsinkpads);
+  DEBUG ("ladspa element class: init %s with %d sink, %d src, %d control\n",
+         g_type_name (G_TYPE_FROM_CLASS (klass)),
+         klass->numsinkpads, klass->numsrcpads, klass->numcontrols);
+
   klass->control_portnums = g_new0(gint,klass->numcontrols);
-  sinkcount = 0;
-  srccount = 0;
   controlcount = 0;
 
-  // walk through the ports, note the portnums for srcpads, sinkpads and control params
-  for (i=0;i<desc->PortCount;i++) {
-    if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) && 
-        LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])){
-      //g_print("input port %d\n", i);
-      klass->sinkpad_portnums[sinkcount++] = i;
-    }
-      
-    if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) && 
-        LADSPA_IS_PORT_OUTPUT(desc->PortDescriptors[i])){
-      //g_print("output port %d\n", i);
-      klass->srcpad_portnums[srccount++] = i;
-    }
-      
-    if (LADSPA_IS_PORT_CONTROL(desc->PortDescriptors[i]) && 
-        LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])){
-      //g_print("control port %d\n", i);
+  /* walk through the ports, note the portnums for control params */
+  for (i=0; i<desc->PortCount; i++) {
+    if (!LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
+        LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i]))
       klass->control_portnums[controlcount++] = i;
-    }
   }
 
-  // no sink pads - we'll use get mode and add params for samplerate and buffersize
-  if (klass->numsinkpads == 0 && klass->numsrcpads > 0){
-    g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_SAMPLERATE,
-      g_param_spec_int("samplerate","samplerate","samplerate",
-                      0,G_MAXINT,44100,G_PARAM_READWRITE));
-    g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_BUFFERSIZE,
-      g_param_spec_int("buffersize","buffersize","buffersize",
-                      0,G_MAXINT,64,G_PARAM_READWRITE));
-
-  }
-  
-  // now build the contorl info from the control ports
+  /* now build the control info from the control ports */
   klass->control_info = g_new0(ladspa_control_info,klass->numcontrols);
     
   for (i=0;i<klass->numcontrols;i++) {
-
     current_portnum = klass->control_portnums[i];
     
-    // short name for hint descriptor
+    /* short name for hint descriptor */
     hintdesc = desc->PortRangeHints[current_portnum].HintDescriptor;
 
-    // get the various bits
+    /* get the various bits */
     if (LADSPA_IS_HINT_TOGGLED(hintdesc))
       klass->control_info[i].toggled = TRUE;
     if (LADSPA_IS_HINT_LOGARITHMIC(hintdesc))
@@ -218,32 +237,80 @@ gst_ladspa_class_init (GstLADSPAClass *klass)
     if (LADSPA_IS_HINT_INTEGER(hintdesc))
       klass->control_info[i].integer = TRUE;
 
-    // figure out the argument details
+    /* figure out the argument details */
     if (klass->control_info[i].toggled) argtype = G_TYPE_BOOLEAN;
     else if (klass->control_info[i].integer) argtype = G_TYPE_INT;
     else argtype = G_TYPE_FLOAT;
 
-    // grab the bounds
+    /* grab the bounds */
     if (LADSPA_IS_HINT_BOUNDED_BELOW(hintdesc)) {
       klass->control_info[i].lower = TRUE;
       klass->control_info[i].lowerbound =
         desc->PortRangeHints[current_portnum].LowerBound;
     } else {
       if (argtype==G_TYPE_INT) klass->control_info[i].lowerbound = (gfloat)G_MININT;
-      if (argtype==G_TYPE_FLOAT) klass->control_info[i].lowerbound = G_MINFLOAT;
+      if (argtype==G_TYPE_FLOAT) klass->control_info[i].lowerbound = -G_MAXFLOAT;
     }
     
     if (LADSPA_IS_HINT_BOUNDED_ABOVE(hintdesc)) {
       klass->control_info[i].upper = TRUE;
       klass->control_info[i].upperbound =
         desc->PortRangeHints[current_portnum].UpperBound;
-      if (LADSPA_IS_HINT_SAMPLE_RATE(hintdesc))
+      if (LADSPA_IS_HINT_SAMPLE_RATE(hintdesc)) {
         klass->control_info[i].samplerate = TRUE;
+        klass->control_info[i].upperbound *= 44100; /* FIXME? */
+      }
     } else {
       if (argtype==G_TYPE_INT) klass->control_info[i].upperbound = (gfloat)G_MAXINT;
       if (argtype==G_TYPE_FLOAT) klass->control_info[i].upperbound = G_MAXFLOAT;
     }
 
+    /* use the lowerbound as the default value */
+    klass->control_info[i].def = klass->control_info[i].lowerbound;
+
+#ifdef LADSPA_IS_HINT_HAS_DEFAULT
+    /* figure out the defaults */
+    if (LADSPA_IS_HINT_HAS_DEFAULT (hintdesc)) {
+      if (LADSPA_IS_HINT_DEFAULT_MINIMUM (hintdesc))
+        klass->control_info[i].def = klass->control_info[i].lowerbound;
+      else if (LADSPA_IS_HINT_DEFAULT_LOW (hintdesc))
+        if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
+          klass->control_info[i].def = exp (0.75*log(klass->control_info[i].lowerbound) +
+                                                0.25*log(klass->control_info[i].upperbound));
+        else
+          klass->control_info[i].def = (0.75*klass->control_info[i].lowerbound +
+                                            0.25*klass->control_info[i].upperbound);
+      else if (LADSPA_IS_HINT_DEFAULT_MIDDLE (hintdesc))
+        if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
+          klass->control_info[i].def = exp (0.5*log(klass->control_info[i].lowerbound) +
+                                                0.5*log(klass->control_info[i].upperbound));
+        else
+          klass->control_info[i].def = (0.5*klass->control_info[i].lowerbound +
+                                            0.5*klass->control_info[i].upperbound);
+      else if (LADSPA_IS_HINT_DEFAULT_HIGH (hintdesc))
+        if (LADSPA_IS_HINT_LOGARITHMIC (hintdesc))
+          klass->control_info[i].def = exp (0.25*log(klass->control_info[i].lowerbound) +
+                                                0.75*log(klass->control_info[i].upperbound));
+        else
+          klass->control_info[i].def = (0.25*klass->control_info[i].lowerbound +
+                                            0.75*klass->control_info[i].upperbound);
+      else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM (hintdesc))
+        klass->control_info[i].def = klass->control_info[i].upperbound;
+      else if (LADSPA_IS_HINT_DEFAULT_0 (hintdesc))
+        klass->control_info[i].def = 0.0;
+      else if (LADSPA_IS_HINT_DEFAULT_1 (hintdesc))
+        klass->control_info[i].def = 1.0;
+      else if (LADSPA_IS_HINT_DEFAULT_100 (hintdesc))
+        klass->control_info[i].def = 100.0;
+      else if (LADSPA_IS_HINT_DEFAULT_440 (hintdesc))
+        klass->control_info[i].def = 440.0;
+    }
+#endif /* LADSPA_IS_HINT_HAS_DEFAULT */
+
+    klass->control_info[i].def = CLAMP(klass->control_info[i].def,
+                                       klass->control_info[i].lowerbound,
+                                       klass->control_info[i].upperbound);
+    
     if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[current_portnum])) {
       argperms = G_PARAM_READWRITE;
       klass->control_info[i].writable = TRUE;
@@ -254,6 +321,12 @@ gst_ladspa_class_init (GstLADSPAClass *klass)
 
     klass->control_info[i].name = g_strdup(desc->PortNames[current_portnum]);
     argname = g_strdup(klass->control_info[i].name);
+    /* find out if there is a (unitname) at the end of the argname and get rid
+       of it */
+    paren = g_strrstr (argname, " (");
+    if (paren != NULL) {
+      *paren = '\0';
+    }
     /* this is the same thing that param_spec_* will do */
     g_strcanon (argname, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
     /* satisfy glib2 (argname[0] must be [A-Za-z]) */
@@ -274,272 +347,272 @@ gst_ladspa_class_init (GstLADSPAClass *klass)
       argname = numargname;
     }
     
-    g_print("adding arg %s from %s\n",argname, klass->control_info[i].name);
+    klass->control_info[i].param_name = argname;
+    
+    DEBUG ("adding arg %s from %s", argname, klass->control_info[i].name);
     
     if (argtype==G_TYPE_BOOLEAN){
       paramspec = g_param_spec_boolean(argname,argname,argname, FALSE, argperms);
     } else if (argtype==G_TYPE_INT){      
       paramspec = g_param_spec_int(argname,argname,argname, 
-        (gint)klass->control_info[i].lowerbound, (gint)klass->control_info[i].upperbound, 0, argperms);
+        (gint)klass->control_info[i].lowerbound, 
+        (gint)klass->control_info[i].upperbound, 
+        (gint)klass->control_info[i].def, argperms);
+    } else if (klass->control_info[i].samplerate){
+      paramspec = g_param_spec_float(argname,argname,argname, 
+        0.0, G_MAXFLOAT, 
+        0.0, argperms);
     } else {
       paramspec = g_param_spec_float(argname,argname,argname, 
         klass->control_info[i].lowerbound, klass->control_info[i].upperbound, 
-        (klass->control_info[i].lowerbound + klass->control_info[i].upperbound) / 2.0f, argperms);
+        klass->control_info[i].def, argperms);
     }
     
-    g_object_class_install_property(G_OBJECT_CLASS(klass), i+ARG_LAST, paramspec);
+    /* properties have an offset of 1 */
+    g_object_class_install_property(G_OBJECT_CLASS(klass), i+1, paramspec);
   }
 }
 
 static void
 gst_ladspa_init (GstLADSPA *ladspa)
 {
-  GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
-  
+  GstLADSPAClass *oclass;
+  ladspa_control_info cinfo;
+  GList *l;
   LADSPA_Descriptor *desc;
-  gint i,sinkcount,srccount,controlcount;
+  gint i,sinkcount,srccount;
 
+  oclass = (GstLADSPAClass*)G_OBJECT_GET_CLASS (ladspa);
   desc = oclass->descriptor;
   ladspa->descriptor = oclass->descriptor;
   
-  // allocate the various arrays
+  /* allocate the various arrays */
   ladspa->srcpads = g_new0(GstPad*,oclass->numsrcpads);
   ladspa->sinkpads = g_new0(GstPad*,oclass->numsinkpads);
   ladspa->controls = g_new(gfloat,oclass->numcontrols);
-
-  // walk through the ports and add all the pads
+  ladspa->dpman = gst_dpman_new ("ladspa_dpman", GST_ELEMENT(ladspa));
+  
+  /* set up pads */
   sinkcount = 0;
   srccount = 0;
-  controlcount = 0;
-  for (i=0;i<desc->PortCount;i++) {
-    if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
-        LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
-      ladspa->sinkpads[sinkcount] = gst_pad_new_from_template 
-      (sinktempl, (gchar *)desc->PortNames[i]);
-      gst_element_add_pad(GST_ELEMENT(ladspa),ladspa->sinkpads[sinkcount]);
-      sinkcount++;
-    }
-    if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
-        LADSPA_IS_PORT_OUTPUT(desc->PortDescriptors[i])) {
-      ladspa->srcpads[srccount] = gst_pad_new_from_template 
-        (srctempl, (gchar *)desc->PortNames[i]);
-      gst_element_add_pad(GST_ELEMENT(ladspa),ladspa->srcpads[srccount]);
-      srccount++;
-    }
-    if (LADSPA_IS_PORT_CONTROL(desc->PortDescriptors[i]) &&
-        LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
-          
-      // use the lowerbound as the default value if it exists
-      if (oclass->control_info[controlcount].lower){
-        ladspa->controls[controlcount]=oclass->control_info[controlcount].lowerbound;
-      } else {
-        ladspa->controls[controlcount] = 0.0;
+  for (l=GST_ELEMENT_CLASS (oclass)->padtemplates; l; l=l->next) {
+    GstPad *pad = gst_pad_new_from_template (GST_PAD_TEMPLATE (l->data),
+                                             GST_PAD_TEMPLATE_NAME_TEMPLATE (l->data));
+    gst_pad_set_link_function (pad, gst_ladspa_link);
+    gst_element_add_pad ((GstElement*)ladspa, pad);
+
+    if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK)
+      ladspa->sinkpads[sinkcount++] = pad;
+    else
+      ladspa->srcpads[srccount++] = pad;
+  }
+  
+  /* set up dparams */
+  for (i=0; i<oclass->numcontrols; i++) {
+    if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
+      cinfo = oclass->control_info[i];
+      ladspa->controls[i]=cinfo.def;
+      
+      if (cinfo.toggled){
+        gst_dpman_add_required_dparam_callback (
+          ladspa->dpman, 
+          g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
+                           0, 1, (gint)(ladspa->controls[i]), G_PARAM_READWRITE),
+          "int", gst_ladspa_update_int, &(ladspa->controls[i])
+        );
+      }
+      else if (cinfo.integer){
+        gst_dpman_add_required_dparam_callback (
+          ladspa->dpman, 
+          g_param_spec_int(cinfo.param_name, cinfo.name, cinfo.name,
+                           (gint)cinfo.lowerbound, (gint)cinfo.upperbound,
+                           (gint)ladspa->controls[i], G_PARAM_READWRITE),
+          "int", gst_ladspa_update_int, &(ladspa->controls[i])
+        );
+      }
+      else if (cinfo.samplerate){
+        gst_dpman_add_required_dparam_direct (
+          ladspa->dpman, 
+          g_param_spec_float(cinfo.param_name, cinfo.name, cinfo.name,
+                           cinfo.lowerbound, cinfo.upperbound,
+                           ladspa->controls[i], G_PARAM_READWRITE),
+          "hertz-rate-bound", &(ladspa->controls[i])
+        );
+      }
+      else {
+        gst_dpman_add_required_dparam_direct (
+          ladspa->dpman, 
+          g_param_spec_float(cinfo.param_name, cinfo.name, cinfo.name,
+                           cinfo.lowerbound, cinfo.upperbound,
+                           ladspa->controls[i], G_PARAM_READWRITE),
+          "float", &(ladspa->controls[i])
+        );
       }
-      controlcount++;
     }
   }
 
-  ladspa->samplerate = 0;
-  ladspa->buffersize = 0;
-  ladspa->newcaps = FALSE;
+  /* nonzero default needed to instantiate() some plugins */
+  ladspa->samplerate = 44100;
+
+  ladspa->buffer_frames = 0; /* should be set with caps */
   ladspa->activated = FALSE;
-  
-  // mono chain
-  if (sinkcount==1 && srccount==1){
-    //g_print("inplace mono chain mode\n");
-    //gst_pad_set_negotiate_function (ladspa->sinkpads[0], gst_ladspa_negotiate_sink_mono);
-    gst_pad_set_chain_function(ladspa->sinkpads[0],gst_ladspa_chain_inplace_mono);
-    //gst_pad_set_negotiate_function (ladspa->srcpads[0], gst_ladspa_negotiate_src_mono);
-  }
-  
-  // mono get (no sink pads)
-  if (sinkcount==0 && srccount == 1){
-    //g_print("get mode\n");
-    ladspa->newcaps = TRUE;
-    ladspa->samplerate = 44100;
-    ladspa->buffersize = 64;
-    gst_pad_set_get_function(ladspa->srcpads[0],gst_ladspa_get_mono);
-    //gst_pad_set_negotiate_function (ladspa->srcpads[0], gst_ladspa_negotiate_src_get_mono);
-    gst_ladspa_instantiate(ladspa);
-  }
-  
-  // multi srcpad get
-  if (sinkcount==0 && srccount > 1){
-    //g_print("multi get mode\n");
-    ladspa->newcaps = TRUE;
-    ladspa->samplerate = 44100;
-    ladspa->buffersize = 64;
-    gst_pad_set_get_function(ladspa->srcpads[0],gst_ladspa_get);
-    //gst_pad_set_negotiate_function (ladspa->srcpads[0], gst_ladspa_negotiate_src_get_mono);
-    gst_ladspa_instantiate(ladspa);
-    ladspa->buffers = g_new0(GstBuffer*,oclass->numsrcpads);
+  ladspa->bufpool = NULL;
+  ladspa->inplace_broken = LADSPA_IS_INPLACE_BROKEN(ladspa->descriptor->Properties);
+
+  if (sinkcount==0 && srccount == 1) {
+    /* get mode (no sink pads) */
+    DEBUG_OBJ (ladspa, "mono get mode with 1 src pad");
+
+    gst_pad_set_get_function (ladspa->srcpads[0], gst_ladspa_get);
+  } else if (sinkcount==1){
+    /* with one sink we can use the chain function */
+    DEBUG_OBJ (ladspa, "chain mode");
+
+    gst_pad_set_chain_function (ladspa->sinkpads[0], gst_ladspa_chain);
+  } else if (sinkcount > 1){
+    /* more than one sink pad needs loop mode */
+    DEBUG_OBJ (ladspa, "loop mode with %d sink pads and %d src pads", sinkcount, srccount);
+
+    gst_element_set_loop_function (GST_ELEMENT (ladspa), gst_ladspa_loop);
+  } else if (sinkcount==0 && srccount == 0) {
+    /* for example, a plugin with only control inputs and output -- just ignore
+     * it for now */
+  } else {
+    g_warning ("%d sink pads, %d src pads not yet supported", sinkcount, srccount);
   }
+
+  gst_ladspa_instantiate (ladspa);
 }
 
-#if 0
-static GstPadNegotiateReturn
-gst_ladspa_negotiate_src_mono (GstPad *pad, GstCaps **caps, gpointer *data)
+static void
+gst_ladspa_update_int(const GValue *value, gpointer data)
 {
-  GstLADSPA *ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
-  
-  // have to instantiate ladspa plugin when samplerate changes (groan)
-  if (ladspa->samplerate != gst_caps_get_int (*caps, "rate")){
-    ladspa->samplerate = gst_caps_get_int (*caps, "rate");
-    if (!gst_ladspa_instantiate(ladspa)) return GST_PAD_NEGOTIATE_FAIL;
-  }
-  return gst_pad_negotiate_proxy (pad, ladspa->sinkpads[0], caps);
+  gfloat *target = (gfloat*) data;
+  *target = (gfloat)g_value_get_int(value);
 }
 
-static GstPadNegotiateReturn
-gst_ladspa_negotiate_sink_mono (GstPad *pad, GstCaps **caps, gpointer *data)
+static GstPadLinkReturn
+gst_ladspa_link (GstPad *pad, GstCaps *caps)
 {
-  GstLADSPA *ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
-  
-  // have to instantiate ladspa plugin when samplerate changes (groan)
-  if (ladspa->samplerate != gst_caps_get_int (*caps, "rate")){
-    ladspa->samplerate = gst_caps_get_int (*caps, "rate");
-    if (!gst_ladspa_instantiate(ladspa)) return GST_PAD_NEGOTIATE_FAIL;
+  GstElement *element = (GstElement*)GST_PAD_PARENT (pad);
+  GstLADSPA *ladspa = (GstLADSPA*)element;
+  const GList *l = NULL;
+  gint rate;
+
+  if (GST_CAPS_IS_FIXED (caps)) {
+    /* if this fails in some other plugin, the graph is left in an inconsistent
+       state */
+    for (l=gst_element_get_pad_list (element); l; l=l->next)
+      if (pad != (GstPad*)l->data)
+        if (gst_pad_try_set_caps ((GstPad*)l->data, caps) <= 0)
+          return GST_PAD_LINK_REFUSED;
+    
+    /* we assume that the ladspa plugin can handle any sample rate, so this
+       check gets put last */
+    gst_caps_get_int (caps, "rate", &rate);
+    /* have to instantiate ladspa plugin when samplerate changes (groan) */
+    if (ladspa->samplerate != rate) {
+      ladspa->samplerate = rate;
+      if (! gst_ladspa_instantiate(ladspa))
+        return GST_PAD_LINK_REFUSED;
+    }
+    
+    gst_caps_get_int (caps, "buffer-frames", &ladspa->buffer_frames);
+    
+    if (ladspa->bufpool)
+      gst_buffer_pool_unref (ladspa->bufpool);
+    ladspa->bufpool = gst_buffer_pool_get_default (ladspa->buffer_frames * sizeof(gfloat),
+                                                   3);
+    
+    return GST_PAD_LINK_OK;
   }
-  return gst_pad_negotiate_proxy (pad, ladspa->srcpads[0], caps);
+  
+  return GST_PAD_LINK_DELAYED;
 }
 
-static GstPadNegotiateReturn 
-gst_ladspa_negotiate_src_get_mono (GstPad *pad, GstCaps **caps, gpointer *data) 
+static void
+gst_ladspa_force_src_caps(GstLADSPA *ladspa, GstPad *pad)
 {
-  GstLADSPA *ladspa;
-  //g_print("gst_ladspa_negotiate_src_get_mono\n");
-  if (*caps) {
-    g_return_val_if_fail (pad != NULL, GST_PAD_NEGOTIATE_FAIL);
-    ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
-    ladspa->samplerate = gst_caps_get_int (*caps, "rate");
-    if (!gst_ladspa_instantiate(ladspa)) return GST_PAD_NEGOTIATE_FAIL;
-    return GST_PAD_NEGOTIATE_AGREE;
+  if (!ladspa->buffer_frames) {
+    ladspa->buffer_frames = 256; /* 5 ms at 44100 kHz (just a default...) */
+    g_return_if_fail (ladspa->bufpool == NULL);
+    ladspa->bufpool =
+      gst_buffer_pool_get_default (ladspa->buffer_frames * sizeof(gfloat), 3);
   }
-  return GST_PAD_NEGOTIATE_FAIL;
-}
-#endif
 
-static void
-gst_ladspa_force_caps(GstLADSPA *ladspa, GstPad *pad) {
-  
-  // g_print("forcing caps\n");
-  gst_pad_try_set_caps (pad, gst_caps_new (
+  DEBUG_OBJ (ladspa, "forcing caps with rate=%d, buffer-frames=%d",
+             ladspa->samplerate, ladspa->buffer_frames);
+
+  gst_pad_try_set_caps (pad,
+    gst_caps_new (
     "ladspa_src_caps",
-    "audio/raw",
+    "audio/x-raw-float",
     gst_props_new (
-      "format",     GST_PROPS_STRING ("float"),
-      "layout",     GST_PROPS_STRING ("gfloat"),
-      "intercept",  GST_PROPS_FLOAT(0.0),
-      "slope",      GST_PROPS_FLOAT(1.0),
-      "rate",       GST_PROPS_INT (ladspa->samplerate),
-      "channels",   GST_PROPS_INT (1),
-      NULL
-    )
-  ));
-  ladspa->newcaps=FALSE;
+      "width",          GST_PROPS_INT (32),
+      "endianness",     GST_PROPS_INT (G_BYTE_ORDER),
+      "rate",           GST_PROPS_INT (ladspa->samplerate),
+      "buffer-frames", GST_PROPS_INT (ladspa->buffer_frames),
+      "channels",      GST_PROPS_INT (1),
+      NULL)));
 }
 
 static void
 gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
 {
   GstLADSPA *ladspa = (GstLADSPA*)object;
-  gint cid = prop_id - ARG_LAST;
   GstLADSPAClass *oclass;
   ladspa_control_info *control_info;
-  gfloat val=0.0;
-    
-  // these are only registered in get mode
-  switch (prop_id) {
-    case ARG_SAMPLERATE:
-      ladspa->samplerate = g_value_get_int (value);
-      ladspa->newcaps=TRUE;
-      break;
-    case ARG_BUFFERSIZE:
-      ladspa->buffersize = g_value_get_int (value);
-      break;
-  }
-  
-  // is it a ladspa plugin arg?
-  if (cid<0) return;
-
-/*
-  if (id == ARG_LOOP_BASED) {
-    // we can only do this in NULL state
-    g_return_if_fail (GST_STATE(object) != GST_STATE_NULL);
-    ladspa->loopbased = g_value_get_boolean (value);
-    if (ladspa->loopbased) {
-      gst_element_set_loop_function (GST_ELEMENT (ladspa), gst_ladspa_loop);
-    } else {
-      gst_element_set_loop_function (GST_ELEMENT (ladspa), NULL);
-    }
-  }
-*/
 
   oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
 
-  // verify it exists and is a control (not a port)
-  g_return_if_fail(cid < oclass->numcontrols);
-  
-  control_info = &(oclass->control_info[cid]);
+  /* remember, properties have an offset of 1 */
+  prop_id--;
+
+  /* verify it exists */
+  g_return_if_fail (prop_id < oclass->numcontrols);
+
+  control_info = &(oclass->control_info[prop_id]);
   g_return_if_fail (control_info->name != NULL);
 
-  // check to see if it's writable
+  /* check to see if it's writable */
   g_return_if_fail (control_info->writable);
 
-  // g_print("set arg %s to %f\n",control_info->name,ladspa->controls[cid]);
+  /* now see what type it is */
+  if (control_info->toggled)
+    ladspa->controls[prop_id] = g_value_get_boolean (value) ? 1.f : 0.f;
+  else if (control_info->integer)
+    ladspa->controls[prop_id] = g_value_get_int (value);
+  else
+    ladspa->controls[prop_id] = g_value_get_float (value);
 
-  // now see what type it is
-  if (control_info->toggled) {
-    if (g_value_get_boolean (value))
-      ladspa->controls[cid] = 1.0;
-    else
-      ladspa->controls[cid] = 0.0;
-  } else if (control_info->integer) {
-    val = (gfloat)g_value_get_int (value);
-    ladspa->controls[cid] = val;
-  } else {
-    val = g_value_get_float (value);
-    ladspa->controls[cid] = val;
-  }    
+  DEBUG_OBJ (object, "set arg %s to %f", control_info->name, ladspa->controls[prop_id]);
 }
 
 static void
 gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
 {
   GstLADSPA *ladspa = (GstLADSPA*)object;
-  gint cid = prop_id - ARG_LAST;
   GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
   ladspa_control_info *control_info;
 
-  // these are only registered in get mode
-  switch (prop_id){
-    case ARG_SAMPLERATE:
-      g_value_set_int (value, ladspa->samplerate);
-      break;
-    case ARG_BUFFERSIZE:
-      g_value_set_int (value, ladspa->buffersize);
-      break;
-  }
-    
-  if (cid<0) return;
-  // verify it exists and is a control (not a port)
-  if (cid >= oclass->numcontrols) return;
-  control_info = &(oclass->control_info[cid]);
-  if (control_info->name == NULL) return;
-
-  //g_print("got arg %s as %f\n",control_info->name,ladspa->controls[cid]);
-
-  // now see what type it is
-  if (control_info->toggled) {
-    if (ladspa->controls[cid] == 1.0)
-      g_value_set_boolean (value, TRUE);
-    else
-      g_value_set_boolean (value, FALSE);
-  } else if (control_info->integer) {
-    g_value_set_int (value, (gint)ladspa->controls[cid]);
-  } else {
-    g_value_set_float (value, ladspa->controls[cid]);
-  }
+  /* remember, properties have an offset of 1 */
+  prop_id--;
+
+  /* verify it exists */
+  g_return_if_fail (prop_id < oclass->numcontrols);
+
+  control_info = &(oclass->control_info[prop_id]);
+  g_return_if_fail (control_info->name != NULL);
+
+  /* now see what type it is */
+  if (control_info->toggled)
+    g_value_set_boolean (value, ladspa->controls[prop_id] == 1.0);
+  else if (control_info->integer)
+    g_value_set_int (value, (gint)ladspa->controls[prop_id]);
+  else
+    g_value_set_float (value, ladspa->controls[prop_id]);
+
+  DEBUG_OBJ (object, "got arg %s as %f", control_info->name, ladspa->controls[prop_id]);
 }
 
 static gboolean
@@ -552,30 +625,29 @@ gst_ladspa_instantiate (GstLADSPA *ladspa)
   
   desc = ladspa->descriptor;
   
-  // check for old handle
+  /* check for old handle */
   was_activated = ladspa->activated;
   if (ladspa->handle != NULL){
     gst_ladspa_deactivate(ladspa);
     desc->cleanup(ladspa->handle);
   }
         
-  // instantiate the plugin  
+  /* instantiate the plugin */ 
+  DEBUG_OBJ (ladspa, "instantiating the plugin at %d Hz", ladspa->samplerate);
+  
   ladspa->handle = desc->instantiate(desc,ladspa->samplerate);
   g_return_val_if_fail (ladspa->handle != NULL, FALSE);
 
-  // walk through the ports and add all the arguments
-  for (i=0;i<oclass->numcontrols;i++) {
-    // connect the argument to the plugin
-    //g_print("added control port %d\n", oclass->control_portnums[i]);
+  /* connect the control ports */
+  for (i=0;i<oclass->numcontrols;i++)
     desc->connect_port(ladspa->handle,
                        oclass->control_portnums[i],
                        &(ladspa->controls[i]));
-  }
 
-  // reactivate if it was activated before the reinstantiation
-  if (was_activated){
+  /* reactivate if it was activated before the reinstantiation */
+  if (was_activated)
     gst_ladspa_activate(ladspa);
-  }
+
   return TRUE;
 }
 
@@ -584,10 +656,8 @@ gst_ladspa_change_state (GstElement *element)
 {
   LADSPA_Descriptor *desc;
   GstLADSPA *ladspa = (GstLADSPA*)element;
-//  GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT(ladspa)->klass);
   desc = ladspa->descriptor;
 
-  //g_print("changing state\n");
   switch (GST_STATE_TRANSITION (element)) {
     case GST_STATE_NULL_TO_READY:
       gst_ladspa_activate(ladspa);
@@ -606,184 +676,293 @@ gst_ladspa_change_state (GstElement *element)
 }
 
 static void
-gst_ladspa_activate(GstLADSPA *ladspa)
+gst_ladspa_activate (GstLADSPA *ladspa)
 {
   LADSPA_Descriptor *desc;
   desc = ladspa->descriptor;
   
-  if (ladspa->activated){
+  if (ladspa->activated)
     gst_ladspa_deactivate(ladspa);
-  }
   
-  //g_print("activating\n");
+  DEBUG_OBJ (ladspa, "activating");
 
-  // activate the plugin (function might be null)
-  if (desc->activate != NULL){
+  /* activate the plugin (function might be null) */
+  if (desc->activate != NULL)
     desc->activate(ladspa->handle);
-  }
-  
+
   ladspa->activated = TRUE;
 }
 
 static void
-gst_ladspa_deactivate(GstLADSPA *ladspa)
+gst_ladspa_deactivate (GstLADSPA *ladspa)
 {
   LADSPA_Descriptor *desc;
   desc = ladspa->descriptor;
 
-  //g_print("deactivating\n");
+  DEBUG_OBJ (ladspa, "deactivating");
 
-  // deactivate the plugin (function might be null)  
-  if (ladspa->activated && desc->deactivate != NULL){
+  /* deactivate the plugin (function might be null) */
+  if (ladspa->activated && (desc->deactivate != NULL))
     desc->deactivate(ladspa->handle);
-  }
+
   ladspa->activated = FALSE;
 }
 
 static void
 gst_ladspa_loop (GstElement *element)
 {
-  int i;
-  GstLADSPA *ladspa = (GstLADSPA *)element;
-  GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
-  LADSPA_Descriptor *desc;
+  guint        i, j, numsrcpads, numsinkpads;
+  guint        num_processed, num_to_process;
+  gint         largest_buffer;
+  LADSPA_Data  **data_in, **data_out;
+  GstBuffer    **buffers_in, **buffers_out;
+  GstLADSPA       *ladspa = (GstLADSPA *)element;
+  GstLADSPAClass  *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
+  LADSPA_Descriptor *desc = ladspa->descriptor;
+
+  numsinkpads = oclass->numsinkpads;
+  numsrcpads = oclass->numsrcpads;
+  
+  /* fixme: these mallocs need to die */
+  data_in = g_new0(LADSPA_Data*, numsinkpads);
+  data_out = g_new0(LADSPA_Data*, numsrcpads);
+  buffers_in = g_new0(GstBuffer*, numsinkpads);
+  buffers_out = g_new0(GstBuffer*, numsrcpads);
+  
+  largest_buffer = -1;
 
-  desc = ladspa->descriptor;
-  printf("looping something\n");
+  /* first get all the necessary data from the input ports */
+  for (i=0 ; i<numsinkpads ; i++){  
+  get_buffer:
+    buffers_in[i] = GST_BUFFER (gst_pad_pull (ladspa->sinkpads[i]));
+    
+    if (GST_IS_EVENT (buffers_in[i])) {
+      /* push it out on all pads */
+      gst_data_ref_by_count ((GstData*)buffers_in[i], numsrcpads);
+      for (j=0; j<numsrcpads; j++)
+        gst_pad_push (ladspa->srcpads[j], GST_DATA (buffers_in[i]));
+      if (GST_EVENT_TYPE (buffers_in[i]) == GST_EVENT_EOS) {
+        /* shut down */
+        gst_element_set_eos (element);
+        return;
+      } else {
+        goto get_buffer;
+      }
+    }
 
-  // first get all the necessary data from the input ports
-  for (i=0;i<oclass->numsinkpads;i++){  
-    ladspa->buffers[i] = gst_pad_pull(ladspa->sinkpads[i]);
-    printf("pulling buffer %d\n", i);
+    if (largest_buffer < 0)
+      largest_buffer = GST_BUFFER_SIZE (buffers_in[i])/sizeof(gfloat);
+    else
+      largest_buffer = MIN (GST_BUFFER_SIZE (buffers_in[i])/sizeof(gfloat), largest_buffer);
+    data_in[i] = (LADSPA_Data *) GST_BUFFER_DATA(buffers_in[i]);
+    GST_BUFFER_TIMESTAMP(buffers_in[i]) = ladspa->timestamp;
   }
-    
-  for (i=0;i<oclass->numsinkpads;i++) {
-//      desc->connect_port(ladspa->handle,i,&(ladspa->controls[i]));
+
+  if (!ladspa->bufpool) {
+    gst_element_error (element, "Caps were never set, bailing...");
+    return;
   }
 
-  for (i=0;i<oclass->numsrcpads && i<oclass->numsinkpads;i++){
-    printf("pushing buffer %d\n", i);
-    gst_pad_push (ladspa->srcpads[i], ladspa->buffers[i]);
-    ladspa->buffers[i] = NULL;
+  i=0;
+  if (!ladspa->inplace_broken) {
+    for (; i<numsrcpads && i<numsinkpads; i++) {
+      /* reuse input buffers */
+      buffers_out[i] = buffers_in[i];
+      data_out[i] = data_in[i];
+    }
+  }
+  for (; i<numsrcpads; i++) {
+    /* we have to make new buffers -- at least we're taking them from a pool */
+    buffers_out[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
+    GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
+    data_out[i] = (LADSPA_Data*)GST_BUFFER_DATA (buffers_out[i]);
   }
+  
+  GST_DPMAN_PREPROCESS(ladspa->dpman, largest_buffer, ladspa->timestamp);
+  num_processed = 0;
+
+  /* split up processing of the buffer into chunks so that dparams can
+   * be updated when required.
+   * In many cases the buffer will be processed in one chunk anyway.
+   */
+  while (GST_DPMAN_PROCESS (ladspa->dpman, num_processed)) {
+    num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
+
+    for (i=0 ; i<numsinkpads ; i++)
+      desc->connect_port (ladspa->handle, oclass->sinkpad_portnums[i], data_in[i]);
+    for (i=0 ; i<numsrcpads ; i++)
+      desc->connect_port (ladspa->handle, oclass->srcpad_portnums[i], data_out[i]);
+
+    desc->run(ladspa->handle, num_to_process);
+
+    for (i=0 ; i<numsinkpads ; i++)
+      data_in[i] += num_to_process;
+    for (i=0 ; i<numsrcpads ; i++)
+      data_out[i] += num_to_process;
+    
+    num_processed += num_to_process;
+  }
+    
+  for (i=0 ; i<numsinkpads ; i++) {
+    if (i >= numsrcpads || buffers_out[i] != buffers_in[i])
+      gst_buffer_unref(buffers_in[i]);
+    data_in[i] = NULL;
+    buffers_in[i] = NULL;
+  }      
+  for (i=0 ; i<numsrcpads ; i++) {
+    DEBUG_OBJ (ladspa, "pushing buffer (%p) on src pad %d", buffers_out[i], i);
+    gst_pad_push (ladspa->srcpads[i], GST_DATA (buffers_out[i]));
+    
+    data_out[i] = NULL;
+    buffers_out[i] = NULL;
+  }
+  
+  ladspa->timestamp += ladspa->buffer_frames * GST_SECOND / ladspa->samplerate;
+
+  /* FIXME: move these mallocs and frees to the state-change handler */
+
+  g_free (buffers_out);
+  g_free (buffers_in);
+  g_free (data_out);
+  g_free (data_in);
 }
 
 static void
-gst_ladspa_chain_inplace_mono (GstPad *pad,GstBuffer *buf)
+gst_ladspa_chain (GstPad *pad, GstData *_data)
 {
+  GstBuffer *buffer_in = GST_BUFFER (_data);
   LADSPA_Descriptor *desc;
-  LADSPA_Data *data;
+  LADSPA_Data *data_in, **data_out = NULL;
+  GstBuffer **buffers_out = NULL;
   unsigned long num_samples;
-  
+  guint num_to_process, num_processed, i, numsrcpads;
   GstLADSPA *ladspa;
   GstLADSPAClass *oclass;
 
-  g_return_if_fail(pad != NULL);
-  g_return_if_fail(GST_IS_PAD(pad));
-  g_return_if_fail(buf != NULL);
+  ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
+  oclass = (GstLADSPAClass *) (G_OBJECT_GET_CLASS (ladspa));
+  data_in = (LADSPA_Data *) GST_BUFFER_DATA(buffer_in);
+  num_samples = GST_BUFFER_SIZE(buffer_in) / sizeof(gfloat);
+  numsrcpads = oclass->numsrcpads;
+  desc = ladspa->descriptor;
 
-  ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
-  g_return_if_fail(ladspa != NULL);
+  /* we shouldn't get events here... */
+  g_return_if_fail (GST_IS_BUFFER (buffer_in));
+  
+  if (!ladspa->bufpool) {
+    gst_element_error ((GstElement*)ladspa, "Caps were never set, bailing...");
+    return;
+  }
 
-  // this might happen if caps nego hasn't happened
-  g_return_if_fail(ladspa->handle != NULL);
+  /* FIXME: this function shouldn't need to malloc() anything */
+  if (numsrcpads > 0) {
+    buffers_out = g_new(GstBuffer*, numsrcpads);
+    data_out = g_new(LADSPA_Data*, numsrcpads);
+  }
 
-  oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
-  data=(LADSPA_Data*)GST_BUFFER_DATA(buf);
-  num_samples = GST_BUFFER_SIZE(buf) / sizeof(gfloat);
-  
-  desc = ladspa->descriptor;
+  i=0;
+  if (!ladspa->inplace_broken && numsrcpads) {
+    /* reuse the first (chained) buffer */
+    buffers_out[i] = buffer_in;
+    DEBUG ("reuse: %d", GST_BUFFER_SIZE (buffer_in));
+    data_out[i] = data_in;
+    i++;
+  }
+  for (; i<numsrcpads; i++) {
+    /* we have to make new buffers -- at least we're taking them from a pool */
+    buffers_out[i] = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
+    /* the size of the buffer returned from the pool is the maximum size; this
+       chained buffer might be smaller */
+    GST_BUFFER_SIZE (buffers_out[i]) = GST_BUFFER_SIZE (buffer_in);
+    DEBUG ("new %d", GST_BUFFER_SIZE (buffer_in));
+    GST_BUFFER_TIMESTAMP (buffers_out[i]) = ladspa->timestamp;
+    data_out[i] = (LADSPA_Data*)GST_BUFFER_DATA (buffers_out[i]);
+  }
 
-  desc->connect_port(ladspa->handle,oclass->sinkpad_portnums[0],data);
-  desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);
+  GST_DPMAN_PREPROCESS(ladspa->dpman, num_samples, GST_BUFFER_TIMESTAMP(buffer_in));
+  num_processed = 0;
 
-  desc->run(ladspa->handle,num_samples);
-  
-  desc->connect_port(ladspa->handle,oclass->sinkpad_portnums[0],NULL);
-  desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],NULL);
+  /* split up processing of the buffer into chunks so that dparams can
+   * be updated when required.
+   * In many cases the buffer will be processed in one chunk anyway.
+   */
+  while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
+    num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
 
-  gst_pad_push (ladspa->srcpads[0], buf);  
-}
+    desc->connect_port(ladspa->handle,oclass->sinkpad_portnums[0],data_in);  
+    for (i=0 ; i<numsrcpads ; i++)
+      desc->connect_port(ladspa->handle,oclass->srcpad_portnums[i],data_out[i]);
 
-static GstBuffer *
-gst_ladspa_get_mono(GstPad *pad)
-{
-  LADSPA_Descriptor *desc;
-  LADSPA_Data *data;
-  
-  GstLADSPA *ladspa;
-  GstLADSPAClass *oclass;
-  GstBuffer *buf;
+    desc->run(ladspa->handle, num_to_process);
+    
+    data_in += num_to_process;
+    for (i=0 ; i<numsrcpads ; i++)
+      data_out[i] += num_to_process;
 
-  g_return_val_if_fail(pad != NULL, NULL);
-  g_return_val_if_fail(GST_IS_PAD(pad), NULL);
+    num_processed += num_to_process;
+  }
 
-  ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
-  g_return_val_if_fail(ladspa != NULL, NULL);
+  if (!numsrcpads || buffers_out[0] != buffer_in)
+    gst_buffer_unref(buffer_in);
 
-  // this might happen if caps nego hasn't happened
-  g_return_val_if_fail(ladspa->handle != NULL, NULL);
+  if (numsrcpads) {
+    for (i=0; i<numsrcpads; i++) {
+      DEBUG_OBJ (ladspa, "pushing buffer (%p, length %u bytes) on src pad %d",
+                 buffers_out[i], GST_BUFFER_SIZE (buffers_out[i]), i);
+      gst_pad_push (ladspa->srcpads[i], GST_DATA (buffers_out[i]));
+    }
 
-  oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
-  
-  if (ladspa->newcaps) {
-    gst_ladspa_force_caps(ladspa, ladspa->srcpads[0]);
+    g_free(buffers_out);
+    g_free(data_out);
   }
-  
-  buf = gst_buffer_new();
-  g_return_val_if_fail (buf, NULL);
-  data = g_new(LADSPA_Data, ladspa->buffersize);
-  GST_BUFFER_DATA(buf) = (gpointer) data;
-  GST_BUFFER_SIZE(buf) = sizeof(LADSPA_Data) * ladspa->buffersize;
-  GST_BUFFER_TIMESTAMP(buf) = ladspa->timestamp;
-
-  desc = ladspa->descriptor;
-  desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);
-  desc->run(ladspa->handle,(unsigned long)ladspa->buffersize);  
-  desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],NULL);
-  
-  return buf;
 }
 
-static GstBuffer *
+static GstData *
 gst_ladspa_get(GstPad *pad)
-{
-  LADSPA_Descriptor *desc;
-  LADSPA_Data *data;
-  
+{  
   GstLADSPA *ladspa;
   GstLADSPAClass *oclass;
   GstBuffer *buf;
-
-  g_return_val_if_fail(pad != NULL, NULL);
-  g_return_val_if_fail(GST_IS_PAD(pad), NULL);
+  LADSPA_Data *data;
+  LADSPA_Descriptor *desc;
+  guint num_to_process, num_processed;
 
   ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
-  g_return_val_if_fail(ladspa != NULL, NULL);
-
-  // this might happen if caps nego hasn't happened
-  g_return_val_if_fail(ladspa->handle != NULL, NULL);
-
   oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
-  
-  if (ladspa->newcaps) {
-    gst_ladspa_force_caps(ladspa, ladspa->srcpads[0]);
+  desc = ladspa->descriptor;
+
+  if (!ladspa->bufpool) {
+    /* capsnego hasn't happened... */
+    gst_ladspa_force_src_caps(ladspa, ladspa->srcpads[0]);
   }
-  
-  buf = gst_buffer_new();
-  g_return_val_if_fail (buf, NULL);
-  data = g_new(LADSPA_Data, ladspa->buffersize);
-  GST_BUFFER_DATA(buf) = (gpointer) data;
-  GST_BUFFER_SIZE(buf) = sizeof(LADSPA_Data) * ladspa->buffersize;
+
+  buf = gst_buffer_new_from_pool (ladspa->bufpool, 0, 0);
   GST_BUFFER_TIMESTAMP(buf) = ladspa->timestamp;
-  ladspa->timestamp+= ladspa->buffersize * ladspa->samplerate * 10^9;
+  data = (LADSPA_Data *) GST_BUFFER_DATA(buf);  
 
-  desc = ladspa->descriptor;
-  desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);
-  desc->run(ladspa->handle,(unsigned long)ladspa->buffersize);  
-  desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],NULL);
+  GST_DPMAN_PREPROCESS(ladspa->dpman, ladspa->buffer_frames, ladspa->timestamp);
+  num_processed = 0;
+
+  /* split up processing of the buffer into chunks so that dparams can
+   * be updated when required.
+   * In many cases the buffer will be processed in one chunk anyway.
+   */
+  while(GST_DPMAN_PROCESS(ladspa->dpman, num_processed)) {
+    num_to_process = GST_DPMAN_FRAMES_TO_PROCESS(ladspa->dpman);
+
+    /* update timestamp */  
+    ladspa->timestamp += num_to_process * GST_SECOND / ladspa->samplerate;
+
+    desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);  
+
+    desc->run(ladspa->handle, num_to_process);
+    
+    data += num_to_process;
+    num_processed = num_to_process;
+  }
   
-  return buf;
+  return GST_DATA (buf);
 }
 
 static void
@@ -792,11 +971,10 @@ ladspa_describe_plugin(const char *pcFullFilename,
                        LADSPA_Descriptor_Function pfDescriptorFunction)
 {
   const LADSPA_Descriptor *desc;
-  int i,j;
-  
-  GstElementDetails *details;
+  gint i;
   GTypeInfo typeinfo = {
-      sizeof(GstLADSPAClass),      NULL,
+      sizeof(GstLADSPAClass),
+      (GBaseInitFunc)gst_ladspa_base_init,
       NULL,
       (GClassInitFunc)gst_ladspa_class_init,
       NULL,
@@ -806,64 +984,47 @@ ladspa_describe_plugin(const char *pcFullFilename,
       (GInstanceInitFunc)gst_ladspa_init,
   };
   GType type;
-  GstElementFactory *factory;
 
-  // walk through all the plugins in this pluginlibrary
+  /* walk through all the plugins in this pluginlibrary */
   i = 0;
   while ((desc = pfDescriptorFunction(i++))) {
     gchar *type_name;
 
-    // construct the type
-    type_name = g_strdup_printf("ladspa_%s",desc->Label);
-    g_strcanon (type_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_+", '-');
-    // if it's already registered, drop it
+    /* construct the type */
+    type_name = g_strdup_printf("ladspa-%s",desc->Label);
+    g_strcanon (type_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-+", '-');
+    /* if it's already registered, drop it */
     if (g_type_from_name(type_name)) {
       g_free(type_name);
       continue;
     }
-    // create the type now
-    type = g_type_register_static(GST_TYPE_ELEMENT, type_name , &typeinfo, 0);
-
-    // construct the element details struct
-    details = g_new0(GstElementDetails,1);
-    details->longname = g_strdup(desc->Name);
-    details->klass = "Filter/LADSPA";
-    details->description = details->longname;
-    details->version = g_strdup_printf("%ld",desc->UniqueID);
-    details->author = g_strdup(desc->Maker);
-    details->copyright = g_strdup(desc->Copyright);
-
-    // register the plugin with gstreamer
-    factory = gst_elementfactory_new(type_name,type,details);
-    g_return_if_fail(factory != NULL);
-    gst_plugin_add_feature (ladspa_plugin, GST_PLUGIN_FEATURE (factory));
-
-    // add this plugin to the hash
+
+    /* base-init temp alloc */
     g_hash_table_insert(ladspa_descriptors,
-                        GINT_TO_POINTER(type),
+                        GINT_TO_POINTER(0),
                         (gpointer)desc);
-    
 
-    // only add sink padtemplate if there are sinkpads                        
-    for (j=0;j<desc->PortCount;j++) {
-      if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j]) &&
-          LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j])) {
-        sinktempl = ladspa_sink_factory();
-        gst_elementfactory_add_padtemplate (factory, sinktempl);
-        break;
-      }
-    }
-  
-    srctempl = ladspa_src_factory();
-    gst_elementfactory_add_padtemplate (factory, srctempl);
+    /* create the type now */
+    type = g_type_register_static(GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
+    if (!gst_element_register (ladspa_plugin, type_name, GST_RANK_NONE, type))
+      continue;
 
+    /* add this plugin to the hash */
+    g_hash_table_insert(ladspa_descriptors,
+                        GINT_TO_POINTER(type),
+                        (gpointer)desc);
   }
+
+  g_hash_table_remove (ladspa_descriptors, GINT_TO_POINTER (0));
 }
 
 static gboolean
-plugin_init (GModule *module, GstPlugin *plugin)
+plugin_init (GstPlugin *plugin)
 {
-  
+  GST_DEBUG_CATEGORY_INIT (ladspa_debug, "ladspa",
+                           GST_DEBUG_FG_GREEN | GST_DEBUG_BG_BLACK | GST_DEBUG_BOLD,
+                           "LADSPA");
+
   ladspa_descriptors = g_hash_table_new(NULL,NULL);
   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
 
@@ -871,13 +1032,20 @@ plugin_init (GModule *module, GstPlugin *plugin)
 
   LADSPAPluginSearch(ladspa_describe_plugin);
 
+  /* initialize dparam support library */
+  gst_control_init(NULL,NULL);
+  
   return TRUE;
 }
 
-GstPluginDesc plugin_desc = {
+GST_PLUGIN_DEFINE (
   GST_VERSION_MAJOR,
   GST_VERSION_MINOR,
   "ladspa",
-  plugin_init
-};
-
+  "All LADSPA plugins",
+  plugin_init,
+  VERSION,
+  GST_LICENSE,
+  GST_PACKAGE,
+  GST_ORIGIN
+)