]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_silencedetect.c
http: add 'timeout' AVOption
[ffmpeg] / libavfilter / af_silencedetect.c
1 /*
2  * Copyright (c) 2012 Clément Bœsch <ubitux@gmail.com>
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file
23  * Audio silence detector
24  */
25
26 #include "libavutil/audioconvert.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/timestamp.h"
29 #include "audio.h"
30 #include "formats.h"
31 #include "avfilter.h"
32 #include "internal.h"
33
34 typedef struct {
35     const AVClass *class;
36     char *noise_str;            ///< noise option string
37     double noise;               ///< noise amplitude ratio
38     int 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;
43
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_INT,    {.i64=2},    0, INT_MAX, FLAGS },
50     { "duration",  "set minimum duration in seconds",  OFFSET(duration),  AV_OPT_TYPE_INT,    {.i64=2},    0, INT_MAX, FLAGS },
51     { NULL },
52 };
53
54 AVFILTER_DEFINE_CLASS(silencedetect);
55
56 static av_cold int init(AVFilterContext *ctx, const char *args)
57 {
58     int ret;
59     char *tail;
60     SilenceDetectContext *silence = ctx->priv;
61
62     silence->class = &silencedetect_class;
63     av_opt_set_defaults(silence);
64
65     if ((ret = av_set_options_string(silence, args, "=", ":")) < 0)
66         return ret;
67
68     silence->noise = strtod(silence->noise_str, &tail);
69     if (!strcmp(tail, "dB")) {
70         silence->noise = pow(10, silence->noise/20);
71     } else if (*tail) {
72         av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for noise parameter.\n",
73                silence->noise_str);
74         return AVERROR(EINVAL);
75     }
76
77     return 0;
78 }
79
80 static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
81 {
82     int i;
83     SilenceDetectContext *silence = inlink->dst->priv;
84     const int nb_channels           = av_get_channel_layout_nb_channels(inlink->channel_layout);
85     const int srate                 = inlink->sample_rate;
86     const int nb_samples            = insamples->audio->nb_samples * nb_channels;
87     const int64_t nb_samples_notify = srate * silence->duration    * nb_channels;
88
89     // scale number of null samples to the new sample rate
90     if (silence->last_sample_rate && silence->last_sample_rate != srate)
91         silence->nb_null_samples =
92             srate * silence->nb_null_samples / silence->last_sample_rate;
93     silence->last_sample_rate = srate;
94
95     // TODO: support more sample formats
96     if (insamples->format == AV_SAMPLE_FMT_DBL) {
97         double *p = (double *)insamples->data[0];
98
99         for (i = 0; i < nb_samples; i++, p++) {
100             if (*p < silence->noise && *p > -silence->noise) {
101                 if (!silence->start) {
102                     silence->nb_null_samples++;
103                     if (silence->nb_null_samples >= nb_samples_notify) {
104                         silence->start = insamples->pts - silence->duration / av_q2d(inlink->time_base);
105                         av_log(silence, AV_LOG_INFO,
106                                "silence_start: %s\n", av_ts2timestr(silence->start, &inlink->time_base));
107                     }
108                 }
109             } else {
110                 if (silence->start)
111                     av_log(silence, AV_LOG_INFO,
112                            "silence_end: %s | silence_duration: %s\n",
113                            av_ts2timestr(insamples->pts,                  &inlink->time_base),
114                            av_ts2timestr(insamples->pts - silence->start, &inlink->time_base));
115                 silence->nb_null_samples = silence->start = 0;
116             }
117         }
118     }
119
120     return ff_filter_samples(inlink->dst->outputs[0], insamples);
121 }
122
123 static int query_formats(AVFilterContext *ctx)
124 {
125     AVFilterFormats *formats = NULL;
126     AVFilterChannelLayouts *layouts = NULL;
127     enum AVSampleFormat sample_fmts[] = {
128         AV_SAMPLE_FMT_DBL,
129         AV_SAMPLE_FMT_NONE
130     };
131
132     layouts = ff_all_channel_layouts();
133     if (!layouts)
134         return AVERROR(ENOMEM);
135     ff_set_common_channel_layouts(ctx, layouts);
136
137     formats = ff_make_format_list(sample_fmts);
138     if (!formats)
139         return AVERROR(ENOMEM);
140     ff_set_common_formats(ctx, formats);
141
142     formats = ff_all_samplerates();
143     if (!formats)
144         return AVERROR(ENOMEM);
145     ff_set_common_samplerates(ctx, formats);
146
147     return 0;
148 }
149
150 AVFilter avfilter_af_silencedetect = {
151     .name          = "silencedetect",
152     .description   = NULL_IF_CONFIG_SMALL("Detect silence."),
153     .priv_size     = sizeof(SilenceDetectContext),
154     .init          = init,
155     .query_formats = query_formats,
156
157     .inputs = (const AVFilterPad[]) {
158         { .name             = "default",
159           .type             = AVMEDIA_TYPE_AUDIO,
160           .get_audio_buffer = ff_null_get_audio_buffer,
161           .filter_samples   = filter_samples, },
162         { .name = NULL }
163     },
164     .outputs = (const AVFilterPad[]) {
165         { .name = "default",
166           .type = AVMEDIA_TYPE_AUDIO, },
167         { .name = NULL }
168     },
169     .priv_class = &silencedetect_class,
170 };