1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-08-10 06:10:52 +02:00

avfilter/af_silenceremove: add ptp detector

This commit is contained in:
Paul B Mahol
2023-05-27 20:52:00 +02:00
parent 163e3a299e
commit e53260c1f4
3 changed files with 74 additions and 2 deletions

View File

@@ -6472,6 +6472,8 @@ Root squared mean of absolute values of samples in moving window.
Maximum of absolute values of samples in moving window. Maximum of absolute values of samples in moving window.
@item median @item median
Median 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.
@end table @end table
Default value is @code{rms}. Default value is @code{rms}.

View File

@@ -37,6 +37,7 @@ enum SilenceDetect {
D_RMS, D_RMS,
D_PEAK, D_PEAK,
D_MEDIAN, D_MEDIAN,
D_PTP,
D_NB 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" }, { "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" }, { "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" }, { "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 }, { "window", "set duration of window for silence detection", OFFSET(window_duration_opt), AV_OPT_TYPE_DURATION, {.i64=20000}, 0, 100000000, AF },
{ NULL } { NULL }
}; };
@@ -237,6 +239,10 @@ static int config_output(AVFilterLink *outlink)
s->compute_flt = compute_avg_flt; s->compute_flt = compute_avg_flt;
s->compute_dbl = compute_avg_dbl; s->compute_dbl = compute_avg_dbl;
break; break;
case D_PTP:
s->compute_flt = compute_ptp_flt;
s->compute_dbl = compute_ptp_dbl;
break;
case D_MEDIAN: case D_MEDIAN:
s->compute_flt = compute_median_flt; s->compute_flt = compute_median_flt;
s->compute_dbl = compute_median_dbl; s->compute_dbl = compute_median_dbl;

View File

@@ -23,6 +23,7 @@
#undef SQRT #undef SQRT
#undef ZERO #undef ZERO
#undef ONE #undef ONE
#undef TMIN
#if DEPTH == 32 #if DEPTH == 32
#define SAMPLE_FORMAT flt #define SAMPLE_FORMAT flt
#define SQRT sqrtf #define SQRT sqrtf
@@ -31,6 +32,7 @@
#define ftype float #define ftype float
#define ZERO 0.f #define ZERO 0.f
#define ONE 1.f #define ONE 1.f
#define TMIN -FLT_MAX
#else #else
#define SAMPLE_FORMAT dbl #define SAMPLE_FORMAT dbl
#define SQRT sqrt #define SQRT sqrt
@@ -39,6 +41,7 @@
#define ftype double #define ftype double
#define ZERO 0.0 #define ZERO 0.0
#define ONE 1.0 #define ONE 1.0
#define TMIN -DBL_MAX
#endif #endif
#define fn3(a,b) a##_##b #define fn3(a,b) a##_##b
@@ -233,6 +236,65 @@ static ftype fn(compute_peak)(ftype *peak, ftype sample, ftype wsample,
return r; 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, static ftype fn(compute_rms)(ftype *cache, ftype sample, ftype wsample,
int window_size, int *unused, int *unused2) int window_size, int *unused, int *unused2)
{ {
@@ -281,7 +343,8 @@ static void fn(filter_start)(AVFilterContext *ctx,
if (s->start_found_periods < 0) if (s->start_found_periods < 0)
goto skip; 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; window_size = s->start_window_size;
for (int ch = 0; ch < nb_channels; ch++) { for (int ch = 0; ch < nb_channels; ch++) {
@@ -374,7 +437,8 @@ static void fn(filter_stop)(AVFilterContext *ctx,
stop_nb_samples, stop_nb_samples,
stop_window_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; window_size = s->stop_window_size;
for (int ch = 0; ch < nb_channels; ch++) { for (int ch = 0; ch < nb_channels; ch++) {