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.

168 lines
6.4KB

  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/channel_layout.h"
  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. typedef struct {
  33. const AVClass *class;
  34. double noise; ///< noise amplitude ratio
  35. double duration; ///< minimum duration of silence until notification
  36. int64_t nb_null_samples; ///< current number of continuous zero samples
  37. int64_t start; ///< if silence is detected, this value contains the time of the first zero sample
  38. int last_sample_rate; ///< last sample rate to check for sample rate changes
  39. } SilenceDetectContext;
  40. #define OFFSET(x) offsetof(SilenceDetectContext, x)
  41. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  42. static const AVOption silencedetect_options[] = {
  43. { "n", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
  44. { "noise", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
  45. { "d", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, FLAGS },
  46. { "duration", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, FLAGS },
  47. { NULL },
  48. };
  49. AVFILTER_DEFINE_CLASS(silencedetect);
  50. static char *get_metadata_val(AVFrame *insamples, const char *key)
  51. {
  52. AVDictionaryEntry *e = av_dict_get(insamples->metadata, key, NULL, 0);
  53. return e && e->value ? e->value : NULL;
  54. }
  55. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  56. {
  57. int i;
  58. SilenceDetectContext *silence = inlink->dst->priv;
  59. const int nb_channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
  60. const int srate = inlink->sample_rate;
  61. const int nb_samples = insamples->nb_samples * nb_channels;
  62. const int64_t nb_samples_notify = srate * silence->duration * nb_channels;
  63. // scale number of null samples to the new sample rate
  64. if (silence->last_sample_rate && silence->last_sample_rate != srate)
  65. silence->nb_null_samples =
  66. srate * silence->nb_null_samples / silence->last_sample_rate;
  67. silence->last_sample_rate = srate;
  68. // TODO: support more sample formats
  69. // TODO: document metadata
  70. if (insamples->format == AV_SAMPLE_FMT_DBL) {
  71. double *p = (double *)insamples->data[0];
  72. for (i = 0; i < nb_samples; i++, p++) {
  73. if (*p < silence->noise && *p > -silence->noise) {
  74. if (!silence->start) {
  75. silence->nb_null_samples++;
  76. if (silence->nb_null_samples >= nb_samples_notify) {
  77. silence->start = insamples->pts - (int64_t)(silence->duration / av_q2d(inlink->time_base) + .5);
  78. av_dict_set(&insamples->metadata, "lavfi.silence_start",
  79. av_ts2timestr(silence->start, &inlink->time_base), 0);
  80. av_log(silence, AV_LOG_INFO, "silence_start: %s\n",
  81. get_metadata_val(insamples, "lavfi.silence_start"));
  82. }
  83. }
  84. } else {
  85. if (silence->start) {
  86. av_dict_set(&insamples->metadata, "lavfi.silence_end",
  87. av_ts2timestr(insamples->pts, &inlink->time_base), 0);
  88. av_dict_set(&insamples->metadata, "lavfi.silence_duration",
  89. av_ts2timestr(insamples->pts - silence->start, &inlink->time_base), 0);
  90. av_log(silence, AV_LOG_INFO,
  91. "silence_end: %s | silence_duration: %s\n",
  92. get_metadata_val(insamples, "lavfi.silence_end"),
  93. get_metadata_val(insamples, "lavfi.silence_duration"));
  94. }
  95. silence->nb_null_samples = silence->start = 0;
  96. }
  97. }
  98. }
  99. return ff_filter_frame(inlink->dst->outputs[0], insamples);
  100. }
  101. static int query_formats(AVFilterContext *ctx)
  102. {
  103. AVFilterFormats *formats = NULL;
  104. AVFilterChannelLayouts *layouts = NULL;
  105. static const enum AVSampleFormat sample_fmts[] = {
  106. AV_SAMPLE_FMT_DBL,
  107. AV_SAMPLE_FMT_NONE
  108. };
  109. layouts = ff_all_channel_layouts();
  110. if (!layouts)
  111. return AVERROR(ENOMEM);
  112. ff_set_common_channel_layouts(ctx, layouts);
  113. formats = ff_make_format_list(sample_fmts);
  114. if (!formats)
  115. return AVERROR(ENOMEM);
  116. ff_set_common_formats(ctx, formats);
  117. formats = ff_all_samplerates();
  118. if (!formats)
  119. return AVERROR(ENOMEM);
  120. ff_set_common_samplerates(ctx, formats);
  121. return 0;
  122. }
  123. static const AVFilterPad silencedetect_inputs[] = {
  124. {
  125. .name = "default",
  126. .type = AVMEDIA_TYPE_AUDIO,
  127. .get_audio_buffer = ff_null_get_audio_buffer,
  128. .filter_frame = filter_frame,
  129. },
  130. { NULL }
  131. };
  132. static const AVFilterPad silencedetect_outputs[] = {
  133. {
  134. .name = "default",
  135. .type = AVMEDIA_TYPE_AUDIO,
  136. },
  137. { NULL }
  138. };
  139. AVFilter avfilter_af_silencedetect = {
  140. .name = "silencedetect",
  141. .description = NULL_IF_CONFIG_SMALL("Detect silence."),
  142. .priv_size = sizeof(SilenceDetectContext),
  143. .query_formats = query_formats,
  144. .inputs = silencedetect_inputs,
  145. .outputs = silencedetect_outputs,
  146. .priv_class = &silencedetect_class,
  147. };