FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_afade.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 124 249 49.8%
Branches: 64 198 32.3%

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 "config_components.h"
27
28 #include "libavutil/opt.h"
29 #include "audio.h"
30 #include "avfilter.h"
31 #include "filters.h"
32 #include "internal.h"
33
34 typedef struct AudioFadeContext {
35 const AVClass *class;
36 int type;
37 int curve, curve2;
38 int64_t nb_samples;
39 int64_t start_sample;
40 int64_t duration;
41 int64_t start_time;
42 int overlap;
43 int cf0_eof;
44 int crossfade_is_over;
45 int64_t pts;
46
47 void (*fade_samples)(uint8_t **dst, uint8_t * const *src,
48 int nb_samples, int channels, int direction,
49 int64_t start, int64_t range, int curve);
50 void (*crossfade_samples)(uint8_t **dst, uint8_t * const *cf0,
51 uint8_t * const *cf1,
52 int nb_samples, int channels,
53 int curve0, int curve1);
54 } AudioFadeContext;
55
56 enum CurveType { NONE = -1, TRI, QSIN, ESIN, HSIN, LOG, IPAR, QUA, CUB, SQU, CBR, PAR, EXP, IQSIN, IHSIN, DESE, DESI, LOSI, SINC, ISINC, NB_CURVES };
57
58 #define OFFSET(x) offsetof(AudioFadeContext, x)
59 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
60 #define TFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
61
62 static const enum AVSampleFormat sample_fmts[] = {
63 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
64 AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
65 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
66 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
67 AV_SAMPLE_FMT_NONE
68 };
69
70 732432 static double fade_gain(int curve, int64_t index, int64_t range)
71 {
72 #define CUBE(a) ((a)*(a)*(a))
73 double gain;
74
75 732432 gain = av_clipd(1.0 * index / range, 0, 1.0);
76
77
6/20
✓ Branch 0 taken 89088 times.
✓ Branch 1 taken 89088 times.
✓ Branch 2 taken 89088 times.
✓ Branch 3 taken 89088 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 177288 times.
✓ Branch 6 taken 198792 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
732432 switch (curve) {
78 89088 case QSIN:
79 89088 gain = sin(gain * M_PI / 2.0);
80 89088 break;
81 89088 case IQSIN:
82 /* 0.6... = 2 / M_PI */
83 89088 gain = 0.6366197723675814 * asin(gain);
84 89088 break;
85 89088 case ESIN:
86 89088 gain = 1.0 - cos(M_PI / 4.0 * (CUBE(2.0*gain - 1) + 1));
87 89088 break;
88 89088 case HSIN:
89 89088 gain = (1.0 - cos(gain * M_PI)) / 2.0;
90 89088 break;
91 case IHSIN:
92 /* 0.3... = 1 / M_PI */
93 gain = 0.3183098861837907 * acos(1 - 2 * gain);
94 break;
95 177288 case EXP:
96 /* -11.5... = 5*ln(0.1) */
97 177288 gain = exp(-11.512925464970227 * (1 - gain));
98 177288 break;
99 198792 case LOG:
100 198792 gain = av_clipd(1 + 0.2 * log10(gain), 0, 1.0);
101 198792 break;
102 case PAR:
103 gain = 1 - sqrt(1 - gain);
104 break;
105 case IPAR:
106 gain = (1 - (1 - gain) * (1 - gain));
107 break;
108 case QUA:
109 gain *= gain;
110 break;
111 case CUB:
112 gain = CUBE(gain);
113 break;
114 case SQU:
115 gain = sqrt(gain);
116 break;
117 case CBR:
118 gain = cbrt(gain);
119 break;
120 case DESE:
121 gain = gain <= 0.5 ? cbrt(2 * gain) / 2: 1 - cbrt(2 * (1 - gain)) / 2;
122 break;
123 case DESI:
124 gain = gain <= 0.5 ? CUBE(2 * gain) / 2: 1 - CUBE(2 * (1 - gain)) / 2;
125 break;
126 case LOSI: {
127 const double a = 1. / (1. - 0.787) - 1;
128 double A = 1. / (1.0 + exp(0 -((gain-0.5) * a * 2.0)));
129 double B = 1. / (1.0 + exp(a));
130 double C = 1. / (1.0 + exp(0-a));
131 gain = (A - B) / (C - B);
132 }
133 break;
134 case SINC:
135 gain = gain >= 1.0 ? 1.0 : sin(M_PI * (1.0 - gain)) / (M_PI * (1.0 - gain));
136 break;
137 case ISINC:
138 gain = gain <= 0.0 ? 0.0 : 1.0 - sin(M_PI * gain) / (M_PI * gain);
139 break;
140 case NONE:
141 gain = 1.0;
142 break;
143 }
144
145 732432 return gain;
146 }
147
148 #define FADE_PLANAR(name, type) \
149 static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
150 int nb_samples, int channels, int dir, \
151 int64_t start, int64_t range, int curve) \
152 { \
153 int i, c; \
154 \
155 for (i = 0; i < nb_samples; i++) { \
156 double gain = fade_gain(curve, start + i * dir, range); \
157 for (c = 0; c < channels; c++) { \
158 type *d = (type *)dst[c]; \
159 const type *s = (type *)src[c]; \
160 \
161 d[i] = s[i] * gain; \
162 } \
163 } \
164 }
165
166 #define FADE(name, type) \
167 static void fade_samples_## name (uint8_t **dst, uint8_t * const *src, \
168 int nb_samples, int channels, int dir, \
169 int64_t start, int64_t range, int curve) \
170 { \
171 type *d = (type *)dst[0]; \
172 const type *s = (type *)src[0]; \
173 int i, c, k = 0; \
174 \
175 for (i = 0; i < nb_samples; i++) { \
176 double gain = fade_gain(curve, start + i * dir, range); \
177 for (c = 0; c < channels; c++, k++) \
178 d[k] = s[k] * gain; \
179 } \
180 }
181
182 FADE_PLANAR(dbl, double)
183 FADE_PLANAR(flt, float)
184 FADE_PLANAR(s16, int16_t)
185 FADE_PLANAR(s32, int32_t)
186
187 FADE(dbl, double)
188 FADE(flt, float)
189
4/4
✓ Branch 1 taken 1112064 times.
✓ Branch 2 taken 556032 times.
✓ Branch 3 taken 556032 times.
✓ Branch 4 taken 543 times.
1668639 FADE(s16, int16_t)
190 FADE(s32, int32_t)
191
192 7 static int config_output(AVFilterLink *outlink)
193 {
194 7 AVFilterContext *ctx = outlink->src;
195 7 AudioFadeContext *s = ctx->priv;
196
197
1/9
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
7 switch (outlink->format) {
198 case AV_SAMPLE_FMT_DBL: s->fade_samples = fade_samples_dbl; break;
199 case AV_SAMPLE_FMT_DBLP: s->fade_samples = fade_samples_dblp; break;
200 case AV_SAMPLE_FMT_FLT: s->fade_samples = fade_samples_flt; break;
201 case AV_SAMPLE_FMT_FLTP: s->fade_samples = fade_samples_fltp; break;
202 7 case AV_SAMPLE_FMT_S16: s->fade_samples = fade_samples_s16; break;
203 case AV_SAMPLE_FMT_S16P: s->fade_samples = fade_samples_s16p; break;
204 case AV_SAMPLE_FMT_S32: s->fade_samples = fade_samples_s32; break;
205 case AV_SAMPLE_FMT_S32P: s->fade_samples = fade_samples_s32p; break;
206 }
207
208
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (s->duration)
209 7 s->nb_samples = av_rescale(s->duration, outlink->sample_rate, AV_TIME_BASE);
210 7 s->duration = 0;
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (s->start_time)
212 s->start_sample = av_rescale(s->start_time, outlink->sample_rate, AV_TIME_BASE);
213 7 s->start_time = 0;
214
215 7 return 0;
216 }
217
218 #if CONFIG_AFADE_FILTER
219
220 static const AVOption afade_options[] = {
221 { "type", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, TFLAGS, "type" },
222 { "t", "set the fade direction", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, TFLAGS, "type" },
223 { "in", "fade-in", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, TFLAGS, "type" },
224 { "out", "fade-out", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, TFLAGS, "type" },
225 { "start_sample", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, TFLAGS },
226 { "ss", "set number of first sample to start fading", OFFSET(start_sample), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, TFLAGS },
227 { "nb_samples", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT64, {.i64 = 44100}, 1, INT64_MAX, TFLAGS },
228 { "ns", "set number of samples for fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT64, {.i64 = 44100}, 1, INT64_MAX, TFLAGS },
229 { "start_time", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0 }, 0, INT64_MAX, TFLAGS },
230 { "st", "set time to start fading", OFFSET(start_time), AV_OPT_TYPE_DURATION, {.i64 = 0 }, 0, INT64_MAX, TFLAGS },
231 { "duration", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0 }, 0, INT64_MAX, TFLAGS },
232 { "d", "set fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0 }, 0, INT64_MAX, TFLAGS },
233 { "curve", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, TFLAGS, "curve" },
234 { "c", "set fade curve type", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, TFLAGS, "curve" },
235 { "nofade", "no fade; keep audio as-is", 0, AV_OPT_TYPE_CONST, {.i64 = NONE }, 0, 0, TFLAGS, "curve" },
236 { "tri", "linear slope", 0, AV_OPT_TYPE_CONST, {.i64 = TRI }, 0, 0, TFLAGS, "curve" },
237 { "qsin", "quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = QSIN }, 0, 0, TFLAGS, "curve" },
238 { "esin", "exponential sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = ESIN }, 0, 0, TFLAGS, "curve" },
239 { "hsin", "half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = HSIN }, 0, 0, TFLAGS, "curve" },
240 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64 = LOG }, 0, 0, TFLAGS, "curve" },
241 { "ipar", "inverted parabola", 0, AV_OPT_TYPE_CONST, {.i64 = IPAR }, 0, 0, TFLAGS, "curve" },
242 { "qua", "quadratic", 0, AV_OPT_TYPE_CONST, {.i64 = QUA }, 0, 0, TFLAGS, "curve" },
243 { "cub", "cubic", 0, AV_OPT_TYPE_CONST, {.i64 = CUB }, 0, 0, TFLAGS, "curve" },
244 { "squ", "square root", 0, AV_OPT_TYPE_CONST, {.i64 = SQU }, 0, 0, TFLAGS, "curve" },
245 { "cbr", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64 = CBR }, 0, 0, TFLAGS, "curve" },
246 { "par", "parabola", 0, AV_OPT_TYPE_CONST, {.i64 = PAR }, 0, 0, TFLAGS, "curve" },
247 { "exp", "exponential", 0, AV_OPT_TYPE_CONST, {.i64 = EXP }, 0, 0, TFLAGS, "curve" },
248 { "iqsin", "inverted quarter of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IQSIN}, 0, 0, TFLAGS, "curve" },
249 { "ihsin", "inverted half of sine wave", 0, AV_OPT_TYPE_CONST, {.i64 = IHSIN}, 0, 0, TFLAGS, "curve" },
250 { "dese", "double-exponential seat", 0, AV_OPT_TYPE_CONST, {.i64 = DESE }, 0, 0, TFLAGS, "curve" },
251 { "desi", "double-exponential sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = DESI }, 0, 0, TFLAGS, "curve" },
252 { "losi", "logistic sigmoid", 0, AV_OPT_TYPE_CONST, {.i64 = LOSI }, 0, 0, TFLAGS, "curve" },
253 { "sinc", "sine cardinal function", 0, AV_OPT_TYPE_CONST, {.i64 = SINC }, 0, 0, TFLAGS, "curve" },
254 { "isinc", "inverted sine cardinal function", 0, AV_OPT_TYPE_CONST, {.i64 = ISINC}, 0, 0, TFLAGS, "curve" },
255 { NULL }
256 };
257
258 AVFILTER_DEFINE_CLASS(afade);
259
260 6 static av_cold int init(AVFilterContext *ctx)
261 {
262 6 AudioFadeContext *s = ctx->priv;
263
264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (INT64_MAX - s->nb_samples < s->start_sample)
265 return AVERROR(EINVAL);
266
267 6 return 0;
268 }
269
270 1554 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
271 {
272 1554 AudioFadeContext *s = inlink->dst->priv;
273 1554 AVFilterLink *outlink = inlink->dst->outputs[0];
274 1554 int nb_samples = buf->nb_samples;
275 AVFrame *out_buf;
276 1554 int64_t cur_sample = av_rescale_q(buf->pts, inlink->time_base, (AVRational){1, inlink->sample_rate});
277
278
3/4
✓ Branch 0 taken 1554 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 543 times.
✓ Branch 3 taken 1011 times.
1554 if ((!s->type && (s->start_sample + s->nb_samples < cur_sample)) ||
279
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 543 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
543 ( s->type && (cur_sample + nb_samples < s->start_sample)))
280 1011 return ff_filter_frame(outlink, buf);
281
282
1/2
✓ Branch 1 taken 543 times.
✗ Branch 2 not taken.
543 if (av_frame_is_writable(buf)) {
283 543 out_buf = buf;
284 } else {
285 out_buf = ff_get_audio_buffer(outlink, nb_samples);
286 if (!out_buf)
287 return AVERROR(ENOMEM);
288 av_frame_copy_props(out_buf, buf);
289 }
290
291
2/4
✓ Branch 0 taken 543 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 543 times.
✗ Branch 3 not taken.
543 if ((!s->type && (cur_sample + nb_samples < s->start_sample)) ||
292
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 543 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
543 ( s->type && (s->start_sample + s->nb_samples < cur_sample))) {
293 av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
294 out_buf->ch_layout.nb_channels, out_buf->format);
295 } else {
296 int64_t start;
297
298
1/2
✓ Branch 0 taken 543 times.
✗ Branch 1 not taken.
543 if (!s->type)
299 543 start = cur_sample - s->start_sample;
300 else
301 start = s->start_sample + s->nb_samples - cur_sample;
302
303 543 s->fade_samples(out_buf->extended_data, buf->extended_data,
304 543 nb_samples, buf->ch_layout.nb_channels,
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 543 times.
543 s->type ? -1 : 1, start,
306 s->nb_samples, s->curve);
307 }
308
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 543 times.
543 if (buf != out_buf)
310 av_frame_free(&buf);
311
312 543 return ff_filter_frame(outlink, out_buf);
313 }
314
315 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
316 char *res, int res_len, int flags)
317 {
318 int ret;
319
320 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
321 if (ret < 0)
322 return ret;
323
324 return config_output(ctx->outputs[0]);
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 };
334
335 static const AVFilterPad avfilter_af_afade_outputs[] = {
336 {
337 .name = "default",
338 .type = AVMEDIA_TYPE_AUDIO,
339 .config_props = config_output,
340 },
341 };
342
343 const AVFilter ff_af_afade = {
344 .name = "afade",
345 .description = NULL_IF_CONFIG_SMALL("Fade in/out input audio."),
346 .priv_size = sizeof(AudioFadeContext),
347 .init = init,
348 FILTER_INPUTS(avfilter_af_afade_inputs),
349 FILTER_OUTPUTS(avfilter_af_afade_outputs),
350 FILTER_SAMPLEFMTS_ARRAY(sample_fmts),
351 .priv_class = &afade_class,
352 .process_command = process_command,
353 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
354 };
355
356 #endif /* CONFIG_AFADE_FILTER */
357
358 #if CONFIG_ACROSSFADE_FILTER
359
360 static const AVOption acrossfade_options[] = {
361 { "nb_samples", "set number of samples for cross fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
362 { "ns", "set number of samples for cross fade duration", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10, FLAGS },
363 { "duration", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0 }, 0, 60000000, FLAGS },
364 { "d", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0 }, 0, 60000000, FLAGS },
365 { "overlap", "overlap 1st stream end with 2nd stream start", OFFSET(overlap), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, FLAGS },
366 { "o", "overlap 1st stream end with 2nd stream start", OFFSET(overlap), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, FLAGS },
367 { "curve1", "set fade curve type for 1st stream", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
368 { "c1", "set fade curve type for 1st stream", OFFSET(curve), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
369 { "nofade", "no fade; keep audio as-is", 0, AV_OPT_TYPE_CONST, {.i64 = NONE }, 0, 0, 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 { "sinc", "sine cardinal function", 0, AV_OPT_TYPE_CONST, {.i64 = SINC }, 0, 0, FLAGS, "curve" },
388 { "isinc", "inverted sine cardinal function", 0, AV_OPT_TYPE_CONST, {.i64 = ISINC}, 0, 0, FLAGS, "curve" },
389 { "curve2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, NB_CURVES - 1, FLAGS, "curve" },
390 { "c2", "set fade curve type for 2nd stream", OFFSET(curve2), AV_OPT_TYPE_INT, {.i64 = TRI }, NONE, 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
4/4
✓ Branch 2 taken 176400 times.
✓ Branch 3 taken 88200 times.
✓ Branch 4 taken 88200 times.
✓ Branch 5 taken 1 times.
264601 CROSSFADE(s16, int16_t)
444 CROSSFADE(s32, int32_t)
445
446 1170 static int activate(AVFilterContext *ctx)
447 {
448 1170 AudioFadeContext *s = ctx->priv;
449 1170 AVFilterLink *outlink = ctx->outputs[0];
450 1170 AVFrame *in = NULL, *out, *cf[2] = { NULL };
451 1170 int ret = 0, nb_samples, status;
452 int64_t pts;
453
454
4/4
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1169 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1 times.
1172 FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
455
456
2/2
✓ Branch 0 taken 649 times.
✓ Branch 1 taken 520 times.
1169 if (s->crossfade_is_over) {
457 649 ret = ff_inlink_consume_frame(ctx->inputs[1], &in);
458
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 325 times.
649 if (ret > 0) {
459 324 in->pts = s->pts;
460 324 s->pts += av_rescale_q(in->nb_samples,
461 324 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
462 324 return ff_filter_frame(outlink, in);
463
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 325 times.
325 } else if (ret < 0) {
464 return ret;
465
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 324 times.
325 } else if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
466 1 ff_outlink_set_status(ctx->outputs[0], status, pts);
467 1 return 0;
468
1/2
✓ Branch 0 taken 324 times.
✗ Branch 1 not taken.
324 } else if (!ret) {
469
1/2
✓ Branch 1 taken 324 times.
✗ Branch 2 not taken.
324 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
470 324 ff_inlink_request_frame(ctx->inputs[1]);
471 324 return 0;
472 }
473 }
474 }
475
476 520 nb_samples = ff_inlink_queued_samples(ctx->inputs[0]);
477
2/2
✓ Branch 0 taken 173 times.
✓ Branch 1 taken 347 times.
520 if (nb_samples > s->nb_samples) {
478 173 nb_samples -= s->nb_samples;
479 173 ret = ff_inlink_consume_samples(ctx->inputs[0], nb_samples, nb_samples, &in);
480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 173 times.
173 if (ret < 0)
481 return ret;
482 173 in->pts = s->pts;
483 173 s->pts += av_rescale_q(in->nb_samples,
484 173 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
485 173 return ff_filter_frame(outlink, in);
486
3/4
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 261 times.
✓ Branch 2 taken 86 times.
✗ Branch 3 not taken.
347 } else if (s->cf0_eof && nb_samples >= s->nb_samples &&
487
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 85 times.
86 ff_inlink_queued_samples(ctx->inputs[1]) >= s->nb_samples) {
488
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (s->overlap) {
489 1 out = ff_get_audio_buffer(outlink, s->nb_samples);
490
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!out)
491 return AVERROR(ENOMEM);
492
493 1 ret = ff_inlink_consume_samples(ctx->inputs[0], s->nb_samples, s->nb_samples, &cf[0]);
494
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
495 av_frame_free(&out);
496 return ret;
497 }
498
499 1 ret = ff_inlink_consume_samples(ctx->inputs[1], s->nb_samples, s->nb_samples, &cf[1]);
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
501 av_frame_free(&out);
502 return ret;
503 }
504
505 1 s->crossfade_samples(out->extended_data, cf[0]->extended_data,
506 1 cf[1]->extended_data,
507 1 s->nb_samples, out->ch_layout.nb_channels,
508 s->curve, s->curve2);
509 1 out->pts = s->pts;
510 1 s->pts += av_rescale_q(s->nb_samples,
511 1 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
512 1 s->crossfade_is_over = 1;
513 1 av_frame_free(&cf[0]);
514 1 av_frame_free(&cf[1]);
515 1 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->ch_layout.nb_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->ch_layout.nb_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
2/2
✓ Branch 1 taken 345 times.
✓ Branch 2 taken 1 times.
346 } else if (ff_outlink_frame_wanted(ctx->outputs[0])) {
557
4/4
✓ Branch 0 taken 260 times.
✓ Branch 1 taken 85 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 259 times.
345 if (!s->cf0_eof && ff_outlink_get_status(ctx->inputs[0])) {
558 1 s->cf0_eof = 1;
559 }
560
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 345 times.
345 if (ff_outlink_get_status(ctx->inputs[1])) {
561 ff_outlink_set_status(ctx->outputs[0], AVERROR_EOF, AV_NOPTS_VALUE);
562 return 0;
563 }
564
2/2
✓ Branch 0 taken 259 times.
✓ Branch 1 taken 86 times.
345 if (!s->cf0_eof)
565 259 ff_inlink_request_frame(ctx->inputs[0]);
566 else
567 86 ff_inlink_request_frame(ctx->inputs[1]);
568 345 return 0;
569 }
570
571 1 return ret;
572 }
573
574 1 static int acrossfade_config_output(AVFilterLink *outlink)
575 {
576 1 AVFilterContext *ctx = outlink->src;
577 1 AudioFadeContext *s = ctx->priv;
578
579 1 outlink->time_base = ctx->inputs[0]->time_base;
580
581
1/9
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
1 switch (outlink->format) {
582 case AV_SAMPLE_FMT_DBL: s->crossfade_samples = crossfade_samples_dbl; break;
583 case AV_SAMPLE_FMT_DBLP: s->crossfade_samples = crossfade_samples_dblp; break;
584 case AV_SAMPLE_FMT_FLT: s->crossfade_samples = crossfade_samples_flt; break;
585 case AV_SAMPLE_FMT_FLTP: s->crossfade_samples = crossfade_samples_fltp; break;
586 1 case AV_SAMPLE_FMT_S16: s->crossfade_samples = crossfade_samples_s16; break;
587 case AV_SAMPLE_FMT_S16P: s->crossfade_samples = crossfade_samples_s16p; break;
588 case AV_SAMPLE_FMT_S32: s->crossfade_samples = crossfade_samples_s32; break;
589 case AV_SAMPLE_FMT_S32P: s->crossfade_samples = crossfade_samples_s32p; break;
590 }
591
592 1 config_output(outlink);
593
594 1 return 0;
595 }
596
597 static const AVFilterPad avfilter_af_acrossfade_inputs[] = {
598 {
599 .name = "crossfade0",
600 .type = AVMEDIA_TYPE_AUDIO,
601 },
602 {
603 .name = "crossfade1",
604 .type = AVMEDIA_TYPE_AUDIO,
605 },
606 };
607
608 static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
609 {
610 .name = "default",
611 .type = AVMEDIA_TYPE_AUDIO,
612 .config_props = acrossfade_config_output,
613 },
614 };
615
616 const AVFilter ff_af_acrossfade = {
617 .name = "acrossfade",
618 .description = NULL_IF_CONFIG_SMALL("Cross fade two input audio streams."),
619 .priv_size = sizeof(AudioFadeContext),
620 .activate = activate,
621 .priv_class = &acrossfade_class,
622 FILTER_INPUTS(avfilter_af_acrossfade_inputs),
623 FILTER_OUTPUTS(avfilter_af_acrossfade_outputs),
624 FILTER_SAMPLEFMTS_ARRAY(sample_fmts),
625 };
626
627 #endif /* CONFIG_ACROSSFADE_FILTER */
628