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
34
37 double noise;
///< noise amplitude ratio
38 double duration;
///< minimum duration of silence until notification
40 int64_t
start;
///< if silence is detected, this value contains the time of the first zero sample
42
44 int nb_samples, int64_t nb_samples_notify,
47
48 #define OFFSET(x) offsetof(SilenceDetectContext, x)
49 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
55 { NULL }
56 };
57
59
61 {
64 }
65
67 int is_silence, int64_t nb_samples_notify,
69 {
70 if (is_silence) {
79 }
80 }
81 } else {
88 "silence_end: %s | silence_duration: %s\n",
91 }
93 }
94 }
95
96 #define SILENCE_DETECT(name, type) \
97 static void silencedetect_##name(SilenceDetectContext *s, AVFrame *insamples, \
98 int nb_samples, int64_t nb_samples_notify, \
99 AVRational time_base) \
100 { \
101 const type *p = (const type *)insamples->data[0]; \
102 const type noise = s->noise; \
103 int i; \
104 \
105 for (i = 0; i < nb_samples; i++, p++) \
106 update(s, insamples, *p < noise && *p > -noise, \
107 nb_samples_notify, time_base); \
108 }
109
114
116 {
119
120 switch (inlink->format) {
124 s->
noise *= INT32_MAX;
126 break;
128 s->
noise *= INT16_MAX;
130 break;
131 }
132
133 return 0;
134 }
135
137 {
143
144 // scale number of null samples to the new sample rate
148
149 // TODO: document metadata
150 s->
silencedetect(s, insamples, nb_samples, nb_samples_notify,
152
154 }
155
157 {
166 };
167
169 if (!layouts)
172
174 if (!formats)
177
179 if (!formats)
182
183 return 0;
184 }
185
187 {
192 },
193 { NULL }
194 };
195
197 {
200 },
201 { NULL }
202 };
203
205 .
name =
"silencedetect",
209 .
inputs = silencedetect_inputs,
210 .
outputs = silencedetect_outputs,
211 .priv_class = &silencedetect_class,
212 };