1 /* GStreamer ReplayGain limiter
2  *
3  * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
4  *
5  * gstrglimiter.c: Element to apply signal compression to raw audio data
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  */
22 
23 /**
24  * SECTION:element-rglimiter
25  * @see_also: #GstRgVolume
26  *
27  * This element applies signal compression/limiting to raw audio data.  It
28  * performs strict hard limiting with soft-knee characteristics, using a
29  * threshold of -6 dB.  This type of filter is mentioned in the proposed <ulink
30  * url="http://replaygain.org">ReplayGain standard</ulink>.
31  *
32  * <refsect2>
33  * <title>Example launch line</title>
34  * |[
35  * gst-launch-1.0 filesrc location=filename.ext ! decodebin ! audioconvert \
36  *            ! rgvolume pre-amp=6.0 headroom=10.0 ! rglimiter \
37  *            ! audioconvert ! audioresample ! alsasink
38  * ]|Playback of a file
39  * </refsect2>
40  */
41 
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45 
46 #include <gst/gst.h>
47 #include <math.h>
48 #include <gst/audio/audio.h>
49 
50 #include "gstrglimiter.h"
51 
52 GST_DEBUG_CATEGORY_STATIC (gst_rg_limiter_debug);
53 #define GST_CAT_DEFAULT gst_rg_limiter_debug
54 
55 enum
56 {
57   PROP_0,
58   PROP_ENABLED,
59 };
60 
61 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
62     GST_PAD_SINK,
63     GST_PAD_ALWAYS,
64     GST_STATIC_CAPS ("audio/x-raw, "
65         "format = (string) " GST_AUDIO_NE (F32) ", "
66         "layout = (string) { interleaved, non-interleaved }, "
67         "channels = (int) [1, MAX], " "rate = (int) [1, MAX]"));
68 
69 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
70     GST_PAD_SRC,
71     GST_PAD_ALWAYS,
72     GST_STATIC_CAPS ("audio/x-raw, "
73         "format = (string) " GST_AUDIO_NE (F32) ", "
74         "layout = (string) { interleaved, non-interleaved}, "
75         "channels = (int) [1, MAX], " "rate = (int) [1, MAX]"));
76 
77 #define gst_rg_limiter_parent_class parent_class
78 G_DEFINE_TYPE (GstRgLimiter, gst_rg_limiter, GST_TYPE_BASE_TRANSFORM);
79 
80 static void gst_rg_limiter_set_property (GObject * object, guint prop_id,
81     const GValue * value, GParamSpec * pspec);
82 static void gst_rg_limiter_get_property (GObject * object, guint prop_id,
83     GValue * value, GParamSpec * pspec);
84 
85 static GstFlowReturn gst_rg_limiter_transform_ip (GstBaseTransform * base,
86     GstBuffer * buf);
87 
88 static void
gst_rg_limiter_class_init(GstRgLimiterClass * klass)89 gst_rg_limiter_class_init (GstRgLimiterClass * klass)
90 {
91   GObjectClass *gobject_class;
92   GstElementClass *element_class;
93   GstBaseTransformClass *trans_class;
94 
95   gobject_class = (GObjectClass *) klass;
96   element_class = (GstElementClass *) klass;
97 
98   gobject_class->set_property = gst_rg_limiter_set_property;
99   gobject_class->get_property = gst_rg_limiter_get_property;
100 
101   g_object_class_install_property (gobject_class, PROP_ENABLED,
102       g_param_spec_boolean ("enabled", "Enabled", "Enable processing", TRUE,
103           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
104 
105   trans_class = GST_BASE_TRANSFORM_CLASS (klass);
106   trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_rg_limiter_transform_ip);
107   trans_class->passthrough_on_same_caps = FALSE;
108 
109   gst_element_class_add_static_pad_template (element_class, &src_factory);
110   gst_element_class_add_static_pad_template (element_class, &sink_factory);
111   gst_element_class_set_static_metadata (element_class, "ReplayGain limiter",
112       "Filter/Effect/Audio",
113       "Apply signal compression to raw audio data",
114       "Ren\xc3\xa9 Stadler <mail@renestadler.de>");
115 
116   GST_DEBUG_CATEGORY_INIT (gst_rg_limiter_debug, "rglimiter", 0,
117       "ReplayGain limiter element");
118 }
119 
120 static void
gst_rg_limiter_init(GstRgLimiter * filter)121 gst_rg_limiter_init (GstRgLimiter * filter)
122 {
123   GstBaseTransform *base = GST_BASE_TRANSFORM (filter);
124 
125   gst_base_transform_set_passthrough (base, FALSE);
126   gst_base_transform_set_gap_aware (base, TRUE);
127 
128   filter->enabled = TRUE;
129 }
130 
131 static void
gst_rg_limiter_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)132 gst_rg_limiter_set_property (GObject * object, guint prop_id,
133     const GValue * value, GParamSpec * pspec)
134 {
135   GstRgLimiter *filter = GST_RG_LIMITER (object);
136 
137   switch (prop_id) {
138     case PROP_ENABLED:
139       filter->enabled = g_value_get_boolean (value);
140       gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter),
141           !filter->enabled);
142       break;
143     default:
144       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
145       break;
146   }
147 }
148 
149 static void
gst_rg_limiter_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)150 gst_rg_limiter_get_property (GObject * object, guint prop_id,
151     GValue * value, GParamSpec * pspec)
152 {
153   GstRgLimiter *filter = GST_RG_LIMITER (object);
154 
155   switch (prop_id) {
156     case PROP_ENABLED:
157       g_value_set_boolean (value, filter->enabled);
158       break;
159     default:
160       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161       break;
162   }
163 }
164 
165 #define LIMIT 1.0
166 #define THRES 0.5               /* ca. -6 dB */
167 #define COMPL 0.5               /* LIMIT - THRESH */
168 
169 static GstFlowReturn
gst_rg_limiter_transform_ip(GstBaseTransform * base,GstBuffer * buf)170 gst_rg_limiter_transform_ip (GstBaseTransform * base, GstBuffer * buf)
171 {
172   GstRgLimiter *filter = GST_RG_LIMITER (base);
173   gfloat *input;
174   GstMapInfo map;
175   guint count;
176   guint i;
177 
178   if (!filter->enabled)
179     return GST_FLOW_OK;
180 
181   if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP))
182     return GST_FLOW_OK;
183 
184   gst_buffer_map (buf, &map, GST_MAP_READ);
185   input = (gfloat *) map.data;
186   count = gst_buffer_get_size (buf) / sizeof (gfloat);
187 
188   for (i = count; i--;) {
189     if (*input > THRES)
190       *input = tanhf ((*input - THRES) / COMPL) * COMPL + THRES;
191     else if (*input < -THRES)
192       *input = tanhf ((*input + THRES) / COMPL) * COMPL - THRES;
193     input++;
194   }
195 
196   gst_buffer_unmap (buf, &map);
197 
198   return GST_FLOW_OK;
199 }
200