• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2011 Tiago Katcipis <tiagokatcipis@gmail.com>
3  * Copyright (C) 2011 Paulo Pizarro  <paulo.pizarro@gmail.com>
4  * Copyright (C) 2012-2016 Nicola Murino  <nicola.murino@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 /**
23  * SECTION:element-removesilence
24  * @title: removesilence
25  *
26  * Removes all silence periods from an audio stream, dropping silence buffers.
27  * If the "silent" property is disabled, removesilence will generate
28  * bus messages named "removesilence".
29  * The message's structure contains one of these fields:
30  *
31  * - #guint64 "silence_detected": the PTS for the first silent buffer after a non silence period.
32  *
33  * - #guint64 "silence_finished": the PTS for the first non silent buffer after a silence period.
34  *
35  * ## Example launch line
36  * |[
37  * gst-launch-1.0 -v -m filesrc location="audiofile" ! decodebin ! removesilence remove=true ! wavenc ! filesink location=without_audio.wav
38  * ]|
39  *
40  */
41 
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45 
46 #include <gst/gst.h>
47 #include <gst/base/gstbasetransform.h>
48 #include <gst/audio/audio.h>
49 
50 #include "gstremovesilence.h"
51 
52 
53 GST_DEBUG_CATEGORY_STATIC (gst_remove_silence_debug);
54 #define GST_CAT_DEFAULT gst_remove_silence_debug
55 #define DEFAULT_VAD_HYSTERESIS  480     /* 60 mseg */
56 #define MINIMUM_SILENCE_BUFFERS_MIN  0
57 #define MINIMUM_SILENCE_BUFFERS_MAX  10000
58 #define MINIMUM_SILENCE_BUFFERS_DEF  0
59 #define MINIMUM_SILENCE_TIME_MIN  0
60 #define MINIMUM_SILENCE_TIME_MAX  10000000000
61 #define MINIMUM_SILENCE_TIME_DEF  0
62 #define DEFAULT_VAD_THRESHOLD -60
63 
64 /* Filter signals and args */
65 enum
66 {
67   /* FILL ME */
68   LAST_SIGNAL
69 };
70 
71 enum
72 {
73   PROP_0,
74   PROP_REMOVE,
75   PROP_HYSTERESIS,
76   PROP_THRESHOLD,
77   PROP_SQUASH,
78   PROP_SILENT,
79   PROP_MINIMUM_SILENCE_BUFFERS,
80   PROP_MINIMUM_SILENCE_TIME
81 };
82 
83 
84 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
85     GST_PAD_SINK,
86     GST_PAD_ALWAYS,
87     GST_STATIC_CAPS ("audio/x-raw, "
88         "format = (string) " GST_AUDIO_NE (S16) ", "
89         "layout = (string) interleaved, "
90         "rate = (int) [ 1, MAX ], " "channels = (int) 1"));
91 
92 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
93     GST_PAD_SRC,
94     GST_PAD_ALWAYS,
95     GST_STATIC_CAPS ("audio/x-raw, "
96         "format = (string) " GST_AUDIO_NE (S16) ", "
97         "layout = (string) interleaved, "
98         "rate = (int) [ 1, MAX ], " "channels = (int) 1"));
99 
100 
101 #define DEBUG_INIT(bla) \
102   GST_DEBUG_CATEGORY_INIT (gst_remove_silence_debug, "removesilence", 0, "removesilence element")
103 
104 #define gst_remove_silence_parent_class parent_class
105 G_DEFINE_TYPE_WITH_CODE (GstRemoveSilence, gst_remove_silence,
106     GST_TYPE_BASE_TRANSFORM, DEBUG_INIT (0));
107 GST_ELEMENT_REGISTER_DEFINE (removesilence, "removesilence", GST_RANK_NONE,
108     gst_remove_silence_get_type ());
109 
110 static void gst_remove_silence_set_property (GObject * object, guint prop_id,
111     const GValue * value, GParamSpec * pspec);
112 static void gst_remove_silence_get_property (GObject * object, guint prop_id,
113     GValue * value, GParamSpec * pspec);
114 
115 static gboolean gst_remove_silence_start (GstBaseTransform * trans);
116 static gboolean gst_remove_silence_sink_event (GstBaseTransform * trans,
117     GstEvent * event);
118 static GstFlowReturn gst_remove_silence_transform_ip (GstBaseTransform * base,
119     GstBuffer * buf);
120 static void gst_remove_silence_finalize (GObject * obj);
121 
122 /* GObject vmethod implementations */
123 
124 /* initialize the removesilence's class */
125 static void
gst_remove_silence_class_init(GstRemoveSilenceClass * klass)126 gst_remove_silence_class_init (GstRemoveSilenceClass * klass)
127 {
128   GObjectClass *gobject_class;
129   GstElementClass *gstelement_class;
130   GstBaseTransformClass *base_transform_class;
131 
132   gobject_class = (GObjectClass *) klass;
133   gstelement_class = (GstElementClass *) klass;
134   base_transform_class = GST_BASE_TRANSFORM_CLASS (klass);
135 
136   gobject_class->finalize = gst_remove_silence_finalize;
137   gobject_class->set_property = gst_remove_silence_set_property;
138   gobject_class->get_property = gst_remove_silence_get_property;
139 
140   g_object_class_install_property (gobject_class, PROP_REMOVE,
141       g_param_spec_boolean ("remove", "Remove",
142           "Set to true to remove silence from the stream, false otherwise",
143           FALSE, G_PARAM_READWRITE));
144 
145   g_object_class_install_property (gobject_class, PROP_HYSTERESIS,
146       g_param_spec_uint64 ("hysteresis",
147           "Hysteresis",
148           "Set the hysteresis (on samples) used on the internal VAD",
149           1, G_MAXUINT64, DEFAULT_VAD_HYSTERESIS, G_PARAM_READWRITE));
150 
151   g_object_class_install_property (gobject_class, PROP_THRESHOLD,
152       g_param_spec_int ("threshold",
153           "Threshold",
154           "Set the silence threshold used on the internal VAD in dB",
155           -70, 70, DEFAULT_VAD_THRESHOLD, G_PARAM_READWRITE));
156 
157   g_object_class_install_property (gobject_class, PROP_SQUASH,
158       g_param_spec_boolean ("squash", "Squash",
159           "Set to true to retimestamp buffers when silence is removed and so avoid timestamp gap",
160           FALSE, G_PARAM_READWRITE));
161 
162   g_object_class_install_property (gobject_class, PROP_SILENT,
163       g_param_spec_boolean ("silent", "Silent",
164           "Disable/enable bus message notifications for silence detected/finished",
165           TRUE, G_PARAM_READWRITE));
166 
167   g_object_class_install_property (gobject_class, PROP_MINIMUM_SILENCE_BUFFERS,
168       g_param_spec_uint ("minimum-silence-buffers", "Minimum silence buffers",
169           "Define the minimum number of consecutive silence buffers before "
170           "removing silence, 0 means disabled. This will not introduce latency",
171           MINIMUM_SILENCE_BUFFERS_MIN, MINIMUM_SILENCE_BUFFERS_MAX,
172           MINIMUM_SILENCE_BUFFERS_DEF, G_PARAM_READWRITE));
173 
174   g_object_class_install_property (gobject_class, PROP_MINIMUM_SILENCE_TIME,
175       g_param_spec_uint64 ("minimum_silence_time",
176           "Minimum silence time",
177           "Define the minimum silence time in nanoseconds before removing "
178           " silence, 0 means disabled. This will not introduce latency",
179           MINIMUM_SILENCE_TIME_MIN, MINIMUM_SILENCE_TIME_MAX,
180           MINIMUM_SILENCE_TIME_DEF, G_PARAM_READWRITE));
181 
182   gst_element_class_set_static_metadata (gstelement_class,
183       "RemoveSilence",
184       "Filter/Effect/Audio",
185       "Removes all the silence periods from the audio stream.",
186       "Tiago Katcipis <tiagokatcipis@gmail.com>\n \
187        Paulo Pizarro  <paulo.pizarro@gmail.com>\n \
188        Nicola Murino  <nicola.murino@gmail.com>");
189 
190   gst_element_class_add_static_pad_template (gstelement_class, &src_template);
191   gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
192 
193   base_transform_class->start = GST_DEBUG_FUNCPTR (gst_remove_silence_start);
194   base_transform_class->sink_event =
195       GST_DEBUG_FUNCPTR (gst_remove_silence_sink_event);
196   base_transform_class->transform_ip =
197       GST_DEBUG_FUNCPTR (gst_remove_silence_transform_ip);
198 }
199 
200 static void
gst_remove_silence_reset(GstRemoveSilence * filter)201 gst_remove_silence_reset (GstRemoveSilence * filter)
202 {
203   filter->ts_offset = 0;
204   filter->silence_detected = FALSE;
205   filter->consecutive_silence_buffers = 0;
206   filter->consecutive_silence_time = 0;
207 }
208 
209 /* initialize the new element
210  * instantiate pads and add them to element
211  * set pad callback functions
212  * initialize instance structure
213  */
214 static void
gst_remove_silence_init(GstRemoveSilence * filter)215 gst_remove_silence_init (GstRemoveSilence * filter)
216 {
217   filter->vad = vad_new (DEFAULT_VAD_HYSTERESIS, DEFAULT_VAD_THRESHOLD);
218   filter->remove = FALSE;
219   filter->squash = FALSE;
220   filter->silent = TRUE;
221   filter->minimum_silence_buffers = MINIMUM_SILENCE_BUFFERS_DEF;
222   filter->minimum_silence_time = MINIMUM_SILENCE_TIME_DEF;
223 
224   gst_remove_silence_reset (filter);
225 
226   if (!filter->vad) {
227     GST_DEBUG ("Error initializing VAD !!");
228     return;
229   }
230 }
231 
232 static gboolean
gst_remove_silence_start(GstBaseTransform * trans)233 gst_remove_silence_start (GstBaseTransform * trans)
234 {
235   GstRemoveSilence *filter = GST_REMOVE_SILENCE (trans);
236 
237   GST_INFO ("reset filter on start");
238   gst_remove_silence_reset (filter);
239 
240   return TRUE;
241 }
242 
243 static gboolean
gst_remove_silence_sink_event(GstBaseTransform * trans,GstEvent * event)244 gst_remove_silence_sink_event (GstBaseTransform * trans, GstEvent * event)
245 {
246   GstRemoveSilence *filter = GST_REMOVE_SILENCE (trans);
247 
248   if (event->type == GST_EVENT_SEGMENT) {
249     GST_INFO ("reset filter on segment event");
250     gst_remove_silence_reset (filter);
251   }
252 
253   return
254       GST_BASE_TRANSFORM_CLASS (gst_remove_silence_parent_class)->sink_event
255       (trans, event);
256 }
257 
258 static void
gst_remove_silence_finalize(GObject * obj)259 gst_remove_silence_finalize (GObject * obj)
260 {
261   GstRemoveSilence *filter = GST_REMOVE_SILENCE (obj);
262   GST_DEBUG ("Destroying VAD");
263   vad_destroy (filter->vad);
264   filter->vad = NULL;
265   GST_DEBUG ("VAD Destroyed");
266   G_OBJECT_CLASS (parent_class)->finalize (obj);
267 }
268 
269 static void
gst_remove_silence_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)270 gst_remove_silence_set_property (GObject * object, guint prop_id,
271     const GValue * value, GParamSpec * pspec)
272 {
273   GstRemoveSilence *filter = GST_REMOVE_SILENCE (object);
274 
275   switch (prop_id) {
276     case PROP_REMOVE:
277       filter->remove = g_value_get_boolean (value);
278       break;
279     case PROP_HYSTERESIS:
280       vad_set_hysteresis (filter->vad, g_value_get_uint64 (value));
281       break;
282     case PROP_THRESHOLD:
283       vad_set_threshold (filter->vad, g_value_get_int (value));
284       break;
285     case PROP_SQUASH:
286       filter->squash = g_value_get_boolean (value);
287       break;
288     case PROP_SILENT:
289       filter->silent = g_value_get_boolean (value);
290       break;
291     case PROP_MINIMUM_SILENCE_BUFFERS:
292       filter->minimum_silence_buffers = g_value_get_uint (value);
293       break;
294     case PROP_MINIMUM_SILENCE_TIME:
295       filter->minimum_silence_time = g_value_get_uint64 (value);
296       break;
297     default:
298       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299       break;
300   }
301 }
302 
303 static void
gst_remove_silence_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)304 gst_remove_silence_get_property (GObject * object, guint prop_id,
305     GValue * value, GParamSpec * pspec)
306 {
307   GstRemoveSilence *filter = GST_REMOVE_SILENCE (object);
308 
309   switch (prop_id) {
310     case PROP_REMOVE:
311       g_value_set_boolean (value, filter->remove);
312       break;
313     case PROP_HYSTERESIS:
314       g_value_set_uint64 (value, vad_get_hysteresis (filter->vad));
315       break;
316     case PROP_THRESHOLD:
317       g_value_set_int (value, vad_get_threshold_as_db (filter->vad));
318       break;
319     case PROP_SQUASH:
320       g_value_set_boolean (value, filter->squash);
321       break;
322     case PROP_SILENT:
323       g_value_set_boolean (value, filter->silent);
324       break;
325     case PROP_MINIMUM_SILENCE_BUFFERS:
326       g_value_set_uint (value, filter->minimum_silence_buffers);
327       break;
328     case PROP_MINIMUM_SILENCE_TIME:
329       g_value_set_uint64 (value, filter->minimum_silence_time);
330       break;
331     default:
332       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
333       break;
334   }
335 }
336 
337 static GstFlowReturn
gst_remove_silence_transform_ip(GstBaseTransform * trans,GstBuffer * inbuf)338 gst_remove_silence_transform_ip (GstBaseTransform * trans, GstBuffer * inbuf)
339 {
340   GstRemoveSilence *filter = NULL;
341   int frame_type;
342   GstMapInfo map;
343   gboolean consecutive_silence_reached;
344 
345   filter = GST_REMOVE_SILENCE (trans);
346 
347   gst_buffer_map (inbuf, &map, GST_MAP_READ);
348   frame_type =
349       vad_update (filter->vad, (gint16 *) map.data, map.size / sizeof (gint16));
350   gst_buffer_unmap (inbuf, &map);
351 
352   if (frame_type == VAD_SILENCE) {
353     GST_DEBUG ("Silence detected");
354     filter->consecutive_silence_buffers++;
355     if (GST_BUFFER_DURATION_IS_VALID (inbuf)) {
356       filter->consecutive_silence_time += inbuf->duration;
357     } else {
358       GST_WARNING
359           ("Invalid buffer duration, consecutive_silence_time update not possible");
360     }
361     if (filter->minimum_silence_buffers == 0
362         && filter->minimum_silence_time == 0) {
363       consecutive_silence_reached = TRUE;
364     } else {
365       consecutive_silence_reached =
366           (filter->minimum_silence_buffers > 0
367           && filter->consecutive_silence_buffers >=
368           filter->minimum_silence_buffers)
369           || (filter->minimum_silence_time > 0
370           && filter->consecutive_silence_time >= filter->minimum_silence_time);
371     }
372     if (!filter->silence_detected && consecutive_silence_reached) {
373       if (!filter->silent) {
374         if (GST_BUFFER_PTS_IS_VALID (inbuf)) {
375           GstStructure *s;
376           GstMessage *m;
377           s = gst_structure_new ("removesilence", "silence_detected",
378               G_TYPE_UINT64, GST_BUFFER_PTS (inbuf) - filter->ts_offset, NULL);
379           m = gst_message_new_element (GST_OBJECT (filter), s);
380           gst_element_post_message (GST_ELEMENT (filter), m);
381         }
382       }
383       filter->silence_detected = TRUE;
384     }
385 
386     if (filter->remove && consecutive_silence_reached) {
387       GST_DEBUG ("Removing silence");
388       if (filter->squash) {
389         if (GST_BUFFER_DURATION_IS_VALID (inbuf)) {
390           filter->ts_offset += inbuf->duration;
391         } else {
392           GST_WARNING ("Invalid buffer duration: ts_offset not updated");
393         }
394       }
395       return GST_BASE_TRANSFORM_FLOW_DROPPED;
396     }
397 
398   } else {
399     filter->consecutive_silence_buffers = 0;
400     filter->consecutive_silence_time = 0;
401     if (filter->silence_detected) {
402       if (!filter->silent) {
403         if (GST_BUFFER_PTS_IS_VALID (inbuf)) {
404           GstStructure *s;
405           GstMessage *m;
406           s = gst_structure_new ("removesilence", "silence_finished",
407               G_TYPE_UINT64, GST_BUFFER_PTS (inbuf) - filter->ts_offset, NULL);
408           m = gst_message_new_element (GST_OBJECT (filter), s);
409           gst_element_post_message (GST_ELEMENT (filter), m);
410         }
411       }
412       filter->silence_detected = FALSE;
413     }
414   }
415 
416   if (filter->squash && filter->ts_offset > 0) {
417     if (GST_BUFFER_PTS_IS_VALID (inbuf)) {
418       inbuf = gst_buffer_make_writable (inbuf);
419       GST_BUFFER_PTS (inbuf) -= filter->ts_offset;
420     } else {
421       GST_WARNING ("Invalid buffer pts, update not possible");
422     }
423   }
424 
425   return GST_FLOW_OK;
426 }
427 
428 /*Plugin init functions*/
429 static gboolean
plugin_init(GstPlugin * plugin)430 plugin_init (GstPlugin * plugin)
431 {
432   return GST_ELEMENT_REGISTER (removesilence, plugin);
433 }
434 
435 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
436     GST_VERSION_MINOR,
437     removesilence,
438     "Removes silence from an audio stream",
439     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
440