From patchwork Fri Sep 23 23:18:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lynne X-Patchwork-Id: 38202 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp791556pzh; Fri, 23 Sep 2022 16:18:30 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7ByzouvweL82LsydE4riXE+2PQok0UiJTkO9ekBqag2mjSzOb9efKS90eDFIj3SbCkD7Z0 X-Received: by 2002:a50:fa8c:0:b0:456:cf8e:40fd with SMTP id w12-20020a50fa8c000000b00456cf8e40fdmr4527041edr.365.1663975109905; Fri, 23 Sep 2022 16:18:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663975109; cv=none; d=google.com; s=arc-20160816; b=BSaKvSFHGwi1h6UtSp0tiSCdPWzzxDPK/vU1Vd/0iQzZScsb2ObPwqf3K3Hxl47Ic5 qVsGYmSW9teDEq8aZAoXqawyJOG6qzVQ9nfzWPhrzo6Zyuucc+M8qj70thvUG3ABm4ec 1UE5pazdokAxehIKVX+mZ9gjPZOeAvxs9V+JPM+EAeyBVZP4r89JswQBsLhwcK7TylRE qRJgshVSmQibEq65LQeftDGY8IBV8BTcnwveutUBffIj7YUxdf2oAZePB8OX7FZfrhFE 3g1OJH5EM7u6aplNzguqZT3IyoXuR5BaPCkxxCAPAv/Cgy0UmlE+kFMPH8BzK681qjv3 4GiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:to:from:date :dkim-signature:delivered-to; bh=51U8J93uo3B3nB/hjlyhEYLF6y2GCZKp4dB81Pq1XIw=; b=BqfhKxbA6seZar2PDUtOI6FndyZX9DQ8Kltb/qUkfV1kRUG7nqWPcsa95gZwPGutBU iQJvlLfuI7KDrdc6XTrLDAXyYzWJXVnnPoF3iCRvfmq8WWKi5p7fcuecIN7bl5ZbOWKy kD3daK5EIHdscy6Dgn8HfKXZss0tQW3VCVTtlcT44PqbLoZQqahvxjhqLJfvMk7tW6HU /blyZPL/tnwJ/ZcnyOmUpEoPjK3lTZo8JMJQy5odX1j6unpRAmN0pk45v1bPwc9twfOi i+twQyS5yLE1eMeyUER9sGm/IB+dQbtOibazOzXQBuQY5NnGOb6Z2j11uL9OfojDni2t utgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@lynne.ee header.s=s1 header.b=c2kAtucV; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=lynne.ee Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id u28-20020a50d51c000000b00451280d355bsi8573701edi.520.2022.09.23.16.18.29; Fri, 23 Sep 2022 16:18:29 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@lynne.ee header.s=s1 header.b=c2kAtucV; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=lynne.ee Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1685368BB94; Sat, 24 Sep 2022 02:18:27 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from w4.tutanota.de (w4.tutanota.de [81.3.6.165]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5131B68BB5C for ; Sat, 24 Sep 2022 02:18:20 +0300 (EEST) Received: from w3.tutanota.de (unknown [192.168.1.164]) by w4.tutanota.de (Postfix) with ESMTP id 05E8D106016E for ; Fri, 23 Sep 2022 23:18:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1663975099; s=s1; d=lynne.ee; h=From:From:To:To:Subject:Subject:Content-Description:Content-ID:Content-Type:Content-Type:Content-Transfer-Encoding:Cc:Date:Date:In-Reply-To:In-Reply-To:MIME-Version:MIME-Version:Message-ID:Message-ID:Reply-To:References:References:Sender; bh=rvZm5sCnAXlz7fkAu4xPBviK3yp/BgsIY5VowbJZabM=; b=c2kAtucVM0rUksja6bj8WUHEla04+Pg/kUMgIzgkfnFPgsG7yQiVTLbq8Gr1dJ+z TLXz5b5+Cg3vklv+WqNFl1fxXbPUN50kh4+4mxapmrzRmCAYWn0sj4ORQe4S7D7Gnr1 Xpq0qMCbGhVJruIDcRFeSUAgAyguSBGCg9zz5WQ0ldRk0Sp9UQRqac1ZUNW8NXx8I6i kdHNlPaFvcKTU1sJq2dgnv3gn11amLBbc9FAncw/gzP34s/LZkgKlSrIoPz3AJ32cwR jO2CZ368AWzN6twtLemyTVkvdOnbKZU57KK+/fZtHY9LPLOTEy+dOPsEst14r3s2hI8 emCqjf3k0Q== Date: Sat, 24 Sep 2022 01:18:19 +0200 (CEST) From: Lynne To: FFmpeg development discussions and patches Message-ID: In-Reply-To: References: MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/6] ac3: convert encoder and decoder to lavu/tx 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: U+RGFa7qbaQT The fixed-point transforms are much better, faster, and more accurate. Patch attached. From e54775cc93ceb27d9faabe1ddf9b1eacb269826b Mon Sep 17 00:00:00 2001 From: Lynne Date: Sat, 24 Sep 2022 01:05:19 +0200 Subject: [PATCH 3/6] ac3: convert encoder and decoder to lavu/tx The fixed-point transforms are much better, faster, and more accurate. --- libavcodec/ac3dec.c | 22 +++++++++++++--------- libavcodec/ac3dec.h | 6 +++--- libavcodec/ac3dec_fixed.c | 3 ++- libavcodec/ac3dec_float.c | 1 + libavcodec/ac3enc.c | 2 +- libavcodec/ac3enc.h | 7 ++++--- libavcodec/ac3enc_fixed.c | 19 ++++--------------- libavcodec/ac3enc_float.c | 18 +++--------------- libavcodec/ac3enc_template.c | 4 ++-- 9 files changed, 33 insertions(+), 49 deletions(-) diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c index aba8e0fb7f..cd3320caa0 100644 --- a/libavcodec/ac3dec.c +++ b/libavcodec/ac3dec.c @@ -217,13 +217,17 @@ static av_cold int ac3_decode_init(AVCodecContext *avctx) { static AVOnce init_static_once = AV_ONCE_INIT; AC3DecodeContext *s = avctx->priv_data; + const float scale = 1.0f; int i, ret; s->avctx = avctx; - if ((ret = ff_mdct_init(&s->imdct_256, 8, 1, 1.0)) < 0 || - (ret = ff_mdct_init(&s->imdct_512, 9, 1, 1.0)) < 0) + if ((ret = av_tx_init(&s->tx_128, &s->tx_fn_128, IMDCT_TYPE, 1, 128, &scale, 0))) return ret; + + if ((ret = av_tx_init(&s->tx_256, &s->tx_fn_256, IMDCT_TYPE, 1, 256, &scale, 0))) + return ret; + AC3_RENAME(ff_kbd_window_init)(s->window, 5.0, 256); ff_bswapdsp_init(&s->bdsp); @@ -721,10 +725,10 @@ static inline void do_imdct(AC3DecodeContext *s, int channels, int offset) for (ch = 1; ch <= channels; ch++) { if (s->block_switch[ch]) { int i; - FFTSample *x = s->tmp_output + 128; + INTFLOAT *x = s->tmp_output + 128; for (i = 0; i < 128; i++) x[i] = s->transform_coeffs[ch][2 * i]; - s->imdct_256.imdct_half(&s->imdct_256, s->tmp_output, x); + s->tx_fn_128(s->tx_128, s->tmp_output, x, sizeof(INTFLOAT)); #if USE_FIXED s->fdsp->vector_fmul_window_scaled(s->outptr[ch - 1], s->delay[ch - 1 + offset], s->tmp_output, s->window, 128, 8); @@ -734,9 +738,9 @@ static inline void do_imdct(AC3DecodeContext *s, int channels, int offset) #endif for (i = 0; i < 128; i++) x[i] = s->transform_coeffs[ch][2 * i + 1]; - s->imdct_256.imdct_half(&s->imdct_256, s->delay[ch - 1 + offset], x); + s->tx_fn_256(s->tx_256, s->delay[ch - 1 + offset], x, sizeof(INTFLOAT)); } else { - s->imdct_512.imdct_half(&s->imdct_512, s->tmp_output, s->transform_coeffs[ch]); + s->tx_fn_256(s->tx_256, s->tmp_output, s->transform_coeffs[ch], sizeof(INTFLOAT)); #if USE_FIXED s->fdsp->vector_fmul_window_scaled(s->outptr[ch - 1], s->delay[ch - 1 + offset], s->tmp_output, s->window, 128, 8); @@ -744,7 +748,7 @@ static inline void do_imdct(AC3DecodeContext *s, int channels, int offset) s->fdsp->vector_fmul_window(s->outptr[ch - 1], s->delay[ch - 1 + offset], s->tmp_output, s->window, 128); #endif - memcpy(s->delay[ch - 1 + offset], s->tmp_output + 128, 128 * sizeof(FFTSample)); + memcpy(s->delay[ch - 1 + offset], s->tmp_output + 128, 128 * sizeof(INTFLOAT)); } } } @@ -1865,8 +1869,8 @@ skip: static av_cold int ac3_decode_end(AVCodecContext *avctx) { AC3DecodeContext *s = avctx->priv_data; - ff_mdct_end(&s->imdct_512); - ff_mdct_end(&s->imdct_256); + av_tx_uninit(&s->tx_256); + av_tx_uninit(&s->tx_128); av_freep(&s->fdsp); av_freep(&s->downmix_coeffs[0]); diff --git a/libavcodec/ac3dec.h b/libavcodec/ac3dec.h index 88651ae61f..138b462abb 100644 --- a/libavcodec/ac3dec.h +++ b/libavcodec/ac3dec.h @@ -50,6 +50,7 @@ #ifndef AVCODEC_AC3DEC_H #define AVCODEC_AC3DEC_H +#include "libavutil/tx.h" #include "libavutil/float_dsp.h" #include "libavutil/fixed_dsp.h" #include "libavutil/lfg.h" @@ -60,7 +61,6 @@ #include "avcodec.h" #include "bswapdsp.h" #include "get_bits.h" -#include "fft.h" #include "fmtconvert.h" #define AC3_OUTPUT_LFEON 8 @@ -223,8 +223,8 @@ typedef struct AC3DecodeContext { ///@name IMDCT int block_switch[AC3_MAX_CHANNELS]; ///< block switch flags (blksw) - FFTContext imdct_512; ///< for 512 sample IMDCT - FFTContext imdct_256; ///< for 256 sample IMDCT + AVTXContext *tx_128, *tx_256; + av_tx_fn tx_fn_128, tx_fn_256; ///@} ///@name Optimization diff --git a/libavcodec/ac3dec_fixed.c b/libavcodec/ac3dec_fixed.c index 0a7ae6cfbf..c9e5cda69c 100644 --- a/libavcodec/ac3dec_fixed.c +++ b/libavcodec/ac3dec_fixed.c @@ -47,11 +47,12 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#define FFT_FLOAT 0 #define USE_FIXED 1 #include "ac3dec.h" #include "codec_internal.h" +#define IMDCT_TYPE AV_TX_INT32_MDCT +#include "ac3dec.h" static const int end_freq_inv_tab[8] = { diff --git a/libavcodec/ac3dec_float.c b/libavcodec/ac3dec_float.c index 8c1adb3e01..b8868d8ee1 100644 --- a/libavcodec/ac3dec_float.c +++ b/libavcodec/ac3dec_float.c @@ -29,6 +29,7 @@ */ #include "config_components.h" +#define IMDCT_TYPE AV_TX_FLOAT_MDCT #include "ac3dec.h" #include "codec_internal.h" diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c index a090576823..fbedf40d20 100644 --- a/libavcodec/ac3enc.c +++ b/libavcodec/ac3enc.c @@ -2203,7 +2203,7 @@ av_cold int ff_ac3_encode_close(AVCodecContext *avctx) av_freep(&block->cpl_coord_mant); } - s->mdct_end(s); + av_tx_uninit(&s->tx); return 0; } diff --git a/libavcodec/ac3enc.h b/libavcodec/ac3enc.h index f0dc006759..55e88d69e4 100644 --- a/libavcodec/ac3enc.h +++ b/libavcodec/ac3enc.h @@ -31,12 +31,13 @@ #include #include "libavutil/opt.h" +#include "libavutil/tx.h" + #include "ac3.h" #include "ac3defs.h" #include "ac3dsp.h" #include "avcodec.h" #include "codec_internal.h" -#include "fft.h" #include "mathops.h" #include "me_cmp.h" #include "put_bits.h" @@ -167,7 +168,8 @@ typedef struct AC3EncodeContext { #endif MECmpContext mecc; AC3DSPContext ac3dsp; ///< AC-3 optimized functions - FFTContext mdct; ///< FFT context for MDCT calculation + AVTXContext *tx; ///< FFT context for MDCT calculation + av_tx_fn tx_fn; const SampleType *mdct_window; ///< MDCT window function array AC3Block blocks[AC3_MAX_BLOCKS]; ///< per-block info @@ -257,7 +259,6 @@ typedef struct AC3EncodeContext { int warned_alternate_bitstream; /* fixed vs. float function pointers */ - void (*mdct_end)(struct AC3EncodeContext *s); int (*mdct_init)(struct AC3EncodeContext *s); /* fixed vs. float templated function pointers */ diff --git a/libavcodec/ac3enc_fixed.c b/libavcodec/ac3enc_fixed.c index a22d3b4abf..76e5392733 100644 --- a/libavcodec/ac3enc_fixed.c +++ b/libavcodec/ac3enc_fixed.c @@ -27,7 +27,7 @@ */ #define AC3ENC_FLOAT 0 -#define FFT_FLOAT 0 +#include "internal.h" #include "audiodsp.h" #include "ac3enc.h" #include "codec_internal.h" @@ -66,20 +66,8 @@ static CoefType calc_cpl_coord(CoefSumType energy_ch, CoefSumType energy_cpl) } } - #include "ac3enc_template.c" - -/** - * Finalize MDCT and free allocated memory. - * - * @param s AC-3 encoder private context - */ -static av_cold void ac3_fixed_mdct_end(AC3EncodeContext *s) -{ - ff_mdct_end(&s->mdct); -} - /** * Initialize MDCT tables. * @@ -89,6 +77,7 @@ static av_cold void ac3_fixed_mdct_end(AC3EncodeContext *s) static av_cold int ac3_fixed_mdct_init(AC3EncodeContext *s) { float fwin[AC3_BLOCK_SIZE]; + const float scale = -1.0f; int32_t *iwin = av_malloc_array(AC3_BLOCK_SIZE, sizeof(*iwin)); if (!iwin) @@ -104,7 +93,8 @@ static av_cold int ac3_fixed_mdct_init(AC3EncodeContext *s) if (!s->fdsp) return AVERROR(ENOMEM); - return ff_mdct_init(&s->mdct, 9, 0, -1.0); + return av_tx_init(&s->tx, &s->tx_fn, AV_TX_INT32_MDCT, 0, + AC3_BLOCK_SIZE, &scale, 0); } @@ -112,7 +102,6 @@ static av_cold int ac3_fixed_encode_init(AVCodecContext *avctx) { AC3EncodeContext *s = avctx->priv_data; s->fixed_point = 1; - s->mdct_end = ac3_fixed_mdct_end; s->mdct_init = ac3_fixed_mdct_init; s->allocate_sample_buffers = allocate_sample_buffers; return ff_ac3_encode_init(avctx); diff --git a/libavcodec/ac3enc_float.c b/libavcodec/ac3enc_float.c index 6238980690..8a3f605b48 100644 --- a/libavcodec/ac3enc_float.c +++ b/libavcodec/ac3enc_float.c @@ -75,21 +75,8 @@ static void sum_square_butterfly(AC3EncodeContext *s, float sum[4], s->ac3dsp.sum_square_butterfly_float(sum, coef0, coef1, len); } - #include "ac3enc_template.c" - -/** - * Finalize MDCT and free allocated memory. - * - * @param s AC-3 encoder private context - */ -static av_cold void ac3_float_mdct_end(AC3EncodeContext *s) -{ - ff_mdct_end(&s->mdct); -} - - /** * Initialize MDCT tables. * @@ -98,6 +85,7 @@ static av_cold void ac3_float_mdct_end(AC3EncodeContext *s) */ static av_cold int ac3_float_mdct_init(AC3EncodeContext *s) { + const float scale = -2.0 / AC3_WINDOW_SIZE; float *window = av_malloc_array(AC3_BLOCK_SIZE, sizeof(*window)); if (!window) { av_log(s->avctx, AV_LOG_ERROR, "Cannot allocate memory.\n"); @@ -107,14 +95,14 @@ static av_cold int ac3_float_mdct_init(AC3EncodeContext *s) ff_kbd_window_init(window, 5.0, AC3_BLOCK_SIZE); s->mdct_window = window; - return ff_mdct_init(&s->mdct, 9, 0, -2.0 / AC3_WINDOW_SIZE); + return av_tx_init(&s->tx, &s->tx_fn, AV_TX_FLOAT_MDCT, 0, + AC3_BLOCK_SIZE, &scale, 0); } av_cold int ff_ac3_float_encode_init(AVCodecContext *avctx) { AC3EncodeContext *s = avctx->priv_data; - s->mdct_end = ac3_float_mdct_end; s->mdct_init = ac3_float_mdct_init; s->allocate_sample_buffers = allocate_sample_buffers; s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT); diff --git a/libavcodec/ac3enc_template.c b/libavcodec/ac3enc_template.c index de66964d0d..be4ecebc9c 100644 --- a/libavcodec/ac3enc_template.c +++ b/libavcodec/ac3enc_template.c @@ -98,8 +98,8 @@ static void apply_mdct(AC3EncodeContext *s) &input_samples[AC3_BLOCK_SIZE], s->mdct_window, AC3_BLOCK_SIZE); - s->mdct.mdct_calc(&s->mdct, block->mdct_coef[ch+1], - s->windowed_samples); + s->tx_fn(s->tx, block->mdct_coef[ch+1], + s->windowed_samples, sizeof(float)); } } } -- 2.37.2.609.g9ff673ca1a