[MPlayer-dev-eng] [PATCH] Statistics audio filter

Nicolas George nicolas.george at normalesup.org
Sun Feb 15 10:58:25 CET 2009


Hi.

The attached patch implements an "-af stats" audio filter which collects
statistics about the audio stream. These statistics are especially intended
to help adjusting the volume while avoiding clipping.

Regards,

-- 
  Nicolas George
-------------- next part --------------
diff --git a/DOCS/man/en/mplayer.1 b/DOCS/man/en/mplayer.1
index 289c0c5..d155be3 100644
--- a/DOCS/man/en/mplayer.1
+++ b/DOCS/man/en/mplayer.1
@@ -5483,6 +5483,11 @@ Changing playback speed, would change pitch, leaving audio tempo at 1.2x.
 .
 .
 .
+.TP
+.B stats
+Collects and prints statistics about the audio stream, especially the volume.
+The volumes are printed in dB and compatible with the volume audio filter.
+.
 .SH "VIDEO FILTERS"
 Video filters allow you to modify the video stream and its properties.
 The syntax is:
diff --git a/Makefile b/Makefile
index 51e6246..10a3438 100644
--- a/Makefile
+++ b/Makefile
@@ -67,6 +67,7 @@ SRCS_COMMON = asxparser.c \
               libaf/af_resample.c \
               libaf/af_scaletempo.c \
               libaf/af_sinesuppress.c \
+              libaf/af_stats.c \
               libaf/af_sub.c \
               libaf/af_surround.c \
               libaf/af_sweep.c \
diff --git a/libaf/af.c b/libaf/af.c
index 77fda8e..acb0522 100644
--- a/libaf/af.c
+++ b/libaf/af.c
@@ -51,6 +51,7 @@ extern af_info_t af_info_center;
 extern af_info_t af_info_sinesuppress;
 extern af_info_t af_info_karaoke;
 extern af_info_t af_info_scaletempo;
+extern af_info_t af_info_stats;
 
 static af_info_t* filter_list[]={ 
    &af_info_dummy,
@@ -85,6 +86,7 @@ static af_info_t* filter_list[]={
    &af_info_sinesuppress,
    &af_info_karaoke,
    &af_info_scaletempo,
+   &af_info_stats,
    NULL 
 };
 
diff --git a/libaf/af_stats.c b/libaf/af_stats.c
new file mode 100644
index 0000000..8b090e4
--- /dev/null
+++ b/libaf/af_stats.c
@@ -0,0 +1,161 @@
+/*
+ * Copyright (C) 2009 Nicolas George <nicolas.george at normalesup.org>
+ *
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <math.h>
+
+#include "af.h"
+
+#define MAX_DB 80
+#define MIN_VAL 1E-8
+
+typedef struct af_stats_s
+{
+    long long n_samples;
+    double tsquare;
+    int max;
+    long long histogram[65536];
+} af_stats_t;
+
+static inline int
+logdb(double v)
+{
+    return v > 1 ? 0 : v <= MIN_VAL ? MAX_DB - 1 :
+        log(v) / -0.23025850929940456840179914546843642076;
+}
+
+static int
+stats_init(af_instance_t *af, af_stats_t *s, af_data_t *data)
+{
+    int i;
+
+    if(!data)
+        return AF_ERROR;
+    *(af->data) = *data;
+    af->data->format = AF_FORMAT_S16_NE;
+    af->data->bps = 2;
+    s->n_samples = 0;
+    s->tsquare = 0;
+    s->max = 0;
+    for(i = 0; i < 65536; i++)
+        s->histogram[i] = 0;
+    return af_test_output(af, data);
+}
+
+static void
+stats_print(af_stats_t *s)
+{
+    int i;
+    long long sum;
+    float v;
+    long long h[MAX_DB];
+
+    if(s->n_samples == 0)
+        return;
+    s->tsquare /= 32768 * 32768;
+    printf("stats:n_samples: %lld\n", s->n_samples);
+    printf("stats:mean_volume: -%d dB\n", logdb(s->tsquare / s->n_samples));
+    printf("stats:max_volume: -%d dB\n", logdb(s->max / 32768.0));
+    for(i = 0; i < MAX_DB; i++)
+        h[i] = 0;
+    for(i = 0; i < 65536; i++) {
+        v = (i - 32768) / 32768.0;
+        h[logdb(v * v)] += s->histogram[i];
+    }
+    for(i = 0; i < MAX_DB; i++)
+        if(h[i] != 0)
+            break;
+    sum = 0;
+    for(; i < MAX_DB; i++) {
+        sum += h[i];
+        printf("stats:histogram_%ddb: %lld\n", i, h[i]);
+        if(sum > s->n_samples / 1000)
+            break;
+    }
+}
+
+static int
+control(struct af_instance_s *af, int cmd, void *arg)
+{
+    af_stats_t *s = (af_stats_t *)af->setup;
+
+    switch(cmd) {
+        case AF_CONTROL_REINIT:
+            return stats_init(af, s, arg);
+
+        case AF_CONTROL_PRE_DESTROY:
+            stats_print(s);
+            return AF_OK;
+    }
+    return AF_UNKNOWN;
+}
+
+static void
+uninit(struct af_instance_s *af)
+{
+    if(af->data)
+        free(af->data);
+    if(af->setup)
+        free(af->setup);
+}
+
+static af_data_t *
+play(struct af_instance_s *af, af_data_t *data)
+{
+    af_stats_t *s = (af_stats_t *)af->setup;
+    int16_t *a, *aend;
+    int v, v2;
+
+    a = data->audio;
+    aend = (int16_t *)((char *)data->audio + data->len);
+    s->n_samples += aend - a;
+    for(; a < aend; a++) {
+        v = *a;
+        v2 = v * v;
+        s->tsquare += v2;
+        s->histogram[v + 32768]++;
+        if(v2 > s->max)
+            s->max = v2;
+    }
+    return data;
+}
+
+static int af_open(af_instance_t* af){
+    af->control = control;
+    af->uninit = uninit;
+    af->play = play;
+    af->mul = 1;
+    af->data = malloc(sizeof(af_data_t));
+    af->setup = malloc(sizeof(af_stats_t));
+    if(af->data == NULL || af->setup == NULL)
+        return AF_ERROR;
+    return AF_OK;
+}
+
+af_info_t af_info_stats = {
+    "Statistics audio filter",
+    "stats",
+    "Nicolas George",
+    "",
+    0,
+    af_open
+};
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/mplayer-dev-eng/attachments/20090215/bd3b0ab5/attachment.pgp>


More information about the MPlayer-dev-eng mailing list