annotate ffmpeg/libavfilter/af_silencedetect.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Copyright (c) 2012 Clément Bœsch <ubitux@gmail.com>
yading@10 3 *
yading@10 4 * This file is part of FFmpeg.
yading@10 5 *
yading@10 6 * FFmpeg is free software; you can redistribute it and/or
yading@10 7 * modify it under the terms of the GNU Lesser General Public
yading@10 8 * License as published by the Free Software Foundation; either
yading@10 9 * version 2.1 of the License, or (at your option) any later version.
yading@10 10 *
yading@10 11 * FFmpeg is distributed in the hope that it will be useful,
yading@10 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 14 * Lesser General Public License for more details.
yading@10 15 *
yading@10 16 * You should have received a copy of the GNU Lesser General Public
yading@10 17 * License along with FFmpeg; if not, write to the Free Software
yading@10 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 19 */
yading@10 20
yading@10 21 /**
yading@10 22 * @file
yading@10 23 * Audio silence detector
yading@10 24 */
yading@10 25
yading@10 26 #include <float.h> /* DBL_MAX */
yading@10 27
yading@10 28 #include "libavutil/channel_layout.h"
yading@10 29 #include "libavutil/opt.h"
yading@10 30 #include "libavutil/timestamp.h"
yading@10 31 #include "audio.h"
yading@10 32 #include "formats.h"
yading@10 33 #include "avfilter.h"
yading@10 34 #include "internal.h"
yading@10 35
yading@10 36 typedef struct {
yading@10 37 const AVClass *class;
yading@10 38 double noise; ///< noise amplitude ratio
yading@10 39 double duration; ///< minimum duration of silence until notification
yading@10 40 int64_t nb_null_samples; ///< current number of continuous zero samples
yading@10 41 int64_t start; ///< if silence is detected, this value contains the time of the first zero sample
yading@10 42 int last_sample_rate; ///< last sample rate to check for sample rate changes
yading@10 43 } SilenceDetectContext;
yading@10 44
yading@10 45 #define OFFSET(x) offsetof(SilenceDetectContext, x)
yading@10 46 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
yading@10 47 static const AVOption silencedetect_options[] = {
yading@10 48 { "n", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
yading@10 49 { "noise", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, DBL_MAX, FLAGS },
yading@10 50 { "d", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, FLAGS },
yading@10 51 { "duration", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, {.dbl=2.}, 0, 24*60*60, FLAGS },
yading@10 52 { NULL },
yading@10 53 };
yading@10 54
yading@10 55 AVFILTER_DEFINE_CLASS(silencedetect);
yading@10 56
yading@10 57 static char *get_metadata_val(AVFrame *insamples, const char *key)
yading@10 58 {
yading@10 59 AVDictionaryEntry *e = av_dict_get(insamples->metadata, key, NULL, 0);
yading@10 60 return e && e->value ? e->value : NULL;
yading@10 61 }
yading@10 62
yading@10 63 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
yading@10 64 {
yading@10 65 int i;
yading@10 66 SilenceDetectContext *silence = inlink->dst->priv;
yading@10 67 const int nb_channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
yading@10 68 const int srate = inlink->sample_rate;
yading@10 69 const int nb_samples = insamples->nb_samples * nb_channels;
yading@10 70 const int64_t nb_samples_notify = srate * silence->duration * nb_channels;
yading@10 71
yading@10 72 // scale number of null samples to the new sample rate
yading@10 73 if (silence->last_sample_rate && silence->last_sample_rate != srate)
yading@10 74 silence->nb_null_samples =
yading@10 75 srate * silence->nb_null_samples / silence->last_sample_rate;
yading@10 76 silence->last_sample_rate = srate;
yading@10 77
yading@10 78 // TODO: support more sample formats
yading@10 79 // TODO: document metadata
yading@10 80 if (insamples->format == AV_SAMPLE_FMT_DBL) {
yading@10 81 double *p = (double *)insamples->data[0];
yading@10 82
yading@10 83 for (i = 0; i < nb_samples; i++, p++) {
yading@10 84 if (*p < silence->noise && *p > -silence->noise) {
yading@10 85 if (!silence->start) {
yading@10 86 silence->nb_null_samples++;
yading@10 87 if (silence->nb_null_samples >= nb_samples_notify) {
yading@10 88 silence->start = insamples->pts - (int64_t)(silence->duration / av_q2d(inlink->time_base) + .5);
yading@10 89 av_dict_set(&insamples->metadata, "lavfi.silence_start",
yading@10 90 av_ts2timestr(silence->start, &inlink->time_base), 0);
yading@10 91 av_log(silence, AV_LOG_INFO, "silence_start: %s\n",
yading@10 92 get_metadata_val(insamples, "lavfi.silence_start"));
yading@10 93 }
yading@10 94 }
yading@10 95 } else {
yading@10 96 if (silence->start) {
yading@10 97 av_dict_set(&insamples->metadata, "lavfi.silence_end",
yading@10 98 av_ts2timestr(insamples->pts, &inlink->time_base), 0);
yading@10 99 av_dict_set(&insamples->metadata, "lavfi.silence_duration",
yading@10 100 av_ts2timestr(insamples->pts - silence->start, &inlink->time_base), 0);
yading@10 101 av_log(silence, AV_LOG_INFO,
yading@10 102 "silence_end: %s | silence_duration: %s\n",
yading@10 103 get_metadata_val(insamples, "lavfi.silence_end"),
yading@10 104 get_metadata_val(insamples, "lavfi.silence_duration"));
yading@10 105 }
yading@10 106 silence->nb_null_samples = silence->start = 0;
yading@10 107 }
yading@10 108 }
yading@10 109 }
yading@10 110
yading@10 111 return ff_filter_frame(inlink->dst->outputs[0], insamples);
yading@10 112 }
yading@10 113
yading@10 114 static int query_formats(AVFilterContext *ctx)
yading@10 115 {
yading@10 116 AVFilterFormats *formats = NULL;
yading@10 117 AVFilterChannelLayouts *layouts = NULL;
yading@10 118 static const enum AVSampleFormat sample_fmts[] = {
yading@10 119 AV_SAMPLE_FMT_DBL,
yading@10 120 AV_SAMPLE_FMT_NONE
yading@10 121 };
yading@10 122
yading@10 123 layouts = ff_all_channel_layouts();
yading@10 124 if (!layouts)
yading@10 125 return AVERROR(ENOMEM);
yading@10 126 ff_set_common_channel_layouts(ctx, layouts);
yading@10 127
yading@10 128 formats = ff_make_format_list(sample_fmts);
yading@10 129 if (!formats)
yading@10 130 return AVERROR(ENOMEM);
yading@10 131 ff_set_common_formats(ctx, formats);
yading@10 132
yading@10 133 formats = ff_all_samplerates();
yading@10 134 if (!formats)
yading@10 135 return AVERROR(ENOMEM);
yading@10 136 ff_set_common_samplerates(ctx, formats);
yading@10 137
yading@10 138 return 0;
yading@10 139 }
yading@10 140
yading@10 141 static const AVFilterPad silencedetect_inputs[] = {
yading@10 142 {
yading@10 143 .name = "default",
yading@10 144 .type = AVMEDIA_TYPE_AUDIO,
yading@10 145 .get_audio_buffer = ff_null_get_audio_buffer,
yading@10 146 .filter_frame = filter_frame,
yading@10 147 },
yading@10 148 { NULL }
yading@10 149 };
yading@10 150
yading@10 151 static const AVFilterPad silencedetect_outputs[] = {
yading@10 152 {
yading@10 153 .name = "default",
yading@10 154 .type = AVMEDIA_TYPE_AUDIO,
yading@10 155 },
yading@10 156 { NULL }
yading@10 157 };
yading@10 158
yading@10 159 AVFilter avfilter_af_silencedetect = {
yading@10 160 .name = "silencedetect",
yading@10 161 .description = NULL_IF_CONFIG_SMALL("Detect silence."),
yading@10 162 .priv_size = sizeof(SilenceDetectContext),
yading@10 163 .query_formats = query_formats,
yading@10 164 .inputs = silencedetect_inputs,
yading@10 165 .outputs = silencedetect_outputs,
yading@10 166 .priv_class = &silencedetect_class,
yading@10 167 };