On Fri, 28 Sep 2012 00:54:06 -0400, Justin Ruggles <[email protected]> 
wrote:
> From: Stefano Sabatini <[email protected]>
> 
> ---
> Made changes recommended by Diego and squashed with patch 2.
> 
>  Changelog                |    1 +
>  doc/filters.texi         |   50 ++++++++++++
>  libavfilter/Makefile     |    1 +
>  libavfilter/af_volume.c  |  201 
> ++++++++++++++++++++++++++++++++++++++++++++++
>  libavfilter/allfilters.c |    1 +
>  libavfilter/version.h    |    2 +-
>  6 files changed, 255 insertions(+), 1 deletions(-)
>  create mode 100644 libavfilter/af_volume.c
> 
> diff --git a/Changelog b/Changelog
> index ad3e211..e0ccc8f 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -48,6 +48,7 @@ version <next>:
>  - Microsoft Screen 2 decoder
>  - RTP depacketization of JPEG
>  - Smooth Streaming live segmenter muxer
> +- audio volume filter
>  
>  
>  version 0.8:
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 4825b0d..8bb2040 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -318,6 +318,56 @@ not meant to be used directly, it is inserted 
> automatically by libavfilter
>  whenever conversion is needed. Use the @var{aformat} filter to force a 
> specific
>  conversion.
>  
> +@section volume
> +
> +Adjust the input audio volume.
> +
> +The filter accepts exactly one parameter @var{vol}, which expresses
> +how the audio volume will be increased or decresed.

decresed

