Browse code

lavfi: add volume filter

Based on the volume filter in FFmpeg written by Stefano Sabatini
<stefasab@gmail.com>.

Justin Ruggles authored on 2012/09/29 13:38:13
Showing 7 changed files
... ...
@@ -4,6 +4,7 @@ releases are sorted from youngest to oldest.
4 4
 version <next>:
5 5
 - ashowinfo audio filter
6 6
 - 24-bit FLAC encoding
7
+- audio volume filter
7 8
 
8 9
 
9 10
 version 9_beta2:
... ...
@@ -359,6 +359,59 @@ not meant to be used directly, it is inserted automatically by libavfilter
359 359
 whenever conversion is needed. Use the @var{aformat} filter to force a specific
360 360
 conversion.
361 361
 
362
+@section volume
363
+
364
+Adjust the input audio volume.
365
+
366
+The filter accepts the following named parameters:
367
+@table @option
368
+
369
+@item volume
370
+Expresses how the audio volume will be increased or decreased.
371
+
372
+Output values are clipped to the maximum value.
373
+
374
+The output audio volume is given by the relation:
375
+@example
376
+@var{output_volume} = @var{volume} * @var{input_volume}
377
+@end example
378
+
379
+Default value for @var{volume} is 1.0.
380
+
381
+@item precision
382
+Mathematical precision.
383
+
384
+This determines which input sample formats will be allowed, which affects the
385
+precision of the volume scaling.
386
+
387
+@table @option
388
+@item fixed
389
+8-bit fixed-point; limits input sample format to U8, S16, and S32.
390
+@item float
391
+32-bit floating-point; limits input sample format to FLT. (default)
392
+@item double
393
+64-bit floating-point; limits input sample format to DBL.
394
+@end table
395
+@end table
396
+
397
+@subsection Examples
398
+
399
+@itemize
400
+@item
401
+Halve the input audio volume:
402
+@example
403
+volume=volume=0.5
404
+volume=volume=1/2
405
+volume=volume=-6.0206dB
406
+@end example
407
+
408
+@item
409
+Increase input audio power by 6 decibels using fixed-point precision:
410
+@example
411
+volume=volume=6dB:precision=fixed
412
+@end example
413
+@end itemize
414
+
362 415
 @c man end AUDIO FILTERS
363 416
 
364 417
 @chapter Audio Sources
... ...
@@ -35,6 +35,7 @@ OBJS-$(CONFIG_CHANNELMAP_FILTER)             += af_channelmap.o
35 35
 OBJS-$(CONFIG_CHANNELSPLIT_FILTER)           += af_channelsplit.o
36 36
 OBJS-$(CONFIG_JOIN_FILTER)                   += af_join.o
37 37
 OBJS-$(CONFIG_RESAMPLE_FILTER)               += af_resample.o
38
+OBJS-$(CONFIG_VOLUME_FILTER)                 += af_volume.o
38 39
 
39 40
 OBJS-$(CONFIG_ANULLSRC_FILTER)               += asrc_anullsrc.o
40 41
 
