[FFmpeg-cvslog] avfilter/af_silenceremove: add ptp detector

Paul B Mahol git at videolan.org
Sun May 28 13:18:41 EEST 2023


ffmpeg | branch: master | Paul B Mahol <onemda at gmail.com> | Sat May 27 20:52:00 2023 +0200| [e53260c1f4ce478943ad0d1ec1ca29a55024ae8a] | committer: Paul B Mahol

avfilter/af_silenceremove: add ptp detector

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=e53260c1f4ce478943ad0d1ec1ca29a55024ae8a
---

 doc/filters.texi                     |  2 ++
 libavfilter/af_silenceremove.c       |  6 ++++
 libavfilter/silenceremove_template.c | 68 ++++++++++++++++++++++++++++++++++--
 3 files changed, 74 insertions(+), 2 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 4d9a3cf0b4..1a28bd86ce 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -6472,6 +6472,8 @@ Root squared mean of absolute values of samples in moving window.
 Maximum of absolute values of samples in moving window.
 @item median
 Median of absolute values of samples in moving window.
+ at item ptp
+Absolute of max peak to min peak difference of samples in moving window.
 @end table
 Default value is @code{rms}.
 
diff --git a/libavfilter/af_silenceremove.c b/libavfilter/af_silenceremove.c
index 752a362203..46f28b4be7 100644
--- a/libavfilter/af_silenceremove.c
+++ b/libavfilter/af_silenceremove.c
@@ -37,6 +37,7 @@ enum SilenceDetect {
     D_RMS,
     D_PEAK,
     D_MEDIAN,
+    D_PTP,
     D_NB
 };
 
@@ -135,6 +136,7 @@ static const AVOption silenceremove_options[] = {
     {   "rms",           "use root mean squared values of samples",            0,                           AV_OPT_TYPE_CONST,    {.i64=D_RMS}, 0,         0, AF, "detection" },
     {   "peak",          "use max absolute values of samples",                 0,                           AV_OPT_TYPE_CONST,    {.i64=D_PEAK},0,         0, AF, "detection" },
     {   "median",        "use median of absolute values of samples",           0,                           AV_OPT_TYPE_CONST,    {.i64=D_MEDIAN},0,       0, AF, "detection" },
+    {   "ptp",           "use absolute of max peak to min peak difference",    0,                           AV_OPT_TYPE_CONST,    {.i64=D_PTP}, 0,         0, AF, "detection" },
     { "window",          "set duration of window for silence detection",       OFFSET(window_duration_opt), AV_OPT_TYPE_DURATION, {.i64=20000}, 0, 100000000, AF },
     { NULL }
 };
@@ -237,6 +239,10 @@ static int config_output(AVFilterLink *outlink)
         s->compute_flt = compute_avg_flt;
         s->compute_dbl = compute_avg_dbl;
         break;
+    case D_PTP:
+        s->compute_flt = compute_ptp_flt;
+        s->compute_dbl = compute_ptp_dbl;
+        break;
     case D_MEDIAN:
         s->compute_flt = compute_median_flt;
         s->compute_dbl = compute_median_dbl;
diff --git a/libavfilter/silenceremove_template.c b/libavfilter/silenceremove_template.c
index 901361d7eb..34d50fcf0e 100644
--- a/libavfilter/silenceremove_template.c
+++ b/libavfilter/silenceremove_template.c
@@ -23,6 +23,7 @@
 #undef SQRT
 #undef ZERO
 #undef ONE
+#undef TMIN
 #if DEPTH == 32
 #define SAMPLE_FORMAT flt
 #define SQRT sqrtf
@@ -31,6 +32,7 @@
 #define ftype float
 #define ZERO 0.f
 #define ONE 1.f
+#define TMIN -FLT_MAX
 #else
 #define SAMPLE_FORMAT dbl
 #define SQRT sqrt
@@ -39,6 +41,7 @@
 #define ftype double
 #define ZERO 0.0
 #define ONE 1.0
+#define TMIN -DBL_MAX
 #endif
 
 #define fn3(a,b)   a##_##b
@@ -233,6 +236,65 @@ static ftype fn(compute_peak)(ftype *peak, ftype sample, ftype wsample,
     return r;
 }
 
+static ftype fn(compute_ptp)(ftype *peak, ftype sample, ftype wsample,
+                             int size, int *ffront, int *bback)
+{
+    int front = *ffront;
+    int back = *bback;
+    int empty = front == back && peak[front] == TMIN;
+    ftype r, max, min;
+
+    if (!empty && wsample == peak[front]) {
+        peak[front] = TMIN;
+        if (back != front) {
+            front--;
+            if (front < 0)
+                front = size - 1;
+        }
+        empty = front == back;
+    }
+
+    if (!empty && sample >= peak[front]) {
+        while (1) {
+            peak[front] = TMIN;
+            if (back == front) {
+                empty = 1;
+                break;
+            }
+            front--;
+            if (front < 0)
+                front = size - 1;
+        }
+    }
+
+    while (!empty && sample >= peak[back]) {
+        peak[back] = TMIN;
+        if (back == front) {
+            empty = 1;
+            break;
+        }
+        back++;
+        if (back >= size)
+            back = 0;
+    }
+
+    if (!empty) {
+        back--;
+        if (back < 0)
+            back = size - 1;
+    }
+
+    peak[back] = sample;
+    max = peak[front];
+    min = (back == front) ? -sample : sample;
+    r = FABS(max - min);
+
+    *ffront = front;
+    *bback = back;
+
+    return r;
+}
+
 static ftype fn(compute_rms)(ftype *cache, ftype sample, ftype wsample,
                              int window_size, int *unused, int *unused2)
 {
@@ -281,7 +343,8 @@ static void fn(filter_start)(AVFilterContext *ctx,
     if (s->start_found_periods < 0)
         goto skip;
 
-    if (s->detection != D_PEAK && s->detection != D_MEDIAN)
+    if (s->detection != D_PEAK && s->detection != D_MEDIAN &&
+        s->detection != D_PTP)
         window_size = s->start_window_size;
 
     for (int ch = 0; ch < nb_channels; ch++) {
@@ -374,7 +437,8 @@ static void fn(filter_stop)(AVFilterContext *ctx,
                      stop_nb_samples,
                      stop_window_nb_samples);
 
-    if (s->detection != D_PEAK && s->detection != D_MEDIAN)
+    if (s->detection != D_PEAK && s->detection != D_MEDIAN &&
+        s->detection != D_PTP)
         window_size = s->stop_window_size;
 
     for (int ch = 0; ch < nb_channels; ch++) {



More information about the ffmpeg-cvslog mailing list