Browse code

avfilter: add ladspa wrapper filter

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

Paul B Mahol authored on 2013/09/20 23:01:00
Showing 7 changed files
... ...
@@ -35,6 +35,7 @@ version <next>
35 35
 - Fraps: use BT.709 colorspace by default for yuv, as reference fraps decoder does
36 36
 - make decoding alpha optional for prores, ffv1 and vp6 by setting
37 37
   the skip_alpha flag.
38
+- ladspa wrapper filter
38 39
 
39 40
 
40 41
 version 2.0:
... ...
@@ -193,6 +193,7 @@ External library support:
193 193
   --enable-frei0r          enable frei0r video filtering
194 194
   --enable-gnutls          enable gnutls [no]
195 195
   --disable-iconv          disable iconv [autodetect]
196
+  --enable-ladspa          enable LADSPA audio filtering
196 197
   --enable-libaacplus      enable AAC+ encoding via libaacplus [no]
197 198
   --enable-libass          enable libass subtitles rendering [no]
198 199
   --enable-libbluray       enable BluRay reading using libbluray [no]
... ...
@@ -1162,6 +1163,7 @@ EXTERNAL_LIBRARY_LIST="
1162 1162
     frei0r
1163 1163
     gnutls
1164 1164
     iconv
1165
+    ladspa
1165 1166
     libaacplus
1166 1167
     libass
1167 1168
     libbluray
... ...
@@ -2210,6 +2212,7 @@ histeq_filter_deps="gpl"
2210 2210
 hqdn3d_filter_deps="gpl"
2211 2211
 interlace_filter_deps="gpl"
2212 2212
 kerndeint_filter_deps="gpl"
2213
+ladspa_filter_deps="ladspa dlopen"
2213 2214
 mcdeint_filter_deps="avcodec gpl"
2214 2215
 movie_filter_deps="avcodec avformat"
2215 2216
 mp_filter_deps="gpl avcodec swscale inline_asm"
