• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Farsight
3  * GStreamer GSM encoder
4  * Copyright (C) 2005 Philippe Khalaf <burger@speedy.org>
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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <string.h>
27 
28 #include "gstgsmdec.h"
29 
30 GST_DEBUG_CATEGORY_STATIC (gsmdec_debug);
31 #define GST_CAT_DEFAULT (gsmdec_debug)
32 
33 /* GSMDec signals and args */
34 enum
35 {
36   /* FILL ME */
37   LAST_SIGNAL
38 };
39 
40 enum
41 {
42   /* FILL ME */
43   ARG_0
44 };
45 
46 static gboolean gst_gsmdec_start (GstAudioDecoder * dec);
47 static gboolean gst_gsmdec_stop (GstAudioDecoder * dec);
48 static gboolean gst_gsmdec_set_format (GstAudioDecoder * dec, GstCaps * caps);
49 static GstFlowReturn gst_gsmdec_parse (GstAudioDecoder * dec,
50     GstAdapter * adapter, gint * offset, gint * length);
51 static GstFlowReturn gst_gsmdec_handle_frame (GstAudioDecoder * dec,
52     GstBuffer * in_buf);
53 
54 /*static guint gst_gsmdec_signals[LAST_SIGNAL] = { 0 }; */
55 
56 #define ENCODED_SAMPLES	160
57 
58 static GstStaticPadTemplate gsmdec_sink_template =
59     GST_STATIC_PAD_TEMPLATE ("sink",
60     GST_PAD_SINK,
61     GST_PAD_ALWAYS,
62     GST_STATIC_CAPS ("audio/x-gsm, rate = (int) 8000, channels = (int) 1; "
63         "audio/ms-gsm, rate = (int) [1, MAX], channels = (int) 1")
64     );
65 
66 static GstStaticPadTemplate gsmdec_src_template =
67 GST_STATIC_PAD_TEMPLATE ("src",
68     GST_PAD_SRC,
69     GST_PAD_ALWAYS,
70     GST_STATIC_CAPS ("audio/x-raw, "
71         "format = (string) " GST_AUDIO_NE (S16) ", "
72         "layout = (string) interleaved, "
73         "rate = (int) [1, MAX], channels = (int) 1")
74     );
75 
76 G_DEFINE_TYPE (GstGSMDec, gst_gsmdec, GST_TYPE_AUDIO_DECODER);
77 GST_ELEMENT_REGISTER_DEFINE (gsmdec, "gsmdec", GST_RANK_PRIMARY,
78     GST_TYPE_GSMDEC);
79 
80 static void
gst_gsmdec_class_init(GstGSMDecClass * klass)81 gst_gsmdec_class_init (GstGSMDecClass * klass)
82 {
83   GstElementClass *element_class;
84   GstAudioDecoderClass *base_class;
85 
86   element_class = (GstElementClass *) klass;
87   base_class = (GstAudioDecoderClass *) klass;
88 
89   gst_element_class_add_static_pad_template (element_class,
90       &gsmdec_sink_template);
91   gst_element_class_add_static_pad_template (element_class,
92       &gsmdec_src_template);
93   gst_element_class_set_static_metadata (element_class, "GSM audio decoder",
94       "Codec/Decoder/Audio", "Decodes GSM encoded audio",
95       "Philippe Khalaf <burger@speedy.org>");
96 
97   base_class->start = GST_DEBUG_FUNCPTR (gst_gsmdec_start);
98   base_class->stop = GST_DEBUG_FUNCPTR (gst_gsmdec_stop);
99   base_class->set_format = GST_DEBUG_FUNCPTR (gst_gsmdec_set_format);
100   base_class->parse = GST_DEBUG_FUNCPTR (gst_gsmdec_parse);
101   base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_gsmdec_handle_frame);
102 
103   GST_DEBUG_CATEGORY_INIT (gsmdec_debug, "gsmdec", 0, "GSM Decoder");
104 }
105 
106 static void
gst_gsmdec_init(GstGSMDec * gsmdec)107 gst_gsmdec_init (GstGSMDec * gsmdec)
108 {
109   gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (gsmdec), TRUE);
110   gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
111       (gsmdec), TRUE);
112   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (gsmdec));
113 }
114 
115 static gboolean
gst_gsmdec_start(GstAudioDecoder * dec)116 gst_gsmdec_start (GstAudioDecoder * dec)
117 {
118   GstGSMDec *gsmdec = GST_GSMDEC (dec);
119 
120   GST_DEBUG_OBJECT (dec, "start");
121 
122   gsmdec->state = gsm_create ();
123 
124   return TRUE;
125 }
126 
127 static gboolean
gst_gsmdec_stop(GstAudioDecoder * dec)128 gst_gsmdec_stop (GstAudioDecoder * dec)
129 {
130   GstGSMDec *gsmdec = GST_GSMDEC (dec);
131 
132   GST_DEBUG_OBJECT (dec, "stop");
133 
134   gsm_destroy (gsmdec->state);
135 
136   return TRUE;
137 }
138 
139 static gboolean
gst_gsmdec_set_format(GstAudioDecoder * dec,GstCaps * caps)140 gst_gsmdec_set_format (GstAudioDecoder * dec, GstCaps * caps)
141 {
142   GstGSMDec *gsmdec;
143   GstStructure *s;
144   gboolean ret = FALSE;
145   gint rate;
146   GstAudioInfo info;
147 
148   gsmdec = GST_GSMDEC (dec);
149 
150   s = gst_caps_get_structure (caps, 0);
151   if (s == NULL)
152     goto wrong_caps;
153 
154   /* figure out if we deal with plain or MSGSM */
155   if (gst_structure_has_name (s, "audio/x-gsm"))
156     gsmdec->use_wav49 = 0;
157   else if (gst_structure_has_name (s, "audio/ms-gsm"))
158     gsmdec->use_wav49 = 1;
159   else
160     goto wrong_caps;
161 
162   gsmdec->needed = 33;
163 
164   if (!gst_structure_get_int (s, "rate", &rate)) {
165     GST_WARNING_OBJECT (gsmdec, "missing sample rate parameter from sink caps");
166     goto beach;
167   }
168 
169   /* MSGSM needs different framing */
170   gsm_option (gsmdec->state, GSM_OPT_WAV49, &gsmdec->use_wav49);
171 
172   /* Setting up src caps based on the input sample rate. */
173   gst_audio_info_init (&info);
174   gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16, rate, 1, NULL);
175 
176   ret = gst_audio_decoder_set_output_format (dec, &info);
177 
178   return ret;
179 
180   /* ERRORS */
181 wrong_caps:
182 
183   GST_ERROR_OBJECT (gsmdec, "invalid caps received");
184 
185 beach:
186 
187   return ret;
188 }
189 
190 static GstFlowReturn
gst_gsmdec_parse(GstAudioDecoder * dec,GstAdapter * adapter,gint * offset,gint * length)191 gst_gsmdec_parse (GstAudioDecoder * dec, GstAdapter * adapter,
192     gint * offset, gint * length)
193 {
194   GstGSMDec *gsmdec = GST_GSMDEC (dec);
195   guint size;
196 
197   size = gst_adapter_available (adapter);
198 
199   /* if input format is TIME each buffer should be self-contained and
200    * the data is presumably packetised, and we should start with a clean
201    * slate/state at the beginning of each buffer (for wav49 case) */
202   if (dec->input_segment.format == GST_FORMAT_TIME) {
203     *offset = 0;
204     *length = size;
205     gsmdec->needed = 33;
206     return GST_FLOW_OK;
207   }
208 
209   g_return_val_if_fail (size > 0, GST_FLOW_ERROR);
210 
211   if (size < gsmdec->needed)
212     return GST_FLOW_EOS;
213 
214   *offset = 0;
215   *length = gsmdec->needed;
216 
217   /* WAV49 requires alternating 33 and 32 bytes of input */
218   if (gsmdec->use_wav49) {
219     gsmdec->needed = (gsmdec->needed == 33 ? 32 : 33);
220   }
221 
222   return GST_FLOW_OK;
223 }
224 
225 static guint
gst_gsmdec_get_frame_count(GstGSMDec * dec,gsize buffer_size)226 gst_gsmdec_get_frame_count (GstGSMDec * dec, gsize buffer_size)
227 {
228   guint count;
229 
230   if (dec->use_wav49) {
231     count = (buffer_size / (33 + 32)) * 2;
232     if (buffer_size % (33 + 32) >= dec->needed)
233       ++count;
234   } else {
235     count = buffer_size / 33;
236   }
237 
238   return count;
239 }
240 
241 static GstFlowReturn
gst_gsmdec_handle_frame(GstAudioDecoder * dec,GstBuffer * buffer)242 gst_gsmdec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
243 {
244   GstGSMDec *gsmdec;
245   gsm_signal *out_data;
246   gsm_byte *data;
247   GstFlowReturn ret = GST_FLOW_OK;
248   GstBuffer *outbuf;
249   GstMapInfo map, omap;
250   gsize outsize;
251   guint frames, i, errors = 0;
252 
253   /* no fancy draining */
254   if (G_UNLIKELY (!buffer))
255     return GST_FLOW_OK;
256 
257   gsmdec = GST_GSMDEC (dec);
258 
259   gst_buffer_map (buffer, &map, GST_MAP_READ);
260 
261   frames = gst_gsmdec_get_frame_count (gsmdec, map.size);
262 
263   /* always the same amount of output samples (20ms worth per frame) */
264   outsize = ENCODED_SAMPLES * frames * sizeof (gsm_signal);
265   outbuf = gst_buffer_new_and_alloc (outsize);
266 
267   gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
268   out_data = (gsm_signal *) omap.data;
269   data = (gsm_byte *) map.data;
270 
271   for (i = 0; i < frames; ++i) {
272     /* now encode frame into the output buffer */
273     if (gsm_decode (gsmdec->state, data, out_data) < 0) {
274       /* invalid frame */
275       GST_AUDIO_DECODER_ERROR (gsmdec, 1, STREAM, DECODE, (NULL),
276           ("tried to decode an invalid frame"), ret);
277       memset (out_data, 0, ENCODED_SAMPLES * sizeof (gsm_signal));
278       ++errors;
279     }
280     out_data += ENCODED_SAMPLES;
281     data += gsmdec->needed;
282     if (gsmdec->use_wav49)
283       gsmdec->needed = (gsmdec->needed == 33 ? 32 : 33);
284   }
285 
286   gst_buffer_unmap (outbuf, &omap);
287   gst_buffer_unmap (buffer, &map);
288 
289   if (errors == frames) {
290     gst_buffer_unref (outbuf);
291     outbuf = NULL;
292   }
293 
294   gst_audio_decoder_finish_frame (dec, outbuf, 1);
295 
296   return ret;
297 }
298