1 /* GStreamer
2 * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2001 Steve Baker <stevebaker_org@yahoo.co.uk>
4 * 2003 Andy Wingo <wingo at pobox.com>
5 * 2016 Thibault Saunier <thibault.saunier@collabora.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include "gstlv2.h"
27 #include "gstlv2utils.h"
28
29 #include <string.h>
30 #include <math.h>
31 #include <glib.h>
32
33 #include <lilv/lilv.h>
34
35 #include <gst/audio/audio.h>
36 #include <gst/audio/gstaudiofilter.h>
37 #include <gst/audio/audio-channels.h>
38
39 GST_DEBUG_CATEGORY_EXTERN (lv2_debug);
40 #define GST_CAT_DEFAULT lv2_debug
41
42 typedef struct _lv2_control_info
43 {
44 gchar *name;
45 gchar *param_name;
46 gfloat lowerbound, upperbound;
47 gfloat def;
48 gboolean lower, upper, samplerate;
49 gboolean toggled, logarithmic, integer, writable;
50 } lv2_control_info;
51
52
53 typedef struct _GstLV2Filter GstLV2Filter;
54 typedef struct _GstLV2FilterClass GstLV2FilterClass;
55
56
57 struct _GstLV2Filter
58 {
59 GstAudioFilter parent;
60
61 GstLV2 lv2;
62 };
63
64 struct _GstLV2FilterClass
65 {
66 GstAudioFilterClass parent_class;
67
68 GstLV2Class lv2;
69 };
70
71 static GstAudioFilter *parent_class = NULL;
72
73 /* preset interface */
74
75 static gchar **
gst_lv2_filter_get_preset_names(GstPreset * preset)76 gst_lv2_filter_get_preset_names (GstPreset * preset)
77 {
78 GstLV2Filter *self = (GstLV2Filter *) preset;
79
80 return gst_lv2_get_preset_names (&self->lv2, (GstObject *) self);
81 }
82
83 static gboolean
gst_lv2_filter_load_preset(GstPreset * preset,const gchar * name)84 gst_lv2_filter_load_preset (GstPreset * preset, const gchar * name)
85 {
86 GstLV2Filter *self = (GstLV2Filter *) preset;
87
88 return gst_lv2_load_preset (&self->lv2, (GstObject *) self, name);
89 }
90
91 static gboolean
gst_lv2_filter_save_preset(GstPreset * preset,const gchar * name)92 gst_lv2_filter_save_preset (GstPreset * preset, const gchar * name)
93 {
94 GstLV2Filter *self = (GstLV2Filter *) preset;
95
96 return gst_lv2_save_preset (&self->lv2, (GstObject *) self, name);
97 }
98
99 static gboolean
gst_lv2_filter_rename_preset(GstPreset * preset,const gchar * old_name,const gchar * new_name)100 gst_lv2_filter_rename_preset (GstPreset * preset, const gchar * old_name,
101 const gchar * new_name)
102 {
103 return FALSE;
104 }
105
106 static gboolean
gst_lv2_filter_delete_preset(GstPreset * preset,const gchar * name)107 gst_lv2_filter_delete_preset (GstPreset * preset, const gchar * name)
108 {
109 GstLV2Filter *self = (GstLV2Filter *) preset;
110
111 return gst_lv2_delete_preset (&self->lv2, (GstObject *) self, name);
112 }
113
114 static gboolean
gst_lv2_filter_set_meta(GstPreset * preset,const gchar * name,const gchar * tag,const gchar * value)115 gst_lv2_filter_set_meta (GstPreset * preset, const gchar * name,
116 const gchar * tag, const gchar * value)
117 {
118 return FALSE;
119 }
120
121 static gboolean
gst_lv2_filter_get_meta(GstPreset * preset,const gchar * name,const gchar * tag,gchar ** value)122 gst_lv2_filter_get_meta (GstPreset * preset, const gchar * name,
123 const gchar * tag, gchar ** value)
124 {
125 *value = NULL;
126 return FALSE;
127 }
128
129 static void
gst_lv2_filter_preset_interface_init(gpointer g_iface,gpointer iface_data)130 gst_lv2_filter_preset_interface_init (gpointer g_iface, gpointer iface_data)
131 {
132 GstPresetInterface *iface = g_iface;
133
134 iface->get_preset_names = gst_lv2_filter_get_preset_names;
135 iface->load_preset = gst_lv2_filter_load_preset;
136 iface->save_preset = gst_lv2_filter_save_preset;
137 iface->rename_preset = gst_lv2_filter_rename_preset;
138 iface->delete_preset = gst_lv2_filter_delete_preset;
139 iface->set_meta = gst_lv2_filter_set_meta;
140 iface->get_meta = gst_lv2_filter_get_meta;
141 }
142
143
144 /* GObject vmethods implementation */
145 static void
gst_lv2_filter_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)146 gst_lv2_filter_set_property (GObject * object, guint prop_id,
147 const GValue * value, GParamSpec * pspec)
148 {
149 GstLV2Filter *self = (GstLV2Filter *) object;
150
151 gst_lv2_object_set_property (&self->lv2, object, prop_id, value, pspec);
152 }
153
154 static void
gst_lv2_filter_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)155 gst_lv2_filter_get_property (GObject * object, guint prop_id, GValue * value,
156 GParamSpec * pspec)
157 {
158 GstLV2Filter *self = (GstLV2Filter *) object;
159
160 gst_lv2_object_get_property (&self->lv2, object, prop_id, value, pspec);
161 }
162
163 static void
gst_lv2_filter_finalize(GObject * object)164 gst_lv2_filter_finalize (GObject * object)
165 {
166 GstLV2Filter *self = (GstLV2Filter *) object;
167
168 gst_lv2_finalize (&self->lv2);
169
170 G_OBJECT_CLASS (parent_class)->finalize (object);
171 }
172
173 #if 0
174 /* Convert an LV2 port role to a Gst channel position
175 * WARNING: If the group has only a single port,
176 * GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER will be returned for pg:centerRole
177 * (which is used by LV2 for mono groups), but this is not correct. In this
178 * case the value must be changed to GST_AUDIO_CHANNEL_POSITION_FRONT_MONO
179 * (this can't be done by this function because this information isn't known
180 * at the time it is used).
181 */
182 static GstAudioChannelPosition
183 gst_lv2_filter_role_to_position (LilvNode * role)
184 {
185 /* Front. Mono and left/right are mututally exclusive */
186 if (lilv_node_equals (role, gst_lv2_center_role_node)) {
187
188 return GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER;
189 } else if (lilv_node_equals (role, gst_lv2_left_role_node)) {
190 return GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
191 } else if (lilv_node_equals (role, gst_lv2_right_role_node)) {
192 return GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
193
194 /* Rear. Left/right and center are mututally exclusive */
195 } else if (lilv_node_equals (role, gst_lv2_rear_center_role_node)) {
196 return GST_AUDIO_CHANNEL_POSITION_REAR_CENTER;
197 } else if (lilv_node_equals (role, gst_lv2_rear_left_role_node)) {
198 return GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
199 } else if (lilv_node_equals (role, gst_lv2_rear_right_role_node)) {
200 return GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
201
202 /* Subwoofer/low-frequency-effects */
203 } else if (lilv_node_equals (role, gst_lv2_lfe_role_node)) {
204 return GST_AUDIO_CHANNEL_POSITION_LFE1;
205
206 /* Center front speakers. Center and left/right_of_center
207 * are mutually exclusive */
208 } else if (lilv_node_equals (role, gst_lv2_center_left_role_node)) {
209 return GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
210 } else if (lilv_node_equals (role, gst_lv2_center_right_role_node)) {
211 return GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
212
213 /* sides */
214 } else if (lilv_node_equals (role, gst_lv2_side_left_role_node)) {
215 return GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT;
216 } else if (lilv_node_equals (role, gst_lv2_side_right_role_node)) {
217 return GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT;
218 }
219
220 return GST_AUDIO_CHANNEL_POSITION_INVALID;
221 }
222
223 static GstAudioChannelPosition *
224 gst_lv2_filter_build_positions (GstLV2Group * group)
225 {
226 GstAudioChannelPosition *positions = NULL;
227
228 /* don't do anything for mono */
229 if (group->ports->len > 1) {
230 gint i;
231
232 positions = g_new (GstAudioChannelPosition, group->ports->len);
233 for (i = 0; i < group->ports->len; ++i)
234 positions[i] = g_array_index (group->ports, GstLV2Port, i).position;
235 }
236 return positions;
237 }
238 #endif
239
240 /* Find and return the group @a uri in @a groups, or NULL if not found */
241 static void
gst_lv2_filter_type_class_add_pad_templates(GstLV2FilterClass * klass)242 gst_lv2_filter_type_class_add_pad_templates (GstLV2FilterClass * klass)
243 {
244 GstCaps *srccaps, *sinkcaps;
245 GstPadTemplate *pad_template;
246 GstElementClass *elem_class = GST_ELEMENT_CLASS (klass);
247
248 gint in_channels = 1, out_channels = 1;
249
250 in_channels = klass->lv2.in_group.ports->len;
251
252 out_channels = klass->lv2.out_group.ports->len;
253
254 /* FIXME Implement deintereleaved audio support */
255 sinkcaps = gst_caps_new_simple ("audio/x-raw",
256 "format", G_TYPE_STRING, GST_AUDIO_NE (F32),
257 "channels", G_TYPE_INT, in_channels,
258 "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT,
259 "layout", G_TYPE_STRING, "interleaved", NULL);
260
261 srccaps = gst_caps_new_simple ("audio/x-raw",
262 "format", G_TYPE_STRING, GST_AUDIO_NE (F32),
263 "channels", G_TYPE_INT, out_channels,
264 "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT,
265 "layout", G_TYPE_STRING, "interleaved", NULL);
266
267 pad_template =
268 gst_pad_template_new (GST_BASE_TRANSFORM_SINK_NAME, GST_PAD_SINK,
269 GST_PAD_ALWAYS, sinkcaps);
270 gst_element_class_add_pad_template (elem_class, pad_template);
271
272 pad_template =
273 gst_pad_template_new (GST_BASE_TRANSFORM_SRC_NAME, GST_PAD_SRC,
274 GST_PAD_ALWAYS, srccaps);
275 gst_element_class_add_pad_template (elem_class, pad_template);
276
277 gst_caps_unref (sinkcaps);
278 gst_caps_unref (srccaps);
279 }
280
281 static gboolean
gst_lv2_filter_setup(GstAudioFilter * gsp,const GstAudioInfo * info)282 gst_lv2_filter_setup (GstAudioFilter * gsp, const GstAudioInfo * info)
283 {
284 GstLV2Filter *self = (GstLV2Filter *) gsp;
285
286 g_return_val_if_fail (self->lv2.activated == FALSE, FALSE);
287
288 GST_DEBUG_OBJECT (self, "instantiating the plugin at %d Hz",
289 GST_AUDIO_INFO_RATE (info));
290
291 if (!gst_lv2_setup (&self->lv2, GST_AUDIO_INFO_RATE (info)))
292 goto no_instance;
293
294 /* FIXME Handle audio channel positioning while negotiating CAPS */
295 #if 0
296 gint i;
297 /* set input group pad audio channel position */
298 for (i = 0; i < oclass->lv2.in_groups->len; ++i) {
299 group = &g_array_index (oclass->lv2.in_groups, GstLV2Group, i);
300 if (group->has_roles) {
301 if ((positions = gst_lv2_filter_build_positions (group))) {
302 if ((pad = gst_element_get_static_pad (GST_ELEMENT (gsp),
303 lilv_node_as_string (group->symbol)))) {
304 GST_INFO_OBJECT (self, "set audio channel positions on sink pad %s",
305 lilv_node_as_string (group->symbol));
306 s = gst_caps_get_structure (caps, 0);
307 gst_audio_set_channel_positions (s, positions);
308 gst_object_unref (pad);
309 }
310 g_free (positions);
311 positions = NULL;
312 }
313 }
314 }
315 /* set output group pad audio channel position */
316 for (i = 0; i < oclass->lv2.out_groups->len; ++i) {
317 group = &g_array_index (oclass->lv2.out_groups, GstLV2Group, i);
318 if (group->has_roles) {
319 if ((positions = gst_lv2_filter_build_positions (group))) {
320 if ((pad = gst_element_get_static_pad (GST_ELEMENT (gsp),
321 lilv_node_as_string (group->symbol)))) {
322 GST_INFO_OBJECT (self, "set audio channel positions on src pad %s",
323 lilv_node_as_string (group->symbol));
324 s = gst_caps_get_structure (caps, 0);
325 gst_audio_set_channel_positions (s, positions);
326 gst_object_unref (pad);
327 }
328 g_free (positions);
329 positions = NULL;
330 }
331 }
332 }
333 #endif
334
335 return TRUE;
336
337 no_instance:
338 {
339 GST_ERROR_OBJECT (gsp, "could not create instance");
340 return FALSE;
341 }
342 }
343
344 static gboolean
gst_lv2_filter_stop(GstBaseTransform * transform)345 gst_lv2_filter_stop (GstBaseTransform * transform)
346 {
347 GstLV2Filter *lv2 = (GstLV2Filter *) transform;
348
349 return gst_lv2_cleanup (&lv2->lv2, (GstObject *) lv2);
350 }
351
352 static inline void
gst_lv2_filter_deinterleave_data(guint n_channels,gfloat * outdata,guint samples,gfloat * indata)353 gst_lv2_filter_deinterleave_data (guint n_channels, gfloat * outdata,
354 guint samples, gfloat * indata)
355 {
356 guint i, j;
357
358 for (i = 0; i < n_channels; i++)
359 for (j = 0; j < samples; j++)
360 outdata[i * samples + j] = indata[j * n_channels + i];
361 }
362
363 static inline void
gst_lv2_filter_interleave_data(guint n_channels,gfloat * outdata,guint samples,gfloat * indata)364 gst_lv2_filter_interleave_data (guint n_channels, gfloat * outdata,
365 guint samples, gfloat * indata)
366 {
367 guint i, j;
368
369 for (i = 0; i < n_channels; i++)
370 for (j = 0; j < samples; j++) {
371 outdata[j * n_channels + i] = indata[i * samples + j];
372 }
373 }
374
375 static GstFlowReturn
gst_lv2_filter_transform_data(GstLV2Filter * self,GstMapInfo * in_map,GstMapInfo * out_map)376 gst_lv2_filter_transform_data (GstLV2Filter * self,
377 GstMapInfo * in_map, GstMapInfo * out_map)
378 {
379 GstLV2FilterClass *klass =
380 (GstLV2FilterClass *) GST_AUDIO_FILTER_GET_CLASS (self);
381 GstLV2Class *lv2_class = &klass->lv2;
382 GstLV2Group *lv2_group;
383 GstLV2Port *lv2_port;
384 guint j, k, l, nframes, samples, out_samples;
385 gfloat *in = NULL, *out = NULL, *cv = NULL, *mem;
386 gfloat val;
387
388 nframes = in_map->size / sizeof (float);
389
390 /* multi channel inputs */
391 lv2_group = &lv2_class->in_group;
392 samples = nframes / lv2_group->ports->len;
393 GST_LOG_OBJECT (self, "in : samples=%u, nframes=%u, ports=%d", samples,
394 nframes, lv2_group->ports->len);
395
396 if (lv2_group->ports->len > 1) {
397 in = g_new0 (gfloat, nframes);
398 out = g_new0 (gfloat, samples * lv2_group->ports->len);
399 gst_lv2_filter_deinterleave_data (lv2_group->ports->len, in,
400 samples, (gfloat *) in_map->data);
401 } else {
402 in = (gfloat *) in_map->data;
403 out = (gfloat *) out_map->data;
404 }
405
406 for (j = 0; j < lv2_group->ports->len; ++j) {
407 lv2_port = &g_array_index (lv2_group->ports, GstLV2Port, j);
408 lilv_instance_connect_port (self->lv2.instance, lv2_port->index,
409 in + (j * samples));
410 }
411
412 /* multi channel outputs */
413 lv2_group = &lv2_class->out_group;
414 out_samples = nframes / lv2_group->ports->len;
415
416 GST_LOG_OBJECT (self, "out: samples=%u, nframes=%u, ports=%d", out_samples,
417 nframes, lv2_group->ports->len);
418 for (j = 0; j < lv2_group->ports->len; ++j) {
419 lv2_port = &g_array_index (lv2_group->ports, GstLV2Port, j);
420 lilv_instance_connect_port (self->lv2.instance, lv2_port->index,
421 out + (j * out_samples));
422 }
423
424 /* cv ports */
425 cv = g_new (gfloat, samples * lv2_class->num_cv_in);
426 for (j = k = 0; j < lv2_class->control_in_ports->len; j++) {
427 lv2_port = &g_array_index (lv2_class->control_in_ports, GstLV2Port, j);
428 if (lv2_port->type != GST_LV2_PORT_CV)
429 continue;
430
431 mem = cv + (k * samples);
432 val = self->lv2.ports.control.in[j];
433 /* FIXME: use gst_control_binding_get_value_array */
434 for (l = 0; l < samples; l++)
435 mem[l] = val;
436 lilv_instance_connect_port (self->lv2.instance, lv2_port->index, mem);
437 k++;
438 }
439
440 lilv_instance_run (self->lv2.instance, samples);
441
442 if (lv2_group->ports->len > 1) {
443 gst_lv2_filter_interleave_data (lv2_group->ports->len,
444 (gfloat *) out_map->data, out_samples, out);
445 g_free (out);
446 g_free (in);
447 }
448 g_free (cv);
449
450 return GST_FLOW_OK;
451 }
452
453 static GstFlowReturn
gst_lv2_filter_transform_ip(GstBaseTransform * transform,GstBuffer * buf)454 gst_lv2_filter_transform_ip (GstBaseTransform * transform, GstBuffer * buf)
455 {
456 GstFlowReturn ret;
457 GstMapInfo map;
458
459 gst_buffer_map (buf, &map, GST_MAP_READWRITE);
460
461 ret = gst_lv2_filter_transform_data ((GstLV2Filter *) transform, &map, &map);
462
463 gst_buffer_unmap (buf, &map);
464
465 return ret;
466 }
467
468 static GstFlowReturn
gst_lv2_filter_transform(GstBaseTransform * transform,GstBuffer * inbuf,GstBuffer * outbuf)469 gst_lv2_filter_transform (GstBaseTransform * transform,
470 GstBuffer * inbuf, GstBuffer * outbuf)
471 {
472 GstMapInfo in_map, out_map;
473 GstFlowReturn ret;
474
475 gst_buffer_map (inbuf, &in_map, GST_MAP_READ);
476 gst_buffer_map (outbuf, &out_map, GST_MAP_WRITE);
477
478 ret = gst_lv2_filter_transform_data ((GstLV2Filter *) transform,
479 &in_map, &out_map);
480
481 gst_buffer_unmap (inbuf, &in_map);
482 gst_buffer_unmap (outbuf, &out_map);
483
484 return ret;
485 }
486
487 static void
gst_lv2_filter_base_init(gpointer g_class)488 gst_lv2_filter_base_init (gpointer g_class)
489 {
490 GstLV2FilterClass *klass = (GstLV2FilterClass *) g_class;
491 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
492
493 gst_lv2_class_init (&klass->lv2, G_TYPE_FROM_CLASS (klass));
494
495 gst_lv2_element_class_set_metadata (&klass->lv2, element_class,
496 "Filter/Effect/Audio/LV2");
497
498 gst_lv2_filter_type_class_add_pad_templates (klass);
499 }
500
501 static void
gst_lv2_filter_base_finalize(GstLV2FilterClass * lv2_class)502 gst_lv2_filter_base_finalize (GstLV2FilterClass * lv2_class)
503 {
504 gst_lv2_class_finalize (&lv2_class->lv2);
505 }
506
507 static void
gst_lv2_filter_class_init(GstLV2FilterClass * klass)508 gst_lv2_filter_class_init (GstLV2FilterClass * klass)
509 {
510 GObjectClass *gobject_class = (GObjectClass *) klass;
511 GstBaseTransformClass *transform_class = GST_BASE_TRANSFORM_CLASS (klass);
512 GstAudioFilterClass *audiofilter_class = GST_AUDIO_FILTER_CLASS (klass);
513
514 GST_DEBUG ("class_init %p", klass);
515
516 gobject_class->set_property = gst_lv2_filter_set_property;
517 gobject_class->get_property = gst_lv2_filter_get_property;
518 gobject_class->finalize = gst_lv2_filter_finalize;
519
520 audiofilter_class->setup = gst_lv2_filter_setup;
521
522 transform_class->stop = gst_lv2_filter_stop;
523 transform_class->transform = gst_lv2_filter_transform;
524 transform_class->transform_ip = gst_lv2_filter_transform_ip;
525
526 gst_lv2_class_install_properties (&klass->lv2, gobject_class, 1);
527 }
528
529 static void
gst_lv2_filter_init(GstLV2Filter * self,GstLV2FilterClass * klass)530 gst_lv2_filter_init (GstLV2Filter * self, GstLV2FilterClass * klass)
531 {
532 gst_lv2_init (&self->lv2, &klass->lv2);
533
534 if (!lilv_plugin_has_feature (klass->lv2.plugin,
535 gst_lv2_in_place_broken_pred_node))
536 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (self), TRUE);
537 }
538
539 void
gst_lv2_filter_register_element(GstPlugin * plugin,GstStructure * lv2_meta)540 gst_lv2_filter_register_element (GstPlugin * plugin, GstStructure * lv2_meta)
541 {
542 GTypeInfo info = {
543 sizeof (GstLV2FilterClass),
544 (GBaseInitFunc) gst_lv2_filter_base_init,
545 (GBaseFinalizeFunc) gst_lv2_filter_base_finalize,
546 (GClassInitFunc) gst_lv2_filter_class_init,
547 NULL,
548 NULL,
549 sizeof (GstLV2Filter),
550 0,
551 (GInstanceInitFunc) gst_lv2_filter_init,
552 };
553 const gchar *type_name =
554 gst_structure_get_string (lv2_meta, "element-type-name");
555 GType element_type =
556 g_type_register_static (GST_TYPE_AUDIO_FILTER, type_name, &info, 0);
557 gboolean can_do_presets;
558
559 /* register interfaces */
560 gst_structure_get_boolean (lv2_meta, "can-do-presets", &can_do_presets);
561 if (can_do_presets) {
562 const GInterfaceInfo preset_interface_info = {
563 (GInterfaceInitFunc) gst_lv2_filter_preset_interface_init,
564 NULL,
565 NULL
566 };
567 g_type_add_interface_static (element_type, GST_TYPE_PRESET,
568 &preset_interface_info);
569 }
570
571 gst_element_register (plugin, type_name, GST_RANK_NONE, element_type);
572
573 if (!parent_class)
574 parent_class = g_type_class_ref (GST_TYPE_AUDIO_FILTER);
575 }
576