LCOV - code coverage report
Current view: top level - src/libavfilter - af_astats.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 339 0.0 %
Date: 2017-06-22 08:51:55 Functions: 0 10 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2009 Rob Sykes <robs@users.sourceforge.net>
       3             :  * Copyright (c) 2013 Paul B Mahol
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include <float.h>
      23             : 
      24             : #include "libavutil/opt.h"
      25             : #include "audio.h"
      26             : #include "avfilter.h"
      27             : #include "internal.h"
      28             : 
      29             : typedef struct ChannelStats {
      30             :     double last;
      31             :     double sigma_x, sigma_x2;
      32             :     double avg_sigma_x2, min_sigma_x2, max_sigma_x2;
      33             :     double min, max;
      34             :     double nmin, nmax;
      35             :     double min_run, max_run;
      36             :     double min_runs, max_runs;
      37             :     double min_diff, max_diff;
      38             :     double diff1_sum;
      39             :     double diff1_sum_x2;
      40             :     uint64_t mask, imask;
      41             :     uint64_t min_count, max_count;
      42             :     uint64_t nb_samples;
      43             : } ChannelStats;
      44             : 
      45             : typedef struct AudioStatsContext {
      46             :     const AVClass *class;
      47             :     ChannelStats *chstats;
      48             :     int nb_channels;
      49             :     uint64_t tc_samples;
      50             :     double time_constant;
      51             :     double mult;
      52             :     int metadata;
      53             :     int reset_count;
      54             :     int nb_frames;
      55             :     int maxbitdepth;
      56             : } AudioStatsContext;
      57             : 
      58             : #define OFFSET(x) offsetof(AudioStatsContext, x)
      59             : #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      60             : 
      61             : static const AVOption astats_options[] = {
      62             :     { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=.05}, .01, 10, FLAGS },
      63             :     { "metadata", "inject metadata in the filtergraph", OFFSET(metadata), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
      64             :     { "reset", "recalculate stats after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
      65             :     { NULL }
      66             : };
      67             : 
      68             : AVFILTER_DEFINE_CLASS(astats);
      69             : 
      70           0 : static int query_formats(AVFilterContext *ctx)
      71             : {
      72             :     AVFilterFormats *formats;
      73             :     AVFilterChannelLayouts *layouts;
      74             :     static const enum AVSampleFormat sample_fmts[] = {
      75             :         AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
      76             :         AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
      77             :         AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64P,
      78             :         AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
      79             :         AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
      80             :         AV_SAMPLE_FMT_NONE
      81             :     };
      82             :     int ret;
      83             : 
      84           0 :     layouts = ff_all_channel_counts();
      85           0 :     if (!layouts)
      86           0 :         return AVERROR(ENOMEM);
      87           0 :     ret = ff_set_common_channel_layouts(ctx, layouts);
      88           0 :     if (ret < 0)
      89           0 :         return ret;
      90             : 
      91           0 :     formats = ff_make_format_list(sample_fmts);
      92           0 :     if (!formats)
      93           0 :         return AVERROR(ENOMEM);
      94           0 :     ret = ff_set_common_formats(ctx, formats);
      95           0 :     if (ret < 0)
      96           0 :         return ret;
      97             : 
      98           0 :     formats = ff_all_samplerates();
      99           0 :     if (!formats)
     100           0 :         return AVERROR(ENOMEM);
     101           0 :     return ff_set_common_samplerates(ctx, formats);
     102             : }
     103             : 
     104           0 : static void reset_stats(AudioStatsContext *s)
     105             : {
     106             :     int c;
     107             : 
     108           0 :     for (c = 0; c < s->nb_channels; c++) {
     109           0 :         ChannelStats *p = &s->chstats[c];
     110             : 
     111           0 :         p->min = p->nmin = p->min_sigma_x2 = DBL_MAX;
     112           0 :         p->max = p->nmax = p->max_sigma_x2 = DBL_MIN;
     113           0 :         p->min_diff = DBL_MAX;
     114           0 :         p->max_diff = DBL_MIN;
     115           0 :         p->sigma_x = 0;
     116           0 :         p->sigma_x2 = 0;
     117           0 :         p->avg_sigma_x2 = 0;
     118           0 :         p->min_sigma_x2 = 0;
     119           0 :         p->max_sigma_x2 = 0;
     120           0 :         p->min_run = 0;
     121           0 :         p->max_run = 0;
     122           0 :         p->min_runs = 0;
     123           0 :         p->max_runs = 0;
     124           0 :         p->diff1_sum = 0;
     125           0 :         p->diff1_sum_x2 = 0;
     126           0 :         p->mask = 0;
     127           0 :         p->imask = 0xFFFFFFFFFFFFFFFF;
     128           0 :         p->min_count = 0;
     129           0 :         p->max_count = 0;
     130           0 :         p->nb_samples = 0;
     131             :     }
     132           0 : }
     133             : 
     134           0 : static int config_output(AVFilterLink *outlink)
     135             : {
     136           0 :     AudioStatsContext *s = outlink->src->priv;
     137             : 
     138           0 :     s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels);
     139           0 :     if (!s->chstats)
     140           0 :         return AVERROR(ENOMEM);
     141           0 :     s->nb_channels = outlink->channels;
     142           0 :     s->mult = exp((-1 / s->time_constant / outlink->sample_rate));
     143           0 :     s->tc_samples = 5 * s->time_constant * outlink->sample_rate + .5;
     144           0 :     s->nb_frames = 0;
     145           0 :     s->maxbitdepth = av_get_bytes_per_sample(outlink->format) * 8;
     146             : 
     147           0 :     reset_stats(s);
     148             : 
     149           0 :     return 0;
     150             : }
     151             : 
     152           0 : static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
     153             : {
     154           0 :     unsigned result = s->maxbitdepth;
     155             : 
     156           0 :     mask = mask & (~imask);
     157             : 
     158           0 :     for (; result && !(mask & 1); --result, mask >>= 1);
     159             : 
     160           0 :     depth->den = result;
     161           0 :     depth->num = 0;
     162             : 
     163           0 :     for (; result; --result, mask >>= 1)
     164           0 :         if (mask & 1)
     165           0 :             depth->num++;
     166           0 : }
     167             : 
     168           0 : static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d, double nd, int64_t i)
     169             : {
     170           0 :     if (d < p->min) {
     171           0 :         p->min = d;
     172           0 :         p->nmin = nd;
     173           0 :         p->min_run = 1;
     174           0 :         p->min_runs = 0;
     175           0 :         p->min_count = 1;
     176           0 :     } else if (d == p->min) {
     177           0 :         p->min_count++;
     178           0 :         p->min_run = d == p->last ? p->min_run + 1 : 1;
     179           0 :     } else if (p->last == p->min) {
     180           0 :         p->min_runs += p->min_run * p->min_run;
     181             :     }
     182             : 
     183           0 :     if (d > p->max) {
     184           0 :         p->max = d;
     185           0 :         p->nmax = nd;
     186           0 :         p->max_run = 1;
     187           0 :         p->max_runs = 0;
     188           0 :         p->max_count = 1;
     189           0 :     } else if (d == p->max) {
     190           0 :         p->max_count++;
     191           0 :         p->max_run = d == p->last ? p->max_run + 1 : 1;
     192           0 :     } else if (p->last == p->max) {
     193           0 :         p->max_runs += p->max_run * p->max_run;
     194             :     }
     195             : 
     196           0 :     p->sigma_x += nd;
     197           0 :     p->sigma_x2 += nd * nd;
     198           0 :     p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * nd * nd;
     199           0 :     p->min_diff = FFMIN(p->min_diff, fabs(d - p->last));
     200           0 :     p->max_diff = FFMAX(p->max_diff, fabs(d - p->last));
     201           0 :     p->diff1_sum += fabs(d - p->last);
     202           0 :     p->diff1_sum_x2 += (d - p->last) * (d - p->last);
     203           0 :     p->last = d;
     204           0 :     p->mask |= i;
     205           0 :     p->imask &= i;
     206             : 
     207           0 :     if (p->nb_samples >= s->tc_samples) {
     208           0 :         p->max_sigma_x2 = FFMAX(p->max_sigma_x2, p->avg_sigma_x2);
     209           0 :         p->min_sigma_x2 = FFMIN(p->min_sigma_x2, p->avg_sigma_x2);
     210             :     }
     211           0 :     p->nb_samples++;
     212           0 : }
     213             : 
     214           0 : static void set_meta(AVDictionary **metadata, int chan, const char *key,
     215             :                      const char *fmt, double val)
     216             : {
     217             :     uint8_t value[128];
     218             :     uint8_t key2[128];
     219             : 
     220           0 :     snprintf(value, sizeof(value), fmt, val);
     221           0 :     if (chan)
     222           0 :         snprintf(key2, sizeof(key2), "lavfi.astats.%d.%s", chan, key);
     223             :     else
     224           0 :         snprintf(key2, sizeof(key2), "lavfi.astats.%s", key);
     225           0 :     av_dict_set(metadata, key2, value, 0);
     226           0 : }
     227             : 
     228             : #define LINEAR_TO_DB(x) (log10(x) * 20)
     229             : 
     230           0 : static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
     231             : {
     232           0 :     uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0;
     233           0 :     double min_runs = 0, max_runs = 0,
     234           0 :            min = DBL_MAX, max = DBL_MIN, min_diff = DBL_MAX, max_diff = 0,
     235           0 :            nmin = DBL_MAX, nmax = DBL_MIN,
     236           0 :            max_sigma_x = 0,
     237           0 :            diff1_sum = 0,
     238           0 :            diff1_sum_x2 = 0,
     239           0 :            sigma_x = 0,
     240           0 :            sigma_x2 = 0,
     241           0 :            min_sigma_x2 = DBL_MAX,
     242           0 :            max_sigma_x2 = DBL_MIN;
     243             :     AVRational depth;
     244             :     int c;
     245             : 
     246           0 :     for (c = 0; c < s->nb_channels; c++) {
     247           0 :         ChannelStats *p = &s->chstats[c];
     248             : 
     249           0 :         if (p->nb_samples < s->tc_samples)
     250           0 :             p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
     251             : 
     252           0 :         min = FFMIN(min, p->min);
     253           0 :         max = FFMAX(max, p->max);
     254           0 :         nmin = FFMIN(nmin, p->nmin);
     255           0 :         nmax = FFMAX(nmax, p->nmax);
     256           0 :         min_diff = FFMIN(min_diff, p->min_diff);
     257           0 :         max_diff = FFMAX(max_diff, p->max_diff);
     258           0 :         diff1_sum += p->diff1_sum;
     259           0 :         diff1_sum_x2 += p->diff1_sum_x2;
     260           0 :         min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
     261           0 :         max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
     262           0 :         sigma_x += p->sigma_x;
     263           0 :         sigma_x2 += p->sigma_x2;
     264           0 :         min_count += p->min_count;
     265           0 :         max_count += p->max_count;
     266           0 :         min_runs += p->min_runs;
     267           0 :         max_runs += p->max_runs;
     268           0 :         mask |= p->mask;
     269           0 :         imask &= p->imask;
     270           0 :         nb_samples += p->nb_samples;
     271           0 :         if (fabs(p->sigma_x) > fabs(max_sigma_x))
     272           0 :             max_sigma_x = p->sigma_x;
     273             : 
     274           0 :         set_meta(metadata, c + 1, "DC_offset", "%f", p->sigma_x / p->nb_samples);
     275           0 :         set_meta(metadata, c + 1, "Min_level", "%f", p->min);
     276           0 :         set_meta(metadata, c + 1, "Max_level", "%f", p->max);
     277           0 :         set_meta(metadata, c + 1, "Min_difference", "%f", p->min_diff);
     278           0 :         set_meta(metadata, c + 1, "Max_difference", "%f", p->max_diff);
     279           0 :         set_meta(metadata, c + 1, "Mean_difference", "%f", p->diff1_sum / (p->nb_samples - 1));
     280           0 :         set_meta(metadata, c + 1, "RMS_difference", "%f", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
     281           0 :         set_meta(metadata, c + 1, "Peak_level", "%f", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
     282           0 :         set_meta(metadata, c + 1, "RMS_level", "%f", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
     283           0 :         set_meta(metadata, c + 1, "RMS_peak", "%f", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
     284           0 :         set_meta(metadata, c + 1, "RMS_trough", "%f", LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
     285           0 :         set_meta(metadata, c + 1, "Crest_factor", "%f", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
     286           0 :         set_meta(metadata, c + 1, "Flat_factor", "%f", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count)));
     287           0 :         set_meta(metadata, c + 1, "Peak_count", "%f", (float)(p->min_count + p->max_count));
     288           0 :         bit_depth(s, p->mask, p->imask, &depth);
     289           0 :         set_meta(metadata, c + 1, "Bit_depth", "%f", depth.num);
     290           0 :         set_meta(metadata, c + 1, "Bit_depth2", "%f", depth.den);
     291             :     }
     292             : 
     293           0 :     set_meta(metadata, 0, "Overall.DC_offset", "%f", max_sigma_x / (nb_samples / s->nb_channels));
     294           0 :     set_meta(metadata, 0, "Overall.Min_level", "%f", min);
     295           0 :     set_meta(metadata, 0, "Overall.Max_level", "%f", max);
     296           0 :     set_meta(metadata, 0, "Overall.Min_difference", "%f", min_diff);
     297           0 :     set_meta(metadata, 0, "Overall.Max_difference", "%f", max_diff);
     298           0 :     set_meta(metadata, 0, "Overall.Mean_difference", "%f", diff1_sum / (nb_samples - s->nb_channels));
     299           0 :     set_meta(metadata, 0, "Overall.RMS_difference", "%f", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
     300           0 :     set_meta(metadata, 0, "Overall.Peak_level", "%f", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
     301           0 :     set_meta(metadata, 0, "Overall.RMS_level", "%f", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
     302           0 :     set_meta(metadata, 0, "Overall.RMS_peak", "%f", LINEAR_TO_DB(sqrt(max_sigma_x2)));
     303           0 :     set_meta(metadata, 0, "Overall.RMS_trough", "%f", LINEAR_TO_DB(sqrt(min_sigma_x2)));
     304           0 :     set_meta(metadata, 0, "Overall.Flat_factor", "%f", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
     305           0 :     set_meta(metadata, 0, "Overall.Peak_count", "%f", (float)(min_count + max_count) / (double)s->nb_channels);
     306           0 :     bit_depth(s, mask, imask, &depth);
     307           0 :     set_meta(metadata, 0, "Overall.Bit_depth", "%f", depth.num);
     308           0 :     set_meta(metadata, 0, "Overall.Bit_depth2", "%f", depth.den);
     309           0 :     set_meta(metadata, 0, "Overall.Number_of_samples", "%f", nb_samples / s->nb_channels);
     310           0 : }
     311             : 
     312           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
     313             : {
     314           0 :     AudioStatsContext *s = inlink->dst->priv;
     315           0 :     AVDictionary **metadata = &buf->metadata;
     316           0 :     const int channels = s->nb_channels;
     317             :     int i, c;
     318             : 
     319           0 :     if (s->reset_count > 0) {
     320           0 :         if (s->nb_frames >= s->reset_count) {
     321           0 :             reset_stats(s);
     322           0 :             s->nb_frames = 0;
     323             :         }
     324           0 :         s->nb_frames++;
     325             :     }
     326             : 
     327           0 :     switch (inlink->format) {
     328           0 :     case AV_SAMPLE_FMT_DBLP:
     329           0 :         for (c = 0; c < channels; c++) {
     330           0 :             ChannelStats *p = &s->chstats[c];
     331           0 :             const double *src = (const double *)buf->extended_data[c];
     332             : 
     333           0 :             for (i = 0; i < buf->nb_samples; i++, src++)
     334           0 :                 update_stat(s, p, *src, *src, llrint(*src * (UINT64_C(1) << 63)));
     335             :         }
     336           0 :         break;
     337           0 :     case AV_SAMPLE_FMT_DBL: {
     338           0 :         const double *src = (const double *)buf->extended_data[0];
     339             : 
     340           0 :         for (i = 0; i < buf->nb_samples; i++) {
     341           0 :             for (c = 0; c < channels; c++, src++)
     342           0 :                 update_stat(s, &s->chstats[c], *src, *src, llrint(*src * (UINT64_C(1) << 63)));
     343             :         }}
     344           0 :         break;
     345           0 :     case AV_SAMPLE_FMT_FLTP:
     346           0 :         for (c = 0; c < channels; c++) {
     347           0 :             ChannelStats *p = &s->chstats[c];
     348           0 :             const float *src = (const float *)buf->extended_data[c];
     349             : 
     350           0 :             for (i = 0; i < buf->nb_samples; i++, src++)
     351           0 :                 update_stat(s, p, *src, *src, llrint(*src * (UINT64_C(1) << 31)));
     352             :         }
     353           0 :         break;
     354           0 :     case AV_SAMPLE_FMT_FLT: {
     355           0 :         const float *src = (const float *)buf->extended_data[0];
     356             : 
     357           0 :         for (i = 0; i < buf->nb_samples; i++) {
     358           0 :             for (c = 0; c < channels; c++, src++)
     359           0 :                 update_stat(s, &s->chstats[c], *src, *src, llrint(*src * (UINT64_C(1) << 31)));
     360             :         }}
     361           0 :         break;
     362           0 :     case AV_SAMPLE_FMT_S64P:
     363           0 :         for (c = 0; c < channels; c++) {
     364           0 :             ChannelStats *p = &s->chstats[c];
     365           0 :             const int64_t *src = (const int64_t *)buf->extended_data[c];
     366             : 
     367           0 :             for (i = 0; i < buf->nb_samples; i++, src++)
     368           0 :                 update_stat(s, p, *src, *src / (double)INT64_MAX, *src);
     369             :         }
     370           0 :         break;
     371           0 :     case AV_SAMPLE_FMT_S64: {
     372           0 :         const int64_t *src = (const int64_t *)buf->extended_data[0];
     373             : 
     374           0 :         for (i = 0; i < buf->nb_samples; i++) {
     375           0 :             for (c = 0; c < channels; c++, src++)
     376           0 :                 update_stat(s, &s->chstats[c], *src, *src / (double)INT64_MAX, *src);
     377             :         }}
     378           0 :         break;
     379           0 :     case AV_SAMPLE_FMT_S32P:
     380           0 :         for (c = 0; c < channels; c++) {
     381           0 :             ChannelStats *p = &s->chstats[c];
     382           0 :             const int32_t *src = (const int32_t *)buf->extended_data[c];
     383             : 
     384           0 :             for (i = 0; i < buf->nb_samples; i++, src++)
     385           0 :                 update_stat(s, p, *src, *src / (double)INT32_MAX, *src);
     386             :         }
     387           0 :         break;
     388           0 :     case AV_SAMPLE_FMT_S32: {
     389           0 :         const int32_t *src = (const int32_t *)buf->extended_data[0];
     390             : 
     391           0 :         for (i = 0; i < buf->nb_samples; i++) {
     392           0 :             for (c = 0; c < channels; c++, src++)
     393           0 :                 update_stat(s, &s->chstats[c], *src, *src / (double)INT32_MAX, *src);
     394             :         }}
     395           0 :         break;
     396           0 :     case AV_SAMPLE_FMT_S16P:
     397           0 :         for (c = 0; c < channels; c++) {
     398           0 :             ChannelStats *p = &s->chstats[c];
     399           0 :             const int16_t *src = (const int16_t *)buf->extended_data[c];
     400             : 
     401           0 :             for (i = 0; i < buf->nb_samples; i++, src++)
     402           0 :                 update_stat(s, p, *src, *src / (double)INT16_MAX, *src);
     403             :         }
     404           0 :         break;
     405           0 :     case AV_SAMPLE_FMT_S16: {
     406           0 :         const int16_t *src = (const int16_t *)buf->extended_data[0];
     407             : 
     408           0 :         for (i = 0; i < buf->nb_samples; i++) {
     409           0 :             for (c = 0; c < channels; c++, src++)
     410           0 :                 update_stat(s, &s->chstats[c], *src, *src / (double)INT16_MAX, *src);
     411             :         }}
     412           0 :         break;
     413             :     }
     414             : 
     415           0 :     if (s->metadata)
     416           0 :         set_metadata(s, metadata);
     417             : 
     418           0 :     return ff_filter_frame(inlink->dst->outputs[0], buf);
     419             : }
     420             : 
     421           0 : static void print_stats(AVFilterContext *ctx)
     422             : {
     423           0 :     AudioStatsContext *s = ctx->priv;
     424           0 :     uint64_t mask = 0, imask = 0xFFFFFFFFFFFFFFFF, min_count = 0, max_count = 0, nb_samples = 0;
     425           0 :     double min_runs = 0, max_runs = 0,
     426           0 :            min = DBL_MAX, max = DBL_MIN, min_diff = DBL_MAX, max_diff = 0,
     427           0 :            nmin = DBL_MAX, nmax = DBL_MIN,
     428           0 :            max_sigma_x = 0,
     429           0 :            diff1_sum_x2 = 0,
     430           0 :            diff1_sum = 0,
     431           0 :            sigma_x = 0,
     432           0 :            sigma_x2 = 0,
     433           0 :            min_sigma_x2 = DBL_MAX,
     434           0 :            max_sigma_x2 = DBL_MIN;
     435             :     AVRational depth;
     436             :     int c;
     437             : 
     438           0 :     for (c = 0; c < s->nb_channels; c++) {
     439           0 :         ChannelStats *p = &s->chstats[c];
     440             : 
     441           0 :         if (p->nb_samples < s->tc_samples)
     442           0 :             p->min_sigma_x2 = p->max_sigma_x2 = p->sigma_x2 / p->nb_samples;
     443             : 
     444           0 :         min = FFMIN(min, p->min);
     445           0 :         max = FFMAX(max, p->max);
     446           0 :         nmin = FFMIN(nmin, p->nmin);
     447           0 :         nmax = FFMAX(nmax, p->nmax);
     448           0 :         min_diff = FFMIN(min_diff, p->min_diff);
     449           0 :         max_diff = FFMAX(max_diff, p->max_diff);
     450           0 :         diff1_sum_x2 += p->diff1_sum_x2;
     451           0 :         diff1_sum += p->diff1_sum;
     452           0 :         min_sigma_x2 = FFMIN(min_sigma_x2, p->min_sigma_x2);
     453           0 :         max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2);
     454           0 :         sigma_x += p->sigma_x;
     455           0 :         sigma_x2 += p->sigma_x2;
     456           0 :         min_count += p->min_count;
     457           0 :         max_count += p->max_count;
     458           0 :         min_runs += p->min_runs;
     459           0 :         max_runs += p->max_runs;
     460           0 :         mask |= p->mask;
     461           0 :         imask &= p->imask;
     462           0 :         nb_samples += p->nb_samples;
     463           0 :         if (fabs(p->sigma_x) > fabs(max_sigma_x))
     464           0 :             max_sigma_x = p->sigma_x;
     465             : 
     466           0 :         av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
     467           0 :         av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
     468           0 :         av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
     469           0 :         av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
     470           0 :         av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", p->min_diff);
     471           0 :         av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", p->max_diff);
     472           0 :         av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", p->diff1_sum / (p->nb_samples - 1));
     473           0 :         av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
     474           0 :         av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
     475           0 :         av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
     476           0 :         av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
     477           0 :         if (p->min_sigma_x2 != 1)
     478           0 :             av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
     479           0 :         av_log(ctx, AV_LOG_INFO, "Crest factor: %f\n", p->sigma_x2 ? FFMAX(-p->nmin, p->nmax) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
     480           0 :         av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count)));
     481           0 :         av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
     482           0 :         bit_depth(s, p->mask, p->imask, &depth);
     483           0 :         av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
     484             :     }
     485             : 
     486           0 :     av_log(ctx, AV_LOG_INFO, "Overall\n");
     487           0 :     av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
     488           0 :     av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
     489           0 :     av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
     490           0 :     av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", min_diff);
     491           0 :     av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", max_diff);
     492           0 :     av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", diff1_sum / (nb_samples - s->nb_channels));
     493           0 :     av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
     494           0 :     av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
     495           0 :     av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
     496           0 :     av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
     497           0 :     if (min_sigma_x2 != 1)
     498           0 :         av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
     499           0 :     av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
     500           0 :     av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
     501           0 :     bit_depth(s, mask, imask, &depth);
     502           0 :     av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
     503           0 :     av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
     504           0 : }
     505             : 
     506           0 : static av_cold void uninit(AVFilterContext *ctx)
     507             : {
     508           0 :     AudioStatsContext *s = ctx->priv;
     509             : 
     510           0 :     if (s->nb_channels)
     511           0 :         print_stats(ctx);
     512           0 :     av_freep(&s->chstats);
     513           0 : }
     514             : 
     515             : static const AVFilterPad astats_inputs[] = {
     516             :     {
     517             :         .name         = "default",
     518             :         .type         = AVMEDIA_TYPE_AUDIO,
     519             :         .filter_frame = filter_frame,
     520             :     },
     521             :     { NULL }
     522             : };
     523             : 
     524             : static const AVFilterPad astats_outputs[] = {
     525             :     {
     526             :         .name         = "default",
     527             :         .type         = AVMEDIA_TYPE_AUDIO,
     528             :         .config_props = config_output,
     529             :     },
     530             :     { NULL }
     531             : };
     532             : 
     533             : AVFilter ff_af_astats = {
     534             :     .name          = "astats",
     535             :     .description   = NULL_IF_CONFIG_SMALL("Show time domain statistics about audio frames."),
     536             :     .query_formats = query_formats,
     537             :     .priv_size     = sizeof(AudioStatsContext),
     538             :     .priv_class    = &astats_class,
     539             :     .uninit        = uninit,
     540             :     .inputs        = astats_inputs,
     541             :     .outputs       = astats_outputs,
     542             : };

Generated by: LCOV version 1.13