[FFmpeg-cvslog] avfilter/f_ebur128: move true peak calculation out of main loop

Niklas Haas git at videolan.org
Sat Jun 21 18:31:21 EEST 2025


ffmpeg | branch: master | Niklas Haas <git at haasn.dev> | Fri Jun 13 16:54:27 2025 +0200| [229393d8dc8fff295a4ff9ab34957dd24edd5fd7] | committer: Niklas Haas

avfilter/f_ebur128: move true peak calculation out of main loop

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)

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=229393d8dc8fff295a4ff9ab34957dd24edd5fd7
---

 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



More information about the ffmpeg-cvslog mailing list