[FFmpeg-cvslog] lavfi/volume: support volume expression and per-frame expression evaluation

Stefano Sabatini git at videolan.org
Wed Dec 25 18:28:44 CET 2013


ffmpeg | branch: master | Stefano Sabatini <stefasab at gmail.com> | Sat Feb 23 00:17:17 2013 +0100| [7619a87cc8b9a1ac6ea6cf03b674f5a74b1ac90c] | committer: Stefano Sabatini

lavfi/volume: support volume expression and per-frame expression evaluation

The eval mode allows to evaluate the expression per-frame or just at
init.

In particular, address ticket #3234.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=7619a87cc8b9a1ac6ea6cf03b674f5a74b1ac90c
---

 doc/filters.texi        |   57 ++++++++++++++++++-
 libavfilter/af_volume.c |  144 ++++++++++++++++++++++++++++++++++++++++-------
 libavfilter/af_volume.h |   28 +++++++++
 libavfilter/version.h   |    2 +-
 4 files changed, 209 insertions(+), 22 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 8bcfd82..548cb43 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -1796,7 +1796,7 @@ The filter accepts the following options:
 @table @option
 
 @item volume
-Expresses how the audio volume will be increased or decreased.
+Set audio volume expression.
 
 Output values are clipped to the maximum value.
 
@@ -1805,7 +1805,7 @@ The output audio volume is given by the relation:
 @var{output_volume} = @var{volume} * @var{input_volume}
 @end example
 
-Default value for @var{volume} is 1.0.
+Default value for @var{volume} is "1.0".
 
 @item precision
 Set the mathematical precision.
@@ -1821,8 +1821,55 @@ precision of the volume scaling.
 @item double
 64-bit floating-point; limits input sample format to DBL.
 @end table
+
+ at item eval
+Set when the volume expression is evaluated.
+
+It accepts the following values:
+ at table @samp
+ at item once
+only evaluate expression once during the filter initialization
+
+ at item frame
+evaluate expression for each incoming frame
 @end table
 
+Default value is @samp{once}.
+ at end table
+
+The volume expression can contain the following parameters.
+
+ at table @option
+ at item n
+frame number (starting at zero)
+ at item nb_channels
+number of channels
+ at item nb_consumed_samples
+number of samples consumed by the filter
+ at item nb_samples
+number of samples in the current frame
+ at item pos
+original frame position in the file
+ at item pts
+frame PTS
+ at item sample_rate
+sample rate
+ at item startpts
+PTS at start of stream
+ at item startt
+time at start of stream
+ at item t
+frame time
+ at item tb
+timestamp timebase
+ at item volume
+last set volume value
+ at end table
+
+Note that when @option{eval} is set to @samp{once} only the
+ at var{sample_rate} and @var{tb} variables are available, all other
+variables will evaluate to NAN.
+
 @subsection Examples
 
 @itemize
@@ -1845,6 +1892,12 @@ Increase input audio power by 6 decibels using fixed-point precision:
 @example
 volume=volume=6dB:precision=fixed
 @end example
+
+ at item
+Fade volume after time 10 with an annihilation period of 5 seconds:
+ at example
+volume='if(lt(t,10),1,max(1-(t-10)/5,0))':eval=frame
+ at end example
 @end itemize
 
 @section volumedetect
diff --git a/libavfilter/af_volume.c b/libavfilter/af_volume.c
index 21fe9a1..b4ab6fe 100644
--- a/libavfilter/af_volume.c
+++ b/libavfilter/af_volume.c
@@ -39,41 +39,75 @@ static const char *precision_str[] = {
     "fixed", "float", "double"
 };
 
