FFmpeg coverage


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