You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
6.3KB

  1. /*
  2. * Copyright (c) 2012 Clément Bœsch <ubitux@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Audio silence detector
  23. */
  24. #include <float.h> /* DBL_MAX */
  25. #include "libavutil/opt.h"
  26. #include "libavutil/timestamp.h"
  27. #include "audio.h"
  28. #include "formats.h"
  29. #include "avfilter.h"
  30. #include "internal.h"
  31. typedef struct {
  32. const AVClass *class;
  33. double noise; ///< noise amplitude ratio
  34. double duration; ///< minimum duration of silence until notification
  35. int64_t nb_null_samples; ///< current number of continuous zero samples
  36. int64_t start; ///< if silence is detected, this value contains the time of the first zero sample
  37. int last_sample_rate; ///< last sample rate to check for sample rate changes
  38. } SilenceDetectContext;
  39. #define OFFSET(x) offsetof(SilenceDetectContext, x)
  40. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  41. static const AVOption silencedetect_options[] = {
  42. { "n", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
  43. { "noise", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
  44. { "d", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, FLAGS },
  45. { "duration", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, FLAGS },
  46. { NULL },
  47. };
  48. AVFILTER_DEFINE_CLASS(silencedetect);
  49. static char *get_metadata_val(AVFrame *insamples, const char *key)
  50. {
  51. AVDictionaryEntry *e = av_dict_get(insamples->metadata, key, NULL, 0);
  52. return e && e->value ? e->value : NULL;
  53. }
  54. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  55. {
  56. int i;
  57. SilenceDetectContext *silence = inlink->dst->priv;
  58. const int nb_channels = inlink->channels;
  59. const int srate = inlink->sample_rate;
  60. const int nb_samples = insamples->nb_samples * nb_channels;
  61. const int64_t nb_samples_notify = srate * silence->duration * nb_channels;
  62. // scale number of null samples to the new sample rate
  63. if (silence->last_sample_rate && silence->last_sample_rate != srate)
  64. silence->nb_null_samples =
  65. srate * silence->nb_null_samples / silence->last_sample_rate;
  66. silence->last_sample_rate = srate;
  67. // TODO: support more sample formats
  68. // TODO: document metadata
  69. if (insamples->format == AV_SAMPLE_FMT_DBL) {
  70. double *p = (double *)insamples->data[0];
  71. for (i = 0; i < nb_samples; i++, p++) {
  72. if (*p < silence->noise && *p > -silence->noise) {
  73. if (!silence->start) {
  74. silence->nb_null_samples++;
  75. if (silence->nb_null_samples >= nb_samples_notify) {
  76. silence->start = insamples->pts - (int64_t)(silence->duration / av_q2d(inlink->time_base) + .5);
  77. av_dict_set(&insamples->metadata, "lavfi.silence_start",
  78. av_ts2timestr(silence->start, &inlink->time_base), 0);
  79. av_log(silence, AV_LOG_INFO, "silence_start: %s\n",
  80. get_metadata_val(insamples, "lavfi.silence_start"));
  81. }
  82. }
  83. } else {
  84. if (silence->start) {
  85. av_dict_set(&insamples->metadata, "lavfi.silence_end",
  86. av_ts2timestr(insamples->pts, &inlink->time_base), 0);
  87. av_dict_set(&insamples->metadata, "lavfi.silence_duration",
  88. av_ts2timestr(insamples->pts - silence->start, &inlink->time_base), 0);
  89. av_log(silence, AV_LOG_INFO,
  90. "silence_end: %s | silence_duration: %s\n",
  91. get_metadata_val(insamples, "lavfi.silence_end"),
  92. get_metadata_val(insamples, "lavfi.silence_duration"));
  93. }
  94. silence->nb_null_samples = silence->start = 0;
  95. }
  96. }
  97. }
  98. return ff_filter_frame(inlink->dst->outputs[0], insamples);
  99. }
  100. static int query_formats(AVFilterContext *ctx)
  101. {
  102. AVFilterFormats *formats = NULL;
  103. AVFilterChannelLayouts *layouts = NULL;
  104. static const enum AVSampleFormat sample_fmts[] = {
  105. AV_SAMPLE_FMT_DBL,
  106. AV_SAMPLE_FMT_NONE
  107. };
  108. layouts = ff_all_channel_layouts();
  109. if (!layouts)
  110. return AVERROR(ENOMEM);
  111. ff_set_common_channel_layouts(ctx, layouts);
  112. formats = ff_make_format_list(sample_fmts);
  113. if (!formats)
  114. return AVERROR(ENOMEM);
  115. ff_set_common_formats(ctx, formats);
  116. formats = ff_all_samplerates();
  117. if (!formats)
  118. return AVERROR(ENOMEM);
  119. ff_set_common_samplerates(ctx, formats);
  120. return 0;
  121. }
  122. static const AVFilterPad silencedetect_inputs[] = {
  123. {
  124. .name = "default",
  125. .type = AVMEDIA_TYPE_AUDIO,
  126. .get_audio_buffer = ff_null_get_audio_buffer,
  127. .filter_frame = filter_frame,
  128. },
  129. { NULL }
  130. };
  131. static const AVFilterPad silencedetect_outputs[] = {
  132. {
  133. .name = "default",
  134. .type = AVMEDIA_TYPE_AUDIO,
  135. },
  136. { NULL }
  137. };
  138. AVFilter avfilter_af_silencedetect = {
  139. .name = "silencedetect",
  140. .description = NULL_IF_CONFIG_SMALL("Detect silence."),
  141. .priv_size = sizeof(SilenceDetectContext),
  142. .query_formats = query_formats,
  143. .inputs = silencedetect_inputs,
  144. .outputs = silencedetect_outputs,
  145. .priv_class = &silencedetect_class,
  146. };