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