2001-12-22 23:26:33 +00:00
|
|
|
/* -*- c-basic-offset: 2 -*-
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (C) 1999-2001 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
*
|
|
|
|
* 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:13 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2003-10-08 07:50:58 +00:00
|
|
|
# include "config.h"
|
2003-06-29 19:46:13 +00:00
|
|
|
#endif
|
2003-10-08 07:50:58 +00:00
|
|
|
|
2001-12-22 23:26:33 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
2003-10-08 07:50:58 +00:00
|
|
|
#include <gst/gst.h>
|
2001-12-23 12:18:18 +00:00
|
|
|
#include <gst/audio/audio.h>
|
2003-10-08 07:50:58 +00:00
|
|
|
|
2001-12-22 23:26:33 +00:00
|
|
|
#include "gstspeed.h"
|
|
|
|
|
2003-10-08 07:50:58 +00:00
|
|
|
/* buffer size to make if no bufferpool is available, must be divisible by
|
2001-12-22 23:26:33 +00:00
|
|
|
* sizeof(gfloat) */
|
|
|
|
#define SPEED_BUFSIZE 4096
|
|
|
|
/* number of buffers to allocate per chunk in sink buffer pool */
|
|
|
|
#define SPEED_NUMBUF 6
|
|
|
|
|
2002-09-18 19:02:52 +00:00
|
|
|
/* elementfactory information */
|
2003-11-02 22:34:11 +00:00
|
|
|
static GstElementDetails speed_details = GST_ELEMENT_DETAILS (
|
2001-12-22 23:26:33 +00:00
|
|
|
"Speed",
|
2002-04-20 21:42:51 +00:00
|
|
|
"Filter/Audio/Effect",
|
2001-12-22 23:26:33 +00:00
|
|
|
"Set speed/pitch on audio/raw streams (resampler)",
|
2003-11-02 22:34:11 +00:00
|
|
|
"Andy Wingo <apwingo@eos.ncsu.edu>"
|
|
|
|
);
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Filter signals and args */
|
|
|
|
enum {
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_0,
|
|
|
|
ARG_SPEED
|
|
|
|
};
|
|
|
|
|
2003-10-08 07:50:58 +00:00
|
|
|
static GstPadTemplate*
|
|
|
|
speed_sink_factory (void)
|
|
|
|
{
|
|
|
|
static GstPadTemplate *template = NULL;
|
|
|
|
|
|
|
|
if (!template) {
|
|
|
|
template = gst_pad_template_new
|
2001-12-22 23:26:33 +00:00
|
|
|
("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
|
2003-07-06 20:49:52 +00:00
|
|
|
gst_caps_append(gst_caps_new ("sink_int", "audio/x-raw-int",
|
2003-10-09 02:23:01 +00:00
|
|
|
GST_AUDIO_INT_PAD_TEMPLATE_PROPS),
|
2003-07-06 20:49:52 +00:00
|
|
|
gst_caps_new ("sink_float", "audio/x-raw-float",
|
2003-07-19 23:47:42 +00:00
|
|
|
GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS)),
|
2001-12-22 23:26:33 +00:00
|
|
|
NULL);
|
2003-10-08 07:50:58 +00:00
|
|
|
}
|
|
|
|
return template;
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstPadTemplate*
|
|
|
|
speed_src_factory (void)
|
|
|
|
{
|
|
|
|
static GstPadTemplate *template = NULL;
|
2003-10-08 07:50:58 +00:00
|
|
|
|
2001-12-22 23:26:33 +00:00
|
|
|
if (!template)
|
2003-10-08 07:50:58 +00:00
|
|
|
template = gst_pad_template_new
|
2001-12-22 23:26:33 +00:00
|
|
|
("src", GST_PAD_SRC, GST_PAD_ALWAYS,
|
2003-07-06 20:49:52 +00:00
|
|
|
gst_caps_append (gst_caps_new ("src_float", "audio/x-raw-float",
|
2003-07-19 23:47:42 +00:00
|
|
|
GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS),
|
2003-07-06 20:49:52 +00:00
|
|
|
gst_caps_new ("src_int", "audio/x-raw-int",
|
2003-10-09 02:23:01 +00:00
|
|
|
GST_AUDIO_INT_PAD_TEMPLATE_PROPS)),
|
2001-12-22 23:26:33 +00:00
|
|
|
NULL);
|
2003-10-08 07:50:58 +00:00
|
|
|
|
2001-12-22 23:26:33 +00:00
|
|
|
return template;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstBufferPool*
|
|
|
|
speed_sink_get_bufferpool (GstPad *pad)
|
|
|
|
{
|
|
|
|
GstSpeed *filter;
|
2003-10-08 07:50:58 +00:00
|
|
|
|
2001-12-22 23:26:33 +00:00
|
|
|
filter = GST_SPEED (gst_pad_get_parent(pad));
|
|
|
|
|
|
|
|
return filter->sinkpool;
|
|
|
|
}
|
|
|
|
|
2003-11-02 22:34:11 +00:00
|
|
|
static void speed_base_init (gpointer g_class);
|
2001-12-22 23:26:33 +00:00
|
|
|
static void speed_class_init (GstSpeedClass *klass);
|
|
|
|
static void speed_init (GstSpeed *filter);
|
|
|
|
|
|
|
|
static void speed_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
|
|
|
|
static void speed_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
|
|
|
|
|
2002-02-17 17:34:23 +00:00
|
|
|
static gboolean speed_parse_caps (GstSpeed *filter, GstCaps *caps);
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
static void speed_loop (GstElement *element);
|
|
|
|
|
|
|
|
static GstElementClass *parent_class = NULL;
|
2002-03-19 04:10:06 +00:00
|
|
|
/*static guint gst_filter_signals[LAST_SIGNAL] = { 0 }; */
|
2001-12-22 23:26:33 +00:00
|
|
|
|
2003-01-10 13:38:32 +00:00
|
|
|
static GstPadLinkReturn
|
2003-10-08 07:50:58 +00:00
|
|
|
speed_link (GstPad *pad, GstCaps *caps)
|
2001-12-22 23:26:33 +00:00
|
|
|
{
|
2002-02-17 17:34:23 +00:00
|
|
|
GstSpeed *filter;
|
2002-02-19 05:59:06 +00:00
|
|
|
GstPad *otherpad;
|
2003-10-08 07:50:58 +00:00
|
|
|
|
2002-02-17 17:34:23 +00:00
|
|
|
filter = GST_SPEED (gst_pad_get_parent (pad));
|
2003-01-10 10:22:25 +00:00
|
|
|
g_return_val_if_fail (filter != NULL, GST_PAD_LINK_REFUSED);
|
|
|
|
g_return_val_if_fail (GST_IS_SPEED (filter), GST_PAD_LINK_REFUSED);
|
2002-02-19 05:59:06 +00:00
|
|
|
otherpad = (pad == filter->srcpad ? filter->sinkpad : filter->srcpad);
|
2003-10-08 07:50:58 +00:00
|
|
|
|
2002-02-17 17:34:23 +00:00
|
|
|
if (GST_CAPS_IS_FIXED (caps)) {
|
2003-10-08 07:50:58 +00:00
|
|
|
if (! speed_parse_caps (filter, caps)) return GST_PAD_LINK_REFUSED;
|
|
|
|
|
2002-09-10 09:31:40 +00:00
|
|
|
return gst_pad_try_set_caps(otherpad, caps);
|
2002-02-17 17:34:23 +00:00
|
|
|
}
|
2003-10-08 07:50:58 +00:00
|
|
|
|
2003-01-10 10:22:25 +00:00
|
|
|
return GST_PAD_LINK_DELAYED;
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
|
2002-02-17 17:34:23 +00:00
|
|
|
static gboolean
|
2001-12-22 23:26:33 +00:00
|
|
|
speed_parse_caps (GstSpeed *filter, GstCaps *caps)
|
|
|
|
{
|
2003-07-06 20:49:52 +00:00
|
|
|
const gchar *mimetype;
|
2003-07-19 23:47:42 +00:00
|
|
|
|
2003-10-08 07:50:58 +00:00
|
|
|
g_return_val_if_fail(filter != NULL, FALSE);
|
|
|
|
g_return_val_if_fail(caps != NULL, FALSE);
|
2003-07-19 23:47:42 +00:00
|
|
|
|
2002-03-30 17:06:26 +00:00
|
|
|
gst_caps_get_int (caps, "rate", &filter->rate);
|
|
|
|
gst_caps_get_int (caps, "channels", &filter->channels);
|
2003-07-19 23:47:42 +00:00
|
|
|
gst_caps_get_int (caps, "width", &filter->width);
|
|
|
|
gst_caps_get_int (caps, "endianness", &filter->endianness);
|
2003-10-08 07:50:58 +00:00
|
|
|
gst_caps_get_int (caps, "buffer-frames", &filter->buffer_frames);
|
|
|
|
|
|
|
|
mimetype = gst_caps_get_mime (caps);
|
2003-07-19 23:47:42 +00:00
|
|
|
|
2003-10-08 07:50:58 +00:00
|
|
|
if (! strncmp(mimetype, "audio/x-raw-int", 15)) {
|
2003-07-19 23:47:42 +00:00
|
|
|
filter->format = GST_SPEED_FORMAT_INT;
|
2003-10-08 07:50:58 +00:00
|
|
|
gst_caps_get_int (caps, "depth", &filter->depth);
|
|
|
|
gst_caps_get_boolean (caps, "signed", &filter->is_signed);
|
|
|
|
} else if (! strncmp(mimetype, "audio/x-raw-float", 17)) {
|
2003-07-19 23:47:42 +00:00
|
|
|
filter->format = GST_SPEED_FORMAT_FLOAT;
|
2001-12-22 23:26:33 +00:00
|
|
|
} else {
|
2002-02-17 17:34:23 +00:00
|
|
|
return FALSE;
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
2002-02-17 17:34:23 +00:00
|
|
|
return TRUE;
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GType
|
|
|
|
gst_speed_get_type(void) {
|
|
|
|
static GType speed_type = 0;
|
|
|
|
|
|
|
|
if (!speed_type) {
|
|
|
|
static const GTypeInfo speed_info = {
|
2003-11-02 22:34:11 +00:00
|
|
|
sizeof(GstSpeedClass),
|
|
|
|
speed_base_init,
|
2001-12-22 23:26:33 +00:00
|
|
|
NULL,
|
|
|
|
(GClassInitFunc)speed_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof(GstSpeed),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc)speed_init,
|
|
|
|
};
|
|
|
|
speed_type = g_type_register_static(GST_TYPE_ELEMENT, "GstSpeed", &speed_info, 0);
|
|
|
|
}
|
|
|
|
return speed_type;
|
|
|
|
}
|
|
|
|
|
2003-11-02 22:34:11 +00:00
|
|
|
static void
|
|
|
|
speed_base_init (gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
|
|
|
|
gst_element_class_set_details (element_class, &speed_details);
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class, speed_src_factory ());
|
|
|
|
gst_element_class_add_pad_template (element_class, speed_sink_factory ());
|
|
|
|
}
|
2001-12-22 23:26:33 +00:00
|
|
|
static void
|
|
|
|
speed_class_init (GstSpeedClass *klass)
|
|
|
|
{
|
2003-10-08 07:50:58 +00:00
|
|
|
GObjectClass *gobject_class = (GObjectClass*)klass;
|
2001-12-22 23:26:33 +00:00
|
|
|
|
2003-10-08 07:50:58 +00:00
|
|
|
gobject_class->set_property = speed_set_property;
|
|
|
|
gobject_class->get_property = speed_get_property;
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
|
|
|
|
|
|
|
|
g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_SPEED,
|
|
|
|
g_param_spec_float("speed","speed","speed",
|
2003-10-08 07:50:58 +00:00
|
|
|
0.1,40.0,1.0,G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
speed_init (GstSpeed *filter)
|
|
|
|
{
|
2003-10-08 07:50:58 +00:00
|
|
|
filter->sinkpad = gst_pad_new_from_template(speed_sink_factory (), "sink");
|
|
|
|
gst_pad_set_link_function(filter->sinkpad, speed_link);
|
2001-12-22 23:26:33 +00:00
|
|
|
gst_element_add_pad(GST_ELEMENT(filter),filter->sinkpad);
|
2003-10-08 07:50:58 +00:00
|
|
|
gst_pad_set_bufferpool_function (filter->sinkpad, speed_sink_get_bufferpool);
|
|
|
|
|
|
|
|
filter->srcpad = gst_pad_new_from_template(speed_src_factory (), "src");
|
|
|
|
gst_pad_set_link_function(filter->srcpad, speed_link);
|
2001-12-22 23:26:33 +00:00
|
|
|
gst_element_add_pad(GST_ELEMENT(filter),filter->srcpad);
|
2003-10-08 07:50:58 +00:00
|
|
|
|
2001-12-22 23:26:33 +00:00
|
|
|
gst_element_set_loop_function(GST_ELEMENT(filter),speed_loop);
|
2003-10-08 07:50:58 +00:00
|
|
|
|
|
|
|
filter->sinkpool = gst_buffer_pool_get_default(SPEED_BUFSIZE, SPEED_NUMBUF);
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
speed_loop (GstElement *element)
|
|
|
|
{
|
|
|
|
GstSpeed *filter = GST_SPEED(element);
|
|
|
|
GstBuffer *in, *out;
|
|
|
|
guint i, j, nin, nout;
|
|
|
|
gfloat interp, speed, lower, i_float;
|
2003-10-08 07:50:58 +00:00
|
|
|
|
2001-12-22 23:26:33 +00:00
|
|
|
g_return_if_fail(filter != NULL);
|
|
|
|
g_return_if_fail(GST_IS_SPEED(filter));
|
|
|
|
|
|
|
|
filter->srcpool = gst_pad_get_bufferpool(filter->srcpad);
|
2003-10-08 07:50:58 +00:00
|
|
|
|
2001-12-22 23:26:33 +00:00
|
|
|
i = j = 0;
|
|
|
|
speed = filter->speed;
|
2003-10-08 16:08:19 +00:00
|
|
|
|
|
|
|
in = GST_BUFFER (gst_pad_pull(filter->sinkpad));
|
2003-10-08 07:50:58 +00:00
|
|
|
|
2002-05-30 19:08:04 +00:00
|
|
|
if (GST_IS_EVENT (in)) {
|
|
|
|
gst_pad_event_default (filter->sinkpad, GST_EVENT (in));
|
|
|
|
return;
|
|
|
|
}
|
2003-10-08 07:50:58 +00:00
|
|
|
|
2002-05-27 04:17:57 +00:00
|
|
|
while (GST_IS_EVENT (in)) {
|
|
|
|
gst_pad_event_default (filter->srcpad, GST_EVENT (in));
|
2003-10-08 16:08:19 +00:00
|
|
|
in = GST_BUFFER (gst_pad_pull (filter->sinkpad));
|
2002-05-27 04:17:57 +00:00
|
|
|
}
|
|
|
|
|
2001-12-22 23:26:33 +00:00
|
|
|
/* this is a bit nasty, but hey, it's what you've got to do to keep the same
|
|
|
|
* algorithm and multiple data types in c. */
|
|
|
|
if (filter->format==GST_SPEED_FORMAT_FLOAT) {
|
|
|
|
#define _FORMAT gfloat
|
|
|
|
#include "filter.func"
|
|
|
|
#undef _FORMAT
|
|
|
|
} else if (filter->format==GST_SPEED_FORMAT_INT && filter->width==16) {
|
|
|
|
#define _FORMAT gint16
|
|
|
|
#include "filter.func"
|
|
|
|
#undef _FORMAT
|
|
|
|
} else if (filter->format==GST_SPEED_FORMAT_INT && filter->width==8) {
|
|
|
|
#define _FORMAT gint8
|
|
|
|
#include "filter.func"
|
|
|
|
#undef _FORMAT
|
2002-02-17 17:34:23 +00:00
|
|
|
} else {
|
2003-09-16 10:00:00 +00:00
|
|
|
gst_element_error (element, "capsnego was never performed, bailing...");
|
2002-05-27 04:17:57 +00:00
|
|
|
gst_element_yield (element);
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
speed_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GstSpeed *filter;
|
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
|
|
|
g_return_if_fail(GST_IS_SPEED(object));
|
|
|
|
filter = GST_SPEED(object);
|
|
|
|
|
2003-10-08 07:50:58 +00:00
|
|
|
switch (prop_id)
|
2001-12-22 23:26:33 +00:00
|
|
|
{
|
|
|
|
case ARG_SPEED:
|
|
|
|
filter->speed = g_value_get_float (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
speed_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GstSpeed *filter;
|
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
|
|
|
g_return_if_fail(GST_IS_SPEED(object));
|
|
|
|
filter = GST_SPEED(object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_SPEED:
|
|
|
|
g_value_set_float (value, filter->speed);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2003-11-02 22:34:11 +00:00
|
|
|
plugin_init (GstPlugin *plugin)
|
2001-12-22 23:26:33 +00:00
|
|
|
{
|
2003-11-02 22:34:11 +00:00
|
|
|
return gst_element_register(plugin, "speed", GST_RANK_NONE, GST_TYPE_SPEED);
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
|
2003-11-02 22:34:11 +00:00
|
|
|
GST_PLUGIN_DEFINE (
|
2001-12-22 23:26:33 +00:00
|
|
|
GST_VERSION_MAJOR,
|
|
|
|
GST_VERSION_MINOR,
|
|
|
|
"speed",
|
2003-11-02 22:34:11 +00:00
|
|
|
"Set speed/pitch on audio/raw streams (resampler)",
|
|
|
|
plugin_init,
|
|
|
|
VERSION,
|
|
|
|
GST_LICENSE,
|
|
|
|
GST_COPYRIGHT,
|
|
|
|
GST_PACKAGE,
|
|
|
|
GST_ORIGIN
|
|
|
|
)
|