1 /*
2  * Siren Encoder 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-sirenenc
24  * @title: sirenenc
25  *
26  * This encodes audio buffers into 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 "gstsirenenc.h"
38 
39 #include <string.h>
40 
41 GST_DEBUG_CATEGORY (sirenenc_debug);
42 #define GST_CAT_DEFAULT (sirenenc_debug)
43 
44 #define FRAME_DURATION  (20 * GST_MSECOND)
45 
46 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
47     GST_PAD_SRC,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS ("audio/x-siren, " "dct-length = (int) 320"));
50 
51 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
52     GST_PAD_SINK,
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_enc_start (GstAudioEncoder * enc);
58 static gboolean gst_siren_enc_stop (GstAudioEncoder * enc);
59 static gboolean gst_siren_enc_set_format (GstAudioEncoder * enc,
60     GstAudioInfo * info);
61 static GstFlowReturn gst_siren_enc_handle_frame (GstAudioEncoder * enc,
62     GstBuffer * in_buf);
63 
64 G_DEFINE_TYPE (GstSirenEnc, gst_siren_enc, GST_TYPE_AUDIO_ENCODER);
65 
66 
67 static void
gst_siren_enc_class_init(GstSirenEncClass * klass)68 gst_siren_enc_class_init (GstSirenEncClass * klass)
69 {
70   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
71   GstAudioEncoderClass *base_class = GST_AUDIO_ENCODER_CLASS (klass);
72 
73   GST_DEBUG_CATEGORY_INIT (sirenenc_debug, "sirenenc", 0, "sirenenc");
74 
75   gst_element_class_add_static_pad_template (element_class, &srctemplate);
76   gst_element_class_add_static_pad_template (element_class, &sinktemplate);
77 
78   gst_element_class_set_static_metadata (element_class, "Siren Encoder element",
79       "Codec/Encoder/Audio ",
80       "Encode 16bit PCM streams into the Siren7 codec",
81       "Youness Alaoui <kakaroto@kakaroto.homelinux.net>");
82 
83   base_class->start = GST_DEBUG_FUNCPTR (gst_siren_enc_start);
84   base_class->stop = GST_DEBUG_FUNCPTR (gst_siren_enc_stop);
85   base_class->set_format = GST_DEBUG_FUNCPTR (gst_siren_enc_set_format);
86   base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_siren_enc_handle_frame);
87 
88   GST_DEBUG ("Class Init done");
89 }
90 
91 static void
gst_siren_enc_init(GstSirenEnc * enc)92 gst_siren_enc_init (GstSirenEnc * enc)
93 {
94   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (enc));
95 }
96 
97 static gboolean
gst_siren_enc_start(GstAudioEncoder * enc)98 gst_siren_enc_start (GstAudioEncoder * enc)
99 {
100   GstSirenEnc *senc = GST_SIREN_ENC (enc);
101 
102   GST_DEBUG_OBJECT (enc, "start");
103 
104   senc->encoder = Siren7_NewEncoder (16000);
105 
106   return TRUE;
107 }
108 
109 static gboolean
gst_siren_enc_stop(GstAudioEncoder * enc)110 gst_siren_enc_stop (GstAudioEncoder * enc)
111 {
112   GstSirenEnc *senc = GST_SIREN_ENC (enc);
113 
114   GST_DEBUG_OBJECT (senc, "stop");
115 
116   Siren7_CloseEncoder (senc->encoder);
117 
118   return TRUE;
119 }
120 
121 static gboolean
gst_siren_enc_set_format(GstAudioEncoder * benc,GstAudioInfo * info)122 gst_siren_enc_set_format (GstAudioEncoder * benc, GstAudioInfo * info)
123 {
124   gboolean res;
125   GstCaps *outcaps;
126 
127   outcaps = gst_static_pad_template_get_caps (&srctemplate);
128   res = gst_audio_encoder_set_output_format (benc, outcaps);
129   gst_caps_unref (outcaps);
130 
131   /* report needs to base class */
132   gst_audio_encoder_set_frame_samples_min (benc, 320);
133   gst_audio_encoder_set_frame_samples_max (benc, 320);
134   /* no remainder or flushing please */
135   gst_audio_encoder_set_hard_min (benc, TRUE);
136   gst_audio_encoder_set_drainable (benc, FALSE);
137 
138   return res;
139 }
140 
141 static GstFlowReturn
gst_siren_enc_handle_frame(GstAudioEncoder * benc,GstBuffer * buf)142 gst_siren_enc_handle_frame (GstAudioEncoder * benc, GstBuffer * buf)
143 {
144   GstSirenEnc *enc;
145   GstFlowReturn ret = GST_FLOW_OK;
146   GstBuffer *out_buf;
147   guint8 *in_data, *out_data;
148   guint i, size, num_frames;
149   gint out_size;
150 #ifndef GST_DISABLE_GST_DEBUG
151   gint in_size;
152 #endif
153   gint encode_ret;
154   GstMapInfo inmap, outmap;
155 
156   g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
157 
158   enc = GST_SIREN_ENC (benc);
159 
160   size = gst_buffer_get_size (buf);
161 
162   GST_LOG_OBJECT (enc, "Received buffer of size %d", size);
163 
164   g_return_val_if_fail (size > 0, GST_FLOW_ERROR);
165   g_return_val_if_fail (size % 640 == 0, GST_FLOW_ERROR);
166 
167   /* we need to process 640 input bytes to produce 40 output bytes */
168   /* calculate the amount of frames we will handle */
169   num_frames = size / 640;
170 
171   /* this is the input/output size */
172 #ifndef GST_DISABLE_GST_DEBUG
173   in_size = num_frames * 640;
174 #endif
175   out_size = num_frames * 40;
176 
177   GST_LOG_OBJECT (enc, "we have %u frames, %u in, %u out", num_frames, in_size,
178       out_size);
179 
180   /* get a buffer */
181   out_buf = gst_audio_encoder_allocate_output_buffer (benc, out_size);
182   if (out_buf == NULL)
183     goto alloc_failed;
184 
185   /* get the input data for all the frames */
186   gst_buffer_map (buf, &inmap, GST_MAP_READ);
187   gst_buffer_map (out_buf, &outmap, GST_MAP_READ);
188   in_data = inmap.data;
189   out_data = outmap.data;
190 
191   for (i = 0; i < num_frames; i++) {
192     GST_LOG_OBJECT (enc, "Encoding frame %u/%u", i, num_frames);
193 
194     /* encode 640 input bytes to 40 output bytes */
195     encode_ret = Siren7_EncodeFrame (enc->encoder, in_data, out_data);
196     if (encode_ret != 0)
197       goto encode_error;
198 
199     /* move to next frame */
200     out_data += 40;
201     in_data += 640;
202   }
203 
204   gst_buffer_unmap (buf, &inmap);
205   gst_buffer_unmap (out_buf, &outmap);
206 
207   GST_LOG_OBJECT (enc, "Finished encoding");
208 
209   /* we encode all we get, pass it along */
210   ret = gst_audio_encoder_finish_frame (benc, out_buf, -1);
211 
212 done:
213   return ret;
214 
215   /* ERRORS */
216 alloc_failed:
217   {
218     GST_DEBUG_OBJECT (enc, "failed to pad_alloc buffer: %d (%s)", ret,
219         gst_flow_get_name (ret));
220     goto done;
221   }
222 encode_error:
223   {
224     GST_ELEMENT_ERROR (enc, STREAM, ENCODE, (NULL),
225         ("Error encoding frame: %d", encode_ret));
226     ret = GST_FLOW_ERROR;
227     gst_buffer_unref (out_buf);
228     goto done;
229   }
230 }
231 
232 gboolean
gst_siren_enc_plugin_init(GstPlugin * plugin)233 gst_siren_enc_plugin_init (GstPlugin * plugin)
234 {
235   return gst_element_register (plugin, "sirenenc",
236       GST_RANK_MARGINAL, GST_TYPE_SIREN_ENC);
237 }
238