FFmpeg
Loading...
Searching...
No Matches
af_ladspa.c
Go to the documentation of this file.
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/avassert.h"
30#include "libavutil/avstring.h"
32#include "libavutil/fifo.h"
33#include "libavutil/mem.h"
34#include "libavutil/opt.h"
35#include "audio.h"
36#include "avfilter.h"
37#include "filters.h"
38#include "formats.h"
39
40typedef struct MetaItem {
42 int nb_samples;
43} MetaItem;
44
45typedef struct LADSPAContext {
46 const AVClass *class;
47 char *dl_name;
48 char *plugin;
49 char *options;
50 void *dl_handle;
51
52 unsigned long nb_inputs;
53 unsigned long *ipmap; /* map input number to port number */
54
55 unsigned long nb_inputcontrols;
56 unsigned long *icmap; /* map input control number to port number */
57 LADSPA_Data *ictlv; /* input controls values */
58
59 unsigned long nb_outputs;
60 unsigned long *opmap; /* map output number to port number */
61
62 unsigned long nb_outputcontrols;
63 unsigned long *ocmap; /* map output control number to port number */
64 LADSPA_Data *octlv; /* output controls values */
65
66 const LADSPA_Descriptor *desc;
69 LADSPA_Handle *handles;
70
80
83
84#define OFFSET(x) offsetof(LADSPAContext, x)
85#define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
86static const AVOption ladspa_options[] = {
87 { "file", "set library name or full path", OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
88 { "f", "set library name or full path", OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
89 { "plugin", "set plugin name", OFFSET(plugin), AV_OPT_TYPE_STRING, .flags = FLAGS },
90 { "p", "set plugin name", OFFSET(plugin), AV_OPT_TYPE_STRING, .flags = FLAGS },
91 { "controls", "set plugin options", OFFSET(options), AV_OPT_TYPE_STRING, .flags = FLAGS },
92 { "c", "set plugin options", OFFSET(options), AV_OPT_TYPE_STRING, .flags = FLAGS },
93 { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT32_MAX, FLAGS },
94 { "s", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT32_MAX, FLAGS },
95 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
96 { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
97 { "duration", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },
98 { "d", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },
99 { "latency", "enable latency compensation", OFFSET(latency), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
100 { "l", "enable latency compensation", OFFSET(latency), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
101 { NULL }
102};
103
105
107{
108 int latency = 0;
109
110 for (int ctl = 0; ctl < s->nb_outputcontrols; ctl++) {
111 if (av_strcasecmp("latency", s->desc->PortNames[s->ocmap[ctl]]))
112 continue;
113
114 latency = lrintf(s->octlv[ctl]);
115 break;
116 }
117
118 return latency;
119}
120
122 LADSPAContext *s, int ctl, unsigned long *map,
123 LADSPA_Data *values, int print)
124{
125 const LADSPA_PortRangeHint *h = s->desc->PortRangeHints + map[ctl];
126
127 av_log(ctx, level, "c%i: %s [", ctl, s->desc->PortNames[map[ctl]]);
128
129 if (LADSPA_IS_HINT_TOGGLED(h->HintDescriptor)) {
130 av_log(ctx, level, "toggled (1 or 0)");
131
132 if (LADSPA_IS_HINT_HAS_DEFAULT(h->HintDescriptor))
133 av_log(ctx, level, " (default %i)", (int)values[ctl]);
134 } else {
135 if (LADSPA_IS_HINT_INTEGER(h->HintDescriptor)) {
136 av_log(ctx, level, "<int>");
137
138 if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor))
139 av_log(ctx, level, ", min: %i", (int)h->LowerBound);
140
141 if (LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor))
142 av_log(ctx, level, ", max: %i", (int)h->UpperBound);
143
144 if (print)
145 av_log(ctx, level, " (value %d)", (int)values[ctl]);
146 else if (LADSPA_IS_HINT_HAS_DEFAULT(h->HintDescriptor))
147 av_log(ctx, level, " (default %d)", (int)values[ctl]);
148 } else {
149 av_log(ctx, level, "<float>");
150
151 if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor))
152 av_log(ctx, level, ", min: %f", h->LowerBound);
153
154 if (LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor))
155 av_log(ctx, level, ", max: %f", h->UpperBound);
156
157 if (print)
158 av_log(ctx, level, " (value %f)", values[ctl]);
159 else if (LADSPA_IS_HINT_HAS_DEFAULT(h->HintDescriptor))
160 av_log(ctx, level, " (default %f)", values[ctl]);
161 }
162
163 if (LADSPA_IS_HINT_SAMPLE_RATE(h->HintDescriptor))
164 av_log(ctx, level, ", multiple of sample rate");
165
166 if (LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor))
167 av_log(ctx, level, ", logarithmic scale");
168 }
169
170 av_log(ctx, level, "]\n");
171}
172
173static int filter_frame(AVFilterLink *inlink, AVFrame *in)
174{
175 AVFilterContext *ctx = inlink->dst;
176 LADSPAContext *s = ctx->priv;
177 AVFrame *out;
178 int i, h, p, new_out_samples;
179 int64_t out_duration;
180 int64_t in_duration;
181 int64_t in_pts;
182 MetaItem meta;
183
184 av_assert0(in->ch_layout.nb_channels == (s->nb_inputs * s->nb_handles));
185
186 if (!s->nb_outputs ||
187 (av_frame_is_writable(in) && s->nb_inputs == s->nb_outputs &&
188 s->in_trim == 0 && s->out_pad == 0 &&
189 !(s->desc->Properties & LADSPA_PROPERTY_INPLACE_BROKEN))) {
190 out = in;
191 } else {
192 out = ff_get_audio_buffer(ctx->outputs[0], in->nb_samples);
193 if (!out) {
194 av_frame_free(&in);
195 return AVERROR(ENOMEM);
196 }
198 }
199
200 av_assert0(!s->nb_outputs || out->ch_layout.nb_channels == (s->nb_outputs * s->nb_handles));
201
202 for (h = 0; h < s->nb_handles; h++) {
203 for (i = 0; i < s->nb_inputs; i++) {
204 p = s->nb_handles > 1 ? h : i;
205 s->desc->connect_port(s->handles[h], s->ipmap[i],
206 (LADSPA_Data*)in->extended_data[p]);
207 }
208
209 for (i = 0; i < s->nb_outputs; i++) {
210 p = s->nb_handles > 1 ? h : i;
211 s->desc->connect_port(s->handles[h], s->opmap[i],
212 (LADSPA_Data*)out->extended_data[p]);
213 }
214
215 s->desc->run(s->handles[h], in->nb_samples);
216 if (s->latency)
217 s->in_trim = s->out_pad = find_latency(ctx, s);
218 s->latency = 0;
219 }
220
221 for (i = 0; i < s->nb_outputcontrols; i++)
222 print_ctl_info(ctx, AV_LOG_VERBOSE, s, i, s->ocmap, s->octlv, 1);
223
224 meta = (MetaItem){ in->pts, in->nb_samples };
225 av_fifo_write(s->fifo, &meta, 1);
226
227 if (out != in)
228 av_frame_free(&in);
229
230 new_out_samples = out->nb_samples;
231 if (s->in_trim > 0) {
232 int trim = FFMIN(new_out_samples, s->in_trim);
233
234 new_out_samples -= trim;
235 s->in_trim -= trim;
236 }
237
238 if (new_out_samples <= 0) {
240 return 0;
241 } else if (new_out_samples < out->nb_samples) {
242 int offset = out->nb_samples - new_out_samples;
243 for (int ch = 0; ch < out->ch_layout.nb_channels; ch++)
244 memmove(out->extended_data[ch], out->extended_data[ch] + sizeof(float) * offset,
245 sizeof(float) * new_out_samples);
246 out->nb_samples = new_out_samples;
247 }
248
249 av_fifo_read(s->fifo, &meta, 1);
250
251 out_duration = av_rescale_q(out->nb_samples, inlink->time_base, av_make_q(1, out->sample_rate));
252 in_duration = av_rescale_q(meta.nb_samples, inlink->time_base, av_make_q(1, out->sample_rate));
253 in_pts = meta.pts;
254
255 if (s->next_out_pts != AV_NOPTS_VALUE && out->pts != s->next_out_pts &&
256 s->next_in_pts != AV_NOPTS_VALUE && in_pts == s->next_in_pts) {
257 out->pts = s->next_out_pts;
258 } else {
259 out->pts = in_pts;
260 }
261 s->next_in_pts = in_pts + in_duration;
262 s->next_out_pts = out->pts + out_duration;
263
264 return ff_filter_frame(ctx->outputs[0], out);
265}
266
267static int request_frame(AVFilterLink *outlink)
268{
269 AVFilterContext *ctx = outlink->src;
270 LADSPAContext *s = ctx->priv;
271 AVFrame *out;
272 int64_t t;
273 int i;
274
275 if (ctx->nb_inputs) {
276 int ret = ff_request_frame(ctx->inputs[0]);
277
278 if (ret == AVERROR_EOF && s->out_pad > 0) {
279 AVFrame *frame = ff_get_audio_buffer(outlink, FFMIN(2048, s->out_pad));
280 if (!frame)
281 return AVERROR(ENOMEM);
282
283 s->out_pad -= frame->nb_samples;
284 frame->pts = s->next_in_pts;
285 return filter_frame(ctx->inputs[0], frame);
286 }
287 return ret;
288 }
289
290 t = av_rescale(s->pts, AV_TIME_BASE, s->sample_rate);
291 if (s->duration >= 0 && t >= s->duration)
292 return AVERROR_EOF;
293
294 out = ff_get_audio_buffer(outlink, s->nb_samples);
295 if (!out)
296 return AVERROR(ENOMEM);
297
298 for (i = 0; i < s->nb_outputs; i++)
299 s->desc->connect_port(s->handles[0], s->opmap[i],
300 (LADSPA_Data*)out->extended_data[i]);
301
302 s->desc->run(s->handles[0], s->nb_samples);
303
304 for (i = 0; i < s->nb_outputcontrols; i++)
305 print_ctl_info(ctx, AV_LOG_INFO, s, i, s->ocmap, s->octlv, 1);
306
307 out->sample_rate = s->sample_rate;
308 out->pts = s->pts;
309 s->pts += s->nb_samples;
310
311 return ff_filter_frame(outlink, out);
312}
313
315 unsigned long *map, LADSPA_Data *values)
316{
317 const LADSPA_PortRangeHint *h = s->desc->PortRangeHints + map[ctl];
318 const LADSPA_Data lower = h->LowerBound;
319 const LADSPA_Data upper = h->UpperBound;
320
321 if (LADSPA_IS_HINT_DEFAULT_MINIMUM(h->HintDescriptor)) {
322 values[ctl] = lower;
323 } else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(h->HintDescriptor)) {
324 values[ctl] = upper;
325 } else if (LADSPA_IS_HINT_DEFAULT_0(h->HintDescriptor)) {
326 values[ctl] = 0.0;
327 } else if (LADSPA_IS_HINT_DEFAULT_1(h->HintDescriptor)) {
328 values[ctl] = 1.0;
329 } else if (LADSPA_IS_HINT_DEFAULT_100(h->HintDescriptor)) {
330 values[ctl] = 100.0;
331 } else if (LADSPA_IS_HINT_DEFAULT_440(h->HintDescriptor)) {
332 values[ctl] = 440.0;
333 } else if (LADSPA_IS_HINT_DEFAULT_LOW(h->HintDescriptor)) {
334 if (LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor))
335 values[ctl] = exp(log(lower) * 0.75 + log(upper) * 0.25);
336 else
337 values[ctl] = lower * 0.75 + upper * 0.25;
338 } else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(h->HintDescriptor)) {
339 if (LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor))
340 values[ctl] = exp(log(lower) * 0.5 + log(upper) * 0.5);
341 else
342 values[ctl] = lower * 0.5 + upper * 0.5;
343 } else if (LADSPA_IS_HINT_DEFAULT_HIGH(h->HintDescriptor)) {
344 if (LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor))
345 values[ctl] = exp(log(lower) * 0.25 + log(upper) * 0.75);
346 else
347 values[ctl] = lower * 0.25 + upper * 0.75;
348 }
349}
350
352{
353 LADSPAContext *s = ctx->priv;
354 int i, j;
355
356 s->nb_handles = s->nb_inputs == 1 && s->nb_outputs == 1 ? link->ch_layout.nb_channels : 1;
357 s->handles = av_calloc(s->nb_handles, sizeof(*s->handles));
358 if (!s->handles)
359 return AVERROR(ENOMEM);
360
361 for (i = 0; i < s->nb_handles; i++) {
362 s->handles[i] = s->desc->instantiate(s->desc, link->sample_rate);
363 if (!s->handles[i]) {
364 av_log(ctx, AV_LOG_ERROR, "Could not instantiate plugin.\n");
365 return AVERROR_EXTERNAL;
366 }
367
368 // Connect the input control ports
369 for (j = 0; j < s->nb_inputcontrols; j++)
370 s->desc->connect_port(s->handles[i], s->icmap[j], s->ictlv + j);
371
372 // Connect the output control ports
373 for (j = 0; j < s->nb_outputcontrols; j++)
374 s->desc->connect_port(s->handles[i], s->ocmap[j], &s->octlv[j]);
375
376 if (s->desc->activate)
377 s->desc->activate(s->handles[i]);
378 }
379
380 av_log(ctx, AV_LOG_DEBUG, "handles: %d\n", s->nb_handles);
381
382 return 0;
383}
384
385static int config_input(AVFilterLink *inlink)
386{
387 AVFilterContext *ctx = inlink->dst;
388
389 return connect_ports(ctx, inlink);
390}
391
392static int config_output(AVFilterLink *outlink)
393{
394 AVFilterContext *ctx = outlink->src;
395 LADSPAContext *s = ctx->priv;
396 int ret;
397
398 if (ctx->nb_inputs) {
399 AVFilterLink *inlink = ctx->inputs[0];
400
401 outlink->format = inlink->format;
402 outlink->sample_rate = inlink->sample_rate;
403 if (s->nb_inputs == s->nb_outputs) {
404 if ((ret = av_channel_layout_copy(&outlink->ch_layout, &inlink->ch_layout)) < 0)
405 return ret;
406 }
407
408 ret = 0;
409 } else {
410 outlink->sample_rate = s->sample_rate;
411 outlink->time_base = (AVRational){1, s->sample_rate};
412
413 ret = connect_ports(ctx, outlink);
414 }
415
416 return ret;
417}
418
419static void count_ports(const LADSPA_Descriptor *desc,
420 unsigned long *nb_inputs, unsigned long *nb_outputs)
421{
422 LADSPA_PortDescriptor pd;
423 int i;
424
425 for (i = 0; i < desc->PortCount; i++) {
426 pd = desc->PortDescriptors[i];
427
428 if (LADSPA_IS_PORT_AUDIO(pd)) {
429 if (LADSPA_IS_PORT_INPUT(pd)) {
430 (*nb_inputs)++;
431 } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
432 (*nb_outputs)++;
433 }
434 }
435 }
436}
437
438static void *try_load(const char *dir, const char *soname)
439{
440 char *path = av_asprintf("%s/%s.so", dir, soname);
441 void *ret = NULL;
442
443 if (path) {
444 ret = dlopen(path, RTLD_LOCAL|RTLD_NOW);
445 av_free(path);
446 }
447
448 return ret;
449}
450
451static int set_control(AVFilterContext *ctx, unsigned long port, LADSPA_Data value)
452{
453 LADSPAContext *s = ctx->priv;
454 const char *label = s->desc->Label;
455 LADSPA_PortRangeHint *h = (LADSPA_PortRangeHint *)s->desc->PortRangeHints +
456 s->icmap[port];
457
458 if (port >= s->nb_inputcontrols) {
459 av_log(ctx, AV_LOG_ERROR, "Control c%ld is out of range [0 - %lu].\n",
460 port, s->nb_inputcontrols);
461 return AVERROR(EINVAL);
462 }
463
464 if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor) &&
465 value < h->LowerBound) {
467 "%s: input control c%ld is below lower boundary of %0.4f.\n",
468 label, port, h->LowerBound);
469 return AVERROR(EINVAL);
470 }
471
472 if (LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor) &&
473 value > h->UpperBound) {
475 "%s: input control c%ld is above upper boundary of %0.4f.\n",
476 label, port, h->UpperBound);
477 return AVERROR(EINVAL);
478 }
479
480 s->ictlv[port] = value;
481
482 return 0;
483}
484
486{
487 LADSPAContext *s = ctx->priv;
488 LADSPA_Descriptor_Function descriptor_fn;
489 const LADSPA_Descriptor *desc;
490 LADSPA_PortDescriptor pd;
491 AVFilterPad pad = { NULL };
492 char *p, *arg, *saveptr = NULL;
493 unsigned long nb_ports;
494 int i, j = 0, ret;
495
496 if (!s->dl_name) {
497 av_log(ctx, AV_LOG_ERROR, "No plugin name provided\n");
498 return AVERROR(EINVAL);
499 }
500
501 if (s->dl_name[0] == '/' || s->dl_name[0] == '.') {
502 // argument is a path
503 s->dl_handle = dlopen(s->dl_name, RTLD_LOCAL|RTLD_NOW);
504 } else {
505 // argument is a shared object name
506 char *paths = av_strdup(getenv("LADSPA_PATH"));
507 const char *home_path = getenv("HOME");
508 const char *separator = ":";
509
510 if (paths) {
511 p = paths;
512 while ((arg = av_strtok(p, separator, &saveptr)) && !s->dl_handle) {
513 s->dl_handle = try_load(arg, s->dl_name);
514 p = NULL;
515 }
516 }
517
518 av_free(paths);
519 if (!s->dl_handle && home_path && (paths = av_asprintf("%s/.ladspa", home_path))) {
520 s->dl_handle = try_load(paths, s->dl_name);
521 av_free(paths);
522 }
523
524 if (!s->dl_handle && home_path && (paths = av_asprintf("%s/.ladspa/lib", home_path))) {
525 s->dl_handle = try_load(paths, s->dl_name);
526 av_free(paths);
527 }
528
529 if (!s->dl_handle)
530 s->dl_handle = try_load("/usr/local/lib/ladspa", s->dl_name);
531
532 if (!s->dl_handle)
533 s->dl_handle = try_load("/usr/lib/ladspa", s->dl_name);
534 }
535 if (!s->dl_handle) {
536 av_log(ctx, AV_LOG_ERROR, "Failed to load '%s'\n", s->dl_name);
537 return AVERROR(EINVAL);
538 }
539
540 descriptor_fn = dlsym(s->dl_handle, "ladspa_descriptor");
541 if (!descriptor_fn) {
542 av_log(ctx, AV_LOG_ERROR, "Could not find ladspa_descriptor: %s\n", dlerror());
543 return AVERROR(EINVAL);
544 }
545
546 // Find the requested plugin, or list plugins
547 if (!s->plugin) {
548 av_log(ctx, AV_LOG_INFO, "The '%s' library contains the following plugins:\n", s->dl_name);
549 av_log(ctx, AV_LOG_INFO, "I = Input Channels\n");
550 av_log(ctx, AV_LOG_INFO, "O = Output Channels\n");
551 av_log(ctx, AV_LOG_INFO, "I:O %-25s %s\n", "Plugin", "Description");
552 av_log(ctx, AV_LOG_INFO, "\n");
553 for (i = 0; desc = descriptor_fn(i); i++) {
554 unsigned long inputs = 0, outputs = 0;
555
557 av_log(ctx, AV_LOG_INFO, "%lu:%lu %-25s %s\n", inputs, outputs, desc->Label,
558 (char *)av_x_if_null(desc->Name, "?"));
559 av_log(ctx, AV_LOG_VERBOSE, "Maker: %s\n",
560 (char *)av_x_if_null(desc->Maker, "?"));
561 av_log(ctx, AV_LOG_VERBOSE, "Copyright: %s\n",
562 (char *)av_x_if_null(desc->Copyright, "?"));
563 }
564 return AVERROR_EXIT;
565 } else {
566 for (i = 0;; i++) {
567 desc = descriptor_fn(i);
568 if (!desc) {
569 av_log(ctx, AV_LOG_ERROR, "Could not find plugin: %s\n", s->plugin);
570 return AVERROR(EINVAL);
571 }
572
573 if (desc->Label && !strcmp(desc->Label, s->plugin))
574 break;
575 }
576 }
577
578 s->desc = desc;
579 nb_ports = desc->PortCount;
580
581 s->ipmap = av_calloc(nb_ports, sizeof(*s->ipmap));
582 s->opmap = av_calloc(nb_ports, sizeof(*s->opmap));
583 s->icmap = av_calloc(nb_ports, sizeof(*s->icmap));
584 s->ocmap = av_calloc(nb_ports, sizeof(*s->ocmap));
585 s->ictlv = av_calloc(nb_ports, sizeof(*s->ictlv));
586 s->octlv = av_calloc(nb_ports, sizeof(*s->octlv));
587 s->ctl_needs_value = av_calloc(nb_ports, sizeof(*s->ctl_needs_value));
588 if (!s->ipmap || !s->opmap || !s->icmap ||
589 !s->ocmap || !s->ictlv || !s->octlv || !s->ctl_needs_value)
590 return AVERROR(ENOMEM);
591
592 for (i = 0; i < nb_ports; i++) {
593 pd = desc->PortDescriptors[i];
594
595 if (LADSPA_IS_PORT_AUDIO(pd)) {
596 if (LADSPA_IS_PORT_INPUT(pd)) {
597 s->ipmap[s->nb_inputs] = i;
598 s->nb_inputs++;
599 } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
600 s->opmap[s->nb_outputs] = i;
601 s->nb_outputs++;
602 }
603 } else if (LADSPA_IS_PORT_CONTROL(pd)) {
604 if (LADSPA_IS_PORT_INPUT(pd)) {
605 s->icmap[s->nb_inputcontrols] = i;
606
607 if (LADSPA_IS_HINT_HAS_DEFAULT(desc->PortRangeHints[i].HintDescriptor))
608 set_default_ctl_value(s, s->nb_inputcontrols, s->icmap, s->ictlv);
609 else
610 s->ctl_needs_value[s->nb_inputcontrols] = 1;
611
612 s->nb_inputcontrols++;
613 } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
614 s->ocmap[s->nb_outputcontrols] = i;
615 s->nb_outputcontrols++;
616 }
617 }
618 }
619
620 // List Control Ports if "help" is specified
621 if (s->options && !strcmp(s->options, "help")) {
622 if (!s->nb_inputcontrols) {
624 "The '%s' plugin does not have any input controls.\n",
625 desc->Label);
626 } else {
628 "The '%s' plugin has the following input controls:\n",
629 desc->Label);
630 for (i = 0; i < s->nb_inputcontrols; i++)
631 print_ctl_info(ctx, AV_LOG_INFO, s, i, s->icmap, s->ictlv, 0);
632 }
633 return AVERROR_EXIT;
634 }
635
636 // Parse control parameters
637 p = s->options;
638 while (s->options) {
639 LADSPA_Data val;
640 int ret;
641
642 if (!(arg = av_strtok(p, " |", &saveptr)))
643 break;
644 p = NULL;
645
646 if (av_sscanf(arg, "c%d=%f", &i, &val) != 2) {
647 if (av_sscanf(arg, "%f", &val) != 1) {
648 av_log(ctx, AV_LOG_ERROR, "Invalid syntax.\n");
649 return AVERROR(EINVAL);
650 }
651 i = j++;
652 }
653
654 if ((ret = set_control(ctx, i, val)) < 0)
655 return ret;
656 s->ctl_needs_value[i] = 0;
657 }
658
659 // Check if any controls are not set
660 for (i = 0; i < s->nb_inputcontrols; i++) {
661 if (s->ctl_needs_value[i]) {
662 av_log(ctx, AV_LOG_ERROR, "Control c%d must be set.\n", i);
663 print_ctl_info(ctx, AV_LOG_ERROR, s, i, s->icmap, s->ictlv, 0);
664 return AVERROR(EINVAL);
665 }
666 }
667
669
670 if (s->nb_inputs) {
671 pad.name = av_asprintf("in0:%s%lu", desc->Label, s->nb_inputs);
672 if (!pad.name)
673 return AVERROR(ENOMEM);
674
677 if ((ret = ff_append_inpad_free_name(ctx, &pad)) < 0)
678 return ret;
679 }
680
681 av_log(ctx, AV_LOG_DEBUG, "ports: %lu\n", nb_ports);
682 av_log(ctx, AV_LOG_DEBUG, "inputs: %lu outputs: %lu\n",
683 s->nb_inputs, s->nb_outputs);
684 av_log(ctx, AV_LOG_DEBUG, "input controls: %lu output controls: %lu\n",
685 s->nb_inputcontrols, s->nb_outputcontrols);
686
687 s->next_out_pts = AV_NOPTS_VALUE;
688 s->next_in_pts = AV_NOPTS_VALUE;
689
690 s->fifo = av_fifo_alloc2(8, sizeof(MetaItem), AV_FIFO_FLAG_AUTO_GROW);
691 if (!s->fifo)
692 return AVERROR(ENOMEM);
693
694 return 0;
695}
696
698 AVFilterFormatsConfig **cfg_in,
699 AVFilterFormatsConfig **cfg_out)
700{
701 const LADSPAContext *s = ctx->priv;
703 static const enum AVSampleFormat sample_fmts[] = {
705 int ret = ff_set_sample_formats_from_list2(ctx, cfg_in, cfg_out, sample_fmts);
706 if (ret < 0)
707 return ret;
708
709 if (!s->nb_inputs) {
710 int sample_rates[] = { s->sample_rate, -1 };
711
713 if (ret < 0)
714 return ret;
715 }
716
717 if (s->nb_inputs == 1 && s->nb_outputs == 1) {
718 // We will instantiate multiple LADSPA_Handle, one over each channel
719 ret = ff_set_common_all_channel_counts2(ctx, cfg_in, cfg_out);
720 if (ret < 0)
721 return ret;
722 } else if (s->nb_inputs == 2 && s->nb_outputs == 2) {
723 layouts = NULL;
725 if (ret < 0)
726 return ret;
727 ret = ff_set_common_channel_layouts2(ctx, cfg_in, cfg_out, layouts);
728 if (ret < 0)
729 return ret;
730 } else {
731 if (s->nb_inputs >= 1) {
732 AVChannelLayout inlayout = FF_COUNT2LAYOUT(s->nb_inputs);
733
734 layouts = NULL;
735 ret = ff_add_channel_layout(&layouts, &inlayout);
736 if (ret < 0)
737 return ret;
739 if (ret < 0)
740 return ret;
741
742 if (!s->nb_outputs) {
744 if (ret < 0)
745 return ret;
746 }
747 }
748
749 if (s->nb_outputs >= 1) {
750 AVChannelLayout outlayout = FF_COUNT2LAYOUT(s->nb_outputs);
751
752 layouts = NULL;
753 ret = ff_add_channel_layout(&layouts, &outlayout);
754 if (ret < 0)
755 return ret;
757 if (ret < 0)
758 return ret;
759 }
760 }
761
762 return 0;
763}
764
766{
767 LADSPAContext *s = ctx->priv;
768 int i;
769
770 for (i = 0; i < s->nb_handles; i++) {
771 if (s->desc->deactivate)
772 s->desc->deactivate(s->handles[i]);
773 if (s->desc->cleanup)
774 s->desc->cleanup(s->handles[i]);
775 }
776
777 if (s->dl_handle)
778 dlclose(s->dl_handle);
779
780 av_freep(&s->ipmap);
781 av_freep(&s->opmap);
782 av_freep(&s->icmap);
783 av_freep(&s->ocmap);
784 av_freep(&s->ictlv);
785 av_freep(&s->octlv);
786 av_freep(&s->handles);
787 av_freep(&s->ctl_needs_value);
788
789 av_fifo_freep2(&s->fifo);
790}
791
792static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
793 char *res, int res_len, int flags)
794{
795 LADSPA_Data value;
796 unsigned long port;
797
798 if (av_sscanf(cmd, "c%ld", &port) + av_sscanf(args, "%f", &value) != 2)
799 return AVERROR(EINVAL);
800
801 return set_control(ctx, port, value);
802}
803
804static const AVFilterPad ladspa_outputs[] = {
805 {
806 .name = "default",
807 .type = AVMEDIA_TYPE_AUDIO,
808 .config_props = config_output,
809 .request_frame = request_frame,
810 },
811};
812
814 .p.name = "ladspa",
815 .p.description = NULL_IF_CONFIG_SMALL("Apply LADSPA effect."),
816 .p.priv_class = &ladspa_class,
818 .priv_size = sizeof(LADSPAContext),
819 .init = init,
820 .uninit = uninit,
824};
static enum AVSampleFormat sample_fmts[]
Definition adpcmenc.c:933
static double val(void *priv, double ch)
Definition aeval.c:77
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static const AVFilterPad inputs[]
Definition af_aap.c:299
static const AVFilterPad outputs[]
Definition af_aap.c:310
static int config_input(AVFilterLink *inlink)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static int request_frame(AVFilterLink *outlink)
Definition af_aecho.c:272
static void set_default_ctl_value(LADSPAContext *s, int ctl, unsigned long *map, LADSPA_Data *values)
Definition af_ladspa.c:314
static void print_ctl_info(AVFilterContext *ctx, int level, LADSPAContext *s, int ctl, unsigned long *map, LADSPA_Data *values, int print)
Definition af_ladspa.c:121
static int connect_ports(AVFilterContext *ctx, AVFilterLink *link)
Definition af_ladspa.c:351
static int set_control(AVFilterContext *ctx, unsigned long port, LADSPA_Data value)
Definition af_ladspa.c:451
static int config_input(AVFilterLink *inlink)
Definition af_ladspa.c:385
static int find_latency(AVFilterContext *ctx, LADSPAContext *s)
Definition af_ladspa.c:106
static int request_frame(AVFilterLink *outlink)
Definition af_ladspa.c:267
static void count_ports(const LADSPA_Descriptor *desc, unsigned long *nb_inputs, unsigned long *nb_outputs)
Definition af_ladspa.c:419
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition af_ladspa.c:173
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition af_ladspa.c:697
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition af_ladspa.c:792
static const AVOption ladspa_options[]
Definition af_ladspa.c:86
static av_cold void uninit(AVFilterContext *ctx)
Definition af_ladspa.c:765
static void * try_load(const char *dir, const char *soname)
Definition af_ladspa.c:438
const FFFilter ff_af_ladspa
Definition af_ladspa.c:813
#define OFFSET(x)
Definition af_ladspa.c:84
static int config_output(AVFilterLink *outlink)
Definition af_ladspa.c:392
static const AVFilterPad ladspa_outputs[]
Definition af_ladspa.c:804
static FILE * out
static AVFormatContext * ctx
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
int ff_append_inpad_free_name(AVFilterContext *f, AVFilterPad *p)
Definition avfilter.c:132
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition avfilter.c:483
Main libavfilter public API header.
int av_sscanf(const char *string, const char *format,...)
Definition avsscanf.c:962
char * av_asprintf(const char *fmt,...)
Definition avstring.c:115
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static const uint16_t channel_layouts[7]
Definition dca_lbr.c:112
static const int sample_rates[]
Definition dcaenc.h:34
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
double value
Definition eval.c:102
int8_t exp
Definition eval.c:76
static char separator(CheckasmFormat format)
Definition checkasm.c:149
static int64_t duration
Definition ffplay.c:330
A generic FIFO API.
int ff_set_common_channel_layouts2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterChannelLayouts *channel_layouts)
Helpers for query_formats2() which set all free audio links to the same list of channel layouts/sampl...
Definition formats.c:1017
int ff_add_channel_layout(AVFilterChannelLayouts **l, const AVChannelLayout *channel_layout)
Definition formats.c:588
int ff_set_common_samplerates_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const int *samplerates)
Definition formats.c:1050
int ff_set_common_all_channel_counts2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition formats.c:1034
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition formats.c:751
int ff_set_sample_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const enum AVSampleFormat *fmts)
Definition formats.c:1154
#define FF_COUNT2LAYOUT(c)
Encode a channel count as a channel layout.
Definition formats.h:102
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
Definition opt.h:318
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition avfilter.h:155
#define AV_CHANNEL_LAYOUT_STEREO
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition error.h:58
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
Definition fifo.c:47
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
Definition fifo.c:286
#define AV_FIFO_FLAG_AUTO_GROW
Automatically resize the FIFO on writes, so that the data fits.
Definition fifo.h:63
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
Definition fifo.c:188
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
Definition fifo.c:240
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition rational.h:71
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
Definition avutil.h:311
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_NONE
Definition samplefmt.h:56
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition avstring.c:179
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition avstring.c:208
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
const VDPAUPixFmtMap * map
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
unsigned offset
Definition libaomenc.c:763
const char * arg
Definition jacosubdec.c:65
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define lrintf(x)
Definition libm_mips.h:72
const char * desc
Definition libsvtav1.c:83
#define FFMIN(a, b)
Definition macros.h:49
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
enum MovChannelLayoutTag * layouts
Definition mov_chan.c:335
#define av_strdup(s)
Definition ops_static.c:55
AVOptions.
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
Definition fifo.c:35
A list of supported channel layouts.
Definition formats.h:85
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
int(* config_props)(AVFilterLink *link)
Link configuration callback.
Definition filters.h:120
enum AVMediaType type
AVFilterPad type.
Definition filters.h:51
int(* filter_frame)(AVFilterLink *link, AVFrame *frame)
Filtering callback.
Definition filters.h:95
const char * name
Pad name.
Definition filters.h:46
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition frame.h:815
uint8_t ** extended_data
pointers to the data planes/channels.
Definition frame.h:533
AVOption.
Definition opt.h:428
Rational number (pair of numerator and denominator).
Definition rational.h:58
unsigned long * ipmap
Definition af_ladspa.c:53
AVFifo * fifo
Definition af_ladspa.c:81
unsigned long nb_outputs
Definition af_ladspa.c:59
const LADSPA_Descriptor * desc
Definition af_ladspa.c:66
unsigned long nb_outputcontrols
Definition af_ladspa.c:62
unsigned long nb_inputcontrols
Definition af_ladspa.c:55
int64_t duration
Definition af_ladspa.c:76
unsigned long * ocmap
Definition af_ladspa.c:63
LADSPA_Handle * handles
Definition af_ladspa.c:69
char * options
Definition af_ladspa.c:49
LADSPA_Data * ictlv
Definition af_ladspa.c:57
int64_t pts
Definition af_ladspa.c:75
void * dl_handle
Definition af_ladspa.c:50
int64_t next_in_pts
Definition af_ladspa.c:73
unsigned long * icmap
Definition af_ladspa.c:56
int * ctl_needs_value
Definition af_ladspa.c:67
unsigned long * opmap
Definition af_ladspa.c:60
LADSPA_Data * octlv
Definition af_ladspa.c:64
unsigned long nb_inputs
Definition af_ladspa.c:52
char * plugin
Definition af_ladspa.c:48
char * dl_name
Definition af_ladspa.c:47
int64_t next_out_pts
Definition af_ladspa.c:74
int nb_samples
Definition af_alimiter.c:39
int64_t pts
Definition af_alimiter.c:38
uint8_t level
Definition svq3.c:208
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
static void print(AVTreeNode *t, int depth)
Definition tree.c:45