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
33
48
51 int64_t start, int64_t range,
int curve,
56 uint8_t * const *cf1,
58 int curve0, int curve1);
60
61 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 };
62
63 #define OFFSET(x) offsetof(AudioFadeContext, x)
64 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
65 #define TFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
66
73 };
74
75 static double fade_gain(
int curve, int64_t
index, int64_t range,
double silence,
double unity)
76 {
77 #define CUBE(a) ((a)*(a)*(a))
78 double gain;
79
81
82 switch (curve) {
84 gain = sin(gain *
M_PI / 2.0);
85 break;
87 /* 0.6... = 2 / M_PI */
88 gain = 0.6366197723675814 * asin(gain);
89 break;
91 gain = 1.0 - cos(
M_PI / 4.0 * (
CUBE(2.0*gain - 1) + 1));
92 break;
94 gain = (1.0 - cos(gain *
M_PI)) / 2.0;
95 break;
97 /* 0.3... = 1 / M_PI */
98 gain = 0.3183098861837907 * acos(1 - 2 * gain);
99 break;
101 /* -11.5... = 5*ln(0.1) */
102 gain =
exp(-11.512925464970227 * (1 - gain));
103 break;
105 gain =
av_clipd(1 + 0.2 * log10(gain), 0, 1.0);
106 break;
108 gain = 1 - sqrt(1 - gain);
109 break;
111 gain = (1 - (1 - gain) * (1 - gain));
112 break;
114 gain *= gain;
115 break;
118 break;
120 gain = sqrt(gain);
121 break;
124 break;
126 gain = gain <= 0.5 ?
cbrt(2 * gain) / 2: 1 -
cbrt(2 * (1 - gain)) / 2;
127 break;
129 gain = gain <= 0.5 ?
CUBE(2 * gain) / 2: 1 -
CUBE(2 * (1 - gain)) / 2;
130 break;
132 const double a = 1. / (1. - 0.787) - 1;
133 double A = 1. / (1.0 +
exp(0 -((gain-0.5) *
a * 2.0)));
134 double B = 1. / (1.0 +
exp(
a));
135 double C = 1. / (1.0 +
exp(0-
a));
136 gain = (
A -
B) / (
C -
B);
137 }
138 break;
140 gain = gain >= 1.0 ? 1.0 : sin(
M_PI * (1.0 - gain)) / (
M_PI * (1.0 - gain));
141 break;
143 gain = gain <= 0.0 ? 0.0 : 1.0 - sin(
M_PI * gain) / (
M_PI * gain);
144 break;
146 gain = 1.0;
147 break;
148 }
149
150 return silence + (unity - silence) * gain;
151 }
152
153 #define FADE_PLANAR(name, type) \
154 static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
155 int nb_samples, int channels, int dir, \
156 int64_t start, int64_t range,int curve,\
157 double silence, double unity) \
158 { \
159 int i, c; \
160 \
161 for (i = 0; i < nb_samples; i++) { \
162 double gain = fade_gain(curve, start + i * dir,range,silence,unity);\
163 for (c = 0; c < channels; c++) { \
164 type *d = (type *)dst[c]; \
165 const type *s = (type *)src[c]; \
166 \
167 d[i] = s[i] * gain; \
168 } \
169 } \
170 }
171
172 #define FADE(name, type) \
173 static void fade_samples_## name (uint8_t **dst, uint8_t * const *src, \
174 int nb_samples, int channels, int dir, \
175 int64_t start, int64_t range, int curve, \
176 double silence, double unity) \
177 { \
178 type *d = (type *)dst[0]; \
179 const type *s = (type *)src[0]; \
180 int i, c, k = 0; \
181 \
182 for (i = 0; i < nb_samples; i++) { \
183 double gain = fade_gain(curve, start + i * dir,range,silence,unity);\
184 for (c = 0; c < channels; c++, k++) \
185 d[k] = s[k] * gain; \
186 } \
187 }
188
193
198
199 #define SCALE_PLANAR(name, type) \
200 static void scale_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
201 int nb_samples, int channels, \
202 double gain) \
203 { \
204 int i, c; \
205 \
206 for (i = 0; i < nb_samples; i++) { \
207 for (c = 0; c < channels; c++) { \
208 type *d = (type *)dst[c]; \
209 const type *s = (type *)src[c]; \
210 \
211 d[i] = s[i] * gain; \
212 } \
213 } \
214 }
215
216 #define SCALE(name, type) \
217 static void scale_samples_## name (uint8_t **dst, uint8_t * const *src, \
218 int nb_samples, int channels, double gain)\
219 { \
220 type *d = (type *)dst[0]; \
221 const type *s = (type *)src[0]; \
222 int i, c, k = 0; \
223 \
224 for (i = 0; i < nb_samples; i++) { \
225 for (c = 0; c < channels; c++, k++) \
226 d[k] = s[k] * gain; \
227 } \
228 }
229
234
239
241 {
244
245 switch (outlink->format) {
247 s->scale_samples = scale_samples_dbl;
248 break;
250 s->scale_samples = scale_samples_dblp;
251 break;
253 s->scale_samples = scale_samples_flt;
254 break;
256 s->scale_samples = scale_samples_fltp;
257 break;
260 break;
262 s->scale_samples = scale_samples_s16p;
263 break;
266 break;
268 s->scale_samples = scale_samples_s32p;
269 break;
270 }
271
278
279 return 0;
280 }
281
282 #if CONFIG_AFADE_FILTER
283
284 static const AVOption afade_options[] = {
289 {
"start_sample",
"set number of first sample to start fading",
OFFSET(start_sample),
AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX,
TFLAGS },
322 };
323
325
327 {
329
330 if (INT64_MAX -
s->nb_samples <
s->start_sample)
332
333 return 0;
334 }
335
337 {
343
344 if (
s->unity == 1.0 &&
345 ((!
s->type && (
s->start_sample +
s->nb_samples < cur_sample)) ||
346 (
s->type && (cur_sample + nb_samples < s->start_sample))))
348
350 out_buf = buf;
351 } else {
353 if (!out_buf)
356 }
357
358 if ((!
s->type && (cur_sample + nb_samples < s->start_sample)) ||
359 (
s->type && (
s->start_sample +
s->nb_samples < cur_sample))) {
360 if (
s->silence == 0.) {
363 } else {
367 }
368 }
else if ((
s->type && (cur_sample + nb_samples < s->start_sample)) ||
369 (!
s->type && (
s->start_sample +
s->nb_samples < cur_sample))) {
373 } else {
374 int64_t start;
375
377 start = cur_sample -
s->start_sample;
378 else
379 start =
s->start_sample +
s->nb_samples - cur_sample;
380
383 s->type ? -1 : 1, start,
384 s->nb_samples,
s->curve,
s->silence,
s->unity);
385 }
386
387 if (buf != out_buf)
389
391 }
392
394 char *res,
int res_len,
int flags)
395 {
397
401
403 }
404
405 static const AVFilterPad avfilter_af_afade_inputs[] = {
406 {
410 },
411 };
412
413 static const AVFilterPad avfilter_af_afade_outputs[] = {
414 {
418 },
419 };
420
429 .priv_class = &afade_class,
432 };
433
434 #endif /* CONFIG_AFADE_FILTER */
435
436 #if CONFIG_ACROSSFADE_FILTER
437
438 static const AVOption acrossfade_options[] = {
439 {
"nb_samples",
"set number of samples for cross fade duration",
OFFSET(nb_samples),
AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10,
FLAGS },
440 {
"ns",
"set number of samples for cross fade duration",
OFFSET(nb_samples),
AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10,
FLAGS },
470 };
471
473
474 #define CROSSFADE_PLANAR(name, type) \
475 static void crossfade_samples_## name ##p(uint8_t **dst, uint8_t * const *cf0, \
476 uint8_t * const *cf1, \
477 int nb_samples, int channels, \
478 int curve0, int curve1) \
479 { \
480 int i, c; \
481 \
482 for (i = 0; i < nb_samples; i++) { \
483 double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples,0.,1.);\
484 double gain1 = fade_gain(curve1, i, nb_samples, 0., 1.); \
485 for (c = 0; c < channels; c++) { \
486 type *d = (type *)dst[c]; \
487 const type *s0 = (type *)cf0[c]; \
488 const type *s1 = (type *)cf1[c]; \
489 \
490 d[i] = s0[i] * gain0 + s1[i] * gain1; \
491 } \
492 } \
493 }
494
495 #define CROSSFADE(name, type) \
496 static void crossfade_samples_## name (uint8_t **dst, uint8_t * const *cf0, \
497 uint8_t * const *cf1, \
498 int nb_samples, int channels, \
499 int curve0, int curve1) \
500 { \
501 type *d = (type *)dst[0]; \
502 const type *s0 = (type *)cf0[0]; \
503 const type *s1 = (type *)cf1[0]; \
504 int i, c, k = 0; \
505 \
506 for (i = 0; i < nb_samples; i++) { \
507 double gain0 = fade_gain(curve0, nb_samples - 1-i,nb_samples,0.,1.);\
508 double gain1 = fade_gain(curve1, i, nb_samples, 0., 1.); \
509 for (c = 0; c < channels; c++, k++) \
510 d[k] = s0[k] * gain0 + s1[k] * gain1; \
511 } \
512 }
513
514 CROSSFADE_PLANAR(dbl, double)
515 CROSSFADE_PLANAR(flt, float)
516 CROSSFADE_PLANAR(s16, int16_t)
518
519 CROSSFADE(dbl, double)
520 CROSSFADE(flt, float)
521 CROSSFADE(s16, int16_t)
523
525 {
531
533
534 if (
s->crossfade_is_over) {
541 }
else if (
ret < 0) {
545 return 0;
549 return 0;
550 }
551 }
552 }
553
555 if (nb_samples >
s->nb_samples) {
556 nb_samples -=
s->nb_samples;
564 }
else if (
s->cf0_eof && nb_samples >=
s->nb_samples &&
570
575 }
576
581 }
582
583 s->crossfade_samples(
out->extended_data, cf[0]->extended_data,
584 cf[1]->extended_data,
585 s->nb_samples,
out->ch_layout.nb_channels,
586 s->curve,
s->curve2);
590 s->crossfade_is_over = 1;
594 } else {
598
603 }
604
605 s->fade_samples(
out->extended_data, cf[0]->extended_data,
s->nb_samples,
614
618
623 }
624
625 s->fade_samples(
out->extended_data, cf[1]->extended_data,
s->nb_samples,
630 s->crossfade_is_over = 1;
633 }
637 }
640 return 0;
641 }
644 else
646 return 0;
647 }
648
650 }
651
652 static int acrossfade_config_output(
AVFilterLink *outlink)
653 {
656
658
659 switch (outlink->
format) {
668 }
669
671
672 return 0;
673 }
674
675 static const AVFilterPad avfilter_af_acrossfade_inputs[] = {
676 {
677 .
name =
"crossfade0",
679 },
680 {
681 .name = "crossfade1",
683 },
684 };
685
686 static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
687 {
690 .config_props = acrossfade_config_output,
691 },
692 };
693
695 .
name =
"acrossfade",
699 .priv_class = &acrossfade_class,
703 };
704
705 #endif /* CONFIG_ACROSSFADE_FILTER */