From 740de9d733bcc182eb09779b256300af18a8df5d Mon Sep 17 00:00:00 2001
From: yigithanyigit <yigithanyigit@gmail.com>
Date: Tue, 12 Mar 2024 01:27:59 +0300
Subject: [PATCH] avfilter/af_volumedetect.c: Add 32bit float audio support

Fixes #9613
---
 libavfilter/af_volumedetect.c | 237 ++++++++++++++++++++++++++--------
 1 file changed, 182 insertions(+), 55 deletions(-)

diff --git a/libavfilter/af_volumedetect.c b/libavfilter/af_volumedetect.c
index 8b001d1cf2..fe5d7bd08a 100644
--- a/libavfilter/af_volumedetect.c
+++ b/libavfilter/af_volumedetect.c
@@ -24,94 +24,210 @@
 #include "avfilter.h"
 #include "internal.h"
 
-typedef struct VolDetectContext {
+typedef struct ChannelStats {
     /**
      * Number of samples at each PCM value.
+     * Only used for integer formats.
+     * For 16 bit signed PCM there are 65536.
      * histogram[0x8000 + i] is the number of samples at value i.
      * The extra element is there for symmetry.
      */
-    uint64_t histogram[0x10001];
+    uint64_t* histogram; ///< distribution of the samples, only used for integer formats
+    uint64_t nb_samples; ///< number of samples
+    double sum;          ///< sum of samples
+    double sum2;         ///< sum of the squares of the samples
+    double max;          ///< maximum sample value
+    double min;          ///< minimum sample value
+} ChannelStats;
+
+typedef struct VolDetectContext {
+    ChannelStats *channel_stats; ///< per-channel statistics
+    int channels;                ///< number of channels
+    int is_float;                ///< true if the input is in floating point
 } VolDetectContext;
 
+static void update_float_stats(ChannelStats *stats, int *plane, float *audio_data)
+{
+    double max_sample, min_sample;
+    min_sample = audio_data[*plane];
+    max_sample = fabsf(audio_data[*plane]);
+    if (max_sample > stats->max)
+        stats->max = max_sample;
+    if (min_sample < stats->min)
+        stats->min = min_sample;
+    stats->sum += audio_data[*plane];
+    stats->sum2 += audio_data[*plane] * audio_data[*plane];
+    stats->nb_samples++;
+}
+
 static int filter_frame(AVFilterLink *inlink, AVFrame *samples)
 {
     AVFilterContext *ctx = inlink->dst;
     VolDetectContext *vd = ctx->priv;
-    int nb_samples  = samples->nb_samples;
     int nb_channels = samples->ch_layout.nb_channels;
     int nb_planes   = nb_channels;
+    int planar      = 0;
     int plane, i;
-    int16_t *pcm;
 
-    if (!av_sample_fmt_is_planar(samples->format)) {
-        nb_samples *= nb_channels;
+
+    planar = av_sample_fmt_is_planar(samples->format);
+    if (!planar) {
         nb_planes = 1;
     }
-    for (plane = 0; plane < nb_planes; plane++) {
-        pcm = (int16_t *)samples->extended_data[plane];
-        for (i = 0; i < nb_samples; i++)
-            vd->histogram[pcm[i] + 0x8000]++;
-    }
 
+    if (vd->is_float) {
+        float *audio_data;
+        for (plane = 0; plane < nb_planes; plane++) {
+            audio_data = (float *)samples->extended_data[plane];
+
+            for (i = 0; i < samples->nb_samples; i++) {
+                /*
+                 * If the input is planar, the samples are in the seperated planes.
+                 * if the input is not planar, the samples are interleaved.
+                 * if the input is not planar, split the samples into the planes.
+                 */
+                if (planar) {
+                    update_float_stats(&vd->channel_stats[plane], &plane, &audio_data[i]);
+                } else {
+                    for (int j = 0; j < nb_channels; j++)
+                        update_float_stats(&vd->channel_stats[j], &plane, &audio_data[i * nb_channels + j]);
+                }
+            }
+        }
+    } else {
+        int16_t *pcm;
+        for (plane = 0; plane < nb_planes; plane++) {
+            pcm = (int16_t *)samples->extended_data[plane];
+            for (i = 0; i < samples->nb_samples; i++) {
+                if (planar) {
+                    vd->channel_stats[plane].histogram[pcm[i] + 0x8000]++;
+                    vd->channel_stats[plane].nb_samples++;
+                } else {
+                    for (int j = 0; j < nb_channels; j++) {
+                        vd->channel_stats[j].histogram[pcm[i * nb_channels + j] + 0x8000]++;
+                        vd->channel_stats[j].nb_samples++;
+                    }
+                }
+            }
+        }
+    }
     return ff_filter_frame(inlink->dst->outputs[0], samples);
 }
 
 #define MAX_DB 91
 
-static inline double logdb(uint64_t v)
+static inline double logdb(double v, enum AVSampleFormat sample_fmt)
 {
-    double d = v / (double)(0x8000 * 0x8000);
-    if (!v)
-        return MAX_DB;
-    return -log10(d) * 10;
+    /*
+    * Since it is a not a power value, able to use 20.0 * log10(v)
+    */
+    if (sample_fmt == AV_SAMPLE_FMT_FLT)
+        return 20.0 * log10(v);
+    else {
+        double d = v / (double)(0x8000 * 0x8000);
+        if (!v)
+            return MAX_DB;
+        return -log10(d) * 10;
+    }
 }
 
 static void print_stats(AVFilterContext *ctx)
 {
     VolDetectContext *vd = ctx->priv;
-    int i, max_volume, shift;
-    uint64_t nb_samples = 0, power = 0, nb_samples_shift = 0, sum = 0;
-    uint64_t histdb[MAX_DB + 1] = { 0 };
-
-    for (i = 0; i < 0x10000; i++)
-        nb_samples += vd->histogram[i];
-    av_log(ctx, AV_LOG_INFO, "n_samples: %"PRId64"\n", nb_samples);
-    if (!nb_samples)
-        return;
-
-    /* If nb_samples > 1<<34, there is a risk of overflow in the
-       multiplication or the sum: shift all histogram values to avoid that.
-       The total number of samples must be recomputed to avoid rounding
-       errors. */
-    shift = av_log2(nb_samples >> 33);
-    for (i = 0; i < 0x10000; i++) {
-        nb_samples_shift += vd->histogram[i] >> shift;
-        power += (i - 0x8000) * (i - 0x8000) * (vd->histogram[i] >> shift);
-    }
-    if (!nb_samples_shift)
-        return;
-    power = (power + nb_samples_shift / 2) / nb_samples_shift;
-    av_assert0(power <= 0x8000 * 0x8000);
-    av_log(ctx, AV_LOG_INFO, "mean_volume: %.1f dB\n", -logdb(power));
-
-    max_volume = 0x8000;
-    while (max_volume > 0 && !vd->histogram[0x8000 + max_volume] &&
-                             !vd->histogram[0x8000 - max_volume])
-        max_volume--;
-    av_log(ctx, AV_LOG_INFO, "max_volume: %.1f dB\n", -logdb(max_volume * max_volume));
-
-    for (i = 0; i < 0x10000; i++)
-        histdb[(int)logdb((i - 0x8000) * (i - 0x8000))] += vd->histogram[i];
-    for (i = 0; i <= MAX_DB && !histdb[i]; i++);
-    for (; i <= MAX_DB && sum < nb_samples / 1000; i++) {
-        av_log(ctx, AV_LOG_INFO, "histogram_%ddb: %"PRId64"\n", i, histdb[i]);
-        sum += histdb[i];
+    ChannelStats *channel_stats = vd->channel_stats;
+
+    if (vd->is_float) {
+        int channel;
+        for (channel = 0; channel < vd->channels; channel++) {
+            double rms;
+            av_log(ctx, AV_LOG_INFO, "Channel: %d\n", channel + 1);
+            av_log(ctx, AV_LOG_INFO, "n_samples: %" PRId64 "\n", channel_stats[channel].nb_samples);
+            if (!channel_stats[channel].nb_samples)
+                return;
+            av_log(ctx, AV_LOG_INFO, "min_volume: %.6f dB\n", channel_stats[channel].min);
+            av_log(ctx, AV_LOG_INFO, "max_volume: %.6f dB\n", channel_stats[channel].max);
+            av_log(ctx, AV_LOG_INFO, "peak_level dB: %.6f dB\n", logdb(channel_stats[channel].max, AV_SAMPLE_FMT_FLT));
+            rms = sqrt(channel_stats[channel].sum2 / channel_stats[channel].nb_samples);
+            av_log(ctx, AV_LOG_INFO, "RMS volume: %.6f dB\n", logdb(rms, AV_SAMPLE_FMT_FLT));
+        }
+    } else {
+        int channel;
+        for (channel = 0; channel < vd->channels; channel++) {
+            int i, max_volume, shift;
+            uint64_t nb_samples = 0, power = 0, nb_samples_shift = 0, sum = 0;
+            uint64_t histdb[MAX_DB + 1] = {0};
+            av_log(ctx, AV_LOG_INFO, "Channel: %d\n", channel + 1);
+            for (i = 0; i < 0x10000; i++)
+                nb_samples += channel_stats[channel].histogram[i];
+            av_log(ctx, AV_LOG_INFO, "n_samples: %" PRId64 "\n", nb_samples);
+            if (!nb_samples)
+                return;
+            /*
+             * If nb_samples > 1<<34, there is a risk of overflow in the
+             * multiplication or the sum: shift all histogram values to avoid that.
+             * The total number of samples must be recomputed to avoid rounding
+             * errors.
+            */
+            shift = av_log2(nb_samples >> 33);
+            for (i = 0; i < 0x10000; i++) {
+                nb_samples_shift += channel_stats[channel].histogram[i] >> shift;
+                power += (i - 0x8000) * (i - 0x8000) * (channel_stats[channel].histogram[i] >> shift);
+            }
+            if (!nb_samples_shift)
+                return;
+            power = (power + nb_samples_shift / 2) / nb_samples_shift;
+            av_assert0(power <= 0x8000 * 0x8000);
+            av_log(ctx, AV_LOG_INFO, "mean_volume: %.1f dB\n", -logdb((double)power, AV_SAMPLE_FMT_S16));
+
+            max_volume = 0x8000;
+            while (max_volume > 0 && !channel_stats[channel].histogram[0x8000 + max_volume] &&
+                   !channel_stats[channel].histogram[0x8000 - max_volume])
+                max_volume--;
+            av_log(ctx, AV_LOG_INFO, "max_volume: %.1f dB\n", -logdb((double)(max_volume * max_volume), AV_SAMPLE_FMT_S16));
+
+            for (i = 0; i < 0x10000; i++)
+                histdb[(int)logdb((double)(i - 0x8000) * (i - 0x8000), AV_SAMPLE_FMT_S16)] += vd->channel_stats[channel].histogram[i];
+            for (i = 0; i <= MAX_DB && !histdb[i]; i++);
+            for (; i <= MAX_DB && sum < nb_samples / 1000; i++) {
+                av_log(ctx, AV_LOG_INFO, "histogram_%ddb: %" PRId64 "\n", i, histdb[i]);
+                sum += histdb[i];
+            }
+        }
     }
 }
 
+static int config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    VolDetectContext *vd = ctx->priv;
+    int i;
+    vd->is_float = outlink->format == AV_SAMPLE_FMT_FLT ||
+                   outlink->format == AV_SAMPLE_FMT_FLTP;
+
+    vd->channels = outlink->ch_layout.nb_channels;
+    vd->channel_stats = av_calloc(vd->channels, sizeof(ChannelStats));
+
+    if (!vd->channel_stats)
+        return AVERROR(ENOMEM);
+        if (!vd->is_float){
+            for (i = 0; i < vd->channels; i++) {
+                vd->channel_stats[i].histogram = av_calloc(0x10000, sizeof(uint64_t));
+                if (!vd->channel_stats[i].histogram)
+                    return AVERROR(ENOMEM);
+            }
+        }
+    return 0;
+}
+
 static av_cold void uninit(AVFilterContext *ctx)
 {
     print_stats(ctx);
+    if (ctx->priv) {
+        VolDetectContext *vd = ctx->priv;
+        for (int i = 0; i < vd->channels; i++)
+            av_freep(&vd->channel_stats[i].histogram);
+        av_freep(&vd->channel_stats);
+    }
 }
 
 static const AVFilterPad volumedetect_inputs[] = {
@@ -122,6 +238,14 @@ static const AVFilterPad volumedetect_inputs[] = {
     },
 };
 
+static const AVFilterPad volumedetect_outputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_AUDIO,
+        .config_props = config_output,
+    },
+};
+
 const AVFilter ff_af_volumedetect = {
     .name          = "volumedetect",
     .description   = NULL_IF_CONFIG_SMALL("Detect audio volume."),
@@ -129,6 +253,9 @@ const AVFilter ff_af_volumedetect = {
     .uninit        = uninit,
     .flags         = AVFILTER_FLAG_METADATA_ONLY,
     FILTER_INPUTS(volumedetect_inputs),
-    FILTER_OUTPUTS(ff_audio_default_filterpad),
-    FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P),
+    FILTER_OUTPUTS(volumedetect_outputs),
+    FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_S16,
+                      AV_SAMPLE_FMT_S16P,
+                      AV_SAMPLE_FMT_FLT,
+                      AV_SAMPLE_FMT_FLTP),
 };
-- 
2.44.0

