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.

186 lines
7.0KB

  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. double 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_DOUBLE, {.dbl=2.}, 0, 24*60*60, FLAGS },
  46. { "duration", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, 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. av_opt_free(silence);
  68. return 0;
  69. }
  70. static char *get_metadata_val(AVFilterBufferRef *insamples, const char *key)
  71. {
  72. AVDictionaryEntry *e = av_dict_get(insamples->metadata, key, NULL, 0);
  73. return e && e->value ? e->value : NULL;
  74. }
  75. static int 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. // TODO: document metadata
  90. if (insamples->format == AV_SAMPLE_FMT_DBL) {
  91. double *p = (double *)insamples->data[0];
  92. for (i = 0; i < nb_samples; i++, p++) {
  93. if (*p < silence->noise && *p > -silence->noise) {
  94. if (!silence->start) {
  95. silence->nb_null_samples++;
  96. if (silence->nb_null_samples >= nb_samples_notify) {
  97. silence->start = insamples->pts - (int64_t)(silence->duration / av_q2d(inlink->time_base) + .5);
  98. av_dict_set(&insamples->metadata, "lavfi.silence_start",
  99. av_ts2timestr(silence->start, &inlink->time_base), 0);
  100. av_log(silence, AV_LOG_INFO, "silence_start: %s\n",
  101. get_metadata_val(insamples, "lavfi.silence_start"));
  102. }
  103. }
  104. } else {
  105. if (silence->start) {
  106. av_dict_set(&insamples->metadata, "lavfi.silence_end",
  107. av_ts2timestr(insamples->pts, &inlink->time_base), 0);
  108. av_dict_set(&insamples->metadata, "lavfi.silence_duration",
  109. av_ts2timestr(insamples->pts - silence->start, &inlink->time_base), 0);
  110. av_log(silence, AV_LOG_INFO,
  111. "silence_end: %s | silence_duration: %s\n",
  112. get_metadata_val(insamples, "lavfi.silence_end"),
  113. get_metadata_val(insamples, "lavfi.silence_duration"));
  114. }
  115. silence->nb_null_samples = silence->start = 0;
  116. }
  117. }
  118. }
  119. return ff_filter_samples(inlink->dst->outputs[0], insamples);
  120. }
  121. static int query_formats(AVFilterContext *ctx)
  122. {
  123. AVFilterFormats *formats = NULL;
  124. AVFilterChannelLayouts *layouts = NULL;
  125. enum AVSampleFormat sample_fmts[] = {
  126. AV_SAMPLE_FMT_DBL,
  127. AV_SAMPLE_FMT_NONE
  128. };
  129. layouts = ff_all_channel_layouts();
  130. if (!layouts)
  131. return AVERROR(ENOMEM);
  132. ff_set_common_channel_layouts(ctx, layouts);
  133. formats = ff_make_format_list(sample_fmts);
  134. if (!formats)
  135. return AVERROR(ENOMEM);
  136. ff_set_common_formats(ctx, formats);
  137. formats = ff_all_samplerates();
  138. if (!formats)
  139. return AVERROR(ENOMEM);
  140. ff_set_common_samplerates(ctx, formats);
  141. return 0;
  142. }
  143. AVFilter avfilter_af_silencedetect = {
  144. .name = "silencedetect",
  145. .description = NULL_IF_CONFIG_SMALL("Detect silence."),
  146. .priv_size = sizeof(SilenceDetectContext),
  147. .init = init,
  148. .query_formats = query_formats,
  149. .inputs = (const AVFilterPad[]) {
  150. { .name = "default",
  151. .type = AVMEDIA_TYPE_AUDIO,
  152. .get_audio_buffer = ff_null_get_audio_buffer,
  153. .filter_samples = filter_samples, },
  154. { .name = NULL }
  155. },
  156. .outputs = (const AVFilterPad[]) {
  157. { .name = "default",
  158. .type = AVMEDIA_TYPE_AUDIO, },
  159. { .name = NULL }
  160. },
  161. .priv_class = &silencedetect_class,
  162. };