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 v4 09/13] avfilter/f_ebur128: move true peak calculation out of main loop
Date: Tue, 17 Jun 2025 14:06:02 +0200
Message-ID: <20250617120609.1987435-9-ffmpeg@haasn.xyz> (raw)
In-Reply-To: <20250617120609.1987435-1-ffmpeg@haasn.xyz>

From: Niklas Haas <git@haasn.dev>

Easier to read, less convoluted, and ~30% faster. Most importantly, this
avoids repeating the redundant recalculation of the true peak on every
single sample, by moving the FIND_PEAK() loop out of the main loop. (Note
that FIND_PEAK() does not depend on the current sample index at all, so
there is no reason for it to ever be recomputed here)
---
 libavfilter/f_ebur128.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/libavfilter/f_ebur128.c b/libavfilter/f_ebur128.c
index 2e1eedd855..23092b597f 100644
--- a/libavfilter/f_ebur128.c
+++ b/libavfilter/f_ebur128.c
@@ -668,16 +668,22 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
                               (const uint8_t **)insamples->data, nb_samples);
         if (ret < 0)
             return ret;
-        for (ch = 0; ch < nb_channels; ch++)
-            ebur128->true_peaks_per_frame[ch] = 0.0;
-        for (idx_insample = 0; idx_insample < ret; idx_insample++) {
-            for (ch = 0; ch < nb_channels; ch++) {
-                ebur128->true_peaks[ch] = FFMAX(ebur128->true_peaks[ch], fabs(*swr_samples));
-                ebur128->true_peaks_per_frame[ch] = FFMAX(ebur128->true_peaks_per_frame[ch],
-                                                          fabs(*swr_samples));
-                swr_samples++;
+
+        double maxpeak = 0.0;
+        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_peak = DBFS(maxpeak);
     }
 #endif
 
@@ -720,7 +726,6 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
         }
 
         FIND_PEAK(ebur128->sample_peak, ebur128->sample_peaks, SAMPLES);
-        FIND_PEAK(ebur128->true_peak,   ebur128->true_peaks,   TRUE);
 
         /* For integrated loudness, gating blocks are 400ms long with 75%
          * overlap (see BS.1770-2 p5), so a re-computation is needed each 100ms
-- 
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-17 12:08 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-17 12:05 [FFmpeg-devel] [PATCH v4 01/13] avfilter/f_ebur128: use transformed direct form II Niklas Haas
2025-06-17 12:05 ` [FFmpeg-devel] [PATCH v4 02/13] avfilter/f_ebur128: simplify sample cache array Niklas Haas
2025-06-17 12:05 ` [FFmpeg-devel] [PATCH v4 03/13] avfilter/f_ebur128: use structs for biquad weights Niklas Haas
2025-06-17 12:05 ` [FFmpeg-devel] [PATCH v4 04/13] avfilter/f_ebur128: use a single packed array for the integrator cache Niklas Haas
2025-06-17 12:05 ` [FFmpeg-devel] [PATCH v4 05/13] avfilter/f_ebur128: move weights and cache to EBUR128DSPContext Niklas Haas
2025-06-17 12:05 ` [FFmpeg-devel] [PATCH v4 06/13] avfilter/f_ebur128: split off C implementation to separate function Niklas Haas
2025-06-17 12:06 ` [FFmpeg-devel] [PATCH v4 07/13] avfilter/x86/f_ebur128: add x86 AVX implementation Niklas Haas
2025-06-17 12:06 ` [FFmpeg-devel] [PATCH v4 08/13] avfilter/f_ebur128: remove pointless macro Niklas Haas
2025-06-17 12:06 ` Niklas Haas [this message]
2025-06-17 12:06 ` [FFmpeg-devel] [PATCH v4 10/13] avfilter/f_ebur128: lift sample peak calculation out of main loop Niklas Haas
2025-06-17 12:06 ` [FFmpeg-devel] [PATCH v4 11/13] avfilter/f_ebur128: move variable declarations to usage site Niklas Haas
2025-06-17 12:06 ` [FFmpeg-devel] [PATCH v4 12/13] avfilter/f_ebur128: move true peak calculation to DSP function Niklas Haas
2025-06-17 12:06 ` [FFmpeg-devel] [PATCH v4 13/13] avfilter/x86/f_ebur128: implement AVX true peak calculation Niklas Haas
2025-06-17 13:48   ` 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=20250617120609.1987435-9-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