diff -Naurp -x .svn -x inspect -x gst-plugins-bad-plugins.args -x gst-plugins-bad-plugins.hierarchy gst-plugins-bad-prepare/docs/plugins/gst-plugins-bad-plugins-docs.sgml gst-plugins-bad/docs/plugins/gst-plugins-bad-plugins-docs.sgml
--- gst-plugins-bad-prepare/docs/plugins/gst-plugins-bad-plugins-docs.sgml	2007-02-23 21:17:00.000000000 +0100
+++ gst-plugins-bad/docs/plugins/gst-plugins-bad-plugins-docs.sgml	2007-01-31 22:58:49.000000000 +0100
@@ -17,6 +17,8 @@
     <xi:include href="xml/element-jackaudiosink.xml" />
     <xi:include href="xml/element-qtdemux.xml" />
     <xi:include href="xml/element-rganalysis.xml" />
+    <xi:include href="xml/element-rglimiter.xml" />
+    <xi:include href="xml/element-rgvolume.xml" />
     <xi:include href="xml/element-sdlaudiosink.xml" />
     <xi:include href="xml/element-sdlvideosink.xml" />
     <xi:include href="xml/element-trm.xml" />
diff -Naurp -x .svn -x inspect -x gst-plugins-bad-plugins.args -x gst-plugins-bad-plugins.hierarchy gst-plugins-bad-prepare/docs/plugins/gst-plugins-bad-plugins-sections.txt gst-plugins-bad/docs/plugins/gst-plugins-bad-plugins-sections.txt
--- gst-plugins-bad-prepare/docs/plugins/gst-plugins-bad-plugins-sections.txt	2007-02-23 21:17:00.000000000 +0100
+++ gst-plugins-bad/docs/plugins/gst-plugins-bad-plugins-sections.txt	2007-01-31 22:58:49.000000000 +0100
@@ -32,6 +32,22 @@ GstRgAnalysisClass
 </SECTION>
 
 <SECTION>
+<FILE>element-rglimiter</FILE>
+GstRgLimiter
+<TITLE>rglimiter</TITLE>
+<SUBSECTION Standard>
+GstRgLimiterClass
+</SECTION>
+
+<SECTION>
+<FILE>element-rgvolume</FILE>
+GstRgVolume
+<TITLE>rgvolume</TITLE>
+<SUBSECTION Standard>
+GstRgVolumeClass
+</SECTION>
+
+<SECTION>
 <FILE>element-sdlaudiosink</FILE>
 GstSDLAudioSink
 <TITLE>sdlaudiosink</TITLE>
diff -Naurp -x .svn -x inspect -x gst-plugins-bad-plugins.args -x gst-plugins-bad-plugins.hierarchy gst-plugins-bad-prepare/docs/plugins/Makefile.am gst-plugins-bad/docs/plugins/Makefile.am
--- gst-plugins-bad-prepare/docs/plugins/Makefile.am	2007-02-26 00:54:23.000000000 +0100
+++ gst-plugins-bad/docs/plugins/Makefile.am	2007-02-26 00:34:58.000000000 +0100
@@ -99,6 +99,8 @@ EXTRA_HFILES = \
 	$(top_srcdir)/ext/wavpack/gstwavpackparse.h \
 	$(top_srcdir)/gst/qtdemux/qtdemux.h \
 	$(top_srcdir)/gst/replaygain/gstrganalysis.h \
+	$(top_srcdir)/gst/replaygain/gstrglimiter.h \
+	$(top_srcdir)/gst/replaygain/gstrgvolume.h \
 	$(top_srcdir)/gst/videocrop/gstvideocrop.h
 
 # Images to copy into HTML directory.
