2001-12-22 22:43:48 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* 2000 Wim Taymans <wim.taymans@chello.be>
|
|
|
|
* 2001 Thomas <thomas@apestaart.org>
|
|
|
|
*
|
|
|
|
* adder.c: Adder element, N in, one out, samples are added
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
2004-01-27 09:00:01 +00:00
|
|
|
/* Element-Checklist-Version: 5 */
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2003-06-29 19:46:12 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2001-12-22 22:43:48 +00:00
|
|
|
#include "gstadder.h"
|
2002-09-09 09:26:23 +00:00
|
|
|
#include <gst/audio/audio.h>
|
2004-03-15 19:32:28 +00:00
|
|
|
#include <string.h> /* strcmp */
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2004-03-31 17:11:14 +00:00
|
|
|
/* highest positive/lowest negative x-bit value we can use for clamping */
|
|
|
|
//#define MAX_INT_x ((guint) 1 << (x - 1))
|
|
|
|
#define MAX_INT_32 2147483647L
|
|
|
|
#define MAX_INT_16 32767
|
|
|
|
#define MAX_INT_8 127
|
|
|
|
|
|
|
|
//#define MIN_INT_x (-((guint) 1 << (x - 1)))
|
|
|
|
/* to make non-C90 happy we need to specify the constant differently */
|
|
|
|
#define MIN_INT_32 (-2147483647L -1L)
|
|
|
|
#define MIN_INT_16 -32768
|
|
|
|
#define MIN_INT_8 -128
|
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
#define GST_ADDER_BUFFER_SIZE 4096
|
|
|
|
#define GST_ADDER_NUM_BUFFERS 8
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2004-03-31 17:11:14 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_adder_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_adder_debug
|
|
|
|
|
2002-09-18 19:02:52 +00:00
|
|
|
/* elementfactory information */
|
2004-03-14 22:34:34 +00:00
|
|
|
static GstElementDetails adder_details = GST_ELEMENT_DETAILS ("Adder",
|
|
|
|
"Generic/Audio",
|
|
|
|
"Add N audio channels together",
|
|
|
|
"Thomas <thomas@apestaart.org>");
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
/* Adder signals and args */
|
2004-03-14 22:34:34 +00:00
|
|
|
enum
|
|
|
|
{
|
2001-12-22 22:43:48 +00:00
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
enum
|
|
|
|
{
|
2001-12-22 22:43:48 +00:00
|
|
|
ARG_0,
|
2004-05-21 22:39:30 +00:00
|
|
|
ARG_NUM_PADS
|
|
|
|
/* FILL ME */
|
2001-12-22 22:43:48 +00:00
|
|
|
};
|
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstStaticPadTemplate gst_adder_src_template =
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS (GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
|
2004-03-31 17:11:14 +00:00
|
|
|
GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS)
|
2004-03-14 22:34:34 +00:00
|
|
|
);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstStaticPadTemplate gst_adder_sink_template =
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink%d",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_REQUEST,
|
|
|
|
GST_STATIC_CAPS (GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
|
2004-03-31 17:11:14 +00:00
|
|
|
GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS)
|
2004-03-14 22:34:34 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
static void gst_adder_class_init (GstAdderClass * klass);
|
|
|
|
static void gst_adder_init (GstAdder * adder);
|
|
|
|
|
|
|
|
static void gst_adder_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
|
|
|
|
static GstPad *gst_adder_request_new_pad (GstElement * element,
|
|
|
|
GstPadTemplate * temp, const gchar * unused);
|
|
|
|
static GstElementStateReturn gst_adder_change_state (GstElement * element);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
/* we do need a loop function */
|
2004-03-14 22:34:34 +00:00
|
|
|
static void gst_adder_loop (GstElement * element);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
static GstElementClass *parent_class = NULL;
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
/* static guint gst_adder_signals[LAST_SIGNAL] = { 0 }; */
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
GType
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_adder_get_type (void)
|
|
|
|
{
|
2001-12-22 22:43:48 +00:00
|
|
|
static GType adder_type = 0;
|
|
|
|
|
|
|
|
if (!adder_type) {
|
|
|
|
static const GTypeInfo adder_info = {
|
2002-09-09 09:26:23 +00:00
|
|
|
sizeof (GstAdderClass), NULL, NULL,
|
|
|
|
(GClassInitFunc) gst_adder_class_init, NULL, NULL,
|
2002-09-23 09:39:33 +00:00
|
|
|
sizeof (GstAdder), 0,
|
2002-09-09 09:26:23 +00:00
|
|
|
(GInstanceInitFunc) gst_adder_init,
|
2001-12-22 22:43:48 +00:00
|
|
|
};
|
2004-03-15 19:32:28 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
adder_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAdder",
|
2004-03-15 19:32:28 +00:00
|
|
|
&adder_info, 0);
|
2004-03-31 17:11:14 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_adder_debug, "adder", 0,
|
|
|
|
"audio channel mixing element");
|
2001-12-22 22:43:48 +00:00
|
|
|
}
|
|
|
|
return adder_type;
|
|
|
|
}
|
|
|
|
|
2003-01-10 13:38:32 +00:00
|
|
|
static GstPadLinkReturn
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_adder_link (GstPad * pad, const GstCaps * caps)
|
2002-02-19 05:59:06 +00:00
|
|
|
{
|
|
|
|
GstAdder *adder;
|
2004-01-27 09:00:01 +00:00
|
|
|
const char *media_type;
|
2004-01-22 03:25:16 +00:00
|
|
|
const GList *pads;
|
2004-01-27 09:00:01 +00:00
|
|
|
GstStructure *structure;
|
2004-01-22 13:22:02 +00:00
|
|
|
GstPadLinkReturn ret;
|
2004-01-27 09:00:01 +00:00
|
|
|
GstElement *element;
|
2003-07-19 23:16:25 +00:00
|
|
|
|
2003-01-10 10:22:25 +00:00
|
|
|
g_return_val_if_fail (caps != NULL, GST_PAD_LINK_REFUSED);
|
2004-03-14 22:34:34 +00:00
|
|
|
g_return_val_if_fail (pad != NULL, GST_PAD_LINK_REFUSED);
|
2002-02-19 05:59:06 +00:00
|
|
|
|
2004-01-22 13:22:02 +00:00
|
|
|
element = GST_PAD_PARENT (pad);
|
|
|
|
adder = GST_ADDER (element);
|
2002-01-22 04:45:10 +00:00
|
|
|
|
2004-01-22 03:25:16 +00:00
|
|
|
pads = gst_element_get_pad_list (element);
|
|
|
|
while (pads) {
|
2004-01-22 13:22:02 +00:00
|
|
|
GstPad *otherpad = GST_PAD (pads->data);
|
2004-01-22 03:25:16 +00:00
|
|
|
|
|
|
|
if (otherpad != pad) {
|
|
|
|
ret = gst_pad_try_set_caps (otherpad, caps);
|
|
|
|
if (GST_PAD_LINK_FAILED (ret)) {
|
2004-03-15 19:32:28 +00:00
|
|
|
return ret;
|
2004-01-22 03:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pads = g_list_next (pads);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-27 09:00:01 +00:00
|
|
|
pads = gst_element_get_pad_list (GST_ELEMENT (adder));
|
|
|
|
while (pads) {
|
|
|
|
GstPad *otherpad = GST_PAD (pads->data);
|
|
|
|
|
|
|
|
if (otherpad != pad) {
|
|
|
|
ret = gst_pad_try_set_caps (otherpad, caps);
|
|
|
|
if (GST_PAD_LINK_FAILED (ret)) {
|
2004-03-15 19:32:28 +00:00
|
|
|
return ret;
|
2002-03-03 00:53:25 +00:00
|
|
|
}
|
2002-02-19 05:59:06 +00:00
|
|
|
}
|
2004-01-27 09:00:01 +00:00
|
|
|
pads = g_list_next (pads);
|
|
|
|
}
|
|
|
|
|
|
|
|
structure = gst_caps_get_structure (caps, 0);
|
|
|
|
media_type = gst_structure_get_name (structure);
|
|
|
|
if (strcmp (media_type, "audio/x-raw-int") == 0) {
|
|
|
|
GST_DEBUG ("parse_caps sets adder to format int");
|
2004-03-14 22:34:34 +00:00
|
|
|
adder->format = GST_ADDER_FORMAT_INT;
|
|
|
|
gst_structure_get_int (structure, "width", &adder->width);
|
|
|
|
gst_structure_get_int (structure, "depth", &adder->depth);
|
|
|
|
gst_structure_get_int (structure, "endianness", &adder->endianness);
|
|
|
|
gst_structure_get_boolean (structure, "signed", &adder->is_signed);
|
|
|
|
gst_structure_get_int (structure, "channels", &adder->channels);
|
|
|
|
gst_structure_get_int (structure, "rate", &adder->rate);
|
2004-01-27 09:00:01 +00:00
|
|
|
} else if (strcmp (media_type, "audio/x-raw-float") == 0) {
|
|
|
|
GST_DEBUG ("parse_caps sets adder to format float");
|
2004-03-14 22:34:34 +00:00
|
|
|
adder->format = GST_ADDER_FORMAT_FLOAT;
|
|
|
|
gst_structure_get_int (structure, "width", &adder->width);
|
|
|
|
gst_structure_get_int (structure, "channels", &adder->channels);
|
|
|
|
gst_structure_get_int (structure, "rate", &adder->rate);
|
2002-02-19 05:59:06 +00:00
|
|
|
}
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2004-01-27 09:00:01 +00:00
|
|
|
return GST_PAD_LINK_OK;
|
2002-01-22 04:45:10 +00:00
|
|
|
}
|
|
|
|
|
2001-12-22 22:43:48 +00:00
|
|
|
static void
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_adder_class_init (GstAdderClass * klass)
|
2001-12-22 22:43:48 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
2002-09-23 09:39:33 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gstelement_class = (GstElementClass *) klass;
|
2002-09-09 09:26:23 +00:00
|
|
|
|
2003-11-01 01:10:23 +00:00
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
2003-12-22 01:47:09 +00:00
|
|
|
gst_static_pad_template_get (&gst_adder_src_template));
|
2003-11-01 01:10:23 +00:00
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
2003-12-22 01:47:09 +00:00
|
|
|
gst_static_pad_template_get (&gst_adder_sink_template));
|
2003-11-01 01:10:23 +00:00
|
|
|
gst_element_class_set_details (gstelement_class, &adder_details);
|
|
|
|
|
2002-09-09 09:26:23 +00:00
|
|
|
parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
|
|
|
|
|
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NUM_PADS,
|
2004-03-14 22:34:34 +00:00
|
|
|
g_param_spec_int ("num_pads", "number of pads", "Number Of Pads",
|
2004-03-15 19:32:28 +00:00
|
|
|
0, G_MAXINT, 0, G_PARAM_READABLE));
|
2002-09-23 09:39:33 +00:00
|
|
|
|
|
|
|
gobject_class->get_property = gst_adder_get_property;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
gstelement_class->request_new_pad = gst_adder_request_new_pad;
|
2004-03-14 22:34:34 +00:00
|
|
|
gstelement_class->change_state = gst_adder_change_state;
|
2001-12-22 22:43:48 +00:00
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
static void
|
|
|
|
gst_adder_init (GstAdder * adder)
|
2001-12-22 22:43:48 +00:00
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
adder->srcpad =
|
|
|
|
gst_pad_new_from_template (gst_static_pad_template_get
|
|
|
|
(&gst_adder_src_template), "src");
|
2001-12-22 22:43:48 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
|
|
|
|
gst_element_set_loop_function (GST_ELEMENT (adder), gst_adder_loop);
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_pad_set_getcaps_function (adder->srcpad, gst_pad_proxy_getcaps);
|
2003-01-11 16:28:29 +00:00
|
|
|
gst_pad_set_link_function (adder->srcpad, gst_adder_link);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
adder->format = GST_ADDER_FORMAT_UNSET;
|
|
|
|
|
2001-12-22 22:43:48 +00:00
|
|
|
/* keep track of the sinkpads requested */
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2001-12-22 22:43:48 +00:00
|
|
|
adder->numsinkpads = 0;
|
|
|
|
adder->input_channels = NULL;
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
static GstPad *
|
|
|
|
gst_adder_request_new_pad (GstElement * element, GstPadTemplate * templ,
|
|
|
|
const gchar * unused)
|
2001-12-22 22:43:48 +00:00
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
gchar *name;
|
|
|
|
GstAdder *adder;
|
2002-01-22 04:45:10 +00:00
|
|
|
GstAdderInputChannel *input;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_ADDER (element), NULL);
|
|
|
|
|
|
|
|
if (templ->direction != GST_PAD_SINK) {
|
|
|
|
g_warning ("gstadder: request new pad that is not a SINK pad\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate space for the input_channel */
|
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
input = (GstAdderInputChannel *) g_malloc (sizeof (GstAdderInputChannel));
|
|
|
|
if (input == NULL) {
|
2002-09-23 09:39:33 +00:00
|
|
|
g_warning ("gstadder: could not allocate adder input channel !\n");
|
2002-01-22 04:45:10 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
adder = GST_ADDER (element);
|
|
|
|
|
2001-12-22 22:43:48 +00:00
|
|
|
/* fill in input_channel structure */
|
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
name = g_strdup_printf ("sink%d", adder->numsinkpads);
|
|
|
|
input->sinkpad = gst_pad_new_from_template (templ, name);
|
|
|
|
input->bytestream = gst_bytestream_new (input->sinkpad);
|
|
|
|
|
|
|
|
gst_element_add_pad (GST_ELEMENT (adder), input->sinkpad);
|
2004-01-22 03:25:16 +00:00
|
|
|
gst_pad_set_getcaps_function (input->sinkpad, gst_pad_proxy_getcaps);
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_pad_set_link_function (input->sinkpad, gst_adder_link);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
/* add the input_channel to the list of input channels */
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
adder->input_channels = g_slist_append (adder->input_channels, input);
|
2001-12-22 22:43:48 +00:00
|
|
|
adder->numsinkpads++;
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
return input->sinkpad;
|
2001-12-22 22:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_adder_get_property (GObject * object, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
2001-12-22 22:43:48 +00:00
|
|
|
{
|
|
|
|
GstAdder *adder;
|
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
|
|
|
g_return_if_fail (GST_IS_ADDER (object));
|
|
|
|
|
|
|
|
adder = GST_ADDER (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_NUM_PADS:
|
|
|
|
g_value_set_int (value, adder->numsinkpads);
|
|
|
|
break;
|
|
|
|
default:
|
2004-03-14 22:34:34 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
2001-12-22 22:43:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* use this loop */
|
|
|
|
static void
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_adder_loop (GstElement * element)
|
2001-12-22 22:43:48 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* combine channels by adding sample values
|
|
|
|
* basic algorithm :
|
2002-01-22 04:45:10 +00:00
|
|
|
* - request an output buffer from the pool
|
2001-12-22 22:43:48 +00:00
|
|
|
* - repeat for each input pipe :
|
2002-01-22 04:45:10 +00:00
|
|
|
* - get number of bytes from the channel's bytestream to fill output buffer
|
|
|
|
* - if there's an EOS event, remove the input channel
|
|
|
|
* - otherwise add the gotten bytes to the output buffer
|
2001-12-22 22:43:48 +00:00
|
|
|
* - push out the output buffer
|
|
|
|
*/
|
2004-03-14 22:34:34 +00:00
|
|
|
GstAdder *adder;
|
2002-01-22 04:45:10 +00:00
|
|
|
GstBuffer *buf_out;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
GSList *inputs;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2002-01-22 04:45:10 +00:00
|
|
|
register guint i;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
|
|
|
g_return_if_fail (element != NULL);
|
|
|
|
g_return_if_fail (GST_IS_ADDER (element));
|
2003-07-19 23:16:25 +00:00
|
|
|
|
2001-12-22 22:43:48 +00:00
|
|
|
adder = GST_ADDER (element);
|
2003-01-11 16:28:29 +00:00
|
|
|
|
|
|
|
/* get new output buffer */
|
2003-12-22 01:47:09 +00:00
|
|
|
/* FIXME the 1024 is arbitrary */
|
|
|
|
buf_out = gst_buffer_new_and_alloc (1024);
|
2003-07-19 23:16:25 +00:00
|
|
|
|
2003-01-11 16:28:29 +00:00
|
|
|
if (buf_out == NULL) {
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (adder, CORE, TOO_LAZY, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
("could not get new output buffer"));
|
2003-01-11 16:28:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize the output data to 0 */
|
|
|
|
memset (GST_BUFFER_DATA (buf_out), 0, GST_BUFFER_SIZE (buf_out));
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2003-01-11 16:28:29 +00:00
|
|
|
/* get data from all of the sinks */
|
|
|
|
inputs = adder->input_channels;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2004-03-31 17:11:14 +00:00
|
|
|
GST_LOG ("starting to cycle through channels");
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2003-01-11 16:28:29 +00:00
|
|
|
while (inputs) {
|
|
|
|
guint32 got_bytes;
|
|
|
|
guint8 *raw_in;
|
|
|
|
GstAdderInputChannel *input;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2003-01-11 16:28:29 +00:00
|
|
|
input = (GstAdderInputChannel *) inputs->data;
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2003-01-11 16:28:29 +00:00
|
|
|
inputs = inputs->next;
|
|
|
|
|
2004-03-31 17:11:14 +00:00
|
|
|
GST_LOG (" looking into channel %p", input);
|
2003-07-19 23:16:25 +00:00
|
|
|
|
2003-01-11 16:28:29 +00:00
|
|
|
if (!GST_PAD_IS_USABLE (input->sinkpad)) {
|
2004-03-31 17:11:14 +00:00
|
|
|
GST_LOG (" adder ignoring pad %s:%s",
|
2004-03-15 19:32:28 +00:00
|
|
|
GST_DEBUG_PAD_NAME (input->sinkpad));
|
2003-01-11 16:28:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
2002-07-26 18:55:43 +00:00
|
|
|
|
2003-07-19 23:16:25 +00:00
|
|
|
/* Get data from the bytestream of each input channel. We need to check for
|
|
|
|
events before passing on the data to the output buffer. */
|
2003-01-11 16:28:29 +00:00
|
|
|
got_bytes = gst_bytestream_peek_bytes (input->bytestream, &raw_in,
|
2004-03-15 19:32:28 +00:00
|
|
|
GST_BUFFER_SIZE (buf_out));
|
2002-01-22 04:45:10 +00:00
|
|
|
|
2003-07-19 23:16:25 +00:00
|
|
|
/* FIXME we should do something with the data if got_bytes > 0 */
|
2004-03-14 22:34:34 +00:00
|
|
|
if (got_bytes < GST_BUFFER_SIZE (buf_out)) {
|
|
|
|
GstEvent *event = NULL;
|
|
|
|
guint32 waiting;
|
2002-01-22 04:45:10 +00:00
|
|
|
|
2003-01-11 16:28:29 +00:00
|
|
|
/* we need to check for an event. */
|
|
|
|
gst_bytestream_get_status (input->bytestream, &waiting, &event);
|
|
|
|
|
|
|
|
if (event) {
|
2004-03-15 19:32:28 +00:00
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_EOS:
|
|
|
|
/* if we get an EOS event from one of our sink pads, we assume that
|
|
|
|
pad's finished handling data. just skip this pad. */
|
2004-03-31 17:11:14 +00:00
|
|
|
GST_DEBUG (" got an EOS event");
|
2004-03-15 19:32:28 +00:00
|
|
|
gst_event_unref (event);
|
|
|
|
continue;
|
|
|
|
case GST_EVENT_INTERRUPT:
|
|
|
|
gst_event_unref (event);
|
2004-03-31 17:11:14 +00:00
|
|
|
GST_DEBUG (" got an interrupt event");
|
2004-03-15 19:32:28 +00:00
|
|
|
/* we have to call interrupt here, the scheduler will switch out
|
|
|
|
this element ASAP or returns TRUE if we need to exit the loop */
|
|
|
|
if (gst_element_interrupt (GST_ELEMENT (adder))) {
|
|
|
|
gst_buffer_unref (buf_out);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2003-01-11 16:28:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
2003-07-19 23:16:25 +00:00
|
|
|
/* here's where the data gets copied. */
|
|
|
|
|
2004-03-31 17:11:14 +00:00
|
|
|
GST_LOG (" copying %d bytes (format %d,%d)",
|
|
|
|
GST_BUFFER_SIZE (buf_out), adder->format, adder->width);
|
|
|
|
GST_LOG (" from channel %p from input data %p", input, raw_in);
|
|
|
|
GST_LOG (" to output data %p in buffer %p",
|
|
|
|
GST_BUFFER_DATA (buf_out), buf_out);
|
2003-01-11 16:28:29 +00:00
|
|
|
|
|
|
|
if (adder->format == GST_ADDER_FORMAT_INT) {
|
2004-03-15 19:32:28 +00:00
|
|
|
if (adder->width == 32) {
|
|
|
|
gint32 *in = (gint32 *) raw_in;
|
|
|
|
gint32 *out = (gint32 *) GST_BUFFER_DATA (buf_out);
|
|
|
|
|
|
|
|
for (i = 0; i < GST_BUFFER_SIZE (buf_out) / 4; i++)
|
2004-08-31 14:12:49 +00:00
|
|
|
out[i] = CLAMP (((gint64) out[i]) + ((gint64) in[i]),
|
|
|
|
MIN_INT_32, MAX_INT_32);
|
2004-03-15 19:32:28 +00:00
|
|
|
} else if (adder->width == 16) {
|
|
|
|
gint16 *in = (gint16 *) raw_in;
|
|
|
|
gint16 *out = (gint16 *) GST_BUFFER_DATA (buf_out);
|
|
|
|
|
|
|
|
for (i = 0; i < GST_BUFFER_SIZE (buf_out) / 2; i++)
|
2004-03-31 17:11:14 +00:00
|
|
|
out[i] = CLAMP (out[i] + in[i], MIN_INT_16, MAX_INT_16);
|
2004-03-15 19:32:28 +00:00
|
|
|
} else if (adder->width == 8) {
|
|
|
|
gint8 *in = (gint8 *) raw_in;
|
|
|
|
gint8 *out = (gint8 *) GST_BUFFER_DATA (buf_out);
|
|
|
|
|
|
|
|
for (i = 0; i < GST_BUFFER_SIZE (buf_out); i++)
|
2004-03-31 17:11:14 +00:00
|
|
|
out[i] = CLAMP (out[i] + in[i], MIN_INT_8, MAX_INT_8);
|
2004-03-15 19:32:28 +00:00
|
|
|
} else {
|
|
|
|
GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
|
|
|
|
("invalid width (%u) for integer audio in gstadder",
|
|
|
|
adder->width));
|
|
|
|
return;
|
|
|
|
}
|
2003-01-11 16:28:29 +00:00
|
|
|
} else if (adder->format == GST_ADDER_FORMAT_FLOAT) {
|
2004-03-15 19:32:28 +00:00
|
|
|
if (adder->width == 64) {
|
|
|
|
gdouble *in = (gdouble *) raw_in;
|
|
|
|
gdouble *out = (gdouble *) GST_BUFFER_DATA (buf_out);
|
|
|
|
|
|
|
|
for (i = 0; i < GST_BUFFER_SIZE (buf_out) / sizeof (gdouble); i++)
|
|
|
|
out[i] = CLAMP (out[i] + in[i], -1.0, 1.0);
|
|
|
|
} else if (adder->width == 32) {
|
|
|
|
gfloat *in = (gfloat *) raw_in;
|
|
|
|
gfloat *out = (gfloat *) GST_BUFFER_DATA (buf_out);
|
|
|
|
|
|
|
|
for (i = 0; i < GST_BUFFER_SIZE (buf_out) / sizeof (gfloat); i++)
|
|
|
|
out[i] = CLAMP (out[i] + in[i], -1.0, 1.0);
|
|
|
|
} else {
|
|
|
|
GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
|
|
|
|
("invalid width (%u) for float audio in gstadder", adder->width));
|
|
|
|
return;
|
|
|
|
}
|
2003-01-11 16:28:29 +00:00
|
|
|
} else {
|
2004-03-15 19:32:28 +00:00
|
|
|
GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
|
|
|
|
("invalid audio format (%d) in gstadder", adder->format));
|
|
|
|
return;
|
2003-01-11 16:28:29 +00:00
|
|
|
}
|
2002-01-22 04:45:10 +00:00
|
|
|
|
2003-01-11 16:28:29 +00:00
|
|
|
gst_bytestream_flush (input->bytestream, GST_BUFFER_SIZE (buf_out));
|
2002-01-22 04:45:10 +00:00
|
|
|
|
2004-03-31 17:11:14 +00:00
|
|
|
GST_LOG ("done copying data");
|
2001-12-23 23:01:28 +00:00
|
|
|
}
|
2003-01-11 16:28:29 +00:00
|
|
|
}
|
2004-06-06 23:43:15 +00:00
|
|
|
if (adder->width == 0) {
|
2004-04-01 11:40:45 +00:00
|
|
|
GST_ELEMENT_ERROR (adder, CORE, NEGOTIATION, (NULL), ("width is 0"));
|
2004-06-06 23:43:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (adder->channels == 0) {
|
2004-04-01 11:40:45 +00:00
|
|
|
GST_ELEMENT_ERROR (adder, CORE, NEGOTIATION, (NULL), ("channels is 0"));
|
2004-06-06 23:43:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (adder->rate == 0) {
|
2004-04-01 11:40:45 +00:00
|
|
|
GST_ELEMENT_ERROR (adder, CORE, NEGOTIATION, (NULL), ("rate is 0"));
|
2004-06-06 23:43:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2003-01-11 16:28:29 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (buf_out) = adder->timestamp;
|
|
|
|
if (adder->format == GST_ADDER_FORMAT_FLOAT)
|
2004-03-31 17:11:14 +00:00
|
|
|
adder->offset += GST_BUFFER_SIZE (buf_out) / adder->width / adder->channels;
|
2003-01-11 16:28:29 +00:00
|
|
|
else
|
2004-03-14 22:34:34 +00:00
|
|
|
adder->offset +=
|
2004-03-15 19:32:28 +00:00
|
|
|
GST_BUFFER_SIZE (buf_out) * 8 / adder->width / adder->channels;
|
2003-01-11 16:28:29 +00:00
|
|
|
adder->timestamp = adder->offset * GST_SECOND / adder->rate;
|
|
|
|
|
|
|
|
/* send it out */
|
|
|
|
|
2004-03-31 17:11:14 +00:00
|
|
|
GST_LOG ("pushing buf_out");
|
2003-10-08 16:08:22 +00:00
|
|
|
gst_pad_push (adder->srcpad, GST_DATA (buf_out));
|
2003-01-11 16:28:29 +00:00
|
|
|
}
|
|
|
|
|
2003-07-19 23:16:25 +00:00
|
|
|
|
2003-01-11 16:28:29 +00:00
|
|
|
static GstElementStateReturn
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_adder_change_state (GstElement * element)
|
2003-01-11 16:28:29 +00:00
|
|
|
{
|
|
|
|
GstAdder *adder;
|
2002-09-23 09:39:33 +00:00
|
|
|
|
2003-01-11 16:28:29 +00:00
|
|
|
adder = GST_ADDER (element);
|
2002-05-27 04:48:57 +00:00
|
|
|
|
2003-01-11 16:28:29 +00:00
|
|
|
switch (GST_STATE_TRANSITION (element)) {
|
|
|
|
case GST_STATE_NULL_TO_READY:
|
|
|
|
break;
|
|
|
|
case GST_STATE_READY_TO_PAUSED:
|
|
|
|
adder->timestamp = 0;
|
|
|
|
adder->offset = 0;
|
|
|
|
break;
|
|
|
|
case GST_STATE_PAUSED_TO_PLAYING:
|
|
|
|
case GST_STATE_PLAYING_TO_PAUSED:
|
|
|
|
case GST_STATE_PAUSED_TO_READY:
|
|
|
|
case GST_STATE_READY_TO_NULL:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2003-01-11 16:28:29 +00:00
|
|
|
if (GST_ELEMENT_CLASS (parent_class)->change_state)
|
|
|
|
return GST_ELEMENT_CLASS (parent_class)->change_state (element);
|
2001-12-22 22:43:48 +00:00
|
|
|
|
2003-01-11 16:28:29 +00:00
|
|
|
return GST_STATE_SUCCESS;
|
2001-12-22 22:43:48 +00:00
|
|
|
}
|
|
|
|
|
2003-07-19 23:16:25 +00:00
|
|
|
|
2001-12-22 22:43:48 +00:00
|
|
|
static gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
plugin_init (GstPlugin * plugin)
|
2001-12-22 22:43:48 +00:00
|
|
|
{
|
2003-10-28 20:52:41 +00:00
|
|
|
if (!gst_library_load ("gstbytestream"))
|
|
|
|
return FALSE;
|
|
|
|
|
2003-11-01 01:10:23 +00:00
|
|
|
if (!gst_element_register (plugin, "adder", GST_RANK_NONE, GST_TYPE_ADDER)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2003-07-19 23:16:25 +00:00
|
|
|
|
2001-12-22 22:43:48 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
|
|
|
|
GST_VERSION_MINOR,
|
|
|
|
"adder",
|
|
|
|
"Adds multiple streams",
|
|
|
|
plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN)
|