• 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 "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 GST_ELEMENT_REGISTER_DEFINE (gsmenc, "gsmenc", GST_RANK_PRIMARY,
72     GST_TYPE_GSMENC);
73 
74 static void
gst_gsmenc_class_init(GstGSMEncClass * klass)75 gst_gsmenc_class_init (GstGSMEncClass * klass)
76 {
77   GstElementClass *element_class;
78   GstAudioEncoderClass *base_class;
79 
80   element_class = (GstElementClass *) klass;
81   base_class = (GstAudioEncoderClass *) klass;
82 
83   gst_element_class_add_static_pad_template (element_class,
84       &gsmenc_sink_template);
85   gst_element_class_add_static_pad_template (element_class,
86       &gsmenc_src_template);
87   gst_element_class_set_static_metadata (element_class, "GSM audio encoder",
88       "Codec/Encoder/Audio", "Encodes GSM audio",
89       "Philippe Khalaf <burger@speedy.org>");
90 
91   base_class->start = GST_DEBUG_FUNCPTR (gst_gsmenc_start);
92   base_class->stop = GST_DEBUG_FUNCPTR (gst_gsmenc_stop);
93   base_class->set_format = GST_DEBUG_FUNCPTR (gst_gsmenc_set_format);
94   base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_gsmenc_handle_frame);
95 
96   GST_DEBUG_CATEGORY_INIT (gsmenc_debug, "gsmenc", 0, "GSM Encoder");
97 }
98 
99 static void
gst_gsmenc_init(GstGSMEnc * gsmenc)100 gst_gsmenc_init (GstGSMEnc * gsmenc)
101 {
102   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (gsmenc));
103 }
104 
105 static gboolean
gst_gsmenc_start(GstAudioEncoder * enc)106 gst_gsmenc_start (GstAudioEncoder * enc)
107 {
108   GstGSMEnc *gsmenc = GST_GSMENC (enc);
109   gint use_wav49;
110 
111   GST_DEBUG_OBJECT (enc, "start");
112 
113   gsmenc->state = gsm_create ();
114 
115   /* turn off WAV49 handling */
116   use_wav49 = 0;
117   gsm_option (gsmenc->state, GSM_OPT_WAV49, &use_wav49);
118 
119   return TRUE;
120 }
121 
122 static gboolean
gst_gsmenc_stop(GstAudioEncoder * enc)123 gst_gsmenc_stop (GstAudioEncoder * enc)
124 {
125   GstGSMEnc *gsmenc = GST_GSMENC (enc);
126 
127   GST_DEBUG_OBJECT (enc, "stop");
128   gsm_destroy (gsmenc->state);
129 
130   return TRUE;
131 }
132 
133 static gboolean
gst_gsmenc_set_format(GstAudioEncoder * benc,GstAudioInfo * info)134 gst_gsmenc_set_format (GstAudioEncoder * benc, GstAudioInfo * info)
135 {
136   GstCaps *srccaps;
137 
138   srccaps = gst_static_pad_template_get_caps (&gsmenc_src_template);
139   gst_audio_encoder_set_output_format (GST_AUDIO_ENCODER (benc), srccaps);
140   gst_caps_unref (srccaps);
141 
142   /* report needs to base class */
143   gst_audio_encoder_set_frame_samples_min (benc, 160);
144   gst_audio_encoder_set_frame_samples_max (benc, 160);
145   gst_audio_encoder_set_frame_max (benc, 1);
146 
147   return TRUE;
148 }
149 
150 static GstFlowReturn
gst_gsmenc_handle_frame(GstAudioEncoder * benc,GstBuffer * buffer)151 gst_gsmenc_handle_frame (GstAudioEncoder * benc, GstBuffer * buffer)
152 {
153   GstGSMEnc *gsmenc;
154   gsm_signal *data;
155   GstFlowReturn ret = GST_FLOW_OK;
156   GstBuffer *outbuf;
157   GstMapInfo map, omap;
158 
159   gsmenc = GST_GSMENC (benc);
160 
161   /* we don't deal with squeezing remnants, so simply discard those */
162   if (G_UNLIKELY (buffer == NULL)) {
163     GST_DEBUG_OBJECT (gsmenc, "no data");
164     goto done;
165   }
166 
167   gst_buffer_map (buffer, &map, GST_MAP_READ);
168   if (G_UNLIKELY (map.size < 320)) {
169     GST_DEBUG_OBJECT (gsmenc, "discarding trailing data %d", (gint) map.size);
170     gst_buffer_unmap (buffer, &map);
171     ret = gst_audio_encoder_finish_frame (benc, NULL, -1);
172     goto done;
173   }
174 
175   outbuf = gst_buffer_new_and_alloc (33 * sizeof (gsm_byte));
176   gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
177 
178   /* encode 160 16-bit samples into 33 bytes */
179   data = (gsm_signal *) map.data;
180   gsm_encode (gsmenc->state, data, (gsm_byte *) omap.data);
181 
182   GST_LOG_OBJECT (gsmenc, "encoded to %d bytes", (gint) omap.size);
183   gst_buffer_unmap (buffer, &map);
184   gst_buffer_unmap (outbuf, &omap);
185 
186   ret = gst_audio_encoder_finish_frame (benc, outbuf, 160);
187 
188 done:
189   return ret;
190 }
191