History

avfilter/af_silenceremove: add median silence detector

Paul B Mahol committed 3 years ago

Commit 5a13b131 parent 68d0b883 files · +74 −3Browse files

Showing the changes to libavfilter (2 of 3 files). Show all

Unified · Split

2 files
  1. libavfilter/af_silenceremove.c +6 −0
  2. libavfilter/silenceremove_template.c +67 −2
libavfilter/af_silenceremove.c+6−0
@@ -36,6 +36,7 @@ enum SilenceDetect { · 35 unmodified lines
3636 D_AVG,
3737 D_RMS,
3838 D_PEAK,
39+ D_MEDIAN,
3940 D_NB
4041 };
4142
@@ -132,6 +133,7 @@ static const AVOption silenceremove_options[] = { · 90 unmodified lines
132133 { "avg", "use mean absolute values of samples", 0, AV_OPT_TYPE_CONST, {.i64=D_AVG}, 0, 0, AF, "detection" },
133134 { "rms", "use root mean squared values of samples", 0, AV_OPT_TYPE_CONST, {.i64=D_RMS}, 0, 0, AF, "detection" },
134135 { "peak", "use max absolute values of samples", 0, AV_OPT_TYPE_CONST, {.i64=D_PEAK},0, 0, AF, "detection" },
136+ { "median", "use median of absolute values of samples", 0, AV_OPT_TYPE_CONST, {.i64=D_MEDIAN},0, 0, AF, "detection" },
135137 { "window", "set duration of window for silence detection", OFFSET(window_duration_opt), AV_OPT_TYPE_DURATION, {.i64=20000}, 0, 100000000, AF },
136138 { NULL }
137139 };
@@ -234,6 +236,10 @@ static int config_output(AVFilterLink *outlink) · 96 unmodified lines
234236 s->compute_flt = compute_avg_flt;
235237 s->compute_dbl = compute_avg_dbl;
236238 break;
239+ case D_MEDIAN:
240+ s->compute_flt = compute_median_flt;
241+ s->compute_dbl = compute_median_dbl;
242+ break;
237243 case D_PEAK:
238244 s->compute_flt = compute_peak_flt;
239245 s->compute_dbl = compute_peak_dbl;
libavfilter/silenceremove_template.c+67−2
@@ -22,6 +22,7 @@ · 21 unmodified lines
2222 #undef SAMPLE_FORMAT
2323 #undef SQRT
2424 #undef ZERO
25+#undef ONE
2526 #if DEPTH == 32
2627 #define SAMPLE_FORMAT flt
2728 #define SQRT sqrtf
@@ -29,6 +30,7 @@ · 1 unmodified line
2930 #define FABS fabsf
3031 #define ftype float
3132 #define ZERO 0.f
33+#define ONE 1.f
3234 #else
3335 #define SAMPLE_FORMAT dbl
3436 #define SQRT sqrt
@@ -36,6 +38,7 @@ · 1 unmodified line
3638 #define FABS fabs
3739 #define ftype double
3840 #define ZERO 0.0
41+#define ONE 1.0
3942 #endif
4043
4144 #define fn3(a,b) a##_##b
@@ -111,6 +114,68 @@ static ftype fn(compute_avg)(ftype *cache, ftype sample, ftype wsample, · 69 unmodified lines
111114 return r / window_size;
112115 }
113116
117+static ftype fn(compute_median)(ftype *peak, ftype sample, ftype wsample,
118+ int size, int *ffront, int *bback)
119+{
120+ ftype r, abs_sample = FABS(sample);
121+ int front = *ffront;
122+ int back = *bback;
123+ int empty = front == back && peak[front] == -ONE;
124+ int idx;
125+
126+ if (!empty && FABS(wsample) == peak[front]) {
127+ peak[front] = -ONE;
128+ if (back != front) {
129+ front--;
130+ if (front < 0)
131+ front = size - 1;
132+ }
133+ empty = front == back;
134+ }
135+
136+ if (!empty && abs_sample > peak[front]) {
137+ while (1) {
138+ peak[front] = -ONE;
139+ if (back == front) {
140+ empty = 1;
141+ break;
142+ }
143+ front--;
144+ if (front < 0)
145+ front = size - 1;
146+ }
147+ }
148+
149+ while (!empty && abs_sample > peak[back]) {
150+ peak[back] = -ONE;
151+ if (back == front) {
152+ empty = 1;
153+ break;
154+ }
155+ back++;
156+ if (back >= size)
157+ back = 0;
158+ }
159+
160+ if (!empty) {
161+ back--;
162+ if (back < 0)
163+ back = size - 1;
164+ }
165+
166+ peak[back] = abs_sample;
167+ idx = (back <= front) ? back + (front - back + 1) / 2 : back + (size + front - back + 1) / 2;
168+ if (idx >= size)
169+ idx -= size;
170+ av_assert2(idx >= 0 && idx < size);
171+ r = peak[idx];
172+
173+ *ffront = front;
174+ *bback = back;
175+
176+ return r;
177+}
178+
114179 static ftype fn(compute_peak)(ftype *peak, ftype sample, ftype wsample,
115180 int size, int *ffront, int *bback)
116181 {
@@ -216,7 +281,7 @@ static void fn(filter_start)(AVFilterContext *ctx, · 99 unmodified lines
216281 if (s->start_found_periods < 0)
217282 goto skip;
218283
219− if (s->detection != D_PEAK)
284+ if (s->detection != D_PEAK && s->detection != D_MEDIAN)
220285 window_size = s->start_window_size;
221286
222287 for (int ch = 0; ch < nb_channels; ch++) {
@@ -308,7 +373,7 @@ static void fn(filter_stop)(AVFilterContext *ctx, · 85 unmodified lines
308373 stop_nb_samples,
309374 stop_window_nb_samples);
310375
311− if (s->detection != D_PEAK)
376+ if (s->detection != D_PEAK && s->detection != D_MEDIAN)
312377 window_size = s->stop_window_size;
313378
314379 for (int ch = 0; ch < nb_channels; ch++) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t