[FFmpeg-devel] [PATCH] lavfi/volume: add dynamic expression evaluation
Stefano Sabatini
stefasab at gmail.com
Mon Mar 4 01:54:06 CET 2013
On date Wednesday 2013-02-27 15:21:49 +0100, Clément Bœsch encoded:
> On Sat, Feb 23, 2013 at 01:00:40AM +0100, Stefano Sabatini wrote:
> > TODO: update docs, bump micro
> > ---
> > libavfilter/af_volume.c | 120 ++++++++++++++++++++++++++++++++++++-----------
> > libavfilter/af_volume.h | 20 ++++++++
> > 2 files changed, 113 insertions(+), 27 deletions(-)
> >
> > diff --git a/libavfilter/af_volume.c b/libavfilter/af_volume.c
> > index 5ffa1fe..87898c8 100644
> > --- a/libavfilter/af_volume.c
> > +++ b/libavfilter/af_volume.c
> > @@ -29,6 +29,7 @@
> > #include "libavutil/eval.h"
> > #include "libavutil/float_dsp.h"
> > #include "libavutil/opt.h"
> > +#include "libavutil/time.h"
> > #include "audio.h"
> > #include "avfilter.h"
> > #include "formats.h"
> > @@ -39,13 +40,28 @@ static const char *precision_str[] = {
> > "fixed", "float", "double"
> > };
> >
> > +static const char *const var_names[] = {
> > + "N", ///< frame number (starting at zero)
> > + "NB_CONSUMED_SAMPLES", ///< number of samples consumed by the filter (only audio)
> > + "NB_SAMPLES", ///< number of samples in the current frame (only audio)
> > + "POS", ///< original position in the file of the frame
> > + "PTS", ///< original pts in the file of the frame
> > + "RTC_STARTT", ///< start wallclock (RTC) time in micro seconds
> > + "SAMPLE_RATE", ///< sample rate (only audio)
> > + "STARTPTS", ///< PTS at start of stream
> > + "STARTT", ///< time at start of stream
> > + "T", ///< original time in the file of the frame
> > + "TB", ///< timebase
> > + NULL
> > +};
> > +
> > #define OFFSET(x) offsetof(VolumeContext, x)
> > #define A AV_OPT_FLAG_AUDIO_PARAM
> > #define F AV_OPT_FLAG_FILTERING_PARAM
> >
> > static const AVOption volume_options[] = {
> > - { "volume", "set volume adjustment",
> > - OFFSET(volume), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, 0, 0x7fffff, A|F },
> > + { "volume", "set volume adjustment expression",
> > + OFFSET(volume_expr), AV_OPT_TYPE_STRING, { .str = "1.0" }, .flags = A|F },
> > { "precision", "select mathematical precision",
> > OFFSET(precision), AV_OPT_TYPE_INT, { .i64 = PRECISION_FLOAT }, PRECISION_FIXED, PRECISION_DOUBLE, A|F, "precision" },
> > { "fixed", "select 8-bit fixed-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FIXED }, INT_MIN, INT_MAX, A|F, "precision" },
> > @@ -68,21 +84,25 @@ static av_cold int init(AVFilterContext *ctx, const char *args)
> > if ((ret = av_opt_set_from_string(vol, args, shorthand, "=", ":")) < 0)
> > return ret;
> >
> > - if (vol->precision == PRECISION_FIXED) {
> > - vol->volume_i = (int)(vol->volume * 256 + 0.5);
> > - vol->volume = vol->volume_i / 256.0;
> > - av_log(ctx, AV_LOG_VERBOSE, "volume:(%d/256)(%f)(%1.2fdB) precision:fixed\n",
> > - vol->volume_i, vol->volume, 20.0*log(vol->volume)/M_LN10);
> > - } else {
> > - av_log(ctx, AV_LOG_VERBOSE, "volume:(%f)(%1.2fdB) precision:%s\n",
> > - vol->volume, 20.0*log(vol->volume)/M_LN10,
> > - precision_str[vol->precision]);
> > + if ((ret = av_expr_parse(&vol->volume_pexpr,vol->volume_expr,
> > + var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
> > + av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", vol->volume_expr);
> > + return ret;
>
> Will that still support the "12dB" syntax?
yes
>
> Also, if it's a problem from a performance PoV (and the feature being
> relatively marginal), would it make sense to instead add a "dyn_volume"
> option?
That's a solution (or add a filter variant). The other solution would
be to make the filter behaves in a different way if the expression is
constant (CONST OP CONST is detected as constant), which would need
some hack to the eval code (of course this can't detect things like
EXPR-EXPR).
Please tell what you prefer (and feel free to pick the patch and
improve it yourself if you're in hurry). Next in the plan is to add
support to a command.
--
FFmpeg = Furious and Fierce MultiPurpose Explosive Goblin
More information about the ffmpeg-devel
mailing list