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
69 int64_t stop_silence_opt;69 int64_t stop_silence_opt;
70 int stop_mode;70 int stop_mode;
71 71
72 double *start_holdoff;72 AVFrame *start_holdoff;
73 double *start_silence_hold;73 AVFrame *start_silence_hold;
74 size_t start_holdoff_offset;74 size_t start_holdoff_offset;
75 size_t start_holdoff_end;75 size_t start_holdoff_end;
76 size_t start_silence_offset;76 size_t start_silence_offset;
77 size_t start_silence_end;77 size_t start_silence_end;
78 int start_found_periods;78 int start_found_periods;
79 79
80 double *stop_holdoff;80 AVFrame *stop_holdoff;
81 double *stop_silence_hold;81 AVFrame *stop_silence_hold;
82 size_t stop_holdoff_offset;82 size_t stop_holdoff_offset;
83 size_t stop_holdoff_end;83 size_t stop_holdoff_end;
84 size_t stop_silence_offset;84 size_t stop_silence_offset;
@@ -96,8 +96,10 @@ typedef struct SilenceRemoveContext { · 11 unmodified lines
96 int64_t next_pts;96 int64_t next_pts;
97 97
98 int detection;98 int detection;
99 void (*update)(struct SilenceRemoveContext *s, double sample);99 void (*update)(struct SilenceRemoveContext *s, AVFrame *frame, int ch, int offset);
100 double(*compute)(struct SilenceRemoveContext *s, double sample);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);
101} SilenceRemoveContext;103} SilenceRemoveContext;
102 104
103#define OFFSET(x) offsetof(SilenceRemoveContext, x)105#define OFFSET(x) offsetof(SilenceRemoveContext, x)
@@ -125,8 +127,50 @@ static const AVOption silenceremove_options[] = { · 21 unmodified lines
125 127
126AVFILTER_DEFINE_CLASS(silenceremove);128AVFILTER_DEFINE_CLASS(silenceremove);
127 129
128static double compute_peak(SilenceRemoveContext *s, double sample)130static void copy_double(SilenceRemoveContext *s, AVFrame *out, AVFrame *in,
131 int ch, int out_offset, int in_offset)
129{132{
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
140static 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
150static 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
160static 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
170static 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];
130 double new_sum;174 double new_sum;
131 175
132 new_sum = s->sum;176 new_sum = s->sum;
@@ -136,8 +180,38 @@ static double compute_peak(SilenceRemoveContext *s, double sample) · 3 unmodified lines
136 return new_sum / s->window_size;180 return new_sum / s->window_size;
137}181}
138 182
139static void update_peak(SilenceRemoveContext *s, double sample)183static 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
197static 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
210static void update_peak_float(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
140{211{
212 const float *samples = (const float *)frame->data[0];
213 float sample = samples[frame->channels * offset + ch];
214
141 s->sum -= *s->window_current;215 s->sum -= *s->window_current;
142 *s->window_current = fabs(sample);216 *s->window_current = fabs(sample);
143 s->sum += *s->window_current;217 s->sum += *s->window_current;
@@ -147,8 +221,10 @@ static void update_peak(SilenceRemoveContext *s, double sample) · 3 unmodified lines
147 s->window_current = s->window;221 s->window_current = s->window;
148}222}
149 223
150static double compute_rms(SilenceRemoveContext *s, double sample)224static double compute_rms_double(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
151{225{
226 const double *samples = (const double *)frame->data[0];
227 double sample = samples[frame->channels * offset + ch];
152 double new_sum;228 double new_sum;
153 229
154 new_sum = s->sum;230 new_sum = s->sum;
@@ -158,8 +234,146 @@ static double compute_rms(SilenceRemoveContext *s, double sample) · 3 unmodified lines
158 return sqrt(new_sum / s->window_size);234 return sqrt(new_sum / s->window_size);
159}235}
160 236
161static void update_rms(SilenceRemoveContext *s, double sample)237static 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
251static 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
264static void update_rms_float(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
162{265{
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
278static 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
291static 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
305static 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
318static 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
332static 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
345static 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
359static 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
372static 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
163 s->sum -= *s->window_current;377 s->sum -= *s->window_current;
164 *s->window_current = sample * sample;378 *s->window_current = sample * sample;
165 s->sum += *s->window_current;379 s->sum += *s->window_current;
@@ -178,17 +392,6 @@ static av_cold int init(AVFilterContext *ctx) · 12 unmodified lines
178 s->restart = 1;392 s->restart = 1;
179 }393 }
180 394
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
192 return 0;395 return 0;
193}396}
194 397
@@ -223,15 +426,13 @@ static int config_input(AVFilterLink *inlink) · 28 unmodified lines
223 s->stop_silence = av_rescale(s->stop_silence_opt, inlink->sample_rate,426 s->stop_silence = av_rescale(s->stop_silence_opt, inlink->sample_rate,
224 AV_TIME_BASE);427 AV_TIME_BASE);
225 428
226 s->start_holdoff = av_malloc_array(FFMAX(s->start_duration, 1),429 s->start_holdoff = ff_get_audio_buffer(ctx->outputs[0],
227 sizeof(*s->start_holdoff) *430 FFMAX(s->start_duration, 1));
228 inlink->channels);
229 if (!s->start_holdoff)431 if (!s->start_holdoff)
230 return AVERROR(ENOMEM);432 return AVERROR(ENOMEM);
231 433
232 s->start_silence_hold = av_malloc_array(FFMAX(s->start_silence, 1),434 s->start_silence_hold = ff_get_audio_buffer(ctx->outputs[0],
233 sizeof(*s->start_silence_hold) *435 FFMAX(s->start_silence, 1));
234 inlink->channels);
235 if (!s->start_silence_hold)436 if (!s->start_silence_hold)
236 return AVERROR(ENOMEM);437 return AVERROR(ENOMEM);
237 438
@@ -239,15 +440,13 @@ static int config_input(AVFilterLink *inlink) · 1 unmodified line
239 s->start_holdoff_end = 0;440 s->start_holdoff_end = 0;
240 s->start_found_periods = 0;441 s->start_found_periods = 0;
241 442
242 s->stop_holdoff = av_malloc_array(FFMAX(s->stop_duration, 1),443 s->stop_holdoff = ff_get_audio_buffer(ctx->outputs[0],
243 sizeof(*s->stop_holdoff) *444 FFMAX(s->stop_duration, 1));
244 inlink->channels);
245 if (!s->stop_holdoff)445 if (!s->stop_holdoff)
246 return AVERROR(ENOMEM);446 return AVERROR(ENOMEM);
247 447
248 s->stop_silence_hold = av_malloc_array(FFMAX(s->stop_silence, 1),448 s->stop_silence_hold = ff_get_audio_buffer(ctx->outputs[0],
249 sizeof(*s->stop_silence_hold) *449 FFMAX(s->stop_silence, 1));
250 inlink->channels);
251 if (!s->stop_silence_hold)450 if (!s->stop_silence_hold)
252 return AVERROR(ENOMEM);451 return AVERROR(ENOMEM);
253 452
@@ -260,6 +459,61 @@ static int config_input(AVFilterLink *inlink) · 6 unmodified lines
260 else459 else
261 s->mode = SILENCE_COPY;460 s->mode = SILENCE_COPY;
262 461
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
263 return 0;517 return 0;
264}518}
265 519
@@ -270,7 +524,7 @@ static void flush(SilenceRemoveContext *s, · 4 unmodified lines
270 AVFrame *silence;524 AVFrame *silence;
271 525
272 if (*nb_samples_written) {526 if (*nb_samples_written) {
273 out->nb_samples = *nb_samples_written / outlink->channels;527 out->nb_samples = *nb_samples_written;
274 528
275 out->pts = s->next_pts;529 out->pts = s->next_pts;
276 s->next_pts += av_rescale_q(out->nb_samples,530 s->next_pts += av_rescale_q(out->nb_samples,
@@ -288,22 +542,24 @@ static void flush(SilenceRemoveContext *s, · 11 unmodified lines
288 if (s->stop_silence_end <= 0 || !flush_silence)542 if (s->stop_silence_end <= 0 || !flush_silence)
289 return;543 return;
290 544
291 silence = ff_get_audio_buffer(outlink, s->stop_silence_end / outlink->channels);545 silence = ff_get_audio_buffer(outlink, s->stop_silence_end);
292 if (!silence) {546 if (!silence) {
293 *ret = AVERROR(ENOMEM);547 *ret = AVERROR(ENOMEM);
294 return;548 return;
295 }549 }
296 550
297 if (s->stop_silence_offset < s->stop_silence_end) {551 if (s->stop_silence_offset < s->stop_silence_end) {
298 memcpy(silence->data[0],552 av_samples_copy(silence->data, s->stop_silence_hold->data, 0,
299 &s->stop_silence_hold[s->stop_silence_offset],553 s->stop_silence_offset,
300 (s->stop_silence_end - s->stop_silence_offset) * sizeof(double));554 s->stop_silence_end - s->stop_silence_offset,
555 outlink->channels, outlink->format);
301 }556 }
302 557
303 if (s->stop_silence_offset > 0) {558 if (s->stop_silence_offset > 0) {
304 memcpy(silence->data[0] + (s->stop_silence_end - s->stop_silence_offset) * sizeof(double),559 av_samples_copy(silence->data, s->stop_silence_hold->data,
305 &s->stop_silence_hold[0],560 s->stop_silence_end - s->stop_silence_offset,
306 s->stop_silence_offset * sizeof(double));561 0, s->stop_silence_offset,
562 outlink->channels, outlink->format);
307 }563 }
308 564
309 s->stop_silence_offset = 0;565 s->stop_silence_offset = 0;
@@ -324,7 +580,6 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 14 unmodified lines
324 SilenceRemoveContext *s = ctx->priv;580 SilenceRemoveContext *s = ctx->priv;
325 int i, j, threshold, ret = 0;581 int i, j, threshold, ret = 0;
326 int nbs, nb_samples_read, nb_samples_written;582 int nbs, nb_samples_read, nb_samples_written;
327 double *obuf, *ibuf = (double *)in->data[0];
328 AVFrame *out;583 AVFrame *out;
329 584
330 nb_samples_read = nb_samples_written = 0;585 nb_samples_read = nb_samples_written = 0;
@@ -335,7 +590,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 4 unmodified lines
335 switch (s->mode) {590 switch (s->mode) {
336 case SILENCE_TRIM:591 case SILENCE_TRIM:
337silence_trim:592silence_trim:
338 nbs = in->nb_samples - nb_samples_read / outlink->channels;593 nbs = in->nb_samples - nb_samples_read;
339 if (!nbs)594 if (!nbs)
340 break;595 break;
341 596
@@ -343,23 +598,24 @@ silence_trim: · 1 unmodified line
343 if (s->start_mode == T_ANY) {598 if (s->start_mode == T_ANY) {
344 threshold = 0;599 threshold = 0;
345 for (j = 0; j < outlink->channels; j++) {600 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;
347 }602 }
348 } else {603 } else {
349 threshold = 1;604 threshold = 1;
350 for (j = 0; j < outlink->channels; j++) {605 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;
352 }607 }
353 }608 }
354 609
355 if (threshold) {610 if (threshold) {
356 for (j = 0; j < outlink->channels; j++) {611 for (j = 0; j < outlink->channels; j++) {
357 s->update(s, *ibuf);612 s->update(s, in, j, nb_samples_read);
358 s->start_holdoff[s->start_holdoff_end++] = *ibuf++;613 s->copy(s, s->start_holdoff, in, j, s->start_holdoff_end, nb_samples_read);
359 }614 }
360 nb_samples_read += outlink->channels;615 s->start_holdoff_end++;
616 nb_samples_read++;
361 617
362 if (s->start_holdoff_end >= s->start_duration * outlink->channels) {618 if (s->start_holdoff_end >= s->start_duration) {
363 if (++s->start_found_periods >= s->start_periods) {619 if (++s->start_found_periods >= s->start_periods) {
364 s->mode = SILENCE_TRIM_FLUSH;620 s->mode = SILENCE_TRIM_FLUSH;
365 goto silence_trim_flush;621 goto silence_trim_flush;
@@ -374,18 +630,19 @@ silence_trim: · 8 unmodified lines
374 s->start_holdoff_end = 0;630 s->start_holdoff_end = 0;
375 631
376 for (j = 0; j < outlink->channels; j++) {632 for (j = 0; j < outlink->channels; j++) {
377 s->update(s, ibuf[j]);633 s->update(s, in, j, nb_samples_read);
378 if (s->start_silence) {634 if (s->start_silence)
379 s->start_silence_hold[s->start_silence_offset++] = ibuf[j];635 s->copy(s, s->start_silence_hold, in, j, s->start_silence_offset, nb_samples_read);
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 }
385 }636 }
386 637
387 ibuf += outlink->channels;638 nb_samples_read++;
388 nb_samples_read += outlink->channels;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 }
389 }646 }
390 }647 }
391 break;648 break;
@@ -393,11 +650,10 @@ silence_trim: · 1 unmodified line
393 case SILENCE_TRIM_FLUSH:650 case SILENCE_TRIM_FLUSH:
394silence_trim_flush:651silence_trim_flush:
395 nbs = s->start_holdoff_end - s->start_holdoff_offset;652 nbs = s->start_holdoff_end - s->start_holdoff_offset;
396 nbs -= nbs % outlink->channels;
397 if (!nbs)653 if (!nbs)
398 break;654 break;
399 655
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);
401 if (!out) {657 if (!out) {
402 av_frame_free(&in);658 av_frame_free(&in);
403 return AVERROR(ENOMEM);659 return AVERROR(ENOMEM);
@@ -405,21 +661,24 @@ silence_trim_flush: · 1 unmodified line
405 661
406 if (s->start_silence_end > 0) {662 if (s->start_silence_end > 0) {
407 if (s->start_silence_offset < s->start_silence_end) {663 if (s->start_silence_offset < s->start_silence_end) {
408 memcpy(out->data[0],664 av_samples_copy(out->data, s->start_silence_hold->data, 0,
409 &s->start_silence_hold[s->start_silence_offset],665 s->start_silence_offset,
410 (s->start_silence_end - s->start_silence_offset) * sizeof(double));666 s->start_silence_end - s->start_silence_offset,
667 outlink->channels, outlink->format);
411 }668 }
412 669
413 if (s->start_silence_offset > 0) {670 if (s->start_silence_offset > 0) {
414 memcpy(out->data[0] + (s->start_silence_end - s->start_silence_offset) * sizeof(double),671 av_samples_copy(out->data, s->start_silence_hold->data,
415 &s->start_silence_hold[0],672 s->start_silence_end - s->start_silence_offset,
416 s->start_silence_offset * sizeof(double));673 0, s->start_silence_offset,
674 outlink->channels, outlink->format);
417 }675 }
418 }676 }
419 677
420 memcpy(out->data[0] + s->start_silence_end * sizeof(double),678 av_samples_copy(out->data, s->start_holdoff->data,
421 &s->start_holdoff[s->start_holdoff_offset],679 s->start_silence_end,
422 nbs * sizeof(double));680 s->start_holdoff_offset, nbs,
681 outlink->channels, outlink->format);
423 682
424 out->pts = s->next_pts;683 out->pts = s->next_pts;
425 s->next_pts += av_rescale_q(out->nb_samples,684 s->next_pts += av_rescale_q(out->nb_samples,
@@ -442,7 +701,7 @@ silence_trim_flush: · 16 unmodified lines
442 701
443 case SILENCE_COPY:702 case SILENCE_COPY:
444silence_copy:703silence_copy:
445 nbs = in->nb_samples - nb_samples_read / outlink->channels;704 nbs = in->nb_samples - nb_samples_read;
446 if (!nbs)705 if (!nbs)
447 break;706 break;
448 707
@@ -451,19 +710,18 @@ silence_copy: · 2 unmodified lines
451 av_frame_free(&in);710 av_frame_free(&in);
452 return AVERROR(ENOMEM);711 return AVERROR(ENOMEM);
453 }712 }
454 obuf = (double *)out->data[0];
455 713
456 if (s->stop_periods) {714 if (s->stop_periods) {
457 for (i = 0; i < nbs; i++) {715 for (i = 0; i < nbs; i++) {
458 if (s->stop_mode == T_ANY) {716 if (s->stop_mode == T_ANY) {
459 threshold = 0;717 threshold = 0;
460 for (j = 0; j < outlink->channels; j++) {718 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;
462 }720 }
463 } else {721 } else {
464 threshold = 1;722 threshold = 1;
465 for (j = 0; j < outlink->channels; j++) {723 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;
467 }725 }
468 }726 }
469 727
@@ -473,27 +731,29 @@ silence_copy: · 3 unmodified lines
473 goto silence_copy_flush;731 goto silence_copy_flush;
474 } else if (threshold) {732 } else if (threshold) {
475 for (j = 0; j < outlink->channels; j++) {733 for (j = 0; j < outlink->channels; j++) {
476 s->update(s, *ibuf);734 s->update(s, in, j, nb_samples_read);
477 *obuf++ = *ibuf++;735 s->copy(s, out, in, j, nb_samples_written, nb_samples_read);
478 }736 }
479 nb_samples_read += outlink->channels;737
480 nb_samples_written += outlink->channels;738 nb_samples_read++;
739 nb_samples_written++;
481 } else if (!threshold) {740 } else if (!threshold) {
482 for (j = 0; j < outlink->channels; j++) {741 for (j = 0; j < outlink->channels; j++) {
483 s->update(s, *ibuf);742 s->update(s, in, j, nb_samples_read);
484 if (s->stop_silence) {743 if (s->stop_silence) {
485 s->stop_silence_hold[s->stop_silence_offset++] = *ibuf;744 s->copy(s, s->stop_silence_hold, in, j, s->stop_silence_offset, nb_samples_read);
486 s->stop_silence_end = FFMIN(s->stop_silence_end + 1, outlink->channels * s->stop_silence);745 s->stop_silence_end = FFMIN(s->stop_silence_end + 1, s->stop_silence);
487 if (s->stop_silence_offset >= outlink->channels * s->stop_silence) {746 if (s->stop_silence_offset >= s->stop_silence) {
488 s->stop_silence_offset = 0;747 s->stop_silence_offset = 0;
489 }748 }
490 }749 }
491 750
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);
493 }752 }
494 nb_samples_read += outlink->channels;753 nb_samples_read++;
754 s->stop_holdoff_end++;
495 755
496 if (s->stop_holdoff_end >= s->stop_duration * outlink->channels) {756 if (s->stop_holdoff_end >= s->stop_duration) {
497 if (++s->stop_found_periods >= s->stop_periods) {757 if (++s->stop_found_periods >= s->stop_periods) {
498 s->stop_holdoff_offset = 0;758 s->stop_holdoff_offset = 0;
499 s->stop_holdoff_end = 0;759 s->stop_holdoff_end = 0;
@@ -523,7 +783,10 @@ silence_copy: · 23 unmodified lines
523 }783 }
524 flush(s, out, outlink, &nb_samples_written, &ret, 0);784 flush(s, out, outlink, &nb_samples_written, &ret, 0);
525 } else {785 } 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);
527 790
528 out->pts = s->next_pts;791 out->pts = s->next_pts;
529 s->next_pts += av_rescale_q(out->nb_samples,792 s->next_pts += av_rescale_q(out->nb_samples,
@@ -537,18 +800,19 @@ silence_copy: · 7 unmodified lines
537 case SILENCE_COPY_FLUSH:800 case SILENCE_COPY_FLUSH:
538silence_copy_flush:801silence_copy_flush:
539 nbs = s->stop_holdoff_end - s->stop_holdoff_offset;802 nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
540 nbs -= nbs % outlink->channels;
541 if (!nbs)803 if (!nbs)
542 break;804 break;
543 805
544 out = ff_get_audio_buffer(outlink, nbs / outlink->channels);806 out = ff_get_audio_buffer(outlink, nbs);
545 if (!out) {807 if (!out) {
546 av_frame_free(&in);808 av_frame_free(&in);
547 return AVERROR(ENOMEM);809 return AVERROR(ENOMEM);
548 }810 }
549 811
550 memcpy(out->data[0], &s->stop_holdoff[s->stop_holdoff_offset],812 av_samples_copy(out->data, s->stop_holdoff->data, 0,
551 nbs * sizeof(double));813 s->stop_holdoff_offset, nbs,
814 outlink->channels, outlink->format);
815
552 s->stop_holdoff_offset += nbs;816 s->stop_holdoff_offset += nbs;
553 817
554 out->pts = s->next_pts;818 out->pts = s->next_pts;
@@ -590,12 +854,13 @@ static int request_frame(AVFilterLink *outlink) · 35 unmodified lines
590 if (nbs) {854 if (nbs) {
591 AVFrame *frame;855 AVFrame *frame;
592 856
593 frame = ff_get_audio_buffer(outlink, nbs / outlink->channels);857 frame = ff_get_audio_buffer(outlink, nbs);
594 if (!frame)858 if (!frame)
595 return AVERROR(ENOMEM);859 return AVERROR(ENOMEM);
596 860
597 memcpy(frame->data[0], &s->stop_holdoff[s->stop_holdoff_offset],861 av_samples_copy(frame->data, s->stop_holdoff->data, 0,
598 nbs * sizeof(double));862 s->stop_holdoff_offset, nbs,
863 outlink->channels, outlink->format);
599 864
600 frame->pts = s->next_pts;865 frame->pts = s->next_pts;
601 s->next_pts += av_rescale_q(frame->nb_samples,866 s->next_pts += av_rescale_q(frame->nb_samples,
@@ -614,7 +879,9 @@ static int query_formats(AVFilterContext *ctx) · 12 unmodified lines
614 AVFilterFormats *formats = NULL;879 AVFilterFormats *formats = NULL;
615 AVFilterChannelLayouts *layouts = NULL;880 AVFilterChannelLayouts *layouts = NULL;
616 static const enum AVSampleFormat sample_fmts[] = {881 static const enum AVSampleFormat sample_fmts[] = {
617 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE882 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
883 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
884 AV_SAMPLE_FMT_NONE
618 };885 };
619 int ret;886 int ret;
620 887
@@ -642,10 +909,10 @@ static av_cold void uninit(AVFilterContext *ctx) · 21 unmodified lines
642{909{
643 SilenceRemoveContext *s = ctx->priv;910 SilenceRemoveContext *s = ctx->priv;
644 911
645 av_freep(&s->start_holdoff);912 av_frame_free(&s->start_holdoff);
646 av_freep(&s->start_silence_hold);913 av_frame_free(&s->start_silence_hold);
647 av_freep(&s->stop_holdoff);914 av_frame_free(&s->stop_holdoff);
648 av_freep(&s->stop_silence_hold);915 av_frame_free(&s->stop_silence_hold);
649 av_freep(&s->window);916 av_freep(&s->window);
650}917}
651 918

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