af_silencedetect.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2012 Clément Bœsch <u pkh me>
  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/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 SilenceDetectContext {
  32. const AVClass *class;
  33. double noise; ///< noise amplitude ratio
  34. double duration; ///< minimum duration of silence until notification
  35. int64_t nb_null_samples; ///< current number of continuous zero samples
  36. int64_t start; ///< if silence is detected, this value contains the time of the first zero sample
  37. int last_sample_rate; ///< last sample rate to check for sample rate changes
  38. void (*silencedetect)(struct SilenceDetectContext *s, AVFrame *insamples,
  39. int nb_samples, int64_t nb_samples_notify,
  40. AVRational time_base);
  41. } SilenceDetectContext;
  42. #define OFFSET(x) offsetof(SilenceDetectContext, x)
  43. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  44. static const AVOption silencedetect_options[] = {
  45. { "n", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
  46. { "noise", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
  47. { "d", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, FLAGS },
  48. { "duration", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, FLAGS },
  49. { NULL }
  50. };
  51. AVFILTER_DEFINE_CLASS(silencedetect);
  52. static char *get_metadata_val(AVFrame *insamples, const char *key)
  53. {
  54. AVDictionaryEntry *e = av_dict_get(insamples->metadata, key, NULL, 0);
  55. return e && e->value ? e->value : NULL;
  56. }
  57. static av_always_inline void update(SilenceDetectContext *s, AVFrame *insamples,
  58. int is_silence, int64_t nb_samples_notify,
  59. AVRational time_base)
  60. {
  61. if (is_silence) {
  62. if (!s->start) {
  63. s->nb_null_samples++;
  64. if (s->nb_null_samples >= nb_samples_notify) {
  65. s->start = insamples->pts - (int64_t)(s->duration / av_q2d(time_base) + .5);
  66. av_dict_set(&insamples->metadata, "lavfi.silence_start",
  67. av_ts2timestr(s->start, &time_base), 0);
  68. av_log(s, AV_LOG_INFO, "silence_start: %s\n",
  69. get_metadata_val(insamples, "lavfi.silence_start"));
  70. }
  71. }
  72. } else {
  73. if (s->start) {
  74. av_dict_set(&insamples->metadata, "lavfi.silence_end",
  75. av_ts2timestr(insamples->pts, &time_base), 0);
  76. av_dict_set(&insamples->metadata, "lavfi.silence_duration",
  77. av_ts2timestr(insamples->pts - s->start, &time_base), 0);
  78. av_log(s, AV_LOG_INFO,
  79. "silence_end: %s | silence_duration: %s\n",
  80. get_metadata_val(insamples, "lavfi.silence_end"),
  81. get_metadata_val(insamples, "lavfi.silence_duration"));
  82. }
  83. s->nb_null_samples = s->start = 0;
  84. }
  85. }
  86. #define SILENCE_DETECT(name, type) \
  87. static void silencedetect_##name(SilenceDetectContext *s, AVFrame *insamples, \
  88. int nb_samples, int64_t nb_samples_notify, \
  89. AVRational time_base) \
  90. { \
  91. const type *p = (const type *)insamples->data[0]; \
  92. const type noise = s->noise; \
  93. int i; \
  94. \
  95. for (i = 0; i < nb_samples; i++, p++) \
  96. update(s, insamples, *p < noise && *p > -noise, \
  97. nb_samples_notify, time_base); \
  98. }
  99. SILENCE_DETECT(dbl, double)
  100. SILENCE_DETECT(flt, float)
  101. SILENCE_DETECT(s32, int32_t)
  102. SILENCE_DETECT(s16, int16_t)
  103. static int config_input(AVFilterLink *inlink)
  104. {
  105. AVFilterContext *ctx = inlink->dst;
  106. SilenceDetectContext *s = ctx->priv;
  107. switch (inlink->format) {
  108. case AV_SAMPLE_FMT_DBL: s->silencedetect = silencedetect_dbl; break;
  109. case AV_SAMPLE_FMT_FLT: s->silencedetect = silencedetect_flt; break;
  110. case AV_SAMPLE_FMT_S32:
  111. s->noise *= INT32_MAX;
  112. s->silencedetect = silencedetect_s32;
  113. break;
  114. case AV_SAMPLE_FMT_S16:
  115. s->noise *= INT16_MAX;
  116. s->silencedetect = silencedetect_s16;
  117. break;
  118. }
  119. return 0;
  120. }
  121. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  122. {
  123. SilenceDetectContext *s = inlink->dst->priv;
  124. const int nb_channels = inlink->channels;
  125. const int srate = inlink->sample_rate;
  126. const int nb_samples = insamples->nb_samples * nb_channels;
  127. const int64_t nb_samples_notify = srate * s->duration * nb_channels;
  128. // scale number of null samples to the new sample rate
  129. if (s->last_sample_rate && s->last_sample_rate != srate)
  130. s->nb_null_samples = srate * s->nb_null_samples / s->last_sample_rate;
  131. s->last_sample_rate = srate;
  132. // TODO: document metadata
  133. s->silencedetect(s, insamples, nb_samples, nb_samples_notify,
  134. inlink->time_base);
  135. return ff_filter_frame(inlink->dst->outputs[0], insamples);
  136. }
  137. static int query_formats(AVFilterContext *ctx)
  138. {
  139. AVFilterFormats *formats = NULL;
  140. AVFilterChannelLayouts *layouts = NULL;
  141. static const enum AVSampleFormat sample_fmts[] = {
  142. AV_SAMPLE_FMT_DBL,
  143. AV_SAMPLE_FMT_FLT,
  144. AV_SAMPLE_FMT_S32,
  145. AV_SAMPLE_FMT_S16,
  146. AV_SAMPLE_FMT_NONE
  147. };
  148. layouts = ff_all_channel_layouts();
  149. if (!layouts)
  150. return AVERROR(ENOMEM);
  151. ff_set_common_channel_layouts(ctx, layouts);
  152. formats = ff_make_format_list(sample_fmts);
  153. if (!formats)
  154. return AVERROR(ENOMEM);
  155. ff_set_common_formats(ctx, formats);
  156. formats = ff_all_samplerates();
  157. if (!formats)
  158. return AVERROR(ENOMEM);
  159. ff_set_common_samplerates(ctx, formats);
  160. return 0;
  161. }
  162. static const AVFilterPad silencedetect_inputs[] = {
  163. {
  164. .name = "default",
  165. .type = AVMEDIA_TYPE_AUDIO,
  166. .config_props = config_input,
  167. .filter_frame = filter_frame,
  168. },
  169. { NULL }
  170. };
  171. static const AVFilterPad silencedetect_outputs[] = {
  172. {
  173. .name = "default",
  174. .type = AVMEDIA_TYPE_AUDIO,
  175. },
  176. { NULL }
  177. };
  178. AVFilter ff_af_silencedetect = {
  179. .name = "silencedetect",
  180. .description = NULL_IF_CONFIG_SMALL("Detect silence."),
  181. .priv_size = sizeof(SilenceDetectContext),
  182. .query_formats = query_formats,
  183. .inputs = silencedetect_inputs,
  184. .outputs = silencedetect_outputs,
  185. .priv_class = &silencedetect_class,
  186. };