From e54775cc93ceb27d9faabe1ddf9b1eacb269826b Mon Sep 17 00:00:00 2001
From: Lynne <dev@lynne.ee>
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(-)
@@ -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]);
@@ -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
@@ -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] =
{
@@ -29,6 +29,7 @@
*/
#include "config_components.h"
+#define IMDCT_TYPE AV_TX_FLOAT_MDCT
#include "ac3dec.h"
#include "codec_internal.h"
@@ -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;
}
@@ -31,12 +31,13 @@
#include <stdint.h>
#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 */
@@ -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);
@@ -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);
@@ -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