From patchwork Thu Mar 4 13:06:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 26086 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id B10AE44A019 for ; Thu, 4 Mar 2021 15:07:05 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8B6EB68A56E; Thu, 4 Mar 2021 15:07:05 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f44.google.com (mail-ej1-f44.google.com [209.85.218.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7A80E687FA1 for ; Thu, 4 Mar 2021 15:06:59 +0200 (EET) Received: by mail-ej1-f44.google.com with SMTP id w1so49295720ejf.11 for ; Thu, 04 Mar 2021 05:06:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=zbO/+9zyypmH0KwEQ4WVAv4WMl5zo/8U02s6g3nVpwg=; b=e+ebxSa7Yuc0o7BjJzKovRJA7t7yEYVxmVqUQhSSMyQfZZCBqNgXdxSUyb2YH3aLTp JvE4WhJPgWrmLUkNjUCiUJ15kJYyf3+DbI69A38156R0eAitUpyPt4t0827O1pkTNHJi dPbowhyuJhFjXhKNwDqzrTIBZ1kBw3+WiZyQog2qSuWDRhu/WwUZNOSPMRckoD6JIJ+f s574jE84ZCCrJ3xH2O/xwg81wkXbtr8dvBGWIgfJ/qV8RxpjelOuWI1tNZaZVd75P7VL sqGkmmpO+EEdt82FfZTYPklBMso5A3XDD1HpQXSsTPdfquw8n2zisIC7Hj+l9tJSvlE1 r4oA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=zbO/+9zyypmH0KwEQ4WVAv4WMl5zo/8U02s6g3nVpwg=; b=Rt7B2melf8yc4RVA2K8InE2WkOUzRNomxd76k9aFaEnqy4q8W/zRG+ETGE1KvO/Kbr ULfL8mB8PBUEXmOdBt1UvDbdYxc80gmuYLA79TDI/MSUdbn82gUYbScjD70Pd315YJPz zBdv2d3NjKTTPcKrgjHLNgy0Up02jX3XykR2T7R8p1QWQySVgn/+aIwEkGEKiJzl2Wc3 /t53YEfRqyuwYtpeiA0nfvZ5gPo39DcIQZnInRUItF38b0G/6CK81Aj/vpPwnuC9JD5S SShe7l8zq6L82btjqq+K4WdLwdf+MRBP4vhlCplnr+Y5/4nCHKsFQIuiFckD5yfVZWiu hZkQ== X-Gm-Message-State: AOAM530bfggMVShepTpe7Mv6iv7r6Myg94qjvMgFZxP8FBXEXCvnNoRA FHdzVER6CGstG3MVQFBlTyixim6OB78otA== X-Google-Smtp-Source: ABdhPJwskuz/PIVT15ZWFM2X0P0U3ztiTDztlkhRLwW+rU+3MpXV9mRsXZg2+9BoWTXl3eW+0Rqs2w== X-Received: by 2002:a17:906:6487:: with SMTP id e7mr4126340ejm.181.1614863218891; Thu, 04 Mar 2021 05:06:58 -0800 (PST) Received: from localhost.localdomain ([94.250.185.123]) by smtp.gmail.com with ESMTPSA id v9sm23516396ejd.92.2021.03.04.05.06.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Mar 2021 05:06:58 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Thu, 4 Mar 2021 14:06:50 +0100 Message-Id: <20210304130650.17186-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter/f_ebuf128: add all sample rates support X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- libavfilter/f_ebur128.c | 58 +++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/libavfilter/f_ebur128.c b/libavfilter/f_ebur128.c index e81520838d..01767c5729 100644 --- a/libavfilter/f_ebur128.c +++ b/libavfilter/f_ebur128.c @@ -24,7 +24,6 @@ * @see http://tech.ebu.ch/loudness * @see https://www.youtube.com/watch?v=iuEtQqC-Sqo "EBU R128 Introduction - Florian Camerer" * @todo implement start/stop/reset through filter command injection - * @todo support other frequencies to avoid resampling */ #include @@ -45,20 +44,6 @@ #define MAX_CHANNELS 63 -/* pre-filter coefficients */ -#define PRE_B0 1.53512485958697 -#define PRE_B1 -2.69169618940638 -#define PRE_B2 1.19839281085285 -#define PRE_A1 -1.69065929318241 -#define PRE_A2 0.73248077421585 - -/* RLB-filter coefficients */ -#define RLB_B0 1.0 -#define RLB_B1 -2.0 -#define RLB_B2 1.0 -#define RLB_A1 -1.99004745483398 -#define RLB_A2 0.99007225036621 - #define ABS_THRES -70 ///< silence gate: we discard anything below this absolute (LUFS) threshold #define ABS_UP_THRES 10 ///< upper loud limit to consider (ABS_THRES being the minimum) #define HIST_GRAIN 100 ///< defines histogram precision @@ -128,6 +113,10 @@ typedef struct EBUR128Context { double x[MAX_CHANNELS * 3]; ///< 3 input samples cache for each channel double y[MAX_CHANNELS * 3]; ///< 3 pre-filter samples cache for each channel double z[MAX_CHANNELS * 3]; ///< 3 RLB-filter samples cache for each channel + double pre_b[3]; ///< pre-filter numerator coefficients + double pre_a[3]; ///< pre-filter denominator coefficients + double rlb_b[3]; ///< pre-filter numerator coefficients + double rlb_a[3]; ///< pre-filter denominator coefficients #define I400_BINS (48000 * 4 / 10) #define I3000_BINS (48000 * 3) @@ -388,6 +377,32 @@ static int config_audio_input(AVFilterLink *inlink) AVFilterContext *ctx = inlink->dst; EBUR128Context *ebur128 = ctx->priv; + double f0 = 1681.974450955533; + double G = 3.999843853973347; + double Q = 0.7071752369554196; + + double K = tan(M_PI * f0 / (double)inlink->sample_rate); + double Vh = pow(10.0, G / 20.0); + double Vb = pow(Vh, 0.4996667741545416); + + double a0 = 1.0 + K / Q + K * K; + + ebur128->pre_b[0] = (Vh + Vb * K / Q + K * K) / a0; + ebur128->pre_b[1] = 2.0 * (K * K - Vh) / a0; + ebur128->pre_b[2] = (Vh - Vb * K / Q + K * K) / a0; + ebur128->pre_a[1] = 2.0 * (K * K - 1.0) / a0; + ebur128->pre_a[2] = (1.0 - K / Q + K * K) / a0; + + f0 = 38.13547087602444; + Q = 0.5003270373238773; + K = tan(M_PI * f0 / (double)inlink->sample_rate); + + ebur128->rlb_b[0] = 1.0; + ebur128->rlb_b[1] = -2.0; + ebur128->rlb_b[2] = 1.0;; + ebur128->rlb_a[1] = 2.0 * (K * K - 1.0) / (1.0 + K / Q + K * K); + ebur128->rlb_a[2] = (1.0 - K / Q + K * K) / (1.0 + K / Q + K * K); + /* Force 100ms framing in case of metadata injection: the frames must have * a granularity of the window overlap to be accurately exploited. * As for the true peaks mode, it just simplifies the resampling buffer @@ -634,20 +649,20 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) continue; /* Y[i] = X[i]*b0 + X[i-1]*b1 + X[i-2]*b2 - Y[i-1]*a1 - Y[i-2]*a2 */ -#define FILTER(Y, X, name) do { \ +#define FILTER(Y, X, NUM, DEN) do { \ double *dst = ebur128->Y + ch*3; \ double *src = ebur128->X + ch*3; \ dst[2] = dst[1]; \ dst[1] = dst[0]; \ - dst[0] = src[0]*name##_B0 + src[1]*name##_B1 + src[2]*name##_B2 \ - - dst[1]*name##_A1 - dst[2]*name##_A2; \ + dst[0] = src[0]*NUM[0] + src[1]*NUM[1] + src[2]*NUM[2] \ + - dst[1]*DEN[1] - dst[2]*DEN[2]; \ } while (0) // TODO: merge both filters in one? - FILTER(y, x, PRE); // apply pre-filter + FILTER(y, x, ebur128->pre_b, ebur128->pre_a); // apply pre-filter ebur128->x[ch * 3 + 2] = ebur128->x[ch * 3 + 1]; ebur128->x[ch * 3 + 1] = ebur128->x[ch * 3 ]; - FILTER(z, y, RLB); // apply RLB-filter + FILTER(z, y, ebur128->rlb_b, ebur128->rlb_a); // apply RLB-filter bin = ebur128->z[ch * 3] * ebur128->z[ch * 3]; @@ -896,7 +911,6 @@ static int query_formats(AVFilterContext *ctx) int ret; static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE }; - static const int input_srate[] = {48000, -1}; // ITU-R BS.1770 provides coeff only for 48kHz static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE }; /* set optional output video format */ @@ -920,7 +934,7 @@ static int query_formats(AVFilterContext *ctx) (ret = ff_channel_layouts_ref(layouts, &outlink->incfg.channel_layouts)) < 0) return ret; - formats = ff_make_format_list(input_srate); + formats = ff_all_samplerates(); if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0 || (ret = ff_formats_ref(formats, &outlink->incfg.samplerates)) < 0) return ret;