docs/libs/gstreamer-libs-sections.txt: Add new function:
[platform/upstream/gstreamer.git] / libs / gst / base / gstbasetransform.c
index 1331c65..2bb7681 100644 (file)
@@ -1,9 +1,9 @@
 /* GStreamer
  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
- *                    2000 Wim Taymans <wtay@chello.be>
  *                    2005 Wim Taymans <wim@fluendo.com>
  *                    2005 Andy Wingo <wingo@fluendo.com>
  *                    2005 Thomas Vander Stichele <thomas at apestaart dot org>
+ *                    2008 Wim Taymans <wim.taymans@gmail.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  *     element negotiates the same caps on both pads.
  *   </para></listitem>
  *   <listitem><para>
- *     passthrough_on_same_caps on an element that doesn't implement a transform_caps
- *     function is useful for elements that only inspect data (such as level)
+ *     passthrough_on_same_caps on an element that doesn't implement a
+ *     transform_caps function is useful for elements that only inspect data
+ *     (such as level)
  *   </para></listitem>
  *   </itemizedlist>
  *   <itemizedlist>
  *   <title>Example elements</title>
  *     <listitem>Level</listitem>
- *     <listitem>Videoscale, audioconvert, ffmpegcolorspace, audioresample in certain modes.</listitem>
+ *     <listitem>Videoscale, audioconvert, ffmpegcolorspace, audioresample in
+ *     certain modes.</listitem>
  *   </itemizedlist>
  * </listitem>
  * <listitem>
- *   <itemizedlist><title>Modifications in-place - input buffer and output buffer are the same thing.</title>
+ *   <itemizedlist>
+ *     <title>Modifications in-place - input buffer and output buffer are the
+ *     same thing.</title>
  *   <listitem><para>
  *     The element must implement a transform_ip function.
  *   </para></listitem>
  *     Output buffer size must <= input buffer size
  *   </para></listitem>
  *   <listitem><para>
- *     If the always_in_place flag is set, non-writable buffers will be copied and
- *     passed to the transform_ip function, otherwise a new buffer will be created
- *     and the transform function called.
+ *     If the always_in_place flag is set, non-writable buffers will be copied
+ *     and passed to the transform_ip function, otherwise a new buffer will be
+ *     created and the transform function called.
  *   </para></listitem>
  *   <listitem><para>
- *     Incoming writable buffers will be passed to the transform_ip function immediately.
- *   </para></listitem>
+ *     Incoming writable buffers will be passed to the transform_ip function
+ *     immediately.  </para></listitem>
  *   <listitem><para>
- *     only implementing transform_ip and not transform implies always_in_place =
- *     TRUE
+ *     only implementing transform_ip and not transform implies always_in_place
+ *     TRUE
  *   </para></listitem>
  *   </itemizedlist>
  *   <itemizedlist>
  *   <title>Example elements</title>
  *     <listitem>Volume</listitem>
- *     <listitem>Audioconvert in certain modes (signed/unsigned conversion)</listitem>
- *     <listitem>ffmpegcolorspace in certain modes (endianness swapping)</listitem>
+ *     <listitem>Audioconvert in certain modes (signed/unsigned
+ *     conversion)</listitem>
+ *     <listitem>ffmpegcolorspace in certain modes (endianness
+ *     swapping)</listitem>
  *   </itemizedlist>
  *  </listitem>
  * <listitem>
- *   <itemizedlist><title>Modifications only to the caps/metadata of a buffer</title>
+ *   <itemizedlist>
+ *   <title>Modifications only to the caps/metadata of a buffer</title>
  *   <listitem><para>
- *     The element does not require writable data, but non-writable buffers should
- *     be subbuffered so that the meta-information can be replaced.
+ *     The element does not require writable data, but non-writable buffers
+ *     should be subbuffered so that the meta-information can be replaced.
  *   </para></listitem>
  *   <listitem><para>
  *     Elements wishing to operate in this mode should replace the
- *     prepare_output_buffer method to create subbuffers of the input buffer and
- *     set always_in_place to TRUE
+ *     prepare_output_buffer method to create subbuffers of the input buffer
+ *     and set always_in_place to TRUE
  *   </para></listitem>
  *   </itemizedlist>
  *   <itemizedlist>
  *   <title>Example elements</title>
- *     <listitem>Capsfilter when setting caps on outgoing buffers that have none.</listitem>
- *     <listitem>identity when it is going to re-timestamp buffers by datarate.</listitem>
+ *     <listitem>Capsfilter when setting caps on outgoing buffers that have
+ *     none.</listitem>
+ *     <listitem>identity when it is going to re-timestamp buffers by
+ *     datarate.</listitem>
  *   </itemizedlist>
  * </listitem>
  * <listitem>
  *   </itemizedlist>
  *   <itemizedlist>
  *   <title>Example elements</title>
- *     <listitem>Videoscale, ffmpegcolorspace, audioconvert when doing scaling/conversions</listitem>
+ *     <listitem>Videoscale, ffmpegcolorspace, audioconvert when doing
+ *     scaling/conversions</listitem>
  *   </itemizedlist>
  * </listitem>
  * <listitem>
  *   <itemizedlist><title>Special output buffer allocations</title>
  *   <listitem><para>
- *     Elements which need to do special allocation of their output buffers other
- *     than what gst_buffer_pad_alloc allows should implement a
+ *     Elements which need to do special allocation of their output buffers
+ *     other than what gst_buffer_pad_alloc allows should implement a
  *     prepare_output_buffer method, which calls the parent implementation and
  *     passes the newly allocated buffer.
  *   </para></listitem>
  *       interested in modifying the buffers.
  *     </para></listitem>
  *     <listitem><para>
- *       Elements which are always in passthrough mode whenever the same caps has
- *       been negotiated on both pads can set the class variable
+ *       Elements which are always in passthrough mode whenever the same caps
+ *       has been negotiated on both pads can set the class variable
  *       passthrough_on_same_caps to have this behaviour automatically.
  *     </para></listitem>
  *   </itemizedlist>
 #include <stdlib.h>
 #include <string.h>
 
+#include "../../../gst/gst_private.h"
 #include "../../../gst/gst-i18n-lib.h"
 #include "gstbasetransform.h"
 #include <gst/gstmarshal.h>
@@ -202,19 +213,46 @@ enum
   LAST_SIGNAL
 };
 
+#define DEFAULT_PROP_QOS       FALSE
+
 enum
 {
   PROP_0,
+  PROP_QOS
+};
+
+#define GST_BASE_TRANSFORM_GET_PRIVATE(obj)  \
+    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_TRANSFORM, GstBaseTransformPrivate))
+
+struct _GstBaseTransformPrivate
+{
+  /* QoS *//* with LOCK */
+  gboolean qos_enabled;
+  gdouble proportion;
+  GstClockTime earliest_time;
+  /* previous buffer had a discont */
+  gboolean discont;
+
+  GstActivateMode pad_mode;
+
+  gboolean gap_aware;
+
+  /* caps used for allocating buffers */
+  gboolean proxy_alloc;
+  GstCaps *sink_alloc;
+  GstCaps *src_alloc;
+  /* upstream caps and size suggestions */
+  GstCaps *sink_suggest;
+  guint size_suggest;
 };
 
 static GstElementClass *parent_class = NULL;
 
-static void gst_base_transform_base_init (gpointer g_class);
 static void gst_base_transform_class_init (GstBaseTransformClass * klass);
 static void gst_base_transform_init (GstBaseTransform * trans,
     GstBaseTransformClass * klass);
-static GstFlowReturn gst_base_transform_prepare_output_buf (GstBaseTransform *
-    trans, GstBuffer * input, gint size, GstCaps * caps, GstBuffer ** buf);
+static GstFlowReturn gst_base_transform_prepare_output_buffer (GstBaseTransform
+    * trans, GstBuffer * input, GstBuffer ** buf);
 
 GType
 gst_base_transform_get_type (void)
