don't mix tabs and spaces
[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 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 {
41   FRAME_ENCODED,
42   /* FILL ME */
43   LAST_SIGNAL
44 };
45
46 enum
47 {
48   ARG_0,
49   /* FILL ME */
50 };
51
52 static void gst_speexenc_base_init (gpointer g_class);
53 static void gst_speexenc_class_init (GstSpeexEnc * klass);
54 static void gst_speexenc_init (GstSpeexEnc * speexenc);
55
56 static void gst_speexenc_chain (GstPad * pad, GstData * _data);
57 static GstPadLinkReturn gst_speexenc_sinkconnect (GstPad * pad,
58     const GstCaps * caps);
59
60 static GstElementClass *parent_class = NULL;
61 static guint gst_speexenc_signals[LAST_SIGNAL] = { 0 };
62
63 GType
64 gst_speexenc_get_type (void)
65 {
66   static GType speexenc_type = 0;
67
68   if (!speexenc_type) {
69     static const GTypeInfo speexenc_info = {
70       sizeof (GstSpeexEncClass),
71       gst_speexenc_base_init,
72       NULL,
73       (GClassInitFunc) gst_speexenc_class_init,
74       NULL,
75       NULL,
76       sizeof (GstSpeexEnc),
77       0,
78       (GInstanceInitFunc) gst_speexenc_init,
79     };
80
81     speexenc_type =
82         g_type_register_static (GST_TYPE_ELEMENT, "GstSpeexEnc", &speexenc_info,
83         0);
84   }
85   return speexenc_type;
86 }
87
88 static GstStaticPadTemplate speex_sink_template =
89 GST_STATIC_PAD_TEMPLATE ("sink",
90     GST_PAD_SINK,
91     GST_PAD_ALWAYS,
92     GST_STATIC_CAPS ("audio/x-raw-int, "
93         "endianness = (int) BYTE_ORDER, "
94         "signed = (boolean) true, "
95         "width = (int) 16, "
96         "depth = (int) 16, "
97         "rate = (int) [ 1000, 48000 ], " "channels = (int) 1")
98     );
99
100 static GstStaticPadTemplate speex_src_template = GST_STATIC_PAD_TEMPLATE ("src",
101     GST_PAD_SRC,
102     GST_PAD_ALWAYS,
103     GST_STATIC_CAPS ("audio/x-speex, "
104         "rate = (int) [ 1000, 48000 ], " "channels = (int) 1")
105     );
106
107 static void
108 gst_speexenc_base_init (gpointer g_class)
109 {
110   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
111
112   gst_element_class_add_pad_template (element_class,
113       gst_static_pad_template_get (&speex_sink_template));
114   gst_element_class_add_pad_template (element_class,
115       gst_static_pad_template_get (&speex_src_template));
116
117   gst_element_class_set_details (element_class, &gst_speexenc_details);
118 }
119
120 static void
121 gst_speexenc_class_init (GstSpeexEnc * klass)
122 {
123   GObjectClass *gobject_class;
124   GstElementClass *gstelement_class;
125
126   gobject_class = (GObjectClass *) klass;
127   gstelement_class = (GstElementClass *) klass;
128
129   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
130
131   gst_speexenc_signals[FRAME_ENCODED] =
132       g_signal_new ("frame-encoded", G_TYPE_FROM_CLASS (klass),
133       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstSpeexEncClass, frame_encoded),
134       NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
135 }
136
137
138 static void
139 gst_speexenc_init (GstSpeexEnc * speexenc)
140 {
141   /* create the sink and src pads */
142   speexenc->sinkpad =
143       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, NULL))) {
172     speex_init_header (&speexenc->header, speexenc->rate, 1, speexenc->mode);
173     speexenc->header.frames_per_packet = speexenc->n_packets;
174
175     speexenc->state = speex_encoder_init (speexenc->mode);
176     speex_encoder_ctl (speexenc->state, SPEEX_GET_FRAME_SIZE,
177         &speexenc->frame_size);
178
179     return GST_PAD_LINK_OK;
180   }
181   return GST_PAD_LINK_REFUSED;
182
183 }
184
185 static void
186 gst_speexenc_chain (GstPad * pad, GstData * _data)
187 {
188   GstBuffer *buf = GST_BUFFER (_data);
189   GstSpeexEnc *speexenc;
190   GstBuffer *outbuf;
191   gint16 *data;
192   guint8 *header_data;
193   gint size;
194   float input[1000];
195   gint frame_size;
196   gint i;
197
198   g_return_if_fail (pad != NULL);
199   g_return_if_fail (GST_IS_PAD (pad));
200   g_return_if_fail (buf != NULL);
201
202   speexenc = GST_SPEEXENC (GST_OBJECT_PARENT (pad));
203
204   if (!GST_PAD_CAPS (speexenc->srcpad)) {
205
206     if (!gst_pad_try_set_caps (speexenc->srcpad,
207             gst_caps_new_simple ("audio/x-speex",
208                 "rate", G_TYPE_INT, speexenc->rate,
209                 "channels", G_TYPE_INT, 1, NULL))) {
210       GST_ELEMENT_ERROR (speexenc, CORE, NEGOTIATION, (NULL), (NULL));
211       return;
212     }
213   }
214
215   if (speexenc->packet_count == 0) {
216     header_data = speex_header_to_packet (&speexenc->header, &size);
217
218     outbuf = gst_buffer_new ();
219     GST_BUFFER_DATA (outbuf) = header_data;
220     GST_BUFFER_SIZE (outbuf) = size;
221
222     gst_pad_push (speexenc->srcpad, GST_DATA (outbuf));
223   }
224
225   data = (gint16 *) GST_BUFFER_DATA (buf);
226   size = GST_BUFFER_SIZE (buf) / sizeof (gint16);
227
228   frame_size = speexenc->frame_size;
229
230   if (speexenc->bufsize && (speexenc->bufsize + size >= frame_size)) {
231     memcpy (speexenc->buffer + speexenc->bufsize, data,
232         (frame_size - speexenc->bufsize) * sizeof (gint16));
233
234     for (i = 0; i < frame_size; i++)
235       input[i] = speexenc->buffer[i];
236
237     speex_encode (speexenc->state, input, &speexenc->bits);
238     speexenc->packet_count++;
239
240     if (speexenc->packet_count % speexenc->n_packets == 0) {
241       GstBuffer *outbuf;
242
243       outbuf = gst_buffer_new_and_alloc (frame_size * speexenc->n_packets);
244       GST_BUFFER_SIZE (outbuf) = speex_bits_write (&speexenc->bits,
245           GST_BUFFER_DATA (outbuf), GST_BUFFER_SIZE (outbuf));
246       GST_BUFFER_TIMESTAMP (outbuf) = speexenc->next_ts;
247       speex_bits_reset (&speexenc->bits);
248
249       gst_pad_push (speexenc->srcpad, GST_DATA (outbuf));
250       speexenc->next_ts += frame_size * GST_SECOND / speexenc->rate;
251     }
252
253     size -= (speexenc->frame_size - speexenc->bufsize);
254     data += (speexenc->frame_size - speexenc->bufsize);
255
256     speexenc->bufsize = 0;
257   }
258
259   while (size >= frame_size) {
260     for (i = 0; i < frame_size; i++)
261       input[i] = data[i];
262
263     speex_encode (speexenc->state, input, &speexenc->bits);
264     speexenc->packet_count++;
265
266     if (speexenc->packet_count % speexenc->n_packets == 0) {
267       GstBuffer *outbuf;
268
269       outbuf = gst_buffer_new_and_alloc (frame_size * speexenc->n_packets);
270       GST_BUFFER_SIZE (outbuf) = speex_bits_write (&speexenc->bits,
271           GST_BUFFER_DATA (outbuf), GST_BUFFER_SIZE (outbuf));
272       GST_BUFFER_TIMESTAMP (outbuf) = speexenc->next_ts;
273       speex_bits_reset (&speexenc->bits);
274
275       gst_pad_push (speexenc->srcpad, GST_DATA (outbuf));
276       speexenc->next_ts += frame_size * GST_SECOND / speexenc->rate;
277     }
278
279     size -= frame_size;
280     data += frame_size;
281   }
282
283   if (size) {
284     memcpy (speexenc->buffer + speexenc->bufsize, data, size * sizeof (gint16));
285     speexenc->bufsize += size;
286   }
287
288   gst_buffer_unref (buf);
289 }