[FFmpeg-devel,v2,4/8] avfilter/silencedetect: fix silence_start accuracy

Submitted by Gaullier Nicolas on Feb. 12, 2018, 9:48 a.m.

Details

Message ID 20180212094830.9304-4-nicolas.gaullier@arkena.com
State New
Headers show

Commit Message

Gaullier Nicolas Feb. 12, 2018, 9:48 a.m.
From: nicolas gaullier <nicolas.gaullier@arkena.com>

---
 libavfilter/af_silencedetect.c               |  7 +++++--
 tests/ref/fate/filter-metadata-silencedetect | 26 +++++++++++++-------------
 2 files changed, 18 insertions(+), 15 deletions(-)

Patch hide | download patch | download mbox

diff --git a/libavfilter/af_silencedetect.c b/libavfilter/af_silencedetect.c
index b4184c2486..749c3fbcb7 100644
--- a/libavfilter/af_silencedetect.c
+++ b/libavfilter/af_silencedetect.c
@@ -37,6 +37,7 @@  typedef struct SilenceDetectContext {
     double noise;               ///< noise amplitude ratio
     double duration;            ///< minimum duration of silence until notification
     int mono;                   ///< mono mode : check each channel separately (default = check when ALL channels are silent)
+    int channels;               ///< number of channels
     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 (default/unset = INT64_MIN)
@@ -79,7 +80,8 @@  static av_always_inline void update(SilenceDetectContext *s, AVFrame *insamples,
         if (s->start[channel] == INT64_MIN) {
             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);
+                s->start[channel] = insamples->pts + av_rescale_q(current_sample / s->channels + 1 - nb_samples_notify * s->independant_channels / s->channels,
+                        (AVRational){ 1, s->last_sample_rate }, time_base);
                 set_meta(insamples, s->mono ? channel + 1 : 0, "silence_start",
                         av_ts2timestr(s->start[channel], &time_base));
                 if (s->mono)
@@ -132,7 +134,8 @@  static int config_input(AVFilterLink *inlink)
     SilenceDetectContext *s = ctx->priv;
     int c;
 
-    s->independant_channels = s->mono ? inlink->channels : 1;
+    s->channels = inlink->channels;
+    s->independant_channels = s->mono ? s->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);
diff --git a/tests/ref/fate/filter-metadata-silencedetect b/tests/ref/fate/filter-metadata-silencedetect
index d1d6f67dc0..917b836250 100644
--- a/tests/ref/fate/filter-metadata-silencedetect
+++ b/tests/ref/fate/filter-metadata-silencedetect
@@ -12,12 +12,12 @@  pkt_pts=3200
 pkt_pts=3520
 pkt_pts=3840
 pkt_pts=4160
-pkt_pts=4480|tag:lavfi.silence_start=-0.02
+pkt_pts=4480|tag:lavfi.silence_start=0
 pkt_pts=4800
 pkt_pts=5120
 pkt_pts=5440
 pkt_pts=5760
-pkt_pts=6080|tag:lavfi.silence_end=0.38|tag:lavfi.silence_duration=0.4
+pkt_pts=6080|tag:lavfi.silence_end=0.38|tag:lavfi.silence_duration=0.38
 pkt_pts=6400
 pkt_pts=6720
 pkt_pts=7040
@@ -72,7 +72,7 @@  pkt_pts=22400
 pkt_pts=22720
 pkt_pts=23040
 pkt_pts=23360
-pkt_pts=23680|tag:lavfi.silence_start=1.18
+pkt_pts=23680|tag:lavfi.silence_start=1.18756
 pkt_pts=24000
 pkt_pts=24320
 pkt_pts=24640
@@ -97,7 +97,7 @@  pkt_pts=30400
 pkt_pts=30720
 pkt_pts=31040
 pkt_pts=31360
-pkt_pts=31680|tag:lavfi.silence_end=1.98|tag:lavfi.silence_duration=0.8
+pkt_pts=31680|tag:lavfi.silence_end=1.98|tag:lavfi.silence_duration=0.792438
 pkt_pts=32000
 pkt_pts=32320
 pkt_pts=32640
@@ -160,7 +160,7 @@  pkt_pts=50560
 pkt_pts=50880
 pkt_pts=51200
 pkt_pts=51520
-pkt_pts=51840|tag:lavfi.silence_start=2.94
+pkt_pts=51840|tag:lavfi.silence_start=2.945
 pkt_pts=52160
 pkt_pts=52480
 pkt_pts=52800
@@ -196,7 +196,7 @@  pkt_pts=62080
 pkt_pts=62400
 pkt_pts=62720
 pkt_pts=63040
-pkt_pts=63360|tag:lavfi.silence_end=3.96|tag:lavfi.silence_duration=1.02
+pkt_pts=63360|tag:lavfi.silence_end=3.96|tag:lavfi.silence_duration=1.015
 pkt_pts=63680
 pkt_pts=64000
 pkt_pts=64320
@@ -250,7 +250,7 @@  pkt_pts=79360
 pkt_pts=79680
 pkt_pts=80000
 pkt_pts=80320
-pkt_pts=80640|tag:lavfi.silence_start=4.74
+pkt_pts=80640|tag:lavfi.silence_start=4.75662
 pkt_pts=80960
 pkt_pts=81280
 pkt_pts=81600
@@ -270,7 +270,7 @@  pkt_pts=85760
 pkt_pts=86080
 pkt_pts=86400
 pkt_pts=86720
-pkt_pts=87040|tag:lavfi.silence_end=5.44|tag:lavfi.silence_duration=0.7
+pkt_pts=87040|tag:lavfi.silence_end=5.44|tag:lavfi.silence_duration=0.683375
 pkt_pts=87360
 pkt_pts=87680
 pkt_pts=88000
@@ -326,7 +326,7 @@  pkt_pts=103680
 pkt_pts=104000
 pkt_pts=104320
 pkt_pts=104640
-pkt_pts=104960|tag:lavfi.silence_start=6.26
+pkt_pts=104960|tag:lavfi.silence_start=6.27319
 pkt_pts=105280
 pkt_pts=105600
 pkt_pts=105920
@@ -362,7 +362,7 @@  pkt_pts=115200
 pkt_pts=115520
 pkt_pts=115840
 pkt_pts=116160
-pkt_pts=116480|tag:lavfi.silence_end=7.28|tag:lavfi.silence_duration=1.02
+pkt_pts=116480|tag:lavfi.silence_end=7.28|tag:lavfi.silence_duration=1.00681
 pkt_pts=116800
 pkt_pts=117120
 pkt_pts=117440
@@ -425,7 +425,7 @@  pkt_pts=135360
 pkt_pts=135680
 pkt_pts=136000
 pkt_pts=136320
-pkt_pts=136640|tag:lavfi.silence_start=8.24
+pkt_pts=136640|tag:lavfi.silence_start=8.25106
 pkt_pts=136960
 pkt_pts=137280
 pkt_pts=137600
@@ -452,7 +452,7 @@  pkt_pts=144000
 pkt_pts=144320
 pkt_pts=144640
 pkt_pts=144960
-pkt_pts=145280|tag:lavfi.silence_end=9.08|tag:lavfi.silence_duration=0.84
+pkt_pts=145280|tag:lavfi.silence_end=9.08|tag:lavfi.silence_duration=0.828937
 pkt_pts=145600
 pkt_pts=145920
 pkt_pts=146240
@@ -504,7 +504,7 @@  pkt_pts=160640
 pkt_pts=160960
 pkt_pts=161280
 pkt_pts=161600
-pkt_pts=161920|tag:lavfi.silence_start=9.82
+pkt_pts=161920|tag:lavfi.silence_start=9.83881
 pkt_pts=162240
 pkt_pts=162560
 pkt_pts=162880