+static const char *const var_names[] = {
+    "n",                   ///< frame number (starting at zero)
+    "nb_channels",         ///< number of channels
+    "nb_consumed_samples", ///< number of samples consumed by the filter
+    "nb_samples",          ///< number of samples in the current frame
+    "pos",                 ///< position in the file of the frame
+    "pts",                 ///< frame presentation timestamp
+    "sample_rate",         ///< sample rate
+    "startpts",            ///< PTS at start of stream
+    "startt",              ///< time at start of stream
+    "t",                   ///< time in the file of the frame
+    "tb",                  ///< timebase
+    "volume",              ///< last set value
+    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" },
         { "float",  "select 32-bit floating-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FLOAT  }, INT_MIN, INT_MAX, A|F, "precision" },
         { "double", "select 64-bit floating-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_DOUBLE }, INT_MIN, INT_MAX, A|F, "precision" },
+    { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_ONCE}, 0, EVAL_MODE_NB-1, .flags = A|F, "eval" },
+         { "once",  "eval volume expression once", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_ONCE},  .flags = A|F, .unit = "eval" },
+         { "frame", "eval volume expression per-frame",                  0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = A|F, .unit = "eval" },
     { NULL }
 };
 
 AVFILTER_DEFINE_CLASS(volume);
 
-static av_cold int init(AVFilterContext *ctx)
+static int set_expr(AVExpr **pexpr, const char *expr, void *log_ctx)
 {
-    VolumeContext *vol = ctx->priv;
-
-    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]);
+    int ret;
+    AVExpr *old = NULL;
+
+    if (*pexpr)
+        old = *pexpr;
+    ret = av_expr_parse(pexpr, expr, var_names,
+                        NULL, NULL, NULL, NULL, 0, log_ctx);
+    if (ret < 0) {
+        av_log(log_ctx, AV_LOG_ERROR,
+               "Error when evaluating the volume expression '%s'\n", expr);
+        *pexpr = old;
+        return ret;
     }
 
+    av_expr_free(old);
     return 0;
 }
 
+static av_cold int init(AVFilterContext *ctx)
+{
+    VolumeContext *vol = ctx->priv;
+    return set_expr(&vol->volume_pexpr, vol->volume_expr, ctx);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    VolumeContext *vol = ctx->priv;
+    av_expr_free(vol->volume_pexpr);
+    av_opt_free(vol);
+}
+
 static int query_formats(AVFilterContext *ctx)
 {
     VolumeContext *vol = ctx->priv;
@@ -199,6 +233,37 @@ static av_cold void volume_init(VolumeContext *vol)
         ff_volume_init_x86(vol);
 }
 
+static int set_volume(AVFilterContext *ctx)
+{
+    VolumeContext *vol = ctx->priv;
+
+    vol->volume = av_expr_eval(vol->volume_pexpr, vol->var_values, NULL);
+    if (isnan(vol->volume)) {
+        if (vol->eval_mode == EVAL_MODE_ONCE) {
+            av_log(ctx, AV_LOG_ERROR, "Invalid value NaN for volume\n");
+            return AVERROR(EINVAL);
+        } else {
+            av_log(ctx, AV_LOG_WARNING, "Invalid value NaN for volume, setting to 0\n");
+            vol->volume = 0;
+        }
+    }
+    vol->var_values[VAR_VOLUME] = vol->volume;
+
+    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]);
+    }
+
+    volume_init(vol);
+    return 0;
+}
+
 static int config_output(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
@@ -209,20 +274,58 @@ static int config_output(AVFilterLink *outlink)
     vol->channels   = inlink->channels;
     vol->planes     = av_sample_fmt_is_planar(inlink->format) ? vol->channels : 1;
 
-    volume_init(vol);
-
-    return 0;
+    vol->var_values[VAR_N] =
+    vol->var_values[VAR_NB_CONSUMED_SAMPLES] =
+    vol->var_values[VAR_NB_SAMPLES] =
+    vol->var_values[VAR_POS] =
+    vol->var_values[VAR_PTS] =
+    vol->var_values[VAR_STARTPTS] =
+    vol->var_values[VAR_STARTT] =
+    vol->var_values[VAR_T] =
+    vol->var_values[VAR_VOLUME] = NAN;
+
+    vol->var_values[VAR_NB_CHANNELS] = inlink->channels;
+    vol->var_values[VAR_TB]          = av_q2d(inlink->time_base);
+    vol->var_values[VAR_SAMPLE_RATE] = inlink->sample_rate;
+
+    av_log(inlink->src, AV_LOG_VERBOSE, "tb:%f sample_rate:%f nb_channels:%f\n",
+           vol->var_values[VAR_TB],
+           vol->var_values[VAR_SAMPLE_RATE],
+           vol->var_values[VAR_NB_CHANNELS]);
+
+    return set_volume(ctx);
 }
 