diff -Naurp -x .svn -x inspect -x gst-plugins-bad-plugins.args -x gst-plugins-bad-plugins.hierarchy gst-plugins-bad-prepare/gst/replaygain/gstrganalysis.c gst-plugins-bad/gst/replaygain/gstrganalysis.c
--- gst-plugins-bad-prepare/gst/replaygain/gstrganalysis.c	2007-02-27 15:19:12.000000000 +0100
+++ gst-plugins-bad/gst/replaygain/gstrganalysis.c	2007-02-27 15:19:00.000000000 +0100
@@ -22,6 +22,7 @@
 
 /**
  * SECTION:element-rganalysis
+ * @see_also: <link linkend="GstRgVolume">rgvolume</link>
  *
  * <refsect2>
  * <para>
diff -Naurp -x .svn -x inspect -x gst-plugins-bad-plugins.args -x gst-plugins-bad-plugins.hierarchy gst-plugins-bad-prepare/gst/replaygain/gstrglimiter.c gst-plugins-bad/gst/replaygain/gstrglimiter.c
--- gst-plugins-bad-prepare/gst/replaygain/gstrglimiter.c	1970-01-01 01:00:00.000000000 +0100
+++ gst-plugins-bad/gst/replaygain/gstrglimiter.c	2007-02-26 16:38:33.000000000 +0100
@@ -0,0 +1,197 @@
+/* GStreamer ReplayGain limiter
+ *
+ * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
+ * 
+ * gstrglimiter.c: Element to apply signal compression to raw audio data
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+/**
+ * SECTION:element-rglimiter
+ * @see_also: <link linkend="GstRgVolume">rgvolume</link>
+ *
+ * <refsect2>
+ * <para>
+ * This element applies signal compression/limiting to raw audio data.  It
+ * performs strict hard limiting with soft-knee characteristics, using a
+ * threshold of -6 dB.  This type of filter is mentioned in the proposed <ulink
+ * url="http://replaygain.org">ReplayGain standard</ulink>.
+ * </para>
+ * <title>Example launch line</title>
+ * <para>Playback of a file:</para>
+ * <programlisting>
+ * gst-launch filesrc location="Filename.ext" ! decodebin ! audioconvert \
+ *            ! rgvolume pre-amp=6.0 headroom=10.0 ! rglimiter \
+ *            ! audioconvert ! audioresample ! alsasink
+ * </programlisting>
+ * </refsect2>
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <gst/gst.h>
+#include <math.h>
+
+#include "gstrglimiter.h"
+
+GST_DEBUG_CATEGORY_STATIC (gst_rg_limiter_debug);
+#define GST_CAT_DEFAULT gst_rg_limiter_debug
+
+enum
+{
+  PROP_0,
+  PROP_ENABLED,
+};
+
+static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-float, "
+        "width = (int) 32, channels = (int) [1, MAX], "
+        "rate = (int) [1, MAX], endianness = (int) BYTE_ORDER"));
+
+static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
+    GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-float, "
+        "width = (int) 32, channels = (int) [1, MAX], "
+        "rate = (int) [1, MAX], endianness = (int) BYTE_ORDER"));
+
+GST_BOILERPLATE (GstRgLimiter, gst_rg_limiter, GstBaseTransform,
+    GST_TYPE_BASE_TRANSFORM);
+
+static void gst_rg_limiter_class_init (GstRgLimiterClass * klass);
+static void gst_rg_limiter_init (GstRgLimiter * filter,
+    GstRgLimiterClass * gclass);
+
+static void gst_rg_limiter_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec);
+static void gst_rg_limiter_get_property (GObject * object, guint prop_id,
+    GValue * value, GParamSpec * pspec);
+
+static GstFlowReturn gst_rg_limiter_transform_ip (GstBaseTransform * base,
+    GstBuffer * buf);
+
+static const GstElementDetails element_details = {
+  "ReplayGain limiter",
+  "Filter/Effect/Audio",
+  "Apply signal compression to raw audio data",
+  "Ren\xc3\xa9 Stadler <mail@renestadler.de>"
+};
+
+static void
+gst_rg_limiter_base_init (gpointer g_class)
+{
+  GstElementClass *element_class = g_class;
+
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&src_factory));
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&sink_factory));
+  gst_element_class_set_details (element_class, &element_details);
+
+  GST_DEBUG_CATEGORY_INIT (gst_rg_limiter_debug, "rglimiter", 0,
+      "ReplayGain limiter element");
+}
+
+static void
+gst_rg_limiter_class_init (GstRgLimiterClass * klass)
+{
+  GObjectClass *gobject_class;
+  GstBaseTransformClass *trans_class;
+
+  gobject_class = (GObjectClass *) klass;
+
+  gobject_class->set_property = gst_rg_limiter_set_property;
+  gobject_class->get_property = gst_rg_limiter_get_property;
+
+  g_object_class_install_property (gobject_class, PROP_ENABLED,
+      g_param_spec_boolean ("enabled", "Enabled", "Enable processing", TRUE,
+          G_PARAM_READWRITE));
+
+  trans_class = GST_BASE_TRANSFORM_CLASS (klass);
+  trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_rg_limiter_transform_ip);
+  trans_class->passthrough_on_same_caps = FALSE;
+}
+
+static void
+gst_rg_limiter_init (GstRgLimiter * filter, GstRgLimiterClass * gclass)
+{
+  filter->enabled = TRUE;
+  gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter), FALSE);
+}
+
+static void
+gst_rg_limiter_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec)
+{
+  GstRgLimiter *filter = GST_RG_LIMITER (object);
+
+  switch (prop_id) {
+    case PROP_ENABLED:
+      filter->enabled = g_value_get_boolean (value);
+      gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter),
+          !filter->enabled);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+static void
+gst_rg_limiter_get_property (GObject * object, guint prop_id,
+    GValue * value, GParamSpec * pspec)
+{
+  GstRgLimiter *filter = GST_RG_LIMITER (object);
+
+  switch (prop_id) {
+    case PROP_ENABLED:
+      g_value_set_boolean (value, filter->enabled);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+#define LIMIT 1.0
+#define THRES 0.5               /* ca. -6 dB */
+#define COMPL 0.5               /* LIMIT - THRESH */
+
+static GstFlowReturn
+gst_rg_limiter_transform_ip (GstBaseTransform * base, GstBuffer * buf)
+{
+  GstRgLimiter *filter = GST_RG_LIMITER (base);
+  gfloat *input;
+  guint count;
+  guint i;
+
+  if (!filter->enabled)
+    return GST_FLOW_OK;
+
+  input = (gfloat *) GST_BUFFER_DATA (buf);
+  count = GST_BUFFER_SIZE (buf) / sizeof (gfloat);
+
+  for (i = count; i--;) {
+    if (*input > THRES)
+      *input = tanhf ((*input - THRES) / COMPL) * COMPL + THRES;
+    else if (*input < -THRES)
+      *input = tanhf ((*input + THRES) / COMPL) * COMPL - THRES;
+    input++;
+  }
+
+  return GST_FLOW_OK;
+}
diff -Naurp -x .svn -x inspect -x gst-plugins-bad-plugins.args -x gst-plugins-bad-plugins.hierarchy gst-plugins-bad-prepare/gst/replaygain/gstrglimiter.h gst-plugins-bad/gst/replaygain/gstrglimiter.h
--- gst-plugins-bad-prepare/gst/replaygain/gstrglimiter.h	1970-01-01 01:00:00.000000000 +0100
+++ gst-plugins-bad/gst/replaygain/gstrglimiter.h	2007-01-31 21:35:52.000000000 +0100
@@ -0,0 +1,62 @@
+/* GStreamer ReplayGain limiter
+ *
+ * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
+ *
+ * gstrglimiter.h: Element to apply signal compression to raw audio data
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#ifndef __GST_RG_LIMITER_H__
+#define __GST_RG_LIMITER_H__
+
+#include <gst/gst.h>
+#include <gst/base/gstbasetransform.h>
+
+#define GST_TYPE_RG_LIMITER \
+  (gst_rg_limiter_get_type())
+#define GST_RG_LIMITER(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RG_LIMITER,GstRgLimiter))
+#define GST_RG_LIMITER_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RG_LIMITER,GstRgLimiterClass))
+#define GST_IS_RG_LIMITER(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RG_LIMITER))
+#define GST_IS_RG_LIMITER_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RG_LIMITER))
+
+typedef struct _GstRgLimiter GstRgLimiter;
+typedef struct _GstRgLimiterClass GstRgLimiterClass;
+
+/**
+ * GstRgLimiter:
+ *
+ * Opaque data structure.
+ */
+struct _GstRgLimiter
+{
+  GstBaseTransform element;
+
+  gboolean enabled;
+};
+
+struct _GstRgLimiterClass
+{
+  GstBaseTransformClass parent_class;
+};
+
+GType gst_rg_limiter_get_type (void);
+
+#endif /* __GST_RG_LIMITER_H__ */
diff -Naurp -x .svn -x inspect -x gst-plugins-bad-plugins.args -x gst-plugins-bad-plugins.hierarchy gst-plugins-bad-prepare/gst/replaygain/gstrgvolume.c gst-plugins-bad/gst/replaygain/gstrgvolume.c
--- gst-plugins-bad-prepare/gst/replaygain/gstrgvolume.c	1970-01-01 01:00:00.000000000 +0100
+++ gst-plugins-bad/gst/replaygain/gstrgvolume.c	2007-02-27 17:06:41.000000000 +0100
@@ -0,0 +1,702 @@
+/* GStreamer ReplayGain volume adjustment
+ *
+ * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
+ * 
+ * gstrgvolume.c: Element to apply ReplayGain volume adjustment
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+/**
+ * SECTION:element-rgvolume
+ * @see_also: <link linkend="GstRgLimiter">rglimiter</link>,
+ *            <link linkend="GstRgAnalysis">rganalysis</link>
+ *
+ * <refsect2>
+ * <para>
+ * This element applies volume changes to streams as lined out in the proposed
+ * <ulink url="http://replaygain.org">ReplayGain standard</ulink>.  It
+ * interprets the ReplayGain meta data tags and carries out the adjustment (by
+ * using a volume element internally).  The relevant tags are:
+ * <itemizedlist>
+ * <listitem>#GST_TAG_TRACK_GAIN</listitem>
+ * <listitem>#GST_TAG_TRACK_PEAK</listitem>
+ * <listitem>#GST_TAG_ALBUM_GAIN</listitem>
+ * <listitem>#GST_TAG_ALBUM_PEAK</listitem>
+ * <listitem>#GST_TAG_REFERENCE_LEVEL</listitem>
+ * </itemizedlist>
+ * The information carried by these tags must have been calculated beforehand by
+ * performing the ReplayGain analysis.  This is implemented by the <link
+ * linkend="GstRgAnalysis">rganalysis</link> element.
+ * </para>
+ * <para>
+ * The signal compression/limiting recommendations outlined in the proposed
+ * standard are not implemented by this element.  This has to be handled by
+ * separate elements because applications might want to have additional filters
+ * between the volume adjustment and the limiting stage.  A basic limiter is
+ * included with this plugin: The <link linkend="GstRgLimiter">rglimiter</link>
+ * element applies -6 dB hard limiting as mentioned in the ReplayGain standard.
+ * </para>
+ * <title>Example launch line</title>
+ * <para>Playback of a file:</para>
+ * <programlisting>
+ * gst-launch filesrc location="Filename.ext" ! decodebin ! audioconvert \
+ *     ! rgvolume ! audioconvert ! audioresample ! alsasink
+ * </programlisting>
+ * </refsect2>
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <gst/gst.h>
+#include <gst/utils/base-utils.h>
+#include <math.h>
+
+#include "gstrgvolume.h"
+#include "replaygain.h"
+
+GST_DEBUG_CATEGORY_STATIC (gst_rg_volume_debug);
+#define GST_CAT_DEFAULT gst_rg_volume_debug
+
+enum
+{
+  PROP_0,
+  PROP_ALBUM_MODE,
+  PROP_HEADROOM,
+  PROP_PRE_AMP,
+  PROP_FALLBACK_GAIN,
+  PROP_TARGET_GAIN,
+  PROP_RESULT_GAIN
+};
+
+#define DEFAULT_ALBUM_MODE TRUE
+#define DEFAULT_HEADROOM 0.0
+#define DEFAULT_PRE_AMP 0.0
+#define DEFAULT_FALLBACK_GAIN 0.0
+
+#define DB_TO_LINEAR(x) pow (10., (x) / 20.)
+#define LINEAR_TO_DB(x) (20. * log10 (x))
+
+#define GAIN_FORMAT "+.02f dB"
+#define PEAK_FORMAT ".06f"
+
+#define VALID_GAIN(x) ((x) > -60.00 && (x) < 60.00)
+#define VALID_PEAK(x) ((x) > 0.)
+
+/* Same template caps as GstVolume, for I don't like having just ANY caps. */
+
+static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-float, "
+        "rate = (int) [ 1, MAX ], "
+        "channels = (int) [ 1, MAX ], "
+        "endianness = (int) BYTE_ORDER, "
+        "width = (int) 32; "
+        "audio/x-raw-int, "
+        "channels = (int) [ 1, MAX ], "
+        "rate = (int) [ 1,  MAX ], "
+        "endianness = (int) BYTE_ORDER, "
+        "width = (int) 16, " "depth = (int) 16, " "signed = (bool) TRUE"));
+
+static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
+    GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-float, "
+        "rate = (int) [ 1, MAX ], "
+        "channels = (int) [ 1, MAX ], "
+        "endianness = (int) BYTE_ORDER, "
+        "width = (int) 32; "
+        "audio/x-raw-int, "
+        "channels = (int) [ 1, MAX ], "
+        "rate = (int) [ 1,  MAX ], "
+        "endianness = (int) BYTE_ORDER, "
+        "width = (int) 16, " "depth = (int) 16, " "signed = (bool) TRUE"));
+
+GST_BOILERPLATE (GstRgVolume, gst_rg_volume, GstBin, GST_TYPE_BIN);
+
+static void gst_rg_volume_class_init (GstRgVolumeClass * klass);
+static void gst_rg_volume_init (GstRgVolume * self, GstRgVolumeClass * gclass);
+
+static void gst_rg_volume_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec);
+static void gst_rg_volume_get_property (GObject * object, guint prop_id,
+    GValue * value, GParamSpec * pspec);
+static void gst_rg_volume_dispose (GObject * object);
+
+static GstStateChangeReturn gst_rg_volume_change_state (GstElement * element,
+    GstStateChange transition);
+static gboolean gst_rg_volume_sink_event (GstPad * pad, GstEvent * event);
+
+static GstEvent *gst_rg_volume_tag_event (GstRgVolume * self, GstEvent * event);
+static void gst_rg_volume_reset (GstRgVolume * self);
+static void gst_rg_volume_update_gain (GstRgVolume * self);
+static inline void gst_rg_volume_determine_gain (GstRgVolume * self,
+    gdouble * target_gain, gdouble * result_gain);
+
+static void
+gst_rg_volume_base_init (gpointer g_class)
+{
+  GstElementClass *element_class = g_class;
+
+  static const GstElementDetails element_details = {
+    "ReplayGain volume",
+    "Filter/Effect/Audio",
+    "Apply ReplayGain volume adjustment",
+    "Ren\xc3\xa9 Stadler <mail@renestadler.de>"
+  };
+
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&src_template));
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&sink_template));
+  gst_element_class_set_details (element_class, &element_details);
+
+  GST_DEBUG_CATEGORY_INIT (gst_rg_volume_debug, "rgvolume", 0,
+      "ReplayGain volume element");
+}
+
+static void
+gst_rg_volume_class_init (GstRgVolumeClass * klass)
+{
+  GObjectClass *gobject_class;
+  GstElementClass *element_class;
+  GstBinClass *bin_class;
+
+  gobject_class = (GObjectClass *) klass;
+
+  gobject_class->set_property = gst_rg_volume_set_property;
+  gobject_class->get_property = gst_rg_volume_get_property;
+  gobject_class->dispose = gst_rg_volume_dispose;
+
+  /**
+   * GstRgVolume:album-mode:
+   *
+   * Whether to prefer album gain over track gain.
+   *
+   * If set to %TRUE, use album gain instead of track gain if both are
+   * available.  This keeps the relative loudness levels of tracks from the same
+   * album intact.
+   *
+   * If set to %FALSE, track mode is used instead.  This effectively leads to
+   * more extensive normalization.
+   *
+   * If album mode is enabled but the album gain tag is absent in the stream,
+   * the track gain is used instead.  If both gain tags are missing, the value
+   * of the <link linkend="GstRgVolume--fallback-gain">fallback-gain</link>
+   * property is used instead.
+   */
+  g_object_class_install_property (gobject_class, PROP_ALBUM_MODE,
+      g_param_spec_boolean ("album-mode", "Album mode",
+          "Prefer album over track gain", DEFAULT_ALBUM_MODE,
+          G_PARAM_READWRITE));
+  /**
+   * GstRgVolume:headroom:
+   *
+   * Extra headroom [dB].  This controls the amount by which the output can
+   * exceed digital full scale.
+   *
+   * Only set this to a value greater than 0.0 if signal compression/limiting of
+   * a suitable form is applied to the output (or output is brought into the
+   * correct range by some other transformation).
+   *
+   * This element internally uses a volume element, which also supports
+   * operating on integer audio formats.  These formats do not allow exceeding
+   * digital full scale.  If extra headroom is used, make sure that the raw
+   * audio data format is floating point (audio/x-raw-float).  Otherwise,
+   * clipping distortion might be introduced as part of the volume adjustment
+   * itself.
+   */
+  g_object_class_install_property (gobject_class, PROP_HEADROOM,
+      g_param_spec_double ("headroom", "Headroom", "Extra headroom [dB]",
+          0., 60., DEFAULT_HEADROOM, G_PARAM_READWRITE));
+  /**
+   * GstRgVolume:pre-amp:
+   *
+   * Additional gain to apply globally [dB].  This controls the trade-off
+   * between uniformity of normalization and utilization of available dynamic
+   * range.
+   *
+   * Note that the default value is 0 dB because the ReplayGain reference value
+   * was adjusted by +6 dB (from 83 to 89 dB).  At the time of this writing, the
+   * <ulink url="http://replaygain.org">webpage</ulink> is still outdated and
+   * does not reflect this change however.  Where the original proposal states
+   * that a proper default pre-amp value is +6 dB, this translates to the used 0
+   * dB.
+   */
+  g_object_class_install_property (gobject_class, PROP_PRE_AMP,
+      g_param_spec_double ("pre-amp", "Pre-amp", "Extra gain [dB]",
+          -60., 60., DEFAULT_PRE_AMP, G_PARAM_READWRITE));
+  /**
+   * GstRgVolume:fallback-gain:
+   *
+   * Fallback gain [dB] for streams missing ReplayGain tags.
+   */
+  g_object_class_install_property (gobject_class, PROP_FALLBACK_GAIN,
+      g_param_spec_double ("fallback-gain", "Fallback gain",
+          "Gain for streams missing tags [dB]",
+          -60., 60., DEFAULT_FALLBACK_GAIN, G_PARAM_READWRITE));
+  /**
+   * GstRgVolume:result-gain:
+   *
+   * Applied gain [dB].  This gain is applied to processed buffer data.
+   *
+   * This is set to the <link linkend="GstRgVolume--target-gain">target
+   * gain</link> if amplification by that amount can be applied safely.
+   * "Safely" means that the volume adjustment does not inflict clipping
+   * distortion.  Should this not be the case, the result gain is set to an
+   * appropriately reduced value (by applying peak normalization).  The proposed
+   * standard calls this "clipping prevention".
+   *
+   * The difference between target and result gain reflects the necessary amount
+   * of reduction.  Applications can make use of this information to temporarily
+   * reduce the <link linkend="GstRgVolume--pre-amp">pre-amp</link> for
+   * subsequent streams, as recommended by the ReplayGain standard.
+   *
+   * Note that target and result gain differing for a great majority of streams
+   * indicates a problem: What happens in this case is that most streams receive
+   * peak normalization instead of amplification by the ideal replay gain.  To
+   * prevent this, the <link linkend="GstRgVolume--pre-amp">pre-amp</link> has
+   * to be lowered and/or a limiter has to be used which facilitates the use of
+   * <link linkend="GstRgVolume--headroom">headroom</link>.
+   */
+  g_object_class_install_property (gobject_class, PROP_RESULT_GAIN,
+      g_param_spec_double ("result-gain", "Result-gain", "Applied gain [dB]",
+          -120., 120., 0., G_PARAM_READABLE));
+  /**
+   * GstRgVolume:target-gain:
+   *
+   * Applicable gain [dB].  This gain is supposed to be applied.
+   *
+   * Depending on the value of the <link
+   * linkend="GstRgVolume--album-mode">album-mode</link> property and the
+   * presence of ReplayGain tags in the stream, this is set according to one of
+   * these simple formulas:
+   *
+   * <itemizedlist>
+   * <listitem><link linkend="GstRgVolume--pre-amp">pre-amp</link> + album gain
+   * of the stream</listitem>
+   * <listitem><link linkend="GstRgVolume--pre-amp">pre-amp</link> + track gain
+   * of the stream</listitem>
+   * <listitem><link linkend="GstRgVolume--pre-amp">pre-amp</link> + <link
+   * linkend="GstRgVolume--fallback-gain">fallback gain</link></listitem>
+   * </itemizedlist>
+   */
+  g_object_class_install_property (gobject_class, PROP_TARGET_GAIN,
+      g_param_spec_double ("target-gain", "Target-gain",
+          "Applicable gain [dB]", -120., 120., 0., G_PARAM_READABLE));
+
+  element_class = (GstElementClass *) klass;
+  element_class->change_state = GST_DEBUG_FUNCPTR (gst_rg_volume_change_state);
+
+  bin_class = (GstBinClass *) klass;
+  /* Setting these to NULL makes gst_bin_add and _remove refuse to let anyone
+   * mess with our internals. */
+  bin_class->add_element = NULL;
+  bin_class->remove_element = NULL;
+}
+
+static void
+gst_rg_volume_init (GstRgVolume * self, GstRgVolumeClass * gclass)
+{
+  GObjectClass *volume_class;
+  GstPad *volume_pad, *ghost_pad;
+
+  self->album_mode = DEFAULT_ALBUM_MODE;
+  self->headroom = DEFAULT_HEADROOM;
+  self->pre_amp = DEFAULT_PRE_AMP;
+  self->fallback_gain = DEFAULT_FALLBACK_GAIN;
+  self->target_gain = 0.0;
+  self->result_gain = 0.0;
+
+  self->volume_element = gst_element_factory_make ("volume", "rgvolume-volume");
+  if (G_UNLIKELY (self->volume_element == NULL)) {
+    GstMessage *msg;
+
+    GST_WARNING_OBJECT (self, "could not create volume element");
+    msg = gst_missing_element_message_new (GST_ELEMENT_CAST (self), "volume");
+    gst_element_post_message (GST_ELEMENT_CAST (self), msg);
+
+    /* Nothing else to do, we will refuse the state change from NULL to READY to
+     * indicate that something went very wrong.  It is doubtful that someone
+     * attempts changing our state though, since we end up having no pads! */
+    return;
+  }
+
+  volume_class = G_OBJECT_GET_CLASS (G_OBJECT (self->volume_element));
+  self->max_volume = G_PARAM_SPEC_DOUBLE
+      (g_object_class_find_property (volume_class, "volume"))->maximum;
+
+  GST_BIN_CLASS (parent_class)->add_element (GST_BIN_CAST (self),
+      self->volume_element);
+
+  volume_pad = gst_element_get_pad (self->volume_element, "sink");
+  ghost_pad = gst_ghost_pad_new_from_template ("sink", volume_pad,
+      gst_pad_get_pad_template (volume_pad));
+  gst_object_unref (volume_pad);
+  gst_pad_set_event_function (ghost_pad, gst_rg_volume_sink_event);
+  gst_element_add_pad (GST_ELEMENT_CAST (self), ghost_pad);
+
+  volume_pad = gst_element_get_pad (self->volume_element, "src");
+  ghost_pad = gst_ghost_pad_new_from_template ("src", volume_pad,
+      gst_pad_get_pad_template (volume_pad));
+  gst_object_unref (volume_pad);
+  gst_element_add_pad (GST_ELEMENT_CAST (self), ghost_pad);
+}
+
+static void
+gst_rg_volume_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec)
+{
+  GstRgVolume *self = GST_RG_VOLUME (object);
+
+  switch (prop_id) {
+    case PROP_ALBUM_MODE:
+      self->album_mode = g_value_get_boolean (value);
+      break;
+    case PROP_HEADROOM:
+      self->headroom = g_value_get_double (value);
+      break;
+    case PROP_PRE_AMP:
+      self->pre_amp = g_value_get_double (value);
+      break;
+    case PROP_FALLBACK_GAIN:
+      self->fallback_gain = g_value_get_double (value);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+
+  gst_rg_volume_update_gain (self);
+}
+
+static void
+gst_rg_volume_get_property (GObject * object, guint prop_id,
+    GValue * value, GParamSpec * pspec)
+{
+  GstRgVolume *self = GST_RG_VOLUME (object);
+
+  switch (prop_id) {
+    case PROP_ALBUM_MODE:
+      g_value_set_boolean (value, self->album_mode);
+      break;
+    case PROP_HEADROOM:
+      g_value_set_double (value, self->headroom);
+      break;
+    case PROP_PRE_AMP:
+      g_value_set_double (value, self->pre_amp);
+      break;
+    case PROP_FALLBACK_GAIN:
+      g_value_set_double (value, self->fallback_gain);
+      break;
+    case PROP_TARGET_GAIN:
+      g_value_set_double (value, self->target_gain);
+      break;
+    case PROP_RESULT_GAIN:
+      g_value_set_double (value, self->result_gain);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+static void
+gst_rg_volume_dispose (GObject * object)
+{
+  GstRgVolume *self = GST_RG_VOLUME (object);
+
+  if (self->volume_element != NULL) {
+    /* Manually remove our child using the bin implementation of remove_element.
+     * This is needed because we prevent gst_bin_remove from working, which the
+     * parent dispose handler would use if we had any children left. */
+    GST_BIN_CLASS (parent_class)->remove_element (GST_BIN_CAST (self),
+        self->volume_element);
+    self->volume_element = NULL;
+  }
+
+  G_OBJECT_CLASS (parent_class)->dispose (object);
+}
+
+static GstStateChangeReturn
+gst_rg_volume_change_state (GstElement * element, GstStateChange transition)
+{
+  GstRgVolume *self = GST_RG_VOLUME (element);
+  GstStateChangeReturn res;
+
+  switch (transition) {
+    case GST_STATE_CHANGE_NULL_TO_READY:
+
+      if (G_UNLIKELY (self->volume_element == NULL)) {
+        /* Creating our child volume element in _init failed. */
+        return GST_STATE_CHANGE_FAILURE;
+      }
+      break;
+
+    case GST_STATE_CHANGE_READY_TO_PAUSED:
+
+      gst_rg_volume_reset (self);
+      break;
+
+    default:
+      break;
+  }
+
+  res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
+
+  return res;
+}
+
+/* Event function for the ghost sink pad. */
+static gboolean
+gst_rg_volume_sink_event (GstPad * pad, GstEvent * event)
+{
+  GstRgVolume *self;
+  GstPad *volume_sink_pad;
+  GstEvent *send_event = event;
+  gboolean res;
+
+  self = GST_RG_VOLUME (gst_pad_get_parent_element (pad));
+  volume_sink_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
+
+  switch (GST_EVENT_TYPE (event)) {
+    case GST_EVENT_TAG:
+
+      GST_LOG_OBJECT (self, "received tag event");
+
+      send_event = gst_rg_volume_tag_event (self, event);
+
+      if (send_event == NULL)
+        GST_LOG_OBJECT (self, "all tags handled, dropping event");
+
+      break;
+
+    case GST_EVENT_EOS:
+
+      gst_rg_volume_reset (self);
+      break;
+
+    default:
+      break;
+  }
+
+  if (G_LIKELY (send_event != NULL))
+    res = gst_pad_send_event (volume_sink_pad, send_event);
+  else
+    res = TRUE;
+
+  gst_object_unref (volume_sink_pad);
+  gst_object_unref (self);
+  return res;
+}
+
+static GstEvent *
+gst_rg_volume_tag_event (GstRgVolume * self, GstEvent * event)
+{
+  GstTagList *tag_list;
+  gboolean has_track_gain, has_track_peak, has_album_gain, has_album_peak;
+  gboolean has_ref_level;
+
+  g_return_val_if_fail (event != NULL, NULL);
+  g_return_val_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_TAG, event);
+
+  gst_event_parse_tag (event, &tag_list);
+
+  if (gst_tag_list_is_empty (tag_list))
+    return event;
+
+  has_track_gain = gst_tag_list_get_double (tag_list, GST_TAG_TRACK_GAIN,
+      &self->track_gain);
+  has_track_peak = gst_tag_list_get_double (tag_list, GST_TAG_TRACK_PEAK,
+      &self->track_peak);
+  has_album_gain = gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_GAIN,
+      &self->album_gain);
+  has_album_peak = gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_PEAK,
+      &self->album_peak);
+  has_ref_level = gst_tag_list_get_double (tag_list, GST_TAG_REFERENCE_LEVEL,
+      &self->reference_level);
+
+  if (!has_track_gain && !has_track_peak && !has_album_gain && !has_album_peak)
+    return event;
+
+  if (has_ref_level && (has_track_gain || has_album_gain)
+      && (ABS (self->reference_level - RG_REFERENCE_LEVEL) > 1.e-6)) {
+    /* Log a message stating the amount of adjustment that is applied below. */
+    GST_DEBUG_OBJECT (self,
+        "compensating for reference level difference by %" GAIN_FORMAT,
+        RG_REFERENCE_LEVEL - self->reference_level);
+  }
+  if (has_track_gain) {
+    self->track_gain += RG_REFERENCE_LEVEL - self->reference_level;
+  }
+  if (has_album_gain) {
+    self->album_gain += RG_REFERENCE_LEVEL - self->reference_level;
+  }
+
+  /* Ignore values that are obviously invalid. */
+  if (G_UNLIKELY (has_track_gain && !VALID_GAIN (self->track_gain))) {
+    GST_DEBUG_OBJECT (self,
+        "ignoring bogus track gain value %" GAIN_FORMAT, self->track_gain);
+    has_track_gain = FALSE;
+  }
+  if (G_UNLIKELY (has_track_peak && !VALID_PEAK (self->track_peak))) {
+    GST_DEBUG_OBJECT (self,
+        "ignoring bogus track peak value %" PEAK_FORMAT, self->track_peak);
+    has_track_peak = FALSE;
+  }
+  if (G_UNLIKELY (has_album_gain && !VALID_GAIN (self->album_gain))) {
+    GST_DEBUG_OBJECT (self,
+        "ignoring bogus album gain value %" GAIN_FORMAT, self->album_gain);
+    has_album_gain = FALSE;
+  }
+  if (G_UNLIKELY (has_album_peak && !VALID_PEAK (self->album_peak))) {
+    GST_DEBUG_OBJECT (self,
+        "ignoring bogus album peak value %" PEAK_FORMAT, self->album_peak);
+    has_album_peak = FALSE;
+  }
+
+  self->has_track_gain |= has_track_gain;
+  self->has_track_peak |= has_track_peak;
+  self->has_album_gain |= has_album_gain;
+  self->has_album_peak |= has_album_peak;
+
+  event = (GstEvent *) gst_mini_object_make_writable (GST_MINI_OBJECT (event));
+  gst_event_parse_tag (event, &tag_list);
+
+  gst_tag_list_remove_tag (tag_list, GST_TAG_TRACK_GAIN);
+  gst_tag_list_remove_tag (tag_list, GST_TAG_TRACK_PEAK);
+  gst_tag_list_remove_tag (tag_list, GST_TAG_ALBUM_GAIN);
+  gst_tag_list_remove_tag (tag_list, GST_TAG_ALBUM_PEAK);
+  gst_tag_list_remove_tag (tag_list, GST_TAG_REFERENCE_LEVEL);
+
+  gst_rg_volume_update_gain (self);
+
+  if (gst_tag_list_is_empty (tag_list)) {
+    gst_event_unref (event);
+    event = NULL;
+  }
+
+  return event;
+}
+
+static void
+gst_rg_volume_reset (GstRgVolume * self)
+{
+  self->has_track_gain = FALSE;
+  self->has_track_peak = FALSE;
+  self->has_album_gain = FALSE;
+  self->has_album_peak = FALSE;
+
+  self->reference_level = RG_REFERENCE_LEVEL;
+
+  gst_rg_volume_update_gain (self);
+}
+
+static void
+gst_rg_volume_update_gain (GstRgVolume * self)
+{
+  gdouble target_gain, result_gain, result_volume;
+  gboolean target_gain_changed, result_gain_changed;
+
+  gst_rg_volume_determine_gain (self, &target_gain, &result_gain);
+
+  result_volume = DB_TO_LINEAR (result_gain);
+
+  /* Ensure that the result volume is within the range that the volume element
+   * can handle.  Currently, the limit is 10. (+20 dB), which should not be
+   * restrictive. */
+  if (G_UNLIKELY (result_volume > self->max_volume)) {
+    GST_INFO_OBJECT (self,
+        "cannot handle result gain of %" GAIN_FORMAT " (%0.6f), adjusting",
+        result_gain, result_volume);
+
+    result_volume = self->max_volume;
+    result_gain = LINEAR_TO_DB (result_volume);
+  }
+
+  /* Direct comparison is OK in this case. */
+  if (target_gain == result_gain) {
+    GST_INFO_OBJECT (self,
+        "result gain is %" GAIN_FORMAT " (%0.6f), matching target",
+        result_gain, result_volume);
+  } else {
+    GST_INFO_OBJECT (self,
+        "result gain is %" GAIN_FORMAT " (%0.6f), target is %" GAIN_FORMAT,
+        result_gain, result_volume, target_gain);
+  }
+
+  target_gain_changed = (self->target_gain != target_gain);
+  result_gain_changed = (self->result_gain != result_gain);
+
+  self->target_gain = target_gain;
+  self->result_gain = result_gain;
+
+  g_object_set (self->volume_element, "volume", result_volume, NULL);
+
+  if (target_gain_changed)
+    g_object_notify ((GObject *) self, "target-gain");
+  if (result_gain_changed)
+    g_object_notify ((GObject *) self, "result-gain");
+}
+
+static inline void
+gst_rg_volume_determine_gain (GstRgVolume * self, gdouble * target_gain,
+    gdouble * result_gain)
+{
+  gdouble gain, peak;
+
+  if (!self->has_track_gain && !self->has_album_gain) {
+
+    GST_DEBUG_OBJECT (self, "using fallback gain");
+    gain = self->fallback_gain;
+    peak = 1.0;
+
+  } else if ((self->album_mode && self->has_album_gain)
+      || (!self->album_mode && !self->has_track_gain)) {
+
+    gain = self->album_gain;
+    if (G_LIKELY (self->has_album_peak)) {
+      peak = self->album_peak;
+    } else {
+      GST_DEBUG_OBJECT (self, "album peak missing, assuming 1.0");
+      peak = 1.0;
+    }
+    /* Falling back from track to album gain shouldn't really happen. */
+    if (G_UNLIKELY (!self->album_mode))
+      GST_INFO_OBJECT (self, "falling back to album gain");
+
+  } else {
+    /* !album_mode && !has_album_gain || album_mode && has_track_gain */
+
+    gain = self->track_gain;
+    if (G_LIKELY (self->has_track_peak)) {
+      peak = self->track_peak;
+    } else {
+      GST_DEBUG_OBJECT (self, "track peak missing, assuming 1.0");
+      peak = 1.0;
+    }
+    if (self->album_mode)
+      GST_INFO_OBJECT (self, "falling back to track gain");
+  }
+
+  gain += self->pre_amp;
+
+  *target_gain = gain;
+  *result_gain = gain;
+
+  if (LINEAR_TO_DB (peak) + gain > self->headroom) {
+    *result_gain = LINEAR_TO_DB (1. / peak) + self->headroom;
+  }
+}
diff -Naurp -x .svn -x inspect -x gst-plugins-bad-plugins.args -x gst-plugins-bad-plugins.hierarchy gst-plugins-bad-prepare/gst/replaygain/gstrgvolume.h gst-plugins-bad/gst/replaygain/gstrgvolume.h
--- gst-plugins-bad-prepare/gst/replaygain/gstrgvolume.h	1970-01-01 01:00:00.000000000 +0100
+++ gst-plugins-bad/gst/replaygain/gstrgvolume.h	2007-02-14 14:49:39.000000000 +0100
@@ -0,0 +1,83 @@
+/* GStreamer ReplayGain volume adjustment
+ *
+ * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
+ *
+ * gstrgvolume.h: Element to apply ReplayGain volume adjustment
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#ifndef __GST_RG_VOLUME_H__
+#define __GST_RG_VOLUME_H__
+
+#include <gst/gst.h>
+
+G_BEGIN_DECLS
+
+#define GST_TYPE_RG_VOLUME \
+  (gst_rg_volume_get_type())
+#define GST_RG_VOLUME(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RG_VOLUME,GstRgVolume))
+#define GST_RG_VOLUME_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RG_VOLUME,GstRgVolumeClass))
+#define GST_IS_PLUGIN_TEMPLATE(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RG_VOLUME))
+#define GST_IS_PLUGIN_TEMPLATE_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RG_VOLUME))
+
+typedef struct _GstRgVolume GstRgVolume;
+typedef struct _GstRgVolumeClass GstRgVolumeClass;
+
+struct _GstRgVolume
+{
+  GstBin bin;
+
+  /*< private >*/
+
+  GstElement *volume_element;
+  gdouble max_volume;
+
+  gboolean album_mode;
+  gdouble headroom;
+  gdouble pre_amp;
+  gdouble fallback_gain;
+
+  gdouble target_gain;
+  gdouble result_gain;
+
+  gdouble track_gain;
+  gdouble track_peak;
+  gdouble album_gain;
+  gdouble album_peak;
+
+  gboolean has_track_gain;
+  gboolean has_track_peak;
+  gboolean has_album_gain;
+  gboolean has_album_peak;
+
+  gdouble reference_level;
+};
+
+struct _GstRgVolumeClass
+{
+  GstBinClass parent_class;
+};
+
+GType gst_rg_volume_get_type (void);
+
+G_END_DECLS
+
+#endif /* __GST_RG_VOLUME_H__ */
diff -Naurp -x .svn -x inspect -x gst-plugins-bad-plugins.args -x gst-plugins-bad-plugins.hierarchy gst-plugins-bad-prepare/gst/replaygain/Makefile.am gst-plugins-bad/gst/replaygain/Makefile.am
--- gst-plugins-bad-prepare/gst/replaygain/Makefile.am	2007-02-26 00:54:56.000000000 +0100
+++ gst-plugins-bad/gst/replaygain/Makefile.am	2007-02-26 00:34:59.000000000 +0100
@@ -2,14 +2,18 @@ plugin_LTLIBRARIES = libgstreplaygain.la
 
 libgstreplaygain_la_SOURCES = \
 	gstrganalysis.c       \
