]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_ladspa.c
Merge commit '5b142a4d334bfa8ee5ec5f364c26c98ddb425fac'
[ffmpeg] / libavfilter / af_ladspa.c
1 /*
2  * Copyright (c) 2013 Paul B Mahol
3  * Copyright (c) 2011 Mina Nagy Zaki
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  * LADSPA wrapper
25  */
26
27 #include <dlfcn.h>
28 #include <ladspa.h>
29 #include "libavutil/avstring.h"
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/opt.h"
32 #include "audio.h"
33 #include "avfilter.h"
34 #include "internal.h"
35
36 typedef struct LADSPAContext {
37     const AVClass *class;
38     char *dl_name;
39     char *plugin;
40     char *options;
41     void *dl_handle;
42
43     unsigned long nb_inputs;
44     unsigned long *ipmap;      /* map input number to port number */
45
46     unsigned long nb_inputcontrols;
47     unsigned long *icmap;      /* map input control number to port number */
48     LADSPA_Data *ictlv;        /* input controls values */
49
50     unsigned long nb_outputs;
51     unsigned long *opmap;      /* map output number to port number */
52
53     unsigned long nb_outputcontrols;
54     unsigned long *ocmap;      /* map output control number to port number */
55     LADSPA_Data *octlv;        /* output controls values */
56
57     const LADSPA_Descriptor *desc;
58     int *ctl_needs_value;
59     int nb_handles;
60     LADSPA_Handle *handles;
61
62     int sample_rate;
63     int nb_samples;
64     int64_t pts;
65     int64_t duration;
66 } LADSPAContext;
67
68 #define OFFSET(x) offsetof(LADSPAContext, x)
69 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
70 static const AVOption ladspa_options[] = {
71     { "file", "set library name or full path", OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
72     { "f",    "set library name or full path", OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
73     { "plugin", "set plugin name", OFFSET(plugin), AV_OPT_TYPE_STRING, .flags = FLAGS },
74     { "p",      "set plugin name", OFFSET(plugin), AV_OPT_TYPE_STRING, .flags = FLAGS },
75     { "controls", "set plugin options", OFFSET(options), AV_OPT_TYPE_STRING, .flags = FLAGS },
76     { "c",        "set plugin options", OFFSET(options), AV_OPT_TYPE_STRING, .flags = FLAGS },
77     { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT32_MAX, FLAGS },
78     { "s",           "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT32_MAX, FLAGS },
79     { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
80     { "n",          "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
81     { "duration", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },
82     { "d",        "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },
83     { NULL }
84 };
85
86 AVFILTER_DEFINE_CLASS(ladspa);
87
88 static void print_ctl_info(AVFilterContext *ctx, int level,
89                            LADSPAContext *s, int ctl, unsigned long *map,
90                            LADSPA_Data *values, int print)
91 {
92     const LADSPA_PortRangeHint *h = s->desc->PortRangeHints + map[ctl];
93
94     av_log(ctx, level, "c%i: %s [", ctl, s->desc->PortNames[map[ctl]]);
95
96     if (LADSPA_IS_HINT_TOGGLED(h->HintDescriptor)) {
97         av_log(ctx, level, "toggled (1 or 0)");
98
99         if (LADSPA_IS_HINT_HAS_DEFAULT(h->HintDescriptor))
100             av_log(ctx, level, " (default %i)", (int)values[ctl]);
101     } else {
102         if (LADSPA_IS_HINT_INTEGER(h->HintDescriptor)) {
103             av_log(ctx, level, "<int>");
104
105             if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor))
106                 av_log(ctx, level, ", min: %i", (int)h->LowerBound);
107
108             if (LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor))
109                 av_log(ctx, level, ", max: %i", (int)h->UpperBound);
110
111             if (print)
112                 av_log(ctx, level, " (value %d)", (int)values[ctl]);
113             else if (LADSPA_IS_HINT_HAS_DEFAULT(h->HintDescriptor))
114                 av_log(ctx, level, " (default %d)", (int)values[ctl]);
115         } else {
116             av_log(ctx, level, "<float>");
117
118             if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor))
119                 av_log(ctx, level, ", min: %f", h->LowerBound);
120
121             if (LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor))
122                 av_log(ctx, level, ", max: %f", h->UpperBound);
123
124             if (print)
125                 av_log(ctx, level, " (value %f)", values[ctl]);
126             else if (LADSPA_IS_HINT_HAS_DEFAULT(h->HintDescriptor))
127                 av_log(ctx, level, " (default %f)", values[ctl]);
128         }
129
130         if (LADSPA_IS_HINT_SAMPLE_RATE(h->HintDescriptor))
131             av_log(ctx, level, ", multiple of sample rate");
132
133         if (LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor))
134             av_log(ctx, level, ", logarithmic scale");
135     }
136
137     av_log(ctx, level, "]\n");
138 }
139
140 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
141 {
142     AVFilterContext *ctx = inlink->dst;
143     LADSPAContext *s = ctx->priv;
144     AVFrame *out;
145     int i, h;
146
147     if (!s->nb_outputs ||
148         (av_frame_is_writable(in) && s->nb_inputs == s->nb_outputs &&
149         !(s->desc->Properties & LADSPA_PROPERTY_INPLACE_BROKEN))) {
150         out = in;
151     } else {
152         out = ff_get_audio_buffer(ctx->outputs[0], in->nb_samples);
153         if (!out) {
154             av_frame_free(&in);
155             return AVERROR(ENOMEM);
156         }
157         av_frame_copy_props(out, in);
158     }
159
160     for (h = 0; h < s->nb_handles; h++) {
161         for (i = 0; i < s->nb_inputs; i++) {
162             s->desc->connect_port(s->handles[h], s->ipmap[i],
163                                   (LADSPA_Data*)in->extended_data[i]);
164         }
165
166         for (i = 0; i < s->nb_outputs; i++) {
167             s->desc->connect_port(s->handles[h], s->opmap[i],
168                                   (LADSPA_Data*)out->extended_data[i]);
169         }
170
171         s->desc->run(s->handles[h], in->nb_samples);
172     }
173
174     for (i = 0; i < s->nb_outputcontrols; i++)
175         print_ctl_info(ctx, AV_LOG_VERBOSE, s, i, s->ocmap, s->octlv, 1);
176
177     if (out != in)
178         av_frame_free(&in);
179
180     return ff_filter_frame(ctx->outputs[0], out);
181 }
182
183 static int request_frame(AVFilterLink *outlink)
184 {
185     AVFilterContext *ctx = outlink->src;
186     LADSPAContext *s = ctx->priv;
187     AVFrame *out;
188     int64_t t;
189     int i;
190
191     if (ctx->nb_inputs)
192         return ff_request_frame(ctx->inputs[0]);
193
194     t = av_rescale(s->pts, AV_TIME_BASE, s->sample_rate);
195     if (s->duration >= 0 && t >= s->duration)
196         return AVERROR_EOF;
197
198     out = ff_get_audio_buffer(outlink, s->nb_samples);
199     if (!out)
200         return AVERROR(ENOMEM);
201
202     for (i = 0; i < s->nb_outputs; i++)
203         s->desc->connect_port(s->handles[0], s->opmap[i],
204                 (LADSPA_Data*)out->extended_data[i]);
205
206     s->desc->run(s->handles[0], s->nb_samples);
207
208     for (i = 0; i < s->nb_outputcontrols; i++)
209         print_ctl_info(ctx, AV_LOG_INFO, s, i, s->ocmap, s->octlv, 1);
210
211     out->sample_rate = s->sample_rate;
212     out->pts         = s->pts;
213     s->pts          += s->nb_samples;
214
215     return ff_filter_frame(outlink, out);
216 }
217
218 static void set_default_ctl_value(LADSPAContext *s, int ctl,
219                                   unsigned long *map, LADSPA_Data *values)
220 {
221     const LADSPA_PortRangeHint *h = s->desc->PortRangeHints + map[ctl];
222     const LADSPA_Data lower = h->LowerBound;
223     const LADSPA_Data upper = h->UpperBound;
224
225     if (LADSPA_IS_HINT_DEFAULT_MINIMUM(h->HintDescriptor)) {
226         values[ctl] = lower;
227     } else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(h->HintDescriptor)) {
228         values[ctl] = upper;
229     } else if (LADSPA_IS_HINT_DEFAULT_0(h->HintDescriptor)) {
230         values[ctl] = 0.0;
231     } else if (LADSPA_IS_HINT_DEFAULT_1(h->HintDescriptor)) {
232         values[ctl] = 1.0;
233     } else if (LADSPA_IS_HINT_DEFAULT_100(h->HintDescriptor)) {
234         values[ctl] = 100.0;
235     } else if (LADSPA_IS_HINT_DEFAULT_440(h->HintDescriptor)) {
236         values[ctl] = 440.0;
237     } else if (LADSPA_IS_HINT_DEFAULT_LOW(h->HintDescriptor)) {
238         if (LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor))
239             values[ctl] = exp(log(lower) * 0.75 + log(upper) * 0.25);
240         else
241             values[ctl] = lower * 0.75 + upper * 0.25;
242     } else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(h->HintDescriptor)) {
243         if (LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor))
244             values[ctl] = exp(log(lower) * 0.5 + log(upper) * 0.5);
245         else
246             values[ctl] = lower * 0.5 + upper * 0.5;
247     } else if (LADSPA_IS_HINT_DEFAULT_HIGH(h->HintDescriptor)) {
248         if (LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor))
249             values[ctl] = exp(log(lower) * 0.25 + log(upper) * 0.75);
250         else
251             values[ctl] = lower * 0.25 + upper * 0.75;
252     }
253 }
254
255 static int connect_ports(AVFilterContext *ctx, AVFilterLink *link)
256 {
257     LADSPAContext *s = ctx->priv;
258     int i, j;
259
260     s->nb_handles = s->nb_inputs == 1 && s->nb_outputs == 1 ? link->channels : 1;
261     s->handles    = av_calloc(s->nb_handles, sizeof(*s->handles));
262     if (!s->handles)
263         return AVERROR(ENOMEM);
264
265     for (i = 0; i < s->nb_handles; i++) {
266         s->handles[i] = s->desc->instantiate(s->desc, link->sample_rate);
267         if (!s->handles[i]) {
268             av_log(ctx, AV_LOG_ERROR, "Could not instantiate plugin.\n");
269             return AVERROR_EXTERNAL;
270         }
271
272         // Connect the input control ports
273         for (j = 0; j < s->nb_inputcontrols; j++)
274             s->desc->connect_port(s->handles[i], s->icmap[j], s->ictlv + j);
275
276         // Connect the output control ports
277         for (j = 0; j < s->nb_outputcontrols; j++)
278             s->desc->connect_port(s->handles[i], s->ocmap[j], &s->octlv[j]);
279
280         if (s->desc->activate)
281             s->desc->activate(s->handles[i]);
282     }
283
284     av_log(ctx, AV_LOG_DEBUG, "handles: %d\n", s->nb_handles);
285
286     return 0;
287 }
288
289 static int config_input(AVFilterLink *inlink)
290 {
291     AVFilterContext *ctx = inlink->dst;
292
293     return connect_ports(ctx, inlink);
294 }
295
296 static int config_output(AVFilterLink *outlink)
297 {
298     AVFilterContext *ctx = outlink->src;
299     int ret;
300
301     if (ctx->nb_inputs) {
302         AVFilterLink *inlink = ctx->inputs[0];
303
304         outlink->format      = inlink->format;
305         outlink->sample_rate = inlink->sample_rate;
306         if (ctx->nb_inputs == ctx->nb_outputs) {
307             outlink->channel_layout = inlink->channel_layout;
308             outlink->channels = inlink->channels;
309         }
310
311         ret = 0;
312     } else {
313         LADSPAContext *s = ctx->priv;
314
315         outlink->sample_rate = s->sample_rate;
316         outlink->time_base   = (AVRational){1, s->sample_rate};
317
318         ret = connect_ports(ctx, outlink);
319     }
320
321     return ret;
322 }
323
324 static void count_ports(const LADSPA_Descriptor *desc,
325                         unsigned long *nb_inputs, unsigned long *nb_outputs)
326 {
327     LADSPA_PortDescriptor pd;
328     int i;
329
330     for (i = 0; i < desc->PortCount; i++) {
331         pd = desc->PortDescriptors[i];
332
333         if (LADSPA_IS_PORT_AUDIO(pd)) {
334             if (LADSPA_IS_PORT_INPUT(pd)) {
335                 (*nb_inputs)++;
336             } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
337                 (*nb_outputs)++;
338             }
339         }
340     }
341 }
342
343 static void *try_load(const char *dir, const char *soname)
344 {
345     char *path = av_asprintf("%s/%s.so", dir, soname);
346     void *ret = NULL;
347
348     if (path) {
349         ret = dlopen(path, RTLD_LOCAL|RTLD_NOW);
350         av_free(path);
351     }
352
353     return ret;
354 }
355
356 static int set_control(AVFilterContext *ctx, unsigned long port, LADSPA_Data value)
357 {
358     LADSPAContext *s = ctx->priv;
359     const char *label = s->desc->Label;
360     LADSPA_PortRangeHint *h = (LADSPA_PortRangeHint *)s->desc->PortRangeHints +
361                               s->icmap[port];
362
363     if (port >= s->nb_inputcontrols) {
364         av_log(ctx, AV_LOG_ERROR, "Control c%ld is out of range [0 - %lu].\n",
365                port, s->nb_inputcontrols);
366         return AVERROR(EINVAL);
367     }
368
369     if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor) &&
370             value < h->LowerBound) {
371         av_log(ctx, AV_LOG_ERROR,
372                 "%s: input control c%ld is below lower boundary of %0.4f.\n",
373                 label, port, h->LowerBound);
374         return AVERROR(EINVAL);
375     }
376
377     if (LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor) &&
378             value > h->UpperBound) {
379         av_log(ctx, AV_LOG_ERROR,
380                 "%s: input control c%ld is above upper boundary of %0.4f.\n",
381                 label, port, h->UpperBound);
382         return AVERROR(EINVAL);
383     }
384
385     s->ictlv[port] = value;
386
387     return 0;
388 }
389
390 static av_cold int init(AVFilterContext *ctx)
391 {
392     LADSPAContext *s = ctx->priv;
393     LADSPA_Descriptor_Function descriptor_fn;
394     const LADSPA_Descriptor *desc;
395     LADSPA_PortDescriptor pd;
396     AVFilterPad pad = { NULL };
397     char *p, *arg, *saveptr = NULL;
398     unsigned long nb_ports;
399     int i, j = 0;
400
401     if (!s->dl_name) {
402         av_log(ctx, AV_LOG_ERROR, "No plugin name provided\n");
403         return AVERROR(EINVAL);
404     }
405
406     if (s->dl_name[0] == '/' || s->dl_name[0] == '.') {
407         // argument is a path
408         s->dl_handle = dlopen(s->dl_name, RTLD_LOCAL|RTLD_NOW);
409     } else {
410         // argument is a shared object name
411         char *paths = av_strdup(getenv("LADSPA_PATH"));
412         const char *separator = ":";
413
414         if (paths) {
415             p = paths;
416             while ((arg = av_strtok(p, separator, &saveptr)) && !s->dl_handle) {
417                 s->dl_handle = try_load(arg, s->dl_name);
418                 p = NULL;
419             }
420         }
421
422         av_free(paths);
423         if (!s->dl_handle && (paths = av_asprintf("%s/.ladspa/lib", getenv("HOME")))) {
424             s->dl_handle = try_load(paths, s->dl_name);
425             av_free(paths);
426         }
427
428         if (!s->dl_handle)
429             s->dl_handle = try_load("/usr/local/lib/ladspa", s->dl_name);
430
431         if (!s->dl_handle)
432             s->dl_handle = try_load("/usr/lib/ladspa", s->dl_name);
433     }
434     if (!s->dl_handle) {
435         av_log(ctx, AV_LOG_ERROR, "Failed to load '%s'\n", s->dl_name);
436         return AVERROR(EINVAL);
437     }
438
439     descriptor_fn = dlsym(s->dl_handle, "ladspa_descriptor");
440     if (!descriptor_fn) {
441         av_log(ctx, AV_LOG_ERROR, "Could not find ladspa_descriptor: %s\n", dlerror());
442         return AVERROR(EINVAL);
443     }
444
445     // Find the requested plugin, or list plugins
446     if (!s->plugin) {
447         av_log(ctx, AV_LOG_INFO, "The '%s' library contains the following plugins:\n", s->dl_name);
448         av_log(ctx, AV_LOG_INFO, "I = Input Channels\n");
449         av_log(ctx, AV_LOG_INFO, "O = Output Channels\n");
450         av_log(ctx, AV_LOG_INFO, "I:O %-25s %s\n", "Plugin", "Description");
451         av_log(ctx, AV_LOG_INFO, "\n");
452         for (i = 0; desc = descriptor_fn(i); i++) {
453             unsigned long inputs = 0, outputs = 0;
454
455             count_ports(desc, &inputs, &outputs);
456             av_log(ctx, AV_LOG_INFO, "%lu:%lu %-25s %s\n", inputs, outputs, desc->Label,
457                    (char *)av_x_if_null(desc->Name, "?"));
458             av_log(ctx, AV_LOG_VERBOSE, "Maker: %s\n",
459                    (char *)av_x_if_null(desc->Maker, "?"));
460             av_log(ctx, AV_LOG_VERBOSE, "Copyright: %s\n",
461                    (char *)av_x_if_null(desc->Copyright, "?"));
462         }
463         return AVERROR_EXIT;
464     } else {
465         for (i = 0;; i++) {
466             desc = descriptor_fn(i);
467             if (!desc) {
468                 av_log(ctx, AV_LOG_ERROR, "Could not find plugin: %s\n", s->plugin);
469                 return AVERROR(EINVAL);
470             }
471
472             if (desc->Label && !strcmp(desc->Label, s->plugin))
473                 break;
474         }
475     }
476
477     s->desc  = desc;
478     nb_ports = desc->PortCount;
479
480     s->ipmap = av_calloc(nb_ports, sizeof(*s->ipmap));
481     s->opmap = av_calloc(nb_ports, sizeof(*s->opmap));
482     s->icmap = av_calloc(nb_ports, sizeof(*s->icmap));
483     s->ocmap = av_calloc(nb_ports, sizeof(*s->ocmap));
484     s->ictlv = av_calloc(nb_ports, sizeof(*s->ictlv));
485     s->octlv = av_calloc(nb_ports, sizeof(*s->octlv));
486     s->ctl_needs_value = av_calloc(nb_ports, sizeof(*s->ctl_needs_value));
487     if (!s->ipmap || !s->opmap || !s->icmap ||
488         !s->ocmap || !s->ictlv || !s->octlv || !s->ctl_needs_value)
489         return AVERROR(ENOMEM);
490
491     for (i = 0; i < nb_ports; i++) {
492         pd = desc->PortDescriptors[i];
493
494         if (LADSPA_IS_PORT_AUDIO(pd)) {
495             if (LADSPA_IS_PORT_INPUT(pd)) {
496                 s->ipmap[s->nb_inputs] = i;
497                 s->nb_inputs++;
498             } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
499                 s->opmap[s->nb_outputs] = i;
500                 s->nb_outputs++;
501             }
502         } else if (LADSPA_IS_PORT_CONTROL(pd)) {
503             if (LADSPA_IS_PORT_INPUT(pd)) {
504                 s->icmap[s->nb_inputcontrols] = i;
505
506                 if (LADSPA_IS_HINT_HAS_DEFAULT(desc->PortRangeHints[i].HintDescriptor))
507                     set_default_ctl_value(s, s->nb_inputcontrols, s->icmap, s->ictlv);
508                 else
509                     s->ctl_needs_value[s->nb_inputcontrols] = 1;
510
511                 s->nb_inputcontrols++;
512             } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
513                 s->ocmap[s->nb_outputcontrols] = i;
514                 s->nb_outputcontrols++;
515             }
516         }
517     }
518
519     // List Control Ports if "help" is specified
520     if (s->options && !strcmp(s->options, "help")) {
521         if (!s->nb_inputcontrols) {
522             av_log(ctx, AV_LOG_INFO,
523                    "The '%s' plugin does not have any input controls.\n",
524                    desc->Label);
525         } else {
526             av_log(ctx, AV_LOG_INFO,
527                    "The '%s' plugin has the following input controls:\n",
528                    desc->Label);
529             for (i = 0; i < s->nb_inputcontrols; i++)
530                 print_ctl_info(ctx, AV_LOG_INFO, s, i, s->icmap, s->ictlv, 0);
531         }
532         return AVERROR_EXIT;
533     }
534
535     // Parse control parameters
536     p = s->options;
537     while (s->options) {
538         LADSPA_Data val;
539         int ret;
540
541         if (!(arg = av_strtok(p, "|", &saveptr)))
542             break;
543         p = NULL;
544
545         if (sscanf(arg, "c%d=%f", &i, &val) != 2) {
546             if (sscanf(arg, "%f", &val) != 1) {
547                 av_log(ctx, AV_LOG_ERROR, "Invalid syntax.\n");
548                 return AVERROR(EINVAL);
549             }
550             i = j++;
551         }
552
553         if ((ret = set_control(ctx, i, val)) < 0)
554             return ret;
555         s->ctl_needs_value[i] = 0;
556     }
557
558     // Check if any controls are not set
559     for (i = 0; i < s->nb_inputcontrols; i++) {
560         if (s->ctl_needs_value[i]) {
561             av_log(ctx, AV_LOG_ERROR, "Control c%d must be set.\n", i);
562             print_ctl_info(ctx, AV_LOG_ERROR, s, i, s->icmap, s->ictlv, 0);
563             return AVERROR(EINVAL);
564         }
565     }
566
567     pad.type = AVMEDIA_TYPE_AUDIO;
568
569     if (s->nb_inputs) {
570         pad.name = av_asprintf("in0:%s%lu", desc->Label, s->nb_inputs);
571         if (!pad.name)
572             return AVERROR(ENOMEM);
573
574         pad.filter_frame = filter_frame;
575         pad.config_props = config_input;
576         if (ff_insert_inpad(ctx, ctx->nb_inputs, &pad) < 0) {
577             av_freep(&pad.name);
578             return AVERROR(ENOMEM);
579         }
580     }
581
582     av_log(ctx, AV_LOG_DEBUG, "ports: %lu\n", nb_ports);
583     av_log(ctx, AV_LOG_DEBUG, "inputs: %lu outputs: %lu\n",
584                               s->nb_inputs, s->nb_outputs);
585     av_log(ctx, AV_LOG_DEBUG, "input controls: %lu output controls: %lu\n",
586                               s->nb_inputcontrols, s->nb_outputcontrols);
587
588     return 0;
589 }
590
591 static int query_formats(AVFilterContext *ctx)
592 {
593     LADSPAContext *s = ctx->priv;
594     AVFilterFormats *formats;
595     AVFilterChannelLayouts *layouts;
596     static const enum AVSampleFormat sample_fmts[] = {
597         AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
598
599     formats = ff_make_format_list(sample_fmts);
600     if (!formats)
601         return AVERROR(ENOMEM);
602     ff_set_common_formats(ctx, formats);
603
604     if (s->nb_inputs) {
605         formats = ff_all_samplerates();
606         if (!formats)
607             return AVERROR(ENOMEM);
608
609         ff_set_common_samplerates(ctx, formats);
610     } else {
611         int sample_rates[] = { s->sample_rate, -1 };
612
613         ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
614     }
615
616     if (s->nb_inputs == 1 && s->nb_outputs == 1) {
617         // We will instantiate multiple LADSPA_Handle, one over each channel
618         layouts = ff_all_channel_counts();
619         if (!layouts)
620             return AVERROR(ENOMEM);
621
622         ff_set_common_channel_layouts(ctx, layouts);
623     } else if (s->nb_inputs == 2 && s->nb_outputs == 2) {
624         layouts = NULL;
625         ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO);
626         ff_set_common_channel_layouts(ctx, layouts);
627     } else {
628         AVFilterLink *outlink = ctx->outputs[0];
629
630         if (s->nb_inputs >= 1) {
631             AVFilterLink *inlink = ctx->inputs[0];
632             int64_t inlayout = FF_COUNT2LAYOUT(s->nb_inputs);
633
634             layouts = NULL;
635             ff_add_channel_layout(&layouts, inlayout);
636             ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
637
638             if (!s->nb_outputs)
639                 ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
640         }
641
642         if (s->nb_outputs >= 1) {
643             int64_t outlayout = FF_COUNT2LAYOUT(s->nb_outputs);
644
645             layouts = NULL;
646             ff_add_channel_layout(&layouts, outlayout);
647             ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
648         }
649     }
650
651     return 0;
652 }
653
654 static av_cold void uninit(AVFilterContext *ctx)
655 {
656     LADSPAContext *s = ctx->priv;
657     int i;
658
659     for (i = 0; i < s->nb_handles; i++) {
660         if (s->desc->deactivate)
661             s->desc->deactivate(s->handles[i]);
662         if (s->desc->cleanup)
663             s->desc->cleanup(s->handles[i]);
664     }
665
666     if (s->dl_handle)
667         dlclose(s->dl_handle);
668
669     av_freep(&s->ipmap);
670     av_freep(&s->opmap);
671     av_freep(&s->icmap);
672     av_freep(&s->ocmap);
673     av_freep(&s->ictlv);
674     av_freep(&s->octlv);
675     av_freep(&s->handles);
676     av_freep(&s->ctl_needs_value);
677
678     if (ctx->nb_inputs)
679         av_freep(&ctx->input_pads[0].name);
680 }
681
682 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
683                            char *res, int res_len, int flags)
684 {
685     LADSPA_Data value;
686     unsigned long port;
687
688     if (sscanf(cmd, "c%ld", &port) + sscanf(args, "%f", &value) != 2)
689         return AVERROR(EINVAL);
690
691     return set_control(ctx, port, value);
692 }
693
694 static const AVFilterPad ladspa_outputs[] = {
695     {
696         .name          = "default",
697         .type          = AVMEDIA_TYPE_AUDIO,
698         .config_props  = config_output,
699         .request_frame = request_frame,
700     },
701     { NULL }
702 };
703
704 AVFilter ff_af_ladspa = {
705     .name          = "ladspa",
706     .description   = NULL_IF_CONFIG_SMALL("Apply LADSPA effect."),
707     .priv_size     = sizeof(LADSPAContext),
708     .priv_class    = &ladspa_class,
709     .init          = init,
710     .uninit        = uninit,
711     .query_formats = query_formats,
712     .process_command = process_command,
713     .inputs        = 0,
714     .outputs       = ladspa_outputs,
715     .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS,
716 };