26 #include "config_components.h"
49 int64_t start, int64_t range,
int curve);
53 int curve0,
int curve1);
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 };
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
72 #define CUBE(a) ((a)*(a)*(a))
79 gain = sin(gain *
M_PI / 2.0);
83 gain = 0.6366197723675814 * asin(gain);
86 gain = 1.0 - cos(
M_PI / 4.0 * (
CUBE(2.0*gain - 1) + 1));
89 gain = (1.0 - cos(gain *
M_PI)) / 2.0;
93 gain = 0.3183098861837907 * acos(1 - 2 * gain);
97 gain =
exp(-11.512925464970227 * (1 - gain));
100 gain =
av_clipd(1 + 0.2 * log10(gain), 0, 1.0);
103 gain = 1 - sqrt(1 - gain);
106 gain = (1 - (1 - gain) * (1 - gain));
121 gain = gain <= 0.5 ?
cbrt(2 * gain) / 2: 1 -
cbrt(2 * (1 - gain)) / 2;
124 gain = gain <= 0.5 ?
CUBE(2 * gain) / 2: 1 -
CUBE(2 * (1 - gain)) / 2;
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);
135 gain = gain >= 1.0 ? 1.0 : sin(
M_PI * (1.0 - gain)) / (
M_PI * (1.0 - gain));
138 gain = gain <= 0.0 ? 0.0 : 1.0 - sin(
M_PI * gain) / (
M_PI * gain);
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) \
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]; \
161 d[i] = s[i] * gain; \
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) \
171 type *d = (type *)dst[0]; \
172 const type *s = (type *)src[0]; \
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; \
197 switch (outlink->format) {
218 #if CONFIG_AFADE_FILTER
220 static const AVOption afade_options[] = {
225 {
"start_sample",
"set number of first sample to start fading",
OFFSET(start_sample),
AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX,
TFLAGS },
264 if (INT64_MAX -
s->nb_samples <
s->start_sample)
278 if ((!
s->type && (
s->start_sample +
s->nb_samples < cur_sample)) ||
279 (
s->type && (cur_sample + nb_samples < s->start_sample)))
291 if ((!
s->type && (cur_sample + nb_samples < s->start_sample)) ||
292 (
s->type && (
s->start_sample +
s->nb_samples < cur_sample))) {
299 start = cur_sample -
s->start_sample;
301 start =
s->start_sample +
s->nb_samples - cur_sample;
305 s->type ? -1 : 1, start,
306 s->nb_samples,
s->curve);
316 char *res,
int res_len,
int flags)
327 static const AVFilterPad avfilter_af_afade_inputs[] = {
335 static const AVFilterPad avfilter_af_afade_outputs[] = {
351 .priv_class = &afade_class,
358 #if CONFIG_ACROSSFADE_FILTER
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 },
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) \
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]; \
412 d[i] = s0[i] * gain0 + s1[i] * gain1; \
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) \
423 type *d = (type *)dst[0]; \
424 const type *s0 = (type *)cf0[0]; \
425 const type *s1 = (type *)cf1[0]; \
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; \
436 CROSSFADE_PLANAR(dbl,
double)
437 CROSSFADE_PLANAR(flt,
float)
438 CROSSFADE_PLANAR(s16, int16_t)
441 CROSSFADE(dbl,
double)
442 CROSSFADE(flt,
float)
443 CROSSFADE(s16, int16_t)
456 if (
s->crossfade_is_over) {
463 }
else if (
ret < 0) {
477 if (nb_samples >
s->nb_samples) {
478 nb_samples -=
s->nb_samples;
486 }
else if (
s->cf0_eof && nb_samples >=
s->nb_samples &&
505 s->crossfade_samples(
out->extended_data, cf[0]->extended_data,
506 cf[1]->extended_data,
507 s->nb_samples,
out->ch_layout.nb_channels,
508 s->curve,
s->curve2);
512 s->crossfade_is_over = 1;
527 s->fade_samples(
out->extended_data, cf[0]->extended_data,
s->nb_samples,
547 s->fade_samples(
out->extended_data, cf[1]->extended_data,
s->nb_samples,
552 s->crossfade_is_over = 1;
574 static int acrossfade_config_output(
AVFilterLink *outlink)
581 switch (outlink->
format) {
597 static const AVFilterPad avfilter_af_acrossfade_inputs[] = {
599 .
name =
"crossfade0",
603 .name =
"crossfade1",
608 static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
612 .config_props = acrossfade_config_output,
617 .
name =
"acrossfade",
621 .priv_class = &acrossfade_class,