2003-04-14 01:20:30 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
|
|
|
|
*
|
|
|
|
* gstaudioconvert.c: Convert audio to different audio formats automatically
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2003-06-29 19:46:12 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2003-07-19 22:58:41 +00:00
|
|
|
# include <config.h>
|
2003-06-29 19:46:12 +00:00
|
|
|
#endif
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
#include <gst/gst.h>
|
2003-07-19 22:58:41 +00:00
|
|
|
#include <gst/audio/audio.h>
|
2003-04-14 01:20:30 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2004-01-12 19:46:45 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (audio_convert_debug);
|
|
|
|
#define GST_CAT_DEFAULT (audio_convert_debug)
|
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
#if 0
|
|
|
|
static void
|
|
|
|
print_caps (GstCaps *caps)
|
|
|
|
{
|
|
|
|
GValue v = { 0, };
|
|
|
|
GValue s = { 0, };
|
|
|
|
g_value_init (&v, GST_TYPE_CAPS);
|
|
|
|
g_value_init (&s, G_TYPE_STRING);
|
|
|
|
g_value_set_boxed (&v, caps);
|
|
|
|
g_value_transform (&v, &s);
|
|
|
|
g_print ("%s\n", g_value_get_string (&s));
|
|
|
|
g_value_unset (&v);
|
|
|
|
g_value_unset (&s);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*** DEFINITIONS **************************************************************/
|
|
|
|
|
2003-07-19 22:58:41 +00:00
|
|
|
#define GST_TYPE_AUDIO_CONVERT (gst_audio_convert_get_type())
|
|
|
|
#define GST_AUDIO_CONVERT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUDIO_CONVERT,GstAudioConvert))
|
|
|
|
#define GST_AUDIO_CONVERT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AUDIO_CONVERT,GstAudioConvert))
|
|
|
|
#define GST_IS_AUDIO_CONVERT(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUDIO_CONVERT))
|
|
|
|
#define GST_IS_AUDIO_CONVERT_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AUDIO_CONVERT))
|
2003-04-14 01:20:30 +00:00
|
|
|
|
|
|
|
typedef struct _GstAudioConvert GstAudioConvert;
|
2003-07-19 22:58:41 +00:00
|
|
|
typedef struct _GstAudioConvertCaps GstAudioConvertCaps;
|
2003-04-14 01:20:30 +00:00
|
|
|
typedef struct _GstAudioConvertClass GstAudioConvertClass;
|
|
|
|
|
2003-07-19 22:58:41 +00:00
|
|
|
/* this struct is a handy way of passing around all the caps info ... */
|
|
|
|
struct _GstAudioConvertCaps {
|
|
|
|
/* general caps */
|
|
|
|
gint endianness;
|
|
|
|
gint width;
|
|
|
|
gint rate;
|
|
|
|
gint channels;
|
|
|
|
|
|
|
|
/* int audio caps */
|
|
|
|
gint depth;
|
|
|
|
gboolean is_signed;
|
|
|
|
};
|
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
struct _GstAudioConvert {
|
2003-07-19 22:58:41 +00:00
|
|
|
GstElement element;
|
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
/* pads */
|
2003-07-19 22:58:41 +00:00
|
|
|
GstPad * sink;
|
|
|
|
GstPad * src;
|
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
/* properties */
|
2003-07-19 22:58:41 +00:00
|
|
|
gboolean aggressive;
|
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
/* caps: 0 = sink, 1 = src, so always convert from 0 to 1 */
|
2003-07-19 22:58:41 +00:00
|
|
|
gboolean caps_set[2];
|
|
|
|
GstAudioConvertCaps caps[2];
|
|
|
|
|
|
|
|
gint law[2];
|
|
|
|
gint endian[2];
|
|
|
|
gint sign[2];
|
|
|
|
gint depth[2]; /* in BITS */
|
2004-01-12 19:46:45 +00:00
|
|
|
gint width[2]; /* in BITS */
|
2003-07-19 22:58:41 +00:00
|
|
|
gint rate[2];
|
|
|
|
gint channels[2];
|
|
|
|
|
|
|
|
/* conversion functions */
|
|
|
|
GstBuffer * (* convert_internal) (GstAudioConvert *this, GstBuffer *buf);
|
2003-04-14 01:20:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GstAudioConvertClass {
|
|
|
|
GstElementClass parent_class;
|
|
|
|
};
|
|
|
|
|
2003-11-01 11:41:42 +00:00
|
|
|
static GstElementDetails audio_convert_details = {
|
|
|
|
"Audio Conversion",
|
2003-11-16 22:02:21 +00:00
|
|
|
"Filter/Converter/Audio",
|
2003-11-01 11:41:42 +00:00
|
|
|
"Convert audio to different formats",
|
|
|
|
"Benjamin Otte <in7y118@public.uni-hamburg.de",
|
|
|
|
};
|
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
/* type functions */
|
2003-07-19 22:58:41 +00:00
|
|
|
static GType gst_audio_convert_get_type (void);
|
2003-11-01 11:41:42 +00:00
|
|
|
static void gst_audio_convert_base_init (gpointer g_class);
|
2003-07-19 22:58:41 +00:00
|
|
|
static void gst_audio_convert_class_init (GstAudioConvertClass *klass);
|
|
|
|
static void gst_audio_convert_init (GstAudioConvert *audio_convert);
|
|
|
|
static void gst_audio_convert_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
|
|
|
|
static void gst_audio_convert_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
|
|
|
|
|
|
|
|
/* gstreamer functions */
|
2003-10-08 16:08:22 +00:00
|
|
|
static void gst_audio_convert_chain (GstPad *pad, GstData *_data);
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstPadLinkReturn gst_audio_convert_link (GstPad *pad, const GstCaps *caps);
|
2003-07-19 22:58:41 +00:00
|
|
|
static GstElementStateReturn gst_audio_convert_change_state (GstElement *element);
|
2003-04-14 01:20:30 +00:00
|
|
|
|
2003-07-19 22:58:41 +00:00
|
|
|
/* actual work */
|
2003-12-31 08:02:04 +00:00
|
|
|
#if 0
|
2003-07-19 22:58:41 +00:00
|
|
|
static gboolean gst_audio_convert_set_caps (GstPad *pad);
|
2003-12-31 08:02:04 +00:00
|
|
|
#endif
|
2003-04-14 01:20:30 +00:00
|
|
|
|
2003-07-19 22:58:41 +00:00
|
|
|
static GstBuffer * gst_audio_convert_buffer_to_default_format (GstAudioConvert *this, GstBuffer *buf);
|
|
|
|
static GstBuffer * gst_audio_convert_buffer_from_default_format (GstAudioConvert *this, GstBuffer *buf);
|
2003-04-14 01:20:30 +00:00
|
|
|
|
2003-07-19 22:58:41 +00:00
|
|
|
static GstBuffer * gst_audio_convert_channels (GstAudioConvert *this, GstBuffer *buf);
|
2003-04-14 01:20:30 +00:00
|
|
|
|
|
|
|
/* AudioConvert signals and args */
|
|
|
|
enum {
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
enum {
|
|
|
|
ARG_0,
|
2003-04-15 03:39:22 +00:00
|
|
|
ARG_AGGRESSIVE,
|
2003-04-14 01:20:30 +00:00
|
|
|
};
|
|
|
|
|
2003-07-19 22:58:41 +00:00
|
|
|
static GstElementClass *parent_class = NULL;
|
|
|
|
/*static guint gst_audio_convert_signals[LAST_SIGNAL] = { 0 }; */
|
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
/*** GSTREAMER PROTOTYPES *****************************************************/
|
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstStaticPadTemplate gst_audio_convert_src_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE (
|
2003-04-14 01:20:30 +00:00
|
|
|
"src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2003-12-22 01:47:09 +00:00
|
|
|
GST_STATIC_CAPS (
|
2004-01-12 19:46:45 +00:00
|
|
|
GST_AUDIO_INT_PAD_TEMPLATE_CAPS
|
2003-04-14 01:20:30 +00:00
|
|
|
)
|
2003-12-22 01:47:09 +00:00
|
|
|
);
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstStaticPadTemplate gst_audio_convert_sink_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE (
|
2003-04-14 01:20:30 +00:00
|
|
|
"sink",
|
2003-12-31 08:02:04 +00:00
|
|
|
GST_PAD_SINK,
|
2003-04-14 01:20:30 +00:00
|
|
|
GST_PAD_ALWAYS,
|
2003-12-22 01:47:09 +00:00
|
|
|
GST_STATIC_CAPS (
|
2004-01-12 19:46:45 +00:00
|
|
|
GST_AUDIO_INT_PAD_TEMPLATE_CAPS
|
2003-04-14 01:20:30 +00:00
|
|
|
)
|
2003-12-22 01:47:09 +00:00
|
|
|
);
|
2003-04-14 01:20:30 +00:00
|
|
|
|
|
|
|
/*** TYPE FUNCTIONS ***********************************************************/
|
|
|
|
|
|
|
|
GType
|
|
|
|
gst_audio_convert_get_type(void) {
|
|
|
|
static GType audio_convert_type = 0;
|
|
|
|
|
|
|
|
if (!audio_convert_type) {
|
|
|
|
static const GTypeInfo audio_convert_info = {
|
2003-11-01 11:41:42 +00:00
|
|
|
sizeof(GstAudioConvertClass),
|
|
|
|
gst_audio_convert_base_init,
|
2003-04-14 01:20:30 +00:00
|
|
|
NULL,
|
|
|
|
(GClassInitFunc)gst_audio_convert_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof(GstAudioConvert),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc)gst_audio_convert_init,
|
|
|
|
};
|
2003-07-19 22:58:41 +00:00
|
|
|
audio_convert_type = g_type_register_static(GST_TYPE_ELEMENT,
|
|
|
|
"GstAudioConvert",
|
|
|
|
&audio_convert_info, 0);
|
2003-04-14 01:20:30 +00:00
|
|
|
}
|
2004-01-12 19:46:45 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (audio_convert_debug, "audioconvert", 0, "audio conversion element");
|
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
return audio_convert_type;
|
|
|
|
}
|
|
|
|
|
2003-11-01 11:41:42 +00:00
|
|
|
static void
|
|
|
|
gst_audio_convert_base_init (gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
2003-12-22 01:47:09 +00:00
|
|
|
gst_static_pad_template_get (&gst_audio_convert_src_template));
|
2003-11-01 11:41:42 +00:00
|
|
|
gst_element_class_add_pad_template (element_class,
|
2003-12-22 01:47:09 +00:00
|
|
|
gst_static_pad_template_get (&gst_audio_convert_sink_template));
|
2003-11-01 11:41:42 +00:00
|
|
|
gst_element_class_set_details (element_class, &audio_convert_details);
|
|
|
|
}
|
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
static void
|
|
|
|
gst_audio_convert_class_init (GstAudioConvertClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass*)klass;
|
|
|
|
gstelement_class = (GstElementClass*)klass;
|
|
|
|
|
|
|
|
parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
|
|
|
|
|
2003-07-19 22:58:41 +00:00
|
|
|
gobject_class->set_property = gst_audio_convert_set_property;
|
|
|
|
gobject_class->get_property = gst_audio_convert_get_property;
|
|
|
|
|
2003-04-15 03:39:22 +00:00
|
|
|
g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_AGGRESSIVE,
|
2003-07-19 22:58:41 +00:00
|
|
|
g_param_spec_boolean("aggressive", "aggressive mode",
|
|
|
|
"if true, tries any possible format before giving up",
|
2003-04-14 01:20:30 +00:00
|
|
|
FALSE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
|
|
|
|
|
2004-01-12 19:46:45 +00:00
|
|
|
gstelement_class->change_state = gst_audio_convert_change_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstCaps *
|
|
|
|
gst_audioconvert_getcaps (GstPad *pad)
|
|
|
|
{
|
|
|
|
GstAudioConvert *this;
|
|
|
|
GstCaps *othercaps;
|
|
|
|
GstCaps *caps;
|
|
|
|
GstPad *otherpad;
|
|
|
|
int i;
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-01-12 19:46:45 +00:00
|
|
|
GST_DEBUG ("gst_audioconvert_getcaps");
|
|
|
|
this = GST_AUDIO_CONVERT (gst_pad_get_parent (pad));
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-01-12 19:46:45 +00:00
|
|
|
otherpad = (pad == this->src) ? this->sink : this->src;
|
|
|
|
othercaps = gst_pad_get_allowed_caps (otherpad);
|
2003-04-14 01:20:30 +00:00
|
|
|
|
2004-01-12 19:46:45 +00:00
|
|
|
GST_DEBUG_CAPS ("othercaps are", othercaps);
|
|
|
|
|
|
|
|
caps = gst_caps_copy (othercaps);
|
|
|
|
for (i = 0; i < gst_caps_get_size (caps); i++) {
|
|
|
|
GstStructure *structure = gst_caps_get_structure (caps, i);
|
|
|
|
|
|
|
|
gst_audio_structure_set_int (structure, GST_AUDIO_FIELD_CHANNELS |
|
|
|
|
GST_AUDIO_FIELD_ENDIANNESS |
|
|
|
|
GST_AUDIO_FIELD_WIDTH |
|
|
|
|
GST_AUDIO_FIELD_DEPTH |
|
|
|
|
GST_AUDIO_FIELD_SIGNED);
|
|
|
|
|
|
|
|
/* FIXME:
|
|
|
|
* since gst_structure_set doesn't handle lists, we need to do lists
|
|
|
|
* manually; would be nice if this could be done more easily */
|
|
|
|
}
|
|
|
|
return caps;
|
2003-04-14 01:20:30 +00:00
|
|
|
}
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
static void
|
|
|
|
gst_audio_convert_init (GstAudioConvert *this)
|
|
|
|
{
|
|
|
|
/* sinkpad */
|
2003-12-22 01:47:09 +00:00
|
|
|
this->sink = gst_pad_new_from_template (
|
|
|
|
gst_static_pad_template_get (&gst_audio_convert_sink_template), "sink");
|
2003-04-14 01:20:30 +00:00
|
|
|
gst_pad_set_link_function (this->sink, gst_audio_convert_link);
|
2004-01-12 19:46:45 +00:00
|
|
|
gst_pad_set_getcaps_function (this->sink, gst_audioconvert_getcaps);
|
2003-04-14 01:20:30 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT(this), this->sink);
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
/* srcpad */
|
2003-12-22 01:47:09 +00:00
|
|
|
this->src = gst_pad_new_from_template (
|
|
|
|
gst_static_pad_template_get (&gst_audio_convert_src_template), "src");
|
2003-04-14 01:20:30 +00:00
|
|
|
gst_pad_set_link_function (this->src, gst_audio_convert_link);
|
2004-01-12 19:46:45 +00:00
|
|
|
gst_pad_set_getcaps_function (this->src, gst_audioconvert_getcaps);
|
2003-04-14 01:20:30 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT(this), this->src);
|
|
|
|
|
|
|
|
gst_pad_set_chain_function(this->sink, gst_audio_convert_chain);
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
/* clear important variables */
|
|
|
|
this->caps_set[0] = this->caps_set[1] = FALSE;
|
2003-07-19 22:58:41 +00:00
|
|
|
this->convert_internal = NULL;
|
2003-04-14 01:20:30 +00:00
|
|
|
}
|
|
|
|
static void
|
|
|
|
gst_audio_convert_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GstAudioConvert *audio_convert;
|
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
|
|
|
g_return_if_fail(GST_IS_AUDIO_CONVERT(object));
|
|
|
|
audio_convert = GST_AUDIO_CONVERT(object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
2003-04-15 03:39:22 +00:00
|
|
|
case ARG_AGGRESSIVE:
|
|
|
|
audio_convert->aggressive = g_value_get_boolean (value);
|
2003-04-14 01:20:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_audio_convert_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GstAudioConvert *audio_convert;
|
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
|
|
|
g_return_if_fail(GST_IS_AUDIO_CONVERT(object));
|
|
|
|
audio_convert = GST_AUDIO_CONVERT(object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
2003-04-15 03:39:22 +00:00
|
|
|
case ARG_AGGRESSIVE:
|
|
|
|
g_value_set_boolean (value, audio_convert->aggressive);
|
2003-04-14 01:20:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*** GSTREAMER FUNCTIONS ******************************************************/
|
|
|
|
|
|
|
|
static void
|
2003-10-08 16:08:22 +00:00
|
|
|
gst_audio_convert_chain (GstPad *pad, GstData *_data)
|
2003-04-14 01:20:30 +00:00
|
|
|
{
|
2003-10-08 16:08:22 +00:00
|
|
|
GstBuffer *buf = GST_BUFFER (_data);
|
2003-04-14 01:20:30 +00:00
|
|
|
GstAudioConvert *this;
|
|
|
|
|
2004-01-12 19:46:45 +00:00
|
|
|
g_return_if_fail (GST_IS_PAD (pad));
|
|
|
|
g_return_if_fail (buf != NULL);
|
|
|
|
g_return_if_fail (GST_IS_AUDIO_CONVERT (GST_OBJECT_PARENT (pad)));
|
|
|
|
this = GST_AUDIO_CONVERT (GST_OBJECT_PARENT (pad));
|
2003-04-14 01:20:30 +00:00
|
|
|
|
|
|
|
/* FIXME */
|
|
|
|
if (GST_IS_EVENT (buf)) {
|
|
|
|
gst_pad_event_default (pad, GST_EVENT (buf));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-07-19 22:58:41 +00:00
|
|
|
g_assert(this->caps_set[0] && this->caps_set[1]);
|
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
/**
|
|
|
|
* Theory of operation:
|
2003-07-19 22:58:41 +00:00
|
|
|
* - convert the format (endianness, signedness, width, depth) to
|
2003-04-14 01:20:30 +00:00
|
|
|
* (G_BYTE_ORDER, TRUE, 32, 32)
|
|
|
|
* - convert rate and channels
|
|
|
|
* - convert back to output format
|
|
|
|
*/
|
|
|
|
|
|
|
|
buf = gst_audio_convert_buffer_to_default_format (this, buf);
|
|
|
|
|
|
|
|
buf = gst_audio_convert_channels (this, buf);
|
|
|
|
|
|
|
|
buf = gst_audio_convert_buffer_from_default_format (this, buf);
|
|
|
|
|
2003-10-08 16:08:22 +00:00
|
|
|
gst_pad_push (this->src, GST_DATA (buf));
|
2003-04-14 01:20:30 +00:00
|
|
|
}
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
static GstPadLinkReturn
|
2003-12-22 01:47:09 +00:00
|
|
|
gst_audio_convert_link (GstPad *pad, const GstCaps *caps)
|
2003-04-14 01:20:30 +00:00
|
|
|
{
|
|
|
|
GstAudioConvert *this;
|
2003-04-15 03:19:08 +00:00
|
|
|
gint nr = 0;
|
2003-04-14 01:20:30 +00:00
|
|
|
gint rate, endianness, depth, width, channels;
|
|
|
|
gboolean sign;
|
2003-12-22 01:47:09 +00:00
|
|
|
GstStructure *structure;
|
|
|
|
gboolean ret;
|
2003-04-14 01:20:30 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail(GST_IS_PAD(pad), GST_PAD_LINK_REFUSED);
|
|
|
|
g_return_val_if_fail(GST_IS_AUDIO_CONVERT(GST_OBJECT_PARENT (pad)), GST_PAD_LINK_REFUSED);
|
|
|
|
this = GST_AUDIO_CONVERT(GST_OBJECT_PARENT (pad));
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-01-12 19:46:45 +00:00
|
|
|
/* nr is 0 for sink pad, 1 for src pad */
|
2003-07-19 22:58:41 +00:00
|
|
|
nr = (pad == this->sink) ? 0 : (pad == this->src) ? 1 : -1;
|
|
|
|
g_assert (nr > -1);
|
2003-04-14 01:20:30 +00:00
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
structure = gst_caps_get_structure (caps, 0);
|
|
|
|
|
|
|
|
ret = gst_structure_get_int (structure, "channels", &channels);
|
|
|
|
ret &= gst_structure_get_boolean (structure, "signed", &sign);
|
|
|
|
ret &= gst_structure_get_int (structure, "depth", &depth);
|
|
|
|
ret &= gst_structure_get_int (structure, "width", &width);
|
|
|
|
ret &= gst_structure_get_int (structure, "rate", &rate);
|
|
|
|
endianness = G_BYTE_ORDER;
|
|
|
|
if (width != 8) {
|
|
|
|
ret &= gst_structure_get_int (structure, "endianness", &endianness);
|
2003-04-14 01:20:30 +00:00
|
|
|
}
|
2003-12-22 01:47:09 +00:00
|
|
|
|
2003-12-31 08:02:04 +00:00
|
|
|
if (!ret) {
|
2004-01-12 19:46:45 +00:00
|
|
|
GST_DEBUG ("could not get some values from structure");
|
2003-12-31 08:02:04 +00:00
|
|
|
return GST_PAD_LINK_REFUSED;
|
|
|
|
}
|
2003-12-22 01:47:09 +00:00
|
|
|
|
2004-01-12 19:46:45 +00:00
|
|
|
/* we don't convert rate changes, this is done by audioscale */
|
|
|
|
/* 1 - nr is "the other caps" */
|
2003-07-19 22:58:41 +00:00
|
|
|
if ((this->caps_set[1 - nr]) &&
|
2003-12-31 08:02:04 +00:00
|
|
|
(rate != this->rate[1 - nr])) {
|
|
|
|
GstPad *otherpad;
|
|
|
|
GstPadLinkReturn ret;
|
2004-01-12 19:46:45 +00:00
|
|
|
|
2003-12-31 08:02:04 +00:00
|
|
|
otherpad = (nr) ? this->src : this->sink;
|
2004-01-12 19:46:45 +00:00
|
|
|
if (gst_pad_is_negotiated (otherpad))
|
|
|
|
{
|
|
|
|
GstCaps *othercaps = gst_caps_copy (othercaps);
|
2003-12-31 08:02:04 +00:00
|
|
|
|
2004-01-12 18:59:57 +00:00
|
|
|
gst_caps_set_simple (othercaps, "rate", G_TYPE_INT, rate, NULL);
|
2003-12-31 08:02:04 +00:00
|
|
|
|
2004-01-12 18:59:57 +00:00
|
|
|
ret = gst_pad_try_set_caps (otherpad, othercaps);
|
2004-01-12 19:46:45 +00:00
|
|
|
if (GST_PAD_LINK_FAILED (ret))
|
|
|
|
{
|
|
|
|
GST_DEBUG ("could not gst_pad_try_set_caps on otherpad using othercaps");
|
|
|
|
return GST_PAD_LINK_REFUSED;
|
|
|
|
}
|
2004-01-12 18:59:57 +00:00
|
|
|
}
|
2004-01-12 19:46:45 +00:00
|
|
|
this->rate[1 - nr] = rate;
|
2003-12-31 08:02:04 +00:00
|
|
|
}
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-01-12 19:46:45 +00:00
|
|
|
GST_DEBUG ("setting caps_set[%d] to TRUE", nr);
|
2003-04-14 01:20:30 +00:00
|
|
|
this->caps_set[nr] = TRUE;
|
|
|
|
this->rate[nr] = rate;
|
|
|
|
this->channels[nr] = channels;
|
|
|
|
this->sign[nr] = sign;
|
|
|
|
this->endian[nr] = endianness;
|
|
|
|
this->depth[nr] = depth;
|
2004-01-12 19:46:45 +00:00
|
|
|
this->width[nr] = width;
|
2003-04-14 01:20:30 +00:00
|
|
|
|
|
|
|
return GST_PAD_LINK_OK;
|
|
|
|
}
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
static GstElementStateReturn
|
|
|
|
gst_audio_convert_change_state (GstElement *element)
|
|
|
|
{
|
|
|
|
GstAudioConvert *this = GST_AUDIO_CONVERT (element);
|
|
|
|
|
|
|
|
switch (GST_STATE_TRANSITION (element)) {
|
|
|
|
case GST_STATE_PAUSED_TO_READY:
|
|
|
|
this->caps_set[0] = this->caps_set[1] = FALSE;
|
2003-07-19 22:58:41 +00:00
|
|
|
this->convert_internal = NULL;
|
2003-04-14 01:20:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent_class->change_state) {
|
|
|
|
return parent_class->change_state (element);
|
|
|
|
} else {
|
|
|
|
return GST_STATE_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-19 22:58:41 +00:00
|
|
|
/* return a writable buffer of size which ideally is the same as before
|
2003-04-14 01:20:30 +00:00
|
|
|
- You must unref the new buffer
|
|
|
|
- The size of the old buffer is undefined after this operation */
|
|
|
|
static GstBuffer*
|
|
|
|
gst_audio_convert_get_buffer (GstBuffer *buf, guint size)
|
|
|
|
{
|
|
|
|
GstBuffer *ret;
|
2003-04-16 18:36:29 +00:00
|
|
|
if (buf->maxsize >= size && gst_buffer_is_writable (buf)) {
|
2003-04-14 01:20:30 +00:00
|
|
|
gst_buffer_ref (buf);
|
|
|
|
buf->size = size;
|
|
|
|
return buf;
|
|
|
|
} else if (buf->maxsize >= size) {
|
|
|
|
buf = gst_buffer_copy (buf);
|
|
|
|
buf->size = size;
|
|
|
|
return buf;
|
|
|
|
} else {
|
|
|
|
g_assert ((ret = gst_buffer_new_and_alloc (size)));
|
|
|
|
ret->timestamp = buf->timestamp;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
static inline guint8 GUINT8_IDENTITY (guint8 x) { return x; }
|
|
|
|
static inline guint8 GINT8_IDENTITY (gint8 x) { return x; }
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
#define CONVERT_TO(to, from, type, sign, endianness, LE_FUNC, BE_FUNC) G_STMT_START{\
|
2003-07-19 22:58:41 +00:00
|
|
|
type value; \
|
|
|
|
memcpy (&value, from, sizeof (type)); \
|
|
|
|
from -= sizeof (type); \
|
|
|
|
value = (endianness == G_LITTLE_ENDIAN) ? LE_FUNC (value) : BE_FUNC (value); \
|
|
|
|
if (sign) { \
|
|
|
|
to = value; \
|
|
|
|
} else { \
|
|
|
|
to = (gint64) value - (1 << (sizeof (type) * 8 - 1)); \
|
|
|
|
} \
|
2003-04-14 01:20:30 +00:00
|
|
|
}G_STMT_END;
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
static GstBuffer*
|
|
|
|
gst_audio_convert_buffer_to_default_format (GstAudioConvert *this, GstBuffer *buf)
|
|
|
|
{
|
|
|
|
GstBuffer *ret;
|
|
|
|
gint i, count;
|
2003-04-15 03:19:08 +00:00
|
|
|
gint64 cur = 0;
|
2003-04-14 01:20:30 +00:00
|
|
|
gint32 write;
|
2003-04-15 19:10:14 +00:00
|
|
|
gint32 *dest;
|
|
|
|
guint8 *src;
|
2003-04-14 01:20:30 +00:00
|
|
|
|
2004-01-12 19:46:45 +00:00
|
|
|
if (this->width[0] == 32 && this->depth[0] == 32 &&
|
2003-04-14 01:20:30 +00:00
|
|
|
this->endian[0] == G_BYTE_ORDER && this->sign[0] == TRUE)
|
|
|
|
return buf;
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-01-12 19:46:45 +00:00
|
|
|
ret = gst_audio_convert_get_buffer (buf, buf->size * 32 / this->width[0]);
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
count = ret->size / 4;
|
2004-01-12 19:46:45 +00:00
|
|
|
src = buf->data + (count - 1) * (this->width[0] / 8);
|
2003-04-15 19:10:14 +00:00
|
|
|
dest = (gint32 *) ret->data;
|
|
|
|
for (i = count - 1; i >= 0; i--) {
|
2003-04-14 01:20:30 +00:00
|
|
|
switch (this->width[0]) {
|
2004-01-12 19:46:45 +00:00
|
|
|
case 8:
|
2003-07-19 22:58:41 +00:00
|
|
|
if (this->sign[0]) {
|
|
|
|
CONVERT_TO (cur, src, gint8, this->sign[0], this->endian[0], GINT8_IDENTITY, GINT8_IDENTITY);
|
|
|
|
} else {
|
|
|
|
CONVERT_TO (cur, src, guint16, this->sign[0], this->endian[0], GUINT8_IDENTITY, GUINT8_IDENTITY);
|
|
|
|
}
|
|
|
|
break;
|
2004-01-12 19:46:45 +00:00
|
|
|
case 16:
|
2003-07-19 22:58:41 +00:00
|
|
|
if (this->sign[0]) {
|
|
|
|
CONVERT_TO (cur, src, gint16, this->sign[0], this->endian[0], GINT16_FROM_LE, GINT16_FROM_BE);
|
|
|
|
} else {
|
|
|
|
CONVERT_TO (cur, src, guint16, this->sign[0], this->endian[0], GUINT16_FROM_LE, GUINT16_FROM_BE);
|
|
|
|
}
|
|
|
|
break;
|
2004-01-12 19:46:45 +00:00
|
|
|
case 32:
|
2003-07-19 22:58:41 +00:00
|
|
|
if (this->sign[0]) {
|
|
|
|
CONVERT_TO (cur, src, gint32, this->sign[0], this->endian[0], GINT32_FROM_LE, GINT32_FROM_BE);
|
|
|
|
} else {
|
|
|
|
CONVERT_TO (cur, src, guint32, this->sign[0], this->endian[0], GUINT32_FROM_LE, GUINT32_FROM_BE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
2003-04-14 01:20:30 +00:00
|
|
|
}
|
|
|
|
cur = cur * ((gint64) 1 << (32 - this->depth[0]));
|
|
|
|
cur = CLAMP (cur, -((gint64)1 << 32), (gint64) 0x7FFFFFFF);
|
|
|
|
write = cur;
|
2003-04-15 19:10:14 +00:00
|
|
|
memcpy (&dest[i], &write, 4);
|
2003-04-14 01:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
return ret;
|
|
|
|
}
|
2003-07-19 22:58:41 +00:00
|
|
|
|
|
|
|
#define POPULATE(format, be_func, le_func) G_STMT_START{ \
|
|
|
|
format val; \
|
|
|
|
format* p = (format *) dest; \
|
|
|
|
int_value >>= (32 - this->depth[1]); \
|
|
|
|
val = (format) int_value; \
|
|
|
|
switch (this->endian[1]) { \
|
|
|
|
case G_LITTLE_ENDIAN: \
|
|
|
|
val = le_func (val); \
|
|
|
|
break; \
|
|
|
|
case G_BIG_ENDIAN: \
|
|
|
|
val = be_func (val); \
|
|
|
|
break; \
|
|
|
|
default: \
|
|
|
|
g_assert_not_reached (); \
|
|
|
|
}; \
|
|
|
|
*p = val; \
|
|
|
|
p ++; \
|
|
|
|
dest = (guint8 *) p; \
|
2003-04-14 01:20:30 +00:00
|
|
|
}G_STMT_END
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
static GstBuffer *
|
|
|
|
gst_audio_convert_buffer_from_default_format (GstAudioConvert *this, GstBuffer *buf)
|
|
|
|
{
|
|
|
|
GstBuffer *ret;
|
|
|
|
guint8 *dest;
|
|
|
|
guint count, i;
|
|
|
|
gint32 *src;
|
|
|
|
|
2004-01-12 19:46:45 +00:00
|
|
|
if (this->width[1] == 32 && this->depth[1] == 32 &&
|
2003-04-14 01:20:30 +00:00
|
|
|
this->endian[1] == G_BYTE_ORDER && this->sign[1] == TRUE)
|
|
|
|
return buf;
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-01-12 19:46:45 +00:00
|
|
|
ret = gst_audio_convert_get_buffer (buf, buf->size * this->width[1] / 32);
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
dest = ret->data;
|
2003-07-19 22:58:41 +00:00
|
|
|
src = (gint32 *) buf->data;
|
|
|
|
|
2004-01-12 19:46:45 +00:00
|
|
|
count = ret->size / (this->width[1] / 8);
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
gint32 int_value = *src;
|
|
|
|
src++;
|
|
|
|
switch (this->width[1]) {
|
2004-01-12 19:46:45 +00:00
|
|
|
case 8:
|
2003-07-19 22:58:41 +00:00
|
|
|
if (this->sign[1]) {
|
|
|
|
POPULATE (gint8, GINT8_IDENTITY, GINT8_IDENTITY);
|
|
|
|
} else {
|
|
|
|
POPULATE (guint8, GUINT8_IDENTITY, GUINT8_IDENTITY);
|
|
|
|
}
|
|
|
|
break;
|
2004-01-12 19:46:45 +00:00
|
|
|
case 16:
|
2003-07-19 22:58:41 +00:00
|
|
|
if (this->sign[1]) {
|
|
|
|
POPULATE (gint16, GINT16_TO_BE, GINT16_TO_LE);
|
|
|
|
} else {
|
|
|
|
POPULATE (guint16, GUINT16_TO_BE, GUINT16_TO_LE);
|
|
|
|
}
|
|
|
|
break;
|
2004-01-12 19:46:45 +00:00
|
|
|
case 32:
|
2003-07-19 22:58:41 +00:00
|
|
|
if (this->sign[1]) {
|
|
|
|
POPULATE (gint32, GINT32_TO_BE, GINT32_TO_LE);
|
|
|
|
} else {
|
|
|
|
POPULATE (guint32, GUINT32_TO_BE, GUINT32_TO_LE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
2003-04-14 01:20:30 +00:00
|
|
|
}
|
|
|
|
}
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
gst_buffer_unref(buf);
|
|
|
|
return ret;
|
|
|
|
}
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
static GstBuffer *
|
|
|
|
gst_audio_convert_channels (GstAudioConvert *this, GstBuffer *buf)
|
|
|
|
{
|
|
|
|
GstBuffer *ret;
|
2003-12-31 08:02:04 +00:00
|
|
|
gint i, count;
|
2003-04-14 01:20:30 +00:00
|
|
|
guint32 *src, *dest;
|
|
|
|
|
|
|
|
if (this->channels[0] == this->channels[1])
|
|
|
|
return buf;
|
|
|
|
|
2003-12-31 08:02:04 +00:00
|
|
|
count = GST_BUFFER_SIZE (buf) / 4 / this->channels[0];
|
|
|
|
ret = gst_audio_convert_get_buffer (buf, count * 4 * this->channels[1]);
|
|
|
|
src = (guint32 *) GST_BUFFER_DATA (buf);
|
|
|
|
dest = (guint32 *) GST_BUFFER_DATA (ret);
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
if (this->channels[0] > this->channels[1]) {
|
|
|
|
for (i = 0; i < count; i++) {
|
2003-04-15 19:10:14 +00:00
|
|
|
*dest = *src >> 1;
|
2003-04-14 01:20:30 +00:00
|
|
|
src++;
|
2003-04-15 19:10:14 +00:00
|
|
|
*dest += (*src + 1) >> 1;
|
2003-04-14 01:20:30 +00:00
|
|
|
src++;
|
|
|
|
dest++;
|
|
|
|
}
|
|
|
|
} else {
|
2003-04-15 19:10:14 +00:00
|
|
|
for (i = count - 1; i >= 0; i--) {
|
|
|
|
dest[2 * i] = dest[2 * i + 1] = src[i];
|
2003-04-14 01:20:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_unref(buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** PLUGIN DETAILS ***********************************************************/
|
|
|
|
|
|
|
|
static gboolean
|
2003-11-01 11:41:42 +00:00
|
|
|
plugin_init (GstPlugin *plugin)
|
2003-04-14 01:20:30 +00:00
|
|
|
{
|
2003-11-01 11:41:42 +00:00
|
|
|
if (!gst_element_register (plugin, "audioconvert", GST_RANK_NONE, GST_TYPE_AUDIO_CONVERT))
|
|
|
|
return FALSE;
|
2004-01-12 19:46:45 +00:00
|
|
|
if (!gst_plugin_load ("gstaudio")) return FALSE;
|
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2003-11-01 11:41:42 +00:00
|
|
|
GST_PLUGIN_DEFINE (
|
2003-04-14 01:20:30 +00:00
|
|
|
GST_VERSION_MAJOR,
|
|
|
|
GST_VERSION_MINOR,
|
|
|
|
"gstaudioconvert",
|
2003-11-01 11:41:42 +00:00
|
|
|
"Convert audio to different formats",
|
|
|
|
plugin_init,
|
|
|
|
VERSION,
|
|
|
|
"LGPL",
|
|
|
|
GST_PACKAGE,
|
|
|
|
GST_ORIGIN)
|