Go to the documentation of this file.
26 #include "config_components.h"
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,
QUAT,
QUATR,
QSIN2,
HSIN2,
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
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);
146 gain = gain * gain * gain * gain;
149 gain = pow(gain, 0.25);
152 gain = sin(gain *
M_PI / 2.0) * sin(gain *
M_PI / 2.0);
155 gain = pow((1.0 - cos(gain *
M_PI)) / 2.0, 2.0);
162 return silence + (unity - silence) * gain;
165 #define FADE_PLANAR(name, type) \
166 static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
167 int nb_samples, int channels, int dir, \
168 int64_t start, int64_t range,int curve,\
169 double silence, double unity) \
173 for (i = 0; i < nb_samples; i++) { \
174 double gain = fade_gain(curve, start + i * dir,range,silence,unity);\
175 for (c = 0; c < channels; c++) { \
176 type *d = (type *)dst[c]; \
177 const type *s = (type *)src[c]; \
179 d[i] = s[i] * gain; \
184 #define FADE(name, type) \
185 static void fade_samples_## name (uint8_t **dst, uint8_t * const *src, \
186 int nb_samples, int channels, int dir, \
187 int64_t start, int64_t range, int curve, \
188 double silence, double unity) \
190 type *d = (type *)dst[0]; \
191 const type *s = (type *)src[0]; \
194 for (i = 0; i < nb_samples; i++) { \
195 double gain = fade_gain(curve, start + i * dir,range,silence,unity);\
196 for (c = 0; c < channels; c++, k++) \
197 d[k] = s[k] * gain; \
211 #define SCALE_PLANAR(name, type) \
212 static void scale_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
213 int nb_samples, int channels, \
218 for (i = 0; i < nb_samples; i++) { \
219 for (c = 0; c < channels; c++) { \
220 type *d = (type *)dst[c]; \
221 const type *s = (type *)src[c]; \
223 d[i] = s[i] * gain; \
228 #define SCALE(name, type) \
229 static void scale_samples_## name (uint8_t **dst, uint8_t * const *src, \
230 int nb_samples, int channels, double gain)\
232 type *d = (type *)dst[0]; \
233 const type *s = (type *)src[0]; \
236 for (i = 0; i < nb_samples; i++) { \
237 for (c = 0; c < channels; c++, k++) \
238 d[k] = s[k] * gain; \
257 switch (outlink->format) {
259 s->scale_samples = scale_samples_dbl;
262 s->scale_samples = scale_samples_dblp;
265 s->scale_samples = scale_samples_flt;
268 s->scale_samples = scale_samples_fltp;
274 s->scale_samples = scale_samples_s16p;
280 s->scale_samples = scale_samples_s32p;
294 #if CONFIG_AFADE_FILTER
296 static const AVOption afade_options[] = {
301 {
"start_sample",
"set number of first sample to start fading",
OFFSET(start_sample),
AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX,
TFLAGS },
346 if (INT64_MAX -
s->nb_samples <
s->start_sample)
360 if (
s->unity == 1.0 &&
361 ((!
s->type && (
s->start_sample +
s->nb_samples < cur_sample)) ||
362 (
s->type && (cur_sample + nb_samples < s->start_sample))))
374 if ((!
s->type && (cur_sample + nb_samples < s->start_sample)) ||
375 (
s->type && (
s->start_sample +
s->nb_samples < cur_sample))) {
376 if (
s->silence == 0.) {
384 }
else if ((
s->type && (cur_sample + nb_samples < s->start_sample)) ||
385 (!
s->type && (
s->start_sample +
s->nb_samples < cur_sample))) {
393 start = cur_sample -
s->start_sample;
395 start =
s->start_sample +
s->nb_samples - cur_sample;
399 s->type ? -1 : 1, start,
400 s->nb_samples,
s->curve,
s->silence,
s->unity);
410 char *res,
int res_len,
int flags)
421 static const AVFilterPad avfilter_af_afade_inputs[] = {
429 static const AVFilterPad avfilter_af_afade_outputs[] = {
445 .priv_class = &afade_class,
452 #if CONFIG_ACROSSFADE_FILTER
454 static const AVOption acrossfade_options[] = {
455 {
"nb_samples",
"set number of samples for cross fade duration",
OFFSET(nb_samples),
AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10,
FLAGS },
456 {
"ns",
"set number of samples for cross fade duration",
OFFSET(nb_samples),
AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10,
FLAGS },
494 #define CROSSFADE_PLANAR(name, type) \
495 static void crossfade_samples_## name ##p(uint8_t **dst, uint8_t * const *cf0, \
496 uint8_t * const *cf1, \
497 int nb_samples, int channels, \
498 int curve0, int curve1) \
502 for (i = 0; i < nb_samples; i++) { \
503 double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples,0.,1.);\
504 double gain1 = fade_gain(curve1, i, nb_samples, 0., 1.); \
505 for (c = 0; c < channels; c++) { \
506 type *d = (type *)dst[c]; \
507 const type *s0 = (type *)cf0[c]; \
508 const type *s1 = (type *)cf1[c]; \
510 d[i] = s0[i] * gain0 + s1[i] * gain1; \
515 #define CROSSFADE(name, type) \
516 static void crossfade_samples_## name (uint8_t **dst, uint8_t * const *cf0, \
517 uint8_t * const *cf1, \
518 int nb_samples, int channels, \
519 int curve0, int curve1) \
521 type *d = (type *)dst[0]; \
522 const type *s0 = (type *)cf0[0]; \
523 const type *s1 = (type *)cf1[0]; \
526 for (i = 0; i < nb_samples; i++) { \
527 double gain0 = fade_gain(curve0, nb_samples - 1-i,nb_samples,0.,1.);\
528 double gain1 = fade_gain(curve1, i, nb_samples, 0., 1.); \
529 for (c = 0; c < channels; c++, k++) \
530 d[k] = s0[k] * gain0 + s1[k] * gain1; \
534 CROSSFADE_PLANAR(dbl,
double)
535 CROSSFADE_PLANAR(flt,
float)
536 CROSSFADE_PLANAR(s16, int16_t)
539 CROSSFADE(dbl,
double)
540 CROSSFADE(flt,
float)
541 CROSSFADE(s16, int16_t)
561 if (
s->passthrough &&
s->status[0]) {
568 }
else if (
ret < 0) {
582 if (nb_samples >
s->nb_samples) {
583 nb_samples -=
s->nb_samples;
592 }
else if (
s->status[0] && nb_samples >=
s->nb_samples &&
611 s->crossfade_samples(
out->extended_data, cf[0]->extended_data,
612 cf[1]->extended_data,
613 s->nb_samples,
out->ch_layout.nb_channels,
614 s->curve,
s->curve2);
633 s->fade_samples(
out->extended_data, cf[0]->extended_data,
s->nb_samples,
653 s->fade_samples(
out->extended_data, cf[1]->extended_data,
s->nb_samples,
663 if (!
s->status[0] && check_input(
ctx->inputs[0]))
665 s->passthrough = !
s->status[0];
666 if (check_input(
ctx->inputs[1])) {
681 static int acrossfade_config_output(
AVFilterLink *outlink)
688 switch (outlink->
format) {
709 return s->passthrough ?
714 static const AVFilterPad avfilter_af_acrossfade_inputs[] = {
716 .
name =
"crossfade0",
721 .name =
"crossfade1",
727 static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
731 .config_props = acrossfade_config_output,
736 .
name =
"acrossfade",
740 .priv_class = &acrossfade_class,
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
@ AV_SAMPLE_FMT_FLTP
float, planar
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
const AVFilter ff_af_afade
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
#define AVERROR_EOF
End of file.
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)
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
This structure describes decoded (raw) audio or video data.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
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.
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
static int config_output(AVFilterLink *outlink)
#define SCALE_PLANAR(name, type)
const char * name
Filter name.
int nb_channels
Number of channels in this layout.
A link between two filters.
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
static void scale_samples_s32(uint8_t *dst, const uint8_t *src, int nb_samples, int volume)
static void scale_samples_s16(uint8_t *dst, const uint8_t *src, int nb_samples, int volume)
static double fade_gain(int curve, int64_t index, int64_t range, double silence, double unity)
AVChannelLayout ch_layout
Channel layout of the audio data.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
A filter pad used for either input or output.
s EdgeDetect Foobar g libavfilter vf_edgedetect c libavfilter vf_foobar c edit libavfilter and add an entry for foobar following the pattern of the other filters edit libavfilter allfilters and add an entry for foobar following the pattern of the other filters configure make j< whatever > ffmpeg ffmpeg i you should get a foobar png with Lena edge detected That s your new playground is ready Some little details about what s going which in turn will define variables for the build system and the C
const AVFilter ff_af_acrossfade
int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min)
Test if enough samples are available on the link.
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
AVFrame * ff_null_get_audio_buffer(AVFilterLink *link, int nb_samples)
get_audio_buffer() handler for filters which simply pass audio along
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
#define FILTER_INPUTS(array)
void(* crossfade_samples)(uint8_t **dst, uint8_t *const *cf0, uint8_t *const *cf1, int nb_samples, int channels, int curve0, int curve1)
Describe the class of an AVClass context structure.
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.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
#define SCALE(name, type)
Rational number (pair of numerator and denominator).
filter_frame For filters that do not use the activate() callback
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
int(* init)(AVBSFContext *ctx)
static AVFrame * get_audio_buffer(AVFilterLink *inlink, int nb_samples)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static int64_t start_time
int format
agreed upon media format
#define AV_NOPTS_VALUE
Undefined timestamp value.
#define FILTER_SAMPLEFMTS_ARRAY(array)
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
void(* scale_samples)(uint8_t **dst, uint8_t *const *src, int nb_samples, int channels, double unity)
AVFilterContext * src
source 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.
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
#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_DEFINE_CLASS(fname)
int nb_samples
number of audio samples (per channel) described by this frame
#define AV_TIME_BASE
Internal time base represented as integer.
uint8_t ** extended_data
pointers to the data planes/channels.
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_S16
signed 16 bits
const char * name
Pad name.
int ff_inlink_queued_samples(AVFilterLink *link)
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
@ AV_SAMPLE_FMT_DBLP
double, planar
#define FADE_PLANAR(name, type)
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
AVFrame * ff_default_get_audio_buffer(AVFilterLink *link, int nb_samples)
default handler for get_audio_buffer() for audio inputs
AVChannelLayout ch_layout
channel layout of current buffer (see libavutil/channel_layout.h)
#define FILTER_OUTPUTS(array)
#define flags(name, subs,...)
static enum AVSampleFormat sample_fmts[]
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
@ AV_SAMPLE_FMT_DBL
double
@ AV_SAMPLE_FMT_S32
signed 32 bits