2012-01-03 23:47:09 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Clément Bœsch <ubitux@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 15:08:38 +01:00
|
|
|
#include <float.h> /* DBL_MAX */
|
|
|
|
|
2012-11-13 14:16:48 +01:00
|
|
|
#include "libavutil/channel_layout.h"
|
2012-01-03 23:47:09 +01:00
|
|
|
#include "libavutil/opt.h"
|
2012-02-15 00:21:14 +01:00
|
|
|
#include "libavutil/timestamp.h"
|
2012-05-12 17:58:14 +02:00
|
|
|
#include "audio.h"
|
2012-05-16 02:27:31 +02:00
|
|
|
#include "formats.h"
|
2012-01-03 23:47:09 +01:00
|
|
|
#include "avfilter.h"
|
2012-06-22 14:33:09 +02:00
|
|
|
#include "internal.h"
|
2012-01-03 23:47:09 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const AVClass *class;
|
|
|
|
double noise; ///< noise amplitude ratio
|
2012-10-21 01:28:19 +02:00
|
|
|
double duration; ///< minimum duration of silence until notification
|
2012-01-03 23:47:09 +01:00
|
|
|
int64_t nb_null_samples; ///< current number of continuous zero samples
|
2012-02-15 00:21:14 +01:00
|
|
|
int64_t start; ///< if silence is detected, this value contains the time of the first zero sample
|
2012-01-03 23:47:09 +01:00
|
|
|
int last_sample_rate; ///< last sample rate to check for sample rate changes
|
|
|
|
} SilenceDetectContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(SilenceDetectContext, x)
|
2012-08-13 13:40:01 +02:00
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
|
2012-01-03 23:47:09 +01:00
|
|
|
static const AVOption silencedetect_options[] = {
|
2012-12-06 15:08:38 +01: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 01:28:19 +02: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 },
|
2012-01-03 23:47:09 +01:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2012-06-22 14:33:09 +02:00
|
|
|
AVFILTER_DEFINE_CLASS(silencedetect);
|
2012-01-03 23:47:09 +01:00
|
|
|
|
2012-06-26 23:27:59 +02:00
|
|
|
static av_cold int init(AVFilterContext *ctx, const char *args)
|
2012-01-03 23:47:09 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
SilenceDetectContext *silence = ctx->priv;
|
|
|
|
|
|
|
|
silence->class = &silencedetect_class;
|
|
|
|
av_opt_set_defaults(silence);
|
|
|
|
|
2012-08-09 00:49:27 +02:00
|
|
|
if ((ret = av_set_options_string(silence, args, "=", ":")) < 0)
|
2012-01-03 23:47:09 +01:00
|
|
|
return ret;
|
|
|
|
|
2012-10-09 22:34:35 +02:00
|
|
|
av_opt_free(silence);
|
2012-01-03 23:47:09 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-10 01:30:30 +01:00
|
|
|
static char *get_metadata_val(AVFrame *insamples, const char *key)
|
2012-10-09 23:01:31 +02:00
|
|
|
{
|
|
|
|
AVDictionaryEntry *e = av_dict_get(insamples->metadata, key, NULL, 0);
|
|
|
|
return e && e->value ? e->value : NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-10 01:30:30 +01:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
|
2012-01-03 23:47:09 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
SilenceDetectContext *silence = inlink->dst->priv;
|
|
|
|
const int nb_channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
|
|
|
|
const int srate = inlink->sample_rate;
|
2013-03-10 01:30:30 +01:00
|
|
|
const int nb_samples = insamples->nb_samples * nb_channels;
|
|
|
|
const int64_t nb_samples_notify = srate * silence->duration * nb_channels;
|
2012-01-03 23:47:09 +01: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-09 23:01:31 +02:00
|
|
|
// TODO: document metadata
|
2012-01-03 23:47:09 +01: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 01:28:19 +02:00
|
|
|
silence->start = insamples->pts - (int64_t)(silence->duration / av_q2d(inlink->time_base) + .5);
|
2012-10-09 23:01:31 +02: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-03 23:47:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2012-10-09 23:01:31 +02: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-03 23:47:09 +01:00
|
|
|
av_log(silence, AV_LOG_INFO,
|
2012-02-15 00:21:14 +01:00
|
|
|
"silence_end: %s | silence_duration: %s\n",
|
2012-10-09 23:01:31 +02:00
|
|
|
get_metadata_val(insamples, "lavfi.silence_end"),
|
|
|
|
get_metadata_val(insamples, "lavfi.silence_duration"));
|
|
|
|
}
|
2012-01-03 23:47:09 +01:00
|
|
|
silence->nb_null_samples = silence->start = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 13:53:48 +01:00
|
|
|
return ff_filter_frame(inlink->dst->outputs[0], insamples);
|
2012-01-03 23:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterFormats *formats = NULL;
|
2012-05-16 02:27:31 +02:00
|
|
|
AVFilterChannelLayouts *layouts = NULL;
|
2012-12-21 23:38:59 +01:00
|
|
|
static const enum AVSampleFormat sample_fmts[] = {
|
2012-01-03 23:47:09 +01:00
|
|
|
AV_SAMPLE_FMT_DBL,
|
|
|
|
AV_SAMPLE_FMT_NONE
|
|
|
|
};
|
|
|
|
|
2012-05-16 02:27:31 +02:00
|
|
|
layouts = ff_all_channel_layouts();
|
|
|
|
if (!layouts)
|
2012-01-03 23:47:09 +01:00
|
|
|
return AVERROR(ENOMEM);
|
2012-05-16 02:27:31 +02:00
|
|
|
ff_set_common_channel_layouts(ctx, layouts);
|
2012-01-03 23:47:09 +01:00
|
|
|
|
2012-06-16 11:47:46 +02:00
|
|
|
formats = ff_make_format_list(sample_fmts);
|
2012-01-03 23:47:09 +01:00
|
|
|
if (!formats)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-06-16 11:47:46 +02:00
|
|
|
ff_set_common_formats(ctx, formats);
|
2012-01-03 23:47:09 +01:00
|
|
|
|
2012-05-16 02:27:31 +02:00
|
|
|
formats = ff_all_samplerates();
|
|
|
|
if (!formats)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ff_set_common_samplerates(ctx, formats);
|
|
|
|
|
2012-01-03 23:47:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-28 20:01:59 +01:00
|
|
|
static const AVFilterPad silencedetect_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.get_audio_buffer = ff_null_get_audio_buffer,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad silencedetect_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2012-01-03 23:47:09 +01:00
|
|
|
AVFilter avfilter_af_silencedetect = {
|
|
|
|
.name = "silencedetect",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Detect silence."),
|
|
|
|
.priv_size = sizeof(SilenceDetectContext),
|
|
|
|
.init = init,
|
|
|
|
.query_formats = query_formats,
|
2012-11-28 20:01:59 +01:00
|
|
|
.inputs = silencedetect_inputs,
|
|
|
|
.outputs = silencedetect_outputs,
|
|
|
|
.priv_class = &silencedetect_class,
|
2012-01-03 23:47:09 +01:00
|
|
|
};
|