[FFmpeg-devel] [PATCH] avfilter/af_astats: calculate audio bit-depth

Paul B Mahol onemda at gmail.com
Wed Jul 15 00:01:48 CEST 2015


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 libavfilter/af_astats.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/libavfilter/af_astats.c b/libavfilter/af_astats.c
index a89ac6d..21cec8c 100644
--- a/libavfilter/af_astats.c
+++ b/libavfilter/af_astats.c
@@ -33,6 +33,7 @@ typedef struct ChannelStats {
     double min, max;
     double min_run, max_run;
     double min_runs, max_runs;
+    uint64_t mask;
     uint64_t min_count, max_count;
     uint64_t nb_samples;
 } ChannelStats;
@@ -121,6 +122,15 @@ static int config_output(AVFilterLink *outlink)
     return 0;
 }
 
+static unsigned bit_depth(uint64_t mask)
+{
+    unsigned result = 64;
+
+    for (; result && !(mask & 1); --result, mask >>= 1);
+
+    return result;
+}
+
 static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d)
 {
     if (d < p->min) {
@@ -151,6 +161,7 @@ static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d)
     p->sigma_x2 += d * d;
     p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * d * d;
     p->last = d;
+    p->mask |= llrint(d * (1LLU<<63));
 
     if (p->nb_samples >= s->tc_samples) {
         p->max_sigma_x2 = FFMAX(p->max_sigma_x2, p->avg_sigma_x2);
@@ -177,7 +188,7 @@ static void set_meta(AVDictionary **metadata, int chan, const char *key,
 
 static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
 {
-    uint64_t min_count = 0, max_count = 0, nb_samples = 0;
+    uint64_t mask = 0, min_count = 0, max_count = 0, nb_samples = 0;
     double min_runs = 0, max_runs = 0,
            min = DBL_MAX, max = DBL_MIN,
            max_sigma_x = 0,
@@ -203,6 +214,7 @@ static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
         max_count += p->max_count;
         min_runs += p->min_runs;
         max_runs += p->max_runs;
+        mask |= p->mask;
         nb_samples += p->nb_samples;
         if (fabs(p->sigma_x) > fabs(max_sigma_x))
             max_sigma_x = p->sigma_x;
@@ -217,6 +229,7 @@ static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
         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));
+        set_meta(metadata, c + 1, "Bit-depth", "%f", bit_depth(p->mask));
     }
 
     set_meta(metadata, 0, "Overall.DC_offset", "%f", max_sigma_x / (nb_samples / s->nb_channels));
@@ -228,6 +241,7 @@ static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
     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);
+    set_meta(metadata, 0, "Overall.Bit-depth", "%f", bit_depth(mask));
     set_meta(metadata, 0, "Overall.Number_of_samples", "%f", nb_samples / s->nb_channels);
 }
 
@@ -276,7 +290,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
 static void print_stats(AVFilterContext *ctx)
 {
     AudioStatsContext *s = ctx->priv;
-    uint64_t min_count = 0, max_count = 0, nb_samples = 0;
+    uint64_t mask = 0, min_count = 0, max_count = 0, nb_samples = 0;
     double min_runs = 0, max_runs = 0,
            min = DBL_MAX, max = DBL_MIN,
            max_sigma_x = 0,
@@ -302,6 +316,7 @@ static void print_stats(AVFilterContext *ctx)
         max_count += p->max_count;
         min_runs += p->min_runs;
         max_runs += p->max_runs;
+        mask |= p->mask;
         nb_samples += p->nb_samples;
         if (fabs(p->sigma_x) > fabs(max_sigma_x))
             max_sigma_x = p->sigma_x;
@@ -318,6 +333,7 @@ static void print_stats(AVFilterContext *ctx)
         av_log(ctx, AV_LOG_INFO, "Crest factor: %f\n", p->sigma_x2 ? FFMAX(-p->min, p->max) / 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);
+        av_log(ctx, AV_LOG_INFO, "Bit-depth: %u\n", bit_depth(p->mask));
     }
 
     av_log(ctx, AV_LOG_INFO, "Overall\n");
@@ -331,6 +347,7 @@ static void print_stats(AVFilterContext *ctx)
         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);
+    av_log(ctx, AV_LOG_INFO, "Bit-depth: %u\n", bit_depth(mask));
     av_log(ctx, AV_LOG_INFO, "Number of samples: %"PRId64"\n", nb_samples / s->nb_channels);
 }
 
-- 
1.7.11.2



More information about the ffmpeg-devel mailing list