2 * Siren Decoder Gst Element
4 * @author: Youness Alaoui <kakaroto@kakaroto.homelinux.net>
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.
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.
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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-sirendec
25 * This decodes audio buffers from the Siren 16 codec (a 16khz extension of
26 * G.722.1) that is meant to be compatible with the Microsoft Windows Live
27 * Messenger(tm) implementation.
29 * Ref: http://www.polycom.com/company/about_us/technology/siren_g7221/index.html
36 #include "gstsirendec.h"
40 GST_DEBUG_CATEGORY (sirendec_debug);
41 #define GST_CAT_DEFAULT (sirendec_debug)
43 #define FRAME_DURATION (20 * GST_MSECOND)
45 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
48 GST_STATIC_CAPS ("audio/x-siren, " "dct-length = (int) 320"));
50 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
53 GST_STATIC_CAPS ("audio/x-raw-int, "
56 "endianness = (int) 1234, "
57 "signed = (boolean) true, "
58 "rate = (int) 16000, " "channels = (int) 1"));
60 /* signals and args */
72 static void gst_siren_dec_finalize (GObject * object);
74 static GstStateChangeReturn
75 gst_siren_change_state (GstElement * element, GstStateChange transition);
77 static gboolean gst_siren_dec_sink_setcaps (GstPad * pad, GstCaps * caps);
78 static gboolean gst_siren_dec_sink_event (GstPad * pad, GstEvent * event);
79 static GstFlowReturn gst_siren_dec_chain (GstPad * pad, GstBuffer * buf);
84 GST_DEBUG_CATEGORY_INIT (sirendec_debug, "sirendec", 0, "sirendec");
87 GST_BOILERPLATE_FULL (GstSirenDec, gst_siren_dec, GstElement,
88 GST_TYPE_ELEMENT, _do_init);
91 gst_siren_dec_base_init (gpointer klass)
93 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
95 gst_element_class_add_static_pad_template (element_class, &srctemplate);
96 gst_element_class_add_static_pad_template (element_class, &sinktemplate);
98 gst_element_class_set_details_simple (element_class, "Siren Decoder element",
99 "Codec/Decoder/Audio ",
100 "Decode streams encoded with the Siren7 codec into 16bit PCM",
101 "Youness Alaoui <kakaroto@kakaroto.homelinux.net>");
105 gst_siren_dec_class_init (GstSirenDecClass * klass)
107 GObjectClass *gobject_class;
108 GstElementClass *gstelement_class;
110 gobject_class = (GObjectClass *) klass;
111 gstelement_class = (GstElementClass *) klass;
113 GST_DEBUG ("Initializing Class");
115 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_siren_dec_finalize);
117 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_siren_change_state);
119 GST_DEBUG ("Class Init done");
123 gst_siren_dec_init (GstSirenDec * dec, GstSirenDecClass * klass)
126 GST_DEBUG_OBJECT (dec, "Initializing");
127 dec->decoder = Siren7_NewDecoder (16000);;
129 dec->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
130 dec->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
132 gst_pad_set_setcaps_function (dec->sinkpad,
133 GST_DEBUG_FUNCPTR (gst_siren_dec_sink_setcaps));
134 gst_pad_set_event_function (dec->sinkpad,
135 GST_DEBUG_FUNCPTR (gst_siren_dec_sink_event));
136 gst_pad_set_chain_function (dec->sinkpad,
137 GST_DEBUG_FUNCPTR (gst_siren_dec_chain));
139 gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
140 gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
142 dec->adapter = gst_adapter_new ();
144 GST_DEBUG_OBJECT (dec, "Init done");
148 gst_siren_dec_finalize (GObject * object)
150 GstSirenDec *dec = GST_SIREN_DEC (object);
152 GST_DEBUG_OBJECT (dec, "Finalize");
154 Siren7_CloseDecoder (dec->decoder);
155 g_object_unref (dec->adapter);
157 G_OBJECT_CLASS (parent_class)->finalize (object);
161 gst_siren_dec_sink_setcaps (GstPad * pad, GstCaps * caps)
167 dec = GST_SIREN_DEC (GST_PAD_PARENT (pad));
169 outcaps = gst_static_pad_template_get_caps (&srctemplate);
170 res = gst_pad_set_caps (dec->srcpad, outcaps);
171 gst_caps_unref (outcaps);
177 gst_siren_dec_sink_event (GstPad * pad, GstEvent * event)
182 dec = GST_SIREN_DEC (GST_PAD_PARENT (pad));
184 switch (GST_EVENT_TYPE (event)) {
186 gst_adapter_clear (dec->adapter);
187 res = gst_pad_push_event (dec->srcpad, event);
189 case GST_EVENT_FLUSH_STOP:
190 gst_adapter_clear (dec->adapter);
191 res = gst_pad_push_event (dec->srcpad, event);
194 res = gst_pad_push_event (dec->srcpad, event);
201 gst_siren_dec_chain (GstPad * pad, GstBuffer * buf)
204 GstFlowReturn ret = GST_FLOW_OK;
206 guint8 *in_data, *out_data;
207 guint8 *to_free = NULL;
208 guint i, size, num_frames;
209 gint out_size, in_size;
212 GstClockTime timestamp;
216 dec = GST_SIREN_DEC (GST_PAD_PARENT (pad));
218 discont = GST_BUFFER_IS_DISCONT (buf);
220 GST_DEBUG_OBJECT (dec, "received DISCONT, flush adapter");
221 gst_adapter_clear (dec->adapter);
225 gst_adapter_push (dec->adapter, buf);
227 size = gst_adapter_available (dec->adapter);
229 GST_LOG_OBJECT (dec, "Received buffer of size %u with adapter of size : %u",
230 GST_BUFFER_SIZE (buf), size);
232 /* process 40 input bytes into 640 output bytes */
233 num_frames = size / 40;
238 /* this is the input/output size */
239 in_size = num_frames * 40;
240 out_size = num_frames * 640;
242 GST_LOG_OBJECT (dec, "we have %u frames, %u in, %u out", num_frames, in_size,
245 /* set output caps when needed */
246 if ((outcaps = GST_PAD_CAPS (dec->srcpad)) == NULL) {
247 outcaps = gst_static_pad_template_get_caps (&srctemplate);
248 gst_pad_set_caps (dec->srcpad, outcaps);
249 gst_caps_unref (outcaps);
253 ret = gst_pad_alloc_buffer_and_set_caps (dec->srcpad, -1,
254 out_size, outcaps, &out_buf);
255 if (ret != GST_FLOW_OK)
258 /* get the timestamp for the output buffer */
259 timestamp = gst_adapter_prev_timestamp (dec->adapter, &distance);
261 /* add the amount of time taken by the distance, each frame is 20ms */
263 timestamp += (distance / 40) * FRAME_DURATION;
266 "timestamp %" GST_TIME_FORMAT ", distance %" G_GUINT64_FORMAT,
267 GST_TIME_ARGS (timestamp), distance);
269 /* get the input data for all the frames */
270 to_free = in_data = gst_adapter_take (dec->adapter, in_size);
271 out_data = GST_BUFFER_DATA (out_buf);
273 for (i = 0; i < num_frames; i++) {
274 GST_LOG_OBJECT (dec, "Decoding frame %u/%u", i, num_frames);
276 /* decode 40 input bytes to 640 output bytes */
277 decode_ret = Siren7_DecodeFrame (dec->decoder, in_data, out_data);
281 /* move to next frame */
286 GST_LOG_OBJECT (dec, "Finished decoding");
290 GST_BUFFER_FLAG_SET (out_buf, GST_BUFFER_FLAG_DISCONT);
291 dec->discont = FALSE;
294 GST_BUFFER_TIMESTAMP (out_buf) = timestamp;
295 GST_BUFFER_DURATION (out_buf) = num_frames * FRAME_DURATION;
297 ret = gst_pad_push (dec->srcpad, out_buf);
308 GST_DEBUG_OBJECT (dec, "failed to pad_alloc buffer: %d (%s)", ret,
309 gst_flow_get_name (ret));
314 GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL),
315 ("Error decoding frame: %d", decode_ret));
316 ret = GST_FLOW_ERROR;
317 gst_buffer_unref (out_buf);
322 static GstStateChangeReturn
323 gst_siren_change_state (GstElement * element, GstStateChange transition)
325 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
326 GstSirenDec *dec = GST_SIREN_DEC (element);
328 switch (transition) {
329 case GST_STATE_CHANGE_READY_TO_PAUSED:
330 dec->discont = FALSE;
336 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
338 switch (transition) {
339 case GST_STATE_CHANGE_PAUSED_TO_READY:
340 gst_adapter_clear (dec->adapter);
350 gst_siren_dec_plugin_init (GstPlugin * plugin)
352 return gst_element_register (plugin, "sirendec",
353 GST_RANK_MARGINAL, GST_TYPE_SIREN_DEC);