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.

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