GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavfilter/af_afade.c Lines: 138 260 53.1 %
Date: 2020-09-25 23:16:12 Branches: 71 204 34.8 %

Line Branch Exec Source
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, NONE, 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
7
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
7
    layouts = ff_all_channel_counts();
73
7
    if (!layouts)
74
        return AVERROR(ENOMEM);
75
7
    ret = ff_set_common_channel_layouts(ctx, layouts);
76
7
    if (ret < 0)
77
        return ret;
78
79
7
    formats = ff_make_format_list(sample_fmts);
80
7
    if (!formats)
81
        return AVERROR(ENOMEM);
82
7
    ret = ff_set_common_formats(ctx, formats);
83
7
    if (ret < 0)
84
        return ret;
85
86
7
    formats = ff_all_samplerates();
87
7
    if (!formats)
88
        return AVERROR(ENOMEM);
89
7
    return ff_set_common_samplerates(ctx, formats);
90
}
91
92
732432
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
732432
    gain = av_clipd(1.0 * index / range, 0, 1.0);
98
99




732432
    switch (curve) {
100
89088
    case QSIN:
101
89088
        gain = sin(gain * M_PI / 2.0);
102
89088
        break;
103
89088
    case IQSIN:
104
        /* 0.6... = 2 / M_PI */
105
89088
        gain = 0.6366197723675814 * asin(gain);
106
89088
        break;
107
89088
    case ESIN:
108
89088
        gain = 1.0 - cos(M_PI / 4.0 * (CUBE(2.0*gain - 1) + 1));
109
89088
        break;
110
89088
    case HSIN:
111
89088
        gain = (1.0 - cos(gain * M_PI)) / 2.0;
112
89088
        break;
113
    case IHSIN:
114
        /* 0.3... = 1 / M_PI */
115
        gain = 0.3183098861837907 * acos(1 - 2 * gain);
116
        break;
117
177288
    case EXP:
118
        /* -11.5... = 5*ln(0.1) */
119
177288
        gain = exp(-11.512925464970227 * (1 - gain));
120
177288
        break;
121
198792
    case LOG:
122
198792
        gain = av_clipd(1 + 0.2 * log10(gain), 0, 1.0);
123
198792
        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
    case NONE:
157
        gain = 1.0;
158
        break;
159
    }
160
161
732432
    return gain;
162
}
163
164
#define FADE_PLANAR(name, type)                                             \
165
static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src,   \
166
                                     int nb_samples, int channels, int dir, \
167
                                     int64_t start, int64_t range, int curve) \
168
{                                                                           \
169
    int i, c;                                                               \
170
                                                                            \
171
    for (i = 0; i < nb_samples; i++) {                                      \
172
        double gain = fade_gain(curve, start + i * dir, range);             \
173
        for (c = 0; c < channels; c++) {                                    \
174
            type *d = (type *)dst[c];                                       \
175
            const type *s = (type *)src[c];                                 \
176
                                                                            \
177
            d[i] = s[i] * gain;                                             \
178
        }                                                                   \
179
    }                                                                       \
180
}
181
182
#define FADE(name, type)                                                    \
183
static void fade_samples_## name (uint8_t **dst, uint8_t * const *src,      \
184
                                  int nb_samples, int channels, int dir,    \
185
                                  int64_t start, int64_t range, int curve)  \
186
{                                                                           \
187
    type *d = (type *)dst[0];                                               \
188
    const type *s = (type *)src[0];                                         \
189
    int i, c, k = 0;                                                        \
190
                                                                            \
191
    for (i = 0; i < nb_samples; i++) {                                      \
192
        double gain = fade_gain(curve, start + i * dir, range);             \
193
        for (c = 0; c < channels; c++, k++)                                 \
194
            d[k] = s[k] * gain;                                             \
195
    }                                                                       \
196
}
197
198
FADE_PLANAR(dbl, double)
199
FADE_PLANAR(flt, float)
200
FADE_PLANAR(s16, int16_t)
201
FADE_PLANAR(s32, int32_t)
202
203
FADE(dbl, double)
204
FADE(flt, float)
205

