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.

743 lines
24KB

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