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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22 /**
23 * SECTION:element-sirendec
24 * @title: sirendec
25 *
26 * This decodes audio buffers from the Siren 16 codec (a 16khz extension of
27 * G.722.1) that is meant to be compatible with the Microsoft Windows Live
28 * Messenger(tm) implementation.
29 *
30 * Ref: http://www.polycom.com/company/about_us/technology/siren_g7221/index.html
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include "gstsirendec.h"
38
39 #include <string.h>
40
41 GST_DEBUG_CATEGORY (sirendec_debug);
42 #define GST_CAT_DEFAULT (sirendec_debug)
43
44 #define FRAME_DURATION (20 * GST_MSECOND)
45
46 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
47 GST_PAD_SINK,
48 GST_PAD_ALWAYS,
49 GST_STATIC_CAPS ("audio/x-siren, " "dct-length = (int) 320"));
50
51 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
52 GST_PAD_SRC,
53 GST_PAD_ALWAYS,
54 GST_STATIC_CAPS ("audio/x-raw, format = (string) \"S16LE\", "
55 "rate = (int) 16000, " "channels = (int) 1"));
56
57 static gboolean gst_siren_dec_start (GstAudioDecoder * dec);
58 static gboolean gst_siren_dec_stop (GstAudioDecoder * dec);
59 static gboolean gst_siren_dec_set_format (GstAudioDecoder * dec,
60 GstCaps * caps);
61 static GstFlowReturn gst_siren_dec_parse (GstAudioDecoder * dec,
62 GstAdapter * adapter, gint * offset, gint * length);
63 static GstFlowReturn gst_siren_dec_handle_frame (GstAudioDecoder * dec,
64 GstBuffer * buffer);
65
66
67 G_DEFINE_TYPE (GstSirenDec, gst_siren_dec, GST_TYPE_AUDIO_DECODER);
68
69 static void
gst_siren_dec_class_init(GstSirenDecClass * klass)70 gst_siren_dec_class_init (GstSirenDecClass * klass)
71 {
72 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
73 GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
74
75 GST_DEBUG_CATEGORY_INIT (sirendec_debug, "sirendec", 0, "sirendec");
76
77 gst_element_class_add_static_pad_template (element_class, &srctemplate);
78 gst_element_class_add_static_pad_template (element_class, &sinktemplate);
79
80 gst_element_class_set_static_metadata (element_class, "Siren Decoder element",
81 "Codec/Decoder/Audio ",
82 "Decode streams encoded with the Siren7 codec into 16bit PCM",
83 "Youness Alaoui <kakaroto@kakaroto.homelinux.net>");
84
85 base_class->start = GST_DEBUG_FUNCPTR (gst_siren_dec_start);
86 base_class->stop = GST_DEBUG_FUNCPTR (gst_siren_dec_stop);
87 base_class->set_format = GST_DEBUG_FUNCPTR (gst_siren_dec_set_format);
88 base_class->parse = GST_DEBUG_FUNCPTR (gst_siren_dec_parse);
89 base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_siren_dec_handle_frame);
90
91 GST_DEBUG ("Class Init done");
92 }
93
94 static void
gst_siren_dec_init(GstSirenDec * dec)95 gst_siren_dec_init (GstSirenDec * dec)
96 {
97 gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE);
98 gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
99 (dec), TRUE);
100 GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (dec));
101 }
102
103 static gboolean
gst_siren_dec_start(GstAudioDecoder * dec)104 gst_siren_dec_start (GstAudioDecoder * dec)
105 {
106 GstSirenDec *sdec = GST_SIREN_DEC (dec);
107
108 GST_DEBUG_OBJECT (dec, "start");
109
110 sdec->decoder = Siren7_NewDecoder (16000);
111
112 /* no flushing please */
113 gst_audio_decoder_set_drainable (dec, FALSE);
114
115 return TRUE;
116 }
117
118 static gboolean
gst_siren_dec_stop(GstAudioDecoder * dec)119 gst_siren_dec_stop (GstAudioDecoder * dec)
120 {
121 GstSirenDec *sdec = GST_SIREN_DEC (dec);
122
123 GST_DEBUG_OBJECT (dec, "stop");
124
125 Siren7_CloseDecoder (sdec->decoder);
126
127 return TRUE;
128 }
129
130 static gboolean
gst_siren_dec_set_format(GstAudioDecoder * bdec,GstCaps * caps)131 gst_siren_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
132 {
133 GstAudioInfo info;
134
135 gst_audio_info_init (&info);
136 gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16LE, 16000, 1, NULL);
137 return gst_audio_decoder_set_output_format (bdec, &info);
138 }
139
140 static GstFlowReturn
gst_siren_dec_parse(GstAudioDecoder * dec,GstAdapter * adapter,gint * offset,gint * length)141 gst_siren_dec_parse (GstAudioDecoder * dec, GstAdapter * adapter,
142 gint * offset, gint * length)
143 {
144 gint size;
145 GstFlowReturn ret;
146
147 size = gst_adapter_available (adapter);
148 g_return_val_if_fail (size > 0, GST_FLOW_ERROR);
149
150 /* accept any multiple of frames */
151 if (size > 40) {
152 ret = GST_FLOW_OK;
153 *offset = 0;
154 *length = size - (size % 40);
155 } else {
156 ret = GST_FLOW_EOS;
157 }
158
159 return ret;
160 }
161
162 static GstFlowReturn
gst_siren_dec_handle_frame(GstAudioDecoder * bdec,GstBuffer * buf)163 gst_siren_dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buf)
164 {
165 GstSirenDec *dec;
166 GstFlowReturn ret = GST_FLOW_OK;
167 GstBuffer *out_buf;
168 guint8 *in_data, *out_data;
169 guint i, size, num_frames;
170 gint out_size;
171 #ifndef GST_DISABLE_GST_DEBUG
172 gint in_size;
173 #endif
174 gint decode_ret;
175 GstMapInfo inmap, outmap;
176
177 dec = GST_SIREN_DEC (bdec);
178
179 size = gst_buffer_get_size (buf);
180
181 GST_LOG_OBJECT (dec, "Received buffer of size %u", size);
182
183 g_return_val_if_fail (size % 40 == 0, GST_FLOW_ERROR);
184 g_return_val_if_fail (size > 0, GST_FLOW_ERROR);
185
186 /* process 40 input bytes into 640 output bytes */
187 num_frames = size / 40;
188
189 /* this is the input/output size */
190 #ifndef GST_DISABLE_GST_DEBUG
191 in_size = num_frames * 40;
192 #endif
193 out_size = num_frames * 640;
194
195 GST_LOG_OBJECT (dec, "we have %u frames, %u in, %u out", num_frames, in_size,
196 out_size);
197
198 out_buf = gst_audio_decoder_allocate_output_buffer (bdec, out_size);
199 if (out_buf == NULL)
200 goto alloc_failed;
201
202 /* get the input data for all the frames */
203 gst_buffer_map (buf, &inmap, GST_MAP_READ);
204 gst_buffer_map (out_buf, &outmap, GST_MAP_WRITE);
205
206 in_data = inmap.data;
207 out_data = outmap.data;
208
209 for (i = 0; i < num_frames; i++) {
210 GST_LOG_OBJECT (dec, "Decoding frame %u/%u", i, num_frames);
211
212 /* decode 40 input bytes to 640 output bytes */
213 decode_ret = Siren7_DecodeFrame (dec->decoder, in_data, out_data);
214 if (decode_ret != 0)
215 goto decode_error;
216
217 /* move to next frame */
218 out_data += 640;
219 in_data += 40;
220 }
221
222 gst_buffer_unmap (buf, &inmap);
223 gst_buffer_unmap (out_buf, &outmap);
224
225 GST_LOG_OBJECT (dec, "Finished decoding");
226
227 /* might really be multiple frames,
228 * but was treated as one for all purposes here */
229 ret = gst_audio_decoder_finish_frame (bdec, out_buf, 1);
230
231 done:
232 return ret;
233
234 /* ERRORS */
235 alloc_failed:
236 {
237 GST_DEBUG_OBJECT (dec, "failed to pad_alloc buffer: %d (%s)", ret,
238 gst_flow_get_name (ret));
239 goto done;
240 }
241 decode_error:
242 {
243 GST_AUDIO_DECODER_ERROR (bdec, 1, STREAM, DECODE, (NULL),
244 ("Error decoding frame: %d", decode_ret), ret);
245 if (ret == GST_FLOW_OK)
246 gst_audio_decoder_finish_frame (bdec, NULL, 1);
247 gst_buffer_unref (out_buf);
248 goto done;
249 }
250 }
251
252 gboolean
gst_siren_dec_plugin_init(GstPlugin * plugin)253 gst_siren_dec_plugin_init (GstPlugin * plugin)
254 {
255 return gst_element_register (plugin, "sirendec",
256 GST_RANK_MARGINAL, GST_TYPE_SIREN_DEC);
257 }
258