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)
Unified · Split
| @@ -668,16 +668,22 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) · 667 unmodified lines | ||
| 668 | 668 | (const uint8_t **)insamples->data, nb_samples); |
| 669 | 669 | if (ret < 0) |
| 670 | 670 | 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); | |
| 679 | 680 | } |
| 681 | + maxpeak = FFMAX(maxpeak, tp); | |
| 682 | + ebur128->true_peaks[ch] = tp; | |
| 683 | + ebur128->true_peaks_per_frame[ch] = tppf; | |
| 680 | 684 | } |
| 685 | + | |
| 686 | + ebur128->true_peak = DBFS(maxpeak); | |
| 681 | 687 | } |
| 682 | 688 | #endif |
| 683 | 689 | |
| @@ -720,7 +726,6 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) · 36 unmodified lines | ||
| 720 | 726 | } |
| 721 | 727 | |
| 722 | 728 | FIND_PEAK(ebur128->sample_peak, ebur128->sample_peaks, SAMPLES); |
| 723 | − FIND_PEAK(ebur128->true_peak, ebur128->true_peaks, TRUE); | |
| 724 | 729 | |
| 725 | 730 | /* For integrated loudness, gating blocks are 400ms long with 75% |
| 726 | 731 | * overlap (see BS.1770-2 p5), so a re-computation is needed each 100ms |