2012-01-04 01:47:09 +03:00
|
|
|
/*
|
2013-09-03 21:42:24 +03:00
|
|
|
* Copyright (c) 2012 Clément Bœsch <u pkh me>
|
2012-01-04 01:47:09 +03:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Audio silence detector
|
|
|
|
*/
|
|
|
|
|
2012-12-06 17:08:38 +03:00
|
|
|
#include <float.h> /* DBL_MAX */
|
|
|
|
|
2012-01-04 01:47:09 +03:00
|
|
|
#include "libavutil/opt.h"
|
2012-02-15 02:21:14 +03:00
|
|
|
#include "libavutil/timestamp.h"
|
2012-05-12 18:58:14 +03:00
|
|
|
#include "audio.h"
|
2012-05-16 03:27:31 +03:00
|
|
|
#include "formats.h"
|
2012-01-04 01:47:09 +03:00
|
|
|
#include "avfilter.h"
|
2012-06-22 15:33:09 +03:00
|
|
|
#include "internal.h"
|
2012-01-04 01:47:09 +03:00
|
|
|
|
2013-10-04 20:57:15 +03:00
|
|
|
typedef struct SilenceDetectContext {
|
2012-01-04 01:47:09 +03:00
|
|
|
const AVClass *class;
|
|
|
|
double noise; ///< noise amplitude ratio
|
2019-10-08 17:04:52 +02:00
|
|
|
int64_t duration; ///< minimum duration of silence until notification
|
2018-02-19 15:15:30 +02:00
|
|
|
int mono; ///< mono mode : check each channel separately (default = check when ALL channels are silent)
|
2018-02-19 15:16:04 +02:00
|
|
|
int channels; ///< number of channels
|
2018-09-10 21:07:15 +02:00
|
|
|
int independent_channels; ///< number of entries in following arrays (always 1 in mono mode)
|
2018-02-19 15:15:30 +02:00
|
|
|
int64_t *nb_null_samples; ///< (array) current number of continuous zero samples
|
2018-02-19 15:15:42 +02:00
|
|
|
int64_t *start; ///< (array) if silence is detected, this value contains the time of the first zero sample (default/unset = INT64_MIN)
|
2018-02-19 15:16:30 +02:00
|
|
|
int64_t frame_end; ///< pts of the end of the current frame (used to compute duration of silence at EOS)
|
2012-01-04 01:47:09 +03:00
|
|
|
int last_sample_rate; ///< last sample rate to check for sample rate changes
|
2018-02-19 15:16:30 +02:00
|
|
|
AVRational time_base; ///< time_base
|
2013-10-04 20:57:15 +03:00
|
|
|
|
|
|
|
void (*silencedetect)(struct SilenceDetectContext *s, AVFrame *insamples,
|
|
|
|
int nb_samples, int64_t nb_samples_notify,
|
|
|
|
AVRational time_base);
|
2012-01-04 01:47:09 +03:00
|
|
|
} SilenceDetectContext;
|
|
|
|
|
2019-10-08 17:04:52 +02:00
|
|
|
#define MAX_DURATION (24*3600*1000000LL)
|
2012-01-04 01:47:09 +03:00
|
|
|
#define OFFSET(x) offsetof(SilenceDetectContext, x)
|
2012-08-13 14:40:01 +03:00
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
|
2012-01-04 01:47:09 +03:00
|
|
|
static const AVOption silencedetect_options[] = {
|
2012-12-06 17:08:38 +03:00
|
|
|
{ "n", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
|
|
|
|
{ "noise", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
|
2019-10-08 17:04:52 +02:00
|
|
|
{ "d", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=2000000}, 0, MAX_DURATION,FLAGS },
|
|
|
|
{ "duration", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=2000000}, 0, MAX_DURATION,FLAGS },
|
2019-10-08 17:04:53 +02:00
|
|
|
{ "mono", "check each channel separately", OFFSET(mono), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
|
|
|
|
{ "m", "check each channel separately", OFFSET(mono), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
|
2013-09-07 15:13:50 +03:00
|
|
|
{ NULL }
|
2012-01-04 01:47:09 +03:00
|
|
|
};
|
|
|
|
|
2012-06-22 15:33:09 +03:00
|
|
|
AVFILTER_DEFINE_CLASS(silencedetect);
|
2012-01-04 01:47:09 +03:00
|
|
|
|
2018-02-19 15:15:30 +02:00
|
|
|
static void set_meta(AVFrame *insamples, int channel, const char *key, char *value)
|
2012-10-10 00:01:31 +03:00
|
|
|
{
|
2018-02-19 15:15:30 +02:00
|
|
|
char key2[128];
|
2012-10-10 00:01:31 +03:00
|
|
|
|
2018-02-19 15:15:30 +02:00
|
|
|
if (channel)
|
|
|
|
snprintf(key2, sizeof(key2), "lavfi.%s.%d", key, channel);
|
|
|
|
else
|
|
|
|
snprintf(key2, sizeof(key2), "lavfi.%s", key);
|
|
|
|
av_dict_set(&insamples->metadata, key2, value, 0);
|
|
|
|
}
|
2013-10-04 20:57:15 +03:00
|
|
|
static av_always_inline void update(SilenceDetectContext *s, AVFrame *insamples,
|
2018-02-19 15:15:30 +02:00
|
|
|
int is_silence, int current_sample, int64_t nb_samples_notify,
|
2013-10-04 20:57:15 +03:00
|
|
|
AVRational time_base)
|
|
|
|
{
|
2018-09-10 21:07:15 +02:00
|
|
|
int channel = current_sample % s->independent_channels;
|
2013-10-04 20:57:15 +03:00
|
|
|
if (is_silence) {
|
2018-02-19 15:15:42 +02:00
|
|
|
if (s->start[channel] == INT64_MIN) {
|
2018-02-19 15:15:30 +02:00
|
|
|
s->nb_null_samples[channel]++;
|
|
|
|
if (s->nb_null_samples[channel] >= nb_samples_notify) {
|
2018-09-10 21:07:15 +02:00
|
|
|
s->start[channel] = insamples->pts + av_rescale_q(current_sample / s->channels + 1 - nb_samples_notify * s->independent_channels / s->channels,
|
2018-02-19 15:16:04 +02:00
|
|
|
(AVRational){ 1, s->last_sample_rate }, time_base);
|
2018-02-19 15:15:30 +02:00
|
|
|
set_meta(insamples, s->mono ? channel + 1 : 0, "silence_start",
|
|
|
|
av_ts2timestr(s->start[channel], &time_base));
|
|
|
|
if (s->mono)
|
|
|
|
av_log(s, AV_LOG_INFO, "channel: %d | ", channel);
|
2013-10-04 20:57:15 +03:00
|
|
|
av_log(s, AV_LOG_INFO, "silence_start: %s\n",
|
2018-02-19 15:15:30 +02:00
|
|
|
av_ts2timestr(s->start[channel], &time_base));
|
2013-10-04 20:57:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-02-19 15:15:42 +02:00
|
|
|
if (s->start[channel] > INT64_MIN) {
|
2018-02-19 15:16:30 +02:00
|
|
|
int64_t end_pts = insamples ? insamples->pts + av_rescale_q(current_sample / s->channels,
|
|
|
|
(AVRational){ 1, s->last_sample_rate }, time_base)
|
|
|
|
: s->frame_end;
|
2018-02-19 15:15:30 +02:00
|
|
|
int64_t duration_ts = end_pts - s->start[channel];
|
2018-02-19 15:16:30 +02:00
|
|
|
if (insamples) {
|
|
|
|
set_meta(insamples, s->mono ? channel + 1 : 0, "silence_end",
|
|
|
|
av_ts2timestr(end_pts, &time_base));
|
|
|
|
set_meta(insamples, s->mono ? channel + 1 : 0, "silence_duration",
|
|
|
|
av_ts2timestr(duration_ts, &time_base));
|
|
|
|
}
|
2018-02-19 15:15:30 +02:00
|
|
|
if (s->mono)
|
|
|
|
av_log(s, AV_LOG_INFO, "channel: %d | ", channel);
|
|
|
|
av_log(s, AV_LOG_INFO, "silence_end: %s | silence_duration: %s\n",
|
|
|
|
av_ts2timestr(end_pts, &time_base),
|
|
|
|
av_ts2timestr(duration_ts, &time_base));
|
2013-10-04 20:57:15 +03:00
|
|
|
}
|
2018-02-19 15:15:42 +02:00
|
|
|
s->nb_null_samples[channel] = 0;
|
|
|
|
s->start[channel] = INT64_MIN;
|
2013-10-04 20:57:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SILENCE_DETECT(name, type) \
|
|
|
|
static void silencedetect_##name(SilenceDetectContext *s, AVFrame *insamples, \
|
|
|
|
int nb_samples, int64_t nb_samples_notify, \
|
|
|
|
AVRational time_base) \
|
|
|
|
{ \
|
|
|
|
const type *p = (const type *)insamples->data[0]; \
|
|
|
|
const type noise = s->noise; \
|
|
|
|
int i; \
|
|
|
|
\
|
|
|
|
for (i = 0; i < nb_samples; i++, p++) \
|
2018-02-19 15:15:30 +02:00
|
|
|
update(s, insamples, *p < noise && *p > -noise, i, \
|
2013-10-04 20:57:15 +03:00
|
|
|
nb_samples_notify, time_base); \
|
|
|
|
}
|
|
|
|
|
|
|
|
SILENCE_DETECT(dbl, double)
|
|
|
|
SILENCE_DETECT(flt, float)
|
|
|
|
SILENCE_DETECT(s32, int32_t)
|
|
|
|
SILENCE_DETECT(s16, int16_t)
|
|
|
|
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
SilenceDetectContext *s = ctx->priv;
|
2018-02-19 15:15:42 +02:00
|
|
|
int c;
|
2013-10-04 20:57:15 +03:00
|
|
|
|
2018-02-19 15:16:04 +02:00
|
|
|
s->channels = inlink->channels;
|
2019-10-08 17:04:52 +02:00
|
|
|
s->duration = av_rescale(s->duration, inlink->sample_rate, AV_TIME_BASE);
|
2018-09-10 21:07:15 +02:00
|
|
|
s->independent_channels = s->mono ? s->channels : 1;
|
|
|
|
s->nb_null_samples = av_mallocz_array(sizeof(*s->nb_null_samples), s->independent_channels);
|
2018-02-19 15:15:30 +02:00
|
|
|
if (!s->nb_null_samples)
|
|
|
|
return AVERROR(ENOMEM);
|
2018-09-10 21:07:15 +02:00
|
|
|
s->start = av_malloc_array(sizeof(*s->start), s->independent_channels);
|
2018-02-19 15:15:30 +02:00
|
|
|
if (!s->start)
|
|
|
|
return AVERROR(ENOMEM);
|
2018-09-10 21:07:15 +02:00
|
|
|
for (c = 0; c < s->independent_channels; c++)
|
2018-02-19 15:15:42 +02:00
|
|
|
s->start[c] = INT64_MIN;
|
2018-02-19 15:15:30 +02:00
|
|
|
|
2013-10-04 20:57:15 +03:00
|
|
|
switch (inlink->format) {
|
|
|
|
case AV_SAMPLE_FMT_DBL: s->silencedetect = silencedetect_dbl; break;
|
|
|
|
case AV_SAMPLE_FMT_FLT: s->silencedetect = silencedetect_flt; break;
|
|
|
|
case AV_SAMPLE_FMT_S32:
|
|
|
|
s->noise *= INT32_MAX;
|
|
|
|
s->silencedetect = silencedetect_s32;
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_S16:
|
|
|
|
s->noise *= INT16_MAX;
|
|
|
|
s->silencedetect = silencedetect_s16;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
|
2012-01-04 01:47:09 +03:00
|
|
|
{
|
2013-10-02 13:28:04 +03:00
|
|
|
SilenceDetectContext *s = inlink->dst->priv;
|
2013-07-14 01:22:12 +03:00
|
|
|
const int nb_channels = inlink->channels;
|
2012-01-04 01:47:09 +03:00
|
|
|
const int srate = inlink->sample_rate;
|
2013-03-10 03:30:30 +03:00
|
|
|
const int nb_samples = insamples->nb_samples * nb_channels;
|
2019-10-08 17:04:52 +02:00
|
|
|
const int64_t nb_samples_notify = s->duration * (s->mono ? 1 : nb_channels);
|
2018-02-19 15:15:30 +02:00
|
|
|
int c;
|
2012-01-04 01:47:09 +03:00
|
|
|
|
|
|
|
// scale number of null samples to the new sample rate
|
2013-10-02 13:28:04 +03:00
|
|
|
if (s->last_sample_rate && s->last_sample_rate != srate)
|
2018-09-10 21:07:15 +02:00
|
|
|
for (c = 0; c < s->independent_channels; c++) {
|
2018-02-19 15:15:30 +02:00
|
|
|
s->nb_null_samples[c] = srate * s->nb_null_samples[c] / s->last_sample_rate;
|
|
|
|
}
|
2013-10-02 13:28:04 +03:00
|
|
|
s->last_sample_rate = srate;
|
2018-02-19 15:16:30 +02:00
|
|
|
s->time_base = inlink->time_base;
|
|
|
|
s->frame_end = insamples->pts + av_rescale_q(insamples->nb_samples,
|
|
|
|
(AVRational){ 1, s->last_sample_rate }, inlink->time_base);
|
2012-01-04 01:47:09 +03:00
|
|
|
|
2013-10-04 20:57:15 +03:00
|
|
|
s->silencedetect(s, insamples, nb_samples, nb_samples_notify,
|
|
|
|
inlink->time_base);
|
2012-01-04 01:47:09 +03:00
|
|
|
|
2012-11-28 15:53:48 +03:00
|
|
|
return ff_filter_frame(inlink->dst->outputs[0], insamples);
|
2012-01-04 01:47:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2012-05-16 03:27:31 +03:00
|
|
|
AVFilterChannelLayouts *layouts = NULL;
|
2012-12-22 01:38:59 +03:00
|
|
|
static const enum AVSampleFormat sample_fmts[] = {
|
2012-01-04 01:47:09 +03:00
|
|
|
AV_SAMPLE_FMT_DBL,
|
2013-10-04 20:57:15 +03:00
|
|
|
AV_SAMPLE_FMT_FLT,
|
|
|
|
AV_SAMPLE_FMT_S32,
|
|
|
|
AV_SAMPLE_FMT_S16,
|
2012-01-04 01:47:09 +03:00
|
|
|
AV_SAMPLE_FMT_NONE
|
|
|
|
};
|
2015-04-03 19:55:18 +02:00
|
|
|
int ret;
|
2012-01-04 01:47:09 +03:00
|
|
|
|
2012-05-16 03:27:31 +03:00
|
|
|
layouts = ff_all_channel_layouts();
|
|
|
|
if (!layouts)
|
2012-01-04 01:47:09 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2015-04-03 19:55:18 +02:00
|
|
|
ret = ff_set_common_channel_layouts(ctx, layouts);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-01-04 01:47:09 +03:00
|
|
|
|
2021-08-10 01:25:31 +02:00
|
|
|
ret = ff_set_common_formats_from_list(ctx, sample_fmts);
|
2015-04-03 19:55:18 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-01-04 01:47:09 +03:00
|
|
|
|
2021-08-10 01:25:31 +02:00
|
|
|
return ff_set_common_all_samplerates(ctx);
|
2012-01-04 01:47:09 +03:00
|
|
|
}
|
|
|
|
|
2018-02-19 15:16:30 +02:00
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
SilenceDetectContext *s = ctx->priv;
|
|
|
|
int c;
|
|
|
|
|
2018-09-10 21:07:15 +02:00
|
|
|
for (c = 0; c < s->independent_channels; c++)
|
2018-02-19 15:16:30 +02:00
|
|
|
if (s->start[c] > INT64_MIN)
|
|
|
|
update(s, NULL, 0, c, 0, s->time_base);
|
|
|
|
av_freep(&s->nb_null_samples);
|
|
|
|
av_freep(&s->start);
|
|
|
|
}
|
|
|
|
|
2012-11-28 22:01:59 +03:00
|
|
|
static const AVFilterPad silencedetect_inputs[] = {
|
|
|
|
{
|
2013-09-07 15:13:50 +03:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
2013-10-04 20:57:15 +03:00
|
|
|
.config_props = config_input,
|
2013-09-07 15:13:50 +03:00
|
|
|
.filter_frame = filter_frame,
|
2012-11-28 22:01:59 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad silencedetect_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_af_silencedetect = {
|
2012-01-04 01:47:09 +03:00
|
|
|
.name = "silencedetect",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Detect silence."),
|
|
|
|
.priv_size = sizeof(SilenceDetectContext),
|
|
|
|
.query_formats = query_formats,
|
2018-02-19 15:16:30 +02:00
|
|
|
.uninit = uninit,
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(silencedetect_inputs),
|
|
|
|
FILTER_OUTPUTS(silencedetect_outputs),
|
2012-11-28 22:01:59 +03:00
|
|
|
.priv_class = &silencedetect_class,
|
2012-01-04 01:47:09 +03:00
|
|
|
};
|