Go to the documentation of this file.
26 #include "config_components.h"
57 int curve0,
int curve1);
60 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 };
62 #define OFFSET(x) offsetof(AudioFadeContext, x)
63 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
64 #define TFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
76 #define CUBE(a) ((a)*(a)*(a))
83 gain = sin(gain *
M_PI / 2.0);
87 gain = 0.6366197723675814 * asin(gain);
90 gain = 1.0 - cos(
M_PI / 4.0 * (
CUBE(2.0*gain - 1) + 1));
93 gain = (1.0 - cos(gain *
M_PI)) / 2.0;
97 gain = 0.3183098861837907 * acos(1 - 2 * gain);
101 gain =
exp(-11.512925464970227 * (1 - gain));
104 gain =
av_clipd(1 + 0.2 * log10(gain), 0, 1.0);
107 gain = 1 - sqrt(1 - gain);
110 gain = (1 - (1 - gain) * (1 - gain));
125 gain = gain <= 0.5 ?
cbrt(2 * gain) / 2: 1 -
cbrt(2 * (1 - gain)) / 2;
128 gain = gain <= 0.5 ?
CUBE(2 * gain) / 2: 1 -
CUBE(2 * (1 - gain)) / 2;
131 const double a = 1. / (1. - 0.787) - 1;
132 double A = 1. / (1.0 +
exp(0 -((gain-0.5) *
a * 2.0)));
133 double B = 1. / (1.0 +
exp(
a));
134 double C = 1. / (1.0 +
exp(0-
a));
135 gain = (
A -
B) / (
C -
B);
139 gain = gain >= 1.0 ? 1.0 : sin(
M_PI * (1.0 - gain)) / (
M_PI * (1.0 - gain));
142 gain = gain <= 0.0 ? 0.0 : 1.0 - sin(
M_PI * gain) / (
M_PI * gain);
145 gain = gain * gain * gain * gain;
148 gain = pow(gain, 0.25);
151 gain = sin(gain *
M_PI / 2.0) * sin(gain *
M_PI / 2.0);
154 gain = pow((1.0 - cos(gain *
M_PI)) / 2.0, 2.0);
161 return silence + (unity - silence) * gain;
164 #define FADE_PLANAR(name, type) \
165 static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
166 int nb_samples, int channels, int dir, \
167 int64_t start, int64_t range,int curve,\
168 double silence, double unity) \
172 for (i = 0; i < nb_samples; i++) { \
173 double gain = fade_gain(curve, start + i * dir,range,silence,unity);\
174 for (c = 0; c < channels; c++) { \
175 type *d = (type *)dst[c]; \
176 const type *s = (type *)src[c]; \
178 d[i] = s[i] * gain; \
183 #define FADE(name, type) \
184 static void fade_samples_## name (uint8_t **dst, uint8_t * const *src, \
185 int nb_samples, int channels, int dir, \
186 int64_t start, int64_t range, int curve, \
187 double silence, double unity) \
189 type *d = (type *)dst[0]; \
190 const type *s = (type *)src[0]; \
193 for (i = 0; i < nb_samples; i++) { \
194 double gain = fade_gain(curve, start + i * dir,range,silence,unity);\
195 for (c = 0; c < channels; c++, k++) \
196 d[k] = s[k] * gain; \
210 #define SCALE_PLANAR(name, type) \
211 static void scale_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
212 int nb_samples, int channels, \
217 for (i = 0; i < nb_samples; i++) { \
218 for (c = 0; c < channels; c++) { \
219 type *d = (type *)dst[c]; \
220 const type *s = (type *)src[c]; \
222 d[i] = s[i] * gain; \
227 #define SCALE(name, type) \
228 static void scale_samples_## name (uint8_t **dst, uint8_t * const *src, \
229 int nb_samples, int channels, double gain)\
231 type *d = (type *)dst[0]; \
232 const type *s = (type *)src[0]; \
235 for (i = 0; i < nb_samples; i++) { \
236 for (c = 0; c < channels; c++, k++) \
237 d[k] = s[k] * gain; \
256 switch (outlink->format) {
258 s->scale_samples = scale_samples_dbl;
261 s->scale_samples = scale_samples_dblp;
264 s->scale_samples = scale_samples_flt;
267 s->scale_samples = scale_samples_fltp;
273 s->scale_samples = scale_samples_s16p;
279 s->scale_samples = scale_samples_s32p;
293 #if CONFIG_AFADE_FILTER
295 static const AVOption afade_options[] = {
300 {
"start_sample",
"set number of first sample to start fading",
OFFSET(start_sample),
AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX,
TFLAGS },
345 if (INT64_MAX -
s->nb_samples <
s->start_sample)
359 if (
s->unity == 1.0 &&
360 ((!
s->type && (
s->start_sample +
s->nb_samples < cur_sample)) ||
361 (
s->type && (cur_sample + nb_samples < s->start_sample))))
373 if ((!
s->type && (cur_sample + nb_samples < s->start_sample)) ||
374 (
s->type && (
s->start_sample +
s->nb_samples < cur_sample))) {
375 if (
s->silence == 0.) {
383 }
else if ((
s->type && (cur_sample + nb_samples < s->start_sample)) ||
384 (!
s->type && (
s->start_sample +
s->nb_samples < cur_sample))) {
392 start = cur_sample -
s->start_sample;
394 start =
s->start_sample +
s->nb_samples - cur_sample;
398 s->type ? -1 : 1, start,
399 s->nb_samples,
s->curve,
s->silence,
s->unity);
409 char *res,
int res_len,
int flags)
420 static const AVFilterPad avfilter_af_afade_inputs[] = {
428 static const AVFilterPad avfilter_af_afade_outputs[] = {
444 .priv_class = &afade_class,
451 #if CONFIG_ACROSSFADE_FILTER
453 static const AVOption acrossfade_options[] = {
454 {
"nb_samples",
"set number of samples for cross fade duration",
OFFSET(nb_samples),
AV_OPT_TYPE_INT64, {.i64 = 44100}, 1, INT32_MAX/10,
FLAGS },
455 {
"ns",
"set number of samples for cross fade duration",
OFFSET(nb_samples),
AV_OPT_TYPE_INT64, {.i64 = 44100}, 1, INT32_MAX/10,
FLAGS },
493 #define CROSSFADE_PLANAR(name, type) \
494 static void crossfade_samples_## name ##p(uint8_t **dst, uint8_t * const *cf0, \
495 uint8_t * const *cf1, \
496 int nb_samples, int channels, \
497 int curve0, int curve1) \
501 for (i = 0; i < nb_samples; i++) { \
502 double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples,0.,1.);\
503 double gain1 = fade_gain(curve1, i, nb_samples, 0., 1.); \
504 for (c = 0; c < channels; c++) { \
505 type *d = (type *)dst[c]; \
506 const type *s0 = (type *)cf0[c]; \
507 const type *s1 = (type *)cf1[c]; \
509 d[i] = s0[i] * gain0 + s1[i] * gain1; \
514 #define CROSSFADE(name, type) \
515 static void crossfade_samples_## name (uint8_t **dst, uint8_t * const *cf0, \
516 uint8_t * const *cf1, \
517 int nb_samples, int channels, \
518 int curve0, int curve1) \
520 type *d = (type *)dst[0]; \
521 const type *s0 = (type *)cf0[0]; \
522 const type *s1 = (type *)cf1[0]; \
525 for (i = 0; i < nb_samples; i++) { \
526 double gain0 = fade_gain(curve0, nb_samples - 1-i,nb_samples,0.,1.);\
527 double gain1 = fade_gain(curve1, i, nb_samples, 0., 1.); \
528 for (c = 0; c < channels; c++, k++) \
529 d[k] = s0[k] * gain0 + s1[k] * gain1; \
533 CROSSFADE_PLANAR(dbl,
double)
534 CROSSFADE_PLANAR(flt,
float)
535 CROSSFADE_PLANAR(s16, int16_t)
538 CROSSFADE(dbl,
double)
539 CROSSFADE(flt,
float)
540 CROSSFADE(s16, int16_t)
560 if (
s->passthrough &&
s->status[0]) {
567 }
else if (
ret < 0) {
581 if (nb_samples >
s->nb_samples) {
582 nb_samples -=
s->nb_samples;
591 }
else if (
s->status[0] && nb_samples >=
s->nb_samples &&
610 s->crossfade_samples(
out->extended_data, cf[0]->extended_data,
611 cf[1]->extended_data,
612 s->nb_samples,
out->ch_layout.nb_channels,
613 s->curve,
s->curve2);
632 s->fade_samples(
out->extended_data, cf[0]->extended_data,
s->nb_samples,
652 s->fade_samples(
out->extended_data, cf[1]->extended_data,
s->nb_samples,
662 if (!
s->status[0] && check_input(
ctx->inputs[0]))
664 s->passthrough = !
s->status[0];
665 if (check_input(
ctx->inputs[1])) {
680 static int acrossfade_config_output(
AVFilterLink *outlink)
687 switch (outlink->
format) {
708 return s->passthrough ?
713 static const AVFilterPad avfilter_af_acrossfade_inputs[] = {
715 .
name =
"crossfade0",
720 .name =
"crossfade1",
726 static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
730 .config_props = acrossfade_config_output,
735 .
name =
"acrossfade",
739 .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.
#define FILTER_INPUTS(array)
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
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
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.
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
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_OUTPUTS(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)
#define AVFILTER_DEFINE_CLASS(fname)
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.
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
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.
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...
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
#define FILTER_SAMPLEFMTS_ARRAY(array)
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_OPT_TYPE_INT
Underlying C type is int.
@ 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)
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
#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
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.