... ...
@@ -4210,6 +4213,7 @@ enabled avisynth          && { { check_lib2 "windows.h" LoadLibrary; } ||
4210 4210
 enabled fontconfig        && require_pkg_config fontconfig "fontconfig/fontconfig.h" FcInit
4211 4211
 enabled frei0r            && { check_header frei0r.h || die "ERROR: frei0r.h header not found"; }
4212 4212
 enabled gnutls            && require_pkg_config gnutls gnutls/gnutls.h gnutls_global_init
4213
+enabled ladspa            && { check_header ladspa.h || die "ERROR: ladspa.h header not found"; }
4213 4214
 enabled libiec61883       && require libiec61883 libiec61883/iec61883.h iec61883_cmp_connect -lraw1394 -lavc1394 -lrom1394 -liec61883
4214 4215
 enabled libaacplus        && require "libaacplus >= 2.0.0" aacplus.h aacplusEncOpen -laacplus
4215 4216
 enabled libass            && require_pkg_config libass ass/ass.h ass_library_init
... ...
@@ -1400,6 +1400,104 @@ ffmpeg -i fl -i fr -i fc -i sl -i sr -i lfe -filter_complex
1400 1400
 out
1401 1401
 @end example
1402 1402
 
1403
+@section ladspa
1404
+
1405
+Load a LADSPA (Linux Audio Developer's Simple Plugin API) plugin.
1406
+
1407
+@table @option
1408
+@item file, f
1409
+Specifies the name of LADSPA plugin library to load. If the environment
1410
+variable @env{LADSPA_PATH} is defined, the LADSPA plugin is searched in
1411
+each one of the directories specified by the colon separated list in
1412
+@env{LADSPA_PATH}, otherwise in the standard LADSPA paths, which are in
1413
+this order: @file{HOME/.ladspa/lib/}, @file{/usr/local/lib/ladspa/},
1414
+@file{/usr/lib/ladspa/}.
1415
+
1416
+@item plugin, p
1417
+Specifies the plugin within the library. Some libraries contain only
1418
+one plugin, but others contain many of them. If this is not set filter
1419
+will list all available plugins within the specified library.
1420
+
1421
+@item controls, c
1422
+Set the '|' separated list of controls which are zero or more floating point
1423
+values that determine the behavior of the loaded plugin (for example delay,
1424
+threshold or gain).
1425
+Controls need to be defined using the following syntax:
1426
+c0=@var{value0}|c1=@var{value1}|c2=@var{value2}|..., where
1427
+@var{valuei} is the value set on the @var{i}-th control.
1428
+If @option{controls} is set to @code{help}, all available controls and
1429
+their valid ranges are printed.
1430
+
1431
+@item sample_rate, s
1432
+Specify the sample rate, default to 44100. Only used if plugin have
1433
+zero inputs.
1434
+
1435
+@item nb_samples, n
1436
+Set the number of samples per channel per each output frame, default
1437
+is 1024. Only used if plugin have zero inputs.
1438
+
1439
+@item duration, d
1440
+Set the minimum duration of the sourced audio. See the function
1441
+@code{av_parse_time()} for the accepted format, also check the "Time duration"
1442
+section in the ffmpeg-utils manual.
1443
+Note that the resulting duration may be greater than the specified duration,
1444
+as the generated audio is always cut at the end of a complete frame.
1445
+If not specified, or the expressed duration is negative, the audio is
1446
+supposed to be generated forever.
1447
+Only used if plugin have zero inputs.
1448
+
1449
+@end table
1450
+
1451
+@subsection Examples
1452
+
1453
+@itemize
1454
+@item
1455
+List all available plugins within amp (LADSPA example plugin) library:
1456
+@example
1457
+ladspa=file=amp
1458
+@end example
1459
+
1460
+@item
1461
+List all available controls and their valid ranges for @code{vcf_notch}
1462
+plugin from @code{VCF} library:
1463
+@example
1464
+ladspa=f=vcf:p=vcf_notch:c=help
1465
+@end example
1466
+
1467
+@item
1468
+Simulate low quality audio equipment using @code{Computer Music Toolkit} (CMT)
1469
+plugin library:
1470
+@example
1471
+ladspa=file=cmt:plugin=lofi:controls=c0=22|c1=12|c2=12
1472
+@end example
1473
+
1474
+@item
1475
+Add reverberation to the audio using TAP-plugins
1476
+(Tom's Audio Processing plugins):
1477
+@example
1478
+ladspa=file=tap_reverb:tap_reverb
1479
+@end example
1480
+
1481
+@item
1482
+Generate white noise, with 0.2 amplitude:
1483
+@example
1484
+ladspa=file=cmt:noise_source_white:c=c0=.2
1485
+@end example
1486
+
1487
+@item
1488
+Generate 20 bpm clicks using plugin @code{C* Click - Metronome} from the
1489
+@code{C* Audio Plugin Suite} (CAPS) library:
1490
+@example
1491
+ladspa=file=caps:Click:c=c1=20'
1492
+@end example
1493
+
1494
+@item
1495
+Apply @code{C* Eq10X2 - Stereo 10-band equaliser} effect:
1496
+@example
1497
+ladspa=caps:Eq10X2:c=c0=-48|c9=-24|c3=12|c4=2
1498
+@end example
1499
+@end itemize
1500
+
1403 1501
 @section lowpass
1404 1502
 
1405 1503
 Apply a low-pass filter with 3dB point frequency.
... ...
@@ -91,6 +91,7 @@ OBJS-$(CONFIG_EBUR128_FILTER)                += f_ebur128.o
91 91
 OBJS-$(CONFIG_EQUALIZER_FILTER)              += af_biquads.o
92 92
 OBJS-$(CONFIG_HIGHPASS_FILTER)               += af_biquads.o
93 93
 OBJS-$(CONFIG_JOIN_FILTER)                   += af_join.o
94
+OBJS-$(CONFIG_LADSPA_FILTER)                 += af_ladspa.o
94 95
 OBJS-$(CONFIG_LOWPASS_FILTER)                += af_biquads.o
95 96
 OBJS-$(CONFIG_PAN_FILTER)                    += af_pan.o
96 97
 OBJS-$(CONFIG_RESAMPLE_FILTER)               += af_resample.o
97 98
new file mode 100644
... ...
@@ -0,0 +1,673 @@
0
+/*
1
+ * Copyright (c) 2013 Paul B Mahol
2
+ * Copyright (c) 2011 Mina Nagy Zaki
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
+ * LADSPA wrapper
24
+ */
25
+
26
+#include <dlfcn.h>
27
+#include <ladspa.h>
28
+#include "libavutil/avstring.h"
29
+#include "libavutil/channel_layout.h"
30
+#include "libavutil/opt.h"
31
+#include "audio.h"
32
+#include "avfilter.h"
33
+#include "internal.h"
34
+
35
+typedef struct LADSPAContext {
36
+    const AVClass *class;
37
+    char *dl_name;
38
+    char *plugin;
39
+    char *options;
40
+    void *dl_handle;
41
+
42
+    unsigned long nb_inputs;
43
+    unsigned long *ipmap;      /* map input number to port number */
44
+
45
+    unsigned long nb_inputcontrols;
46
+    unsigned long *icmap;      /* map input control number to port number */
47
+    LADSPA_Data *ictlv;        /* input controls values */
48
+
49
+    unsigned long nb_outputs;
50
+    unsigned long *opmap;      /* map output number to port number */
51
+
52
+    unsigned long nb_outputcontrols;
53
+    unsigned long *ocmap;      /* map output control number to port number */
54
+    LADSPA_Data *octlv;        /* output controls values */
55
+
56
+    const LADSPA_Descriptor *desc;
57
+    int *ctl_needs_value;
58
+    int nb_handles;
59
+    LADSPA_Handle *handles;
60
+
61
+    int sample_rate;
62
+    int nb_samples;
63
+    int64_t pts;
64
+    int64_t duration;
65
+} LADSPAContext;
66
+
67
+#define OFFSET(x) offsetof(LADSPAContext, x)
68
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
69
+static const AVOption ladspa_options[] = {
70
+    { "file", "set library name or full path", OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
71
+    { "f",    "set library name or full path", OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
72
+    { "plugin", "set plugin name", OFFSET(plugin), AV_OPT_TYPE_STRING, .flags = FLAGS },
73
+    { "p",      "set plugin name", OFFSET(plugin), AV_OPT_TYPE_STRING, .flags = FLAGS },
74
+    { "controls", "set plugin options", OFFSET(options), AV_OPT_TYPE_STRING, .flags = FLAGS },
75
+    { "c",        "set plugin options", OFFSET(options), AV_OPT_TYPE_STRING, .flags = FLAGS },
76
+    { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT32_MAX, FLAGS },
77
+    { "s",           "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT32_MAX, FLAGS },
78
+    { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
79
+    { "n",          "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
80
+    { "duration", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },
81
+    { "d",        "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },
82
+    { NULL }
83
+};
84
+
85
+AVFILTER_DEFINE_CLASS(ladspa);
86
+
87
+static void print_ctl_info(AVFilterContext *ctx, int level,
88
+                           LADSPAContext *s, int ctl, unsigned long *map,
89
+                           LADSPA_Data *values, int print)
90
+{
91
+    const LADSPA_PortRangeHint *h = s->desc->PortRangeHints + map[ctl];
92
+
93
+    av_log(ctx, level, "c%i: %s [", ctl, s->desc->PortNames[map[ctl]]);
94
+
95
+    if (LADSPA_IS_HINT_TOGGLED(h->HintDescriptor)) {
96
+        av_log(ctx, level, "toggled (1 or 0)");
97
+
98
+        if (LADSPA_IS_HINT_HAS_DEFAULT(h->HintDescriptor))
99
+            av_log(ctx, level, " (default %i)", (int)values[ctl]);
100
+    } else {
101
+        if (LADSPA_IS_HINT_INTEGER(h->HintDescriptor)) {
102
+            av_log(ctx, level, "<int>");
103
+
104
+            if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor))
105
+                av_log(ctx, level, ", min: %i", (int)h->LowerBound);
106
+
107
+            if (LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor))
108
+                av_log(ctx, level, ", max: %i", (int)h->UpperBound);
109
+
110
+            if (print)
111
+                av_log(ctx, level, " (value %d)", (int)values[ctl]);
112
+            else if (LADSPA_IS_HINT_HAS_DEFAULT(h->HintDescriptor))
113
+                av_log(ctx, level, " (default %d)", (int)values[ctl]);
114
+        } else {
115
+            av_log(ctx, level, "<float>");
116
+
117
+            if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor))
118
+                av_log(ctx, level, ", min: %f", h->LowerBound);
119
+
120
+            if (LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor))
121
+                av_log(ctx, level, ", max: %f", h->UpperBound);
122
+
123
+            if (print)
124
+                av_log(ctx, level, " (value %f)", values[ctl]);
125
+            else if (LADSPA_IS_HINT_HAS_DEFAULT(h->HintDescriptor))
126
+                av_log(ctx, level, " (default %f)", values[ctl]);
127
+        }
128
+
129
+        if (LADSPA_IS_HINT_SAMPLE_RATE(h->HintDescriptor))
130
+            av_log(ctx, level, ", multiple of sample rate");
131
+
132
+        if (LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor))
133
+            av_log(ctx, level, ", logarithmic scale");
134
+    }
135
+
136
+    av_log(ctx, level, "]\n");
137
+}
138
+
139
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
140
+{
141
+    AVFilterContext *ctx = inlink->dst;
142
+    LADSPAContext *s = ctx->priv;
143
+    AVFrame *out;
144
+    int i, h;
145
+
146
+    if (!s->nb_outputs ||
147
+        (av_frame_is_writable(in) && s->nb_inputs == s->nb_outputs &&
148
+        !(s->desc->Properties & LADSPA_PROPERTY_INPLACE_BROKEN))) {
149
+        out = in;
150
+    } else {
151
+        out = ff_get_audio_buffer(ctx->outputs[0], in->nb_samples);
152
+        if (!out) {
153
+            av_frame_free(&in);
154
+            return AVERROR(ENOMEM);
155
+        }
156
+        av_frame_copy_props(out, in);
157
+    }
158
+
159
+    for (h = 0; h < s->nb_handles; h++) {
160
+        for (i = 0; i < s->nb_inputs; i++) {
161
+            s->desc->connect_port(s->handles[h], s->ipmap[i],
162
+                                  (LADSPA_Data*)in->extended_data[i]);
163
+        }
164
+
165
+        for (i = 0; i < s->nb_outputs; i++) {
166
+            s->desc->connect_port(s->handles[h], s->opmap[i],
167
+                                  (LADSPA_Data*)out->extended_data[i]);
168
+        }
169
+
170
+        s->desc->run(s->handles[h], in->nb_samples);
171
+    }
172
+
173
+    for (i = 0; i < s->nb_outputcontrols; i++)
174
+        print_ctl_info(ctx, AV_LOG_VERBOSE, s, i, s->ocmap, s->octlv, 1);
175
+
176
+    if (out != in)
177
+        av_frame_free(&in);
178
+
179
+    return ff_filter_frame(ctx->outputs[0], out);
180
+}
181
+
182
+static int request_frame(AVFilterLink *outlink)
183
+{
184
+    AVFilterContext *ctx = outlink->src;
185
+    LADSPAContext *s = ctx->priv;
186
+    AVFrame *out;
187
+    int64_t t;
188
+    int i;
189
+
190
+    if (ctx->nb_inputs)
191
+        return ff_request_frame(ctx->inputs[0]);
192
+
193
+    t = av_rescale(s->pts, AV_TIME_BASE, s->sample_rate);
194
+    if (s->duration >= 0 && t >= s->duration)
195
+        return AVERROR_EOF;
196
+
197
+    out = ff_get_audio_buffer(outlink, s->nb_samples);
198
+    if (!out)
199
+        return AVERROR(ENOMEM);
200
+
201
+    for (i = 0; i < s->nb_outputs; i++)
202
+        s->desc->connect_port(s->handles[0], s->opmap[i],
203
+                (LADSPA_Data*)out->extended_data[i]);
204
+
205
+    s->desc->run(s->handles[0], s->nb_samples);
206
+
207
+    for (i = 0; i < s->nb_outputcontrols; i++)
208
+        print_ctl_info(ctx, AV_LOG_INFO, s, i, s->ocmap, s->octlv, 1);
209
+
210
+    out->sample_rate = s->sample_rate;
211
+    out->pts         = s->pts;
212
+    s->pts          += s->nb_samples;
213
+
214
+    return ff_filter_frame(outlink, out);
215
+}
216
+
217
+static void set_default_ctl_value(LADSPAContext *s, int ctl,
218
+                                  unsigned long *map, LADSPA_Data *values)
219
+{
220
+    const LADSPA_PortRangeHint *h = s->desc->PortRangeHints + map[ctl];
221
+    const LADSPA_Data lower = h->LowerBound;
222
+    const LADSPA_Data upper = h->UpperBound;
223
+
224
+    if (LADSPA_IS_HINT_DEFAULT_MINIMUM(h->HintDescriptor)) {
225
+        values[ctl] = lower;
226
+    } else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(h->HintDescriptor)) {
227
+        values[ctl] = upper;
228
+    } else if (LADSPA_IS_HINT_DEFAULT_0(h->HintDescriptor)) {
229
+        values[ctl] = 0.0;
230
+    } else if (LADSPA_IS_HINT_DEFAULT_1(h->HintDescriptor)) {
231
+        values[ctl] = 1.0;
232
+    } else if (LADSPA_IS_HINT_DEFAULT_100(h->HintDescriptor)) {
233
+        values[ctl] = 100.0;
234
+    } else if (LADSPA_IS_HINT_DEFAULT_440(h->HintDescriptor)) {
235
+        values[ctl] = 440.0;
236
+    } else if (LADSPA_IS_HINT_DEFAULT_LOW(h->HintDescriptor)) {
237
+        if (LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor))
238
+            values[ctl] = exp(log(lower) * 0.75 + log(upper) * 0.25);
239
+        else
240
+            values[ctl] = lower * 0.75 + upper * 0.25;
241
+    } else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(h->HintDescriptor)) {
242
+        if (LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor))
243
+            values[ctl] = exp(log(lower) * 0.5 + log(upper) * 0.5);
244
+        else
245
+            values[ctl] = lower * 0.5 + upper * 0.5;
246
+    } else if (LADSPA_IS_HINT_DEFAULT_HIGH(h->HintDescriptor)) {
247
+        if (LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor))
248
+            values[ctl] = exp(log(lower) * 0.25 + log(upper) * 0.75);
249
+        else
250
+            values[ctl] = lower * 0.25 + upper * 0.75;
251
+    }
252
+}
253
+
254
+static int connect_ports(AVFilterContext *ctx, AVFilterLink *link)
255
+{
256
+    LADSPAContext *s = ctx->priv;
257
+    int i, j;
258
+
259
+    s->nb_handles = s->nb_inputs == 1 && s->nb_outputs == 1 ? link->channels : 1;
260
+    s->handles    = av_calloc(s->nb_handles, sizeof(*s->handles));
261
+    if (!s->handles)
262
+        return AVERROR(ENOMEM);
263
+
264
+    for (i = 0; i < s->nb_handles; i++) {
265
+        s->handles[i] = s->desc->instantiate(s->desc, link->sample_rate);
266
+        if (!s->handles[i]) {
267
+            av_log(ctx, AV_LOG_ERROR, "Could not instantiate plugin.\n");
268
+            return AVERROR_EXTERNAL;
269
+        }
270
+
271
+        // Connect the input control ports
272
+        for (j = 0; j < s->nb_inputcontrols; j++)
273
+            s->desc->connect_port(s->handles[i], s->icmap[j], s->ictlv + j);
274
+
275
+        // Connect the output control ports
276
+        for (j = 0; j < s->nb_outputcontrols; j++)
277
+            s->desc->connect_port(s->handles[i], s->ocmap[j], &s->octlv[j]);
278
+
279
+        if (s->desc->activate)
280
+            s->desc->activate(s->handles[i]);
281
+    }
282
+
283
+    av_log(ctx, AV_LOG_DEBUG, "handles: %d\n", s->nb_handles);
284
+
285
+    return 0;
286
+}
287
+
288
+static int config_input(AVFilterLink *inlink)
289
+{
290
+    AVFilterContext *ctx = inlink->dst;
291
+
292
+    return connect_ports(ctx, inlink);
293
+}
294
+
295
+static int config_output(AVFilterLink *outlink)
296
+{
297
+    AVFilterContext *ctx = outlink->src;
298
+    int ret;
299
+
300
+    if (ctx->nb_inputs) {
301
+        AVFilterLink *inlink = ctx->inputs[0];
302
+
303
+        outlink->format      = inlink->format;
304
+        outlink->sample_rate = inlink->sample_rate;
305
+
306
+        ret = 0;
307
+    } else {
308
+        LADSPAContext *s = ctx->priv;
309
+
310
+        outlink->sample_rate = s->sample_rate;
311
+        outlink->time_base   = (AVRational){1, s->sample_rate};
312
+
313
+        ret = connect_ports(ctx, outlink);
314
+    }
315
+
316
+    return ret;
317
+}
318
+
319
+static void count_ports(const LADSPA_Descriptor *desc,
320
+                        unsigned long *nb_inputs, unsigned long *nb_outputs)
321
+{
322
+    LADSPA_PortDescriptor pd;
323
+    int i;
324
+
325
+    for (i = 0; i < desc->PortCount; i++) {
326
+        pd = desc->PortDescriptors[i];
327
+
328
+        if (LADSPA_IS_PORT_AUDIO(pd)) {
329
+            if (LADSPA_IS_PORT_INPUT(pd)) {
330
+                (*nb_inputs)++;
331
+            } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
332
+                (*nb_outputs)++;
333
+            }
334
+        }
335
+    }
336
+}
337
+
338
+static void *try_load(const char *dir, const char *soname)
339
+{
340
+    char *path = av_asprintf("%s/%s.so", dir, soname);
341
+    void *ret = NULL;
342
+
343
+    if (path) {
344
+        ret = dlopen(path, RTLD_LOCAL|RTLD_NOW);
345
+        av_free(path);
346
+    }
347
+
348
+    return ret;
349
+}
350
+
351
+static av_cold int init(AVFilterContext *ctx)
352
+{
353
+    LADSPAContext *s = ctx->priv;
354
+    LADSPA_Descriptor_Function descriptor_fn;
355
+    const LADSPA_Descriptor *desc;
356
+    LADSPA_PortDescriptor pd;
357
+    AVFilterPad pad = { NULL };
358
+    char *p, *arg, *saveptr = NULL;
359
+    unsigned long nb_ports;
360
+    int i;
361
+
362
+    if (!s->dl_name) {
363
+        av_log(ctx, AV_LOG_ERROR, "No plugin name provided\n");
364
+        return AVERROR(EINVAL);
365
+    }
366
+
367
+    if (s->dl_name[0] == '/' || s->dl_name[0] == '.') {
368
+        // argument is a path
369
+        s->dl_handle = dlopen(s->dl_name, RTLD_LOCAL|RTLD_NOW);
370
+    } else {
371
+        // argument is a shared object name
372
+        char *paths = av_strdup(getenv("LADSPA_PATH"));
373
+        const char *separator = ":";
374
+
375
+        if (paths) {
376
+            p = paths;
377
+            while ((arg = av_strtok(p, separator, &saveptr)) && !s->dl_handle) {
378
+                s->dl_handle = try_load(arg, s->dl_name);
379
+                p = NULL;
380
+            }
381
+        }
382
+
383
+        av_free(paths);
384
+        if (!s->dl_handle && (paths = av_asprintf("%s/.ladspa/lib", getenv("HOME")))) {
385
+            s->dl_handle = try_load(paths, s->dl_name);
386
+            av_free(paths);
387
+        }
388
+
389
+        if (!s->dl_handle)
390
+            s->dl_handle = try_load("/usr/local/lib/ladspa", s->dl_name);
391
+
392
+        if (!s->dl_handle)
393
+            s->dl_handle = try_load("/usr/lib/ladspa", s->dl_name);
394
+    }
395
+    if (!s->dl_handle) {
396
+        av_log(ctx, AV_LOG_ERROR, "Failed to load '%s'\n", s->dl_name);
397
+        return AVERROR(EINVAL);
398
+    }
399
+
400
+    descriptor_fn = dlsym(s->dl_handle, "ladspa_descriptor");
401
+    if (!descriptor_fn) {
402
+        av_log(ctx, AV_LOG_ERROR, "Could not find ladspa_descriptor: %s\n", dlerror());
403
+        return AVERROR(EINVAL);
404
+    }
405
+
406
+    // Find the requested plugin, or list plugins
407
+    if (!s->plugin) {
408
+        av_log(ctx, AV_LOG_INFO, "The '%s' library contains the following plugins:\n", s->dl_name);
409
+        av_log(ctx, AV_LOG_INFO, "I = Input Channels\n");
410
+        av_log(ctx, AV_LOG_INFO, "O = Output Channels\n");
411
+        av_log(ctx, AV_LOG_INFO, "I:O %-25s %s\n", "Plugin", "Description");
412
+        av_log(ctx, AV_LOG_INFO, "\n");
413
+        for (i = 0; desc = descriptor_fn(i); i++) {
414
+            unsigned long inputs = 0, outputs = 0;
415
+
416
+            count_ports(desc, &inputs, &outputs);
417
+            av_log(ctx, AV_LOG_INFO, "%lu:%lu %-25s %s\n", inputs, outputs, desc->Label,
418
+                                     av_x_if_null(desc->Name, "?"));
419
+            av_log(ctx, AV_LOG_VERBOSE, "Maker: %s\n", av_x_if_null(desc->Maker, "?"));
420
+            av_log(ctx, AV_LOG_VERBOSE, "Copyright: %s\n", av_x_if_null(desc->Copyright, "?"));
421
+        }
422
+        return AVERROR_EXIT;
423
+    } else {
424
+        for (i = 0;; i++) {
425
+            desc = descriptor_fn(i);
426
+            if (!desc) {
427
+                av_log(ctx, AV_LOG_ERROR, "Could not find plugin: %s\n", s->plugin);
428
+                return AVERROR(EINVAL);
429
+            }
430
+
431
+            if (desc->Label && !strcmp(desc->Label, s->plugin))
432
+                break;
433
+        }
434
+    }
435
+
436
+    s->desc  = desc;
437
+    nb_ports = desc->PortCount;
438
+
439
+    s->ipmap = av_calloc(nb_ports, sizeof(*s->ipmap));
440
+    s->opmap = av_calloc(nb_ports, sizeof(*s->opmap));
441
+    s->icmap = av_calloc(nb_ports, sizeof(*s->icmap));
442
+    s->ocmap = av_calloc(nb_ports, sizeof(*s->ocmap));
443
+    s->ictlv = av_calloc(nb_ports, sizeof(*s->ictlv));
444
+    s->octlv = av_calloc(nb_ports, sizeof(*s->octlv));
445
+    s->ctl_needs_value = av_calloc(nb_ports, sizeof(*s->ctl_needs_value));
446
+    if (!s->ipmap || !s->opmap || !s->icmap ||
447
+        !s->ocmap || !s->ictlv || !s->octlv || !s->ctl_needs_value)
448
+        return AVERROR(ENOMEM);
449
+
450
+    for (i = 0; i < nb_ports; i++) {
451
+        pd = desc->PortDescriptors[i];
452
+
453
+        if (LADSPA_IS_PORT_AUDIO(pd)) {
454
+            if (LADSPA_IS_PORT_INPUT(pd)) {
455
+                s->ipmap[s->nb_inputs] = i;
456
+                s->nb_inputs++;
457
+            } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
458
+                s->opmap[s->nb_outputs] = i;
459
+                s->nb_outputs++;
460
+            }
461
+        } else if (LADSPA_IS_PORT_CONTROL(pd)) {
462
+            if (LADSPA_IS_PORT_INPUT(pd)) {
463
+                s->icmap[s->nb_inputcontrols] = i;
464
+
465
+                if (LADSPA_IS_HINT_HAS_DEFAULT(desc->PortRangeHints[i].HintDescriptor))
466
+                    set_default_ctl_value(s, s->nb_inputcontrols, s->icmap, s->ictlv);
467
+                else
468
+                    s->ctl_needs_value[s->nb_inputcontrols] = 1;
469
+
470
+                s->nb_inputcontrols++;
471
+            } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
472
+                s->ocmap[s->nb_outputcontrols] = i;
473
+                s->nb_outputcontrols++;
474
+            }
475
+        }
476
+    }
477
+
478
+    // List Control Ports if "help" is specified
479
+    if (s->options && !strcmp(s->options, "help")) {
480
+        if (!s->nb_inputcontrols) {
481
+            av_log(ctx, AV_LOG_INFO,
482
+                   "The '%s' plugin does not have any input controls.\n",
483
+                   desc->Label);
484
+        } else {
485
+            av_log(ctx, AV_LOG_INFO,
486
+                   "The '%s' plugin has the following input controls:\n",
487
+                   desc->Label);
488
+            for (i = 0; i < s->nb_inputcontrols; i++)
489
+                print_ctl_info(ctx, AV_LOG_INFO, s, i, s->icmap, s->ictlv, 0);
490
+        }
491
+        return AVERROR_EXIT;
492
+    }
493
+
494
+    // Parse control parameters
495
+    p = s->options;
496
+    while (s->options) {
497
+        LADSPA_PortRangeHint *h;
498
+        LADSPA_Data val;
499
+
500
+        if (!(arg = av_strtok(p, "|", &saveptr)))
501
+            break;
502
+        p = NULL;
503
+
504
+        if (sscanf(arg, "c%d=%f", &i, &val) != 2) {
505
+            av_log(ctx, AV_LOG_ERROR, "Invalid syntax.\n");
506
+            return AVERROR(EINVAL);
507
+        }
508
+
509
+        if (i < 0 || i >= s->nb_inputcontrols) {
510
+            av_log(ctx, AV_LOG_ERROR,
511
+                   "Control c%d is out of range [0 - %lu].\n",
512
+                   i, s->nb_inputcontrols);
513
+            return AVERROR(EINVAL);
514
+        }
515
+
516
+        h = (LADSPA_PortRangeHint *)s->desc->PortRangeHints + s->icmap[i];
517
+        s->ictlv[i]           = val;
518
+        s->ctl_needs_value[i] = 0;
519
+        if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor) &&
520
+                val < h->LowerBound) {
521
+            av_log(ctx, AV_LOG_ERROR,
522
+                   "%s: input control c%d is below lower boundary of %0.4f.\n",
523
+                   s->desc->Label, i, h->LowerBound);
524
+            return AVERROR(EINVAL);
525
+        }
526
+        if (LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor) &&
527
+                val > h->UpperBound) {
528
+            av_log(ctx, AV_LOG_ERROR,
529
+                   "%s: input control c%d is above upper boundary of %0.4f.\n",
530
+                   s->desc->Label, i, h->UpperBound);
531
+            return AVERROR(EINVAL);
532
+        }
533
+    }
534
+
535
+    // Check if any controls are not set
536
+    for (i = 0; i < s->nb_inputcontrols; i++) {
537
+        if (s->ctl_needs_value[i]) {
538
+            av_log(ctx, AV_LOG_ERROR, "Control c%d must be set.\n", i);
539
+            print_ctl_info(ctx, AV_LOG_ERROR, s, i, s->icmap, s->ictlv, 0);
540
+            return AVERROR(EINVAL);
541
+        }
542
+    }
543
+
544
+    pad.type = AVMEDIA_TYPE_AUDIO;
545
+
546
+    if (s->nb_inputs) {
547
+        pad.name = av_asprintf("in0:%s%lu", desc->Label, s->nb_inputs);
548
+        if (!pad.name)
549
+            return AVERROR(ENOMEM);
550
+
551
+        pad.filter_frame = filter_frame;
552
+        pad.config_props = config_input;
553
+        if (ff_insert_inpad(ctx, ctx->nb_inputs, &pad) < 0) {
554
+            av_freep(&pad.name);
555
+            return AVERROR(ENOMEM);
556
+        }
557
+    }
558
+
559
+    av_log(ctx, AV_LOG_DEBUG, "ports: %lu\n", nb_ports);
560
+    av_log(ctx, AV_LOG_DEBUG, "inputs: %lu outputs: %lu\n",
561
+                              s->nb_inputs, s->nb_outputs);
562
+    av_log(ctx, AV_LOG_DEBUG, "input controls: %lu output controls: %lu\n",
563
+                              s->nb_inputcontrols, s->nb_outputcontrols);
564
+
565
+    return 0;
566
+}
567
+
568
+static int query_formats(AVFilterContext *ctx)
569
+{
570
+    LADSPAContext *s = ctx->priv;
571
+    AVFilterFormats *formats;
572
+    AVFilterChannelLayouts *layouts;
573
+    static const enum AVSampleFormat sample_fmts[] = {
574
+        AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
575
+
576
+    formats = ff_make_format_list(sample_fmts);
577
+    if (!formats)
578
+        return AVERROR(ENOMEM);
579
+    ff_set_common_formats(ctx, formats);
580
+
581
+    if (s->nb_inputs) {
582
+        formats = ff_all_samplerates();
583
+        if (!formats)
584
+            return AVERROR(ENOMEM);
585
+
586
+        ff_set_common_samplerates(ctx, formats);
587
+    } else {
588
+        int sample_rates[] = { s->sample_rate, -1 };
589
+
590
+        ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
591
+    }
592
+
593
+    if (s->nb_inputs == 1 && s->nb_outputs == 1) {
594
+        // We will instantiate multiple LADSPA_Handle, one over each channel
595
+        layouts = ff_all_channel_layouts();
596
+        if (!layouts)
597
+            return AVERROR(ENOMEM);
598
+
599
+        ff_set_common_channel_layouts(ctx, layouts);
600
+    } else {
601
+        if (s->nb_inputs >= 1) {
602
+            AVFilterLink *inlink = ctx->inputs[0];
603
+            int64_t inlayout = FF_COUNT2LAYOUT(s->nb_inputs);
604
+
605
+            layouts = NULL;
606
+            ff_add_channel_layout(&layouts, inlayout);
607
+            ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
608
+        }
609
+
610
+        if (s->nb_outputs >= 1) {
611
+            AVFilterLink *outlink = ctx->outputs[0];
612
+            int64_t outlayout = FF_COUNT2LAYOUT(s->nb_outputs);
613
+
614
+            layouts = NULL;
615
+            ff_add_channel_layout(&layouts, outlayout);
616
+            ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
617
+        }
618
+    }
619
+
620
+    return 0;
621
+}
622
+
623
+static av_cold void uninit(AVFilterContext *ctx)
624
+{
625
+    LADSPAContext *s = ctx->priv;
626
+    int i;
627
+
628
+    for (i = 0; i < s->nb_handles; i++) {
629
+        if (s->desc->deactivate)
630
+            s->desc->deactivate(s->handles[i]);
631
+        if (s->desc->cleanup)
632
+            s->desc->cleanup(s->handles[i]);
633
+    }
634
+
635
+    if (s->dl_handle)
636
+        dlclose(s->dl_handle);
637
+
638
+    av_freep(&s->ipmap);
639
+    av_freep(&s->opmap);
640
+    av_freep(&s->icmap);
641
+    av_freep(&s->ocmap);
642
+    av_freep(&s->ictlv);
643
+    av_freep(&s->octlv);
644
+    av_freep(&s->handles);
645
+    av_freep(&s->ctl_needs_value);
646
+
647
+    if (ctx->nb_inputs)
648
+        av_freep(&ctx->input_pads[0].name);
649
+}
650
+
651
+static const AVFilterPad ladspa_outputs[] = {
652
+    {
653
+        .name          = "default",
654
+        .type          = AVMEDIA_TYPE_AUDIO,
655
+        .config_props  = config_output,
656
+        .request_frame = request_frame,
657
+    },
658
+    { NULL }
659
+};
660
+
661
+AVFilter avfilter_af_ladspa = {
662
+    .name          = "ladspa",
663
+    .description   = NULL_IF_CONFIG_SMALL("Apply LADSPA effect."),
664
+    .priv_size     = sizeof(LADSPAContext),
665
+    .priv_class    = &ladspa_class,
666
+    .init          = init,
667
+    .uninit        = uninit,
668
+    .query_formats = query_formats,
669
+    .inputs        = 0,
670
+    .outputs       = ladspa_outputs,
671
+    .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS,
672
+};
... ...
@@ -87,6 +87,7 @@ void avfilter_register_all(void)
87 87
     REGISTER_FILTER(EQUALIZER,      equalizer,      af);
88 88
     REGISTER_FILTER(HIGHPASS,       highpass,       af);
89 89
     REGISTER_FILTER(JOIN,           join,           af);
90
+    REGISTER_FILTER(LADSPA,         ladspa,         af);
90 91
     REGISTER_FILTER(LOWPASS,        lowpass,        af);
91 92
     REGISTER_FILTER(PAN,            pan,            af);
92 93
     REGISTER_FILTER(RESAMPLE,       resample,       af);
... ...
@@ -30,8 +30,8 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR  3
33
-#define LIBAVFILTER_VERSION_MINOR  86
34
-#define LIBAVFILTER_VERSION_MICRO 102
33
+#define LIBAVFILTER_VERSION_MINOR  87
34
+#define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
                                                LIBAVFILTER_VERSION_MINOR, \