FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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", "fade-in", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "type" },
54  { "out", "fade-out", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "type" },
55  { "start_sample", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
56  { "ss", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
57  { "nb_samples", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX, FLAGS },
58  { "ns", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX, FLAGS },
59  { "start_time", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
60  { "st", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
61  { "duration", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
62  { "d", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
63  { "curve", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, TRI, CBR, FLAGS, "curve" },
64  { "c", "set fade curve type", 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 *s = ctx->priv;
83 
84  if (INT64_MAX - s->nb_samples < s->start_sample)
85  return AVERROR(EINVAL);
86 
87  return 0;
88 }
89 
91 {
94  static const enum AVSampleFormat sample_fmts[] = {
100  };
101  int ret;
102 
103  layouts = ff_all_channel_layouts();
104  if (!layouts)
105  return AVERROR(ENOMEM);
106  ret = ff_set_common_channel_layouts(ctx, layouts);
107  if (ret < 0)
108  return ret;
109 
110  formats = ff_make_format_list(sample_fmts);
111  if (!formats)
112  return AVERROR(ENOMEM);
113  ret = ff_set_common_formats(ctx, formats);
114  if (ret < 0)
115  return ret;
116 
117  formats = ff_all_samplerates();
118  if (!formats)
119  return AVERROR(ENOMEM);
120  return ff_set_common_samplerates(ctx, formats);
121 }
122 
123 static double fade_gain(int curve, int64_t index, int range)
124 {
125  double gain;
126 
127  gain = FFMAX(0.0, FFMIN(1.0, 1.0 * index / range));
128 
129  switch (curve) {
130  case QSIN:
131  gain = sin(gain * M_PI / 2.0);
132  break;
133  case ESIN:
134  gain = 1.0 - cos(M_PI / 4.0 * (pow(2.0*gain - 1, 3) + 1));
135  break;
136  case HSIN:
137  gain = (1.0 - cos(gain * M_PI)) / 2.0;
138  break;
139  case LOG:
140  gain = pow(0.1, (1 - gain) * 5.0);
141  break;
142  case PAR:
143  gain = (1 - (1 - gain) * (1 - gain));
144  break;
145  case QUA:
146  gain *= gain;
147  break;
148  case CUB:
149  gain = gain * gain * gain;
150  break;
151  case SQU:
152  gain = sqrt(gain);
153  break;
154  case CBR:
155  gain = cbrt(gain);
156  break;
157  }
158 
159  return gain;
160 }
161 
162 #define FADE_PLANAR(name, type) \
163 static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
164  int nb_samples, int channels, int dir, \
165  int64_t start, int range, int curve) \
166 { \
167  int i, c; \
168  \
169  for (i = 0; i < nb_samples; i++) { \
170  double gain = fade_gain(curve, start + i * dir, range); \
171  for (c = 0; c < channels; c++) { \
172  type *d = (type *)dst[c]; \
173  const type *s = (type *)src[c]; \
174  \
175  d[i] = s[i] * gain; \
176  } \
177  } \
178 }
179 
180 #define FADE(name, type) \
181 static void fade_samples_## name (uint8_t **dst, uint8_t * const *src, \
182  int nb_samples, int channels, int dir, \
183  int64_t start, int range, int curve) \
184 { \
185  type *d = (type *)dst[0]; \
186  const type *s = (type *)src[0]; \
187  int i, c, k = 0; \
188  \
189  for (i = 0; i < nb_samples; i++) { \
190  double gain = fade_gain(curve, start + i * dir, range); \
191  for (c = 0; c < channels; c++, k++) \
192  d[k] = s[k] * gain; \
193  } \
194 }
195 
196 FADE_PLANAR(dbl, double)
197 FADE_PLANAR(flt, float)
198 FADE_PLANAR(s16, int16_t)
199 FADE_PLANAR(s32, int32_t)
200 
201 FADE(dbl, double)
202 FADE(flt, float)
203 FADE(s16, int16_t)
204 FADE(s32, int32_t)
205 
206 static int config_input(AVFilterLink *inlink)
207 {
208  AVFilterContext *ctx = inlink->dst;
209  AudioFadeContext *s = ctx->priv;
210 
211  switch (inlink->format) {
212  case AV_SAMPLE_FMT_DBL: s->fade_samples = fade_samples_dbl; break;
213  case AV_SAMPLE_FMT_DBLP: s->fade_samples = fade_samples_dblp; break;
214  case AV_SAMPLE_FMT_FLT: s->fade_samples = fade_samples_flt; break;
215  case AV_SAMPLE_FMT_FLTP: s->fade_samples = fade_samples_fltp; break;
216  case AV_SAMPLE_FMT_S16: s->fade_samples = fade_samples_s16; break;
217  case AV_SAMPLE_FMT_S16P: s->fade_samples = fade_samples_s16p; break;
218  case AV_SAMPLE_FMT_S32: s->fade_samples = fade_samples_s32; break;
219  case AV_SAMPLE_FMT_S32P: s->fade_samples = fade_samples_s32p; break;
220  }
221 
222  if (s->duration)
223  s->nb_samples = av_rescale(s->duration, inlink->sample_rate, AV_TIME_BASE);
224  if (s->start_time)
225  s->start_sample = av_rescale(s->start_time, inlink->sample_rate, AV_TIME_BASE);
226 
227  return 0;
228 }
229 
230 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
231 {
232  AudioFadeContext *s = inlink->dst->priv;
233  AVFilterLink *outlink = inlink->dst->outputs[0];
234  int nb_samples = buf->nb_samples;
235  AVFrame *out_buf;
236  int64_t cur_sample = av_rescale_q(buf->pts, inlink->time_base, (AVRational){1, inlink->sample_rate});
237 
238  if ((!s->type && (s->start_sample + s->nb_samples < cur_sample)) ||
239  ( s->type && (cur_sample + s->nb_samples < s->start_sample)))
240  return ff_filter_frame(outlink, buf);
241 
242  if (av_frame_is_writable(buf)) {
243  out_buf = buf;
244  } else {
245  out_buf = ff_get_audio_buffer(inlink, nb_samples);
246  if (!out_buf)
247  return AVERROR(ENOMEM);
248  av_frame_copy_props(out_buf, buf);
249  }
250 
251  if ((!s->type && (cur_sample + nb_samples < s->start_sample)) ||
252  ( s->type && (s->start_sample + s->nb_samples < cur_sample))) {
253  av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
254  av_frame_get_channels(out_buf), out_buf->format);
255  } else {
256  int64_t start;
257 
258  if (!s->type)
259  start = cur_sample - s->start_sample;
260  else
261  start = s->start_sample + s->nb_samples - cur_sample;
262 
263  s->fade_samples(out_buf->extended_data, buf->extended_data,
264  nb_samples, av_frame_get_channels(buf),
265  s->type ? -1 : 1, start,
266  s->nb_samples, s->curve);
267  }
268 
269  if (buf != out_buf)
270  av_frame_free(&buf);
271 
272  return ff_filter_frame(outlink, out_buf);
273 }
274 
276  {
277  .name = "default",
278  .type = AVMEDIA_TYPE_AUDIO,
279  .filter_frame = filter_frame,
280  .config_props = config_input,
281  },
282  { NULL }
283 };
284 
286  {
287  .name = "default",
288  .type = AVMEDIA_TYPE_AUDIO,
289  },
290  { NULL }
291 };
292 
294  .name = "afade",
295  .description = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
296  .query_formats = query_formats,
297  .priv_size = sizeof(AudioFadeContext),
298  .init = init,
299  .inputs = avfilter_af_afade_inputs,
300  .outputs = avfilter_af_afade_outputs,
301  .priv_class = &afade_class,
303 };
float, planar
Definition: samplefmt.h:70
#define NULL
Definition: coverity.c:32
int 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:523
AVFilter ff_af_afade
Definition: af_afade.c:293
const char * s
Definition: avisynth_c.h:631
static double fade_gain(int curve, int64_t index, int range)
Definition: af_afade.c:123
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVOption.
Definition: opt.h:255
static const AVFilterPad avfilter_af_afade_inputs[]
Definition: af_afade.c:275
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:248
Main libavfilter public API header.
double, planar
Definition: samplefmt.h:71
static enum AVSampleFormat formats[]
Definition: af_afade.c:45
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:451
const char * name
Pad name.
Definition: internal.h:67
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1145
static int64_t start_time
Definition: ffplay.c:320
uint8_t
#define av_cold
Definition: attributes.h:74
AVOptions.
static int query_formats(AVFilterContext *ctx)
Definition: af_afade.c:90
#define FADE_PLANAR(name, type)
Definition: af_afade.c:162
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:257
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
static int64_t duration
Definition: ffplay.c:321
signed 32 bits
Definition: samplefmt.h:63
A filter pad used for either input or output.
Definition: internal.h:61
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:140
int 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:542
static av_always_inline double cbrt(double x)
Definition: libm.h:52
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:235
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:70
#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:148
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
void * priv
private data for use by the filter
Definition: avfilter.h:654
#define FFMAX(a, b)
Definition: common.h:64
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:127
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:247
#define FFMIN(a, b)
Definition: common.h:66
signed 32 bits, planar
Definition: samplefmt.h:69
int64_t start_sample
Definition: af_afade.c:36
ret
Definition: avfilter.c:974
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
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 (w...
Definition: formats.c:385
static const AVOption afade_options[]
Definition: af_afade.c:50
A list of supported channel layouts.
Definition: formats.h:85
static const AVFilterPad avfilter_af_afade_outputs[]
Definition: af_afade.c:285
AVS_Value src
Definition: avisynth_c.h:482
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:488
void * buf
Definition: avisynth_c.h:553
Definition: af_afade.c:45
int64_t start_time
Definition: af_afade.c:38
GLint GLenum type
Definition: opengl_enc.c:105
Definition: af_afade.c:45
Describe the class of an AVClass context structure.
Definition: log.h:67
int av_frame_get_channels(const AVFrame *frame)
Filter definition.
Definition: avfilter.h:470
int index
Definition: gxfenc.c:89
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:239
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:474
Definition: af_afade.c:45
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:379
static int flags
Definition: cpu.c:47
Definition: af_afade.c:45
signed 16 bits
Definition: samplefmt.h:62
static int config_input(AVFilterLink *inlink)
Definition: af_afade.c:206
#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
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:633
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
Definition: af_afade.c:230
void INT64 start
Definition: avisynth_c.h:553
signed 16 bits, planar
Definition: samplefmt.h:68
#define M_PI
Definition: mathematics.h:46
internal API functions
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:530
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:548
#define FADE(name, type)
Definition: af_afade.c:180
Definition: af_afade.c:45