FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_silencedetect.c
Date: 2024-02-29 09:57:37
Exec Total Coverage
Lines: 74 107 69.2%
Functions: 6 13 46.2%
Branches: 35 109 32.1%

Line Branch Exec Source
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 "avfilter.h"
32 #include "internal.h"
33
34 typedef struct SilenceDetectContext {
35 const AVClass *class;
36 double noise; ///< noise amplitude ratio
37 int64_t duration; ///< minimum duration of silence until notification
38 int mono; ///< mono mode : check each channel separately (default = check when ALL channels are silent)
39 int channels; ///< number of channels
40 int independent_channels; ///< number of entries in following arrays (always 1 in mono mode)
41 int64_t *nb_null_samples; ///< (array) current number of continuous zero samples
42 int64_t *start; ///< (array) if silence is detected, this value contains the time of the first zero sample (default/unset = INT64_MIN)
43 int64_t frame_end; ///< pts of the end of the current frame (used to compute duration of silence at EOS)
44 int last_sample_rate; ///< last sample rate to check for sample rate changes
45 AVRational time_base; ///< time_base
46
47 void (*silencedetect)(struct SilenceDetectContext *s, AVFrame *insamples,
48 int nb_samples, int64_t nb_samples_notify,
49 AVRational time_base);
50 } SilenceDetectContext;
51
52 #define MAX_DURATION (24*3600*1000000LL)
53 #define OFFSET(x) offsetof(SilenceDetectContext, x)
54 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
55 static const AVOption silencedetect_options[] = {
56 { "n", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
57 { "noise", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
58 { "d", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=2000000}, 0, MAX_DURATION,FLAGS },
59 { "duration", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=2000000}, 0, MAX_DURATION,FLAGS },
60 { "mono", "check each channel separately", OFFSET(mono), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
61 { "m", "check each channel separately", OFFSET(mono), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
62 { NULL }
63 };
64
65 AVFILTER_DEFINE_CLASS(silencedetect);
66
67 9 static void set_meta(AVFrame *insamples, int channel, const char *key, char *value)
68 {
69 char key2[128];
70
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (channel)
72 snprintf(key2, sizeof(key2), "lavfi.%s.%d", key, channel);
73 else
74 9 snprintf(key2, sizeof(key2), "lavfi.%s", key);
75 9 av_dict_set(&insamples->metadata, key2, value, 0);
76 9 }
77 1048554 static av_always_inline void update(SilenceDetectContext *s, AVFrame *insamples,
78 int is_silence, int current_sample, int64_t nb_samples_notify,
79 AVRational time_base)
80 {
81 1048554 int channel = current_sample % s->independent_channels;
82
2/2
✓ Branch 0 taken 304774 times.
✓ Branch 1 taken 743780 times.
1048554 if (is_silence) {
83
2/2
✓ Branch 0 taken 221400 times.
✓ Branch 1 taken 83374 times.
304774 if (s->start[channel] == INT64_MIN) {
84 221400 s->nb_null_samples[channel]++;
85
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 221397 times.
221400 if (s->nb_null_samples[channel] >= nb_samples_notify) {
86 3 s->start[channel] = insamples->pts + av_rescale_q(current_sample / s->channels + 1 - nb_samples_notify * s->independent_channels / s->channels,
87 3 (AVRational){ 1, s->last_sample_rate }, time_base);
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 set_meta(insamples, s->mono ? channel + 1 : 0, "silence_start",
89 3 av_ts2timestr(s->start[channel], &time_base));
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->mono)
91 av_log(s, AV_LOG_INFO, "channel: %d | ", channel);
92 3 av_log(s, AV_LOG_INFO, "silence_start: %s\n",
93 3 av_ts2timestr(s->start[channel], &time_base));
94 }
95 }
96 } else {
97
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 743777 times.
743780 if (s->start[channel] > INT64_MIN) {
98 6 int64_t end_pts = insamples ? insamples->pts + av_rescale_q(current_sample / s->channels,
99 3 (AVRational){ 1, s->last_sample_rate }, time_base)
100
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 : s->frame_end;
101 3 int64_t duration_ts = end_pts - s->start[channel];
102
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (insamples) {
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 set_meta(insamples, s->mono ? channel + 1 : 0, "silence_end",
104 3 av_ts2timestr(end_pts, &time_base));
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 set_meta(insamples, s->mono ? channel + 1 : 0, "silence_duration",
106 3 av_ts2timestr(duration_ts, &time_base));
107 }
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->mono)
109 av_log(s, AV_LOG_INFO, "channel: %d | ", channel);
110 3 av_log(s, AV_LOG_INFO, "silence_end: %s | silence_duration: %s\n",
111 3 av_ts2timestr(end_pts, &time_base),
112 3 av_ts2timestr(duration_ts, &time_base));
113 }
114 743780 s->nb_null_samples[channel] = 0;
115 743780 s->start[channel] = INT64_MIN;
116 }
117 1048554 }
118
119 #define SILENCE_DETECT(name, type) \
120 static void silencedetect_##name(SilenceDetectContext *s, AVFrame *insamples, \
121 int nb_samples, int64_t nb_samples_notify, \
122 AVRational time_base) \
123 { \
124 const type *p = (const type *)insamples->data[0]; \
125 const type noise = s->noise; \
126 int i; \
127 \
128 for (i = 0; i < nb_samples; i++, p++) \
129 update(s, insamples, *p < noise && *p > -noise, i, \
130 nb_samples_notify, time_base); \
131 }
132
133 #define SILENCE_DETECT_PLANAR(name, type) \
134 static void silencedetect_##name(SilenceDetectContext *s, AVFrame *insamples, \
135 int nb_samples, int64_t nb_samples_notify, \
136 AVRational time_base) \
137 { \
138 const int channels = insamples->ch_layout.nb_channels; \
139 const type noise = s->noise; \
140 \
141 nb_samples /= channels; \
142 for (int i = 0; i < nb_samples; i++) { \
143 for (int ch = 0; ch < insamples->ch_layout.nb_channels; ch++) { \
144 const type *p = (const type *)insamples->extended_data[ch]; \
145 update(s, insamples, p[i] < noise && p[i] > -noise, \
146 channels * i + ch, \
147 nb_samples_notify, time_base); \
148 } \
149 } \
150 }
151
152 SILENCE_DETECT(dbl, double)
153 SILENCE_DETECT(flt, float)
154 SILENCE_DETECT(s32, int32_t)
155
6/6
✓ Branch 0 taken 678862 times.
✓ Branch 1 taken 369692 times.
✓ Branch 2 taken 304774 times.
✓ Branch 3 taken 374088 times.
✓ Branch 5 taken 1048554 times.
✓ Branch 6 taken 12 times.
1048566 SILENCE_DETECT(s16, int16_t)
156
157 SILENCE_DETECT_PLANAR(dblp, double)
158 SILENCE_DETECT_PLANAR(fltp, float)
159 SILENCE_DETECT_PLANAR(s32p, int32_t)
160 SILENCE_DETECT_PLANAR(s16p, int16_t)
161
162 1 static int config_input(AVFilterLink *inlink)
163 {
164 1 AVFilterContext *ctx = inlink->dst;
165 1 SilenceDetectContext *s = ctx->priv;
166 int c;
167
168 1 s->channels = inlink->ch_layout.nb_channels;
169 1 s->duration = av_rescale(s->duration, inlink->sample_rate, AV_TIME_BASE);
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 s->independent_channels = s->mono ? s->channels : 1;
171 1 s->nb_null_samples = av_calloc(s->independent_channels,
172 sizeof(*s->nb_null_samples));
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->nb_null_samples)
174 return AVERROR(ENOMEM);
175 1 s->start = av_malloc_array(sizeof(*s->start), s->independent_channels);
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->start)
177 return AVERROR(ENOMEM);
178
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (c = 0; c < s->independent_channels; c++)
179 1 s->start[c] = INT64_MIN;
180
181
1/9
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
1 switch (inlink->format) {
182 case AV_SAMPLE_FMT_DBL: s->silencedetect = silencedetect_dbl; break;
183 case AV_SAMPLE_FMT_FLT: s->silencedetect = silencedetect_flt; break;
184 case AV_SAMPLE_FMT_S32:
185 s->noise *= INT32_MAX;
186 s->silencedetect = silencedetect_s32;
187 break;
188 1 case AV_SAMPLE_FMT_S16:
189 1 s->noise *= INT16_MAX;
190 1 s->silencedetect = silencedetect_s16;
191 1 break;
192 case AV_SAMPLE_FMT_DBLP: s->silencedetect = silencedetect_dblp; break;
193 case AV_SAMPLE_FMT_FLTP: s->silencedetect = silencedetect_fltp; break;
194 case AV_SAMPLE_FMT_S32P:
195 s->noise *= INT32_MAX;
196 s->silencedetect = silencedetect_s32p;
197 break;
198 case AV_SAMPLE_FMT_S16P:
199 s->noise *= INT16_MAX;
200 s->silencedetect = silencedetect_s16p;
201 break;
202 default:
203 return AVERROR_BUG;
204 }
205
206 1 return 0;
207 }
208
209 12 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
210 {
211 12 SilenceDetectContext *s = inlink->dst->priv;
212 12 const int nb_channels = inlink->ch_layout.nb_channels;
213 12 const int srate = inlink->sample_rate;
214 12 const int nb_samples = insamples->nb_samples * nb_channels;
215
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 const int64_t nb_samples_notify = s->duration * (s->mono ? 1 : nb_channels);
216 int c;
217
218 // scale number of null samples to the new sample rate
219
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
12 if (s->last_sample_rate && s->last_sample_rate != srate)
220 for (c = 0; c < s->independent_channels; c++) {
221 s->nb_null_samples[c] = srate * s->nb_null_samples[c] / s->last_sample_rate;
222 }
223 12 s->last_sample_rate = srate;
224 12 s->time_base = inlink->time_base;
225 12 s->frame_end = insamples->pts + av_rescale_q(insamples->nb_samples,
226 12 (AVRational){ 1, s->last_sample_rate }, inlink->time_base);
227
228 12 s->silencedetect(s, insamples, nb_samples, nb_samples_notify,
229 inlink->time_base);
230
231 12 return ff_filter_frame(inlink->dst->outputs[0], insamples);
232 }
233
234 1 static av_cold void uninit(AVFilterContext *ctx)
235 {
236 1 SilenceDetectContext *s = ctx->priv;
237 int c;
238
239
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (c = 0; c < s->independent_channels; c++)
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (s->start[c] > INT64_MIN)
241 update(s, NULL, 0, c, 0, s->time_base);
242 1 av_freep(&s->nb_null_samples);
243 1 av_freep(&s->start);
244 1 }
245
246 static const AVFilterPad silencedetect_inputs[] = {
247 {
248 .name = "default",
249 .type = AVMEDIA_TYPE_AUDIO,
250 .config_props = config_input,
251 .filter_frame = filter_frame,
252 },
253 };
254
255 const AVFilter ff_af_silencedetect = {
256 .name = "silencedetect",
257 .description = NULL_IF_CONFIG_SMALL("Detect silence."),
258 .priv_size = sizeof(SilenceDetectContext),
259 .uninit = uninit,
260 FILTER_INPUTS(silencedetect_inputs),
261 FILTER_OUTPUTS(ff_audio_default_filterpad),
262 FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
263 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
264 AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
265 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P),
266 .priv_class = &silencedetect_class,
267 .flags = AVFILTER_FLAG_METADATA_ONLY,
268 };
269