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.

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