af_silencedetect.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "libavutil/audioconvert.h"
  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. char *noise_str; ///< noise option string
  34. double noise; ///< noise amplitude ratio
  35. int 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_str), AV_OPT_TYPE_STRING, {.str="-60dB"}, CHAR_MIN, CHAR_MAX, FLAGS },
  44. { "noise", "set noise tolerance", OFFSET(noise_str), AV_OPT_TYPE_STRING, {.str="-60dB"}, CHAR_MIN, CHAR_MAX, FLAGS },
  45. { "d", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, FLAGS },
  46. { "duration", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, FLAGS },
  47. { NULL },
  48. };
  49. AVFILTER_DEFINE_CLASS(silencedetect);
  50. static av_cold int init(AVFilterContext *ctx, const char *args)
  51. {
  52. int ret;
  53. char *tail;
  54. SilenceDetectContext *silence = ctx->priv;
  55. silence->class = &silencedetect_class;
  56. av_opt_set_defaults(silence);
  57. if ((ret = av_set_options_string(silence, args, "=", ":")) < 0)
  58. return ret;
  59. silence->noise = strtod(silence->noise_str, &tail);
  60. if (!strcmp(tail, "dB")) {
  61. silence->noise = pow(10, silence->noise/20);
  62. } else if (*tail) {
  63. av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for noise parameter.\n",
  64. silence->noise_str);
  65. return AVERROR(EINVAL);
  66. }
  67. return 0;
  68. }
  69. static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  70. {
  71. int i;
  72. SilenceDetectContext *silence = inlink->dst->priv;
  73. const int nb_channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
  74. const int srate = inlink->sample_rate;
  75. const int nb_samples = insamples->audio->nb_samples * nb_channels;
  76. const int64_t nb_samples_notify = srate * silence->duration * nb_channels;
  77. // scale number of null samples to the new sample rate
  78. if (silence->last_sample_rate && silence->last_sample_rate != srate)
  79. silence->nb_null_samples =
  80. srate * silence->nb_null_samples / silence->last_sample_rate;
  81. silence->last_sample_rate = srate;
  82. // TODO: support more sample formats
  83. if (insamples->format == AV_SAMPLE_FMT_DBL) {
  84. double *p = (double *)insamples->data[0];
  85. for (i = 0; i < nb_samples; i++, p++) {
  86. if (*p < silence->noise && *p > -silence->noise) {
  87. if (!silence->start) {
  88. silence->nb_null_samples++;
  89. if (silence->nb_null_samples >= nb_samples_notify) {
  90. silence->start = insamples->pts - silence->duration / av_q2d(inlink->time_base);
  91. av_log(silence, AV_LOG_INFO,
  92. "silence_start: %s\n", av_ts2timestr(silence->start, &inlink->time_base));
  93. }
  94. }
  95. } else {
  96. if (silence->start)
  97. av_log(silence, AV_LOG_INFO,
  98. "silence_end: %s | silence_duration: %s\n",
  99. av_ts2timestr(insamples->pts, &inlink->time_base),
  100. av_ts2timestr(insamples->pts - silence->start, &inlink->time_base));
  101. silence->nb_null_samples = silence->start = 0;
  102. }
  103. }
  104. }
  105. return ff_filter_samples(inlink->dst->outputs[0], insamples);
  106. }
  107. static int query_formats(AVFilterContext *ctx)
  108. {
  109. AVFilterFormats *formats = NULL;
  110. AVFilterChannelLayouts *layouts = NULL;
  111. enum AVSampleFormat sample_fmts[] = {
  112. AV_SAMPLE_FMT_DBL,
  113. AV_SAMPLE_FMT_NONE
  114. };
  115. layouts = ff_all_channel_layouts();
  116. if (!layouts)
  117. return AVERROR(ENOMEM);
  118. ff_set_common_channel_layouts(ctx, layouts);
  119. formats = ff_make_format_list(sample_fmts);
  120. if (!formats)
  121. return AVERROR(ENOMEM);
  122. ff_set_common_formats(ctx, formats);
  123. formats = ff_all_samplerates();
  124. if (!formats)
  125. return AVERROR(ENOMEM);
  126. ff_set_common_samplerates(ctx, formats);
  127. return 0;
  128. }
  129. AVFilter avfilter_af_silencedetect = {
  130. .name = "silencedetect",
  131. .description = NULL_IF_CONFIG_SMALL("Detect silence."),
  132. .priv_size = sizeof(SilenceDetectContext),
  133. .init = init,
  134. .query_formats = query_formats,
  135. .inputs = (const AVFilterPad[]) {
  136. { .name = "default",
  137. .type = AVMEDIA_TYPE_AUDIO,
  138. .get_audio_buffer = ff_null_get_audio_buffer,
  139. .filter_samples = filter_samples, },
  140. { .name = NULL }
  141. },
  142. .outputs = (const AVFilterPad[]) {
  143. { .name = "default",
  144. .type = AVMEDIA_TYPE_AUDIO, },
  145. { .name = NULL }
  146. },
  147. .priv_class = &silencedetect_class,
  148. };