FFmpeg
Loading...
Searching...
No Matches
af_silenceremove.c
Go to the documentation of this file.
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
42
48
53
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
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
157
158#define DEPTH 32
160
161#undef DEPTH
162#define DEPTH 64
164
166{
167 SilenceRemoveContext *s = ctx->priv;
168
169 if (s->stop_periods < 0) {
170 s->stop_periods = -s->stop_periods;
171 s->restart = 1;
172 }
173
174 return 0;
175}
176
178{
179 av_samples_set_silence(s->start_window->extended_data, 0,
180 s->start_window->nb_samples,
181 s->start_window->ch_layout.nb_channels,
182 s->start_window->format);
183 av_samples_set_silence(s->stop_window->extended_data, 0,
184 s->stop_window->nb_samples,
185 s->stop_window->ch_layout.nb_channels,
186 s->stop_window->format);
187
188 s->start_window_pos = 0;
189 s->start_window_size = 0;
190 s->stop_window_pos = 0;
191 s->stop_window_size = 0;
192 s->start_queue_pos = 0;
193 s->start_queue_size = 0;
194 s->stop_queue_pos = 0;
195 s->stop_queue_size = 0;
196}
197
198static int config_input(AVFilterLink *inlink)
199{
200 AVFilterContext *ctx = inlink->dst;
201 SilenceRemoveContext *s = ctx->priv;
202
203 s->next_pts = AV_NOPTS_VALUE;
204 s->window_duration = av_rescale(s->window_duration_opt, inlink->sample_rate,
206 s->window_duration = FFMAX(1, s->window_duration);
207
208 s->start_duration = av_rescale(s->start_duration_opt, inlink->sample_rate,
210 s->start_silence = av_rescale(s->start_silence_opt, inlink->sample_rate,
212 s->stop_duration = av_rescale(s->stop_duration_opt, inlink->sample_rate,
214 s->stop_silence = av_rescale(s->stop_silence_opt, inlink->sample_rate,
216
217 s->start_found_periods = 0;
218 s->stop_found_periods = 0;
219
220 return 0;
221}
222
223static int config_output(AVFilterLink *outlink)
224{
225 AVFilterContext *ctx = outlink->src;
226 SilenceRemoveContext *s = ctx->priv;
227
228 switch (s->detection) {
229 case D_AVG:
230 case D_RMS:
231 s->cache_size = 1;
232 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 s->start_window = ff_get_audio_buffer(outlink, s->window_duration);
244 s->stop_window = ff_get_audio_buffer(outlink, s->window_duration);
245 s->start_cache = av_calloc(outlink->ch_layout.nb_channels, s->cache_size * sizeof(*s->start_cache));
246 s->stop_cache = av_calloc(outlink->ch_layout.nb_channels, s->cache_size * sizeof(*s->stop_cache));
247 if (!s->start_window || !s->stop_window || !s->start_cache || !s->stop_cache)
248 return AVERROR(ENOMEM);
249
250 s->start_queuef = ff_get_audio_buffer(outlink, s->start_silence + 1);
251 s->stop_queuef = ff_get_audio_buffer(outlink, s->stop_silence + 1);
252 if (!s->start_queuef || !s->stop_queuef)
253 return AVERROR(ENOMEM);
254
255 s->start_front = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->start_front));
256 s->start_back = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->start_back));
257 s->stop_front = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->stop_front));
258 s->stop_back = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->stop_back));
259 if (!s->start_front || !s->start_back || !s->stop_front || !s->stop_back)
260 return AVERROR(ENOMEM);
261
263
264 switch (s->detection) {
265 case D_AVG:
266 s->compute_flt = compute_avg_flt;
267 s->compute_dbl = compute_avg_dbl;
268 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 return 0;
292}
293
294static int filter_frame(AVFilterLink *outlink, AVFrame *in)
295{
296 const int nb_channels = outlink->ch_layout.nb_channels;
297 AVFilterContext *ctx = outlink->src;
298 SilenceRemoveContext *s = ctx->priv;
299 int max_out_nb_samples;
300 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 if (s->next_pts == AV_NOPTS_VALUE)
309 s->next_pts = in->pts;
310
311 in_nb_samples = in->nb_samples;
312 max_out_nb_samples = in->nb_samples +
313 s->start_silence +
314 s->stop_silence;
315 if (max_out_nb_samples <= 0) {
316 av_frame_free(&in);
318 return 0;
319 }
320
321 out = ff_get_audio_buffer(outlink, max_out_nb_samples);
322 if (!out) {
323 av_frame_free(&in);
324 return AVERROR(ENOMEM);
325 }
326
327 if (s->timestamp_mode == TS_WRITE)
328 out->pts = s->next_pts;
329 else
330 out->pts = in->pts;
331
332 switch (outlink->format) {
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;
368 srcd = (const double *)in->data[0];
369 dstd = (double *)out->data[0];
370 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 } 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 } else if (s->stop_periods > 0) {
394 for (int n = 0; n < in_nb_samples; n++) {
395 filter_stop_dbl(ctx, srcd + n * nb_channels,
396 dstd, &out_nb_samples,
397 nb_channels);
398 }
399 }
400 break;
401 }
402
403 av_frame_free(&in);
404 if (out_nb_samples > 0) {
405 s->next_pts += out_nb_samples;
406 out->nb_samples = out_nb_samples;
407 return ff_filter_frame(outlink, out);
408 }
409
412
413 return 0;
414}
415
417{
418 AVFilterLink *outlink = ctx->outputs[0];
419 AVFilterLink *inlink = ctx->inputs[0];
420 SilenceRemoveContext *s = ctx->priv;
421 AVFrame *in;
422 int ret;
423
424 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
425
426 ret = ff_inlink_consume_frame(inlink, &in);
427 if (ret < 0)
428 return ret;
429 if (ret > 0) {
430 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 if (s->start_periods == 0 && s->stop_periods == 0)
438 return ff_filter_frame(outlink, in);
439 return filter_frame(outlink, in);
440 }
441
442 FF_FILTER_FORWARD_STATUS(inlink, outlink);
443 FF_FILTER_FORWARD_WANTED(outlink, inlink);
444
445 return FFERROR_NOT_READY;
446}
447
449{
450 SilenceRemoveContext *s = ctx->priv;
451
452 av_frame_free(&s->start_window);
453 av_frame_free(&s->stop_window);
454 av_frame_free(&s->start_queuef);
455 av_frame_free(&s->stop_queuef);
456
457 av_freep(&s->start_cache);
458 av_freep(&s->stop_cache);
459 av_freep(&s->start_front);
460 av_freep(&s->start_back);
461 av_freep(&s->stop_front);
462 av_freep(&s->stop_back);
463}
464
466 {
467 .name = "default",
468 .type = AVMEDIA_TYPE_AUDIO,
469 .config_props = config_input,
470 },
471};
472
474 {
475 .name = "default",
476 .type = AVMEDIA_TYPE_AUDIO,
477 .config_props = config_output,
478 },
479};
480
482 .p.name = "silenceremove",
483 .p.description = NULL_IF_CONFIG_SMALL("Remove silence."),
484 .p.priv_class = &silenceremove_class,
486 .priv_size = sizeof(SilenceRemoveContext),
487 .init = init,
489 .uninit = uninit,
494 .process_command = ff_filter_process_command,
495};
static int config_input(AVFilterLink *inlink)
#define AF
#define AFR
Definition af_afftdn.c:165
static const AVFilterPad silenceremove_outputs[]
SilenceDetect
@ D_PTP
@ D_NB
@ D_PEAK
@ D_DEV
@ D_MEDIAN
@ D_AVG
@ D_RMS
static int config_input(AVFilterLink *inlink)
static void clear_windows(SilenceRemoveContext *s)
const FFFilter ff_af_silenceremove
ThresholdMode
@ T_ANY
@ T_ALL
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
static int config_output(AVFilterLink *outlink)
static const AVFilterPad silenceremove_inputs[]
static const AVOption silenceremove_options[]
static int filter_frame(AVFilterLink *outlink, AVFrame *in)
TimestampMode
@ TS_COPY
@ TS_NB
@ TS_WRITE
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
simple assert() macros that are a bit more flexible than ISO C assert().
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:906
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition avfilter.c:229
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition avfilter.c:1520
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
Definition opt.h:318
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition avfilter.h:204
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
@ AV_SAMPLE_FMT_DBL
double
Definition samplefmt.h:61
int av_samples_set_silence(uint8_t *const *audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition samplefmt.c:246
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_SAMPLEFMTS(...)
Definition filters.h:252
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition filters.h:694
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
Definition filters.h:666
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
Definition filters.h:34
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition filters.h:639
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMAX(a, b)
Definition macros.h:47
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
AVOptions.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
AVOption.
Definition opt.h:428
double(* compute_dbl)(double *c, double s, double ws, int size, int *front, int *back)
float(* compute_flt)(float *c, float s, float ws, int size, int *front, int *back)
#define av_freep(p)
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
int size
static double c[64]