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 * @title: rglimiter
26 * @see_also: #GstRgVolume
27 *
28 * This element applies signal compression/limiting to raw audio data. It
29 * performs strict hard limiting with soft-knee characteristics, using a
30 * threshold of -6 dB. This type of filter is mentioned in the proposed
31 * [ReplayGain standard](https://wiki.hydrogenaud.io/index.php?title=ReplayGain).
32 *
33 * ## Example launch line
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 *
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 GST_ELEMENT_REGISTER_DEFINE (rglimiter, "rglimiter", GST_RANK_NONE,
80 GST_TYPE_RG_LIMITER);
81
82 static void gst_rg_limiter_set_property (GObject * object, guint prop_id,
83 const GValue * value, GParamSpec * pspec);
84 static void gst_rg_limiter_get_property (GObject * object, guint prop_id,
85 GValue * value, GParamSpec * pspec);
86
87 static GstFlowReturn gst_rg_limiter_transform_ip (GstBaseTransform * base,
88 GstBuffer * buf);
89
90 static void
gst_rg_limiter_class_init(GstRgLimiterClass * klass)91 gst_rg_limiter_class_init (GstRgLimiterClass * klass)
92 {
93 GObjectClass *gobject_class;
94 GstElementClass *element_class;
95 GstBaseTransformClass *trans_class;
96
97 gobject_class = (GObjectClass *) klass;
98 element_class = (GstElementClass *) klass;
99
100 gobject_class->set_property = gst_rg_limiter_set_property;
101 gobject_class->get_property = gst_rg_limiter_get_property;
102
103 g_object_class_install_property (gobject_class, PROP_ENABLED,
104 g_param_spec_boolean ("enabled", "Enabled", "Enable processing", TRUE,
105 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
106
107 trans_class = GST_BASE_TRANSFORM_CLASS (klass);
108 trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_rg_limiter_transform_ip);
109 trans_class->passthrough_on_same_caps = FALSE;
110
111 gst_element_class_add_static_pad_template (element_class, &src_factory);
112 gst_element_class_add_static_pad_template (element_class, &sink_factory);
113 gst_element_class_set_static_metadata (element_class, "ReplayGain limiter",
114 "Filter/Effect/Audio",
115 "Apply signal compression to raw audio data",
116 "Ren\xc3\xa9 Stadler <mail@renestadler.de>");
117
118 GST_DEBUG_CATEGORY_INIT (gst_rg_limiter_debug, "rglimiter", 0,
119 "ReplayGain limiter element");
120 }
121
122 static void
gst_rg_limiter_init(GstRgLimiter * filter)123 gst_rg_limiter_init (GstRgLimiter * filter)
124 {
125 GstBaseTransform *base = GST_BASE_TRANSFORM (filter);
126
127 gst_base_transform_set_passthrough (base, FALSE);
128 gst_base_transform_set_gap_aware (base, TRUE);
129
130 filter->enabled = TRUE;
131 }
132
133 static void
gst_rg_limiter_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)134 gst_rg_limiter_set_property (GObject * object, guint prop_id,
135 const GValue * value, GParamSpec * pspec)
136 {
137 GstRgLimiter *filter = GST_RG_LIMITER (object);
138
139 switch (prop_id) {
140 case PROP_ENABLED:
141 filter->enabled = g_value_get_boolean (value);
142 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter),
143 !filter->enabled);
144 break;
145 default:
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
147 break;
148 }
149 }
150
151 static void
gst_rg_limiter_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)152 gst_rg_limiter_get_property (GObject * object, guint prop_id,
153 GValue * value, GParamSpec * pspec)
154 {
155 GstRgLimiter *filter = GST_RG_LIMITER (object);
156
157 switch (prop_id) {
158 case PROP_ENABLED:
159 g_value_set_boolean (value, filter->enabled);
160 break;
161 default:
162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163 break;
164 }
165 }
166
167 #define LIMIT 1.0
168 #define THRES 0.5 /* ca. -6 dB */
169 #define COMPL 0.5 /* LIMIT - THRESH */
170
171 static GstFlowReturn
gst_rg_limiter_transform_ip(GstBaseTransform * base,GstBuffer * buf)172 gst_rg_limiter_transform_ip (GstBaseTransform * base, GstBuffer * buf)
173 {
174 GstRgLimiter *filter = GST_RG_LIMITER (base);
175 gfloat *input;
176 GstMapInfo map;
177 guint count;
178 guint i;
179
180 if (!filter->enabled)
181 return GST_FLOW_OK;
182
183 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP))
184 return GST_FLOW_OK;
185
186 gst_buffer_map (buf, &map, GST_MAP_READ);
187 input = (gfloat *) map.data;
188 count = gst_buffer_get_size (buf) / sizeof (gfloat);
189
190 for (i = count; i--;) {
191 if (*input > THRES)
192 *input = tanhf ((*input - THRES) / COMPL) * COMPL + THRES;
193 else if (*input < -THRES)
194 *input = tanhf ((*input + THRES) / COMPL) * COMPL - THRES;
195 input++;
196 }
197
198 gst_buffer_unmap (buf, &map);
199
200 return GST_FLOW_OK;
201 }
202