aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul B Mahol <onemda@gmail.com>2023-05-28 17:37:55 +0200
committerPaul B Mahol <onemda@gmail.com>2023-05-29 11:47:10 +0200
commitf02964aee1ed06ac4a67103904d5fae2862fa45e (patch)
tree9b8287adb5aa73acdaec4fadee3b1b618292c728
parentaa4acc111e6ea1117dfec230435fdb26ec7631d9 (diff)
downloadffmpeg-f02964aee1ed06ac4a67103904d5fae2862fa45e.tar.gz
avfilter/af_silenceremove: add standard deviation detector
Useful in cases audio samples DC offset is not ~0.0, where other detectors will fail to detect silence.
-rw-r--r--doc/filters.texi2
-rw-r--r--libavfilter/af_silenceremove.c9
-rw-r--r--libavfilter/silenceremove_template.c17
3 files changed, 28 insertions, 0 deletions
diff --git a/doc/filters.texi b/doc/filters.texi
index 2333cfaf7a..c47e88e5d4 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -6474,6 +6474,8 @@ Maximum of absolute values of samples in moving window.
Median of absolute values of samples in moving window.
@item ptp
Absolute of max peak to min peak difference of samples in moving window.
+@item dev
+Standard deviation of values 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 c7975a9365..4cb662080b 100644
--- a/libavfilter/af_silenceremove.c
+++ b/libavfilter/af_silenceremove.c
@@ -38,6 +38,7 @@ enum SilenceDetect {
D_PEAK,
D_MEDIAN,
D_PTP,
+ D_DEV,
D_NB
};
@@ -146,6 +147,7 @@ static const AVOption silenceremove_options[] = {
{ "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" },
+ { "dev", "use standard deviation from values of samples", 0, AV_OPT_TYPE_CONST, {.i64=D_DEV}, 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 },
{ "timestamp", "set how every output frame timestamp is processed", OFFSET(timestamp_mode), AV_OPT_TYPE_INT, {.i64=TS_WRITE}, 0, TS_NB-1, AF, "timestamp" },
{ "write", "full timestamps rewrite, keep only the start time", 0, AV_OPT_TYPE_CONST, {.i64=TS_WRITE}, 0, 0, AF, "timestamp" },
@@ -230,6 +232,9 @@ static int config_output(AVFilterLink *outlink)
case D_RMS:
s->cache_size = 1;
break;
+ case D_DEV:
+ s->cache_size = 2;
+ break;
case D_MEDIAN:
case D_PEAK:
case D_PTP:
@@ -263,6 +268,10 @@ static int config_output(AVFilterLink *outlink)
s->compute_flt = compute_avg_flt;
s->compute_dbl = compute_avg_dbl;
break;
+ case D_DEV:
+ s->compute_flt = compute_dev_flt;
+ s->compute_dbl = compute_dev_dbl;
+ break;
case D_PTP:
s->compute_flt = compute_ptp_flt;
s->compute_dbl = compute_ptp_dbl;
diff --git a/libavfilter/silenceremove_template.c b/libavfilter/silenceremove_template.c
index 8536d5e723..009ed52f89 100644
--- a/libavfilter/silenceremove_template.c
+++ b/libavfilter/silenceremove_template.c
@@ -307,6 +307,23 @@ static ftype fn(compute_rms)(ftype *cache, ftype sample, ftype wsample,
return SQRT(r / window_size);
}
+static ftype fn(compute_dev)(ftype *ss, ftype x, ftype px,
+ int n, int *unused, int *unused2)
+{
+ ftype r;
+
+ ss[0] += x;
+ ss[0] -= px;
+
+ ss[1] += x * x;
+ ss[1] -= px * px;
+ ss[1] = FMAX(ss[1], ZERO);
+
+ r = FMAX(ss[1] - ss[0] * ss[0] / n, ZERO) / n;
+
+ return SQRT(r);
+}
+
static void fn(filter_start)(AVFilterContext *ctx,
const ftype *src, ftype *dst,
int *nb_out_samples,