- reenable speex plugins for speex 1.1.5 (API/ABI compatible with 1.0.x)
[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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 #include <string.h>
25
26 #include "gstspeexenc.h"
27
28 /* elementfactory information */
29 GstElementDetails gst_speexenc_details = {
30   "speex audio encoder",
31   "Codec/Encoder/Audio",
32   ".speex",
33   "Wim Taymans <wim.taymans@chello.be>",
34 };
35
36 /* SpeexEnc signals and args */
37 enum
38 {
39   FRAME_ENCODED,
40   /* FILL ME */
41   LAST_SIGNAL
42 };
43
44 enum
45 {
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,
56     const GstCaps * caps);
57
58 static GstElementClass *parent_class = NULL;
59 static guint gst_speexenc_signals[LAST_SIGNAL] = { 0 };
60
61 GType
62 gst_speexenc_get_type (void)
63 {
64   static GType speexenc_type = 0;
65
66   if (!speexenc_type) {
67     static const GTypeInfo speexenc_info = {
68       sizeof (GstSpeexEncClass),
69       gst_speexenc_base_init,
70       NULL,
71       (GClassInitFunc) gst_speexenc_class_init,
72       NULL,
73       NULL,
74       sizeof (GstSpeexEnc),
75       0,
76       (GInstanceInitFunc) gst_speexenc_init,
77     };
78
79     speexenc_type =
80         g_type_register_static (GST_TYPE_ELEMENT, "GstSpeexEnc", &speexenc_info,
81         0);
82   }
83   return speexenc_type;
84 }
85
86 static GstStaticPadTemplate speexenc_sink_template =
87 GST_STATIC_PAD_TEMPLATE ("sink",
88     GST_PAD_SINK,
89     GST_PAD_ALWAYS,
90     GST_STATIC_CAPS ("audio/x-raw-int, "
91         "endianness = (int) BYTE_ORDER, "
92         "signed = (boolean) true, "
93         "width = (int) 16, "
94         "depth = (int) 16, "
95         "rate = (int) [ 1000, 48000 ], " "channels = (int) 1")
96     );
97
98 static GstStaticPadTemplate speexenc_src_template =
99 GST_STATIC_PAD_TEMPLATE ("src",
100     GST_PAD_SRC,
101     GST_PAD_ALWAYS,
102     GST_STATIC_CAPS ("audio/x-speex, "
103         "rate = (int) [ 1000, 48000 ], " "channels = (int) 1")
104     );
105
106 static void
107 gst_speexenc_base_init (gpointer g_class)
108 {
109   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
110
111   gst_element_class_add_pad_template (element_class,
112       gst_static_pad_template_get (&speexenc_sink_template));
113   gst_element_class_add_pad_template (element_class,
114       gst_static_pad_template_get (&speexenc_src_template));
115
116   gst_element_class_set_details (element_class, &gst_speexenc_details);
117 }
118
119 static void
120 gst_speexenc_class_init (GstSpeexEnc * klass)
121 {
122   GObjectClass *gobject_class;
123   GstElementClass *gstelement_class;
124
125   gobject_class = (GObjectClass *) klass;
126   gstelement_class = (GstElementClass *) klass;
127
128   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
129
130   gst_speexenc_signals[FRAME_ENCODED] =
131       g_signal_new ("frame-encoded", G_TYPE_FROM_CLASS (klass),
132       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstSpeexEncClass, frame_encoded),
133       NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
134 }
135
136
137 static void
138 gst_speexenc_init (GstSpeexEnc * speexenc)
139 {
140   /* create the sink and src pads */
141   speexenc->sinkpad =
142       gst_pad_new_from_template (gst_static_pad_template_get
143       (&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 =
149       gst_pad_new_from_template (gst_static_pad_template_get
150       (&speexenc_src_template), "src");
151   gst_element_add_pad (GST_ELEMENT (speexenc), speexenc->srcpad);
152
153   speex_bits_init (&speexenc->bits);
154   speexenc->mode = (SpeexMode *) & speex_nb_mode;
155   speexenc->bufsize = 0;
156   speexenc->packet_count = 0;
157   speexenc->n_packets = 20;
158 }
159
160 static GstPadLinkReturn
161 gst_speexenc_sinkconnect (GstPad * pad, const GstCaps * caps)
162 {
163   GstSpeexEnc *speexenc;
164   GstStructure *structure;
165
166   speexenc = GST_SPEEXENC (gst_pad_get_parent (pad));
167
168   structure = gst_caps_get_structure (caps, 0);
169   gst_structure_get_int (structure, "rate", &speexenc->rate);
170   if (gst_pad_try_set_caps (speexenc->srcpad,
171           gst_caps_new_simple ("audio/x-speex",
172               "rate", G_TYPE_INT, speexenc->rate,
173               "channels", G_TYPE_INT, 1, NULL))) {
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,
179         &speexenc->frame_size);
180
181     return GST_PAD_LINK_OK;
182   }
183   return GST_PAD_LINK_REFUSED;
184
185 }
186
187 static void
188 gst_speexenc_chain (GstPad * pad, GstData * _data)
189 {
190   GstBuffer *buf = GST_BUFFER (_data);
191   GstSpeexEnc *speexenc;
192   GstBuffer *outbuf;
193   gint16 *data;
194   guint8 *header_data;
195   gint size;
196   float input[1000];
197   gint frame_size;
198   gint i;
199
200   g_return_if_fail (pad != NULL);
201   g_return_if_fail (GST_IS_PAD (pad));
202   g_return_if_fail (buf != NULL);
203
204   speexenc = GST_SPEEXENC (GST_OBJECT_PARENT (pad));
205
206   if (!GST_PAD_CAPS (speexenc->srcpad)) {
207
208     if (!gst_pad_try_set_caps (speexenc->srcpad,
209             gst_caps_new_simple ("audio/x-speex",
210                 "rate", G_TYPE_INT, speexenc->rate,
211                 "channels", G_TYPE_INT, 1, NULL))) {
212       GST_ELEMENT_ERROR (speexenc, CORE, NEGOTIATION, (NULL), (NULL));
213       return;
214     }
215   }
216
217   if (speexenc->packet_count == 0) {
218     header_data = speex_header_to_packet (&speexenc->header, &size);
219
220     outbuf = gst_buffer_new ();
221     GST_BUFFER_DATA (outbuf) = header_data;
222     GST_BUFFER_SIZE (outbuf) = size;
223
224     gst_pad_push (speexenc->srcpad, GST_DATA (outbuf));
225   }
226
227   data = (gint16 *) GST_BUFFER_DATA (buf);
228   size = GST_BUFFER_SIZE (buf) / sizeof (gint16);
229
230   frame_size = speexenc->frame_size;
231
232   if (speexenc->bufsize && (speexenc->bufsize + size >= frame_size)) {
233     memcpy (speexenc->buffer + speexenc->bufsize, data,
234         (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 }