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.

165 lines
6.1KB

  1. /*
  2. * Copyright (c) 2012 Clément Bœsch <u pkh me>
  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 *s = 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 * s->duration * nb_channels;
  62. // scale number of null samples to the new sample rate
  63. if (s->last_sample_rate && s->last_sample_rate != srate)
  64. s->nb_null_samples = srate * s->nb_null_samples / s->last_sample_rate;
  65. s->last_sample_rate = srate;
  66. // TODO: support more sample formats
  67. // TODO: document metadata
  68. if (insamples->format == AV_SAMPLE_FMT_DBL) {
  69. double *p = (double *)insamples->data[0];
  70. for (i = 0; i < nb_samples; i++, p++) {
  71. if (*p < s->noise && *p > -s->noise) {
  72. if (!s->start) {
  73. s->nb_null_samples++;
  74. if (s->nb_null_samples >= nb_samples_notify) {
  75. s->start = insamples->pts - (int64_t)(s->duration / av_q2d(inlink->time_base) + .5);
  76. av_dict_set(&insamples->metadata, "lavfi.silence_start",
  77. av_ts2timestr(s->start, &inlink->time_base), 0);
  78. av_log(s, AV_LOG_INFO, "silence_start: %s\n",
  79. get_metadata_val(insamples, "lavfi.silence_start"));
  80. }
  81. }
  82. } else {
  83. if (s->start) {
  84. av_dict_set(&insamples->metadata, "lavfi.silence_end",
  85. av_ts2timestr(insamples->pts, &inlink->time_base), 0);
  86. av_dict_set(&insamples->metadata, "lavfi.silence_duration",
  87. av_ts2timestr(insamples->pts - s->start, &inlink->time_base), 0);
  88. av_log(s, AV_LOG_INFO,
  89. "silence_end: %s | silence_duration: %s\n",
  90. get_metadata_val(insamples, "lavfi.silence_end"),
  91. get_metadata_val(insamples, "lavfi.silence_duration"));
  92. }
  93. s->nb_null_samples = s->start = 0;
  94. }
  95. }
  96. }
  97. return ff_filter_frame(inlink->dst->outputs[0], insamples);
  98. }
  99. static int query_formats(AVFilterContext *ctx)
  100. {
  101. AVFilterFormats *formats = NULL;
  102. AVFilterChannelLayouts *layouts = NULL;
  103. static const enum AVSampleFormat sample_fmts[] = {
  104. AV_SAMPLE_FMT_DBL,
  105. AV_SAMPLE_FMT_NONE
  106. };
  107. layouts = ff_all_channel_layouts();
  108. if (!layouts)
  109. return AVERROR(ENOMEM);
  110. ff_set_common_channel_layouts(ctx, layouts);
  111. formats = ff_make_format_list(sample_fmts);
  112. if (!formats)
  113. return AVERROR(ENOMEM);
  114. ff_set_common_formats(ctx, formats);
  115. formats = ff_all_samplerates();
  116. if (!formats)
  117. return AVERROR(ENOMEM);
  118. ff_set_common_samplerates(ctx, formats);
  119. return 0;
  120. }
  121. static const AVFilterPad silencedetect_inputs[] = {
  122. {
  123. .name = "default",
  124. .type = AVMEDIA_TYPE_AUDIO,
  125. .filter_frame = filter_frame,
  126. },
  127. { NULL }
  128. };
  129. static const AVFilterPad silencedetect_outputs[] = {
  130. {
  131. .name = "default",
  132. .type = AVMEDIA_TYPE_AUDIO,
  133. },
  134. { NULL }
  135. };
  136. AVFilter avfilter_af_silencedetect = {
  137. .name = "silencedetect",
  138. .description = NULL_IF_CONFIG_SMALL("Detect silence."),
  139. .priv_size = sizeof(SilenceDetectContext),
  140. .query_formats = query_formats,
  141. .inputs = silencedetect_inputs,
  142. .outputs = silencedetect_outputs,
  143. .priv_class = &silencedetect_class,
  144. };