[FFmpeg-devel] [PATCH] avfilter: add ladspa wrapper filter

Stefano Sabatini stefasab at gmail.com
Sat Sep 21 23:10:20 CEST 2013


On date Saturday 2013-09-21 12:45:10 +0000, Paul B Mahol encoded:
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
>  configure                |   4 +
>  doc/filters.texi         |  82 ++++++
>  libavfilter/Makefile     |   1 +
>  libavfilter/af_ladspa.c  | 643 +++++++++++++++++++++++++++++++++++++++++++++++
>  libavfilter/allfilters.c |   1 +
>  5 files changed, 731 insertions(+)
>  create mode 100644 libavfilter/af_ladspa.c
> 
> diff --git a/configure b/configure
> index a219c26..f25f7dc 100755
> --- a/configure
> +++ b/configure
> @@ -193,6 +193,7 @@ External library support:
>    --enable-frei0r          enable frei0r video filtering
>    --enable-gnutls          enable gnutls [no]
>    --disable-iconv          disable iconv [autodetect]
> +  --enable-ladspa          enable LADSPA audio filtering
>    --enable-libaacplus      enable AAC+ encoding via libaacplus [no]
>    --enable-libass          enable libass subtitles rendering [no]
>    --enable-libbluray       enable BluRay reading using libbluray [no]
> @@ -1162,6 +1163,7 @@ EXTERNAL_LIBRARY_LIST="
>      frei0r
>      gnutls
>      iconv
> +    ladspa
>      libaacplus
>      libass
>      libbluray
> @@ -2210,6 +2212,7 @@ histeq_filter_deps="gpl"
>  hqdn3d_filter_deps="gpl"
>  interlace_filter_deps="gpl"
>  kerndeint_filter_deps="gpl"
> +ladspa_filter_deps="ladspa dlopen"
>  mcdeint_filter_deps="avcodec gpl"
>  movie_filter_deps="avcodec avformat"
>  mp_filter_deps="gpl avcodec swscale inline_asm"
> @@ -4210,6 +4213,7 @@ enabled avisynth          && { { check_lib2 "windows.h" LoadLibrary; } ||
>  enabled fontconfig        && require_pkg_config fontconfig "fontconfig/fontconfig.h" FcInit
>  enabled frei0r            && { check_header frei0r.h || die "ERROR: frei0r.h header not found"; }
>  enabled gnutls            && require_pkg_config gnutls gnutls/gnutls.h gnutls_global_init
> +enabled ladspa            && { check_header ladspa.h || die "ERROR: ladspa.h header not found"; }
>  enabled libiec61883       && require libiec61883 libiec61883/iec61883.h iec61883_cmp_connect -lraw1394 -lavc1394 -lrom1394 -liec61883
>  enabled libaacplus        && require "libaacplus >= 2.0.0" aacplus.h aacplusEncOpen -laacplus
>  enabled libass            && require_pkg_config libass ass/ass.h ass_library_init
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 6575454..631202d 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -1400,6 +1400,88 @@ ffmpeg -i fl -i fr -i fc -i sl -i sr -i lfe -filter_complex
>  out
>  @end example
>  
> + at section ladspa
> +
> +Load a LADSPA (Linux Audio Developer's Simple Plugin API) plugin.
> +
> + at table @option
> + at item file, f
> +Specifies the name of LADSPA plugin library to load. If the environment
> +variable @env{LADSPA_PATH} is defined, the LADSPA plugin is searched in
> +each one of the directories specified by the colon separated list in
> + at env{LADSPA_PATH}, otherwise in the standard LADSPA paths, which are in
> +this order: @file{HOME/.ladspa/lib/}, @file{/usr/local/lib/ladspa/},
> + at file{/usr/lib/ladspa/}.
> +
> + at item plugin, p
> +Specifies the plugin within the library. Some libraries contain only
> +one plugin, but others contain many of them. If this is not set filter
> +will list all available plugins within the specified library.
> +

> + at item controls, c
> +Set the '|' separated list of controls which are zero or more floating point
> +values that determine the behavior of the loaded plugin (for example delay,
> +threshold or gain).
> +If @code{controls} is set to @value{help}, all available controls and their
> +valid ranges are printed.

Here the syntax should be specified more extensively.

> +
> + at item sample_rate, s
> +Specify the sample rate, default to 44100. Only used if plugin have
> +zero inputs.
> +
> + at item nb_samples, n
> +Set the number of samples per channel per each output frame, default
> +is 1024. Only used if plugin have zero inputs.
> +

> + at item duration, d
> +Set the minimum duration of the sourced audio. See the function
> + at code{av_parse_time()} for the accepted format.

Nit: you could mention the utils manual section dealing with duration
specification.

> +Note that the resulting duration may be greater than
> +the specified duration, as the generated audio is always cut at the
> +end of a complete frame.
> +If not specified, or the expressed duration is negative, the audio is
> +supposed to be generated forever.
> +Only used if plugin have zero inputs.
> +
> + at end table
> +
> + at subsection Examples
> +
> + at itemize
> + at item
> +List all available plugins within amp (LADSPA example plugin) library:
> + at example
> +ladspa=file=amp
> + at end example
> +
> + at item
> +List all available controls and their valid ranges for @code{vcf_notch}
> +plugin from @code{vcf} library:
> + at example
> +ladspa=f=vcf:p=vcf_notch:c=help
> + at end example
> +
> + at item
> +Simulate low quality audio equipment using Computer Music Toolkit (CMT)
> +plugin library:
> + at example
> +ladspa=file=cmt:plugin=lofi:controls=c0=22|c1=12|c2=12
> + at end example
> +
> + at item
> +Add reverberation to the audio using TAP-plugins
> +(Tom's Audio Processing plugins):
> + at example
> +ladspa=file=tap_reverb:tap_reverb
> + at end example
> +
> + at item
> +Generate white noise, with 0.2 amplitude:
> + at example
> +file=cmt:noise_source_white:c=c0=.2'
> + at end example
> + at end itemize
> +
>  @section lowpass
>  
>  Apply a low-pass filter with 3dB point frequency.
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index e5afba4..7e6d901 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -91,6 +91,7 @@ OBJS-$(CONFIG_EBUR128_FILTER)                += f_ebur128.o
>  OBJS-$(CONFIG_EQUALIZER_FILTER)              += af_biquads.o
>  OBJS-$(CONFIG_HIGHPASS_FILTER)               += af_biquads.o
>  OBJS-$(CONFIG_JOIN_FILTER)                   += af_join.o
> +OBJS-$(CONFIG_LADSPA_FILTER)                 += af_ladspa.o
>  OBJS-$(CONFIG_LOWPASS_FILTER)                += af_biquads.o
>  OBJS-$(CONFIG_PAN_FILTER)                    += af_pan.o
>  OBJS-$(CONFIG_RESAMPLE_FILTER)               += af_resample.o
> diff --git a/libavfilter/af_ladspa.c b/libavfilter/af_ladspa.c
> new file mode 100644
> index 0000000..31ff083
> --- /dev/null
> +++ b/libavfilter/af_ladspa.c
> @@ -0,0 +1,643 @@
> +/*
> + * Copyright (c) 2013 Paul B Mahol
> + * Copyright (c) 2011 Mina Nagy Zaki
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +/**
> + * @file
> + * LADSPA wrapper
> + */
> +
> +#include <dlfcn.h>
> +#include <ladspa.h>
> +#include "libavutil/avstring.h"
> +#include "libavutil/channel_layout.h"
> +#include "libavutil/opt.h"
> +#include "audio.h"
> +#include "avfilter.h"
> +#include "internal.h"
> +
> +typedef struct LADSPAContext {
> +    const AVClass *class;
> +    char *dl_name;
> +    char *plugin;
> +    char *options;
> +    void *dl_handle;
> +
> +    int status;
> +    int activated;
> +
> +    unsigned long nb_ports;
> +
> +    unsigned long nb_inputs;
> +    unsigned long *ipmap;
> +
> +    unsigned long nb_inputcontrols;
> +    unsigned long *icmap;
> +    LADSPA_Data *ictlv;
> +
> +    unsigned long nb_outputs;
> +    unsigned long *opmap;
> +
> +    unsigned long nb_outputcontrols;
> +    unsigned long *ocmap;
> +    LADSPA_Data *octlv;
> +
> +    const LADSPA_Descriptor *desc;

> +    int *ctlnv;

obfuscated name

> +    int nb_handles;
> +    LADSPA_Handle *handles;
> +
> +    int sample_rate;
> +    int nb_samples;
> +    int64_t pts;
> +    int64_t duration;
> +} LADSPAContext;

Please document some of these, especially the ones with weird
acronyms.

> +#define OFFSET(x) offsetof(LADSPAContext, x)
> +#define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
> +static const AVOption ladspa_options[] = {
> +    { "file", "set library name or full path", OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
> +    { "f",    "set library name or full path", OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
> +    { "plugin", "set plugin name", OFFSET(plugin), AV_OPT_TYPE_STRING, .flags = FLAGS },
> +    { "p",      "set plugin name", OFFSET(plugin), AV_OPT_TYPE_STRING, .flags = FLAGS },
> +    { "controls", "set plugin options", OFFSET(options), AV_OPT_TYPE_STRING, .flags = FLAGS },
> +    { "c",        "set plugin options", OFFSET(options), AV_OPT_TYPE_STRING, .flags = FLAGS },
> +    { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT32_MAX, FLAGS },
> +    { "s",           "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT32_MAX, FLAGS },
> +    { "nb_samples",  "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
> +    { "n",           "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
> +    { "duration",    "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
> +    { "d",           "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
> +    { NULL }
> +};
> +
> +AVFILTER_DEFINE_CLASS(ladspa);
> +
> +static int filter_frame(AVFilterLink *inlink, AVFrame *in)
> +{
> +    AVFilterContext *ctx = inlink->dst;
> +    LADSPAContext *s = ctx->priv;
> +    AVFrame *out;
> +    int i, h, ret = 0;
> +
> +    if (av_frame_is_writable(in) && s->nb_inputs == s->nb_outputs &&
> +        !(s->desc->Properties & LADSPA_PROPERTY_INPLACE_BROKEN)) {
> +        out = in;
> +    } else if (s->nb_outputs) {
> +        out = ff_get_audio_buffer(ctx->outputs[0], in->nb_samples);
> +        if (!out) {
> +            av_frame_free(&in);
> +            return AVERROR(ENOMEM);
> +        }
> +        av_frame_copy_props(out, in);
> +    } else {
> +        out = NULL;
> +    }
> +
> +    for (h = 0; h < s->nb_handles; h++) {
> +        for (i = 0; i < s->nb_inputs; i++) {
> +            s->desc->connect_port(s->handles[h], s->ipmap[i],
> +                                  (LADSPA_Data*)in->extended_data[i]);
> +        }
> +
> +        for (i = 0; i < s->nb_outputs; i++) {
> +            s->desc->connect_port(s->handles[h], s->opmap[i],
> +                                  (LADSPA_Data*)out->extended_data[i]);
> +        }
> +
> +        s->desc->run(s->handles[h], in->nb_samples);
> +    }
> +
> +    if (out != in)
> +        av_frame_free(&in);
> +    if (out)
> +        ret = ff_filter_frame(ctx->outputs[0], out);
> +    return ret;
> +}
> +
> +static int request_frame(AVFilterLink *outlink)
> +{
> +    AVFilterContext *ctx = outlink->src;
> +    LADSPAContext *s = ctx->priv;
> +    AVFrame *out;
> +    int64_t t = av_rescale(s->pts, AV_TIME_BASE, s->sample_rate);
> +    int i;
> +
> +    if (s->duration >= 0 && t >= s->duration)
> +        return AVERROR_EOF;
> +
> +    out = ff_get_audio_buffer(outlink, s->nb_samples);
> +    if (!out)
> +        return AVERROR(ENOMEM);
> +
> +    for (i = 0; i < s->nb_outputs; i++)
> +        s->desc->connect_port(s->handles[0], s->opmap[i],
> +                (LADSPA_Data*)out->extended_data[i]);
> +
> +    s->desc->run(s->handles[0], s->nb_samples);
> +
> +    out->pts         = s->pts;
> +    out->sample_rate = s->sample_rate;
> +    s->pts          += s->nb_samples;
> +
> +    return ff_filter_frame(outlink, out);
> +}
> +

> +static void *try_load(const char *dir, const char *soname)

Nit: return int for more robust feedback/error propagation

> +{
> +    char *path = av_asprintf("%s/%s.so", dir, soname);
> +    if (!path)
> +        return NULL;
> +    return dlopen(path, RTLD_NOW);
> +}
> +

> +static void set_default_ctl_value(LADSPAContext *s, int ctl)
> +{
> +    const LADSPA_PortRangeHint *h = s->desc->PortRangeHints + s->icmap[ctl];
> +    const LADSPA_Data lower = h->LowerBound;
> +    const LADSPA_Data upper = h->UpperBound;
> +
> +    if (LADSPA_IS_HINT_DEFAULT_MINIMUM(h->HintDescriptor)) {
> +        s->ictlv[ctl] = lower;
> +    } else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(h->HintDescriptor)) {
> +        s->ictlv[ctl] = upper;
> +    } else if (LADSPA_IS_HINT_DEFAULT_0(h->HintDescriptor)) {
> +        s->ictlv[ctl] = 0.0;
> +    } else if (LADSPA_IS_HINT_DEFAULT_1(h->HintDescriptor)) {
> +        s->ictlv[ctl] = 1.0;
> +    } else if (LADSPA_IS_HINT_DEFAULT_100(h->HintDescriptor)) {
> +        s->ictlv[ctl] = 100.0;
> +    } else if (LADSPA_IS_HINT_DEFAULT_440(h->HintDescriptor)) {
> +        s->ictlv[ctl] = 440.0;
> +    } else if (LADSPA_IS_HINT_DEFAULT_LOW(h->HintDescriptor)) {
> +        if (LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor))
> +            s->ictlv[ctl] = exp(log(lower) * 0.75 + log(upper) * 0.25);
> +        else
> +            s->ictlv[ctl] = lower * 0.75 + upper * 0.25;
> +    } else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(h->HintDescriptor)) {
> +        if (LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor))
> +            s->ictlv[ctl] = exp(log(lower) * 0.5 + log(upper) * 0.5);
> +        else
> +            s->ictlv[ctl] = lower * 0.5 + upper * 0.5;
> +    } else if (LADSPA_IS_HINT_DEFAULT_HIGH(h->HintDescriptor)) {
> +        if (LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor))
> +            s->ictlv[ctl] = exp(log(lower) * 0.25 + log(upper) * 0.75);
> +        else
> +            s->ictlv[ctl] = lower * 0.25 + upper * 0.75;
> +    }
> +}

Nit: macros for increased readability

> +
> +static void print_ctl_info(AVFilterContext *ctx, int level,
> +                           LADSPAContext *s, int ctl)
> +{
> +    const LADSPA_PortRangeHint *h = s->desc->PortRangeHints + s->icmap[ctl];
> +    av_log(ctx, level, "c%i: %-15s [", ctl,
> +           s->desc->PortNames[s->icmap[ctl]]);
> +    if (LADSPA_IS_HINT_TOGGLED(h->HintDescriptor)) {
> +        av_log(ctx, level, "toggled (1 or 0)");
> +
> +        if (LADSPA_IS_HINT_HAS_DEFAULT(h->HintDescriptor))
> +            av_log(ctx, level, " (default %i)", (int)s->ictlv[ctl]);
> +    } else {
> +        if (LADSPA_IS_HINT_INTEGER(h->HintDescriptor)) {
> +            av_log(ctx, level, "<int>");
> +
> +            if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor))
> +                av_log(ctx, level, ", min: %i", (int)h->LowerBound);
> +
> +            if (LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor))
> +                av_log(ctx, level, ", max: %i", (int)h->UpperBound);
> +
> +            if (LADSPA_IS_HINT_HAS_DEFAULT(h->HintDescriptor))
> +                av_log(ctx, level, " (default %d)", (int)s->ictlv[ctl]);
> +        } else {
> +            av_log(ctx, level, "<float>");
> +
> +            if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor))
> +                av_log(ctx, level, ", min: %f", h->LowerBound);
> +
> +            if (LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor))
> +                av_log(ctx, level, ", max: %f", h->UpperBound);
> +
> +            if (LADSPA_IS_HINT_HAS_DEFAULT(h->HintDescriptor))
> +                av_log(ctx, level, " (default %f)", s->ictlv[ctl]);
> +        }
> +
> +        if (LADSPA_IS_HINT_SAMPLE_RATE(h->HintDescriptor))
> +            av_log(ctx, level, ", multiple of sample rate");
> +
> +        if (LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor))
> +            av_log(ctx, level, ", logarithmic scale");
> +    }
> +
> +    av_log(ctx, level, "]\n");
> +}
> +
> +static int connect_ports(AVFilterContext *ctx, AVFilterLink *link)
> +{
> +    LADSPAContext *s = ctx->priv;
> +    int i, j;
> +
> +    s->nb_handles = s->nb_inputs == 1 && s->nb_outputs == 1 ? link->channels : 1;
> +    s->handles    = av_calloc(s->nb_handles, sizeof(*s->handles));
> +    if (!s->handles)
> +        return AVERROR(ENOMEM);
> +
> +    for (i = 0; i < s->nb_handles; i++) {
> +        s->handles[i] = s->desc->instantiate(s->desc, link->sample_rate);
> +        if (!s->handles[i]) {
> +            av_log(ctx, AV_LOG_ERROR, "Could not instantiate plugin.\n");

> +            return AVERROR(EINVAL);

AVERROR_EXTERNAL

> +        }
> +
> +        // Connect the input control ports
> +        for (j = 0; j < s->nb_inputcontrols; j++)
> +            s->desc->connect_port(s->handles[i], s->icmap[j], s->ictlv + j);
> +
> +        // Connect the output control ports
> +        for (j = 0; j < s->nb_outputcontrols; j++)
> +            s->desc->connect_port(s->handles[i], s->ocmap[j], &s->octlv[j]);
> +
> +        if (s->desc->activate)
> +            s->desc->activate(s->handles[i]);
> +    }
> +
> +    av_log(ctx, AV_LOG_DEBUG, "handles: %d\n", s->nb_handles);
> +
> +    return 0;
> +}
> +
> +static int config_input(AVFilterLink *inlink)
> +{
> +    AVFilterContext *ctx = inlink->dst;
> +
> +    return connect_ports(ctx, inlink);
> +}
> +
> +static int config_output(AVFilterLink *outlink)
> +{
> +    AVFilterContext *ctx = outlink->src;
> +    LADSPAContext *s = ctx->priv;
> +    int ret;
> +
> +    if (s->nb_inputs) {
> +        AVFilterLink *inlink = ctx->inputs[0];
> +
> +        outlink->format      = inlink->format;
> +        outlink->sample_rate = inlink->sample_rate;
> +
> +        ret = 0;
> +    } else {
> +        outlink->time_base   = (AVRational){1, s->sample_rate};
> +        outlink->sample_rate = s->sample_rate;
> +
> +        ret = connect_ports(ctx, outlink);
> +    }
> +    return ret;
> +}
> +
> +static void count_ports(const LADSPA_Descriptor *desc,
> +                        unsigned long *nb_inputs, unsigned long *nb_outputs)
> +{
> +    LADSPA_PortDescriptor pd;
> +    int i;
> +
> +    for (i = 0; i < desc->PortCount; i++) {
> +        pd = desc->PortDescriptors[i];
> +
> +        if (LADSPA_IS_PORT_AUDIO(pd)) {
> +            if (LADSPA_IS_PORT_INPUT(pd)) {
> +                (*nb_inputs)++;
> +            } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
> +                (*nb_outputs)++;
> +            }
> +        }
> +    }
> +}
> +

