2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2001 Thomas <thomas@apestaart.org>
4 * 2005 Wim Taymans <wim@fluendo.com>
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 /* highest positive/lowest negative x-bit value we can use for clamping */
33 #define MAX_INT_32 ((gint32) (0x7fffffff))
34 #define MAX_INT_16 ((gint16) (0x7fff))
35 #define MAX_INT_8 ((gint8) (0x7f))
36 #define MAX_UINT_32 ((guint32)(0xffffffff))
37 #define MAX_UINT_16 ((guint16)(0xffff))
38 #define MAX_UINT_8 ((guint8) (0xff))
40 #define MIN_INT_32 ((gint32) (0x80000000))
41 #define MIN_INT_16 ((gint16) (0x8000))
42 #define MIN_INT_8 ((gint8) (0x80))
43 #define MIN_UINT_32 ((guint32)(0x00000000))
44 #define MIN_UINT_16 ((guint16)(0x0000))
45 #define MIN_UINT_8 ((guint8) (0x00))
47 GST_DEBUG_CATEGORY_STATIC (gst_adder_debug);
48 #define GST_CAT_DEFAULT gst_adder_debug
50 /* elementfactory information */
51 static GstElementDetails adder_details = GST_ELEMENT_DETAILS ("Adder",
53 "Add N audio channels together",
54 "Thomas <thomas@apestaart.org>");
56 static GstStaticPadTemplate gst_adder_src_template =
57 GST_STATIC_PAD_TEMPLATE ("src",
60 GST_STATIC_CAPS (GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
61 GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS)
64 static GstStaticPadTemplate gst_adder_sink_template =
65 GST_STATIC_PAD_TEMPLATE ("sink%d",
68 GST_STATIC_CAPS (GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
69 GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS)
72 static void gst_adder_class_init (GstAdderClass * klass);
73 static void gst_adder_init (GstAdder * adder);
74 static void gst_adder_dispose (GObject * object);
76 static GstPad *gst_adder_request_new_pad (GstElement * element,
77 GstPadTemplate * temp, const gchar * unused);
78 static GstStateChangeReturn gst_adder_change_state (GstElement * element,
79 GstStateChange transition);
81 static gboolean gst_adder_setcaps (GstPad * pad, GstCaps * caps);
83 static GstFlowReturn gst_adder_collected (GstCollectPads * pads,
86 static GstElementClass *parent_class = NULL;
89 gst_adder_get_type (void)
91 static GType adder_type = 0;
94 static const GTypeInfo adder_info = {
95 sizeof (GstAdderClass), NULL, NULL,
96 (GClassInitFunc) gst_adder_class_init, NULL, NULL,
98 (GInstanceInitFunc) gst_adder_init,
101 adder_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAdder",
103 GST_DEBUG_CATEGORY_INIT (gst_adder_debug, "adder", 0,
104 "audio channel mixing element");
109 #define MAKE_FUNC(name,type,ttype,min,max) \
110 static void name (type *out, type *in, gint bytes) { \
112 for (i = 0; i < bytes / sizeof (type); i++) \
113 out[i] = CLAMP ((ttype)out[i] + (ttype)in[i], min, max); \
116 MAKE_FUNC (add_int32, gint32, gint64, MIN_INT_32, MAX_INT_32)
117 MAKE_FUNC (add_int16, gint16, gint32, MIN_INT_16, MAX_INT_16)
118 MAKE_FUNC (add_int8, gint8, gint16, MIN_INT_8, MAX_INT_8)
119 MAKE_FUNC (add_uint32, guint32, guint64, MIN_UINT_32, MAX_UINT_32)
120 MAKE_FUNC (add_uint16, guint16, guint32, MIN_UINT_16, MAX_UINT_16)
121 MAKE_FUNC (add_uint8, guint8, guint16, MIN_UINT_8, MAX_UINT_8)
122 MAKE_FUNC (add_float64, gdouble, gdouble, -1.0, 1.0)
123 MAKE_FUNC (add_float32, gfloat, gfloat, -1.0, 1.0)
125 static gboolean gst_adder_setcaps (GstPad * pad, GstCaps * caps)
129 GstStructure *structure;
130 const char *media_type;
132 adder = GST_ADDER (GST_PAD_PARENT (pad));
134 /* see if the other pads can accept the format */
135 GST_OBJECT_LOCK (adder);
136 pads = GST_ELEMENT (adder)->pads;
138 GstPad *otherpad = GST_PAD (pads->data);
140 if (otherpad != pad) {
141 gst_caps_replace (&GST_PAD_CAPS (otherpad), caps);
143 pads = g_list_next (pads);
145 GST_OBJECT_UNLOCK (adder);
148 structure = gst_caps_get_structure (caps, 0);
149 media_type = gst_structure_get_name (structure);
150 if (strcmp (media_type, "audio/x-raw-int") == 0) {
151 GST_DEBUG_OBJECT (adder, "parse_caps sets adder to format int");
152 adder->format = GST_ADDER_FORMAT_INT;
153 gst_structure_get_int (structure, "width", &adder->width);
154 gst_structure_get_int (structure, "depth", &adder->depth);
155 gst_structure_get_int (structure, "endianness", &adder->endianness);
156 gst_structure_get_boolean (structure, "signed", &adder->is_signed);
158 if (adder->endianness != G_BYTE_ORDER)
161 switch (adder->width) {
163 adder->func = (adder->is_signed ?
164 (GstAdderFunction) add_int8 : (GstAdderFunction) add_uint8);
167 adder->func = (adder->is_signed ?
168 (GstAdderFunction) add_int16 : (GstAdderFunction) add_uint16);
171 adder->func = (adder->is_signed ?
172 (GstAdderFunction) add_int32 : (GstAdderFunction) add_uint32);
177 } else if (strcmp (media_type, "audio/x-raw-float") == 0) {
178 GST_DEBUG_OBJECT (adder, "parse_caps sets adder to format float");
179 adder->format = GST_ADDER_FORMAT_FLOAT;
180 gst_structure_get_int (structure, "width", &adder->width);
182 switch (adder->width) {
184 adder->func = (GstAdderFunction) add_float32;
187 adder->func = (GstAdderFunction) add_float64;
196 gst_structure_get_int (structure, "channels", &adder->channels);
197 gst_structure_get_int (structure, "rate", &adder->rate);
208 gst_adder_query (GstPad * pad, GstQuery * query)
210 GstAdder *adder = GST_ADDER (gst_pad_get_parent (pad));
211 gboolean res = FALSE;
213 switch (GST_QUERY_TYPE (query)) {
214 case GST_QUERY_POSITION:
218 gst_query_parse_position (query, &format, NULL);
221 case GST_FORMAT_TIME:
222 gst_query_set_position (query, GST_FORMAT_TIME, adder->timestamp);
225 case GST_FORMAT_DEFAULT:
226 gst_query_set_position (query, GST_FORMAT_DEFAULT, adder->offset);
234 /* FIXME: what to do about the length? query all pads upstream and
235 * pick the longest length? or the shortest length? or what? */
236 case GST_QUERY_DURATION:
242 gst_object_unref (adder);
247 gst_adder_class_init (GstAdderClass * klass)
249 GObjectClass *gobject_class;
250 GstElementClass *gstelement_class;
252 gobject_class = (GObjectClass *) klass;
254 gobject_class->dispose = gst_adder_dispose;
256 gstelement_class = (GstElementClass *) klass;
258 gst_element_class_add_pad_template (gstelement_class,
259 gst_static_pad_template_get (&gst_adder_src_template));
260 gst_element_class_add_pad_template (gstelement_class,
261 gst_static_pad_template_get (&gst_adder_sink_template));
262 gst_element_class_set_details (gstelement_class, &adder_details);
264 parent_class = g_type_class_peek_parent (klass);
266 gstelement_class->request_new_pad = gst_adder_request_new_pad;
267 gstelement_class->change_state = gst_adder_change_state;
271 gst_adder_init (GstAdder * adder)
274 gst_pad_new_from_template (gst_static_pad_template_get
275 (&gst_adder_src_template), "src");
276 gst_pad_set_getcaps_function (adder->srcpad,
277 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
278 gst_pad_set_setcaps_function (adder->srcpad,
279 GST_DEBUG_FUNCPTR (gst_adder_setcaps));
280 gst_pad_set_query_function (adder->srcpad,
281 GST_DEBUG_FUNCPTR (gst_adder_query));
282 gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
284 adder->format = GST_ADDER_FORMAT_UNSET;
288 /* keep track of the sinkpads requested */
289 adder->collect = gst_collect_pads_new ();
290 gst_collect_pads_set_function (adder->collect, gst_adder_collected, adder);
294 gst_adder_dispose (GObject * object)
296 GstAdder *adder = GST_ADDER (object);
298 gst_object_unref (adder->collect);
299 adder->collect = NULL;
303 gst_adder_request_new_pad (GstElement * element, GstPadTemplate * templ,
304 const gchar * unused)
310 g_return_val_if_fail (GST_IS_ADDER (element), NULL);
312 if (templ->direction != GST_PAD_SINK)
315 adder = GST_ADDER (element);
317 name = g_strdup_printf ("sink%d", adder->numpads);
318 newpad = gst_pad_new_from_template (templ, name);
320 gst_pad_set_getcaps_function (newpad,
321 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
322 gst_pad_set_setcaps_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_setcaps));
323 gst_collect_pads_add_pad (adder->collect, newpad, sizeof (GstCollectData));
324 if (!gst_element_add_pad (GST_ELEMENT (adder), newpad))
334 g_warning ("gstadder: request new pad that is not a SINK pad\n");
339 gst_collect_pads_remove_pad (adder->collect, newpad);
340 gst_object_unref (newpad);
346 gst_adder_collected (GstCollectPads * pads, gpointer user_data)
349 * combine channels by adding sample values
351 * - this function is called when all pads have a buffer
352 * - get available bytes on all pads.
353 * - repeat for each input pad :
354 * - read available bytes, copy or add to target buffer
355 * - if there's an EOS event, remove the input channel
356 * - push out the output buffer
365 adder = GST_ADDER (user_data);
367 /* get available bytes for reading */
368 size = gst_collect_pads_available (pads);
375 if (adder->func == NULL)
378 GST_LOG_OBJECT (adder,
379 "starting to cycle through channels, collecting %d bytes", size);
381 for (collected = pads->data; collected; collected = g_slist_next (collected)) {
382 GstCollectData *data;
386 data = (GstCollectData *) collected->data;
388 GST_LOG_OBJECT (adder, "looking into channel %p", data);
390 /* get pointer to copy size bytes */
391 len = gst_collect_pads_read (pads, data, &bytes, size);
395 GST_LOG_OBJECT (adder, " copying %d bytes (format %d,%d)",
396 len, adder->format, adder->width);
397 GST_LOG_OBJECT (adder, " from channel %p from input data %p", data, bytes);
399 if (outbuf == NULL) {
400 /* first buffer, alloc size bytes */
401 outbuf = gst_buffer_new_and_alloc (size);
402 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (adder->srcpad));
403 outbytes = GST_BUFFER_DATA (outbuf);
405 memset (outbytes, 0, size);
407 /* and copy the data into it */
408 memcpy (outbytes, bytes, len);
410 /* other buffers, need to add them */
411 adder->func ((gpointer) outbytes, (gpointer) bytes, len);
413 gst_collect_pads_flush (pads, data, len);
416 /* set timestamps on the output buffer */
421 /* width is in bits and we need bytes */
422 samples = size / ((adder->width / 8) * adder->channels);
423 duration = samples * GST_SECOND / adder->rate;
425 GST_BUFFER_TIMESTAMP (outbuf) = adder->timestamp;
426 GST_BUFFER_OFFSET (outbuf) = adder->offset;
428 adder->offset += samples;
429 adder->timestamp = adder->offset * GST_SECOND / adder->rate;
431 GST_BUFFER_DURATION (outbuf) = adder->timestamp -
432 GST_BUFFER_TIMESTAMP (outbuf);
436 GST_LOG_OBJECT (adder, "pushing outbuf");
437 ret = gst_pad_push (adder->srcpad, outbuf);
444 return GST_FLOW_NOT_NEGOTIATED;
448 static GstStateChangeReturn
449 gst_adder_change_state (GstElement * element, GstStateChange transition)
452 GstStateChangeReturn ret;
454 adder = GST_ADDER (element);
456 switch (transition) {
457 case GST_STATE_CHANGE_NULL_TO_READY:
459 case GST_STATE_CHANGE_READY_TO_PAUSED:
460 adder->timestamp = 0;
462 gst_collect_pads_start (adder->collect);
464 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
466 case GST_STATE_CHANGE_PAUSED_TO_READY:
467 /* need to unblock the collectpads before calling the
468 * parent change_state so that streaming can finish */
469 gst_collect_pads_stop (adder->collect);
475 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
477 switch (transition) {
487 plugin_init (GstPlugin * plugin)
489 if (!gst_element_register (plugin, "adder", GST_RANK_NONE, GST_TYPE_ADDER)) {
496 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
499 "Adds multiple streams",
500 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)