]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/af_astats.c
lavfi/sidedata: add missed frame side data type
[ffmpeg] / libavfilter / af_astats.c
index 2922da9f4486acec46df8ec18834bfb55795bee5..92368793c276ee4083ebc110bd151697a2eadcec 100644 (file)
 #include "avfilter.h"
 #include "internal.h"
 
+#define MEASURE_ALL                     UINT_MAX
+#define MEASURE_NONE                           0
+
+#define MEASURE_DC_OFFSET               (1 <<  0)
+#define MEASURE_MIN_LEVEL               (1 <<  1)
+#define MEASURE_MAX_LEVEL               (1 <<  2)
+#define MEASURE_MIN_DIFFERENCE          (1 <<  3)
+#define MEASURE_MAX_DIFFERENCE          (1 <<  4)
+#define MEASURE_MEAN_DIFFERENCE         (1 <<  5)
+#define MEASURE_RMS_DIFFERENCE          (1 <<  6)
+#define MEASURE_PEAK_LEVEL              (1 <<  7)
+#define MEASURE_RMS_LEVEL               (1 <<  8)
+#define MEASURE_RMS_PEAK                (1 <<  9)
+#define MEASURE_RMS_TROUGH              (1 << 10)
+#define MEASURE_CREST_FACTOR            (1 << 11)
+#define MEASURE_FLAT_FACTOR             (1 << 12)
+#define MEASURE_PEAK_COUNT              (1 << 13)
+#define MEASURE_BIT_DEPTH               (1 << 14)
+#define MEASURE_DYNAMIC_RANGE           (1 << 15)
+#define MEASURE_ZERO_CROSSINGS          (1 << 16)
+#define MEASURE_ZERO_CROSSINGS_RATE     (1 << 17)
+#define MEASURE_NUMBER_OF_SAMPLES       (1 << 18)
+
+#define MEASURE_MINMAXPEAK              (MEASURE_MIN_LEVEL | MEASURE_MAX_LEVEL | MEASURE_PEAK_LEVEL)
+
 typedef struct ChannelStats {
     double last;
+    double last_non_zero;
     double min_non_zero;
     double sigma_x, sigma_x2;
     double avg_sigma_x2, min_sigma_x2, max_sigma_x2;
@@ -40,6 +66,7 @@ typedef struct ChannelStats {
     double diff1_sum_x2;
     uint64_t mask, imask;
     uint64_t min_count, max_count;
+    uint64_t zero_runs;
     uint64_t nb_samples;
 } ChannelStats;
 
@@ -54,6 +81,8 @@ typedef struct AudioStatsContext {
     int reset_count;
     int nb_frames;
     int maxbitdepth;
+    int measure_perchannel;
+    int measure_overall;
 } AudioStatsContext;
 
 #define OFFSET(x) offsetof(AudioStatsContext, x)
@@ -63,6 +92,29 @@ static const AVOption astats_options[] = {
     { "length", "set the window length", OFFSET(time_constant), AV_OPT_TYPE_DOUBLE, {.dbl=.05}, .01, 10, FLAGS },
     { "metadata", "inject metadata in the filtergraph", OFFSET(metadata), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
     { "reset", "recalculate stats after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
+    { "measure_perchannel", "only measure_perchannel these per-channel statistics", OFFSET(measure_perchannel), AV_OPT_TYPE_FLAGS, {.i64=MEASURE_ALL}, 0, UINT_MAX, FLAGS, "measure" },
+      { "none"                      , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NONE                }, 0, 0, FLAGS, "measure" },
+      { "all"                       , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ALL                 }, 0, 0, FLAGS, "measure" },
+      { "DC_offset"                 , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_DC_OFFSET           }, 0, 0, FLAGS, "measure" },
+      { "Min_level"                 , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MIN_LEVEL           }, 0, 0, FLAGS, "measure" },
+      { "Max_level"                 , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MAX_LEVEL           }, 0, 0, FLAGS, "measure" },
+      { "Min_difference"            , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MIN_DIFFERENCE      }, 0, 0, FLAGS, "measure" },
+      { "Max_difference"            , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MAX_DIFFERENCE      }, 0, 0, FLAGS, "measure" },
+      { "Mean_difference"           , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_MEAN_DIFFERENCE     }, 0, 0, FLAGS, "measure" },
+      { "RMS_difference"            , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_DIFFERENCE      }, 0, 0, FLAGS, "measure" },
+      { "Peak_level"                , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_PEAK_LEVEL          }, 0, 0, FLAGS, "measure" },
+      { "RMS_level"                 , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_LEVEL           }, 0, 0, FLAGS, "measure" },
+      { "RMS_peak"                  , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_PEAK            }, 0, 0, FLAGS, "measure" },
+      { "RMS_trough"                , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_RMS_TROUGH          }, 0, 0, FLAGS, "measure" },
+      { "Crest_factor"              , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_CREST_FACTOR        }, 0, 0, FLAGS, "measure" },
+      { "Flat_factor"               , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_FLAT_FACTOR         }, 0, 0, FLAGS, "measure" },
+      { "Peak_count"                , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_PEAK_COUNT          }, 0, 0, FLAGS, "measure" },
+      { "Bit_depth"                 , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_BIT_DEPTH           }, 0, 0, FLAGS, "measure" },
+      { "Dynamic_range"             , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_DYNAMIC_RANGE       }, 0, 0, FLAGS, "measure" },
+      { "Zero_crossings"            , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ZERO_CROSSINGS      }, 0, 0, FLAGS, "measure" },
+      { "Zero_crossings_rate"       , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ZERO_CROSSINGS_RATE }, 0, 0, FLAGS, "measure" },
+      { "Number_of_samples"         , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_SAMPLES   }, 0, 0, FLAGS, "measure" },
+    { "measure_overall", "only measure_perchannel these overall statistics", OFFSET(measure_overall), AV_OPT_TYPE_FLAGS, {.i64=MEASURE_ALL}, 0, UINT_MAX, FLAGS, "measure" },
     { NULL }
 };
 
