1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
/* -*- 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.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include <math.h>
#include <gst/gst.h>
#include <gst/audio/audio.h>
#include "gstspeed.h"
/* buffer size to make if no bufferpool is available, must be divisible by
* sizeof(gfloat) */
#define SPEED_BUFSIZE 4096
/* number of buffers to allocate per chunk in sink buffer pool */
#define SPEED_NUMBUF 6
/* elementfactory information */
static GstElementDetails speed_details = {
"Speed",
"Filter/Audio/Effect",
"LGPL",
"Set speed/pitch on audio/raw streams (resampler)",
VERSION,
"Andy Wingo <apwingo@eos.ncsu.edu>",
"(C) 2001"
};
/* Filter signals and args */
enum {
/* FILL ME */
LAST_SIGNAL
};
enum {
ARG_0,
ARG_SPEED
};
static GstPadTemplate*
speed_sink_factory (void)
{
static GstPadTemplate *template = NULL;
if (!template) {
template = gst_pad_template_new
("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
gst_caps_append(gst_caps_new ("sink_int", "audio/x-raw-int",
GST_AUDIO_INT_STANDARD_PAD_TEMPLATE_PROPS),
gst_caps_new ("sink_float", "audio/x-raw-float",
GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS)),
NULL);
}
return template;
}
static GstPadTemplate*
speed_src_factory (void)
{
static GstPadTemplate *template = NULL;
if (!template)
template = gst_pad_template_new
("src", GST_PAD_SRC, GST_PAD_ALWAYS,
gst_caps_append (gst_caps_new ("src_float", "audio/x-raw-float",
GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS),
gst_caps_new ("src_int", "audio/x-raw-int",
GST_AUDIO_INT_STANDARD_PAD_TEMPLATE_PROPS)),
NULL);
return template;
}
static GstBufferPool*
speed_sink_get_bufferpool (GstPad *pad)
{
GstSpeed *filter;
filter = GST_SPEED (gst_pad_get_parent(pad));
return filter->sinkpool;
}
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);
static gboolean speed_parse_caps (GstSpeed *filter, GstCaps *caps);
static void speed_loop (GstElement *element);
static GstElementClass *parent_class = NULL;
/*static guint gst_filter_signals[LAST_SIGNAL] = { 0 }; */
static GstPadLinkReturn
speed_link (GstPad *pad, GstCaps *caps)
{
GstSpeed *filter;
GstPad *otherpad;
filter = GST_SPEED (gst_pad_get_parent (pad));
g_return_val_if_fail (filter != NULL, GST_PAD_LINK_REFUSED);
g_return_val_if_fail (GST_IS_SPEED (filter), GST_PAD_LINK_REFUSED);
otherpad = (pad == filter->srcpad ? filter->sinkpad : filter->srcpad);
if (GST_CAPS_IS_FIXED (caps)) {
if (! speed_parse_caps (filter, caps)) return GST_PAD_LINK_REFUSED;
return gst_pad_try_set_caps(otherpad, caps);
}
return GST_PAD_LINK_DELAYED;
}
static gboolean
speed_parse_caps (GstSpeed *filter, GstCaps *caps)
{
const gchar *mimetype;
g_return_val_if_fail(filter != NULL, FALSE);
g_return_val_if_fail(caps != NULL, FALSE);
gst_caps_get_int (caps, "rate", &filter->rate);
gst_caps_get_int (caps, "channels", &filter->channels);
gst_caps_get_int (caps, "width", &filter->width);
gst_caps_get_int (caps, "endianness", &filter->endianness);
gst_caps_get_int (caps, "buffer-frames", &filter->buffer_frames);
mimetype = gst_caps_get_mime (caps);
if (! strncmp(mimetype, "audio/x-raw-int", 15)) {
filter->format = GST_SPEED_FORMAT_INT;
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)) {
filter->format = GST_SPEED_FORMAT_FLOAT;
} else {
return FALSE;
}
return TRUE;
}
GType
gst_speed_get_type(void) {
static GType speed_type = 0;
if (!speed_type) {
static const GTypeInfo speed_info = {
sizeof(GstSpeedClass), NULL,
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;
}
static void
speed_class_init (GstSpeedClass *klass)
{
GObjectClass *gobject_class = (GObjectClass*)klass;
gobject_class->set_property = speed_set_property;
gobject_class->get_property = speed_get_property;
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",
0.1,40.0,1.0,G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
}
static void
speed_init (GstSpeed *filter)
{
filter->sinkpad = gst_pad_new_from_template(speed_sink_factory (), "sink");
gst_pad_set_link_function(filter->sinkpad, speed_link);
gst_element_add_pad(GST_ELEMENT(filter),filter->sinkpad);
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);
gst_element_add_pad(GST_ELEMENT(filter),filter->srcpad);
gst_element_set_loop_function(GST_ELEMENT(filter),speed_loop);
filter->sinkpool = gst_buffer_pool_get_default(SPEED_BUFSIZE, SPEED_NUMBUF);
}
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;
g_return_if_fail(filter != NULL);
g_return_if_fail(GST_IS_SPEED(filter));
filter->srcpool = gst_pad_get_bufferpool(filter->srcpad);
i = j = 0;
speed = filter->speed;
in = GST_BUFFER (gst_pad_pull(filter->sinkpad));
if (GST_IS_EVENT (in)) {
gst_pad_event_default (filter->sinkpad, GST_EVENT (in));
return;
}
while (GST_IS_EVENT (in)) {
gst_pad_event_default (filter->srcpad, GST_EVENT (in));
in = GST_BUFFER (gst_pad_pull (filter->sinkpad));
}
/* 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
} else {
gst_element_error (element, "capsnego was never performed, bailing...");
gst_element_yield (element);
}
}
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);
switch (prop_id)
{
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
plugin_init (GModule *module, GstPlugin *plugin)
{
GstElementFactory *factory;
factory = gst_element_factory_new("speed", GST_TYPE_SPEED, &speed_details);
g_return_val_if_fail(factory != NULL, FALSE);
gst_element_factory_add_pad_template (factory, speed_src_factory ());
gst_element_factory_add_pad_template (factory, speed_sink_factory ());
gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
return TRUE;
}
GstPluginDesc plugin_desc = {
GST_VERSION_MAJOR,
GST_VERSION_MINOR,
"speed",
plugin_init
};
|