> +static av_cold int init(AVFilterContext *ctx)

Note: I consider much more readable when the functions are defined in
the same order they are called (init, query_default, config, filter,
etc.).

> +{
> +    LADSPAContext *s = ctx->priv;
> +    LADSPA_Descriptor_Function descriptor_fn;
> +    const LADSPA_Descriptor *desc;
> +    LADSPA_PortDescriptor pd;
> +    AVFilterPad pad = { NULL };
> +    char *arg, *ptr, *p;
> +    int i;
> +
> +    if (!s->dl_name) {
> +        av_log(ctx, AV_LOG_ERROR, "No plugin name provided.\n");

nit: skip ending "." in non complete sentence

> +        return AVERROR(EINVAL);
> +    }
> +
> +    if (s->dl_name[0] == '/' || s->dl_name[0] == '.') {
> +        // argument is a path
> +        s->dl_handle = dlopen(s->dl_name, RTLD_NOW|RTLD_LOCAL);
> +    } else {
> +        // argument is a shared object name
> +        char *paths = av_strdup(getenv("LADSPA_PATH"));

> +        const char *separator = ":";

I wonder if we should use ";" in windows (check frei0r code)
> +
> +        arg = NULL;
> +        if (paths) {

> +            arg = av_strtok(paths, separator, &ptr);

Why do you skip the first path?

> +            while ((arg = av_strtok(NULL, separator, &ptr)) && !s->dl_handle)
> +                s->dl_handle = try_load(arg, s->dl_name);
> +        }
> +
> +        av_free(paths);
> +        if (!s->dl_handle && (paths = av_asprintf("%s/.ladspa/lib", getenv("HOME")))) {
> +            s->dl_handle = try_load(paths, s->dl_name);
> +            av_free(paths);
> +        }
> +
> +        if (!s->dl_handle)
> +            s->dl_handle = try_load("/usr/local/lib/ladspa", s->dl_name);
> +
> +        if (!s->dl_handle)
> +            s->dl_handle = try_load("/usr/lib/ladspa", s->dl_name);
> +    }
> +    if (!s->dl_handle) {
> +        av_log(ctx, AV_LOG_ERROR, "Failed to load '%s'\n", s->dl_name);
> +        return AVERROR(EINVAL);
> +    }
> +
> +    descriptor_fn = dlsym(s->dl_handle, "ladspa_descriptor");
> +    if (!descriptor_fn) {
> +        av_log(ctx, AV_LOG_ERROR, "Could not find ladspa_descriptor: %s\n", dlerror());
> +        return AVERROR(EINVAL);
> +    }
> +

> +    // Find the requested plugin, or list plugins
> +    if (!s->plugin) {
> +        av_log(ctx, AV_LOG_INFO, "The '%s' library contains the following plugins:\n", s->dl_name);
> +        av_log(ctx, AV_LOG_INFO, "I = Input Channels\n");
> +        av_log(ctx, AV_LOG_INFO, "O = Output Channels\n");
> +        av_log(ctx, AV_LOG_INFO, "I:O %-25s %s\n", "Plugin", "Description");
> +        av_log(ctx, AV_LOG_INFO, "\n");
> +        for (i = 0; desc = descriptor_fn(i); i++) {
> +            unsigned long inputs = 0, outputs = 0;
> +
> +            count_ports(desc, &inputs, &outputs);
> +            av_log(ctx, AV_LOG_INFO, "%lu:%lu %-25s %s\n", inputs, outputs, desc->Label,
> +                                     av_x_if_null(desc->Name, "?"));
> +            av_log(ctx, AV_LOG_VERBOSE, "Maker: %s\n", av_x_if_null(desc->Maker, "?"));
> +            av_log(ctx, AV_LOG_VERBOSE, "Copyright: %s\n", av_x_if_null(desc->Copyright, "?"));
> +        }
> +        return AVERROR_EXIT;

I'd prefer to implement a specific option for this (list_plugins?)

> +    } else {
> +        for (i = 0;; i++) {
> +            desc = descriptor_fn(i);
> +            if (!desc) {
> +                av_log(ctx, AV_LOG_ERROR, "Could not find plugin: %s\n", s->plugin);
> +                return AVERROR(EINVAL);
> +            }
> +
> +            if (desc->Label && !strcmp(desc->Label, s->plugin))
> +                break;
> +        }
> +    }
> +
> +    s->desc     = desc;
> +    s->nb_ports = desc->PortCount;
> +
> +    s->ipmap = av_calloc(s->nb_ports, sizeof(*s->ipmap));
> +    s->opmap = av_calloc(s->nb_ports, sizeof(*s->opmap));
> +    s->icmap = av_calloc(s->nb_ports, sizeof(*s->icmap));
> +    s->ocmap = av_calloc(s->nb_ports, sizeof(*s->ocmap));
> +    s->ictlv = av_calloc(s->nb_ports, sizeof(*s->ictlv));
> +    s->octlv = av_calloc(s->nb_ports, sizeof(*s->octlv));
> +    s->ctlnv = av_calloc(s->nb_ports, sizeof(*s->ctlnv));
> +    if (!s->ipmap || !s->opmap || !s->icmap ||
> +        !s->ocmap || !s->ictlv || !s->octlv || !s->ctlnv)
> +        return AVERROR(ENOMEM);
> +
> +    for (i = 0; i < s->nb_ports; i++) {
> +        pd = desc->PortDescriptors[i];
> +
> +        if (LADSPA_IS_PORT_AUDIO(pd)) {
> +            if (LADSPA_IS_PORT_INPUT(pd)) {
> +                s->ipmap[s->nb_inputs] = i;
> +                s->nb_inputs++;
> +            } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
> +                s->opmap[s->nb_outputs] = i;
> +                s->nb_outputs++;
> +            }
> +        } else if (LADSPA_IS_PORT_CONTROL(pd)) {
> +            if (LADSPA_IS_PORT_INPUT(pd)) {
> +                s->icmap[s->nb_inputcontrols] = i;
> +                if (LADSPA_IS_HINT_HAS_DEFAULT(desc->PortRangeHints[i].HintDescriptor))
> +                    set_default_ctl_value(s, s->nb_inputcontrols);
> +                else
> +                    s->ctlnv[s->nb_inputcontrols] = 1;
> +                s->nb_inputcontrols++;
> +            } else if (LADSPA_IS_PORT_OUTPUT(pd)) {
> +                s->ocmap[s->nb_outputcontrols] = i;
> +                s->nb_outputcontrols++;
> +            }
> +        }
> +    }
> +
> +    // List Control Ports if "help" is specified
> +    if (s->options && !strcmp(s->options, "help")) {
> +        if (!s->nb_inputcontrols) {
> +            av_log(ctx, AV_LOG_INFO,
> +                   "The '%s' plugin does not have any input controls.\n",
> +                   desc->Label);
> +        } else {
> +            av_log(ctx, AV_LOG_INFO,
> +                   "The '%s' plugin has the following input controls:\n",
> +                   desc->Label);
> +            for (i = 0; i < s->nb_inputcontrols; i++)
> +                print_ctl_info(ctx, AV_LOG_INFO, s, i);
> +        }
> +        return AVERROR_EXIT;
> +    }
> +
> +    // Parse control parameters
> +    p = s->options;
> +    while (s->options) {
> +        LADSPA_PortRangeHint *h;
> +        LADSPA_Data val;
> +
> +        if (!(arg = av_strtok(p, "|", &ptr)))
> +            break;
> +        p = NULL;
> +

> +        if (sscanf(arg, "c%d=%f", &i, &val) != 2 ||
> +            i < 0 || i >= s->nb_inputcontrols) {

Missing error message

> +            return AVERROR(EINVAL);
> +        }
> +
> +        h = (LADSPA_PortRangeHint *)s->desc->PortRangeHints + s->icmap[i];
> +        s->ictlv[i] = val;
> +        s->ctlnv[i] = 0;
> +        if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor) &&
> +                val < h->LowerBound) {
> +            av_log(ctx, AV_LOG_ERROR,
> +                   "%s: input control #%d is below lower boundary of %0.4f.\n",
> +                   s->desc->Label, i, h->LowerBound);
> +            return AVERROR(EINVAL);
> +        }
> +        if (LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor) &&
> +                val > h->UpperBound) {
> +            av_log(ctx, AV_LOG_ERROR,
> +                   "%s: input control #%d is above upper boundary of %0.4f.\n",
> +                   s->desc->Label, i, h->UpperBound);
> +            return AVERROR(EINVAL);
> +        }
> +    }
> +
> +    // Check if any controls are not set
> +    for (i = 0; i < s->nb_inputcontrols; i++) {
> +        if (s->ctlnv[i]) {
> +            av_log(ctx, AV_LOG_ERROR, "Control c%i must be set.\n", i);
> +            print_ctl_info(ctx, AV_LOG_ERROR, s, i);
> +            return AVERROR(EINVAL);
> +        }
> +    }

