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
|
2012-10-21 02:28:19 +03:00
|
|
|
double duration; ///< minimum duration of silence until notification
|
2012-01-04 01:47:09 +03:00
|
|
|
int64_t nb_null_samples; ///< current number of continuous zero samples
|
2012-02-15 02:21:14 +03:00
|
|
|
int64_t start; ///< if silence is detected, this value contains the time of the first zero sample
|
2012-01-04 01:47:09 +03:00
|
|
|
int last_sample_rate; ///< last sample rate to check for sample rate changes
|
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;
|
|
|
|
|
|
|
|
#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 },
|
2012-10-21 02:28:19 +03:00
|
|
|
{ "d", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, FLAGS },
|
|
|
|
{ "duration", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, 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
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
static char *get_metadata_val(AVFrame *insamples, const char *key)
|
2012-10-10 00:01:31 +03:00
|
|
|
{
|
|
|
|
AVDictionaryEntry *e = av_dict_get(insamples->metadata, key, NULL, 0);
|
|
|
|
return e && e->value ? e->value : NULL;
|
|
|
|
}
|
|
|
|
|
2013-10-04 20:57:15 +03:00
|
|
|
static av_always_inline void update(SilenceDetectContext *s, AVFrame *insamples,
|
|
|
|
int is_silence, int64_t nb_samples_notify,
|
|
|
|
AVRational time_base)
|
|
|
|
{
|
|
|
|
if (is_silence) {
|
|
|
|
if (!s->start) {
|
|
|
|
s->nb_null_samples++;
|
|
|
|
if (s->nb_null_samples >= nb_samples_notify) {
|
|
|
|
s->start = insamples->pts - (int64_t)(s->duration / av_q2d(time_base) + .5);
|
|
|
|
av_dict_set(&insamples->metadata, "lavfi.silence_start",
|
|
|
|
av_ts2timestr(s->start, &time_base), 0);
|
|
|
|
av_log(s, AV_LOG_INFO, "silence_start: %s\n",
|
|
|
|
get_metadata_val(insamples, "lavfi.silence_start"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (s->start) {
|
|
|
|
av_dict_set(&insamples->metadata, "lavfi.silence_end",
|
|
|
|
av_ts2timestr(insamples->pts, &time_base), 0);
|
|
|
|
av_dict_set(&insamples->metadata, "lavfi.silence_duration",
|
|
|
|
av_ts2timestr(insamples->pts - s->start, &time_base), 0);
|
|
|
|
av_log(s, AV_LOG_INFO,
|
|
|
|
"silence_end: %s | silence_duration: %s\n",
|
|
|
|
get_metadata_val(insamples, "lavfi.silence_end"),
|
|
|
|
get_metadata_val(insamples, "lavfi.silence_duration"));
|
|
|
|
}
|
|
|
|
s->nb_null_samples = s->start = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#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++) \
|
|
|
|
update(s, insamples, *p < noise && *p > -noise, \
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
2013-10-02 13:28:04 +03:00
|
|
|
const int64_t nb_samples_notify = srate * s->duration * nb_channels;
|
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)
|
|
|
|
s->nb_null_samples = srate * s->nb_null_samples / s->last_sample_rate;
|
|
|
|
s->last_sample_rate = srate;
|
2012-01-04 01:47:09 +03:00
|
|
|
|
2012-10-10 00:01:31 +03:00
|
|
|
// TODO: document metadata
|
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)
|
|
|
|
{
|
|
|
|
AVFilterFormats *formats = NULL;
|
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
|
|
|
|
};
|
|
|
|
|
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);
|
2012-05-16 03:27:31 +03:00
|
|
|
ff_set_common_channel_layouts(ctx, layouts);
|
2012-01-04 01:47:09 +03:00
|
|
|
|
2012-06-16 12:47:46 +03:00
|
|
|
formats = ff_make_format_list(sample_fmts);
|
2012-01-04 01:47:09 +03:00
|
|
|
if (!formats)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-06-16 12:47:46 +03:00
|
|
|
ff_set_common_formats(ctx, formats);
|
2012-01-04 01:47:09 +03:00
|
|
|
|
2012-05-16 03:27:31 +03:00
|
|
|
formats = ff_all_samplerates();
|
|
|
|
if (!formats)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ff_set_common_samplerates(ctx, formats);
|
|
|
|
|
2012-01-04 01:47:09 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad silencedetect_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-10-29 13:50:56 +03:00
|
|
|
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,
|
2012-11-28 22:01:59 +03:00
|
|
|
.inputs = silencedetect_inputs,
|
|
|
|
.outputs = silencedetect_outputs,
|
|
|
|
.priv_class = &silencedetect_class,
|
2012-01-04 01:47:09 +03:00
|
|
|
};
|