[FFmpeg-devel] [PATCH] lavfi: add avolume filter

Stefano Sabatini stefasab at gmail.com
Tue Nov 1 21:45:39 CET 2011


---
 doc/filters.texi         |   42 ++++++++++
 libavfilter/Makefile     |    1 +
 libavfilter/af_avolume.c |  189 ++++++++++++++++++++++++++++++++++++++++++++++
 libavfilter/allfilters.c |    1 +
 4 files changed, 233 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/af_avolume.c

diff --git a/doc/filters.texi b/doc/filters.texi
index 7085ae1..9b6f614 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -224,6 +224,48 @@ expressed in the form "[@var{c0} @var{c1} @var{c2} @var{c3} @var{c4} @var{c5}
 @var{c6} @var{c7}]"
 @end table
 
+ at section avolume
+
+Adjust the input audio volume. The filter accepts exactly one
+parameter, which express how the audio volume will be increased or
+decresed.
+
+Output values are clipped to the maximum value.
+
+If the parameter value is an integer, baseline value is 256, and the
+output audio volume is given by the relation:
+ at example
+ at var{output_volume}=@var{vol}/256 * @var{input_volume}
+ at end example
+
+If the parameter is expressed as a decimal number, baseline value is
+1.0, and the output audio volume is given by the relation:
+ at example
+ at var{output_volume}=@var{vol} * @var{input_volume}
+ at end example
+
+Note that when choosing the decimal number representation, "." must be
+specified in the parameter or it will be interpreted as an integer
+value.
+
+The expressed value must to be a non-negative value either way.
+Default parameter value is 1.0.
+
+ at subsection Examples
+
+ at itemize
+ at item
+Half the input audio volume:
+ at example
+avolume=0.5
+ at end example
+
+The above example is equivalent to:
+ at example
+avolume=128
+ at end example
+ at end itemize
+
 @section earwax
 
 Make audio easier to listen to on headphones. Adds `cues' to 44.1kHz
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 583dd6d..2eaa74e 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -27,6 +27,7 @@ OBJS-$(CONFIG_ACONVERT_FILTER)               += af_aconvert.o
 OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
 OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
 OBJS-$(CONFIG_ARESAMPLE_FILTER)              += af_aresample.o
+OBJS-$(CONFIG_AVOLUME_FILTER)                += af_avolume.o
 OBJS-$(CONFIG_ASHOWINFO_FILTER)              += af_ashowinfo.o
 OBJS-$(CONFIG_EARWAX_FILTER)                 += af_earwax.o
 OBJS-$(CONFIG_LADSPA_FILTER)                 += af_ladspa.o
diff --git a/libavfilter/af_avolume.c b/libavfilter/af_avolume.c
new file mode 100644
index 0000000..62f2c97
--- /dev/null
+++ b/libavfilter/af_avolume.c
@@ -0,0 +1,189 @@
+/*
+ * Copyright (c) 2011 Stefano Sabatini
+ *
+ * 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
+ * audio volume filter
+ * based on ffmpeg.c code
+ */
+
+#include "libavutil/audioconvert.h"
+#include "avfilter.h"
+
+typedef struct {
+    /* todo: express it as a float (0-1) or int (0-256) */
+    int    volume_i;
+    double volume_d;
+} VolumeContext;
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    VolumeContext *vol = ctx->priv;
+    char *tail;
+    vol->volume_i = 256;
+    vol->volume_d = 1.0;
+
+    if (args) {
+        /* parse the number as an integer */
+        long int li = strtol(args, &tail, 10);
+
+        if (!*tail) {
+            if (li < 0 || li >= INT_MAX) {
+                av_log(ctx, AV_LOG_ERROR,
+                       "Invalid or too big integer %ld, must be >= 0\n", li);
+                return AVERROR(EINVAL);
+            }
+            vol->volume_i = li;
+            vol->volume_d = (double)vol->volume_i / 256;
+        } else {
+            /* parse the number as a decimal number */
+            double d = strtod(args, &tail);
+            int err = errno;
+
+            if (*tail) {
+                av_log(ctx, AV_LOG_ERROR,
+                       "String provided '%s' cannot be parsed as a number\n",
+                       args);
+                return AVERROR(EINVAL);
+            }
+            if (d < 0 || err) {
+                av_log(ctx, AV_LOG_ERROR,
+                       "Invalid or too big decimal number %f, must be >= 0.0\n", d);
+                return AVERROR(EINVAL);
+            }
+            vol->volume_d = d;
+            vol->volume_i = (int)(vol->volume_d * 256 + 0.5);
+        }
+    }
+
+    av_log(ctx, AV_LOG_INFO, "volume_i:%d volume_d=%f\n", vol->volume_i, vol->volume_d);
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    AVFilterFormats *formats = NULL;
+    enum AVSampleFormat sample_fmts[] = {
+        AV_SAMPLE_FMT_U8,
+        AV_SAMPLE_FMT_S16,
+        AV_SAMPLE_FMT_S32,
+        AV_SAMPLE_FMT_FLT,
+        AV_SAMPLE_FMT_DBL,
+        AV_SAMPLE_FMT_NONE
+    };
+    int packing_fmts[] = { AVFILTER_PACKED, -1 };
+
+    formats = avfilter_make_all_channel_layouts();
+    if (!formats)
+        return AVERROR(ENOMEM);
+    avfilter_set_common_channel_layouts(ctx, formats);
+
+    formats = avfilter_make_format_list(sample_fmts);
+    if (!formats)
+        return AVERROR(ENOMEM);
+    avfilter_set_common_sample_formats(ctx, formats);
+
+    formats = avfilter_make_format_list(packing_fmts);
+    if (!formats)
+        return AVERROR(ENOMEM);
+    avfilter_set_common_packing_formats(ctx, formats);
+
+    return 0;
+}
+
+static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
+{
+    VolumeContext *vol = inlink->dst->priv;
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    int nb_samples = insamples->audio->nb_samples *
+        av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
+    const int    volume_i = vol->volume_i;
+    const double volume_d = vol->volume_d;
+    int i;
+
+    if (volume_i != 256) {
+        switch (insamples->format) {
+        case AV_SAMPLE_FMT_U8:
+        {
+            uint8_t *p = (void *)insamples->data[0];
+            for (i = 0; i < nb_samples; i++) {
+                int v = (((*p - 128) * volume_i + 128) >> 8) + 128;
+                *p++ = av_clip_uint8(v);
+            }
+            break;
+        }
+        case AV_SAMPLE_FMT_S16:
+        {
+            int16_t *p = (void *)insamples->data[0];
+            for (i = 0; i < nb_samples; i++) {
+                int v = ((*p) * volume_i + 128) >> 8;
+                *p++ = av_clip_int16(v);
+            }
+            break;
+        }
+        case AV_SAMPLE_FMT_S32:
+        {
+            int32_t *p = (void *)insamples->data[0];
+            for (i = 0; i < nb_samples; i++) {
+                int64_t v = (((int64_t)*p * volume_i + 128) >> 8);
+                *p++ = av_clipl_int32(v);
+            }
+            break;
+        }
+        case AV_SAMPLE_FMT_FLT:
+        {
+            float *p = (void *)insamples->data[0];
+            float scale = (float)volume_d;
+            for (i = 0; i < nb_samples; i++) {
+                *p++ *= scale;
+            }
+            break;
+        }
+        case AV_SAMPLE_FMT_DBL:
+        {
+            double *p = (void *)insamples->data[0];
+            for (i = 0; i < nb_samples; i++) {
+                *p *= volume_d;
+                p++;
+            }
+            break;
+        }
+        }
+    }
+    avfilter_filter_samples(outlink, insamples);
+}
+
+AVFilter avfilter_af_avolume = {
+    .name           = "avolume",
+    .description    = NULL_IF_CONFIG_SMALL("Change input volume."),
+    .query_formats  = query_formats,
+    .priv_size      = sizeof(VolumeContext),
+    .init           = init,
+
+    .inputs  = (AVFilterPad[])  {{ .name           = "default",
+                                   .type           = AVMEDIA_TYPE_AUDIO,
+                                   .filter_samples = filter_samples,
+                                   .min_perms      = AV_PERM_READ, AV_PERM_WRITE},
+                                 { .name = NULL}},
+
+    .outputs = (AVFilterPad[])  {{ .name           = "default",
+                                   .type           = AVMEDIA_TYPE_AUDIO, },
+                                 { .name = NULL}},
+};
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 185114c..37ca7f0 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -39,6 +39,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (ANULL,       anull,       af);
     REGISTER_FILTER (ARESAMPLE,   aresample,   af);
     REGISTER_FILTER (ASHOWINFO,   ashowinfo,   af);
+    REGISTER_FILTER (AVOLUME,     avolume,     af);
     REGISTER_FILTER (EARWAX,      earwax,      af);
     REGISTER_FILTER (LADSPA,      ladspa,      af);
     REGISTER_FILTER (SOX,         sox,         af);
-- 
1.7.4.1



More information about the ffmpeg-devel mailing list