]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_silencedetect.c
Merge commit '488b2984fece7ad0c2596826fee18e74aa904667'
[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 <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 *silence = 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 * silence->duration * nb_channels;
70
71     // scale number of null samples to the new sample rate
72     if (silence->last_sample_rate && silence->last_sample_rate != srate)
73         silence->nb_null_samples =
74             srate * silence->nb_null_samples / silence->last_sample_rate;
75     silence->last_sample_rate = srate;
76
77     // TODO: support more sample formats
78     // TODO: document metadata
79     if (insamples->format == AV_SAMPLE_FMT_DBL) {
80         double *p = (double *)insamples->data[0];
81
82         for (i = 0; i < nb_samples; i++, p++) {
83             if (*p < silence->noise && *p > -silence->noise) {
84                 if (!silence->start) {
85                     silence->nb_null_samples++;
86                     if (silence->nb_null_samples >= nb_samples_notify) {
87                         silence->start = insamples->pts - (int64_t)(silence->duration / av_q2d(inlink->time_base) + .5);
88                         av_dict_set(&insamples->metadata, "lavfi.silence_start",
89                                     av_ts2timestr(silence->start, &inlink->time_base), 0);
90                         av_log(silence, AV_LOG_INFO, "silence_start: %s\n",
91                                get_metadata_val(insamples, "lavfi.silence_start"));
92                     }
93                 }
94             } else {
95                 if (silence->start) {
96                     av_dict_set(&insamples->metadata, "lavfi.silence_end",
97                                 av_ts2timestr(insamples->pts, &inlink->time_base), 0);
98                     av_dict_set(&insamples->metadata, "lavfi.silence_duration",
99                                 av_ts2timestr(insamples->pts - silence->start, &inlink->time_base), 0);
100                     av_log(silence, AV_LOG_INFO,
101                            "silence_end: %s | silence_duration: %s\n",
102                            get_metadata_val(insamples, "lavfi.silence_end"),
103                            get_metadata_val(insamples, "lavfi.silence_duration"));
104                 }
105                 silence->nb_null_samples = silence->start = 0;
106             }
107         }
108     }
109
110     return ff_filter_frame(inlink->dst->outputs[0], insamples);
111 }
112
113 static int query_formats(AVFilterContext *ctx)
114 {
115     AVFilterFormats *formats = NULL;
116     AVFilterChannelLayouts *layouts = NULL;
117     static const enum AVSampleFormat sample_fmts[] = {
118         AV_SAMPLE_FMT_DBL,
119         AV_SAMPLE_FMT_NONE
120     };
121
122     layouts = ff_all_channel_layouts();
123     if (!layouts)
124         return AVERROR(ENOMEM);
125     ff_set_common_channel_layouts(ctx, layouts);
126
127     formats = ff_make_format_list(sample_fmts);
128     if (!formats)
129         return AVERROR(ENOMEM);
130     ff_set_common_formats(ctx, formats);
131
132     formats = ff_all_samplerates();
133     if (!formats)
134         return AVERROR(ENOMEM);
135     ff_set_common_samplerates(ctx, formats);
136
137     return 0;
138 }
139
140 static const AVFilterPad silencedetect_inputs[] = {
141     {
142         .name             = "default",
143         .type             = AVMEDIA_TYPE_AUDIO,
144         .get_audio_buffer = ff_null_get_audio_buffer,
145         .filter_frame     = filter_frame,
146     },
147     { NULL }
148 };
149
150 static const AVFilterPad silencedetect_outputs[] = {
151     {
152         .name = "default",
153         .type = AVMEDIA_TYPE_AUDIO,
154     },
155     { NULL }
156 };
157
158 AVFilter avfilter_af_silencedetect = {
159     .name          = "silencedetect",
160     .description   = NULL_IF_CONFIG_SMALL("Detect silence."),
161     .priv_size     = sizeof(SilenceDetectContext),
162     .query_formats = query_formats,
163     .inputs        = silencedetect_inputs,
164     .outputs       = silencedetect_outputs,
165     .priv_class    = &silencedetect_class,
166 };