Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / gst / siren / gstsirenenc.c
1 /*
2  * Siren Encoder 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-sirenenc
24  *
25  * This encodes audio buffers into 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 "gstsirenenc.h"
37
38 #include <string.h>
39
40 GST_DEBUG_CATEGORY (sirenenc_debug);
41 #define GST_CAT_DEFAULT (sirenenc_debug)
42
43 #define FRAME_DURATION  (20 * GST_MSECOND)
44
45 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
46     GST_PAD_SRC,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("audio/x-siren, " "dct-length = (int) 320"));
49
50 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
51     GST_PAD_SINK,
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
73
74 static void gst_siren_enc_finalize (GObject * object);
75
76 static gboolean gst_siren_enc_sink_setcaps (GstPad * pad, GstCaps * caps);
77 static gboolean gst_siren_enc_sink_event (GstPad * pad, GstEvent * event);
78
79 static GstFlowReturn gst_siren_enc_chain (GstPad * pad, GstBuffer * buf);
80 static GstStateChangeReturn
81 gst_siren_change_state (GstElement * element, GstStateChange transition);
82
83
84 static void
85 _do_init (GType type)
86 {
87   GST_DEBUG_CATEGORY_INIT (sirenenc_debug, "sirenenc", 0, "sirenenc");
88 }
89
90 GST_BOILERPLATE_FULL (GstSirenEnc, gst_siren_enc, GstElement,
91     GST_TYPE_ELEMENT, _do_init);
92
93 static void
94 gst_siren_enc_base_init (gpointer klass)
95 {
96   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
97
98   gst_element_class_add_static_pad_template (element_class, &srctemplate);
99   gst_element_class_add_static_pad_template (element_class, &sinktemplate);
100
101   gst_element_class_set_details_simple (element_class, "Siren Encoder element",
102       "Codec/Encoder/Audio ",
103       "Encode 16bit PCM streams into the Siren7 codec",
104       "Youness Alaoui <kakaroto@kakaroto.homelinux.net>");
105 }
106
107 static void
108 gst_siren_enc_class_init (GstSirenEncClass * klass)
109 {
110   GObjectClass *gobject_class;
111   GstElementClass *gstelement_class;
112
113   gobject_class = (GObjectClass *) klass;
114   gstelement_class = (GstElementClass *) klass;
115
116   GST_DEBUG ("Initializing Class");
117
118   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_siren_enc_finalize);
119
120   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_siren_change_state);
121
122   GST_DEBUG ("Class Init done");
123 }
124
125 static void
126 gst_siren_enc_init (GstSirenEnc * enc, GstSirenEncClass * klass)
127 {
128
129   GST_DEBUG_OBJECT (enc, "Initializing");
130   enc->encoder = Siren7_NewEncoder (16000);
131   enc->adapter = gst_adapter_new ();
132
133   enc->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
134   enc->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
135
136   gst_pad_set_setcaps_function (enc->sinkpad,
137       GST_DEBUG_FUNCPTR (gst_siren_enc_sink_setcaps));
138   gst_pad_set_event_function (enc->sinkpad,
139       GST_DEBUG_FUNCPTR (gst_siren_enc_sink_event));
140   gst_pad_set_chain_function (enc->sinkpad,
141       GST_DEBUG_FUNCPTR (gst_siren_enc_chain));
142
143   gst_element_add_pad (GST_ELEMENT (enc), enc->sinkpad);
144   gst_element_add_pad (GST_ELEMENT (enc), enc->srcpad);
145
146   GST_DEBUG_OBJECT (enc, "Init done");
147 }
148
149 static void
150 gst_siren_enc_finalize (GObject * object)
151 {
152   GstSirenEnc *enc = GST_SIREN_ENC (object);
153
154   GST_DEBUG_OBJECT (object, "Disposing");
155
156   Siren7_CloseEncoder (enc->encoder);
157   g_object_unref (enc->adapter);
158
159   G_OBJECT_CLASS (parent_class)->finalize (object);
160 }
161
162 static gboolean
163 gst_siren_enc_sink_setcaps (GstPad * pad, GstCaps * caps)
164 {
165   GstSirenEnc *enc;
166   gboolean res;
167   GstCaps *outcaps;
168
169   enc = GST_SIREN_ENC (GST_PAD_PARENT (pad));
170
171   outcaps = gst_static_pad_template_get_caps (&srctemplate);
172   res = gst_pad_set_caps (enc->srcpad, outcaps);
173   gst_caps_unref (outcaps);
174
175   return res;
176 }
177
178 static gboolean
179 gst_siren_enc_sink_event (GstPad * pad, GstEvent * event)
180 {
181   GstSirenEnc *enc;
182   gboolean res;
183
184   enc = GST_SIREN_ENC (GST_PAD_PARENT (pad));
185
186   switch (GST_EVENT_TYPE (event)) {
187     case GST_EVENT_EOS:
188       gst_adapter_clear (enc->adapter);
189       res = gst_pad_push_event (enc->srcpad, event);
190       break;
191     case GST_EVENT_FLUSH_STOP:
192       gst_adapter_clear (enc->adapter);
193       res = gst_pad_push_event (enc->srcpad, event);
194       break;
195     default:
196       res = gst_pad_push_event (enc->srcpad, event);
197       break;
198   }
199   return res;
200 }
201
202 static GstFlowReturn
203 gst_siren_enc_chain (GstPad * pad, GstBuffer * buf)
204 {
205   GstSirenEnc *enc;
206   GstFlowReturn ret = GST_FLOW_OK;
207   GstBuffer *out_buf;
208   guint8 *in_data, *out_data;
209   guint8 *to_free = NULL;
210   guint i, size, num_frames;
211   gint out_size, in_size;
212   gint encode_ret;
213   gboolean discont;
214   GstClockTime timestamp;
215   guint64 distance;
216   GstCaps *outcaps;
217
218   enc = GST_SIREN_ENC (GST_PAD_PARENT (pad));
219
220   discont = GST_BUFFER_IS_DISCONT (buf);
221   if (discont) {
222     GST_DEBUG_OBJECT (enc, "received DISCONT, flush adapter");
223     gst_adapter_clear (enc->adapter);
224     enc->discont = TRUE;
225   }
226
227   gst_adapter_push (enc->adapter, buf);
228
229   size = gst_adapter_available (enc->adapter);
230
231   GST_LOG_OBJECT (enc, "Received buffer of size %d with adapter of size : %d",
232       GST_BUFFER_SIZE (buf), size);
233
234   /* we need to process 640 input bytes to produce 40 output bytes */
235   /* calculate the amount of frames we will handle */
236   num_frames = size / 640;
237
238   /* no frames, wait some more */
239   if (num_frames == 0)
240     goto done;
241
242   /* this is the input/output size */
243   in_size = num_frames * 640;
244   out_size = num_frames * 40;
245
246   GST_LOG_OBJECT (enc, "we have %u frames, %u in, %u out", num_frames, in_size,
247       out_size);
248
249   /* set output caps when needed */
250   if ((outcaps = GST_PAD_CAPS (enc->srcpad)) == NULL) {
251     outcaps = gst_static_pad_template_get_caps (&srctemplate);
252     gst_pad_set_caps (enc->srcpad, outcaps);
253     gst_caps_unref (outcaps);
254   }
255
256   /* get a buffer */
257   ret = gst_pad_alloc_buffer_and_set_caps (enc->srcpad, -1,
258       out_size, outcaps, &out_buf);
259   if (ret != GST_FLOW_OK)
260     goto alloc_failed;
261
262   /* get the timestamp for the output buffer */
263   timestamp = gst_adapter_prev_timestamp (enc->adapter, &distance);
264
265   /* add the amount of time taken by the distance */
266   if (timestamp != -1)
267     timestamp += gst_util_uint64_scale_int (distance / 2, GST_SECOND, 16000);
268
269   GST_LOG_OBJECT (enc,
270       "timestamp %" GST_TIME_FORMAT ", distance %" G_GUINT64_FORMAT,
271       GST_TIME_ARGS (timestamp), distance);
272
273   /* get the input data for all the frames */
274   to_free = in_data = gst_adapter_take (enc->adapter, in_size);
275   out_data = GST_BUFFER_DATA (out_buf);
276
277   for (i = 0; i < num_frames; i++) {
278     GST_LOG_OBJECT (enc, "Encoding frame %u/%u", i, num_frames);
279
280     /* encode 640 input bytes to 40 output bytes */
281     encode_ret = Siren7_EncodeFrame (enc->encoder, in_data, out_data);
282     if (encode_ret != 0)
283       goto encode_error;
284
285     /* move to next frame */
286     out_data += 40;
287     in_data += 640;
288   }
289
290   GST_LOG_OBJECT (enc, "Finished encoding");
291
292   /* mark discont */
293   if (enc->discont) {
294     GST_BUFFER_FLAG_SET (out_buf, GST_BUFFER_FLAG_DISCONT);
295     enc->discont = FALSE;
296   }
297   GST_BUFFER_TIMESTAMP (out_buf) = timestamp;
298   GST_BUFFER_DURATION (out_buf) = num_frames * FRAME_DURATION;
299
300   ret = gst_pad_push (enc->srcpad, out_buf);
301
302 done:
303   if (to_free)
304     g_free (to_free);
305
306   return ret;
307
308   /* ERRORS */
309 alloc_failed:
310   {
311     GST_DEBUG_OBJECT (enc, "failed to pad_alloc buffer: %d (%s)", ret,
312         gst_flow_get_name (ret));
313     goto done;
314   }
315 encode_error:
316   {
317     GST_ELEMENT_ERROR (enc, STREAM, ENCODE, (NULL),
318         ("Error encoding frame: %d", encode_ret));
319     ret = GST_FLOW_ERROR;
320     gst_buffer_unref (out_buf);
321     goto done;
322   }
323 }
324
325 static GstStateChangeReturn
326 gst_siren_change_state (GstElement * element, GstStateChange transition)
327 {
328   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
329   GstSirenEnc *enc = GST_SIREN_ENC (element);
330
331   switch (transition) {
332     case GST_STATE_CHANGE_READY_TO_PAUSED:
333       enc->discont = FALSE;
334       break;
335     default:
336       break;
337   }
338
339   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
340
341   switch (transition) {
342     case GST_STATE_CHANGE_PAUSED_TO_READY:
343       gst_adapter_clear (enc->adapter);
344       break;
345     default:
346       break;
347   }
348
349   return ret;
350 }
351
352 gboolean
353 gst_siren_enc_plugin_init (GstPlugin * plugin)
354 {
355   return gst_element_register (plugin, "sirenenc",
356       GST_RANK_MARGINAL, GST_TYPE_SIREN_ENC);
357 }