af_ladspa.c 23 KB

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