From 5bd7b8d9995bfec13172993457d6c59b45b5ec01 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Thu, 29 Feb 2024 16:42:01 +0100 Subject: [PATCH] avcodec/aacdec: Split SBR context from ChannelElement The AAC fixed-point and floating-point decoders have a lot of duplicated code; the main obstacle to deduplicating it is that several structures with the same name are actually different types, because they contain INTFLOATs (int or float) and AAC_FLOATs (SoftFloat or float). SoftFloat and float typically have different sizes, so dealing with it is the more complicated of the two. AAC_FLOAT is mainly used in the sbr code and structures, so one can still deduplicate the code by only exposing the common part of ChannelElement (without SBR context) to the common decoder part. One prerequisite of this is to move allocating the whole ChannelElement to code that will stay unduplicated. It is most natural to move said allocation to ff_aac_sbr_ctx_init(). Signed-off-by: Andreas Rheinhardt --- libavcodec/aacdec.h | 2 -- libavcodec/aacdec_template.c | 5 +---- libavcodec/aacsbr.h | 7 +++++-- libavcodec/aacsbr_template.c | 28 +++++++++++++++++++++------- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/libavcodec/aacdec.h b/libavcodec/aacdec.h index 1b245f9258..e8d3297d17 100644 --- a/libavcodec/aacdec.h +++ b/libavcodec/aacdec.h @@ -41,7 +41,6 @@ #include "aac.h" #include "aac_defines.h" #include "mpeg4audio.h" -#include "sbr.h" /** * Output configuration status @@ -153,7 +152,6 @@ typedef struct ChannelElement { SingleChannelElement ch[2]; // CCE specific ChannelCoupling coup; - SpectralBandReplication sbr; } ChannelElement; typedef struct OutputConfiguration { diff --git a/libavcodec/aacdec_template.c b/libavcodec/aacdec_template.c index 17280a16a1..6362e6c25c 100644 --- a/libavcodec/aacdec_template.c +++ b/libavcodec/aacdec_template.c @@ -134,10 +134,7 @@ static av_cold int che_configure(AACDecContext *ac, return AVERROR_INVALIDDATA; if (che_pos) { if (!ac->che[type][id]) { - int ret; - if (!(ac->che[type][id] = av_mallocz(sizeof(ChannelElement)))) - return AVERROR(ENOMEM); - ret = AAC_RENAME(ff_aac_sbr_ctx_init)(ac, ac->che[type][id], type); + int ret = AAC_RENAME(ff_aac_sbr_ctx_alloc_init)(ac, &ac->che[type][id], type); if (ret < 0) return ret; } diff --git a/libavcodec/aacsbr.h b/libavcodec/aacsbr.h index 36289d23f2..855885ce87 100644 --- a/libavcodec/aacsbr.h +++ b/libavcodec/aacsbr.h @@ -68,8 +68,11 @@ enum { /** Initialize SBR. */ void AAC_RENAME(ff_aac_sbr_init)(void); -/** Initialize one SBR context. */ -int AAC_RENAME(ff_aac_sbr_ctx_init)(AACDecContext *ac, ChannelElement *che, int id_aac); +/** + * Allocate an ExtChannelElement (if necessary) and + * initialize the SBR context contained in it. + */ +int AAC_RENAME(ff_aac_sbr_ctx_alloc_init)(AACDecContext *ac, ChannelElement **che, int id_aac); /** Close one SBR context. */ void AAC_RENAME(ff_aac_sbr_ctx_close)(ChannelElement *che); /** Decode one SBR element. */ diff --git a/libavcodec/aacsbr_template.c b/libavcodec/aacsbr_template.c index 9a29fdc66a..89e8bee75a 100644 --- a/libavcodec/aacsbr_template.c +++ b/libavcodec/aacsbr_template.c @@ -38,6 +38,16 @@ #include "libavutil/qsort.h" #include "libavutil/mem.h" +typedef struct ExtChannelElement { + ChannelElement ch; + SpectralBandReplication sbr; +} ExtChannelElement; + +static inline SpectralBandReplication *get_sbr(ChannelElement *ch) +{ + return &((ExtChannelElement*)ch)->sbr; +} + static av_cold void aacsbr_tableinit(void) { int n; @@ -65,14 +75,18 @@ static void sbr_turnoff(SpectralBandReplication *sbr) { memset(&sbr->spectrum_params, -1, sizeof(SpectrumParameters)); } -av_cold int AAC_RENAME(ff_aac_sbr_ctx_init)(AACDecContext *ac, ChannelElement *che, int id_aac) +av_cold int AAC_RENAME(ff_aac_sbr_ctx_alloc_init)(AACDecContext *ac, + ChannelElement **che, int id_aac) { - SpectralBandReplication *sbr = &che->sbr; + SpectralBandReplication *sbr; + ExtChannelElement *ext = av_mallocz(sizeof(*ext)); int ret; float scale; - if (sbr->mdct) - return 0; + if (!ext) + return AVERROR(ENOMEM); + *che = &ext->ch; + sbr = &ext->sbr; sbr->kx[0] = sbr->kx[1]; sbr->id_aac = id_aac; @@ -106,7 +120,7 @@ av_cold int AAC_RENAME(ff_aac_sbr_ctx_init)(AACDecContext *ac, ChannelElement *c av_cold void AAC_RENAME(ff_aac_sbr_ctx_close)(ChannelElement *che) { - SpectralBandReplication *sbr = &che->sbr; + SpectralBandReplication *sbr = get_sbr(che); av_tx_uninit(&sbr->mdct); av_tx_uninit(&sbr->mdct_ana); } @@ -1097,7 +1111,7 @@ int AAC_RENAME(ff_aac_sbr_decode_extension)(AACDecContext *ac, ChannelElement *c GetBitContext *gb_host, int crc, int cnt, int id_aac) { - SpectralBandReplication *sbr = &che->sbr; + SpectralBandReplication *sbr = get_sbr(che); unsigned int num_sbr_bits = 0, num_align_bits; unsigned bytes_read; GetBitContext gbc = *gb_host, *gb = &gbc; @@ -1464,7 +1478,7 @@ static void sbr_env_estimate(AAC_FLOAT (*e_curr)[48], INTFLOAT X_high[64][40][2] void AAC_RENAME(ff_aac_sbr_apply)(AACDecContext *ac, ChannelElement *che, int id_aac, INTFLOAT* L, INTFLOAT* R) { - SpectralBandReplication *sbr = &che->sbr; + SpectralBandReplication *sbr = get_sbr(che); int downsampled = ac->oc[1].m4ac.ext_sample_rate < sbr->sample_rate; int ch; int nch = (id_aac == TYPE_CPE) ? 2 : 1;