You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

714 lines
23KB

  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. if (ctx->nb_inputs == ctx->nb_outputs) {
  251. outlink->channel_layout = inlink->channel_layout;
  252. outlink->channels = inlink->channels;
  253. }
  254. ret = 0;
  255. } else {
  256. LADSPAContext *s = ctx->priv;
  257. outlink->sample_rate = s->sample_rate;
  258. outlink->time_base = (AVRational){1, s->sample_rate};
  259. ret = connect_ports(ctx, outlink);
  260. }
  261. return ret;
  262. }
  263. static void count_ports(const LADSPA_Descriptor *desc,
  264. unsigned long *nb_inputs, unsigned long *nb_outputs)
  265. {
  266. LADSPA_PortDescriptor pd;
  267. int i;
  268. for (i = 0; i < desc->PortCount; i++) {
  269. pd = desc->PortDescriptors[i];
  270. if (LADSPA_IS_PORT_AUDIO(pd)) {
  271. if (LADSPA_IS_PORT_INPUT(pd)) {
  272. (*nb_inputs)++;
  273. } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
  274. (*nb_outputs)++;
  275. }
  276. }
  277. }
  278. }
  279. static void *try_load(const char *dir, const char *soname)
  280. {
  281. char *path = av_asprintf("%s/%s.so", dir, soname);
  282. void *ret = NULL;
  283. if (path) {
  284. ret = dlopen(path, RTLD_LOCAL|RTLD_NOW);
  285. av_free(path);
  286. }
  287. return ret;
  288. }
  289. static int set_control(AVFilterContext *ctx, unsigned long port, LADSPA_Data value)
  290. {
  291. LADSPAContext *s = ctx->priv;
  292. const char *label = s->desc->Label;
  293. LADSPA_PortRangeHint *h = (LADSPA_PortRangeHint *)s->desc->PortRangeHints +
  294. s->icmap[port];
  295. if (port >= s->nb_inputcontrols) {
  296. av_log(ctx, AV_LOG_ERROR, "Control c%ld is out of range [0 - %lu].\n",
  297. port, s->nb_inputcontrols);
  298. return AVERROR(EINVAL);
  299. }
  300. if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor) &&
  301. value < h->LowerBound) {
  302. av_log(ctx, AV_LOG_ERROR,
  303. "%s: input control c%ld is below lower boundary of %0.4f.\n",
  304. label, port, h->LowerBound);
  305. return AVERROR(EINVAL);
  306. }
  307. if (LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor) &&
  308. value > h->UpperBound) {
  309. av_log(ctx, AV_LOG_ERROR,
  310. "%s: input control c%ld is above upper boundary of %0.4f.\n",
  311. label, port, h->UpperBound);
  312. return AVERROR(EINVAL);
  313. }
  314. s->ictlv[port] = value;
  315. return 0;
  316. }
  317. static av_cold int init(AVFilterContext *ctx)
  318. {
  319. LADSPAContext *s = ctx->priv;
  320. LADSPA_Descriptor_Function descriptor_fn;
  321. const LADSPA_Descriptor *desc;
  322. LADSPA_PortDescriptor pd;
  323. AVFilterPad pad = { NULL };
  324. char *p, *arg, *saveptr = NULL;
  325. unsigned long nb_ports;
  326. int i;
  327. if (!s->dl_name) {
  328. av_log(ctx, AV_LOG_ERROR, "No plugin name provided\n");
  329. return AVERROR(EINVAL);
  330. }
  331. if (s->dl_name[0] == '/' || s->dl_name[0] == '.') {
  332. // argument is a path
  333. s->dl_handle = dlopen(s->dl_name, RTLD_LOCAL|RTLD_NOW);
  334. } else {
  335. // argument is a shared object name
  336. char *paths = av_strdup(getenv("LADSPA_PATH"));
  337. const char *separator = ":";
  338. if (paths) {
  339. p = paths;
  340. while ((arg = av_strtok(p, separator, &saveptr)) && !s->dl_handle) {
  341. s->dl_handle = try_load(arg, s->dl_name);
  342. p = NULL;
  343. }
  344. }
  345. av_free(paths);
  346. if (!s->dl_handle && (paths = av_asprintf("%s/.ladspa/lib", getenv("HOME")))) {
  347. s->dl_handle = try_load(paths, s->dl_name);
  348. av_free(paths);
  349. }
  350. if (!s->dl_handle)
  351. s->dl_handle = try_load("/usr/local/lib/ladspa", s->dl_name);
  352. if (!s->dl_handle)
  353. s->dl_handle = try_load("/usr/lib/ladspa", s->dl_name);
  354. }
  355. if (!s->dl_handle) {
  356. av_log(ctx, AV_LOG_ERROR, "Failed to load '%s'\n", s->dl_name);
  357. return AVERROR(EINVAL);
  358. }
  359. descriptor_fn = dlsym(s->dl_handle, "ladspa_descriptor");
  360. if (!descriptor_fn) {
  361. av_log(ctx, AV_LOG_ERROR, "Could not find ladspa_descriptor: %s\n", dlerror());
  362. return AVERROR(EINVAL);
  363. }
  364. // Find the requested plugin, or list plugins
  365. if (!s->plugin) {
  366. av_log(ctx, AV_LOG_INFO, "The '%s' library contains the following plugins:\n", s->dl_name);
  367. av_log(ctx, AV_LOG_INFO, "I = Input Channels\n");
  368. av_log(ctx, AV_LOG_INFO, "O = Output Channels\n");
  369. av_log(ctx, AV_LOG_INFO, "I:O %-25s %s\n", "Plugin", "Description");
  370. av_log(ctx, AV_LOG_INFO, "\n");
  371. for (i = 0; desc = descriptor_fn(i); i++) {
  372. unsigned long inputs = 0, outputs = 0;
  373. count_ports(desc, &inputs, &outputs);
  374. av_log(ctx, AV_LOG_INFO, "%lu:%lu %-25s %s\n", inputs, outputs, desc->Label,
  375. (char *)av_x_if_null(desc->Name, "?"));
  376. av_log(ctx, AV_LOG_VERBOSE, "Maker: %s\n",
  377. (char *)av_x_if_null(desc->Maker, "?"));
  378. av_log(ctx, AV_LOG_VERBOSE, "Copyright: %s\n",
  379. (char *)av_x_if_null(desc->Copyright, "?"));
  380. }
  381. return AVERROR_EXIT;
  382. } else {
  383. for (i = 0;; i++) {
  384. desc = descriptor_fn(i);
  385. if (!desc) {
  386. av_log(ctx, AV_LOG_ERROR, "Could not find plugin: %s\n", s->plugin);
  387. return AVERROR(EINVAL);
  388. }
  389. if (desc->Label && !strcmp(desc->Label, s->plugin))
  390. break;
  391. }
  392. }
  393. s->desc = desc;
  394. nb_ports = desc->PortCount;
  395. s->ipmap = av_calloc(nb_ports, sizeof(*s->ipmap));
  396. s->opmap = av_calloc(nb_ports, sizeof(*s->opmap));
  397. s->icmap = av_calloc(nb_ports, sizeof(*s->icmap));
  398. s->ocmap = av_calloc(nb_ports, sizeof(*s->ocmap));
  399. s->ictlv = av_calloc(nb_ports, sizeof(*s->ictlv));
  400. s->octlv = av_calloc(nb_ports, sizeof(*s->octlv));
  401. s->ctl_needs_value = av_calloc(nb_ports, sizeof(*s->ctl_needs_value));
  402. if (!s->ipmap || !s->opmap || !s->icmap ||
  403. !s->ocmap || !s->ictlv || !s->octlv || !s->ctl_needs_value)
  404. return AVERROR(ENOMEM);
  405. for (i = 0; i < nb_ports; i++) {
  406. pd = desc->PortDescriptors[i];
  407. if (LADSPA_IS_PORT_AUDIO(pd)) {
  408. if (LADSPA_IS_PORT_INPUT(pd)) {
  409. s->ipmap[s->nb_inputs] = i;
  410. s->nb_inputs++;
  411. } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
  412. s->opmap[s->nb_outputs] = i;
  413. s->nb_outputs++;
  414. }
  415. } else if (LADSPA_IS_PORT_CONTROL(pd)) {
  416. if (LADSPA_IS_PORT_INPUT(pd)) {
  417. s->icmap[s->nb_inputcontrols] = i;
  418. if (LADSPA_IS_HINT_HAS_DEFAULT(desc->PortRangeHints[i].HintDescriptor))
  419. set_default_ctl_value(s, s->nb_inputcontrols, s->icmap, s->ictlv);
  420. else
  421. s->ctl_needs_value[s->nb_inputcontrols] = 1;
  422. s->nb_inputcontrols++;
  423. } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
  424. s->ocmap[s->nb_outputcontrols] = i;
  425. s->nb_outputcontrols++;
  426. }
  427. }
  428. }
  429. // List Control Ports if "help" is specified
  430. if (s->options && !strcmp(s->options, "help")) {
  431. if (!s->nb_inputcontrols) {
  432. av_log(ctx, AV_LOG_INFO,
  433. "The '%s' plugin does not have any input controls.\n",
  434. desc->Label);
  435. } else {
  436. av_log(ctx, AV_LOG_INFO,
  437. "The '%s' plugin has the following input controls:\n",
  438. desc->Label);
  439. for (i = 0; i < s->nb_inputcontrols; i++)
  440. print_ctl_info(ctx, AV_LOG_INFO, s, i, s->icmap, s->ictlv, 0);
  441. }
  442. return AVERROR_EXIT;
  443. }
  444. // Parse control parameters
  445. p = s->options;
  446. while (s->options) {
  447. LADSPA_Data val;
  448. int ret;
  449. if (!(arg = av_strtok(p, "|", &saveptr)))
  450. break;
  451. p = NULL;
  452. if (sscanf(arg, "c%d=%f", &i, &val) != 2) {
  453. av_log(ctx, AV_LOG_ERROR, "Invalid syntax.\n");
  454. return AVERROR(EINVAL);
  455. }
  456. if ((ret = set_control(ctx, i, val)) < 0)
  457. return ret;
  458. s->ctl_needs_value[i] = 0;
  459. }
  460. // Check if any controls are not set
  461. for (i = 0; i < s->nb_inputcontrols; i++) {
  462. if (s->ctl_needs_value[i]) {
  463. av_log(ctx, AV_LOG_ERROR, "Control c%d must be set.\n", i);
  464. print_ctl_info(ctx, AV_LOG_ERROR, s, i, s->icmap, s->ictlv, 0);
  465. return AVERROR(EINVAL);
  466. }
  467. }
  468. pad.type = AVMEDIA_TYPE_AUDIO;
  469. if (s->nb_inputs) {
  470. pad.name = av_asprintf("in0:%s%lu", desc->Label, s->nb_inputs);
  471. if (!pad.name)
  472. return AVERROR(ENOMEM);
  473. pad.filter_frame = filter_frame;
  474. pad.config_props = config_input;
  475. if (ff_insert_inpad(ctx, ctx->nb_inputs, &pad) < 0) {
  476. av_freep(&pad.name);
  477. return AVERROR(ENOMEM);
  478. }
  479. }
  480. av_log(ctx, AV_LOG_DEBUG, "ports: %lu\n", nb_ports);
  481. av_log(ctx, AV_LOG_DEBUG, "inputs: %lu outputs: %lu\n",
  482. s->nb_inputs, s->nb_outputs);
  483. av_log(ctx, AV_LOG_DEBUG, "input controls: %lu output controls: %lu\n",
  484. s->nb_inputcontrols, s->nb_outputcontrols);
  485. return 0;
  486. }
  487. static int query_formats(AVFilterContext *ctx)
  488. {
  489. LADSPAContext *s = ctx->priv;
  490. AVFilterFormats *formats;
  491. AVFilterChannelLayouts *layouts;
  492. static const enum AVSampleFormat sample_fmts[] = {
  493. AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
  494. formats = ff_make_format_list(sample_fmts);
  495. if (!formats)
  496. return AVERROR(ENOMEM);
  497. ff_set_common_formats(ctx, formats);
  498. if (s->nb_inputs) {
  499. formats = ff_all_samplerates();
  500. if (!formats)
  501. return AVERROR(ENOMEM);
  502. ff_set_common_samplerates(ctx, formats);
  503. } else {
  504. int sample_rates[] = { s->sample_rate, -1 };
  505. ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
  506. }
  507. if (s->nb_inputs == 1 && s->nb_outputs == 1) {
  508. // We will instantiate multiple LADSPA_Handle, one over each channel
  509. layouts = ff_all_channel_layouts();
  510. if (!layouts)
  511. return AVERROR(ENOMEM);
  512. ff_set_common_channel_layouts(ctx, layouts);
  513. } else if (s->nb_inputs == 2 && s->nb_outputs == 2) {
  514. layouts = NULL;
  515. ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO);
  516. ff_set_common_channel_layouts(ctx, layouts);
  517. } else {
  518. AVFilterLink *outlink = ctx->outputs[0];
  519. if (s->nb_inputs >= 1) {
  520. AVFilterLink *inlink = ctx->inputs[0];
  521. int64_t inlayout = FF_COUNT2LAYOUT(s->nb_inputs);
  522. layouts = NULL;
  523. ff_add_channel_layout(&layouts, inlayout);
  524. ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
  525. if (!s->nb_outputs)
  526. ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
  527. }
  528. if (s->nb_outputs >= 1) {
  529. int64_t outlayout = FF_COUNT2LAYOUT(s->nb_outputs);
  530. layouts = NULL;
  531. ff_add_channel_layout(&layouts, outlayout);
  532. ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
  533. }
  534. }
  535. return 0;
  536. }
  537. static av_cold void uninit(AVFilterContext *ctx)
  538. {
  539. LADSPAContext *s = ctx->priv;
  540. int i;
  541. for (i = 0; i < s->nb_handles; i++) {
  542. if (s->desc->deactivate)
  543. s->desc->deactivate(s->handles[i]);
  544. if (s->desc->cleanup)
  545. s->desc->cleanup(s->handles[i]);
  546. }
  547. if (s->dl_handle)
  548. dlclose(s->dl_handle);
  549. av_freep(&s->ipmap);
  550. av_freep(&s->opmap);
  551. av_freep(&s->icmap);
  552. av_freep(&s->ocmap);
  553. av_freep(&s->ictlv);
  554. av_freep(&s->octlv);
  555. av_freep(&s->handles);
  556. av_freep(&s->ctl_needs_value);
  557. if (ctx->nb_inputs)
  558. av_freep(&ctx->input_pads[0].name);
  559. }
  560. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  561. char *res, int res_len, int flags)
  562. {
  563. LADSPA_Data value;
  564. unsigned long port;
  565. if (sscanf(cmd, "c%ld", &port) + sscanf(args, "%f", &value) != 2)
  566. return AVERROR(EINVAL);
  567. return set_control(ctx, port, value);
  568. }
  569. static const AVFilterPad ladspa_outputs[] = {
  570. {
  571. .name = "default",
  572. .type = AVMEDIA_TYPE_AUDIO,
  573. .config_props = config_output,
  574. .request_frame = request_frame,
  575. },
  576. { NULL }
  577. };
  578. AVFilter ff_af_ladspa = {
  579. .name = "ladspa",
  580. .description = NULL_IF_CONFIG_SMALL("Apply LADSPA effect."),
  581. .priv_size = sizeof(LADSPAContext),
  582. .priv_class = &ladspa_class,
  583. .init = init,
  584. .uninit = uninit,
  585. .query_formats = query_formats,
  586. .process_command = process_command,
  587. .inputs = 0,
  588. .outputs = ladspa_outputs,
  589. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  590. };