Browse code

lavfi: add audio silencedetect filter.

Clément Bœsch authored on 2012/01/04 07:47:09
Showing 6 changed files
... ...
@@ -20,6 +20,7 @@ version next:
20 20
 - v308 Quicktime Uncompressed 4:4:4 encoder and decoder
21 21
 - yuv4 libquicktime packed 4:2:0 encoder and decoder
22 22
 - ffprobe -show_frames option
23
+- silencedetect audio filter
23 24
 
24 25
 
25 26
 version 0.9:
... ...
@@ -358,6 +358,36 @@ Note that @command{ffmpeg} integrates a default down-mix (and up-mix) system
358 358
 that should be preferred (see "-ac" option) unless you have very specific
359 359
 needs.
360 360
 
361
+@section silencedetect
362
+
363
+Detect silence in an audio stream.
364
+
365
+This filter logs a message when it detects that the input audio volume is less
366
+or equal to a noise tolerance value for a duration greater or equal to the
367
+minimum detected noise duration.
368
+
369
+The printed times and duration are expressed in seconds.
370
+
371
+@table @option
372
+@item duration, d
373
+Set silence duration until notification (default is 2 seconds).
374
+
375
+@item noise, n
376
+Set noise tolerance. Can be specified in dB (in case "dB" is appended to the
377
+specified value) or amplitude ratio. Default is -60dB, or 0.001.
378
+@end table
379
+
380
+Detect 5 seconds of silence with -50dB noise tolerance:
381
+@example
382
+silencedetect=n=-50dB:d=5
383
+@end example
384
+
385
+Complete example with @command{ffmpeg} to detect silence with 0.0001 noise
386
+tolerance in @file{silence.mp3}:
387
+@example
388
+ffmpeg -f lavfi -i amovie=silence.mp3,silencedetect=noise=0.0001 -f null -
389
+@end example
390
+
361 391
 @section volume
362 392
 
363 393
 Adjust the input audio volume.
... ...
@@ -34,6 +34,7 @@ OBJS-$(CONFIG_ASPLIT_FILTER)                 += af_asplit.o
34 34
 OBJS-$(CONFIG_ASTREAMSYNC_FILTER)            += af_astreamsync.o
35 35
 OBJS-$(CONFIG_EARWAX_FILTER)                 += af_earwax.o
36 36
 OBJS-$(CONFIG_PAN_FILTER)                    += af_pan.o
37
+OBJS-$(CONFIG_SILENCEDETECT_FILTER)          += af_silencedetect.o
37 38
 OBJS-$(CONFIG_VOLUME_FILTER)                 += af_volume.o
38 39
 
39 40
 OBJS-$(CONFIG_ABUFFER_FILTER)                += asrc_abuffer.o
