2 * Copyright (c) 2017 Paul B Mahol
3 * Copyright (c) 2007-2016 David Robillard <http://drobilla.net>
5 * This file is part of FFmpeg.
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.
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.
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
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>
31 #include "libavutil/avassert.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/channel_layout.h"
34 #include "libavutil/opt.h"
39 typedef struct URITable {
44 typedef struct LV2Context {
50 unsigned nb_inputcontrols;
59 const LilvPlugin *plugin;
64 LV2_Feature map_feature;
66 LV2_Feature unmap_feature;
67 LV2_Atom_Sequence seq_in[2];
68 LV2_Atom_Sequence *seq_out;
69 const LV2_Feature *features[5];
75 LilvInstance *instance;
77 LilvNode *atom_AtomPort;
78 LilvNode *atom_Sequence;
79 LilvNode *lv2_AudioPort;
81 LilvNode *lv2_ControlPort;
82 LilvNode *lv2_Optional;
83 LilvNode *lv2_InputPort;
84 LilvNode *lv2_OutputPort;
86 LilvNode *powerOf2BlockLength;
87 LilvNode *fixedBlockLength;
88 LilvNode *boundedBlockLength;
91 #define OFFSET(x) offsetof(LV2Context, x)
92 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
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 },
108 AVFILTER_DEFINE_CLASS(lv2);
110 static void uri_table_init(URITable *table)
116 static void uri_table_destroy(URITable *table)
120 for (i = 0; i < table->n_uris; i++) {
121 av_freep(&table->uris[i]);
124 av_freep(&table->uris);
127 static LV2_URID uri_table_map(LV2_URID_Map_Handle handle, const char *uri)
129 URITable *table = (URITable*)handle;
130 const size_t len = strlen(uri);
134 for (i = 0; i < table->n_uris; i++) {
135 if (!strcmp(table->uris[i], uri)) {
140 tmp = av_calloc(table->n_uris + 1, sizeof(char*));
142 return table->n_uris;
143 memcpy(tmp, table->uris, table->n_uris * sizeof(char**));
145 av_free(table->uris);
147 table->uris[table->n_uris] = av_malloc(len + 1);
148 if (!table->uris[table->n_uris])
149 return table->n_uris;
151 memcpy(table->uris[table->n_uris], uri, len + 1);
154 return table->n_uris;
157 static const char *uri_table_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
159 URITable *table = (URITable*)handle;
161 if (urid > 0 && urid <= table->n_uris) {
162 return table->uris[urid - 1];
168 static void connect_ports(LV2Context *s, AVFrame *in, AVFrame *out)
170 int ich = 0, och = 0, i;
172 for (i = 0; i < s->nb_ports; i++) {
173 const LilvPort *port = lilv_plugin_get_port_by_index(s->plugin, i);
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++]);
182 av_log(s, AV_LOG_WARNING, "port %d neither input nor output, skipping\n", i);
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);
188 lilv_instance_connect_port(s->instance, i, s->seq_out);
190 } else if (lilv_port_is_a(s->plugin, port, s->lv2_ControlPort)) {
191 lilv_instance_connect_port(s->instance, i, &s->controls[i]);
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);
201 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
203 AVFilterContext *ctx = inlink->dst;
204 LV2Context *s = ctx->priv;
207 if (!s->nb_outputs ||
208 (av_frame_is_writable(in) && s->nb_inputs == s->nb_outputs)) {
211 out = ff_get_audio_buffer(ctx->outputs[0], in->nb_samples);
214 return AVERROR(ENOMEM);
216 av_frame_copy_props(out, in);
219 connect_ports(s, in, out);
221 lilv_instance_run(s->instance, in->nb_samples);
226 return ff_filter_frame(ctx->outputs[0], out);
229 static int request_frame(AVFilterLink *outlink)
231 AVFilterContext *ctx = outlink->src;
232 LV2Context *s = ctx->priv;
237 return ff_request_frame(ctx->inputs[0]);
239 t = av_rescale(s->pts, AV_TIME_BASE, s->sample_rate);
240 if (s->duration >= 0 && t >= s->duration)
243 out = ff_get_audio_buffer(outlink, s->nb_samples);
245 return AVERROR(ENOMEM);
247 connect_ports(s, out, out);
249 lilv_instance_run(s->instance, out->nb_samples);
251 out->sample_rate = s->sample_rate;
253 s->pts += s->nb_samples;
255 return ff_filter_frame(outlink, out);
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 },
264 static int config_output(AVFilterLink *outlink)
266 AVFilterContext *ctx = outlink->src;
267 LV2Context *s = ctx->priv;
268 char *p, *arg, *saveptr = NULL;
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];
286 if (ctx->nb_inputs) {
287 AVFilterLink *inlink = ctx->inputs[0];
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;
297 outlink->sample_rate = sample_rate = s->sample_rate;
298 outlink->time_base = (AVRational){1, s->sample_rate};
301 s->instance = lilv_plugin_instantiate(s->plugin, sample_rate, s->features);
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);
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));
311 if (!s->mins || !s->maxes || !s->controls)
312 return AVERROR(ENOMEM);
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);
317 return AVERROR(ENOMEM);
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",
325 av_log(ctx, AV_LOG_INFO,
326 "The '%s' plugin has the following input controls:\n",
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);
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));
340 lilv_node_free(name);
348 const LilvPort *port;
354 if (!(arg = av_strtok(p, " |", &saveptr)))
358 vstr = strstr(arg, "=");
360 av_log(ctx, AV_LOG_ERROR, "Invalid syntax.\n");
361 return AVERROR(EINVAL);
367 sym = lilv_new_string(s->world, str);
368 port = lilv_plugin_get_port_by_symbol(s->plugin, sym);
371 av_log(s, AV_LOG_WARNING, "Unknown option: <%s>\n", str);
373 index = lilv_port_get_index(s->plugin, port);
374 s->controls[index] = val;
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];
384 inlink->partial_buf_size = inlink->min_samples = inlink->max_samples = 4096;
390 static av_cold int init(AVFilterContext *ctx)
392 LV2Context *s = ctx->priv;
393 const LilvPlugins *plugins;
394 const LilvPlugin *plugin;
395 AVFilterPad pad = { NULL };
399 s->world = lilv_world_new();
401 return AVERROR(ENOMEM);
403 uri = lilv_new_uri(s->world, s->plugin_uri);
405 av_log(s, AV_LOG_ERROR, "Invalid plugin URI <%s>\n", s->plugin_uri);
406 return AVERROR(EINVAL);
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);
415 av_log(s, AV_LOG_ERROR, "Plugin <%s> not found\n", s->plugin_uri);
416 return AVERROR(EINVAL);
420 s->nb_ports = lilv_plugin_get_num_ports(s->plugin);
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);
434 for (i = 0; i < s->nb_ports; i++) {
435 const LilvPort *lport = lilv_plugin_get_port_by_index(s->plugin, i);
439 is_optional = lilv_port_has_property(s->plugin, lport, s->lv2_Optional);
441 if (lilv_port_is_a(s->plugin, lport, s->lv2_InputPort)) {
443 } else if (!lilv_port_is_a(s->plugin, lport, s->lv2_OutputPort) && !is_optional) {
444 return AVERROR(EINVAL);
447 if (lilv_port_is_a(s->plugin, lport, s->lv2_ControlPort)) {
449 s->nb_inputcontrols++;
451 } else if (lilv_port_is_a(s->plugin, lport, s->lv2_AudioPort)) {
460 pad.type = AVMEDIA_TYPE_AUDIO;
463 pad.name = av_asprintf("in0:%s:%u", s->plugin_uri, s->nb_inputs);
465 return AVERROR(ENOMEM);
467 pad.filter_frame = filter_frame;
468 if (ff_insert_inpad(ctx, ctx->nb_inputs, &pad) < 0) {
470 return AVERROR(ENOMEM);
477 static int query_formats(AVFilterContext *ctx)
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 };
487 formats = ff_make_format_list(sample_fmts);
489 return AVERROR(ENOMEM);
490 ret = ff_set_common_formats(ctx, formats);
495 formats = ff_all_samplerates();
497 return AVERROR(ENOMEM);
499 ret = ff_set_common_samplerates(ctx, formats);
503 int sample_rates[] = { s->sample_rate, -1 };
505 ret = ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
510 if (s->nb_inputs == 2 && s->nb_outputs == 2) {
512 ret = ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO);
515 ret = ff_set_common_channel_layouts(ctx, layouts);
519 if (s->nb_inputs >= 1) {
520 AVFilterLink *inlink = ctx->inputs[0];
521 uint64_t inlayout = FF_COUNT2LAYOUT(s->nb_inputs);
524 ret = ff_add_channel_layout(&layouts, inlayout);
527 ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts);
531 if (!s->nb_outputs) {
532 ret = ff_channel_layouts_ref(layouts, &outlink->incfg.channel_layouts);
538 if (s->nb_outputs >= 1) {
539 uint64_t outlayout = FF_COUNT2LAYOUT(s->nb_outputs);
542 ret = ff_add_channel_layout(&layouts, outlayout);
545 ret = ff_channel_layouts_ref(layouts, &outlink->incfg.channel_layouts);
554 static av_cold void uninit(AVFilterContext *ctx)
556 LV2Context *s = ctx->priv;
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);
574 av_freep(&s->controls);
575 av_freep(&s->seq_out);
578 av_freep(&ctx->input_pads[0].name);
581 static const AVFilterPad lv2_outputs[] = {
584 .type = AVMEDIA_TYPE_AUDIO,
585 .config_props = config_output,
586 .request_frame = request_frame,
591 const AVFilter ff_af_lv2 = {
593 .description = NULL_IF_CONFIG_SMALL("Apply LV2 effect."),
594 .priv_size = sizeof(LV2Context),
595 .priv_class = &lv2_class,
598 .query_formats = query_formats,
600 .outputs = lv2_outputs,
601 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,