FFmpeg  4.4
af_silencedetect.c
Go to the documentation of this file.
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 /**
22  * @file
23  * Audio silence detector
24  */
25 
26 #include <float.h> /* DBL_MAX */
27 
28 #include "libavutil/opt.h"
29 #include "libavutil/timestamp.h"
30 #include "audio.h"
31 #include "formats.h"
32 #include "avfilter.h"
33 #include "internal.h"
34 
35 typedef struct SilenceDetectContext {
36  const AVClass *class;
37  double noise; ///< noise amplitude ratio
38  int64_t duration; ///< minimum duration of silence until notification
39  int mono; ///< mono mode : check each channel separately (default = check when ALL channels are silent)
40  int channels; ///< number of channels
41  int independent_channels; ///< number of entries in following arrays (always 1 in mono mode)
42  int64_t *nb_null_samples; ///< (array) current number of continuous zero samples
43  int64_t *start; ///< (array) if silence is detected, this value contains the time of the first zero sample (default/unset = INT64_MIN)
44  int64_t frame_end; ///< pts of the end of the current frame (used to compute duration of silence at EOS)
45  int last_sample_rate; ///< last sample rate to check for sample rate changes
46  AVRational time_base; ///< time_base
47 
49  int nb_samples, int64_t nb_samples_notify,
52 
53 #define MAX_DURATION (24*3600*1000000LL)
54 #define OFFSET(x) offsetof(SilenceDetectContext, x)
55 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
56 static const AVOption silencedetect_options[] = {
57  { "n", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
58  { "noise", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
59  { "d", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=2000000}, 0, MAX_DURATION,FLAGS },
60  { "duration", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=2000000}, 0, MAX_DURATION,FLAGS },
61  { "mono", "check each channel separately", OFFSET(mono), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
62  { "m", "check each channel separately", OFFSET(mono), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
63  { NULL }
64 };
65 
67 
68 static void set_meta(AVFrame *insamples, int channel, const char *key, char *value)
69 {
70  char key2[128];
71 
72  if (channel)
73  snprintf(key2, sizeof(key2), "lavfi.%s.%d", key, channel);
74  else
75  snprintf(key2, sizeof(key2), "lavfi.%s", key);
76  av_dict_set(&insamples->metadata, key2, value, 0);
77 }
79  int is_silence, int current_sample, int64_t nb_samples_notify,
81 {
82  int channel = current_sample % s->independent_channels;
83  if (is_silence) {
84  if (s->start[channel] == INT64_MIN) {
85  s->nb_null_samples[channel]++;
86  if (s->nb_null_samples[channel] >= nb_samples_notify) {
87  s->start[channel] = insamples->pts + av_rescale_q(current_sample / s->channels + 1 - nb_samples_notify * s->independent_channels / s->channels,
88  (AVRational){ 1, s->last_sample_rate }, time_base);
89  set_meta(insamples, s->mono ? channel + 1 : 0, "silence_start",
90  av_ts2timestr(s->start[channel], &time_base));
91  if (s->mono)
92  av_log(s, AV_LOG_INFO, "channel: %d | ", channel);
93  av_log(s, AV_LOG_INFO, "silence_start: %s\n",
94  av_ts2timestr(s->start[channel], &time_base));
95  }
96  }
97  } else {
98  if (s->start[channel] > INT64_MIN) {
99  int64_t end_pts = insamples ? insamples->pts + av_rescale_q(current_sample / s->channels,
100  (AVRational){ 1, s->last_sample_rate }, time_base)
101  : s->frame_end;
102  int64_t duration_ts = end_pts - s->start[channel];
103  if (insamples) {
104  set_meta(insamples, s->mono ? channel + 1 : 0, "silence_end",
105  av_ts2timestr(end_pts, &time_base));
106  set_meta(insamples, s->mono ? channel + 1 : 0, "silence_duration",
107  av_ts2timestr(duration_ts, &time_base));
108  }
109  if (s->mono)
110  av_log(s, AV_LOG_INFO, "channel: %d | ", channel);
111  av_log(s, AV_LOG_INFO, "silence_end: %s | silence_duration: %s\n",
112  av_ts2timestr(end_pts, &time_base),
113  av_ts2timestr(duration_ts, &time_base));
114  }
115  s->nb_null_samples[channel] = 0;
116  s->start[channel] = INT64_MIN;
117  }
118 }
119 
120 #define SILENCE_DETECT(name, type) \
121 static void silencedetect_##name(SilenceDetectContext *s, AVFrame *insamples, \
122  int nb_samples, int64_t nb_samples_notify, \
123  AVRational time_base) \
124 { \
125  const type *p = (const type *)insamples->data[0]; \
126  const type noise = s->noise; \
127  int i; \
128  \
129  for (i = 0; i < nb_samples; i++, p++) \
130  update(s, insamples, *p < noise && *p > -noise, i, \
131  nb_samples_notify, time_base); \
132 }
133 
134 SILENCE_DETECT(dbl, double)
135 SILENCE_DETECT(flt, float)
137 SILENCE_DETECT(s16, int16_t)
138 
139 static int config_input(AVFilterLink *inlink)
140 {
141  AVFilterContext *ctx = inlink->dst;
142  SilenceDetectContext *s = ctx->priv;
143  int c;
144 
145  s->channels = inlink->channels;
146  s->duration = av_rescale(s->duration, inlink->sample_rate, AV_TIME_BASE);
147  s->independent_channels = s->mono ? s->channels : 1;
148  s->nb_null_samples = av_mallocz_array(sizeof(*s->nb_null_samples), s->independent_channels);
149  if (!s->nb_null_samples)
150  return AVERROR(ENOMEM);
151  s->start = av_malloc_array(sizeof(*s->start), s->independent_channels);
152  if (!s->start)
153  return AVERROR(ENOMEM);
154  for (c = 0; c < s->independent_channels; c++)
155  s->start[c] = INT64_MIN;
156 
157  switch (inlink->format) {
158  case AV_SAMPLE_FMT_DBL: s->silencedetect = silencedetect_dbl; break;
159  case AV_SAMPLE_FMT_FLT: s->silencedetect = silencedetect_flt; break;
160  case AV_SAMPLE_FMT_S32:
161  s->noise *= INT32_MAX;
162  s->silencedetect = silencedetect_s32;
163  break;
164  case AV_SAMPLE_FMT_S16:
165  s->noise *= INT16_MAX;
166  s->silencedetect = silencedetect_s16;
167  break;
168  }
169 
170  return 0;
171 }
172 
173 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
174 {
175  SilenceDetectContext *s = inlink->dst->priv;
176  const int nb_channels = inlink->channels;
177  const int srate = inlink->sample_rate;
178  const int nb_samples = insamples->nb_samples * nb_channels;
179  const int64_t nb_samples_notify = s->duration * (s->mono ? 1 : nb_channels);
180  int c;
181 
182  // scale number of null samples to the new sample rate
183  if (s->last_sample_rate && s->last_sample_rate != srate)
184  for (c = 0; c < s->independent_channels; c++) {
185  s->nb_null_samples[c] = srate * s->nb_null_samples[c] / s->last_sample_rate;
186  }
187  s->last_sample_rate = srate;
188  s->time_base = inlink->time_base;
189  s->frame_end = insamples->pts + av_rescale_q(insamples->nb_samples,
190  (AVRational){ 1, s->last_sample_rate }, inlink->time_base);
191 
192  s->silencedetect(s, insamples, nb_samples, nb_samples_notify,
193  inlink->time_base);
194 
195  return ff_filter_frame(inlink->dst->outputs[0], insamples);
196 }
197 
199 {
202  static const enum AVSampleFormat sample_fmts[] = {
208  };
209  int ret;
210 
212  if (!layouts)
213  return AVERROR(ENOMEM);
215  if (ret < 0)
216  return ret;
217 
219  if (!formats)
220  return AVERROR(ENOMEM);
222  if (ret < 0)
223  return ret;
224 
226  if (!formats)
227  return AVERROR(ENOMEM);
229 }
230 
232 {
233  SilenceDetectContext *s = ctx->priv;
234  int c;
235 
236  for (c = 0; c < s->independent_channels; c++)
237  if (s->start[c] > INT64_MIN)
238  update(s, NULL, 0, c, 0, s->time_base);
239  av_freep(&s->nb_null_samples);
240  av_freep(&s->start);
241 }
242 
244  {
245  .name = "default",
246  .type = AVMEDIA_TYPE_AUDIO,
247  .config_props = config_input,
248  .filter_frame = filter_frame,
249  },
250  { NULL }
251 };
252 
254  {
255  .name = "default",
256  .type = AVMEDIA_TYPE_AUDIO,
257  },
258  { NULL }
259 };
260 
262  .name = "silencedetect",
263  .description = NULL_IF_CONFIG_SMALL("Detect silence."),
264  .priv_size = sizeof(SilenceDetectContext),
266  .uninit = uninit,
269  .priv_class = &silencedetect_class,
270 };
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:925
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
AVFILTER_DEFINE_CLASS(silencedetect)
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
static int query_formats(AVFilterContext *ctx)
static void set_meta(AVFrame *insamples, int channel, const char *key, char *value)
static int config_input(AVFilterLink *inlink)
static const AVFilterPad silencedetect_outputs[]
#define FLAGS
#define MAX_DURATION
static av_cold void uninit(AVFilterContext *ctx)
static const AVOption silencedetect_options[]
#define OFFSET(x)
#define SILENCE_DETECT(name, type)
static const AVFilterPad silencedetect_inputs[]
static av_always_inline void update(SilenceDetectContext *s, AVFrame *insamples, int is_silence, int current_sample, int64_t nb_samples_notify, AVRational time_base)
AVFilter ff_af_silencedetect
#define av_always_inline
Definition: attributes.h:45
#define av_cold
Definition: attributes.h:88
int32_t
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1094
Main libavfilter public API header.
#define s(width, name)
Definition: cbs_vp9.c:257
int nb_channels
#define NULL
Definition: coverity.c:32
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
double value
Definition: eval.c:98
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout (w...
Definition: formats.c:427
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:575
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *channel_layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates.
Definition: formats.c:568
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:421
@ AV_OPT_TYPE_DURATION
Definition: opt.h:239
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:227
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:190
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:63
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:62
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:64
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
const char * key
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
static int noise(AVBSFContext *ctx, AVPacket *pkt)
Definition: noise_bsf.c:36
AVOptions.
typedef void(RENAME(mix_any_func_type))
formats
Definition: signature.h:48
#define snprintf
Definition: snprintf.h:34
Describe the class of an AVClass context structure.
Definition: log.h:67
A list of supported channel layouts.
Definition: formats.h:86
An instance of a filter.
Definition: avfilter.h:341
void * priv
private data for use by the filter
Definition: avfilter.h:356
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:353
A list of supported formats for one end of a filter link.
Definition: formats.h:65
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:384
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:411
AVDictionary * metadata
metadata.
Definition: frame.h:604
AVOption.
Definition: opt.h:248
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int64_t duration
minimum duration of silence until notification
AVRational time_base
time_base
int channels
number of channels
int64_t frame_end
pts of the end of the current frame (used to compute duration of silence at EOS)
int64_t * start
(array) if silence is detected, this value contains the time of the first zero sample (default/unset ...
int mono
mono mode : check each channel separately (default = check when ALL channels are silent)
void(* silencedetect)(struct SilenceDetectContext *s, AVFrame *insamples, int nb_samples, int64_t nb_samples_notify, AVRational time_base)
int last_sample_rate
last sample rate to check for sample rate changes
int64_t * nb_null_samples
(array) current number of continuous zero samples
double noise
noise amplitude ratio
int independent_channels
number of entries in following arrays (always 1 in mono mode)
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
int64_t duration
Definition: movenc.c:64
AVFormatContext * ctx
Definition: movenc.c:48
timestamp utils, mostly useful for debugging/logging purposes
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:76
static double c[64]