125#define OFFSET(x) offsetof(SilenceRemoveContext, x)
126#define AF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
127#define AFR AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
130 {
"start_periods",
"set periods of silence parts to skip from start",
OFFSET(start_periods),
AV_OPT_TYPE_INT, {.i64=0}, 0, 9000,
AF },
131 {
"start_duration",
"set start duration of non-silence part",
OFFSET(start_duration_opt),
AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX,
AF },
132 {
"start_threshold",
"set threshold for start silence detection",
OFFSET(start_threshold),
AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX,
AFR },
133 {
"start_silence",
"set start duration of silence part to keep",
OFFSET(start_silence_opt),
AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX,
AF },
137 {
"stop_periods",
"set periods of silence parts to skip from end",
OFFSET(stop_periods),
AV_OPT_TYPE_INT, {.i64=0}, -9000, 9000,
AF },
139 {
"stop_threshold",
"set threshold for stop silence detection",
OFFSET(stop_threshold),
AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX,
AFR },
140 {
"stop_silence",
"set stop duration of silence part to keep",
OFFSET(stop_silence_opt),
AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX,
AF },
143 {
"avg",
"use mean absolute values of samples", 0,
AV_OPT_TYPE_CONST, {.i64=
D_AVG}, 0, 0,
AF, .unit =
"detection" },
144 {
"rms",
"use root mean squared values of samples", 0,
AV_OPT_TYPE_CONST, {.i64=
D_RMS}, 0, 0,
AF, .unit =
"detection" },
147 {
"ptp",
"use absolute of max peak to min peak difference", 0,
AV_OPT_TYPE_CONST, {.i64=
D_PTP}, 0, 0,
AF, .unit =
"detection" },
148 {
"dev",
"use standard deviation from values of samples", 0,
AV_OPT_TYPE_CONST, {.i64=
D_DEV}, 0, 0,
AF, .unit =
"detection" },
149 {
"window",
"set duration of window for silence detection",
OFFSET(window_duration_opt),
AV_OPT_TYPE_DURATION, {.i64=20000}, 0, 100000000,
AF },
151 {
"write",
"full timestamps rewrite, keep only the start time", 0,
AV_OPT_TYPE_CONST, {.i64=
TS_WRITE}, 0, 0,
AF, .unit =
"timestamp" },
152 {
"copy",
"non-dropped frames are left with same timestamp", 0,
AV_OPT_TYPE_CONST, {.i64=
TS_COPY}, 0, 0,
AF, .unit =
"timestamp" },
169 if (
s->stop_periods < 0) {
170 s->stop_periods = -
s->stop_periods;
180 s->start_window->nb_samples,
181 s->start_window->ch_layout.nb_channels,
182 s->start_window->format);
184 s->stop_window->nb_samples,
185 s->stop_window->ch_layout.nb_channels,
186 s->stop_window->format);
188 s->start_window_pos = 0;
189 s->start_window_size = 0;
190 s->stop_window_pos = 0;
191 s->stop_window_size = 0;
192 s->start_queue_pos = 0;
193 s->start_queue_size = 0;
194 s->stop_queue_pos = 0;
195 s->stop_queue_size = 0;
206 s->window_duration =
FFMAX(1,
s->window_duration);
217 s->start_found_periods = 0;
218 s->stop_found_periods = 0;
228 switch (
s->detection) {
239 s->cache_size =
s->window_duration;
247 if (!
s->start_window || !
s->stop_window || !
s->start_cache || !
s->stop_cache)
252 if (!
s->start_queuef || !
s->stop_queuef)
259 if (!
s->start_front || !
s->start_back || !
s->stop_front || !
s->stop_back)
264 switch (
s->detection) {
266 s->compute_flt = compute_avg_flt;
267 s->compute_dbl = compute_avg_dbl;
270 s->compute_flt = compute_dev_flt;
271 s->compute_dbl = compute_dev_dbl;
274 s->compute_flt = compute_ptp_flt;
275 s->compute_dbl = compute_ptp_dbl;
278 s->compute_flt = compute_median_flt;
279 s->compute_dbl = compute_median_dbl;
282 s->compute_flt = compute_peak_flt;
283 s->compute_dbl = compute_peak_dbl;
286 s->compute_flt = compute_rms_flt;
287 s->compute_dbl = compute_rms_dbl;
299 int max_out_nb_samples;
300 int out_nb_samples = 0;
309 s->next_pts = in->
pts;
315 if (max_out_nb_samples <= 0) {
328 out->pts =
s->next_pts;
332 switch (outlink->
format) {
334 srcf = (
const float *)in->
data[0];
335 dstf = (
float *)
out->data[0];
336 if (
s->start_periods > 0 &&
s->stop_periods > 0) {
337 const float *
src = srcf;
338 if (
s->start_found_periods >= 0) {
339 for (
int n = 0; n < in_nb_samples; n++) {
340 filter_start_flt(
ctx,
src + n * nb_channels,
341 dstf, &out_nb_samples,
344 in_nb_samples = out_nb_samples;
348 for (
int n = 0; n < in_nb_samples; n++) {
349 filter_stop_flt(
ctx,
src + n * nb_channels,
350 dstf, &out_nb_samples,
353 }
else if (
s->start_periods > 0) {
354 for (
int n = 0; n < in_nb_samples; n++) {
355 filter_start_flt(
ctx, srcf + n * nb_channels,
356 dstf, &out_nb_samples,
359 }
else if (
s->stop_periods > 0) {
360 for (
int n = 0; n < in_nb_samples; n++) {
361 filter_stop_flt(
ctx, srcf + n * nb_channels,
362 dstf, &out_nb_samples,
368 srcd = (
const double *)in->
data[0];
369 dstd = (
double *)
out->data[0];
370 if (
s->start_periods > 0 &&
s->stop_periods > 0) {
371 const double *
src = srcd;
372 if (
s->start_found_periods >= 0) {
373 for (
int n = 0; n < in_nb_samples; n++) {
374 filter_start_dbl(
ctx,
src + n * nb_channels,
375 dstd, &out_nb_samples,
378 in_nb_samples = out_nb_samples;
382 for (
int n = 0; n < in_nb_samples; n++) {
383 filter_stop_dbl(
ctx,
src + n * nb_channels,
384 dstd, &out_nb_samples,
387 }
else if (
s->start_periods > 0) {
388 for (
int n = 0; n < in_nb_samples; n++) {
389 filter_start_dbl(
ctx, srcd + n * nb_channels,
390 dstd, &out_nb_samples,
393 }
else if (
s->stop_periods > 0) {
394 for (
int n = 0; n < in_nb_samples; n++) {
395 filter_stop_dbl(
ctx, srcd + n * nb_channels,
396 dstd, &out_nb_samples,
404 if (out_nb_samples > 0) {
405 s->next_pts += out_nb_samples;
406 out->nb_samples = out_nb_samples;
430 if (
s->start_periods == 1 &&
s->stop_periods == 0 &&
431 s->start_found_periods < 0) {
433 in->
pts =
s->next_pts;
437 if (
s->start_periods == 0 &&
s->stop_periods == 0)
482 .p.name =
"silenceremove",
484 .p.priv_class = &silenceremove_class,
static int config_input(AVFilterLink *inlink)
static const AVFilterPad silenceremove_outputs[]
static int config_input(AVFilterLink *inlink)
static void clear_windows(SilenceRemoveContext *s)
const FFFilter ff_af_silenceremove
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static int config_output(AVFilterLink *outlink)
static const AVFilterPad silenceremove_inputs[]
static const AVOption silenceremove_options[]
static int filter_frame(AVFilterLink *outlink, AVFrame *in)
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().
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.
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.
Main libavfilter public API header.
int(* init)(AVBSFContext *ctx)
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
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_DBL
double
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_NOPTS_VALUE
Undefined timestamp value.
#define AV_TIME_BASE
Internal time base represented as integer.
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
#define FILTER_OUTPUTS(array)
#define FILTER_SAMPLEFMTS(...)
#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(outlink, inlink)
Forward the status on an output link to an input link.
#define AVFILTER_DEFINE_CLASS(fname)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
A link between two filters.
AVFilterContext * src
source filter
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.
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).
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int64_t start_silence_opt
double(* compute_dbl)(double *c, double s, double ws, int size, int *front, int *back)
float(* compute_flt)(float *c, float s, float ws, int size, int *front, int *back)
int64_t start_duration_opt
int64_t stop_duration_opt
int64_t window_duration_opt
static AVFormatContext * ctx