2 * Copyright (c) 2013 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
26 #include "libavutil/opt.h"
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);
45 enum CurveType { TRI, QSIN, ESIN, HSIN, LOG, PAR, QUA, CUB, SQU, CBR };
47 #define OFFSET(x) offsetof(AudioFadeContext, x)
48 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
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" },
78 AVFILTER_DEFINE_CLASS(afade);
80 static av_cold int init(AVFilterContext *ctx)
82 AudioFadeContext *s = ctx->priv;
84 if (INT64_MAX - s->nb_samples < s->start_sample)
85 return AVERROR(EINVAL);
90 static int query_formats(AVFilterContext *ctx)
92 AVFilterFormats *formats;
93 AVFilterChannelLayouts *layouts;
94 static const enum AVSampleFormat sample_fmts[] = {
95 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
96 AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
97 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
98 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
103 layouts = ff_all_channel_layouts();
105 return AVERROR(ENOMEM);
106 ret = ff_set_common_channel_layouts(ctx, layouts);
110 formats = ff_make_format_list(sample_fmts);
112 return AVERROR(ENOMEM);
113 ret = ff_set_common_formats(ctx, formats);
117 formats = ff_all_samplerates();
119 return AVERROR(ENOMEM);
120 return ff_set_common_samplerates(ctx, formats);
123 static double fade_gain(int curve, int64_t index, int range)
127 gain = FFMAX(0.0, FFMIN(1.0, 1.0 * index / range));
131 gain = sin(gain * M_PI / 2.0);
134 gain = 1.0 - cos(M_PI / 4.0 * (pow(2.0*gain - 1, 3) + 1));
137 gain = (1.0 - cos(gain * M_PI)) / 2.0;
140 gain = pow(0.1, (1 - gain) * 5.0);
143 gain = (1 - (1 - gain) * (1 - gain));
149 gain = gain * gain * gain;
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) \
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]; \
175 d[i] = s[i] * gain; \
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) \
185 type *d = (type *)dst[0]; \
186 const type *s = (type *)src[0]; \
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; \
196 FADE_PLANAR(dbl, double)
197 FADE_PLANAR(flt, float)
198 FADE_PLANAR(s16, int16_t)
199 FADE_PLANAR(s32, int32_t)
206 static int config_input(AVFilterLink *inlink)
208 AVFilterContext *ctx = inlink->dst;
209 AudioFadeContext *s = ctx->priv;
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;
223 s->nb_samples = av_rescale(s->duration, inlink->sample_rate, AV_TIME_BASE);
225 s->start_sample = av_rescale(s->start_time, inlink->sample_rate, AV_TIME_BASE);
230 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
232 AudioFadeContext *s = inlink->dst->priv;
233 AVFilterLink *outlink = inlink->dst->outputs[0];
234 int nb_samples = buf->nb_samples;
236 int64_t cur_sample = av_rescale_q(buf->pts, inlink->time_base, (AVRational){1, inlink->sample_rate});
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);
242 if (av_frame_is_writable(buf)) {
245 out_buf = ff_get_audio_buffer(inlink, nb_samples);
247 return AVERROR(ENOMEM);
248 av_frame_copy_props(out_buf, buf);
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);
259 start = cur_sample - s->start_sample;
261 start = s->start_sample + s->nb_samples - cur_sample;
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);
272 return ff_filter_frame(outlink, out_buf);
275 static const AVFilterPad avfilter_af_afade_inputs[] = {
278 .type = AVMEDIA_TYPE_AUDIO,
279 .filter_frame = filter_frame,
280 .config_props = config_input,
285 static const AVFilterPad avfilter_af_afade_outputs[] = {
288 .type = AVMEDIA_TYPE_AUDIO,
293 AVFilter ff_af_afade = {
295 .description = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
296 .query_formats = query_formats,
297 .priv_size = sizeof(AudioFadeContext),
299 .inputs = avfilter_af_afade_inputs,
300 .outputs = avfilter_af_afade_outputs,
301 .priv_class = &afade_class,
302 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,