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.

674 lines
22KB

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