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 double start_threshold;
51 int64_t stop_duration;
52 double stop_threshold;
54 double *start_holdoff;
55 size_t start_holdoff_offset;
56 size_t start_holdoff_end;
57 int start_found_periods;
60 size_t stop_holdoff_offset;
61 size_t stop_holdoff_end;
62 int stop_found_periods;
66 double *window_current;
76 void (*update)(struct SilenceRemoveContext *s, double sample);
77 double(*compute)(struct SilenceRemoveContext *s, double sample);
78 } SilenceRemoveContext;
80 #define OFFSET(x) offsetof(SilenceRemoveContext, x)
81 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
82 static const AVOption silenceremove_options[] = {
83 { "start_periods", NULL, OFFSET(start_periods), AV_OPT_TYPE_INT, {.i64=0}, 0, 9000, FLAGS },
84 { "start_duration", NULL, OFFSET(start_duration), AV_OPT_TYPE_DURATION, {.i64=0}, 0, 9000, FLAGS },
85 { "start_threshold", NULL, OFFSET(start_threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX, FLAGS },
86 { "stop_periods", NULL, OFFSET(stop_periods), AV_OPT_TYPE_INT, {.i64=0}, -9000, 9000, FLAGS },
87 { "stop_duration", NULL, OFFSET(stop_duration), AV_OPT_TYPE_DURATION, {.i64=0}, 0, 9000, FLAGS },
88 { "stop_threshold", NULL, OFFSET(stop_threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX, FLAGS },
89 { "leave_silence", NULL, OFFSET(leave_silence), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
90 { "detection", NULL, OFFSET(detection), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "detection" },
91 { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "detection" },
92 { "rms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "detection" },
93 { "window", NULL, OFFSET(window_ratio), AV_OPT_TYPE_DOUBLE, {.dbl=0.02}, 0, 10, FLAGS },
97 AVFILTER_DEFINE_CLASS(silenceremove);
99 static double compute_peak(SilenceRemoveContext *s, double sample)
104 new_sum -= *s->window_current;
105 new_sum += fabs(sample);
107 return new_sum / s->window_size;
110 static void update_peak(SilenceRemoveContext *s, double sample)
112 s->sum -= *s->window_current;
113 *s->window_current = fabs(sample);
114 s->sum += *s->window_current;
117 if (s->window_current >= s->window_end)
118 s->window_current = s->window;
121 static double compute_rms(SilenceRemoveContext *s, double sample)
126 new_sum -= *s->window_current;
127 new_sum += sample * sample;
129 return sqrt(new_sum / s->window_size);
132 static void update_rms(SilenceRemoveContext *s, double sample)
134 s->sum -= *s->window_current;
135 *s->window_current = sample * sample;
136 s->sum += *s->window_current;
139 if (s->window_current >= s->window_end)
140 s->window_current = s->window;
143 static av_cold int init(AVFilterContext *ctx)
145 SilenceRemoveContext *s = ctx->priv;
147 if (s->stop_periods < 0) {
148 s->stop_periods = -s->stop_periods;
152 switch (s->detection) {
154 s->update = update_peak;
155 s->compute = compute_peak;
158 s->update = update_rms;
159 s->compute = compute_rms;
166 static void clear_window(SilenceRemoveContext *s)
168 memset(s->window, 0, s->window_size * sizeof(*s->window));
170 s->window_current = s->window;
171 s->window_end = s->window + s->window_size;
175 static int config_input(AVFilterLink *inlink)
177 AVFilterContext *ctx = inlink->dst;
178 SilenceRemoveContext *s = ctx->priv;
180 s->window_size = FFMAX((inlink->sample_rate * s->window_ratio), 1) * inlink->channels;
181 s->window = av_malloc_array(s->window_size, sizeof(*s->window));
183 return AVERROR(ENOMEM);
187 s->start_duration = av_rescale(s->start_duration, inlink->sample_rate,
189 if (s->start_duration < 0) {
190 av_log(ctx, AV_LOG_WARNING, "start duration must be non-negative\n");
191 s->start_duration = -s->start_duration;
194 s->stop_duration = av_rescale(s->stop_duration, inlink->sample_rate,
196 if (s->stop_duration < 0) {
197 av_log(ctx, AV_LOG_WARNING, "stop duration must be non-negative\n");
198 s->stop_duration = -s->stop_duration;
201 s->start_holdoff = av_malloc_array(FFMAX(s->start_duration, 1),
202 sizeof(*s->start_holdoff) *
204 if (!s->start_holdoff)
205 return AVERROR(ENOMEM);
207 s->start_holdoff_offset = 0;
208 s->start_holdoff_end = 0;
209 s->start_found_periods = 0;
211 s->stop_holdoff = av_malloc_array(FFMAX(s->stop_duration, 1),
212 sizeof(*s->stop_holdoff) *
214 if (!s->stop_holdoff)
215 return AVERROR(ENOMEM);
217 s->stop_holdoff_offset = 0;
218 s->stop_holdoff_end = 0;
219 s->stop_found_periods = 0;
221 if (s->start_periods)
222 s->mode = SILENCE_TRIM;
224 s->mode = SILENCE_COPY;
229 static void flush(SilenceRemoveContext *s,
230 AVFrame *out, AVFilterLink *outlink,
231 int *nb_samples_written, int *ret)
233 if (*nb_samples_written) {
234 out->nb_samples = *nb_samples_written / outlink->channels;
236 out->pts = s->next_pts;
237 s->next_pts += av_rescale_q(out->nb_samples,
238 (AVRational){1, outlink->sample_rate},
241 *ret = ff_filter_frame(outlink, out);
242 *nb_samples_written = 0;
248 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
250 AVFilterContext *ctx = inlink->dst;
251 AVFilterLink *outlink = ctx->outputs[0];
252 SilenceRemoveContext *s = ctx->priv;
253 int i, j, threshold, ret = 0;
254 int nbs, nb_samples_read, nb_samples_written;
255 double *obuf, *ibuf = (double *)in->data[0];
258 nb_samples_read = nb_samples_written = 0;
263 nbs = in->nb_samples - nb_samples_read / inlink->channels;
267 for (i = 0; i < nbs; i++) {
269 for (j = 0; j < inlink->channels; j++) {
270 threshold |= s->compute(s, ibuf[j]) > s->start_threshold;
274 for (j = 0; j < inlink->channels; j++) {
276 s->start_holdoff[s->start_holdoff_end++] = *ibuf++;
278 nb_samples_read += inlink->channels;
280 if (s->start_holdoff_end >= s->start_duration * inlink->channels) {
281 if (++s->start_found_periods >= s->start_periods) {
282 s->mode = SILENCE_TRIM_FLUSH;
283 goto silence_trim_flush;
286 s->start_holdoff_offset = 0;
287 s->start_holdoff_end = 0;
290 s->start_holdoff_end = 0;
292 for (j = 0; j < inlink->channels; j++)
293 s->update(s, ibuf[j]);
295 ibuf += inlink->channels;
296 nb_samples_read += inlink->channels;
301 case SILENCE_TRIM_FLUSH:
303 nbs = s->start_holdoff_end - s->start_holdoff_offset;
304 nbs -= nbs % inlink->channels;
308 out = ff_get_audio_buffer(inlink, nbs / inlink->channels);
311 return AVERROR(ENOMEM);
314 memcpy(out->data[0], &s->start_holdoff[s->start_holdoff_offset],
315 nbs * sizeof(double));
317 out->pts = s->next_pts;
318 s->next_pts += av_rescale_q(out->nb_samples,
319 (AVRational){1, outlink->sample_rate},
322 s->start_holdoff_offset += nbs;
324 ret = ff_filter_frame(outlink, out);
326 if (s->start_holdoff_offset == s->start_holdoff_end) {
327 s->start_holdoff_offset = 0;
328 s->start_holdoff_end = 0;
329 s->mode = SILENCE_COPY;
336 nbs = in->nb_samples - nb_samples_read / inlink->channels;
340 out = ff_get_audio_buffer(inlink, nbs);
343 return AVERROR(ENOMEM);
345 obuf = (double *)out->data[0];
347 if (s->stop_periods) {
348 for (i = 0; i < nbs; i++) {
350 for (j = 0; j < inlink->channels; j++)
351 threshold &= s->compute(s, ibuf[j]) > s->stop_threshold;
353 if (threshold && s->stop_holdoff_end && !s->leave_silence) {
354 s->mode = SILENCE_COPY_FLUSH;
355 flush(s, out, outlink, &nb_samples_written, &ret);
356 goto silence_copy_flush;
357 } else if (threshold) {
358 for (j = 0; j < inlink->channels; j++) {
362 nb_samples_read += inlink->channels;
363 nb_samples_written += inlink->channels;
364 } else if (!threshold) {
365 for (j = 0; j < inlink->channels; j++) {
367 if (s->leave_silence) {
369 nb_samples_written++;
372 s->stop_holdoff[s->stop_holdoff_end++] = *ibuf++;
374 nb_samples_read += inlink->channels;
376 if (s->stop_holdoff_end >= s->stop_duration * inlink->channels) {
377 if (++s->stop_found_periods >= s->stop_periods) {
378 s->stop_holdoff_offset = 0;
379 s->stop_holdoff_end = 0;
382 s->mode = SILENCE_STOP;
383 flush(s, out, outlink, &nb_samples_written, &ret);
386 s->stop_found_periods = 0;
387 s->start_found_periods = 0;
388 s->start_holdoff_offset = 0;
389 s->start_holdoff_end = 0;
391 s->mode = SILENCE_TRIM;
392 flush(s, out, outlink, &nb_samples_written, &ret);
396 s->mode = SILENCE_COPY_FLUSH;
397 flush(s, out, outlink, &nb_samples_written, &ret);
398 goto silence_copy_flush;
402 flush(s, out, outlink, &nb_samples_written, &ret);
404 memcpy(obuf, ibuf, sizeof(double) * nbs * inlink->channels);
406 out->pts = s->next_pts;
407 s->next_pts += av_rescale_q(out->nb_samples,
408 (AVRational){1, outlink->sample_rate},
411 ret = ff_filter_frame(outlink, out);
415 case SILENCE_COPY_FLUSH:
417 nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
418 nbs -= nbs % inlink->channels;
422 out = ff_get_audio_buffer(inlink, nbs / inlink->channels);
425 return AVERROR(ENOMEM);
428 memcpy(out->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
429 nbs * sizeof(double));
430 s->stop_holdoff_offset += nbs;
432 out->pts = s->next_pts;
433 s->next_pts += av_rescale_q(out->nb_samples,
434 (AVRational){1, outlink->sample_rate},
437 ret = ff_filter_frame(outlink, out);
439 if (s->stop_holdoff_offset == s->stop_holdoff_end) {
440 s->stop_holdoff_offset = 0;
441 s->stop_holdoff_end = 0;
442 s->mode = SILENCE_COPY;
456 static int request_frame(AVFilterLink *outlink)
458 AVFilterContext *ctx = outlink->src;
459 SilenceRemoveContext *s = ctx->priv;
462 ret = ff_request_frame(ctx->inputs[0]);
463 if (ret == AVERROR_EOF && (s->mode == SILENCE_COPY_FLUSH ||
464 s->mode == SILENCE_COPY)) {
465 int nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
469 frame = ff_get_audio_buffer(outlink, nbs / outlink->channels);
471 return AVERROR(ENOMEM);
473 memcpy(frame->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
474 nbs * sizeof(double));
476 frame->pts = s->next_pts;
477 s->next_pts += av_rescale_q(frame->nb_samples,
478 (AVRational){1, outlink->sample_rate},
481 ret = ff_filter_frame(outlink, frame);
483 s->mode = SILENCE_STOP;
488 static int query_formats(AVFilterContext *ctx)
490 AVFilterFormats *formats = NULL;
491 AVFilterChannelLayouts *layouts = NULL;
492 static const enum AVSampleFormat sample_fmts[] = {
493 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE
497 layouts = ff_all_channel_counts();
499 return AVERROR(ENOMEM);
500 ret = ff_set_common_channel_layouts(ctx, layouts);
504 formats = ff_make_format_list(sample_fmts);
506 return AVERROR(ENOMEM);
507 ret = ff_set_common_formats(ctx, formats);
511 formats = ff_all_samplerates();
513 return AVERROR(ENOMEM);
514 return ff_set_common_samplerates(ctx, formats);
517 static av_cold void uninit(AVFilterContext *ctx)
519 SilenceRemoveContext *s = ctx->priv;
521 av_freep(&s->start_holdoff);
522 av_freep(&s->stop_holdoff);
523 av_freep(&s->window);
526 static const AVFilterPad silenceremove_inputs[] = {
529 .type = AVMEDIA_TYPE_AUDIO,
530 .config_props = config_input,
531 .filter_frame = filter_frame,
536 static const AVFilterPad silenceremove_outputs[] = {
539 .type = AVMEDIA_TYPE_AUDIO,
540 .request_frame = request_frame,
545 AVFilter ff_af_silenceremove = {
546 .name = "silenceremove",
547 .description = NULL_IF_CONFIG_SMALL("Remove silence."),
548 .priv_size = sizeof(SilenceRemoveContext),
549 .priv_class = &silenceremove_class,
552 .query_formats = query_formats,
553 .inputs = silenceremove_inputs,
554 .outputs = silenceremove_outputs,