Browse code

avfilter: add lv2 wrapper filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2017/11/24 00:08:42
Showing 7 changed files
... ...
@@ -20,6 +20,7 @@ version <next>:
20 20
 - OpenCL overlay filter
21 21
 - video mix filter
22 22
 - video normalize filter
23
+- audio lv2 wrapper filter
23 24
 
24 25
 
25 26
 version 3.4:
... ...
@@ -283,6 +283,7 @@ External library support:
283 283
   --enable-libzimg         enable z.lib, needed for zscale filter [no]
284 284
   --enable-libzmq          enable message passing via libzmq [no]
285 285
   --enable-libzvbi         enable teletext support via libzvbi [no]
286
+  --enable-lv2             enable LV2 audio filtering [no]
286 287
   --disable-lzma           disable lzma [autodetect]
287 288
   --enable-decklink        enable Blackmagic DeckLink I/O support [no]
288 289
   --enable-libndi_newtek   enable Newteck NDI I/O support [no]
... ...
@@ -1631,6 +1632,7 @@ EXTERNAL_LIBRARY_LIST="
1631 1631
     libzimg
1632 1632
     libzmq
1633 1633
     libzvbi
1634
+    lv2
1634 1635
     mediacodec
1635 1636
     openal
1636 1637
     opengl
... ...
@@ -3229,6 +3231,7 @@ hqdn3d_filter_deps="gpl"
3229 3229
 interlace_filter_deps="gpl"
3230 3230
 kerndeint_filter_deps="gpl"
3231 3231
 ladspa_filter_deps="ladspa libdl"
3232
+lv2_filter_deps="lv2"
3232 3233
 mcdeint_filter_deps="avcodec gpl"
3233 3234
 movie_filter_deps="avcodec avformat"
3234 3235
 mpdecimate_filter_deps="gpl"
... ...
@@ -5825,6 +5828,7 @@ enabled gmp               && require gmp gmp.h mpz_export -lgmp
5825 5825
 enabled gnutls            && require_pkg_config gnutls gnutls gnutls/gnutls.h gnutls_global_init
5826 5826
 enabled jni               && { [ $target_os = "android" ] && check_header jni.h && enabled pthreads || die "ERROR: jni not found"; }
5827 5827
 enabled ladspa            && require_header ladspa.h
5828
+enabled lv2               && require_pkg_config lv2 lilv-0 "lilv-0/lilv/lilv.h" lilv_world_new
5828 5829
 enabled libiec61883       && require libiec61883 libiec61883/iec61883.h iec61883_cmp_connect -lraw1394 -lavc1394 -lrom1394 -liec61883
5829 5830
 enabled libass            && require_pkg_config libass libass ass/ass.h ass_library_init
5830 5831
 enabled libbluray         && require_pkg_config libbluray libbluray libbluray/bluray.h bd_open
... ...
@@ -3281,6 +3281,65 @@ lowpass=c=LFE
3281 3281
 @end example
3282 3282
 @end itemize
3283 3283
 
