FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
af_volume.c
Go to the documentation of this file.
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 
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 static const char *const var_names[] = {
43  "n", ///< frame number (starting at zero)
44  "nb_channels", ///< number of channels
45  "nb_consumed_samples", ///< number of samples consumed by the filter
46  "nb_samples", ///< number of samples in the current frame
47  "pos", ///< position in the file of the frame
48  "pts", ///< frame presentation timestamp
49  "sample_rate", ///< sample rate
50  "startpts", ///< PTS at start of stream
51  "startt", ///< time at start of stream
52  "t", ///< time in the file of the frame
53  "tb", ///< timebase
54  "volume", ///< last set value
55  NULL
56 };
57 
58 #define OFFSET(x) offsetof(VolumeContext, x)
59 #define A AV_OPT_FLAG_AUDIO_PARAM
60 #define F AV_OPT_FLAG_FILTERING_PARAM
61 
62 static const AVOption volume_options[] = {
63  { "volume", "set volume adjustment expression",
64  OFFSET(volume_expr), AV_OPT_TYPE_STRING, { .str = "1.0" }, .flags = A|F },
65  { "precision", "select mathematical precision",
66  OFFSET(precision), AV_OPT_TYPE_INT, { .i64 = PRECISION_FLOAT }, PRECISION_FIXED, PRECISION_DOUBLE, A|F, "precision" },
67  { "fixed", "select 8-bit fixed-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FIXED }, INT_MIN, INT_MAX, A|F, "precision" },
68  { "float", "select 32-bit floating-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FLOAT }, INT_MIN, INT_MAX, A|F, "precision" },
69  { "double", "select 64-bit floating-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_DOUBLE }, INT_MIN, INT_MAX, A|F, "precision" },
70  { "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" },
71  { "once", "eval volume expression once", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_ONCE}, .flags = A|F, .unit = "eval" },
72  { "frame", "eval volume expression per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = A|F, .unit = "eval" },
73  { NULL }
74 };
75 
76 AVFILTER_DEFINE_CLASS(volume);
77 
78 static int set_expr(AVExpr **pexpr, const char *expr, void *log_ctx)
79 {
80  int ret;
81  AVExpr *old = NULL;
82 
83  if (*pexpr)
84  old = *pexpr;
85  ret = av_expr_parse(pexpr, expr, var_names,
86  NULL, NULL, NULL, NULL, 0, log_ctx);
87  if (ret < 0) {
88  av_log(log_ctx, AV_LOG_ERROR,
89  "Error when evaluating the volume expression '%s'\n", expr);
90  *pexpr = old;
91  return ret;
92  }
93 
94  av_expr_free(old);
95  return 0;
96 }
97 
98 static av_cold int init(AVFilterContext *ctx)
99 {
100  VolumeContext *vol = ctx->priv;
101  return set_expr(&vol->volume_pexpr, vol->volume_expr, ctx);
102 }
103 
104 static av_cold void uninit(AVFilterContext *ctx)
105 {
106  VolumeContext *vol = ctx->priv;
108  av_opt_free(vol);
109 }
110 
112 {
113  VolumeContext *vol = ctx->priv;
114  AVFilterFormats *formats = NULL;
116  static const enum AVSampleFormat sample_fmts[][7] = {
117  [PRECISION_FIXED] = {
125  },
126  [PRECISION_FLOAT] = {
130  },
131  [PRECISION_DOUBLE] = {
135  }
136  };
137 
138  layouts = ff_all_channel_counts();
139  if (!layouts)
140  return AVERROR(ENOMEM);
141  ff_set_common_channel_layouts(ctx, layouts);
142 
143  formats = ff_make_format_list(sample_fmts[vol->precision]);
144  if (!formats)
145  return AVERROR(ENOMEM);
146  ff_set_common_formats(ctx, formats);
147 
148  formats = ff_all_samplerates();
149  if (!formats)
150  return AVERROR(ENOMEM);
151  ff_set_common_samplerates(ctx, formats);
152 
153  return 0;
154 }
155 
156 static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src,
157  int nb_samples, int volume)
158 {
159  int i;
160  for (i = 0; i < nb_samples; i++)
161  dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128);
162 }
163 
164 static inline void scale_samples_u8_small(uint8_t *dst, const uint8_t *src,
165  int nb_samples, int volume)
166 {
167  int i;
168  for (i = 0; i < nb_samples; i++)
169  dst[i] = av_clip_uint8((((src[i] - 128) * volume + 128) >> 8) + 128);
170 }
171 
172 static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src,
173  int nb_samples, int volume)
174 {
175  int i;
176  int16_t *smp_dst = (int16_t *)dst;
177  const int16_t *smp_src = (const int16_t *)src;
178  for (i = 0; i < nb_samples; i++)
179  smp_dst[i] = av_clip_int16(((int64_t)smp_src[i] * volume + 128) >> 8);
180 }
181 
182 static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src,
183  int nb_samples, int volume)
184 {
185  int i;
186  int16_t *smp_dst = (int16_t *)dst;
187  const int16_t *smp_src = (const int16_t *)src;
188  for (i = 0; i < nb_samples; i++)
189  smp_dst[i] = av_clip_int16((smp_src[i] * volume + 128) >> 8);
190 }
191 
192 static inline void scale_samples_s32(uint8_t *dst, const uint8_t *src,
193  int nb_samples, int volume)
194 {
195  int i;
196  int32_t *smp_dst = (int32_t *)dst;
197  const int32_t *smp_src = (const int32_t *)src;
198  for (i = 0; i < nb_samples; i++)
199  smp_dst[i] = av_clipl_int32((((int64_t)smp_src[i] * volume + 128) >> 8));
200 }
201 
203 {
204  vol->samples_align = 1;
205 
206  switch (av_get_packed_sample_fmt(vol->sample_fmt)) {
207  case AV_SAMPLE_FMT_U8:
208  if (vol->volume_i < 0x1000000)
210  else
212  break;
213  case AV_SAMPLE_FMT_S16:
214  if (vol->volume_i < 0x10000)
216  else
218  break;
219  case AV_SAMPLE_FMT_S32:
221  break;
222  case AV_SAMPLE_FMT_FLT:
223  avpriv_float_dsp_init(&vol->fdsp, 0);
224  vol->samples_align = 4;
225  break;
226  case AV_SAMPLE_FMT_DBL:
227  avpriv_float_dsp_init(&vol->fdsp, 0);
228  vol->samples_align = 8;
229  break;
230  }
231 
232  if (ARCH_X86)
233  ff_volume_init_x86(vol);
234 }
235 
236 static int set_volume(AVFilterContext *ctx)
237 {
238  VolumeContext *vol = ctx->priv;
239 
240  vol->volume = av_expr_eval(vol->volume_pexpr, vol->var_values, NULL);
241  if (isnan(vol->volume)) {
242  if (vol->eval_mode == EVAL_MODE_ONCE) {
243  av_log(ctx, AV_LOG_ERROR, "Invalid value NaN for volume\n");
244  return AVERROR(EINVAL);
245  } else {
246  av_log(ctx, AV_LOG_WARNING, "Invalid value NaN for volume, setting to 0\n");
247  vol->volume = 0;
248  }
249  }
250  vol->var_values[VAR_VOLUME] = vol->volume;
251 
252  av_log(ctx, AV_LOG_VERBOSE, "n:%f t:%f pts:%f precision:%s ",
253  vol->var_values[VAR_N], vol->var_values[VAR_T], vol->var_values[VAR_PTS],
254  precision_str[vol->precision]);
255 
256  if (vol->precision == PRECISION_FIXED) {
257  vol->volume_i = (int)(vol->volume * 256 + 0.5);
258  vol->volume = vol->volume_i / 256.0;
259  av_log(ctx, AV_LOG_VERBOSE, "volume_i:%d/255 ", vol->volume_i);
260  }
261  av_log(ctx, AV_LOG_VERBOSE, "volume:%f volume_dB:%f\n",
262  vol->volume, 20.0*log(vol->volume)/M_LN10);
263 
264  volume_init(vol);
265  return 0;
266 }
267 
268 static int config_output(AVFilterLink *outlink)
269 {
270  AVFilterContext *ctx = outlink->src;
271  VolumeContext *vol = ctx->priv;
272  AVFilterLink *inlink = ctx->inputs[0];
273 
274  vol->sample_fmt = inlink->format;
275  vol->channels = inlink->channels;
276  vol->planes = av_sample_fmt_is_planar(inlink->format) ? vol->channels : 1;
277 
278  vol->var_values[VAR_N] =
280  vol->var_values[VAR_NB_SAMPLES] =
281  vol->var_values[VAR_POS] =
282  vol->var_values[VAR_PTS] =
283  vol->var_values[VAR_STARTPTS] =
284  vol->var_values[VAR_STARTT] =
285  vol->var_values[VAR_T] =
286  vol->var_values[VAR_VOLUME] = NAN;
287 
288  vol->var_values[VAR_NB_CHANNELS] = inlink->channels;
289  vol->var_values[VAR_TB] = av_q2d(inlink->time_base);
290  vol->var_values[VAR_SAMPLE_RATE] = inlink->sample_rate;
291 
292  av_log(inlink->src, AV_LOG_VERBOSE, "tb:%f sample_rate:%f nb_channels:%f\n",
293  vol->var_values[VAR_TB],
296 
297  return set_volume(ctx);
298 }
299 
300 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
301  char *res, int res_len, int flags)
302 {
303  VolumeContext *vol = ctx->priv;
304  int ret = AVERROR(ENOSYS);
305 
306  if (!strcmp(cmd, "volume")) {
307  if ((ret = set_expr(&vol->volume_pexpr, args, ctx)) < 0)
308  return ret;
309  if (vol->eval_mode == EVAL_MODE_ONCE)
310  set_volume(ctx);
311  }
312 
313  return ret;
314 }
315 
316 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
317 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
318 #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
319 
320 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
321 {
322  AVFilterContext *ctx = inlink->dst;
323  VolumeContext *vol = inlink->dst->priv;
324  AVFilterLink *outlink = inlink->dst->outputs[0];
325  int nb_samples = buf->nb_samples;
326  AVFrame *out_buf;
327  int64_t pos;
328  int ret;
329 
330  if (isnan(vol->var_values[VAR_STARTPTS])) {
331  vol->var_values[VAR_STARTPTS] = TS2D(buf->pts);
332  vol->var_values[VAR_STARTT ] = TS2T(buf->pts, inlink->time_base);
333  }
334  vol->var_values[VAR_PTS] = TS2D(buf->pts);
335  vol->var_values[VAR_T ] = TS2T(buf->pts, inlink->time_base);
336  vol->var_values[VAR_N ] = inlink->frame_count;
337 
338  pos = av_frame_get_pkt_pos(buf);
339  vol->var_values[VAR_POS] = pos == -1 ? NAN : pos;
340  if (vol->eval_mode == EVAL_MODE_FRAME)
341  set_volume(ctx);
342 
343  if (vol->volume == 1.0 || vol->volume_i == 256) {
344  out_buf = buf;
345  goto end;
346  }
347 
348  /* do volume scaling in-place if input buffer is writable */
349  if (av_frame_is_writable(buf)) {
350  out_buf = buf;
351  } else {
352  out_buf = ff_get_audio_buffer(inlink, nb_samples);
353  if (!out_buf)
354  return AVERROR(ENOMEM);
355  ret = av_frame_copy_props(out_buf, buf);
356  if (ret < 0) {
357  av_frame_free(&out_buf);
358  av_frame_free(&buf);
359  return ret;
360  }
361  }
362 
363  if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) {
364  int p, plane_samples;
365 
367  plane_samples = FFALIGN(nb_samples, vol->samples_align);
368  else
369  plane_samples = FFALIGN(nb_samples * vol->channels, vol->samples_align);
370 
371  if (vol->precision == PRECISION_FIXED) {
372  for (p = 0; p < vol->planes; p++) {
373  vol->scale_samples(out_buf->extended_data[p],
374  buf->extended_data[p], plane_samples,
375  vol->volume_i);
376  }
378  for (p = 0; p < vol->planes; p++) {
379  vol->fdsp.vector_fmul_scalar((float *)out_buf->extended_data[p],
380  (const float *)buf->extended_data[p],
381  vol->volume, plane_samples);
382  }
383  } else {
384  for (p = 0; p < vol->planes; p++) {
385  vol->fdsp.vector_dmul_scalar((double *)out_buf->extended_data[p],
386  (const double *)buf->extended_data[p],
387  vol->volume, plane_samples);
388  }
389  }
390  }
391 
392  if (buf != out_buf)
393  av_frame_free(&buf);
394 
395 end:
397  return ff_filter_frame(outlink, out_buf);
398 }
399 
401  {
402  .name = "default",
403  .type = AVMEDIA_TYPE_AUDIO,
404  .filter_frame = filter_frame,
405  },
406  { NULL }
407 };
408 
410  {
411  .name = "default",
412  .type = AVMEDIA_TYPE_AUDIO,
413  .config_props = config_output,
414  },
415  { NULL }
416 };
417 
419  .name = "volume",
420  .description = NULL_IF_CONFIG_SMALL("Change input volume."),
421  .query_formats = query_formats,
422  .priv_size = sizeof(VolumeContext),
423  .priv_class = &volume_class,
424  .init = init,
425  .uninit = uninit,
426  .inputs = avfilter_af_volume_inputs,
427  .outputs = avfilter_af_volume_outputs,
430 };