2 * Copyright (c) 2011 Stefano Sabatini
3 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
5 * This file is part of FFmpeg.
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.
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.
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
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/common.h"
29 #include "libavutil/eval.h"
30 #include "libavutil/ffmath.h"
31 #include "libavutil/float_dsp.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/replaygain.h"
40 #include "af_volume.h"
42 static const char * const precision_str[] = {
43 "fixed", "float", "double"
46 static const char *const var_names[] = {
47 "n", ///< frame number (starting at zero)
48 "nb_channels", ///< number of channels
49 "nb_consumed_samples", ///< number of samples consumed by the filter
50 "nb_samples", ///< number of samples in the current frame
51 "pos", ///< position in the file of the frame
52 "pts", ///< frame presentation timestamp
53 "sample_rate", ///< sample rate
54 "startpts", ///< PTS at start of stream
55 "startt", ///< time at start of stream
56 "t", ///< time in the file of the frame
58 "volume", ///< last set value
62 #define OFFSET(x) offsetof(VolumeContext, x)
63 #define A AV_OPT_FLAG_AUDIO_PARAM
64 #define F AV_OPT_FLAG_FILTERING_PARAM
65 #define T AV_OPT_FLAG_RUNTIME_PARAM
67 static const AVOption volume_options[] = {
68 { "volume", "set volume adjustment expression",
69 OFFSET(volume_expr), AV_OPT_TYPE_STRING, { .str = "1.0" }, .flags = A|F|T },
70 { "precision", "select mathematical precision",
71 OFFSET(precision), AV_OPT_TYPE_INT, { .i64 = PRECISION_FLOAT }, PRECISION_FIXED, PRECISION_DOUBLE, A|F, "precision" },
72 { "fixed", "select 8-bit fixed-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FIXED }, INT_MIN, INT_MAX, A|F, "precision" },
73 { "float", "select 32-bit floating-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FLOAT }, INT_MIN, INT_MAX, A|F, "precision" },
74 { "double", "select 64-bit floating-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_DOUBLE }, INT_MIN, INT_MAX, A|F, "precision" },
75 { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_ONCE}, 0, EVAL_MODE_NB-1, .flags = A|F, "eval" },
76 { "once", "eval volume expression once", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_ONCE}, .flags = A|F, .unit = "eval" },
77 { "frame", "eval volume expression per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = A|F, .unit = "eval" },
78 { "replaygain", "Apply replaygain side data when present",
79 OFFSET(replaygain), AV_OPT_TYPE_INT, { .i64 = REPLAYGAIN_DROP }, REPLAYGAIN_DROP, REPLAYGAIN_ALBUM, A|F, "replaygain" },
80 { "drop", "replaygain side data is dropped", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_DROP }, 0, 0, A|F, "replaygain" },
81 { "ignore", "replaygain side data is ignored", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_IGNORE }, 0, 0, A|F, "replaygain" },
82 { "track", "track gain is preferred", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_TRACK }, 0, 0, A|F, "replaygain" },
83 { "album", "album gain is preferred", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_ALBUM }, 0, 0, A|F, "replaygain" },
84 { "replaygain_preamp", "Apply replaygain pre-amplification",
85 OFFSET(replaygain_preamp), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, -15.0, 15.0, A|F },
86 { "replaygain_noclip", "Apply replaygain clipping prevention",
87 OFFSET(replaygain_noclip), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, A|F },
91 AVFILTER_DEFINE_CLASS(volume);
93 static int set_expr(AVExpr **pexpr, const char *expr, void *log_ctx)
100 ret = av_expr_parse(pexpr, expr, var_names,
101 NULL, NULL, NULL, NULL, 0, log_ctx);
103 av_log(log_ctx, AV_LOG_ERROR,
104 "Error when evaluating the volume expression '%s'\n", expr);
113 static av_cold int init(AVFilterContext *ctx)
115 VolumeContext *vol = ctx->priv;
117 vol->fdsp = avpriv_float_dsp_alloc(0);
119 return AVERROR(ENOMEM);
121 return set_expr(&vol->volume_pexpr, vol->volume_expr, ctx);
124 static av_cold void uninit(AVFilterContext *ctx)
126 VolumeContext *vol = ctx->priv;
127 av_expr_free(vol->volume_pexpr);
129 av_freep(&vol->fdsp);
132 static int query_formats(AVFilterContext *ctx)
134 VolumeContext *vol = ctx->priv;
135 AVFilterFormats *formats = NULL;
136 AVFilterChannelLayouts *layouts;
137 static const enum AVSampleFormat sample_fmts[][7] = {
138 [PRECISION_FIXED] = {
147 [PRECISION_FLOAT] = {
152 [PRECISION_DOUBLE] = {
160 layouts = ff_all_channel_counts();
162 return AVERROR(ENOMEM);
163 ret = ff_set_common_channel_layouts(ctx, layouts);
167 formats = ff_make_format_list(sample_fmts[vol->precision]);
169 return AVERROR(ENOMEM);
170 ret = ff_set_common_formats(ctx, formats);
174 formats = ff_all_samplerates();
176 return AVERROR(ENOMEM);
177 return ff_set_common_samplerates(ctx, formats);
180 static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src,
181 int nb_samples, int volume)
184 for (i = 0; i < nb_samples; i++)
185 dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128);
188 static inline void scale_samples_u8_small(uint8_t *dst, const uint8_t *src,
189 int nb_samples, int volume)
192 for (i = 0; i < nb_samples; i++)
193 dst[i] = av_clip_uint8((((src[i] - 128) * volume + 128) >> 8) + 128);
196 static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src,
197 int nb_samples, int volume)
200 int16_t *smp_dst = (int16_t *)dst;
201 const int16_t *smp_src = (const int16_t *)src;
202 for (i = 0; i < nb_samples; i++)
203 smp_dst[i] = av_clip_int16(((int64_t)smp_src[i] * volume + 128) >> 8);
206 static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src,
207 int nb_samples, int volume)
210 int16_t *smp_dst = (int16_t *)dst;
211 const int16_t *smp_src = (const int16_t *)src;
212 for (i = 0; i < nb_samples; i++)
213 smp_dst[i] = av_clip_int16((smp_src[i] * volume + 128) >> 8);
216 static inline void scale_samples_s32(uint8_t *dst, const uint8_t *src,
217 int nb_samples, int volume)
220 int32_t *smp_dst = (int32_t *)dst;
221 const int32_t *smp_src = (const int32_t *)src;
222 for (i = 0; i < nb_samples; i++)
223 smp_dst[i] = av_clipl_int32((((int64_t)smp_src[i] * volume + 128) >> 8));
226 static av_cold void volume_init(VolumeContext *vol)
228 vol->samples_align = 1;
230 switch (av_get_packed_sample_fmt(vol->sample_fmt)) {
231 case AV_SAMPLE_FMT_U8:
232 if (vol->volume_i < 0x1000000)
233 vol->scale_samples = scale_samples_u8_small;
235 vol->scale_samples = scale_samples_u8;
237 case AV_SAMPLE_FMT_S16:
238 if (vol->volume_i < 0x10000)
239 vol->scale_samples = scale_samples_s16_small;
241 vol->scale_samples = scale_samples_s16;
243 case AV_SAMPLE_FMT_S32:
244 vol->scale_samples = scale_samples_s32;
246 case AV_SAMPLE_FMT_FLT:
247 vol->samples_align = 4;
249 case AV_SAMPLE_FMT_DBL:
250 vol->samples_align = 8;
255 ff_volume_init_x86(vol);
258 static int set_volume(AVFilterContext *ctx)
260 VolumeContext *vol = ctx->priv;
262 vol->volume = av_expr_eval(vol->volume_pexpr, vol->var_values, NULL);
263 if (isnan(vol->volume)) {
264 if (vol->eval_mode == EVAL_MODE_ONCE) {
265 av_log(ctx, AV_LOG_ERROR, "Invalid value NaN for volume\n");
266 return AVERROR(EINVAL);
268 av_log(ctx, AV_LOG_WARNING, "Invalid value NaN for volume, setting to 0\n");
272 vol->var_values[VAR_VOLUME] = vol->volume;
274 av_log(ctx, AV_LOG_VERBOSE, "n:%f t:%f pts:%f precision:%s ",
275 vol->var_values[VAR_N], vol->var_values[VAR_T], vol->var_values[VAR_PTS],
276 precision_str[vol->precision]);
278 if (vol->precision == PRECISION_FIXED) {
279 vol->volume_i = (int)(vol->volume * 256 + 0.5);
280 vol->volume = vol->volume_i / 256.0;
281 av_log(ctx, AV_LOG_VERBOSE, "volume_i:%d/255 ", vol->volume_i);
283 av_log(ctx, AV_LOG_VERBOSE, "volume:%f volume_dB:%f\n",
284 vol->volume, 20.0*log10(vol->volume));
290 static int config_output(AVFilterLink *outlink)
292 AVFilterContext *ctx = outlink->src;
293 VolumeContext *vol = ctx->priv;
294 AVFilterLink *inlink = ctx->inputs[0];
296 vol->sample_fmt = inlink->format;
297 vol->channels = inlink->channels;
298 vol->planes = av_sample_fmt_is_planar(inlink->format) ? vol->channels : 1;
300 vol->var_values[VAR_N] =
301 vol->var_values[VAR_NB_CONSUMED_SAMPLES] =
302 vol->var_values[VAR_NB_SAMPLES] =
303 vol->var_values[VAR_POS] =
304 vol->var_values[VAR_PTS] =
305 vol->var_values[VAR_STARTPTS] =
306 vol->var_values[VAR_STARTT] =
307 vol->var_values[VAR_T] =
308 vol->var_values[VAR_VOLUME] = NAN;
310 vol->var_values[VAR_NB_CHANNELS] = inlink->channels;
311 vol->var_values[VAR_TB] = av_q2d(inlink->time_base);
312 vol->var_values[VAR_SAMPLE_RATE] = inlink->sample_rate;
314 av_log(inlink->src, AV_LOG_VERBOSE, "tb:%f sample_rate:%f nb_channels:%f\n",
315 vol->var_values[VAR_TB],
316 vol->var_values[VAR_SAMPLE_RATE],
317 vol->var_values[VAR_NB_CHANNELS]);
319 return set_volume(ctx);
322 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
323 char *res, int res_len, int flags)
325 VolumeContext *vol = ctx->priv;
326 int ret = AVERROR(ENOSYS);
328 if (!strcmp(cmd, "volume")) {
329 if ((ret = set_expr(&vol->volume_pexpr, args, ctx)) < 0)
331 if (vol->eval_mode == EVAL_MODE_ONCE)
338 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
340 AVFilterContext *ctx = inlink->dst;
341 VolumeContext *vol = inlink->dst->priv;
342 AVFilterLink *outlink = inlink->dst->outputs[0];
343 int nb_samples = buf->nb_samples;
346 AVFrameSideData *sd = av_frame_get_side_data(buf, AV_FRAME_DATA_REPLAYGAIN);
349 if (sd && vol->replaygain != REPLAYGAIN_IGNORE) {
350 if (vol->replaygain != REPLAYGAIN_DROP) {
351 AVReplayGain *replaygain = (AVReplayGain*)sd->data;
352 int32_t gain = 100000;
353 uint32_t peak = 100000;
356 if (vol->replaygain == REPLAYGAIN_TRACK &&
357 replaygain->track_gain != INT32_MIN) {
358 gain = replaygain->track_gain;
360 if (replaygain->track_peak != 0)
361 peak = replaygain->track_peak;
362 } else if (replaygain->album_gain != INT32_MIN) {
363 gain = replaygain->album_gain;
365 if (replaygain->album_peak != 0)
366 peak = replaygain->album_peak;
368 av_log(inlink->dst, AV_LOG_WARNING, "Both ReplayGain gain "
369 "values are unknown.\n");
371 g = gain / 100000.0f;
372 p = peak / 100000.0f;
374 av_log(inlink->dst, AV_LOG_VERBOSE,
375 "Using gain %f dB from replaygain side data.\n", g);
377 vol->volume = ff_exp10((g + vol->replaygain_preamp) / 20);
378 if (vol->replaygain_noclip)
379 vol->volume = FFMIN(vol->volume, 1.0 / p);
380 vol->volume_i = (int)(vol->volume * 256 + 0.5);
384 av_frame_remove_side_data(buf, AV_FRAME_DATA_REPLAYGAIN);
387 if (isnan(vol->var_values[VAR_STARTPTS])) {
388 vol->var_values[VAR_STARTPTS] = TS2D(buf->pts);
389 vol->var_values[VAR_STARTT ] = TS2T(buf->pts, inlink->time_base);
391 vol->var_values[VAR_PTS] = TS2D(buf->pts);
392 vol->var_values[VAR_T ] = TS2T(buf->pts, inlink->time_base);
393 vol->var_values[VAR_N ] = inlink->frame_count_out;
396 vol->var_values[VAR_POS] = pos == -1 ? NAN : pos;
397 if (vol->eval_mode == EVAL_MODE_FRAME)
400 if (vol->volume == 1.0 || vol->volume_i == 256) {
405 /* do volume scaling in-place if input buffer is writable */
406 if (av_frame_is_writable(buf)
407 && (vol->precision != PRECISION_FIXED || vol->volume_i > 0)) {
410 out_buf = ff_get_audio_buffer(outlink, nb_samples);
413 return AVERROR(ENOMEM);
415 ret = av_frame_copy_props(out_buf, buf);
417 av_frame_free(&out_buf);
423 if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) {
424 int p, plane_samples;
426 if (av_sample_fmt_is_planar(buf->format))
427 plane_samples = FFALIGN(nb_samples, vol->samples_align);
429 plane_samples = FFALIGN(nb_samples * vol->channels, vol->samples_align);
431 if (vol->precision == PRECISION_FIXED) {
432 for (p = 0; p < vol->planes; p++) {
433 vol->scale_samples(out_buf->extended_data[p],
434 buf->extended_data[p], plane_samples,
437 } else if (av_get_packed_sample_fmt(vol->sample_fmt) == AV_SAMPLE_FMT_FLT) {
438 for (p = 0; p < vol->planes; p++) {
439 vol->fdsp->vector_fmul_scalar((float *)out_buf->extended_data[p],
440 (const float *)buf->extended_data[p],
441 vol->volume, plane_samples);
444 for (p = 0; p < vol->planes; p++) {
445 vol->fdsp->vector_dmul_scalar((double *)out_buf->extended_data[p],
446 (const double *)buf->extended_data[p],
447 vol->volume, plane_samples);
458 vol->var_values[VAR_NB_CONSUMED_SAMPLES] += out_buf->nb_samples;
459 return ff_filter_frame(outlink, out_buf);
462 static const AVFilterPad avfilter_af_volume_inputs[] = {
465 .type = AVMEDIA_TYPE_AUDIO,
466 .filter_frame = filter_frame,
471 static const AVFilterPad avfilter_af_volume_outputs[] = {
474 .type = AVMEDIA_TYPE_AUDIO,
475 .config_props = config_output,
480 AVFilter ff_af_volume = {
482 .description = NULL_IF_CONFIG_SMALL("Change input volume."),
483 .query_formats = query_formats,
484 .priv_size = sizeof(VolumeContext),
485 .priv_class = &volume_class,
488 .inputs = avfilter_af_volume_inputs,
489 .outputs = avfilter_af_volume_outputs,
490 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
491 .process_command = process_command,