2 * Copyright (c) 2001 Heikki Leinonen
3 * Copyright (c) 2001 Chris Bagwell
4 * Copyright (c) 2003 Donnie Smith
5 * Copyright (c) 2014 Paul B Mahol
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <float.h> /* DBL_MAX */
26 #include "libavutil/opt.h"
27 #include "libavutil/timestamp.h"
51 typedef struct SilenceRemoveContext {
54 enum SilenceMode mode;
57 int64_t start_duration;
58 int64_t start_duration_opt;
59 double start_threshold;
60 int64_t start_silence;
61 int64_t start_silence_opt;
65 int64_t stop_duration;
66 int64_t stop_duration_opt;
67 double stop_threshold;
69 int64_t stop_silence_opt;
72 double *start_holdoff;
73 double *start_silence_hold;
74 size_t start_holdoff_offset;
75 size_t start_holdoff_end;
76 size_t start_silence_offset;
77 size_t start_silence_end;
78 int start_found_periods;
81 double *stop_silence_hold;
82 size_t stop_holdoff_offset;
83 size_t stop_holdoff_end;
84 size_t stop_silence_offset;
85 size_t stop_silence_end;
86 int stop_found_periods;
90 double *window_current;
99 void (*update)(struct SilenceRemoveContext *s, double sample);
100 double(*compute)(struct SilenceRemoveContext *s, double sample);
101 } SilenceRemoveContext;
103 #define OFFSET(x) offsetof(SilenceRemoveContext, x)
104 #define AF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
106 static const AVOption silenceremove_options[] = {
107 { "start_periods", NULL, OFFSET(start_periods), AV_OPT_TYPE_INT, {.i64=0}, 0, 9000, AF },
108 { "start_duration", "set start duration of non-silence part", OFFSET(start_duration_opt), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX, AF },
109 { "start_threshold", "set threshold for start silence detection", OFFSET(start_threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX, AF },
110 { "start_silence", "set start duration of silence part to keep", OFFSET(start_silence_opt), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX, AF },
111 { "start_mode", "set which channel will trigger trimming from start", OFFSET(start_mode), AV_OPT_TYPE_INT, {.i64=T_ANY}, T_ANY, T_ALL, AF, "mode" },
112 { "any", 0, 0, AV_OPT_TYPE_CONST, {.i64=T_ANY}, 0, 0, AF, "mode" },
113 { "all", 0, 0, AV_OPT_TYPE_CONST, {.i64=T_ALL}, 0, 0, AF, "mode" },
114 { "stop_periods", NULL, OFFSET(stop_periods), AV_OPT_TYPE_INT, {.i64=0}, -9000, 9000, AF },
115 { "stop_duration", "set stop duration of non-silence part", OFFSET(stop_duration_opt), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX, AF },
116 { "stop_threshold", "set threshold for stop silence detection", OFFSET(stop_threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX, AF },
117 { "stop_silence", "set stop duration of silence part to keep", OFFSET(stop_silence_opt), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX, AF },
118 { "stop_mode", "set which channel will trigger trimming from end", OFFSET(stop_mode), AV_OPT_TYPE_INT, {.i64=T_ANY}, T_ANY, T_ALL, AF, "mode" },
119 { "detection", "set how silence is detected", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=D_RMS}, D_PEAK,D_RMS, AF, "detection" },
120 { "peak", "use absolute values of samples", 0, AV_OPT_TYPE_CONST, {.i64=D_PEAK},0, 0, AF, "detection" },
121 { "rms", "use squared values of samples", 0, AV_OPT_TYPE_CONST, {.i64=D_RMS}, 0, 0, AF, "detection" },
122 { "window", "set duration of window in seconds", OFFSET(window_ratio), AV_OPT_TYPE_DOUBLE, {.dbl=0.02}, 0, 10, AF },
126 AVFILTER_DEFINE_CLASS(silenceremove);
128 static double compute_peak(SilenceRemoveContext *s, double sample)
133 new_sum -= *s->window_current;
134 new_sum += fabs(sample);
136 return new_sum / s->window_size;
139 static void update_peak(SilenceRemoveContext *s, double sample)
141 s->sum -= *s->window_current;
142 *s->window_current = fabs(sample);
143 s->sum += *s->window_current;
146 if (s->window_current >= s->window_end)
147 s->window_current = s->window;
150 static double compute_rms(SilenceRemoveContext *s, double sample)
155 new_sum -= *s->window_current;
156 new_sum += sample * sample;
158 return sqrt(new_sum / s->window_size);
161 static void update_rms(SilenceRemoveContext *s, double sample)
163 s->sum -= *s->window_current;
164 *s->window_current = sample * sample;
165 s->sum += *s->window_current;
168 if (s->window_current >= s->window_end)
169 s->window_current = s->window;
172 static av_cold int init(AVFilterContext *ctx)
174 SilenceRemoveContext *s = ctx->priv;
176 if (s->stop_periods < 0) {
177 s->stop_periods = -s->stop_periods;
181 switch (s->detection) {
183 s->update = update_peak;
184 s->compute = compute_peak;
187 s->update = update_rms;
188 s->compute = compute_rms;
195 static void clear_window(SilenceRemoveContext *s)
197 memset(s->window, 0, s->window_size * sizeof(*s->window));
199 s->window_current = s->window;
200 s->window_end = s->window + s->window_size;
204 static int config_input(AVFilterLink *inlink)
206 AVFilterContext *ctx = inlink->dst;
207 SilenceRemoveContext *s = ctx->priv;
209 s->window_size = FFMAX((inlink->sample_rate * s->window_ratio), 1) * inlink->channels;
210 s->window = av_malloc_array(s->window_size, sizeof(*s->window));
212 return AVERROR(ENOMEM);
216 s->start_duration = av_rescale(s->start_duration_opt, inlink->sample_rate,
218 s->start_silence = av_rescale(s->start_silence_opt, inlink->sample_rate,
220 s->stop_duration = av_rescale(s->stop_duration_opt, inlink->sample_rate,
222 s->stop_silence = av_rescale(s->stop_silence_opt, inlink->sample_rate,
225 s->start_holdoff = av_malloc_array(FFMAX(s->start_duration, 1),
226 sizeof(*s->start_holdoff) *
228 if (!s->start_holdoff)
229 return AVERROR(ENOMEM);
231 s->start_silence_hold = av_malloc_array(FFMAX(s->start_silence, 1),
232 sizeof(*s->start_silence_hold) *
234 if (!s->start_silence_hold)
235 return AVERROR(ENOMEM);
237 s->start_holdoff_offset = 0;
238 s->start_holdoff_end = 0;
239 s->start_found_periods = 0;
241 s->stop_holdoff = av_malloc_array(FFMAX(s->stop_duration, 1),
242 sizeof(*s->stop_holdoff) *
244 if (!s->stop_holdoff)
245 return AVERROR(ENOMEM);
247 s->stop_silence_hold = av_malloc_array(FFMAX(s->stop_silence, 1),
248 sizeof(*s->stop_silence_hold) *
250 if (!s->stop_silence_hold)
251 return AVERROR(ENOMEM);
253 s->stop_holdoff_offset = 0;
254 s->stop_holdoff_end = 0;
255 s->stop_found_periods = 0;
257 if (s->start_periods)
258 s->mode = SILENCE_TRIM;
260 s->mode = SILENCE_COPY;
265 static void flush(SilenceRemoveContext *s,
266 AVFrame *out, AVFilterLink *outlink,
267 int *nb_samples_written, int *ret, int flush_silence)
271 if (*nb_samples_written) {
272 out->nb_samples = *nb_samples_written / outlink->channels;
274 out->pts = s->next_pts;
275 s->next_pts += av_rescale_q(out->nb_samples,
276 (AVRational){1, outlink->sample_rate},
279 *ret = ff_filter_frame(outlink, out);
282 *nb_samples_written = 0;
287 if (s->stop_silence_end <= 0 || !flush_silence)
290 silence = ff_get_audio_buffer(outlink, s->stop_silence_end / outlink->channels);
292 *ret = AVERROR(ENOMEM);
296 if (s->stop_silence_offset < s->stop_silence_end) {
297 memcpy(silence->data[0],
298 &s->stop_silence_hold[s->stop_silence_offset],
299 (s->stop_silence_end - s->stop_silence_offset) * sizeof(double));
302 if (s->stop_silence_offset > 0) {
303 memcpy(silence->data[0] + (s->stop_silence_end - s->stop_silence_offset) * sizeof(double),
304 &s->stop_silence_hold[0],
305 s->stop_silence_offset * sizeof(double));
308 s->stop_silence_offset = 0;
309 s->stop_silence_end = 0;
311 silence->pts = s->next_pts;
312 s->next_pts += av_rescale_q(silence->nb_samples,
313 (AVRational){1, outlink->sample_rate},
316 *ret = ff_filter_frame(outlink, silence);
319 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
321 AVFilterContext *ctx = inlink->dst;
322 AVFilterLink *outlink = ctx->outputs[0];
323 SilenceRemoveContext *s = ctx->priv;
324 int i, j, threshold, ret = 0;
325 int nbs, nb_samples_read, nb_samples_written;
326 double *obuf, *ibuf = (double *)in->data[0];
329 nb_samples_read = nb_samples_written = 0;
334 nbs = in->nb_samples - nb_samples_read / outlink->channels;
338 for (i = 0; i < nbs; i++) {
339 if (s->start_mode == T_ANY) {
341 for (j = 0; j < outlink->channels; j++) {
342 threshold |= s->compute(s, ibuf[j]) > s->start_threshold;
346 for (j = 0; j < outlink->channels; j++) {
347 threshold &= s->compute(s, ibuf[j]) > s->start_threshold;
352 for (j = 0; j < outlink->channels; j++) {
354 s->start_holdoff[s->start_holdoff_end++] = *ibuf++;
356 nb_samples_read += outlink->channels;
358 if (s->start_holdoff_end >= s->start_duration * outlink->channels) {
359 if (++s->start_found_periods >= s->start_periods) {
360 s->mode = SILENCE_TRIM_FLUSH;
361 goto silence_trim_flush;
364 s->start_holdoff_offset = 0;
365 s->start_holdoff_end = 0;
366 s->start_silence_offset = 0;
367 s->start_silence_end = 0;
370 s->start_holdoff_end = 0;
372 for (j = 0; j < outlink->channels; j++) {
373 s->update(s, ibuf[j]);
374 if (s->start_silence) {
375 s->start_silence_hold[s->start_silence_offset++] = ibuf[j];
376 s->start_silence_end = FFMIN(s->start_silence_end + 1, outlink->channels * s->start_silence);
377 if (s->start_silence_offset >= outlink->channels * s->start_silence) {
378 s->start_silence_offset = 0;
383 ibuf += outlink->channels;
384 nb_samples_read += outlink->channels;
389 case SILENCE_TRIM_FLUSH:
391 nbs = s->start_holdoff_end - s->start_holdoff_offset;
392 nbs -= nbs % outlink->channels;
396 out = ff_get_audio_buffer(outlink, nbs / outlink->channels + s->start_silence_end / outlink->channels);
399 return AVERROR(ENOMEM);
402 if (s->start_silence_end > 0) {
403 if (s->start_silence_offset < s->start_silence_end) {
405 &s->start_silence_hold[s->start_silence_offset],
406 (s->start_silence_end - s->start_silence_offset) * sizeof(double));
409 if (s->start_silence_offset > 0) {
410 memcpy(out->data[0] + (s->start_silence_end - s->start_silence_offset) * sizeof(double),
411 &s->start_silence_hold[0],
412 s->start_silence_offset * sizeof(double));
416 memcpy(out->data[0] + s->start_silence_end * sizeof(double),
417 &s->start_holdoff[s->start_holdoff_offset],
418 nbs * sizeof(double));
420 out->pts = s->next_pts;
421 s->next_pts += av_rescale_q(out->nb_samples,
422 (AVRational){1, outlink->sample_rate},
425 s->start_holdoff_offset += nbs;
427 ret = ff_filter_frame(outlink, out);
429 if (s->start_holdoff_offset == s->start_holdoff_end) {
430 s->start_holdoff_offset = 0;
431 s->start_holdoff_end = 0;
432 s->start_silence_offset = 0;
433 s->start_silence_end = 0;
434 s->mode = SILENCE_COPY;
441 nbs = in->nb_samples - nb_samples_read / outlink->channels;
445 out = ff_get_audio_buffer(outlink, nbs);
448 return AVERROR(ENOMEM);
450 obuf = (double *)out->data[0];
452 if (s->stop_periods) {
453 for (i = 0; i < nbs; i++) {
454 if (s->stop_mode == T_ANY) {
456 for (j = 0; j < outlink->channels; j++) {
457 threshold |= s->compute(s, ibuf[j]) > s->stop_threshold;
461 for (j = 0; j < outlink->channels; j++) {
462 threshold &= s->compute(s, ibuf[j]) > s->stop_threshold;
466 if (threshold && s->stop_holdoff_end && !s->stop_silence) {
467 s->mode = SILENCE_COPY_FLUSH;
468 flush(s, out, outlink, &nb_samples_written, &ret, 0);
469 goto silence_copy_flush;
470 } else if (threshold) {
471 for (j = 0; j < outlink->channels; j++) {
475 nb_samples_read += outlink->channels;
476 nb_samples_written += outlink->channels;
477 } else if (!threshold) {
478 for (j = 0; j < outlink->channels; j++) {
480 if (s->stop_silence) {
481 s->stop_silence_hold[s->stop_silence_offset++] = *ibuf;
482 s->stop_silence_end = FFMIN(s->stop_silence_end + 1, outlink->channels * s->stop_silence);
483 if (s->stop_silence_offset >= outlink->channels * s->stop_silence) {
484 s->stop_silence_offset = 0;
488 s->stop_holdoff[s->stop_holdoff_end++] = *ibuf++;
490 nb_samples_read += outlink->channels;
492 if (s->stop_holdoff_end >= s->stop_duration * outlink->channels) {
493 if (++s->stop_found_periods >= s->stop_periods) {
494 s->stop_holdoff_offset = 0;
495 s->stop_holdoff_end = 0;
498 s->mode = SILENCE_STOP;
499 flush(s, out, outlink, &nb_samples_written, &ret, 1);
502 s->stop_found_periods = 0;
503 s->start_found_periods = 0;
504 s->start_holdoff_offset = 0;
505 s->start_holdoff_end = 0;
506 s->start_silence_offset = 0;
507 s->start_silence_end = 0;
509 s->mode = SILENCE_TRIM;
510 flush(s, out, outlink, &nb_samples_written, &ret, 1);
514 s->mode = SILENCE_COPY_FLUSH;
515 flush(s, out, outlink, &nb_samples_written, &ret, 0);
516 goto silence_copy_flush;
520 flush(s, out, outlink, &nb_samples_written, &ret, 0);
522 memcpy(obuf, ibuf, sizeof(double) * nbs * outlink->channels);
524 out->pts = s->next_pts;
525 s->next_pts += av_rescale_q(out->nb_samples,
526 (AVRational){1, outlink->sample_rate},
529 ret = ff_filter_frame(outlink, out);
533 case SILENCE_COPY_FLUSH:
535 nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
536 nbs -= nbs % outlink->channels;
540 out = ff_get_audio_buffer(outlink, nbs / outlink->channels);
543 return AVERROR(ENOMEM);
546 memcpy(out->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
547 nbs * sizeof(double));
548 s->stop_holdoff_offset += nbs;
550 out->pts = s->next_pts;
551 s->next_pts += av_rescale_q(out->nb_samples,
552 (AVRational){1, outlink->sample_rate},
555 ret = ff_filter_frame(outlink, out);
557 if (s->stop_holdoff_offset == s->stop_holdoff_end) {
558 s->stop_holdoff_offset = 0;
559 s->stop_holdoff_end = 0;
560 s->stop_silence_offset = 0;
561 s->stop_silence_end = 0;
562 s->mode = SILENCE_COPY;
576 static int request_frame(AVFilterLink *outlink)
578 AVFilterContext *ctx = outlink->src;
579 SilenceRemoveContext *s = ctx->priv;
582 ret = ff_request_frame(ctx->inputs[0]);
583 if (ret == AVERROR_EOF && (s->mode == SILENCE_COPY_FLUSH ||
584 s->mode == SILENCE_COPY)) {
585 int nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
589 frame = ff_get_audio_buffer(outlink, nbs / outlink->channels);
591 return AVERROR(ENOMEM);
593 memcpy(frame->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
594 nbs * sizeof(double));
596 frame->pts = s->next_pts;
597 s->next_pts += av_rescale_q(frame->nb_samples,
598 (AVRational){1, outlink->sample_rate},
601 ret = ff_filter_frame(outlink, frame);
603 s->mode = SILENCE_STOP;
608 static int query_formats(AVFilterContext *ctx)
610 AVFilterFormats *formats = NULL;
611 AVFilterChannelLayouts *layouts = NULL;
612 static const enum AVSampleFormat sample_fmts[] = {
613 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE
617 layouts = ff_all_channel_counts();
619 return AVERROR(ENOMEM);
620 ret = ff_set_common_channel_layouts(ctx, layouts);
624 formats = ff_make_format_list(sample_fmts);
626 return AVERROR(ENOMEM);
627 ret = ff_set_common_formats(ctx, formats);
631 formats = ff_all_samplerates();
633 return AVERROR(ENOMEM);
634 return ff_set_common_samplerates(ctx, formats);
637 static av_cold void uninit(AVFilterContext *ctx)
639 SilenceRemoveContext *s = ctx->priv;
641 av_freep(&s->start_holdoff);
642 av_freep(&s->start_silence_hold);
643 av_freep(&s->stop_holdoff);
644 av_freep(&s->stop_silence_hold);
645 av_freep(&s->window);
648 static const AVFilterPad silenceremove_inputs[] = {
651 .type = AVMEDIA_TYPE_AUDIO,
652 .config_props = config_input,
653 .filter_frame = filter_frame,
658 static const AVFilterPad silenceremove_outputs[] = {
661 .type = AVMEDIA_TYPE_AUDIO,
662 .request_frame = request_frame,
667 AVFilter ff_af_silenceremove = {
668 .name = "silenceremove",
669 .description = NULL_IF_CONFIG_SMALL("Remove silence."),
670 .priv_size = sizeof(SilenceRemoveContext),
671 .priv_class = &silenceremove_class,
674 .query_formats = query_formats,
675 .inputs = silenceremove_inputs,
676 .outputs = silenceremove_outputs,