41 42
new file mode 100644
... ...
@@ -0,0 +1,314 @@
0
+/*
1
+ * Copyright (c) 2011 Stefano Sabatini
2
+ * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav 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
+ * Libav 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 Libav; 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
+ */
25
+
26
+#include "libavutil/audioconvert.h"
27
+#include "libavutil/common.h"
28
+#include "libavutil/eval.h"
29
+#include "libavutil/float_dsp.h"
30
+#include "libavutil/opt.h"
31
+#include "audio.h"
32
+#include "avfilter.h"
33
+#include "formats.h"
34
+#include "internal.h"
35
+#include "af_volume.h"
36
+
37
+static const char *precision_str[] = {
38
+    "fixed", "float", "double"
39
+};
40
+
41
+#define OFFSET(x) offsetof(VolumeContext, x)
42
+#define A AV_OPT_FLAG_AUDIO_PARAM
43
+
44
+static const AVOption options[] = {
45
+    { "volume", "Volume adjustment.",
46
+            OFFSET(volume), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, 0, 0x7fffff, A },
47
+    { "precision", "Mathematical precision.",
48
+            OFFSET(precision), AV_OPT_TYPE_INT, { .i64 = PRECISION_FLOAT }, PRECISION_FIXED, PRECISION_DOUBLE, A, "precision" },
49
+        { "fixed",  "8-bit fixed-point.",     0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FIXED  }, INT_MIN, INT_MAX, A, "precision" },
50
+        { "float",  "32-bit floating-point.", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FLOAT  }, INT_MIN, INT_MAX, A, "precision" },
51
+        { "double", "64-bit floating-point.", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_DOUBLE }, INT_MIN, INT_MAX, A, "precision" },
52
+    { NULL },
53
+};
54
+
55
+static const AVClass volume_class = {
56
+    .class_name = "volume filter",
57
+    .item_name  = av_default_item_name,
58
+    .option     = options,
59
+    .version    = LIBAVUTIL_VERSION_INT,
60
+};
61
+
62
+static av_cold int init(AVFilterContext *ctx, const char *args)
63
+{
64
+    VolumeContext *vol = ctx->priv;
65
+    int ret;
66
+
67
+    vol->class = &volume_class;
68
+    av_opt_set_defaults(vol);
69
+
70
+    if ((ret = av_set_options_string(vol, args, "=", ":")) < 0) {
71
+        av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
72
+        return ret;
73
+    }
74
+
75
+    if (vol->precision == PRECISION_FIXED) {
76
+        vol->volume_i = (int)(vol->volume * 256 + 0.5);
77
+        vol->volume   = vol->volume_i / 256.0;
78
+        av_log(ctx, AV_LOG_VERBOSE, "volume:(%d/256)(%f)(%1.2fdB) precision:fixed\n",
79
+               vol->volume_i, vol->volume, 20.0*log(vol->volume)/M_LN10);
80
+    } else {
81
+        av_log(ctx, AV_LOG_VERBOSE, "volume:(%f)(%1.2fdB) precision:%s\n",
82
+               vol->volume, 20.0*log(vol->volume)/M_LN10,
83
+               precision_str[vol->precision]);
84
+    }
85
+
86
+    av_opt_free(vol);
87
+    return ret;
88
+}
89
+
90
+static int query_formats(AVFilterContext *ctx)
91
+{
92
+    VolumeContext *vol = ctx->priv;
93
+    AVFilterFormats *formats = NULL;
94
+    AVFilterChannelLayouts *layouts;
95
+    static const enum AVSampleFormat sample_fmts[][7] = {
96
+        /* PRECISION_FIXED */
97
+        {
98
+            AV_SAMPLE_FMT_U8,
99
+            AV_SAMPLE_FMT_U8P,
100
+            AV_SAMPLE_FMT_S16,
101
+            AV_SAMPLE_FMT_S16P,
102
+            AV_SAMPLE_FMT_S32,
103
+            AV_SAMPLE_FMT_S32P,
104
+            AV_SAMPLE_FMT_NONE
105
+        },
106
+        /* PRECISION_FLOAT */
107
+        {
108
+            AV_SAMPLE_FMT_FLT,
109
+            AV_SAMPLE_FMT_FLTP,
110
+            AV_SAMPLE_FMT_NONE
111
+        },
112
+        /* PRECISION_DOUBLE */
113
+        {
114
+            AV_SAMPLE_FMT_DBL,
115
+            AV_SAMPLE_FMT_DBLP,
116
+            AV_SAMPLE_FMT_NONE
117
+        }
118
+    };
119
+
120
+    layouts = ff_all_channel_layouts();
121
+    if (!layouts)
122
+        return AVERROR(ENOMEM);
123
+    ff_set_common_channel_layouts(ctx, layouts);
124
+
125
+    formats = ff_make_format_list(sample_fmts[vol->precision]);
126
+    if (!formats)
127
+        return AVERROR(ENOMEM);
128
+    ff_set_common_formats(ctx, formats);
129
+
130
+    formats = ff_all_samplerates();
131
+    if (!formats)
132
+        return AVERROR(ENOMEM);
133
+    ff_set_common_samplerates(ctx, formats);
134
+
135
+    return 0;
136
+}
137
+
138
+static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src,
139
+                                    int nb_samples, int volume)
140
+{
141
+    int i;
142
+    for (i = 0; i < nb_samples; i++)
143
+        dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128);
144
+}
145
+
146
+static inline void scale_samples_u8_small(uint8_t *dst, const uint8_t *src,
147
+                                          int nb_samples, int volume)
148
+{
149
+    int i;
150
+    for (i = 0; i < nb_samples; i++)
151
+        dst[i] = av_clip_uint8((((src[i] - 128) * volume + 128) >> 8) + 128);
152
+}
153
+
154
+static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src,
155
+                                     int nb_samples, int volume)
156
+{
157
+    int i;
158
+    int16_t *smp_dst       = (int16_t *)dst;
159
+    const int16_t *smp_src = (const int16_t *)src;
160
+    for (i = 0; i < nb_samples; i++)
161
+        smp_dst[i] = av_clip_int16(((int64_t)smp_src[i] * volume + 128) >> 8);
162
+}
163
+
164
+static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src,
165
+                                           int nb_samples, int volume)
166
+{
167
+    int i;
168
+    int16_t *smp_dst       = (int16_t *)dst;
169
+    const int16_t *smp_src = (const int16_t *)src;
170
+    for (i = 0; i < nb_samples; i++)
171
+        smp_dst[i] = av_clip_int16((smp_src[i] * volume + 128) >> 8);
172
+}
173
+
174
+static inline void scale_samples_s32(uint8_t *dst, const uint8_t *src,
175
+                                     int nb_samples, int volume)
176
+{
177
+    int i;
178
+    int32_t *smp_dst       = (int32_t *)dst;
179
+    const int32_t *smp_src = (const int32_t *)src;
180
+    for (i = 0; i < nb_samples; i++)
181
+        smp_dst[i] = av_clipl_int32((((int64_t)smp_src[i] * volume + 128) >> 8));
182
+}
183
+
184
+
185
+
186
+static void volume_init(VolumeContext *vol)
187
+{
188
+    vol->samples_align = 1;
189
+
190
+    switch (av_get_packed_sample_fmt(vol->sample_fmt)) {
191
+    case AV_SAMPLE_FMT_U8:
192
+        if (vol->volume_i < 0x1000000)
193
+            vol->scale_samples = scale_samples_u8_small;
194
+        else
195
+            vol->scale_samples = scale_samples_u8;
196
+        break;
197
+    case AV_SAMPLE_FMT_S16:
198
+        if (vol->volume_i < 0x10000)
199
+            vol->scale_samples = scale_samples_s16_small;
200
+        else
201
+            vol->scale_samples = scale_samples_s16;
202
+        break;
203
+    case AV_SAMPLE_FMT_S32:
204
+        vol->scale_samples = scale_samples_s32;
205
+        break;
206
+    case AV_SAMPLE_FMT_FLT:
207
+        avpriv_float_dsp_init(&vol->fdsp, 0);
208
+        vol->samples_align = 4;
209
+        break;
210
+    case AV_SAMPLE_FMT_DBL:
211
+        avpriv_float_dsp_init(&vol->fdsp, 0);
212
+        vol->samples_align = 8;
213
+        break;
214
+    }
215
+}
216
+
217
+static int config_output(AVFilterLink *outlink)
218
+{
219
+    AVFilterContext *ctx = outlink->src;
220
+    VolumeContext *vol   = ctx->priv;
221
+    AVFilterLink *inlink = ctx->inputs[0];
222
+
223
+    vol->sample_fmt = inlink->format;
224
+    vol->channels   = av_get_channel_layout_nb_channels(inlink->channel_layout);
225
+    vol->planes     = av_sample_fmt_is_planar(inlink->format) ? vol->channels : 1;
226
+
227
+    volume_init(vol);
228
+
229
+    return 0;
230
+}
231
+
232
+static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
233
+{
234
+    VolumeContext *vol    = inlink->dst->priv;
235
+    AVFilterLink *outlink = inlink->dst->outputs[0];
236
+    int nb_samples        = buf->audio->nb_samples;
237
+    AVFilterBufferRef *out_buf;
238
+
239
+    if (vol->volume == 1.0 || vol->volume_i == 256)
240
+        return ff_filter_frame(outlink, buf);
241
+
242
+    /* do volume scaling in-place if input buffer is writable */
243
+    if (buf->perms & AV_PERM_WRITE) {
244
+        out_buf = buf;
245
+    } else {
246
+        out_buf = ff_get_audio_buffer(inlink, AV_PERM_WRITE, nb_samples);
247
+        if (!out_buf)
248
+            return AVERROR(ENOMEM);
249
+        out_buf->pts = buf->pts;
250
+    }
251
+
252
+    if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) {
253
+        int p, plane_samples;
254
+
255
+        if (av_sample_fmt_is_planar(buf->format))
256
+            plane_samples = FFALIGN(nb_samples, vol->samples_align);
257
+        else
258
+            plane_samples = FFALIGN(nb_samples * vol->channels, vol->samples_align);
259
+
260
+        if (vol->precision == PRECISION_FIXED) {
261
+            for (p = 0; p < vol->planes; p++) {
262
+                vol->scale_samples(out_buf->extended_data[p],
263
+                                   buf->extended_data[p], plane_samples,
264
+                                   vol->volume_i);
265
+            }
266
+        } else if (av_get_packed_sample_fmt(vol->sample_fmt) == AV_SAMPLE_FMT_FLT) {
267
+            for (p = 0; p < vol->planes; p++) {
268
+                vol->fdsp.vector_fmul_scalar((float *)out_buf->extended_data[p],
269
+                                             (const float *)buf->extended_data[p],
270
+                                             vol->volume, plane_samples);
271
+            }
272
+        } else {
273
+            for (p = 0; p < vol->planes; p++) {
274
+                vol->fdsp.vector_dmul_scalar((double *)out_buf->extended_data[p],
275
+                                             (const double *)buf->extended_data[p],
276
+                                             vol->volume, plane_samples);
277
+            }
278
+        }
279
+    }
280
+
281
+    if (buf != out_buf)
282
+        avfilter_unref_buffer(buf);
283
+
284
+    return ff_filter_frame(outlink, out_buf);
285
+}
286
+
287
+static const AVFilterPad avfilter_af_volume_inputs[] = {
288
+    {
289
+        .name           = "default",
290
+        .type           = AVMEDIA_TYPE_AUDIO,
291
+        .filter_frame   = filter_frame,
292
+    },
293
+    { NULL }
294
+};
295
+
296
+static const AVFilterPad avfilter_af_volume_outputs[] = {
297
+    {
298
+        .name         = "default",
299
+        .type         = AVMEDIA_TYPE_AUDIO,
300
+        .config_props = config_output,
301
+    },
302
+    { NULL }
303
+};
304
+
305
+AVFilter avfilter_af_volume = {
306
+    .name           = "volume",
307
+    .description    = NULL_IF_CONFIG_SMALL("Change input volume."),
308
+    .query_formats  = query_formats,
309
+    .priv_size      = sizeof(VolumeContext),
310
+    .init           = init,
311
+    .inputs         = avfilter_af_volume_inputs,
312
+    .outputs        = avfilter_af_volume_outputs,
313
+};
0 314
new file mode 100644
... ...
@@ -0,0 +1,53 @@
0
+/*
1
+ * This file is part of Libav.
2
+ *
3
+ * Libav is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU Lesser General Public
5
+ * License as published by the Free Software Foundation; either
6
+ * version 2.1 of the License, or (at your option) any later version.
7
+ *
8
+ * Libav is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
+ * Lesser General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Lesser General Public
14
+ * License along with Libav; if not, write to the Free Software
15
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+ */
17
+
18
+/**
19
+ * @file
20
+ * audio volume filter
21
+ */
22
+
23
+#ifndef AVFILTER_AF_VOLUME_H
24
+#define AVFILTER_AF_VOLUME_H
25
+
26
+#include "libavutil/common.h"
27
+#include "libavutil/float_dsp.h"
28
+#include "libavutil/opt.h"
29
+#include "libavutil/samplefmt.h"
30
+
31
+enum PrecisionType {
32
+    PRECISION_FIXED = 0,
33
+    PRECISION_FLOAT,
34
+    PRECISION_DOUBLE,
35
+};
36
+
37
+typedef struct VolumeContext {
38
+    const AVClass *class;
39
+    AVFloatDSPContext fdsp;
40
+    enum PrecisionType precision;
41
+    double volume;
42
+    int    volume_i;
43
+    int    channels;
44
+    int    planes;
45
+    enum AVSampleFormat sample_fmt;
46
+
47
+    void (*scale_samples)(uint8_t *dst, const uint8_t *src, int nb_samples,
48
+                          int volume);
49
+    int samples_align;
50
+} VolumeContext;
51
+
52
+#endif /* AVFILTER_AF_VOLUME_H */
... ...
@@ -46,6 +46,7 @@ void avfilter_register_all(void)
46 46
     REGISTER_FILTER (CHANNELSPLIT,channelsplit,af);
47 47
     REGISTER_FILTER (JOIN,        join,        af);
48 48
     REGISTER_FILTER (RESAMPLE,    resample,    af);
49
+    REGISTER_FILTER (VOLUME,      volume,      af);
49 50
 
50 51
     REGISTER_FILTER (ANULLSRC,    anullsrc,    asrc);
51 52
 
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32
-#define LIBAVFILTER_VERSION_MINOR  2
32
+#define LIBAVFILTER_VERSION_MINOR  3
33 33
 #define LIBAVFILTER_VERSION_MICRO  0
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \