af_ladspa.c 24 KB

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