+	gstrglimiter.c        \
+	gstrgvolume.c         \
 	replaygain.c          \
 	rganalysis.c
 libgstreplaygain_la_CFLAGS  = $(GST_CFLAGS) $(GST_BASE_CFLAGS)
-libgstreplaygain_la_LIBADD  = $(GST_LIBS) $(GST_BASE_LIBS) $(LIBM)
+libgstreplaygain_la_LIBADD  = $(GST_LIBS) $(GST_BASE_LIBS) -lgstbaseutils-0.10 $(LIBM)
 libgstreplaygain_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
 
 # headers we need but don't want installed
 noinst_HEADERS =         \
 	gstrganalysis.h  \
+	gstrglimiter.h   \
+	gstrgvolume.h    \
 	replaygain.h     \
 	rganalysis.h
diff -Naurp -x .svn -x inspect -x gst-plugins-bad-plugins.args -x gst-plugins-bad-plugins.hierarchy gst-plugins-bad-prepare/gst/replaygain/replaygain.c gst-plugins-bad/gst/replaygain/replaygain.c
--- gst-plugins-bad-prepare/gst/replaygain/replaygain.c	2007-02-23 22:58:46.000000000 +0100
+++ gst-plugins-bad/gst/replaygain/replaygain.c	2007-02-06 00:38:26.000000000 +0100
@@ -27,6 +27,8 @@
 #include <gst/gst.h>
 
 #include "gstrganalysis.h"
