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->next_pts = AV_NOPTS_VALUE;
210 s->window_size = FFMAX((inlink->sample_rate * s->window_ratio), 1) * inlink->channels;
211 s->window = av_malloc_array(s->window_size, sizeof(*s->window));
213 return AVERROR(ENOMEM);
217 s->start_duration = av_rescale(s->start_duration_opt, inlink->sample_rate,
219 s->start_silence = av_rescale(s->start_silence_opt, inlink->sample_rate,
221 s->stop_duration = av_rescale(s->stop_duration_opt, inlink->sample_rate,
223 s->stop_silence = av_rescale(s->stop_silence_opt, inlink->sample_rate,
226 s->start_holdoff = av_malloc_array(FFMAX(s->start_duration, 1),
227 sizeof(*s->start_holdoff) *
229 if (!s->start_holdoff)
230 return AVERROR(ENOMEM);
232 s->start_silence_hold = av_malloc_array(FFMAX(s->start_silence, 1),
233 sizeof(*s->start_silence_hold) *
235 if (!s->start_silence_hold)
236 return AVERROR(ENOMEM);
238 s->start_holdoff_offset = 0;
239 s->start_holdoff_end = 0;
240 s->start_found_periods = 0;
242 s->stop_holdoff = av_malloc_array(FFMAX(s->stop_duration, 1),
243 sizeof(*s->stop_holdoff) *
245 if (!s->stop_holdoff)
246 return AVERROR(ENOMEM);
248 s->stop_silence_hold = av_malloc_array(FFMAX(s->stop_silence, 1),
249 sizeof(*s->stop_silence_hold) *
251 if (!s->stop_silence_hold)
252 return AVERROR(ENOMEM);
254 s->stop_holdoff_offset = 0;
255 s->stop_holdoff_end = 0;
256 s->stop_found_periods = 0;
258 if (s->start_periods)
259 s->mode = SILENCE_TRIM;
261 s->mode = SILENCE_COPY;
266 static void flush(SilenceRemoveContext *s,
267 AVFrame *out, AVFilterLink *outlink,
268 int *nb_samples_written, int *ret, int flush_silence)
272 if (*nb_samples_written) {
273 out->nb_samples = *nb_samples_written / outlink->channels;
275 out->pts = s->next_pts;
276 s->next_pts += av_rescale_q(out->nb_samples,
277 (AVRational){1, outlink->sample_rate},
280 *ret = ff_filter_frame(outlink, out);
283 *nb_samples_written = 0;
288 if (s->stop_silence_end <= 0 || !flush_silence)
291 silence = ff_get_audio_buffer(outlink, s->stop_silence_end / outlink->channels);
293 *ret = AVERROR(ENOMEM);
297 if (s->stop_silence_offset < s->stop_silence_end) {
298 memcpy(silence->data[0],
299 &s->stop_silence_hold[s->stop_silence_offset],
300 (s->stop_silence_end - s->stop_silence_offset) * sizeof(double));
303 if (s->stop_silence_offset > 0) {
304 memcpy(silence->data[0] + (s->stop_silence_end - s->stop_silence_offset) * sizeof(double),
305 &s->stop_silence_hold[0],
306 s->stop_silence_offset * sizeof(double));
309 s->stop_silence_offset = 0;
310 s->stop_silence_end = 0;
312 silence->pts = s->next_pts;
313 s->next_pts += av_rescale_q(silence->nb_samples,
314 (AVRational){1, outlink->sample_rate},
317 *ret = ff_filter_frame(outlink, silence);
320 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
322 AVFilterContext *ctx = inlink->dst;
323 AVFilterLink *outlink = ctx->outputs[0];
324 SilenceRemoveContext *s = ctx->priv;
325 int i, j, threshold, ret = 0;
326 int nbs, nb_samples_read, nb_samples_written;
327 double *obuf, *ibuf = (double *)in->data[0];
330 nb_samples_read = nb_samples_written = 0;
332 if (s->next_pts == AV_NOPTS_VALUE)
333 s->next_pts = in->pts;
338 nbs = in->nb_samples - nb_samples_read / outlink->channels;
342 for (i = 0; i < nbs; i++) {
343 if (s->start_mode == T_ANY) {
345 for (j = 0; j < outlink->channels; j++) {
346 threshold |= s->compute(s, ibuf[j]) > s->start_threshold;
350 for (j = 0; j < outlink->channels; j++) {
351 threshold &= s->compute(s, ibuf[j]) > s->start_threshold;
356 for (j = 0; j < outlink->channels; j++) {
358 s->start_holdoff[s->start_holdoff_end++] = *ibuf++;
360 nb_samples_read += outlink->channels;
362 if (s->start_holdoff_end >= s->start_duration * outlink->channels) {
363 if (++s->start_found_periods >= s->start_periods) {
364 s->mode = SILENCE_TRIM_FLUSH;
365 goto silence_trim_flush;
368 s->start_holdoff_offset = 0;
369 s->start_holdoff_end = 0;
370 s->start_silence_offset = 0;
371 s->start_silence_end = 0;
374 s->start_holdoff_end = 0;
376 for (j = 0; j < outlink->channels; j++) {
377 s->update(s, ibuf[j]);
378 if (s->start_silence) {
379 s->start_silence_hold[s->start_silence_offset++] = ibuf[j];
380 s->start_silence_end = FFMIN(s->start_silence_end + 1, outlink->channels * s->start_silence);
381 if (s->start_silence_offset >= outlink->channels * s->start_silence) {
382 s->start_silence_offset = 0;
387 ibuf += outlink->channels;
388 nb_samples_read += outlink->channels;
393 case SILENCE_TRIM_FLUSH:
395 nbs = s->start_holdoff_end - s->start_holdoff_offset;
396 nbs -= nbs % outlink->channels;
400 out = ff_get_audio_buffer(outlink, nbs / outlink->channels + s->start_silence_end / outlink->channels);
403 return AVERROR(ENOMEM);
406 if (s->start_silence_end > 0) {
407 if (s->start_silence_offset < s->start_silence_end) {
409 &s->start_silence_hold[s->start_silence_offset],
410 (s->start_silence_end - s->start_silence_offset) * sizeof(double));
413 if (s->start_silence_offset > 0) {
414 memcpy(out->data[0] + (s->start_silence_end - s->start_silence_offset) * sizeof(double),
415 &s->start_silence_hold[0],
416 s->start_silence_offset * sizeof(double));
420 memcpy(out->data[0] + s->start_silence_end * sizeof(double),
421 &s->start_holdoff[s->start_holdoff_offset],
422 nbs * sizeof(double));
424 out->pts = s->next_pts;
425 s->next_pts += av_rescale_q(out->nb_samples,
426 (AVRational){1, outlink->sample_rate},
429 s->start_holdoff_offset += nbs;
431 ret = ff_filter_frame(outlink, out);
433 if (s->start_holdoff_offset == s->start_holdoff_end) {
434 s->start_holdoff_offset = 0;
435 s->start_holdoff_end = 0;
436 s->start_silence_offset = 0;
437 s->start_silence_end = 0;
438 s->mode = SILENCE_COPY;
445 nbs = in->nb_samples - nb_samples_read / outlink->channels;
449 out = ff_get_audio_buffer(outlink, nbs);
452 return AVERROR(ENOMEM);
454 obuf = (double *)out->data[0];
456 if (s->stop_periods) {
457 for (i = 0; i < nbs; i++) {
458 if (s->stop_mode == T_ANY) {
460 for (j = 0; j < outlink->channels; j++) {
461 threshold |= s->compute(s, ibuf[j]) > s->stop_threshold;
465 for (j = 0; j < outlink->channels; j++) {
466 threshold &= s->compute(s, ibuf[j]) > s->stop_threshold;
470 if (threshold && s->stop_holdoff_end && !s->stop_silence) {
471 s->mode = SILENCE_COPY_FLUSH;
472 flush(s, out, outlink, &nb_samples_written, &ret, 0);
473 goto silence_copy_flush;
474 } else if (threshold) {
475 for (j = 0; j < outlink->channels; j++) {
479 nb_samples_read += outlink->channels;
480 nb_samples_written += outlink->channels;
481 } else if (!threshold) {
482 for (j = 0; j < outlink->channels; j++) {
484 if (s->stop_silence) {
485 s->stop_silence_hold[s->stop_silence_offset++] = *ibuf;
486 s->stop_silence_end = FFMIN(s->stop_silence_end + 1, outlink->channels * s->stop_silence);
487 if (s->stop_silence_offset >= outlink->channels * s->stop_silence) {
488 s->stop_silence_offset = 0;
492 s->stop_holdoff[s->stop_holdoff_end++] = *ibuf++;
494 nb_samples_read += outlink->channels;
496 if (s->stop_holdoff_end >= s->stop_duration * outlink->channels) {
497 if (++s->stop_found_periods >= s->stop_periods) {
498 s->stop_holdoff_offset = 0;
499 s->stop_holdoff_end = 0;
502 s->mode = SILENCE_STOP;
503 flush(s, out, outlink, &nb_samples_written, &ret, 1);
506 s->stop_found_periods = 0;
507 s->start_found_periods = 0;
508 s->start_holdoff_offset = 0;
509 s->start_holdoff_end = 0;
510 s->start_silence_offset = 0;
511 s->start_silence_end = 0;
513 s->mode = SILENCE_TRIM;
514 flush(s, out, outlink, &nb_samples_written, &ret, 1);
518 s->mode = SILENCE_COPY_FLUSH;
519 flush(s, out, outlink, &nb_samples_written, &ret, 0);
520 goto silence_copy_flush;
524 flush(s, out, outlink, &nb_samples_written, &ret, 0);
526 memcpy(obuf, ibuf, sizeof(double) * nbs * outlink->channels);
528 out->pts = s->next_pts;
529 s->next_pts += av_rescale_q(out->nb_samples,
530 (AVRational){1, outlink->sample_rate},
533 ret = ff_filter_frame(outlink, out);
537 case SILENCE_COPY_FLUSH:
539 nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
540 nbs -= nbs % outlink->channels;
544 out = ff_get_audio_buffer(outlink, nbs / outlink->channels);
547 return AVERROR(ENOMEM);
550 memcpy(out->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
551 nbs * sizeof(double));
552 s->stop_holdoff_offset += nbs;
554 out->pts = s->next_pts;
555 s->next_pts += av_rescale_q(out->nb_samples,
556 (AVRational){1, outlink->sample_rate},
559 ret = ff_filter_frame(outlink, out);
561 if (s->stop_holdoff_offset == s->stop_holdoff_end) {
562 s->stop_holdoff_offset = 0;
563 s->stop_holdoff_end = 0;
564 s->stop_silence_offset = 0;
565 s->stop_silence_end = 0;
566 s->mode = SILENCE_COPY;
580 static int request_frame(AVFilterLink *outlink)
582 AVFilterContext *ctx = outlink->src;
583 SilenceRemoveContext *s = ctx->priv;
586 ret = ff_request_frame(ctx->inputs[0]);
587 if (ret == AVERROR_EOF && (s->mode == SILENCE_COPY_FLUSH ||
588 s->mode == SILENCE_COPY)) {
589 int nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
593 frame = ff_get_audio_buffer(outlink, nbs / outlink->channels);
595 return AVERROR(ENOMEM);
597 memcpy(frame->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
598 nbs * sizeof(double));
600 frame->pts = s->next_pts;
601 s->next_pts += av_rescale_q(frame->nb_samples,
602 (AVRational){1, outlink->sample_rate},
605 ret = ff_filter_frame(outlink, frame);
607 s->mode = SILENCE_STOP;
612 static int query_formats(AVFilterContext *ctx)
614 AVFilterFormats *formats = NULL;
615 AVFilterChannelLayouts *layouts = NULL;
616 static const enum AVSampleFormat sample_fmts[] = {
617 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE
621 layouts = ff_all_channel_counts();
623 return AVERROR(ENOMEM);
624 ret = ff_set_common_channel_layouts(ctx, layouts);
628 formats = ff_make_format_list(sample_fmts);
630 return AVERROR(ENOMEM);
631 ret = ff_set_common_formats(ctx, formats);
635 formats = ff_all_samplerates();
637 return AVERROR(ENOMEM);
638 return ff_set_common_samplerates(ctx, formats);
641 static av_cold void uninit(AVFilterContext *ctx)
643 SilenceRemoveContext *s = ctx->priv;
645 av_freep(&s->start_holdoff);
646 av_freep(&s->start_silence_hold);
647 av_freep(&s->stop_holdoff);
648 av_freep(&s->stop_silence_hold);
649 av_freep(&s->window);
652 static const AVFilterPad silenceremove_inputs[] = {
655 .type = AVMEDIA_TYPE_AUDIO,
656 .config_props = config_input,
657 .filter_frame = filter_frame,
662 static const AVFilterPad silenceremove_outputs[] = {
665 .type = AVMEDIA_TYPE_AUDIO,
666 .request_frame = request_frame,
671 AVFilter ff_af_silenceremove = {
672 .name = "silenceremove",
673 .description = NULL_IF_CONFIG_SMALL("Remove silence."),
674 .priv_size = sizeof(SilenceRemoveContext),
675 .priv_class = &silenceremove_class,
678 .query_formats = query_formats,
679 .inputs = silenceremove_inputs,
680 .outputs = silenceremove_outputs,