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/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 
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" },
53  { NULL },
54 };
55 
56 static const AVClass volume_class = {
57  .class_name = "volume filter",
58  .item_name = av_default_item_name,
59  .option = options,
60  .version = LIBAVUTIL_VERSION_INT,
61 };
62 
63 static av_cold int init(AVFilterContext *ctx)
64 {
65  VolumeContext *vol = ctx->priv;
66 
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);
72  } else {
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]);
76  }
77 
78  return 0;
79 }
80 
82 {
83  VolumeContext *vol = ctx->priv;
86  static const enum AVSampleFormat sample_fmts[][7] = {
87  /* PRECISION_FIXED */
88  {
96  },
97  /* PRECISION_FLOAT */
98  {
102  },
103  /* PRECISION_DOUBLE */
104  {
108  }
109  };
110 
111  layouts = ff_all_channel_layouts();
112  if (!layouts)
113  return AVERROR(ENOMEM);
114  ff_set_common_channel_layouts(ctx, layouts);
115 
116  formats = ff_make_format_list(sample_fmts[vol->precision]);
117  if (!formats)
118  return AVERROR(ENOMEM);
119  ff_set_common_formats(ctx, formats);
120 
121  formats = ff_all_samplerates();
122  if (!formats)
123  return AVERROR(ENOMEM);
124  ff_set_common_samplerates(ctx, formats);
125 
126  return 0;
127 }
128 
129 static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src,
130  int nb_samples, int volume)
131 {
132  int i;
133  for (i = 0; i < nb_samples; i++)
134  dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128);
135 }
136 
137 static inline void scale_samples_u8_small(uint8_t *dst, const uint8_t *src,
138  int nb_samples, int volume)
139 {
140  int i;
141  for (i = 0; i < nb_samples; i++)
142  dst[i] = av_clip_uint8((((src[i] - 128) * volume + 128) >> 8) + 128);
143 }
144 
145 static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src,
146  int nb_samples, int volume)
147 {
148  int i;
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);
153 }
154 
155 static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src,
156  int nb_samples, int volume)
157 {
158  int i;
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);
163 }
164 
165 static inline void scale_samples_s32(uint8_t *dst, const uint8_t *src,
166  int nb_samples, int volume)
167 {
168  int i;
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));
173 }
174 
175 
176 
178 {
179  vol->samples_align = 1;
180 
181  switch (av_get_packed_sample_fmt(vol->sample_fmt)) {
182  case AV_SAMPLE_FMT_U8:
183  if (vol->volume_i < 0x1000000)
185  else
187  break;
188  case AV_SAMPLE_FMT_S16:
189  if (vol->volume_i < 0x10000)
191  else
193  break;
194  case AV_SAMPLE_FMT_S32:
196  break;
197  case AV_SAMPLE_FMT_FLT:
198  avpriv_float_dsp_init(&vol->fdsp, 0);
199  vol->samples_align = 4;
200  break;
201  case AV_SAMPLE_FMT_DBL:
202  avpriv_float_dsp_init(&vol->fdsp, 0);
203  vol->samples_align = 8;
204  break;
205  }
206 
207  if (ARCH_X86)
208  ff_volume_init_x86(vol);
209 }
210 
211 static int config_output(AVFilterLink *outlink)
212 {
213  AVFilterContext *ctx = outlink->src;
214  VolumeContext *vol = ctx->priv;
215  AVFilterLink *inlink = ctx->inputs[0];
216 
217  vol->sample_fmt = inlink->format;
219  vol->planes = av_sample_fmt_is_planar(inlink->format) ? vol->channels : 1;
220 
221  volume_init(vol);
222 
223  return 0;
224 }
225 
226 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
227 {
228  VolumeContext *vol = inlink->dst->priv;
229  AVFilterLink *outlink = inlink->dst->outputs[0];
230  int nb_samples = buf->nb_samples;
231  AVFrame *out_buf;
232  int ret;
233 
234  if (vol->volume == 1.0 || vol->volume_i == 256)
235  return ff_filter_frame(outlink, buf);
236 
237  /* do volume scaling in-place if input buffer is writable */
238  if (av_frame_is_writable(buf)) {
239  out_buf = buf;
240  } else {
241  out_buf = ff_get_audio_buffer(inlink, nb_samples);
242  if (!out_buf)
243  return AVERROR(ENOMEM);
244  ret = av_frame_copy_props(out_buf, buf);
245  if (ret < 0) {
246  av_frame_free(&out_buf);
247  av_frame_free(&buf);
248  return ret;
249  }
250  }
251 
252  if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) {
253  int p, plane_samples;
254 
256  plane_samples = FFALIGN(nb_samples, vol->samples_align);
257  else
258  plane_samples = FFALIGN(nb_samples * vol->channels, vol->samples_align);
259 
260  if (vol->precision == PRECISION_FIXED) {
261  for (p = 0; p < vol->planes; p++) {
262  vol->scale_samples(out_buf->extended_data[p],
263  buf->extended_data[p], plane_samples,
264  vol->volume_i);
265  }
267  for (p = 0; p < vol->planes; p++) {
268  vol->fdsp.vector_fmul_scalar((float *)out_buf->extended_data[p],
269  (const float *)buf->extended_data[p],
270  vol->volume, plane_samples);
271  }
272  } else {
273  for (p = 0; p < vol->planes; p++) {
274  vol->fdsp.vector_dmul_scalar((double *)out_buf->extended_data[p],
275  (const double *)buf->extended_data[p],
276  vol->volume, plane_samples);
277  }
278  }
279  }
280 
281  emms_c();
282 
283  if (buf != out_buf)
284  av_frame_free(&buf);
285 
286  return ff_filter_frame(outlink, out_buf);
287 }
288 
290  {
291  .name = "default",
292  .type = AVMEDIA_TYPE_AUDIO,
293  .filter_frame = filter_frame,
294  },
295  { NULL }
296 };
297 
299  {
300  .name = "default",
301  .type = AVMEDIA_TYPE_AUDIO,
302  .config_props = config_output,
303  },
304  { NULL }
305 };
306 
308  .name = "volume",
309  .description = NULL_IF_CONFIG_SMALL("Change input volume."),
310  .query_formats = query_formats,
311  .priv_size = sizeof(VolumeContext),
312  .priv_class = &volume_class,
313  .init = init,
314  .inputs = avfilter_af_volume_inputs,
315  .outputs = avfilter_af_volume_outputs,
316 };
#define A
Definition: af_volume.c:43
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
AVOption.
Definition: opt.h:233
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
Main libavfilter public API header.
enum PrecisionType precision
Definition: af_volume.h:41
static av_cold void volume_init(VolumeContext *vol)
Definition: af_volume.c:177
AVFloatDSPContext fdsp
Definition: af_volume.h:40
#define ARCH_X86
Definition: config.h:33
static av_cold int init(AVFilterContext *ctx)
Definition: af_volume.c:63
static enum AVSampleFormat formats[]
signed 16 bits
Definition: samplefmt.h:52
#define FFALIGN(x, a)
Definition: common.h:62
AVFilter ff_af_volume
Definition: af_volume.c:307
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
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:728
uint8_t
#define av_cold
Definition: attributes.h:66
AV_SAMPLE_FMT_U8
AVOptions.
#define emms_c()
Definition: internal.h:46
int samples_align
Definition: af_volume.h:50
void(* scale_samples)(uint8_t *dst, const uint8_t *src, int nb_samples, int volume)
Definition: af_volume.h:48
#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:375
signed 32 bits, planar
Definition: samplefmt.h:59
static const AVOption options[]
Definition: af_volume.c:45
static const AVClass volume_class
Definition: af_volume.c:56
float, planar
Definition: samplefmt.h:60
static void scale_samples_s32(uint8_t *dst, const uint8_t *src, int nb_samples, int volume)
Definition: af_volume.c:165
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:55
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:142
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:148
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:81
signed 32 bits
Definition: samplefmt.h:53
static void scale_samples_s16(uint8_t *dst, const uint8_t *src, int nb_samples, int volume)
Definition: af_volume.c:145
audio channel layout utility functions
static void scale_samples_u8_small(uint8_t *dst, const uint8_t *src, int nb_samples, int volume)
Definition: af_volume.c:137
int32_t
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
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
static const AVFilterPad avfilter_af_volume_outputs[]
Definition: af_volume.c:298
static void scale_samples_u8(uint8_t *dst, const uint8_t *src, int nb_samples, int volume)
Definition: af_volume.c:129
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:244
double volume
Definition: af_volume.h:42
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:158
enum AVSampleFormat sample_fmt
Definition: af_volume.h:46
NULL
Definition: eval.c:55
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:301
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:68
av_default_item_name
Definition: dnxhdenc.c:45
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:111
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:101
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:238
static void scale_samples_s16_small(uint8_t *dst, const uint8_t *src, int nb_samples, int volume)
Definition: af_volume.c:155
static const char * precision_str[]
Definition: af_volume.c:38
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:363
void ff_volume_init_x86(VolumeContext *vol)
static const AVFilterPad avfilter_af_volume_inputs[]
Definition: af_volume.c:289
common internal and external API header
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
Definition: af_volume.c:226
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
unsigned 8 bits, planar
Definition: samplefmt.h:57
#define OFFSET(x)
Definition: af_volume.c:42
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:58
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:356
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:141
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:151
double, planar
Definition: samplefmt.h:61
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:362
simple arithmetic expression evaluator
static int config_output(AVFilterLink *outlink)
Definition: af_volume.c:211