+#define D2TS(d)  (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
+#define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
+#define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
+
 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
 {
+    AVFilterContext *ctx = inlink->dst;
     VolumeContext *vol    = inlink->dst->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
     int nb_samples        = buf->nb_samples;
     AVFrame *out_buf;
+    int64_t pos;
+
+    if (isnan(vol->var_values[VAR_STARTPTS])) {
+        vol->var_values[VAR_STARTPTS] = TS2D(buf->pts);
+        vol->var_values[VAR_STARTT  ] = TS2T(buf->pts, inlink->time_base);
+    }
+    vol->var_values[VAR_PTS] = TS2D(buf->pts);
+    vol->var_values[VAR_T  ] = TS2T(buf->pts, inlink->time_base);
+    vol->var_values[VAR_N  ] = inlink->frame_count;
 
-    if (vol->volume == 1.0 || vol->volume_i == 256)
-        return ff_filter_frame(outlink, buf);
+    pos = av_frame_get_pkt_pos(buf);
+    vol->var_values[VAR_POS] = pos == -1 ? NAN : pos;
+    if (vol->eval_mode == EVAL_MODE_FRAME)
+        set_volume(ctx);
+
+    if (vol->volume == 1.0 || vol->volume_i == 256) {
+        out_buf = buf;
+        goto end;
+    }
 
     /* do volume scaling in-place if input buffer is writable */
     if (av_frame_is_writable(buf)) {
@@ -266,6 +369,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
     if (buf != out_buf)
         av_frame_free(&buf);
 
+end:
+    vol->var_values[VAR_NB_CONSUMED_SAMPLES] += buf->nb_samples;
     return ff_filter_frame(outlink, out_buf);
 }
 
@@ -294,6 +399,7 @@ AVFilter ff_af_volume = {
     .priv_size      = sizeof(VolumeContext),
     .priv_class     = &volume_class,
     .init           = init,
+    .uninit         = uninit,
     .inputs         = avfilter_af_volume_inputs,
     .outputs        = avfilter_af_volume_outputs,
     .flags          = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
diff --git a/libavfilter/af_volume.h b/libavfilter/af_volume.h
index bd7932e..10ef6fb 100644
--- a/libavfilter/af_volume.h
+++ b/libavfilter/af_volume.h
@@ -25,6 +25,7 @@
 #define AVFILTER_AF_VOLUME_H
 
 #include "libavutil/common.h"
+#include "libavutil/eval.h"
 #include "libavutil/float_dsp.h"
 #include "libavutil/opt.h"
 #include "libavutil/samplefmt.h"
@@ -35,10 +36,37 @@ enum PrecisionType {
     PRECISION_DOUBLE,
 };
 
+enum EvalMode {
+    EVAL_MODE_ONCE,
+    EVAL_MODE_FRAME,
+    EVAL_MODE_NB
+};
+
+enum VolumeVarName {
+    VAR_N,
+    VAR_NB_CHANNELS,
+    VAR_NB_CONSUMED_SAMPLES,
+    VAR_NB_SAMPLES,
+    VAR_POS,
+    VAR_PTS,
+    VAR_SAMPLE_RATE,
+    VAR_STARTPTS,
+    VAR_STARTT,
+    VAR_T,
+    VAR_TB,
+    VAR_VOLUME,
+    VAR_VARS_NB
+};
+
 typedef struct VolumeContext {
     const AVClass *class;
     AVFloatDSPContext fdsp;
     enum PrecisionType precision;
+    enum EvalMode eval_mode;
+    const char *volume_expr;
+    AVExpr *volume_pexpr;
+    double var_values[VAR_VARS_NB];
+
     double volume;
     int    volume_i;
     int    channels;
diff --git a/libavfilter/version.h b/libavfilter/version.h
index 0427760..59fef97 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -31,7 +31,7 @@
 
 #define LIBAVFILTER_VERSION_MAJOR   4
 #define LIBAVFILTER_VERSION_MINOR   0
-#define LIBAVFILTER_VERSION_MICRO 100
+#define LIBAVFILTER_VERSION_MICRO 101
 
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
                                                LIBAVFILTER_VERSION_MINOR, \



More information about the ffmpeg-cvslog mailing list