af_afade.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * fade audio filter
24  */
25 
26 #include "libavutil/opt.h"
27 #include "audio.h"
28 #include "avfilter.h"
29 #include "internal.h"
30 
31 typedef struct {
32  const AVClass *class;
33  int type;
34  int curve;
36  int64_t start_sample;
37  int64_t duration;
38  int64_t start_time;
39 
40  void (*fade_samples)(uint8_t **dst, uint8_t * const *src,
41  int nb_samples, int channels, int direction,
42  int64_t start, int range, int curve);
44 
45 enum CurveType { TRI, QSIN, ESIN, HSIN, LOG, PAR, QUA, CUB, SQU, CBR };
46 
47 #define OFFSET(x) offsetof(AudioFadeContext, x)
48 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
49 
50 static const AVOption afade_options[] = {
51  { "type", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "type" },
52  { "t", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "type" },
53  { "in", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "type" },
54  { "out", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "type" },
55  { "start_sample", "set expression of sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
56  { "ss", "set expression of sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
57  { "nb_samples", "set expression for fade duration in samples", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX, FLAGS },
58  { "ns", "set expression for fade duration in samples", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX, FLAGS },
59  { "start_time", "set expression of time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
60  { "st", "set expression of time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
61  { "duration", "set expression for fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
62  { "d", "set expression for fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
63  { "curve", "set expression for fade curve", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, TRI, CBR, FLAGS, "curve" },
64  { "c", "set expression for fade curve", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, TRI, CBR, FLAGS, "curve" },
65  { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve" },
66  { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
67  { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
68  { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
69  { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve" },
70  { "par", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve" },
71  { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve" },
72  { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve" },
73  { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve" },
74  { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve" },
75  {NULL},
76 };
77 
79 
80 static av_cold int init(AVFilterContext *ctx)
81 {
82  AudioFadeContext *afade = ctx->priv;
83 
84  if (INT64_MAX - afade->nb_samples < afade->start_sample)
85  return AVERROR(EINVAL);
86 
87  return 0;
88 }
89 
91 {
94  static const enum AVSampleFormat sample_fmts[] = {
100  };
101 
102  layouts = ff_all_channel_layouts();
103  if (!layouts)
104  return AVERROR(ENOMEM);
105  ff_set_common_channel_layouts(ctx, layouts);
106 
107  formats = ff_make_format_list(sample_fmts);
108  if (!formats)
109  return AVERROR(ENOMEM);
110  ff_set_common_formats(ctx, formats);
111 
112  formats = ff_all_samplerates();
113  if (!formats)
114  return AVERROR(ENOMEM);
115  ff_set_common_samplerates(ctx, formats);
116 
117  return 0;
118 }
119 
120 static double fade_gain(int curve, int64_t index, int range)
121 {
122  double gain;
123 
124  gain = FFMAX(0.0, FFMIN(1.0, 1.0 * index / range));
125 
126  switch (curve) {
127  case QSIN:
128  gain = sin(gain * M_PI / 2.0);
129  break;
130  case ESIN:
131  gain = 1.0 - cos(M_PI / 4.0 * (pow(2.0*gain - 1, 3) + 1));
132  break;
133  case HSIN:
134  gain = (1.0 - cos(gain * M_PI)) / 2.0;
135  break;
136  case LOG:
137  gain = pow(0.1, (1 - gain) * 5.0);
138  break;
139  case PAR:
140  gain = (1 - (1 - gain) * (1 - gain));
141  break;
142  case QUA:
143  gain *= gain;
144  break;
145  case CUB:
146  gain = gain * gain * gain;
147  break;
148  case SQU:
149  gain = sqrt(gain);
150  break;
151  case CBR:
152  gain = cbrt(gain);
153  break;
154  }
155 
156  return gain;
157 }
158 
159 #define FADE_PLANAR(name, type) \
160 static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
161  int nb_samples, int channels, int dir, \
162  int64_t start, int range, int curve) \
163 { \
164  int i, c; \
165  \
166  for (i = 0; i < nb_samples; i++) { \
167  double gain = fade_gain(curve, start + i * dir, range); \
168  for (c = 0; c < channels; c++) { \
169  type *d = (type *)dst[c]; \
170  const type *s = (type *)src[c]; \
171  \
172  d[i] = s[i] * gain; \
173  } \
174  } \
175 }
176 
177 #define FADE(name, type) \
178 static void fade_samples_## name (uint8_t **dst, uint8_t * const *src, \
179  int nb_samples, int channels, int dir, \
180  int64_t start, int range, int curve) \
181 { \
182  type *d = (type *)dst[0]; \
183  const type *s = (type *)src[0]; \
184  int i, c, k = 0; \
185  \
186  for (i = 0; i < nb_samples; i++) { \
187  double gain = fade_gain(curve, start + i * dir, range); \
188  for (c = 0; c < channels; c++, k++) \
189  d[k] = s[k] * gain; \
190  } \
191 }
192 
193 FADE_PLANAR(dbl, double)
194 FADE_PLANAR(flt, float)
195 FADE_PLANAR(s16, int16_t)
196 FADE_PLANAR(s32, int32_t)
197 
198 FADE(dbl, double)
199 FADE(flt, float)
200 FADE(s16, int16_t)
201 FADE(s32, int32_t)
202 
203 static int config_output(AVFilterLink *outlink)
204 {
205  AVFilterContext *ctx = outlink->src;
206  AudioFadeContext *afade = ctx->priv;
207  AVFilterLink *inlink = ctx->inputs[0];
208 
209  switch (inlink->format) {
210  case AV_SAMPLE_FMT_DBL: afade->fade_samples = fade_samples_dbl; break;
211  case AV_SAMPLE_FMT_DBLP: afade->fade_samples = fade_samples_dblp; break;
212  case AV_SAMPLE_FMT_FLT: afade->fade_samples = fade_samples_flt; break;
213  case AV_SAMPLE_FMT_FLTP: afade->fade_samples = fade_samples_fltp; break;
214  case AV_SAMPLE_FMT_S16: afade->fade_samples = fade_samples_s16; break;
215  case AV_SAMPLE_FMT_S16P: afade->fade_samples = fade_samples_s16p; break;
216  case AV_SAMPLE_FMT_S32: afade->fade_samples = fade_samples_s32; break;
217  case AV_SAMPLE_FMT_S32P: afade->fade_samples = fade_samples_s32p; break;
218  }
219 
220  if (afade->duration)
221  afade->nb_samples = afade->duration * inlink->sample_rate / AV_TIME_BASE;
222  if (afade->start_time)
223  afade->start_sample = afade->start_time * inlink->sample_rate / AV_TIME_BASE;
224 
225  return 0;
226 }
227 
228 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
229 {
230  AudioFadeContext *afade = inlink->dst->priv;
231  AVFilterLink *outlink = inlink->dst->outputs[0];
232  int nb_samples = buf->nb_samples;
233  AVFrame *out_buf;
234  int64_t cur_sample = av_rescale_q(buf->pts, (AVRational){1, outlink->sample_rate}, outlink->time_base);
235 
236  if ((!afade->type && (afade->start_sample + afade->nb_samples < cur_sample)) ||
237  ( afade->type && (cur_sample + afade->nb_samples < afade->start_sample)))
238  return ff_filter_frame(outlink, buf);
239 
240  if (av_frame_is_writable(buf)) {
241  out_buf = buf;
242  } else {
243  out_buf = ff_get_audio_buffer(inlink, nb_samples);
244  if (!out_buf)
245  return AVERROR(ENOMEM);
246  av_frame_copy_props(out_buf, buf);
247  }
248 
249  if ((!afade->type && (cur_sample + nb_samples < afade->start_sample)) ||
250  ( afade->type && (afade->start_sample + afade->nb_samples < cur_sample))) {
251  av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
252  av_frame_get_channels(out_buf), out_buf->format);
253  } else {
254  int64_t start;
255 
256  if (!afade->type)
257  start = cur_sample - afade->start_sample;
258  else
259  start = afade->start_sample + afade->nb_samples - cur_sample;
260 
261  afade->fade_samples(out_buf->extended_data, buf->extended_data,
262  nb_samples, av_frame_get_channels(buf),
263  afade->type ? -1 : 1, start,
264  afade->nb_samples, afade->curve);
265  }
266 
267  if (buf != out_buf)
268  av_frame_free(&buf);
269 
270  return ff_filter_frame(outlink, out_buf);
271 }
272 
274  {
275  .name = "default",
276  .type = AVMEDIA_TYPE_AUDIO,
277  .filter_frame = filter_frame,
278  },
279  { NULL }
280 };
281 
283  {
284  .name = "default",
285  .type = AVMEDIA_TYPE_AUDIO,
286  .config_props = config_output,
287  },
288  { NULL }
289 };
290 
292  .name = "afade",
293  .description = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
294  .query_formats = query_formats,
295  .priv_size = sizeof(AudioFadeContext),
296  .init = init,
297  .inputs = avfilter_af_afade_inputs,
298  .outputs = avfilter_af_afade_outputs,
299  .priv_class = &afade_class,
300 };
static double fade_gain(int curve, int64_t index, int range)
Definition: af_afade.c:120
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:424
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVOption.
Definition: opt.h:251
static const AVFilterPad avfilter_af_afade_inputs[]
Definition: af_afade.c:273
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
static int config_output(AVFilterLink *outlink)
Definition: af_afade.c:203
location of range
signed 16 bits
Definition: samplefmt.h:52
Definition: af_afade.c:45
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:308
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:532
static int64_t start_time
Definition: ffplay.c:293
uint8_t
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define av_cold
Definition: attributes.h:78
AVOptions.
static int query_formats(AVFilterContext *ctx)
Definition: af_afade.c:90
#define FADE_PLANAR(name, type)
Definition: af_afade.c:159
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
integer sqrt
Definition: avutil.txt:2
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:545
signed 32 bits, planar
Definition: samplefmt.h:59
static int64_t duration
Definition: ffplay.c:294
float, planar
Definition: samplefmt.h:60
A filter pad used for either input or output.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:130
static av_always_inline double cbrt(double x)
Definition: libm.h:52
CurveType
Definition: af_afade.c:45
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:84
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:361
#define FFMAX(a, b)
Definition: common.h:56
void(* fade_samples)(uint8_t **dst, uint8_t *const *src, int nb_samples, int channels, int direction, int64_t start, int range, int curve)
Definition: af_afade.c:40
signed 32 bits
Definition: samplefmt.h:53
int av_frame_get_channels(const AVFrame *frame)
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:196
#define FFMIN(a, b)
Definition: common.h:58
int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition: samplefmt.c:249
int64_t start_sample
Definition: af_afade.c:36
AVFILTER_DEFINE_CLASS(afade)
Definition: af_afade.c:45
int32_t
Definition: af_afade.c:45
int64_t duration
Definition: af_afade.c:37
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout (...
Definition: formats.c:402
static const AVOption afade_options[]
Definition: af_afade.c:50
A list of supported channel layouts.
Definition: formats.h:85
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:134
NULL
Definition: eval.c:55
static const AVFilterPad avfilter_af_afade_outputs[]
Definition: af_afade.c:282
AVS_Value src
Definition: avisynth_c.h:523
typedef void(RENAME(mix_any_func_type))
void * buf
Definition: avisynth_c.h:594
Definition: af_afade.c:45
int64_t start_time
Definition: af_afade.c:38
Definition: af_afade.c:45
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:436
int index
Definition: gxfenc.c:89
rational number numerator/denominator
Definition: rational.h:43
Definition: af_afade.c:45
#define FLAGS
Definition: af_afade.c:48
const char * name
filter name
Definition: avfilter.h:437
Definition: af_afade.c:45
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
#define type
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:396
void ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:533
Definition: af_afade.c:45
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common formats
Definition: swscale.txt:33
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
#define OFFSET(x)
Definition: af_afade.c:47
static av_cold int init(AVFilterContext *ctx)
Definition: af_afade.c:80
Definition: af_afade.c:45
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:524
signed 16 bits, planar
Definition: samplefmt.h:58
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
Definition: af_afade.c:228
void INT64 start
Definition: avisynth_c.h:594
#define M_PI
Definition: mathematics.h:46
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:526
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:117
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
double, planar
Definition: samplefmt.h:61
#define FADE(name, type)
Definition: af_afade.c:177
Definition: af_afade.c:45
AVFilter avfilter_af_afade
Definition: af_afade.c:291