a51038edffdbe6a98cb1755c220b0e209b7c3f94
[platform/upstream/gstreamer.git] / gst / removesilence / gstremovesilence.c
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  *
28  * ## Example launch line
29  * |[
30  * gst-launch-1.0 -v -m filesrc location="audiofile" ! decodebin ! removesilence remove=true ! wavenc ! filesink location=without_audio.wav
31  * ]|
32  *
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <gst/gst.h>
40 #include <gst/base/gstbasetransform.h>
41 #include <gst/audio/audio.h>
42
43 #include "gstremovesilence.h"
44
45
46 GST_DEBUG_CATEGORY_STATIC (gst_remove_silence_debug);
47 #define GST_CAT_DEFAULT gst_remove_silence_debug
48 #define DEFAULT_VAD_HYSTERESIS  480     /* 60 mseg */
49
50 /* Filter signals and args */
51 enum
52 {
53   /* FILL ME */
54   LAST_SIGNAL
55 };
56
57 enum
58 {
59   PROP_0,
60   PROP_REMOVE,
61   PROP_HYSTERESIS,
62   PROP_SQUASH
63 };
64
65
66 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
67     GST_PAD_SINK,
68     GST_PAD_ALWAYS,
69     GST_STATIC_CAPS ("audio/x-raw, "
70         "format = (string) " GST_AUDIO_NE (S16) ", "
71         "layout = (string) interleaved, "
72         "rate = (int) [ 1, MAX ], " "channels = (int) 1"));
73
74 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
75     GST_PAD_SRC,
76     GST_PAD_ALWAYS,
77     GST_STATIC_CAPS ("audio/x-raw, "
78         "format = (string) " GST_AUDIO_NE (S16) ", "
79         "layout = (string) interleaved, "
80         "rate = (int) [ 1, MAX ], " "channels = (int) 1"));
81
82
83 #define DEBUG_INIT(bla) \
84   GST_DEBUG_CATEGORY_INIT (gst_remove_silence_debug, "removesilence", 0, "removesilence element")
85
86 #define gst_remove_silence_parent_class parent_class
87 G_DEFINE_TYPE_WITH_CODE (GstRemoveSilence, gst_remove_silence,
88     GST_TYPE_BASE_TRANSFORM, DEBUG_INIT (0));
89
90 static void gst_remove_silence_set_property (GObject * object, guint prop_id,
91     const GValue * value, GParamSpec * pspec);
92 static void gst_remove_silence_get_property (GObject * object, guint prop_id,
93     GValue * value, GParamSpec * pspec);
94
95 static GstFlowReturn gst_remove_silence_transform_ip (GstBaseTransform * base,
96     GstBuffer * buf);
97 static void gst_remove_silence_finalize (GObject * obj);
98
99 /* GObject vmethod implementations */
100
101 /* initialize the removesilence's class */
102 static void
103 gst_remove_silence_class_init (GstRemoveSilenceClass * klass)
104 {
105   GObjectClass *gobject_class;
106   GstElementClass *gstelement_class;
107
108   gobject_class = (GObjectClass *) klass;
109   gstelement_class = (GstElementClass *) klass;
110
111   gobject_class->finalize = gst_remove_silence_finalize;
112   gobject_class->set_property = gst_remove_silence_set_property;
113   gobject_class->get_property = gst_remove_silence_get_property;
114
115   g_object_class_install_property (gobject_class, PROP_REMOVE,
116       g_param_spec_boolean ("remove", "Remove",
117           "Set to true to remove silence from the stream, false otherwhise",
118           FALSE, G_PARAM_READWRITE));
119
120   g_object_class_install_property (gobject_class, PROP_HYSTERESIS,
121       g_param_spec_uint64 ("hysteresis",
122           "Hysteresis",
123           "Set the hysteresis (on samples) used on the internal VAD",
124           1, G_MAXUINT64, DEFAULT_VAD_HYSTERESIS, G_PARAM_READWRITE));
125
126   g_object_class_install_property (gobject_class, PROP_SQUASH,
127       g_param_spec_boolean ("squash", "Squash",
128           "Set to true to retimestamp buffers when silence is removed and so avoid timestamp gap",
129           FALSE, G_PARAM_READWRITE));
130
131   gst_element_class_set_static_metadata (gstelement_class,
132       "RemoveSilence",
133       "Filter/Effect/Audio",
134       "Removes all the silence periods from the audio stream.",
135       "Tiago Katcipis <tiagokatcipis@gmail.com>\n \
136        Paulo Pizarro  <paulo.pizarro@gmail.com>\n \
137        Nicola Murino  <nicola.murino@gmail.com>");
138
139   gst_element_class_add_static_pad_template (gstelement_class, &src_template);
140   gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
141
142   GST_BASE_TRANSFORM_CLASS (klass)->transform_ip =
143       GST_DEBUG_FUNCPTR (gst_remove_silence_transform_ip);
144 }
145
146 /* initialize the new element
147  * instantiate pads and add them to element
148  * set pad calback functions
149  * initialize instance structure
150  */
151 static void
152 gst_remove_silence_init (GstRemoveSilence * filter)
153 {
154   filter->vad = vad_new (DEFAULT_VAD_HYSTERESIS);
155   filter->remove = FALSE;
156   filter->squash = FALSE;
157   filter->ts_offset = 0;
158   filter->silence_detected = FALSE;
159
160   if (!filter->vad) {
161     GST_DEBUG ("Error initializing VAD !!");
162     return;
163   }
164 }
165
166 static void
167 gst_remove_silence_finalize (GObject * obj)
168 {
169   GstRemoveSilence *filter = GST_REMOVE_SILENCE (obj);
170   GST_DEBUG ("Destroying VAD");
171   vad_destroy (filter->vad);
172   filter->vad = NULL;
173   GST_DEBUG ("VAD Destroyed");
174   G_OBJECT_CLASS (parent_class)->finalize (obj);
175 }
176
177 static void
178 gst_remove_silence_set_property (GObject * object, guint prop_id,
179     const GValue * value, GParamSpec * pspec)
180 {
181   GstRemoveSilence *filter = GST_REMOVE_SILENCE (object);
182
183   switch (prop_id) {
184     case PROP_REMOVE:
185       filter->remove = g_value_get_boolean (value);
186       break;
187     case PROP_HYSTERESIS:
188       vad_set_hysteresis (filter->vad, g_value_get_uint64 (value));
189       break;
190     case PROP_SQUASH:
191       filter->squash = g_value_get_boolean (value);
192       break;
193     default:
194       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
195       break;
196   }
197 }
198
199 static void
200 gst_remove_silence_get_property (GObject * object, guint prop_id,
201     GValue * value, GParamSpec * pspec)
202 {
203   GstRemoveSilence *filter = GST_REMOVE_SILENCE (object);
204
205   switch (prop_id) {
206     case PROP_REMOVE:
207       g_value_set_boolean (value, filter->remove);
208       break;
209     case PROP_HYSTERESIS:
210       g_value_set_uint64 (value, vad_get_hysteresis (filter->vad));
211       break;
212     case PROP_SQUASH:
213       g_value_set_boolean (value, filter->squash);
214       break;
215     default:
216       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217       break;
218   }
219 }
220
221 static GstFlowReturn
222 gst_remove_silence_transform_ip (GstBaseTransform * trans, GstBuffer * inbuf)
223 {
224   GstRemoveSilence *filter = NULL;
225   int frame_type;
226   GstMapInfo map;
227
228   filter = GST_REMOVE_SILENCE (trans);
229
230   gst_buffer_map (inbuf, &map, GST_MAP_READ);
231   frame_type =
232       vad_update (filter->vad, (gint16 *) map.data, map.size / sizeof (gint16));
233   gst_buffer_unmap (inbuf, &map);
234
235   if (frame_type == VAD_SILENCE) {
236     GST_DEBUG ("Silence detected");
237     if (!filter->silence_detected) {
238       if (GST_BUFFER_PTS_IS_VALID (inbuf)) {
239         GstStructure *s;
240         GstMessage *m;
241         s = gst_structure_new ("removesilence", "silence_detected",
242             G_TYPE_UINT64, GST_BUFFER_PTS (inbuf) - filter->ts_offset, NULL);
243         m = gst_message_new_element (GST_OBJECT (filter), s);
244         gst_element_post_message (GST_ELEMENT (filter), m);
245       }
246       filter->silence_detected = TRUE;
247     }
248
249     if (filter->remove) {
250       GST_DEBUG ("Removing silence");
251       if (filter->squash) {
252         if (GST_BUFFER_DURATION_IS_VALID (inbuf)) {
253           filter->ts_offset += inbuf->duration;
254         } else {
255           GST_WARNING ("Invalid buffer duration: ts_offset not updated");
256         }
257       }
258       return GST_BASE_TRANSFORM_FLOW_DROPPED;
259     }
260
261   } else {
262     if (filter->silence_detected) {
263       if (GST_BUFFER_PTS_IS_VALID (inbuf)) {
264         GstStructure *s;
265         GstMessage *m;
266         s = gst_structure_new ("removesilence", "silence_finished",
267             G_TYPE_UINT64, GST_BUFFER_PTS (inbuf) - filter->ts_offset, NULL);
268         m = gst_message_new_element (GST_OBJECT (filter), s);
269         gst_element_post_message (GST_ELEMENT (filter), m);
270       }
271       filter->silence_detected = FALSE;
272     }
273   }
274
275   if (filter->squash && filter->ts_offset > 0) {
276     if (GST_BUFFER_PTS_IS_VALID (inbuf)) {
277       inbuf = gst_buffer_make_writable (inbuf);
278       GST_BUFFER_PTS (inbuf) -= filter->ts_offset;
279     } else {
280       GST_WARNING ("Invalid buffer pts, update not possibile");
281     }
282   }
283
284   return GST_FLOW_OK;
285 }
286
287 /*Plugin init functions*/
288 static gboolean
289 plugin_init (GstPlugin * plugin)
290 {
291   return gst_element_register (plugin, "removesilence", GST_RANK_NONE,
292       gst_remove_silence_get_type ());
293 }
294
295 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
296     GST_VERSION_MINOR,
297     removesilence,
298     "Removes silence from an audio stream",
299     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);