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