26 #include "config_components.h"
51 int64_t start, int64_t range,
int curve,
58 int curve0,
int curve1);
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 };
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
75 static double fade_gain(
int curve, int64_t
index, int64_t range,
double silence,
double unity)
77 #define CUBE(a) ((a)*(a)*(a))
84 gain = sin(gain *
M_PI / 2.0);
88 gain = 0.6366197723675814 * asin(gain);
91 gain = 1.0 - cos(
M_PI / 4.0 * (
CUBE(2.0*gain - 1) + 1));
94 gain = (1.0 - cos(gain *
M_PI)) / 2.0;
98 gain = 0.3183098861837907 * acos(1 - 2 * gain);
102 gain =
exp(-11.512925464970227 * (1 - gain));
105 gain =
av_clipd(1 + 0.2 * log10(gain), 0, 1.0);
108 gain = 1 - sqrt(1 - gain);
111 gain = (1 - (1 - gain) * (1 - gain));
126 gain = gain <= 0.5 ?
cbrt(2 * gain) / 2: 1 -
cbrt(2 * (1 - gain)) / 2;
129 gain = gain <= 0.5 ?
CUBE(2 * gain) / 2: 1 -
CUBE(2 * (1 - gain)) / 2;
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);
140 gain = gain >= 1.0 ? 1.0 : sin(
M_PI * (1.0 - gain)) / (
M_PI * (1.0 - gain));
143 gain = gain <= 0.0 ? 0.0 : 1.0 - sin(
M_PI * gain) / (
M_PI * gain);
150 return silence + (unity - silence) * gain;
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) \
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]; \
167 d[i] = s[i] * gain; \
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) \
178 type *d = (type *)dst[0]; \
179 const type *s = (type *)src[0]; \
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; \
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, \
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]; \
211 d[i] = s[i] * gain; \
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)\
220 type *d = (type *)dst[0]; \
221 const type *s = (type *)src[0]; \
224 for (i = 0; i < nb_samples; i++) { \
225 for (c = 0; c < channels; c++, k++) \
226 d[k] = s[k] * gain; \
245 switch (outlink->format) {
247 s->scale_samples = scale_samples_dbl;
250 s->scale_samples = scale_samples_dblp;
253 s->scale_samples = scale_samples_flt;
256 s->scale_samples = scale_samples_fltp;
262 s->scale_samples = scale_samples_s16p;
268 s->scale_samples = scale_samples_s32p;
282 #if CONFIG_AFADE_FILTER
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 },
330 if (INT64_MAX -
s->nb_samples <
s->start_sample)
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))))
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.) {
368 }
else if ((
s->type && (cur_sample + nb_samples < s->start_sample)) ||
369 (!
s->type && (
s->start_sample +
s->nb_samples < cur_sample))) {
377 start = cur_sample -
s->start_sample;
379 start =
s->start_sample +
s->nb_samples - cur_sample;
383 s->type ? -1 : 1, start,
384 s->nb_samples,
s->curve,
s->silence,
s->unity);
394 char *res,
int res_len,
int flags)
405 static const AVFilterPad avfilter_af_afade_inputs[] = {
413 static const AVFilterPad avfilter_af_afade_outputs[] = {
429 .priv_class = &afade_class,
436 #if CONFIG_ACROSSFADE_FILTER
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 },
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) \
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]; \
490 d[i] = s0[i] * gain0 + s1[i] * gain1; \
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) \
501 type *d = (type *)dst[0]; \
502 const type *s0 = (type *)cf0[0]; \
503 const type *s1 = (type *)cf1[0]; \
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; \
514 CROSSFADE_PLANAR(dbl,
double)
515 CROSSFADE_PLANAR(flt,
float)
516 CROSSFADE_PLANAR(s16, int16_t)
519 CROSSFADE(dbl,
double)
520 CROSSFADE(flt,
float)
521 CROSSFADE(s16, int16_t)
534 if (
s->crossfade_is_over) {
541 }
else if (
ret < 0) {
555 if (nb_samples >
s->nb_samples) {
556 nb_samples -=
s->nb_samples;
564 }
else if (
s->cf0_eof && nb_samples >=
s->nb_samples &&
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;
605 s->fade_samples(
out->extended_data, cf[0]->extended_data,
s->nb_samples,
625 s->fade_samples(
out->extended_data, cf[1]->extended_data,
s->nb_samples,
630 s->crossfade_is_over = 1;
652 static int acrossfade_config_output(
AVFilterLink *outlink)
659 switch (outlink->
format) {
675 static const AVFilterPad avfilter_af_acrossfade_inputs[] = {
677 .
name =
"crossfade0",
681 .name =
"crossfade1",
686 static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
690 .config_props = acrossfade_config_output,
695 .
name =
"acrossfade",
699 .priv_class = &acrossfade_class,