Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / gst / siren / gstsirendec.c
1 /*
2  * Siren Decoder Gst Element
3  *
4  *   @author: Youness Alaoui <kakaroto@kakaroto.homelinux.net>
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 /**
23  * SECTION:element-sirendec
24  *
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.
28  *
29  * Ref: http://www.polycom.com/company/about_us/technology/siren_g7221/index.html
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include "gstsirendec.h"
37
38 #include <string.h>
39
40 GST_DEBUG_CATEGORY (sirendec_debug);
41 #define GST_CAT_DEFAULT (sirendec_debug)
42
43 #define FRAME_DURATION  (20 * GST_MSECOND)
44
45 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("audio/x-siren, " "dct-length = (int) 320"));
49
50 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS ("audio/x-raw-int, "
54         "width = (int) 16, "
55         "depth = (int) 16, "
56         "endianness = (int) 1234, "
57         "signed = (boolean) true, "
58         "rate = (int) 16000, " "channels = (int) 1"));
59
60 /* signals and args */
61 enum
62 {
63   /* FILL ME */
64   LAST_SIGNAL
65 };
66
67 enum
68 {
69   ARG_0,
70 };
71
72 static void gst_siren_dec_finalize (GObject * object);
73
74 static GstStateChangeReturn
75 gst_siren_change_state (GstElement * element, GstStateChange transition);
76
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);
80
81 static void
82 _do_init (GType type)
83 {
84   GST_DEBUG_CATEGORY_INIT (sirendec_debug, "sirendec", 0, "sirendec");
85 }
86
87 GST_BOILERPLATE_FULL (GstSirenDec, gst_siren_dec, GstElement,
88     GST_TYPE_ELEMENT, _do_init);
89
90 static void
91 gst_siren_dec_base_init (gpointer klass)
92 {
93   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
94
95   gst_element_class_add_static_pad_template (element_class, &srctemplate);
96   gst_element_class_add_static_pad_template (element_class, &sinktemplate);
97
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>");
102 }
103
104 static void
105 gst_siren_dec_class_init (GstSirenDecClass * klass)
106 {
107   GObjectClass *gobject_class;
108   GstElementClass *gstelement_class;
109
110   gobject_class = (GObjectClass *) klass;
111   gstelement_class = (GstElementClass *) klass;
112
113   GST_DEBUG ("Initializing Class");
114
115   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_siren_dec_finalize);
116
117   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_siren_change_state);
118
119   GST_DEBUG ("Class Init done");
120 }
121
122 static void
123 gst_siren_dec_init (GstSirenDec * dec, GstSirenDecClass * klass)
124 {
125
126   GST_DEBUG_OBJECT (dec, "Initializing");
127   dec->decoder = Siren7_NewDecoder (16000);;
128
129   dec->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
130   dec->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
131
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));
138
139   gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
140   gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
141
142   dec->adapter = gst_adapter_new ();
143
144   GST_DEBUG_OBJECT (dec, "Init done");
145 }
146
147 static void
148 gst_siren_dec_finalize (GObject * object)
149 {
150   GstSirenDec *dec = GST_SIREN_DEC (object);
151
152   GST_DEBUG_OBJECT (dec, "Finalize");
153
154   Siren7_CloseDecoder (dec->decoder);
155   g_object_unref (dec->adapter);
156
157   G_OBJECT_CLASS (parent_class)->finalize (object);
158 }
159
160 static gboolean
161 gst_siren_dec_sink_setcaps (GstPad * pad, GstCaps * caps)
162 {
163   GstSirenDec *dec;
164   gboolean res;
165   GstCaps *outcaps;
166
167   dec = GST_SIREN_DEC (GST_PAD_PARENT (pad));
168
169   outcaps = gst_static_pad_template_get_caps (&srctemplate);
170   res = gst_pad_set_caps (dec->srcpad, outcaps);
171   gst_caps_unref (outcaps);
172
173   return res;
174 }
175
176 static gboolean
177 gst_siren_dec_sink_event (GstPad * pad, GstEvent * event)
178 {
179   GstSirenDec *dec;
180   gboolean res;
181
182   dec = GST_SIREN_DEC (GST_PAD_PARENT (pad));
183
184   switch (GST_EVENT_TYPE (event)) {
185     case GST_EVENT_EOS:
186       gst_adapter_clear (dec->adapter);
187       res = gst_pad_push_event (dec->srcpad, event);
188       break;
189     case GST_EVENT_FLUSH_STOP:
190       gst_adapter_clear (dec->adapter);
191       res = gst_pad_push_event (dec->srcpad, event);
192       break;
193     default:
194       res = gst_pad_push_event (dec->srcpad, event);
195       break;
196   }
197   return res;
198 }
199
200 static GstFlowReturn
201 gst_siren_dec_chain (GstPad * pad, GstBuffer * buf)
202 {
203   GstSirenDec *dec;
204   GstFlowReturn ret = GST_FLOW_OK;
205   GstBuffer *out_buf;
206   guint8 *in_data, *out_data;
207   guint8 *to_free = NULL;
208   guint i, size, num_frames;
209   gint out_size, in_size;
210   gint decode_ret;
211   gboolean discont;
212   GstClockTime timestamp;
213   guint64 distance;
214   GstCaps *outcaps;
215
216   dec = GST_SIREN_DEC (GST_PAD_PARENT (pad));
217
218   discont = GST_BUFFER_IS_DISCONT (buf);
219   if (discont) {
220     GST_DEBUG_OBJECT (dec, "received DISCONT, flush adapter");
221     gst_adapter_clear (dec->adapter);
222     dec->discont = TRUE;
223   }
224
225   gst_adapter_push (dec->adapter, buf);
226
227   size = gst_adapter_available (dec->adapter);
228
229   GST_LOG_OBJECT (dec, "Received buffer of size %u with adapter of size : %u",
230       GST_BUFFER_SIZE (buf), size);
231
232   /* process 40 input bytes into 640 output bytes */
233   num_frames = size / 40;
234
235   if (num_frames == 0)
236     goto done;
237
238   /* this is the input/output size */
239   in_size = num_frames * 40;
240   out_size = num_frames * 640;
241
242   GST_LOG_OBJECT (dec, "we have %u frames, %u in, %u out", num_frames, in_size,
243       out_size);
244
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);
250   }
251
252   /* get a buffer */
253   ret = gst_pad_alloc_buffer_and_set_caps (dec->srcpad, -1,
254       out_size, outcaps, &out_buf);
255   if (ret != GST_FLOW_OK)
256     goto alloc_failed;
257
258   /* get the timestamp for the output buffer */
259   timestamp = gst_adapter_prev_timestamp (dec->adapter, &distance);
260
261   /* add the amount of time taken by the distance, each frame is 20ms */
262   if (timestamp != -1)
263     timestamp += (distance / 40) * FRAME_DURATION;
264
265   GST_LOG_OBJECT (dec,
266       "timestamp %" GST_TIME_FORMAT ", distance %" G_GUINT64_FORMAT,
267       GST_TIME_ARGS (timestamp), distance);
268
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);
272
273   for (i = 0; i < num_frames; i++) {
274     GST_LOG_OBJECT (dec, "Decoding frame %u/%u", i, num_frames);
275
276     /* decode 40 input bytes to 640 output bytes */
277     decode_ret = Siren7_DecodeFrame (dec->decoder, in_data, out_data);
278     if (decode_ret != 0)
279       goto decode_error;
280
281     /* move to next frame */
282     out_data += 640;
283     in_data += 40;
284   }
285
286   GST_LOG_OBJECT (dec, "Finished decoding");
287
288   /* mark discont */
289   if (dec->discont) {
290     GST_BUFFER_FLAG_SET (out_buf, GST_BUFFER_FLAG_DISCONT);
291     dec->discont = FALSE;
292   }
293
294   GST_BUFFER_TIMESTAMP (out_buf) = timestamp;
295   GST_BUFFER_DURATION (out_buf) = num_frames * FRAME_DURATION;
296
297   ret = gst_pad_push (dec->srcpad, out_buf);
298
299 done:
300   if (to_free)
301     g_free (to_free);
302
303   return ret;
304
305   /* ERRORS */
306 alloc_failed:
307   {
308     GST_DEBUG_OBJECT (dec, "failed to pad_alloc buffer: %d (%s)", ret,
309         gst_flow_get_name (ret));
310     goto done;
311   }
312 decode_error:
313   {
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);
318     goto done;
319   }
320 }
321
322 static GstStateChangeReturn
323 gst_siren_change_state (GstElement * element, GstStateChange transition)
324 {
325   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
326   GstSirenDec *dec = GST_SIREN_DEC (element);
327
328   switch (transition) {
329     case GST_STATE_CHANGE_READY_TO_PAUSED:
330       dec->discont = FALSE;
331       break;
332     default:
333       break;
334   }
335
336   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
337
338   switch (transition) {
339     case GST_STATE_CHANGE_PAUSED_TO_READY:
340       gst_adapter_clear (dec->adapter);
341       break;
342     default:
343       break;
344   }
345
346   return ret;
347 }
348
349 gboolean
350 gst_siren_dec_plugin_init (GstPlugin * plugin)
351 {
352   return gst_element_register (plugin, "sirendec",
353       GST_RANK_MARGINAL, GST_TYPE_SIREN_DEC);
354 }