libavfilter/af_silencedetect.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012 Clément Bœsch <ubitux@gmail.com>
00003  *
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * FFmpeg is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with FFmpeg; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00026 #include "libavutil/opt.h"
00027 #include "avfilter.h"
00028 
00029 typedef struct {
00030     const AVClass *class;
00031     char *noise_str;            
00032     double noise;               
00033     int duration;               
00034     int64_t nb_null_samples;    
00035     double start;               
00036     int last_sample_rate;       
00037 } SilenceDetectContext;
00038 
00039 #define OFFSET(x) offsetof(SilenceDetectContext, x)
00040 static const AVOption silencedetect_options[] = {
00041     { "n",         "set noise tolerance",              OFFSET(noise_str), AV_OPT_TYPE_STRING, {.str="-60dB"}, CHAR_MIN, CHAR_MAX },
00042     { "noise",     "set noise tolerance",              OFFSET(noise_str), AV_OPT_TYPE_STRING, {.str="-60dB"}, CHAR_MIN, CHAR_MAX },
00043     { "d",         "set minimum duration in seconds",  OFFSET(duration),  AV_OPT_TYPE_INT,    {.dbl=2},    0, INT_MAX},
00044     { "duration",  "set minimum duration in seconds",  OFFSET(duration),  AV_OPT_TYPE_INT,    {.dbl=2},    0, INT_MAX},
00045     { NULL },
00046 };
00047 
00048 static const char *silencedetect_get_name(void *ctx)
00049 {
00050     return "silencedetect";
00051 }
00052 
00053 static const AVClass silencedetect_class = {
00054     .class_name = "SilenceDetectContext",
00055     .item_name  = silencedetect_get_name,
00056     .option     = silencedetect_options,
00057 };
00058 
00059 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00060 {
00061     int ret;
00062     char *tail;
00063     SilenceDetectContext *silence = ctx->priv;
00064 
00065     silence->class = &silencedetect_class;
00066     av_opt_set_defaults(silence);
00067 
00068     if ((ret = av_set_options_string(silence, args, "=", ":")) < 0) {
00069         av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
00070         return ret;
00071     }
00072 
00073     silence->noise = strtod(silence->noise_str, &tail);
00074     if (!strcmp(tail, "dB")) {
00075         silence->noise = pow(10, silence->noise/20);
00076     } else if (*tail) {
00077         av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for noise parameter.\n",
00078                silence->noise_str);
00079         return AVERROR(EINVAL);
00080     }
00081 
00082     return 0;
00083 }
00084 
00085 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
00086 {
00087     int i;
00088     SilenceDetectContext *silence = inlink->dst->priv;
00089     const int nb_channels           = av_get_channel_layout_nb_channels(inlink->channel_layout);
00090     const int srate                 = inlink->sample_rate;
00091     const int nb_samples            = insamples->audio->nb_samples * nb_channels;
00092     const int64_t nb_samples_notify = srate * silence->duration    * nb_channels;
00093 
00094     // scale number of null samples to the new sample rate
00095     if (silence->last_sample_rate && silence->last_sample_rate != srate)
00096         silence->nb_null_samples =
00097             srate * silence->nb_null_samples / silence->last_sample_rate;
00098     silence->last_sample_rate = srate;
00099 
00100     // TODO: support more sample formats
00101     if (insamples->format == AV_SAMPLE_FMT_DBL) {
00102         double *p = (double *)insamples->data[0];
00103 
00104         for (i = 0; i < nb_samples; i++, p++) {
00105             if (*p < silence->noise && *p > -silence->noise) {
00106                 if (!silence->start) {
00107                     silence->nb_null_samples++;
00108                     if (silence->nb_null_samples >= nb_samples_notify) {
00109                         silence->start = insamples->pts * av_q2d(inlink->time_base) - silence->duration;
00110                         av_log(silence, AV_LOG_INFO,
00111                                "silence_start: %f\n", silence->start);
00112                     }
00113                 }
00114             } else {
00115                 if (silence->start) {
00116                     double end = insamples->pts * av_q2d(inlink->time_base);
00117                     av_log(silence, AV_LOG_INFO,
00118                            "silence_end: %f | silence_duration: %f\n",
00119                            end, end - silence->start);
00120                 }
00121                 silence->nb_null_samples = silence->start = 0;
00122             }
00123         }
00124     }
00125 
00126     avfilter_filter_samples(inlink->dst->outputs[0], insamples);
00127 }
00128 
00129 static int query_formats(AVFilterContext *ctx)
00130 {
00131     AVFilterFormats *formats = NULL;
00132     enum AVSampleFormat sample_fmts[] = {
00133         AV_SAMPLE_FMT_DBL,
00134         AV_SAMPLE_FMT_NONE
00135     };
00136     int packing_fmts[] = { AVFILTER_PACKED, -1 };
00137 
00138     formats = avfilter_make_all_channel_layouts();
00139     if (!formats)
00140         return AVERROR(ENOMEM);
00141     avfilter_set_common_channel_layouts(ctx, formats);
00142 
00143     formats = avfilter_make_format_list(sample_fmts);
00144     if (!formats)
00145         return AVERROR(ENOMEM);
00146     avfilter_set_common_sample_formats(ctx, formats);
00147 
00148     formats = avfilter_make_format_list(packing_fmts);
00149     if (!formats)
00150         return AVERROR(ENOMEM);
00151     avfilter_set_common_packing_formats(ctx, formats);
00152 
00153     return 0;
00154 }
00155 
00156 AVFilter avfilter_af_silencedetect = {
00157     .name          = "silencedetect",
00158     .description   = NULL_IF_CONFIG_SMALL("Detect silence."),
00159     .priv_size     = sizeof(SilenceDetectContext),
00160     .init          = init,
00161     .query_formats = query_formats,
00162 
00163     .inputs = (const AVFilterPad[]) {
00164         { .name             = "default",
00165           .type             = AVMEDIA_TYPE_AUDIO,
00166           .get_audio_buffer = avfilter_null_get_audio_buffer,
00167           .filter_samples   = filter_samples, },
00168         { .name = NULL }
00169     },
00170     .outputs = (const AVFilterPad[]) {
00171         { .name = "default",
00172           .type = AVMEDIA_TYPE_AUDIO, },
00173         { .name = NULL }
00174     },
00175 };