> +
> +Output values are clipped to the maximum value.
> +
> +If @var{vol} is expressed as a decimal number, and the output audio
> +volume is given by the relation:
> +@example
> +@var{output_volume} = @var{vol} * @var{input_volume}
> +@end example
> +
> +If @var{vol} is expressed as a decimal number followed by the string
> +"dB", the value represents the requested change in decibels of the
> +input audio power, and the output audio volume is given by the
> +relation:
> +@example
> +@var{output_volume} = 10^(@var{vol}/20) * @var{input_volume}
> +@end example
> +
> +Otherwise @var{vol} is considered an expression and its evaluated
> +value is used for computing the output audio volume according to the
> +first relation.
> +
> +Default value for @var{vol} is 1.0.
> +
> +@subsection Examples
> +
> +@itemize
> +@item
> +Halve the input audio volume:
> +@example
> +volume=0.5
> +@end example
> +
> +The above example is equivalent to:
> +@example
> +volume=1/2
> +@end example
> +
> +@item
> +Decrease input audio power by 12 decibels:
> +@example
> +volume=-12dB
> +@end example
> +@end itemize
> +
>  @c man end AUDIO FILTERS
>  
>  @chapter Audio Sources
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 530aa57..876025f 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -34,6 +34,7 @@ OBJS-$(CONFIG_CHANNELMAP_FILTER)             += 
> af_channelmap.o
>  OBJS-$(CONFIG_CHANNELSPLIT_FILTER)           += af_channelsplit.o
>  OBJS-$(CONFIG_JOIN_FILTER)                   += af_join.o
>  OBJS-$(CONFIG_RESAMPLE_FILTER)               += af_resample.o
> +OBJS-$(CONFIG_VOLUME_FILTER)                 += af_volume.o
>  
>  OBJS-$(CONFIG_ANULLSRC_FILTER)               += asrc_anullsrc.o
>  
> diff --git a/libavfilter/af_volume.c b/libavfilter/af_volume.c
> new file mode 100644
> index 0000000..aa42e13
> --- /dev/null
> +++ b/libavfilter/af_volume.c
> @@ -0,0 +1,201 @@
> +/*
> + * Copyright (c) 2011 Stefano Sabatini
> + *
> + * This file is part of Libav.
> + *
> + * Libav 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.
> + *
> + * Libav 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 Libav; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 
> USA
> + */
> +
> +/**
> + * @file
> + * audio volume filter
> + */
> +
> +#include "libavutil/audioconvert.h"
> +#include "libavutil/common.h"
> +#include "libavutil/eval.h"
> +#include "audio.h"
> +#include "avfilter.h"
> +#include "formats.h"
> +
> +typedef struct {
> +    double volume;
> +    int    volume_i;
> +} VolumeContext;
> +
> +static av_cold int init(AVFilterContext *ctx, const char *args)
> +{
> +    VolumeContext *vol = ctx->priv;
> +    char *tail;
> +    int ret = 0;
> +
> +    vol->volume = 1.0;
> +
> +    if (args) {
> +        /* parse the number as a decimal number */
> +        double d = strtod(args, &tail);
> +
> +        if (*tail) {
> +            if (!strcmp(tail, "dB")) {
> +                /* consider the argument an adjustement in decibels */
> +                d = pow(10, d/20);
> +            } else {
> +                /* parse the argument as an expression */
> +                ret = av_expr_parse_and_eval(&d, args, NULL, NULL,
> +                                             NULL, NULL, NULL, NULL,
> +                                             NULL, 0, ctx);
> +            }
> +        }
> +
> +        if (ret < 0) {
> +            av_log(ctx, AV_LOG_ERROR,
> +                   "Invalid volume argument '%s'\n", args);
> +            return AVERROR(EINVAL);
> +        }
> +
> +        if (d < 0 || d > 65536) { /* 65536 = INT_MIN / (128 * 256) */
> +            av_log(ctx, AV_LOG_ERROR,
> +                   "Negative or too big volume value %f\n", d);
> +            return AVERROR(EINVAL);
> +        }
> +
> +        vol->volume = d;
> +    }
> +
> +    vol->volume_i = (int)(vol->volume * 256 + 0.5);
> +    av_log(ctx, AV_LOG_VERBOSE, "volume=%f\n", vol->volume);
> +    return 0;
> +}
> +
> +static int query_formats(AVFilterContext *ctx)
> +{
> +    AVFilterFormats *formats = NULL;
> +    AVFilterChannelLayouts *layouts;
> +    enum AVSampleFormat sample_fmts[] = {

static const?

> +        AV_SAMPLE_FMT_U8,
> +        AV_SAMPLE_FMT_S16,
> +        AV_SAMPLE_FMT_S32,
> +        AV_SAMPLE_FMT_FLT,
> +        AV_SAMPLE_FMT_DBL,
> +        AV_SAMPLE_FMT_NONE
> +    };
> +
> +    layouts = ff_all_channel_layouts();
> +    if (!layouts)
> +        return AVERROR(ENOMEM);
> +    ff_set_common_channel_layouts(ctx, layouts);
> +
> +    formats = ff_make_format_list(sample_fmts);
> +    if (!formats)
> +        return AVERROR(ENOMEM);
> +    ff_set_common_formats(ctx, formats);
> +
> +    formats = ff_all_samplerates();
> +    if (!formats)
> +        return AVERROR(ENOMEM);
> +    ff_set_common_samplerates(ctx, formats);
> +
> +    return 0;
> +}
> +
> +static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)

Nit: I don't like the 'insamples' name, it's quite unwieldy. I'd just
use 'buf' like everywhere else.

> +{
> +    VolumeContext *vol = inlink->dst->priv;
> +    AVFilterLink *outlink = inlink->dst->outputs[0];
> +    const int nb_samples = insamples->audio->nb_samples *
> +        av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
> +    const double volume   = vol->volume;
> +    const int    volume_i = vol->volume_i;
> +    int i;
> +
> +    if (volume_i != 256) {
> +        switch (insamples->format) {
> +        case AV_SAMPLE_FMT_U8:
> +        {
> +            uint8_t *p = 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 = (int16_t *)insamples->data[0];
> +            for (i = 0; i < nb_samples; i++) {
> +                int v = ((int64_t)*p * volume_i + 128) >> 8;
> +                *p++ = av_clip_int16(v);
> +            }
> +            break;
> +        }
> +        case AV_SAMPLE_FMT_S32:
> +        {
> +            int32_t *p = (int32_t *)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 = (float *)insamples->data[0];
> +            float scale = (float)volume;
> +            for (i = 0; i < nb_samples; i++) {
> +                *p++ *= scale;
> +            }
> +            break;
> +        }
> +        case AV_SAMPLE_FMT_DBL:
> +        {
> +            double *p = (double *)insamples->data[0];
> +            for (i = 0; i < nb_samples; i++) {
> +                *p *= volume;
> +                p++;

Why is p++ separate here?

-- 
Anton Khirnov
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to