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.

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