History

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)

Niklas Haas committed 1 year ago

Commit 229393d1 parent a96175e1 file · +14 −9Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/f_ebur128.c+14−9
@@ -668,16 +668,22 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) · 667 unmodified lines
668668 (const uint8_t **)insamples->data, nb_samples);
669669 if (ret < 0)
670670 return ret;
671− for (ch = 0; ch < nb_channels; ch++)
672− ebur128->true_peaks_per_frame[ch] = 0.0;
673− for (idx_insample = 0; idx_insample < ret; idx_insample++) {
674− for (ch = 0; ch < nb_channels; ch++) {
675− ebur128->true_peaks[ch] = FFMAX(ebur128->true_peaks[ch], fabs(*swr_samples));
676− ebur128->true_peaks_per_frame[ch] = FFMAX(ebur128->true_peaks_per_frame[ch],
677− fabs(*swr_samples));
678− swr_samples++;
671+
672+ double maxpeak = 0.0;
673+ for (int ch = 0; ch < nb_channels; ch++) {
674+ double tp = ebur128->true_peaks[ch];
675+ double tppf = 0.0;
676+ for (int i = 0; i < ret; i++) {
677+ const double sample = fabs(swr_samples[i * nb_channels]);
678+ tp = FFMAX(tp, sample);
679+ tppf = FFMAX(tppf, sample);
679680 }
681+ maxpeak = FFMAX(maxpeak, tp);
682+ ebur128->true_peaks[ch] = tp;
683+ ebur128->true_peaks_per_frame[ch] = tppf;
680684 }
685+
686+ ebur128->true_peak = DBFS(maxpeak);
681687 }
682688 #endif
683689
@@ -720,7 +726,6 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) · 36 unmodified lines
720726 }
721727
722728 FIND_PEAK(ebur128->sample_peak, ebur128->sample_peaks, SAMPLES);
723− FIND_PEAK(ebur128->true_peak, ebur128->true_peaks, TRUE);
724729
725730 /* For integrated loudness, gating blocks are 400ms long with 75%
726731 * overlap (see BS.1770-2 p5), so a re-computation is needed each 100ms

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t