47 int nb_samples,
int channels,
int direction,
48 int64_t
start,
int range,
int curve);
51 int nb_samples,
int channels,
52 int curve0,
int curve1);
55 enum CurveType {
TRI,
QSIN,
ESIN,
HSIN,
LOG,
IPAR,
QUA,
CUB,
SQU,
CBR,
PAR,
EXP,
IQSIN,
IHSIN,
DESE,
DESI,
NB_CURVES };
57 #define OFFSET(x) offsetof(AudioFadeContext, x)
58 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
97 gain = av_clipd(1.0 * index / range, 0, 1.0);
101 gain = sin(gain *
M_PI / 2.0);
104 gain = 0.636943 * asin(gain);
107 gain = 1.0 - cos(
M_PI / 4.0 * (pow(2.0*gain - 1, 3) + 1));
110 gain = (1.0 - cos(gain *
M_PI)) / 2.0;
113 gain = 0.318471 * acos(1 - 2 * gain);
116 gain = pow(0.1, (1 - gain) * 5.0);
119 gain = av_clipd(0.0868589 * log(100000 * gain), 0, 1.0);
122 gain = 1 - sqrt(1 - gain);
125 gain = (1 - (1 - gain) * (1 - gain));
131 gain = gain * gain * gain;
140 gain = gain <= 0.5 ? pow(2 * gain, 1/3.) / 2: 1 - pow(2 * (1 - gain), 1/3.) / 2;
143 gain = gain <= 0.5 ? pow(2 * gain, 3) / 2: 1 - pow(2 * (1 - gain), 3) / 2;
150 #define FADE_PLANAR(name, type) \
151 static void fade_samples_## name ##p(uint8_t **dst, uint8_t * const *src, \
152 int nb_samples, int channels, int dir, \
153 int64_t start, int range, int curve) \
157 for (i = 0; i < nb_samples; i++) { \
158 double gain = fade_gain(curve, start + i * dir, range); \
159 for (c = 0; c < channels; c++) { \
160 type *d = (type *)dst[c]; \
161 const type *s = (type *)src[c]; \
163 d[i] = s[i] * gain; \
168 #define FADE(name, type) \
169 static void fade_samples_## name (uint8_t **dst, uint8_t * const *src, \
170 int nb_samples, int channels, int dir, \
171 int64_t start, int range, int curve) \
173 type *d = (type *)dst[0]; \
174 const type *s = (type *)src[0]; \
177 for (i = 0; i < nb_samples; i++) { \
178 double gain = fade_gain(curve, start + i * dir, range); \
179 for (c = 0; c < channels; c++, k++) \
180 d[k] = s[k] * gain; \
199 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,
FLAGS },
227 {
"nb_samples",
"set number of samples for fade duration",
OFFSET(nb_samples),
AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX,
FLAGS },
228 {
"ns",
"set number of samples for fade duration",
OFFSET(nb_samples),
AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX,
FLAGS },
287 if ((!s->
type && (cur_sample + nb_samples < s->start_sample)) ||
301 s->
type ? -1 : 1, start,
311 static const AVFilterPad avfilter_af_afade_inputs[] = {
320 static const AVFilterPad avfilter_af_afade_outputs[] = {
335 .
inputs = avfilter_af_afade_inputs,
336 .
outputs = avfilter_af_afade_outputs,
337 .priv_class = &afade_class,
343 #if CONFIG_ACROSSFADE_FILTER
345 static const AVOption acrossfade_options[] = {
346 {
"nb_samples",
"set number of samples for cross fade duration",
OFFSET(nb_samples),
AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10,
FLAGS },
347 {
"ns",
"set number of samples for cross fade duration",
OFFSET(nb_samples),
AV_OPT_TYPE_INT, {.i64 = 44100}, 1, INT32_MAX/10,
FLAGS },
393 #define CROSSFADE_PLANAR(name, type) \
394 static void crossfade_samples_## name ##p(uint8_t **dst, uint8_t * const *cf0, \
395 uint8_t * const *cf1, \
396 int nb_samples, int channels, \
397 int curve0, int curve1) \
401 for (i = 0; i < nb_samples; i++) { \
402 double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
403 double gain1 = fade_gain(curve1, i, nb_samples); \
404 for (c = 0; c < channels; c++) { \
405 type *d = (type *)dst[c]; \
406 const type *s0 = (type *)cf0[c]; \
407 const type *s1 = (type *)cf1[c]; \
409 d[i] = s0[i] * gain0 + s1[i] * gain1; \
414 #define CROSSFADE(name, type) \
415 static void crossfade_samples_## name (uint8_t **dst, uint8_t * const *cf0, \
416 uint8_t * const *cf1, \
417 int nb_samples, int channels, \
418 int curve0, int curve1) \
420 type *d = (type *)dst[0]; \
421 const type *s0 = (type *)cf0[0]; \
422 const type *s1 = (type *)cf1[0]; \
425 for (i = 0; i < nb_samples; i++) { \
426 double gain0 = fade_gain(curve0, nb_samples - 1 - i, nb_samples); \
427 double gain1 = fade_gain(curve1, i, nb_samples); \
428 for (c = 0; c < channels; c++, k++) \
429 d[k] = s0[k] * gain0 + s1[k] * gain1; \
433 CROSSFADE_PLANAR(dbl,
double)
434 CROSSFADE_PLANAR(flt,
float)
435 CROSSFADE_PLANAR(s16, int16_t)
438 CROSSFADE(dbl,
double)
439 CROSSFADE(flt,
float)
440 CROSSFADE(s16, int16_t)
449 int ret = 0, nb_samples;
456 }
else if (inlink == ctx->
inputs[0]) {
460 if (nb_samples > 0) {
478 if (!out || !cf[0]) {
485 outlink->
channels, -1, nb_samples - 1, nb_samples, s->
curve);
500 if (!out || !cf[0] || !cf[1]) {
522 if (!out || !cf[1]) {
541 if (nb_samples > 0) {
564 static int acrossfade_request_frame(
AVFilterLink *outlink)
597 static int acrossfade_config_output(
AVFilterLink *outlink)
604 "Inputs must have the same sample rate "
605 "%d for in0 vs %d for in1\n",
616 switch (outlink->
format) {
645 static const AVFilterPad avfilter_af_acrossfade_inputs[] = {
647 .
name =
"crossfade0",
649 .filter_frame = acrossfade_filter_frame,
652 .name =
"crossfade1",
654 .filter_frame = acrossfade_filter_frame,
659 static const AVFilterPad avfilter_af_acrossfade_outputs[] = {
663 .request_frame = acrossfade_request_frame,
664 .config_props = acrossfade_config_output,
670 .
name =
"acrossfade",
675 .priv_class = &acrossfade_class,
676 .
inputs = avfilter_af_acrossfade_inputs,
677 .
outputs = avfilter_af_acrossfade_outputs,
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
static double fade_gain(int curve, int64_t index, int range)
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
This structure describes decoded (raw) audio or video data.
static const AVFilterPad outputs[]
Main libavfilter public API header.
static av_cold int init(AVCodecContext *avctx)
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
static int config_output(AVFilterLink *outlink)
static enum AVSampleFormat formats[]
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
static int64_t start_time
static av_cold int uninit(AVCodecContext *avctx)
void(* crossfade_samples)(uint8_t **dst, uint8_t *const *cf0, uint8_t *const *cf1, int nb_samples, int channels, int curve0, int curve1)
static int query_formats(AVFilterContext *ctx)
#define FADE_PLANAR(name, type)
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
void(* fade_samples)(uint8_t **dst, uint8_t *const *src, int nb_samples, int channels, int direction, int64_t start, int range, int curve)
#define AVERROR_EOF
End of file.
A filter pad used for either input or output.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
A link between two filters.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static av_always_inline double cbrt(double x)
unsigned flags
Link processing flags.
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.
int sample_rate
samples per second
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link...
Context for an Audio FIFO Buffer.
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
#define AV_TIME_BASE
Internal time base represented as integer.
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
AVFilterContext * src
source filter
Frame requests may need to loop in order to be fulfilled.
int format
agreed upon media format
A list of supported channel layouts.
int nb_samples
number of samples currently in the FIFO
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
AVSampleFormat
Audio sample formats.
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
Describe the class of an AVClass context structure.
int av_frame_get_channels(const AVFrame *frame)
static const AVFilterPad inputs[]
rational number numerator/denominator
const char * name
Filter name.
AVFilterLink ** outputs
array of pointers to output links
enum MovChannelLayoutTag * layouts
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
uint64_t channel_layout
channel layout of current buffer (see libavutil/channel_layout.h)
int channels
Number of channels.
AVFilterContext * dst
dest filter
#define AVFILTER_DEFINE_CLASS(fname)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
static enum AVSampleFormat sample_fmts[]
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
uint8_t ** extended_data
pointers to the data planes/channels.
int nb_samples
number of audio samples (per channel) described by this frame
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.