History

avfilter/af_silenceremove: use duration option for window option

Paul B Mahol committed 5 years ago

Commit 4e1ee161 parent 15e844e1 file · +21 −18Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/af_silenceremove.c+21−18
@@ -69,6 +69,8 @@ typedef struct SilenceRemoveContext { · 68 unmodified lines
6969 int64_t stop_silence_opt;
7070 int stop_mode;
7171
72+ int64_t window_duration_opt;
73+
7274 AVFrame *start_holdoff;
7375 AVFrame *start_silence_hold;
7476 size_t start_holdoff_offset;
@@ -85,10 +87,9 @@ typedef struct SilenceRemoveContext { · 10 unmodified lines
8587 size_t stop_silence_end;
8688 int stop_found_periods;
8789
88− double window_ratio;
8990 AVFrame *window;
9091 int window_offset;
91− int window_size;
92+ int64_t window_duration;
9293 double sum;
9394
9495 int restart;
@@ -120,7 +121,7 @@ static const AVOption silenceremove_options[] = { · 25 unmodified lines
120121 { "detection", "set how silence is detected", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=D_RMS}, D_PEAK,D_RMS, AF, "detection" },
121122 { "peak", "use absolute values of samples", 0, AV_OPT_TYPE_CONST, {.i64=D_PEAK},0, 0, AF, "detection" },
122123 { "rms", "use squared values of samples", 0, AV_OPT_TYPE_CONST, {.i64=D_RMS}, 0, 0, AF, "detection" },
123− { "window", "set duration of window in seconds", OFFSET(window_ratio), AV_OPT_TYPE_DOUBLE, {.dbl=0.02}, 0, 10, AF },
124+ { "window", "set duration of window for silence detection", OFFSET(window_duration_opt), AV_OPT_TYPE_DURATION, {.i64=20000}, 0, 100000000, AF },
124125 { NULL }
125126 };
126127
@@ -178,7 +179,7 @@ static double compute_peak_double(SilenceRemoveContext *s, AVFrame *frame, int c · 51 unmodified lines
178179 new_sum -= wsample;
179180 new_sum += fabs(sample);
180181
181− return new_sum / s->window_size;
182+ return new_sum / s->window_duration;
182183 }
183184
184185 static void update_peak_double(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
@@ -205,7 +206,7 @@ static double compute_peak_float(SilenceRemoveContext *s, AVFrame *frame, int ch · 20 unmodified lines
205206 new_sum -= wsample;
206207 new_sum += fabsf(sample);
207208
208− return new_sum / s->window_size;
209+ return new_sum / s->window_duration;
209210 }
210211
211212 static void update_peak_float(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
@@ -232,7 +233,7 @@ static double compute_rms_double(SilenceRemoveContext *s, AVFrame *frame, int ch · 20 unmodified lines
232233 new_sum -= wsample;
233234 new_sum += sample * sample;
234235
235− return sqrt(new_sum / s->window_size);
236+ return sqrt(new_sum / s->window_duration);
236237 }
237238
238239 static void update_rms_double(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
@@ -259,7 +260,7 @@ static double compute_rms_float(SilenceRemoveContext *s, AVFrame *frame, int ch, · 20 unmodified lines
259260 new_sum -= wsample;
260261 new_sum += sample * sample;
261262
262− return sqrtf(new_sum / s->window_size);
263+ return sqrtf(new_sum / s->window_duration);
263264 }
264265
265266 static void update_rms_float(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
@@ -286,7 +287,7 @@ static double compute_peak_doublep(SilenceRemoveContext *s, AVFrame *frame, int · 20 unmodified lines
286287 new_sum -= wsample;
287288 new_sum += fabs(sample);
288289
289− return new_sum / s->window_size;
290+ return new_sum / s->window_duration;
290291 }
291292
292293 static void update_peak_doublep(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
@@ -313,7 +314,7 @@ static double compute_peak_floatp(SilenceRemoveContext *s, AVFrame *frame, int c · 20 unmodified lines
313314 new_sum -= wsample;
314315 new_sum += fabsf(sample);
315316
316− return new_sum / s->window_size;
317+ return new_sum / s->window_duration;
317318 }
318319
319320 static void update_peak_floatp(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
@@ -340,7 +341,7 @@ static double compute_rms_doublep(SilenceRemoveContext *s, AVFrame *frame, int c · 20 unmodified lines
340341 new_sum -= wsample;
341342 new_sum += sample * sample;
342343
343− return sqrt(new_sum / s->window_size);
344+ return sqrt(new_sum / s->window_duration);
344345 }
345346
346347 static void update_rms_doublep(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
@@ -367,7 +368,7 @@ static double compute_rms_floatp(SilenceRemoveContext *s, AVFrame *frame, int ch · 20 unmodified lines
367368 new_sum -= wsample;
368369 new_sum += sample * sample;
369370
370− return sqrtf(new_sum / s->window_size);
371+ return sqrtf(new_sum / s->window_duration);
371372 }
372373
373374 static void update_rms_floatp(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
@@ -396,7 +397,7 @@ static av_cold int init(AVFilterContext *ctx) · 22 unmodified lines
396397
397398 static void clear_window(SilenceRemoveContext *s)
398399 {
399− av_samples_set_silence(s->window->extended_data, 0, s->window_size,
400+ av_samples_set_silence(s->window->extended_data, 0, s->window_duration,
400401 s->window->channels, s->window->format);
401402
402403 s->window_offset = 0;
@@ -409,8 +410,10 @@ static int config_input(AVFilterLink *inlink) · 6 unmodified lines
409410 SilenceRemoveContext *s = ctx->priv;
410411
411412 s->next_pts = AV_NOPTS_VALUE;
412− s->window_size = FFMAX((inlink->sample_rate * s->window_ratio), 1);
413− s->window = ff_get_audio_buffer(ctx->outputs[0], s->window_size);
413+ s->window_duration = av_rescale(s->window_duration_opt, inlink->sample_rate,
414+ AV_TIME_BASE);
415+ s->window_duration = FFMAX(1, s->window_duration);
416+ s->window = ff_get_audio_buffer(ctx->outputs[0], s->window_duration);
414417 if (!s->window)
415418 return AVERROR(ENOMEM);
416419
@@ -615,7 +618,7 @@ silence_trim: · 198 unmodified lines
615618 }
616619
617620 s->window_offset++;
618− if (s->window_offset >= s->window_size)
621+ if (s->window_offset >= s->window_duration)
619622 s->window_offset = 0;
620623 s->start_holdoff_end++;
621624 nb_samples_read++;
@@ -641,7 +644,7 @@ silence_trim: · 19 unmodified lines
641644 }
642645
643646 s->window_offset++;
644− if (s->window_offset >= s->window_size)
647+ if (s->window_offset >= s->window_duration)
645648 s->window_offset = 0;
646649 nb_samples_read++;
647650 s->start_silence_offset++;
@@ -744,7 +747,7 @@ silence_copy: · 96 unmodified lines
744747 }
745748
746749 s->window_offset++;
747− if (s->window_offset >= s->window_size)
750+ if (s->window_offset >= s->window_duration)
748751 s->window_offset = 0;
749752 nb_samples_read++;
750753 nb_samples_written++;
@@ -763,7 +766,7 @@ silence_copy: · 12 unmodified lines
763766 }
764767
765768 s->window_offset++;
766− if (s->window_offset >= s->window_size)
769+ if (s->window_offset >= s->window_duration)
767770 s->window_offset = 0;
768771 nb_samples_read++;
769772 s->stop_holdoff_end++;

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