2 * Copyright (c) 2011 Stefano Sabatini
3 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/common.h"
29 #include "libavutil/eval.h"
30 #include "libavutil/float_dsp.h"
31 #include "libavutil/opt.h"
36 #include "af_volume.h"
38 static const char *precision_str
[] = {
39 "fixed", "float", "double"
42 #define OFFSET(x) offsetof(VolumeContext, x)
43 #define A AV_OPT_FLAG_AUDIO_PARAM
45 static const AVOption options
[] = {
46 { "volume", "Volume adjustment.",
47 OFFSET(volume
), AV_OPT_TYPE_DOUBLE
, { .dbl
= 1.0 }, 0, 0x7fffff, A
},
48 { "precision", "Mathematical precision.",
49 OFFSET(precision
), AV_OPT_TYPE_INT
, { .i64
= PRECISION_FLOAT
}, PRECISION_FIXED
, PRECISION_DOUBLE
, A
, "precision" },
50 { "fixed", "8-bit fixed-point.", 0, AV_OPT_TYPE_CONST
, { .i64
= PRECISION_FIXED
}, INT_MIN
, INT_MAX
, A
, "precision" },
51 { "float", "32-bit floating-point.", 0, AV_OPT_TYPE_CONST
, { .i64
= PRECISION_FLOAT
}, INT_MIN
, INT_MAX
, A
, "precision" },
52 { "double", "64-bit floating-point.", 0, AV_OPT_TYPE_CONST
, { .i64
= PRECISION_DOUBLE
}, INT_MIN
, INT_MAX
, A
, "precision" },
56 static const AVClass volume_class
= {
57 .class_name
= "volume filter",
58 .item_name
= av_default_item_name
,
60 .version
= LIBAVUTIL_VERSION_INT
,
63 static av_cold
int init(AVFilterContext
*ctx
)
65 VolumeContext
*vol
= ctx
->priv
;
67 if (vol
->precision
== PRECISION_FIXED
) {
68 vol
->volume_i
= (int)(vol
->volume
* 256 + 0.5);
69 vol
->volume
= vol
->volume_i
/ 256.0;
70 av_log(ctx
, AV_LOG_VERBOSE
, "volume:(%d/256)(%f)(%1.2fdB) precision:fixed\n",
71 vol
->volume_i
, vol
->volume
, 20.0*log(vol
->volume
)/M_LN10
);
73 av_log(ctx
, AV_LOG_VERBOSE
, "volume:(%f)(%1.2fdB) precision:%s\n",
74 vol
->volume
, 20.0*log(vol
->volume
)/M_LN10
,
75 precision_str
[vol
->precision
]);
81 static int query_formats(AVFilterContext
*ctx
)
83 VolumeContext
*vol
= ctx
->priv
;
84 AVFilterFormats
*formats
= NULL
;
85 AVFilterChannelLayouts
*layouts
;
86 static const enum AVSampleFormat sample_fmts
[][7] = {
103 /* PRECISION_DOUBLE */
111 layouts
= ff_all_channel_layouts();
113 return AVERROR(ENOMEM
);
114 ff_set_common_channel_layouts(ctx
, layouts
);
116 formats
= ff_make_format_list(sample_fmts
[vol
->precision
]);
118 return AVERROR(ENOMEM
);
119 ff_set_common_formats(ctx
, formats
);
121 formats
= ff_all_samplerates();
123 return AVERROR(ENOMEM
);
124 ff_set_common_samplerates(ctx
, formats
);
129 static inline void scale_samples_u8(uint8_t *dst
, const uint8_t *src
,
130 int nb_samples
, int volume
)
133 for (i
= 0; i
< nb_samples
; i
++)
134 dst
[i
] = av_clip_uint8(((((int64_t)src
[i
] - 128) * volume
+ 128) >> 8) + 128);
137 static inline void scale_samples_u8_small(uint8_t *dst
, const uint8_t *src
,
138 int nb_samples
, int volume
)
141 for (i
= 0; i
< nb_samples
; i
++)
142 dst
[i
] = av_clip_uint8((((src
[i
] - 128) * volume
+ 128) >> 8) + 128);
145 static inline void scale_samples_s16(uint8_t *dst
, const uint8_t *src
,
146 int nb_samples
, int volume
)
149 int16_t *smp_dst
= (int16_t *)dst
;
150 const int16_t *smp_src
= (const int16_t *)src
;
151 for (i
= 0; i
< nb_samples
; i
++)
152 smp_dst
[i
] = av_clip_int16(((int64_t)smp_src
[i
] * volume
+ 128) >> 8);
155 static inline void scale_samples_s16_small(uint8_t *dst
, const uint8_t *src
,
156 int nb_samples
, int volume
)
159 int16_t *smp_dst
= (int16_t *)dst
;
160 const int16_t *smp_src
= (const int16_t *)src
;
161 for (i
= 0; i
< nb_samples
; i
++)
162 smp_dst
[i
] = av_clip_int16((smp_src
[i
] * volume
+ 128) >> 8);
165 static inline void scale_samples_s32(uint8_t *dst
, const uint8_t *src
,
166 int nb_samples
, int volume
)
169 int32_t *smp_dst
= (int32_t *)dst
;
170 const int32_t *smp_src
= (const int32_t *)src
;
171 for (i
= 0; i
< nb_samples
; i
++)
172 smp_dst
[i
] = av_clipl_int32((((int64_t)smp_src
[i
] * volume
+ 128) >> 8));
177 static av_cold
void volume_init(VolumeContext
*vol
)
179 vol
->samples_align
= 1;
181 switch (av_get_packed_sample_fmt(vol
->sample_fmt
)) {
182 case AV_SAMPLE_FMT_U8
:
183 if (vol
->volume_i
< 0x1000000)
184 vol
->scale_samples
= scale_samples_u8_small
;
186 vol
->scale_samples
= scale_samples_u8
;
188 case AV_SAMPLE_FMT_S16
:
189 if (vol
->volume_i
< 0x10000)
190 vol
->scale_samples
= scale_samples_s16_small
;
192 vol
->scale_samples
= scale_samples_s16
;
194 case AV_SAMPLE_FMT_S32
:
195 vol
->scale_samples
= scale_samples_s32
;
197 case AV_SAMPLE_FMT_FLT
:
198 avpriv_float_dsp_init(&vol
->fdsp
, 0);
199 vol
->samples_align
= 4;
201 case AV_SAMPLE_FMT_DBL
:
202 avpriv_float_dsp_init(&vol
->fdsp
, 0);
203 vol
->samples_align
= 8;
208 ff_volume_init_x86(vol
);
211 static int config_output(AVFilterLink
*outlink
)
213 AVFilterContext
*ctx
= outlink
->src
;
214 VolumeContext
*vol
= ctx
->priv
;
215 AVFilterLink
*inlink
= ctx
->inputs
[0];
217 vol
->sample_fmt
= inlink
->format
;
218 vol
->channels
= av_get_channel_layout_nb_channels(inlink
->channel_layout
);
219 vol
->planes
= av_sample_fmt_is_planar(inlink
->format
) ? vol
->channels
: 1;
226 static int filter_frame(AVFilterLink
*inlink
, AVFrame
*buf
)
228 VolumeContext
*vol
= inlink
->dst
->priv
;
229 AVFilterLink
*outlink
= inlink
->dst
->outputs
[0];
230 int nb_samples
= buf
->nb_samples
;
233 if (vol
->volume
== 1.0 || vol
->volume_i
== 256)
234 return ff_filter_frame(outlink
, buf
);
236 /* do volume scaling in-place if input buffer is writable */
237 if (av_frame_is_writable(buf
)) {
240 out_buf
= ff_get_audio_buffer(inlink
, nb_samples
);
242 return AVERROR(ENOMEM
);
243 out_buf
->pts
= buf
->pts
;
246 if (vol
->precision
!= PRECISION_FIXED
|| vol
->volume_i
> 0) {
247 int p
, plane_samples
;
249 if (av_sample_fmt_is_planar(buf
->format
))
250 plane_samples
= FFALIGN(nb_samples
, vol
->samples_align
);
252 plane_samples
= FFALIGN(nb_samples
* vol
->channels
, vol
->samples_align
);
254 if (vol
->precision
== PRECISION_FIXED
) {
255 for (p
= 0; p
< vol
->planes
; p
++) {
256 vol
->scale_samples(out_buf
->extended_data
[p
],
257 buf
->extended_data
[p
], plane_samples
,
260 } else if (av_get_packed_sample_fmt(vol
->sample_fmt
) == AV_SAMPLE_FMT_FLT
) {
261 for (p
= 0; p
< vol
->planes
; p
++) {
262 vol
->fdsp
.vector_fmul_scalar((float *)out_buf
->extended_data
[p
],
263 (const float *)buf
->extended_data
[p
],
264 vol
->volume
, plane_samples
);
267 for (p
= 0; p
< vol
->planes
; p
++) {
268 vol
->fdsp
.vector_dmul_scalar((double *)out_buf
->extended_data
[p
],
269 (const double *)buf
->extended_data
[p
],
270 vol
->volume
, plane_samples
);
278 return ff_filter_frame(outlink
, out_buf
);
281 static const AVFilterPad avfilter_af_volume_inputs
[] = {
284 .type
= AVMEDIA_TYPE_AUDIO
,
285 .filter_frame
= filter_frame
,
290 static const AVFilterPad avfilter_af_volume_outputs
[] = {
293 .type
= AVMEDIA_TYPE_AUDIO
,
294 .config_props
= config_output
,
299 AVFilter ff_af_volume
= {
301 .description
= NULL_IF_CONFIG_SMALL("Change input volume."),
302 .query_formats
= query_formats
,
303 .priv_size
= sizeof(VolumeContext
),
304 .priv_class
= &volume_class
,
306 .inputs
= avfilter_af_volume_inputs
,
307 .outputs
= avfilter_af_volume_outputs
,