+#include "gstrglimiter.h"
+#include "gstrgvolume.h"
 
 static gboolean
 plugin_init (GstPlugin * plugin)
@@ -35,6 +37,14 @@ plugin_init (GstPlugin * plugin)
           GST_TYPE_RG_ANALYSIS))
     return FALSE;
 
+  if (!gst_element_register (plugin, "rglimiter", GST_RANK_NONE,
+          GST_TYPE_RG_LIMITER))
+    return FALSE;
+
+  if (!gst_element_register (plugin, "rgvolume", GST_RANK_NONE,
+          GST_TYPE_RG_VOLUME))
+    return FALSE;
+
   return TRUE;
 }
 
diff -Naurp -x .svn -x inspect -x gst-plugins-bad-plugins.args -x gst-plugins-bad-plugins.hierarchy gst-plugins-bad-prepare/tests/check/elements/.cvsignore gst-plugins-bad/tests/check/elements/.cvsignore
--- gst-plugins-bad-prepare/tests/check/elements/.cvsignore	2007-02-23 21:17:15.000000000 +0100
+++ gst-plugins-bad/tests/check/elements/.cvsignore	2007-02-09 18:19:54.000000000 +0100
@@ -3,6 +3,8 @@ tagid3v2mux
 gdpdepay
 gdppay
 mpeg2enc
