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-06-22 12:59:29 +02:00
|
|
|
#include "libavutil/audioconvert.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;
|
|
|
|
char *noise_str; ///< noise option string
|
|
|
|
double noise; ///< noise amplitude ratio
|
|
|
|
int duration; ///< minimum duration of silence until notification
|
|
|
|
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-08-13 13:40:01 +02:00
|
|
|
{ "n", "set noise tolerance", OFFSET(noise_str), AV_OPT_TYPE_STRING, {.str="-60dB"}, CHAR_MIN, CHAR_MAX, FLAGS },
|
|
|
|
{ "noise", "set noise tolerance", OFFSET(noise_str), AV_OPT_TYPE_STRING, {.str="-60dB"}, CHAR_MIN, CHAR_MAX, FLAGS },
|
2012-09-05 14:26:01 +02:00
|
|
|
{ "d", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, FLAGS },
|
|
|
|
{ "duration", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, 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;
|
|
|
|
char *tail;
|
|
|
|
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;
|
|
|
|
|
|
|
|
silence->noise = strtod(silence->noise_str, &tail);
|
|
|
|
if (!strcmp(tail, "dB")) {
|
|
|
|
silence->noise = pow(10, silence->noise/20);
|
|
|
|
} else if (*tail) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for noise parameter.\n",
|
|
|
|
silence->noise_str);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-09 22:10:38 +02:00
|
|
|
static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *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;
|
|
|
|
const int nb_samples = insamples->audio->nb_samples * nb_channels;
|
|
|
|
const int64_t nb_samples_notify = srate * silence->duration * nb_channels;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
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-02-15 00:21:14 +01:00
|
|
|
silence->start = insamples->pts - silence->duration / av_q2d(inlink->time_base);
|
2012-01-03 23:47:09 +01:00
|
|
|
av_log(silence, AV_LOG_INFO,
|
2012-02-15 00:21:14 +01:00
|
|
|
"silence_start: %s\n", av_ts2timestr(silence->start, &inlink->time_base));
|
2012-01-03 23:47:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2012-02-15 00:21:14 +01:00
|
|
|
if (silence->start)
|
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",
|
|
|
|
av_ts2timestr(insamples->pts, &inlink->time_base),
|
|
|
|
av_ts2timestr(insamples->pts - silence->start, &inlink->time_base));
|
2012-01-03 23:47:09 +01:00
|
|
|
silence->nb_null_samples = silence->start = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-09 22:10:38 +02:00
|
|
|
return ff_filter_samples(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-01-03 23:47:09 +01:00
|
|
|
enum AVSampleFormat sample_fmts[] = {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVFilter avfilter_af_silencedetect = {
|
|
|
|
.name = "silencedetect",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Detect silence."),
|
|
|
|
.priv_size = sizeof(SilenceDetectContext),
|
|
|
|
.init = init,
|
|
|
|
.query_formats = query_formats,
|
|
|
|
|
|
|
|
.inputs = (const AVFilterPad[]) {
|
|
|
|
{ .name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
2012-05-10 22:41:29 +02:00
|
|
|
.get_audio_buffer = ff_null_get_audio_buffer,
|
2012-01-03 23:47:09 +01:00
|
|
|
.filter_samples = filter_samples, },
|
|
|
|
{ .name = NULL }
|
|
|
|
},
|
|
|
|
.outputs = (const AVFilterPad[]) {
|
|
|
|
{ .name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO, },
|
|
|
|
{ .name = NULL }
|
|
|
|
},
|
2012-08-13 13:40:01 +02:00
|
|
|
.priv_class = &silencedetect_class,
|
2012-01-03 23:47:09 +01:00
|
|
|
};
|