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 "gstgsmenc.h"
29 
30 GST_DEBUG_CATEGORY_STATIC (gsmenc_debug);
31 #define GST_CAT_DEFAULT (gsmenc_debug)
32 
33 /* GSMEnc 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_gsmenc_start (GstAudioEncoder * enc);
47 static gboolean gst_gsmenc_stop (GstAudioEncoder * enc);
48 static gboolean gst_gsmenc_set_format (GstAudioEncoder * enc,
49     GstAudioInfo * info);
50 static GstFlowReturn gst_gsmenc_handle_frame (GstAudioEncoder * enc,
51     GstBuffer * in_buf);
52 
53 static GstStaticPadTemplate gsmenc_src_template =
54 GST_STATIC_PAD_TEMPLATE ("src",
55     GST_PAD_SRC,
56     GST_PAD_ALWAYS,
57     GST_STATIC_CAPS ("audio/x-gsm, " "rate = (int) 8000, " "channels = (int) 1")
58     );
59 
60 static GstStaticPadTemplate gsmenc_sink_template =
61 GST_STATIC_PAD_TEMPLATE ("sink",
62     GST_PAD_SINK,
63     GST_PAD_ALWAYS,
64     GST_STATIC_CAPS ("audio/x-raw, "
65         "format = (string) " GST_AUDIO_NE (S16) ", "
66         "layout = (string) interleaved, "
67         "rate = (int) 8000, channels = (int) 1")
68     );
69 
70 G_DEFINE_TYPE (GstGSMEnc, gst_gsmenc, GST_TYPE_AUDIO_ENCODER);
71 
72 static void
gst_gsmenc_class_init(GstGSMEncClass * klass)73 gst_gsmenc_class_init (GstGSMEncClass * klass)
74 {
75   GstElementClass *element_class;
76   GstAudioEncoderClass *base_class;
77 
78   element_class = (GstElementClass *) klass;
79   base_class = (GstAudioEncoderClass *) klass;
80 
81   gst_element_class_add_static_pad_template (element_class,
82       &gsmenc_sink_template);
83   gst_element_class_add_static_pad_template (element_class,
84       &gsmenc_src_template);
85   gst_element_class_set_static_metadata (element_class, "GSM audio encoder",
86       "Codec/Encoder/Audio", "Encodes GSM audio",
87       "Philippe Khalaf <burger@speedy.org>");
88 
89   base_class->start = GST_DEBUG_FUNCPTR (gst_gsmenc_start);
90   base_class->stop = GST_DEBUG_FUNCPTR (gst_gsmenc_stop);
91   base_class->set_format = GST_DEBUG_FUNCPTR (gst_gsmenc_set_format);
92   base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_gsmenc_handle_frame);
93 
94   GST_DEBUG_CATEGORY_INIT (gsmenc_debug, "gsmenc", 0, "GSM Encoder");
95 }
96 
97 static void
gst_gsmenc_init(GstGSMEnc * gsmenc)98 gst_gsmenc_init (GstGSMEnc * gsmenc)
99 {
100   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (gsmenc));
101 }
102 
103 static gboolean
gst_gsmenc_start(GstAudioEncoder * enc)104 gst_gsmenc_start (GstAudioEncoder * enc)
105 {
106   GstGSMEnc *gsmenc = GST_GSMENC (enc);
107   gint use_wav49;
108 
109   GST_DEBUG_OBJECT (enc, "start");
110 
111   gsmenc->state = gsm_create ();
112 
113   /* turn off WAV49 handling */
114   use_wav49 = 0;
115   gsm_option (gsmenc->state, GSM_OPT_WAV49, &use_wav49);
116 
117   return TRUE;
118 }
119 
120 static gboolean
gst_gsmenc_stop(GstAudioEncoder * enc)121 gst_gsmenc_stop (GstAudioEncoder * enc)
122 {
123   GstGSMEnc *gsmenc = GST_GSMENC (enc);
124 
125   GST_DEBUG_OBJECT (enc, "stop");
126   gsm_destroy (gsmenc->state);
127 
128   return TRUE;
129 }
130 
131 static gboolean
gst_gsmenc_set_format(GstAudioEncoder * benc,GstAudioInfo * info)132 gst_gsmenc_set_format (GstAudioEncoder * benc, GstAudioInfo * info)
133 {
134   GstCaps *srccaps;
135 
136   srccaps = gst_static_pad_template_get_caps (&gsmenc_src_template);
137   gst_audio_encoder_set_output_format (GST_AUDIO_ENCODER (benc), srccaps);
138   gst_caps_unref (srccaps);
139 
140   /* report needs to base class */
141   gst_audio_encoder_set_frame_samples_min (benc, 160);
142   gst_audio_encoder_set_frame_samples_max (benc, 160);
143   gst_audio_encoder_set_frame_max (benc, 1);
144 
145   return TRUE;
146 }
147 
148 static GstFlowReturn
gst_gsmenc_handle_frame(GstAudioEncoder * benc,GstBuffer * buffer)149 gst_gsmenc_handle_frame (GstAudioEncoder * benc, GstBuffer * buffer)
150 {
151   GstGSMEnc *gsmenc;
152   gsm_signal *data;
153   GstFlowReturn ret = GST_FLOW_OK;
154   GstBuffer *outbuf;
155   GstMapInfo map, omap;
156 
157   gsmenc = GST_GSMENC (benc);
158 
159   /* we don't deal with squeezing remnants, so simply discard those */
160   if (G_UNLIKELY (buffer == NULL)) {
161     GST_DEBUG_OBJECT (gsmenc, "no data");
162     goto done;
163   }
164 
165   gst_buffer_map (buffer, &map, GST_MAP_READ);
166   if (G_UNLIKELY (map.size < 320)) {
167     GST_DEBUG_OBJECT (gsmenc, "discarding trailing data %d", (gint) map.size);
168     gst_buffer_unmap (buffer, &map);
169     ret = gst_audio_encoder_finish_frame (benc, NULL, -1);
170     goto done;
171   }
172 
173   outbuf = gst_buffer_new_and_alloc (33 * sizeof (gsm_byte));
174   gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
175 
176   /* encode 160 16-bit samples into 33 bytes */
177   data = (gsm_signal *) map.data;
178   gsm_encode (gsmenc->state, data, (gsm_byte *) omap.data);
179 
180   GST_LOG_OBJECT (gsmenc, "encoded to %d bytes", (gint) omap.size);
181   gst_buffer_unmap (buffer, &map);
182   gst_buffer_unmap (outbuf, &omap);
183 
184   ret = gst_audio_encoder_finish_frame (benc, outbuf, 160);
185 
186 done:
187   return ret;
188 }
189