Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Niklas Haas <ffmpeg@haasn.xyz>
To: ffmpeg-devel@ffmpeg.org
Cc: Niklas Haas <git@haasn.dev>
Subject: [FFmpeg-devel] [PATCH v5 12/13] avfilter/f_ebur128: move peak detection to reusable DSP function
Date: Fri, 20 Jun 2025 15:21:37 +0200
Message-ID: <20250620132139.2431475-12-ffmpeg@haasn.xyz> (raw)
In-Reply-To: <20250620132139.2431475-1-ffmpeg@haasn.xyz>

From: Niklas Haas <git@haasn.dev>

True peak and sample peak share almost the same logic. Define this logic in
a separate function for reusability, and so we can write SIMD versions.
---
 libavfilter/f_ebur128.c | 59 ++++++++++++++++++++++-------------------
 libavfilter/f_ebur128.h |  6 +++++
 2 files changed, 37 insertions(+), 28 deletions(-)

diff --git a/libavfilter/f_ebur128.c b/libavfilter/f_ebur128.c
index 1fb7129271..514be7a8e3 100644
--- a/libavfilter/f_ebur128.c
+++ b/libavfilter/f_ebur128.c
@@ -580,6 +580,8 @@ static av_cold int init(AVFilterContext *ctx)
     av_log(ctx, AV_LOG_VERBOSE, "EBU +%d scale\n", ebur128->meter);
 
     ebur128->dsp.filter_channels = ff_ebur128_filter_channels_c;
+    ebur128->dsp.find_peak = ff_ebur128_find_peak_c;
+
 #if ARCH_X86
     ff_ebur128_init_x86(&ebur128->dsp);
 #endif
@@ -650,6 +652,23 @@ void ff_ebur128_filter_channels_c(const EBUR128DSPContext *dsp,
     }
 }
 
+double ff_ebur128_find_peak_c(double *restrict ch_peaks, const int nb_channels,
+                              const double *samples, const int nb_samples)
+{
+    double maxpeak = 0.0;
+    for (int ch = 0; ch < nb_channels; ch++) {
+        double ch_peak = ch_peaks[ch];
+        for (int i = 0; i < nb_samples; i++) {
+            const double sample = fabs(samples[i * nb_channels]);
+            ch_peak = FFMAX(ch_peak, sample);
+        }
+        maxpeak = FFMAX(maxpeak, ch_peak);
+        ch_peaks[ch] = ch_peak;
+    }
+
+    return maxpeak;
+}
+
 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
 {
     int ret;
@@ -669,42 +688,26 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
         if (ret < 0)
             return ret;
 
-        double maxpeak = 0.0;
+        memset(ebur128->true_peaks_per_frame, 0,
+               nb_channels * sizeof(*ebur128->true_peaks_per_frame));
+
+        double peak = dsp->find_peak(ebur128->true_peaks_per_frame, nb_channels,
+                                     swr_samples, ret);
+
+        ebur128->true_peak = DBFS(peak);
         for (int ch = 0; ch < nb_channels; ch++) {
-            double tp   = ebur128->true_peaks[ch];
-            double tppf = 0.0;
-            for (int i = 0; i < ret; i++) {
-                const double sample = fabs(swr_samples[i * nb_channels]);
-                tp   = FFMAX(tp,   sample);
-                tppf = FFMAX(tppf, sample);
-            }
-            maxpeak = FFMAX(maxpeak, tp);
-            ebur128->true_peaks[ch] = tp;
-            ebur128->true_peaks_per_frame[ch] = tppf;
+            ebur128->true_peaks[ch] = FFMAX(ebur128->true_peaks[ch],
+                                            ebur128->true_peaks_per_frame[ch]);
         }
-
-        ebur128->true_peak = DBFS(maxpeak);
     }
 #endif
 
     if (ebur128->peak_mode & PEAK_MODE_SAMPLES_PEAKS) {
-        double maxpeak = 0.0;
-        for (int ch = 0; ch < nb_channels; ch++) {
-            const double *restrict samples_ch = &samples[ch];
-            double sp = ebur128->sample_peaks[ch];
-
-            for (int i = ebur128->idx_insample; i < nb_samples; i++) {
-                const double sample = fabs(samples_ch[nb_channels * i]);
-                sp = FFMAX(sp, sample);
-            }
-            maxpeak = FFMAX(maxpeak, sp);
-            ebur128->sample_peaks[ch] = sp;
-        }
-
-        ebur128->sample_peak = DBFS(maxpeak);
+        double peak = dsp->find_peak(ebur128->sample_peaks, nb_channels,
+                                     samples, nb_samples);
+        ebur128->sample_peak = DBFS(peak);
     }
 
