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.

807 lines
26KB

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