gstreamer/gst/replaygain/gstrglimiter.c

198 lines
6 KiB
C
Raw Normal View History

Add replaygain playback elements (#412710). Original commit message from CVS: Patch by: René Stadler <mail at renestadler de> * docs/plugins/Makefile.am: * docs/plugins/gst-plugins-bad-plugins-docs.sgml: * docs/plugins/gst-plugins-bad-plugins-sections.txt: * docs/plugins/inspect/plugin-replaygain.xml: * gst/replaygain/Makefile.am: * gst/replaygain/gstrganalysis.c: (gst_rg_analysis_class_init), (gst_rg_analysis_start), (gst_rg_analysis_set_caps), (gst_rg_analysis_transform_ip), (gst_rg_analysis_event), (gst_rg_analysis_stop), (gst_rg_analysis_handle_tags), (gst_rg_analysis_handle_eos), (gst_rg_analysis_track_result), (gst_rg_analysis_album_result): * gst/replaygain/gstrganalysis.h: * gst/replaygain/gstrglimiter.c: (gst_rg_limiter_base_init), (gst_rg_limiter_class_init), (gst_rg_limiter_init), (gst_rg_limiter_set_property), (gst_rg_limiter_get_property), (gst_rg_limiter_transform_ip): * gst/replaygain/gstrglimiter.h: * gst/replaygain/gstrgvolume.c: (gst_rg_volume_base_init), (gst_rg_volume_class_init), (gst_rg_volume_init), (gst_rg_volume_set_property), (gst_rg_volume_get_property), (gst_rg_volume_dispose), (gst_rg_volume_change_state), (gst_rg_volume_sink_event), (gst_rg_volume_tag_event), (gst_rg_volume_reset), (gst_rg_volume_update_gain), (gst_rg_volume_determine_gain): * gst/replaygain/gstrgvolume.h: * gst/replaygain/replaygain.c: (plugin_init): * gst/replaygain/replaygain.h: * gst/replaygain/rganalysis.h: * tests/check/Makefile.am: * tests/check/elements/.cvsignore: * tests/check/elements/rganalysis.c: (send_eos_event), (GST_START_TEST): * tests/check/elements/rglimiter.c: (setup_rglimiter), (cleanup_rglimiter), (set_playing_state), (create_test_buffer), (verify_test_buffer), (GST_START_TEST), (rglimiter_suite), (main): * tests/check/elements/rgvolume.c: (event_func), (setup_rgvolume), (cleanup_rgvolume), (set_playing_state), (set_null_state), (send_eos_event), (send_tag_event), (test_buffer_new), (fail_unless_target_gain), (fail_unless_result_gain), (fail_unless_gain), (GST_START_TEST), (rgvolume_suite), (main): Add replaygain playback elements (#412710).
2007-05-19 10:01:45 +00:00
/* 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;
}