26#include "config_components.h"
59 int curve0,
int curve1);
62enum CurveType {
NONE = -1,
TRI,
QSIN,
ESIN,
HSIN,
LOG,
IPAR,
QUA,
CUB,
SQU,
CBR,
PAR,
EXP,
IQSIN,
IHSIN,
DESE,
DESI,
LOSI,
SINC,
ISINC,
QUAT,
QUATR,
QSIN2,
HSIN2,
NB_CURVES };
64#define OFFSET(x) offsetof(AudioFadeContext, x)
65#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
66#define TFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
78#define CUBE(a) ((a)*(a)*(a))
85 gain = sin(gain *
M_PI / 2.0);
89 gain = 0.6366197723675814 * asin(gain);
92 gain = 1.0 - cos(
M_PI / 4.0 * (
CUBE(2.0*gain - 1) + 1));
95 gain = (1.0 - cos(gain *
M_PI)) / 2.0;
99 gain = 0.3183098861837907 * acos(1 - 2 * gain);
103 gain =
exp(-11.512925464970227 * (1 - gain));
106 gain =
av_clipd(1 + 0.2 * log10(gain), 0, 1.0);
109 gain = 1 - sqrt(1 - gain);
112 gain = (1 - (1 - gain) * (1 - gain));
127 gain = gain <= 0.5 ?
cbrt(2 * gain) / 2: 1 -
cbrt(2 * (1 - gain)) / 2;
130 gain = gain <= 0.5 ?
CUBE(2 * gain) / 2: 1 -
CUBE(2 * (1 - gain)) / 2;
133 const double a = 1. / (1. - 0.787) - 1;
134 double A = 1. / (1.0 +
exp(0 -((gain-0.5) *
a * 2.0)));
135 double B = 1. / (1.0 +
exp(
a));
136 double C = 1. / (1.0 +
exp(0-
a));
137 gain = (
A -
B) / (
C -
B);
141 gain = gain >= 1.0 ? 1.0 : sin(
M_PI * (1.0 - gain)) / (
M_PI * (1.0 - gain));
144 gain = gain <= 0.0 ? 0.0 : 1.0 - sin(
M_PI * gain) / (
M_PI * gain);
147 gain = gain * gain * gain * gain;
150 gain = pow(gain, 0.25);
153 gain = sin(gain *
M_PI / 2.0) * sin(gain *
M_PI / 2.0);
156 gain = pow((1.0 - cos(gain *
M_PI)) / 2.0, 2.0);
163 return silence + (unity - silence) * gain;
166#define FADE_PLANAR(name, type) \
167static void fade_samples_## name ##p(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 double silence, double unity) \
174 for (i = 0; i < nb_samples; i++) { \
175 double gain = fade_gain(curve, start + i * dir,range,silence,unity);\
176 for (c = 0; c < channels; c++) { \
177 type *d = (type *)dst[c]; \
178 const type *s = (type *)src[c]; \
180 d[i] = s[i] * gain; \
185#define FADE(name, type) \
186static void fade_samples_## name (uint8_t **dst, uint8_t * const *src, \
187 int nb_samples, int channels, int dir, \
188 int64_t start, int64_t range, int curve, \
189 double silence, double unity) \
191 type *d = (type *)dst[0]; \
192 const type *s = (type *)src[0]; \
195 for (i = 0; i < nb_samples; i++) { \
196 double gain = fade_gain(curve, start + i * dir,range,silence,unity);\
197 for (c = 0; c < channels; c++, k++) \
198 d[k] = s[k] * gain; \
212#define SCALE_PLANAR(name, type) \
213static void scale_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
214 int nb_samples, int channels, \
219 for (i = 0; i < nb_samples; i++) { \
220 for (c = 0; c < channels; c++) { \
221 type *d = (type *)dst[c]; \
222 const type *s = (type *)src[c]; \
224 d[i] = s[i] * gain; \
229#define SCALE(name, type) \
230static void scale_samples_## name (uint8_t **dst, uint8_t * const *src, \
231 int nb_samples, int channels, double gain)\
233 type *d = (type *)dst[0]; \
234 const type *s = (type *)src[0]; \
237 for (i = 0; i < nb_samples; i++) { \
238 for (c = 0; c < channels; c++, k++) \
239 d[k] = s[k] * gain; \
258 switch (outlink->format) {
259 case AV_SAMPLE_FMT_DBL: s->fade_samples = fade_samples_dbl;
260 s->scale_samples = scale_samples_dbl;
262 case AV_SAMPLE_FMT_DBLP: s->fade_samples = fade_samples_dblp;
263 s->scale_samples = scale_samples_dblp;
265 case AV_SAMPLE_FMT_FLT: s->fade_samples = fade_samples_flt;
266 s->scale_samples = scale_samples_flt;
268 case AV_SAMPLE_FMT_FLTP: s->fade_samples = fade_samples_fltp;
269 s->scale_samples = scale_samples_fltp;
271 case AV_SAMPLE_FMT_S16: s->fade_samples = fade_samples_s16;
272 s->scale_samples = scale_samples_s16;
274 case AV_SAMPLE_FMT_S16P: s->fade_samples = fade_samples_s16p;
275 s->scale_samples = scale_samples_s16p;
277 case AV_SAMPLE_FMT_S32: s->fade_samples = fade_samples_s32;
278 s->scale_samples = scale_samples_s32;
280 case AV_SAMPLE_FMT_S32P: s->fade_samples = fade_samples_s32p;
281 s->scale_samples = scale_samples_s32p;
295#if CONFIG_AFADE_FILTER
297static const AVOption afade_options[] = {
302 {
"start_sample",
"set number of first sample to start fading",
OFFSET(start_sample),
AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX,
TFLAGS },
347 if (INT64_MAX -
s->nb_samples <
s->start_sample)
361 if (
s->unity == 1.0 &&
362 ((!
s->type && (
s->start_sample +
s->nb_samples < cur_sample)) ||
363 (
s->type && (cur_sample + nb_samples < s->start_sample))))
375 if ((!
s->type && (cur_sample + nb_samples < s->start_sample)) ||
376 (
s->type && (
s->start_sample +
s->nb_samples < cur_sample))) {
377 if (
s->silence == 0.) {
385 }
else if ((
s->type && (cur_sample + nb_samples < s->start_sample)) ||
386 (!
s->type && (
s->start_sample +
s->nb_samples < cur_sample))) {
394 start = cur_sample -
s->start_sample;
396 start =
s->start_sample +
s->nb_samples - cur_sample;
400 s->type ? -1 : 1, start,
401 s->nb_samples,
s->curve,
s->silence,
s->unity);
411 char *res,
int res_len,
int flags)
422static const AVFilterPad avfilter_af_afade_inputs[] = {
430static const AVFilterPad avfilter_af_afade_outputs[] = {
441 .p.priv_class = &afade_class,
453#if CONFIG_ACROSSFADE_FILTER
455static const AVOption acrossfade_options[] = {
458 {
"nb_samples",
"set number of samples for cross fade duration",
OFFSET(nb_samples),
AV_OPT_TYPE_INT64, {.i64 = 44100}, 1, INT32_MAX/10,
FLAGS },
459 {
"ns",
"set number of samples for cross fade duration",
OFFSET(nb_samples),
AV_OPT_TYPE_INT64, {.i64 = 44100}, 1, INT32_MAX/10,
FLAGS },
497#define CROSSFADE_PLANAR(name, type) \
498static void crossfade_samples_## name ##p(uint8_t **dst, uint8_t * const *cf0, \
499 uint8_t * const *cf1, \
500 int nb_samples, int channels, \
501 int curve0, int curve1) \
505 for (i = 0; i < nb_samples; i++) { \
506 double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples,0.,1.);\
507 double gain1 = fade_gain(curve1, i, nb_samples, 0., 1.); \
508 for (c = 0; c < channels; c++) { \
509 type *d = (type *)dst[c]; \
510 const type *s0 = (type *)cf0[c]; \
511 const type *s1 = (type *)cf1[c]; \
513 d[i] = s0[i] * gain0 + s1[i] * gain1; \
518#define CROSSFADE(name, type) \
519static void crossfade_samples_## name (uint8_t **dst, uint8_t * const *cf0, \
520 uint8_t * const *cf1, \
521 int nb_samples, int channels, \
522 int curve0, int curve1) \
524 type *d = (type *)dst[0]; \
525 const type *s0 = (type *)cf0[0]; \
526 const type *s1 = (type *)cf1[0]; \
529 for (i = 0; i < nb_samples; i++) { \
530 double gain0 = fade_gain(curve0, nb_samples - 1-i,nb_samples,0.,1.);\
531 double gain1 = fade_gain(curve1, i, nb_samples, 0., 1.); \
532 for (c = 0; c < channels; c++, k++) \
533 d[k] = s0[k] * gain0 + s1[k] * gain1; \
537CROSSFADE_PLANAR(dbl,
double)
538CROSSFADE_PLANAR(flt,
float)
539CROSSFADE_PLANAR(s16, int16_t)
542CROSSFADE(dbl,
double)
544CROSSFADE(s16, int16_t)
588 queued_samples1 /= 2;
589 queued_samples1 =
FFMIN(queued_samples1,
s->nb_samples);
592 int nb_samples =
FFMIN(queued_samples0, queued_samples1);
593 if (nb_samples < s->nb_samples) {
595 "is shorter than crossfade duration (%"PRId64
" samples), "
596 "crossfade will be shorter by %"PRId64
" samples.\n",
597 queued_samples0 <= queued_samples1 ? idx0 : idx1,
598 nb_samples,
s->nb_samples,
s->nb_samples - nb_samples);
600 if (queued_samples0 > nb_samples) {
601 ret = pass_samples(in0, outlink, queued_samples0 - nb_samples, &
s->pts);
628 s->crossfade_samples(
out->extended_data, cf[0]->extended_data,
629 cf[1]->extended_data, nb_samples,
630 out->ch_layout.nb_channels,
s->curve,
s->curve2);
638 if (queued_samples0 < s->nb_samples) {
640 "is shorter than crossfade duration (%"PRId64
" samples), "
641 "fade-out will be shorter by %"PRId64
" samples.\n",
642 idx0, queued_samples0,
s->nb_samples,
643 s->nb_samples - queued_samples0);
644 if (!queued_samples0)
658 s->fade_samples(
out->extended_data, cf[0]->extended_data, cf[0]->nb_samples,
669 if (queued_samples1 < s->nb_samples) {
671 "is shorter than crossfade duration (%"PRId64
" samples), "
672 "fade-in will be shorter by %"PRId64
" samples.\n",
674 s->nb_samples - queued_samples1);
675 if (!queued_samples1)
689 s->fade_samples(
out->extended_data, cf[1]->extended_data, cf[1]->nb_samples,
702 const int idx0 =
s->xfade_idx;
703 const int idx1 =
s->xfade_idx + 1;
709 if (idx0 ==
s->nb_inputs - 1) {
712 return pass_frame(in0, outlink, &
s->pts);
720 if (queued_samples0 >
s->nb_samples) {
722 if (queued_samples0 -
s->nb_samples >=
frame->nb_samples)
723 return pass_frame(in0, outlink, &
s->pts);
728 if (queued_samples0 >
s->nb_samples)
729 return pass_samples(in0, outlink, queued_samples0 -
s->nb_samples, &
s->pts);
743 int needed_samples =
s->nb_samples;
744 if (idx1 < s->nb_inputs - 1)
754 return pass_crossfade(
ctx, idx0, idx1);
766 for (
int i = 0;
i <
s->nb_inputs;
i++) {
782static int acrossfade_config_output(
AVFilterLink *outlink)
789 switch (outlink->
format) {
805static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
809 .config_props = acrossfade_config_output,
814 .p.name =
"acrossfade",
816 .p.priv_class = &acrossfade_class,
819 .
init = acrossfade_init,
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static enum AVSampleFormat sample_fmts[]
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
#define FADE_PLANAR(name, type)
#define SCALE_PLANAR(name, type)
static double fade_gain(int curve, int64_t index, int64_t range, double silence, double unity)
#define SCALE(name, type)
static int config_output(AVFilterLink *outlink)
const FFFilter ff_af_afade
const FFFilter ff_af_acrossfade
static AVFormatContext * ctx
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_append_inpad_free_name(AVFilterContext *f, AVFilterPad *p)
int ff_outlink_get_status(AVFilterLink *link)
Get the status on an output link.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
size_t ff_inlink_queued_frames(AVFilterLink *link)
Get the number of frames available on the link.
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
AVFrame * ff_inlink_peek_frame(AVFilterLink *link, size_t idx)
Access a frame in the link fifo without consuming it.
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
int ff_inlink_queued_samples(AVFilterLink *link)
Main libavfilter public API header.
char * av_asprintf(const char *fmt,...)
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
int(* init)(AVBSFContext *ctx)
static int64_t start_time
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
#define AV_LOG_WARNING
Something somehow does not look correct.
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_FLTP
float, planar
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
@ AV_SAMPLE_FMT_S32
signed 32 bits
@ AV_SAMPLE_FMT_DBLP
double, planar
@ AV_SAMPLE_FMT_DBL
double
@ AV_SAMPLE_FMT_S16
signed 16 bits
int av_samples_set_silence(uint8_t *const *audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
#define AV_TIME_BASE
Internal time base represented as integer.
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
#define FILTER_SAMPLEFMTS_ARRAY(array)
#define FILTER_OUTPUTS(array)
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
#define AVFILTER_DEFINE_CLASS(fname)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
void * priv
private data for use by the filter
AVFilterLink ** outputs
array of pointers to output links
A link between two filters.
AVFilterContext * src
source filter
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
int sample_rate
samples per second
AVChannelLayout ch_layout
channel layout of current buffer (see libavutil/channel_layout.h)
AVFilterContext * dst
dest filter
int format
agreed upon media format
A filter pad used for either input or output.
const char * name
Pad name.
This structure describes decoded (raw) audio or video data.
int nb_samples
number of audio samples (per channel) described by this frame
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
AVChannelLayout ch_layout
Channel layout of the audio data.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
uint8_t ** extended_data
pointers to the data planes/channels.
Rational number (pair of numerator and denominator).
void(* fade_samples)(uint8_t **dst, uint8_t *const *src, int nb_samples, int channels, int direction, int64_t start, int64_t range, int curve, double silence, double unity)
void(* crossfade_samples)(uint8_t **dst, uint8_t *const *cf0, uint8_t *const *cf1, int nb_samples, int channels, int curve0, int curve1)
void(* scale_samples)(uint8_t **dst, uint8_t *const *src, int nb_samples, int channels, double unity)