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.

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