3284
+@section lv2
3285
+
3286
+Load a LV2 (LADSPA Version 2) plugin.
3287
+
3288
+To enable compilation of this filter you need to configure FFmpeg with
3289
+@code{--enable-lv2}.
3290
+
3291
+@table @option
3292
+@item plugin, p
3293
+Specifies the plugin URI. You may need to escape ':'.
3294
+
3295
+@item controls, c
3296
+Set the '|' separated list of controls which are zero or more floating point
3297
+values that determine the behavior of the loaded plugin (for example delay,
3298
+threshold or gain).
3299
+If @option{controls} is set to @code{help}, all available controls and
3300
+their valid ranges are printed.
3301
+
3302
+@item sample_rate, s
3303
+Specify the sample rate, default to 44100. Only used if plugin have
3304
+zero inputs.
3305
+
3306
+@item nb_samples, n
3307
+Set the number of samples per channel per each output frame, default
3308
+is 1024. Only used if plugin have zero inputs.
3309
+
3310
+@item duration, d
3311
+Set the minimum duration of the sourced audio. See
3312
+@ref{time duration syntax,,the Time duration section in the ffmpeg-utils(1) manual,ffmpeg-utils}
3313
+for the accepted syntax.
3314
+Note that the resulting duration may be greater than the specified duration,
3315
+as the generated audio is always cut at the end of a complete frame.
3316
+If not specified, or the expressed duration is negative, the audio is
3317
+supposed to be generated forever.
3318
+Only used if plugin have zero inputs.
3319
+@end table
3320
+
3321
+@subsection Examples
3322
+
3323
+@itemize
3324
+@item
3325
+Apply bass enhancer plugin from Calf:
3326
+@example
3327
+lv2=p=http\\\\://calf.sourceforge.net/plugins/BassEnhancer:c=amount=2
3328
+@end example
3329
+
3330
+@item
3331
+Apply bass vinyl plugin from Calf:
3332
+@example
3333
+lv2=p=http\\\\://calf.sourceforge.net/plugins/Vinyl:c=drone=0.2|aging=0.5
3334
+@end example
3335
+
3336
+@item
3337
+Apply bit crusher plugin from ArtyFX:
3338
+@example
3339
+lv2=p=http\\\\://www.openavproductions.com/artyfx#bitta:c=crush=0.3
3340
+@end example
3341
+@end itemize
3342
+
3284 3343
 @section mcompand
3285 3344
 Multiband Compress or expand the audio's dynamic range.
3286 3345
 
... ...
@@ -101,6 +101,7 @@ OBJS-$(CONFIG_JOIN_FILTER)                   += af_join.o
101 101
 OBJS-$(CONFIG_LADSPA_FILTER)                 += af_ladspa.o
102 102
 OBJS-$(CONFIG_LOUDNORM_FILTER)               += af_loudnorm.o ebur128.o
103 103
 OBJS-$(CONFIG_LOWPASS_FILTER)                += af_biquads.o
104
+OBJS-$(CONFIG_LV2_FILTER)                    += af_lv2.o
104 105
 OBJS-$(CONFIG_MCOMPAND_FILTER)               += af_mcompand.o
105 106
 OBJS-$(CONFIG_PAN_FILTER)                    += af_pan.o
106 107
 OBJS-$(CONFIG_REPLAYGAIN_FILTER)             += af_replaygain.o