40 41
new file mode 100644
... ...
@@ -0,0 +1,175 @@
0
+/*
1
+ * Copyright (c) 2012 Clément Bœsch <ubitux@gmail.com>
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * Audio silence detector
23
+ */
24
+
25
+#include "libavutil/opt.h"
26
+#include "avfilter.h"
27
+
28
+typedef struct {
29
+    const AVClass *class;
30
+    char *noise_str;            ///< noise option string
31
+    double noise;               ///< noise amplitude ratio
32
+    int duration;               ///< minimum duration of silence until notification
33
+    int64_t nb_null_samples;    ///< current number of continuous zero samples
34
+    double start;               ///< if silence is detected, this value contains the time of the first zero sample
35
+    int last_sample_rate;       ///< last sample rate to check for sample rate changes
36
+} SilenceDetectContext;
37
+
38
+#define OFFSET(x) offsetof(SilenceDetectContext, x)
39
+static const AVOption silencedetect_options[] = {
40
+    { "n",         "set noise tolerance",              OFFSET(noise_str), AV_OPT_TYPE_STRING, {.str="-60dB"}, CHAR_MIN, CHAR_MAX },
41
+    { "noise",     "set noise tolerance",              OFFSET(noise_str), AV_OPT_TYPE_STRING, {.str="-60dB"}, CHAR_MIN, CHAR_MAX },
42
+    { "d",         "set minimum duration in seconds",  OFFSET(duration),  AV_OPT_TYPE_INT,    {.dbl=2},    0, INT_MAX},
43
+    { "duration",  "set minimum duration in seconds",  OFFSET(duration),  AV_OPT_TYPE_INT,    {.dbl=2},    0, INT_MAX},
44
+    { NULL },
45
+};
46
+
47
+static const char *silencedetect_get_name(void *ctx)
48
+{
49
+    return "silencedetect";
50
+}
51
+
52
+static const AVClass silencedetect_class = {
53
+    .class_name = "SilenceDetectContext",
54
+    .item_name  = silencedetect_get_name,
55
+    .option     = silencedetect_options,
56
+};
57
+
58
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
59
+{
60
+    int ret;
61
+    char *tail;
62
+    SilenceDetectContext *silence = ctx->priv;
63
+
64
+    silence->class = &silencedetect_class;
65
+    av_opt_set_defaults(silence);
66
+
67
+    if ((ret = av_set_options_string(silence, args, "=", ":")) < 0) {
68
+        av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
69
+        return ret;
70
+    }
71
+
72
+    silence->noise = strtod(silence->noise_str, &tail);
73
+    if (!strcmp(tail, "dB")) {
74
+        silence->noise = pow(10, silence->noise/20);
75
+    } else if (*tail) {
76
+        av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for noise parameter.\n",
77
+               silence->noise_str);
78
+        return AVERROR(EINVAL);
79
+    }
80
+
81
+    return 0;
82
+}
83
+
84
+static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
85
+{
86
+    int i;
87
+    SilenceDetectContext *silence = inlink->dst->priv;
88
+    const int nb_channels           = av_get_channel_layout_nb_channels(inlink->channel_layout);
89
+    const int srate                 = inlink->sample_rate;
90
+    const int nb_samples            = insamples->audio->nb_samples * nb_channels;
91
+    const int64_t nb_samples_notify = srate * silence->duration    * nb_channels;
92
+
93
+    // scale number of null samples to the new sample rate
94
+    if (silence->last_sample_rate && silence->last_sample_rate != srate)
95
+        silence->nb_null_samples =
96
+            srate * silence->nb_null_samples / silence->last_sample_rate;
97
+    silence->last_sample_rate = srate;
98
+
99
+    // TODO: support more sample formats
100
+    if (insamples->format == AV_SAMPLE_FMT_DBL) {
101
+        double *p = (double *)insamples->data[0];
102
+
103
+        for (i = 0; i < nb_samples; i++, p++) {
104
+            if (*p < silence->noise && *p > -silence->noise) {
105
+                if (!silence->start) {
106
+                    silence->nb_null_samples++;
107
+                    if (silence->nb_null_samples >= nb_samples_notify) {
108
+                        silence->start = insamples->pts * av_q2d(inlink->time_base) - silence->duration;
109
+                        av_log(silence, AV_LOG_INFO,
110
+                               "silence_start: %f\n", silence->start);
111
+                    }
112
+                }
113
+            } else {
114
+                if (silence->start) {
115
+                    double end = insamples->pts * av_q2d(inlink->time_base);
116
+                    av_log(silence, AV_LOG_INFO,
117
+                           "silence_end: %f | silence_duration: %f\n",
118
+                           end, end - silence->start);
119
+                }
120
+                silence->nb_null_samples = silence->start = 0;
121
+            }
122
+        }
123
+    }
124
+
125
+    avfilter_filter_samples(inlink->dst->outputs[0], insamples);
126
+}
127
+
128
+static int query_formats(AVFilterContext *ctx)
129
+{
130
+    AVFilterFormats *formats = NULL;
131
+    enum AVSampleFormat sample_fmts[] = {
132
+        AV_SAMPLE_FMT_DBL,
133
+        AV_SAMPLE_FMT_NONE
134
+    };
135
+    int packing_fmts[] = { AVFILTER_PACKED, -1 };
136
+
137
+    formats = avfilter_make_all_channel_layouts();
138
+    if (!formats)
139
+        return AVERROR(ENOMEM);
140
+    avfilter_set_common_channel_layouts(ctx, formats);
141
+
142
+    formats = avfilter_make_format_list(sample_fmts);
143
+    if (!formats)
144
+        return AVERROR(ENOMEM);
145
+    avfilter_set_common_sample_formats(ctx, formats);
146
+
147
+    formats = avfilter_make_format_list(packing_fmts);
148
+    if (!formats)
149
+        return AVERROR(ENOMEM);
150
+    avfilter_set_common_packing_formats(ctx, formats);
151
+
152
+    return 0;
153
+}
154
+
155
+AVFilter avfilter_af_silencedetect = {
156
+    .name          = "silencedetect",
157
+    .description   = NULL_IF_CONFIG_SMALL("Detect silence."),
158
+    .priv_size     = sizeof(SilenceDetectContext),
159
+    .init          = init,
160
+    .query_formats = query_formats,
161
+
162
+    .inputs = (const AVFilterPad[]) {
163
+        { .name             = "default",
164
+          .type             = AVMEDIA_TYPE_AUDIO,
165
+          .get_audio_buffer = avfilter_null_get_audio_buffer,
166
+          .filter_samples   = filter_samples, },
167
+        { .name = NULL }
168
+    },
169
+    .outputs = (const AVFilterPad[]) {
170
+        { .name = "default",
171
+          .type = AVMEDIA_TYPE_AUDIO, },
172
+        { .name = NULL }
173
+    },
174
+};
... ...
@@ -44,6 +44,7 @@ void avfilter_register_all(void)
44 44
     REGISTER_FILTER (ASTREAMSYNC, astreamsync, af);
45 45
     REGISTER_FILTER (EARWAX,      earwax,      af);
46 46
     REGISTER_FILTER (PAN,         pan,         af);
47
+    REGISTER_FILTER (SILENCEDETECT, silencedetect, af);
47 48
     REGISTER_FILTER (VOLUME,      volume,      af);
48 49
 
49 50
     REGISTER_FILTER (ABUFFER,     abuffer,     asrc);
... ...
@@ -30,8 +30,8 @@
30 30
 #include "libavcodec/avcodec.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR  2
33
-#define LIBAVFILTER_VERSION_MINOR 57
34
-#define LIBAVFILTER_VERSION_MICRO 101
33
+#define LIBAVFILTER_VERSION_MINOR 58
34
+#define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
                                                LIBAVFILTER_VERSION_MINOR, \