FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
af_silenceremove.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001 Heikki Leinonen
3  * Copyright (c) 2001 Chris Bagwell
4  * Copyright (c) 2003 Donnie Smith
5  * Copyright (c) 2014 Paul B Mahol
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include <float.h> /* DBL_MAX */
25 
26 #include "libavutil/opt.h"
27 #include "libavutil/timestamp.h"
28 #include "audio.h"
29 #include "formats.h"
30 #include "avfilter.h"
31 #include "internal.h"
32 
36 };
37 
41 };
42 
49 };
50 
51 typedef struct SilenceRemoveContext {
52  const AVClass *class;
53 
55 
57  int64_t start_duration;
60  int64_t start_silence;
63 
65  int64_t stop_duration;
68  int64_t stop_silence;
70  int stop_mode;
71 
72  double *start_holdoff;
79 
80  double *stop_holdoff;
87 
88  double window_ratio;
89  double *window;
90  double *window_current;
91  double *window_end;
93  double sum;
94 
95  int restart;
96  int64_t next_pts;
97 
98  int detection;
99  void (*update)(struct SilenceRemoveContext *s, double sample);
100  double(*compute)(struct SilenceRemoveContext *s, double sample);
102 
103 #define OFFSET(x) offsetof(SilenceRemoveContext, x)
104 #define AF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
105 
106 static const AVOption silenceremove_options[] = {
107  { "start_periods", NULL, OFFSET(start_periods), AV_OPT_TYPE_INT, {.i64=0}, 0, 9000, AF },
108  { "start_duration", "set start duration of non-silence part", OFFSET(start_duration_opt), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX, AF },
109  { "start_threshold", "set threshold for start silence detection", OFFSET(start_threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX, AF },
110  { "start_silence", "set start duration of silence part to keep", OFFSET(start_silence_opt), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX, AF },
111  { "start_mode", "set which channel will trigger trimming from start", OFFSET(start_mode), AV_OPT_TYPE_INT, {.i64=T_ANY}, T_ANY, T_ALL, AF, "mode" },
112  { "any", 0, 0, AV_OPT_TYPE_CONST, {.i64=T_ANY}, 0, 0, AF, "mode" },
113  { "all", 0, 0, AV_OPT_TYPE_CONST, {.i64=T_ALL}, 0, 0, AF, "mode" },
114  { "stop_periods", NULL, OFFSET(stop_periods), AV_OPT_TYPE_INT, {.i64=0}, -9000, 9000, AF },
115  { "stop_duration", "set stop duration of non-silence part", OFFSET(stop_duration_opt), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX, AF },
116  { "stop_threshold", "set threshold for stop silence detection", OFFSET(stop_threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX, AF },
117  { "stop_silence", "set stop duration of silence part to keep", OFFSET(stop_silence_opt), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT32_MAX, AF },
118  { "stop_mode", "set which channel will trigger trimming from end", OFFSET(stop_mode), AV_OPT_TYPE_INT, {.i64=T_ANY}, T_ANY, T_ALL, AF, "mode" },
119  { "detection", "set how silence is detected", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=D_RMS}, D_PEAK,D_RMS, AF, "detection" },
120  { "peak", "use absolute values of samples", 0, AV_OPT_TYPE_CONST, {.i64=D_PEAK},0, 0, AF, "detection" },
121  { "rms", "use squared values of samples", 0, AV_OPT_TYPE_CONST, {.i64=D_RMS}, 0, 0, AF, "detection" },
122  { "window", "set duration of window in seconds", OFFSET(window_ratio), AV_OPT_TYPE_DOUBLE, {.dbl=0.02}, 0, 10, AF },
123  { NULL }
124 };
125 
126 AVFILTER_DEFINE_CLASS(silenceremove);
127 
128 static double compute_peak(SilenceRemoveContext *s, double sample)
129 {
130  double new_sum;
131 
132  new_sum = s->sum;
133  new_sum -= *s->window_current;
134  new_sum += fabs(sample);
135 
136  return new_sum / s->window_size;
137 }
138 
140 {
141  s->sum -= *s->window_current;
142  *s->window_current = fabs(sample);
143  s->sum += *s->window_current;
144 
145  s->window_current++;
146  if (s->window_current >= s->window_end)
147  s->window_current = s->window;
148 }
149 
150 static double compute_rms(SilenceRemoveContext *s, double sample)
151 {
152  double new_sum;
153 
154  new_sum = s->sum;
155  new_sum -= *s->window_current;
156  new_sum += sample * sample;
157 
158  return sqrt(new_sum / s->window_size);
159 }
160 
162 {
163  s->sum -= *s->window_current;
164  *s->window_current = sample * sample;
165  s->sum += *s->window_current;
166 
167  s->window_current++;
168  if (s->window_current >= s->window_end)
169  s->window_current = s->window;
170 }
171 
173 {
174  SilenceRemoveContext *s = ctx->priv;
175 
176  if (s->stop_periods < 0) {
177  s->stop_periods = -s->stop_periods;
178  s->restart = 1;
179  }
180 
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;
193 }
194 
196 {
197  memset(s->window, 0, s->window_size * sizeof(*s->window));
198 
199  s->window_current = s->window;
200  s->window_end = s->window + s->window_size;
201  s->sum = 0;
202 }
203 
204 static int config_input(AVFilterLink *inlink)
205 {
206  AVFilterContext *ctx = inlink->dst;
207  SilenceRemoveContext *s = ctx->priv;
208 
209  s->window_size = FFMAX((inlink->sample_rate * s->window_ratio), 1) * inlink->channels;
210  s->window = av_malloc_array(s->window_size, sizeof(*s->window));
211  if (!s->window)
212  return AVERROR(ENOMEM);
213 
214  clear_window(s);
215 
217  AV_TIME_BASE);
219  AV_TIME_BASE);
221  AV_TIME_BASE);
223  AV_TIME_BASE);
224 
226  sizeof(*s->start_holdoff) *
227  inlink->channels);
228  if (!s->start_holdoff)
229  return AVERROR(ENOMEM);
230 
232  sizeof(*s->start_silence_hold) *
233  inlink->channels);
234  if (!s->start_silence_hold)
235  return AVERROR(ENOMEM);
236 
237  s->start_holdoff_offset = 0;
238  s->start_holdoff_end = 0;
239  s->start_found_periods = 0;
240 
242  sizeof(*s->stop_holdoff) *
243  inlink->channels);
244  if (!s->stop_holdoff)
245  return AVERROR(ENOMEM);
246 
248  sizeof(*s->stop_silence_hold) *
249  inlink->channels);
250  if (!s->stop_silence_hold)
251  return AVERROR(ENOMEM);
252 
253  s->stop_holdoff_offset = 0;
254  s->stop_holdoff_end = 0;
255  s->stop_found_periods = 0;
256 
257  if (s->start_periods)
258  s->mode = SILENCE_TRIM;
259  else
260  s->mode = SILENCE_COPY;
261 
262  return 0;
263 }
264 
266  AVFrame *out, AVFilterLink *outlink,
267  int *nb_samples_written, int *ret, int flush_silence)
268 {
269  AVFrame *silence;
270 
271  if (*nb_samples_written) {
272  out->nb_samples = *nb_samples_written / outlink->channels;
273 
274  out->pts = s->next_pts;
275  s->next_pts += av_rescale_q(out->nb_samples,
276  (AVRational){1, outlink->sample_rate},
277  outlink->time_base);
278 
279  *ret = ff_filter_frame(outlink, out);
280  if (*ret < 0)
281  return;
282  *nb_samples_written = 0;
283  } else {
284  av_frame_free(&out);
285  }
286 
287  if (s->stop_silence_end <= 0 || !flush_silence)
288  return;
289 
290  silence = ff_get_audio_buffer(outlink, s->stop_silence_end / outlink->channels);
291  if (!silence) {
292  *ret = AVERROR(ENOMEM);
293  return;
294  }
295 
296  if (s->stop_silence_offset < s->stop_silence_end) {
297  memcpy(silence->data[0],
298  &s->stop_silence_hold[s->stop_silence_offset],
299  (s->stop_silence_end - s->stop_silence_offset) * sizeof(double));
300  }
301 
302  if (s->stop_silence_offset > 0) {
303  memcpy(silence->data[0] + (s->stop_silence_end - s->stop_silence_offset) * sizeof(double),
304  &s->stop_silence_hold[0],
305  s->stop_silence_offset * sizeof(double));
306  }
307 
308  s->stop_silence_offset = 0;
309  s->stop_silence_end = 0;
310 
311  silence->pts = s->next_pts;
312  s->next_pts += av_rescale_q(silence->nb_samples,
313  (AVRational){1, outlink->sample_rate},
314  outlink->time_base);
315 
316  *ret = ff_filter_frame(outlink, silence);
317 }
318 
319 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
320 {
321  AVFilterContext *ctx = inlink->dst;
322  AVFilterLink *outlink = ctx->outputs[0];
323  SilenceRemoveContext *s = ctx->priv;
324  int i, j, threshold, ret = 0;
325  int nbs, nb_samples_read, nb_samples_written;
326  double *obuf, *ibuf = (double *)in->data[0];
327  AVFrame *out;
328 
329  nb_samples_read = nb_samples_written = 0;
330 
331  switch (s->mode) {
332  case SILENCE_TRIM:
333 silence_trim:
334  nbs = in->nb_samples - nb_samples_read / outlink->channels;
335  if (!nbs)
336  break;
337 
338  for (i = 0; i < nbs; i++) {
339  if (s->start_mode == T_ANY) {
340  threshold = 0;
341  for (j = 0; j < outlink->channels; j++) {
342  threshold |= s->compute(s, ibuf[j]) > s->start_threshold;
343  }
344  } else {
345  threshold = 1;
346  for (j = 0; j < outlink->channels; j++) {
347  threshold &= s->compute(s, ibuf[j]) > s->start_threshold;
348  }
349  }
350 
351  if (threshold) {
352  for (j = 0; j < outlink->channels; j++) {
353  s->update(s, *ibuf);
354  s->start_holdoff[s->start_holdoff_end++] = *ibuf++;
355  }
356  nb_samples_read += outlink->channels;
357 
358  if (s->start_holdoff_end >= s->start_duration * outlink->channels) {
359  if (++s->start_found_periods >= s->start_periods) {
361  goto silence_trim_flush;
362  }
363 
364  s->start_holdoff_offset = 0;
365  s->start_holdoff_end = 0;
366  s->start_silence_offset = 0;
367  s->start_silence_end = 0;
368  }
369  } else {
370  s->start_holdoff_end = 0;
371 
372  for (j = 0; j < outlink->channels; j++) {
373  s->update(s, ibuf[j]);
374  if (s->start_silence) {
375  s->start_silence_hold[s->start_silence_offset++] = ibuf[j];
376  s->start_silence_end = FFMIN(s->start_silence_end + 1, outlink->channels * s->start_silence);
377  if (s->start_silence_offset >= outlink->channels * s->start_silence) {
378  s->start_silence_offset = 0;
379  }
380  }
381  }
382 
383  ibuf += outlink->channels;
384  nb_samples_read += outlink->channels;
385  }
386  }
387  break;
388 
389  case SILENCE_TRIM_FLUSH:
390 silence_trim_flush:
392  nbs -= nbs % outlink->channels;
393  if (!nbs)
394  break;
395 
396  out = ff_get_audio_buffer(outlink, nbs / outlink->channels + s->start_silence_end / outlink->channels);
397  if (!out) {
398  av_frame_free(&in);
399  return AVERROR(ENOMEM);
400  }
401 
402  if (s->start_silence_end > 0) {
404  memcpy(out->data[0],
406  (s->start_silence_end - s->start_silence_offset) * sizeof(double));
407  }
408 
409  if (s->start_silence_offset > 0) {
410  memcpy(out->data[0] + (s->start_silence_end - s->start_silence_offset) * sizeof(double),
411  &s->start_silence_hold[0],
412  s->start_silence_offset * sizeof(double));
413  }
414  }
415 
416  memcpy(out->data[0] + s->start_silence_end * sizeof(double),
418  nbs * sizeof(double));
419 
420  out->pts = s->next_pts;
421  s->next_pts += av_rescale_q(out->nb_samples,
422  (AVRational){1, outlink->sample_rate},
423  outlink->time_base);
424 
425  s->start_holdoff_offset += nbs;
426 
427  ret = ff_filter_frame(outlink, out);
428 
430  s->start_holdoff_offset = 0;
431  s->start_holdoff_end = 0;
432  s->start_silence_offset = 0;
433  s->start_silence_end = 0;
434  s->mode = SILENCE_COPY;
435  goto silence_copy;
436  }
437  break;
438 
439  case SILENCE_COPY:
440 silence_copy:
441  nbs = in->nb_samples - nb_samples_read / outlink->channels;
442  if (!nbs)
443  break;
444 
445  out = ff_get_audio_buffer(outlink, nbs);
446  if (!out) {
447  av_frame_free(&in);
448  return AVERROR(ENOMEM);
449  }
450  obuf = (double *)out->data[0];
451 
452  if (s->stop_periods) {
453  for (i = 0; i < nbs; i++) {
454  if (s->stop_mode == T_ANY) {
455  threshold = 0;
456  for (j = 0; j < outlink->channels; j++) {
457  threshold |= s->compute(s, ibuf[j]) > s->stop_threshold;
458  }
459  } else {
460  threshold = 1;
461  for (j = 0; j < outlink->channels; j++) {
462  threshold &= s->compute(s, ibuf[j]) > s->stop_threshold;
463  }
464  }
465 
466  if (threshold && s->stop_holdoff_end && !s->stop_silence) {
468  flush(s, out, outlink, &nb_samples_written, &ret, 0);
469  goto silence_copy_flush;
470  } else if (threshold) {
471  for (j = 0; j < outlink->channels; j++) {
472  s->update(s, *ibuf);
473  *obuf++ = *ibuf++;
474  }
475  nb_samples_read += outlink->channels;
476  nb_samples_written += outlink->channels;
477  } else if (!threshold) {
478  for (j = 0; j < outlink->channels; j++) {
479  s->update(s, *ibuf);
480  if (s->stop_silence) {
481  s->stop_silence_hold[s->stop_silence_offset++] = *ibuf;
482  s->stop_silence_end = FFMIN(s->stop_silence_end + 1, outlink->channels * s->stop_silence);
483  if (s->stop_silence_offset >= outlink->channels * s->stop_silence) {
484  s->stop_silence_offset = 0;
485  }
486  }
487 
488  s->stop_holdoff[s->stop_holdoff_end++] = *ibuf++;
489  }
490  nb_samples_read += outlink->channels;
491 
492  if (s->stop_holdoff_end >= s->stop_duration * outlink->channels) {
493  if (++s->stop_found_periods >= s->stop_periods) {
494  s->stop_holdoff_offset = 0;
495  s->stop_holdoff_end = 0;
496 
497  if (!s->restart) {
498  s->mode = SILENCE_STOP;
499  flush(s, out, outlink, &nb_samples_written, &ret, 1);
500  goto silence_stop;
501  } else {
502  s->stop_found_periods = 0;
503  s->start_found_periods = 0;
504  s->start_holdoff_offset = 0;
505  s->start_holdoff_end = 0;
506  s->start_silence_offset = 0;
507  s->start_silence_end = 0;
508  clear_window(s);
509  s->mode = SILENCE_TRIM;
510  flush(s, out, outlink, &nb_samples_written, &ret, 1);
511  goto silence_trim;
512  }
513  }
515  flush(s, out, outlink, &nb_samples_written, &ret, 0);
516  goto silence_copy_flush;
517  }
518  }
519  }
520  flush(s, out, outlink, &nb_samples_written, &ret, 0);
521  } else {
522  memcpy(obuf, ibuf, sizeof(double) * nbs * outlink->channels);
523 
524  out->pts = s->next_pts;
525  s->next_pts += av_rescale_q(out->nb_samples,
526  (AVRational){1, outlink->sample_rate},
527  outlink->time_base);
528 
529  ret = ff_filter_frame(outlink, out);
530  }
531  break;
532 
533  case SILENCE_COPY_FLUSH:
534 silence_copy_flush:
535  nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
536  nbs -= nbs % outlink->channels;
537  if (!nbs)
538  break;
539 
540  out = ff_get_audio_buffer(outlink, nbs / outlink->channels);
541  if (!out) {
542  av_frame_free(&in);
543  return AVERROR(ENOMEM);
544  }
545 
546  memcpy(out->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
547  nbs * sizeof(double));
548  s->stop_holdoff_offset += nbs;
549 
550  out->pts = s->next_pts;
551  s->next_pts += av_rescale_q(out->nb_samples,
552  (AVRational){1, outlink->sample_rate},
553  outlink->time_base);
554 
555  ret = ff_filter_frame(outlink, out);
556 
557  if (s->stop_holdoff_offset == s->stop_holdoff_end) {
558  s->stop_holdoff_offset = 0;
559  s->stop_holdoff_end = 0;
560  s->stop_silence_offset = 0;
561  s->stop_silence_end = 0;
562  s->mode = SILENCE_COPY;
563  goto silence_copy;
564  }
565  break;
566  case SILENCE_STOP:
567 silence_stop:
568  break;
569  }
570 
571  av_frame_free(&in);
572 
573  return ret;
574 }
575 
576 static int request_frame(AVFilterLink *outlink)
577 {
578  AVFilterContext *ctx = outlink->src;
579  SilenceRemoveContext *s = ctx->priv;
580  int ret;
581 
582  ret = ff_request_frame(ctx->inputs[0]);
583  if (ret == AVERROR_EOF && (s->mode == SILENCE_COPY_FLUSH ||
584  s->mode == SILENCE_COPY)) {
585  int nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
586  if (nbs) {
587  AVFrame *frame;
588 
589  frame = ff_get_audio_buffer(outlink, nbs / outlink->channels);
590  if (!frame)
591  return AVERROR(ENOMEM);
592 
593  memcpy(frame->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
594  nbs * sizeof(double));
595 
596  frame->pts = s->next_pts;
597  s->next_pts += av_rescale_q(frame->nb_samples,
598  (AVRational){1, outlink->sample_rate},
599  outlink->time_base);
600 
601  ret = ff_filter_frame(outlink, frame);
602  }
603  s->mode = SILENCE_STOP;
604  }
605  return ret;
606 }
607 
609 {
612  static const enum AVSampleFormat sample_fmts[] = {
614  };
615  int ret;
616 
617  layouts = ff_all_channel_counts();
618  if (!layouts)
619  return AVERROR(ENOMEM);
620  ret = ff_set_common_channel_layouts(ctx, layouts);
621  if (ret < 0)
622  return ret;
623 
624  formats = ff_make_format_list(sample_fmts);
625  if (!formats)
626  return AVERROR(ENOMEM);
627  ret = ff_set_common_formats(ctx, formats);
628  if (ret < 0)
629  return ret;
630 
631  formats = ff_all_samplerates();
632  if (!formats)
633  return AVERROR(ENOMEM);
634  return ff_set_common_samplerates(ctx, formats);
635 }
636 
638 {
639  SilenceRemoveContext *s = ctx->priv;
640 
641  av_freep(&s->start_holdoff);
643  av_freep(&s->stop_holdoff);
645  av_freep(&s->window);
646 }
647 
649  {
650  .name = "default",
651  .type = AVMEDIA_TYPE_AUDIO,
652  .config_props = config_input,
653  .filter_frame = filter_frame,
654  },
655  { NULL }
656 };
657 
659  {
660  .name = "default",
661  .type = AVMEDIA_TYPE_AUDIO,
662  .request_frame = request_frame,
663  },
664  { NULL }
665 };
666 
668  .name = "silenceremove",
669  .description = NULL_IF_CONFIG_SMALL("Remove silence."),
670  .priv_size = sizeof(SilenceRemoveContext),
671  .priv_class = &silenceremove_class,
672  .init = init,
673  .uninit = uninit,
675  .inputs = silenceremove_inputs,
676  .outputs = silenceremove_outputs,
677 };
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
#define NULL
Definition: coverity.c:32
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:549
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
SilenceDetect
AVOption.
Definition: opt.h:246
static void flush(SilenceRemoveContext *s, AVFrame *out, AVFilterLink *outlink, int *nb_samples_written, int *ret, int flush_silence)
static const AVFilterPad silenceremove_outputs[]
Main libavfilter public API header.
void(* update)(struct SilenceRemoveContext *s, double sample)
AVFilter ff_af_silenceremove
#define sample
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
double(* compute)(struct SilenceRemoveContext *s, double sample)
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
#define av_cold
Definition: attributes.h:82
AVOptions.
timestamp utils, mostly useful for debugging/logging purposes
SilenceMode
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:319
static AVFrame * frame
static av_cold int init(AVFilterContext *ctx)
#define AVERROR_EOF
End of file.
Definition: error.h:55
A filter pad used for either input or output.
Definition: internal.h:54
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:568
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:86
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define FFMAX(a, b)
Definition: common.h:94
ThresholdMode
static int query_formats(AVFilterContext *ctx)
static int config_input(AVFilterLink *inlink)
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
#define FFMIN(a, b)
Definition: common.h:96
static const AVFilterPad silenceremove_inputs[]
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
static int request_frame(AVFilterLink *outlink)
AVFILTER_DEFINE_CLASS(silenceremove)
enum SilenceMode mode
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
A list of supported channel layouts.
Definition: formats.h:85
static double compute_peak(SilenceRemoveContext *s, double sample)
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
static av_cold void uninit(AVFilterContext *ctx)
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
static void update_rms(SilenceRemoveContext *s, double sample)
Rational number (pair of numerator and denominator).
Definition: rational.h:58
static double compute_rms(SilenceRemoveContext *s, double sample)
static void clear_window(SilenceRemoveContext *s)
#define AF
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
static void update_peak(SilenceRemoveContext *s, double sample)
if(ret< 0)
Definition: vf_mcdeint.c:279
static const AVOption silenceremove_options[]
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
FILE * out
Definition: movenc.c:54
#define av_freep(p)
#define av_malloc_array(a, b)
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:407
formats
Definition: signature.h:48
internal API functions
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition...
Definition: formats.c:410
#define OFFSET(x)
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:292
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556