2 * Copyright (c) 2013 Paul B Mahol
3 * Copyright (c) 2011 Mina Nagy Zaki
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
29 #include "libavutil/avstring.h"
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/opt.h"
36 typedef struct LADSPAContext {
43 unsigned long nb_inputs;
44 unsigned long *ipmap; /* map input number to port number */
46 unsigned long nb_inputcontrols;
47 unsigned long *icmap; /* map input control number to port number */
48 LADSPA_Data *ictlv; /* input controls values */
50 unsigned long nb_outputs;
51 unsigned long *opmap; /* map output number to port number */
53 unsigned long nb_outputcontrols;
54 unsigned long *ocmap; /* map output control number to port number */
55 LADSPA_Data *octlv; /* output controls values */
57 const LADSPA_Descriptor *desc;
60 LADSPA_Handle *handles;
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 },
86 AVFILTER_DEFINE_CLASS(ladspa);
88 static void print_ctl_info(AVFilterContext *ctx, int level,
89 LADSPAContext *s, int ctl, unsigned long *map,
90 LADSPA_Data *values, int print)
92 const LADSPA_PortRangeHint *h = s->desc->PortRangeHints + map[ctl];
94 av_log(ctx, level, "c%i: %s [", ctl, s->desc->PortNames[map[ctl]]);
96 if (LADSPA_IS_HINT_TOGGLED(h->HintDescriptor)) {
97 av_log(ctx, level, "toggled (1 or 0)");
99 if (LADSPA_IS_HINT_HAS_DEFAULT(h->HintDescriptor))
100 av_log(ctx, level, " (default %i)", (int)values[ctl]);
102 if (LADSPA_IS_HINT_INTEGER(h->HintDescriptor)) {
103 av_log(ctx, level, "<int>");
105 if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor))
106 av_log(ctx, level, ", min: %i", (int)h->LowerBound);
108 if (LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor))
109 av_log(ctx, level, ", max: %i", (int)h->UpperBound);
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]);
116 av_log(ctx, level, "<float>");
118 if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor))
119 av_log(ctx, level, ", min: %f", h->LowerBound);
121 if (LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor))
122 av_log(ctx, level, ", max: %f", h->UpperBound);
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]);
130 if (LADSPA_IS_HINT_SAMPLE_RATE(h->HintDescriptor))
131 av_log(ctx, level, ", multiple of sample rate");
133 if (LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor))
134 av_log(ctx, level, ", logarithmic scale");
137 av_log(ctx, level, "]\n");
140 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
142 AVFilterContext *ctx = inlink->dst;
143 LADSPAContext *s = ctx->priv;
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))) {
152 out = ff_get_audio_buffer(ctx->outputs[0], in->nb_samples);
155 return AVERROR(ENOMEM);
157 av_frame_copy_props(out, in);
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]);
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]);
171 s->desc->run(s->handles[h], in->nb_samples);
174 for (i = 0; i < s->nb_outputcontrols; i++)
175 print_ctl_info(ctx, AV_LOG_VERBOSE, s, i, s->ocmap, s->octlv, 1);
180 return ff_filter_frame(ctx->outputs[0], out);
183 static int request_frame(AVFilterLink *outlink)
185 AVFilterContext *ctx = outlink->src;
186 LADSPAContext *s = ctx->priv;
192 return ff_request_frame(ctx->inputs[0]);
194 t = av_rescale(s->pts, AV_TIME_BASE, s->sample_rate);
195 if (s->duration >= 0 && t >= s->duration)
198 out = ff_get_audio_buffer(outlink, s->nb_samples);
200 return AVERROR(ENOMEM);
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]);
206 s->desc->run(s->handles[0], s->nb_samples);
208 for (i = 0; i < s->nb_outputcontrols; i++)
209 print_ctl_info(ctx, AV_LOG_INFO, s, i, s->ocmap, s->octlv, 1);
211 out->sample_rate = s->sample_rate;
213 s->pts += s->nb_samples;
215 return ff_filter_frame(outlink, out);
218 static void set_default_ctl_value(LADSPAContext *s, int ctl,
219 unsigned long *map, LADSPA_Data *values)
221 const LADSPA_PortRangeHint *h = s->desc->PortRangeHints + map[ctl];
222 const LADSPA_Data lower = h->LowerBound;
223 const LADSPA_Data upper = h->UpperBound;
225 if (LADSPA_IS_HINT_DEFAULT_MINIMUM(h->HintDescriptor)) {
227 } else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(h->HintDescriptor)) {
229 } else if (LADSPA_IS_HINT_DEFAULT_0(h->HintDescriptor)) {
231 } else if (LADSPA_IS_HINT_DEFAULT_1(h->HintDescriptor)) {
233 } else if (LADSPA_IS_HINT_DEFAULT_100(h->HintDescriptor)) {
235 } else if (LADSPA_IS_HINT_DEFAULT_440(h->HintDescriptor)) {
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);
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);
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);
251 values[ctl] = lower * 0.25 + upper * 0.75;
255 static int connect_ports(AVFilterContext *ctx, AVFilterLink *link)
257 LADSPAContext *s = ctx->priv;
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));
263 return AVERROR(ENOMEM);
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;
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);
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]);
280 if (s->desc->activate)
281 s->desc->activate(s->handles[i]);
284 av_log(ctx, AV_LOG_DEBUG, "handles: %d\n", s->nb_handles);
289 static int config_input(AVFilterLink *inlink)
291 AVFilterContext *ctx = inlink->dst;
293 return connect_ports(ctx, inlink);
296 static int config_output(AVFilterLink *outlink)
298 AVFilterContext *ctx = outlink->src;
301 if (ctx->nb_inputs) {
302 AVFilterLink *inlink = ctx->inputs[0];
304 outlink->format = inlink->format;
305 outlink->sample_rate = inlink->sample_rate;
309 LADSPAContext *s = ctx->priv;
311 outlink->sample_rate = s->sample_rate;
312 outlink->time_base = (AVRational){1, s->sample_rate};
314 ret = connect_ports(ctx, outlink);
320 static void count_ports(const LADSPA_Descriptor *desc,
321 unsigned long *nb_inputs, unsigned long *nb_outputs)
323 LADSPA_PortDescriptor pd;
326 for (i = 0; i < desc->PortCount; i++) {
327 pd = desc->PortDescriptors[i];
329 if (LADSPA_IS_PORT_AUDIO(pd)) {
330 if (LADSPA_IS_PORT_INPUT(pd)) {
332 } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
339 static void *try_load(const char *dir, const char *soname)
341 char *path = av_asprintf("%s/%s.so", dir, soname);
345 ret = dlopen(path, RTLD_LOCAL|RTLD_NOW);
352 static av_cold int init(AVFilterContext *ctx)
354 LADSPAContext *s = ctx->priv;
355 LADSPA_Descriptor_Function descriptor_fn;
356 const LADSPA_Descriptor *desc;
357 LADSPA_PortDescriptor pd;
358 AVFilterPad pad = { NULL };
359 char *p, *arg, *saveptr = NULL;
360 unsigned long nb_ports;
364 av_log(ctx, AV_LOG_ERROR, "No plugin name provided\n");
365 return AVERROR(EINVAL);
368 if (s->dl_name[0] == '/' || s->dl_name[0] == '.') {
369 // argument is a path
370 s->dl_handle = dlopen(s->dl_name, RTLD_LOCAL|RTLD_NOW);
372 // argument is a shared object name
373 char *paths = av_strdup(getenv("LADSPA_PATH"));
374 const char *separator = ":";
378 while ((arg = av_strtok(p, separator, &saveptr)) && !s->dl_handle) {
379 s->dl_handle = try_load(arg, s->dl_name);
385 if (!s->dl_handle && (paths = av_asprintf("%s/.ladspa/lib", getenv("HOME")))) {
386 s->dl_handle = try_load(paths, s->dl_name);
391 s->dl_handle = try_load("/usr/local/lib/ladspa", s->dl_name);
394 s->dl_handle = try_load("/usr/lib/ladspa", s->dl_name);
397 av_log(ctx, AV_LOG_ERROR, "Failed to load '%s'\n", s->dl_name);
398 return AVERROR(EINVAL);
401 descriptor_fn = dlsym(s->dl_handle, "ladspa_descriptor");
402 if (!descriptor_fn) {
403 av_log(ctx, AV_LOG_ERROR, "Could not find ladspa_descriptor: %s\n", dlerror());
404 return AVERROR(EINVAL);
407 // Find the requested plugin, or list plugins
409 av_log(ctx, AV_LOG_INFO, "The '%s' library contains the following plugins:\n", s->dl_name);
410 av_log(ctx, AV_LOG_INFO, "I = Input Channels\n");
411 av_log(ctx, AV_LOG_INFO, "O = Output Channels\n");
412 av_log(ctx, AV_LOG_INFO, "I:O %-25s %s\n", "Plugin", "Description");
413 av_log(ctx, AV_LOG_INFO, "\n");
414 for (i = 0; desc = descriptor_fn(i); i++) {
415 unsigned long inputs = 0, outputs = 0;
417 count_ports(desc, &inputs, &outputs);
418 av_log(ctx, AV_LOG_INFO, "%lu:%lu %-25s %s\n", inputs, outputs, desc->Label,
419 av_x_if_null(desc->Name, "?"));
420 av_log(ctx, AV_LOG_VERBOSE, "Maker: %s\n", av_x_if_null(desc->Maker, "?"));
421 av_log(ctx, AV_LOG_VERBOSE, "Copyright: %s\n", av_x_if_null(desc->Copyright, "?"));
426 desc = descriptor_fn(i);
428 av_log(ctx, AV_LOG_ERROR, "Could not find plugin: %s\n", s->plugin);
429 return AVERROR(EINVAL);
432 if (desc->Label && !strcmp(desc->Label, s->plugin))
438 nb_ports = desc->PortCount;
440 s->ipmap = av_calloc(nb_ports, sizeof(*s->ipmap));
441 s->opmap = av_calloc(nb_ports, sizeof(*s->opmap));
442 s->icmap = av_calloc(nb_ports, sizeof(*s->icmap));
443 s->ocmap = av_calloc(nb_ports, sizeof(*s->ocmap));
444 s->ictlv = av_calloc(nb_ports, sizeof(*s->ictlv));
445 s->octlv = av_calloc(nb_ports, sizeof(*s->octlv));
446 s->ctl_needs_value = av_calloc(nb_ports, sizeof(*s->ctl_needs_value));
447 if (!s->ipmap || !s->opmap || !s->icmap ||
448 !s->ocmap || !s->ictlv || !s->octlv || !s->ctl_needs_value)
449 return AVERROR(ENOMEM);
451 for (i = 0; i < nb_ports; i++) {
452 pd = desc->PortDescriptors[i];
454 if (LADSPA_IS_PORT_AUDIO(pd)) {
455 if (LADSPA_IS_PORT_INPUT(pd)) {
456 s->ipmap[s->nb_inputs] = i;
458 } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
459 s->opmap[s->nb_outputs] = i;
462 } else if (LADSPA_IS_PORT_CONTROL(pd)) {
463 if (LADSPA_IS_PORT_INPUT(pd)) {
464 s->icmap[s->nb_inputcontrols] = i;
466 if (LADSPA_IS_HINT_HAS_DEFAULT(desc->PortRangeHints[i].HintDescriptor))
467 set_default_ctl_value(s, s->nb_inputcontrols, s->icmap, s->ictlv);
469 s->ctl_needs_value[s->nb_inputcontrols] = 1;
471 s->nb_inputcontrols++;
472 } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
473 s->ocmap[s->nb_outputcontrols] = i;
474 s->nb_outputcontrols++;
479 // List Control Ports if "help" is specified
480 if (s->options && !strcmp(s->options, "help")) {
481 if (!s->nb_inputcontrols) {
482 av_log(ctx, AV_LOG_INFO,
483 "The '%s' plugin does not have any input controls.\n",
486 av_log(ctx, AV_LOG_INFO,
487 "The '%s' plugin has the following input controls:\n",
489 for (i = 0; i < s->nb_inputcontrols; i++)
490 print_ctl_info(ctx, AV_LOG_INFO, s, i, s->icmap, s->ictlv, 0);
495 // Parse control parameters
498 LADSPA_PortRangeHint *h;
501 if (!(arg = av_strtok(p, "|", &saveptr)))
505 if (sscanf(arg, "c%d=%f", &i, &val) != 2) {
506 av_log(ctx, AV_LOG_ERROR, "Invalid syntax.\n");
507 return AVERROR(EINVAL);
510 if (i < 0 || i >= s->nb_inputcontrols) {
511 av_log(ctx, AV_LOG_ERROR,
512 "Control c%d is out of range [0 - %lu].\n",
513 i, s->nb_inputcontrols);
514 return AVERROR(EINVAL);
517 h = (LADSPA_PortRangeHint *)s->desc->PortRangeHints + s->icmap[i];
519 s->ctl_needs_value[i] = 0;
520 if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor) &&
521 val < h->LowerBound) {
522 av_log(ctx, AV_LOG_ERROR,
523 "%s: input control c%d is below lower boundary of %0.4f.\n",
524 s->desc->Label, i, h->LowerBound);
525 return AVERROR(EINVAL);
527 if (LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor) &&
528 val > h->UpperBound) {
529 av_log(ctx, AV_LOG_ERROR,
530 "%s: input control c%d is above upper boundary of %0.4f.\n",
531 s->desc->Label, i, h->UpperBound);
532 return AVERROR(EINVAL);
536 // Check if any controls are not set
537 for (i = 0; i < s->nb_inputcontrols; i++) {
538 if (s->ctl_needs_value[i]) {
539 av_log(ctx, AV_LOG_ERROR, "Control c%d must be set.\n", i);
540 print_ctl_info(ctx, AV_LOG_ERROR, s, i, s->icmap, s->ictlv, 0);
541 return AVERROR(EINVAL);
545 pad.type = AVMEDIA_TYPE_AUDIO;
548 pad.name = av_asprintf("in0:%s%lu", desc->Label, s->nb_inputs);
550 return AVERROR(ENOMEM);
552 pad.filter_frame = filter_frame;
553 pad.config_props = config_input;
554 if (ff_insert_inpad(ctx, ctx->nb_inputs, &pad) < 0) {
556 return AVERROR(ENOMEM);
560 av_log(ctx, AV_LOG_DEBUG, "ports: %lu\n", nb_ports);
561 av_log(ctx, AV_LOG_DEBUG, "inputs: %lu outputs: %lu\n",
562 s->nb_inputs, s->nb_outputs);
563 av_log(ctx, AV_LOG_DEBUG, "input controls: %lu output controls: %lu\n",
564 s->nb_inputcontrols, s->nb_outputcontrols);
569 static int query_formats(AVFilterContext *ctx)
571 LADSPAContext *s = ctx->priv;
572 AVFilterFormats *formats;
573 AVFilterChannelLayouts *layouts;
574 static const enum AVSampleFormat sample_fmts[] = {
575 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
577 formats = ff_make_format_list(sample_fmts);
579 return AVERROR(ENOMEM);
580 ff_set_common_formats(ctx, formats);
583 formats = ff_all_samplerates();
585 return AVERROR(ENOMEM);
587 ff_set_common_samplerates(ctx, formats);
589 int sample_rates[] = { s->sample_rate, -1 };
591 ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
594 if (s->nb_inputs == 1 && s->nb_outputs == 1) {
595 // We will instantiate multiple LADSPA_Handle, one over each channel
596 layouts = ff_all_channel_layouts();
598 return AVERROR(ENOMEM);
600 ff_set_common_channel_layouts(ctx, layouts);
602 if (s->nb_inputs >= 1) {
603 AVFilterLink *inlink = ctx->inputs[0];
604 int64_t inlayout = FF_COUNT2LAYOUT(s->nb_inputs);
607 ff_add_channel_layout(&layouts, inlayout);
608 ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
611 if (s->nb_outputs >= 1) {
612 AVFilterLink *outlink = ctx->outputs[0];
613 int64_t outlayout = FF_COUNT2LAYOUT(s->nb_outputs);
616 ff_add_channel_layout(&layouts, outlayout);
617 ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
624 static av_cold void uninit(AVFilterContext *ctx)
626 LADSPAContext *s = ctx->priv;
629 for (i = 0; i < s->nb_handles; i++) {
630 if (s->desc->deactivate)
631 s->desc->deactivate(s->handles[i]);
632 if (s->desc->cleanup)
633 s->desc->cleanup(s->handles[i]);
637 dlclose(s->dl_handle);
645 av_freep(&s->handles);
646 av_freep(&s->ctl_needs_value);
649 av_freep(&ctx->input_pads[0].name);
652 static const AVFilterPad ladspa_outputs[] = {
655 .type = AVMEDIA_TYPE_AUDIO,
656 .config_props = config_output,
657 .request_frame = request_frame,
662 AVFilter avfilter_af_ladspa = {
664 .description = NULL_IF_CONFIG_SMALL("Apply LADSPA effect."),
665 .priv_size = sizeof(LADSPAContext),
666 .priv_class = &ladspa_class,
669 .query_formats = query_formats,
671 .outputs = ladspa_outputs,
672 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,