1668639
FADE(s16, int16_t)
206
FADE(s32, int32_t)
207
208
7
static int config_output(AVFilterLink *outlink)
209
{
210
7
    AVFilterContext *ctx = outlink->src;
211
7
    AudioFadeContext *s  = ctx->priv;
212
213


7
    switch (outlink->format) {
214
    case AV_SAMPLE_FMT_DBL:  s->fade_samples = fade_samples_dbl;  break;
215
    case AV_SAMPLE_FMT_DBLP: s->fade_samples = fade_samples_dblp; break;
216
    case AV_SAMPLE_FMT_FLT:  s->fade_samples = fade_samples_flt;  break;
217
    case AV_SAMPLE_FMT_FLTP: s->fade_samples = fade_samples_fltp; break;
218
7
    case AV_SAMPLE_FMT_S16:  s->fade_samples = fade_samples_s16;  break;
219
    case AV_SAMPLE_FMT_S16P: s->fade_samples = fade_samples_s16p; break;
220
    case AV_SAMPLE_FMT_S32:  s->fade_samples = fade_samples_s32;  break;
221
    case AV_SAMPLE_FMT_S32P: s->fade_samples = fade_samples_s32p; break;
222
    }
223
224
7
    if (s->duration)
225
7
        s->nb_samples = av_rescale(s->duration, outlink->sample_rate, AV_TIME_BASE);
226
7
    if (s->start_time)
227
        s->start_sample = av_rescale(s->start_time, outlink->sample_rate, AV_TIME_BASE);
228
229
7
    return 0;
230
}
231
232
#if CONFIG_AFADE_FILTER
233
234
static const AVOption afade_options[] = {
235
    { "type",         "set the fade direction",                      OFFSET(type),         AV_OPT_TYPE_INT,    {.i64 = 0    }, 0, 1, FLAGS, "type" },
236
    { "t",            "set the fade direction",                      OFFSET(type),         AV_OPT_TYPE_INT,    {.i64 = 0    }, 0, 1, FLAGS, "type" },
237
    { "in",           "fade-in",                                     0,                    AV_OPT_TYPE_CONST,  {.i64 = 0    }, 0, 0, FLAGS, "type" },
238
    { "out",          "fade-out",                                    0,                    AV_OPT_TYPE_CONST,  {.i64 = 1    }, 0, 0, FLAGS, "type" },
239
    { "start_sample", "set number of first sample to start fading",  OFFSET(start_sample), AV_OPT_TYPE_INT64,  {.i64 = 0    }, 0, INT64_MAX, FLAGS },
240
    { "ss",           "set number of first sample to start fading",  OFFSET(start_sample), AV_OPT_TYPE_INT64,  {.i64 = 0    }, 0, INT64_MAX, FLAGS },
241
    { "nb_samples",   "set number of samples for fade duration",     OFFSET(nb_samples),   AV_OPT_TYPE_INT64,  {.i64 = 44100}, 1, INT64_MAX, FLAGS },
242
    { "ns",           "set number of samples for fade duration",     OFFSET(nb_samples),   AV_OPT_TYPE_INT64,  {.i64 = 44100}, 1, INT64_MAX, FLAGS },
243
    { "start_time",   "set time to start fading",                    OFFSET(start_time),   AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
244
    { "st",           "set time to start fading",                    OFFSET(start_time),   AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
245
    { "duration",     "set fade duration",                           OFFSET(duration),     AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
246
    { "d",            "set fade duration",                           OFFSET(duration),     AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT64_MAX, FLAGS },
247
    { "curve",        "set fade curve type",                         OFFSET(curve),        AV_OPT_TYPE_INT,    {.i64 = TRI  }, 0, NB_CURVES - 1, FLAGS, "curve" },
248
    { "c",            "set fade curve type",                         OFFSET(curve),        AV_OPT_TYPE_INT,    {.i64 = TRI  }, 0, NB_CURVES - 1, FLAGS, "curve" },
249
    { "tri",          "linear slope",                                0,                    AV_OPT_TYPE_CONST,  {.i64 = TRI  }, 0, 0, FLAGS, "curve" },
250
    { "qsin",         "quarter of sine wave",                        0,                    AV_OPT_TYPE_CONST,  {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
251
    { "esin",         "exponential sine wave",                       0,                    AV_OPT_TYPE_CONST,  {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
252
    { "hsin",         "half of sine wave",                           0,                    AV_OPT_TYPE_CONST,  {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
253
    { "log",          "logarithmic",                                 0,                    AV_OPT_TYPE_CONST,  {.i64 = LOG  }, 0, 0, FLAGS, "curve" },
254
    { "ipar",         "inverted parabola",                           0,                    AV_OPT_TYPE_CONST,  {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
255
    { "qua",          "quadratic",                                   0,                    AV_OPT_TYPE_CONST,  {.i64 = QUA  }, 0, 0, FLAGS, "curve" },
256
    { "cub",          "cubic",                                       0,                    AV_OPT_TYPE_CONST,  {.i64 = CUB  }, 0, 0, FLAGS, "curve" },
257
    { "squ",          "square root",                                 0,                    AV_OPT_TYPE_CONST,  {.i64 = SQU  }, 0, 0, FLAGS, "curve" },
258
    { "cbr",          "cubic root",                                  0,                    AV_OPT_TYPE_CONST,  {.i64 = CBR  }, 0, 0, FLAGS, "curve" },
259
    { "par",          "parabola",                                    0,                    AV_OPT_TYPE_CONST,  {.i64 = PAR  }, 0, 0, FLAGS, "curve" },
260
    { "exp",          "exponential",                                 0,                    AV_OPT_TYPE_CONST,  {.i64 = EXP  }, 0, 0, FLAGS, "curve" },
261
    { "iqsin",        "inverted quarter of sine wave",               0,                    AV_OPT_TYPE_CONST,  {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
262
    { "ihsin",        "inverted half of sine wave",                  0,                    AV_OPT_TYPE_CONST,  {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
263
    { "dese",         "double-exponential seat",                     0,                    AV_OPT_TYPE_CONST,  {.i64 = DESE }, 0, 0, FLAGS, "curve" },
264
    { "desi",         "double-exponential sigmoid",                  0,                    AV_OPT_TYPE_CONST,  {.i64 = DESI }, 0, 0, FLAGS, "curve" },
265
    { "losi",         "logistic sigmoid",                            0,                    AV_OPT_TYPE_CONST,  {.i64 = LOSI }, 0, 0, FLAGS, "curve" },
266
    { "nofade",       "no fade; keep audio as-is",                   0,                    AV_OPT_TYPE_CONST,  {.i64 = NONE }, 0, 0, FLAGS, "curve" },
267
    { NULL }
268
};
269
270
AVFILTER_DEFINE_CLASS(afade);
271
272
6
static av_cold int init(AVFilterContext *ctx)
273
{
274
6
    AudioFadeContext *s = ctx->priv;
275
276
6
    if (INT64_MAX - s->nb_samples < s->start_sample)
277
        return AVERROR(EINVAL);
278
279
6
    return 0;
280
}
281
282
1554
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
283
{
284
1554
    AudioFadeContext *s     = inlink->dst->priv;
285
1554
    AVFilterLink *outlink   = inlink->dst->outputs[0];
286
1554
    int nb_samples          = buf->nb_samples;
287
    AVFrame *out_buf;
288
1554
    int64_t cur_sample = av_rescale_q(buf->pts, inlink->time_base, (AVRational){1, inlink->sample_rate});
289
290

1554
    if ((!s->type && (s->start_sample + s->nb_samples < cur_sample)) ||
291

543
        ( s->type && (cur_sample + nb_samples < s->start_sample)))
292
1011
        return ff_filter_frame(outlink, buf);
293
294
543
    if (av_frame_is_writable(buf)) {
295
543
        out_buf = buf;
296
    } else {
297
        out_buf = ff_get_audio_buffer(outlink, nb_samples);
298
        if (!out_buf)
299
            return AVERROR(ENOMEM);
300
        av_frame_copy_props(out_buf, buf);
301
    }
302
303

543
    if ((!s->type && (cur_sample + nb_samples < s->start_sample)) ||
304

543
        ( s->type && (s->start_sample + s->nb_samples < cur_sample))) {
305
        av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
306
                               out_buf->channels, out_buf->format);
307
    } else {
308
        int64_t start;
309
310
543
        if (!s->type)
311
543
            start = cur_sample - s->start_sample;
312
        else
313
            start = s->start_sample + s->nb_samples - cur_sample;
314
315
543
        s->fade_samples(out_buf->extended_data, buf->extended_data,
316
543
                        nb_samples, buf->channels,
317
543
                        s->type ? -1 : 1, start,
318
                        s->nb_samples, s->curve);
319
    }
320
321
543
    if (buf != out_buf)
322
        av_frame_free(&buf);
323
324
543
    return ff_filter_frame(outlink, out_buf);
325
}
326
327
static const AVFilterPad avfilter_af_afade_inputs[] = {
328
    {
329
        .name         = "default",
330
        .type         = AVMEDIA_TYPE_AUDIO,
331
        .filter_frame = filter_frame,
332
    },
333
    { NULL }
334
};
335
336
static const AVFilterPad avfilter_af_afade_outputs[] = {
337
    {
338
        .name         = "default",
339
        .type         = AVMEDIA_TYPE_AUDIO,
340
        .config_props = config_output,
341
    },
342
    { NULL }
343
};
344
345
AVFilter ff_af_afade = {
346
    .name          = "afade",
347
    .description   = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
348
    .query_formats = query_formats,
349
    .priv_size     = sizeof(AudioFadeContext),
350
    .init          = init,
351
    .inputs        = avfilter_af_afade_inputs,
352
    .outputs       = avfilter_af_afade_outputs,
353
    .priv_class    = &afade_class,
354
    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
355
};
356
357
#endif /* CONFIG_AFADE_FILTER */
358
359
#if CONFIG_ACROSSFADE_FILTER
360
361
static const AVOption acrossfade_options[] = {
362
    { "nb_samples",   "set number of samples for cross fade duration", OFFSET(nb_samples),   AV_OPT_TYPE_INT,    {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
363
    { "ns",           "set number of samples for cross fade duration", OFFSET(nb_samples),   AV_OPT_TYPE_INT,    {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
364
    { "duration",     "set cross fade duration",                       OFFSET(duration),     AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, 60000000, FLAGS },
365
    { "d",            "set cross fade duration",                       OFFSET(duration),     AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, 60000000, FLAGS },
366
    { "overlap",      "overlap 1st stream end with 2nd stream start",  OFFSET(overlap),      AV_OPT_TYPE_BOOL,   {.i64 = 1    }, 0,  1, FLAGS },
367
    { "o",            "overlap 1st stream end with 2nd stream start",  OFFSET(overlap),      AV_OPT_TYPE_BOOL,   {.i64 = 1    }, 0,  1, FLAGS },
368
    { "curve1",       "set fade curve type for 1st stream",            OFFSET(curve),        AV_OPT_TYPE_INT,    {.i64 = TRI  }, 0, NB_CURVES - 1, FLAGS, "curve" },
369
    { "c1",           "set fade curve type for 1st stream",            OFFSET(curve),        AV_OPT_TYPE_INT,    {.i64 = TRI  }, 0, NB_CURVES - 1, FLAGS, "curve" },
370
    {     "tri",      "linear slope",                                  0,                    AV_OPT_TYPE_CONST,  {.i64 = TRI  }, 0, 0, FLAGS, "curve" },
371
    {     "qsin",     "quarter of sine wave",                          0,                    AV_OPT_TYPE_CONST,  {.i64 = QSIN }, 0, 0, FLAGS, "curve" },
372
    {     "esin",     "exponential sine wave",                         0,                    AV_OPT_TYPE_CONST,  {.i64 = ESIN }, 0, 0, FLAGS, "curve" },
373
    {     "hsin",     "half of sine wave",                             0,                    AV_OPT_TYPE_CONST,  {.i64 = HSIN }, 0, 0, FLAGS, "curve" },
374
    {     "log",      "logarithmic",                                   0,                    AV_OPT_TYPE_CONST,  {.i64 = LOG  }, 0, 0, FLAGS, "curve" },
375
    {     "ipar",     "inverted parabola",                             0,                    AV_OPT_TYPE_CONST,  {.i64 = IPAR }, 0, 0, FLAGS, "curve" },
376
    {     "qua",      "quadratic",                                     0,                    AV_OPT_TYPE_CONST,  {.i64 = QUA  }, 0, 0, FLAGS, "curve" },
377
    {     "cub",      "cubic",                                         0,                    AV_OPT_TYPE_CONST,  {.i64 = CUB  }, 0, 0, FLAGS, "curve" },
378
    {     "squ",      "square root",                                   0,                    AV_OPT_TYPE_CONST,  {.i64 = SQU  }, 0, 0, FLAGS, "curve" },
379
    {     "cbr",      "cubic root",                                    0,                    AV_OPT_TYPE_CONST,  {.i64 = CBR  }, 0, 0, FLAGS, "curve" },
380
    {     "par",      "parabola",                                      0,                    AV_OPT_TYPE_CONST,  {.i64 = PAR  }, 0, 0, FLAGS, "curve" },
381
    {     "exp",      "exponential",                                   0,                    AV_OPT_TYPE_CONST,  {.i64 = EXP  }, 0, 0, FLAGS, "curve" },
382
    {     "iqsin",    "inverted quarter of sine wave",                 0,                    AV_OPT_TYPE_CONST,  {.i64 = IQSIN}, 0, 0, FLAGS, "curve" },
383
    {     "ihsin",    "inverted half of sine wave",                    0,                    AV_OPT_TYPE_CONST,  {.i64 = IHSIN}, 0, 0, FLAGS, "curve" },
384
    {     "dese",     "double-exponential seat",                       0,                    AV_OPT_TYPE_CONST,  {.i64 = DESE }, 0, 0, FLAGS, "curve" },
385
    {     "desi",     "double-exponential sigmoid",                    0,                    AV_OPT_TYPE_CONST,  {.i64 = DESI }, 0, 0, FLAGS, "curve" },
386
    {     "losi",     "logistic sigmoid",                              0,                    AV_OPT_TYPE_CONST,  {.i64 = LOSI }, 0, 0, FLAGS, "curve" },
387
    {     "nofade",   "no fade; keep audio as-is",                     0,                    AV_OPT_TYPE_CONST,  {.i64 = NONE }, 0, 0, FLAGS, "curve" },
388
    { "curve2",       "set fade curve type for 2nd stream",            OFFSET(curve2),       AV_OPT_TYPE_INT,    {.i64 = TRI  }, 0, NB_CURVES - 1, FLAGS, "curve" },
389
    { "c2",           "set fade curve type for 2nd stream",            OFFSET(curve2),       AV_OPT_TYPE_INT,    {.i64 = TRI  }, 0, NB_CURVES - 1, FLAGS, "curve" },
390
    { NULL }
391
};
392
393
AVFILTER_DEFINE_CLASS(acrossfade);
394
395
#define CROSSFADE_PLANAR(name, type)                                           \
396
static void crossfade_samples_## name ##p(uint8_t **dst, uint8_t * const *cf0, \
397
                                          uint8_t * const *cf1,                \
398
                                          int nb_samples, int channels,        \
399
                                          int curve0, int curve1)              \
400
{                                                                              \
401
    int i, c;                                                                  \
402
                                                                               \
403
    for (i = 0; i < nb_samples; i++) {                                         \
404
        double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples);      \
405
        double gain1 = fade_gain(curve1, i, nb_samples);                       \
406
        for (c = 0; c < channels; c++) {                                       \
407
            type *d = (type *)dst[c];                                          \
408
            const type *s0 = (type *)cf0[c];                                   \
409
            const type *s1 = (type *)cf1[c];                                   \
410
                                                                               \
411
            d[i] = s0[i] * gain0 + s1[i] * gain1;                              \
412
        }                                                                      \
413
    }                                                                          \
414
}
415
416
#define CROSSFADE(name, type)                                               \
417
static void crossfade_samples_## name (uint8_t **dst, uint8_t * const *cf0, \
418
                                       uint8_t * const *cf1,                \
419
                                       int nb_samples, int channels,        \
420
                                       int curve0, int curve1)              \
421
{                                                                           \
422
    type *d = (type *)dst[0];                                               \
423
    const type *s0 = (type *)cf0[0];                                        \
424
    const type *s1 = (type *)cf1[0];                                        \
425
    int i, c, k = 0;                                                        \
426
                                                                            \
427
    for (i = 0; i < nb_samples; i++) {                                      \
428
        double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples);   \
429
        double gain1 = fade_gain(curve1, i, nb_samples);                    \
430
        for (c = 0; c < channels; c++, k++)                                 \
431
            d[k] = s0[k] * gain0 + s1[k] * gain1;                           \
432
    }                                                                       \
433
}
434
435
CROSSFADE_PLANAR(dbl, double)
436
CROSSFADE_PLANAR(flt, float)
437
CROSSFADE_PLANAR(s16, int16_t)
438
CROSSFADE_PLANAR(s32, int32_t)
439
440
CROSSFADE(dbl, double)
441
CROSSFADE(flt, float)
442

264601
CROSSFADE(s16, int16_t)
443
CROSSFADE(s32, int32_t)
444
445
1170
static int activate(AVFilterContext *ctx)
446
{
447
1170
    AudioFadeContext *s   = ctx->priv;
448
1170
    AVFilterLink *outlink = ctx->outputs[0];
449
1170
    AVFrame *in = NULL, *out, *cf[2] = { NULL };
450
1170
    int ret = 0, nb_samples, status;
451
    int64_t pts;
452
453

1172
    FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
454
455
1169
    if (s->crossfade_is_over) {
456
649
        ret = ff_inlink_consume_frame(ctx->inputs[1], &in);
457
649
        if (ret > 0) {
458
324
            in->pts = s->pts;
459
324
            s->pts += av_rescale_q(in->nb_samples,
460
324
                      (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
461
324
            return ff_filter_frame(outlink, in);
462
325
        } else if (ret < 0) {
463
            return ret;
464
325
        } else if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
465
1
            ff_outlink_set_status(ctx->outputs[0], status, pts);
466
1
            return 0;
467
324
        } else if (!ret) {
468
324
            if (ff_outlink_frame_wanted(ctx->outputs[0])) {
469
324
                ff_inlink_request_frame(ctx->inputs[1]);
470
324
                return 0;
471
            }
472
        }
473
    }
474
475
520
    if (ff_inlink_queued_samples(ctx->inputs[0]) > s->nb_samples) {
476
173
        nb_samples = ff_inlink_queued_samples(ctx->inputs[0]) - s->nb_samples;
477
173
        if (nb_samples > 0) {
478
173
            ret = ff_inlink_consume_samples(ctx->inputs[0], nb_samples, nb_samples, &in);
479
173
            if (ret < 0) {
480
                return ret;
481
            }
482
        }
483
173
        in->pts = s->pts;
484
173
        s->pts += av_rescale_q(in->nb_samples,
485
173
            (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
486
173
        return ff_filter_frame(outlink, in);
487
347
    } else if (ff_inlink_queued_samples(ctx->inputs[0]) >= s->nb_samples &&
488

260
               ff_inlink_queued_samples(ctx->inputs[1]) >= s->nb_samples && s->cf0_eof) {
489
1
        if (s->overlap) {
490
1
            out = ff_get_audio_buffer(outlink, s->nb_samples);
491
1
            if (!out)
492
                return AVERROR(ENOMEM);
493
494
1
            ret = ff_inlink_consume_samples(ctx->inputs[0], s->nb_samples, s->nb_samples, &cf[0]);
495
1
            if (ret < 0) {
496
                av_frame_free(&out);
497
                return ret;
498
            }
499
500
1
            ret = ff_inlink_consume_samples(ctx->inputs[1], s->nb_samples, s->nb_samples, &cf[1]);
501
1
            if (ret < 0) {
502
                av_frame_free(&out);
503
                return ret;
504
            }
505
506
1
            s->crossfade_samples(out->extended_data, cf[0]->extended_data,
507
1
                                 cf[1]->extended_data,
508
1
                                 s->nb_samples, out->channels,
509
                                 s->curve, s->curve2);
510
1
            out->pts = s->pts;
511
1
            s->pts += av_rescale_q(s->nb_samples,
512
1
                (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
513
1
            s->crossfade_is_over = 1;
514
1
            av_frame_free(&cf[0]);
515
1
            av_frame_free(&cf[1]);
516
1
            return ff_filter_frame(outlink, out);
517
        } else {
518
            out = ff_get_audio_buffer(outlink, s->nb_samples);
519
            if (!out)
520
                return AVERROR(ENOMEM);
521
522
            ret = ff_inlink_consume_samples(ctx->inputs[0], s->nb_samples, s->nb_samples, &cf[0]);
523
            if (ret < 0) {
524
                av_frame_free(&out);
525
                return ret;
526
            }
527
528
            s->fade_samples(out->extended_data, cf[0]->extended_data, s->nb_samples,
529
                            outlink->channels, -1, s->nb_samples - 1, s->nb_samples, s->curve);
530
            out->pts = s->pts;
531
            s->pts += av_rescale_q(s->nb_samples,
532
                (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
533
            av_frame_free(&cf[0]);
534
            ret = ff_filter_frame(outlink, out);
535
            if (ret < 0)
536
                return ret;
537
538
            out = ff_get_audio_buffer(outlink, s->nb_samples);
539
            if (!out)
540
                return AVERROR(ENOMEM);
541
542
            ret = ff_inlink_consume_samples(ctx->inputs[1], s->nb_samples, s->nb_samples, &cf[1]);
543
            if (ret < 0) {
544
                av_frame_free(&out);
545
                return ret;
546
            }
547
548
            s->fade_samples(out->extended_data, cf[1]->extended_data, s->nb_samples,
549
                            outlink->channels, 1, 0, s->nb_samples, s->curve2);
550
            out->pts = s->pts;
551
            s->pts += av_rescale_q(s->nb_samples,
552
                (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
553
            s->crossfade_is_over = 1;
554
            av_frame_free(&cf[1]);
555
            return ff_filter_frame(outlink, out);
556
        }
557
346
    } else if (ff_outlink_frame_wanted(ctx->outputs[0])) {
558

345
        if (!s->cf0_eof && ff_outlink_get_status(ctx->inputs[0])) {
559
1
            s->cf0_eof = 1;
560
        }
561
345
        if (ff_outlink_get_status(ctx->inputs[1])) {
562
            ff_outlink_set_status(ctx->outputs[0], AVERROR_EOF, AV_NOPTS_VALUE);
563
            return 0;
564
        }
565
345
        if (!s->cf0_eof)
566
259
            ff_inlink_request_frame(ctx->inputs[0]);
567
        else
568
86
            ff_inlink_request_frame(ctx->inputs[1]);
569
345
        return 0;
570
    }
571
572
1
    return ret;
573
}
574
575
1
static int acrossfade_config_output(AVFilterLink *outlink)
576
{
577
1
    AVFilterContext *ctx = outlink->src;
578
1
    AudioFadeContext *s  = ctx->priv;
579
580
1
    if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
581
        av_log(ctx, AV_LOG_ERROR,
582
               "Inputs must have the same sample rate "
583
               "%d for in0 vs %d for in1\n",
584
               ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
585
        return AVERROR(EINVAL);
586
    }
587
588
1
    outlink->sample_rate = ctx->inputs[0]->sample_rate;
589
1
    outlink->time_base   = ctx->inputs[0]->time_base;
590
1
    outlink->channel_layout = ctx->inputs[0]->channel_layout;
591
1
    outlink->channels = ctx->inputs[0]->channels;
592
593


1
    switch (outlink->format) {
594
    case AV_SAMPLE_FMT_DBL:  s->crossfade_samples = crossfade_samples_dbl;  break;
595
    case AV_SAMPLE_FMT_DBLP: s->crossfade_samples = crossfade_samples_dblp; break;
596
    case AV_SAMPLE_FMT_FLT:  s->crossfade_samples = crossfade_samples_flt;  break;
597
    case AV_SAMPLE_FMT_FLTP: s->crossfade_samples = crossfade_samples_fltp; break;
598
1
    case AV_SAMPLE_FMT_S16:  s->crossfade_samples = crossfade_samples_s16;  break;
599
    case AV_SAMPLE_FMT_S16P: s->crossfade_samples = crossfade_samples_s16p; break;
600
    case AV_SAMPLE_FMT_S32:  s->crossfade_samples = crossfade_samples_s32;  break;
601
    case AV_SAMPLE_FMT_S32P: s->crossfade_samples = crossfade_samples_s32p; break;
602
    }
603
604
1
    config_output(outlink);
605
606
1
    return 0;
607
}
608
609
static const AVFilterPad avfilter_af_acrossfade_inputs[] = {
610
    {
611
        .name         = "crossfade0",
612
        .type         = AVMEDIA_TYPE_AUDIO,
613
    },
614
    {
615
        .name         = "crossfade1",
616
        .type         = AVMEDIA_TYPE_AUDIO,
617
    },
618
    { NULL }
619
};
620
621
static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
622
    {
623
        .name          = "default",
624
        .type          = AVMEDIA_TYPE_AUDIO,
625
        .config_props  = acrossfade_config_output,
626
    },
627
    { NULL }
628
};
629
630
AVFilter ff_af_acrossfade = {
631
    .name          = "acrossfade",
632
    .description   = NULL_IF_CONFIG_SMALL("Cross fade two input audio streams."),
633
    .query_formats = query_formats,
634
    .priv_size     = sizeof(AudioFadeContext),
635
    .activate      = activate,
636
    .priv_class    = &acrossfade_class,
637
    .inputs        = avfilter_af_acrossfade_inputs,
638
    .outputs       = avfilter_af_acrossfade_outputs,
639
};
640
641
#endif /* CONFIG_ACROSSFADE_FILTER */