[FFmpeg-cvslog] avfilter/silenceremove_template: refactor detectors using common peaks code

Paul B Mahol git at videolan.org
Mon May 29 12:41:55 EEST 2023


ffmpeg | branch: master | Paul B Mahol <onemda at gmail.com> | Mon May 29 09:18:15 2023 +0200| [45fa85a77797630e361e8f8254560c4f56e0a5cb] | committer: Paul B Mahol

avfilter/silenceremove_template: refactor detectors using common peaks code

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

 libavfilter/silenceremove_template.c | 217 ++++++++++++-----------------------
 1 file changed, 72 insertions(+), 145 deletions(-)

diff --git a/libavfilter/silenceremove_template.c b/libavfilter/silenceremove_template.c
index 009ed52f89..2f34fb5958 100644
--- a/libavfilter/silenceremove_template.c
+++ b/libavfilter/silenceremove_template.c
@@ -105,73 +105,76 @@ static void fn(queue_sample)(AVFilterContext *ctx,
         *window_pos = 0;
 }
 
-static ftype fn(compute_avg)(ftype *cache, ftype sample, ftype wsample,
+static ftype fn(compute_avg)(ftype *cache, ftype x, ftype px,
                              int window_size, int *unused, int *unused2)
 {
     ftype r;
 
-    cache[0] += FABS(sample);
-    cache[0] -= FABS(wsample);
+    cache[0] += FABS(x);
+    cache[0] -= FABS(px);
     cache[0] = r = FMAX(cache[0], ZERO);
 
     return r / window_size;
 }
 
-static ftype fn(compute_median)(ftype *peak, ftype sample, ftype wsample,
-                                int size, int *ffront, int *bback)
+#define PEAKS(empty_value,op,sample, psample)\
+    if (!empty && psample == ss[front]) {    \
+        ss[front] = empty_value;             \
+        if (back != front) {                 \
+            front--;                         \
+            if (front < 0)                   \
+                front = n - 1;               \
+        }                                    \
+        empty = front == back;               \
+    }                                        \
+                                             \
+    if (!empty && sample op ss[front]) {     \
+        while (1) {                          \
+            ss[front] = empty_value;         \
+            if (back == front) {             \
+                empty = 1;                   \
+                break;                       \
+            }                                \
+            front--;                         \
+            if (front < 0)                   \
+                front = n - 1;               \
+        }                                    \
+    }                                        \
+                                             \
+    while (!empty && sample op ss[back]) {   \
+        ss[back] = empty_value;              \
+        if (back == front) {                 \
+            empty = 1;                       \
+            break;                           \
+        }                                    \
+        back++;                              \
+        if (back >= n)                       \
+            back = 0;                        \
+    }                                        \
+                                             \
+    if (!empty) {                            \
+        back--;                              \
+        if (back < 0)                        \
+            back = n - 1;                    \
+    }
+
+static ftype fn(compute_median)(ftype *ss, ftype x, ftype px,
+                                int n, int *ffront, int *bback)
 {
-    ftype r, abs_sample = FABS(sample);
+    ftype r, ax = FABS(x);
     int front = *ffront;
     int back = *bback;
-    int empty = front == back && peak[front] == -ONE;
+    int empty = front == back && ss[front] == -ONE;
     int idx;
 
-    if (!empty && FABS(wsample) == peak[front]) {
-        peak[front] = -ONE;
-        if (back != front) {
-            front--;
-            if (front < 0)
-                front = size - 1;
-        }
-        empty = front == back;
-    }
-
-    if (!empty && abs_sample > peak[front]) {
-        while (1) {
-            peak[front] = -ONE;
-            if (back == front) {
-                empty = 1;
-                break;
-            }
-            front--;
-            if (front < 0)
-                front = size - 1;
-        }
-    }
-
-    while (!empty && abs_sample > peak[back]) {
-        peak[back] = -ONE;
-        if (back == front) {
-            empty = 1;
-            break;
-        }
-        back++;
-        if (back >= size)
-            back = 0;
-    }
-
-    if (!empty) {
-        back--;
-        if (back < 0)
-            back = size - 1;
-    }
+    PEAKS(-ONE, >, ax, FABS(px))
 
-    peak[back] = abs_sample;
-    idx = (back <= front) ? back + (front - back + 1) / 2 : back + (size + front - back + 1) / 2;
-    if (idx >= size)
-        idx -= size;
-    av_assert2(idx >= 0 && idx < size);
-    r = peak[idx];
+    ss[back] = ax;
+    idx = (back <= front) ? back + (front - back + 1) / 2 : back + (n + front - back + 1) / 2;
+    if (idx >= n)
+        idx -= n;
+    av_assert2(idx >= 0 && idx < n);
+    r = ss[idx];
 
     *ffront = front;
     *bback = back;
@@ -179,56 +182,18 @@ static ftype fn(compute_median)(ftype *peak, ftype sample, ftype wsample,
     return r;
 }
 
-static ftype fn(compute_peak)(ftype *peak, ftype sample, ftype wsample,
-                              int size, int *ffront, int *bback)
+static ftype fn(compute_peak)(ftype *ss, ftype x, ftype px,
+                              int n, int *ffront, int *bback)
 {
-    ftype r, abs_sample = FABS(sample);
+    ftype r, ax = FABS(x);
     int front = *ffront;
     int back = *bback;
-    int empty = front == back && peak[front] == ZERO;
-
-    if (!empty && FABS(wsample) == peak[front]) {
-        peak[front] = ZERO;
-        if (back != front) {
-            front--;
-            if (front < 0)
-                front = size - 1;
-        }
-        empty = front == back;
-    }
+    int empty = front == back && ss[front] == ZERO;
 
-    if (!empty && abs_sample >= peak[front]) {
-        while (1) {
-            peak[front] = ZERO;
-            if (back == front) {
-                empty = 1;
-                break;
-            }
-            front--;
-            if (front < 0)
-                front = size - 1;
-        }
-    }
+    PEAKS(ZERO, >=, ax, FABS(px))
 
-    while (!empty && abs_sample >= peak[back]) {
-        peak[back] = ZERO;
-        if (back == front) {
-            empty = 1;
-            break;
-        }
-        back++;
-        if (back >= size)
-            back = 0;
-    }
-
-    if (!empty) {
-        back--;
-        if (back < 0)
-            back = size - 1;
-    }
-
-    peak[back] = abs_sample;
-    r = peak[front];
+    ss[back] = ax;
+    r = ss[front];
 
     *ffront = front;
     *bback = back;
@@ -236,57 +201,19 @@ 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)
+static ftype fn(compute_ptp)(ftype *ss, ftype x, ftype px,
+                             int n, int *ffront, int *bback)
 {
     int front = *ffront;
     int back = *bback;
-    int empty = front == back && peak[front] == TMIN;
+    int empty = front == back && ss[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;
-    }
+    PEAKS(TMIN, >=, x, px)
 
-    peak[back] = sample;
-    max = peak[front];
-    min = sample;
+    ss[back] = x;
+    max = ss[front];
+    min = x;
     r = FABS(min) + FABS(max - min);
 
     *ffront = front;
@@ -295,13 +222,13 @@ static ftype fn(compute_ptp)(ftype *peak, ftype sample, ftype wsample,
     return r;
 }
 
-static ftype fn(compute_rms)(ftype *cache, ftype sample, ftype wsample,
+static ftype fn(compute_rms)(ftype *cache, ftype x, ftype px,
                              int window_size, int *unused, int *unused2)
 {
     ftype r;
 
-    cache[0] += sample * sample;
-    cache[0] -= wsample * wsample;
+    cache[0] += x * x;
+    cache[0] -= px * px;
     cache[0] = r = FMAX(cache[0], ZERO);
 
     return SQRT(r / window_size);



More information about the ffmpeg-cvslog mailing list