00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "libavutil/audioconvert.h"
00027 #include "libavutil/opt.h"
00028 #include "libavutil/timestamp.h"
00029 #include "audio.h"
00030 #include "formats.h"
00031 #include "avfilter.h"
00032 #include "internal.h"
00033
00034 typedef struct {
00035 const AVClass *class;
00036 char *noise_str;
00037 double noise;
00038 int duration;
00039 int64_t nb_null_samples;
00040 int64_t start;
00041 int last_sample_rate;
00042 } SilenceDetectContext;
00043
00044 #define OFFSET(x) offsetof(SilenceDetectContext, x)
00045 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
00046 static const AVOption silencedetect_options[] = {
00047 { "n", "set noise tolerance", OFFSET(noise_str), AV_OPT_TYPE_STRING, {.str="-60dB"}, CHAR_MIN, CHAR_MAX, FLAGS },
00048 { "noise", "set noise tolerance", OFFSET(noise_str), AV_OPT_TYPE_STRING, {.str="-60dB"}, CHAR_MIN, CHAR_MAX, FLAGS },
00049 { "d", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, FLAGS },
00050 { "duration", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, FLAGS },
00051 { NULL },
00052 };
00053
00054 AVFILTER_DEFINE_CLASS(silencedetect);
00055
00056 static av_cold int init(AVFilterContext *ctx, const char *args)
00057 {
00058 int ret;
00059 char *tail;
00060 SilenceDetectContext *silence = ctx->priv;
00061
00062 silence->class = &silencedetect_class;
00063 av_opt_set_defaults(silence);
00064
00065 if ((ret = av_set_options_string(silence, args, "=", ":")) < 0)
00066 return ret;
00067
00068 silence->noise = strtod(silence->noise_str, &tail);
00069 if (!strcmp(tail, "dB")) {
00070 silence->noise = pow(10, silence->noise/20);
00071 } else if (*tail) {
00072 av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for noise parameter.\n",
00073 silence->noise_str);
00074 return AVERROR(EINVAL);
00075 }
00076
00077 return 0;
00078 }
00079
00080 static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
00081 {
00082 int i;
00083 SilenceDetectContext *silence = inlink->dst->priv;
00084 const int nb_channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
00085 const int srate = inlink->sample_rate;
00086 const int nb_samples = insamples->audio->nb_samples * nb_channels;
00087 const int64_t nb_samples_notify = srate * silence->duration * nb_channels;
00088
00089
00090 if (silence->last_sample_rate && silence->last_sample_rate != srate)
00091 silence->nb_null_samples =
00092 srate * silence->nb_null_samples / silence->last_sample_rate;
00093 silence->last_sample_rate = srate;
00094
00095
00096 if (insamples->format == AV_SAMPLE_FMT_DBL) {
00097 double *p = (double *)insamples->data[0];
00098
00099 for (i = 0; i < nb_samples; i++, p++) {
00100 if (*p < silence->noise && *p > -silence->noise) {
00101 if (!silence->start) {
00102 silence->nb_null_samples++;
00103 if (silence->nb_null_samples >= nb_samples_notify) {
00104 silence->start = insamples->pts - silence->duration / av_q2d(inlink->time_base);
00105 av_log(silence, AV_LOG_INFO,
00106 "silence_start: %s\n", av_ts2timestr(silence->start, &inlink->time_base));
00107 }
00108 }
00109 } else {
00110 if (silence->start)
00111 av_log(silence, AV_LOG_INFO,
00112 "silence_end: %s | silence_duration: %s\n",
00113 av_ts2timestr(insamples->pts, &inlink->time_base),
00114 av_ts2timestr(insamples->pts - silence->start, &inlink->time_base));
00115 silence->nb_null_samples = silence->start = 0;
00116 }
00117 }
00118 }
00119
00120 return ff_filter_samples(inlink->dst->outputs[0], insamples);
00121 }
00122
00123 static int query_formats(AVFilterContext *ctx)
00124 {
00125 AVFilterFormats *formats = NULL;
00126 AVFilterChannelLayouts *layouts = NULL;
00127 enum AVSampleFormat sample_fmts[] = {
00128 AV_SAMPLE_FMT_DBL,
00129 AV_SAMPLE_FMT_NONE
00130 };
00131
00132 layouts = ff_all_channel_layouts();
00133 if (!layouts)
00134 return AVERROR(ENOMEM);
00135 ff_set_common_channel_layouts(ctx, layouts);
00136
00137 formats = ff_make_format_list(sample_fmts);
00138 if (!formats)
00139 return AVERROR(ENOMEM);
00140 ff_set_common_formats(ctx, formats);
00141
00142 formats = ff_all_samplerates();
00143 if (!formats)
00144 return AVERROR(ENOMEM);
00145 ff_set_common_samplerates(ctx, formats);
00146
00147 return 0;
00148 }
00149
00150 AVFilter avfilter_af_silencedetect = {
00151 .name = "silencedetect",
00152 .description = NULL_IF_CONFIG_SMALL("Detect silence."),
00153 .priv_size = sizeof(SilenceDetectContext),
00154 .init = init,
00155 .query_formats = query_formats,
00156
00157 .inputs = (const AVFilterPad[]) {
00158 { .name = "default",
00159 .type = AVMEDIA_TYPE_AUDIO,
00160 .get_audio_buffer = ff_null_get_audio_buffer,
00161 .filter_samples = filter_samples, },
00162 { .name = NULL }
00163 },
00164 .outputs = (const AVFilterPad[]) {
00165 { .name = "default",
00166 .type = AVMEDIA_TYPE_AUDIO, },
00167 { .name = NULL }
00168 },
00169 .priv_class = &silencedetect_class,
00170 };