@@ -224,7 +262,7 @@ gst_base_transform_get_type (void)
   if (!base_transform_type) {
     static const GTypeInfo base_transform_info = {
       sizeof (GstBaseTransformClass),
-      (GBaseInitFunc) gst_base_transform_base_init,
+      NULL,
       NULL,
       (GClassInitFunc) gst_base_transform_class_init,
       NULL,
@@ -249,15 +287,18 @@ static gboolean gst_base_transform_src_activate_pull (GstPad * pad,
     gboolean active);
 static gboolean gst_base_transform_sink_activate_push (GstPad * pad,
     gboolean active);
+static gboolean gst_base_transform_activate (GstBaseTransform * trans,
+    gboolean active);
 static gboolean gst_base_transform_get_unit_size (GstBaseTransform * trans,
     GstCaps * caps, guint * size);
 
-static GstStateChangeReturn gst_base_transform_change_state (GstElement *
-    element, GstStateChange transition);
-
-static gboolean gst_base_transform_event (GstPad * pad, GstEvent * event);
-static gboolean gst_base_transform_eventfunc (GstBaseTransform * trans,
+static gboolean gst_base_transform_src_event (GstPad * pad, GstEvent * event);
+static gboolean gst_base_transform_src_eventfunc (GstBaseTransform * trans,
+    GstEvent * event);
+static gboolean gst_base_transform_sink_event (GstPad * pad, GstEvent * event);
+static gboolean gst_base_transform_sink_eventfunc (GstBaseTransform * trans,
     GstEvent * event);
+static gboolean gst_base_transform_check_get_range (GstPad * pad);
 static GstFlowReturn gst_base_transform_getrange (GstPad * pad, guint64 offset,
     guint length, GstBuffer ** buffer);
 static GstFlowReturn gst_base_transform_chain (GstPad * pad,
@@ -270,19 +311,13 @@ static GstFlowReturn gst_base_transform_buffer_alloc (GstPad * pad,
 /* static guint gst_base_transform_signals[LAST_SIGNAL] = { 0 }; */
 
 static void
-gst_base_transform_base_init (gpointer g_class)
-{
-  GST_DEBUG_CATEGORY_INIT (gst_base_transform_debug, "basetransform", 0,
-      "basetransform element");
-}
-
-static void
 gst_base_transform_finalize (GObject * object)
 {
   GstBaseTransform *trans;
 
   trans = GST_BASE_TRANSFORM (object);
 
+  gst_caps_replace (&trans->priv->sink_suggest, NULL);
   g_mutex_free (trans->transform_lock);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
@@ -292,10 +327,15 @@ static void
 gst_base_transform_class_init (GstBaseTransformClass * klass)
 {
   GObjectClass *gobject_class;
-  GstElementClass *gstelement_class;
 
   gobject_class = G_OBJECT_CLASS (klass);
-  gstelement_class = GST_ELEMENT_CLASS (klass);
+
+  GST_DEBUG_CATEGORY_INIT (gst_base_transform_debug, "basetransform", 0,
+      "basetransform element");
+
+  GST_DEBUG ("gst_base_transform_class_init");
+
+  g_type_class_add_private (klass, sizeof (GstBaseTransformPrivate));
 
   parent_class = g_type_class_peek_parent (klass);
 
@@ -304,13 +344,15 @@ gst_base_transform_class_init (GstBaseTransformClass * klass)
   gobject_class->get_property =
       GST_DEBUG_FUNCPTR (gst_base_transform_get_property);
 
-  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_transform_finalize);
+  g_object_class_install_property (gobject_class, PROP_QOS,
+      g_param_spec_boolean ("qos", "QoS", "Handle Quality-of-Service events",
+          DEFAULT_PROP_QOS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
-  gstelement_class->change_state =
-      GST_DEBUG_FUNCPTR (gst_base_transform_change_state);
+  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_transform_finalize);
 
   klass->passthrough_on_same_caps = FALSE;
-  klass->event = GST_DEBUG_FUNCPTR (gst_base_transform_eventfunc);
+  klass->event = GST_DEBUG_FUNCPTR (gst_base_transform_sink_eventfunc);
+  klass->src_event = GST_DEBUG_FUNCPTR (gst_base_transform_src_eventfunc);
 }
 
 static void
@@ -321,6 +363,8 @@ gst_base_transform_init (GstBaseTransform * trans,
 
   GST_DEBUG ("gst_base_transform_init");
 
+  trans->priv = GST_BASE_TRANSFORM_GET_PRIVATE (trans);
+
   pad_template =
       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
   g_return_if_fail (pad_template != NULL);
@@ -330,7 +374,7 @@ gst_base_transform_init (GstBaseTransform * trans,
   gst_pad_set_setcaps_function (trans->sinkpad,
       GST_DEBUG_FUNCPTR (gst_base_transform_setcaps));
   gst_pad_set_event_function (trans->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_base_transform_event));
+      GST_DEBUG_FUNCPTR (gst_base_transform_sink_event));
   gst_pad_set_chain_function (trans->sinkpad,
       GST_DEBUG_FUNCPTR (gst_base_transform_chain));
   gst_pad_set_activatepush_function (trans->sinkpad,
@@ -345,8 +389,10 @@ gst_base_transform_init (GstBaseTransform * trans,
   trans->srcpad = gst_pad_new_from_template (pad_template, "src");
   gst_pad_set_getcaps_function (trans->srcpad,
       GST_DEBUG_FUNCPTR (gst_base_transform_getcaps));
-  gst_pad_set_setcaps_function (trans->srcpad,
-      GST_DEBUG_FUNCPTR (gst_base_transform_setcaps));
+  gst_pad_set_event_function (trans->srcpad,
+      GST_DEBUG_FUNCPTR (gst_base_transform_src_event));
+  gst_pad_set_checkgetrange_function (trans->srcpad,
+      GST_DEBUG_FUNCPTR (gst_base_transform_check_get_range));
   gst_pad_set_getrange_function (trans->srcpad,
       GST_DEBUG_FUNCPTR (gst_base_transform_getrange));
   gst_pad_set_activatepull_function (trans->srcpad,
@@ -354,10 +400,12 @@ gst_base_transform_init (GstBaseTransform * trans,
   gst_element_add_pad (GST_ELEMENT (trans), trans->srcpad);
 
   trans->transform_lock = g_mutex_new ();
-  trans->delay_configure = FALSE;
   trans->pending_configure = FALSE;
+  trans->priv->qos_enabled = DEFAULT_PROP_QOS;
   trans->cache_caps1 = NULL;
   trans->cache_caps2 = NULL;
+  trans->priv->pad_mode = GST_ACTIVATE_NONE;
+  trans->priv->gap_aware = FALSE;
 
   trans->passthrough = FALSE;
   if (bclass->transform == NULL) {
@@ -365,11 +413,18 @@ gst_base_transform_init (GstBaseTransform * trans,
     GST_DEBUG_OBJECT (trans, "setting in_place TRUE");
     trans->always_in_place = TRUE;
 
-    if (bclass->transform_ip == NULL)
+    if (bclass->transform_ip == NULL) {
+      GST_DEBUG_OBJECT (trans, "setting passthrough TRUE");
       trans->passthrough = TRUE;
+    }
   }
 }
 
+/* given @caps on the src or sink pad (given by @direction)
+ * calculate the possible caps on the other pad.
+ *
+ * Returns new caps, unref after usage.
+ */
 static GstCaps *
 gst_base_transform_transform_caps (GstBaseTransform * trans,
     GstPadDirection direction, GstCaps * caps)
@@ -377,6 +432,9 @@ gst_base_transform_transform_caps (GstBaseTransform * trans,
   GstCaps *ret;
   GstBaseTransformClass *klass;
 
+  if (caps == NULL)
+    return NULL;
+
   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
 
   /* if there is a custom transform function, use this */
@@ -384,14 +442,17 @@ gst_base_transform_transform_caps (GstBaseTransform * trans,
     GstCaps *temp;
     gint i;
 
+    /* start with empty caps */
     ret = gst_caps_new_empty ();
+    GST_DEBUG_OBJECT (trans, "transform caps (direction = %d)", direction);
 
     if (gst_caps_is_any (caps)) {
       /* for any caps we still have to call the transform function */
-      GST_DEBUG_OBJECT (trans, "from ANY:");
+      GST_DEBUG_OBJECT (trans, "from: ANY");
       temp = klass->transform_caps (trans, direction, caps);
       GST_DEBUG_OBJECT (trans, "  to: %" GST_PTR_FORMAT, temp);
 
+      temp = gst_caps_make_writable (temp);
       gst_caps_append (ret, temp);
     } else {
       /* we send caps with just one structure to the transform
@@ -400,24 +461,43 @@ gst_base_transform_transform_caps (GstBaseTransform * trans,
         GstCaps *nth;
 
         nth = gst_caps_copy_nth (caps, i);
-        GST_DEBUG_OBJECT (trans, "from[%d]: %" GST_PTR_FORMAT, i, nth);
+        GST_LOG_OBJECT (trans, "from[%d]: %" GST_PTR_FORMAT, i, nth);
         temp = klass->transform_caps (trans, direction, nth);
         gst_caps_unref (nth);
-        GST_DEBUG_OBJECT (trans, "  to[%d]: %" GST_PTR_FORMAT, i, temp);
+        GST_LOG_OBJECT (trans, "  to[%d]: %" GST_PTR_FORMAT, i, temp);
+
+        temp = gst_caps_make_writable (temp);
+        /* here we need to only append those structures, that are not yet
+         * in there, we use the merge function for this */
+        gst_caps_merge (ret, temp);
 
-        gst_caps_append (ret, temp);
+        GST_LOG_OBJECT (trans, "  merged[%d]: %" GST_PTR_FORMAT, i, ret);
       }
+      GST_LOG_OBJECT (trans, "merged: (%d)", gst_caps_get_size (ret));
+      /* we can't do much simplification here because we don't really want to
+       * change the caps order */
     }
   } else {
-    /* else use the identity transform */
+    GST_DEBUG_OBJECT (trans, "identity from: %" GST_PTR_FORMAT, caps);
+    /* no transform function, use the identity transform */
     ret = gst_caps_ref (caps);
   }
 
-  GST_DEBUG_OBJECT (trans, "to:   %" GST_PTR_FORMAT, ret);
+  GST_DEBUG_OBJECT (trans, "to: (%d) %" GST_PTR_FORMAT, gst_caps_get_size (ret),
+      ret);
 
   return ret;
 }
 
+/* transform a buffer of @size with @caps on the pad with @direction to
+ * the size of a buffer with @othercaps and store the result in @othersize
+ *
+ * We have two ways of doing this:
+ *  1) use a custom transform size function, this is for complicated custom
+ *     cases with no fixed unit_size.
+ *  2) use the unit_size functions where there is a relationship between the
+ *     caps and the size of a buffer.
+ */
 static gboolean
 gst_base_transform_transform_size (GstBaseTransform * trans,
     GstPadDirection direction, GstCaps * caps,
@@ -425,7 +505,7 @@ gst_base_transform_transform_size (GstBaseTransform * trans,
 {
   guint inunitsize, outunitsize, units;
   GstBaseTransformClass *klass;
-  gint ret = -1;
+  gboolean ret;
 
   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
 
@@ -433,36 +513,72 @@ gst_base_transform_transform_size (GstBaseTransform * trans,
       GST_PTR_FORMAT " to size for caps %" GST_PTR_FORMAT " in direction %s",
       size, caps, othercaps, direction == GST_PAD_SRC ? "SRC" : "SINK");
 
-  /* if there is a custom transform function, use this */
   if (klass->transform_size) {
+    /* if there is a custom transform function, use this */
     ret = klass->transform_size (trans, direction, caps, size, othercaps,
         othersize);
   } else {
-    gboolean got_in_unit_size, got_out_unit_size;
+    /* there is no transform_size function, we have to use the unit_size
+     * functions. This method assumes there is a fixed unit_size associated with
+     * each caps. We provide the same amount of units on both sides. */
+    if (!gst_base_transform_get_unit_size (trans, caps, &inunitsize))
+      goto no_in_size;
 
-    got_in_unit_size = gst_base_transform_get_unit_size (trans, caps,
-        &inunitsize);
-    g_return_val_if_fail (got_in_unit_size == TRUE, FALSE);
     GST_DEBUG_OBJECT (trans, "input size %d, input unit size %d", size,
         inunitsize);
-    g_return_val_if_fail (inunitsize != 0, FALSE);
-    if (size % inunitsize != 0) {
-      g_warning ("Size %u is not a multiple of unit size %u", size, inunitsize);
-      return FALSE;
-    }
 
+    /* input size must be a multiple of the unit_size of the input caps */
+    if (inunitsize == 0 || (size % inunitsize != 0))
+      goto no_multiple;
+
+    /* get the amount of units */
     units = size / inunitsize;
-    got_out_unit_size = gst_base_transform_get_unit_size (trans, othercaps,
-        &outunitsize);
-    g_return_val_if_fail (got_out_unit_size == TRUE, FALSE);
 
+    /* now get the unit size of the output */
+    if (!gst_base_transform_get_unit_size (trans, othercaps, &outunitsize))
+      goto no_out_size;
+
+    /* the output size is the unit_size times the amount of units on the
+     * input */
     *othersize = units * outunitsize;
     GST_DEBUG_OBJECT (trans, "transformed size to %d", *othersize);
-  }
 
+    ret = TRUE;
+  }
   return ret;
+
+  /* ERRORS */
+no_in_size:
+  {
+    GST_DEBUG_OBJECT (trans, "could not get in_size");
+    g_warning ("%s: could not get in_size", GST_ELEMENT_NAME (trans));
+    return FALSE;
+  }
+no_multiple:
+  {
+    GST_DEBUG_OBJECT (trans, "Size %u is not a multiple of unit size %u", size,
+        inunitsize);
+    g_warning ("%s: size %u is not a multiple of unit size %u",
+        GST_ELEMENT_NAME (trans), size, inunitsize);
+    return FALSE;
+  }
+no_out_size:
+  {
+    GST_DEBUG_OBJECT (trans, "could not get out_size");
+    g_warning ("%s: could not get out_size", GST_ELEMENT_NAME (trans));
+    return FALSE;
+  }
 }
 
+/* get the caps that can be handled by @pad. We perforn:
+ *
+ *  - take the caps of peer of otherpad,
+ *  - filter against the padtemplate of otherpad, 
+ *  - calculate all transforms of remaining caps
+ *  - filter against template of @pad
+ *
+ * If there is no peer, we simply return the caps of the padtemplate of pad.
+ */
 static GstCaps *
 gst_base_transform_getcaps (GstPad * pad)
 {
@@ -517,6 +633,8 @@ done:
   return caps;
 }
 
+/* function triggered when the in and out caps are negotiated and need
+ * to be configured in the subclass. */
 static gboolean
 gst_base_transform_configure_caps (GstBaseTransform * trans, GstCaps * in,
     GstCaps * out)
@@ -526,10 +644,17 @@ gst_base_transform_configure_caps (GstBaseTransform * trans, GstCaps * in,
 
   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
 
+  GST_DEBUG_OBJECT (trans, "in caps:  %" GST_PTR_FORMAT, in);
+  GST_DEBUG_OBJECT (trans, "out caps: %" GST_PTR_FORMAT, out);
+
   /* clear the cache */
   gst_caps_replace (&trans->cache_caps1, NULL);
   gst_caps_replace (&trans->cache_caps2, NULL);
 
+  /* figure out same caps state */
+  trans->have_same_caps = gst_caps_is_equal (in, out);
+  GST_DEBUG_OBJECT (trans, "have_same_caps: %d", trans->have_same_caps);
+
   /* If we've a transform_ip method and same input/output caps, set in_place
    * by default. If for some reason the sub-class prefers using a transform
    * function, it can clear the in place flag in the set_caps */
@@ -552,32 +677,90 @@ gst_base_transform_configure_caps (GstBaseTransform * trans, GstCaps * in,
   return ret;
 }
 
+/* check if caps @in on @pad can be transformed to @out on the other pad.
+ * We don't have a vmethod to test this yet so we have to do a somewhat less
+ * efficient check for this.
+ */
 static gboolean
-gst_base_transform_setcaps (GstPad * pad, GstCaps * caps)
+gst_base_transform_can_transform (GstBaseTransform * trans, GstPad * pad,
+    GstCaps * in, GstCaps * out)
+{
+  GstCaps *othercaps;
+
+  /* convert the in caps to all possible out caps */
+  othercaps =
+      gst_base_transform_transform_caps (trans, GST_PAD_DIRECTION (pad), in);
+
+  /* check if transform is empty */
+  if (!othercaps || gst_caps_is_empty (othercaps))
+    goto no_transform;
+
+  /* check if the out caps is a subset of the othercaps */
+  if (!gst_caps_is_subset (out, othercaps))
+    goto no_subset;
+
+  if (othercaps)
+    gst_caps_unref (othercaps);
+
+  GST_DEBUG_OBJECT (trans, "from %" GST_PTR_FORMAT, in);
+  GST_DEBUG_OBJECT (trans, "to   %" GST_PTR_FORMAT, out);
+
+  return TRUE;
+
+  /* ERRORS */
+no_transform:
+  {
+    GST_DEBUG_OBJECT (trans,
+        "transform returned useless %" GST_PTR_FORMAT, othercaps);
+    if (othercaps)
+      gst_caps_unref (othercaps);
+    return FALSE;
+  }
+no_subset:
+  {
+    GST_DEBUG_OBJECT (trans, "no subset");
+    if (othercaps)
+      gst_caps_unref (othercaps);
+    return FALSE;
+  }
+}
+
+/* given a fixed @caps on @pad, create the best possible caps for the
+ * other pad.
+ * @caps must be fixed when calling this function.
+ *
+ * This function calls the transform caps vmethod of the basetransform to figure
+ * out the possible target formats. It then tries to select the best format from
+ * this list by:
+ *
+ * - attempt passthrough if the target caps is a superset of the input caps
+ * - fixating by using peer caps
+ * - fixating with transform fixate function
+ * - fixating with pad fixate functions.
+ *
+ * this function returns a caps that can be transformed into and is accepted by
+ * the peer element.
+ */
+static GstCaps *
+gst_base_transform_find_transform (GstBaseTransform * trans, GstPad * pad,
+    GstCaps * caps)
 {
-  GstBaseTransform *trans;
   GstBaseTransformClass *klass;
   GstPad *otherpad, *otherpeer;
-  GstCaps *othercaps = NULL;
-  gboolean ret = TRUE;
+  GstCaps *othercaps;
   gboolean peer_checked = FALSE;
 
-  trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
+  /* caps must be fixed here, this is a programming error if it's not */
+  g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
+
   klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
 
   otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
   otherpeer = gst_pad_get_peer (otherpad);
 
-  /* if we get called recursively, we bail out now to avoid an
-   * infinite loop. */
-  if (GST_PAD_IS_IN_SETCAPS (otherpad))
-    goto done;
-
-  /* caps must be fixed here */
-  if (!gst_caps_is_fixed (caps))
-    goto unfixed_caps;
-
-  /* see how we can transform the input caps. */
+  /* see how we can transform the input caps. We need to do this even for
+   * passthrough because it might be possible that this element cannot support
+   * passthrough at all. */
   othercaps = gst_base_transform_transform_caps (trans,
       GST_PAD_DIRECTION (pad), caps);
 
@@ -588,6 +771,9 @@ gst_base_transform_setcaps (GstPad * pad, GstCaps * caps)
     const GstCaps *templ_caps;
 
     templ_caps = gst_pad_get_pad_template_caps (otherpad);
+    GST_DEBUG_OBJECT (trans,
+        "intersecting against padtemplate %" GST_PTR_FORMAT, templ_caps);
+
     intersect = gst_caps_intersect (othercaps, templ_caps);
 
     gst_caps_unref (othercaps);
@@ -600,6 +786,8 @@ gst_base_transform_setcaps (GstPad * pad, GstCaps * caps)
 
   /* if the othercaps are not fixed, we need to fixate them, first attempt
    * is by attempting passthrough if the othercaps are a superset of caps. */
+  /* FIXME. maybe the caps is not fixed because it has multiple structures of
+   * fixed caps */
   if (!gst_caps_is_fixed (othercaps)) {
     GstCaps *temp;
 
@@ -611,22 +799,28 @@ gst_base_transform_setcaps (GstPad * pad, GstCaps * caps)
     temp = gst_caps_intersect (othercaps, caps);
     GST_DEBUG_OBJECT (trans, "intersect returned %" GST_PTR_FORMAT, temp);
     if (temp) {
-      if (!gst_caps_is_empty (temp) && otherpeer) {
+      if (!gst_caps_is_empty (temp)) {
         GST_DEBUG_OBJECT (trans, "try passthrough with %" GST_PTR_FORMAT, caps);
-        /* try passthrough. we know it's fixed, because caps is fixed */
-        if (gst_pad_accept_caps (otherpeer, caps)) {
-          GST_DEBUG_OBJECT (trans, "peer accepted %" GST_PTR_FORMAT, caps);
-          /* peer accepted unmodified caps, we free the original non-fixed
-           * caps and work with the passthrough caps */
+        if (otherpeer) {
+          /* try passthrough. we know it's fixed, because caps is fixed */
+          if (gst_pad_accept_caps (otherpeer, caps)) {
+            GST_DEBUG_OBJECT (trans, "peer accepted %" GST_PTR_FORMAT, caps);
+            /* peer accepted unmodified caps, we free the original non-fixed
+             * caps and work with the passthrough caps */
+            gst_caps_unref (othercaps);
+            othercaps = gst_caps_ref (caps);
+            /* mark that we checked othercaps with the peer, this
+             * makes sure we don't call accept_caps again with these same
+             * caps */
+            peer_checked = TRUE;
+          } else {
+            GST_DEBUG_OBJECT (trans,
+                "peer did not accept %" GST_PTR_FORMAT, caps);
+          }
+        } else {
+          GST_DEBUG_OBJECT (trans, "no peer, doing passthrough");
           gst_caps_unref (othercaps);
           othercaps = gst_caps_ref (caps);
-          /* mark that we checked othercaps with the peer, this
-           * makes sure we don't call accept_caps again with these same
-           * caps */
-          peer_checked = TRUE;
-        } else {
-          GST_DEBUG_OBJECT (trans,
-              "peer did not accept %" GST_PTR_FORMAT, caps);
         }
       }
       gst_caps_unref (temp);
@@ -702,35 +896,120 @@ gst_base_transform_setcaps (GstPad * pad, GstCaps * caps)
   if (!peer_checked && otherpeer && !gst_pad_accept_caps (otherpeer, othercaps))
     goto peer_no_accept;
 
-  GST_DEBUG_OBJECT (trans, "got final caps %" GST_PTR_FORMAT, othercaps);
+  GST_DEBUG_OBJECT (trans, "Input caps were %" GST_PTR_FORMAT
+      ", and got final caps %" GST_PTR_FORMAT, caps, othercaps);
 
-  trans->have_same_caps = gst_caps_is_equal (caps, othercaps);
-  GST_DEBUG_OBJECT (trans, "have_same_caps: %d", trans->have_same_caps);
+  if (otherpeer)
+    gst_object_unref (otherpeer);
 
-  /* see if we have to configure the element now */
-  if (!trans->delay_configure) {
-    GstCaps *incaps, *outcaps;
+  return othercaps;
 
-    /* make sure in and out caps are correct */
-    if (pad == trans->sinkpad) {
-      incaps = caps;
-      outcaps = othercaps;
-    } else {
-      incaps = othercaps;
-      outcaps = caps;
-    }
-    /* call configure now */
-    if (!(ret = gst_base_transform_configure_caps (trans, incaps, outcaps)))
-      goto failed_configure;
+  /* ERRORS */
+no_transform:
+  {
+    GST_DEBUG_OBJECT (trans,
+        "transform returned useless  %" GST_PTR_FORMAT, othercaps);
+    goto error_cleanup;
+  }
+no_transform_possible:
+  {
+    GST_DEBUG_OBJECT (trans,
+        "transform could not transform %" GST_PTR_FORMAT
+        " in anything we support", caps);
+    goto error_cleanup;
+  }
+could_not_fixate:
+  {
+    GST_DEBUG_OBJECT (trans, "FAILED to fixate %" GST_PTR_FORMAT, othercaps);
+    goto error_cleanup;
+  }
+peer_no_accept:
+  {
+    GST_DEBUG_OBJECT (trans, "FAILED to get peer of %" GST_PTR_FORMAT
+        " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
+    goto error_cleanup;
+  }
+error_cleanup:
+  {
+    if (otherpeer)
+      gst_object_unref (otherpeer);
+    if (othercaps)
+      gst_caps_unref (othercaps);
+    return NULL;
+  }
+}
+
+/* called when new caps arrive on the sink or source pad,
+ * We try to find the best caps for the other side using our _find_transform()
+ * function. If there are caps, we configure the transform for this new
+ * transformation.
+ *
+ * FIXME, this function is currently commutative but this should not really be
+ * because we never set caps starting from the srcpad.
+ */
+static gboolean
+gst_base_transform_setcaps (GstPad * pad, GstCaps * caps)
+{
+  GstBaseTransform *trans;
+  GstBaseTransformClass *klass;
+  GstPad *otherpad, *otherpeer;
+  GstCaps *othercaps = NULL;
+  gboolean ret = TRUE;
+  GstCaps *incaps, *outcaps;
+
+  trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
+  klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
+
+  otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
+  otherpeer = gst_pad_get_peer (otherpad);
+
+  /* if we get called recursively, we bail out now to avoid an
+   * infinite loop. */
+  if (GST_PAD_IS_IN_SETCAPS (otherpad))
+    goto done;
+
+  GST_DEBUG_OBJECT (pad, "have new caps %" GST_PTR_FORMAT, caps);
+
+  /* find best possible caps for the other pad */
+  othercaps = gst_base_transform_find_transform (trans, pad, caps);
+  if (!othercaps || gst_caps_is_empty (othercaps))
+    goto no_transform_possible;
+
+  /* configure the element now */
+  /* make sure in and out caps are correct */
+  if (pad == trans->sinkpad) {
+    incaps = caps;
+    outcaps = othercaps;
   } else {
-    /* set pending configure, the configure will happen later with the
-     * caps we set on the pads above. */
-    trans->pending_configure = TRUE;
+    incaps = othercaps;
+    outcaps = caps;
   }
 
+#if 0
+  /* if we have the same caps, we can optimize and reuse the input caps */
+  if (gst_caps_is_equal (incaps, outcaps)) {
+    GST_INFO_OBJECT (trans, "reuse caps");
+    gst_caps_unref (outcaps);
+    outcaps = gst_caps_ref (incaps);
+  }
+#endif
+
+  /* call configure now */
+  if (!(ret = gst_base_transform_configure_caps (trans, incaps, outcaps)))
+    goto failed_configure;
+
   /* we know this will work, we implement the setcaps */
   gst_pad_set_caps (otherpad, othercaps);
 
+  if (pad == trans->srcpad && trans->priv->pad_mode == GST_ACTIVATE_PULL) {
+    /* FIXME hm? */
+    ret &= gst_pad_set_caps (otherpeer, othercaps);
+    if (!ret) {
+      GST_INFO_OBJECT (trans, "otherpeer setcaps(%" GST_PTR_FORMAT ") failed",
+          othercaps);
+    }
+  }
+
 done:
   if (otherpeer)
     gst_object_unref (otherpeer);
@@ -744,147 +1023,256 @@ done:
   return ret;
 
   /* ERRORS */
-unfixed_caps:
-  {
-    GST_DEBUG_OBJECT (trans, "caps are not fixed  %" GST_PTR_FORMAT, caps);
-    ret = FALSE;
-    goto done;
-  }
-no_transform:
-  {
-    GST_DEBUG_OBJECT (trans,
-        "transform returned useless  %" GST_PTR_FORMAT, othercaps);
-    ret = FALSE;
-    goto done;
-  }
 no_transform_possible:
   {
-    GST_DEBUG_OBJECT (trans,
+    GST_WARNING_OBJECT (trans,
         "transform could not transform %" GST_PTR_FORMAT
         " in anything we support", caps);
     ret = FALSE;
     goto done;
   }
-could_not_fixate:
-  {
-    GST_ERROR_OBJECT (trans, "FAILED to fixate %" GST_PTR_FORMAT, othercaps);
-    ret = FALSE;
-    goto done;
-  }
-peer_no_accept:
-  {
-    GST_DEBUG_OBJECT (trans, "FAILED to get peer of %" GST_PTR_FORMAT
-        " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
-    ret = FALSE;
-    goto done;
-  }
 failed_configure:
   {
-    GST_DEBUG_OBJECT (trans, "FAILED to configure caps %" GST_PTR_FORMAT
+    GST_WARNING_OBJECT (trans, "FAILED to configure caps %" GST_PTR_FORMAT
         " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
     ret = FALSE;
     goto done;
   }
 }
 
-/* Allocate a buffer using gst_pad_alloc_buffer_and_set_caps.
+/* Allocate a buffer using gst_pad_alloc_buffer
+ *
+ * This function can do renegotiation on the source pad
  *
- * This function can trigger a renegotiation on the source pad when the
- * peer alloc_buffer function sets new caps. Since we currently are
- * processing a buffer on the sinkpad when this function is called, we cannot
- * reconfigure the transform with sinkcaps different from those of the current
- * buffer. FIXME, we currently don't check if the pluging can transform to the
- * new srcpad caps using the same sinkcaps, we alloc a proper outbuf buffer
- * ourselves instead.
+ * The output buffer is always writable. outbuf can be equal to
+ * inbuf, the caller should be prepared for this and perform 
+ * appropriate refcounting.
  */
 static GstFlowReturn
-gst_base_transform_prepare_output_buf (GstBaseTransform * trans,
-    GstBuffer * in_buf, gint out_size, GstCaps * out_caps, GstBuffer ** out_buf)
+gst_base_transform_prepare_output_buffer (GstBaseTransform * trans,
+    GstBuffer * in_buf, GstBuffer ** out_buf)
 {
   GstBaseTransformClass *bclass;
+  GstBaseTransformPrivate *priv;
   GstFlowReturn ret = GST_FLOW_OK;
-  gboolean copy_inbuf = FALSE;
+  guint outsize, newsize;
+  gboolean discard;
+  GstCaps *incaps, *oldcaps, *newcaps;
 
   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
 
-  /* we cannot reconfigure the element yet as we are still processing
-   * the old buffer. We will therefore delay the reconfiguration of the
-   * element until we have processed this last buffer. */
-  trans->delay_configure = TRUE;
-  /* out_caps is the caps of the src pad gathered through the GST_PAD_CAPS 
-     macro. If a set_caps occurs during this function this caps will become
-     invalid. We want to keep them during preparation of the output buffer. */
-  if (out_caps)
-    gst_caps_ref (out_caps);
-
-  /* see if the subclass wants to alloc a buffer */
+  priv = trans->priv;
+
+  /* figure out how to allocate a buffer based on the current configuration */
+  if (trans->passthrough) {
+    GST_DEBUG_OBJECT (trans, "doing passthrough alloc");
+    /* passthrough, we don't really need to call pad alloc but we still need to
+     * in order to get upstream negotiation. The output size is the same as the
+     * input size. */
+    outsize = GST_BUFFER_SIZE (in_buf);
+    /* we always alloc and discard here */
+    discard = TRUE;
+  } else {
+    gboolean want_in_place = (bclass->transform_ip != NULL)
+        && trans->always_in_place;
+
+    if (want_in_place) {
+      GST_DEBUG_OBJECT (trans, "doing inplace alloc");
+      /* we alloc a buffer of the same size as the input */
+      outsize = GST_BUFFER_SIZE (in_buf);
+      /* only discard it when the input was not writable, otherwise, we reuse
+       * the input buffer. */
+      discard = gst_buffer_is_writable (in_buf);
+    } else {
+      GST_DEBUG_OBJECT (trans, "getting output size for copy transform");
+      /* copy transform, figure out the output size */
+      if (!gst_base_transform_transform_size (trans,
+              GST_PAD_SINK, GST_PAD_CAPS (trans->sinkpad),
+              GST_BUFFER_SIZE (in_buf), GST_PAD_CAPS (trans->srcpad),
+              &outsize)) {
+        goto unknown_size;
+      }
+      /* never discard this buffer, we need it for storing the output */
+      discard = FALSE;
+    }
+  }
+
+  oldcaps = GST_PAD_CAPS (trans->srcpad);
+
   if (bclass->prepare_output_buffer) {
+    GST_DEBUG_OBJECT (trans,
+        "calling prepare buffer with caps %" GST_PTR_FORMAT, oldcaps);
     ret =
-        bclass->prepare_output_buffer (trans, in_buf, out_size, out_caps,
+        bclass->prepare_output_buffer (trans, in_buf, outsize, oldcaps,
         out_buf);
-    if (ret != GST_FLOW_OK)
-      goto done;
+
+    /* FIXME 0.11:
+     * decrease refcount again if vmethod returned refcounted in_buf. This
+     * is because we need to make sure that the buffer is writable for the
+     * in_place transform. The docs of the vmethod say that you should return
+     * a reffed inbuf, which is exactly what we don't want :), oh well.. */
+    if (in_buf == *out_buf)
+      gst_buffer_unref (in_buf);
+
+    /* never discard the buffer from the prepare_buffer method */
+    discard = FALSE;
+  } else {
+    GST_DEBUG_OBJECT (trans, "doing alloc with caps %" GST_PTR_FORMAT, oldcaps);
+
+    ret = gst_pad_alloc_buffer (trans->srcpad,
+        GST_BUFFER_OFFSET (in_buf), outsize, oldcaps, out_buf);
   }
 
-  /* See if we want to prepare the buffer for in place output */
-  if (*out_buf == NULL && GST_BUFFER_SIZE (in_buf) == out_size
-      && bclass->transform_ip) {
-    if (gst_buffer_is_writable (in_buf)) {
-      if (trans->have_same_caps) {
-        /* Input buffer is already writable and caps are the same, just ref and return it */
-        *out_buf = in_buf;
-        gst_buffer_ref (in_buf);
-      } else {
-        /* Writable buffer, but need to change caps => subbuffer */
-        *out_buf = gst_buffer_create_sub (in_buf, 0, GST_BUFFER_SIZE (in_buf));
-        gst_caps_replace (&GST_BUFFER_CAPS (*out_buf), out_caps);
+  if (ret != GST_FLOW_OK)
+    goto alloc_failed;
+
+  /* must always return a buffer */
+  g_assert (*out_buf != NULL);
+
+  /* check if we got different caps on this new output buffer */
+  newcaps = GST_BUFFER_CAPS (*out_buf);
+  newsize = GST_BUFFER_SIZE (*out_buf);
+  if (!gst_caps_is_equal (newcaps, oldcaps)) {
+    GstCaps *othercaps;
+    gboolean can_convert;
+
+    GST_DEBUG_OBJECT (trans, "received new caps %" GST_PTR_FORMAT, newcaps);
+
+    incaps = GST_PAD_CAPS (trans->sinkpad);
+
+    /* it's possible that the buffer we got is of the wrong size, get the
+     * expected size here */
+    gst_base_transform_transform_size (trans,
+        GST_PAD_SINK, incaps, GST_BUFFER_SIZE (in_buf), newcaps, &outsize);
+
+    /* check if we can convert the current incaps to the new target caps */
+    can_convert =
+        gst_base_transform_can_transform (trans, trans->sinkpad, incaps,
+        newcaps);
+
+    if (can_convert) {
+      GST_DEBUG_OBJECT (trans, "reconfigure transform for current buffer");
+      /* caps not empty, try to renegotiate to the new format */
+      if (!gst_base_transform_configure_caps (trans, incaps, newcaps)) {
+        /* not sure we need to fail hard here, we can simply continue our
+         * conversion with what we negotiated before */
+        goto failed_configure;
       }
-      goto done;
+      /* new format configure, and use the new output buffer */
+      gst_pad_set_caps (trans->srcpad, newcaps);
+      discard = FALSE;
     } else {
-      /* Make a writable buffer below and copy the data */
-      copy_inbuf = TRUE;
+      GST_DEBUG_OBJECT (trans, "cannot perform transform on current buffer");
+
+      /* we cannot convert the current buffer but we might be able to suggest a
+       * new format upstream, try to find what the best format is. */
+      othercaps =
+          gst_base_transform_find_transform (trans, trans->srcpad, newcaps);
+
+      if (!othercaps) {
+        GST_DEBUG_OBJECT (trans, "incompatible caps, ignoring");
+        /* we received caps that we cannot transform. Upstream is behaving badly
+         * because it should have checked if we could handle these caps. We can
+         * simply ignore these caps and produce a buffer with our original caps. */
+      } else {
+        guint size_suggest;
+
+        GST_DEBUG_OBJECT (trans, "getting size of suggestion");
+
+        /* not a subset, we have a new upstream suggestion, remember it and
+         * allocate a default buffer. First we try to convert the size */
+        if (gst_base_transform_transform_size (trans,
+                GST_PAD_SRC, newcaps, outsize, othercaps, &size_suggest)) {
+
+          /* ok, remember the suggestions now */
+          GST_DEBUG_OBJECT (trans,
+              "storing new caps and size suggestion of %u and %" GST_PTR_FORMAT,
+              size_suggest, othercaps);
+
+          GST_OBJECT_LOCK (trans->sinkpad);
+          if (priv->sink_suggest)
+            gst_caps_unref (priv->sink_suggest);
+          priv->sink_suggest = gst_caps_ref (othercaps);
+          priv->size_suggest = size_suggest;
+          GST_OBJECT_UNLOCK (trans->sinkpad);
+        }
+        gst_caps_unref (othercaps);
+      }
+      if (in_buf != *out_buf)
+        gst_buffer_unref (*out_buf);
+      *out_buf = NULL;
+    }
+
+    /* if we got a buffer of the wrong size, discard it now */
+    if (newsize != outsize) {
+      if (in_buf != *out_buf)
+        gst_buffer_unref (*out_buf);
+      *out_buf = NULL;
+      discard = FALSE;
     }
   }
 
   if (*out_buf == NULL) {
-    /* Sub-class didn't already provide a buffer for us. Make one */
-    ret =
-        gst_pad_alloc_buffer_and_set_caps (trans->srcpad,
-        GST_BUFFER_OFFSET (in_buf), out_size, out_caps, out_buf);
-    if (ret != GST_FLOW_OK || *out_buf == NULL)
-      goto done;
-
-    /* allocated buffer could be of different caps than what we requested */
-    if (G_UNLIKELY (!gst_caps_is_equal (out_caps, GST_BUFFER_CAPS (*out_buf)))) {
-      /* FIXME, it is possible we can reconfigure the transform with new caps at this
-       * point but for now we just create a buffer ourselves */
+    if (!discard) {
+      GST_DEBUG_OBJECT (trans, "make default output buffer");
+      /* no valid buffer yet, make one */
+      *out_buf = gst_buffer_new_and_alloc (outsize);
+      gst_buffer_copy_metadata (*out_buf, in_buf,
+          GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS);
+    } else {
+      GST_DEBUG_OBJECT (trans, "reuse input buffer");
+      *out_buf = in_buf;
+    }
+  } else {
+    if (discard) {
+      GST_DEBUG_OBJECT (trans, "discard buffer, reuse input buffer");
       gst_buffer_unref (*out_buf);
-      *out_buf = gst_buffer_new_and_alloc (out_size);
-      gst_buffer_set_caps (*out_buf, out_caps);
+      *out_buf = in_buf;
+    } else {
+      GST_DEBUG_OBJECT (trans, "using pad-alloc buffer");
+      gst_buffer_copy_metadata (*out_buf, in_buf,
+          GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS);
     }
   }
+  gst_buffer_set_caps (*out_buf, GST_PAD_CAPS (trans->srcpad));
 
-  /* If the output buffer metadata is modifiable, copy timestamps and
-   * buffer flags */
-  if (*out_buf != in_buf && gst_buffer_is_metadata_writable (*out_buf) == 1) {
+  /* clear the GAP flag when the subclass does not understand it */
+  if (!trans->priv->gap_aware)
+    GST_BUFFER_FLAG_UNSET (*out_buf, GST_BUFFER_FLAG_GAP);
 
-    if (copy_inbuf && gst_buffer_is_writable (*out_buf))
-      memcpy (GST_BUFFER_DATA (*out_buf), GST_BUFFER_DATA (in_buf), out_size);
+  return ret;
 
-    gst_buffer_stamp (*out_buf, in_buf);
-    GST_BUFFER_FLAGS (*out_buf) |= GST_BUFFER_FLAGS (in_buf) &
-        (GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_IN_CAPS |
-        GST_BUFFER_FLAG_DELTA_UNIT);
+  /* ERRORS */
+alloc_failed:
+  {
+    GST_WARNING_OBJECT (trans, "pad-alloc failed: %s", gst_flow_get_name (ret));
+    return ret;
+  }
+unknown_size:
+  {
+    GST_ERROR_OBJECT (trans, "unknown output size");
+    return GST_FLOW_ERROR;
+  }
+failed_configure:
+  {
+    GST_WARNING_OBJECT (trans, "failed to configure caps");
+    return GST_FLOW_NOT_NEGOTIATED;
   }
-
-done:
-  if (out_caps)
-    gst_caps_unref (out_caps);
-  trans->delay_configure = FALSE;
-
-  return ret;
 }
 
+/* Given @caps calcultate the size of one unit.
+ *
+ * For video caps, this is the size of one frame (and thus one buffer).
+ * For audio caps, this is the size of one sample.
+ *
+ * These values are cached since they do not change and the calculation
+ * potentially involves parsing caps and other expensive stuff.
+ *
+ * We have two cache locations to store the size, one for the source caps
+ * and one for the sink caps.
+ *
+ * this function returns FALSE if no size could be calculated.
+ */
 static gboolean
 gst_base_transform_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
     guint * size)
@@ -895,12 +1283,12 @@ gst_base_transform_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
   /* see if we have the result cached */
   if (trans->cache_caps1 == caps) {
     *size = trans->cache_caps1_size;
-    GST_DEBUG_OBJECT (trans, "get size returned cached 1 %d", *size);
+    GST_DEBUG_OBJECT (trans, "returned %d from first cache", *size);
     return TRUE;
   }
   if (trans->cache_caps2 == caps) {
     *size = trans->cache_caps2_size;
-    GST_DEBUG_OBJECT (trans, "get size returned cached 2 %d", *size);
+    GST_DEBUG_OBJECT (trans, "returned %d from second cached", *size);
     return TRUE;
   }
 
@@ -911,16 +1299,21 @@ gst_base_transform_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
         ") has unit size %d, result %s", caps, *size, res ? "TRUE" : "FALSE");
 
     if (res) {
+      /* and cache the values */
       if (trans->cache_caps1 == NULL) {
         gst_caps_replace (&trans->cache_caps1, caps);
         trans->cache_caps1_size = *size;
+        GST_DEBUG_OBJECT (trans, "caching %d in first cache", *size);
       } else if (trans->cache_caps2 == NULL) {
         gst_caps_replace (&trans->cache_caps2, caps);
         trans->cache_caps2_size = *size;
+        GST_DEBUG_OBJECT (trans, "caching %d in second cache", *size);
+      } else {
+        GST_DEBUG_OBJECT (trans, "no free spot to cache unit_size");
       }
     }
   } else {
-    GST_DEBUG ("Sub-class does not implement get_unit_size");
+    GST_DEBUG_OBJECT (trans, "Sub-class does not implement get_unit_size");
   }
   return res;
 }
@@ -934,141 +1327,180 @@ gst_base_transform_buffer_alloc (GstPad * pad, guint64 offset, guint size,
     GstCaps * caps, GstBuffer ** buf)
 {
   GstBaseTransform *trans;
+  GstBaseTransformPrivate *priv;
   GstFlowReturn res;
-  guint new_size;
+  gboolean proxy = FALSE, suggest = FALSE;
+  GstCaps *sink_suggest;
+  guint size_suggest = 0;
 
   trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
+  priv = trans->priv;
 
-  /* we cannot run this when we are transforming data and as such doing 
-   * another negotiation in the transform method. */
-  g_mutex_lock (trans->transform_lock);
+  GST_DEBUG_OBJECT (pad, "alloc with caps %" GST_PTR_FORMAT ", size %u", caps,
+      size);
 
   *buf = NULL;
 
-  GST_DEBUG_OBJECT (trans, "allocating a buffer of size %d ...", size, offset);
-  if (offset == GST_BUFFER_OFFSET_NONE)
-    GST_DEBUG_OBJECT (trans, "... and offset NONE");
-  else
-    GST_DEBUG_OBJECT (trans, "... and offset %" G_GUINT64_FORMAT, offset);
-
-  /* before any buffers are pushed, have_same_caps is TRUE; allocating can trigger
-   * a renegotiation and change that to FALSE */
-  if (trans->have_same_caps) {
-    /* request a buffer with the same caps */
-    GST_DEBUG_OBJECT (trans, "requesting buffer with same caps, size %d", size);
-
-    res =
-        gst_pad_alloc_buffer_and_set_caps (trans->srcpad, offset, size, caps,
-        buf);
+  /* we remember our previous alloc request to quickly see if we can proxy or
+   * not. */
+  if (caps && gst_caps_is_equal (priv->sink_alloc, caps)) {
+    /* we have seen this before, see if we need to proxy */
+    GST_DEBUG_OBJECT (trans, "have old caps");
+    sink_suggest = caps;
+    suggest = FALSE;
+    res = GST_FLOW_OK;
   } else {
-    /* if we are configured, request a buffer with the src caps */
-    GstCaps *srccaps = gst_pad_get_negotiated_caps (trans->srcpad);
-    GstCaps *sinkcaps = gst_pad_get_negotiated_caps (trans->sinkpad);
-
-    if (!srccaps)
-      goto not_configured;
-
-    if (sinkcaps != NULL) {
-      if (sinkcaps != caps || !gst_caps_is_equal (sinkcaps, caps)) {
-        gst_caps_unref (sinkcaps);
-        gst_caps_unref (srccaps);
-        goto not_configured;
-      }
-      gst_caps_unref (sinkcaps);
+    GstCaps *temp, *othercaps;
+    const GstCaps *templ;
+    gboolean empty;
+
+    GST_DEBUG_OBJECT (trans, "new format %" GST_PTR_FORMAT, caps);
+
+    /* if we have a suggestion, pretend we got these as input */
+    GST_OBJECT_LOCK (pad);
+    if ((priv->sink_suggest && !gst_caps_is_equal (caps, priv->sink_suggest))) {
+      sink_suggest = gst_caps_ref (priv->sink_suggest);
+      size_suggest = priv->size_suggest;
+      GST_DEBUG_OBJECT (trans, "have suggestion %" GST_PTR_FORMAT,
+          sink_suggest);
+      suggest = TRUE;
+    } else {
+      GST_DEBUG_OBJECT (trans, "using caps %" GST_PTR_FORMAT, caps);
+      sink_suggest = caps;
+      suggest = FALSE;
     }
+    GST_OBJECT_UNLOCK (pad);
 
-    GST_DEBUG_OBJECT (trans, "calling transform_size");
-    if (!gst_base_transform_transform_size (trans,
-            GST_PAD_DIRECTION (pad), caps, size, srccaps, &new_size)) {
-      gst_caps_unref (srccaps);
-      goto unknown_size;
-    }
+    /* check if we actually handle this format on the sinkpad */
+    if (sink_suggest) {
+      templ = gst_pad_get_pad_template_caps (pad);
+      temp = gst_caps_intersect (sink_suggest, templ);
 
-    res =
-        gst_pad_alloc_buffer_and_set_caps (trans->srcpad, offset, new_size,
-        srccaps, buf);
-    gst_caps_unref (srccaps);
-  }
+      empty = gst_caps_is_empty (temp);
+      gst_caps_unref (temp);
 
-  if (res == GST_FLOW_OK && !trans->have_same_caps) {
-    /* note that we might have had same caps before, but calling the
-       alloc_buffer caused setcaps to switch us out of in_place -- in any case
-       the alloc_buffer served to transmit caps information but we can't use the
-       buffer. fall through and allocate a buffer corresponding to our sink
-       caps, if any */
-    GstCaps *sinkcaps = gst_pad_get_negotiated_caps (trans->sinkpad);
-    GstCaps *srccaps = gst_pad_get_negotiated_caps (trans->srcpad);
-
-    if (!sinkcaps)
-      goto not_configured;
-
-    if (!gst_base_transform_transform_size (trans,
-            GST_PAD_DIRECTION (trans->srcpad), srccaps, GST_BUFFER_SIZE (*buf),
-            sinkcaps, &new_size)) {
-      gst_caps_unref (srccaps);
-      gst_caps_unref (sinkcaps);
-      goto unknown_size;
+      if (empty)
+        goto not_supported;
     }
 
-    gst_buffer_unref (*buf);
-
-    *buf = gst_buffer_new_and_alloc (new_size);
-    gst_buffer_set_caps (*buf, sinkcaps);
-    GST_BUFFER_OFFSET (*buf) = offset;
+    /* find the best format for the other side */
+    if (sink_suggest == NULL) {
+      /* always proxy when the caps are NULL. When this is a new format, see if
+       * we can proxy it downstream */
+      GST_DEBUG_OBJECT (trans, "null caps, marking for proxy");
+      priv->proxy_alloc = TRUE;
+    } else {
+      othercaps = gst_base_transform_find_transform (trans, pad, sink_suggest);
+      if (!othercaps || gst_caps_is_empty (othercaps)) {
+        /* no transform, can't proxy */
+        GST_DEBUG_OBJECT (trans, "can't find transform, disable proxy");
+        priv->proxy_alloc = FALSE;
+      } else {
+        if (gst_caps_is_equal (caps, othercaps)) {
+          GST_DEBUG_OBJECT (trans,
+              "best caps same as input, marking for proxy");
+          priv->proxy_alloc = TRUE;
+        } else {
+          GST_DEBUG_OBJECT (trans,
+              "best caps different from input, disable proxy");
+          priv->proxy_alloc = FALSE;
+        }
+      }
+      if (othercaps)
+        gst_caps_unref (othercaps);
+    }
+  }
+  /* remember the new caps */
+  gst_caps_replace (&priv->sink_alloc, sink_suggest);
+  proxy = priv->proxy_alloc;
+  GST_DEBUG_OBJECT (trans, "doing default alloc, proxy %d", proxy);
+
+  if (proxy) {
+    GstCaps *newcaps;
+
+    GST_DEBUG_OBJECT (trans, "proxy buffer-alloc with caps %" GST_PTR_FORMAT
+        ", size %u", caps, size);
+
+    res = gst_pad_alloc_buffer (trans->srcpad, offset, size, caps, buf);
+    if (res != GST_FLOW_OK)
+      goto alloc_failed;
+
+    /* check if the caps changed */
+    newcaps = GST_BUFFER_CAPS (*buf);
+
+    GST_DEBUG_OBJECT (trans, "got caps %" GST_PTR_FORMAT, newcaps);
+
+    if (newcaps != caps) {
+      GST_DEBUG_OBJECT (trans, "got new caps %" GST_PTR_FORMAT, newcaps);
+      if (!suggest) {
+        /* we have new caps, see if we can proxy downstream */
+        if (gst_pad_peer_accept_caps (trans->sinkpad, newcaps)) {
+          /* peer accepts the caps, return a buffer in this format */
+          GST_DEBUG_OBJECT (trans, "peer accepted new caps");
+          /* remember the format */
+          gst_caps_replace (&priv->sink_alloc, newcaps);
+        } else {
+          GST_DEBUG_OBJECT (trans, "peer did not accept new caps");
+          /* peer does not accept the caps, create a buffer of the requested
+           * format. */
+          gst_buffer_unref (*buf);
+          *buf = NULL;
+        }
+      }
+    }
+  }
+  if (suggest) {
+    *buf = gst_buffer_new_and_alloc (size_suggest);
+    GST_DEBUG_OBJECT (trans,
+        "doing suggestion of size %u, caps %" GST_PTR_FORMAT, size_suggest,
+        sink_suggest);
+    GST_BUFFER_CAPS (*buf) = sink_suggest;
     res = GST_FLOW_OK;
-
-    gst_caps_unref (srccaps);
-    gst_caps_unref (sinkcaps);
   }
-  g_mutex_unlock (trans->transform_lock);
 
   gst_object_unref (trans);
 
   return res;
 
-not_configured:
+  /* ERRORS */
+alloc_failed:
   {
-    /* let the default allocator handle it */
-    GST_DEBUG_OBJECT (trans, "not configured");
-    if (*buf) {
-      gst_buffer_unref (*buf);
-      *buf = NULL;
-    }
-    g_mutex_unlock (trans->transform_lock);
+    GST_DEBUG_OBJECT (trans, "pad alloc failed: %s", gst_flow_get_name (res));
+    if (suggest)
+      gst_caps_unref (sink_suggest);
     gst_object_unref (trans);
-    return GST_FLOW_OK;
+    return res;
   }
-unknown_size:
+not_supported:
   {
-    /* let the default allocator handle it */
-    GST_DEBUG_OBJECT (trans, "unknown size");
-    if (*buf) {
-      gst_buffer_unref (*buf);
-      *buf = NULL;
-    }
-    g_mutex_unlock (trans->transform_lock);
+    GST_DEBUG_OBJECT (trans, "pad alloc with unsupported caps");
+    if (suggest)
+      gst_caps_unref (sink_suggest);
     gst_object_unref (trans);
-    return GST_FLOW_OK;
+    return GST_FLOW_NOT_NEGOTIATED;
   }
 }
 
 static gboolean
-gst_base_transform_event (GstPad * pad, GstEvent * event)
+gst_base_transform_sink_event (GstPad * pad, GstEvent * event)
 {
   GstBaseTransform *trans;
   GstBaseTransformClass *bclass;
   gboolean ret = TRUE;
+  gboolean forward = TRUE;
 
   trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
 
   if (bclass->event)
-    ret = bclass->event (trans, event);
+    forward = bclass->event (trans, event);
 
   /* FIXME, do this in the default event handler so the subclass can do
    * something different. */
-  if (ret)
-    ret = gst_pad_event_default (pad, event);
+  if (forward)
+    ret = gst_pad_push_event (trans->srcpad, event);
+  else
+    gst_event_unref (event);
 
   gst_object_unref (trans);
 
@@ -1076,13 +1508,20 @@ gst_base_transform_event (GstPad * pad, GstEvent * event)
 }
 
 static gboolean
-gst_base_transform_eventfunc (GstBaseTransform * trans, GstEvent * event)
+gst_base_transform_sink_eventfunc (GstBaseTransform * trans, GstEvent * event)
 {
   switch (GST_EVENT_TYPE (event)) {
     case GST_EVENT_FLUSH_START:
       break;
     case GST_EVENT_FLUSH_STOP:
+      GST_OBJECT_LOCK (trans);
+      /* reset QoS parameters */
+      trans->priv->proportion = 1.0;
+      trans->priv->earliest_time = -1;
+      trans->priv->discont = FALSE;
+      GST_OBJECT_UNLOCK (trans);
       /* we need new segment info after the flush. */
+      trans->have_newsegment = FALSE;
       gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
       break;
     case GST_EVENT_EOS:
@@ -1092,20 +1531,20 @@ gst_base_transform_eventfunc (GstBaseTransform * trans, GstEvent * event)
     case GST_EVENT_NEWSEGMENT:
     {
       GstFormat format;
-      gdouble rate;
+      gdouble rate, arate;
       gint64 start, stop, time;
       gboolean update;
 
-      gst_event_parse_new_segment (event, &update, &rate, &format, &start,
-          &stop, &time);
-
-      gst_segment_set_newsegment (&trans->segment, update, rate, format, start,
-          stop, time);
+      gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
+          &start, &stop, &time);
 
       trans->have_newsegment = TRUE;
 
+      gst_segment_set_newsegment_full (&trans->segment, update, rate, arate,
+          format, start, stop, time);
+
       if (format == GST_FORMAT_TIME) {
-        GST_DEBUG_OBJECT (trans, "received NEW_SEGMENT %" GST_TIME_FORMAT
+        GST_DEBUG_OBJECT (trans, "received TIME NEW_SEGMENT %" GST_TIME_FORMAT
             " -- %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT
             ", accum %" GST_TIME_FORMAT,
             GST_TIME_ARGS (trans->segment.start),
@@ -1128,23 +1567,75 @@ gst_base_transform_eventfunc (GstBaseTransform * trans, GstEvent * event)
   return TRUE;
 }
 
+static gboolean
+gst_base_transform_src_event (GstPad * pad, GstEvent * event)
+{
+  GstBaseTransform *trans;
+  GstBaseTransformClass *bclass;
+  gboolean ret = TRUE;
+
+  trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
+  bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
+
+  if (bclass->src_event)
+    ret = bclass->src_event (trans, event);
+
+  gst_object_unref (trans);
+
+  return ret;
+}
+
+static gboolean
+gst_base_transform_src_eventfunc (GstBaseTransform * trans, GstEvent * event)
+{
+  gboolean ret;
+
+  switch (GST_EVENT_TYPE (event)) {
+    case GST_EVENT_SEEK:
+      break;
+    case GST_EVENT_NAVIGATION:
+      break;
+    case GST_EVENT_QOS:
+    {
+      gdouble proportion;
+      GstClockTimeDiff diff;
+      GstClockTime timestamp;
+
+      gst_event_parse_qos (event, &proportion, &diff, &timestamp);
+      gst_base_transform_update_qos (trans, proportion, diff, timestamp);
+      break;
+    }
+    default:
+      break;
+  }
+
+  ret = gst_pad_push_event (trans->sinkpad, event);
+
+  return ret;
+}
+
+/* perform a transform on @inbuf and put the result in @outbuf.
+ *
+ * This function is common to the push and pull-based operations.
+ *
+ * This function takes ownership of @inbuf */
 static GstFlowReturn
 gst_base_transform_handle_buffer (GstBaseTransform * trans, GstBuffer * inbuf,
     GstBuffer ** outbuf)
 {
   GstBaseTransformClass *bclass;
   GstFlowReturn ret = GST_FLOW_OK;
-  guint out_size;
   gboolean want_in_place;
+  GstClockTime qostime;
 
   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
 
   if (GST_BUFFER_OFFSET_IS_VALID (inbuf))
-    GST_LOG_OBJECT (trans, "handling buffer %p of size %d and offset %"
+    GST_DEBUG_OBJECT (trans, "handling buffer %p of size %d and offset %"
         G_GUINT64_FORMAT, inbuf, GST_BUFFER_SIZE (inbuf),
         GST_BUFFER_OFFSET (inbuf));
   else
-    GST_LOG_OBJECT (trans, "handling buffer %p of size %d and offset NONE",
+    GST_DEBUG_OBJECT (trans, "handling buffer %p of size %d and offset NONE",
         inbuf, GST_BUFFER_SIZE (inbuf));
 
   /* Don't allow buffer handling before negotiation, except in passthrough mode
@@ -1154,78 +1645,93 @@ gst_base_transform_handle_buffer (GstBaseTransform * trans, GstBuffer * inbuf,
   if (!trans->negotiated && !trans->passthrough && (bclass->set_caps != NULL))
     goto not_negotiated;
 
-  if (trans->passthrough) {
-    /* In passthrough mode, give transform_ip a look at the
-     * buffer, without making it writable, or just push the
-     * data through */
-    GST_LOG_OBJECT (trans, "element is in passthrough mode");
-
-    if (bclass->transform_ip)
-      ret = bclass->transform_ip (trans, inbuf);
-
-    *outbuf = inbuf;
-
-    return ret;
+  /* Set discont flag so we can mark the outgoing buffer */
+  if (GST_BUFFER_IS_DISCONT (inbuf)) {
+    GST_DEBUG_OBJECT (trans, "got DISCONT buffer %p", inbuf);
+    trans->priv->discont = TRUE;
   }
 
-  want_in_place = (bclass->transform_ip != NULL) && trans->always_in_place;
-  *outbuf = NULL;
-
-  if (want_in_place) {
-    /* If want_in_place is TRUE, we may need to prepare a new output buffer
-     * Sub-classes can implement a prepare_output_buffer function as they
-     * wish. */
-    GST_LOG_OBJECT (trans, "doing inplace transform");
-
-    ret = gst_base_transform_prepare_output_buf (trans, inbuf,
-        GST_BUFFER_SIZE (inbuf), GST_PAD_CAPS (trans->srcpad), outbuf);
-    if (G_UNLIKELY (ret != GST_FLOW_OK))
-      goto no_buffer;
+  /* can only do QoS if the segment is in TIME */
+  if (trans->segment.format != GST_FORMAT_TIME)
+    goto no_qos;
+
+  /* QOS is done on the running time of the buffer, get it now */
+  qostime = gst_segment_to_running_time (&trans->segment, GST_FORMAT_TIME,
+      GST_BUFFER_TIMESTAMP (inbuf));
+
+  if (qostime != -1) {
+    gboolean need_skip;
+    GstClockTime earliest_time;
+
+    /* lock for getting the QoS parameters that are set (in a different thread)
+     * with the QOS events */
+    GST_OBJECT_LOCK (trans);
+    earliest_time = trans->priv->earliest_time;
+    /* check for QoS, don't perform conversion for buffers
+     * that are known to be late. */
+    need_skip = trans->priv->qos_enabled &&
+        earliest_time != -1 && qostime != -1 && qostime <= earliest_time;
+    GST_OBJECT_UNLOCK (trans);
+
+    if (need_skip) {
+      GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "skipping transform: qostime %"
+          GST_TIME_FORMAT " <= %" GST_TIME_FORMAT,
+          GST_TIME_ARGS (qostime), GST_TIME_ARGS (earliest_time));
+      /* mark discont for next buffer */
+      trans->priv->discont = TRUE;
+      goto skip;
+    }
+  }
 
-    ret = bclass->transform_ip (trans, *outbuf);
+no_qos:
 
-  } else {
-    GST_LOG_OBJECT (trans, "doing non-inplace transform");
+  /* first try to allocate an output buffer based on the currently negotiated
+   * format. While we call pad-alloc we could renegotiate the srcpad format or
+   * have a new suggestion for upstream buffer-alloc. 
+   * In any case, outbuf will contain a buffer suitable for doing the configured
+   * transform after this function. */
+  ret = gst_base_transform_prepare_output_buffer (trans, inbuf, outbuf);
+  if (G_UNLIKELY (ret != GST_FLOW_OK))
+    goto no_buffer;
 
-    /* not transforming inplace, figure out the output size */
-    if (trans->always_in_place) {
-      out_size = GST_BUFFER_SIZE (inbuf);
+  /* now perform the needed transform */
+  if (trans->passthrough) {
+    /* In passthrough mode, give transform_ip a look at the
+     * buffer, without making it writable, or just push the
+     * data through */
+    if (bclass->transform_ip) {
+      GST_DEBUG_OBJECT (trans, "doing passthrough transform");
+      ret = bclass->transform_ip (trans, *outbuf);
     } else {
-      if (!gst_base_transform_transform_size (trans,
-              GST_PAD_DIRECTION (trans->sinkpad), GST_PAD_CAPS (trans->sinkpad),
-              GST_BUFFER_SIZE (inbuf), GST_PAD_CAPS (trans->srcpad),
-              &out_size)) {
-        /* we have an error */
-        goto no_size;
-      }
+      GST_DEBUG_OBJECT (trans, "element is in passthrough");
     }
+  } else {
+    want_in_place = (bclass->transform_ip != NULL) && trans->always_in_place;
 
-    /* no in place transform, get buffer, this might renegotiate. */
-    ret = gst_base_transform_prepare_output_buf (trans, inbuf, out_size,
-        GST_PAD_CAPS (trans->srcpad), outbuf);
-    if (ret != GST_FLOW_OK)
-      goto no_buffer;
-
-    if (bclass->transform)
-      ret = bclass->transform (trans, inbuf, *outbuf);
-    else
-      ret = GST_FLOW_NOT_SUPPORTED;
-  }
-
-  /* if we got renegotiated we can configure now */
-  if (trans->pending_configure) {
-    gboolean success;
-
-    success =
-        gst_base_transform_configure_caps (trans,
-        GST_PAD_CAPS (trans->sinkpad), GST_PAD_CAPS (trans->srcpad));
+    if (want_in_place) {
+      GST_DEBUG_OBJECT (trans, "doing inplace transform");
 
-    trans->pending_configure = FALSE;
+      if (inbuf != *outbuf) {
+        /* different buffers, copy the input to the output first, we then do an
+         * in-place transform on the output buffer. */
+        memcpy (GST_BUFFER_DATA (*outbuf), GST_BUFFER_DATA (inbuf),
+            GST_BUFFER_SIZE (inbuf));
+      }
+      ret = bclass->transform_ip (trans, *outbuf);
+    } else {
+      GST_DEBUG_OBJECT (trans, "doing non-inplace transform");
 
-    if (!success)
-      goto configure_failed;
+      if (bclass->transform)
+        ret = bclass->transform (trans, inbuf, *outbuf);
+      else
+        ret = GST_FLOW_NOT_SUPPORTED;
+    }
   }
-  gst_buffer_unref (inbuf);
+
+skip:
+  /* only unref input buffer if we allocated a new outbuf buffer */
+  if (*outbuf != inbuf)
+    gst_buffer_unref (inbuf);
 
   return ret;
 
@@ -1237,26 +1743,28 @@ not_negotiated:
         ("not negotiated"), ("not negotiated"));
     return GST_FLOW_NOT_NEGOTIATED;
   }
-no_size:
-  {
-    gst_buffer_unref (inbuf);
-    GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED,
-        ("subclass did not specify output size"),
-        ("subclass did not specify output size"));
-    return GST_FLOW_ERROR;
-  }
 no_buffer:
   {
     gst_buffer_unref (inbuf);
-    GST_DEBUG_OBJECT (trans, "could not get buffer from pool");
+    GST_WARNING_OBJECT (trans, "could not get buffer from pool: %s",
+        gst_flow_get_name (ret));
     return ret;
   }
-configure_failed:
-  {
-    gst_buffer_unref (inbuf);
-    GST_DEBUG_OBJECT (trans, "could not negotiate");
-    return GST_FLOW_NOT_NEGOTIATED;
-  }
+}
+
+static gboolean
+gst_base_transform_check_get_range (GstPad * pad)
+{
+  GstBaseTransform *trans;
+  gboolean ret;
+
+  trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
+
+  ret = gst_pad_check_pull_range (trans->sinkpad);
+
+  gst_object_unref (trans);
+
+  return ret;
 }
 
 /* FIXME, getrange is broken, need to pull range from the other
@@ -1274,9 +1782,7 @@ gst_base_transform_getrange (GstPad * pad, guint64 offset,
 
   ret = gst_pad_pull_range (trans->sinkpad, offset, length, &inbuf);
   if (ret == GST_FLOW_OK) {
-    g_mutex_lock (trans->transform_lock);
     ret = gst_base_transform_handle_buffer (trans, inbuf, buffer);
-    g_mutex_unlock (trans->transform_lock);
   }
 
   gst_object_unref (trans);
@@ -1289,21 +1795,49 @@ gst_base_transform_chain (GstPad * pad, GstBuffer * buffer)
 {
   GstBaseTransform *trans;
   GstFlowReturn ret;
+  GstClockTime last_stop = GST_CLOCK_TIME_NONE;
   GstBuffer *outbuf = NULL;
 
-  trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
+  trans = GST_BASE_TRANSFORM (GST_OBJECT_PARENT (pad));
+
+  /* calculate end position of the incoming buffer */
+  if (GST_BUFFER_TIMESTAMP (buffer) != GST_CLOCK_TIME_NONE) {
+    if (GST_BUFFER_DURATION (buffer) != GST_CLOCK_TIME_NONE)
+      last_stop = GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
+    else
+      last_stop = GST_BUFFER_TIMESTAMP (buffer);
+  }
 
   /* protect transform method and concurrent buffer alloc */
-  g_mutex_lock (trans->transform_lock);
   ret = gst_base_transform_handle_buffer (trans, buffer, &outbuf);
-  g_mutex_unlock (trans->transform_lock);
 
-  if (ret == GST_FLOW_OK) {
-    ret = gst_pad_push (trans->srcpad, outbuf);
-  } else if (outbuf != NULL)
-    gst_buffer_unref (outbuf);
+  /* outbuf can be NULL, this means a dropped buffer, if we have a buffer but
+   * GST_BASE_TRANSFORM_FLOW_DROPPED we will not push either. */
+  if (outbuf != NULL) {
+    if ((ret == GST_FLOW_OK)) {
+      /* Remember last stop position */
+      if ((last_stop != GST_CLOCK_TIME_NONE) &&
+          (trans->segment.format == GST_FORMAT_TIME))
+        gst_segment_set_last_stop (&trans->segment, GST_FORMAT_TIME, last_stop);
+
+      /* apply DISCONT flag if the buffer is not yet marked as such */
+      if (trans->priv->discont) {
+        if (!GST_BUFFER_IS_DISCONT (outbuf)) {
+          outbuf = gst_buffer_make_metadata_writable (outbuf);
+          GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
+        }
+        trans->priv->discont = FALSE;
+      }
+      ret = gst_pad_push (trans->srcpad, outbuf);
+    } else
+      gst_buffer_unref (outbuf);
+  }
 
-  gst_object_unref (trans);
+  /* convert internal flow to OK and mark discont for the next buffer. */
+  if (ret == GST_BASE_TRANSFORM_FLOW_DROPPED) {
+    trans->priv->discont = TRUE;
+    ret = GST_FLOW_OK;
+  }
 
   return ret;
 }
@@ -1317,6 +1851,9 @@ gst_base_transform_set_property (GObject * object, guint prop_id,
   trans = GST_BASE_TRANSFORM (object);
 
   switch (prop_id) {
+    case PROP_QOS:
+      gst_base_transform_set_qos_enabled (trans, g_value_get_boolean (value));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -1332,104 +1869,105 @@ gst_base_transform_get_property (GObject * object, guint prop_id,
   trans = GST_BASE_TRANSFORM (object);
 
   switch (prop_id) {
+    case PROP_QOS:
+      g_value_set_boolean (value, gst_base_transform_is_qos_enabled (trans));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
   }
 }
 
+/* not a vmethod of anything, just an internal method */
 static gboolean
-gst_base_transform_sink_activate_push (GstPad * pad, gboolean active)
+gst_base_transform_activate (GstBaseTransform * trans, gboolean active)
 {
-  gboolean result = TRUE;
-  GstBaseTransform *trans;
   GstBaseTransformClass *bclass;
+  gboolean result = TRUE;
 
-  trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
 
   if (active) {
-    if (bclass->start)
-      result = bclass->start (trans);
+    if (trans->priv->pad_mode == GST_ACTIVATE_NONE && bclass->start)
+      result &= bclass->start (trans);
+
+    GST_OBJECT_LOCK (trans);
+
+    if (GST_PAD_CAPS (trans->sinkpad) && GST_PAD_CAPS (trans->srcpad))
+      trans->have_same_caps =
+          gst_caps_is_equal (GST_PAD_CAPS (trans->sinkpad),
+          GST_PAD_CAPS (trans->srcpad)) || trans->passthrough;
+    else
+      trans->have_same_caps = trans->passthrough;
+    GST_DEBUG_OBJECT (trans, "have_same_caps %d", trans->have_same_caps);
+    trans->negotiated = FALSE;
+    trans->have_newsegment = FALSE;
+    gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
+    trans->priv->proportion = 1.0;
+    trans->priv->earliest_time = -1;
+    trans->priv->discont = FALSE;
+    gst_caps_replace (&trans->priv->sink_suggest, NULL);
+
+    GST_OBJECT_UNLOCK (trans);
+  } else {
+    /* We must make sure streaming has finished before resetting things
+     * and calling the ::stop vfunc */
+    GST_PAD_STREAM_LOCK (trans->sinkpad);
+    GST_PAD_STREAM_UNLOCK (trans->sinkpad);
+
+    trans->have_same_caps = FALSE;
+    /* We can only reset the passthrough mode if the instance told us to 
+       handle it in configure_caps */
+    if (bclass->passthrough_on_same_caps) {
+      gst_base_transform_set_passthrough (trans, FALSE);
+    }
+    gst_caps_replace (&trans->cache_caps1, NULL);
+    gst_caps_replace (&trans->cache_caps2, NULL);
+    gst_caps_replace (&trans->priv->sink_alloc, NULL);
+    gst_caps_replace (&trans->priv->sink_suggest, NULL);
+
+    if (trans->priv->pad_mode != GST_ACTIVATE_NONE && bclass->stop)
+      result &= bclass->stop (trans);
   }
-  gst_object_unref (trans);
 
   return result;
 }
 
 static gboolean
-gst_base_transform_src_activate_pull (GstPad * pad, gboolean active)
+gst_base_transform_sink_activate_push (GstPad * pad, gboolean active)
 {
-  gboolean result = FALSE;
+  gboolean result = TRUE;
   GstBaseTransform *trans;
-  GstBaseTransformClass *bclass;
 
   trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
-  bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
 
-  result = gst_pad_activate_pull (trans->sinkpad, active);
+  result = gst_base_transform_activate (trans, active);
+
+  if (result)
+    trans->priv->pad_mode = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
 
-  if (active) {
-    if (result && bclass->start)
-      result &= bclass->start (trans);
-  }
   gst_object_unref (trans);
 
   return result;
 }
 
-static GstStateChangeReturn
-gst_base_transform_change_state (GstElement * element,
-    GstStateChange transition)
+static gboolean
+gst_base_transform_src_activate_pull (GstPad * pad, gboolean active)
 {
+  gboolean result = FALSE;
   GstBaseTransform *trans;
-  GstBaseTransformClass *bclass;
-  GstStateChangeReturn result;
 
-  trans = GST_BASE_TRANSFORM (element);
-  bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
+  trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
 
+  result = gst_pad_activate_pull (trans->sinkpad, active);
 
-  switch (transition) {
-    case GST_STATE_CHANGE_NULL_TO_READY:
-      break;
-    case GST_STATE_CHANGE_READY_TO_PAUSED:
-      GST_OBJECT_LOCK (trans);
-      if (GST_PAD_CAPS (trans->sinkpad) && GST_PAD_CAPS (trans->srcpad))
-        trans->have_same_caps =
-            gst_caps_is_equal (GST_PAD_CAPS (trans->sinkpad),
-            GST_PAD_CAPS (trans->srcpad)) || trans->passthrough;
-      else
-        trans->have_same_caps = trans->passthrough;
-      GST_DEBUG_OBJECT (trans, "have_same_caps %d", trans->have_same_caps);
-      trans->negotiated = FALSE;
-      trans->have_newsegment = FALSE;
-      gst_segment_init (&trans->segment, GST_FORMAT_UNDEFINED);
-      GST_OBJECT_UNLOCK (trans);
-      break;
-    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
-      break;
-    case GST_STATE_CHANGE_PAUSED_TO_READY:
-      gst_caps_replace (&trans->cache_caps1, NULL);
-      gst_caps_replace (&trans->cache_caps2, NULL);
-    default:
-      break;
-  }
+  if (result)
+    result &= gst_base_transform_activate (trans, active);
 
-  result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
+  if (result)
+    trans->priv->pad_mode = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
 
-  switch (transition) {
-    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
-      break;
-    case GST_STATE_CHANGE_PAUSED_TO_READY:
-      if (bclass->stop)
-        result = bclass->stop (trans);
-      break;
-    case GST_STATE_CHANGE_READY_TO_NULL:
-      break;
-    default:
-      break;
-  }
+  gst_object_unref (trans);
 
   return result;
 }
@@ -1453,7 +1991,7 @@ gst_base_transform_set_passthrough (GstBaseTransform * trans,
 {
   GstBaseTransformClass *bclass;
 
-  g_return_if_fail (trans != NULL);
+  g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
 
   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
 
@@ -1484,7 +2022,7 @@ gst_base_transform_is_passthrough (GstBaseTransform * trans)
 {
   gboolean result;
 
-  g_return_val_if_fail (trans != NULL, FALSE);
+  g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
 
   GST_OBJECT_LOCK (trans);
   result = trans->passthrough;
@@ -1503,7 +2041,7 @@ gst_base_transform_is_passthrough (GstBaseTransform * trans)
  * to the transform_ip function.
  * <itemizedlist>
  *   <listitem>Always TRUE if no transform function is implemented.</listitem>
- *   <listitem>Always FALSE if ONLY transform_ip function is implemented.</listitem>
+ *   <listitem>Always FALSE if ONLY transform function is implemented.</listitem>
  * </itemizedlist>
  *
  * MT safe.
@@ -1513,7 +2051,7 @@ gst_base_transform_set_in_place (GstBaseTransform * trans, gboolean in_place)
 {
   GstBaseTransformClass *bclass;
 
-  g_return_if_fail (trans != NULL);
+  g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
 
   bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
 
@@ -1549,7 +2087,7 @@ gst_base_transform_is_in_place (GstBaseTransform * trans)
 {
   gboolean result;
 
-  g_return_val_if_fail (trans != NULL, FALSE);
+  g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
 
   GST_OBJECT_LOCK (trans);
   result = trans->always_in_place;
@@ -1557,3 +2095,155 @@ gst_base_transform_is_in_place (GstBaseTransform * trans)
 
   return result;
 }
+
+/**
+ * gst_base_transform_update_qos:
+ * @trans: a #GstBaseTransform
+ * @proportion: the proportion
+ * @diff: the diff against the clock
+ * @timestamp: the timestamp of the buffer generating the QoS expressed in
+ * running_time.
+ *
+ * Set the QoS parameters in the transform. This function is called internally
+ * when a QOS event is received but subclasses can provide custom information
+ * when needed.
+ *
+ * Since: 0.10.5
+ *
+ * MT safe.
+ */
+void
+gst_base_transform_update_qos (GstBaseTransform * trans,
+    gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
+{
+
+  g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
+
+  GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans,
+      "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
+      GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
+
+  GST_OBJECT_LOCK (trans);
+  trans->priv->proportion = proportion;
+  trans->priv->earliest_time = timestamp + diff;
+  GST_OBJECT_UNLOCK (trans);
+}
+
+/**
+ * gst_base_transform_set_qos_enabled:
+ * @trans: a #GstBaseTransform
+ * @enabled: new state
+ *
+ * Enable or disable QoS handling in the transform.
+ *
+ * Since: 0.10.5
+ *
+ * MT safe.
+ */
+void
+gst_base_transform_set_qos_enabled (GstBaseTransform * trans, gboolean enabled)
+{
+  g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
+
+  GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, trans, "enabled: %d", enabled);
+
+  GST_OBJECT_LOCK (trans);
+  trans->priv->qos_enabled = enabled;
+  GST_OBJECT_UNLOCK (trans);
+}
+
+/**
+ * gst_base_transform_is_qos_enabled:
+ * @trans: a #GstBaseTransform
+ *
+ * Queries if the transform will handle QoS.
+ *
+ * Returns: TRUE if QoS is enabled.
+ *
+ * Since: 0.10.5
+ *
+ * MT safe.
+ */
+gboolean
+gst_base_transform_is_qos_enabled (GstBaseTransform * trans)
+{
+  gboolean result;
+
+  g_return_val_if_fail (GST_IS_BASE_TRANSFORM (trans), FALSE);
+
+  GST_OBJECT_LOCK (trans);
+  result = trans->priv->qos_enabled;
+  GST_OBJECT_UNLOCK (trans);
+
+  return result;
+}
+
+/**
+ * gst_base_transform_set_gap_aware:
+ * @trans: a #GstBaseTransform
+ * @gap_aware: New state
+ *
+ * If @gap_aware is %FALSE (the default), output buffers will have the
+ * %GST_BUFFER_FLAG_GAP flag unset.
+ *
+ * If set to %TRUE, the element must handle output buffers with this flag set
+ * correctly, i.e. it can assume that the buffer contains neutral data but must
+ * unset the flag if the output is no neutral data.
+ *
+ * Since: 0.10.16
+ *
+ * MT safe.
+ */
+void
+gst_base_transform_set_gap_aware (GstBaseTransform * trans, gboolean gap_aware)
+{
+  g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
+
+  GST_OBJECT_LOCK (trans);
+  trans->priv->gap_aware = gap_aware;
+  GST_DEBUG_OBJECT (trans, "set gap aware %d", trans->priv->gap_aware);
+  GST_OBJECT_UNLOCK (trans);
+}
+
+/**
+ * gst_base_transform_suggest:
+ * @trans: a #GstBaseTransform
+ * @gcaps: caps to suggest
+ *
+ * Instructs @trans to suggest new @caps upstream.
+ *
+ * Since: 0.10.21
+ */
+void
+gst_base_transform_suggest (GstBaseTransform * trans, GstCaps * caps,
+    guint size)
+{
+  g_return_if_fail (GST_IS_BASE_TRANSFORM (trans));
+
+  GST_OBJECT_LOCK (trans->sinkpad);
+  if (trans->priv->sink_suggest)
+    gst_caps_unref (trans->priv->sink_suggest);
+  trans->priv->sink_suggest = gst_caps_copy (caps);
+  trans->priv->size_suggest = size;
+  gst_caps_replace (&trans->priv->sink_alloc, NULL);
+  GST_DEBUG_OBJECT (trans, "new suggest %" GST_PTR_FORMAT, caps);
+  GST_OBJECT_UNLOCK (trans->sinkpad);
+}
+
+#if 0
+/**
+ * gst_base_transform_reconfigure:
+ * @trans: a #GstBaseTransform
+ *
+ * Instructs @trans to renegotiate a new downstream transform on the next
+ * buffer.
+ *
+ * Note: Not yet implemented.
+ *
+ * Since: 0.10.21
+ */
+void
+gst_base_transform_reconfigure (GstBaseTransform * trans)
+{
+}
+#endif