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