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.

269 lines
11KB

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