+rglimiter
+rgvolume
 wavpackdec
 wavpackenc
 wavpackparse
diff -Naurp -x .svn -x inspect -x gst-plugins-bad-plugins.args -x gst-plugins-bad-plugins.hierarchy gst-plugins-bad-prepare/tests/check/elements/rglimiter.c gst-plugins-bad/tests/check/elements/rglimiter.c
--- gst-plugins-bad-prepare/tests/check/elements/rglimiter.c	1970-01-01 01:00:00.000000000 +0100
+++ gst-plugins-bad/tests/check/elements/rglimiter.c	2007-02-27 17:06:41.000000000 +0100
@@ -0,0 +1,238 @@
+/* GStreamer ReplayGain limiter
+ *
+ * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
+ *
+ * rglimiter.c: Unit test for the rglimiter element
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#include <gst/check/gstcheck.h>
+
+#include <math.h>
+
+GList *buffers = NULL;
+
+/* For ease of programming we use globals to keep refs for our floating
+ * src and sink pads we create; otherwise we always have to do get_pad,
+ * get_peer, and then remove references in every test function */
+static GstPad *mysrcpad, *mysinkpad;
+
+#define RG_LIMITER_CAPS_TEMPLATE_STRING   \
+  "audio/x-raw-float, "                   \
+  "width = (int) 32, "                    \
+  "endianness = (int) BYTE_ORDER, "       \
+  "channels = (int) [ 1, MAX ], "         \
+  "rate = (int) [ 1, MAX ]"
+
+static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS (RG_LIMITER_CAPS_TEMPLATE_STRING)
+    );
+static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
+    GST_PAD_SRC,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS (RG_LIMITER_CAPS_TEMPLATE_STRING)
+    );
+
+GstElement *
+setup_rglimiter ()
+{
+  GstElement *element;
+  GstBus *bus;
+
+  GST_DEBUG ("setup_rglimiter");
+  element = gst_check_setup_element ("rglimiter");
+  mysrcpad = gst_check_setup_src_pad (element, &srctemplate, NULL);
+  mysinkpad = gst_check_setup_sink_pad (element, &sinktemplate, NULL);
+  gst_pad_set_active (mysrcpad, TRUE);
+  gst_pad_set_active (mysinkpad, TRUE);
+
+  return element;
+}
+
+void
+cleanup_rglimiter (GstElement * element)
+{
+  GST_DEBUG ("cleanup_rglimiter");
+
+  g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
+  g_list_free (buffers);
+  buffers = NULL;
+
+  gst_check_teardown_src_pad (element);
+  gst_check_teardown_sink_pad (element);
+  gst_check_teardown_element (element);
+}
+
+static void
+set_playing_state (GstElement * element)
+{
+  fail_unless (gst_element_set_state (element,
+          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
+      "Could not set state to PLAYING");
+}
+
+static const gfloat test_input[] = {
+  -2.0, -1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75, 1.0, 2.0
+};
+static const gfloat test_output[] = {
+  -0.99752737684336523,         /* -2.0  */
+  -0.88079707797788243,         /* -1.0  */
+  -0.7310585786300049,          /* -0.75 */
+  -0.5, -0.25, 0.0, 0.25, 0.5,
+  0.7310585786300049,           /* 0.75 */
+  0.88079707797788243,          /* 1.0  */
+  0.99752737684336523,          /* 2.0  */
+};
+
+static GstBuffer *
+create_test_buffer ()
+{
+  GstBuffer *buf = gst_buffer_new_and_alloc (sizeof (test_input));
+  GstCaps *caps;
+
+  memcpy (GST_BUFFER_DATA (buf), test_input, sizeof (test_input));
+
+  caps = gst_caps_new_simple ("audio/x-raw-float",
+      "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 1,
+      "endianess", G_TYPE_INT, G_BYTE_ORDER, "width", G_TYPE_INT, 32, NULL);
+  gst_buffer_set_caps (buf, caps);
+  gst_caps_unref (caps);
+
+  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);
+
+  return buf;
+}
+
+static void
+verify_test_buffer (GstBuffer * buf)
+{
+  gfloat *output = (gfloat *) GST_BUFFER_DATA (buf);
+  gint i;
+
+  fail_unless (GST_BUFFER_SIZE (buf) == sizeof (test_output));
+  for (i = 0; i < G_N_ELEMENTS (test_input); i++)
+    fail_unless (ABS (output[i] - test_output[i]) < 1.e-6,
+        "Incorrect output value %.6f for input %.2f, expected %.6f",
+        output[i], test_input[i], test_output[i]);
+}
+
+/* Start of tests. */
+
+GST_START_TEST (test_no_buffer)
+{
+  GstElement *element = setup_rglimiter ();
+
+  set_playing_state (element);
+
+  cleanup_rglimiter (element);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_disabled)
+{
+  GstElement *element = setup_rglimiter ();
+  GstBuffer *buf, *out_buf;
+
+  g_object_set (element, "enabled", FALSE, NULL);
+  set_playing_state (element);
+
+  buf = create_test_buffer ();
+  fail_unless (gst_pad_push (mysrcpad, buf) == GST_FLOW_OK);
+  fail_unless (g_list_length (buffers) == 1);
+  out_buf = buffers->data;
+  fail_if (out_buf == NULL);
+  buffers = g_list_remove (buffers, out_buf);
+  ASSERT_BUFFER_REFCOUNT (out_buf, "out_buf", 1);
+  fail_unless (buf == out_buf);
+  gst_buffer_unref (out_buf);
+
+  cleanup_rglimiter (element);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_limiting)
+{
+  GstElement *element = setup_rglimiter ();
+  GstBuffer *buf, *out_buf;
+
+  set_playing_state (element);
+
+  /* Mutable variant. */
+  buf = create_test_buffer ();
+  fail_unless (gst_pad_push (mysrcpad, buf) == GST_FLOW_OK);
+  fail_unless (g_list_length (buffers) == 1);
+  out_buf = buffers->data;
+  fail_if (out_buf == NULL);
+  ASSERT_BUFFER_REFCOUNT (out_buf, "out_buf", 1);
+  verify_test_buffer (out_buf);
+
+  /* Immutable variant. */
+  buf = create_test_buffer ();
+  /* Extra ref: */
+  gst_buffer_ref (buf);
+  ASSERT_BUFFER_REFCOUNT (buf, "buf", 2);
+  fail_unless (gst_pad_push (mysrcpad, buf) == GST_FLOW_OK);
+  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);
+  fail_unless (g_list_length (buffers) == 2);
+  out_buf = g_list_last (buffers)->data;
+  fail_if (out_buf == NULL);
+  ASSERT_BUFFER_REFCOUNT (out_buf, "out_buf", 1);
+  fail_unless (buf != out_buf);
+  /* Drop our extra ref: */
+  gst_buffer_unref (buf);
+  verify_test_buffer (out_buf);
+
+  cleanup_rglimiter (element);
+}
+
+GST_END_TEST;
+
+Suite *
+rglimiter_suite (void)
+{
+  Suite *s = suite_create ("rglimiter");
+  TCase *tc_chain = tcase_create ("general");
+
+  suite_add_tcase (s, tc_chain);
+
+  tcase_add_test (tc_chain, test_no_buffer);
+  tcase_add_test (tc_chain, test_disabled);
+  tcase_add_test (tc_chain, test_limiting);
+
+  return s;
+}
+
+int
+main (int argc, char **argv)
+{
+  gint nf;
+
+  Suite *s = rglimiter_suite ();
+  SRunner *sr = srunner_create (s);
+
+  gst_check_init (&argc, &argv);
+
+  srunner_run_all (sr, CK_ENV);
+  nf = srunner_ntests_failed (sr);
+  srunner_free (sr);
+
+  return nf;
+}
diff -Naurp -x .svn -x inspect -x gst-plugins-bad-plugins.args -x gst-plugins-bad-plugins.hierarchy gst-plugins-bad-prepare/tests/check/elements/rgvolume.c gst-plugins-bad/tests/check/elements/rgvolume.c
--- gst-plugins-bad-prepare/tests/check/elements/rgvolume.c	1970-01-01 01:00:00.000000000 +0100
+++ gst-plugins-bad/tests/check/elements/rgvolume.c	2007-02-27 17:06:41.000000000 +0100
@@ -0,0 +1,573 @@
+/* GStreamer ReplayGain volume adjustment
+ *
+ * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
+ *
+ * rgvolume.c: Unit test for the rgvolume element
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#include <gst/check/gstcheck.h>
+
+#include <math.h>
+
+GList *buffers = NULL;
+GList *events = NULL;
+
+/* For ease of programming we use globals to keep refs for our floating src and
+ * sink pads we create; otherwise we always have to do get_pad, get_peer, and
+ * then remove references in every test function */
+static GstPad *mysrcpad, *mysinkpad;
+
+#define RG_VOLUME_CAPS_TEMPLATE_STRING    \
+  "audio/x-raw-float, "                   \
+  "width = (int) 32, "                    \
+  "endianness = (int) BYTE_ORDER, "       \
+  "channels = (int) [ 1, MAX ], "         \
+  "rate = (int) [ 1, MAX ]"
+
+static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS (RG_VOLUME_CAPS_TEMPLATE_STRING)
+    );
+static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
+    GST_PAD_SRC,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS (RG_VOLUME_CAPS_TEMPLATE_STRING)
+    );
+
+/* gstcheck sets up a chain function that appends buffers to a global list.
+ * This is our equivalent of that for event handling. */
+static gboolean
+event_func (GstPad * pad, GstEvent * event)
+{
+  events = g_list_append (events, event);
+
+  return TRUE;
+}
+
+GstElement *
+setup_rgvolume ()
+{
+  GstElement *element;
+
+  GST_DEBUG ("setup_rgvolume");
+  element = gst_check_setup_element ("rgvolume");
+  mysrcpad = gst_check_setup_src_pad (element, &srctemplate, NULL);
+  mysinkpad = gst_check_setup_sink_pad (element, &sinktemplate, NULL);
+
+  /* Capture events, to test tag filtering behavior: */
+  gst_pad_set_event_function (mysinkpad, event_func);
+
+  gst_pad_set_active (mysrcpad, TRUE);
+  gst_pad_set_active (mysinkpad, TRUE);
+
+  return element;
+}
+
+void
+cleanup_rgvolume (GstElement * element)
+{
+  GST_DEBUG ("cleanup_rgvolume");
+
+  g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
+  g_list_free (buffers);
+  buffers = NULL;
+
+  g_list_foreach (events, (GFunc) gst_mini_object_unref, NULL);
+  g_list_free (events);
+  events = NULL;
+
+  gst_pad_set_active (mysrcpad, FALSE);
+  gst_pad_set_active (mysinkpad, FALSE);
+  gst_check_teardown_src_pad (element);
+  gst_check_teardown_sink_pad (element);
+  gst_check_teardown_element (element);
+}
+
+static void
+set_playing_state (GstElement * element)
+{
+  fail_unless (gst_element_set_state (element,
+          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
+      "Could not set state to PLAYING");
+}
+
+static void
+set_null_state (GstElement * element)
+{
+  fail_unless (gst_element_set_state (element,
+          GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS,
+      "Could not set state to NULL");
+}
+
+static void
+send_eos_event (GstElement * element)
+{
+  GstEvent *event = gst_event_new_eos ();
+
+  fail_unless (g_list_length (events) == 0);
+  fail_unless (gst_pad_push_event (mysrcpad, event),
+      "Pushing EOS event failed");
+  fail_unless (g_list_length (events) == 1);
+  fail_unless (events->data == event);
+  gst_mini_object_unref ((GstMiniObject *) events->data);
+  events = g_list_remove (events, event);
+}
+
+static GstEvent *
+send_tag_event (GstElement * element, GstEvent * event)
+{
+  g_return_val_if_fail (event->type == GST_EVENT_TAG, NULL);
+
+  fail_unless (g_list_length (events) == 0);
+  fail_unless (gst_pad_push_event (mysrcpad, event),
+      "Pushing tag event failed");
+
+  if (g_list_length (events) == 0) {
+    /* Event got filtered out. */
+    event = NULL;
+  } else {
+    GstTagList *tag_list;
+    gdouble dummy;
+
+    event = events->data;
+    events = g_list_remove (events, event);
+
+    fail_unless (event->type == GST_EVENT_TAG);
+    gst_event_parse_tag (event, &tag_list);
+
+    /* The element is supposed to filter out ReplayGain related tags. */
+    fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_GAIN, &dummy),
+        "tag event still contains track gain tag");
+    fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_PEAK, &dummy),
+        "tag event still contains track peak tag");
+    fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_GAIN, &dummy),
+        "tag event still contains album gain tag");
+    fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_PEAK, &dummy),
+        "tag event still contains album peak tag");
+  }
+
+  return event;
+}
+
+static GstBuffer *
+test_buffer_new (gfloat value)
+{
+  GstBuffer *buf;
+  GstCaps *caps;
+  gfloat *data;
+  gint i;
+
+  buf = gst_buffer_new_and_alloc (8 * sizeof (gfloat));
+  data = (gfloat *) GST_BUFFER_DATA (buf);
+  for (i = 0; i < 8; i++)
+    data[i] = value;
+
+  caps = gst_caps_from_string ("audio/x-raw-float, "
+      "rate = 8000, channels = 1, endianess = BYTE_ORDER, width = 32");
+  gst_buffer_set_caps (buf, caps);
+  gst_caps_unref (caps);
+
+  ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);
+
+  return buf;
+}
+
+#define MATCH_GAIN(g1, g2) ((g1 < g2 + 1e-6) && (g2 < g1 + 1e-6))
+
+static void
+fail_unless_target_gain (GstElement * element, gdouble expected_gain)
+{
+  gdouble prop_gain;
+
+  g_object_get (element, "target-gain", &prop_gain, NULL);
+
+  fail_unless (MATCH_GAIN (prop_gain, expected_gain),
+      "Target gain is %.2f dB, expected %.2f dB", prop_gain, expected_gain);
+}
+
+static void
+fail_unless_result_gain (GstElement * element, gdouble expected_gain)
+{
+  GstBuffer *input_buf, *output_buf;
+  gfloat input_sample, output_sample;
+  gdouble gain, prop_gain;
+  gboolean is_passthrough, expect_passthrough;
+  gint i;
+
+  fail_unless (g_list_length (buffers) == 0);
+
+  input_sample = 1.0;
+  input_buf = test_buffer_new (input_sample);
+
+  /* We keep an extra reference to detect passthrough mode. */
+  gst_buffer_ref (input_buf);
+  /* Pushing steals a reference. */
+  fail_unless (gst_pad_push (mysrcpad, input_buf) == GST_FLOW_OK);
+  gst_buffer_unref (input_buf);
+
+  /* The output buffer ends up on the global buffer list. */
+  fail_unless (g_list_length (buffers) == 1);
+  output_buf = buffers->data;
+  fail_if (output_buf == NULL);
+
+  buffers = g_list_remove (buffers, output_buf);
+  ASSERT_BUFFER_REFCOUNT (output_buf, "output_buf", 1);
+  fail_unless_equals_int (GST_BUFFER_SIZE (output_buf), 8 * sizeof (gfloat));
+
+  output_sample = *((gfloat *) GST_BUFFER_DATA (output_buf));
+
+  fail_if (output_sample == 0.0, "First output sample is zero");
+  for (i = 1; i < 8; i++) {
+    gfloat output = ((gfloat *) GST_BUFFER_DATA (output_buf))[i];
+
+    fail_unless (output_sample == output, "Output samples not uniform");
+  };
+
+  gain = 20. * log10 (output_sample / input_sample);
+  fail_unless (MATCH_GAIN (gain, expected_gain),
+      "Applied gain is %.2f dB, expected %.2f dB", gain, expected_gain);
+  g_object_get (element, "result-gain", &prop_gain, NULL);
+  fail_unless (MATCH_GAIN (prop_gain, expected_gain),
+      "Result gain is %.2f dB, expected %.2f dB", prop_gain, expected_gain);
+
+  is_passthrough = (output_buf == input_buf);
+  expect_passthrough = MATCH_GAIN (expected_gain, +0.00);
+  fail_unless (is_passthrough == expect_passthrough,
+      expect_passthrough
+      ? "Expected operation in passthrough mode"
+      : "Incorrect passthrough behaviour");
+
+  gst_buffer_unref (output_buf);
+}
+
+static void
+fail_unless_gain (GstElement * element, gdouble expected_gain)
+{
+  fail_unless_target_gain (element, expected_gain);
+  fail_unless_result_gain (element, expected_gain);
+}
+
+/* Start of tests. */
+
+GST_START_TEST (test_no_buffer)
+{
+  GstElement *element = setup_rgvolume ();
+
+  set_playing_state (element);
+  set_null_state (element);
+  set_playing_state (element);
+  send_eos_event (element);
+
+  cleanup_rgvolume (element);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_events)
+{
+  GstElement *element = setup_rgvolume ();
+  GstEvent *event;
+  GstEvent *new_event;
+  GstTagList *tag_list;
+  gchar *artist;
+
+  set_playing_state (element);
+
+  tag_list = gst_tag_list_new ();
+  gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
+      GST_TAG_TRACK_GAIN, +4.95, GST_TAG_TRACK_PEAK, 0.59463,
+      GST_TAG_ALBUM_GAIN, -1.54, GST_TAG_ALBUM_PEAK, 0.693415,
+      GST_TAG_ARTIST, "Foobar", NULL);
+  event = gst_event_new_tag (tag_list);
+  new_event = send_tag_event (element, event);
+  /* Expect the element to modify the writable event. */
+  fail_unless (event == new_event, "Writable tag event not reused");
+  gst_event_parse_tag (new_event, &tag_list);
+  fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
+  fail_unless (g_str_equal (artist, "Foobar"));
+  g_free (artist);
+  gst_event_unref (new_event);
+
+  /* Same as above, but with a non-writable event. */
+
+  tag_list = gst_tag_list_new ();
+  gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
+      GST_TAG_TRACK_GAIN, +4.95, GST_TAG_TRACK_PEAK, 0.59463,
+      GST_TAG_ALBUM_GAIN, -1.54, GST_TAG_ALBUM_PEAK, 0.693415,
+      GST_TAG_ARTIST, "Foobar", NULL);
+  event = gst_event_new_tag (tag_list);
+  /* Holding an extra ref makes the event unwritable: */
+  gst_event_ref (event);
+  new_event = send_tag_event (element, event);
+  fail_unless (event != new_event, "Unwritable tag event reused");
+  gst_event_parse_tag (new_event, &tag_list);
+  fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
+  fail_unless (g_str_equal (artist, "Foobar"));
+  g_free (artist);
+  gst_event_unref (event);
+  gst_event_unref (new_event);
+
+  cleanup_rgvolume (element);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_simple)
+{
+  GstElement *element = setup_rgvolume ();
+  GstTagList *tag_list;
+
+  g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
+      "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
+  set_playing_state (element);
+
+  tag_list = gst_tag_list_new ();
+  gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
+      GST_TAG_TRACK_GAIN, -3.45, GST_TAG_TRACK_PEAK, 1.0,
+      GST_TAG_ALBUM_GAIN, +2.09, GST_TAG_ALBUM_PEAK, 1.0, NULL);
+  fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
+  fail_unless_gain (element, -9.45);    /* pre-amp + track gain */
+  send_eos_event (element);
+
+  g_object_set (element, "album-mode", TRUE, NULL);
+
+  tag_list = gst_tag_list_new ();
+  gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
+      GST_TAG_TRACK_GAIN, -3.45, GST_TAG_TRACK_PEAK, 1.0,
+      GST_TAG_ALBUM_GAIN, +2.09, GST_TAG_ALBUM_PEAK, 1.0, NULL);
+  fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
+  fail_unless_gain (element, -3.91);    /* pre-amp + album gain */
+
+  /* Switching back to track mode in the middle of a stream: */
+  g_object_set (element, "album-mode", FALSE, NULL);
+  fail_unless_gain (element, -9.45);    /* pre-amp + track gain */
+  send_eos_event (element);
+
+  cleanup_rgvolume (element);
+}
+
+GST_END_TEST;
+
+/* If there are no gain tags at all, the fallback gain is used. */
+
+GST_START_TEST (test_fallback_gain)
+{
+  GstElement *element = setup_rgvolume ();
+  GstTagList *tag_list;
+
+  /* First some track where fallback does _not_ apply. */
+
+  g_object_set (element, "album-mode", FALSE, "headroom", 10.00,
+      "pre-amp", -6.00, "fallback-gain", -3.00, NULL);
+  set_playing_state (element);
+
+  tag_list = gst_tag_list_new ();
+  gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
+      GST_TAG_TRACK_GAIN, +3.5, GST_TAG_TRACK_PEAK, 1.0,
+      GST_TAG_ALBUM_GAIN, -0.5, GST_TAG_ALBUM_PEAK, 1.0, NULL);
+  fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
+  fail_unless_gain (element, -2.50);    /* pre-amp + track gain */
+  send_eos_event (element);
+
+  /* Now a track completely missing tags. */
+
+  fail_unless_gain (element, -9.00);    /* pre-amp + fallback-gain */
+
+  /* Changing the fallback gain in the middle of a stream, going to pass-through
+   * mode: */
+  g_object_set (element, "fallback-gain", +6.00, NULL);
+  fail_unless_gain (element, +0.00);    /* pre-amp + fallback-gain */
+  send_eos_event (element);
+
+  /* Verify that result gain is set to +0.00 with pre-amp + fallback-gain >
+   * +0.00 and no headroom. */
+
+  g_object_set (element, "fallback-gain", +12.00, "headroom", +0.00, NULL);
+  fail_unless_target_gain (element, +6.00);     /* pre-amp + fallback-gain */
+  fail_unless_result_gain (element, +0.00);
+  send_eos_event (element);
+
+  cleanup_rgvolume (element);
+}
+
+GST_END_TEST;
+
+/* If album gain is to be preferred but not available, the track gain is to be
+ * taken instead. */
+
+GST_START_TEST (test_fallback_track)
+{
+  GstElement *element = setup_rgvolume ();
+  GstTagList *tag_list;
+
+  g_object_set (element, "album-mode", TRUE, "headroom", +0.00,
+      "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
+  set_playing_state (element);
+
+  tag_list = gst_tag_list_new ();
+  gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
+      GST_TAG_TRACK_GAIN, +2.11, GST_TAG_TRACK_PEAK, 1.0, NULL);
+  fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
+  fail_unless_gain (element, -3.89);    /* pre-amp + track gain */
+
+  send_eos_event (element);
+
+  cleanup_rgvolume (element);
+}
+
+GST_END_TEST;
+
+/* If track gain is to be preferred but not available, the album gain is to be
+ * taken instead. */
+
+GST_START_TEST (test_fallback_album)
+{
+  GstElement *element = setup_rgvolume ();
+  GstTagList *tag_list;
+
+  g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
+      "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
+  set_playing_state (element);
+
+  tag_list = gst_tag_list_new ();
+  gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
+      GST_TAG_ALBUM_GAIN, +3.73, GST_TAG_ALBUM_PEAK, 1.0, NULL);
+  fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
+  fail_unless_gain (element, -2.27);    /* pre-amp + album gain */
+
+  send_eos_event (element);
+
+  cleanup_rgvolume (element);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_headroom)
+{
+  GstElement *element = setup_rgvolume ();
+  GstTagList *tag_list;
+
+  g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
+      "pre-amp", +0.00, "fallback-gain", +1.23, NULL);
+  set_playing_state (element);
+
+  tag_list = gst_tag_list_new ();
+  gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
+      GST_TAG_TRACK_GAIN, +3.50, GST_TAG_TRACK_PEAK, 1.0, NULL);
+  fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
+  fail_unless_target_gain (element, +3.50);     /* pre-amp + track gain */
+  fail_unless_result_gain (element, +0.00);
+  send_eos_event (element);
+
+  g_object_set (element, "headroom", +2.00, NULL);
+  tag_list = gst_tag_list_new ();
+  gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
+      GST_TAG_TRACK_GAIN, +9.18, GST_TAG_TRACK_PEAK, 0.687149, NULL);
+  fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
+  fail_unless_target_gain (element, +9.18);     /* pre-amp + track gain */
+  /* Result is 20. * log10 (1. / peak) + headroom. */
+  fail_unless_result_gain (element, 5.2589816238303335);
+  send_eos_event (element);
+
+  g_object_set (element, "album-mode", TRUE, NULL);
+  tag_list = gst_tag_list_new ();
+  gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
+      GST_TAG_ALBUM_GAIN, +5.50, GST_TAG_ALBUM_PEAK, 1.0, NULL);
+  fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
+  fail_unless_target_gain (element, +5.50);     /* pre-amp + album gain */
+  fail_unless_result_gain (element, +2.00);     /* headroom */
+  send_eos_event (element);
+
+  cleanup_rgvolume (element);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_reference_level)
+{
+  GstElement *element = setup_rgvolume ();
+  GstTagList *tag_list;
+
+  g_object_set (element,
+      "album-mode", FALSE,
+      "headroom", +0.00, "pre-amp", +0.00, "fallback-gain", +1.23, NULL);
+  set_playing_state (element);
+
+  tag_list = gst_tag_list_new ();
+  gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
+      GST_TAG_TRACK_GAIN, 0.00, GST_TAG_TRACK_PEAK, 0.2,
+      GST_TAG_REFERENCE_LEVEL, 83., NULL);
+  fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
+  /* Because our authorative reference is 89 dB, we bump it up by +6 dB. */
+  fail_unless_gain (element, +6.00);    /* pre-amp + track gain */
+  send_eos_event (element);
+
+  g_object_set (element, "album-mode", TRUE, NULL);
+
+  /* Same as above, but with album gain. */
+
+  tag_list = gst_tag_list_new ();
+  gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
+      GST_TAG_TRACK_GAIN, 1.23, GST_TAG_TRACK_PEAK, 0.1,
+      GST_TAG_ALBUM_GAIN, 0.00, GST_TAG_ALBUM_PEAK, 0.2,
+      GST_TAG_REFERENCE_LEVEL, 83., NULL);
+  fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
+  fail_unless_gain (element, +6.00);    /* pre-amp + album gain */
+
+  cleanup_rgvolume (element);
+}
+
+GST_END_TEST;
+
+Suite *
+rgvolume_suite (void)
+{
+  Suite *s = suite_create ("rgvolume");
+  TCase *tc_chain = tcase_create ("general");
+
+  suite_add_tcase (s, tc_chain);
+
+  tcase_add_test (tc_chain, test_no_buffer);
+  tcase_add_test (tc_chain, test_events);
+  tcase_add_test (tc_chain, test_simple);
+  tcase_add_test (tc_chain, test_fallback_gain);
+  tcase_add_test (tc_chain, test_fallback_track);
+  tcase_add_test (tc_chain, test_fallback_album);
+  tcase_add_test (tc_chain, test_headroom);
+  tcase_add_test (tc_chain, test_reference_level);
+
+  return s;
+}
+
+int
+main (int argc, char **argv)
+{
+  gint nf;
+
+  Suite *s = rgvolume_suite ();
+  SRunner *sr = srunner_create (s);
+
+  gst_check_init (&argc, &argv);
+
+  srunner_run_all (sr, CK_ENV);
+  nf = srunner_ntests_failed (sr);
+  srunner_free (sr);
+
+  return nf;
+}
diff -Naurp -x .svn -x inspect -x gst-plugins-bad-plugins.args -x gst-plugins-bad-plugins.hierarchy gst-plugins-bad-prepare/tests/check/Makefile.am gst-plugins-bad/tests/check/Makefile.am
--- gst-plugins-bad-prepare/tests/check/Makefile.am	2007-02-23 21:17:15.000000000 +0100
+++ gst-plugins-bad/tests/check/Makefile.am	2006-12-24 14:17:50.000000000 +0100
@@ -60,6 +60,8 @@ check_PROGRAMS = \
 	$(check_mpeg2enc)  \
 	$(check_neon)      \
 	elements/rganalysis \
+	elements/rglimiter \
+	elements/rgvolume \
 	elements/videocrop \
 	$(check_wavpack)   \
 	elements/y4menc
