1 /*
2 * Copyright (c) 2017 Paul B Mahol
3 * Copyright (c) 2007-2016 David Robillard <http://drobilla.net>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg 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 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * LV2 wrapper
25 */
26
27 #include <lilv/lilv.h>
28 #include <lv2/lv2plug.in/ns/ext/atom/atom.h>
29 #include <lv2/lv2plug.in/ns/ext/buf-size/buf-size.h>
30
31 #include "libavutil/avassert.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/channel_layout.h"
34 #include "libavutil/opt.h"
35 #include "audio.h"
36 #include "avfilter.h"
37 #include "internal.h"
38
39 typedef struct URITable {
40 char **uris;
41 size_t n_uris;
42 } URITable;
43
44 typedef struct LV2Context {
45 const AVClass *class;
46 char *plugin_uri;
47 char *options;
48
49 unsigned nb_inputs;
50 unsigned nb_inputcontrols;
51 unsigned nb_outputs;
52
53 int sample_rate;
54 int nb_samples;
55 int64_t pts;
56 int64_t duration;
57
58 LilvWorld *world;
59 const LilvPlugin *plugin;
60 uint32_t nb_ports;
61 float *values;
62 URITable uri_table;
63 LV2_URID_Map map;
64 LV2_Feature map_feature;
65 LV2_URID_Unmap unmap;
66 LV2_Feature unmap_feature;
67 LV2_Atom_Sequence seq_in[2];
68 LV2_Atom_Sequence *seq_out;
69 const LV2_Feature *features[5];
70
71 float *mins;
72 float *maxes;
73 float *controls;
74
75 LilvInstance *instance;
76
77 LilvNode *atom_AtomPort;
78 LilvNode *atom_Sequence;
79 LilvNode *lv2_AudioPort;
80 LilvNode *lv2_CVPort;
81 LilvNode *lv2_ControlPort;
82 LilvNode *lv2_Optional;
83 LilvNode *lv2_InputPort;
84 LilvNode *lv2_OutputPort;
85 LilvNode *urid_map;
86 LilvNode *powerOf2BlockLength;
87 LilvNode *fixedBlockLength;
88 LilvNode *boundedBlockLength;
89 } LV2Context;
90
91 #define OFFSET(x) offsetof(LV2Context, x)
92 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
93
94 static const AVOption lv2_options[] = {
95 { "plugin", "set plugin uri", OFFSET(plugin_uri), AV_OPT_TYPE_STRING, .flags = FLAGS },
96 { "p", "set plugin uri", OFFSET(plugin_uri), AV_OPT_TYPE_STRING, .flags = FLAGS },
97 { "controls", "set plugin options", OFFSET(options), AV_OPT_TYPE_STRING, .flags = FLAGS },
98 { "c", "set plugin options", OFFSET(options), AV_OPT_TYPE_STRING, .flags = FLAGS },
99 { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT32_MAX, FLAGS },
100 { "s", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT32_MAX, FLAGS },
101 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
102 { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
103 { "duration", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },
104 { "d", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },
105 { NULL }
106 };
107
108 AVFILTER_DEFINE_CLASS(lv2);
109
uri_table_init(URITable * table)110 static void uri_table_init(URITable *table)
111 {
112 table->uris = NULL;
113 table->n_uris = 0;
114 }
115
uri_table_destroy(URITable * table)116 static void uri_table_destroy(URITable *table)
117 {
118 int i;
119
120 for (i = 0; i < table->n_uris; i++) {
121 av_freep(&table->uris[i]);
122 }
123
124 av_freep(&table->uris);
125 }
126
uri_table_map(LV2_URID_Map_Handle handle,const char * uri)127 static LV2_URID uri_table_map(LV2_URID_Map_Handle handle, const char *uri)
128 {
129 URITable *table = (URITable*)handle;
130 const size_t len = strlen(uri);
131 size_t i;
132 char **tmp;
133
134 for (i = 0; i < table->n_uris; i++) {
135 if (!strcmp(table->uris[i], uri)) {
136 return i + 1;
137 }
138 }
139
140 tmp = av_calloc(table->n_uris + 1, sizeof(char*));
141 if (!tmp)
142 return table->n_uris;
143 memcpy(tmp, table->uris, table->n_uris * sizeof(char**));
144
145 av_free(table->uris);
146 table->uris = tmp;
147 table->uris[table->n_uris] = av_malloc(len + 1);
148 if (!table->uris[table->n_uris])
149 return table->n_uris;
150
151 memcpy(table->uris[table->n_uris], uri, len + 1);
152 table->n_uris++;
153
154 return table->n_uris;
155 }
156
uri_table_unmap(LV2_URID_Map_Handle handle,LV2_URID urid)157 static const char *uri_table_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
158 {
159 URITable *table = (URITable*)handle;
160
161 if (urid > 0 && urid <= table->n_uris) {
162 return table->uris[urid - 1];
163 }
164
165 return NULL;
166 }
167
connect_ports(LV2Context * s,AVFrame * in,AVFrame * out)168 static void connect_ports(LV2Context *s, AVFrame *in, AVFrame *out)
169 {
170 int ich = 0, och = 0, i;
171
172 for (i = 0; i < s->nb_ports; i++) {
173 const LilvPort *port = lilv_plugin_get_port_by_index(s->plugin, i);
174
175 if (lilv_port_is_a(s->plugin, port, s->lv2_AudioPort) ||
176 lilv_port_is_a(s->plugin, port, s->lv2_CVPort)) {
177 if (lilv_port_is_a(s->plugin, port, s->lv2_InputPort)) {
178 lilv_instance_connect_port(s->instance, i, in->extended_data[ich++]);
179 } else if (lilv_port_is_a(s->plugin, port, s->lv2_OutputPort)) {
180 lilv_instance_connect_port(s->instance, i, out->extended_data[och++]);
181 } else {
182 av_log(s, AV_LOG_WARNING, "port %d neither input nor output, skipping\n", i);
183 }
184 } else if (lilv_port_is_a(s->plugin, port, s->atom_AtomPort)) {
185 if (lilv_port_is_a(s->plugin, port, s->lv2_InputPort)) {
186 lilv_instance_connect_port(s->instance, i, &s->seq_in);
187 } else {
188 lilv_instance_connect_port(s->instance, i, s->seq_out);
189 }
190 } else if (lilv_port_is_a(s->plugin, port, s->lv2_ControlPort)) {
191 lilv_instance_connect_port(s->instance, i, &s->controls[i]);
192 }
193 }
194
195 s->seq_in[0].atom.size = sizeof(LV2_Atom_Sequence_Body);
196 s->seq_in[0].atom.type = uri_table_map(&s->uri_table, LV2_ATOM__Sequence);
197 s->seq_out->atom.size = 9624;
198 s->seq_out->atom.type = uri_table_map(&s->uri_table, LV2_ATOM__Chunk);
199 }
200
filter_frame(AVFilterLink * inlink,AVFrame * in)201 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
202 {
203 AVFilterContext *ctx = inlink->dst;
204 LV2Context *s = ctx->priv;
205 AVFrame *out;
206
207 if (!s->nb_outputs ||
208 (av_frame_is_writable(in) && s->nb_inputs == s->nb_outputs)) {
209 out = in;
210 } else {
211 out = ff_get_audio_buffer(ctx->outputs[0], in->nb_samples);
212 if (!out) {
213 av_frame_free(&in);
214 return AVERROR(ENOMEM);
215 }
216 av_frame_copy_props(out, in);
217 }
218
219 connect_ports(s, in, out);
220
221 lilv_instance_run(s->instance, in->nb_samples);
222
223 if (out != in)
224 av_frame_free(&in);
225
226 return ff_filter_frame(ctx->outputs[0], out);
227 }
228
request_frame(AVFilterLink * outlink)229 static int request_frame(AVFilterLink *outlink)
230 {
231 AVFilterContext *ctx = outlink->src;
232 LV2Context *s = ctx->priv;
233 AVFrame *out;
234 int64_t t;
235
236 if (ctx->nb_inputs)
237 return ff_request_frame(ctx->inputs[0]);
238
239 t = av_rescale(s->pts, AV_TIME_BASE, s->sample_rate);
240 if (s->duration >= 0 && t >= s->duration)
241 return AVERROR_EOF;
242
243 out = ff_get_audio_buffer(outlink, s->nb_samples);
244 if (!out)
245 return AVERROR(ENOMEM);
246
247 connect_ports(s, out, out);
248
249 lilv_instance_run(s->instance, out->nb_samples);
250
251 out->sample_rate = s->sample_rate;
252 out->pts = s->pts;
253 s->pts += s->nb_samples;
254
255 return ff_filter_frame(outlink, out);
256 }
257
258 static const LV2_Feature buf_size_features[3] = {
259 { LV2_BUF_SIZE__powerOf2BlockLength, NULL },
260 { LV2_BUF_SIZE__fixedBlockLength, NULL },
261 { LV2_BUF_SIZE__boundedBlockLength, NULL },
262 };
263
config_output(AVFilterLink * outlink)264 static int config_output(AVFilterLink *outlink)
265 {
266 AVFilterContext *ctx = outlink->src;
267 LV2Context *s = ctx->priv;
268 char *p, *arg, *saveptr = NULL;
269 int i, sample_rate;
270
271 uri_table_init(&s->uri_table);
272 s->map.handle = &s->uri_table;
273 s->map.map = uri_table_map;
274 s->map_feature.URI = LV2_URID_MAP_URI;
275 s->map_feature.data = &s->map;
276 s->unmap.handle = &s->uri_table;
277 s->unmap.unmap = uri_table_unmap;
278 s->unmap_feature.URI = LV2_URID_UNMAP_URI;
279 s->unmap_feature.data = &s->unmap;
280 s->features[0] = &s->map_feature;
281 s->features[1] = &s->unmap_feature;
282 s->features[2] = &buf_size_features[0];
283 s->features[3] = &buf_size_features[1];
284 s->features[4] = &buf_size_features[2];
285
286 if (ctx->nb_inputs) {
287 AVFilterLink *inlink = ctx->inputs[0];
288
289 outlink->format = inlink->format;
290 outlink->sample_rate = sample_rate = inlink->sample_rate;
291 if (s->nb_inputs == s->nb_outputs) {
292 outlink->channel_layout = inlink->channel_layout;
293 outlink->channels = inlink->channels;
294 }
295
296 } else {
297 outlink->sample_rate = sample_rate = s->sample_rate;
298 outlink->time_base = (AVRational){1, s->sample_rate};
299 }
300
301 s->instance = lilv_plugin_instantiate(s->plugin, sample_rate, s->features);
302 if (!s->instance) {
303 av_log(s, AV_LOG_ERROR, "Failed to instantiate <%s>\n", lilv_node_as_uri(lilv_plugin_get_uri(s->plugin)));
304 return AVERROR(EINVAL);
305 }
306
307 s->mins = av_calloc(s->nb_ports, sizeof(float));
308 s->maxes = av_calloc(s->nb_ports, sizeof(float));
309 s->controls = av_calloc(s->nb_ports, sizeof(float));
310
311 if (!s->mins || !s->maxes || !s->controls)
312 return AVERROR(ENOMEM);
313
314 lilv_plugin_get_port_ranges_float(s->plugin, s->mins, s->maxes, s->controls);
315 s->seq_out = av_malloc(sizeof(LV2_Atom_Sequence) + 9624);
316 if (!s->seq_out)
317 return AVERROR(ENOMEM);
318
319 if (s->options && !strcmp(s->options, "help")) {
320 if (!s->nb_inputcontrols) {
321 av_log(ctx, AV_LOG_INFO,
322 "The '%s' plugin does not have any input controls.\n",
323 s->plugin_uri);
324 } else {
325 av_log(ctx, AV_LOG_INFO,
326 "The '%s' plugin has the following input controls:\n",
327 s->plugin_uri);
328 for (i = 0; i < s->nb_ports; i++) {
329 const LilvPort *port = lilv_plugin_get_port_by_index(s->plugin, i);
330 const LilvNode *symbol = lilv_port_get_symbol(s->plugin, port);
331 LilvNode *name = lilv_port_get_name(s->plugin, port);
332
333 if (lilv_port_is_a(s->plugin, port, s->lv2_InputPort) &&
334 lilv_port_is_a(s->plugin, port, s->lv2_ControlPort)) {
335 av_log(ctx, AV_LOG_INFO, "%s\t\t<float> (from %f to %f) (default %f)\t\t%s\n",
336 lilv_node_as_string(symbol), s->mins[i], s->maxes[i], s->controls[i],
337 lilv_node_as_string(name));
338 }
339
340 lilv_node_free(name);
341 }
342 }
343 return AVERROR_EXIT;
344 }
345
346 p = s->options;
347 while (s->options) {
348 const LilvPort *port;
349 LilvNode *sym;
350 float val;
351 char *str, *vstr;
352 int index;
353
354 if (!(arg = av_strtok(p, " |", &saveptr)))
355 break;
356 p = NULL;
357
358 vstr = strstr(arg, "=");
359 if (vstr == NULL) {
360 av_log(ctx, AV_LOG_ERROR, "Invalid syntax.\n");
361 return AVERROR(EINVAL);
362 }
363
364 vstr[0] = 0;
365 str = arg;
366 val = atof(vstr+1);
367 sym = lilv_new_string(s->world, str);
368 port = lilv_plugin_get_port_by_symbol(s->plugin, sym);
369 lilv_node_free(sym);
370 if (!port) {
371 av_log(s, AV_LOG_WARNING, "Unknown option: <%s>\n", str);
372 } else {
373 index = lilv_port_get_index(s->plugin, port);
374 s->controls[index] = val;
375 }
376 }
377
378 if (s->nb_inputs &&
379 (lilv_plugin_has_feature(s->plugin, s->powerOf2BlockLength) ||
380 lilv_plugin_has_feature(s->plugin, s->fixedBlockLength) ||
381 lilv_plugin_has_feature(s->plugin, s->boundedBlockLength))) {
382 AVFilterLink *inlink = ctx->inputs[0];
383
384 inlink->partial_buf_size = inlink->min_samples = inlink->max_samples = 4096;
385 }
386
387 return 0;
388 }
389
init(AVFilterContext * ctx)390 static av_cold int init(AVFilterContext *ctx)
391 {
392 LV2Context *s = ctx->priv;
393 const LilvPlugins *plugins;
394 const LilvPlugin *plugin;
395 AVFilterPad pad = { NULL };
396 LilvNode *uri;
397 int i;
398
399 s->world = lilv_world_new();
400 if (!s->world)
401 return AVERROR(ENOMEM);
402
403 uri = lilv_new_uri(s->world, s->plugin_uri);
404 if (!uri) {
405 av_log(s, AV_LOG_ERROR, "Invalid plugin URI <%s>\n", s->plugin_uri);
406 return AVERROR(EINVAL);
407 }
408
409 lilv_world_load_all(s->world);
410 plugins = lilv_world_get_all_plugins(s->world);
411 plugin = lilv_plugins_get_by_uri(plugins, uri);
412 lilv_node_free(uri);
413
414 if (!plugin) {
415 av_log(s, AV_LOG_ERROR, "Plugin <%s> not found\n", s->plugin_uri);
416 return AVERROR(EINVAL);
417 }
418
419 s->plugin = plugin;
420 s->nb_ports = lilv_plugin_get_num_ports(s->plugin);
421
422 s->lv2_InputPort = lilv_new_uri(s->world, LV2_CORE__InputPort);
423 s->lv2_OutputPort = lilv_new_uri(s->world, LV2_CORE__OutputPort);
424 s->lv2_AudioPort = lilv_new_uri(s->world, LV2_CORE__AudioPort);
425 s->lv2_ControlPort = lilv_new_uri(s->world, LV2_CORE__ControlPort);
426 s->lv2_Optional = lilv_new_uri(s->world, LV2_CORE__connectionOptional);
427 s->atom_AtomPort = lilv_new_uri(s->world, LV2_ATOM__AtomPort);
428 s->atom_Sequence = lilv_new_uri(s->world, LV2_ATOM__Sequence);
429 s->urid_map = lilv_new_uri(s->world, LV2_URID__map);
430 s->powerOf2BlockLength = lilv_new_uri(s->world, LV2_BUF_SIZE__powerOf2BlockLength);
431 s->fixedBlockLength = lilv_new_uri(s->world, LV2_BUF_SIZE__fixedBlockLength);
432 s->boundedBlockLength = lilv_new_uri(s->world, LV2_BUF_SIZE__boundedBlockLength);
433
434 for (i = 0; i < s->nb_ports; i++) {
435 const LilvPort *lport = lilv_plugin_get_port_by_index(s->plugin, i);
436 int is_input = 0;
437 int is_optional = 0;
438
439 is_optional = lilv_port_has_property(s->plugin, lport, s->lv2_Optional);
440
441 if (lilv_port_is_a(s->plugin, lport, s->lv2_InputPort)) {
442 is_input = 1;
443 } else if (!lilv_port_is_a(s->plugin, lport, s->lv2_OutputPort) && !is_optional) {
444 return AVERROR(EINVAL);
445 }
446
447 if (lilv_port_is_a(s->plugin, lport, s->lv2_ControlPort)) {
448 if (is_input) {
449 s->nb_inputcontrols++;
450 }
451 } else if (lilv_port_is_a(s->plugin, lport, s->lv2_AudioPort)) {
452 if (is_input) {
453 s->nb_inputs++;
454 } else {
455 s->nb_outputs++;
456 }
457 }
458 }
459
460 pad.type = AVMEDIA_TYPE_AUDIO;
461
462 if (s->nb_inputs) {
463 pad.name = av_asprintf("in0:%s:%u", s->plugin_uri, s->nb_inputs);
464 if (!pad.name)
465 return AVERROR(ENOMEM);
466
467 pad.filter_frame = filter_frame;
468 if (ff_insert_inpad(ctx, ctx->nb_inputs, &pad) < 0) {
469 av_freep(&pad.name);
470 return AVERROR(ENOMEM);
471 }
472 }
473
474 return 0;
475 }
476
query_formats(AVFilterContext * ctx)477 static int query_formats(AVFilterContext *ctx)
478 {
479 LV2Context *s = ctx->priv;
480 AVFilterFormats *formats;
481 AVFilterChannelLayouts *layouts;
482 AVFilterLink *outlink = ctx->outputs[0];
483 static const enum AVSampleFormat sample_fmts[] = {
484 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
485 int ret;
486
487 formats = ff_make_format_list(sample_fmts);
488 if (!formats)
489 return AVERROR(ENOMEM);
490 ret = ff_set_common_formats(ctx, formats);
491 if (ret < 0)
492 return ret;
493
494 if (s->nb_inputs) {
495 formats = ff_all_samplerates();
496 if (!formats)
497 return AVERROR(ENOMEM);
498
499 ret = ff_set_common_samplerates(ctx, formats);
500 if (ret < 0)
501 return ret;
502 } else {
503 int sample_rates[] = { s->sample_rate, -1 };
504
505 ret = ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
506 if (ret < 0)
507 return ret;
508 }
509
510 if (s->nb_inputs == 2 && s->nb_outputs == 2) {
511 layouts = NULL;
512 ret = ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO);
513 if (ret < 0)
514 return ret;
515 ret = ff_set_common_channel_layouts(ctx, layouts);
516 if (ret < 0)
517 return ret;
518 } else {
519 if (s->nb_inputs >= 1) {
520 AVFilterLink *inlink = ctx->inputs[0];
521 uint64_t inlayout = FF_COUNT2LAYOUT(s->nb_inputs);
522
523 layouts = NULL;
524 ret = ff_add_channel_layout(&layouts, inlayout);
525 if (ret < 0)
526 return ret;
527 ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
528 if (ret < 0)
529 return ret;
530
531 if (!s->nb_outputs) {
532 ret = ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
533 if (ret < 0)
534 return ret;
535 }
536 }
537
538 if (s->nb_outputs >= 1) {
539 uint64_t outlayout = FF_COUNT2LAYOUT(s->nb_outputs);
540
541 layouts = NULL;
542 ret = ff_add_channel_layout(&layouts, outlayout);
543 if (ret < 0)
544 return ret;
545 ret = ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
546 if (ret < 0)
547 return ret;
548 }
549 }
550
551 return 0;
552 }
553
uninit(AVFilterContext * ctx)554 static av_cold void uninit(AVFilterContext *ctx)
555 {
556 LV2Context *s = ctx->priv;
557
558 lilv_node_free(s->powerOf2BlockLength);
559 lilv_node_free(s->fixedBlockLength);
560 lilv_node_free(s->boundedBlockLength);
561 lilv_node_free(s->urid_map);
562 lilv_node_free(s->atom_Sequence);
563 lilv_node_free(s->atom_AtomPort);
564 lilv_node_free(s->lv2_Optional);
565 lilv_node_free(s->lv2_ControlPort);
566 lilv_node_free(s->lv2_AudioPort);
567 lilv_node_free(s->lv2_OutputPort);
568 lilv_node_free(s->lv2_InputPort);
569 uri_table_destroy(&s->uri_table);
570 lilv_instance_free(s->instance);
571 lilv_world_free(s->world);
572 av_freep(&s->mins);
573 av_freep(&s->maxes);
574 av_freep(&s->controls);
575 av_freep(&s->seq_out);
576
577 if (ctx->nb_inputs)
578 av_freep(&ctx->input_pads[0].name);
579 }
580
581 static const AVFilterPad lv2_outputs[] = {
582 {
583 .name = "default",
584 .type = AVMEDIA_TYPE_AUDIO,
585 .config_props = config_output,
586 .request_frame = request_frame,
587 },
588 { NULL }
589 };
590
591 AVFilter ff_af_lv2 = {
592 .name = "lv2",
593 .description = NULL_IF_CONFIG_SMALL("Apply LV2 effect."),
594 .priv_size = sizeof(LV2Context),
595 .priv_class = &lv2_class,
596 .init = init,
597 .uninit = uninit,
598 .query_formats = query_formats,
599 .inputs = 0,
600 .outputs = lv2_outputs,
601 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
602 };
603