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