af_silencedetect.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <float.h> /* DBL_MAX */
  25. #include "libavutil/channel_layout.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/timestamp.h"
  28. #include "audio.h"
  29. #include "formats.h"
  30. #include "avfilter.h"
  31. #include "internal.h"
  32. typedef struct {
  33. const AVClass *class;
  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), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
  44. { "noise", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_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. SilenceDetectContext *silence = ctx->priv;
  54. silence->class = &silencedetect_class;
  55. av_opt_set_defaults(silence);
  56. if ((ret = av_set_options_string(silence, args, "=", ":")) < 0)
  57. return ret;
  58. av_opt_free(silence);
  59. return 0;
  60. }
  61. static char *get_metadata_val(AVFilterBufferRef *insamples, const char *key)
  62. {
  63. AVDictionaryEntry *e = av_dict_get(insamples->metadata, key, NULL, 0);
  64. return e && e->value ? e->value : NULL;
  65. }
  66. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  67. {
  68. int i;
  69. SilenceDetectContext *silence = inlink->dst->priv;
  70. const int nb_channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
  71. const int srate = inlink->sample_rate;
  72. const int nb_samples = insamples->audio->nb_samples * nb_channels;
  73. const int64_t nb_samples_notify = srate * silence->duration * nb_channels;
  74. // scale number of null samples to the new sample rate
  75. if (silence->last_sample_rate && silence->last_sample_rate != srate)
  76. silence->nb_null_samples =
  77. srate * silence->nb_null_samples / silence->last_sample_rate;
  78. silence->last_sample_rate = srate;
  79. // TODO: support more sample formats
  80. // TODO: document metadata
  81. if (insamples->format == AV_SAMPLE_FMT_DBL) {
  82. double *p = (double *)insamples->data[0];
  83. for (i = 0; i < nb_samples; i++, p++) {
  84. if (*p < silence->noise && *p > -silence->noise) {
  85. if (!silence->start) {
  86. silence->nb_null_samples++;
  87. if (silence->nb_null_samples >= nb_samples_notify) {
  88. silence->start = insamples->pts - (int64_t)(silence->duration / av_q2d(inlink->time_base) + .5);
  89. av_dict_set(&insamples->metadata, "lavfi.silence_start",
  90. av_ts2timestr(silence->start, &inlink->time_base), 0);
  91. av_log(silence, AV_LOG_INFO, "silence_start: %s\n",
  92. get_metadata_val(insamples, "lavfi.silence_start"));
  93. }
  94. }
  95. } else {
  96. if (silence->start) {
  97. av_dict_set(&insamples->metadata, "lavfi.silence_end",
  98. av_ts2timestr(insamples->pts, &inlink->time_base), 0);
  99. av_dict_set(&insamples->metadata, "lavfi.silence_duration",
  100. av_ts2timestr(insamples->pts - silence->start, &inlink->time_base), 0);
  101. av_log(silence, AV_LOG_INFO,
  102. "silence_end: %s | silence_duration: %s\n",
  103. get_metadata_val(insamples, "lavfi.silence_end"),
  104. get_metadata_val(insamples, "lavfi.silence_duration"));
  105. }
  106. silence->nb_null_samples = silence->start = 0;
  107. }
  108. }
  109. }
  110. return ff_filter_frame(inlink->dst->outputs[0], insamples);
  111. }
  112. static int query_formats(AVFilterContext *ctx)
  113. {
  114. AVFilterFormats *formats = NULL;
  115. AVFilterChannelLayouts *layouts = NULL;
  116. static const enum AVSampleFormat sample_fmts[] = {
  117. AV_SAMPLE_FMT_DBL,
  118. AV_SAMPLE_FMT_NONE
  119. };
  120. layouts = ff_all_channel_layouts();
  121. if (!layouts)
  122. return AVERROR(ENOMEM);
  123. ff_set_common_channel_layouts(ctx, layouts);
  124. formats = ff_make_format_list(sample_fmts);
  125. if (!formats)
  126. return AVERROR(ENOMEM);
  127. ff_set_common_formats(ctx, formats);
  128. formats = ff_all_samplerates();
  129. if (!formats)
  130. return AVERROR(ENOMEM);
  131. ff_set_common_samplerates(ctx, formats);
  132. return 0;
  133. }
  134. static const AVFilterPad silencedetect_inputs[] = {
  135. {
  136. .name = "default",
  137. .type = AVMEDIA_TYPE_AUDIO,
  138. .get_audio_buffer = ff_null_get_audio_buffer,
  139. .filter_frame = filter_frame,
  140. },
  141. { NULL }
  142. };
  143. static const AVFilterPad silencedetect_outputs[] = {
  144. {
  145. .name = "default",
  146. .type = AVMEDIA_TYPE_AUDIO,
  147. },
  148. { NULL }
  149. };
  150. AVFilter avfilter_af_silencedetect = {
  151. .name = "silencedetect",
  152. .description = NULL_IF_CONFIG_SMALL("Detect silence."),
  153. .priv_size = sizeof(SilenceDetectContext),
  154. .init = init,
  155. .query_formats = query_formats,
  156. .inputs = silencedetect_inputs,
  157. .outputs = silencedetect_outputs,
  158. .priv_class = &silencedetect_class,
  159. };