Libav
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 Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21 
28 #include "libavutil/common.h"
29 #include "libavutil/eval.h"
30 #include "libavutil/float_dsp.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/replaygain.h"
34 
35 #include "audio.h"
36 #include "avfilter.h"
37 #include "formats.h"
38 #include "internal.h"
39 #include "af_volume.h"
40 
41 static const char *precision_str[] = {
42  "fixed", "float", "double"
43 };
44 
45 #define OFFSET(x) offsetof(VolumeContext, x)
46 #define A AV_OPT_FLAG_AUDIO_PARAM
47 
48 static const AVOption options[] = {
49  { "volume", "Volume adjustment.",
50  OFFSET(volume), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, 0, 0x7fffff, A },
51  { "precision", "Mathematical precision.",
52  OFFSET(precision), AV_OPT_TYPE_INT, { .i64 = PRECISION_FLOAT }, PRECISION_FIXED, PRECISION_DOUBLE, A, "precision" },
53  { "fixed", "8-bit fixed-point.", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FIXED }, INT_MIN, INT_MAX, A, "precision" },
54  { "float", "32-bit floating-point.", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FLOAT }, INT_MIN, INT_MAX, A, "precision" },
55  { "double", "64-bit floating-point.", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_DOUBLE }, INT_MIN, INT_MAX, A, "precision" },
56  { "replaygain", "Apply replaygain side data when present",
57  OFFSET(replaygain), AV_OPT_TYPE_INT, { .i64 = REPLAYGAIN_DROP }, REPLAYGAIN_DROP, REPLAYGAIN_ALBUM, A, "replaygain" },
58  { "drop", "replaygain side data is dropped", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_DROP }, 0, 0, A, "replaygain" },
59  { "ignore", "replaygain side data is ignored", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_IGNORE }, 0, 0, A, "replaygain" },
60  { "track", "track gain is preferred", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_TRACK }, 0, 0, A, "replaygain" },
61  { "album", "album gain is preferred", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_ALBUM }, 0, 0, A, "replaygain" },
62  { "replaygain_preamp", "Apply replaygain pre-amplification",
63  OFFSET(replaygain_preamp), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, -15.0, 15.0, A },
64  { "replaygain_noclip", "Apply replaygain clipping prevention",
65  OFFSET(replaygain_noclip), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, A },
66  { NULL },
67 };
68 
69 static const AVClass volume_class = {
70  .class_name = "volume filter",
71  .item_name = av_default_item_name,
72  .option = options,
73  .version = LIBAVUTIL_VERSION_INT,
74 };
75 
76 static av_cold int init(AVFilterContext *ctx)
77 {
78  VolumeContext *vol = ctx->priv;
79 
80  if (vol->precision == PRECISION_FIXED) {
81  vol->volume_i = (int)(vol->volume * 256 + 0.5);
82  vol->volume = vol->volume_i / 256.0;
83  av_log(ctx, AV_LOG_VERBOSE, "volume:(%d/256)(%f)(%1.2fdB) precision:fixed\n",
84  vol->volume_i, vol->volume, 20.0*log(vol->volume)/M_LN10);
85  } else {
86  av_log(ctx, AV_LOG_VERBOSE, "volume:(%f)(%1.2fdB) precision:%s\n",
87  vol->volume, 20.0*log(vol->volume)/M_LN10,
88  precision_str[vol->precision]);
89  }
90 
91  return 0;
92 }
93 
95 {
96  VolumeContext *vol = ctx->priv;
99  static const enum AVSampleFormat sample_fmts[][7] = {
100  /* PRECISION_FIXED */
101  {
109  },
110  /* PRECISION_FLOAT */
111  {
115  },
116  /* PRECISION_DOUBLE */
117  {
121  }
122  };
123 
124  layouts = ff_all_channel_layouts();
125  if (!layouts)
126  return AVERROR(ENOMEM);
127  ff_set_common_channel_layouts(ctx, layouts);
128 
129  formats = ff_make_format_list(sample_fmts[vol->precision]);
130  if (!formats)
131  return AVERROR(ENOMEM);
132  ff_set_common_formats(ctx, formats);
133 
134  formats = ff_all_samplerates();
135  if (!formats)
136  return AVERROR(ENOMEM);
137  ff_set_common_samplerates(ctx, formats);
138 
139  return 0;
140 }
141 
142 static inline void scale_samples_u8(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(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128);
148 }
149 
150 static inline void scale_samples_u8_small(uint8_t *dst, const uint8_t *src,
151  int nb_samples, int volume)
152 {
153  int i;
154  for (i = 0; i < nb_samples; i++)
155  dst[i] = av_clip_uint8((((src[i] - 128) * volume + 128) >> 8) + 128);
156 }
157 
158 static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src,
159  int nb_samples, int volume)
160 {
161  int i;
162  int16_t *smp_dst = (int16_t *)dst;
163  const int16_t *smp_src = (const int16_t *)src;
164  for (i = 0; i < nb_samples; i++)
165  smp_dst[i] = av_clip_int16(((int64_t)smp_src[i] * volume + 128) >> 8);
166 }
167 
168 static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src,
169  int nb_samples, int volume)
170 {
171  int i;
172  int16_t *smp_dst = (int16_t *)dst;
173  const int16_t *smp_src = (const int16_t *)src;
174  for (i = 0; i < nb_samples; i++)
175  smp_dst[i] = av_clip_int16((smp_src[i] * volume + 128) >> 8);
176 }
177 
178 static inline void scale_samples_s32(uint8_t *dst, const uint8_t *src,
179  int nb_samples, int volume)
180 {
181  int i;
182  int32_t *smp_dst = (int32_t *)dst;
183  const int32_t *smp_src = (const int32_t *)src;
184  for (i = 0; i < nb_samples; i++)
185  smp_dst[i] = av_clipl_int32((((int64_t)smp_src[i] * volume + 128) >> 8));
186 }
187 
188 
189 
191 {
192  vol->samples_align = 1;
193 
194  switch (av_get_packed_sample_fmt(vol->sample_fmt)) {
195  case AV_SAMPLE_FMT_U8:
196  if (vol->volume_i < 0x1000000)
198  else
200  break;
201  case AV_SAMPLE_FMT_S16:
202  if (vol->volume_i < 0x10000)
204  else
206  break;
207  case AV_SAMPLE_FMT_S32:
209  break;
210  case AV_SAMPLE_FMT_FLT:
211  avpriv_float_dsp_init(&vol->fdsp, 0);
212  vol->samples_align = 4;
213  break;
214  case AV_SAMPLE_FMT_DBL:
215  avpriv_float_dsp_init(&vol->fdsp, 0);
216  vol->samples_align = 8;
217  break;
218  }
219 
220  if (ARCH_X86)
221  ff_volume_init_x86(vol);
222 }
223 
224 static int config_output(AVFilterLink *outlink)
225 {
226  AVFilterContext *ctx = outlink->src;
227  VolumeContext *vol = ctx->priv;
228  AVFilterLink *inlink = ctx->inputs[0];
229 
230  vol->sample_fmt = inlink->format;
232  vol->planes = av_sample_fmt_is_planar(inlink->format) ? vol->channels : 1;
233 
234  volume_init(vol);
235 
236  return 0;
237 }
238 
239 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
240 {
241  VolumeContext *vol = inlink->dst->priv;
242  AVFilterLink *outlink = inlink->dst->outputs[0];
243  int nb_samples = buf->nb_samples;
244  AVFrame *out_buf;
246  int ret;
247 
248  if (sd && vol->replaygain != REPLAYGAIN_IGNORE) {
249  if (vol->replaygain != REPLAYGAIN_DROP) {
250  AVReplayGain *replaygain = (AVReplayGain*)sd->data;
251  int32_t gain = 100000;
252  uint32_t peak = 100000;
253  float g, p;
254 
255  if (vol->replaygain == REPLAYGAIN_TRACK &&
256  replaygain->track_gain != INT32_MIN) {
257  gain = replaygain->track_gain;
258 
259  if (replaygain->track_peak != 0)
260  peak = replaygain->track_peak;
261  } else if (replaygain->album_gain != INT32_MIN) {
262  gain = replaygain->album_gain;
263 
264  if (replaygain->album_peak != 0)
265  peak = replaygain->album_peak;
266  } else {
267  av_log(inlink->dst, AV_LOG_WARNING, "Both ReplayGain gain "
268  "values are unknown.\n");
269  }
270  g = gain / 100000.0f;
271  p = peak / 100000.0f;
272 
273  av_log(inlink->dst, AV_LOG_VERBOSE,
274  "Using gain %f dB from replaygain side data.\n", g);
275 
276  vol->volume = pow(10, (g + vol->replaygain_preamp) / 20);
277  if (vol->replaygain_noclip)
278  vol->volume = FFMIN(vol->volume, 1.0 / p);
279  vol->volume_i = (int)(vol->volume * 256 + 0.5);
280 
281  volume_init(vol);
282  }
284  }
285 
286  if (vol->volume == 1.0 || vol->volume_i == 256)
287  return ff_filter_frame(outlink, buf);
288 
289  /* do volume scaling in-place if input buffer is writable */
290  if (av_frame_is_writable(buf)) {
291  out_buf = buf;
292  } else {
293  out_buf = ff_get_audio_buffer(inlink, nb_samples);
294  if (!out_buf)
295  return AVERROR(ENOMEM);
296  ret = av_frame_copy_props(out_buf, buf);
297  if (ret < 0) {
298  av_frame_free(&out_buf);
299  av_frame_free(&buf);
300  return ret;
301  }
302  }
303 
304  if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) {
305  int p, plane_samples;
306 
308  plane_samples = FFALIGN(nb_samples, vol->samples_align);
309  else
310  plane_samples = FFALIGN(nb_samples * vol->channels, vol->samples_align);
311 
312  if (vol->precision == PRECISION_FIXED) {
313  for (p = 0; p < vol->planes; p++) {
314  vol->scale_samples(out_buf->extended_data[p],
315  buf->extended_data[p], plane_samples,
316  vol->volume_i);
317  }
319  for (p = 0; p < vol->planes; p++) {
320  vol->fdsp.vector_fmul_scalar((float *)out_buf->extended_data[p],
321  (const float *)buf->extended_data[p],
322  vol->volume, plane_samples);
323  }
324  } else {
325  for (p = 0; p < vol->planes; p++) {
326  vol->fdsp.vector_dmul_scalar((double *)out_buf->extended_data[p],
327  (const double *)buf->extended_data[p],
328  vol->volume, plane_samples);
329  }
330  }
331  }
332 
333  emms_c();
334 
335  if (buf != out_buf)
336  av_frame_free(&buf);
337 
338  return ff_filter_frame(outlink, out_buf);
339 }
340 
342  {
343  .name = "default",
344  .type = AVMEDIA_TYPE_AUDIO,
345  .filter_frame = filter_frame,
346  },
347  { NULL }
348 };
349 
351  {
352  .name = "default",
353  .type = AVMEDIA_TYPE_AUDIO,
354  .config_props = config_output,
355  },
356  { NULL }
357 };
358 
360  .name = "volume",
361  .description = NULL_IF_CONFIG_SMALL("Change input volume."),
362  .query_formats = query_formats,
363  .priv_size = sizeof(VolumeContext),
364  .priv_class = &volume_class,
365  .init = init,
366  .inputs = avfilter_af_volume_inputs,
367  .outputs = avfilter_af_volume_outputs,
368 };
float, planar
Definition: samplefmt.h:72
#define A
Definition: af_volume.c:46
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
AVOption.
Definition: opt.h:234
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:232
Main libavfilter public API header.
enum PrecisionType precision
Definition: af_volume.h:48
static av_cold void volume_init(VolumeContext *vol)
Definition: af_volume.c:190
AVFloatDSPContext fdsp
Definition: af_volume.h:47
#define ARCH_X86
Definition: config.h:33
double, planar
Definition: samplefmt.h:73
static av_cold int init(AVFilterContext *ctx)
Definition: af_volume.c:76
static enum AVSampleFormat formats[]
uint32_t track_peak
Peak track amplitude, with 100000 representing full scale (but values may overflow).
Definition: replaygain.h:40
#define FFALIGN(x, a)
Definition: common.h:62
AVFilter ff_af_volume
Definition: af_volume.c:359
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:477
double replaygain_preamp
Definition: af_volume.h:50
const char * name
Pad name.
Definition: internal.h:42
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:571
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:733
uint8_t
#define av_cold
Definition: attributes.h:66
AV_SAMPLE_FMT_U8
AVOptions.
#define emms_c()
Definition: internal.h:47
int samples_align
Definition: af_volume.h:60
void(* scale_samples)(uint8_t *dst, const uint8_t *src, int nb_samples, int volume)
Definition: af_volume.h:58
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:139
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:379
static const AVOption options[]
Definition: af_volume.c:48
signed 32 bits
Definition: samplefmt.h:65
int32_t album_gain
Same as track_gain, but for the whole album.
Definition: replaygain.h:44
static const AVClass volume_class
Definition: af_volume.c:69
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:101
static void scale_samples_s32(uint8_t *dst, const uint8_t *src, int nb_samples, int volume)
Definition: af_volume.c:178
A filter pad used for either input or output.
Definition: internal.h:36
audio volume filter
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:57
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:69
sample_fmts
Definition: avconv_filter.c:68
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
g
Definition: yuv2rgb.c:535
void * priv
private data for use by the filter
Definition: avfilter.h:584
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
void(* vector_dmul_scalar)(double *dst, const double *src, double mul, int len)
Multiply a vector of double by a scalar double.
Definition: float_dsp.h:84
static int query_formats(AVFilterContext *ctx)
Definition: af_volume.c:94
enum ReplayGainType replaygain
Definition: af_volume.h:49
static void scale_samples_s16(uint8_t *dst, const uint8_t *src, int nb_samples, int volume)
Definition: af_volume.c:158
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:57
signed 32 bits, planar
Definition: samplefmt.h:71
static void scale_samples_u8_small(uint8_t *dst, const uint8_t *src, int nb_samples, int volume)
Definition: af_volume.c:150
int replaygain_noclip
Definition: af_volume.h:51
int32_t
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
Definition: float_dsp.h:69
unsigned 8 bits, planar
Definition: samplefmt.h:69
static const AVFilterPad avfilter_af_volume_outputs[]
Definition: af_volume.c:350
static void scale_samples_u8(uint8_t *dst, const uint8_t *src, int nb_samples, int volume)
Definition: af_volume.c:142
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout/s...
Definition: formats.c:248
if(ac->has_optimized_func)
double volume
Definition: af_volume.h:52
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:186
enum AVSampleFormat sample_fmt
Definition: af_volume.h:56
NULL
Definition: eval.c:55
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:61
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:309
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:68
av_default_item_name
Definition: dnxhdenc.c:52
uint8_t * data
Definition: frame.h:104
void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
If side data of the supplied type exists in the frame, free it and remove it from the frame...
Definition: frame.c:545
Describe the class of an AVClass context structure.
Definition: log.h:33
Filter definition.
Definition: avfilter.h:421
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:221
const char * name
Filter name.
Definition: avfilter.h:425
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:578
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:433
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:242
static void scale_samples_s16_small(uint8_t *dst, const uint8_t *src, int nb_samples, int volume)
Definition: af_volume.c:168
static const char * precision_str[]
Definition: af_volume.c:41
av_cold void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
Initialize a float DSP context.
Definition: float_dsp.c:115
void ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:367
void ff_volume_init_x86(VolumeContext *vol)
static const AVFilterPad avfilter_af_volume_inputs[]
Definition: af_volume.c:341
common internal and external API header
enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt)
Get the packed alternative form of the given sample format.
Definition: samplefmt.c:64
signed 16 bits
Definition: samplefmt.h:64
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
Definition: af_volume.c:239
uint32_t album_peak
Same as track_peak, but for the whole album,.
Definition: replaygain.h:48
#define OFFSET(x)
Definition: af_volume.c:45
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:563
signed 16 bits, planar
Definition: samplefmt.h:70
int32_t track_gain
Track replay gain in microbels (divide by 100000 to get the value in dB).
Definition: replaygain.h:35
void ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:360
ReplayGain information in the form of the AVReplayGain struct.
Definition: frame.h:76
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:169
ReplayGain information (see http://wiki.hydrogenaudio.org/index.php?title=ReplayGain_1.0_specification).
Definition: replaygain.h:30
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:179
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:367
simple arithmetic expression evaluator
static int config_output(AVFilterLink *outlink)
Definition: af_volume.c:224