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,
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);
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,
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
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
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).
@ 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
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.
int(* init)(AVBSFContext *ctx)
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.
#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.
int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
@ 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.
int ff_outlink_get_status(AVFilterLink *link)
Get the status on an output link.
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