PadConnect -> PadLink
[platform/upstream/gstreamer.git] / ext / speex / gstspeexenc.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20
21 #include <string.h>
22
23 #include "gstspeexenc.h"
24
25 extern GstPadTemplate *speexenc_src_template, *speexenc_sink_template;
26
27 /* elementfactory information */
28 GstElementDetails gst_speexenc_details = {
29   "speex audio encoder",
30   "Codec/Audio/Encoder",
31   ".speex",
32   VERSION,
33   "Wim Taymans <wim.taymans@chello.be>",
34   "(C) 2000",
35 };
36
37 /* SpeexEnc signals and args */
38 enum {
39   FRAME_ENCODED,
40   /* FILL ME */
41   LAST_SIGNAL
42 };
43
44 enum {
45   ARG_0,
46   /* FILL ME */
47 };
48
49 static void                     gst_speexenc_class_init (GstSpeexEnc *klass);
50 static void                     gst_speexenc_init               (GstSpeexEnc *speexenc);
51
52 static void                     gst_speexenc_chain      (GstPad *pad,GstBuffer *buf);
53 static GstPadLinkReturn gst_speexenc_sinkconnect        (GstPad *pad, GstCaps *caps);
54
55 static GstElementClass *parent_class = NULL;
56 static guint gst_speexenc_signals[LAST_SIGNAL] = { 0 };
57
58 GType
59 gst_speexenc_get_type (void)
60 {
61   static GType speexenc_type = 0;
62
63   if (!speexenc_type) {
64     static const GTypeInfo speexenc_info = {
65       sizeof (GstSpeexEncClass),
66       NULL,
67       NULL,
68       (GClassInitFunc) gst_speexenc_class_init,
69       NULL,
70       NULL,
71       sizeof (GstSpeexEnc),
72       0,
73       (GInstanceInitFunc) gst_speexenc_init,
74     };
75     speexenc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstSpeexEnc", &speexenc_info, 0);
76   }
77   return speexenc_type;
78 }
79
80 static void
81 gst_speexenc_class_init (GstSpeexEnc *klass)
82 {
83   GObjectClass *gobject_class;
84   GstElementClass *gstelement_class;
85
86   gobject_class = (GObjectClass*) klass;
87   gstelement_class = (GstElementClass*) klass;
88
89   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
90
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);
95 }
96
97
98 static void
99 gst_speexenc_init (GstSpeexEnc *speexenc)
100 {
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);
106
107   speexenc->srcpad = gst_pad_new_from_template (speexenc_src_template, "src");
108   gst_element_add_pad (GST_ELEMENT (speexenc), speexenc->srcpad);
109
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;
115 }
116
117 static GstPadLinkReturn
118 gst_speexenc_sinkconnect (GstPad *pad, GstCaps *caps)
119 {
120   GstSpeexEnc *speexenc;
121
122   speexenc = GST_SPEEXENC (gst_pad_get_parent (pad));
123
124   if (!GST_CAPS_IS_FIXED (caps)) 
125     return GST_PAD_LINK_DELAYED;
126
127   gst_caps_get_int (caps, "rate", &speexenc->rate);
128   if (gst_pad_try_set_caps (speexenc->srcpad, GST_CAPS_NEW (
129                               "speex_speex",
130                               "audio/x-speex",
131                                 "rate",       GST_PROPS_INT (speexenc->rate)
132                                )))
133   {
134     speex_init_header(&speexenc->header, speexenc->rate, 1, speexenc->mode);
135     speexenc->header.frames_per_packet = speexenc->n_packets;
136
137     speexenc->state = speex_encoder_init(speexenc->mode);
138     speex_encoder_ctl(speexenc->state, SPEEX_GET_FRAME_SIZE, &speexenc->frame_size);
139
140     return GST_PAD_LINK_OK;
141   }
142   return GST_PAD_LINK_REFUSED;
143
144 }
145
146 static void
147 gst_speexenc_chain (GstPad *pad, GstBuffer *buf)
148 {
149   GstSpeexEnc *speexenc;
150   GstBuffer *outbuf;
151   gint16 *data;
152   guint8 *header_data;
153   gint size;
154   float input[1000];
155   gint frame_size;
156   gint i;
157
158   g_return_if_fail (pad != NULL);
159   g_return_if_fail (GST_IS_PAD (pad));
160   g_return_if_fail (buf != NULL);
161
162   speexenc = GST_SPEEXENC (GST_OBJECT_PARENT (pad));
163               
164   if (!GST_PAD_CAPS (speexenc->srcpad)) {
165
166     if (!gst_pad_try_set_caps (speexenc->srcpad,
167                       GST_CAPS_NEW (
168                         "speex_enc",
169                         "audio/x-speex",
170                         "rate",  GST_PROPS_INT (speexenc->rate)
171                       )))
172     {
173       gst_element_error (GST_ELEMENT (speexenc), "could not negotiate");
174       return;
175     }
176   }
177
178   if (speexenc->packet_count == 0) {
179     header_data = speex_header_to_packet(&speexenc->header, &size);
180
181     outbuf = gst_buffer_new ();
182     GST_BUFFER_DATA (outbuf) = header_data;
183     GST_BUFFER_SIZE (outbuf) = size;
184
185     gst_pad_push (speexenc->srcpad, outbuf);
186   }
187
188   data = (gint16 *) GST_BUFFER_DATA (buf);
189   size = GST_BUFFER_SIZE (buf) / sizeof (gint16);
190
191   frame_size = speexenc->frame_size;
192
193   if (speexenc->bufsize && (speexenc->bufsize + size >= frame_size)) {
194     memcpy (speexenc->buffer + speexenc->bufsize, data, (frame_size - speexenc->bufsize) * sizeof (gint16));
195
196     for (i = 0; i < frame_size; i++)
197       input[i] = speexenc->buffer[i];
198
199     speex_encode (speexenc->state, input, &speexenc->bits);
200     speexenc->packet_count++;
201
202     if (speexenc->packet_count % speexenc->n_packets == 0) {
203       GstBuffer *outbuf;
204
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);
210
211       gst_pad_push (speexenc->srcpad, outbuf);
212       speexenc->next_ts += frame_size * GST_SECOND / speexenc->rate;
213     }
214
215     size -= (speexenc->frame_size - speexenc->bufsize);
216     data += (speexenc->frame_size - speexenc->bufsize);
217
218     speexenc->bufsize = 0;
219   }
220
221   while (size >= frame_size) {
222     for (i = 0; i < frame_size; i++)
223       input[i] = data[i];
224
225     speex_encode (speexenc->state, input, &speexenc->bits);
226     speexenc->packet_count++;
227
228     if (speexenc->packet_count % speexenc->n_packets == 0) {
229       GstBuffer *outbuf;
230
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);
236
237       gst_pad_push (speexenc->srcpad, outbuf);
238       speexenc->next_ts += frame_size * GST_SECOND / speexenc->rate;
239     }
240
241     size -= frame_size;
242     data += frame_size;
243   }
244
245   if (size) {
246     memcpy (speexenc->buffer + speexenc->bufsize, data, size * sizeof (gint16));
247     speexenc->bufsize += size;
248   }
249   
250   gst_buffer_unref(buf);
251 }