]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_silencedetect.c
Merge commit 'd347a7b248d4ffdc278373fecf033b0ade030343'
[ffmpeg] / libavfilter / af_silencedetect.c
1 /*
2  * Copyright (c) 2012 Clément Bœsch <u pkh me>
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 <float.h> /* DBL_MAX */
27
28 #include "libavutil/opt.h"
29 #include "libavutil/timestamp.h"
30 #include "audio.h"
31 #include "formats.h"
32 #include "avfilter.h"
33 #include "internal.h"
34
35 typedef struct {
36     const AVClass *class;
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;
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),     AV_OPT_TYPE_DOUBLE, {.dbl=0.001},          0, DBL_MAX,  FLAGS },
48     { "noise",     "set noise tolerance",              OFFSET(noise),     AV_OPT_TYPE_DOUBLE, {.dbl=0.001},          0, DBL_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 },
51     { NULL }
52 };
53
54 AVFILTER_DEFINE_CLASS(silencedetect);
55
56 static char *get_metadata_val(AVFrame *insamples, const char *key)
57 {
58     AVDictionaryEntry *e = av_dict_get(insamples->metadata, key, NULL, 0);
59     return e && e->value ? e->value : NULL;
60 }
61
62 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
63 {
64     int i;
65     SilenceDetectContext *s         = inlink->dst->priv;
66     const int nb_channels           = inlink->channels;
67     const int srate                 = inlink->sample_rate;
68     const int nb_samples            = insamples->nb_samples     * nb_channels;
69     const int64_t nb_samples_notify = srate * s->duration * nb_channels;
70
71     // scale number of null samples to the new sample rate
72     if (s->last_sample_rate && s->last_sample_rate != srate)
73         s->nb_null_samples = srate * s->nb_null_samples / s->last_sample_rate;
74     s->last_sample_rate = srate;
75
76     // TODO: support more sample formats
77     // TODO: document metadata
78     if (insamples->format == AV_SAMPLE_FMT_DBL) {
79         double *p = (double *)insamples->data[0];
80
81         for (i = 0; i < nb_samples; i++, p++) {
82             if (*p < s->noise && *p > -s->noise) {
83                 if (!s->start) {
84                     s->nb_null_samples++;
85                     if (s->nb_null_samples >= nb_samples_notify) {
86                         s->start = insamples->pts - (int64_t)(s->duration / av_q2d(inlink->time_base) + .5);
87                         av_dict_set(&insamples->metadata, "lavfi.silence_start",
88                                     av_ts2timestr(s->start, &inlink->time_base), 0);
89                         av_log(s, AV_LOG_INFO, "silence_start: %s\n",
90                                get_metadata_val(insamples, "lavfi.silence_start"));
91                     }
92                 }
93             } else {
94                 if (s->start) {
95                     av_dict_set(&insamples->metadata, "lavfi.silence_end",
96                                 av_ts2timestr(insamples->pts, &inlink->time_base), 0);
97                     av_dict_set(&insamples->metadata, "lavfi.silence_duration",
98                                 av_ts2timestr(insamples->pts - s->start, &inlink->time_base), 0);
99                     av_log(s, AV_LOG_INFO,
100                            "silence_end: %s | silence_duration: %s\n",
101                            get_metadata_val(insamples, "lavfi.silence_end"),
102                            get_metadata_val(insamples, "lavfi.silence_duration"));
103                 }
104                 s->nb_null_samples = s->start = 0;
105             }
106         }
107     }
108
109     return ff_filter_frame(inlink->dst->outputs[0], insamples);
110 }
111
112 static int query_formats(AVFilterContext *ctx)
113 {
114     AVFilterFormats *formats = NULL;
115     AVFilterChannelLayouts *layouts = NULL;
116     static const enum AVSampleFormat sample_fmts[] = {
117         AV_SAMPLE_FMT_DBL,
118         AV_SAMPLE_FMT_NONE
119     };
120
121     layouts = ff_all_channel_layouts();
122     if (!layouts)
123         return AVERROR(ENOMEM);
124     ff_set_common_channel_layouts(ctx, layouts);
125
126     formats = ff_make_format_list(sample_fmts);
127     if (!formats)
128         return AVERROR(ENOMEM);
129     ff_set_common_formats(ctx, formats);
130
131     formats = ff_all_samplerates();
132     if (!formats)
133         return AVERROR(ENOMEM);
134     ff_set_common_samplerates(ctx, formats);
135
136     return 0;
137 }
138
139 static const AVFilterPad silencedetect_inputs[] = {
140     {
141         .name         = "default",
142         .type         = AVMEDIA_TYPE_AUDIO,
143         .filter_frame = filter_frame,
144     },
145     { NULL }
146 };
147
148 static const AVFilterPad silencedetect_outputs[] = {
149     {
150         .name = "default",
151         .type = AVMEDIA_TYPE_AUDIO,
152     },
153     { NULL }
154 };
155
156 AVFilter avfilter_af_silencedetect = {
157     .name          = "silencedetect",
158     .description   = NULL_IF_CONFIG_SMALL("Detect silence."),
159     .priv_size     = sizeof(SilenceDetectContext),
160     .query_formats = query_formats,
161     .inputs        = silencedetect_inputs,
162     .outputs       = silencedetect_outputs,
163     .priv_class    = &silencedetect_class,
164 };