2 * Copyright (c) 2012 Clément Bœsch <u pkh me>
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 <float.h> /* DBL_MAX */
28 #include "libavutil/opt.h"
29 #include "libavutil/timestamp.h"
35 typedef struct SilenceDetectContext {
37 double noise; ///< noise amplitude ratio
38 double duration; ///< minimum duration of silence until notification
39 int mono; ///< mono mode : check each channel separately (default = check when ALL channels are silent)
40 int channels; ///< number of channels
41 int independent_channels; ///< number of entries in following arrays (always 1 in mono mode)
42 int64_t *nb_null_samples; ///< (array) current number of continuous zero samples
43 int64_t *start; ///< (array) if silence is detected, this value contains the time of the first zero sample (default/unset = INT64_MIN)
44 int64_t frame_end; ///< pts of the end of the current frame (used to compute duration of silence at EOS)
45 int last_sample_rate; ///< last sample rate to check for sample rate changes
46 AVRational time_base; ///< time_base
48 void (*silencedetect)(struct SilenceDetectContext *s, AVFrame *insamples,
49 int nb_samples, int64_t nb_samples_notify,
50 AVRational time_base);
51 } SilenceDetectContext;
53 #define OFFSET(x) offsetof(SilenceDetectContext, x)
54 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
55 static const AVOption silencedetect_options[] = {
56 { "n", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
57 { "noise", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
58 { "d", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, FLAGS },
59 { "duration", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, FLAGS },
60 { "mono", "check each channel separately", OFFSET(mono), AV_OPT_TYPE_BOOL, {.i64=0.}, 0, 1, FLAGS },
61 { "m", "check each channel separately", OFFSET(mono), AV_OPT_TYPE_BOOL, {.i64=0.}, 0, 1, FLAGS },
65 AVFILTER_DEFINE_CLASS(silencedetect);
67 static void set_meta(AVFrame *insamples, int channel, const char *key, char *value)
72 snprintf(key2, sizeof(key2), "lavfi.%s.%d", key, channel);
74 snprintf(key2, sizeof(key2), "lavfi.%s", key);
75 av_dict_set(&insamples->metadata, key2, value, 0);
77 static av_always_inline void update(SilenceDetectContext *s, AVFrame *insamples,
78 int is_silence, int current_sample, int64_t nb_samples_notify,
81 int channel = current_sample % s->independent_channels;
83 if (s->start[channel] == INT64_MIN) {
84 s->nb_null_samples[channel]++;
85 if (s->nb_null_samples[channel] >= nb_samples_notify) {
86 s->start[channel] = insamples->pts + av_rescale_q(current_sample / s->channels + 1 - nb_samples_notify * s->independent_channels / s->channels,
87 (AVRational){ 1, s->last_sample_rate }, time_base);
88 set_meta(insamples, s->mono ? channel + 1 : 0, "silence_start",
89 av_ts2timestr(s->start[channel], &time_base));
91 av_log(s, AV_LOG_INFO, "channel: %d | ", channel);
92 av_log(s, AV_LOG_INFO, "silence_start: %s\n",
93 av_ts2timestr(s->start[channel], &time_base));
97 if (s->start[channel] > INT64_MIN) {
98 int64_t end_pts = insamples ? insamples->pts + av_rescale_q(current_sample / s->channels,
99 (AVRational){ 1, s->last_sample_rate }, time_base)
101 int64_t duration_ts = end_pts - s->start[channel];
103 set_meta(insamples, s->mono ? channel + 1 : 0, "silence_end",
104 av_ts2timestr(end_pts, &time_base));
105 set_meta(insamples, s->mono ? channel + 1 : 0, "silence_duration",
106 av_ts2timestr(duration_ts, &time_base));
109 av_log(s, AV_LOG_INFO, "channel: %d | ", channel);
110 av_log(s, AV_LOG_INFO, "silence_end: %s | silence_duration: %s\n",
111 av_ts2timestr(end_pts, &time_base),
112 av_ts2timestr(duration_ts, &time_base));
114 s->nb_null_samples[channel] = 0;
115 s->start[channel] = INT64_MIN;
119 #define SILENCE_DETECT(name, type) \
120 static void silencedetect_##name(SilenceDetectContext *s, AVFrame *insamples, \
121 int nb_samples, int64_t nb_samples_notify, \
122 AVRational time_base) \
124 const type *p = (const type *)insamples->data[0]; \
125 const type noise = s->noise; \
128 for (i = 0; i < nb_samples; i++, p++) \
129 update(s, insamples, *p < noise && *p > -noise, i, \
130 nb_samples_notify, time_base); \
133 SILENCE_DETECT(dbl, double)
134 SILENCE_DETECT(flt, float)
135 SILENCE_DETECT(s32, int32_t)
136 SILENCE_DETECT(s16, int16_t)
138 static int config_input(AVFilterLink *inlink)
140 AVFilterContext *ctx = inlink->dst;
141 SilenceDetectContext *s = ctx->priv;
144 s->channels = inlink->channels;
145 s->independent_channels = s->mono ? s->channels : 1;
146 s->nb_null_samples = av_mallocz_array(sizeof(*s->nb_null_samples), s->independent_channels);
147 if (!s->nb_null_samples)
148 return AVERROR(ENOMEM);
149 s->start = av_malloc_array(sizeof(*s->start), s->independent_channels);
151 return AVERROR(ENOMEM);
152 for (c = 0; c < s->independent_channels; c++)
153 s->start[c] = INT64_MIN;
155 switch (inlink->format) {
156 case AV_SAMPLE_FMT_DBL: s->silencedetect = silencedetect_dbl; break;
157 case AV_SAMPLE_FMT_FLT: s->silencedetect = silencedetect_flt; break;
158 case AV_SAMPLE_FMT_S32:
159 s->noise *= INT32_MAX;
160 s->silencedetect = silencedetect_s32;
162 case AV_SAMPLE_FMT_S16:
163 s->noise *= INT16_MAX;
164 s->silencedetect = silencedetect_s16;
171 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
173 SilenceDetectContext *s = inlink->dst->priv;
174 const int nb_channels = inlink->channels;
175 const int srate = inlink->sample_rate;
176 const int nb_samples = insamples->nb_samples * nb_channels;
177 const int64_t nb_samples_notify = srate * s->duration * (s->mono ? 1 : nb_channels);
180 // scale number of null samples to the new sample rate
181 if (s->last_sample_rate && s->last_sample_rate != srate)
182 for (c = 0; c < s->independent_channels; c++) {
183 s->nb_null_samples[c] = srate * s->nb_null_samples[c] / s->last_sample_rate;
185 s->last_sample_rate = srate;
186 s->time_base = inlink->time_base;
187 s->frame_end = insamples->pts + av_rescale_q(insamples->nb_samples,
188 (AVRational){ 1, s->last_sample_rate }, inlink->time_base);
190 // TODO: document metadata
191 s->silencedetect(s, insamples, nb_samples, nb_samples_notify,
194 return ff_filter_frame(inlink->dst->outputs[0], insamples);
197 static int query_formats(AVFilterContext *ctx)
199 AVFilterFormats *formats = NULL;
200 AVFilterChannelLayouts *layouts = NULL;
201 static const enum AVSampleFormat sample_fmts[] = {
210 layouts = ff_all_channel_layouts();
212 return AVERROR(ENOMEM);
213 ret = ff_set_common_channel_layouts(ctx, layouts);
217 formats = ff_make_format_list(sample_fmts);
219 return AVERROR(ENOMEM);
220 ret = ff_set_common_formats(ctx, formats);
224 formats = ff_all_samplerates();
226 return AVERROR(ENOMEM);
227 return ff_set_common_samplerates(ctx, formats);
230 static av_cold void uninit(AVFilterContext *ctx)
232 SilenceDetectContext *s = ctx->priv;
235 for (c = 0; c < s->independent_channels; c++)
236 if (s->start[c] > INT64_MIN)
237 update(s, NULL, 0, c, 0, s->time_base);
238 av_freep(&s->nb_null_samples);
242 static const AVFilterPad silencedetect_inputs[] = {
245 .type = AVMEDIA_TYPE_AUDIO,
246 .config_props = config_input,
247 .filter_frame = filter_frame,
252 static const AVFilterPad silencedetect_outputs[] = {
255 .type = AVMEDIA_TYPE_AUDIO,
260 AVFilter ff_af_silencedetect = {
261 .name = "silencedetect",
262 .description = NULL_IF_CONFIG_SMALL("Detect silence."),
263 .priv_size = sizeof(SilenceDetectContext),
264 .query_formats = query_formats,
266 .inputs = silencedetect_inputs,
267 .outputs = silencedetect_outputs,
268 .priv_class = &silencedetect_class,