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
58 #define TFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
60 static int query_formats(AVFilterContext *ctx)
62 AVFilterFormats *formats;
63 AVFilterChannelLayouts *layouts;
64 static const enum AVSampleFormat sample_fmts[] = {
65 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
66 AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
67 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
68 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
73 layouts = ff_all_channel_counts();
75 return AVERROR(ENOMEM);
76 ret = ff_set_common_channel_layouts(ctx, layouts);
80 formats = ff_make_format_list(sample_fmts);
82 return AVERROR(ENOMEM);
83 ret = ff_set_common_formats(ctx, formats);
87 formats = ff_all_samplerates();
89 return AVERROR(ENOMEM);
90 return ff_set_common_samplerates(ctx, formats);
93 static double fade_gain(int curve, int64_t index, int64_t range)
95 #define CUBE(a) ((a)*(a)*(a))
98 gain = av_clipd(1.0 * index / range, 0, 1.0);
102 gain = sin(gain * M_PI / 2.0);
105 /* 0.6... = 2 / M_PI */
106 gain = 0.6366197723675814 * asin(gain);
109 gain = 1.0 - cos(M_PI / 4.0 * (CUBE(2.0*gain - 1) + 1));
112 gain = (1.0 - cos(gain * M_PI)) / 2.0;
115 /* 0.3... = 1 / M_PI */
116 gain = 0.3183098861837907 * acos(1 - 2 * gain);
119 /* -11.5... = 5*ln(0.1) */
120 gain = exp(-11.512925464970227 * (1 - gain));
123 gain = av_clipd(1 + 0.2 * log10(gain), 0, 1.0);
126 gain = 1 - sqrt(1 - gain);
129 gain = (1 - (1 - gain) * (1 - gain));
144 gain = gain <= 0.5 ? cbrt(2 * gain) / 2: 1 - cbrt(2 * (1 - gain)) / 2;
147 gain = gain <= 0.5 ? CUBE(2 * gain) / 2: 1 - CUBE(2 * (1 - gain)) / 2;
150 const double a = 1. / (1. - 0.787) - 1;
151 double A = 1. / (1.0 + exp(0 -((gain-0.5) * a * 2.0)));
152 double B = 1. / (1.0 + exp(a));
153 double C = 1. / (1.0 + exp(0-a));
154 gain = (A - B) / (C - B);
158 gain = gain >= 1.0 ? 1.0 : sin(M_PI * (1.0 - gain)) / (M_PI * (1.0 - gain));
161 gain = gain <= 0.0 ? 0.0 : 1.0 - sin(M_PI * gain) / (M_PI * gain);
171 #define FADE_PLANAR(name, type) \
172 static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
173 int nb_samples, int channels, int dir, \
174 int64_t start, int64_t range, int curve) \
178 for (i = 0; i < nb_samples; i++) { \
179 double gain = fade_gain(curve, start + i * dir, range); \
180 for (c = 0; c < channels; c++) { \
181 type *d = (type *)dst[c]; \
182 const type *s = (type *)src[c]; \
184 d[i] = s[i] * gain; \
189 #define FADE(name, type) \
190 static void fade_samples_## name (uint8_t **dst, uint8_t * const *src, \
191 int nb_samples, int channels, int dir, \
192 int64_t start, int64_t range, int curve) \
194 type *d = (type *)dst[0]; \
195 const type *s = (type *)src[0]; \
198 for (i = 0; i < nb_samples; i++) { \
199 double gain = fade_gain(curve, start + i * dir, range); \
200 for (c = 0; c < channels; c++, k++) \
201 d[k] = s[k] * gain; \
205 FADE_PLANAR(dbl, double)
206 FADE_PLANAR(flt, float)
207 FADE_PLANAR(s16, int16_t)
208 FADE_PLANAR(s32, int32_t)
215 static int config_output(AVFilterLink *outlink)
217 AVFilterContext *ctx = outlink->src;
218 AudioFadeContext *s = ctx->priv;
220 switch (outlink->format) {
221 case AV_SAMPLE_FMT_DBL: s->fade_samples = fade_samples_dbl; break;
222 case AV_SAMPLE_FMT_DBLP: s->fade_samples = fade_samples_dblp; break;
223 case AV_SAMPLE_FMT_FLT: s->fade_samples = fade_samples_flt; break;
224 case AV_SAMPLE_FMT_FLTP: s->fade_samples = fade_samples_fltp; break;
225 case AV_SAMPLE_FMT_S16: s->fade_samples = fade_samples_s16; break;
226 case AV_SAMPLE_FMT_S16P: s->fade_samples = fade_samples_s16p; break;
227 case AV_SAMPLE_FMT_S32: s->fade_samples = fade_samples_s32; break;
228 case AV_SAMPLE_FMT_S32P: s->fade_samples = fade_samples_s32p; break;
232 s->nb_samples = av_rescale(s->duration, outlink->sample_rate, AV_TIME_BASE);
235 s->start_sample = av_rescale(s->start_time, outlink->sample_rate, AV_TIME_BASE);
241 #if CONFIG_AFADE_FILTER
243 static const AVOption afade_options[] = {
244 { "type", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, TFLAGS, "type" },
245 { "t", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, TFLAGS, "type" },
246 { "in", "fade-in", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, TFLAGS, "type" },
247 { "out", "fade-out", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, TFLAGS, "type" },
248 { "start_sample", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, TFLAGS },
249 { "ss", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, TFLAGS },
250 { "nb_samples", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT64, {.i64 = 44100}, 1, INT64_MAX, TFLAGS },
251 { "ns", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT64, {.i64 = 44100}, 1, INT64_MAX, TFLAGS },
252 { "start_time", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0 }, 0, INT64_MAX, TFLAGS },
253 { "st", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0 }, 0, INT64_MAX, TFLAGS },
254 { "duration", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0 }, 0, INT64_MAX, TFLAGS },
255 { "d", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0 }, 0, INT64_MAX, TFLAGS },
256 { "curve", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, TFLAGS, "curve" },
257 { "c", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, TFLAGS, "curve" },
258 { "nofade", "no fade; keep audio as-is", 0, AV_OPT_TYPE_CONST, {.i64 = NONE }, 0, 0, TFLAGS, "curve" },
259 { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, TFLAGS, "curve" },
260 { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, TFLAGS, "curve" },
261 { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, TFLAGS, "curve" },
262 { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, TFLAGS, "curve" },
263 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, TFLAGS, "curve" },
264 { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, TFLAGS, "curve" },
265 { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, TFLAGS, "curve" },
266 { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, TFLAGS, "curve" },
267 { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, TFLAGS, "curve" },
268 { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, TFLAGS, "curve" },
269 { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, TFLAGS, "curve" },
270 { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, TFLAGS, "curve" },
271 { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, TFLAGS, "curve" },
272 { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, TFLAGS, "curve" },
273 { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, TFLAGS, "curve" },
274 { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, TFLAGS, "curve" },
275 { "losi", "logistic sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = LOSI }, 0, 0, TFLAGS, "curve" },
276 { "sinc", "sine cardinal function", 0, AV_OPT_TYPE_CONST, {.i64 = SINC }, 0, 0, TFLAGS, "curve" },
277 { "isinc", "inverted sine cardinal function", 0, AV_OPT_TYPE_CONST, {.i64 = ISINC}, 0, 0, TFLAGS, "curve" },
281 AVFILTER_DEFINE_CLASS(afade);
283 static av_cold int init(AVFilterContext *ctx)
285 AudioFadeContext *s = ctx->priv;
287 if (INT64_MAX - s->nb_samples < s->start_sample)
288 return AVERROR(EINVAL);
293 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
295 AudioFadeContext *s = inlink->dst->priv;
296 AVFilterLink *outlink = inlink->dst->outputs[0];
297 int nb_samples = buf->nb_samples;
299 int64_t cur_sample = av_rescale_q(buf->pts, inlink->time_base, (AVRational){1, inlink->sample_rate});
301 if ((!s->type && (s->start_sample + s->nb_samples < cur_sample)) ||
302 ( s->type && (cur_sample + nb_samples < s->start_sample)))
303 return ff_filter_frame(outlink, buf);
305 if (av_frame_is_writable(buf)) {
308 out_buf = ff_get_audio_buffer(outlink, nb_samples);
310 return AVERROR(ENOMEM);
311 av_frame_copy_props(out_buf, buf);
314 if ((!s->type && (cur_sample + nb_samples < s->start_sample)) ||
315 ( s->type && (s->start_sample + s->nb_samples < cur_sample))) {
316 av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
317 out_buf->channels, out_buf->format);
322 start = cur_sample - s->start_sample;
324 start = s->start_sample + s->nb_samples - cur_sample;
326 s->fade_samples(out_buf->extended_data, buf->extended_data,
327 nb_samples, buf->channels,
328 s->type ? -1 : 1, start,
329 s->nb_samples, s->curve);
335 return ff_filter_frame(outlink, out_buf);
338 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
339 char *res, int res_len, int flags)
343 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
347 return config_output(ctx->outputs[0]);
350 static const AVFilterPad avfilter_af_afade_inputs[] = {
353 .type = AVMEDIA_TYPE_AUDIO,
354 .filter_frame = filter_frame,
359 static const AVFilterPad avfilter_af_afade_outputs[] = {
362 .type = AVMEDIA_TYPE_AUDIO,
363 .config_props = config_output,
368 AVFilter ff_af_afade = {
370 .description = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
371 .query_formats = query_formats,
372 .priv_size = sizeof(AudioFadeContext),
374 .inputs = avfilter_af_afade_inputs,
375 .outputs = avfilter_af_afade_outputs,
376 .priv_class = &afade_class,
377 .process_command = process_command,
378 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
381 #endif /* CONFIG_AFADE_FILTER */
383 #if CONFIG_ACROSSFADE_FILTER
385 static const AVOption acrossfade_options[] = {
386 { "nb_samples", "set number of samples for cross fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
387 { "ns", "set number of samples for cross fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
388 { "duration", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0 }, 0, 60000000, FLAGS },
389 { "d", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0 }, 0, 60000000, FLAGS },
390 { "overlap", "overlap 1st stream end with 2nd stream start", OFFSET(overlap), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, FLAGS },
391 { "o", "overlap 1st stream end with 2nd stream start", OFFSET(overlap), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, FLAGS },
392 { "curve1", "set fade curve type for 1st stream", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
393 { "c1", "set fade curve type for 1st stream", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
394 { "nofade", "no fade; keep audio as-is", 0, AV_OPT_TYPE_CONST, {.i64 = NONE }, 0, 0, FLAGS, "curve" },
395 { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve" },
396 { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
397 { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
398 { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
399 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve" },
400 { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
401 { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve" },
402 { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve" },
403 { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve" },
404 { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve" },
405 { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve" },
406 { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, FLAGS, "curve" },
407 { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
408 { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
409 { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, FLAGS, "curve" },
410 { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, FLAGS, "curve" },
411 { "losi", "logistic sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = LOSI }, 0, 0, FLAGS, "curve" },
412 { "sinc", "sine cardinal function", 0, AV_OPT_TYPE_CONST, {.i64 = SINC }, 0, 0, FLAGS, "curve" },
413 { "isinc", "inverted sine cardinal function", 0, AV_OPT_TYPE_CONST, {.i64 = ISINC}, 0, 0, FLAGS, "curve" },
414 { "curve2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
415 { "c2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
419 AVFILTER_DEFINE_CLASS(acrossfade);
421 #define CROSSFADE_PLANAR(name, type) \
422 static void crossfade_samples_## name ##p(uint8_t **dst, uint8_t * const *cf0, \
423 uint8_t * const *cf1, \
424 int nb_samples, int channels, \
425 int curve0, int curve1) \
429 for (i = 0; i < nb_samples; i++) { \
430 double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
431 double gain1 = fade_gain(curve1, i, nb_samples); \
432 for (c = 0; c < channels; c++) { \
433 type *d = (type *)dst[c]; \
434 const type *s0 = (type *)cf0[c]; \
435 const type *s1 = (type *)cf1[c]; \
437 d[i] = s0[i] * gain0 + s1[i] * gain1; \
442 #define CROSSFADE(name, type) \
443 static void crossfade_samples_## name (uint8_t **dst, uint8_t * const *cf0, \
444 uint8_t * const *cf1, \
445 int nb_samples, int channels, \
446 int curve0, int curve1) \
448 type *d = (type *)dst[0]; \
449 const type *s0 = (type *)cf0[0]; \
450 const type *s1 = (type *)cf1[0]; \
453 for (i = 0; i < nb_samples; i++) { \
454 double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
455 double gain1 = fade_gain(curve1, i, nb_samples); \
456 for (c = 0; c < channels; c++, k++) \
457 d[k] = s0[k] * gain0 + s1[k] * gain1; \
461 CROSSFADE_PLANAR(dbl, double)
462 CROSSFADE_PLANAR(flt, float)
463 CROSSFADE_PLANAR(s16, int16_t)
464 CROSSFADE_PLANAR(s32, int32_t)
466 CROSSFADE(dbl, double)
467 CROSSFADE(flt, float)
468 CROSSFADE(s16, int16_t)
469 CROSSFADE(s32, int32_t)
471 static int activate(AVFilterContext *ctx)
473 AudioFadeContext *s = ctx->priv;
474 AVFilterLink *outlink = ctx->outputs[0];
475 AVFrame *in = NULL, *out, *cf[2] = { NULL };
476 int ret = 0, nb_samples, status;
479 FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
481 if (s->crossfade_is_over) {
482 ret = ff_inlink_consume_frame(ctx->inputs[1], &in);
485 s->pts += av_rescale_q(in->nb_samples,
486 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
487 return ff_filter_frame(outlink, in);
488 } else if (ret < 0) {
490 } else if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
491 ff_outlink_set_status(ctx->outputs[0], status, pts);
494 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
495 ff_inlink_request_frame(ctx->inputs[1]);
501 if (ff_inlink_queued_samples(ctx->inputs[0]) > s->nb_samples) {
502 nb_samples = ff_inlink_queued_samples(ctx->inputs[0]) - s->nb_samples;
503 if (nb_samples > 0) {
504 ret = ff_inlink_consume_samples(ctx->inputs[0], nb_samples, nb_samples, &in);
510 s->pts += av_rescale_q(in->nb_samples,
511 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
512 return ff_filter_frame(outlink, in);
513 } else if (ff_inlink_queued_samples(ctx->inputs[0]) >= s->nb_samples &&
514 ff_inlink_queued_samples(ctx->inputs[1]) >= s->nb_samples && s->cf0_eof) {
516 out = ff_get_audio_buffer(outlink, s->nb_samples);
518 return AVERROR(ENOMEM);
520 ret = ff_inlink_consume_samples(ctx->inputs[0], s->nb_samples, s->nb_samples, &cf[0]);
526 ret = ff_inlink_consume_samples(ctx->inputs[1], s->nb_samples, s->nb_samples, &cf[1]);
532 s->crossfade_samples(out->extended_data, cf[0]->extended_data,
533 cf[1]->extended_data,
534 s->nb_samples, out->channels,
535 s->curve, s->curve2);
537 s->pts += av_rescale_q(s->nb_samples,
538 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
539 s->crossfade_is_over = 1;
540 av_frame_free(&cf[0]);
541 av_frame_free(&cf[1]);
542 return ff_filter_frame(outlink, out);
544 out = ff_get_audio_buffer(outlink, s->nb_samples);
546 return AVERROR(ENOMEM);
548 ret = ff_inlink_consume_samples(ctx->inputs[0], s->nb_samples, s->nb_samples, &cf[0]);
554 s->fade_samples(out->extended_data, cf[0]->extended_data, s->nb_samples,
555 outlink->channels, -1, s->nb_samples - 1, s->nb_samples, s->curve);
557 s->pts += av_rescale_q(s->nb_samples,
558 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
559 av_frame_free(&cf[0]);
560 ret = ff_filter_frame(outlink, out);
564 out = ff_get_audio_buffer(outlink, s->nb_samples);
566 return AVERROR(ENOMEM);
568 ret = ff_inlink_consume_samples(ctx->inputs[1], s->nb_samples, s->nb_samples, &cf[1]);
574 s->fade_samples(out->extended_data, cf[1]->extended_data, s->nb_samples,
575 outlink->channels, 1, 0, s->nb_samples, s->curve2);
577 s->pts += av_rescale_q(s->nb_samples,
578 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
579 s->crossfade_is_over = 1;
580 av_frame_free(&cf[1]);
581 return ff_filter_frame(outlink, out);
583 } else if (ff_outlink_frame_wanted(ctx->outputs[0])) {
584 if (!s->cf0_eof && ff_outlink_get_status(ctx->inputs[0])) {
587 if (ff_outlink_get_status(ctx->inputs[1])) {
588 ff_outlink_set_status(ctx->outputs[0], AVERROR_EOF, AV_NOPTS_VALUE);
592 ff_inlink_request_frame(ctx->inputs[0]);
594 ff_inlink_request_frame(ctx->inputs[1]);
601 static int acrossfade_config_output(AVFilterLink *outlink)
603 AVFilterContext *ctx = outlink->src;
604 AudioFadeContext *s = ctx->priv;
606 if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
607 av_log(ctx, AV_LOG_ERROR,
608 "Inputs must have the same sample rate "
609 "%d for in0 vs %d for in1\n",
610 ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
611 return AVERROR(EINVAL);
614 outlink->sample_rate = ctx->inputs[0]->sample_rate;
615 outlink->time_base = ctx->inputs[0]->time_base;
616 outlink->channel_layout = ctx->inputs[0]->channel_layout;
617 outlink->channels = ctx->inputs[0]->channels;
619 switch (outlink->format) {
620 case AV_SAMPLE_FMT_DBL: s->crossfade_samples = crossfade_samples_dbl; break;
621 case AV_SAMPLE_FMT_DBLP: s->crossfade_samples = crossfade_samples_dblp; break;
622 case AV_SAMPLE_FMT_FLT: s->crossfade_samples = crossfade_samples_flt; break;
623 case AV_SAMPLE_FMT_FLTP: s->crossfade_samples = crossfade_samples_fltp; break;
624 case AV_SAMPLE_FMT_S16: s->crossfade_samples = crossfade_samples_s16; break;
625 case AV_SAMPLE_FMT_S16P: s->crossfade_samples = crossfade_samples_s16p; break;
626 case AV_SAMPLE_FMT_S32: s->crossfade_samples = crossfade_samples_s32; break;
627 case AV_SAMPLE_FMT_S32P: s->crossfade_samples = crossfade_samples_s32p; break;
630 config_output(outlink);
635 static const AVFilterPad avfilter_af_acrossfade_inputs[] = {
637 .name = "crossfade0",
638 .type = AVMEDIA_TYPE_AUDIO,
641 .name = "crossfade1",
642 .type = AVMEDIA_TYPE_AUDIO,
647 static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
650 .type = AVMEDIA_TYPE_AUDIO,
651 .config_props = acrossfade_config_output,
656 AVFilter ff_af_acrossfade = {
657 .name = "acrossfade",
658 .description = NULL_IF_CONFIG_SMALL("Cross fade two input audio streams."),
659 .query_formats = query_formats,
660 .priv_size = sizeof(AudioFadeContext),
661 .activate = activate,
662 .priv_class = &acrossfade_class,
663 .inputs = avfilter_af_acrossfade_inputs,
664 .outputs = avfilter_af_acrossfade_outputs,
667 #endif /* CONFIG_ACROSSFADE_FILTER */