af_silencedetect.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. int mono; ///< mono mode : check each channel separately (default = check when ALL channels are silent)
  36. int channels; ///< number of channels
  37. int independent_channels; ///< number of entries in following arrays (always 1 in mono mode)
  38. int64_t *nb_null_samples; ///< (array) current number of continuous zero samples
  39. int64_t *start; ///< (array) if silence is detected, this value contains the time of the first zero sample (default/unset = INT64_MIN)
  40. int64_t frame_end; ///< pts of the end of the current frame (used to compute duration of silence at EOS)
  41. int last_sample_rate; ///< last sample rate to check for sample rate changes
  42. AVRational time_base; ///< time_base
  43. void (*silencedetect)(struct SilenceDetectContext *s, AVFrame *insamples,
  44. int nb_samples, int64_t nb_samples_notify,
  45. AVRational time_base);
  46. } SilenceDetectContext;
  47. #define OFFSET(x) offsetof(SilenceDetectContext, x)
  48. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
  49. static const AVOption silencedetect_options[] = {
  50. { "n", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
  51. { "noise", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
  52. { "d", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, FLAGS },
  53. { "duration", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, FLAGS },
  54. { "mono", "check each channel separately", OFFSET(mono), AV_OPT_TYPE_BOOL, {.i64=0.}, 0, 1, FLAGS },
  55. { "m", "check each channel separately", OFFSET(mono), AV_OPT_TYPE_BOOL, {.i64=0.}, 0, 1, FLAGS },
  56. { NULL }
  57. };
  58. AVFILTER_DEFINE_CLASS(silencedetect);
  59. static void set_meta(AVFrame *insamples, int channel, const char *key, char *value)
  60. {
  61. char key2[128];
  62. if (channel)
  63. snprintf(key2, sizeof(key2), "lavfi.%s.%d", key, channel);
  64. else
  65. snprintf(key2, sizeof(key2), "lavfi.%s", key);
  66. av_dict_set(&insamples->metadata, key2, value, 0);
  67. }
  68. static av_always_inline void update(SilenceDetectContext *s, AVFrame *insamples,
  69. int is_silence, int current_sample, int64_t nb_samples_notify,
  70. AVRational time_base)
  71. {
  72. int channel = current_sample % s->independent_channels;
  73. if (is_silence) {
  74. if (s->start[channel] == INT64_MIN) {
  75. s->nb_null_samples[channel]++;
  76. if (s->nb_null_samples[channel] >= nb_samples_notify) {
  77. s->start[channel] = insamples->pts + av_rescale_q(current_sample / s->channels + 1 - nb_samples_notify * s->independent_channels / s->channels,
  78. (AVRational){ 1, s->last_sample_rate }, time_base);
  79. set_meta(insamples, s->mono ? channel + 1 : 0, "silence_start",
  80. av_ts2timestr(s->start[channel], &time_base));
  81. if (s->mono)
  82. av_log(s, AV_LOG_INFO, "channel: %d | ", channel);
  83. av_log(s, AV_LOG_INFO, "silence_start: %s\n",
  84. av_ts2timestr(s->start[channel], &time_base));
  85. }
  86. }
  87. } else {
  88. if (s->start[channel] > INT64_MIN) {
  89. int64_t end_pts = insamples ? insamples->pts + av_rescale_q(current_sample / s->channels,
  90. (AVRational){ 1, s->last_sample_rate }, time_base)
  91. : s->frame_end;
  92. int64_t duration_ts = end_pts - s->start[channel];
  93. if (insamples) {
  94. set_meta(insamples, s->mono ? channel + 1 : 0, "silence_end",
  95. av_ts2timestr(end_pts, &time_base));
  96. set_meta(insamples, s->mono ? channel + 1 : 0, "silence_duration",
  97. av_ts2timestr(duration_ts, &time_base));
  98. }
  99. if (s->mono)
  100. av_log(s, AV_LOG_INFO, "channel: %d | ", channel);
  101. av_log(s, AV_LOG_INFO, "silence_end: %s | silence_duration: %s\n",
  102. av_ts2timestr(end_pts, &time_base),
  103. av_ts2timestr(duration_ts, &time_base));
  104. }
  105. s->nb_null_samples[channel] = 0;
  106. s->start[channel] = INT64_MIN;
  107. }
  108. }
  109. #define SILENCE_DETECT(name, type) \
  110. static void silencedetect_##name(SilenceDetectContext *s, AVFrame *insamples, \
  111. int nb_samples, int64_t nb_samples_notify, \
  112. AVRational time_base) \
  113. { \
  114. const type *p = (const type *)insamples->data[0]; \
  115. const type noise = s->noise; \
  116. int i; \
  117. \
  118. for (i = 0; i < nb_samples; i++, p++) \
  119. update(s, insamples, *p < noise && *p > -noise, i, \
  120. nb_samples_notify, time_base); \
  121. }
  122. SILENCE_DETECT(dbl, double)
  123. SILENCE_DETECT(flt, float)
  124. SILENCE_DETECT(s32, int32_t)
  125. SILENCE_DETECT(s16, int16_t)
  126. static int config_input(AVFilterLink *inlink)
  127. {
  128. AVFilterContext *ctx = inlink->dst;
  129. SilenceDetectContext *s = ctx->priv;
  130. int c;
  131. s->channels = inlink->channels;
  132. s->independent_channels = s->mono ? s->channels : 1;
  133. s->nb_null_samples = av_mallocz_array(sizeof(*s->nb_null_samples), s->independent_channels);
  134. if (!s->nb_null_samples)
  135. return AVERROR(ENOMEM);
  136. s->start = av_malloc_array(sizeof(*s->start), s->independent_channels);
  137. if (!s->start)
  138. return AVERROR(ENOMEM);
  139. for (c = 0; c < s->independent_channels; c++)
  140. s->start[c] = INT64_MIN;
  141. switch (inlink->format) {
  142. case AV_SAMPLE_FMT_DBL: s->silencedetect = silencedetect_dbl; break;
  143. case AV_SAMPLE_FMT_FLT: s->silencedetect = silencedetect_flt; break;
  144. case AV_SAMPLE_FMT_S32:
  145. s->noise *= INT32_MAX;
  146. s->silencedetect = silencedetect_s32;
  147. break;
  148. case AV_SAMPLE_FMT_S16:
  149. s->noise *= INT16_MAX;
  150. s->silencedetect = silencedetect_s16;
  151. break;
  152. }
  153. return 0;
  154. }
  155. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  156. {
  157. SilenceDetectContext *s = inlink->dst->priv;
  158. const int nb_channels = inlink->channels;
  159. const int srate = inlink->sample_rate;
  160. const int nb_samples = insamples->nb_samples * nb_channels;
  161. const int64_t nb_samples_notify = srate * s->duration * (s->mono ? 1 : nb_channels);
  162. int c;
  163. // scale number of null samples to the new sample rate
  164. if (s->last_sample_rate && s->last_sample_rate != srate)
  165. for (c = 0; c < s->independent_channels; c++) {
  166. s->nb_null_samples[c] = srate * s->nb_null_samples[c] / s->last_sample_rate;
  167. }
  168. s->last_sample_rate = srate;
  169. s->time_base = inlink->time_base;
  170. s->frame_end = insamples->pts + av_rescale_q(insamples->nb_samples,
  171. (AVRational){ 1, s->last_sample_rate }, inlink->time_base);
  172. // TODO: document metadata
  173. s->silencedetect(s, insamples, nb_samples, nb_samples_notify,
  174. inlink->time_base);
  175. return ff_filter_frame(inlink->dst->outputs[0], insamples);
  176. }
  177. static int query_formats(AVFilterContext *ctx)
  178. {
  179. AVFilterFormats *formats = NULL;
  180. AVFilterChannelLayouts *layouts = NULL;
  181. static const enum AVSampleFormat sample_fmts[] = {
  182. AV_SAMPLE_FMT_DBL,
  183. AV_SAMPLE_FMT_FLT,
  184. AV_SAMPLE_FMT_S32,
  185. AV_SAMPLE_FMT_S16,
  186. AV_SAMPLE_FMT_NONE
  187. };
  188. int ret;
  189. layouts = ff_all_channel_layouts();
  190. if (!layouts)
  191. return AVERROR(ENOMEM);
  192. ret = ff_set_common_channel_layouts(ctx, layouts);
  193. if (ret < 0)
  194. return ret;
  195. formats = ff_make_format_list(sample_fmts);
  196. if (!formats)
  197. return AVERROR(ENOMEM);
  198. ret = ff_set_common_formats(ctx, formats);
  199. if (ret < 0)
  200. return ret;
  201. formats = ff_all_samplerates();
  202. if (!formats)
  203. return AVERROR(ENOMEM);
  204. return ff_set_common_samplerates(ctx, formats);
  205. }
  206. static av_cold void uninit(AVFilterContext *ctx)
  207. {
  208. SilenceDetectContext *s = ctx->priv;
  209. int c;
  210. for (c = 0; c < s->independent_channels; c++)
  211. if (s->start[c] > INT64_MIN)
  212. update(s, NULL, 0, c, 0, s->time_base);
  213. av_freep(&s->nb_null_samples);
  214. av_freep(&s->start);
  215. }
  216. static const AVFilterPad silencedetect_inputs[] = {
  217. {
  218. .name = "default",
  219. .type = AVMEDIA_TYPE_AUDIO,
  220. .config_props = config_input,
  221. .filter_frame = filter_frame,
  222. },
  223. { NULL }
  224. };
  225. static const AVFilterPad silencedetect_outputs[] = {
  226. {
  227. .name = "default",
  228. .type = AVMEDIA_TYPE_AUDIO,
  229. },
  230. { NULL }
  231. };
  232. AVFilter ff_af_silencedetect = {
  233. .name = "silencedetect",
  234. .description = NULL_IF_CONFIG_SMALL("Detect silence."),
  235. .priv_size = sizeof(SilenceDetectContext),
  236. .query_formats = query_formats,
  237. .uninit = uninit,
  238. .inputs = silencedetect_inputs,
  239. .outputs = silencedetect_outputs,
  240. .priv_class = &silencedetect_class,
  241. };