-
     for (int idx_insample = ebur128->idx_insample; idx_insample < nb_samples; idx_insample++) {
         const int bin_id_400  = ebur128->i400.cache_pos++;
         const int bin_id_3000 = ebur128->i3000.cache_pos++;
diff --git a/libavfilter/f_ebur128.h b/libavfilter/f_ebur128.h
index 1889e28bdd..ad24a3c129 100644
--- a/libavfilter/f_ebur128.h
+++ b/libavfilter/f_ebur128.h
@@ -45,6 +45,10 @@ typedef struct EBUR128DSPContext {
                             double *cache_400, double *cache_3000,
                             double *sum_400, double *sum_3000,
                             int nb_channels);
+
+    /* Updates ch_peaks[] and returns maximum from all channels */
+    double (*find_peak)(double *ch_peaks, int nb_channels,
+                        const double *samples, int nb_samples);
 } EBUR128DSPContext;
 
 static_assert(offsetof(EBUR128DSPContext, pre) == 0,                   "struct layout mismatch");
@@ -56,4 +60,6 @@ void ff_ebur128_init_x86(EBUR128DSPContext *dsp);
 void ff_ebur128_filter_channels_c(const EBUR128DSPContext *, const double *,
                                   double *, double *, double *, double *, int);
 
+double ff_ebur128_find_peak_c(double *, int, const double *, int);
+
 #endif /* AVFILTER_F_EBUR128_H */
-- 
2.49.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

  parent reply	other threads:[~2025-06-20 13:22 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-20 13:21 [FFmpeg-devel] [PATCH v5 01/13] avfilter/f_ebur128: use transformed direct form II Niklas Haas
2025-06-20 13:21 ` [FFmpeg-devel] [PATCH v5 02/13] avfilter/f_ebur128: simplify sample cache array Niklas Haas
2025-06-20 13:21 ` [FFmpeg-devel] [PATCH v5 03/13] avfilter/f_ebur128: use structs for biquad weights Niklas Haas
2025-06-20 13:21 ` [FFmpeg-devel] [PATCH v5 04/13] avfilter/f_ebur128: use a single packed array for the integrator cache Niklas Haas
2025-06-20 13:21 ` [FFmpeg-devel] [PATCH v5 05/13] avfilter/f_ebur128: move weights and cache to EBUR128DSPContext Niklas Haas
2025-06-20 13:21 ` [FFmpeg-devel] [PATCH v5 06/13] avfilter/f_ebur128: split off C implementation to separate function Niklas Haas
2025-06-20 13:21 ` [FFmpeg-devel] [PATCH v5 07/13] avfilter/x86/f_ebur128: add x86 AVX implementation Niklas Haas
2025-06-20 13:49   ` James Almer
2025-06-20 13:21 ` [FFmpeg-devel] [PATCH v5 08/13] avfilter/f_ebur128: remove pointless macro Niklas Haas
2025-06-20 13:21 ` [FFmpeg-devel] [PATCH v5 09/13] avfilter/f_ebur128: move true peak calculation out of main loop Niklas Haas
2025-06-20 13:21 ` [FFmpeg-devel] [PATCH v5 10/13] avfilter/f_ebur128: lift sample " Niklas Haas
2025-06-20 13:21 ` [FFmpeg-devel] [PATCH v5 11/13] avfilter/f_ebur128: move variable declarations to usage site Niklas Haas
2025-06-20 13:21 ` Niklas Haas [this message]
2025-06-20 13:21 ` [FFmpeg-devel] [PATCH v5 13/13] avfilter/x86/f_ebur128: implement AVX peak calculation Niklas Haas
2025-06-20 13:23 ` [FFmpeg-devel] [PATCH v5 01/13] avfilter/f_ebur128: use transformed direct form II Niklas Haas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250620132139.2431475-12-ffmpeg@haasn.xyz \
    --to=ffmpeg@haasn.xyz \
    --cc=ffmpeg-devel@ffmpeg.org \
    --cc=git@haasn.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git