2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wim.taymans@chello.be>
4 * 2001 Thomas <thomas@apestaart.org>
6 * adder.c: Adder element, N in, one out, samples are added
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
23 /* Element-Checklist-Version: 5 */
29 #include <gst/audio/audio.h>
30 #include <string.h> /* strcmp */
32 #define GST_ADDER_BUFFER_SIZE 4096
33 #define GST_ADDER_NUM_BUFFERS 8
35 /* elementfactory information */
36 static GstElementDetails adder_details = GST_ELEMENT_DETAILS (
39 "Add N audio channels together",
40 "Thomas <thomas@apestaart.org>"
43 /* Adder signals and args */
55 static GstStaticPadTemplate gst_adder_src_template =
56 GST_STATIC_PAD_TEMPLATE (
61 GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
62 GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_CAPS
66 static GstStaticPadTemplate gst_adder_sink_template =
67 GST_STATIC_PAD_TEMPLATE (
72 GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
73 GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_CAPS
77 static void gst_adder_class_init (GstAdderClass *klass);
78 static void gst_adder_init (GstAdder *adder);
80 static void gst_adder_get_property (GObject *object, guint prop_id,
81 GValue *value, GParamSpec *pspec);
83 static GstPad* gst_adder_request_new_pad (GstElement *element, GstPadTemplate *temp,
85 static GstElementStateReturn
86 gst_adder_change_state (GstElement *element);
88 /* we do need a loop function */
89 static void gst_adder_loop (GstElement *element);
91 static GstElementClass *parent_class = NULL;
92 /* static guint gst_adder_signals[LAST_SIGNAL] = { 0 }; */
95 gst_adder_get_type (void) {
96 static GType adder_type = 0;
99 static const GTypeInfo adder_info = {
100 sizeof (GstAdderClass), NULL, NULL,
101 (GClassInitFunc) gst_adder_class_init, NULL, NULL,
102 sizeof (GstAdder), 0,
103 (GInstanceInitFunc) gst_adder_init,
105 adder_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAdder",
111 static GstPadLinkReturn
112 gst_adder_link (GstPad *pad, const GstCaps *caps)
115 const char *media_type;
117 GstStructure *structure;
118 GstPadLinkReturn ret;
121 g_return_val_if_fail (caps != NULL, GST_PAD_LINK_REFUSED);
122 g_return_val_if_fail (pad != NULL, GST_PAD_LINK_REFUSED);
124 element = GST_PAD_PARENT (pad);
125 adder = GST_ADDER (element);
127 pads = gst_element_get_pad_list (element);
129 GstPad *otherpad = GST_PAD (pads->data);
131 if (otherpad != pad) {
132 ret = gst_pad_try_set_caps (otherpad, caps);
133 if (GST_PAD_LINK_FAILED (ret)) {
137 pads = g_list_next (pads);
141 pads = gst_element_get_pad_list (GST_ELEMENT (adder));
143 GstPad *otherpad = GST_PAD (pads->data);
145 if (otherpad != pad) {
146 ret = gst_pad_try_set_caps (otherpad, caps);
147 if (GST_PAD_LINK_FAILED (ret)) {
151 pads = g_list_next (pads);
154 structure = gst_caps_get_structure (caps, 0);
155 media_type = gst_structure_get_name (structure);
156 if (strcmp (media_type, "audio/x-raw-int") == 0) {
157 GST_DEBUG ("parse_caps sets adder to format int");
158 adder->format = GST_ADDER_FORMAT_INT;
159 gst_structure_get_int (structure, "width", &adder->width);
160 gst_structure_get_int (structure, "depth", &adder->depth);
161 gst_structure_get_int (structure, "endianness", &adder->endianness);
162 gst_structure_get_boolean (structure, "signed", &adder->is_signed);
163 gst_structure_get_int (structure, "channels", &adder->channels);
164 gst_structure_get_int (structure, "rate", &adder->rate);
165 } else if (strcmp (media_type, "audio/x-raw-float") == 0) {
166 GST_DEBUG ("parse_caps sets adder to format float");
167 adder->format = GST_ADDER_FORMAT_FLOAT;
168 gst_structure_get_int (structure, "width", &adder->width);
169 gst_structure_get_int (structure, "channels", &adder->channels);
170 gst_structure_get_int (structure, "rate", &adder->rate);
173 return GST_PAD_LINK_OK;
177 gst_adder_class_init (GstAdderClass *klass)
179 GObjectClass *gobject_class;
180 GstElementClass *gstelement_class;
182 gobject_class = (GObjectClass *) klass;
183 gstelement_class = (GstElementClass *) klass;
185 gst_element_class_add_pad_template (gstelement_class,
186 gst_static_pad_template_get (&gst_adder_src_template));
187 gst_element_class_add_pad_template (gstelement_class,
188 gst_static_pad_template_get (&gst_adder_sink_template));
189 gst_element_class_set_details (gstelement_class, &adder_details);
191 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
193 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NUM_PADS,
194 g_param_spec_int ("num_pads","number of pads","Number Of Pads",
195 0, G_MAXINT, 0, G_PARAM_READABLE));
197 gobject_class->get_property = gst_adder_get_property;
199 gstelement_class->request_new_pad = gst_adder_request_new_pad;
200 gstelement_class->change_state = gst_adder_change_state;
204 gst_adder_init (GstAdder *adder)
206 adder->srcpad = gst_pad_new_from_template (
207 gst_static_pad_template_get (&gst_adder_src_template), "src");
208 gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
209 gst_element_set_loop_function (GST_ELEMENT (adder), gst_adder_loop);
210 gst_pad_set_getcaps_function(adder->srcpad, gst_pad_proxy_getcaps);
211 gst_pad_set_link_function (adder->srcpad, gst_adder_link);
213 adder->format = GST_ADDER_FORMAT_UNSET;
215 /* keep track of the sinkpads requested */
217 adder->numsinkpads = 0;
218 adder->input_channels = NULL;
222 gst_adder_request_new_pad (GstElement *element, GstPadTemplate *templ,
227 GstAdderInputChannel *input;
229 g_return_val_if_fail (GST_IS_ADDER (element), NULL);
231 if (templ->direction != GST_PAD_SINK) {
232 g_warning ("gstadder: request new pad that is not a SINK pad\n");
236 /* allocate space for the input_channel */
238 input = (GstAdderInputChannel *) g_malloc (sizeof (GstAdderInputChannel));
240 g_warning ("gstadder: could not allocate adder input channel !\n");
244 adder = GST_ADDER (element);
246 /* fill in input_channel structure */
248 name = g_strdup_printf ("sink%d", adder->numsinkpads);
249 input->sinkpad = gst_pad_new_from_template (templ, name);
250 input->bytestream = gst_bytestream_new (input->sinkpad);
252 gst_element_add_pad (GST_ELEMENT (adder), input->sinkpad);
253 gst_pad_set_getcaps_function (input->sinkpad, gst_pad_proxy_getcaps);
254 gst_pad_set_link_function(input->sinkpad, gst_adder_link);
256 /* add the input_channel to the list of input channels */
258 adder->input_channels = g_slist_append (adder->input_channels, input);
259 adder->numsinkpads++;
261 return input->sinkpad;
265 gst_adder_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
269 /* it's not null if we got it, but it might not be ours */
270 g_return_if_fail (GST_IS_ADDER (object));
272 adder = GST_ADDER (object);
276 g_value_set_int (value, adder->numsinkpads);
279 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
286 gst_adder_loop (GstElement *element)
289 * combine channels by adding sample values
291 * - request an output buffer from the pool
292 * - repeat for each input pipe :
293 * - get number of bytes from the channel's bytestream to fill output buffer
294 * - if there's an EOS event, remove the input channel
295 * - otherwise add the gotten bytes to the output buffer
296 * - push out the output buffer
305 g_return_if_fail (element != NULL);
306 g_return_if_fail (GST_IS_ADDER (element));
308 adder = GST_ADDER (element);
310 /* get new output buffer */
311 /* FIXME the 1024 is arbitrary */
312 buf_out = gst_buffer_new_and_alloc (1024);
314 if (buf_out == NULL) {
315 GST_ELEMENT_ERROR (adder, CORE, TOO_LAZY, (NULL),
316 ("could not get new output buffer"));
320 /* initialize the output data to 0 */
321 memset (GST_BUFFER_DATA (buf_out), 0, GST_BUFFER_SIZE (buf_out));
323 /* get data from all of the sinks */
324 inputs = adder->input_channels;
326 GST_DEBUG ("starting to cycle through channels");
331 GstAdderInputChannel *input;
333 input = (GstAdderInputChannel *) inputs->data;
335 inputs = inputs->next;
337 GST_DEBUG ("looking into channel %p", input);
339 if (!GST_PAD_IS_USABLE (input->sinkpad)) {
340 GST_DEBUG ("adder ignoring pad %s:%s",
341 GST_DEBUG_PAD_NAME (input->sinkpad));
345 /* Get data from the bytestream of each input channel. We need to check for
346 events before passing on the data to the output buffer. */
347 got_bytes = gst_bytestream_peek_bytes (input->bytestream, &raw_in,
348 GST_BUFFER_SIZE (buf_out));
350 /* FIXME we should do something with the data if got_bytes > 0 */
351 if (got_bytes < GST_BUFFER_SIZE(buf_out)) {
352 GstEvent *event = NULL;
355 /* we need to check for an event. */
356 gst_bytestream_get_status (input->bytestream, &waiting, &event);
359 switch (GST_EVENT_TYPE (event)) {
361 /* if we get an EOS event from one of our sink pads, we assume that
362 pad's finished handling data. just skip this pad. */
363 GST_DEBUG ("got an EOS event");
364 gst_event_unref (event);
366 case GST_EVENT_INTERRUPT:
367 gst_event_unref (event);
368 GST_DEBUG ("got an interrupt event");
369 /* we have to call interrupt here, the scheduler will switch out
370 this element ASAP or returns TRUE if we need to exit the loop */
371 if (gst_element_interrupt (GST_ELEMENT (adder))) {
372 gst_buffer_unref (buf_out);
380 /* here's where the data gets copied. */
382 GST_DEBUG ("copying %d bytes from channel %p to output data %p "
384 GST_BUFFER_SIZE (buf_out), input,
385 GST_BUFFER_DATA (buf_out), buf_out);
387 if (adder->format == GST_ADDER_FORMAT_INT) {
388 if (adder->width == 32) {
389 gint32 *in = (gint32 *) raw_in;
390 gint32 *out = (gint32 *) GST_BUFFER_DATA (buf_out);
391 for (i = 0; i < GST_BUFFER_SIZE (buf_out) / 4; i++)
392 out[i] = CLAMP(out[i] + in[i], 0x80000000, 0x7fffffff);
393 } else if (adder->width == 16) {
394 gint16 *in = (gint16 *) raw_in;
395 gint16 *out = (gint16 *) GST_BUFFER_DATA (buf_out);
396 for (i = 0; i < GST_BUFFER_SIZE (buf_out) / 2; i++)
397 out[i] = CLAMP(out[i] + in[i], 0x8000, 0x7fff);
398 } else if (adder->width == 8) {
399 gint8 *in = (gint8 *) raw_in;
400 gint8 *out = (gint8 *) GST_BUFFER_DATA (buf_out);
401 for (i = 0; i < GST_BUFFER_SIZE (buf_out); i++)
402 out[i] = CLAMP(out[i] + in[i], 0x80, 0x7f);
404 GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
405 ("invalid width (%u) for integer audio in gstadder",
409 } else if (adder->format == GST_ADDER_FORMAT_FLOAT) {
410 if (adder->width == 64) {
411 gdouble *in = (gdouble *) raw_in;
412 gdouble *out = (gdouble *) GST_BUFFER_DATA (buf_out);
413 for (i = 0; i < GST_BUFFER_SIZE (buf_out) / sizeof (gdouble); i++)
414 out[i] = CLAMP(out[i] + in[i], -1.0, 1.0);
415 } else if (adder->width == 32) {
416 gfloat *in = (gfloat *) raw_in;
417 gfloat *out = (gfloat *) GST_BUFFER_DATA (buf_out);
418 for (i = 0; i < GST_BUFFER_SIZE (buf_out) / sizeof (gfloat); i++)
419 out[i] = CLAMP(out[i] + in[i], -1.0, 1.0);
421 GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
422 ("invalid width (%u) for float audio in gstadder",
427 GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
428 ("invalid audio format (%d) in gstadder",
433 gst_bytestream_flush (input->bytestream, GST_BUFFER_SIZE (buf_out));
435 GST_DEBUG ("done copying data");
439 GST_BUFFER_TIMESTAMP (buf_out) = adder->timestamp;
440 if (adder->format == GST_ADDER_FORMAT_FLOAT)
441 adder->offset += GST_BUFFER_SIZE (buf_out) / sizeof (gfloat) / adder->channels;
443 adder->offset += GST_BUFFER_SIZE (buf_out) * 8 / adder->width / adder->channels;
444 adder->timestamp = adder->offset * GST_SECOND / adder->rate;
448 GST_DEBUG ("pushing buf_out");
449 gst_pad_push (adder->srcpad, GST_DATA (buf_out));
453 static GstElementStateReturn
454 gst_adder_change_state (GstElement *element)
458 adder = GST_ADDER (element);
460 switch (GST_STATE_TRANSITION (element)) {
461 case GST_STATE_NULL_TO_READY:
463 case GST_STATE_READY_TO_PAUSED:
464 adder->timestamp = 0;
467 case GST_STATE_PAUSED_TO_PLAYING:
469 case GST_STATE_PLAYING_TO_PAUSED:
471 case GST_STATE_PAUSED_TO_READY:
473 case GST_STATE_READY_TO_NULL:
476 g_assert_not_reached ();
480 if (GST_ELEMENT_CLASS (parent_class)->change_state)
481 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
483 return GST_STATE_SUCCESS;
488 plugin_init (GstPlugin *plugin)
490 if (!gst_library_load ("gstbytestream"))
493 if (!gst_element_register (plugin, "adder", GST_RANK_NONE, GST_TYPE_ADDER)) {
504 "Adds multiple streams",