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.

244 lines
9.5KB

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