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