@@ -127,6 +179,7 @@ static void reset_stats(AudioStatsContext *s)
         p->imask = 0xFFFFFFFFFFFFFFFF;
         p->min_count = 0;
         p->max_count = 0;
+        p->zero_runs = 0;
         p->nb_samples = 0;
     }
 }
@@ -165,6 +218,14 @@ static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRat
             depth->num++;
 }
 
+static inline void update_minmax(AudioStatsContext *s, ChannelStats *p, double d)
+{
+    if (d < p->min)
+        p->min = d;
+    if (d > p->max)
+        p->max = d;
+}
+
 static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d, double nd, int64_t i)
 {
     if (d < p->min) {
@@ -196,6 +257,11 @@ static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d,
         p->max_runs += p->max_run * p->max_run;
     }
 
+    if (d != 0) {
+        p->zero_runs += FFSIGN(d) != FFSIGN(p->last_non_zero);
+        p->last_non_zero = d;
+    }
+
     p->sigma_x += nd;
     p->sigma_x2 += nd * nd;
     p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * nd * nd;
@@ -274,51 +340,116 @@ static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
         if (fabs(p->sigma_x) > fabs(max_sigma_x))
             max_sigma_x = p->sigma_x;
 
-        set_meta(metadata, c + 1, "DC_offset", "%f", p->sigma_x / p->nb_samples);
-        set_meta(metadata, c + 1, "Min_level", "%f", p->min);
-        set_meta(metadata, c + 1, "Max_level", "%f", p->max);
-        set_meta(metadata, c + 1, "Min_difference", "%f", p->min_diff);
-        set_meta(metadata, c + 1, "Max_difference", "%f", p->max_diff);
-        set_meta(metadata, c + 1, "Mean_difference", "%f", p->diff1_sum / (p->nb_samples - 1));
-        set_meta(metadata, c + 1, "RMS_difference", "%f", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
-        set_meta(metadata, c + 1, "Peak_level", "%f", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
-        set_meta(metadata, c + 1, "RMS_level", "%f", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
-        set_meta(metadata, c + 1, "RMS_peak", "%f", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
-        set_meta(metadata, c + 1, "RMS_trough", "%f", LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
-        set_meta(metadata, c + 1, "Crest_factor", "%f", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
-        set_meta(metadata, c + 1, "Flat_factor", "%f", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count)));
-        set_meta(metadata, c + 1, "Peak_count", "%f", (float)(p->min_count + p->max_count));
-        bit_depth(s, p->mask, p->imask, &depth);
-        set_meta(metadata, c + 1, "Bit_depth", "%f", depth.num);
-        set_meta(metadata, c + 1, "Bit_depth2", "%f", depth.den);
-        set_meta(metadata, c + 1, "Dynamic_range", "%f", LINEAR_TO_DB(2 * FFMAX(FFABS(p->min), FFABS(p->max))/ p->min_non_zero));
+        if (s->measure_perchannel & MEASURE_DC_OFFSET)
+            set_meta(metadata, c + 1, "DC_offset", "%f", p->sigma_x / p->nb_samples);
+        if (s->measure_perchannel & MEASURE_MIN_LEVEL)
+            set_meta(metadata, c + 1, "Min_level", "%f", p->min);
+        if (s->measure_perchannel & MEASURE_MAX_LEVEL)
+            set_meta(metadata, c + 1, "Max_level", "%f", p->max);
+        if (s->measure_perchannel & MEASURE_MIN_DIFFERENCE)
+            set_meta(metadata, c + 1, "Min_difference", "%f", p->min_diff);
+        if (s->measure_perchannel & MEASURE_MAX_DIFFERENCE)
+            set_meta(metadata, c + 1, "Max_difference", "%f", p->max_diff);
+        if (s->measure_perchannel & MEASURE_MEAN_DIFFERENCE)
+            set_meta(metadata, c + 1, "Mean_difference", "%f", p->diff1_sum / (p->nb_samples - 1));
+        if (s->measure_perchannel & MEASURE_RMS_DIFFERENCE)
+            set_meta(metadata, c + 1, "RMS_difference", "%f", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
+        if (s->measure_perchannel & MEASURE_PEAK_LEVEL)
+            set_meta(metadata, c + 1, "Peak_level", "%f", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
+        if (s->measure_perchannel & MEASURE_RMS_LEVEL)
+            set_meta(metadata, c + 1, "RMS_level", "%f", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
+        if (s->measure_perchannel & MEASURE_RMS_PEAK)
+            set_meta(metadata, c + 1, "RMS_peak", "%f", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
+        if (s->measure_perchannel & MEASURE_RMS_TROUGH)
+            set_meta(metadata, c + 1, "RMS_trough", "%f", LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
+        if (s->measure_perchannel & MEASURE_CREST_FACTOR)
+            set_meta(metadata, c + 1, "Crest_factor", "%f", p->sigma_x2 ? FFMAX(-p->min, p->max) / sqrt(p->sigma_x2 / p->nb_samples) : 1);
+        if (s->measure_perchannel & MEASURE_FLAT_FACTOR)
+            set_meta(metadata, c + 1, "Flat_factor", "%f", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count)));
+        if (s->measure_perchannel & MEASURE_PEAK_COUNT)
+            set_meta(metadata, c + 1, "Peak_count", "%f", (float)(p->min_count + p->max_count));
+        if (s->measure_perchannel & MEASURE_BIT_DEPTH) {
+            bit_depth(s, p->mask, p->imask, &depth);
+            set_meta(metadata, c + 1, "Bit_depth", "%f", depth.num);
+            set_meta(metadata, c + 1, "Bit_depth2", "%f", depth.den);
+        }
+        if (s->measure_perchannel & MEASURE_DYNAMIC_RANGE)
+            set_meta(metadata, c + 1, "Dynamic_range", "%f", LINEAR_TO_DB(2 * FFMAX(FFABS(p->min), FFABS(p->max))/ p->min_non_zero));
+        if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS)
+            set_meta(metadata, c + 1, "Zero_crossings", "%f", p->zero_runs);
+        if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS_RATE)
+            set_meta(metadata, c + 1, "Zero_crossings_rate", "%f", p->zero_runs/(double)p->nb_samples);
     }
 
-    set_meta(metadata, 0, "Overall.DC_offset", "%f", max_sigma_x / (nb_samples / s->nb_channels));
-    set_meta(metadata, 0, "Overall.Min_level", "%f", min);
-    set_meta(metadata, 0, "Overall.Max_level", "%f", max);
-    set_meta(metadata, 0, "Overall.Min_difference", "%f", min_diff);
-    set_meta(metadata, 0, "Overall.Max_difference", "%f", max_diff);
-    set_meta(metadata, 0, "Overall.Mean_difference", "%f", diff1_sum / (nb_samples - s->nb_channels));
-    set_meta(metadata, 0, "Overall.RMS_difference", "%f", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
-    set_meta(metadata, 0, "Overall.Peak_level", "%f", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
-    set_meta(metadata, 0, "Overall.RMS_level", "%f", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
-    set_meta(metadata, 0, "Overall.RMS_peak", "%f", LINEAR_TO_DB(sqrt(max_sigma_x2)));
-    set_meta(metadata, 0, "Overall.RMS_trough", "%f", LINEAR_TO_DB(sqrt(min_sigma_x2)));
-    set_meta(metadata, 0, "Overall.Flat_factor", "%f", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
-    set_meta(metadata, 0, "Overall.Peak_count", "%f", (float)(min_count + max_count) / (double)s->nb_channels);
-    bit_depth(s, mask, imask, &depth);
-    set_meta(metadata, 0, "Overall.Bit_depth", "%f", depth.num);
-    set_meta(metadata, 0, "Overall.Bit_depth2", "%f", depth.den);
-    set_meta(metadata, 0, "Overall.Number_of_samples", "%f", nb_samples / s->nb_channels);
+    if (s->measure_overall & MEASURE_DC_OFFSET)
+        set_meta(metadata, 0, "Overall.DC_offset", "%f", max_sigma_x / (nb_samples / s->nb_channels));
+    if (s->measure_overall & MEASURE_MIN_LEVEL)
+        set_meta(metadata, 0, "Overall.Min_level", "%f", min);
+    if (s->measure_overall & MEASURE_MAX_LEVEL)
+        set_meta(metadata, 0, "Overall.Max_level", "%f", max);
+    if (s->measure_overall & MEASURE_MIN_DIFFERENCE)
+        set_meta(metadata, 0, "Overall.Min_difference", "%f", min_diff);
+    if (s->measure_overall & MEASURE_MAX_DIFFERENCE)
+        set_meta(metadata, 0, "Overall.Max_difference", "%f", max_diff);
+    if (s->measure_overall & MEASURE_MEAN_DIFFERENCE)
+        set_meta(metadata, 0, "Overall.Mean_difference", "%f", diff1_sum / (nb_samples - s->nb_channels));
+    if (s->measure_overall & MEASURE_RMS_DIFFERENCE)
+        set_meta(metadata, 0, "Overall.RMS_difference", "%f", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
+    if (s->measure_overall & MEASURE_PEAK_LEVEL)
+        set_meta(metadata, 0, "Overall.Peak_level", "%f", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
+    if (s->measure_overall & MEASURE_RMS_LEVEL)
+        set_meta(metadata, 0, "Overall.RMS_level", "%f", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
+    if (s->measure_overall & MEASURE_RMS_PEAK)
+        set_meta(metadata, 0, "Overall.RMS_peak", "%f", LINEAR_TO_DB(sqrt(max_sigma_x2)));
+    if (s->measure_overall & MEASURE_RMS_TROUGH)
+        set_meta(metadata, 0, "Overall.RMS_trough", "%f", LINEAR_TO_DB(sqrt(min_sigma_x2)));
+    if (s->measure_overall & MEASURE_FLAT_FACTOR)
+        set_meta(metadata, 0, "Overall.Flat_factor", "%f", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
+    if (s->measure_overall & MEASURE_PEAK_COUNT)
+        set_meta(metadata, 0, "Overall.Peak_count", "%f", (float)(min_count + max_count) / (double)s->nb_channels);
+    if (s->measure_overall & MEASURE_BIT_DEPTH) {
+        bit_depth(s, mask, imask, &depth);
+        set_meta(metadata, 0, "Overall.Bit_depth", "%f", depth.num);
+        set_meta(metadata, 0, "Overall.Bit_depth2", "%f", depth.den);
+    }
+    if (s->measure_overall & MEASURE_NUMBER_OF_SAMPLES)
+        set_meta(metadata, 0, "Overall.Number_of_samples", "%f", nb_samples / s->nb_channels);
 }
 
+#define UPDATE_STATS_P(type, update_func, channel_func)                         \
+    for (int c = 0; c < channels; c++) {                                        \
+        ChannelStats *p = &s->chstats[c];                                       \
+        const type *src = (const type *)data[c];                                \
+        const type * const srcend = src + samples;                              \
+        for (; src < srcend; src++)                                             \
+            update_func;                                                        \
+        channel_func;                                                           \
+    }
+
+#define UPDATE_STATS_I(type, update_func, channel_func)                         \
+    for (int c = 0; c < channels; c++) {                                        \
+        ChannelStats *p = &s->chstats[c];                                       \
+        const type *src = (const type *)data[0];                                \
+        const type * const srcend = src + samples * channels;                   \
+        for (src += c; src < srcend; src += channels)                           \
+            update_func;                                                        \
+        channel_func;                                                           \
+    }
+
+#define UPDATE_STATS(planar, type, sample, normalizer_suffix, int_sample) \
+    if ((s->measure_overall | s->measure_perchannel) & ~MEASURE_MINMAXPEAK) {                          \
+        UPDATE_STATS_##planar(type, update_stat(s, p, sample, sample normalizer_suffix, int_sample), );    \
+    } else {                                                                                           \
+        UPDATE_STATS_##planar(type, update_minmax(s, p, sample), p->nmin = p->min normalizer_suffix; p->nmax = p->max normalizer_suffix;); \
+    }
+
 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
 {
     AudioStatsContext *s = inlink->dst->priv;
     AVDictionary **metadata = &buf->metadata;
     const int channels = s->nb_channels;
-    int i, c;
+    const int samples = buf->nb_samples;
+    const uint8_t * const * const data = (const uint8_t * const *)buf->extended_data;
 
     if (s->reset_count > 0) {
         if (s->nb_frames >= s->reset_count) {
@@ -330,89 +461,34 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
 
     switch (inlink->format) {
     case AV_SAMPLE_FMT_DBLP:
-        for (c = 0; c < channels; c++) {
-            ChannelStats *p = &s->chstats[c];
-            const double *src = (const double *)buf->extended_data[c];
-
-            for (i = 0; i < buf->nb_samples; i++, src++)
-                update_stat(s, p, *src, *src, llrint(*src * (UINT64_C(1) << 63)));
-        }
+        UPDATE_STATS(P, double, *src, , llrint(*src * (UINT64_C(1) << 63)));
         break;
-    case AV_SAMPLE_FMT_DBL: {
-        const double *src = (const double *)buf->extended_data[0];
-
-        for (i = 0; i < buf->nb_samples; i++) {
-            for (c = 0; c < channels; c++, src++)
-                update_stat(s, &s->chstats[c], *src, *src, llrint(*src * (UINT64_C(1) << 63)));
-        }}
+    case AV_SAMPLE_FMT_DBL:
+        UPDATE_STATS(I, double, *src, , llrint(*src * (UINT64_C(1) << 63)));
         break;
     case AV_SAMPLE_FMT_FLTP:
-        for (c = 0; c < channels; c++) {
-            ChannelStats *p = &s->chstats[c];
-            const float *src = (const float *)buf->extended_data[c];
-
-            for (i = 0; i < buf->nb_samples; i++, src++)
-                update_stat(s, p, *src, *src, llrint(*src * (UINT64_C(1) << 31)));
-        }
+        UPDATE_STATS(P, float, *src, , llrint(*src * (UINT64_C(1) << 31)));
         break;
-    case AV_SAMPLE_FMT_FLT: {
-        const float *src = (const float *)buf->extended_data[0];
-
-        for (i = 0; i < buf->nb_samples; i++) {
-            for (c = 0; c < channels; c++, src++)
-                update_stat(s, &s->chstats[c], *src, *src, llrint(*src * (UINT64_C(1) << 31)));
-        }}
+    case AV_SAMPLE_FMT_FLT:
+        UPDATE_STATS(I, float, *src, , llrint(*src * (UINT64_C(1) << 31)));
         break;
     case AV_SAMPLE_FMT_S64P:
-        for (c = 0; c < channels; c++) {
-            ChannelStats *p = &s->chstats[c];
-            const int64_t *src = (const int64_t *)buf->extended_data[c];
-
-            for (i = 0; i < buf->nb_samples; i++, src++)
-                update_stat(s, p, *src, *src / (double)INT64_MAX, *src);
-        }
+        UPDATE_STATS(P, int64_t, *src, / (double)INT64_MAX, *src);
         break;
-    case AV_SAMPLE_FMT_S64: {
-        const int64_t *src = (const int64_t *)buf->extended_data[0];
-
-        for (i = 0; i < buf->nb_samples; i++) {
-            for (c = 0; c < channels; c++, src++)
-                update_stat(s, &s->chstats[c], *src, *src / (double)INT64_MAX, *src);
-        }}
+    case AV_SAMPLE_FMT_S64:
+        UPDATE_STATS(I, int64_t, *src, / (double)INT64_MAX, *src);
         break;
     case AV_SAMPLE_FMT_S32P:
-        for (c = 0; c < channels; c++) {
-            ChannelStats *p = &s->chstats[c];
-            const int32_t *src = (const int32_t *)buf->extended_data[c];
-
-            for (i = 0; i < buf->nb_samples; i++, src++)
-                update_stat(s, p, *src, *src / (double)INT32_MAX, *src);
-        }
+        UPDATE_STATS(P, int32_t, *src, / (double)INT32_MAX, *src);
         break;
-    case AV_SAMPLE_FMT_S32: {
-        const int32_t *src = (const int32_t *)buf->extended_data[0];
-
-        for (i = 0; i < buf->nb_samples; i++) {
-            for (c = 0; c < channels; c++, src++)
-                update_stat(s, &s->chstats[c], *src, *src / (double)INT32_MAX, *src);
-        }}
+    case AV_SAMPLE_FMT_S32:
+        UPDATE_STATS(I, int32_t, *src, / (double)INT32_MAX, *src);
         break;
     case AV_SAMPLE_FMT_S16P:
-        for (c = 0; c < channels; c++) {
-            ChannelStats *p = &s->chstats[c];
-            const int16_t *src = (const int16_t *)buf->extended_data[c];
-
-            for (i = 0; i < buf->nb_samples; i++, src++)
-                update_stat(s, p, *src, *src / (double)INT16_MAX, *src);
-        }
+        UPDATE_STATS(P, int16_t, *src, / (double)INT16_MAX, *src);
         break;
-    case AV_SAMPLE_FMT_S16: {
-        const int16_t *src = (const int16_t *)buf->extended_data[0];
-
-        for (i = 0; i < buf->nb_samples; i++) {
-            for (c = 0; c < channels; c++, src++)
-                update_stat(s, &s->chstats[c], *src, *src / (double)INT16_MAX, *src);
-        }}
+    case AV_SAMPLE_FMT_S16:
+        UPDATE_STATS(I, int16_t, *src, / (double)INT16_MAX, *src);
         break;
     }
 
@@ -468,44 +544,81 @@ static void print_stats(AVFilterContext *ctx)
             max_sigma_x = p->sigma_x;
 
         av_log(ctx, AV_LOG_INFO, "Channel: %d\n", c + 1);
-        av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
-        av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
-        av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
-        av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", p->min_diff);
-        av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", p->max_diff);
-        av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", p->diff1_sum / (p->nb_samples - 1));
-        av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
-        av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
-        av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
-        av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
-        if (p->min_sigma_x2 != 1)
-            av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
-        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);
-        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)));
-        av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
-        bit_depth(s, p->mask, p->imask, &depth);
-        av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
-        av_log(ctx, AV_LOG_INFO, "Dynamic range: %f\n", LINEAR_TO_DB(2 * FFMAX(FFABS(p->min), FFABS(p->max))/ p->min_non_zero));
+        if (s->measure_perchannel & MEASURE_DC_OFFSET)
+            av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", p->sigma_x / p->nb_samples);
+        if (s->measure_perchannel & MEASURE_MIN_LEVEL)
+            av_log(ctx, AV_LOG_INFO, "Min level: %f\n", p->min);
+        if (s->measure_perchannel & MEASURE_MAX_LEVEL)
+            av_log(ctx, AV_LOG_INFO, "Max level: %f\n", p->max);
+        if (s->measure_perchannel & MEASURE_MIN_DIFFERENCE)
+            av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", p->min_diff);
+        if (s->measure_perchannel & MEASURE_MAX_DIFFERENCE)
+            av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", p->max_diff);
+        if (s->measure_perchannel & MEASURE_MEAN_DIFFERENCE)
+            av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", p->diff1_sum / (p->nb_samples - 1));
+        if (s->measure_perchannel & MEASURE_RMS_DIFFERENCE)
+            av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(p->diff1_sum_x2 / (p->nb_samples - 1)));
+        if (s->measure_perchannel & MEASURE_PEAK_LEVEL)
+            av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-p->nmin, p->nmax)));
+        if (s->measure_perchannel & MEASURE_RMS_LEVEL)
+            av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(p->sigma_x2 / p->nb_samples)));
+        if (s->measure_perchannel & MEASURE_RMS_PEAK)
+            av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(p->max_sigma_x2)));
+        if (s->measure_perchannel & MEASURE_RMS_TROUGH)
+            if (p->min_sigma_x2 != 1)
+                av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n",LINEAR_TO_DB(sqrt(p->min_sigma_x2)));
+        if (s->measure_perchannel & MEASURE_CREST_FACTOR)
+            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);
+        if (s->measure_perchannel & MEASURE_FLAT_FACTOR)
+            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)));
+        if (s->measure_perchannel & MEASURE_PEAK_COUNT)
+            av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count);
+        if (s->measure_perchannel & MEASURE_BIT_DEPTH) {
+            bit_depth(s, p->mask, p->imask, &depth);
+            av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
+        }
+        if (s->measure_perchannel & MEASURE_DYNAMIC_RANGE)
+            av_log(ctx, AV_LOG_INFO, "Dynamic range: %f\n", LINEAR_TO_DB(2 * FFMAX(FFABS(p->min), FFABS(p->max))/ p->min_non_zero));
+        if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS)
+            av_log(ctx, AV_LOG_INFO, "Zero crossings: %"PRId64"\n", p->zero_runs);
+        if (s->measure_perchannel & MEASURE_ZERO_CROSSINGS_RATE)
+            av_log(ctx, AV_LOG_INFO, "Zero crossings rate: %f\n", p->zero_runs/(double)p->nb_samples);
     }
 
     av_log(ctx, AV_LOG_INFO, "Overall\n");
