2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
26 #include "gstspeexenc.h"
28 static GstPadTemplate *speexenc_src_template, *speexenc_sink_template;
30 /* elementfactory information */
31 GstElementDetails gst_speexenc_details = {
32 "speex audio encoder",
33 "Codec/Audio/Encoder",
35 "Wim Taymans <wim.taymans@chello.be>",
38 /* SpeexEnc signals and args */
50 static void gst_speexenc_base_init (gpointer g_class);
51 static void gst_speexenc_class_init (GstSpeexEnc *klass);
52 static void gst_speexenc_init (GstSpeexEnc *speexenc);
54 static void gst_speexenc_chain (GstPad *pad,GstData *_data);
55 static GstPadLinkReturn gst_speexenc_sinkconnect (GstPad *pad, const GstCaps *caps);
57 static GstElementClass *parent_class = NULL;
58 static guint gst_speexenc_signals[LAST_SIGNAL] = { 0 };
61 gst_speexenc_get_type (void)
63 static GType speexenc_type = 0;
66 static const GTypeInfo speexenc_info = {
67 sizeof (GstSpeexEncClass),
68 gst_speexenc_base_init,
70 (GClassInitFunc) gst_speexenc_class_init,
75 (GInstanceInitFunc) gst_speexenc_init,
77 speexenc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstSpeexEnc", &speexenc_info, 0);
82 static GstStaticPadTemplate speex_sink_template =
83 GST_STATIC_PAD_TEMPLATE (
87 GST_STATIC_CAPS ("audio/x-raw-int, "
88 "endianness = (int) BYTE_ORDER, "
89 "signed = (boolean) true, "
92 "rate = (int) [ 1000, 48000 ], "
97 static GstStaticPadTemplate speex_src_template =
98 GST_STATIC_PAD_TEMPLATE (
102 GST_STATIC_CAPS ("audio/x-speex, "
103 "rate = (int) [ 1000, 48000 ], "
109 gst_speexenc_base_init (gpointer g_class)
111 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
113 gst_element_class_add_pad_template (element_class,
114 gst_static_pad_template_get (&speex_sink_template));
115 gst_element_class_add_pad_template (element_class,
116 gst_static_pad_template_get (&speex_src_template));
118 gst_element_class_set_details (element_class, &gst_speexenc_details);
122 gst_speexenc_class_init (GstSpeexEnc *klass)
124 GObjectClass *gobject_class;
125 GstElementClass *gstelement_class;
127 gobject_class = (GObjectClass*) klass;
128 gstelement_class = (GstElementClass*) klass;
130 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
132 gst_speexenc_signals[FRAME_ENCODED] =
133 g_signal_new ("frame_encoded", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
134 G_STRUCT_OFFSET (GstSpeexEncClass, frame_encoded), NULL, NULL,
135 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
140 gst_speexenc_init (GstSpeexEnc *speexenc)
142 /* create the sink and src pads */
143 speexenc->sinkpad = gst_pad_new_from_template (speexenc_sink_template, "sink");
144 gst_element_add_pad (GST_ELEMENT (speexenc), speexenc->sinkpad);
145 gst_pad_set_chain_function (speexenc->sinkpad, gst_speexenc_chain);
146 gst_pad_set_link_function (speexenc->sinkpad, gst_speexenc_sinkconnect);
148 speexenc->srcpad = gst_pad_new_from_template (speexenc_src_template, "src");
149 gst_element_add_pad (GST_ELEMENT (speexenc), speexenc->srcpad);
151 speex_bits_init(&speexenc->bits);
152 speexenc->mode = &speex_nb_mode;
153 speexenc->bufsize = 0;
154 speexenc->packet_count = 0;
155 speexenc->n_packets = 20;
158 static GstPadLinkReturn
159 gst_speexenc_sinkconnect (GstPad *pad, const GstCaps *caps)
161 GstSpeexEnc *speexenc;
162 GstStructure *structure;
164 speexenc = GST_SPEEXENC (gst_pad_get_parent (pad));
166 structure = gst_caps_get_structure (caps, 0);
167 gst_structure_get_int (structure, "rate", &speexenc->rate);
168 if (gst_pad_try_set_caps (speexenc->srcpad,
169 gst_caps_new_simple ("audio/x-speex",
170 "rate", G_TYPE_INT, speexenc->rate,
171 "channels", G_TYPE_INT, 1,
174 speex_init_header(&speexenc->header, speexenc->rate, 1, speexenc->mode);
175 speexenc->header.frames_per_packet = speexenc->n_packets;
177 speexenc->state = speex_encoder_init(speexenc->mode);
178 speex_encoder_ctl(speexenc->state, SPEEX_GET_FRAME_SIZE, &speexenc->frame_size);
180 return GST_PAD_LINK_OK;
182 return GST_PAD_LINK_REFUSED;
187 gst_speexenc_chain (GstPad *pad, GstData *_data)
189 GstBuffer *buf = GST_BUFFER (_data);
190 GstSpeexEnc *speexenc;
199 g_return_if_fail (pad != NULL);
200 g_return_if_fail (GST_IS_PAD (pad));
201 g_return_if_fail (buf != NULL);
203 speexenc = GST_SPEEXENC (GST_OBJECT_PARENT (pad));
205 if (!GST_PAD_CAPS (speexenc->srcpad)) {
207 if (!gst_pad_try_set_caps (speexenc->srcpad,
208 gst_caps_new_simple ("audio/x-speex",
209 "rate", G_TYPE_INT, speexenc->rate,
210 "channels", G_TYPE_INT, 1,
213 GST_ELEMENT_ERROR (speexenc, CORE, NEGOTIATION, NULL, NULL);
218 if (speexenc->packet_count == 0) {
219 header_data = speex_header_to_packet(&speexenc->header, &size);
221 outbuf = gst_buffer_new ();
222 GST_BUFFER_DATA (outbuf) = header_data;
223 GST_BUFFER_SIZE (outbuf) = size;
225 gst_pad_push (speexenc->srcpad, GST_DATA (outbuf));
228 data = (gint16 *) GST_BUFFER_DATA (buf);
229 size = GST_BUFFER_SIZE (buf) / sizeof (gint16);
231 frame_size = speexenc->frame_size;
233 if (speexenc->bufsize && (speexenc->bufsize + size >= frame_size)) {
234 memcpy (speexenc->buffer + speexenc->bufsize, data, (frame_size - speexenc->bufsize) * sizeof (gint16));
236 for (i = 0; i < frame_size; i++)
237 input[i] = speexenc->buffer[i];
239 speex_encode (speexenc->state, input, &speexenc->bits);
240 speexenc->packet_count++;
242 if (speexenc->packet_count % speexenc->n_packets == 0) {
245 outbuf = gst_buffer_new_and_alloc (frame_size * speexenc->n_packets);
246 GST_BUFFER_SIZE (outbuf) = speex_bits_write(&speexenc->bits,
247 GST_BUFFER_DATA (outbuf), GST_BUFFER_SIZE (outbuf));
248 GST_BUFFER_TIMESTAMP (outbuf) = speexenc->next_ts;
249 speex_bits_reset(&speexenc->bits);
251 gst_pad_push (speexenc->srcpad, GST_DATA (outbuf));
252 speexenc->next_ts += frame_size * GST_SECOND / speexenc->rate;
255 size -= (speexenc->frame_size - speexenc->bufsize);
256 data += (speexenc->frame_size - speexenc->bufsize);
258 speexenc->bufsize = 0;
261 while (size >= frame_size) {
262 for (i = 0; i < frame_size; i++)
265 speex_encode (speexenc->state, input, &speexenc->bits);
266 speexenc->packet_count++;
268 if (speexenc->packet_count % speexenc->n_packets == 0) {
271 outbuf = gst_buffer_new_and_alloc (frame_size * speexenc->n_packets);
272 GST_BUFFER_SIZE (outbuf) = speex_bits_write(&speexenc->bits,
273 GST_BUFFER_DATA (outbuf), GST_BUFFER_SIZE (outbuf));
274 GST_BUFFER_TIMESTAMP (outbuf) = speexenc->next_ts;
275 speex_bits_reset(&speexenc->bits);
277 gst_pad_push (speexenc->srcpad, GST_DATA (outbuf));
278 speexenc->next_ts += frame_size * GST_SECOND / speexenc->rate;
286 memcpy (speexenc->buffer + speexenc->bufsize, data, size * sizeof (gint16));
287 speexenc->bufsize += size;
290 gst_buffer_unref(buf);