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"
41 typedef struct SilenceRemoveContext {
44 enum SilenceMode mode;
47 int64_t start_duration;
48 int64_t start_duration_opt;
49 double start_threshold;
50 int64_t start_silence;
51 int64_t start_silence_opt;
54 int64_t stop_duration;
55 int64_t stop_duration_opt;
56 double stop_threshold;
58 int64_t stop_silence_opt;
60 double *start_holdoff;
61 double *start_silence_hold;
62 size_t start_holdoff_offset;
63 size_t start_holdoff_end;
64 size_t start_silence_offset;
65 size_t start_silence_end;
66 int start_found_periods;
69 double *stop_silence_hold;
70 size_t stop_holdoff_offset;
71 size_t stop_holdoff_end;
72 size_t stop_silence_offset;
73 size_t stop_silence_end;
74 int stop_found_periods;
78 double *window_current;
87 void (*update)(struct SilenceRemoveContext *s, double sample);
88 double(*compute)(struct SilenceRemoveContext *s, double sample);
89 } SilenceRemoveContext;
91 #define OFFSET(x) offsetof(SilenceRemoveContext, x)
92 #define AF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
94 static const AVOption silenceremove_options[] = {
95 { "start_periods", NULL, OFFSET(start_periods), AV_OPT_TYPE_INT, {.i64=0}, 0, 9000, AF },
96 { "start_duration", NULL, OFFSET(start_duration_opt), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX, AF },
97 { "start_threshold", NULL, OFFSET(start_threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX, AF },
98 { "start_silence", NULL, OFFSET(start_silence_opt), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX, AF },
99 { "stop_periods", NULL, OFFSET(stop_periods), AV_OPT_TYPE_INT, {.i64=0}, -9000, 9000, AF },
100 { "stop_duration", NULL, OFFSET(stop_duration_opt), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX, AF },
101 { "stop_threshold", NULL, OFFSET(stop_threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX, AF },
102 { "stop_silence", NULL, OFFSET(stop_silence_opt), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX, AF },
103 { "detection", NULL, OFFSET(detection), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AF, "detection" },
104 { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "detection" },
105 { "rms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "detection" },
106 { "window", NULL, OFFSET(window_ratio), AV_OPT_TYPE_DOUBLE, {.dbl=0.02}, 0, 10, AF },
110 AVFILTER_DEFINE_CLASS(silenceremove);
112 static double compute_peak(SilenceRemoveContext *s, double sample)
117 new_sum -= *s->window_current;
118 new_sum += fabs(sample);
120 return new_sum / s->window_size;
123 static void update_peak(SilenceRemoveContext *s, double sample)
125 s->sum -= *s->window_current;
126 *s->window_current = fabs(sample);
127 s->sum += *s->window_current;
130 if (s->window_current >= s->window_end)
131 s->window_current = s->window;
134 static double compute_rms(SilenceRemoveContext *s, double sample)
139 new_sum -= *s->window_current;
140 new_sum += sample * sample;
142 return sqrt(new_sum / s->window_size);
145 static void update_rms(SilenceRemoveContext *s, double sample)
147 s->sum -= *s->window_current;
148 *s->window_current = sample * sample;
149 s->sum += *s->window_current;
152 if (s->window_current >= s->window_end)
153 s->window_current = s->window;
156 static av_cold int init(AVFilterContext *ctx)
158 SilenceRemoveContext *s = ctx->priv;
160 if (s->stop_periods < 0) {
161 s->stop_periods = -s->stop_periods;
165 switch (s->detection) {
167 s->update = update_peak;
168 s->compute = compute_peak;
171 s->update = update_rms;
172 s->compute = compute_rms;
179 static void clear_window(SilenceRemoveContext *s)
181 memset(s->window, 0, s->window_size * sizeof(*s->window));
183 s->window_current = s->window;
184 s->window_end = s->window + s->window_size;
188 static int config_input(AVFilterLink *inlink)
190 AVFilterContext *ctx = inlink->dst;
191 SilenceRemoveContext *s = ctx->priv;
193 s->window_size = FFMAX((inlink->sample_rate * s->window_ratio), 1) * inlink->channels;
194 s->window = av_malloc_array(s->window_size, sizeof(*s->window));
196 return AVERROR(ENOMEM);
200 s->start_duration = av_rescale(s->start_duration_opt, inlink->sample_rate,
202 s->start_silence = av_rescale(s->start_silence_opt, inlink->sample_rate,
204 s->stop_duration = av_rescale(s->stop_duration_opt, inlink->sample_rate,
206 s->stop_silence = av_rescale(s->stop_silence_opt, inlink->sample_rate,
209 s->start_holdoff = av_malloc_array(FFMAX(s->start_duration, 1),
210 sizeof(*s->start_holdoff) *
212 if (!s->start_holdoff)
213 return AVERROR(ENOMEM);
215 s->start_silence_hold = av_malloc_array(FFMAX(s->start_silence, 1),
216 sizeof(*s->start_silence_hold) *
218 if (!s->start_silence_hold)
219 return AVERROR(ENOMEM);
221 s->start_holdoff_offset = 0;
222 s->start_holdoff_end = 0;
223 s->start_found_periods = 0;
225 s->stop_holdoff = av_malloc_array(FFMAX(s->stop_duration, 1),
226 sizeof(*s->stop_holdoff) *
228 if (!s->stop_holdoff)
229 return AVERROR(ENOMEM);
231 s->stop_silence_hold = av_malloc_array(FFMAX(s->stop_silence, 1),
232 sizeof(*s->stop_silence_hold) *
234 if (!s->stop_silence_hold)
235 return AVERROR(ENOMEM);
237 s->stop_holdoff_offset = 0;
238 s->stop_holdoff_end = 0;
239 s->stop_found_periods = 0;
241 if (s->start_periods)
242 s->mode = SILENCE_TRIM;
244 s->mode = SILENCE_COPY;
249 static void flush(SilenceRemoveContext *s,
250 AVFrame *out, AVFilterLink *outlink,
251 int *nb_samples_written, int *ret, int flush_silence)
255 if (*nb_samples_written) {
256 out->nb_samples = *nb_samples_written / outlink->channels;
258 out->pts = s->next_pts;
259 s->next_pts += av_rescale_q(out->nb_samples,
260 (AVRational){1, outlink->sample_rate},
263 *ret = ff_filter_frame(outlink, out);
266 *nb_samples_written = 0;
271 if (s->stop_silence_end <= 0 || !flush_silence)
274 silence = ff_get_audio_buffer(outlink, s->stop_silence_end / outlink->channels);
276 *ret = AVERROR(ENOMEM);
280 if (s->stop_silence_offset < s->stop_silence_end) {
281 memcpy(silence->data[0],
282 &s->stop_silence_hold[s->stop_silence_offset],
283 (s->stop_silence_end - s->stop_silence_offset) * sizeof(double));
286 if (s->stop_silence_offset > 0) {
287 memcpy(silence->data[0] + (s->stop_silence_end - s->stop_silence_offset) * sizeof(double),
288 &s->stop_silence_hold[0],
289 s->stop_silence_offset * sizeof(double));
292 s->stop_silence_offset = 0;
293 s->stop_silence_end = 0;
295 silence->pts = s->next_pts;
296 s->next_pts += av_rescale_q(silence->nb_samples,
297 (AVRational){1, outlink->sample_rate},
300 *ret = ff_filter_frame(outlink, silence);
303 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
305 AVFilterContext *ctx = inlink->dst;
306 AVFilterLink *outlink = ctx->outputs[0];
307 SilenceRemoveContext *s = ctx->priv;
308 int i, j, threshold, ret = 0;
309 int nbs, nb_samples_read, nb_samples_written;
310 double *obuf, *ibuf = (double *)in->data[0];
313 nb_samples_read = nb_samples_written = 0;
318 nbs = in->nb_samples - nb_samples_read / inlink->channels;
322 for (i = 0; i < nbs; i++) {
324 for (j = 0; j < inlink->channels; j++) {
325 threshold |= s->compute(s, ibuf[j]) > s->start_threshold;
329 for (j = 0; j < inlink->channels; j++) {
331 s->start_holdoff[s->start_holdoff_end++] = *ibuf++;
333 nb_samples_read += inlink->channels;
335 if (s->start_holdoff_end >= s->start_duration * inlink->channels) {
336 if (++s->start_found_periods >= s->start_periods) {
337 s->mode = SILENCE_TRIM_FLUSH;
338 goto silence_trim_flush;
341 s->start_holdoff_offset = 0;
342 s->start_holdoff_end = 0;
343 s->start_silence_offset = 0;
344 s->start_silence_end = 0;
347 s->start_holdoff_end = 0;
349 for (j = 0; j < inlink->channels; j++) {
350 s->update(s, ibuf[j]);
351 if (s->start_silence) {
352 s->start_silence_hold[s->start_silence_offset++] = ibuf[j];
353 s->start_silence_end = FFMIN(s->start_silence_end + 1, inlink->channels * s->start_silence);
354 if (s->start_silence_offset >= inlink->channels * s->start_silence) {
355 s->start_silence_offset = 0;
360 ibuf += inlink->channels;
361 nb_samples_read += inlink->channels;
366 case SILENCE_TRIM_FLUSH:
368 nbs = s->start_holdoff_end - s->start_holdoff_offset;
369 nbs -= nbs % inlink->channels;
373 out = ff_get_audio_buffer(inlink, nbs / inlink->channels + s->start_silence_end / inlink->channels);
376 return AVERROR(ENOMEM);
379 if (s->start_silence_end > 0) {
380 if (s->start_silence_offset < s->start_silence_end) {
382 &s->start_silence_hold[s->start_silence_offset],
383 (s->start_silence_end - s->start_silence_offset) * sizeof(double));
386 if (s->start_silence_offset > 0) {
387 memcpy(out->data[0] + (s->start_silence_end - s->start_silence_offset) * sizeof(double),
388 &s->start_silence_hold[0],
389 s->start_silence_offset * sizeof(double));
393 memcpy(out->data[0] + s->start_silence_end * sizeof(double),
394 &s->start_holdoff[s->start_holdoff_offset],
395 nbs * sizeof(double));
397 out->pts = s->next_pts;
398 s->next_pts += av_rescale_q(out->nb_samples,
399 (AVRational){1, outlink->sample_rate},
402 s->start_holdoff_offset += nbs;
404 ret = ff_filter_frame(outlink, out);
406 if (s->start_holdoff_offset == s->start_holdoff_end) {
407 s->start_holdoff_offset = 0;
408 s->start_holdoff_end = 0;
409 s->start_silence_offset = 0;
410 s->start_silence_end = 0;
411 s->mode = SILENCE_COPY;
418 nbs = in->nb_samples - nb_samples_read / inlink->channels;
422 out = ff_get_audio_buffer(inlink, nbs);
425 return AVERROR(ENOMEM);
427 obuf = (double *)out->data[0];
429 if (s->stop_periods) {
430 for (i = 0; i < nbs; i++) {
432 for (j = 0; j < inlink->channels; j++)
433 threshold &= s->compute(s, ibuf[j]) > s->stop_threshold;
435 if (threshold && s->stop_holdoff_end && !s->stop_silence) {
436 s->mode = SILENCE_COPY_FLUSH;
437 flush(s, out, outlink, &nb_samples_written, &ret, 0);
438 goto silence_copy_flush;
439 } else if (threshold) {
440 for (j = 0; j < inlink->channels; j++) {
444 nb_samples_read += inlink->channels;
445 nb_samples_written += inlink->channels;
446 } else if (!threshold) {
447 for (j = 0; j < inlink->channels; j++) {
449 if (s->stop_silence) {
450 s->stop_silence_hold[s->stop_silence_offset++] = *ibuf;
451 s->stop_silence_end = FFMIN(s->stop_silence_end + 1, inlink->channels * s->stop_silence);
452 if (s->stop_silence_offset >= inlink->channels * s->stop_silence) {
453 s->stop_silence_offset = 0;
457 s->stop_holdoff[s->stop_holdoff_end++] = *ibuf++;
459 nb_samples_read += inlink->channels;
461 if (s->stop_holdoff_end >= s->stop_duration * inlink->channels) {
462 if (++s->stop_found_periods >= s->stop_periods) {
463 s->stop_holdoff_offset = 0;
464 s->stop_holdoff_end = 0;
467 s->mode = SILENCE_STOP;
468 flush(s, out, outlink, &nb_samples_written, &ret, 1);
471 s->stop_found_periods = 0;
472 s->start_found_periods = 0;
473 s->start_holdoff_offset = 0;
474 s->start_holdoff_end = 0;
475 s->start_silence_offset = 0;
476 s->start_silence_end = 0;
478 s->mode = SILENCE_TRIM;
479 flush(s, out, outlink, &nb_samples_written, &ret, 1);
483 s->mode = SILENCE_COPY_FLUSH;
484 flush(s, out, outlink, &nb_samples_written, &ret, 0);
485 goto silence_copy_flush;
489 flush(s, out, outlink, &nb_samples_written, &ret, 0);
491 memcpy(obuf, ibuf, sizeof(double) * nbs * inlink->channels);
493 out->pts = s->next_pts;
494 s->next_pts += av_rescale_q(out->nb_samples,
495 (AVRational){1, outlink->sample_rate},
498 ret = ff_filter_frame(outlink, out);
502 case SILENCE_COPY_FLUSH:
504 nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
505 nbs -= nbs % inlink->channels;
509 out = ff_get_audio_buffer(inlink, nbs / inlink->channels);
512 return AVERROR(ENOMEM);
515 memcpy(out->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
516 nbs * sizeof(double));
517 s->stop_holdoff_offset += nbs;
519 out->pts = s->next_pts;
520 s->next_pts += av_rescale_q(out->nb_samples,
521 (AVRational){1, outlink->sample_rate},
524 ret = ff_filter_frame(outlink, out);
526 if (s->stop_holdoff_offset == s->stop_holdoff_end) {
527 s->stop_holdoff_offset = 0;
528 s->stop_holdoff_end = 0;
529 s->stop_silence_offset = 0;
530 s->stop_silence_end = 0;
531 s->mode = SILENCE_COPY;
545 static int request_frame(AVFilterLink *outlink)
547 AVFilterContext *ctx = outlink->src;
548 SilenceRemoveContext *s = ctx->priv;
551 ret = ff_request_frame(ctx->inputs[0]);
552 if (ret == AVERROR_EOF && (s->mode == SILENCE_COPY_FLUSH ||
553 s->mode == SILENCE_COPY)) {
554 int nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
558 frame = ff_get_audio_buffer(outlink, nbs / outlink->channels);
560 return AVERROR(ENOMEM);
562 memcpy(frame->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
563 nbs * sizeof(double));
565 frame->pts = s->next_pts;
566 s->next_pts += av_rescale_q(frame->nb_samples,
567 (AVRational){1, outlink->sample_rate},
570 ret = ff_filter_frame(outlink, frame);
572 s->mode = SILENCE_STOP;
577 static int query_formats(AVFilterContext *ctx)
579 AVFilterFormats *formats = NULL;
580 AVFilterChannelLayouts *layouts = NULL;
581 static const enum AVSampleFormat sample_fmts[] = {
582 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE
586 layouts = ff_all_channel_counts();
588 return AVERROR(ENOMEM);
589 ret = ff_set_common_channel_layouts(ctx, layouts);
593 formats = ff_make_format_list(sample_fmts);
595 return AVERROR(ENOMEM);
596 ret = ff_set_common_formats(ctx, formats);
600 formats = ff_all_samplerates();
602 return AVERROR(ENOMEM);
603 return ff_set_common_samplerates(ctx, formats);
606 static av_cold void uninit(AVFilterContext *ctx)
608 SilenceRemoveContext *s = ctx->priv;
610 av_freep(&s->start_holdoff);
611 av_freep(&s->start_silence_hold);
612 av_freep(&s->stop_holdoff);
613 av_freep(&s->stop_silence_hold);
614 av_freep(&s->window);
617 static const AVFilterPad silenceremove_inputs[] = {
620 .type = AVMEDIA_TYPE_AUDIO,
621 .config_props = config_input,
622 .filter_frame = filter_frame,
627 static const AVFilterPad silenceremove_outputs[] = {
630 .type = AVMEDIA_TYPE_AUDIO,
631 .request_frame = request_frame,
636 AVFilter ff_af_silenceremove = {
637 .name = "silenceremove",
638 .description = NULL_IF_CONFIG_SMALL("Remove silence."),
639 .priv_size = sizeof(SilenceRemoveContext),
640 .priv_class = &silenceremove_class,
643 .query_formats = query_formats,
644 .inputs = silenceremove_inputs,
645 .outputs = silenceremove_outputs,