107 108
new file mode 100644
... ...
@@ -0,0 +1,602 @@
0
+/*
1
+ * Copyright (c) 2017 Paul B Mahol
2
+ * Copyright (c) 2007-2016 David Robillard <http://drobilla.net>
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * LV2 wrapper
24
+ */
25
+
26
+#include <lilv/lilv.h>
27
+#include <lv2/lv2plug.in/ns/ext/atom/atom.h>
28
+#include <lv2/lv2plug.in/ns/ext/buf-size/buf-size.h>
29
+
30
+#include "libavutil/avassert.h"
31
+#include "libavutil/avstring.h"
32
+#include "libavutil/channel_layout.h"
33
+#include "libavutil/opt.h"
34
+#include "audio.h"
35
+#include "avfilter.h"
36
+#include "internal.h"
37
+
38
+typedef struct URITable {
39
+    char    **uris;
40
+    size_t    n_uris;
41
+} URITable;
42
+
43
+typedef struct LV2Context {
44
+    const AVClass *class;
45
+    char *plugin_uri;
46
+    char *options;
47
+
48
+    unsigned nb_inputs;
49
+    unsigned nb_inputcontrols;
50
+    unsigned nb_outputs;
51
+
52
+    int sample_rate;
53
+    int nb_samples;
54
+    int64_t pts;
55
+    int64_t duration;
56
+
57
+    LilvWorld         *world;
58
+    const LilvPlugin  *plugin;
59
+    uint32_t           nb_ports;
60
+    float             *values;
61
+    URITable           uri_table;
62
+    LV2_URID_Map       map;
63
+    LV2_Feature        map_feature;
64
+    LV2_URID_Unmap     unmap;
65
+    LV2_Feature        unmap_feature;
66
+    LV2_Atom_Sequence  seq_in[2];
67
+    LV2_Atom_Sequence *seq_out;
68
+    const LV2_Feature *features[5];
69
+
70
+    float *mins;
71
+    float *maxes;
72
+    float *controls;
73
+
74
+    LilvInstance *instance;
75
+
76
+    LilvNode  *atom_AtomPort;
77
+    LilvNode  *atom_Sequence;
78
+    LilvNode  *lv2_AudioPort;
79
+    LilvNode  *lv2_CVPort;
80
+    LilvNode  *lv2_ControlPort;
81
+    LilvNode  *lv2_Optional;
82
+    LilvNode  *lv2_InputPort;
83
+    LilvNode  *lv2_OutputPort;
84
+    LilvNode  *urid_map;
85
+    LilvNode  *powerOf2BlockLength;
86
+    LilvNode  *fixedBlockLength;
87
+    LilvNode  *boundedBlockLength;
88
+} LV2Context;
89
+
90
+#define OFFSET(x) offsetof(LV2Context, x)
91
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
92
+
93
+static const AVOption lv2_options[] = {
94
+    { "plugin", "set plugin uri", OFFSET(plugin_uri), AV_OPT_TYPE_STRING, .flags = FLAGS },
95
+    { "p",      "set plugin uri", OFFSET(plugin_uri), AV_OPT_TYPE_STRING, .flags = FLAGS },
96
+    { "controls", "set plugin options", OFFSET(options), AV_OPT_TYPE_STRING, .flags = FLAGS },
97
+    { "c",        "set plugin options", OFFSET(options), AV_OPT_TYPE_STRING, .flags = FLAGS },
98
+    { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT32_MAX, FLAGS },
99
+    { "s",           "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT32_MAX, FLAGS },
100
+    { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
101
+    { "n",          "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
102
+    { "duration", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },
103
+    { "d",        "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },
104
+    { NULL }
105
+};
106
+
107
+AVFILTER_DEFINE_CLASS(lv2);
108
+
109
+static void uri_table_init(URITable *table)
110
+{
111
+    table->uris   = NULL;
112
+    table->n_uris = 0;
113
+}
114
+
115
+static void uri_table_destroy(URITable *table)
116
+{
117
+    int i;
118
+
119
+    for (i = 0; i < table->n_uris; i++) {
120
+        av_freep(&table->uris[i]);
121
+    }
122
+
123
+    av_freep(&table->uris);
124
+}
125
+
126
+static LV2_URID uri_table_map(LV2_URID_Map_Handle handle, const char *uri)
127
+{
128
+    URITable *table = (URITable*)handle;
129
+    const size_t len = strlen(uri);
130
+    size_t i;
131
+    char **tmp;
132
+
133
+    for (i = 0; i < table->n_uris; i++) {
134
+        if (!strcmp(table->uris[i], uri)) {
135
+            return i + 1;
136
+        }
137
+    }
138
+
139
+    tmp = av_calloc(table->n_uris + 1, sizeof(char*));
140
+    if (!tmp)
141
+        return table->n_uris;
142
+    memcpy(tmp, table->uris, table->n_uris * sizeof(char**));
143
+
144
+    av_free(table->uris);
145
+    table->uris = tmp;
146
+    table->uris[table->n_uris] = av_malloc(len + 1);
147
+    if (!table->uris[table->n_uris])
148
+        return table->n_uris;
149
+
150
+    memcpy(table->uris[table->n_uris], uri, len + 1);
151
+    table->n_uris++;
152
+
153
+    return table->n_uris;
154
+}
155
+
156
+static const char *uri_table_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
157
+{
158
+    URITable *table = (URITable*)handle;
159
+
160
+    if (urid > 0 && urid <= table->n_uris) {
161
+        return table->uris[urid - 1];
162
+    }
163
+
164
+    return NULL;
165
+}
166
+
167
+static void connect_ports(LV2Context *s, AVFrame *in, AVFrame *out)
168
+{
169
+    int ich = 0, och = 0, i;
170
+
171
+    for (i = 0; i < s->nb_ports; i++) {
172
+        const LilvPort *port = lilv_plugin_get_port_by_index(s->plugin, i);
173
+
174
+        if (lilv_port_is_a(s->plugin, port, s->lv2_AudioPort) ||
175
+            lilv_port_is_a(s->plugin, port, s->lv2_CVPort)) {
176
+            if (lilv_port_is_a(s->plugin, port, s->lv2_InputPort)) {
177
+                lilv_instance_connect_port(s->instance, i, in->extended_data[ich++]);
178
+            } else if (lilv_port_is_a(s->plugin, port, s->lv2_OutputPort)) {
179
+                lilv_instance_connect_port(s->instance, i, out->extended_data[och++]);
180
+            } else {
181
+                av_log(s, AV_LOG_WARNING, "port %d neither input nor output, skipping\n", i);
182
+            }
183
+        } else if (lilv_port_is_a(s->plugin, port, s->atom_AtomPort)) {
184
+            if (lilv_port_is_a(s->plugin, port, s->lv2_InputPort)) {
185
+                lilv_instance_connect_port(s->instance, i, &s->seq_in);
186
+            } else {
187
+                lilv_instance_connect_port(s->instance, i, s->seq_out);
188
+            }
189
+        } else if (lilv_port_is_a(s->plugin, port, s->lv2_ControlPort)) {
190
+            lilv_instance_connect_port(s->instance, i, &s->controls[i]);
191
+        }
192
+    }
193
+
194
+    s->seq_in[0].atom.size = sizeof(LV2_Atom_Sequence_Body);
195
+    s->seq_in[0].atom.type = uri_table_map(&s->uri_table, LV2_ATOM__Sequence);
196
+    s->seq_out->atom.size  = 9624;
197
+    s->seq_out->atom.type  = uri_table_map(&s->uri_table, LV2_ATOM__Chunk);
198
+}
199
+
200
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
201
+{
202
+    AVFilterContext *ctx = inlink->dst;
203
+    LV2Context *s = ctx->priv;
204
+    AVFrame *out;
205
+
206
+    if (!s->nb_outputs ||
207
+        (av_frame_is_writable(in) && s->nb_inputs == s->nb_outputs)) {
208
+        out = in;
209
+    } else {
210
+        out = ff_get_audio_buffer(ctx->outputs[0], in->nb_samples);
211
+        if (!out) {
212
+            av_frame_free(&in);
213
+            return AVERROR(ENOMEM);
214
+        }
215
+        av_frame_copy_props(out, in);
216
+    }
217
+
218
+    connect_ports(s, in, out);
219
+
220
+    lilv_instance_run(s->instance, in->nb_samples);
221
+
222
+    if (out != in)
223
+        av_frame_free(&in);
224
+
225
+    return ff_filter_frame(ctx->outputs[0], out);
226
+}
227
+
228
+static int request_frame(AVFilterLink *outlink)
229
+{
230
+    AVFilterContext *ctx = outlink->src;
231
+    LV2Context *s = ctx->priv;
232
+    AVFrame *out;
233
+    int64_t t;
234
+
235
+    if (ctx->nb_inputs)
236
+        return ff_request_frame(ctx->inputs[0]);
237
+
238
+    t = av_rescale(s->pts, AV_TIME_BASE, s->sample_rate);
239
+    if (s->duration >= 0 && t >= s->duration)
240
+        return AVERROR_EOF;
241
+
242
+    out = ff_get_audio_buffer(outlink, s->nb_samples);
243
+    if (!out)
244
+        return AVERROR(ENOMEM);
245
+
246
+    connect_ports(s, out, out);
247
+
248
+    lilv_instance_run(s->instance, out->nb_samples);
249
+
250
+    out->sample_rate = s->sample_rate;
251
+    out->pts         = s->pts;
252
+    s->pts          += s->nb_samples;
253
+
254
+    return ff_filter_frame(outlink, out);
255
+}
256
+
257
+static const LV2_Feature buf_size_features[3] = {
258
+    { LV2_BUF_SIZE__powerOf2BlockLength, NULL },
259
+    { LV2_BUF_SIZE__fixedBlockLength,    NULL },
260
+    { LV2_BUF_SIZE__boundedBlockLength,  NULL },
261
+};
262
+
263
+static int config_output(AVFilterLink *outlink)
264
+{
265
+    AVFilterContext *ctx = outlink->src;
266
+    LV2Context *s = ctx->priv;
267
+    char *p, *arg, *saveptr = NULL;
268
+    int i, sample_rate;
269
+
270
+    uri_table_init(&s->uri_table);
271
+    s->map.handle = &s->uri_table;
272
+    s->map.map = uri_table_map;
273
+    s->map_feature.URI = LV2_URID_MAP_URI;
274
+    s->map_feature.data = &s->map;
275
+    s->unmap.handle = &s->uri_table;
276
+    s->unmap.unmap  = uri_table_unmap;
277
+    s->unmap_feature.URI = LV2_URID_UNMAP_URI;
278
+    s->unmap_feature.data = &s->unmap;
279
+    s->features[0] = &s->map_feature;
280
+    s->features[1] = &s->unmap_feature;
281
+    s->features[2] = &buf_size_features[0];
282
+    s->features[3] = &buf_size_features[1];
283
+    s->features[4] = &buf_size_features[2];
284
+
285
+    if (ctx->nb_inputs) {
286
+        AVFilterLink *inlink = ctx->inputs[0];
287
+
288
+        outlink->format      = inlink->format;
289
+        outlink->sample_rate = sample_rate = inlink->sample_rate;
290
+        if (s->nb_inputs == s->nb_outputs) {
291
+            outlink->channel_layout = inlink->channel_layout;
292
+            outlink->channels = inlink->channels;
293
+        }
294
+
295
+    } else {
296
+        outlink->sample_rate = sample_rate = s->sample_rate;
297
+        outlink->time_base   = (AVRational){1, s->sample_rate};
298
+    }
299
+
300
+    s->instance = lilv_plugin_instantiate(s->plugin, sample_rate, s->features);
301
+    if (!s->instance) {
302
+        av_log(s, AV_LOG_ERROR, "Failed to instantiate <%s>\n", lilv_node_as_uri(lilv_plugin_get_uri(s->plugin)));
303
+        return AVERROR(EINVAL);
304
+    }
305
+
306
+    s->mins     = av_calloc(s->nb_ports, sizeof(float));
307
+    s->maxes    = av_calloc(s->nb_ports, sizeof(float));
308
+    s->controls = av_calloc(s->nb_ports, sizeof(float));
309
+
310
+    if (!s->mins || !s->maxes || !s->controls)
311
+        return AVERROR(ENOMEM);
312
+
313
+    lilv_plugin_get_port_ranges_float(s->plugin, s->mins, s->maxes, s->controls);
314
+    s->seq_out = av_malloc(sizeof(LV2_Atom_Sequence) + 9624);
315
+    if (!s->seq_out)
316
+        return AVERROR(ENOMEM);
317
+
318
+    if (s->options && !strcmp(s->options, "help")) {
319
+        if (!s->nb_inputcontrols) {
320
+            av_log(ctx, AV_LOG_INFO,
321
+                   "The '%s' plugin does not have any input controls.\n",
322
+                   s->plugin_uri);
323
+        } else {
324
+            av_log(ctx, AV_LOG_INFO,
325
+                   "The '%s' plugin has the following input controls:\n",
326
+                   s->plugin_uri);
327
+            for (i = 0; i < s->nb_ports; i++) {
328
+                const LilvPort *port = lilv_plugin_get_port_by_index(s->plugin, i);
329
+                const LilvNode *symbol = lilv_port_get_symbol(s->plugin, port);
330
+                LilvNode *name = lilv_port_get_name(s->plugin, port);
331
+
332
+                if (lilv_port_is_a(s->plugin, port, s->lv2_InputPort) &&
333
+                    lilv_port_is_a(s->plugin, port, s->lv2_ControlPort)) {
334
+                    av_log(ctx, AV_LOG_INFO, "%s\t\t<float> (from %f to %f) (default %f)\t\t%s\n",
335
+                           lilv_node_as_string(symbol), s->mins[i], s->maxes[i], s->controls[i],
336
+                           lilv_node_as_string(name));
337
+                }
338
+
339
+                lilv_node_free(name);
340
+            }
341
+        }
342
+        return AVERROR_EXIT;
343
+    }
344
+
345
+    p = s->options;
346
+    while (s->options) {
347
+        const LilvPort *port;
348
+        LilvNode *sym;
349
+        float val;
350
+        char *str, *vstr;
351
+        int index;
352
+
353
+        if (!(arg = av_strtok(p, " |", &saveptr)))
354
+            break;
355
+        p = NULL;
356
+
357
+        vstr = strstr(arg, "=");
358
+        if (vstr == NULL) {
359
+            av_log(ctx, AV_LOG_ERROR, "Invalid syntax.\n");
360
+            return AVERROR(EINVAL);
361
+        }
362
+
363
+        vstr[0] = 0;
364
+        str  = arg;
365
+        val  = atof(vstr+1);
366
+        sym  = lilv_new_string(s->world, str);
367
+        port = lilv_plugin_get_port_by_symbol(s->plugin, sym);
368
+        lilv_node_free(sym);
369
+        if (!port) {
370
+            av_log(s, AV_LOG_WARNING, "Unknown option: <%s>\n", str);
371
+        } else {
372
+            index = lilv_port_get_index(s->plugin, port);
373
+            s->controls[index] = val;
374
+        }
375
+    }
376
+
377
+    if (s->nb_inputs &&
378
+        (lilv_plugin_has_feature(s->plugin, s->powerOf2BlockLength) ||
379
+         lilv_plugin_has_feature(s->plugin, s->fixedBlockLength) ||
380
+         lilv_plugin_has_feature(s->plugin, s->boundedBlockLength))) {
381
+        AVFilterLink *inlink = ctx->inputs[0];
382
+
383
+        inlink->partial_buf_size = inlink->min_samples = inlink->max_samples = 4096;
384
+    }
385
+
386
+    return 0;
387
+}
388
+
389
+static av_cold int init(AVFilterContext *ctx)
390
+{
391
+    LV2Context *s = ctx->priv;
392
+    const LilvPlugins *plugins;
393
+    const LilvPlugin *plugin;
394
+    AVFilterPad pad = { NULL };
395
+    LilvNode *uri;
396
+    int i;
397
+
398
+    s->world = lilv_world_new();
399
+    if (!s->world)
400
+        return AVERROR(ENOMEM);
401
+
402
+    uri = lilv_new_uri(s->world, s->plugin_uri);
403
+    if (!uri) {
404
+        av_log(s, AV_LOG_ERROR, "Invalid plugin URI <%s>\n", s->plugin_uri);
405
+        return AVERROR(EINVAL);
406
+    }
407
+
408
+    lilv_world_load_all(s->world);
409
+    plugins = lilv_world_get_all_plugins(s->world);
410
+    plugin  = lilv_plugins_get_by_uri(plugins, uri);
411
+    lilv_node_free(uri);
412
+
413
+    if (!plugin) {
414
+        av_log(s, AV_LOG_ERROR, "Plugin <%s> not found\n", s->plugin_uri);
415
+        return AVERROR(EINVAL);
416
+    }
417
+
418
+    s->plugin = plugin;
419
+    s->nb_ports = lilv_plugin_get_num_ports(s->plugin);
420
+
421
+    s->lv2_InputPort       = lilv_new_uri(s->world, LV2_CORE__InputPort);
422
+    s->lv2_OutputPort      = lilv_new_uri(s->world, LV2_CORE__OutputPort);
423
+    s->lv2_AudioPort       = lilv_new_uri(s->world, LV2_CORE__AudioPort);
424
+    s->lv2_ControlPort     = lilv_new_uri(s->world, LV2_CORE__ControlPort);
425
+    s->lv2_Optional        = lilv_new_uri(s->world, LV2_CORE__connectionOptional);
426
+    s->atom_AtomPort       = lilv_new_uri(s->world, LV2_ATOM__AtomPort);
427
+    s->atom_Sequence       = lilv_new_uri(s->world, LV2_ATOM__Sequence);
428
+    s->urid_map            = lilv_new_uri(s->world, LV2_URID__map);
429
+    s->powerOf2BlockLength = lilv_new_uri(s->world, LV2_BUF_SIZE__powerOf2BlockLength);
430
+    s->fixedBlockLength    = lilv_new_uri(s->world, LV2_BUF_SIZE__fixedBlockLength);
431
+    s->boundedBlockLength  = lilv_new_uri(s->world, LV2_BUF_SIZE__boundedBlockLength);
432
+
433
+    for (i = 0; i < s->nb_ports; i++) {
434
+        const LilvPort *lport = lilv_plugin_get_port_by_index(s->plugin, i);
435
+        int is_input = 0;
436
+        int is_optional = 0;
437
+
438
+        is_optional = lilv_port_has_property(s->plugin, lport, s->lv2_Optional);
439
+
440
+        if (lilv_port_is_a(s->plugin, lport, s->lv2_InputPort)) {
441
+            is_input = 1;
442
+        } else if (!lilv_port_is_a(s->plugin, lport, s->lv2_OutputPort) && !is_optional) {
443
+            return AVERROR(EINVAL);
444
+        }
445
+
446
+        if (lilv_port_is_a(s->plugin, lport, s->lv2_ControlPort)) {
447
+            if (is_input) {
448
+                s->nb_inputcontrols++;
449
+            }
450
+        } else if (lilv_port_is_a(s->plugin, lport, s->lv2_AudioPort)) {
451
+            if (is_input) {
452
+                s->nb_inputs++;
453
+            } else {
454
+                s->nb_outputs++;
455
+            }
456
+        }
457
+    }
458
+
459
+    pad.type = AVMEDIA_TYPE_AUDIO;
460
+
461
+    if (s->nb_inputs) {
462
+        pad.name = av_asprintf("in0:%s:%u", s->plugin_uri, s->nb_inputs);
463
+        if (!pad.name)
464
+            return AVERROR(ENOMEM);
465
+
466
+        pad.filter_frame = filter_frame;
467
+        if (ff_insert_inpad(ctx, ctx->nb_inputs, &pad) < 0) {
468
+            av_freep(&pad.name);
469
+            return AVERROR(ENOMEM);
470
+        }
471
+    }
472
+
473
+    return 0;
474
+}
475
+
476
+static int query_formats(AVFilterContext *ctx)
477
+{
478
+    LV2Context *s = ctx->priv;
479
+    AVFilterFormats *formats;
480
+    AVFilterChannelLayouts *layouts;
481
+    AVFilterLink *outlink = ctx->outputs[0];
482
+    static const enum AVSampleFormat sample_fmts[] = {
483
+        AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
484
+    int ret;
485
+
486
+    formats = ff_make_format_list(sample_fmts);
487
+    if (!formats)
488
+        return AVERROR(ENOMEM);
489
+    ret = ff_set_common_formats(ctx, formats);
490
+    if (ret < 0)
491
+        return ret;
492
+
493
+    if (s->nb_inputs) {
494
+        formats = ff_all_samplerates();
495
+        if (!formats)
496
+            return AVERROR(ENOMEM);
497
+
498
+        ret = ff_set_common_samplerates(ctx, formats);
499
+        if (ret < 0)
500
+            return ret;
501
+    } else {
502
+        int sample_rates[] = { s->sample_rate, -1 };
503
+
504
+        ret = ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
505
+        if (ret < 0)
506
+            return ret;
507
+    }
508
+
509
+    if (s->nb_inputs == 2 && s->nb_outputs == 2) {
510
+        layouts = NULL;
511
+        ret = ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO);
512
+        if (ret < 0)
513
+            return ret;
514
+        ret = ff_set_common_channel_layouts(ctx, layouts);
515
+        if (ret < 0)
516
+            return ret;
517
+    } else {
518
+        if (s->nb_inputs >= 1) {
519
+            AVFilterLink *inlink = ctx->inputs[0];
520
+            uint64_t inlayout = FF_COUNT2LAYOUT(s->nb_inputs);
521
+
522
+            layouts = NULL;
523
+            ret = ff_add_channel_layout(&layouts, inlayout);
524
+            if (ret < 0)
525
+                return ret;
526
+            ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
527
+            if (ret < 0)
528
+                return ret;
529
+
530
+            if (!s->nb_outputs) {
531
+                ret = ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
532
+                if (ret < 0)
533
+                    return ret;
534
+            }
535
+        }
536
+
537
+        if (s->nb_outputs >= 1) {
538
+            uint64_t outlayout = FF_COUNT2LAYOUT(s->nb_outputs);
539
+
540
+            layouts = NULL;
541
+            ret = ff_add_channel_layout(&layouts, outlayout);
542
+            if (ret < 0)
543
+                return ret;
544
+            ret = ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
545
+            if (ret < 0)
546
+                return ret;
547
+        }
548
+    }
549
+
550
+    return 0;
551
+}
552
+
553
+static av_cold void uninit(AVFilterContext *ctx)
554
+{
555
+    LV2Context *s = ctx->priv;
556
+
557
+    lilv_node_free(s->powerOf2BlockLength);
558
+    lilv_node_free(s->fixedBlockLength);
559
+    lilv_node_free(s->boundedBlockLength);
560
+    lilv_node_free(s->urid_map);
561
+    lilv_node_free(s->atom_Sequence);
562
+    lilv_node_free(s->atom_AtomPort);
563
+    lilv_node_free(s->lv2_Optional);
564
+    lilv_node_free(s->lv2_ControlPort);
565
+    lilv_node_free(s->lv2_AudioPort);
566
+    lilv_node_free(s->lv2_OutputPort);
567
+    lilv_node_free(s->lv2_InputPort);
568
+    uri_table_destroy(&s->uri_table);
569
+    lilv_instance_free(s->instance);
570
+    lilv_world_free(s->world);
571
+    av_freep(&s->mins);
572
+    av_freep(&s->maxes);
573
+    av_freep(&s->controls);
574
+    av_freep(&s->seq_out);
575
+
576
+    if (ctx->nb_inputs)
577
+        av_freep(&ctx->input_pads[0].name);
578
+}
579
+
580
+static const AVFilterPad lv2_outputs[] = {
581
+    {
582
+        .name          = "default",
583
+        .type          = AVMEDIA_TYPE_AUDIO,
584
+        .config_props  = config_output,
585
+        .request_frame = request_frame,
586
+    },
587
+    { NULL }
588
+};
589
+
590
+AVFilter ff_af_lv2 = {
591
+    .name          = "lv2",
592
+    .description   = NULL_IF_CONFIG_SMALL("Apply LV2 effect."),
593
+    .priv_size     = sizeof(LV2Context),
594
+    .priv_class    = &lv2_class,
595
+    .init          = init,
596
+    .uninit        = uninit,
597
+    .query_formats = query_formats,
598
+    .inputs        = 0,
599
+    .outputs       = lv2_outputs,
600
+    .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS,
601
+};
... ...
@@ -112,6 +112,7 @@ static void register_all(void)
112 112
     REGISTER_FILTER(LADSPA,         ladspa,         af);
113 113
     REGISTER_FILTER(LOUDNORM,       loudnorm,       af);
114 114
     REGISTER_FILTER(LOWPASS,        lowpass,        af);
115
+    REGISTER_FILTER(LV2,            lv2,            af);
115 116
     REGISTER_FILTER(MCOMPAND,       mcompand,       af);
116 117
     REGISTER_FILTER(PAN,            pan,            af);
117 118
     REGISTER_FILTER(REPLAYGAIN,     replaygain,     af);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   7
33
-#define LIBAVFILTER_VERSION_MINOR   4
33
+#define LIBAVFILTER_VERSION_MINOR   5
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \