From patchwork Mon Feb 19 13:15:30 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gaullier Nicolas X-Patchwork-Id: 7652 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.150.33 with SMTP id c30csp1506721jai; Mon, 19 Feb 2018 05:15:42 -0800 (PST) X-Google-Smtp-Source: AH8x2271f/X680Lw3bg8byWoKEK3avO7Q5ZFMqKlC8uWa+trJqDpEkS0GBy2vK6ERel4fpDUKfmZ X-Received: by 10.223.162.144 with SMTP id s16mr12952122wra.173.1519046141961; Mon, 19 Feb 2018 05:15:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519046141; cv=none; d=google.com; s=arc-20160816; b=F2gbWphfsoBXkqJSulEZMLWCF146CTMVfnN99I9aC97SpOG3c9KKtUHsUrtP+A+bvn 5al1m+peC349KTrEv/O33wAGIVbw0eOWbTNpNObQSSGpov8l8yvV/CzltivZyhLW833g zLWwoJyo2eFFVUPBG6P+GYHl8COl6t82bNuaHVo6nPJRuaPMX6msuW2991H1TCQjVoj4 /BBaoJK0OAHkj2ym6VRDhteT2Xco8xkaFcOXpF35b8fOHiF48DCgLnPpqh400rp5Iase 3Wk8EKKMldqK5VsT7ub5Zuof+pN2yGkA3wC0imtJbJBQQqvVyUbyPhAc3ooA/Hnq6AG6 50zA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=YfS+/bMthrZN0vT6CtSLmEskHpfoOxi1Rs9x0mkVvRg=; b=sfR3NN53IzDG683P0hGZHxxODGpR2QvlEjCjBFDwAsKgx24El5rdUe6xNrQG072GD5 5xsin7bu0TXzPrc9Sk9djFQ3W7iDWZ3gDavV/8xtygOk12rqzUW+knS+XCrupHmyvHr5 C7ahTonkolj/P3PmWd7T3LuaGSaPJg3iTpGFDQWSW+oKOK5+c9DoCxSPvarGorWYNc/N CH82xOtGIvsVh3PslBn0jSI6tAcpfuzaxxSUDrnednSb74evUSwv47U4mDmpX4AeZpix h0yqBY0/CmjxluY7cNsi/kOWJTxaLgPwoxeR6kIvh7VKzjRgYWb4/VG51DXpWA2VFKqj LyQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@arkena.com header.s=20150421 header.b=HQIfrqM5; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 128si9943425wms.31.2018.02.19.05.15.41; Mon, 19 Feb 2018 05:15:41 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@arkena.com header.s=20150421 header.b=HQIfrqM5; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 80F1D689F3C; Mon, 19 Feb 2018 15:15:39 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smtp-1.arkena.net (smtp-1.arkena.net [95.81.173.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3B0DC689EEF for ; Mon, 19 Feb 2018 15:15:33 +0200 (EET) Received: from secu2 (unknown [10.180.103.10]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp-1.arkena.net (Postfix) with ESMTPSA id 3zlPPd1PN6zMcWc; Mon, 19 Feb 2018 13:15:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arkena.com; s=20150421; t=1519046133; bh=yyeIpJvucz9TSBR4PirEJoSSzliu+2b9Z9fMwtrEEY4=; h=From:To:Cc:Subject:Date:Message-Id; b=HQIfrqM5Xpu/ZTK8Z/iuYCjrS3HgaFcI8YeOFCB1ER2V/eVanP49EQ87pqY7g+Hex +Jf5fj9lYz5VA1OO5QW4p1OzPpG/gZN4Lo9OVmBx75poRZ4zsvv5uRnArOAUpasEds Mf4194SmBlbIZvyahUTIVPnXbDHLDHeQn7LY1Kug= Received: from arkena.com (unknown [172.16.3.159]) by secu2 (Postfix) with ESMTP id 2C77A3FA71; Mon, 19 Feb 2018 16:05:21 +0100 (CET) From: Nicolas Gaullier To: ffmpeg-devel@ffmpeg.org Date: Mon, 19 Feb 2018 14:15:30 +0100 Message-Id: <20180219131530.1344-1-nicolas.gaullier@arkena.com> X-Mailer: git-send-email 2.15.0 Subject: [FFmpeg-devel] [PATCH v3 1/8] lavfi/silencedetect: Add mono mode X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Nicolas Gaullier MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" In mono mode, silence is detected in any single channel instead of all of them simultaneously --- libavfilter/af_silencedetect.c | 78 +++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 27 deletions(-) diff --git a/libavfilter/af_silencedetect.c b/libavfilter/af_silencedetect.c index b048d63738..c53b9d9218 100644 --- a/libavfilter/af_silencedetect.c +++ b/libavfilter/af_silencedetect.c @@ -36,8 +36,10 @@ typedef struct SilenceDetectContext { const AVClass *class; double noise; ///< noise amplitude ratio double duration; ///< minimum duration of silence until notification - int64_t nb_null_samples; ///< current number of continuous zero samples - int64_t start; ///< if silence is detected, this value contains the time of the first zero sample + int mono; ///< mono mode : check each channel separately (default = check when ALL channels are silent) + int independant_channels; ///< number of entries in following arrays (always 1 in mono mode) + int64_t *nb_null_samples; ///< (array) current number of continuous zero samples + int64_t *start; ///< (array) if silence is detected, this value contains the time of the first zero sample int last_sample_rate; ///< last sample rate to check for sample rate changes void (*silencedetect)(struct SilenceDetectContext *s, AVFrame *insamples, @@ -52,44 +54,55 @@ static const AVOption silencedetect_options[] = { { "noise", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS }, { "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 }, + { "mono", "check each channel separately", OFFSET(mono), AV_OPT_TYPE_BOOL, {.i64=0.}, 0, 1, FLAGS }, { NULL } }; AVFILTER_DEFINE_CLASS(silencedetect); -static char *get_metadata_val(AVFrame *insamples, const char *key) +static void set_meta(AVFrame *insamples, int channel, const char *key, char *value) { - AVDictionaryEntry *e = av_dict_get(insamples->metadata, key, NULL, 0); - return e && e->value ? e->value : NULL; -} + char key2[128]; + if (channel) + snprintf(key2, sizeof(key2), "lavfi.%s.%d", key, channel); + else + snprintf(key2, sizeof(key2), "lavfi.%s", key); + av_dict_set(&insamples->metadata, key2, value, 0); +} static av_always_inline void update(SilenceDetectContext *s, AVFrame *insamples, - int is_silence, int64_t nb_samples_notify, + int is_silence, int current_sample, int64_t nb_samples_notify, AVRational time_base) { + int channel = current_sample % s->independant_channels; if (is_silence) { - if (!s->start) { - s->nb_null_samples++; - if (s->nb_null_samples >= nb_samples_notify) { - s->start = insamples->pts - (int64_t)(s->duration / av_q2d(time_base) + .5); - av_dict_set(&insamples->metadata, "lavfi.silence_start", - av_ts2timestr(s->start, &time_base), 0); + if (!s->start[channel]) { + s->nb_null_samples[channel]++; + if (s->nb_null_samples[channel] >= nb_samples_notify) { + s->start[channel] = insamples->pts - (int64_t)(s->duration / av_q2d(time_base) + .5); + set_meta(insamples, s->mono ? channel + 1 : 0, "silence_start", + av_ts2timestr(s->start[channel], &time_base)); + if (s->mono) + av_log(s, AV_LOG_INFO, "channel: %d | ", channel); av_log(s, AV_LOG_INFO, "silence_start: %s\n", - get_metadata_val(insamples, "lavfi.silence_start")); + av_ts2timestr(s->start[channel], &time_base)); } } } else { - if (s->start) { - av_dict_set(&insamples->metadata, "lavfi.silence_end", - av_ts2timestr(insamples->pts, &time_base), 0); - av_dict_set(&insamples->metadata, "lavfi.silence_duration", - av_ts2timestr(insamples->pts - s->start, &time_base), 0); - av_log(s, AV_LOG_INFO, - "silence_end: %s | silence_duration: %s\n", - get_metadata_val(insamples, "lavfi.silence_end"), - get_metadata_val(insamples, "lavfi.silence_duration")); + if (s->start[channel]) { + int64_t end_pts = insamples->pts; + int64_t duration_ts = end_pts - s->start[channel]; + set_meta(insamples, s->mono ? channel + 1 : 0, "silence_end", + av_ts2timestr(end_pts, &time_base)); + set_meta(insamples, s->mono ? channel + 1 : 0, "silence_duration", + av_ts2timestr(duration_ts, &time_base)); + if (s->mono) + av_log(s, AV_LOG_INFO, "channel: %d | ", channel); + av_log(s, AV_LOG_INFO, "silence_end: %s | silence_duration: %s\n", + av_ts2timestr(end_pts, &time_base), + av_ts2timestr(duration_ts, &time_base)); } - s->nb_null_samples = s->start = 0; + s->nb_null_samples[channel] = s->start[channel] = 0; } } @@ -103,7 +116,7 @@ static void silencedetect_##name(SilenceDetectContext *s, AVFrame *insamples, int i; \ \ for (i = 0; i < nb_samples; i++, p++) \ - update(s, insamples, *p < noise && *p > -noise, \ + update(s, insamples, *p < noise && *p > -noise, i, \ nb_samples_notify, time_base); \ } @@ -117,6 +130,14 @@ static int config_input(AVFilterLink *inlink) AVFilterContext *ctx = inlink->dst; SilenceDetectContext *s = ctx->priv; + s->independant_channels = s->mono ? inlink->channels : 1; + s->nb_null_samples = av_mallocz_array(sizeof(*s->nb_null_samples), s->independant_channels); + if (!s->nb_null_samples) + return AVERROR(ENOMEM); + s->start = av_mallocz_array(sizeof(*s->start), s->independant_channels); + if (!s->start) + return AVERROR(ENOMEM); + switch (inlink->format) { case AV_SAMPLE_FMT_DBL: s->silencedetect = silencedetect_dbl; break; case AV_SAMPLE_FMT_FLT: s->silencedetect = silencedetect_flt; break; @@ -139,11 +160,14 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) const int nb_channels = inlink->channels; const int srate = inlink->sample_rate; const int nb_samples = insamples->nb_samples * nb_channels; - const int64_t nb_samples_notify = srate * s->duration * nb_channels; + const int64_t nb_samples_notify = srate * s->duration * (s->mono ? 1 : nb_channels); + int c; // scale number of null samples to the new sample rate if (s->last_sample_rate && s->last_sample_rate != srate) - s->nb_null_samples = srate * s->nb_null_samples / s->last_sample_rate; + for (c = 0; c < s->independant_channels; c++) { + s->nb_null_samples[c] = srate * s->nb_null_samples[c] / s->last_sample_rate; + } s->last_sample_rate = srate; // TODO: document metadata