History

avfilter/af_silenceremove: add support for more sample formats

Paul B Mahol committed 5 years ago

Commit d08deb11 parent 6f0f7bc1 file · +365 −98Browse files

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

Unified · Split

libavfilter/af_silenceremove.c+365−98
@@ -69,16 +69,16 @@ typedef struct SilenceRemoveContext { · 68 unmodified lines
6969 int64_t stop_silence_opt;
7070 int stop_mode;
7171
72− double *start_holdoff;
73− double *start_silence_hold;
72+ AVFrame *start_holdoff;
73+ AVFrame *start_silence_hold;
7474 size_t start_holdoff_offset;
7575 size_t start_holdoff_end;
7676 size_t start_silence_offset;
7777 size_t start_silence_end;
7878 int start_found_periods;
7979
80− double *stop_holdoff;
81− double *stop_silence_hold;
80+ AVFrame *stop_holdoff;
81+ AVFrame *stop_silence_hold;
8282 size_t stop_holdoff_offset;
8383 size_t stop_holdoff_end;
8484 size_t stop_silence_offset;
@@ -96,8 +96,10 @@ typedef struct SilenceRemoveContext { · 11 unmodified lines
9696 int64_t next_pts;
9797
9898 int detection;
99− void (*update)(struct SilenceRemoveContext *s, double sample);
100− double(*compute)(struct SilenceRemoveContext *s, double sample);
99+ void (*update)(struct SilenceRemoveContext *s, AVFrame *frame, int ch, int offset);
100+ double (*compute)(struct SilenceRemoveContext *s, AVFrame *frame, int ch, int offset);
101+ void (*copy)(struct SilenceRemoveContext *s, AVFrame *out, AVFrame *in,
102+ int ch, int out_offset, int in_offset);
101103 } SilenceRemoveContext;
102104
103105 #define OFFSET(x) offsetof(SilenceRemoveContext, x)
@@ -125,8 +127,50 @@ static const AVOption silenceremove_options[] = { · 21 unmodified lines
125127
126128 AVFILTER_DEFINE_CLASS(silenceremove);
127129
128−static double compute_peak(SilenceRemoveContext *s, double sample)
130+static void copy_double(SilenceRemoveContext *s, AVFrame *out, AVFrame *in,
131+ int ch, int out_offset, int in_offset)
129132 {
133+ const double *srcp = (const double *)in->data[0];
134+ const double src = srcp[in->channels * in_offset + ch];
135+ double *dstp = (double *)out->data[0];
136+
137+ dstp[out->channels * out_offset + ch] = src;
138+}
139+
140+static void copy_doublep(SilenceRemoveContext *s, AVFrame *out, AVFrame *in,
141+ int ch, int out_offset, int in_offset)
142+{
143+ const double *srcp = (const double *)in->data[ch];
144+ const double src = srcp[in_offset];
145+ double *dstp = (double *)out->data[ch];
146+
147+ dstp[out_offset] = src;
148+}
149+
150+static void copy_float(SilenceRemoveContext *s, AVFrame *out, AVFrame *in,
151+ int ch, int out_offset, int in_offset)
152+{
153+ const float *srcp = (const float *)in->data[0];
154+ const float src = srcp[in->channels * in_offset + ch];
155+ float *dstp = (float *)out->data[0];
156+
157+ dstp[out->channels * out_offset + ch] = src;
158+}
159+
160+static void copy_floatp(SilenceRemoveContext *s, AVFrame *out, AVFrame *in,
161+ int ch, int out_offset, int in_offset)
162+{
163+ const float *srcp = (const float *)in->data[ch];
164+ const float src = srcp[in_offset];
165+ float *dstp = (float *)out->data[ch];
166+
167+ dstp[out_offset] = src;
168+}
169+
170+static double compute_peak_double(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
171+{
172+ const double *samples = (const double *)frame->data[0];
173+ double sample = samples[frame->channels * offset + ch];
130174 double new_sum;
131175
132176 new_sum = s->sum;
@@ -136,8 +180,38 @@ static double compute_peak(SilenceRemoveContext *s, double sample) · 3 unmodified lines
136180 return new_sum / s->window_size;
137181 }
138182
139−static void update_peak(SilenceRemoveContext *s, double sample)
183+static void update_peak_double(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
184+{
185+ const double *samples = (const double *)frame->data[0];
186+ double sample = samples[frame->channels * offset + ch];
187+
188+ s->sum -= *s->window_current;
189+ *s->window_current = fabs(sample);
190+ s->sum += *s->window_current;
191+
192+ s->window_current++;
193+ if (s->window_current >= s->window_end)
194+ s->window_current = s->window;
195+}
196+
197+static double compute_peak_float(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
198+{
199+ const float *samples = (const float *)frame->data[0];
200+ float sample = samples[frame->channels * offset + ch];
201+ float new_sum;
202+
203+ new_sum = s->sum;
204+ new_sum -= *s->window_current;
205+ new_sum += fabs(sample);
206+
207+ return new_sum / s->window_size;
208+}
209+
210+static void update_peak_float(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
140211 {
212+ const float *samples = (const float *)frame->data[0];
213+ float sample = samples[frame->channels * offset + ch];
214+
141215 s->sum -= *s->window_current;
142216 *s->window_current = fabs(sample);
143217 s->sum += *s->window_current;
@@ -147,8 +221,10 @@ static void update_peak(SilenceRemoveContext *s, double sample) · 3 unmodified lines
147221 s->window_current = s->window;
148222 }
149223
150−static double compute_rms(SilenceRemoveContext *s, double sample)
224+static double compute_rms_double(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
151225 {
226+ const double *samples = (const double *)frame->data[0];
227+ double sample = samples[frame->channels * offset + ch];
152228 double new_sum;
153229
154230 new_sum = s->sum;
@@ -158,8 +234,146 @@ static double compute_rms(SilenceRemoveContext *s, double sample) · 3 unmodified lines
158234 return sqrt(new_sum / s->window_size);
159235 }
160236
161−static void update_rms(SilenceRemoveContext *s, double sample)
237+static void update_rms_double(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
238+{
239+ const double *samples = (const double *)frame->data[0];
240+ double sample = samples[frame->channels * offset + ch];
241+
242+ s->sum -= *s->window_current;
243+ *s->window_current = sample * sample;
244+ s->sum += *s->window_current;
245+
246+ s->window_current++;
247+ if (s->window_current >= s->window_end)
248+ s->window_current = s->window;
249+}
250+
251+static double compute_rms_float(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
252+{
253+ const float *samples = (const float *)frame->data[0];
254+ float sample = samples[frame->channels * offset + ch];
255+ float new_sum;
256+
257+ new_sum = s->sum;
258+ new_sum -= *s->window_current;
259+ new_sum += sample * sample;
260+
261+ return sqrtf(new_sum / s->window_size);
262+}
263+
264+static void update_rms_float(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
162265 {
266+ const float *samples = (const float *)frame->data[0];
267+ float sample = samples[frame->channels * offset + ch];
268+
269+ s->sum -= *s->window_current;
270+ *s->window_current = sample * sample;
271+ s->sum += *s->window_current;
272+
273+ s->window_current++;
274+ if (s->window_current >= s->window_end)
275+ s->window_current = s->window;
276+}
277+
278+static double compute_peak_doublep(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
279+{
280+ const double *samples = (const double *)frame->data[ch];
281+ double sample = samples[offset];
282+ double new_sum;
283+
284+ new_sum = s->sum;
285+ new_sum -= *s->window_current;
286+ new_sum += fabs(sample);
287+
288+ return new_sum / s->window_size;
289+}
290+
291+static void update_peak_doublep(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
292+{
293+ const double *samples = (const double *)frame->data[ch];
294+ double sample = samples[offset];
295+
296+ s->sum -= *s->window_current;
297+ *s->window_current = fabs(sample);
298+ s->sum += *s->window_current;
299+
300+ s->window_current++;
301+ if (s->window_current >= s->window_end)
302+ s->window_current = s->window;
303+}
304+
305+static double compute_peak_floatp(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
306+{
307+ const float *samples = (const float *)frame->data[ch];
308+ float sample = samples[offset];
309+ float new_sum;
310+
311+ new_sum = s->sum;
312+ new_sum -= *s->window_current;
313+ new_sum += fabs(sample);
314+
315+ return new_sum / s->window_size;
316+}
317+
318+static void update_peak_floatp(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
319+{
320+ const float *samples = (const float *)frame->data[ch];
321+ float sample = samples[offset];
322+
323+ s->sum -= *s->window_current;
324+ *s->window_current = fabs(sample);
325+ s->sum += *s->window_current;
326+
327+ s->window_current++;
328+ if (s->window_current >= s->window_end)
329+ s->window_current = s->window;
330+}
331+
332+static double compute_rms_doublep(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
333+{
334+ const double *samples = (const double *)frame->data[ch];
335+ double sample = samples[offset];
336+ double new_sum;
337+
338+ new_sum = s->sum;
339+ new_sum -= *s->window_current;
340+ new_sum += sample * sample;
341+
342+ return sqrt(new_sum / s->window_size);
343+}
344+
345+static void update_rms_doublep(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
346+{
347+ const double *samples = (const double *)frame->data[ch];
348+ double sample = samples[offset];
349+
350+ s->sum -= *s->window_current;
351+ *s->window_current = sample * sample;
352+ s->sum += *s->window_current;
353+
354+ s->window_current++;
355+ if (s->window_current >= s->window_end)
356+ s->window_current = s->window;
357+}
358+
359+static double compute_rms_floatp(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
360+{
361+ const float *samples = (const float *)frame->data[ch];
362+ float sample = samples[offset];
363+ float new_sum;
364+
365+ new_sum = s->sum;
366+ new_sum -= *s->window_current;
367+ new_sum += sample * sample;
368+
369+ return sqrtf(new_sum / s->window_size);
370+}
371+
372+static void update_rms_floatp(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
373+{
374+ const float *samples = (const float *)frame->data[ch];
375+ float sample = samples[offset];
376+
163377 s->sum -= *s->window_current;
164378 *s->window_current = sample * sample;
165379 s->sum += *s->window_current;
@@ -178,17 +392,6 @@ static av_cold int init(AVFilterContext *ctx) · 12 unmodified lines
178392 s->restart = 1;
179393 }
180394
181− switch (s->detection) {
182− case D_PEAK:
183− s->update = update_peak;
184− s->compute = compute_peak;
185− break;
186− case D_RMS:
187− s->update = update_rms;
188− s->compute = compute_rms;
189− break;
190− }
191−
192395 return 0;
193396 }
194397
@@ -223,15 +426,13 @@ static int config_input(AVFilterLink *inlink) · 28 unmodified lines
223426 s->stop_silence = av_rescale(s->stop_silence_opt, inlink->sample_rate,
224427 AV_TIME_BASE);
225428
226− s->start_holdoff = av_malloc_array(FFMAX(s->start_duration, 1),
227− sizeof(*s->start_holdoff) *
228− inlink->channels);
429+ s->start_holdoff = ff_get_audio_buffer(ctx->outputs[0],
430+ FFMAX(s->start_duration, 1));
229431 if (!s->start_holdoff)
230432 return AVERROR(ENOMEM);
231433
232− s->start_silence_hold = av_malloc_array(FFMAX(s->start_silence, 1),
233− sizeof(*s->start_silence_hold) *
234− inlink->channels);
434+ s->start_silence_hold = ff_get_audio_buffer(ctx->outputs[0],
435+ FFMAX(s->start_silence, 1));
235436 if (!s->start_silence_hold)
236437 return AVERROR(ENOMEM);
237438
@@ -239,15 +440,13 @@ static int config_input(AVFilterLink *inlink) · 1 unmodified line
239440 s->start_holdoff_end = 0;
240441 s->start_found_periods = 0;
241442
242− s->stop_holdoff = av_malloc_array(FFMAX(s->stop_duration, 1),
243− sizeof(*s->stop_holdoff) *
244− inlink->channels);
443+ s->stop_holdoff = ff_get_audio_buffer(ctx->outputs[0],
444+ FFMAX(s->stop_duration, 1));
245445 if (!s->stop_holdoff)
246446 return AVERROR(ENOMEM);
247447
248− s->stop_silence_hold = av_malloc_array(FFMAX(s->stop_silence, 1),
249− sizeof(*s->stop_silence_hold) *
250− inlink->channels);
448+ s->stop_silence_hold = ff_get_audio_buffer(ctx->outputs[0],
449+ FFMAX(s->stop_silence, 1));
251450 if (!s->stop_silence_hold)
252451 return AVERROR(ENOMEM);
253452
@@ -260,6 +459,61 @@ static int config_input(AVFilterLink *inlink) · 6 unmodified lines
260459 else
261460 s->mode = SILENCE_COPY;
262461
462+ switch (inlink->format) {
463+ case AV_SAMPLE_FMT_DBL:
464+ s->copy = copy_double;
465+ switch (s->detection) {
466+ case D_PEAK:
467+ s->update = update_peak_double;
468+ s->compute = compute_peak_double;
469+ break;
470+ case D_RMS:
471+ s->update = update_rms_double;
472+ s->compute = compute_rms_double;
473+ break;
474+ }
475+ break;
476+ case AV_SAMPLE_FMT_FLT:
477+ s->copy = copy_float;
478+ switch (s->detection) {
479+ case D_PEAK:
480+ s->update = update_peak_float;
481+ s->compute = compute_peak_float;
482+ break;
483+ case D_RMS:
484+ s->update = update_rms_float;
485+ s->compute = compute_rms_float;
486+ break;
487+ }
488+ break;
489+ case AV_SAMPLE_FMT_DBLP:
490+ s->copy = copy_doublep;
491+ switch (s->detection) {
492+ case D_PEAK:
493+ s->update = update_peak_doublep;
494+ s->compute = compute_peak_doublep;
495+ break;
496+ case D_RMS:
497+ s->update = update_rms_doublep;
498+ s->compute = compute_rms_doublep;
499+ break;
500+ }
501+ break;
502+ case AV_SAMPLE_FMT_FLTP:
503+ s->copy = copy_floatp;
504+ switch (s->detection) {
505+ case D_PEAK:
506+ s->update = update_peak_floatp;
507+ s->compute = compute_peak_floatp;
508+ break;
509+ case D_RMS:
510+ s->update = update_rms_floatp;
511+ s->compute = compute_rms_floatp;
512+ break;
513+ }
514+ break;
515+ }
516+
263517 return 0;
264518 }
265519
@@ -270,7 +524,7 @@ static void flush(SilenceRemoveContext *s, · 4 unmodified lines
270524 AVFrame *silence;
271525
272526 if (*nb_samples_written) {
273− out->nb_samples = *nb_samples_written / outlink->channels;
527+ out->nb_samples = *nb_samples_written;
274528
275529 out->pts = s->next_pts;
276530 s->next_pts += av_rescale_q(out->nb_samples,
@@ -288,22 +542,24 @@ static void flush(SilenceRemoveContext *s, · 11 unmodified lines
288542 if (s->stop_silence_end <= 0 || !flush_silence)
289543 return;
290544
291− silence = ff_get_audio_buffer(outlink, s->stop_silence_end / outlink->channels);
545+ silence = ff_get_audio_buffer(outlink, s->stop_silence_end);
292546 if (!silence) {
293547 *ret = AVERROR(ENOMEM);
294548 return;
295549 }
296550
297551 if (s->stop_silence_offset < s->stop_silence_end) {
298− memcpy(silence->data[0],
299− &s->stop_silence_hold[s->stop_silence_offset],
300− (s->stop_silence_end - s->stop_silence_offset) * sizeof(double));
552+ av_samples_copy(silence->data, s->stop_silence_hold->data, 0,
553+ s->stop_silence_offset,
554+ s->stop_silence_end - s->stop_silence_offset,
555+ outlink->channels, outlink->format);
301556 }
302557
303558 if (s->stop_silence_offset > 0) {
304− memcpy(silence->data[0] + (s->stop_silence_end - s->stop_silence_offset) * sizeof(double),
305− &s->stop_silence_hold[0],
306− s->stop_silence_offset * sizeof(double));
559+ av_samples_copy(silence->data, s->stop_silence_hold->data,
560+ s->stop_silence_end - s->stop_silence_offset,
561+ 0, s->stop_silence_offset,
562+ outlink->channels, outlink->format);
307563 }
308564
309565 s->stop_silence_offset = 0;
@@ -324,7 +580,6 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 14 unmodified lines
324580 SilenceRemoveContext *s = ctx->priv;
325581 int i, j, threshold, ret = 0;
326582 int nbs, nb_samples_read, nb_samples_written;
327− double *obuf, *ibuf = (double *)in->data[0];
328583 AVFrame *out;
329584
330585 nb_samples_read = nb_samples_written = 0;
@@ -335,7 +590,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 4 unmodified lines
335590 switch (s->mode) {
336591 case SILENCE_TRIM:
337592 silence_trim:
338− nbs = in->nb_samples - nb_samples_read / outlink->channels;
593+ nbs = in->nb_samples - nb_samples_read;
339594 if (!nbs)
340595 break;
341596
@@ -343,23 +598,24 @@ silence_trim: · 1 unmodified line
343598 if (s->start_mode == T_ANY) {
344599 threshold = 0;
345600 for (j = 0; j < outlink->channels; j++) {
346− threshold |= s->compute(s, ibuf[j]) > s->start_threshold;
601+ threshold |= s->compute(s, in, j, nb_samples_read) > s->start_threshold;
347602 }
348603 } else {
349604 threshold = 1;
350605 for (j = 0; j < outlink->channels; j++) {
351− threshold &= s->compute(s, ibuf[j]) > s->start_threshold;
606+ threshold &= s->compute(s, in, j, nb_samples_read) > s->start_threshold;
352607 }
353608 }
354609
355610 if (threshold) {
356611 for (j = 0; j < outlink->channels; j++) {
357− s->update(s, *ibuf);
358− s->start_holdoff[s->start_holdoff_end++] = *ibuf++;
612+ s->update(s, in, j, nb_samples_read);
613+ s->copy(s, s->start_holdoff, in, j, s->start_holdoff_end, nb_samples_read);
359614 }
360− nb_samples_read += outlink->channels;
615+ s->start_holdoff_end++;
616+ nb_samples_read++;
361617
362− if (s->start_holdoff_end >= s->start_duration * outlink->channels) {
618+ if (s->start_holdoff_end >= s->start_duration) {
363619 if (++s->start_found_periods >= s->start_periods) {
364620 s->mode = SILENCE_TRIM_FLUSH;
365621 goto silence_trim_flush;
@@ -374,18 +630,19 @@ silence_trim: · 8 unmodified lines
374630 s->start_holdoff_end = 0;
375631
376632 for (j = 0; j < outlink->channels; j++) {
377− s->update(s, ibuf[j]);
378− if (s->start_silence) {
379− s->start_silence_hold[s->start_silence_offset++] = ibuf[j];
380− s->start_silence_end = FFMIN(s->start_silence_end + 1, outlink->channels * s->start_silence);
381− if (s->start_silence_offset >= outlink->channels * s->start_silence) {
382− s->start_silence_offset = 0;
383− }
384− }
633+ s->update(s, in, j, nb_samples_read);
634+ if (s->start_silence)
635+ s->copy(s, s->start_silence_hold, in, j, s->start_silence_offset, nb_samples_read);
385636 }
386637
387− ibuf += outlink->channels;
388− nb_samples_read += outlink->channels;
638+ nb_samples_read++;
639+ s->start_silence_offset++;
640+
641+ if (s->start_silence) {
642+ s->start_silence_end = FFMIN(s->start_silence_end + 1, s->start_silence);
643+ if (s->start_silence_offset >= s->start_silence)
644+ s->start_silence_offset = 0;
645+ }
389646 }
390647 }
391648 break;
@@ -393,11 +650,10 @@ silence_trim: · 1 unmodified line
393650 case SILENCE_TRIM_FLUSH:
394651 silence_trim_flush:
395652 nbs = s->start_holdoff_end - s->start_holdoff_offset;
396− nbs -= nbs % outlink->channels;
397653 if (!nbs)
398654 break;
399655
400− out = ff_get_audio_buffer(outlink, nbs / outlink->channels + s->start_silence_end / outlink->channels);
656+ out = ff_get_audio_buffer(outlink, nbs + s->start_silence_end);
401657 if (!out) {
402658 av_frame_free(&in);
403659 return AVERROR(ENOMEM);
@@ -405,21 +661,24 @@ silence_trim_flush: · 1 unmodified line
405661
406662 if (s->start_silence_end > 0) {
407663 if (s->start_silence_offset < s->start_silence_end) {
408− memcpy(out->data[0],
409− &s->start_silence_hold[s->start_silence_offset],
410− (s->start_silence_end - s->start_silence_offset) * sizeof(double));
664+ av_samples_copy(out->data, s->start_silence_hold->data, 0,
665+ s->start_silence_offset,
666+ s->start_silence_end - s->start_silence_offset,
667+ outlink->channels, outlink->format);
411668 }
412669
413670 if (s->start_silence_offset > 0) {
414− memcpy(out->data[0] + (s->start_silence_end - s->start_silence_offset) * sizeof(double),
415− &s->start_silence_hold[0],
416− s->start_silence_offset * sizeof(double));
671+ av_samples_copy(out->data, s->start_silence_hold->data,
672+ s->start_silence_end - s->start_silence_offset,
673+ 0, s->start_silence_offset,
674+ outlink->channels, outlink->format);
417675 }
418676 }
419677
420− memcpy(out->data[0] + s->start_silence_end * sizeof(double),
421− &s->start_holdoff[s->start_holdoff_offset],
422− nbs * sizeof(double));
678+ av_samples_copy(out->data, s->start_holdoff->data,
679+ s->start_silence_end,
680+ s->start_holdoff_offset, nbs,
681+ outlink->channels, outlink->format);
423682
424683 out->pts = s->next_pts;
425684 s->next_pts += av_rescale_q(out->nb_samples,
@@ -442,7 +701,7 @@ silence_trim_flush: · 16 unmodified lines
442701
443702 case SILENCE_COPY:
444703 silence_copy:
445− nbs = in->nb_samples - nb_samples_read / outlink->channels;
704+ nbs = in->nb_samples - nb_samples_read;
446705 if (!nbs)
447706 break;
448707
@@ -451,19 +710,18 @@ silence_copy: · 2 unmodified lines
451710 av_frame_free(&in);
452711 return AVERROR(ENOMEM);
453712 }
454− obuf = (double *)out->data[0];
455713
456714 if (s->stop_periods) {
457715 for (i = 0; i < nbs; i++) {
458716 if (s->stop_mode == T_ANY) {
459717 threshold = 0;
460718 for (j = 0; j < outlink->channels; j++) {
461− threshold |= s->compute(s, ibuf[j]) > s->stop_threshold;
719+ threshold |= s->compute(s, in, j, nb_samples_read) > s->stop_threshold;
462720 }
463721 } else {
464722 threshold = 1;
465723 for (j = 0; j < outlink->channels; j++) {
466− threshold &= s->compute(s, ibuf[j]) > s->stop_threshold;
724+ threshold &= s->compute(s, in, j, nb_samples_read) > s->stop_threshold;
467725 }
468726 }
469727
@@ -473,27 +731,29 @@ silence_copy: · 3 unmodified lines
473731 goto silence_copy_flush;
474732 } else if (threshold) {
475733 for (j = 0; j < outlink->channels; j++) {
476− s->update(s, *ibuf);
477− *obuf++ = *ibuf++;
734+ s->update(s, in, j, nb_samples_read);
735+ s->copy(s, out, in, j, nb_samples_written, nb_samples_read);
478736 }
479− nb_samples_read += outlink->channels;
480− nb_samples_written += outlink->channels;
737+
738+ nb_samples_read++;
739+ nb_samples_written++;
481740 } else if (!threshold) {
482741 for (j = 0; j < outlink->channels; j++) {
483− s->update(s, *ibuf);
742+ s->update(s, in, j, nb_samples_read);
484743 if (s->stop_silence) {
485− s->stop_silence_hold[s->stop_silence_offset++] = *ibuf;
486− s->stop_silence_end = FFMIN(s->stop_silence_end + 1, outlink->channels * s->stop_silence);
487− if (s->stop_silence_offset >= outlink->channels * s->stop_silence) {
744+ s->copy(s, s->stop_silence_hold, in, j, s->stop_silence_offset, nb_samples_read);
745+ s->stop_silence_end = FFMIN(s->stop_silence_end + 1, s->stop_silence);
746+ if (s->stop_silence_offset >= s->stop_silence) {
488747 s->stop_silence_offset = 0;
489748 }
490749 }
491750
492− s->stop_holdoff[s->stop_holdoff_end++] = *ibuf++;
751+ s->copy(s, s->stop_holdoff, in, j, s->stop_holdoff_end, nb_samples_read);
493752 }
494− nb_samples_read += outlink->channels;
753+ nb_samples_read++;
754+ s->stop_holdoff_end++;
495755
496− if (s->stop_holdoff_end >= s->stop_duration * outlink->channels) {
756+ if (s->stop_holdoff_end >= s->stop_duration) {
497757 if (++s->stop_found_periods >= s->stop_periods) {
498758 s->stop_holdoff_offset = 0;
499759 s->stop_holdoff_end = 0;
@@ -523,7 +783,10 @@ silence_copy: · 23 unmodified lines
523783 }
524784 flush(s, out, outlink, &nb_samples_written, &ret, 0);
525785 } else {
526− memcpy(obuf, ibuf, sizeof(double) * nbs * outlink->channels);
786+ av_samples_copy(out->data, in->data,
787+ nb_samples_written,
788+ nb_samples_read, nbs,
789+ outlink->channels, outlink->format);
527790
528791 out->pts = s->next_pts;
529792 s->next_pts += av_rescale_q(out->nb_samples,
@@ -537,18 +800,19 @@ silence_copy: · 7 unmodified lines
537800 case SILENCE_COPY_FLUSH:
538801 silence_copy_flush:
539802 nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
540− nbs -= nbs % outlink->channels;
541803 if (!nbs)
542804 break;
543805
544− out = ff_get_audio_buffer(outlink, nbs / outlink->channels);
806+ out = ff_get_audio_buffer(outlink, nbs);
545807 if (!out) {
546808 av_frame_free(&in);
547809 return AVERROR(ENOMEM);
548810 }
549811
550− memcpy(out->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
551− nbs * sizeof(double));
812+ av_samples_copy(out->data, s->stop_holdoff->data, 0,
813+ s->stop_holdoff_offset, nbs,
814+ outlink->channels, outlink->format);
815+
552816 s->stop_holdoff_offset += nbs;
553817
554818 out->pts = s->next_pts;
@@ -590,12 +854,13 @@ static int request_frame(AVFilterLink *outlink) · 35 unmodified lines
590854 if (nbs) {
591855 AVFrame *frame;
592856
593− frame = ff_get_audio_buffer(outlink, nbs / outlink->channels);
857+ frame = ff_get_audio_buffer(outlink, nbs);
594858 if (!frame)
595859 return AVERROR(ENOMEM);
596860
597− memcpy(frame->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
598− nbs * sizeof(double));
861+ av_samples_copy(frame->data, s->stop_holdoff->data, 0,
862+ s->stop_holdoff_offset, nbs,
863+ outlink->channels, outlink->format);
599864
600865 frame->pts = s->next_pts;
601866 s->next_pts += av_rescale_q(frame->nb_samples,
@@ -614,7 +879,9 @@ static int query_formats(AVFilterContext *ctx) · 12 unmodified lines
614879 AVFilterFormats *formats = NULL;
615880 AVFilterChannelLayouts *layouts = NULL;
616881 static const enum AVSampleFormat sample_fmts[] = {
617− AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE
882+ AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
883+ AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
884+ AV_SAMPLE_FMT_NONE
618885 };
619886 int ret;
620887
@@ -642,10 +909,10 @@ static av_cold void uninit(AVFilterContext *ctx) · 21 unmodified lines
642909 {
643910 SilenceRemoveContext *s = ctx->priv;
644911
645− av_freep(&s->start_holdoff);
646− av_freep(&s->start_silence_hold);
647− av_freep(&s->stop_holdoff);
648− av_freep(&s->stop_silence_hold);
912+ av_frame_free(&s->start_holdoff);
913+ av_frame_free(&s->start_silence_hold);
914+ av_frame_free(&s->stop_holdoff);
915+ av_frame_free(&s->stop_silence_hold);
649916 av_freep(&s->window);
650917 }
651918

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