FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_silenceremove.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 124 198 62.6%
Functions: 7 7 100.0%
Branches: 36 104 34.6%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2001 Heikki Leinonen
3 * Copyright (c) 2001 Chris Bagwell
4 * Copyright (c) 2003 Donnie Smith
5 * Copyright (c) 2014 Paul B Mahol
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include <float.h> /* DBL_MAX */
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/opt.h"
29 #include "audio.h"
30 #include "filters.h"
31 #include "avfilter.h"
32
33 enum SilenceDetect {
34 D_AVG,
35 D_RMS,
36 D_PEAK,
37 D_MEDIAN,
38 D_PTP,
39 D_DEV,
40 D_NB
41 };
42
43 enum TimestampMode {
44 TS_WRITE,
45 TS_COPY,
46 TS_NB
47 };
48
49 enum ThresholdMode {
50 T_ANY,
51 T_ALL,
52 };
53
54 typedef struct SilenceRemoveContext {
55 const AVClass *class;
56
57 int start_mode;
58 int start_periods;
59 int64_t start_duration;
60 int64_t start_duration_opt;
61 double start_threshold;
62 int64_t start_silence;
63 int64_t start_silence_opt;
64
65 int stop_mode;
66 int stop_periods;
67 int64_t stop_duration;
68 int64_t stop_duration_opt;
69 double stop_threshold;
70 int64_t stop_silence;
71 int64_t stop_silence_opt;
72
73 int64_t window_duration_opt;
74
75 int timestamp_mode;
76
77 int start_found_periods;
78 int stop_found_periods;
79
80 int start_sample_count;
81 int start_silence_count;
82
83 int stop_sample_count;
84 int stop_silence_count;
85
86 AVFrame *start_window;
87 AVFrame *stop_window;
88
89 int *start_front;
90 int *start_back;
91
92 int *stop_front;
93 int *stop_back;
94
95 int64_t window_duration;
96 int cache_size;
97
98 int start_window_pos;
99 int start_window_size;
100
101 int stop_window_pos;
102 int stop_window_size;
103
104 double *start_cache;
105 double *stop_cache;
106
107 AVFrame *start_queuef;
108 int start_queue_pos;
109 int start_queue_size;
110
111 AVFrame *stop_queuef;
112 int stop_queue_pos;
113 int stop_queue_size;
114
115 int restart;
116 int found_nonsilence;
117 int64_t next_pts;
118
119 int detection;
120
121 float (*compute_flt)(float *c, float s, float ws, int size, int *front, int *back);
122 double (*compute_dbl)(double *c, double s, double ws, int size, int *front, int *back);
123 } SilenceRemoveContext;
124
125 #define OFFSET(x) offsetof(SilenceRemoveContext, x)
126 #define AF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
127 #define AFR AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
128
129 static const AVOption silenceremove_options[] = {
130 { "start_periods", "set periods of silence parts to skip from start", OFFSET(start_periods), AV_OPT_TYPE_INT, {.i64=0}, 0, 9000, AF },
131 { "start_duration", "set start duration of non-silence part", OFFSET(start_duration_opt), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX, AF },
132 { "start_threshold", "set threshold for start silence detection", OFFSET(start_threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX, AFR },
133 { "start_silence", "set start duration of silence part to keep", OFFSET(start_silence_opt), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX, AF },
134 { "start_mode", "set which channel will trigger trimming from start", OFFSET(start_mode), AV_OPT_TYPE_INT, {.i64=T_ANY}, T_ANY, T_ALL, AFR, .unit = "mode" },
135 { "any", 0, 0, AV_OPT_TYPE_CONST, {.i64=T_ANY}, 0, 0, AFR, .unit = "mode" },
136 { "all", 0, 0, AV_OPT_TYPE_CONST, {.i64=T_ALL}, 0, 0, AFR, .unit = "mode" },
137 { "stop_periods", "set periods of silence parts to skip from end", OFFSET(stop_periods), AV_OPT_TYPE_INT, {.i64=0}, -9000, 9000, AF },
138 { "stop_duration", "set stop duration of silence part", OFFSET(stop_duration_opt), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX, AF },
139 { "stop_threshold", "set threshold for stop silence detection", OFFSET(stop_threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX, AFR },
140 { "stop_silence", "set stop duration of silence part to keep", OFFSET(stop_silence_opt), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX, AF },
141 { "stop_mode", "set which channel will trigger trimming from end", OFFSET(stop_mode), AV_OPT_TYPE_INT, {.i64=T_ALL}, T_ANY, T_ALL, AFR, .unit = "mode" },
142 { "detection", "set how silence is detected", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=D_RMS}, 0, D_NB-1, AF, .unit = "detection" },
143 { "avg", "use mean absolute values of samples", 0, AV_OPT_TYPE_CONST, {.i64=D_AVG}, 0, 0, AF, .unit = "detection" },
144 { "rms", "use root mean squared values of samples", 0, AV_OPT_TYPE_CONST, {.i64=D_RMS}, 0, 0, AF, .unit = "detection" },
145 { "peak", "use max absolute values of samples", 0, AV_OPT_TYPE_CONST, {.i64=D_PEAK},0, 0, AF, .unit = "detection" },
146 { "median", "use median of absolute values of samples", 0, AV_OPT_TYPE_CONST, {.i64=D_MEDIAN},0, 0, AF, .unit = "detection" },
147 { "ptp", "use absolute of max peak to min peak difference", 0, AV_OPT_TYPE_CONST, {.i64=D_PTP}, 0, 0, AF, .unit = "detection" },
148 { "dev", "use standard deviation from values of samples", 0, AV_OPT_TYPE_CONST, {.i64=D_DEV}, 0, 0, AF, .unit = "detection" },
149 { "window", "set duration of window for silence detection", OFFSET(window_duration_opt), AV_OPT_TYPE_DURATION, {.i64=20000}, 0, 100000000, AF },
150 { "timestamp", "set how every output frame timestamp is processed", OFFSET(timestamp_mode), AV_OPT_TYPE_INT, {.i64=TS_WRITE}, 0, TS_NB-1, AF, .unit = "timestamp" },
151 { "write", "full timestamps rewrite, keep only the start time", 0, AV_OPT_TYPE_CONST, {.i64=TS_WRITE}, 0, 0, AF, .unit = "timestamp" },
152 { "copy", "non-dropped frames are left with same timestamp", 0, AV_OPT_TYPE_CONST, {.i64=TS_COPY}, 0, 0, AF, .unit = "timestamp" },
153 { NULL }
154 };
155
156 AVFILTER_DEFINE_CLASS(silenceremove);
157
158 #define DEPTH 32
159 #include "silenceremove_template.c"
160
161 #undef DEPTH
162 #define DEPTH 64
163 #include "silenceremove_template.c"
164
165 1 static av_cold int init(AVFilterContext *ctx)
166 {
167 1 SilenceRemoveContext *s = ctx->priv;
168
169
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (s->stop_periods < 0) {
170 1 s->stop_periods = -s->stop_periods;
171 1 s->restart = 1;
172 }
173
174 1 return 0;
175 }
176
177 1 static void clear_windows(SilenceRemoveContext *s)
178 {
179 1 av_samples_set_silence(s->start_window->extended_data, 0,
180 1 s->start_window->nb_samples,
181 1 s->start_window->ch_layout.nb_channels,
182 1 s->start_window->format);
183 1 av_samples_set_silence(s->stop_window->extended_data, 0,
184 1 s->stop_window->nb_samples,
185 1 s->stop_window->ch_layout.nb_channels,
186 1 s->stop_window->format);
187
188 1 s->start_window_pos = 0;
189 1 s->start_window_size = 0;
190 1 s->stop_window_pos = 0;
191 1 s->stop_window_size = 0;
192 1 s->start_queue_pos = 0;
193 1 s->start_queue_size = 0;
194 1 s->stop_queue_pos = 0;
195 1 s->stop_queue_size = 0;
196 1 }
197
198 1 static int config_input(AVFilterLink *inlink)
199 {
200 1 AVFilterContext *ctx = inlink->dst;
201 1 SilenceRemoveContext *s = ctx->priv;
202
203 1 s->next_pts = AV_NOPTS_VALUE;
204 1 s->window_duration = av_rescale(s->window_duration_opt, inlink->sample_rate,
205 AV_TIME_BASE);
206 1 s->window_duration = FFMAX(1, s->window_duration);
207
208 1 s->start_duration = av_rescale(s->start_duration_opt, inlink->sample_rate,
209 AV_TIME_BASE);
210 1 s->start_silence = av_rescale(s->start_silence_opt, inlink->sample_rate,
211 AV_TIME_BASE);
212 1 s->stop_duration = av_rescale(s->stop_duration_opt, inlink->sample_rate,
213 AV_TIME_BASE);
214 1 s->stop_silence = av_rescale(s->stop_silence_opt, inlink->sample_rate,
215 AV_TIME_BASE);
216
217 1 s->start_found_periods = 0;
218 1 s->stop_found_periods = 0;
219
220 1 return 0;
221 }
222
223 1 static int config_output(AVFilterLink *outlink)
224 {
225 1 AVFilterContext *ctx = outlink->src;
226 1 SilenceRemoveContext *s = ctx->priv;
227
228
1/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 switch (s->detection) {
229 1 case D_AVG:
230 case D_RMS:
231 1 s->cache_size = 1;
232 1 break;
233 case D_DEV:
234 s->cache_size = 2;
235 break;
236 case D_MEDIAN:
237 case D_PEAK:
238 case D_PTP:
239 s->cache_size = s->window_duration;
240 break;
241 }
242
243 1 s->start_window = ff_get_audio_buffer(outlink, s->window_duration);
244 1 s->stop_window = ff_get_audio_buffer(outlink, s->window_duration);
245 1 s->start_cache = av_calloc(outlink->ch_layout.nb_channels, s->cache_size * sizeof(*s->start_cache));
246 1 s->stop_cache = av_calloc(outlink->ch_layout.nb_channels, s->cache_size * sizeof(*s->stop_cache));
247
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
1 if (!s->start_window || !s->stop_window || !s->start_cache || !s->stop_cache)
248 return AVERROR(ENOMEM);
249
250 1 s->start_queuef = ff_get_audio_buffer(outlink, s->start_silence + 1);
251 1 s->stop_queuef = ff_get_audio_buffer(outlink, s->stop_silence + 1);
252
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!s->start_queuef || !s->stop_queuef)
253 return AVERROR(ENOMEM);
254
255 1 s->start_front = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->start_front));
256 1 s->start_back = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->start_back));
257 1 s->stop_front = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->stop_front));
258 1 s->stop_back = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->stop_back));
259
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
1 if (!s->start_front || !s->start_back || !s->stop_front || !s->stop_back)
260 return AVERROR(ENOMEM);
261
262 1 clear_windows(s);
263
264
1/7
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
1 switch (s->detection) {
265 1 case D_AVG:
266 1 s->compute_flt = compute_avg_flt;
267 1 s->compute_dbl = compute_avg_dbl;
268 1 break;
269 case D_DEV:
270 s->compute_flt = compute_dev_flt;
271 s->compute_dbl = compute_dev_dbl;
272 break;
273 case D_PTP:
274 s->compute_flt = compute_ptp_flt;
275 s->compute_dbl = compute_ptp_dbl;
276 break;
277 case D_MEDIAN:
278 s->compute_flt = compute_median_flt;
279 s->compute_dbl = compute_median_dbl;
280 break;
281 case D_PEAK:
282 s->compute_flt = compute_peak_flt;
283 s->compute_dbl = compute_peak_dbl;
284 break;
285 case D_RMS:
286 s->compute_flt = compute_rms_flt;
287 s->compute_dbl = compute_rms_dbl;
288 break;
289 }
290
291 1 return 0;
292 }
293
294 54 static int filter_frame(AVFilterLink *outlink, AVFrame *in)
295 {
296 54 const int nb_channels = outlink->ch_layout.nb_channels;
297 54 AVFilterContext *ctx = outlink->src;
298 54 SilenceRemoveContext *s = ctx->priv;
299 int max_out_nb_samples;
300 54 int out_nb_samples = 0;
301 int in_nb_samples;
302 const double *srcd;
303 const float *srcf;
304 AVFrame *out;
305 double *dstd;
306 float *dstf;
307
308
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 53 times.
54 if (s->next_pts == AV_NOPTS_VALUE)
309 1 s->next_pts = in->pts;
310
311 54 in_nb_samples = in->nb_samples;
312 54 max_out_nb_samples = in->nb_samples +
313 54 s->start_silence +
314 54 s->stop_silence;
315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (max_out_nb_samples <= 0) {
316 av_frame_free(&in);
317 ff_filter_set_ready(ctx, 100);
318 return 0;
319 }
320
321 54 out = ff_get_audio_buffer(outlink, max_out_nb_samples);
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (!out) {
323 av_frame_free(&in);
324 return AVERROR(ENOMEM);
325 }
326
327
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 if (s->timestamp_mode == TS_WRITE)
328 54 out->pts = s->next_pts;
329 else
330 out->pts = in->pts;
331
332
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
54 switch (outlink->format) {
333 case AV_SAMPLE_FMT_FLT:
334 srcf = (const float *)in->data[0];
335 dstf = (float *)out->data[0];
336 if (s->start_periods > 0 && s->stop_periods > 0) {
337 const float *src = srcf;
338 if (s->start_found_periods >= 0) {
339 for (int n = 0; n < in_nb_samples; n++) {
340 filter_start_flt(ctx, src + n * nb_channels,
341 dstf, &out_nb_samples,
342 nb_channels);
343 }
344 in_nb_samples = out_nb_samples;
345 out_nb_samples = 0;
346 src = dstf;
347 }
348 for (int n = 0; n < in_nb_samples; n++) {
349 filter_stop_flt(ctx, src + n * nb_channels,
350 dstf, &out_nb_samples,
351 nb_channels);
352 }
353 } else if (s->start_periods > 0) {
354 for (int n = 0; n < in_nb_samples; n++) {
355 filter_start_flt(ctx, srcf + n * nb_channels,
356 dstf, &out_nb_samples,
357 nb_channels);
358 }
359 } else if (s->stop_periods > 0) {
360 for (int n = 0; n < in_nb_samples; n++) {
361 filter_stop_flt(ctx, srcf + n * nb_channels,
362 dstf, &out_nb_samples,
363 nb_channels);
364 }
365 }
366 break;
367 54 case AV_SAMPLE_FMT_DBL:
368 54 srcd = (const double *)in->data[0];
369 54 dstd = (double *)out->data[0];
370
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
54 if (s->start_periods > 0 && s->stop_periods > 0) {
371 const double *src = srcd;
372 if (s->start_found_periods >= 0) {
373 for (int n = 0; n < in_nb_samples; n++) {
374 filter_start_dbl(ctx, src + n * nb_channels,
375 dstd, &out_nb_samples,
376 nb_channels);
377 }
378 in_nb_samples = out_nb_samples;
379 out_nb_samples = 0;
380 src = dstd;
381 }
382 for (int n = 0; n < in_nb_samples; n++) {
383 filter_stop_dbl(ctx, src + n * nb_channels,
384 dstd, &out_nb_samples,
385 nb_channels);
386 }
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 } else if (s->start_periods > 0) {
388 for (int n = 0; n < in_nb_samples; n++) {
389 filter_start_dbl(ctx, srcd + n * nb_channels,
390 dstd, &out_nb_samples,
391 nb_channels);
392 }
393
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 } else if (s->stop_periods > 0) {
394
2/2
✓ Branch 0 taken 441000 times.
✓ Branch 1 taken 54 times.
441054 for (int n = 0; n < in_nb_samples; n++) {
395 441000 filter_stop_dbl(ctx, srcd + n * nb_channels,
396 dstd, &out_nb_samples,
397 nb_channels);
398 }
399 }
400 54 break;
401 }
402
403 54 av_frame_free(&in);
404
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 25 times.
54 if (out_nb_samples > 0) {
405 29 s->next_pts += out_nb_samples;
406 29 out->nb_samples = out_nb_samples;
407 29 return ff_filter_frame(outlink, out);
408 }
409
410 25 av_frame_free(&out);
411 25 ff_filter_set_ready(ctx, 100);
412
413 25 return 0;
414 }
415
416 110 static int activate(AVFilterContext *ctx)
417 {
418 110 AVFilterLink *outlink = ctx->outputs[0];
419 110 AVFilterLink *inlink = ctx->inputs[0];
420 110 SilenceRemoveContext *s = ctx->priv;
421 AVFrame *in;
422 int ret;
423
424
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 110 times.
110 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
425
426 110 ret = ff_inlink_consume_frame(inlink, &in);
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if (ret < 0)
428 return ret;
429
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 56 times.
110 if (ret > 0) {
430
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
54 if (s->start_periods == 1 && s->stop_periods == 0 &&
431 s->start_found_periods < 0) {
432 if (s->timestamp_mode == TS_WRITE)
433 in->pts = s->next_pts;
434 s->next_pts += in->nb_samples;
435 return ff_filter_frame(outlink, in);
436 }
437
2/4
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 54 times.
54 if (s->start_periods == 0 && s->stop_periods == 0)
438 return ff_filter_frame(outlink, in);
439 54 return filter_frame(outlink, in);
440 }
441
442
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 55 times.
56 FF_FILTER_FORWARD_STATUS(inlink, outlink);
443
1/2
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
55 FF_FILTER_FORWARD_WANTED(outlink, inlink);
444
445 return FFERROR_NOT_READY;
446 }
447
448 1 static av_cold void uninit(AVFilterContext *ctx)
449 {
450 1 SilenceRemoveContext *s = ctx->priv;
451
452 1 av_frame_free(&s->start_window);
453 1 av_frame_free(&s->stop_window);
454 1 av_frame_free(&s->start_queuef);
455 1 av_frame_free(&s->stop_queuef);
456
457 1 av_freep(&s->start_cache);
458 1 av_freep(&s->stop_cache);
459 1 av_freep(&s->start_front);
460 1 av_freep(&s->start_back);
461 1 av_freep(&s->stop_front);
462 1 av_freep(&s->stop_back);
463 1 }
464
465 static const AVFilterPad silenceremove_inputs[] = {
466 {
467 .name = "default",
468 .type = AVMEDIA_TYPE_AUDIO,
469 .config_props = config_input,
470 },
471 };
472
473 static const AVFilterPad silenceremove_outputs[] = {
474 {
475 .name = "default",
476 .type = AVMEDIA_TYPE_AUDIO,
477 .config_props = config_output,
478 },
479 };
480
481 const AVFilter ff_af_silenceremove = {
482 .name = "silenceremove",
483 .description = NULL_IF_CONFIG_SMALL("Remove silence."),
484 .priv_size = sizeof(SilenceRemoveContext),
485 .priv_class = &silenceremove_class,
486 .init = init,
487 .activate = activate,
488 .uninit = uninit,
489 FILTER_INPUTS(silenceremove_inputs),
490 FILTER_OUTPUTS(silenceremove_outputs),
491 FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_FLT,
492 AV_SAMPLE_FMT_DBL),
493 .process_command = ff_filter_process_command,
494 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
495 };
496