• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GST_ELEMENT_REGISTER_DEFINE (sirendec, "sirendec",
69     GST_RANK_MARGINAL, GST_TYPE_SIREN_DEC);
70 
71 static void
gst_siren_dec_class_init(GstSirenDecClass * klass)72 gst_siren_dec_class_init (GstSirenDecClass * klass)
73 {
74   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
75   GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
76 
77   GST_DEBUG_CATEGORY_INIT (sirendec_debug, "sirendec", 0, "sirendec");
78 
79   gst_element_class_add_static_pad_template (element_class, &srctemplate);
80   gst_element_class_add_static_pad_template (element_class, &sinktemplate);
81 
82   gst_element_class_set_static_metadata (element_class, "Siren Decoder element",
83       "Codec/Decoder/Audio ",
84       "Decode streams encoded with the Siren7 codec into 16bit PCM",
85       "Youness Alaoui <kakaroto@kakaroto.homelinux.net>");
86 
87   base_class->start = GST_DEBUG_FUNCPTR (gst_siren_dec_start);
88   base_class->stop = GST_DEBUG_FUNCPTR (gst_siren_dec_stop);
89   base_class->set_format = GST_DEBUG_FUNCPTR (gst_siren_dec_set_format);
90   base_class->parse = GST_DEBUG_FUNCPTR (gst_siren_dec_parse);
91   base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_siren_dec_handle_frame);
92 
93   GST_DEBUG ("Class Init done");
94 }
95 
96 static void
gst_siren_dec_init(GstSirenDec * dec)97 gst_siren_dec_init (GstSirenDec * dec)
98 {
99   gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE);
100   gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
101       (dec), TRUE);
102   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (dec));
103 }
104 
105 static gboolean
gst_siren_dec_start(GstAudioDecoder * dec)106 gst_siren_dec_start (GstAudioDecoder * dec)
107 {
108   GstSirenDec *sdec = GST_SIREN_DEC (dec);
109 
110   GST_DEBUG_OBJECT (dec, "start");
111 
112   sdec->decoder = Siren7_NewDecoder (16000);
113 
114   /* no flushing please */
115   gst_audio_decoder_set_drainable (dec, FALSE);
116 
117   return TRUE;
118 }
119 
120 static gboolean
gst_siren_dec_stop(GstAudioDecoder * dec)121 gst_siren_dec_stop (GstAudioDecoder * dec)
122 {
123   GstSirenDec *sdec = GST_SIREN_DEC (dec);
124 
125   GST_DEBUG_OBJECT (dec, "stop");
126 
127   Siren7_CloseDecoder (sdec->decoder);
128 
129   return TRUE;
130 }
131 
132 static gboolean
gst_siren_dec_set_format(GstAudioDecoder * bdec,GstCaps * caps)133 gst_siren_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
134 {
135   GstAudioInfo info;
136 
137   gst_audio_info_init (&info);
138   gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16LE, 16000, 1, NULL);
139   return gst_audio_decoder_set_output_format (bdec, &info);
140 }
141 
142 static GstFlowReturn
gst_siren_dec_parse(GstAudioDecoder * dec,GstAdapter * adapter,gint * offset,gint * length)143 gst_siren_dec_parse (GstAudioDecoder * dec, GstAdapter * adapter,
144     gint * offset, gint * length)
145 {
146   gint size;
147   GstFlowReturn ret;
148 
149   size = gst_adapter_available (adapter);
150   g_return_val_if_fail (size > 0, GST_FLOW_ERROR);
151 
152   /* accept any multiple of frames */
153   if (size > 40) {
154     ret = GST_FLOW_OK;
155     *offset = 0;
156     *length = size - (size % 40);
157   } else {
158     ret = GST_FLOW_EOS;
159   }
160 
161   return ret;
162 }
163 
164 static GstFlowReturn
gst_siren_dec_handle_frame(GstAudioDecoder * bdec,GstBuffer * buf)165 gst_siren_dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buf)
166 {
167   GstSirenDec *dec;
168   GstFlowReturn ret = GST_FLOW_OK;
169   GstBuffer *out_buf;
170   guint8 *in_data, *out_data;
171   guint i, size, num_frames;
172   gint out_size;
173 #ifndef GST_DISABLE_GST_DEBUG
174   gint in_size;
175 #endif
176   gint decode_ret;
177   GstMapInfo inmap, outmap;
178 
179   dec = GST_SIREN_DEC (bdec);
180 
181   size = gst_buffer_get_size (buf);
182 
183   GST_LOG_OBJECT (dec, "Received buffer of size %u", size);
184 
185   g_return_val_if_fail (size % 40 == 0, GST_FLOW_ERROR);
186   g_return_val_if_fail (size > 0, GST_FLOW_ERROR);
187 
188   /* process 40 input bytes into 640 output bytes */
189   num_frames = size / 40;
190 
191   /* this is the input/output size */
192 #ifndef GST_DISABLE_GST_DEBUG
193   in_size = num_frames * 40;
194 #endif
195   out_size = num_frames * 640;
196 
197   GST_LOG_OBJECT (dec, "we have %u frames, %u in, %u out", num_frames, in_size,
198       out_size);
199 
200   out_buf = gst_audio_decoder_allocate_output_buffer (bdec, out_size);
201   if (out_buf == NULL)
202     goto alloc_failed;
203 
204   /* get the input data for all the frames */
205   gst_buffer_map (buf, &inmap, GST_MAP_READ);
206   gst_buffer_map (out_buf, &outmap, GST_MAP_WRITE);
207 
208   in_data = inmap.data;
209   out_data = outmap.data;
210 
211   for (i = 0; i < num_frames; i++) {
212     GST_LOG_OBJECT (dec, "Decoding frame %u/%u", i, num_frames);
213 
214     /* decode 40 input bytes to 640 output bytes */
215     decode_ret = Siren7_DecodeFrame (dec->decoder, in_data, out_data);
216     if (decode_ret != 0)
217       goto decode_error;
218 
219     /* move to next frame */
220     out_data += 640;
221     in_data += 40;
222   }
223 
224   gst_buffer_unmap (buf, &inmap);
225   gst_buffer_unmap (out_buf, &outmap);
226 
227   GST_LOG_OBJECT (dec, "Finished decoding");
228 
229   /* might really be multiple frames,
230    * but was treated as one for all purposes here */
231   ret = gst_audio_decoder_finish_frame (bdec, out_buf, 1);
232 
233 done:
234   return ret;
235 
236   /* ERRORS */
237 alloc_failed:
238   {
239     GST_DEBUG_OBJECT (dec, "failed to pad_alloc buffer: %d (%s)", ret,
240         gst_flow_get_name (ret));
241     goto done;
242   }
243 decode_error:
244   {
245     GST_AUDIO_DECODER_ERROR (bdec, 1, STREAM, DECODE, (NULL),
246         ("Error decoding frame: %d", decode_ret), ret);
247     if (ret == GST_FLOW_OK)
248       gst_audio_decoder_finish_frame (bdec, NULL, 1);
249     gst_buffer_unref (out_buf);
250     goto done;
251   }
252 }
253