Commit 1d682b3c3c for ffmpeg

commit 1d682b3c3cb204ab2adb743c847da619f4e76459
Author: Rainbaby <r@bgr.jp>
Date:   Sun Nov 29 01:10:15 2026 +0900

    avcodec/aacsbr: decouple the SBR core from AACDecContext

    Decoding and applying an SBR element need only a few facts from the
    decoder: whether Parametric Stereo is allowed and applied, whether the
    output is downsampled, and the DSP context. Pass these in, and move the
    reads and writes of AACDecContext into the two entry points, which
    become wrappers around ff_sbr_decode_extension() and ff_sbr_apply().

    Signed-off-by: Rainbaby <r@bgr.jp>

diff --git a/libavcodec/aacsbr_template.c b/libavcodec/aacsbr_template.c
index f3d7951d30..8c5d703b5d 100644
--- a/libavcodec/aacsbr_template.c
+++ b/libavcodec/aacsbr_template.c
@@ -986,21 +986,19 @@ static int read_sbr_noise(SpectralBandReplication *sbr, GetBitContext *gb,
     return 0;
 }

-static void read_sbr_extension(AACDecContext *ac, SpectralBandReplication *sbr,
-                               GetBitContext *gb,
-                               int bs_extension_id, int *num_bits_left)
+static void read_sbr_extension(SpectralBandReplication *sbr, GetBitContext *gb,
+                               int bs_extension_id, int *num_bits_left,
+                               int ps_allowed, int *ps_found)
 {
     switch (bs_extension_id) {
     case EXTENSION_ID_PS:
-        if (!ac->oc[1].m4ac.ps) {
+        if (!ps_allowed) {
             av_log(sbr->logctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
             skip_bits_long(gb, *num_bits_left); // bs_fill_bits
             *num_bits_left = 0;
         } else {
             *num_bits_left -= ff_ps_read_data(sbr->logctx, gb, &sbr->ps.common, *num_bits_left);
-            ac->avctx->profile = AV_PROFILE_AAC_HE_V2;
-            // ensure the warning is not printed if PS extension is present
-            ac->warned_he_aac_mono = 1;
+            *ps_found = 1;
         }
         break;
     default:
@@ -1087,8 +1085,9 @@ static int read_sbr_channel_pair_element(SpectralBandReplication *sbr,
     return 0;
 }

-static unsigned int read_sbr_data(AACDecContext *ac, SpectralBandReplication *sbr,
-                                  GetBitContext *gb, int id_aac, int numTimeSlots)
+static unsigned int read_sbr_data(SpectralBandReplication *sbr, GetBitContext *gb,
+                                  int id_aac, int numTimeSlots,
+                                  int ps_allowed, int *ps_found)
 {
     unsigned int cnt = get_bits_count(gb);

@@ -1119,7 +1118,8 @@ static unsigned int read_sbr_data(AACDecContext *ac, SpectralBandReplication *sb
         num_bits_left <<= 3;
         while (num_bits_left > 7) {
             num_bits_left -= 2;
-            read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left); // bs_extension_id
+            read_sbr_extension(sbr, gb, get_bits(gb, 2), &num_bits_left, // bs_extension_id
+                               ps_allowed, ps_found);
         }
         if (num_bits_left < 0) {
             av_log(sbr->logctx, AV_LOG_ERROR, "SBR Extension over read.\n");
@@ -1144,19 +1144,11 @@ static void sbr_reset(SpectralBandReplication *sbr)
     }
 }

-/**
- * Decode Spectral Band Replication extension data; reference: table 4.55.
- *
- * @param   crc flag indicating the presence of CRC checksum
- * @param   cnt length of TYPE_FIL syntactic element in bytes
- *
- * @return  Returns number of bytes consumed from the TYPE_FIL element.
- */
-int AAC_RENAME(ff_aac_sbr_decode_extension)(AACDecContext *ac, ChannelElement *che,
-                                            GetBitContext *gb_host, int crc,
-                                            int cnt, int id_aac, int fl960)
+int AAC_RENAME(ff_sbr_decode_extension)(SpectralBandReplication *sbr,
+                                        GetBitContext *gb_host, int crc,
+                                        int cnt, int id_aac, int fl960,
+                                        int ps_allowed, int *ps_found)
 {
-    SpectralBandReplication *sbr = get_sbr(che);
     unsigned int num_sbr_bits = 0, num_align_bits;
     unsigned bytes_read;
     GetBitContext gbc = *gb_host, *gb = &gbc;
@@ -1165,11 +1157,6 @@ int AAC_RENAME(ff_aac_sbr_decode_extension)(AACDecContext *ac, ChannelElement *c

     sbr->reset = 0;

-    if (!sbr->sample_rate)
-        sbr->sample_rate = 2 * ac->oc[1].m4ac.sample_rate; //TODO use the nominal sample rate for arbitrary sample rate support
-    if (!ac->oc[1].m4ac.ext_sample_rate)
-        ac->oc[1].m4ac.ext_sample_rate = 2 * ac->oc[1].m4ac.sample_rate;
-
     if (crc) {
         skip_bits(gb, 10); // bs_sbr_crc_bits; TODO - implement CRC check
         num_sbr_bits += 10;
@@ -1188,7 +1175,8 @@ int AAC_RENAME(ff_aac_sbr_decode_extension)(AACDecContext *ac, ChannelElement *c
         sbr_reset(sbr);

     if (sbr->start)
-        num_sbr_bits += read_sbr_data(ac, sbr, gb, id_aac, numTimeSlots);
+        num_sbr_bits += read_sbr_data(sbr, gb, id_aac, numTimeSlots,
+                                      ps_allowed, ps_found);

     num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
     bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
@@ -1201,6 +1189,30 @@ int AAC_RENAME(ff_aac_sbr_decode_extension)(AACDecContext *ac, ChannelElement *c
     return cnt;
 }

+int AAC_RENAME(ff_aac_sbr_decode_extension)(AACDecContext *ac, ChannelElement *che,
+                                            GetBitContext *gb, int crc,
+                                            int cnt, int id_aac, int fl960)
+{
+    SpectralBandReplication *sbr = get_sbr(che);
+    int ps_found = 0;
+    int ret;
+
+    if (!sbr->sample_rate)
+        sbr->sample_rate = 2 * ac->oc[1].m4ac.sample_rate; //TODO use the nominal sample rate for arbitrary sample rate support
+    if (!ac->oc[1].m4ac.ext_sample_rate)
+        ac->oc[1].m4ac.ext_sample_rate = 2 * ac->oc[1].m4ac.sample_rate;
+
+    ret = AAC_RENAME(ff_sbr_decode_extension)(sbr, gb, crc, cnt, id_aac, fl960,
+                                              ac->oc[1].m4ac.ps, &ps_found);
+    if (ps_found) {
+        ac->avctx->profile = AV_PROFILE_AAC_HE_V2;
+        // ensure the warning is not printed if PS extension is present
+        ac->warned_he_aac_mono = 1;
+    }
+
+    return ret;
+}
+
 #if !USE_FIXED
 static void copy_usac_default_header(SpectralBandReplication *sbr,
                                      AACUsacElemConfig *ue)
@@ -1716,12 +1728,14 @@ 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, int fl960, void *L_, void *R_)
+#if USE_FIXED
+void AAC_RENAME(ff_sbr_apply)(SpectralBandReplication *sbr, AVFixedDSPContext *fdsp,
+#else
+void AAC_RENAME(ff_sbr_apply)(SpectralBandReplication *sbr, AVFloatDSPContext *fdsp,
+#endif /* USE_FIXED */
+                              int id_aac, int fl960, int downsampled, int ps,
+                              INTFLOAT *L, INTFLOAT *R)
 {
-    INTFLOAT *L = L_, *R = R_;
-    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;
     int err;
@@ -1752,7 +1766,7 @@ void AAC_RENAME(ff_aac_sbr_apply)(AACDecContext *ac, ChannelElement *che,
     }
     for (ch = 0; ch < nch; ch++) {
         /* decode channel */
-        sbr_qmf_analysis(ac->fdsp, sbr->mdct_ana, sbr->mdct_ana_fn, &sbr->dsp,
+        sbr_qmf_analysis(fdsp, sbr->mdct_ana, sbr->mdct_ana_fn, &sbr->dsp,
                          ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
                          (INTFLOAT*)sbr->qmf_filter_scratch,
                          sbr->data[ch].W, sbr->data[ch].Ypos, numTimeSlots);
@@ -1791,7 +1805,7 @@ void AAC_RENAME(ff_aac_sbr_apply)(AACDecContext *ac, ChannelElement *che,
                          (const INTFLOAT (*)[40][2]) sbr->X_low, ch, numTimeSlots);
     }

-    if (ac->oc[1].m4ac.ps == 1) {
+    if (ps) {
         if (sbr->ps.common.start) {
             AAC_RENAME(ff_ps_apply)(&sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);
         } else {
@@ -1800,19 +1814,29 @@ void AAC_RENAME(ff_aac_sbr_apply)(AACDecContext *ac, ChannelElement *che,
         nch = 2;
     }

-    sbr_qmf_synthesis(sbr->mdct, sbr->mdct_fn, &sbr->dsp, ac->fdsp,
+    sbr_qmf_synthesis(sbr->mdct, sbr->mdct_fn, &sbr->dsp, fdsp,
                       L, sbr->X[0], sbr->qmf_filter_scratch,
                       sbr->data[0].synthesis_filterbank_samples,
                       &sbr->data[0].synthesis_filterbank_samples_offset,
                       numTimeSlots, downsampled);
     if (nch == 2)
-        sbr_qmf_synthesis(sbr->mdct, sbr->mdct_fn, &sbr->dsp, ac->fdsp,
+        sbr_qmf_synthesis(sbr->mdct, sbr->mdct_fn, &sbr->dsp, fdsp,
                           R, sbr->X[1], sbr->qmf_filter_scratch,
                           sbr->data[1].synthesis_filterbank_samples,
                           &sbr->data[1].synthesis_filterbank_samples_offset,
                           numTimeSlots, downsampled);
 }

+void AAC_RENAME(ff_aac_sbr_apply)(AACDecContext *ac, ChannelElement *che,
+                                  int id_aac, int fl960, void *L, void *R)
+{
+    SpectralBandReplication *sbr = get_sbr(che);
+    int downsampled = ac->oc[1].m4ac.ext_sample_rate < sbr->sample_rate;
+
+    AAC_RENAME(ff_sbr_apply)(sbr, ac->fdsp, id_aac, fl960, downsampled,
+                             ac->oc[1].m4ac.ps == 1, L, R);
+}
+
 static void aacsbr_func_ptr_init(AACSBRContext *c)
 {
     c->sbr_lf_gen            = sbr_lf_gen;
diff --git a/libavcodec/sbr.h b/libavcodec/sbr.h
index 3654717875..8373e148aa 100644
--- a/libavcodec/sbr.h
+++ b/libavcodec/sbr.h
@@ -31,6 +31,9 @@

 #include <stdint.h>

+#include "libavutil/attributes_internal.h"
+#include "libavutil/fixed_dsp.h"
+#include "libavutil/float_dsp.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/tx.h"

@@ -224,4 +227,36 @@ struct SpectralBandReplication {
     void              *logctx;
 };

+FF_VISIBILITY_PUSH_HIDDEN
+/**
+ * Decode Spectral Band Replication extension data; reference: table 4.55.
+ * sbr->sample_rate must be set. gb is positioned after the 4 bit
+ * extension_type, which cnt includes.
+ *
+ * @param   crc flag indicating the presence of CRC checksum
+ * @param   cnt length of TYPE_FIL syntactic element in bytes
+ * @param   ps_found set to 1 if Parametric Stereo data was read, left
+ *                   unchanged otherwise
+ *
+ * @return  Returns number of bytes consumed from the TYPE_FIL element.
+ */
+int AAC_RENAME(ff_sbr_decode_extension)(SpectralBandReplication *sbr,
+                                        GetBitContext *gb, int crc,
+                                        int cnt, int id_aac, int fl960,
+                                        int ps_allowed, int *ps_found);
+
+/**
+ * Apply one SBR element in place. L and R need room for 2048 samples and
+ * the alignment of an output of fdsp->vector_fmul(); R is also written for
+ * a single channel if ps is set. downsampled is 0 or 1.
+ */
+#if USE_FIXED
+void AAC_RENAME(ff_sbr_apply)(SpectralBandReplication *sbr, AVFixedDSPContext *fdsp,
+#else
+void AAC_RENAME(ff_sbr_apply)(SpectralBandReplication *sbr, AVFloatDSPContext *fdsp,
+#endif /* USE_FIXED */
+                              int id_aac, int fl960, int downsampled, int ps,
+                              INTFLOAT *L, INTFLOAT *R);
+FF_VISIBILITY_POP_HIDDEN
+
 #endif /* AVCODEC_SBR_H */