]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_volume.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / af_volume.c
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * audio volume filter
24  * based on ffmpeg.c code
25  */
26
27 #include "libavutil/audioconvert.h"
28 #include "libavutil/eval.h"
29 #include "audio.h"
30 #include "avfilter.h"
31 #include "formats.h"
32
33 typedef struct {
34     double volume;
35     int    volume_i;
36 } VolumeContext;
37
38 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
39 {
40     VolumeContext *vol = ctx->priv;
41     char *tail;
42     int ret = 0;
43
44     vol->volume = 1.0;
45
46     if (args) {
47         /* parse the number as a decimal number */
48         double d = strtod(args, &tail);
49
50         if (*tail) {
51             if (!strcmp(tail, "dB")) {
52                 /* consider the argument an adjustement in decibels */
53                 d = pow(10, d/20);
54             } else {
55                 /* parse the argument as an expression */
56                 ret = av_expr_parse_and_eval(&d, args, NULL, NULL,
57                                              NULL, NULL, NULL, NULL,
58                                              NULL, 0, ctx);
59             }
60         }
61
62         if (ret < 0) {
63             av_log(ctx, AV_LOG_ERROR,
64                    "Invalid volume argument '%s'\n", args);
65             return AVERROR(EINVAL);
66         }
67
68         if (d < 0 || d > 65536) { /* 65536 = INT_MIN / (128 * 256) */
69             av_log(ctx, AV_LOG_ERROR,
70                    "Negative or too big volume value %f\n", d);
71             return AVERROR(EINVAL);
72         }
73
74         vol->volume = d;
75     }
76
77     vol->volume_i = (int)(vol->volume * 256 + 0.5);
78     av_log(ctx, AV_LOG_INFO, "volume=%f\n", vol->volume);
79     return 0;
80 }
81
82 static int query_formats(AVFilterContext *ctx)
83 {
84     AVFilterFormats *formats = NULL;
85     AVFilterChannelLayouts *layouts;
86     enum AVSampleFormat sample_fmts[] = {
87         AV_SAMPLE_FMT_U8,
88         AV_SAMPLE_FMT_S16,
89         AV_SAMPLE_FMT_S32,
90         AV_SAMPLE_FMT_FLT,
91         AV_SAMPLE_FMT_DBL,
92         AV_SAMPLE_FMT_NONE
93     };
94     int packing_fmts[] = { AVFILTER_PACKED, -1 };
95
96     layouts = ff_all_channel_layouts();
97     if (!layouts)
98         return AVERROR(ENOMEM);
99     ff_set_common_channel_layouts(ctx, layouts);
100
101     formats = avfilter_make_format_list(sample_fmts);
102     if (!formats)
103         return AVERROR(ENOMEM);
104     avfilter_set_common_sample_formats(ctx, formats);
105
106     formats = avfilter_make_format_list(packing_fmts);
107     if (!formats)
108         return AVERROR(ENOMEM);
109     avfilter_set_common_packing_formats(ctx, formats);
110
111     formats = ff_all_samplerates();
112     if (!formats)
113         return AVERROR(ENOMEM);
114     ff_set_common_samplerates(ctx, formats);
115
116     return 0;
117 }
118
119 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
120 {
121     VolumeContext *vol = inlink->dst->priv;
122     AVFilterLink *outlink = inlink->dst->outputs[0];
123     const int nb_samples = insamples->audio->nb_samples *
124         av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
125     const double volume   = vol->volume;
126     const int    volume_i = vol->volume_i;
127     int i;
128
129     if (volume_i != 256) {
130         switch (insamples->format) {
131         case AV_SAMPLE_FMT_U8:
132         {
133             uint8_t *p = (void *)insamples->data[0];
134             for (i = 0; i < nb_samples; i++) {
135                 int v = (((*p - 128) * volume_i + 128) >> 8) + 128;
136                 *p++ = av_clip_uint8(v);
137             }
138             break;
139         }
140         case AV_SAMPLE_FMT_S16:
141         {
142             int16_t *p = (void *)insamples->data[0];
143             for (i = 0; i < nb_samples; i++) {
144                 int v = ((int64_t)*p * volume_i + 128) >> 8;
145                 *p++ = av_clip_int16(v);
146             }
147             break;
148         }
149         case AV_SAMPLE_FMT_S32:
150         {
151             int32_t *p = (void *)insamples->data[0];
152             for (i = 0; i < nb_samples; i++) {
153                 int64_t v = (((int64_t)*p * volume_i + 128) >> 8);
154                 *p++ = av_clipl_int32(v);
155             }
156             break;
157         }
158         case AV_SAMPLE_FMT_FLT:
159         {
160             float *p = (void *)insamples->data[0];
161             float scale = (float)volume;
162             for (i = 0; i < nb_samples; i++) {
163                 *p++ *= scale;
164             }
165             break;
166         }
167         case AV_SAMPLE_FMT_DBL:
168         {
169             double *p = (void *)insamples->data[0];
170             for (i = 0; i < nb_samples; i++) {
171                 *p *= volume;
172                 p++;
173             }
174             break;
175         }
176         }
177     }
178     ff_filter_samples(outlink, insamples);
179 }
180
181 AVFilter avfilter_af_volume = {
182     .name           = "volume",
183     .description    = NULL_IF_CONFIG_SMALL("Change input volume."),
184     .query_formats  = query_formats,
185     .priv_size      = sizeof(VolumeContext),
186     .init           = init,
187
188     .inputs  = (const AVFilterPad[])  {{ .name     = "default",
189                                    .type           = AVMEDIA_TYPE_AUDIO,
190                                    .filter_samples = filter_samples,
191                                    .min_perms      = AV_PERM_READ|AV_PERM_WRITE},
192                                  { .name = NULL}},
193
194     .outputs = (const AVFilterPad[])  {{ .name     = "default",
195                                    .type           = AVMEDIA_TYPE_AUDIO, },
196                                  { .name = NULL}},
197 };