From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 063F74F4BB for ; Tue, 17 Jun 2025 12:08:39 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id F0A2468D142; Tue, 17 Jun 2025 15:06:38 +0300 (EEST) Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id A17A968CF6F for ; Tue, 17 Jun 2025 15:06:15 +0300 (EEST) Received: from haasn.dev (unknown [10.30.1.1]) by haasn.dev (Postfix) with UTF8SMTP id 9645142537; Tue, 17 Jun 2025 14:06:10 +0200 (CEST) From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Tue, 17 Jun 2025 14:05:59 +0200 Message-ID: <20250617120609.1987435-6-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250617120609.1987435-1-ffmpeg@haasn.xyz> References: <20250617120609.1987435-1-ffmpeg@haasn.xyz> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4 06/13] avfilter/f_ebur128: split off C implementation to separate function X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Niklas Haas Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: From: Niklas Haas I decided to separate out the peak measurement loop to avoid bloating the signature, and since it's only conditionally used. --- libavfilter/f_ebur128.c | 83 ++++++++++++++++++++++++----------------- libavfilter/f_ebur128.h | 3 ++ 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/libavfilter/f_ebur128.c b/libavfilter/f_ebur128.c index c3328dc520..b9e210c05a 100644 --- a/libavfilter/f_ebur128.c +++ b/libavfilter/f_ebur128.c @@ -609,11 +609,48 @@ static int gate_update(struct integrator *integ, double power, return gate_hist_pos; } +void ff_ebur128_filter_channels_c(const EBUR128DSPContext *dsp, + const double *restrict samples, + double *restrict cache_400, + double *restrict cache_3000, + double *restrict sum_400, + double *restrict sum_3000, + const int nb_channels) +{ + const EBUR128Biquad pre = dsp->pre; + const EBUR128Biquad rlb = dsp->rlb; + + for (int ch = 0; ch < nb_channels; ch++) { + /* Y[i] = X[i]*b0 + X[i-1]*b1 + X[i-2]*b2 - Y[i-1]*a1 - Y[i-2]*a2 */ +#define FILTER(DST, SRC, FILT) do { \ + const double tmp = DST[0] = FILT.b0 * SRC + DST[1]; \ + DST[1] = FILT.b1 * SRC + DST[2] - FILT.a1 * tmp; \ + DST[2] = FILT.b2 * SRC - FILT.a2 * tmp; \ +} while (0) + + const double x = samples[ch]; + double *restrict y = &dsp->y[3 * ch]; + double *restrict z = &dsp->z[3 * ch]; + + // TODO: merge both filters in one? + FILTER(y, x, pre); // apply pre-filter + FILTER(z, *y, rlb); // apply RLB-filter + + /* add the new value, and limit the sum to the cache size (400ms or 3s) + * by removing the oldest one */ + const double bin = *z * *z; + sum_400 [ch] += bin - cache_400[ch]; + sum_3000[ch] += bin - cache_3000[ch]; + cache_400[ch] = cache_3000[ch] = bin; + } +} + static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) { int i, ch, idx_insample, ret; AVFilterContext *ctx = inlink->dst; EBUR128Context *ebur128 = ctx->priv; + const EBUR128DSPContext *dsp = &ebur128->dsp; const int nb_channels = ebur128->nb_channels; const int nb_samples = insamples->nb_samples; const double *samples = (double *)insamples->data[0]; @@ -639,14 +676,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) } #endif - const EBUR128Biquad pre = ebur128->dsp.pre; - const EBUR128Biquad rlb = ebur128->dsp.rlb; - for (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; - double *restrict cache_400 = &ebur128->i400.cache[bin_id_400 * nb_channels]; - double *restrict cache_3000 = &ebur128->i3000.cache[bin_id_3000 * nb_channels]; #define MOVE_TO_NEXT_CACHED_ENTRY(time) do { \ ebur128->i##time.cache_pos++; \ @@ -660,35 +692,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) MOVE_TO_NEXT_CACHED_ENTRY(400); MOVE_TO_NEXT_CACHED_ENTRY(3000); - for (ch = 0; ch < nb_channels; ch++) { - if (ebur128->peak_mode & PEAK_MODE_SAMPLES_PEAKS) - ebur128->sample_peaks[ch] = FFMAX(ebur128->sample_peaks[ch], fabs(samples[idx_insample * nb_channels + ch])); - - if (!ebur128->ch_weighting[ch]) - continue; - - /* Y[i] = X[i]*b0 + X[i-1]*b1 + X[i-2]*b2 - Y[i-1]*a1 - Y[i-2]*a2 */ -#define FILTER(DST, SRC, FILT) do { \ - const double tmp = DST[0] = FILT.b0 * SRC + DST[1]; \ - DST[1] = FILT.b1 * SRC + DST[2] - FILT.a1 * tmp; \ - DST[2] = FILT.b2 * SRC - FILT.a2 * tmp; \ -} while (0) - - const double x = samples[idx_insample * nb_channels + ch]; - double *restrict y = &ebur128->dsp.y[3 * ch]; - double *restrict z = &ebur128->dsp.z[3 * ch]; - - // TODO: merge both filters in one? - FILTER(y, x, pre); // apply pre-filter - FILTER(z, *y, rlb); // apply RLB-filter - - /* add the new value, and limit the sum to the cache size (400ms or 3s) - * by removing the oldest one */ - const double bin = *z * *z; - ebur128->i400.sum [ch] += bin - cache_400[ch]; - ebur128->i3000.sum[ch] += bin - cache_3000[ch]; - cache_400[ch] = cache_3000[ch] = bin; - } + ff_ebur128_filter_channels_c(dsp, &samples[idx_insample * nb_channels], + &ebur128->i400.cache[bin_id_400 * nb_channels], + &ebur128->i3000.cache[bin_id_3000 * nb_channels], + ebur128->i400.sum, ebur128->i3000.sum, + nb_channels); #define FIND_PEAK(global, sp, ptype) do { \ int ch; \ @@ -701,6 +709,13 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) } \ } while (0) + if (ebur128->peak_mode & PEAK_MODE_SAMPLES_PEAKS) { + for (ch = 0; ch < nb_channels; ch++) { + const double sample = samples[idx_insample * nb_channels + ch]; + ebur128->sample_peaks[ch] = FFMAX(ebur128->sample_peaks[ch], fabs(sample)); + } + } + FIND_PEAK(ebur128->sample_peak, ebur128->sample_peaks, SAMPLES); FIND_PEAK(ebur128->true_peak, ebur128->true_peaks, TRUE); diff --git a/libavfilter/f_ebur128.h b/libavfilter/f_ebur128.h index 42cce9a5e4..7b8e876576 100644 --- a/libavfilter/f_ebur128.h +++ b/libavfilter/f_ebur128.h @@ -37,4 +37,7 @@ typedef struct EBUR128DSPContext { double *z; /* after RLB-filter */ } EBUR128DSPContext; +void ff_ebur128_filter_channels_c(const EBUR128DSPContext *, const double *, + double *, double *, double *, 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".