From: Niklas Haas <ffmpeg@haasn.xyz> To: ffmpeg-devel@ffmpeg.org Cc: Niklas Haas <git@haasn.dev> Subject: [FFmpeg-devel] [PATCH v2 06/13] avfilter/f_ebur128: split off C implementation to separate function Date: Fri, 13 Jun 2025 18:37:49 +0200 Message-ID: <20250613163801.197737-6-ffmpeg@haasn.xyz> (raw) In-Reply-To: <20250613163801.197737-1-ffmpeg@haasn.xyz> From: Niklas Haas <git@haasn.dev> 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 a8247e5aa0..dfc2e7c75b 100644 --- a/libavfilter/f_ebur128.h +++ b/libavfilter/f_ebur128.h @@ -33,3 +33,6 @@ typedef struct EBUR128DSPContext { double *y; /* after pre-filter */ double *z; /* after RLB-filter */ } EBUR128DSPContext; + +void ff_ebur128_filter_channels_c(const EBUR128DSPContext *, const double *, + double *, double *, double *, double *, int); -- 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".
next prev parent reply other threads:[~2025-06-13 16:39 UTC|newest] Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top 2025-06-13 16:37 [FFmpeg-devel] [PATCH v2 01/13] avfilter/f_ebur128: use transformed direct form II Niklas Haas 2025-06-13 16:37 ` [FFmpeg-devel] [PATCH v2 02/13] avfilter/f_ebur128: simplify sample cache array Niklas Haas 2025-06-13 16:37 ` [FFmpeg-devel] [PATCH v2 03/13] avfilter/f_ebur128: use structs for biquad weights Niklas Haas 2025-06-13 16:37 ` [FFmpeg-devel] [PATCH v2 04/13] avfilter/f_ebur128: use a single packed array for the integrator cache Niklas Haas 2025-06-13 16:37 ` [FFmpeg-devel] [PATCH v2 05/13] avfilter/f_ebur128: move weights and cache to EBUR128DSPContext Niklas Haas 2025-06-13 16:37 ` Niklas Haas [this message] 2025-06-13 16:37 ` [FFmpeg-devel] [PATCH v2 07/13] avfilter/x86/f_ebur128: add x86 AVX implementation Niklas Haas 2025-06-14 1:12 ` Michael Niedermayer 2025-06-16 11:19 ` Niklas Haas 2025-06-14 1:20 ` James Almer 2025-06-16 11:15 ` Niklas Haas 2025-06-13 16:37 ` [FFmpeg-devel] [PATCH v2 08/13] avfilter/f_ebur128: remove pointless macro Niklas Haas 2025-06-13 16:37 ` [FFmpeg-devel] [PATCH v2 09/13] avfilter/f_ebur128: move true peak calculation out of main loop Niklas Haas 2025-06-13 16:37 ` [FFmpeg-devel] [PATCH v2 10/13] avfilter/f_ebur128: lift sample " Niklas Haas 2025-06-13 16:37 ` [FFmpeg-devel] [PATCH v2 11/13] avfilter/f_ebur128: move variable declarations to usage site Niklas Haas 2025-06-13 16:37 ` [FFmpeg-devel] [PATCH v2 12/13] avfilter/f_ebur128: move true peak calculation to DSP function Niklas Haas 2025-06-13 16:37 ` [FFmpeg-devel] [PATCH v2 13/13] avfilter/x86/f_ebur128: implement AVX true peak calculation Niklas Haas 2025-06-16 11:19 [FFmpeg-devel] [PATCH v2 01/13] avfilter/f_ebur128: use transformed direct form II Niklas Haas 2025-06-16 11:19 ` [FFmpeg-devel] [PATCH v2 06/13] avfilter/f_ebur128: split off C implementation to separate function 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=20250613163801.197737-6-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