FFmpeg
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 
48  void (*silencedetect)(struct SilenceDetectContext *s, AVFrame *insamples,
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 #define SILENCE_DETECT_PLANAR(name, type) \
135 static void silencedetect_##name(SilenceDetectContext *s, AVFrame *insamples, \
136  int nb_samples, int64_t nb_samples_notify, \
137  AVRational time_base) \
138 { \
139  const int channels = insamples->channels; \
140  const type noise = s->noise; \
141  \
142  nb_samples /= channels; \
143  for (int i = 0; i < nb_samples; i++) { \
144  for (int ch = 0; ch < insamples->channels; ch++) { \
145  const type *p = (const type *)insamples->extended_data[ch]; \
146  update(s, insamples, p[i] < noise && p[i] > -noise, \
147  channels * i + ch, \
148  nb_samples_notify, time_base); \
149  } \
150  } \
151 }
152 
153 SILENCE_DETECT(dbl, double)
154 SILENCE_DETECT(flt, float)
156 SILENCE_DETECT(s16, int16_t)
157 
158 SILENCE_DETECT_PLANAR(dblp, double)
159 SILENCE_DETECT_PLANAR(fltp, float)
161 SILENCE_DETECT_PLANAR(s16p, int16_t)
162 
164 {
165  AVFilterContext *ctx = inlink->dst;
166  SilenceDetectContext *s = ctx->priv;
167  int c;
168 
169  s->channels = inlink->channels;
170  s->duration = av_rescale(s->duration, inlink->sample_rate, AV_TIME_BASE);
171  s->independent_channels = s->mono ? s->channels : 1;
172  s->nb_null_samples = av_mallocz_array(sizeof(*s->nb_null_samples), s->independent_channels);
173  if (!s->nb_null_samples)
174  return AVERROR(ENOMEM);
175  s->start = av_malloc_array(sizeof(*s->start), s->independent_channels);
176  if (!s->start)
177  return AVERROR(ENOMEM);
178  for (c = 0; c < s->independent_channels; c++)
179  s->start[c] = INT64_MIN;
180 
181  switch (inlink->format) {
182  case AV_SAMPLE_FMT_DBL: s->silencedetect = silencedetect_dbl; break;
183  case AV_SAMPLE_FMT_FLT: s->silencedetect = silencedetect_flt; break;
184  case AV_SAMPLE_FMT_S32:
185  s->noise *= INT32_MAX;
186  s->silencedetect = silencedetect_s32;
187  break;
188  case AV_SAMPLE_FMT_S16:
189  s->noise *= INT16_MAX;
190  s->silencedetect = silencedetect_s16;
191  break;
192  case AV_SAMPLE_FMT_DBLP: s->silencedetect = silencedetect_dblp; break;
193  case AV_SAMPLE_FMT_FLTP: s->silencedetect = silencedetect_fltp; break;
194  case AV_SAMPLE_FMT_S32P:
195  s->noise *= INT32_MAX;
196  s->silencedetect = silencedetect_s32p;
197  break;
198  case AV_SAMPLE_FMT_S16P:
199  s->noise *= INT16_MAX;
200  s->silencedetect = silencedetect_s16p;
201  break;
202  default:
203  return AVERROR_BUG;
204  }
205 
206  return 0;
207 }
208 
209 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
210 {
211  SilenceDetectContext *s = inlink->dst->priv;
212  const int nb_channels = inlink->channels;
213  const int srate = inlink->sample_rate;
214  const int nb_samples = insamples->nb_samples * nb_channels;
215  const int64_t nb_samples_notify = s->duration * (s->mono ? 1 : nb_channels);
216  int c;
217 
218  // scale number of null samples to the new sample rate
219  if (s->last_sample_rate && s->last_sample_rate != srate)
220  for (c = 0; c < s->independent_channels; c++) {
221  s->nb_null_samples[c] = srate * s->nb_null_samples[c] / s->last_sample_rate;
222  }
223  s->last_sample_rate = srate;
224  s->time_base = inlink->time_base;
225  s->frame_end = insamples->pts + av_rescale_q(insamples->nb_samples,
226  (AVRational){ 1, s->last_sample_rate }, inlink->time_base);
227 
228  s->silencedetect(s, insamples, nb_samples, nb_samples_notify,
229  inlink->time_base);
230 
231  return ff_filter_frame(inlink->dst->outputs[0], insamples);
232 }
233 
235 {
236  static const enum AVSampleFormat sample_fmts[] = {
242  };
244  if (ret < 0)
245  return ret;
246 
248  if (ret < 0)
249  return ret;
250 
252 }
253 
255 {
256  SilenceDetectContext *s = ctx->priv;
257  int c;
258 
259  for (c = 0; c < s->independent_channels; c++)
260  if (s->start[c] > INT64_MIN)
261  update(s, NULL, 0, c, 0, s->time_base);
262  av_freep(&s->nb_null_samples);
263  av_freep(&s->start);
264 }
265 
267  {
268  .name = "default",
269  .type = AVMEDIA_TYPE_AUDIO,
270  .config_props = config_input,
271  .filter_frame = filter_frame,
272  },
273 };
274 
276  {
277  .name = "default",
278  .type = AVMEDIA_TYPE_AUDIO,
279  },
280 };
281 
283  .name = "silencedetect",
284  .description = NULL_IF_CONFIG_SMALL("Detect silence."),
285  .priv_size = sizeof(SilenceDetectContext),
287  .uninit = uninit,
290  .priv_class = &silencedetect_class,
291 };
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
SILENCE_DETECT
#define SILENCE_DETECT(name, type)
Definition: af_silencedetect.c:120
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1017
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
SilenceDetectContext::noise
double noise
noise amplitude ratio
Definition: af_silencedetect.c:37
silencedetect_outputs
static const AVFilterPad silencedetect_outputs[]
Definition: af_silencedetect.c:275
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
Definition: af_silencedetect.c:209
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:396
AVOption
AVOption.
Definition: opt.h:247
SilenceDetectContext::frame_end
int64_t frame_end
pts of the end of the current frame (used to compute duration of silence at EOS)
Definition: af_silencedetect.c:44
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:68
silencedetect_inputs
static const AVFilterPad silencedetect_inputs[]
Definition: af_silencedetect.c:266
AV_OPT_TYPE_DURATION
@ AV_OPT_TYPE_DURATION
Definition: opt.h:238
av_mallocz_array
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:214
ff_set_common_all_samplerates
int ff_set_common_all_samplerates(AVFilterContext *ctx)
Equivalent to ff_set_common_samplerates(ctx, ff_all_samplerates())
Definition: formats.c:687
float.h
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:153
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_silencedetect.c:254
formats.h
update
static av_always_inline void update(SilenceDetectContext *s, AVFrame *insamples, int is_silence, int current_sample, int64_t nb_samples_notify, AVRational time_base)
Definition: af_silencedetect.c:78
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
av_cold
#define av_cold
Definition: attributes.h:90
duration
int64_t duration
Definition: movenc.c:64
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:226
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ff_set_common_formats_from_list
int ff_set_common_formats_from_list(AVFilterContext *ctx, const int *fmts)
Equivalent to ff_set_common_formats(ctx, ff_make_format_list(fmts))
Definition: formats.c:703
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:141
SilenceDetectContext::mono
int mono
mono mode : check each channel separately (default = check when ALL channels are silent)
Definition: af_silencedetect.c:39
MAX_DURATION
#define MAX_DURATION
Definition: af_silencedetect.c:53
key
const char * key
Definition: hwcontext_opencl.c:168
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_silencedetect.c:234
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:152
SilenceDetectContext::channels
int channels
number of channels
Definition: af_silencedetect.c:40
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
SilenceDetectContext::start
int64_t * start
(array) if silence is detected, this value contains the time of the first zero sample (default/unset ...
Definition: af_silencedetect.c:43
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
ff_af_silencedetect
const AVFilter ff_af_silencedetect
Definition: af_silencedetect.c:282
SILENCE_DETECT_PLANAR
#define SILENCE_DETECT_PLANAR(name, type)
Definition: af_silencedetect.c:134
ff_set_common_all_channel_counts
int ff_set_common_all_channel_counts(AVFilterContext *ctx)
Equivalent to ff_set_common_channel_layouts(ctx, ff_all_channel_counts())
Definition: formats.c:669
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
SilenceDetectContext::time_base
AVRational time_base
time_base
Definition: af_silencedetect.c:46
av_ts2timestr
#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
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
OFFSET
#define OFFSET(x)
Definition: af_silencedetect.c:54
set_meta
static void set_meta(AVFrame *insamples, int channel, const char *key, char *value)
Definition: af_silencedetect.c:68
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:67
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
internal.h
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:369
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
FLAGS
#define FLAGS
Definition: af_silencedetect.c:55
av_always_inline
#define av_always_inline
Definition: attributes.h:49
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
SilenceDetectContext
Definition: af_silencedetect.c:35
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
silencedetect_options
static const AVOption silencedetect_options[]
Definition: af_silencedetect.c:56
av_rescale
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:128
AVFilter
Filter definition.
Definition: avfilter.h:149
ret
ret
Definition: filter_design.txt:187
SilenceDetectContext::duration
int64_t duration
minimum duration of silence until notification
Definition: af_silencedetect.c:38
SilenceDetectContext::independent_channels
int independent_channels
number of entries in following arrays (always 1 in mono mode)
Definition: af_silencedetect.c:41
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(silencedetect)
noise
static int noise(AVBSFContext *ctx, AVPacket *pkt)
Definition: noise_bsf.c:121
avfilter.h
AVFrame::metadata
AVDictionary * metadata
metadata.
Definition: frame.h:572
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:70
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_silencedetect.c:163
AVFilterContext
An instance of a filter.
Definition: avfilter.h:346
audio.h
SilenceDetectContext::silencedetect
void(* silencedetect)(struct SilenceDetectContext *s, AVFrame *insamples, int nb_samples, int64_t nb_samples_notify, AVRational time_base)
Definition: af_silencedetect.c:48
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:241
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:153
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
av_dict_set
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
SilenceDetectContext::nb_null_samples
int64_t * nb_null_samples
(array) current number of continuous zero samples
Definition: af_silencedetect.c:42
int32_t
int32_t
Definition: audioconvert.c:56
timestamp.h
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
SilenceDetectContext::last_sample_rate
int last_sample_rate
last sample rate to check for sample rate changes
Definition: af_silencedetect.c:45
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:64
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:62
snprintf
#define snprintf
Definition: snprintf.h:34
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:63
channel
channel
Definition: ebur128.h:39
nb_channels
int nb_channels
Definition: channel_layout.c:81