2 * Copyright (c) 2012 Clément Bœsch <ubitux@gmail.com>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Audio silence detector
26 #include "libavutil/audioconvert.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/timestamp.h"
36 char *noise_str; ///< noise option string
37 double noise; ///< noise amplitude ratio
38 double duration; ///< minimum duration of silence until notification
39 int64_t nb_null_samples; ///< current number of continuous zero samples
40 int64_t start; ///< if silence is detected, this value contains the time of the first zero sample
41 int last_sample_rate; ///< last sample rate to check for sample rate changes
42 } SilenceDetectContext;
44 #define OFFSET(x) offsetof(SilenceDetectContext, x)
45 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
46 static const AVOption silencedetect_options[] = {
47 { "n", "set noise tolerance", OFFSET(noise_str), AV_OPT_TYPE_STRING, {.str="-60dB"}, CHAR_MIN, CHAR_MAX, FLAGS },
48 { "noise", "set noise tolerance", OFFSET(noise_str), AV_OPT_TYPE_STRING, {.str="-60dB"}, CHAR_MIN, CHAR_MAX, FLAGS },
49 { "d", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, FLAGS },
50 { "duration", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, FLAGS },
54 AVFILTER_DEFINE_CLASS(silencedetect);
56 static av_cold int init(AVFilterContext *ctx, const char *args)
60 SilenceDetectContext *silence = ctx->priv;
62 silence->class = &silencedetect_class;
63 av_opt_set_defaults(silence);
65 if ((ret = av_set_options_string(silence, args, "=", ":")) < 0)
68 silence->noise = strtod(silence->noise_str, &tail);
69 if (!strcmp(tail, "dB")) {
70 silence->noise = pow(10, silence->noise/20);
72 av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for noise parameter.\n",
74 return AVERROR(EINVAL);
81 static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
84 SilenceDetectContext *silence = inlink->dst->priv;
85 const int nb_channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
86 const int srate = inlink->sample_rate;
87 const int nb_samples = insamples->audio->nb_samples * nb_channels;
88 const int64_t nb_samples_notify = srate * silence->duration * nb_channels;
90 // scale number of null samples to the new sample rate
91 if (silence->last_sample_rate && silence->last_sample_rate != srate)
92 silence->nb_null_samples =
93 srate * silence->nb_null_samples / silence->last_sample_rate;
94 silence->last_sample_rate = srate;
96 // TODO: support more sample formats
97 if (insamples->format == AV_SAMPLE_FMT_DBL) {
98 double *p = (double *)insamples->data[0];
100 for (i = 0; i < nb_samples; i++, p++) {
101 if (*p < silence->noise && *p > -silence->noise) {
102 if (!silence->start) {
103 silence->nb_null_samples++;
104 if (silence->nb_null_samples >= nb_samples_notify) {
105 silence->start = insamples->pts - (int64_t)(silence->duration / av_q2d(inlink->time_base) + .5);
106 av_log(silence, AV_LOG_INFO,
107 "silence_start: %s\n", av_ts2timestr(silence->start, &inlink->time_base));
112 av_log(silence, AV_LOG_INFO,
113 "silence_end: %s | silence_duration: %s\n",
114 av_ts2timestr(insamples->pts, &inlink->time_base),
115 av_ts2timestr(insamples->pts - silence->start, &inlink->time_base));
116 silence->nb_null_samples = silence->start = 0;
121 return ff_filter_samples(inlink->dst->outputs[0], insamples);
124 static int query_formats(AVFilterContext *ctx)
126 AVFilterFormats *formats = NULL;
127 AVFilterChannelLayouts *layouts = NULL;
128 enum AVSampleFormat sample_fmts[] = {
133 layouts = ff_all_channel_layouts();
135 return AVERROR(ENOMEM);
136 ff_set_common_channel_layouts(ctx, layouts);
138 formats = ff_make_format_list(sample_fmts);
140 return AVERROR(ENOMEM);
141 ff_set_common_formats(ctx, formats);
143 formats = ff_all_samplerates();
145 return AVERROR(ENOMEM);
146 ff_set_common_samplerates(ctx, formats);
151 AVFilter avfilter_af_silencedetect = {
152 .name = "silencedetect",
153 .description = NULL_IF_CONFIG_SMALL("Detect silence."),
154 .priv_size = sizeof(SilenceDetectContext),
156 .query_formats = query_formats,
158 .inputs = (const AVFilterPad[]) {
160 .type = AVMEDIA_TYPE_AUDIO,
161 .get_audio_buffer = ff_null_get_audio_buffer,
162 .filter_samples = filter_samples, },
165 .outputs = (const AVFilterPad[]) {
167 .type = AVMEDIA_TYPE_AUDIO, },
170 .priv_class = &silencedetect_class,