LCOV - code coverage report
Current view: top level - libavfilter - af_silencedetect.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 87 109 79.8 %
Date: 2018-05-20 11:54:08 Functions: 7 10 70.0 %

          Line data    Source code
       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             :     double 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 independant_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,
      50             :                           AVRational time_base);
      51             : } SilenceDetectContext;
      52             : 
      53             : #define OFFSET(x) offsetof(SilenceDetectContext, x)
      54             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
      55             : static const AVOption silencedetect_options[] = {
      56             :     { "n",         "set noise tolerance",              OFFSET(noise),     AV_OPT_TYPE_DOUBLE, {.dbl=0.001},          0, DBL_MAX,  FLAGS },
      57             :     { "noise",     "set noise tolerance",              OFFSET(noise),     AV_OPT_TYPE_DOUBLE, {.dbl=0.001},          0, DBL_MAX,  FLAGS },
      58             :     { "d",         "set minimum duration in seconds",  OFFSET(duration),  AV_OPT_TYPE_DOUBLE, {.dbl=2.},             0, 24*60*60, FLAGS },
      59             :     { "duration",  "set minimum duration in seconds",  OFFSET(duration),  AV_OPT_TYPE_DOUBLE, {.dbl=2.},             0, 24*60*60, FLAGS },
      60             :     { "mono",      "check each channel separately",    OFFSET(mono),      AV_OPT_TYPE_BOOL,   {.i64=0.},             0, 1, FLAGS },
      61             :     { NULL }
      62             : };
      63             : 
      64             : AVFILTER_DEFINE_CLASS(silencedetect);
      65             : 
      66           9 : static void set_meta(AVFrame *insamples, int channel, const char *key, char *value)
      67             : {
      68             :     char key2[128];
      69             : 
      70           9 :     if (channel)
      71           0 :         snprintf(key2, sizeof(key2), "lavfi.%s.%d", key, channel);
      72             :     else
      73           9 :         snprintf(key2, sizeof(key2), "lavfi.%s", key);
      74           9 :     av_dict_set(&insamples->metadata, key2, value, 0);
      75           9 : }
      76     1048554 : static av_always_inline void update(SilenceDetectContext *s, AVFrame *insamples,
      77             :                                     int is_silence, int current_sample, int64_t nb_samples_notify,
      78             :                                     AVRational time_base)
      79             : {
      80     1048554 :     int channel = current_sample % s->independant_channels;
      81     1048554 :     if (is_silence) {
      82      304774 :         if (s->start[channel] == INT64_MIN) {
      83      221400 :             s->nb_null_samples[channel]++;
      84      221400 :             if (s->nb_null_samples[channel] >= nb_samples_notify) {
      85           6 :                 s->start[channel] = insamples->pts + av_rescale_q(current_sample / s->channels + 1 - nb_samples_notify * s->independant_channels / s->channels,
      86           3 :                         (AVRational){ 1, s->last_sample_rate }, time_base);
      87           6 :                 set_meta(insamples, s->mono ? channel + 1 : 0, "silence_start",
      88           6 :                         av_ts2timestr(s->start[channel], &time_base));
      89           3 :                 if (s->mono)
      90           0 :                     av_log(s, AV_LOG_INFO, "channel: %d | ", channel);
      91           6 :                 av_log(s, AV_LOG_INFO, "silence_start: %s\n",
      92           6 :                         av_ts2timestr(s->start[channel], &time_base));
      93             :             }
      94             :         }
      95             :     } else {
      96      743780 :         if (s->start[channel] > INT64_MIN) {
      97           9 :             int64_t end_pts = insamples ? insamples->pts + av_rescale_q(current_sample / s->channels,
      98           3 :                     (AVRational){ 1, s->last_sample_rate }, time_base)
      99           6 :                     : s->frame_end;
     100           3 :             int64_t duration_ts = end_pts - s->start[channel];
     101           3 :             if (insamples) {
     102           3 :                 set_meta(insamples, s->mono ? channel + 1 : 0, "silence_end",
     103           3 :                         av_ts2timestr(end_pts, &time_base));
     104           3 :                 set_meta(insamples, s->mono ? channel + 1 : 0, "silence_duration",
     105           3 :                         av_ts2timestr(duration_ts, &time_base));
     106             :             }
     107           3 :             if (s->mono)
     108           0 :                 av_log(s, AV_LOG_INFO, "channel: %d | ", channel);
     109           6 :             av_log(s, AV_LOG_INFO, "silence_end: %s | silence_duration: %s\n",
     110           3 :                     av_ts2timestr(end_pts, &time_base),
     111           3 :                     av_ts2timestr(duration_ts, &time_base));
     112             :         }
     113      743780 :         s->nb_null_samples[channel] = 0;
     114      743780 :         s->start[channel] = INT64_MIN;
     115             :     }
     116     1048554 : }
     117             : 
     118             : #define SILENCE_DETECT(name, type)                                               \
     119             : static void silencedetect_##name(SilenceDetectContext *s, AVFrame *insamples,    \
     120             :                                  int nb_samples, int64_t nb_samples_notify,      \
     121             :                                  AVRational time_base)                           \
     122             : {                                                                                \
     123             :     const type *p = (const type *)insamples->data[0];                            \
     124             :     const type noise = s->noise;                                                 \
     125             :     int i;                                                                       \
     126             :                                                                                  \
     127             :     for (i = 0; i < nb_samples; i++, p++)                                        \
     128             :         update(s, insamples, *p < noise && *p > -noise, i,                       \
     129             :                nb_samples_notify, time_base);                                    \
     130             : }
     131             : 
     132           0 : SILENCE_DETECT(dbl, double)
     133           0 : SILENCE_DETECT(flt, float)
     134           0 : SILENCE_DETECT(s32, int32_t)
     135          12 : SILENCE_DETECT(s16, int16_t)
     136             : 
     137           1 : static int config_input(AVFilterLink *inlink)
     138             : {
     139           1 :     AVFilterContext *ctx = inlink->dst;
     140           1 :     SilenceDetectContext *s = ctx->priv;
     141             :     int c;
     142             : 
     143           1 :     s->channels = inlink->channels;
     144           1 :     s->independant_channels = s->mono ? s->channels : 1;
     145           1 :     s->nb_null_samples = av_mallocz_array(sizeof(*s->nb_null_samples), s->independant_channels);
     146           1 :     if (!s->nb_null_samples)
     147           0 :         return AVERROR(ENOMEM);
     148           1 :     s->start = av_malloc_array(sizeof(*s->start), s->independant_channels);
     149           1 :     if (!s->start)
     150           0 :         return AVERROR(ENOMEM);
     151           2 :     for (c = 0; c < s->independant_channels; c++)
     152           1 :         s->start[c] = INT64_MIN;
     153             : 
     154           1 :     switch (inlink->format) {
     155           0 :     case AV_SAMPLE_FMT_DBL: s->silencedetect = silencedetect_dbl; break;
     156           0 :     case AV_SAMPLE_FMT_FLT: s->silencedetect = silencedetect_flt; break;
     157           0 :     case AV_SAMPLE_FMT_S32:
     158           0 :         s->noise *= INT32_MAX;
     159           0 :         s->silencedetect = silencedetect_s32;
     160           0 :         break;
     161           1 :     case AV_SAMPLE_FMT_S16:
     162           1 :         s->noise *= INT16_MAX;
     163           1 :         s->silencedetect = silencedetect_s16;
     164           1 :         break;
     165             :     }
     166             : 
     167           1 :     return 0;
     168             : }
     169             : 
     170          12 : static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
     171             : {
     172          12 :     SilenceDetectContext *s         = inlink->dst->priv;
     173          12 :     const int nb_channels           = inlink->channels;
     174          12 :     const int srate                 = inlink->sample_rate;
     175          12 :     const int nb_samples            = insamples->nb_samples     * nb_channels;
     176          12 :     const int64_t nb_samples_notify = srate * s->duration * (s->mono ? 1 : nb_channels);
     177             :     int c;
     178             : 
     179             :     // scale number of null samples to the new sample rate
     180          12 :     if (s->last_sample_rate && s->last_sample_rate != srate)
     181           0 :         for (c = 0; c < s->independant_channels; c++) {
     182           0 :             s->nb_null_samples[c] = srate * s->nb_null_samples[c] / s->last_sample_rate;
     183             :         }
     184          12 :     s->last_sample_rate = srate;
     185          12 :     s->time_base = inlink->time_base;
     186          24 :     s->frame_end = insamples->pts + av_rescale_q(insamples->nb_samples,
     187          12 :             (AVRational){ 1, s->last_sample_rate }, inlink->time_base);
     188             : 
     189             :     // TODO: document metadata
     190          12 :     s->silencedetect(s, insamples, nb_samples, nb_samples_notify,
     191             :                      inlink->time_base);
     192             : 
     193          12 :     return ff_filter_frame(inlink->dst->outputs[0], insamples);
     194             : }
     195             : 
     196           1 : static int query_formats(AVFilterContext *ctx)
     197             : {
     198           1 :     AVFilterFormats *formats = NULL;
     199           1 :     AVFilterChannelLayouts *layouts = NULL;
     200             :     static const enum AVSampleFormat sample_fmts[] = {
     201             :         AV_SAMPLE_FMT_DBL,
     202             :         AV_SAMPLE_FMT_FLT,
     203             :         AV_SAMPLE_FMT_S32,
     204             :         AV_SAMPLE_FMT_S16,
     205             :         AV_SAMPLE_FMT_NONE
     206             :     };
     207             :     int ret;
     208             : 
     209           1 :     layouts = ff_all_channel_layouts();
     210           1 :     if (!layouts)
     211           0 :         return AVERROR(ENOMEM);
     212           1 :     ret = ff_set_common_channel_layouts(ctx, layouts);
     213           1 :     if (ret < 0)
     214           0 :         return ret;
     215             : 
     216           1 :     formats = ff_make_format_list(sample_fmts);
     217           1 :     if (!formats)
     218           0 :         return AVERROR(ENOMEM);
     219           1 :     ret = ff_set_common_formats(ctx, formats);
     220           1 :     if (ret < 0)
     221           0 :         return ret;
     222             : 
     223           1 :     formats = ff_all_samplerates();
     224           1 :     if (!formats)
     225           0 :         return AVERROR(ENOMEM);
     226           1 :     return ff_set_common_samplerates(ctx, formats);
     227             : }
     228             : 
     229           1 : static av_cold void uninit(AVFilterContext *ctx)
     230             : {
     231           1 :     SilenceDetectContext *s = ctx->priv;
     232             :     int c;
     233             : 
     234           2 :     for (c = 0; c < s->independant_channels; c++)
     235           1 :         if (s->start[c] > INT64_MIN)
     236           0 :             update(s, NULL, 0, c, 0, s->time_base);
     237           1 :     av_freep(&s->nb_null_samples);
     238           1 :     av_freep(&s->start);
     239           1 : }
     240             : 
     241             : static const AVFilterPad silencedetect_inputs[] = {
     242             :     {
     243             :         .name         = "default",
     244             :         .type         = AVMEDIA_TYPE_AUDIO,
     245             :         .config_props = config_input,
     246             :         .filter_frame = filter_frame,
     247             :     },
     248             :     { NULL }
     249             : };
     250             : 
     251             : static const AVFilterPad silencedetect_outputs[] = {
     252             :     {
     253             :         .name = "default",
     254             :         .type = AVMEDIA_TYPE_AUDIO,
     255             :     },
     256             :     { NULL }
     257             : };
     258             : 
     259             : AVFilter ff_af_silencedetect = {
     260             :     .name          = "silencedetect",
     261             :     .description   = NULL_IF_CONFIG_SMALL("Detect silence."),
     262             :     .priv_size     = sizeof(SilenceDetectContext),
     263             :     .query_formats = query_formats,
     264             :     .uninit        = uninit,
     265             :     .inputs        = silencedetect_inputs,
     266             :     .outputs       = silencedetect_outputs,
     267             :     .priv_class    = &silencedetect_class,
     268             : };

Generated by: LCOV version 1.13