LCOV - code coverage report
Current view: top level - libavfilter - af_drmeter.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 91 0.0 %
Date: 2018-05-20 11:54:08 Functions: 0 7 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2018 Paul B Mahol
       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             : #include <float.h>
      22             : 
      23             : #include "libavutil/ffmath.h"
      24             : #include "libavutil/opt.h"
      25             : #include "audio.h"
      26             : #include "avfilter.h"
      27             : #include "internal.h"
      28             : 
      29             : typedef struct ChannelStats {
      30             :     uint64_t nb_samples;
      31             :     uint64_t blknum;
      32             :     float peak;
      33             :     float sum;
      34             :     uint32_t peaks[10001];
      35             :     uint32_t rms[10001];
      36             : } ChannelStats;
      37             : 
      38             : typedef struct DRMeterContext {
      39             :     const AVClass *class;
      40             :     ChannelStats *chstats;
      41             :     int nb_channels;
      42             :     uint64_t tc_samples;
      43             :     double time_constant;
      44             : } DRMeterContext;
      45             : 
      46             : #define OFFSET(x) offsetof(DRMeterContext, x)
      47             : #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      48             : 
      49             : static const AVOption drmeter_options[] = {
      50             :     { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=3}, .01, 10, FLAGS },
      51             :     { NULL }
      52             : };
      53             : 
      54             : AVFILTER_DEFINE_CLASS(drmeter);
      55             : 
      56           0 : static int query_formats(AVFilterContext *ctx)
      57             : {
      58             :     AVFilterFormats *formats;
      59             :     AVFilterChannelLayouts *layouts;
      60             :     static const enum AVSampleFormat sample_fmts[] = {
      61             :         AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_FLT,
      62             :         AV_SAMPLE_FMT_NONE
      63             :     };
      64             :     int ret;
      65             : 
      66           0 :     layouts = ff_all_channel_counts();
      67           0 :     if (!layouts)
      68           0 :         return AVERROR(ENOMEM);
      69           0 :     ret = ff_set_common_channel_layouts(ctx, layouts);
      70           0 :     if (ret < 0)
      71           0 :         return ret;
      72             : 
      73           0 :     formats = ff_make_format_list(sample_fmts);
      74           0 :     if (!formats)
      75           0 :         return AVERROR(ENOMEM);
      76           0 :     ret = ff_set_common_formats(ctx, formats);
      77           0 :     if (ret < 0)
      78           0 :         return ret;
      79             : 
      80           0 :     formats = ff_all_samplerates();
      81           0 :     if (!formats)
      82           0 :         return AVERROR(ENOMEM);
      83           0 :     return ff_set_common_samplerates(ctx, formats);
      84             : }
      85             : 
      86           0 : static int config_output(AVFilterLink *outlink)
      87             : {
      88           0 :     DRMeterContext *s = outlink->src->priv;
      89             : 
      90           0 :     s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
      91           0 :     if (!s->chstats)
      92           0 :         return AVERROR(ENOMEM);
      93           0 :     s->nb_channels = outlink->channels;
      94           0 :     s->tc_samples = s->time_constant * outlink->sample_rate + .5;
      95             : 
      96           0 :     return 0;
      97             : }
      98             : 
      99           0 : static void finish_block(ChannelStats *p)
     100             : {
     101             :     int peak_bin, rms_bin;
     102             :     float peak, rms;
     103             : 
     104           0 :     rms = sqrt(2 * p->sum / p->nb_samples);
     105           0 :     peak = p->peak;
     106           0 :     rms_bin = av_clip(rms * 10000, 0, 10000);
     107           0 :     peak_bin = av_clip(peak * 10000, 0, 10000);
     108           0 :     p->rms[rms_bin]++;
     109           0 :     p->peaks[peak_bin]++;
     110             : 
     111           0 :     p->peak = 0;
     112           0 :     p->sum = 0;
     113           0 :     p->nb_samples = 0;
     114           0 :     p->blknum++;
     115           0 : }
     116             : 
     117           0 : static void update_stat(DRMeterContext *s, ChannelStats *p, float sample)
     118             : {
     119           0 :     if (p->nb_samples >= s->tc_samples) {
     120           0 :         finish_block(p);
     121             :     }
     122             : 
     123           0 :     p->peak = FFMAX(FFABS(sample), p->peak);
     124           0 :     p->sum += sample * sample;
     125           0 :     p->nb_samples++;
     126           0 : }
     127             : 
     128           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
     129             : {
     130           0 :     DRMeterContext *s = inlink->dst->priv;
     131           0 :     const int channels = s->nb_channels;
     132             :     int i, c;
     133             : 
     134           0 :     switch (inlink->format) {
     135           0 :     case AV_SAMPLE_FMT_FLTP:
     136           0 :         for (c = 0; c < channels; c++) {
     137           0 :             ChannelStats *p = &s->chstats[c];
     138           0 :             const float *src = (const float *)buf->extended_data[c];
     139             : 
     140           0 :             for (i = 0; i < buf->nb_samples; i++, src++)
     141           0 :                 update_stat(s, p, *src);
     142             :         }
     143           0 :         break;
     144           0 :     case AV_SAMPLE_FMT_FLT: {
     145           0 :         const float *src = (const float *)buf->extended_data[0];
     146             : 
     147           0 :         for (i = 0; i < buf->nb_samples; i++) {
     148           0 :             for (c = 0; c < channels; c++, src++)
     149           0 :                 update_stat(s, &s->chstats[c], *src);
     150             :         }}
     151           0 :         break;
     152             :     }
     153             : 
     154           0 :     return ff_filter_frame(inlink->dst->outputs[0], buf);
     155             : }
     156             : 
     157             : #define SQR(a) ((a)*(a))
     158             : 
     159           0 : static void print_stats(AVFilterContext *ctx)
     160             : {
     161           0 :     DRMeterContext *s = ctx->priv;
     162           0 :     float dr = 0;
     163             :     int ch;
     164             : 
     165           0 :     for (ch = 0; ch < s->nb_channels; ch++) {
     166           0 :         ChannelStats *p = &s->chstats[ch];
     167           0 :         float chdr, secondpeak, rmssum = 0;
     168           0 :         int i, j, first = 0;
     169             : 
     170           0 :         finish_block(p);
     171             : 
     172           0 :         for (i = 0; i <= 10000; i++) {
     173           0 :             if (p->peaks[10000 - i]) {
     174           0 :                 if (first)
     175           0 :                     break;
     176           0 :                 first = 1;
     177             :             }
     178             :         }
     179             : 
     180           0 :         secondpeak = (10000 - i) / 10000.;
     181             : 
     182           0 :         for (i = 10000, j = 0; i >= 0 && j < 0.2 * p->blknum; i--) {
     183           0 :             if (p->rms[i]) {
     184           0 :                 rmssum += SQR(i / 10000.) * p->rms[i];
     185           0 :                 j += p->rms[i];
     186             :             }
     187             :         }
     188             : 
     189           0 :         chdr = 20 * log10(secondpeak / sqrt(rmssum / (0.2 * p->blknum)));
     190           0 :         dr += chdr;
     191           0 :         av_log(ctx, AV_LOG_INFO, "Channel %d: DR: %.1f\n", ch + 1, chdr);
     192             :     }
     193             : 
     194           0 :     av_log(ctx, AV_LOG_INFO, "Overall DR: %.1f\n", dr / s->nb_channels);
     195           0 : }
     196             : 
     197           0 : static av_cold void uninit(AVFilterContext *ctx)
     198             : {
     199           0 :     DRMeterContext *s = ctx->priv;
     200             : 
     201           0 :     if (s->nb_channels)
     202           0 :         print_stats(ctx);
     203           0 :     av_freep(&s->chstats);
     204           0 : }
     205             : 
     206             : static const AVFilterPad drmeter_inputs[] = {
     207             :     {
     208             :         .name         = "default",
     209             :         .type         = AVMEDIA_TYPE_AUDIO,
     210             :         .filter_frame = filter_frame,
     211             :     },
     212             :     { NULL }
     213             : };
     214             : 
     215             : static const AVFilterPad drmeter_outputs[] = {
     216             :     {
     217             :         .name         = "default",
     218             :         .type         = AVMEDIA_TYPE_AUDIO,
     219             :         .config_props = config_output,
     220             :     },
     221             :     { NULL }
     222             : };
     223             : 
     224             : AVFilter ff_af_drmeter = {
     225             :     .name          = "drmeter",
     226             :     .description   = NULL_IF_CONFIG_SMALL("Measure audio dynamic range."),
     227             :     .query_formats = query_formats,
     228             :     .priv_size     = sizeof(DRMeterContext),
     229             :     .priv_class    = &drmeter_class,
     230             :     .uninit        = uninit,
     231             :     .inputs        = drmeter_inputs,
     232             :     .outputs       = drmeter_outputs,
     233             : };

Generated by: LCOV version 1.13