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.
23 #include "gstspeexenc.h"
25 extern GstPadTemplate *speexenc_src_template, *speexenc_sink_template;
27 /* elementfactory information */
28 GstElementDetails gst_speexenc_details = {
29 "speex audio encoder",
30 "Codec/Audio/Encoder",
33 "Wim Taymans <wim.taymans@chello.be>",
37 /* SpeexEnc signals and args */
49 static void gst_speexenc_class_init (GstSpeexEnc *klass);
50 static void gst_speexenc_init (GstSpeexEnc *speexenc);
52 static void gst_speexenc_chain (GstPad *pad,GstBuffer *buf);
53 static GstPadLinkReturn gst_speexenc_sinkconnect (GstPad *pad, GstCaps *caps);
55 static GstElementClass *parent_class = NULL;
56 static guint gst_speexenc_signals[LAST_SIGNAL] = { 0 };
59 gst_speexenc_get_type (void)
61 static GType speexenc_type = 0;
64 static const GTypeInfo speexenc_info = {
65 sizeof (GstSpeexEncClass),
68 (GClassInitFunc) gst_speexenc_class_init,
73 (GInstanceInitFunc) gst_speexenc_init,
75 speexenc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstSpeexEnc", &speexenc_info, 0);
81 gst_speexenc_class_init (GstSpeexEnc *klass)
83 GObjectClass *gobject_class;
84 GstElementClass *gstelement_class;
86 gobject_class = (GObjectClass*) klass;
87 gstelement_class = (GstElementClass*) klass;
89 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
91 gst_speexenc_signals[FRAME_ENCODED] =
92 g_signal_new ("frame_encoded", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
93 G_STRUCT_OFFSET (GstSpeexEncClass, frame_encoded), NULL, NULL,
94 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
99 gst_speexenc_init (GstSpeexEnc *speexenc)
101 /* create the sink and src pads */
102 speexenc->sinkpad = gst_pad_new_from_template (speexenc_sink_template, "sink");
103 gst_element_add_pad (GST_ELEMENT (speexenc), speexenc->sinkpad);
104 gst_pad_set_chain_function (speexenc->sinkpad, gst_speexenc_chain);
105 gst_pad_set_link_function (speexenc->sinkpad, gst_speexenc_sinkconnect);
107 speexenc->srcpad = gst_pad_new_from_template (speexenc_src_template, "src");
108 gst_element_add_pad (GST_ELEMENT (speexenc), speexenc->srcpad);
110 speex_bits_init(&speexenc->bits);
111 speexenc->mode = &speex_nb_mode;
112 speexenc->bufsize = 0;
113 speexenc->packet_count = 0;
114 speexenc->n_packets = 20;
117 static GstPadLinkReturn
118 gst_speexenc_sinkconnect (GstPad *pad, GstCaps *caps)
120 GstSpeexEnc *speexenc;
122 speexenc = GST_SPEEXENC (gst_pad_get_parent (pad));
124 if (!GST_CAPS_IS_FIXED (caps))
125 return GST_PAD_LINK_DELAYED;
127 gst_caps_get_int (caps, "rate", &speexenc->rate);
128 if (gst_pad_try_set_caps (speexenc->srcpad, GST_CAPS_NEW (
131 "rate", GST_PROPS_INT (speexenc->rate)
134 speex_init_header(&speexenc->header, speexenc->rate, 1, speexenc->mode);
135 speexenc->header.frames_per_packet = speexenc->n_packets;
137 speexenc->state = speex_encoder_init(speexenc->mode);
138 speex_encoder_ctl(speexenc->state, SPEEX_GET_FRAME_SIZE, &speexenc->frame_size);
140 return GST_PAD_LINK_OK;
142 return GST_PAD_LINK_REFUSED;
147 gst_speexenc_chain (GstPad *pad, GstBuffer *buf)
149 GstSpeexEnc *speexenc;
158 g_return_if_fail (pad != NULL);
159 g_return_if_fail (GST_IS_PAD (pad));
160 g_return_if_fail (buf != NULL);
162 speexenc = GST_SPEEXENC (GST_OBJECT_PARENT (pad));
164 if (!GST_PAD_CAPS (speexenc->srcpad)) {
166 if (!gst_pad_try_set_caps (speexenc->srcpad,
170 "rate", GST_PROPS_INT (speexenc->rate)
173 gst_element_error (GST_ELEMENT (speexenc), "could not negotiate");
178 if (speexenc->packet_count == 0) {
179 header_data = speex_header_to_packet(&speexenc->header, &size);
181 outbuf = gst_buffer_new ();
182 GST_BUFFER_DATA (outbuf) = header_data;
183 GST_BUFFER_SIZE (outbuf) = size;
185 gst_pad_push (speexenc->srcpad, outbuf);
188 data = (gint16 *) GST_BUFFER_DATA (buf);
189 size = GST_BUFFER_SIZE (buf) / sizeof (gint16);
191 frame_size = speexenc->frame_size;
193 if (speexenc->bufsize && (speexenc->bufsize + size >= frame_size)) {
194 memcpy (speexenc->buffer + speexenc->bufsize, data, (frame_size - speexenc->bufsize) * sizeof (gint16));
196 for (i = 0; i < frame_size; i++)
197 input[i] = speexenc->buffer[i];
199 speex_encode (speexenc->state, input, &speexenc->bits);
200 speexenc->packet_count++;
202 if (speexenc->packet_count % speexenc->n_packets == 0) {
205 outbuf = gst_buffer_new_and_alloc (frame_size * speexenc->n_packets);
206 GST_BUFFER_SIZE (outbuf) = speex_bits_write(&speexenc->bits,
207 GST_BUFFER_DATA (outbuf), GST_BUFFER_SIZE (outbuf));
208 GST_BUFFER_TIMESTAMP (outbuf) = speexenc->next_ts;
209 speex_bits_reset(&speexenc->bits);
211 gst_pad_push (speexenc->srcpad, outbuf);
212 speexenc->next_ts += frame_size * GST_SECOND / speexenc->rate;
215 size -= (speexenc->frame_size - speexenc->bufsize);
216 data += (speexenc->frame_size - speexenc->bufsize);
218 speexenc->bufsize = 0;
221 while (size >= frame_size) {
222 for (i = 0; i < frame_size; i++)
225 speex_encode (speexenc->state, input, &speexenc->bits);
226 speexenc->packet_count++;
228 if (speexenc->packet_count % speexenc->n_packets == 0) {
231 outbuf = gst_buffer_new_and_alloc (frame_size * speexenc->n_packets);
232 GST_BUFFER_SIZE (outbuf) = speex_bits_write(&speexenc->bits,
233 GST_BUFFER_DATA (outbuf), GST_BUFFER_SIZE (outbuf));
234 GST_BUFFER_TIMESTAMP (outbuf) = speexenc->next_ts;
235 speex_bits_reset(&speexenc->bits);
237 gst_pad_push (speexenc->srcpad, outbuf);
238 speexenc->next_ts += frame_size * GST_SECOND / speexenc->rate;
246 memcpy (speexenc->buffer + speexenc->bufsize, data, size * sizeof (gint16));
247 speexenc->bufsize += size;
250 gst_buffer_unref(buf);