]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_silencedetect.c
Merge remote-tracking branch 'qatar/master'
[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/opt.h"
27 #include "libavutil/timestamp.h"
28 #include "avfilter.h"
29
30 typedef struct {
31     const AVClass *class;
32     char *noise_str;            ///< noise option string
33     double noise;               ///< noise amplitude ratio
34     int duration;               ///< minimum duration of silence until notification
35     int64_t nb_null_samples;    ///< current number of continuous zero samples
36     int64_t start;              ///< if silence is detected, this value contains the time of the first zero sample
37     int last_sample_rate;       ///< last sample rate to check for sample rate changes
38 } SilenceDetectContext;
39
40 #define OFFSET(x) offsetof(SilenceDetectContext, x)
41 static const AVOption silencedetect_options[] = {
42     { "n",         "set noise tolerance",              OFFSET(noise_str), AV_OPT_TYPE_STRING, {.str="-60dB"}, CHAR_MIN, CHAR_MAX },
43     { "noise",     "set noise tolerance",              OFFSET(noise_str), AV_OPT_TYPE_STRING, {.str="-60dB"}, CHAR_MIN, CHAR_MAX },
44     { "d",         "set minimum duration in seconds",  OFFSET(duration),  AV_OPT_TYPE_INT,    {.dbl=2},    0, INT_MAX},
45     { "duration",  "set minimum duration in seconds",  OFFSET(duration),  AV_OPT_TYPE_INT,    {.dbl=2},    0, INT_MAX},
46     { NULL },
47 };
48
49 static const char *silencedetect_get_name(void *ctx)
50 {
51     return "silencedetect";
52 }
53
54 static const AVClass silencedetect_class = {
55     .class_name = "SilenceDetectContext",
56     .item_name  = silencedetect_get_name,
57     .option     = silencedetect_options,
58 };
59
60 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
61 {
62     int ret;
63     char *tail;
64     SilenceDetectContext *silence = ctx->priv;
65
66     silence->class = &silencedetect_class;
67     av_opt_set_defaults(silence);
68
69     if ((ret = av_set_options_string(silence, args, "=", ":")) < 0) {
70         av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
71         return ret;
72     }
73
74     silence->noise = strtod(silence->noise_str, &tail);
75     if (!strcmp(tail, "dB")) {
76         silence->noise = pow(10, silence->noise/20);
77     } else if (*tail) {
78         av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for noise parameter.\n",
79                silence->noise_str);
80         return AVERROR(EINVAL);
81     }
82
83     return 0;
84 }
85
86 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
87 {
88     int i;
89     SilenceDetectContext *silence = inlink->dst->priv;
90     const int nb_channels           = av_get_channel_layout_nb_channels(inlink->channel_layout);
91     const int srate                 = inlink->sample_rate;
92     const int nb_samples            = insamples->audio->nb_samples * nb_channels;
93     const int64_t nb_samples_notify = srate * silence->duration    * nb_channels;
94
95     // scale number of null samples to the new sample rate
96     if (silence->last_sample_rate && silence->last_sample_rate != srate)
97         silence->nb_null_samples =
98             srate * silence->nb_null_samples / silence->last_sample_rate;
99     silence->last_sample_rate = srate;
100
101     // TODO: support more sample formats
102     if (insamples->format == AV_SAMPLE_FMT_DBL) {
103         double *p = (double *)insamples->data[0];
104
105         for (i = 0; i < nb_samples; i++, p++) {
106             if (*p < silence->noise && *p > -silence->noise) {
107                 if (!silence->start) {
108                     silence->nb_null_samples++;
109                     if (silence->nb_null_samples >= nb_samples_notify) {
110                         silence->start = insamples->pts - silence->duration / av_q2d(inlink->time_base);
111                         av_log(silence, AV_LOG_INFO,
112                                "silence_start: %s\n", av_ts2timestr(silence->start, &inlink->time_base));
113                     }
114                 }
115             } else {
116                 if (silence->start)
117                     av_log(silence, AV_LOG_INFO,
118                            "silence_end: %s | silence_duration: %s\n",
119                            av_ts2timestr(insamples->pts,                  &inlink->time_base),
120                            av_ts2timestr(insamples->pts - silence->start, &inlink->time_base));
121                 silence->nb_null_samples = silence->start = 0;
122             }
123         }
124     }
125
126     avfilter_filter_samples(inlink->dst->outputs[0], insamples);
127 }
128
129 static int query_formats(AVFilterContext *ctx)
130 {
131     AVFilterFormats *formats = NULL;
132     enum AVSampleFormat sample_fmts[] = {
133         AV_SAMPLE_FMT_DBL,
134         AV_SAMPLE_FMT_NONE
135     };
136     int packing_fmts[] = { AVFILTER_PACKED, -1 };
137
138     formats = avfilter_make_all_channel_layouts();
139     if (!formats)
140         return AVERROR(ENOMEM);
141     avfilter_set_common_channel_layouts(ctx, formats);
142
143     formats = avfilter_make_format_list(sample_fmts);
144     if (!formats)
145         return AVERROR(ENOMEM);
146     avfilter_set_common_sample_formats(ctx, formats);
147
148     formats = avfilter_make_format_list(packing_fmts);
149     if (!formats)
150         return AVERROR(ENOMEM);
151     avfilter_set_common_packing_formats(ctx, formats);
152
153     return 0;
154 }
155
156 AVFilter avfilter_af_silencedetect = {
157     .name          = "silencedetect",
158     .description   = NULL_IF_CONFIG_SMALL("Detect silence."),
159     .priv_size     = sizeof(SilenceDetectContext),
160     .init          = init,
161     .query_formats = query_formats,
162
163     .inputs = (const AVFilterPad[]) {
164         { .name             = "default",
165           .type             = AVMEDIA_TYPE_AUDIO,
166           .get_audio_buffer = avfilter_null_get_audio_buffer,
167           .filter_samples   = filter_samples, },
168         { .name = NULL }
169     },
170     .outputs = (const AVFilterPad[]) {
171         { .name = "default",
172           .type = AVMEDIA_TYPE_AUDIO, },
173         { .name = NULL }
174     },
175 };