a4eb453218ab8042bfde52d17863ad222fe625ab
[platform/upstream/gst-plugins-good.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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 #include <string.h>
25
26 #include "gstspeexenc.h"
27
28 static GstPadTemplate *speexenc_src_template, *speexenc_sink_template;
29
30 /* elementfactory information */
31 GstElementDetails gst_speexenc_details = {
32   "speex audio encoder",
33   "Codec/Audio/Encoder",
34   ".speex",
35   "Wim Taymans <wim.taymans@chello.be>",
36 };
37
38 /* SpeexEnc signals and args */
39 enum {
40   FRAME_ENCODED,
41   /* FILL ME */
42   LAST_SIGNAL
43 };
44
45 enum {
46   ARG_0,
47   /* FILL ME */
48 };
49
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);
53
54 static void                     gst_speexenc_chain      (GstPad *pad,GstData *_data);
55 static GstPadLinkReturn gst_speexenc_sinkconnect        (GstPad *pad, const GstCaps *caps);
56
57 static GstElementClass *parent_class = NULL;
58 static guint gst_speexenc_signals[LAST_SIGNAL] = { 0 };
59
60 GType
61 gst_speexenc_get_type (void)
62 {
63   static GType speexenc_type = 0;
64
65   if (!speexenc_type) {
66     static const GTypeInfo speexenc_info = {
67       sizeof (GstSpeexEncClass),
68       gst_speexenc_base_init,
69       NULL,
70       (GClassInitFunc) gst_speexenc_class_init,
71       NULL,
72       NULL,
73       sizeof (GstSpeexEnc),
74       0,
75       (GInstanceInitFunc) gst_speexenc_init,
76     };
77     speexenc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstSpeexEnc", &speexenc_info, 0);
78   }
79   return speexenc_type;
80 }
81
82 static GstStaticPadTemplate speex_sink_template =
83 GST_STATIC_PAD_TEMPLATE (
84   "sink",
85   GST_PAD_SINK,
86   GST_PAD_ALWAYS,
87   GST_STATIC_CAPS ("audio/x-raw-int, "
88       "endianness = (int) BYTE_ORDER, "
89       "signed = (boolean) true, "
90       "width = (int) 16, "
91       "depth = (int) 16, "
92       "rate = (int) [ 1000, 48000 ], "
93       "channels = (int) 1"
94   )
95 );
96
97 static GstStaticPadTemplate speex_src_template =
98 GST_STATIC_PAD_TEMPLATE (
99   "src",
100   GST_PAD_SRC,
101   GST_PAD_ALWAYS,
102   GST_STATIC_CAPS ("audio/x-speex, "
103       "rate = (int) [ 1000, 48000 ], "
104       "channels = (int) 1"
105   )
106 );
107
108 static void
109 gst_speexenc_base_init (gpointer g_class)
110 {
111   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
112   
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));
117
118   gst_element_class_set_details (element_class, &gst_speexenc_details);
119 }
120
121 static void
122 gst_speexenc_class_init (GstSpeexEnc *klass)
123 {
124   GObjectClass *gobject_class;
125   GstElementClass *gstelement_class;
126
127   gobject_class = (GObjectClass*) klass;
128   gstelement_class = (GstElementClass*) klass;
129
130   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
131
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);
136 }
137
138
139 static void
140 gst_speexenc_init (GstSpeexEnc *speexenc)
141 {
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);
147
148   speexenc->srcpad = gst_pad_new_from_template (speexenc_src_template, "src");
149   gst_element_add_pad (GST_ELEMENT (speexenc), speexenc->srcpad);
150
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;
156 }
157
158 static GstPadLinkReturn
159 gst_speexenc_sinkconnect (GstPad *pad, const GstCaps *caps)
160 {
161   GstSpeexEnc *speexenc;
162   GstStructure *structure;
163
164   speexenc = GST_SPEEXENC (gst_pad_get_parent (pad));
165
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,
172           NULL)))
173   {
174     speex_init_header(&speexenc->header, speexenc->rate, 1, speexenc->mode);
175     speexenc->header.frames_per_packet = speexenc->n_packets;
176
177     speexenc->state = speex_encoder_init(speexenc->mode);
178     speex_encoder_ctl(speexenc->state, SPEEX_GET_FRAME_SIZE, &speexenc->frame_size);
179
180     return GST_PAD_LINK_OK;
181   }
182   return GST_PAD_LINK_REFUSED;
183
184 }
185
186 static void
187 gst_speexenc_chain (GstPad *pad, GstData *_data)
188 {
189   GstBuffer *buf = GST_BUFFER (_data);
190   GstSpeexEnc *speexenc;
191   GstBuffer *outbuf;
192   gint16 *data;
193   guint8 *header_data;
194   gint size;
195   float input[1000];
196   gint frame_size;
197   gint i;
198
199   g_return_if_fail (pad != NULL);
200   g_return_if_fail (GST_IS_PAD (pad));
201   g_return_if_fail (buf != NULL);
202
203   speexenc = GST_SPEEXENC (GST_OBJECT_PARENT (pad));
204               
205   if (!GST_PAD_CAPS (speexenc->srcpad)) {
206
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,
211             NULL)))
212     {
213       GST_ELEMENT_ERROR (speexenc, CORE, NEGOTIATION, NULL, NULL);
214       return;
215     }
216   }
217
218   if (speexenc->packet_count == 0) {
219     header_data = speex_header_to_packet(&speexenc->header, &size);
220
221     outbuf = gst_buffer_new ();
222     GST_BUFFER_DATA (outbuf) = header_data;
223     GST_BUFFER_SIZE (outbuf) = size;
224
225     gst_pad_push (speexenc->srcpad, GST_DATA (outbuf));
226   }
227
228   data = (gint16 *) GST_BUFFER_DATA (buf);
229   size = GST_BUFFER_SIZE (buf) / sizeof (gint16);
230
231   frame_size = speexenc->frame_size;
232
233   if (speexenc->bufsize && (speexenc->bufsize + size >= frame_size)) {
234     memcpy (speexenc->buffer + speexenc->bufsize, data, (frame_size - speexenc->bufsize) * sizeof (gint16));
235
236     for (i = 0; i < frame_size; i++)
237       input[i] = speexenc->buffer[i];
238
239     speex_encode (speexenc->state, input, &speexenc->bits);
240     speexenc->packet_count++;
241
242     if (speexenc->packet_count % speexenc->n_packets == 0) {
243       GstBuffer *outbuf;
244
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);
250
251       gst_pad_push (speexenc->srcpad, GST_DATA (outbuf));
252       speexenc->next_ts += frame_size * GST_SECOND / speexenc->rate;
253     }
254
255     size -= (speexenc->frame_size - speexenc->bufsize);
256     data += (speexenc->frame_size - speexenc->bufsize);
257
258     speexenc->bufsize = 0;
259   }
260
261   while (size >= frame_size) {
262     for (i = 0; i < frame_size; i++)
263       input[i] = data[i];
264
265     speex_encode (speexenc->state, input, &speexenc->bits);
266     speexenc->packet_count++;
267
268     if (speexenc->packet_count % speexenc->n_packets == 0) {
269       GstBuffer *outbuf;
270
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);
276
277       gst_pad_push (speexenc->srcpad, GST_DATA (outbuf));
278       speexenc->next_ts += frame_size * GST_SECOND / speexenc->rate;
279     }
280
281     size -= frame_size;
282     data += frame_size;
283   }
284
285   if (size) {
286     memcpy (speexenc->buffer + speexenc->bufsize, data, size * sizeof (gint16));
287     speexenc->bufsize += size;
288   }
289   
290   gst_buffer_unref(buf);
291 }