]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_volume.c
Merge commit '88bd7fdc821aaa0cbcf44cf075c62aaa42121e3f'
[ffmpeg] / libavfilter / af_volume.c
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
3  * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * audio volume filter
25  */
26
27 #include "libavutil/audioconvert.h"
28 #include "libavutil/common.h"
29 #include "libavutil/eval.h"
30 #include "libavutil/float_dsp.h"
31 #include "libavutil/opt.h"
32 #include "audio.h"
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "internal.h"
36 #include "af_volume.h"
37
38 static const char *precision_str[] = {
39     "fixed", "float", "double"
40 };
41
42 #define OFFSET(x) offsetof(VolumeContext, x)
43 #define A AV_OPT_FLAG_AUDIO_PARAM
44 #define F AV_OPT_FLAG_FILTERING_PARAM
45
46 static const AVOption volume_options[] = {
47     { "volume", "set volume adjustment",
48             OFFSET(volume), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, 0, 0x7fffff, A|F },
49     { "precision", "select mathematical precision",
50             OFFSET(precision), AV_OPT_TYPE_INT, { .i64 = PRECISION_FLOAT }, PRECISION_FIXED, PRECISION_DOUBLE, A|F, "precision" },
51         { "fixed",  "select 8-bit fixed-point",     0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FIXED  }, INT_MIN, INT_MAX, A|F, "precision" },
52         { "float",  "select 32-bit floating-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FLOAT  }, INT_MIN, INT_MAX, A|F, "precision" },
53         { "double", "select 64-bit floating-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_DOUBLE }, INT_MIN, INT_MAX, A|F, "precision" },
54     { NULL },
55 };
56
57 AVFILTER_DEFINE_CLASS(volume);
58
59 static av_cold int init(AVFilterContext *ctx, const char *args)
60 {
61     VolumeContext *vol = ctx->priv;
62     static const char *shorthand[] = { "volume", "precision", NULL };
63     int ret;
64
65     vol->class = &volume_class;
66     av_opt_set_defaults(vol);
67
68     if ((ret = av_opt_set_from_string(vol, args, shorthand, "=", ":")) < 0)
69         return ret;
70
71     if (vol->precision == PRECISION_FIXED) {
72         vol->volume_i = (int)(vol->volume * 256 + 0.5);
73         vol->volume   = vol->volume_i / 256.0;
74         av_log(ctx, AV_LOG_VERBOSE, "volume:(%d/256)(%f)(%1.2fdB) precision:fixed\n",
75                vol->volume_i, vol->volume, 20.0*log(vol->volume)/M_LN10);
76     } else {
77         av_log(ctx, AV_LOG_VERBOSE, "volume:(%f)(%1.2fdB) precision:%s\n",
78                vol->volume, 20.0*log(vol->volume)/M_LN10,
79                precision_str[vol->precision]);
80     }
81
82     av_opt_free(vol);
83     return ret;
84 }
85
86 static int query_formats(AVFilterContext *ctx)
87 {
88     VolumeContext *vol = ctx->priv;
89     AVFilterFormats *formats = NULL;
90     AVFilterChannelLayouts *layouts;
91     static const enum AVSampleFormat sample_fmts[][7] = {
92         /* PRECISION_FIXED */
93         {
94             AV_SAMPLE_FMT_U8,
95             AV_SAMPLE_FMT_U8P,
96             AV_SAMPLE_FMT_S16,
97             AV_SAMPLE_FMT_S16P,
98             AV_SAMPLE_FMT_S32,
99             AV_SAMPLE_FMT_S32P,
100             AV_SAMPLE_FMT_NONE
101         },
102         /* PRECISION_FLOAT */
103         {
104             AV_SAMPLE_FMT_FLT,
105             AV_SAMPLE_FMT_FLTP,
106             AV_SAMPLE_FMT_NONE
107         },
108         /* PRECISION_DOUBLE */
109         {
110             AV_SAMPLE_FMT_DBL,
111             AV_SAMPLE_FMT_DBLP,
112             AV_SAMPLE_FMT_NONE
113         }
114     };
115
116     layouts = ff_all_channel_layouts();
117     if (!layouts)
118         return AVERROR(ENOMEM);
119     ff_set_common_channel_layouts(ctx, layouts);
120
121     formats = ff_make_format_list(sample_fmts[vol->precision]);
122     if (!formats)
123         return AVERROR(ENOMEM);
124     ff_set_common_formats(ctx, formats);
125
126     formats = ff_all_samplerates();
127     if (!formats)
128         return AVERROR(ENOMEM);
129     ff_set_common_samplerates(ctx, formats);
130
131     return 0;
132 }
133
134 static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src,
135                                     int nb_samples, int volume)
136 {
137     int i;
138     for (i = 0; i < nb_samples; i++)
139         dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128);
140 }
141
142 static inline void scale_samples_u8_small(uint8_t *dst, const uint8_t *src,
143                                           int nb_samples, int volume)
144 {
145     int i;
146     for (i = 0; i < nb_samples; i++)
147         dst[i] = av_clip_uint8((((src[i] - 128) * volume + 128) >> 8) + 128);
148 }
149
150 static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src,
151                                      int nb_samples, int volume)
152 {
153     int i;
154     int16_t *smp_dst       = (int16_t *)dst;
155     const int16_t *smp_src = (const int16_t *)src;
156     for (i = 0; i < nb_samples; i++)
157         smp_dst[i] = av_clip_int16(((int64_t)smp_src[i] * volume + 128) >> 8);
158 }
159
160 static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src,
161                                            int nb_samples, int volume)
162 {
163     int i;
164     int16_t *smp_dst       = (int16_t *)dst;
165     const int16_t *smp_src = (const int16_t *)src;
166     for (i = 0; i < nb_samples; i++)
167         smp_dst[i] = av_clip_int16((smp_src[i] * volume + 128) >> 8);
168 }
169
170 static inline void scale_samples_s32(uint8_t *dst, const uint8_t *src,
171                                      int nb_samples, int volume)
172 {
173     int i;
174     int32_t *smp_dst       = (int32_t *)dst;
175     const int32_t *smp_src = (const int32_t *)src;
176     for (i = 0; i < nb_samples; i++)
177         smp_dst[i] = av_clipl_int32((((int64_t)smp_src[i] * volume + 128) >> 8));
178 }
179
180 static void volume_init(VolumeContext *vol)
181 {
182     vol->samples_align = 1;
183
184     switch (av_get_packed_sample_fmt(vol->sample_fmt)) {
185     case AV_SAMPLE_FMT_U8:
186         if (vol->volume_i < 0x1000000)
187             vol->scale_samples = scale_samples_u8_small;
188         else
189             vol->scale_samples = scale_samples_u8;
190         break;
191     case AV_SAMPLE_FMT_S16:
192         if (vol->volume_i < 0x10000)
193             vol->scale_samples = scale_samples_s16_small;
194         else
195             vol->scale_samples = scale_samples_s16;
196         break;
197     case AV_SAMPLE_FMT_S32:
198         vol->scale_samples = scale_samples_s32;
199         break;
200     case AV_SAMPLE_FMT_FLT:
201         avpriv_float_dsp_init(&vol->fdsp, 0);
202         vol->samples_align = 4;
203         break;
204     case AV_SAMPLE_FMT_DBL:
205         avpriv_float_dsp_init(&vol->fdsp, 0);
206         vol->samples_align = 8;
207         break;
208     }
209
210     if (ARCH_X86)
211         ff_volume_init_x86(vol);
212 }
213
214 static int config_output(AVFilterLink *outlink)
215 {
216     AVFilterContext *ctx = outlink->src;
217     VolumeContext *vol   = ctx->priv;
218     AVFilterLink *inlink = ctx->inputs[0];
219
220     vol->sample_fmt = inlink->format;
221     vol->channels   = av_get_channel_layout_nb_channels(inlink->channel_layout);
222     vol->planes     = av_sample_fmt_is_planar(inlink->format) ? vol->channels : 1;
223
224     volume_init(vol);
225
226     return 0;
227 }
228
229 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
230 {
231     VolumeContext *vol    = inlink->dst->priv;
232     AVFilterLink *outlink = inlink->dst->outputs[0];
233     int nb_samples        = buf->audio->nb_samples;
234     AVFilterBufferRef *out_buf;
235
236     if (vol->volume == 1.0 || vol->volume_i == 256)
237         return ff_filter_frame(outlink, buf);
238
239     /* do volume scaling in-place if input buffer is writable */
240     if (buf->perms & AV_PERM_WRITE) {
241         out_buf = buf;
242     } else {
243         out_buf = ff_get_audio_buffer(inlink, AV_PERM_WRITE, nb_samples);
244         if (!out_buf)
245             return AVERROR(ENOMEM);
246         out_buf->pts = buf->pts;
247     }
248
249     if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) {
250         int p, plane_samples;
251
252         if (av_sample_fmt_is_planar(buf->format))
253             plane_samples = FFALIGN(nb_samples, vol->samples_align);
254         else
255             plane_samples = FFALIGN(nb_samples * vol->channels, vol->samples_align);
256
257         if (vol->precision == PRECISION_FIXED) {
258             for (p = 0; p < vol->planes; p++) {
259                 vol->scale_samples(out_buf->extended_data[p],
260                                    buf->extended_data[p], plane_samples,
261                                    vol->volume_i);
262             }
263         } else if (av_get_packed_sample_fmt(vol->sample_fmt) == AV_SAMPLE_FMT_FLT) {
264             for (p = 0; p < vol->planes; p++) {
265                 vol->fdsp.vector_fmul_scalar((float *)out_buf->extended_data[p],
266                                              (const float *)buf->extended_data[p],
267                                              vol->volume, plane_samples);
268             }
269         } else {
270             for (p = 0; p < vol->planes; p++) {
271                 vol->fdsp.vector_dmul_scalar((double *)out_buf->extended_data[p],
272                                              (const double *)buf->extended_data[p],
273                                              vol->volume, plane_samples);
274             }
275         }
276     }
277
278     if (buf != out_buf)
279         avfilter_unref_buffer(buf);
280
281     return ff_filter_frame(outlink, out_buf);
282 }
283
284 static const AVFilterPad avfilter_af_volume_inputs[] = {
285     {
286         .name           = "default",
287         .type           = AVMEDIA_TYPE_AUDIO,
288         .filter_frame   = filter_frame,
289     },
290     { NULL }
291 };
292
293 static const AVFilterPad avfilter_af_volume_outputs[] = {
294     {
295         .name         = "default",
296         .type         = AVMEDIA_TYPE_AUDIO,
297         .config_props = config_output,
298     },
299     { NULL }
300 };
301
302 AVFilter avfilter_af_volume = {
303     .name           = "volume",
304     .description    = NULL_IF_CONFIG_SMALL("Change input volume."),
305     .query_formats  = query_formats,
306     .priv_size      = sizeof(VolumeContext),
307     .init           = init,
308     .inputs         = avfilter_af_volume_inputs,
309     .outputs        = avfilter_af_volume_outputs,
310     .priv_class     = &volume_class,
311 };