-    av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
-    av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
-    av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
-    av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", min_diff);
-    av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", max_diff);
-    av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", diff1_sum / (nb_samples - s->nb_channels));
-    av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
-    av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
-    av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
-    av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
-    if (min_sigma_x2 != 1)
-        av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
-    av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
-    av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
-    bit_depth(s, mask, imask, &depth);
-    av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
-    av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
+    if (s->measure_overall & MEASURE_DC_OFFSET)
+        av_log(ctx, AV_LOG_INFO, "DC offset: %f\n", max_sigma_x / (nb_samples / s->nb_channels));
+    if (s->measure_overall & MEASURE_MIN_LEVEL)
+        av_log(ctx, AV_LOG_INFO, "Min level: %f\n", min);
+    if (s->measure_overall & MEASURE_MAX_LEVEL)
+        av_log(ctx, AV_LOG_INFO, "Max level: %f\n", max);
+    if (s->measure_overall & MEASURE_MIN_DIFFERENCE)
+        av_log(ctx, AV_LOG_INFO, "Min difference: %f\n", min_diff);
+    if (s->measure_overall & MEASURE_MAX_DIFFERENCE)
+        av_log(ctx, AV_LOG_INFO, "Max difference: %f\n", max_diff);
+    if (s->measure_overall & MEASURE_MEAN_DIFFERENCE)
+        av_log(ctx, AV_LOG_INFO, "Mean difference: %f\n", diff1_sum / (nb_samples - s->nb_channels));
+    if (s->measure_overall & MEASURE_RMS_DIFFERENCE)
+        av_log(ctx, AV_LOG_INFO, "RMS difference: %f\n", sqrt(diff1_sum_x2 / (nb_samples - s->nb_channels)));
+    if (s->measure_overall & MEASURE_PEAK_LEVEL)
+        av_log(ctx, AV_LOG_INFO, "Peak level dB: %f\n", LINEAR_TO_DB(FFMAX(-nmin, nmax)));
+    if (s->measure_overall & MEASURE_RMS_LEVEL)
+        av_log(ctx, AV_LOG_INFO, "RMS level dB: %f\n", LINEAR_TO_DB(sqrt(sigma_x2 / nb_samples)));
+    if (s->measure_overall & MEASURE_RMS_PEAK)
+        av_log(ctx, AV_LOG_INFO, "RMS peak dB: %f\n", LINEAR_TO_DB(sqrt(max_sigma_x2)));
+    if (s->measure_overall & MEASURE_RMS_TROUGH)
+        if (min_sigma_x2 != 1)
+            av_log(ctx, AV_LOG_INFO, "RMS trough dB: %f\n", LINEAR_TO_DB(sqrt(min_sigma_x2)));
+    if (s->measure_overall & MEASURE_FLAT_FACTOR)
+        av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count)));
+    if (s->measure_overall & MEASURE_PEAK_COUNT)
+        av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels);
+    if (s->measure_overall & MEASURE_BIT_DEPTH) {
+        bit_depth(s, mask, imask, &depth);
+        av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
+    }
+    if (s->measure_overall & MEASURE_NUMBER_OF_SAMPLES)
+        av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
 }
 
 static av_cold void uninit(AVFilterContext *ctx)