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 #define FF_INTERNAL_FIELDS 1
27 #include "framequeue.h"
29 #include "libavutil/audio_fifo.h"
30 #include "libavutil/opt.h"
36 typedef struct AudioFadeContext {
47 int crossfade_is_over;
51 void (*fade_samples)(uint8_t **dst, uint8_t * const *src,
52 int nb_samples, int channels, int direction,
53 int64_t start, int64_t range, int curve);
54 void (*crossfade_samples)(uint8_t **dst, uint8_t * const *cf0,
56 int nb_samples, int channels,
57 int curve0, int curve1);
60 enum CurveType { TRI, QSIN, ESIN, HSIN, LOG, IPAR, QUA, CUB, SQU, CBR, PAR, EXP, IQSIN, IHSIN, DESE, DESI, NB_CURVES };
62 #define OFFSET(x) offsetof(AudioFadeContext, x)
63 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
65 static int query_formats(AVFilterContext *ctx)
67 AVFilterFormats *formats;
68 AVFilterChannelLayouts *layouts;
69 static const enum AVSampleFormat sample_fmts[] = {
70 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
71 AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
72 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
73 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
78 layouts = ff_all_channel_counts();
80 return AVERROR(ENOMEM);
81 ret = ff_set_common_channel_layouts(ctx, layouts);
85 formats = ff_make_format_list(sample_fmts);
87 return AVERROR(ENOMEM);
88 ret = ff_set_common_formats(ctx, formats);
92 formats = ff_all_samplerates();
94 return AVERROR(ENOMEM);
95 return ff_set_common_samplerates(ctx, formats);
98 static double fade_gain(int curve, int64_t index, int64_t range)
100 #define CUBE(a) ((a)*(a)*(a))
103 gain = av_clipd(1.0 * index / range, 0, 1.0);
107 gain = sin(gain * M_PI / 2.0);
110 /* 0.6... = 2 / M_PI */
111 gain = 0.6366197723675814 * asin(gain);
114 gain = 1.0 - cos(M_PI / 4.0 * (CUBE(2.0*gain - 1) + 1));
117 gain = (1.0 - cos(gain * M_PI)) / 2.0;
120 /* 0.3... = 1 / M_PI */
121 gain = 0.3183098861837907 * acos(1 - 2 * gain);
124 /* -11.5... = 5*ln(0.1) */
125 gain = exp(-11.512925464970227 * (1 - gain));
128 gain = av_clipd(1 + 0.2 * log10(gain), 0, 1.0);
131 gain = 1 - sqrt(1 - gain);
134 gain = (1 - (1 - gain) * (1 - gain));
149 gain = gain <= 0.5 ? cbrt(2 * gain) / 2: 1 - cbrt(2 * (1 - gain)) / 2;
152 gain = gain <= 0.5 ? CUBE(2 * gain) / 2: 1 - CUBE(2 * (1 - gain)) / 2;
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, int64_t range, int curve) \
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]; \
172 d[i] = s[i] * gain; \
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, int64_t range, int curve) \
182 type *d = (type *)dst[0]; \
183 const type *s = (type *)src[0]; \
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; \
193 FADE_PLANAR(dbl, double)
194 FADE_PLANAR(flt, float)
195 FADE_PLANAR(s16, int16_t)
196 FADE_PLANAR(s32, int32_t)
203 static int config_output(AVFilterLink *outlink)
205 AVFilterContext *ctx = outlink->src;
206 AudioFadeContext *s = ctx->priv;
208 switch (outlink->format) {
209 case AV_SAMPLE_FMT_DBL: s->fade_samples = fade_samples_dbl; break;
210 case AV_SAMPLE_FMT_DBLP: s->fade_samples = fade_samples_dblp; break;
211 case AV_SAMPLE_FMT_FLT: s->fade_samples = fade_samples_flt; break;
212 case AV_SAMPLE_FMT_FLTP: s->fade_samples = fade_samples_fltp; break;
213 case AV_SAMPLE_FMT_S16: s->fade_samples = fade_samples_s16; break;
214 case AV_SAMPLE_FMT_S16P: s->fade_samples = fade_samples_s16p; break;
215 case AV_SAMPLE_FMT_S32: s->fade_samples = fade_samples_s32; break;
216 case AV_SAMPLE_FMT_S32P: s->fade_samples = fade_samples_s32p; break;
220 s->nb_samples = av_rescale(s->duration, outlink->sample_rate, AV_TIME_BASE);
222 s->start_sample = av_rescale(s->start_time, outlink->sample_rate, AV_TIME_BASE);
227 #if CONFIG_AFADE_FILTER
229 static const AVOption afade_options[] = {
230 { "type", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "type" },
231 { "t", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS, "type" },
232 { "in", "fade-in", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "type" },
233 { "out", "fade-out", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "type" },
234 { "start_sample", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
235 { "ss", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS },
236 { "nb_samples", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT64, {.i64 = 44100}, 1, INT64_MAX, FLAGS },
237 { "ns", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT64, {.i64 = 44100}, 1, INT64_MAX, FLAGS },
238 { "start_time", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
239 { "st", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
240 { "duration", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
241 { "d", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
242 { "curve", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
243 { "c", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
244 { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve" },
245 { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
246 { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
247 { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
248 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve" },
249 { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
250 { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve" },
251 { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve" },
252 { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve" },
253 { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve" },
254 { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve" },
255 { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, FLAGS, "curve" },
256 { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
257 { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
258 { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, FLAGS, "curve" },
259 { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, FLAGS, "curve" },
263 AVFILTER_DEFINE_CLASS(afade);
265 static av_cold int init(AVFilterContext *ctx)
267 AudioFadeContext *s = ctx->priv;
269 if (INT64_MAX - s->nb_samples < s->start_sample)
270 return AVERROR(EINVAL);
275 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
277 AudioFadeContext *s = inlink->dst->priv;
278 AVFilterLink *outlink = inlink->dst->outputs[0];
279 int nb_samples = buf->nb_samples;
281 int64_t cur_sample = av_rescale_q(buf->pts, inlink->time_base, (AVRational){1, inlink->sample_rate});
283 if ((!s->type && (s->start_sample + s->nb_samples < cur_sample)) ||
284 ( s->type && (cur_sample + nb_samples < s->start_sample)))
285 return ff_filter_frame(outlink, buf);
287 if (av_frame_is_writable(buf)) {
290 out_buf = ff_get_audio_buffer(outlink, nb_samples);
292 return AVERROR(ENOMEM);
293 av_frame_copy_props(out_buf, buf);
296 if ((!s->type && (cur_sample + nb_samples < s->start_sample)) ||
297 ( s->type && (s->start_sample + s->nb_samples < cur_sample))) {
298 av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
299 out_buf->channels, out_buf->format);
304 start = cur_sample - s->start_sample;
306 start = s->start_sample + s->nb_samples - cur_sample;
308 s->fade_samples(out_buf->extended_data, buf->extended_data,
309 nb_samples, buf->channels,
310 s->type ? -1 : 1, start,
311 s->nb_samples, s->curve);
317 return ff_filter_frame(outlink, out_buf);
320 static const AVFilterPad avfilter_af_afade_inputs[] = {
323 .type = AVMEDIA_TYPE_AUDIO,
324 .filter_frame = filter_frame,
329 static const AVFilterPad avfilter_af_afade_outputs[] = {
332 .type = AVMEDIA_TYPE_AUDIO,
333 .config_props = config_output,
338 AVFilter ff_af_afade = {
340 .description = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
341 .query_formats = query_formats,
342 .priv_size = sizeof(AudioFadeContext),
344 .inputs = avfilter_af_afade_inputs,
345 .outputs = avfilter_af_afade_outputs,
346 .priv_class = &afade_class,
347 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
350 #endif /* CONFIG_AFADE_FILTER */
352 #if CONFIG_ACROSSFADE_FILTER
354 static const AVOption acrossfade_options[] = {
355 { "nb_samples", "set number of samples for cross fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
356 { "ns", "set number of samples for cross fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
357 { "duration", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, 60, FLAGS },
358 { "d", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, 60, FLAGS },
359 { "overlap", "overlap 1st stream end with 2nd stream start", OFFSET(overlap), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, FLAGS },
360 { "o", "overlap 1st stream end with 2nd stream start", OFFSET(overlap), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, FLAGS },
361 { "curve1", "set fade curve type for 1st stream", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
362 { "c1", "set fade curve type for 1st stream", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
363 { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, FLAGS, "curve" },
364 { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
365 { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
366 { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
367 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, FLAGS, "curve" },
368 { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
369 { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, FLAGS, "curve" },
370 { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, FLAGS, "curve" },
371 { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, FLAGS, "curve" },
372 { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, FLAGS, "curve" },
373 { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, FLAGS, "curve" },
374 { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, FLAGS, "curve" },
375 { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
376 { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
377 { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, FLAGS, "curve" },
378 { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, FLAGS, "curve" },
379 { "curve2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
380 { "c2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, 0, NB_CURVES - 1, FLAGS, "curve" },
384 AVFILTER_DEFINE_CLASS(acrossfade);
386 #define CROSSFADE_PLANAR(name, type) \
387 static void crossfade_samples_## name ##p(uint8_t **dst, uint8_t * const *cf0, \
388 uint8_t * const *cf1, \
389 int nb_samples, int channels, \
390 int curve0, int curve1) \
394 for (i = 0; i < nb_samples; i++) { \
395 double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
396 double gain1 = fade_gain(curve1, i, nb_samples); \
397 for (c = 0; c < channels; c++) { \
398 type *d = (type *)dst[c]; \
399 const type *s0 = (type *)cf0[c]; \
400 const type *s1 = (type *)cf1[c]; \
402 d[i] = s0[i] * gain0 + s1[i] * gain1; \
407 #define CROSSFADE(name, type) \
408 static void crossfade_samples_## name (uint8_t **dst, uint8_t * const *cf0, \
409 uint8_t * const *cf1, \
410 int nb_samples, int channels, \
411 int curve0, int curve1) \
413 type *d = (type *)dst[0]; \
414 const type *s0 = (type *)cf0[0]; \
415 const type *s1 = (type *)cf1[0]; \
418 for (i = 0; i < nb_samples; i++) { \
419 double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
420 double gain1 = fade_gain(curve1, i, nb_samples); \
421 for (c = 0; c < channels; c++, k++) \
422 d[k] = s0[k] * gain0 + s1[k] * gain1; \
426 CROSSFADE_PLANAR(dbl, double)
427 CROSSFADE_PLANAR(flt, float)
428 CROSSFADE_PLANAR(s16, int16_t)
429 CROSSFADE_PLANAR(s32, int32_t)
431 CROSSFADE(dbl, double)
432 CROSSFADE(flt, float)
433 CROSSFADE(s16, int16_t)
434 CROSSFADE(s32, int32_t)
436 static int activate(AVFilterContext *ctx)
438 AudioFadeContext *s = ctx->priv;
439 AVFilterLink *outlink = ctx->outputs[0];
440 AVFrame *in = NULL, *out, *cf[2] = { NULL };
441 int ret = 0, nb_samples, status;
444 FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
446 if (s->crossfade_is_over) {
447 ret = ff_inlink_consume_frame(ctx->inputs[1], &in);
450 } else if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
451 ff_outlink_set_status(ctx->outputs[0], status, pts);
454 if (ff_outlink_frame_wanted(ctx->outputs[0]) && !in) {
455 ff_inlink_request_frame(ctx->inputs[1]);
460 s->pts += av_rescale_q(in->nb_samples,
461 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
462 return ff_filter_frame(outlink, in);
465 if (ff_framequeue_queued_samples(&ctx->inputs[0]->fifo) > s->nb_samples) {
466 nb_samples = ff_framequeue_queued_samples(&ctx->inputs[0]->fifo) - s->nb_samples;
467 if (nb_samples > 0) {
468 ret = ff_inlink_consume_samples(ctx->inputs[0], nb_samples, nb_samples, &in);
474 s->pts += av_rescale_q(in->nb_samples,
475 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
476 return ff_filter_frame(outlink, in);
477 } else if (ff_framequeue_queued_samples(&ctx->inputs[1]->fifo) >= s->nb_samples) {
479 out = ff_get_audio_buffer(outlink, s->nb_samples);
481 return AVERROR(ENOMEM);
483 ret = ff_inlink_consume_samples(ctx->inputs[0], s->nb_samples, s->nb_samples, &cf[0]);
489 ret = ff_inlink_consume_samples(ctx->inputs[1], s->nb_samples, s->nb_samples, &cf[1]);
495 s->crossfade_samples(out->extended_data, cf[0]->extended_data,
496 cf[1]->extended_data,
497 s->nb_samples, out->channels,
498 s->curve, s->curve2);
500 s->pts += av_rescale_q(s->nb_samples,
501 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
502 s->crossfade_is_over = 1;
503 av_frame_free(&cf[0]);
504 av_frame_free(&cf[1]);
505 return ff_filter_frame(outlink, out);
507 out = ff_get_audio_buffer(outlink, s->nb_samples);
509 return AVERROR(ENOMEM);
511 ret = ff_inlink_consume_samples(ctx->inputs[0], s->nb_samples, s->nb_samples, &cf[0]);
517 s->fade_samples(out->extended_data, cf[0]->extended_data, s->nb_samples,
518 outlink->channels, -1, s->nb_samples - 1, s->nb_samples, s->curve);
520 s->pts += av_rescale_q(s->nb_samples,
521 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
522 av_frame_free(&cf[0]);
523 ret = ff_filter_frame(outlink, out);
527 out = ff_get_audio_buffer(outlink, s->nb_samples);
529 return AVERROR(ENOMEM);
531 ret = ff_inlink_consume_samples(ctx->inputs[1], s->nb_samples, s->nb_samples, &cf[1]);
537 s->fade_samples(out->extended_data, cf[1]->extended_data, s->nb_samples,
538 outlink->channels, 1, 0, s->nb_samples, s->curve2);
540 s->pts += av_rescale_q(s->nb_samples,
541 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
542 s->crossfade_is_over = 1;
543 av_frame_free(&cf[1]);
544 return ff_filter_frame(outlink, out);
546 } else if (ff_outlink_frame_wanted(ctx->outputs[0])) {
547 if (!s->cf0_eof && ctx->inputs[0]->status_in) {
550 if (ctx->inputs[1]->status_in) {
551 ff_outlink_set_status(ctx->outputs[0], AVERROR_EOF, AV_NOPTS_VALUE);
555 ff_inlink_request_frame(ctx->inputs[0]);
557 ff_inlink_request_frame(ctx->inputs[1]);
564 static int acrossfade_config_output(AVFilterLink *outlink)
566 AVFilterContext *ctx = outlink->src;
567 AudioFadeContext *s = ctx->priv;
569 if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
570 av_log(ctx, AV_LOG_ERROR,
571 "Inputs must have the same sample rate "
572 "%d for in0 vs %d for in1\n",
573 ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
574 return AVERROR(EINVAL);
577 outlink->sample_rate = ctx->inputs[0]->sample_rate;
578 outlink->time_base = ctx->inputs[0]->time_base;
579 outlink->channel_layout = ctx->inputs[0]->channel_layout;
580 outlink->channels = ctx->inputs[0]->channels;
582 switch (outlink->format) {
583 case AV_SAMPLE_FMT_DBL: s->crossfade_samples = crossfade_samples_dbl; break;
584 case AV_SAMPLE_FMT_DBLP: s->crossfade_samples = crossfade_samples_dblp; break;
585 case AV_SAMPLE_FMT_FLT: s->crossfade_samples = crossfade_samples_flt; break;
586 case AV_SAMPLE_FMT_FLTP: s->crossfade_samples = crossfade_samples_fltp; break;
587 case AV_SAMPLE_FMT_S16: s->crossfade_samples = crossfade_samples_s16; break;
588 case AV_SAMPLE_FMT_S16P: s->crossfade_samples = crossfade_samples_s16p; break;
589 case AV_SAMPLE_FMT_S32: s->crossfade_samples = crossfade_samples_s32; break;
590 case AV_SAMPLE_FMT_S32P: s->crossfade_samples = crossfade_samples_s32p; break;
593 config_output(outlink);
598 static const AVFilterPad avfilter_af_acrossfade_inputs[] = {
600 .name = "crossfade0",
601 .type = AVMEDIA_TYPE_AUDIO,
604 .name = "crossfade1",
605 .type = AVMEDIA_TYPE_AUDIO,
610 static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
613 .type = AVMEDIA_TYPE_AUDIO,
614 .config_props = acrossfade_config_output,
619 AVFilter ff_af_acrossfade = {
620 .name = "acrossfade",
621 .description = NULL_IF_CONFIG_SMALL("Cross fade two input audio streams."),
622 .query_formats = query_formats,
623 .priv_size = sizeof(AudioFadeContext),
624 .activate = activate,
625 .priv_class = &acrossfade_class,
626 .inputs = avfilter_af_acrossfade_inputs,
627 .outputs = avfilter_af_acrossfade_outputs,
630 #endif /* CONFIG_ACROSSFADE_FILTER */