Any reason why set only input controls?

> +    pad.type = AVMEDIA_TYPE_AUDIO;
> +
> +    if (s->nb_inputs) {
> +        pad.name = av_asprintf("in0:%s%lu", desc->Label, s->nb_inputs);
> +        if (!pad.name)
> +            return AVERROR(ENOMEM);
> +
> +        pad.filter_frame = filter_frame;
> +        pad.config_props = config_input;

> +        ff_insert_inpad(ctx, ctx->nb_inputs, &pad);

Nit: better to check for return value

> +    }
> +
> +    if (s->nb_outputs) {
> +        pad.name = av_asprintf("out0:%s%lu", desc->Label, s->nb_outputs);
> +        if (!pad.name)
> +            return AVERROR(ENOMEM);
> +
> +        if (!s->nb_inputs)
> +            pad.request_frame = request_frame;
> +        pad.config_props = config_output;

> +        ff_insert_outpad(ctx, ctx->nb_outputs, &pad);

same

> +    }
> +
> +    av_log(ctx, AV_LOG_DEBUG, "ports: %lu\n", s->nb_ports);
> +    av_log(ctx, AV_LOG_DEBUG, "inputs: %lu outputs: %lu\n",
> +                              s->nb_inputs, s->nb_outputs);
> +    av_log(ctx, AV_LOG_DEBUG, "input controls: %lu output controls: %lu\n",
> +                              s->nb_inputcontrols, s->nb_outputcontrols);
> +
> +    return 0;
> +}
> +
> +static int query_formats(AVFilterContext *ctx)
> +{
> +    LADSPAContext *s = ctx->priv;
> +    AVFilterFormats *formats;
> +    AVFilterChannelLayouts *layouts;
> +    static const enum AVSampleFormat sample_fmts[] = {
> +        AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
> +
> +    formats = ff_make_format_list(sample_fmts);
> +    if (!formats)
> +        return AVERROR(ENOMEM);
> +    ff_set_common_formats(ctx, formats);
> +
> +    if (s->nb_inputs) {
> +        formats = ff_all_samplerates();
> +        if (!formats)
> +            return AVERROR(ENOMEM);
> +        ff_set_common_samplerates(ctx, formats);
> +    } else {
> +        int sample_rates[] = { s->sample_rate, -1 };
> +
> +        ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
> +    }
> +
> +    if (s->nb_inputs == 1 && s->nb_outputs == 1) {
> +        // We will instantiate multiple instances, one over each channel
> +        layouts = ff_all_channel_layouts();
> +        if (!layouts)
> +            return AVERROR(ENOMEM);
> +        ff_set_common_channel_layouts(ctx, layouts);
> +    } else {
> +        if (s->nb_inputs >= 1) {
> +            AVFilterLink *inlink = ctx->inputs[0];
> +            int64_t inlayout = FF_COUNT2LAYOUT(s->nb_inputs);
> +
> +            layouts = NULL;
> +            ff_add_channel_layout(&layouts, inlayout);
> +            ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
> +        }
> +
> +        if (s->nb_outputs >= 1) {
> +            AVFilterLink *outlink = ctx->outputs[0];
> +            int64_t outlayout = FF_COUNT2LAYOUT(s->nb_outputs);
> +
> +            layouts = NULL;
> +            ff_add_channel_layout(&layouts, outlayout);
> +            ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
> +        }
> +    }
> +
> +    return 0;
> +}
> +
> +static av_cold void uninit(AVFilterContext *ctx)
> +{
> +    LADSPAContext *s = ctx->priv;
> +    int i;
> +
> +    for (i = 0; i < s->nb_handles; i++) {
> +        if (s->desc->deactivate)
> +            s->desc->deactivate(s->handles[i]);
> +        if (s->desc->cleanup)
> +            s->desc->cleanup(s->handles[i]);
> +    }
> +
> +    if (s->dl_handle)
> +        dlclose(s->dl_handle);
> +
> +    av_freep(&s->ipmap);
> +    av_freep(&s->opmap);
> +    av_freep(&s->icmap);
> +    av_freep(&s->ocmap);
> +    av_freep(&s->ictlv);
> +    av_freep(&s->octlv);
> +    av_freep(&s->ctlnv);
> +    av_freep(&s->handles);
> +
> +    if (ctx->nb_inputs)
> +        av_freep(&ctx->input_pads[0].name);
> +    if (ctx->nb_outputs)
> +        av_freep(&ctx->output_pads[0].name);
> +}
> +
> +AVFilter avfilter_af_ladspa = {
> +    .name          = "ladspa",
> +    .description   = NULL_IF_CONFIG_SMALL("LADSPA plugin loader."),

Apply LADSPA effect/plugin.

[...]
-- 
FFmpeg = Fanciful Faithful Magnificient Portable Evil Gigant


More information about the ffmpeg-devel mailing list