diff options
author | Paul B Mahol <onemda@gmail.com> | 2023-05-27 20:52:00 +0200 |
---|---|---|
committer | Paul B Mahol <onemda@gmail.com> | 2023-05-28 12:23:11 +0200 |
commit | e53260c1f4ce478943ad0d1ec1ca29a55024ae8a (patch) | |
tree | 9a139282474148fc3d11a7c1d4c474ff3bd08b38 /libavfilter | |
parent | 163e3a299e1cc06f0f871d8140def974757e4a7d (diff) | |
download | ffmpeg-e53260c1f4ce478943ad0d1ec1ca29a55024ae8a.tar.gz |
avfilter/af_silenceremove: add ptp detector
Diffstat (limited to 'libavfilter')
-rw-r--r-- | libavfilter/af_silenceremove.c | 6 | ||||
-rw-r--r-- | libavfilter/silenceremove_template.c | 68 |
2 files changed, 72 insertions, 2 deletions
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++) { |