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
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
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
|
|
|
|
} 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-03-10 03:30:30 +03:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
|
2012-01-04 01:47:09 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
SilenceDetectContext *silence = 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;
|
|
|
|
const int64_t nb_samples_notify = srate * silence->duration * nb_channels;
|
2012-01-04 01:47:09 +03:00
|
|
|
|
|
|
|
// scale number of null samples to the new sample rate
|
|
|
|
if (silence->last_sample_rate && silence->last_sample_rate != srate)
|
|
|
|
silence->nb_null_samples =
|
|
|
|
srate * silence->nb_null_samples / silence->last_sample_rate;
|
|
|
|
silence->last_sample_rate = srate;
|
|
|
|
|
|
|
|
// TODO: support more sample formats
|
2012-10-10 00:01:31 +03:00
|
|
|
// TODO: document metadata
|
2012-01-04 01:47:09 +03:00
|
|
|
if (insamples->format == AV_SAMPLE_FMT_DBL) {
|
|
|
|
double *p = (double *)insamples->data[0];
|
|
|
|
|
|
|
|
for (i = 0; i < nb_samples; i++, p++) {
|
|
|
|
if (*p < silence->noise && *p > -silence->noise) {
|
|
|
|
if (!silence->start) {
|
|
|
|
silence->nb_null_samples++;
|
|
|
|
if (silence->nb_null_samples >= nb_samples_notify) {
|
2012-10-21 02:28:19 +03:00
|
|
|
silence->start = insamples->pts - (int64_t)(silence->duration / av_q2d(inlink->time_base) + .5);
|
2012-10-10 00:01:31 +03:00
|
|
|
av_dict_set(&insamples->metadata, "lavfi.silence_start",
|
|
|
|
av_ts2timestr(silence->start, &inlink->time_base), 0);
|
|
|
|
av_log(silence, AV_LOG_INFO, "silence_start: %s\n",
|
|
|
|
get_metadata_val(insamples, "lavfi.silence_start"));
|
2012-01-04 01:47:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2012-10-10 00:01:31 +03:00
|
|
|
if (silence->start) {
|
|
|
|
av_dict_set(&insamples->metadata, "lavfi.silence_end",
|
|
|
|
av_ts2timestr(insamples->pts, &inlink->time_base), 0);
|
|
|
|
av_dict_set(&insamples->metadata, "lavfi.silence_duration",
|
|
|
|
av_ts2timestr(insamples->pts - silence->start, &inlink->time_base), 0);
|
2012-01-04 01:47:09 +03:00
|
|
|
av_log(silence, AV_LOG_INFO,
|
2012-02-15 02:21:14 +03:00
|
|
|
"silence_end: %s | silence_duration: %s\n",
|
2012-10-10 00:01:31 +03:00
|
|
|
get_metadata_val(insamples, "lavfi.silence_end"),
|
|
|
|
get_metadata_val(insamples, "lavfi.silence_duration"));
|
|
|
|
}
|
2012-01-04 01:47:09 +03:00
|
|
|
silence->nb_null_samples = silence->start = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
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,
|
|
|
|
.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 }
|
|
|
|
};
|
|
|
|
|
2012-01-04 01:47:09 +03:00
|
|
|
AVFilter avfilter_af_silencedetect = {
|
|
|
|
.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
|
|
|
};
|