2 * Copyright (c) 2013-2015 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"
32 typedef struct AudioFadeContext {
42 int crossfade_is_over;
45 void (*fade_samples)(uint8_t **dst, uint8_t * const *src,
46 int nb_samples, int channels, int direction,
47 int64_t start, int64_t range, int curve);
48 void (*crossfade_samples)(uint8_t **dst, uint8_t * const *cf0,
50 int nb_samples, int channels,
51 int curve0, int curve1);
54 enum CurveType { NONE = -1, TRI, QSIN, ESIN, HSIN, LOG, IPAR, QUA, CUB, SQU, CBR, PAR, EXP, IQSIN, IHSIN, DESE, DESI, LOSI, SINC, ISINC, NB_CURVES };
56 #define OFFSET(x) offsetof(AudioFadeContext, x)
57 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
59 static int query_formats(AVFilterContext *ctx)
61 AVFilterFormats *formats;
62 AVFilterChannelLayouts *layouts;
63 static const enum AVSampleFormat sample_fmts[] = {
64 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
65 AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
66 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
67 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
72 layouts = ff_all_channel_counts();
74 return AVERROR(ENOMEM);
75 ret = ff_set_common_channel_layouts(ctx, layouts);
79 formats = ff_make_format_list(sample_fmts);
81 return AVERROR(ENOMEM);
82 ret = ff_set_common_formats(ctx, formats);
86 formats = ff_all_samplerates();
88 return AVERROR(ENOMEM);
89 return ff_set_common_samplerates(ctx, formats);
92 static double fade_gain(int curve, int64_t index, int64_t range)
94 #define CUBE(a) ((a)*(a)*(a))
97 gain = av_clipd(1.0 * index / range, 0, 1.0);
101 gain = sin(gain * M_PI / 2.0);
104 /* 0.6... = 2 / M_PI */
105 gain = 0.6366197723675814 * asin(gain);
108 gain = 1.0 - cos(M_PI / 4.0 * (CUBE(2.0*gain - 1) + 1));
111 gain = (1.0 - cos(gain * M_PI)) / 2.0;
114 /* 0.3... = 1 / M_PI */
115 gain = 0.3183098861837907 * acos(1 - 2 * gain);
118 /* -11.5... = 5*ln(0.1) */
119 gain = exp(-11.512925464970227 * (1 - gain));
122 gain = av_clipd(1 + 0.2 * log10(gain), 0, 1.0);
125 gain = 1 - sqrt(1 - gain);
128 gain = (1 - (1 - gain) * (1 - gain));
143 gain = gain <= 0.5 ? cbrt(2 * gain) / 2: 1 - cbrt(2 * (1 - gain)) / 2;
146 gain = gain <= 0.5 ? CUBE(2 * gain) / 2: 1 - CUBE(2 * (1 - gain)) / 2;
149 const double a = 1. / (1. - 0.787) - 1;
150 double A = 1. / (1.0 + exp(0 -((gain-0.5) * a * 2.0)));
151 double B = 1. / (1.0 + exp(a));
152 double C = 1. / (1.0 + exp(0-a));
153 gain = (A - B) / (C - B);
157 gain = gain >= 1.0 ? 1.0 : sin(M_PI * (1.0 - gain)) / (M_PI * (1.0 - gain));
160 gain = gain <= 0.0 ? 0.0 : 1.0 - sin(M_PI * gain) / (M_PI * gain);
170 #define FADE_PLANAR(name, type) \
171 static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
172 int nb_samples, int channels, int dir, \
173 int64_t start, int64_t range, int curve) \
177 for (i = 0; i < nb_samples; i++) { \
178 double gain = fade_gain(curve, start + i * dir, range); \
179 for (c = 0; c < channels; c++) { \
180 type *d = (type *)dst[c]; \
181 const type *s = (type *)src[c]; \
183 d[i] = s[i] * gain; \
188 #define FADE(name, type) \
189 static void fade_samples_## name (uint8_t **dst, uint8_t * const *src, \
190 int nb_samples, int channels, int dir, \
191 int64_t start, int64_t range, int curve) \
193 type *d = (type *)dst[0]; \
194 const type *s = (type *)src[0]; \
197 for (i = 0; i < nb_samples; i++) { \
198 double gain = fade_gain(curve, start + i * dir, range); \
199 for (c = 0; c < channels; c++, k++) \
200 d[k] = s[k] * gain; \
204 FADE_PLANAR(dbl, double)
205 FADE_PLANAR(flt, float)
206 FADE_PLANAR(s16, int16_t)
207 FADE_PLANAR(s32, int32_t)
214 static int config_output(AVFilterLink *outlink)
216 AVFilterContext *ctx = outlink->src;
217 AudioFadeContext *s = ctx->priv;
219 switch (outlink->format) {
220 case AV_SAMPLE_FMT_DBL: s->fade_samples = fade_samples_dbl; break;
221 case AV_SAMPLE_FMT_DBLP: s->fade_samples = fade_samples_dblp; break;
222 case AV_SAMPLE_FMT_FLT: s->fade_samples = fade_samples_flt; break;
223 case AV_SAMPLE_FMT_FLTP: s->fade_samples = fade_samples_fltp; break;
224 case AV_SAMPLE_FMT_S16: s->fade_samples = fade_samples_s16; break;
225 case AV_SAMPLE_FMT_S16P: s->fade_samples = fade_samples_s16p; break;
226 case AV_SAMPLE_FMT_S32: s->fade_samples = fade_samples_s32; break;
227 case AV_SAMPLE_FMT_S32P: s->fade_samples = fade_samples_s32p; break;
231 s->nb_samples = av_rescale(s->duration, outlink->sample_rate, AV_TIME_BASE);
233 s->start_sample = av_rescale(s->start_time, outlink->sample_rate, AV_TIME_BASE);
238 #if CONFIG_AFADE_FILTER
240 static const AVOption afade_options[] = {
241 { "type", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "type" },
242 { "t", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "type" },
243 { "in", "fade-in", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "type" },
244 { "out", "fade-out", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "type" },
245 { "start_sample", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
246 { "ss", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
247 { "nb_samples", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT64, {.i64 = 44100}, 1, INT64_MAX, FLAGS },
248 { "ns", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT64, {.i64 = 44100}, 1, INT64_MAX, FLAGS },
249 { "start_time", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
250 { "st", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
251 { "duration", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
252 { "d", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
253 { "curve", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
254 { "c", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
255 { "nofade", "no fade; keep audio as-is", 0, AV_OPT_TYPE_CONST, {.i64 = NONE }, 0, 0, FLAGS, "curve" },
256 { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve" },
257 { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
258 { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
259 { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
260 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve" },
261 { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
262 { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve" },
263 { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve" },
264 { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve" },
265 { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve" },
266 { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve" },
267 { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, FLAGS, "curve" },
268 { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
269 { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
270 { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, FLAGS, "curve" },
271 { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, FLAGS, "curve" },
272 { "losi", "logistic sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = LOSI }, 0, 0, FLAGS, "curve" },
273 { "sinc", "sine cardinal function", 0, AV_OPT_TYPE_CONST, {.i64 = SINC }, 0, 0, FLAGS, "curve" },
274 { "isinc", "inverted sine cardinal function", 0, AV_OPT_TYPE_CONST, {.i64 = ISINC}, 0, 0, FLAGS, "curve" },
278 AVFILTER_DEFINE_CLASS(afade);
280 static av_cold int init(AVFilterContext *ctx)
282 AudioFadeContext *s = ctx->priv;
284 if (INT64_MAX - s->nb_samples < s->start_sample)
285 return AVERROR(EINVAL);
290 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
292 AudioFadeContext *s = inlink->dst->priv;
293 AVFilterLink *outlink = inlink->dst->outputs[0];
294 int nb_samples = buf->nb_samples;
296 int64_t cur_sample = av_rescale_q(buf->pts, inlink->time_base, (AVRational){1, inlink->sample_rate});
298 if ((!s->type && (s->start_sample + s->nb_samples < cur_sample)) ||
299 ( s->type && (cur_sample + nb_samples < s->start_sample)))
300 return ff_filter_frame(outlink, buf);
302 if (av_frame_is_writable(buf)) {
305 out_buf = ff_get_audio_buffer(outlink, nb_samples);
307 return AVERROR(ENOMEM);
308 av_frame_copy_props(out_buf, buf);
311 if ((!s->type && (cur_sample + nb_samples < s->start_sample)) ||
312 ( s->type && (s->start_sample + s->nb_samples < cur_sample))) {
313 av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
314 out_buf->channels, out_buf->format);
319 start = cur_sample - s->start_sample;
321 start = s->start_sample + s->nb_samples - cur_sample;
323 s->fade_samples(out_buf->extended_data, buf->extended_data,
324 nb_samples, buf->channels,
325 s->type ? -1 : 1, start,
326 s->nb_samples, s->curve);
332 return ff_filter_frame(outlink, out_buf);
335 static const AVFilterPad avfilter_af_afade_inputs[] = {
338 .type = AVMEDIA_TYPE_AUDIO,
339 .filter_frame = filter_frame,
344 static const AVFilterPad avfilter_af_afade_outputs[] = {
347 .type = AVMEDIA_TYPE_AUDIO,
348 .config_props = config_output,
353 AVFilter ff_af_afade = {
355 .description = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
356 .query_formats = query_formats,
357 .priv_size = sizeof(AudioFadeContext),
359 .inputs = avfilter_af_afade_inputs,
360 .outputs = avfilter_af_afade_outputs,
361 .priv_class = &afade_class,
362 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
365 #endif /* CONFIG_AFADE_FILTER */
367 #if CONFIG_ACROSSFADE_FILTER
369 static const AVOption acrossfade_options[] = {
370 { "nb_samples", "set number of samples for cross fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
371 { "ns", "set number of samples for cross fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
372 { "duration", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, 60000000, FLAGS },
373 { "d", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, 60000000, FLAGS },
374 { "overlap", "overlap 1st stream end with 2nd stream start", OFFSET(overlap), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, FLAGS },
375 { "o", "overlap 1st stream end with 2nd stream start", OFFSET(overlap), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, FLAGS },
376 { "curve1", "set fade curve type for 1st stream", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
377 { "c1", "set fade curve type for 1st stream", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
378 { "nofade", "no fade; keep audio as-is", 0, AV_OPT_TYPE_CONST, {.i64 = NONE }, 0, 0, FLAGS, "curve" },
379 { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve" },
380 { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
381 { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
382 { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
383 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve" },
384 { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
385 { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve" },
386 { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve" },
387 { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve" },
388 { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve" },
389 { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve" },
390 { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, FLAGS, "curve" },
391 { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
392 { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
393 { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, FLAGS, "curve" },
394 { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, FLAGS, "curve" },
395 { "losi", "logistic sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = LOSI }, 0, 0, FLAGS, "curve" },
396 { "sinc", "sine cardinal function", 0, AV_OPT_TYPE_CONST, {.i64 = SINC }, 0, 0, FLAGS, "curve" },
397 { "isinc", "inverted sine cardinal function", 0, AV_OPT_TYPE_CONST, {.i64 = ISINC}, 0, 0, FLAGS, "curve" },
398 { "curve2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
399 { "c2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
403 AVFILTER_DEFINE_CLASS(acrossfade);
405 #define CROSSFADE_PLANAR(name, type) \
406 static void crossfade_samples_## name ##p(uint8_t **dst, uint8_t * const *cf0, \
407 uint8_t * const *cf1, \
408 int nb_samples, int channels, \
409 int curve0, int curve1) \
413 for (i = 0; i < nb_samples; i++) { \
414 double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
415 double gain1 = fade_gain(curve1, i, nb_samples); \
416 for (c = 0; c < channels; c++) { \
417 type *d = (type *)dst[c]; \
418 const type *s0 = (type *)cf0[c]; \
419 const type *s1 = (type *)cf1[c]; \
421 d[i] = s0[i] * gain0 + s1[i] * gain1; \
426 #define CROSSFADE(name, type) \
427 static void crossfade_samples_## name (uint8_t **dst, uint8_t * const *cf0, \
428 uint8_t * const *cf1, \
429 int nb_samples, int channels, \
430 int curve0, int curve1) \
432 type *d = (type *)dst[0]; \
433 const type *s0 = (type *)cf0[0]; \
434 const type *s1 = (type *)cf1[0]; \
437 for (i = 0; i < nb_samples; i++) { \
438 double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
439 double gain1 = fade_gain(curve1, i, nb_samples); \
440 for (c = 0; c < channels; c++, k++) \
441 d[k] = s0[k] * gain0 + s1[k] * gain1; \
445 CROSSFADE_PLANAR(dbl, double)
446 CROSSFADE_PLANAR(flt, float)
447 CROSSFADE_PLANAR(s16, int16_t)
448 CROSSFADE_PLANAR(s32, int32_t)
450 CROSSFADE(dbl, double)
451 CROSSFADE(flt, float)
452 CROSSFADE(s16, int16_t)
453 CROSSFADE(s32, int32_t)
455 static int activate(AVFilterContext *ctx)
457 AudioFadeContext *s = ctx->priv;
458 AVFilterLink *outlink = ctx->outputs[0];
459 AVFrame *in = NULL, *out, *cf[2] = { NULL };
460 int ret = 0, nb_samples, status;
463 FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
465 if (s->crossfade_is_over) {
466 ret = ff_inlink_consume_frame(ctx->inputs[1], &in);
469 s->pts += av_rescale_q(in->nb_samples,
470 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
471 return ff_filter_frame(outlink, in);
472 } else if (ret < 0) {
474 } else if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
475 ff_outlink_set_status(ctx->outputs[0], status, pts);
478 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
479 ff_inlink_request_frame(ctx->inputs[1]);
485 if (ff_inlink_queued_samples(ctx->inputs[0]) > s->nb_samples) {
486 nb_samples = ff_inlink_queued_samples(ctx->inputs[0]) - s->nb_samples;
487 if (nb_samples > 0) {
488 ret = ff_inlink_consume_samples(ctx->inputs[0], nb_samples, nb_samples, &in);
494 s->pts += av_rescale_q(in->nb_samples,
495 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
496 return ff_filter_frame(outlink, in);
497 } else if (ff_inlink_queued_samples(ctx->inputs[0]) >= s->nb_samples &&
498 ff_inlink_queued_samples(ctx->inputs[1]) >= s->nb_samples && s->cf0_eof) {
500 out = ff_get_audio_buffer(outlink, s->nb_samples);
502 return AVERROR(ENOMEM);
504 ret = ff_inlink_consume_samples(ctx->inputs[0], s->nb_samples, s->nb_samples, &cf[0]);
510 ret = ff_inlink_consume_samples(ctx->inputs[1], s->nb_samples, s->nb_samples, &cf[1]);
516 s->crossfade_samples(out->extended_data, cf[0]->extended_data,
517 cf[1]->extended_data,
518 s->nb_samples, out->channels,
519 s->curve, s->curve2);
521 s->pts += av_rescale_q(s->nb_samples,
522 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
523 s->crossfade_is_over = 1;
524 av_frame_free(&cf[0]);
525 av_frame_free(&cf[1]);
526 return ff_filter_frame(outlink, out);
528 out = ff_get_audio_buffer(outlink, s->nb_samples);
530 return AVERROR(ENOMEM);
532 ret = ff_inlink_consume_samples(ctx->inputs[0], s->nb_samples, s->nb_samples, &cf[0]);
538 s->fade_samples(out->extended_data, cf[0]->extended_data, s->nb_samples,
539 outlink->channels, -1, s->nb_samples - 1, s->nb_samples, s->curve);
541 s->pts += av_rescale_q(s->nb_samples,
542 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
543 av_frame_free(&cf[0]);
544 ret = ff_filter_frame(outlink, out);
548 out = ff_get_audio_buffer(outlink, s->nb_samples);
550 return AVERROR(ENOMEM);
552 ret = ff_inlink_consume_samples(ctx->inputs[1], s->nb_samples, s->nb_samples, &cf[1]);
558 s->fade_samples(out->extended_data, cf[1]->extended_data, s->nb_samples,
559 outlink->channels, 1, 0, s->nb_samples, s->curve2);
561 s->pts += av_rescale_q(s->nb_samples,
562 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
563 s->crossfade_is_over = 1;
564 av_frame_free(&cf[1]);
565 return ff_filter_frame(outlink, out);
567 } else if (ff_outlink_frame_wanted(ctx->outputs[0])) {
568 if (!s->cf0_eof && ff_outlink_get_status(ctx->inputs[0])) {
571 if (ff_outlink_get_status(ctx->inputs[1])) {
572 ff_outlink_set_status(ctx->outputs[0], AVERROR_EOF, AV_NOPTS_VALUE);
576 ff_inlink_request_frame(ctx->inputs[0]);
578 ff_inlink_request_frame(ctx->inputs[1]);
585 static int acrossfade_config_output(AVFilterLink *outlink)
587 AVFilterContext *ctx = outlink->src;
588 AudioFadeContext *s = ctx->priv;
590 if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
591 av_log(ctx, AV_LOG_ERROR,
592 "Inputs must have the same sample rate "
593 "%d for in0 vs %d for in1\n",
594 ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
595 return AVERROR(EINVAL);
598 outlink->sample_rate = ctx->inputs[0]->sample_rate;
599 outlink->time_base = ctx->inputs[0]->time_base;
600 outlink->channel_layout = ctx->inputs[0]->channel_layout;
601 outlink->channels = ctx->inputs[0]->channels;
603 switch (outlink->format) {
604 case AV_SAMPLE_FMT_DBL: s->crossfade_samples = crossfade_samples_dbl; break;
605 case AV_SAMPLE_FMT_DBLP: s->crossfade_samples = crossfade_samples_dblp; break;
606 case AV_SAMPLE_FMT_FLT: s->crossfade_samples = crossfade_samples_flt; break;
607 case AV_SAMPLE_FMT_FLTP: s->crossfade_samples = crossfade_samples_fltp; break;
608 case AV_SAMPLE_FMT_S16: s->crossfade_samples = crossfade_samples_s16; break;
609 case AV_SAMPLE_FMT_S16P: s->crossfade_samples = crossfade_samples_s16p; break;
610 case AV_SAMPLE_FMT_S32: s->crossfade_samples = crossfade_samples_s32; break;
611 case AV_SAMPLE_FMT_S32P: s->crossfade_samples = crossfade_samples_s32p; break;
614 config_output(outlink);
619 static const AVFilterPad avfilter_af_acrossfade_inputs[] = {
621 .name = "crossfade0",
622 .type = AVMEDIA_TYPE_AUDIO,
625 .name = "crossfade1",
626 .type = AVMEDIA_TYPE_AUDIO,
631 static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
634 .type = AVMEDIA_TYPE_AUDIO,
635 .config_props = acrossfade_config_output,
640 AVFilter ff_af_acrossfade = {
641 .name = "acrossfade",
642 .description = NULL_IF_CONFIG_SMALL("Cross fade two input audio streams."),
643 .query_formats = query_formats,
644 .priv_size = sizeof(AudioFadeContext),
645 .activate = activate,
646 .priv_class = &acrossfade_class,
647 .inputs = avfilter_af_acrossfade_inputs,
648 .outputs = avfilter_af_acrossfade_outputs,
651 #endif /* CONFIG_ACROSSFADE_FILTER */