]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_afade.c
avfilter/af_afade: add logistic sigmoid curve
[ffmpeg] / libavfilter / af_afade.c
1 /*
2  * Copyright (c) 2013-2015 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 #define FF_INTERNAL_FIELDS 1
27 #include "framequeue.h"
28
29 #include "libavutil/audio_fifo.h"
30 #include "libavutil/opt.h"
31 #include "audio.h"
32 #include "avfilter.h"
33 #include "filters.h"
34 #include "internal.h"
35
36 typedef struct AudioFadeContext {
37     const AVClass *class;
38     int type;
39     int curve, curve2;
40     int64_t nb_samples;
41     int64_t start_sample;
42     int64_t duration;
43     int64_t start_time;
44     int overlap;
45     int cf0_eof;
46     int prev_size;
47     int crossfade_is_over;
48     AVAudioFifo *fifo[2];
49     int64_t pts;
50
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,
55                               uint8_t * const *cf1,
56                               int nb_samples, int channels,
57                               int curve0, int curve1);
58 } AudioFadeContext;
59
60 enum CurveType { TRI, QSIN, ESIN, HSIN, LOG, IPAR, QUA, CUB, SQU, CBR, PAR, EXP, IQSIN, IHSIN, DESE, DESI, LOSI, NB_CURVES };
61
62 #define OFFSET(x) offsetof(AudioFadeContext, x)
63 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
64
65 static int query_formats(AVFilterContext *ctx)
66 {
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,
74         AV_SAMPLE_FMT_NONE
75     };
76     int ret;
77
78     layouts = ff_all_channel_counts();
79     if (!layouts)
80         return AVERROR(ENOMEM);
81     ret = ff_set_common_channel_layouts(ctx, layouts);
82     if (ret < 0)
83         return ret;
84
85     formats = ff_make_format_list(sample_fmts);
86     if (!formats)
87         return AVERROR(ENOMEM);
88     ret = ff_set_common_formats(ctx, formats);
89     if (ret < 0)
90         return ret;
91
92     formats = ff_all_samplerates();
93     if (!formats)
94         return AVERROR(ENOMEM);
95     return ff_set_common_samplerates(ctx, formats);
96 }
97
98 static double fade_gain(int curve, int64_t index, int64_t range)
99 {
100 #define CUBE(a) ((a)*(a)*(a))
101     double gain;
102
103     gain = av_clipd(1.0 * index / range, 0, 1.0);
104
105     switch (curve) {
106     case QSIN:
107         gain = sin(gain * M_PI / 2.0);
108         break;
109     case IQSIN:
110         /* 0.6... = 2 / M_PI */
111         gain = 0.6366197723675814 * asin(gain);
112         break;
113     case ESIN:
114         gain = 1.0 - cos(M_PI / 4.0 * (CUBE(2.0*gain - 1) + 1));
115         break;
116     case HSIN:
117         gain = (1.0 - cos(gain * M_PI)) / 2.0;
118         break;
119     case IHSIN:
120         /* 0.3... = 1 / M_PI */
121         gain = 0.3183098861837907 * acos(1 - 2 * gain);
122         break;
123     case EXP:
124         /* -11.5... = 5*ln(0.1) */
125         gain = exp(-11.512925464970227 * (1 - gain));
126         break;
127     case LOG:
128         gain = av_clipd(1 + 0.2 * log10(gain), 0, 1.0);
129         break;
130     case PAR:
131         gain = 1 - sqrt(1 - gain);
132         break;
133     case IPAR:
134         gain = (1 - (1 - gain) * (1 - gain));
135         break;
136     case QUA:
137         gain *= gain;
138         break;
139     case CUB:
140         gain = CUBE(gain);
141         break;
142     case SQU:
143         gain = sqrt(gain);
144         break;
145     case CBR:
146         gain = cbrt(gain);
147         break;
148     case DESE:
149         gain = gain <= 0.5 ? cbrt(2 * gain) / 2: 1 - cbrt(2 * (1 - gain)) / 2;
150         break;
151     case DESI:
152         gain = gain <= 0.5 ? CUBE(2 * gain) / 2: 1 - CUBE(2 * (1 - gain)) / 2;
153         break;
154     case LOSI: {
155                    const double a = 1. / (1. - 0.787) - 1;
156                    double A = 1. / (1.0 + exp(0 -((gain-0.5) * a * 2.0)));
157                    double B = 1. / (1.0 + exp(a));
158                    double C = 1. / (1.0 + exp(0-a));
159                    gain = (A - B) / (C - B);
160                }
161         break;
162     }
163
164     return gain;
165 }
166
167 #define FADE_PLANAR(name, type)                                             \
168 static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src,   \
169                                      int nb_samples, int channels, int dir, \
170                                      int64_t start, int64_t range, int curve) \
171 {                                                                           \
172     int i, c;                                                               \
173                                                                             \
174     for (i = 0; i < nb_samples; i++) {                                      \
175         double gain = fade_gain(curve, start + i * dir, range);             \
176         for (c = 0; c < channels; c++) {                                    \
177             type *d = (type *)dst[c];                                       \
178             const type *s = (type *)src[c];                                 \
179                                                                             \
180             d[i] = s[i] * gain;                                             \
181         }                                                                   \
182     }                                                                       \
183 }
184
185 #define FADE(name, type)                                                    \
186 static void fade_samples_## name (uint8_t **dst, uint8_t * const *src,      \
187                                   int nb_samples, int channels, int dir,    \
188                                   int64_t start, int64_t range, int curve)  \
189 {                                                                           \
190     type *d = (type *)dst[0];                                               \
191     const type *s = (type *)src[0];                                         \
192     int i, c, k = 0;                                                        \
193                                                                             \
194     for (i = 0; i < nb_samples; i++) {                                      \
195         double gain = fade_gain(curve, start + i * dir, range);             \
196         for (c = 0; c < channels; c++, k++)                                 \
197             d[k] = s[k] * gain;                                             \
198     }                                                                       \
199 }
200
201 FADE_PLANAR(dbl, double)
202 FADE_PLANAR(flt, float)
203 FADE_PLANAR(s16, int16_t)
204 FADE_PLANAR(s32, int32_t)
205
206 FADE(dbl, double)
207 FADE(flt, float)
208 FADE(s16, int16_t)
209 FADE(s32, int32_t)
210
211 static int config_output(AVFilterLink *outlink)
212 {
213     AVFilterContext *ctx = outlink->src;
214     AudioFadeContext *s  = ctx->priv;
215
216     switch (outlink->format) {
217     case AV_SAMPLE_FMT_DBL:  s->fade_samples = fade_samples_dbl;  break;
218     case AV_SAMPLE_FMT_DBLP: s->fade_samples = fade_samples_dblp; break;
219     case AV_SAMPLE_FMT_FLT:  s->fade_samples = fade_samples_flt;  break;
220     case AV_SAMPLE_FMT_FLTP: s->fade_samples = fade_samples_fltp; break;
221     case AV_SAMPLE_FMT_S16:  s->fade_samples = fade_samples_s16;  break;
222     case AV_SAMPLE_FMT_S16P: s->fade_samples = fade_samples_s16p; break;
223     case AV_SAMPLE_FMT_S32:  s->fade_samples = fade_samples_s32;  break;
224     case AV_SAMPLE_FMT_S32P: s->fade_samples = fade_samples_s32p; break;
225     }
226
227     if (s->duration)
228         s->nb_samples = av_rescale(s->duration, outlink->sample_rate, AV_TIME_BASE);
229     if (s->start_time)
230         s->start_sample = av_rescale(s->start_time, outlink->sample_rate, AV_TIME_BASE);
231
232     return 0;
233 }
234
235 #if CONFIG_AFADE_FILTER
236
237 static const AVOption afade_options[] = {
238     { "type",         "set the fade direction",                      OFFSET(type),         AV_OPT_TYPE_INT,    {.i64 = 0    }, 0, 1, FLAGS, "type" },
239     { "t",            "set the fade direction",                      OFFSET(type),         AV_OPT_TYPE_INT,    {.i64 = 0    }, 0, 1, FLAGS, "type" },
240     { "in",           "fade-in",                                     0,                    AV_OPT_TYPE_CONST,  {.i64 = 0    }, 0, 0, FLAGS, "type" },
241     { "out",          "fade-out",                                    0,                    AV_OPT_TYPE_CONST,  {.i64 = 1    }, 0, 0, FLAGS, "type" },
242     { "start_sample", "set number of first sample to start fading",  OFFSET(start_sample), AV_OPT_TYPE_INT64,  {.i64 = 0    }, 0, INT64_MAX, FLAGS },
243     { "ss",           "set number of first sample to start fading",  OFFSET(start_sample), AV_OPT_TYPE_INT64,  {.i64 = 0    }, 0, INT64_MAX, FLAGS },
244     { "nb_samples",   "set number of samples for fade duration",     OFFSET(nb_samples),   AV_OPT_TYPE_INT64,  {.i64 = 44100}, 1, INT64_MAX, FLAGS },
245     { "ns",           "set number of samples for fade duration",     OFFSET(nb_samples),   AV_OPT_TYPE_INT64,  {.i64 = 44100}, 1, INT64_MAX, FLAGS },
246     { "start_time",   "set time to start fading",                    OFFSET(start_time),   AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
247     { "st",           "set time to start fading",                    OFFSET(start_time),   AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
248     { "duration",     "set fade duration",                           OFFSET(duration),     AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
249     { "d",            "set fade duration",                           OFFSET(duration),     AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
250     { "curve",        "set fade curve type",                         OFFSET(curve),        AV_OPT_TYPE_INT,    {.i64 = TRI  }, 0, NB_CURVES - 1, FLAGS, "curve" },
251     { "c",            "set fade curve type",                         OFFSET(curve),        AV_OPT_TYPE_INT,    {.i64 = TRI  }, 0, NB_CURVES - 1, FLAGS, "curve" },
252     { "tri",          "linear slope",                                0,                    AV_OPT_TYPE_CONST,  {.i64 = TRI  }, 0, 0, FLAGS, "curve" },
253     { "qsin",         "quarter of sine wave",                        0,                    AV_OPT_TYPE_CONST,  {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
254     { "esin",         "exponential sine wave",                       0,                    AV_OPT_TYPE_CONST,  {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
255     { "hsin",         "half of sine wave",                           0,                    AV_OPT_TYPE_CONST,  {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
256     { "log",          "logarithmic",                                 0,                    AV_OPT_TYPE_CONST,  {.i64 = LOG  }, 0, 0, FLAGS, "curve" },
257     { "ipar",         "inverted parabola",                           0,                    AV_OPT_TYPE_CONST,  {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
258     { "qua",          "quadratic",                                   0,                    AV_OPT_TYPE_CONST,  {.i64 = QUA  }, 0, 0, FLAGS, "curve" },
259     { "cub",          "cubic",                                       0,                    AV_OPT_TYPE_CONST,  {.i64 = CUB  }, 0, 0, FLAGS, "curve" },
260     { "squ",          "square root",                                 0,                    AV_OPT_TYPE_CONST,  {.i64 = SQU  }, 0, 0, FLAGS, "curve" },
261     { "cbr",          "cubic root",                                  0,                    AV_OPT_TYPE_CONST,  {.i64 = CBR  }, 0, 0, FLAGS, "curve" },
262     { "par",          "parabola",                                    0,                    AV_OPT_TYPE_CONST,  {.i64 = PAR  }, 0, 0, FLAGS, "curve" },
263     { "exp",          "exponential",                                 0,                    AV_OPT_TYPE_CONST,  {.i64 = EXP  }, 0, 0, FLAGS, "curve" },
264     { "iqsin",        "inverted quarter of sine wave",               0,                    AV_OPT_TYPE_CONST,  {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
265     { "ihsin",        "inverted half of sine wave",                  0,                    AV_OPT_TYPE_CONST,  {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
266     { "dese",         "double-exponential seat",                     0,                    AV_OPT_TYPE_CONST,  {.i64 = DESE }, 0, 0, FLAGS, "curve" },
267     { "desi",         "double-exponential sigmoid",                  0,                    AV_OPT_TYPE_CONST,  {.i64 = DESI }, 0, 0, FLAGS, "curve" },
268     { "losi",         "logistic sigmoid",                            0,                    AV_OPT_TYPE_CONST,  {.i64 = LOSI }, 0, 0, FLAGS, "curve" },
269     { NULL }
270 };
271
272 AVFILTER_DEFINE_CLASS(afade);
273
274 static av_cold int init(AVFilterContext *ctx)
275 {
276     AudioFadeContext *s = ctx->priv;
277
278     if (INT64_MAX - s->nb_samples < s->start_sample)
279         return AVERROR(EINVAL);
280
281     return 0;
282 }
283
284 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
285 {
286     AudioFadeContext *s     = inlink->dst->priv;
287     AVFilterLink *outlink   = inlink->dst->outputs[0];
288     int nb_samples          = buf->nb_samples;
289     AVFrame *out_buf;
290     int64_t cur_sample = av_rescale_q(buf->pts, inlink->time_base, (AVRational){1, inlink->sample_rate});
291
292     if ((!s->type && (s->start_sample + s->nb_samples < cur_sample)) ||
293         ( s->type && (cur_sample + nb_samples < s->start_sample)))
294         return ff_filter_frame(outlink, buf);
295
296     if (av_frame_is_writable(buf)) {
297         out_buf = buf;
298     } else {
299         out_buf = ff_get_audio_buffer(outlink, nb_samples);
300         if (!out_buf)
301             return AVERROR(ENOMEM);
302         av_frame_copy_props(out_buf, buf);
303     }
304
305     if ((!s->type && (cur_sample + nb_samples < s->start_sample)) ||
306         ( s->type && (s->start_sample + s->nb_samples < cur_sample))) {
307         av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
308                                out_buf->channels, out_buf->format);
309     } else {
310         int64_t start;
311
312         if (!s->type)
313             start = cur_sample - s->start_sample;
314         else
315             start = s->start_sample + s->nb_samples - cur_sample;
316
317         s->fade_samples(out_buf->extended_data, buf->extended_data,
318                         nb_samples, buf->channels,
319                         s->type ? -1 : 1, start,
320                         s->nb_samples, s->curve);
321     }
322
323     if (buf != out_buf)
324         av_frame_free(&buf);
325
326     return ff_filter_frame(outlink, out_buf);
327 }
328
329 static const AVFilterPad avfilter_af_afade_inputs[] = {
330     {
331         .name         = "default",
332         .type         = AVMEDIA_TYPE_AUDIO,
333         .filter_frame = filter_frame,
334     },
335     { NULL }
336 };
337
338 static const AVFilterPad avfilter_af_afade_outputs[] = {
339     {
340         .name         = "default",
341         .type         = AVMEDIA_TYPE_AUDIO,
342         .config_props = config_output,
343     },
344     { NULL }
345 };
346
347 AVFilter ff_af_afade = {
348     .name          = "afade",
349     .description   = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
350     .query_formats = query_formats,
351     .priv_size     = sizeof(AudioFadeContext),
352     .init          = init,
353     .inputs        = avfilter_af_afade_inputs,
354     .outputs       = avfilter_af_afade_outputs,
355     .priv_class    = &afade_class,
356     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
357 };
358
359 #endif /* CONFIG_AFADE_FILTER */
360
361 #if CONFIG_ACROSSFADE_FILTER
362
363 static const AVOption acrossfade_options[] = {
364     { "nb_samples",   "set number of samples for cross fade duration", OFFSET(nb_samples),   AV_OPT_TYPE_INT,    {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
365     { "ns",           "set number of samples for cross fade duration", OFFSET(nb_samples),   AV_OPT_TYPE_INT,    {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
366     { "duration",     "set cross fade duration",                       OFFSET(duration),     AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, 60, FLAGS },
367     { "d",            "set cross fade duration",                       OFFSET(duration),     AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, 60, FLAGS },
368     { "overlap",      "overlap 1st stream end with 2nd stream start",  OFFSET(overlap),      AV_OPT_TYPE_BOOL,   {.i64 = 1    }, 0,  1, FLAGS },
369     { "o",            "overlap 1st stream end with 2nd stream start",  OFFSET(overlap),      AV_OPT_TYPE_BOOL,   {.i64 = 1    }, 0,  1, FLAGS },
370     { "curve1",       "set fade curve type for 1st stream",            OFFSET(curve),        AV_OPT_TYPE_INT,    {.i64 = TRI  }, 0, NB_CURVES - 1, FLAGS, "curve" },
371     { "c1",           "set fade curve type for 1st stream",            OFFSET(curve),        AV_OPT_TYPE_INT,    {.i64 = TRI  }, 0, NB_CURVES - 1, FLAGS, "curve" },
372     {     "tri",      "linear slope",                                  0,                    AV_OPT_TYPE_CONST,  {.i64 = TRI  }, 0, 0, FLAGS, "curve" },
373     {     "qsin",     "quarter of sine wave",                          0,                    AV_OPT_TYPE_CONST,  {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
374     {     "esin",     "exponential sine wave",                         0,                    AV_OPT_TYPE_CONST,  {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
375     {     "hsin",     "half of sine wave",                             0,                    AV_OPT_TYPE_CONST,  {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
376     {     "log",      "logarithmic",                                   0,                    AV_OPT_TYPE_CONST,  {.i64 = LOG  }, 0, 0, FLAGS, "curve" },
377     {     "ipar",     "inverted parabola",                             0,                    AV_OPT_TYPE_CONST,  {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
378     {     "qua",      "quadratic",                                     0,                    AV_OPT_TYPE_CONST,  {.i64 = QUA  }, 0, 0, FLAGS, "curve" },
379     {     "cub",      "cubic",                                         0,                    AV_OPT_TYPE_CONST,  {.i64 = CUB  }, 0, 0, FLAGS, "curve" },
380     {     "squ",      "square root",                                   0,                    AV_OPT_TYPE_CONST,  {.i64 = SQU  }, 0, 0, FLAGS, "curve" },
381     {     "cbr",      "cubic root",                                    0,                    AV_OPT_TYPE_CONST,  {.i64 = CBR  }, 0, 0, FLAGS, "curve" },
382     {     "par",      "parabola",                                      0,                    AV_OPT_TYPE_CONST,  {.i64 = PAR  }, 0, 0, FLAGS, "curve" },
383     {     "exp",      "exponential",                                   0,                    AV_OPT_TYPE_CONST,  {.i64 = EXP  }, 0, 0, FLAGS, "curve" },
384     {     "iqsin",    "inverted quarter of sine wave",                 0,                    AV_OPT_TYPE_CONST,  {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
385     {     "ihsin",    "inverted half of sine wave",                    0,                    AV_OPT_TYPE_CONST,  {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
386     {     "dese",     "double-exponential seat",                       0,                    AV_OPT_TYPE_CONST,  {.i64 = DESE }, 0, 0, FLAGS, "curve" },
387     {     "desi",     "double-exponential sigmoid",                    0,                    AV_OPT_TYPE_CONST,  {.i64 = DESI }, 0, 0, FLAGS, "curve" },
388     {     "losi",     "logistic sigmoid",                              0,                    AV_OPT_TYPE_CONST,  {.i64 = LOSI }, 0, 0, FLAGS, "curve" },
389     { "curve2",       "set fade curve type for 2nd stream",            OFFSET(curve2),       AV_OPT_TYPE_INT,    {.i64 = TRI  }, 0, NB_CURVES - 1, FLAGS, "curve" },
390     { "c2",           "set fade curve type for 2nd stream",            OFFSET(curve2),       AV_OPT_TYPE_INT,    {.i64 = TRI  }, 0, NB_CURVES - 1, FLAGS, "curve" },
391     { NULL }
392 };
393
394 AVFILTER_DEFINE_CLASS(acrossfade);
395
396 #define CROSSFADE_PLANAR(name, type)                                           \
397 static void crossfade_samples_## name ##p(uint8_t **dst, uint8_t * const *cf0, \
398                                           uint8_t * const *cf1,                \
399                                           int nb_samples, int channels,        \
400                                           int curve0, int curve1)              \
401 {                                                                              \
402     int i, c;                                                                  \
403                                                                                \
404     for (i = 0; i < nb_samples; i++) {                                         \
405         double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples);      \
406         double gain1 = fade_gain(curve1, i, nb_samples);                       \
407         for (c = 0; c < channels; c++) {                                       \
408             type *d = (type *)dst[c];                                          \
409             const type *s0 = (type *)cf0[c];                                   \
410             const type *s1 = (type *)cf1[c];                                   \
411                                                                                \
412             d[i] = s0[i] * gain0 + s1[i] * gain1;                              \
413         }                                                                      \
414     }                                                                          \
415 }
416
417 #define CROSSFADE(name, type)                                               \
418 static void crossfade_samples_## name (uint8_t **dst, uint8_t * const *cf0, \
419                                        uint8_t * const *cf1,                \
420                                        int nb_samples, int channels,        \
421                                        int curve0, int curve1)              \
422 {                                                                           \
423     type *d = (type *)dst[0];                                               \
424     const type *s0 = (type *)cf0[0];                                        \
425     const type *s1 = (type *)cf1[0];                                        \
426     int i, c, k = 0;                                                        \
427                                                                             \
428     for (i = 0; i < nb_samples; i++) {                                      \
429         double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples);   \
430         double gain1 = fade_gain(curve1, i, nb_samples);                    \
431         for (c = 0; c < channels; c++, k++)                                 \
432             d[k] = s0[k] * gain0 + s1[k] * gain1;                           \
433     }                                                                       \
434 }
435
436 CROSSFADE_PLANAR(dbl, double)
437 CROSSFADE_PLANAR(flt, float)
438 CROSSFADE_PLANAR(s16, int16_t)
439 CROSSFADE_PLANAR(s32, int32_t)
440
441 CROSSFADE(dbl, double)
442 CROSSFADE(flt, float)
443 CROSSFADE(s16, int16_t)
444 CROSSFADE(s32, int32_t)
445
446 static int activate(AVFilterContext *ctx)
447 {
448     AudioFadeContext *s   = ctx->priv;
449     AVFilterLink *outlink = ctx->outputs[0];
450     AVFrame *in = NULL, *out, *cf[2] = { NULL };
451     int ret = 0, nb_samples, status;
452     int64_t pts;
453
454     FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
455
456     if (s->crossfade_is_over) {
457         ret = ff_inlink_consume_frame(ctx->inputs[1], &in);
458         if (ret < 0) {
459             return ret;
460         } else if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
461             ff_outlink_set_status(ctx->outputs[0], status, pts);
462             return 0;
463         } else {
464             if (ff_outlink_frame_wanted(ctx->outputs[0]) && !in) {
465                 ff_inlink_request_frame(ctx->inputs[1]);
466                 return 0;
467             }
468         }
469         in->pts = s->pts;
470         s->pts += av_rescale_q(in->nb_samples,
471             (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
472         return ff_filter_frame(outlink, in);
473     }
474
475     if (ff_framequeue_queued_samples(&ctx->inputs[0]->fifo) > s->nb_samples) {
476         nb_samples = ff_framequeue_queued_samples(&ctx->inputs[0]->fifo) - s->nb_samples;
477         if (nb_samples > 0) {
478             ret = ff_inlink_consume_samples(ctx->inputs[0], nb_samples, nb_samples, &in);
479             if (ret < 0) {
480                 return ret;
481             }
482         }
483         in->pts = s->pts;
484         s->pts += av_rescale_q(in->nb_samples,
485             (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
486         return ff_filter_frame(outlink, in);
487     } else if (ff_framequeue_queued_samples(&ctx->inputs[1]->fifo) >= s->nb_samples) {
488         if (s->overlap) {
489             out = ff_get_audio_buffer(outlink, s->nb_samples);
490             if (!out)
491                 return AVERROR(ENOMEM);
492
493             ret = ff_inlink_consume_samples(ctx->inputs[0], s->nb_samples, s->nb_samples, &cf[0]);
494             if (ret < 0) {
495                 av_frame_free(&out);
496                 return ret;
497             }
498
499             ret = ff_inlink_consume_samples(ctx->inputs[1], s->nb_samples, s->nb_samples, &cf[1]);
500             if (ret < 0) {
501                 av_frame_free(&out);
502                 return ret;
503             }
504
505             s->crossfade_samples(out->extended_data, cf[0]->extended_data,
506                                  cf[1]->extended_data,
507                                  s->nb_samples, out->channels,
508                                  s->curve, s->curve2);
509             out->pts = s->pts;
510             s->pts += av_rescale_q(s->nb_samples,
511                 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
512             s->crossfade_is_over = 1;
513             av_frame_free(&cf[0]);
514             av_frame_free(&cf[1]);
515             return ff_filter_frame(outlink, out);
516         } else {
517             out = ff_get_audio_buffer(outlink, s->nb_samples);
518             if (!out)
519                 return AVERROR(ENOMEM);
520
521             ret = ff_inlink_consume_samples(ctx->inputs[0], s->nb_samples, s->nb_samples, &cf[0]);
522             if (ret < 0) {
523                 av_frame_free(&out);
524                 return ret;
525             }
526
527             s->fade_samples(out->extended_data, cf[0]->extended_data, s->nb_samples,
528                             outlink->channels, -1, s->nb_samples - 1, s->nb_samples, s->curve);
529             out->pts = s->pts;
530             s->pts += av_rescale_q(s->nb_samples,
531                 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
532             av_frame_free(&cf[0]);
533             ret = ff_filter_frame(outlink, out);
534             if (ret < 0)
535                 return ret;
536
537             out = ff_get_audio_buffer(outlink, s->nb_samples);
538             if (!out)
539                 return AVERROR(ENOMEM);
540
541             ret = ff_inlink_consume_samples(ctx->inputs[1], s->nb_samples, s->nb_samples, &cf[1]);
542             if (ret < 0) {
543                 av_frame_free(&out);
544                 return ret;
545             }
546
547             s->fade_samples(out->extended_data, cf[1]->extended_data, s->nb_samples,
548                             outlink->channels, 1, 0, s->nb_samples, s->curve2);
549             out->pts = s->pts;
550             s->pts += av_rescale_q(s->nb_samples,
551                 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
552             s->crossfade_is_over = 1;
553             av_frame_free(&cf[1]);
554             return ff_filter_frame(outlink, out);
555         }
556     } else if (ff_outlink_frame_wanted(ctx->outputs[0])) {
557         if (!s->cf0_eof && ctx->inputs[0]->status_in) {
558             s->cf0_eof = 1;
559         }
560         if (ctx->inputs[1]->status_in) {
561             ff_outlink_set_status(ctx->outputs[0], AVERROR_EOF, AV_NOPTS_VALUE);
562             return 0;
563         }
564         if (!s->cf0_eof)
565             ff_inlink_request_frame(ctx->inputs[0]);
566         else
567             ff_inlink_request_frame(ctx->inputs[1]);
568         return 0;
569     }
570
571     return ret;
572 }
573
574 static int acrossfade_config_output(AVFilterLink *outlink)
575 {
576     AVFilterContext *ctx = outlink->src;
577     AudioFadeContext *s  = ctx->priv;
578
579     if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
580         av_log(ctx, AV_LOG_ERROR,
581                "Inputs must have the same sample rate "
582                "%d for in0 vs %d for in1\n",
583                ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
584         return AVERROR(EINVAL);
585     }
586
587     outlink->sample_rate = ctx->inputs[0]->sample_rate;
588     outlink->time_base   = ctx->inputs[0]->time_base;
589     outlink->channel_layout = ctx->inputs[0]->channel_layout;
590     outlink->channels = ctx->inputs[0]->channels;
591
592     switch (outlink->format) {
593     case AV_SAMPLE_FMT_DBL:  s->crossfade_samples = crossfade_samples_dbl;  break;
594     case AV_SAMPLE_FMT_DBLP: s->crossfade_samples = crossfade_samples_dblp; break;
595     case AV_SAMPLE_FMT_FLT:  s->crossfade_samples = crossfade_samples_flt;  break;
596     case AV_SAMPLE_FMT_FLTP: s->crossfade_samples = crossfade_samples_fltp; break;
597     case AV_SAMPLE_FMT_S16:  s->crossfade_samples = crossfade_samples_s16;  break;
598     case AV_SAMPLE_FMT_S16P: s->crossfade_samples = crossfade_samples_s16p; break;
599     case AV_SAMPLE_FMT_S32:  s->crossfade_samples = crossfade_samples_s32;  break;
600     case AV_SAMPLE_FMT_S32P: s->crossfade_samples = crossfade_samples_s32p; break;
601     }
602
603     config_output(outlink);
604
605     return 0;
606 }
607
608 static const AVFilterPad avfilter_af_acrossfade_inputs[] = {
609     {
610         .name         = "crossfade0",
611         .type         = AVMEDIA_TYPE_AUDIO,
612     },
613     {
614         .name         = "crossfade1",
615         .type         = AVMEDIA_TYPE_AUDIO,
616     },
617     { NULL }
618 };
619
620 static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
621     {
622         .name          = "default",
623         .type          = AVMEDIA_TYPE_AUDIO,
624         .config_props  = acrossfade_config_output,
625     },
626     { NULL }
627 };
628
629 AVFilter ff_af_acrossfade = {
630     .name          = "acrossfade",
631     .description   = NULL_IF_CONFIG_SMALL("Cross fade two input audio streams."),
632     .query_formats = query_formats,
633     .priv_size     = sizeof(AudioFadeContext),
634     .activate      = activate,
635     .priv_class    = &acrossfade_class,
636     .inputs        = avfilter_af_acrossfade_inputs,
637     .outputs       = avfilter_af_acrossfade_outputs,
638 };
639
640 #endif /* CONFIG_ACROSSFADE_FILTER */