libavfilter/af_volume.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011 Stefano Sabatini
00003  *
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * FFmpeg is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with FFmpeg; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00027 #include "libavutil/audioconvert.h"
00028 #include "libavutil/eval.h"
00029 #include "avfilter.h"
00030 
00031 typedef struct {
00032     double volume;
00033     int    volume_i;
00034 } VolumeContext;
00035 
00036 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00037 {
00038     VolumeContext *vol = ctx->priv;
00039     char *tail;
00040     int ret = 0;
00041 
00042     vol->volume = 1.0;
00043 
00044     if (args) {
00045         /* parse the number as a decimal number */
00046         double d = strtod(args, &tail);
00047 
00048         if (*tail) {
00049             if (!strcmp(tail, "dB")) {
00050                 /* consider the argument an adjustement in decibels */
00051                 d = pow(10, d/20);
00052             } else {
00053                 /* parse the argument as an expression */
00054                 ret = av_expr_parse_and_eval(&d, args, NULL, NULL,
00055                                              NULL, NULL, NULL, NULL,
00056                                              NULL, 0, ctx);
00057             }
00058         }
00059 
00060         if (ret < 0) {
00061             av_log(ctx, AV_LOG_ERROR,
00062                    "Invalid volume argument '%s'\n", args);
00063             return AVERROR(EINVAL);
00064         }
00065 
00066         if (d < 0 || d > 65536) { /* 65536 = INT_MIN / (128 * 256) */
00067             av_log(ctx, AV_LOG_ERROR,
00068                    "Negative or too big volume value %f\n", d);
00069             return AVERROR(EINVAL);
00070         }
00071 
00072         vol->volume = d;
00073     }
00074 
00075     vol->volume_i = (int)(vol->volume * 256 + 0.5);
00076     av_log(ctx, AV_LOG_INFO, "volume=%f\n", vol->volume);
00077     return 0;
00078 }
00079 
00080 static int query_formats(AVFilterContext *ctx)
00081 {
00082     AVFilterFormats *formats = NULL;
00083     enum AVSampleFormat sample_fmts[] = {
00084         AV_SAMPLE_FMT_U8,
00085         AV_SAMPLE_FMT_S16,
00086         AV_SAMPLE_FMT_S32,
00087         AV_SAMPLE_FMT_FLT,
00088         AV_SAMPLE_FMT_DBL,
00089         AV_SAMPLE_FMT_NONE
00090     };
00091     int packing_fmts[] = { AVFILTER_PACKED, -1 };
00092 
00093     formats = avfilter_make_all_channel_layouts();
00094     if (!formats)
00095         return AVERROR(ENOMEM);
00096     avfilter_set_common_channel_layouts(ctx, formats);
00097 
00098     formats = avfilter_make_format_list(sample_fmts);
00099     if (!formats)
00100         return AVERROR(ENOMEM);
00101     avfilter_set_common_sample_formats(ctx, formats);
00102 
00103     formats = avfilter_make_format_list(packing_fmts);
00104     if (!formats)
00105         return AVERROR(ENOMEM);
00106     avfilter_set_common_packing_formats(ctx, formats);
00107 
00108     return 0;
00109 }
00110 
00111 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
00112 {
00113     VolumeContext *vol = inlink->dst->priv;
00114     AVFilterLink *outlink = inlink->dst->outputs[0];
00115     const int nb_samples = insamples->audio->nb_samples *
00116         av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
00117     const double volume   = vol->volume;
00118     const int    volume_i = vol->volume_i;
00119     int i;
00120 
00121     if (volume_i != 256) {
00122         switch (insamples->format) {
00123         case AV_SAMPLE_FMT_U8:
00124         {
00125             uint8_t *p = (void *)insamples->data[0];
00126             for (i = 0; i < nb_samples; i++) {
00127                 int v = (((*p - 128) * volume_i + 128) >> 8) + 128;
00128                 *p++ = av_clip_uint8(v);
00129             }
00130             break;
00131         }
00132         case AV_SAMPLE_FMT_S16:
00133         {
00134             int16_t *p = (void *)insamples->data[0];
00135             for (i = 0; i < nb_samples; i++) {
00136                 int v = ((int64_t)*p * volume_i + 128) >> 8;
00137                 *p++ = av_clip_int16(v);
00138             }
00139             break;
00140         }
00141         case AV_SAMPLE_FMT_S32:
00142         {
00143             int32_t *p = (void *)insamples->data[0];
00144             for (i = 0; i < nb_samples; i++) {
00145                 int64_t v = (((int64_t)*p * volume_i + 128) >> 8);
00146                 *p++ = av_clipl_int32(v);
00147             }
00148             break;
00149         }
00150         case AV_SAMPLE_FMT_FLT:
00151         {
00152             float *p = (void *)insamples->data[0];
00153             float scale = (float)volume;
00154             for (i = 0; i < nb_samples; i++) {
00155                 *p++ *= scale;
00156             }
00157             break;
00158         }
00159         case AV_SAMPLE_FMT_DBL:
00160         {
00161             double *p = (void *)insamples->data[0];
00162             for (i = 0; i < nb_samples; i++) {
00163                 *p *= volume;
00164                 p++;
00165             }
00166             break;
00167         }
00168         }
00169     }
00170     avfilter_filter_samples(outlink, insamples);
00171 }
00172 
00173 AVFilter avfilter_af_volume = {
00174     .name           = "volume",
00175     .description    = NULL_IF_CONFIG_SMALL("Change input volume."),
00176     .query_formats  = query_formats,
00177     .priv_size      = sizeof(VolumeContext),
00178     .init           = init,
00179 
00180     .inputs  = (const AVFilterPad[])  {{ .name     = "default",
00181                                    .type           = AVMEDIA_TYPE_AUDIO,
00182                                    .filter_samples = filter_samples,
00183                                    .min_perms      = AV_PERM_READ|AV_PERM_WRITE},
00184                                  { .name = NULL}},
00185 
00186     .outputs = (const AVFilterPad[])  {{ .name     = "default",
00187                                    .type           = AVMEDIA_TYPE_AUDIO, },
00188                                  { .name = NULL}},
00189 };