Commit 28c961e85d for ffmpeg

commit 28c961e85da7ef87c2e13bf18c1319d0f02301d3
Author: Patrick Domack <patrickdk@patrickdk.com>
Date:   Sun Nov 1 04:56:47 2026 -0500

    avcodec/dss_sp: support format 1, variable rate SP

    Format 1 is SP at 12000 Hz with a type bit at the start of every frame.
    A frame with the bit set is a 328-bit SP frame, except that the 14th
    filter index has 2 bits and its own 4-entry table. A frame with the bit
    clear is 72 bits: the type bit, the 51 filter bits and four 5-bit noise
    gains. The caller passes the format as one byte of extradata; the next
    commit makes the demuxer do so.

    The decoder reads all four gains of a short frame as 5 bits. In Olympus
    encodes of ten test clips, 30 of 1280 short frames carry a value above 7
    in the third gain, and the 5-bit value tracks the neighboring gains
    twice as closely as its low 3 bits do. The decoder uses the filter that
    the short frame carries, keeps one running noise seed, takes the noise
    as signed and scales it by dss_sp_noise_gain. R8's DssDecoder.dll
    1.1.1.0 and the 2007 dss32.dll differ on short frames, and where they
    differ this decoder takes the choice that scores closer to the source
    audio. dss32.dll also gives 264 samples a frame and labels them
    11025 Hz. R8 gives 288 samples a frame at 12000 Hz.

    [Guillain d'Erceville: named the format 1 extradata value
    DSS_ACODEC_DSS_SP_VR, as asked in review]
    Signed-off-by: Patrick Domack <patrickdk@patrickdk.com>
    Signed-off-by: Guillain d'Erceville <guillain@poulpe.us>

diff --git a/libavcodec/dss_sp.c b/libavcodec/dss_sp.c
index a149d5e4dc..7387f3ce88 100644
--- a/libavcodec/dss_sp.c
+++ b/libavcodec/dss_sp.c
@@ -35,6 +35,9 @@
 #define DSS_SP_SAMPLE_COUNT     (66 * SUBFRAMES)
 #define DSS_SP_FORMULA(a, b, c) ((int)((((a) * (1 << 15)) + (b) * (unsigned)(c)) + 0x4000) >> 15)

+/* audio format in the file header, given by the demuxer as extradata */
+#define DSS_ACODEC_DSS_SP_VR 0x1 /* SP mode, variable rate */
+
 typedef struct DssSpSubframe {
     int16_t gain;
     int32_t combined_pulse_pos;
@@ -65,6 +68,10 @@ typedef struct DssSpContext {

     int pulse_dec_mode;

+    int variable_rate;
+    int noise_frame;
+    uint16_t noise_seed;
+
     DECLARE_ALIGNED(16, uint8_t, bits)[DSS_SP_FRAME_SIZE +
                                        AV_INPUT_BUFFER_PADDING_SIZE];
 } DssSpContext;
@@ -275,6 +282,15 @@ static const uint16_t dss_sp_adaptive_gain[] = {
     3194, 3323, 3451, 3580, 3709, 3838, 3967, 4096,
 };

+static const int16_t dss_sp_vr_filter_cb13[4] = {
+    -8166, -2419, 1997, 6705,
+};
+
+static const uint8_t dss_sp_noise_gain[32] = {
+     0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
+    16, 17, 18, 20, 22, 24, 26, 28, 30, 34, 40, 46, 52, 60, 72, 84,
+};
+
 static const int32_t dss_sp_sinc[67] = {
       262,   293,   323,   348,   356,   336,   269,   139,
       -67,  -358,  -733, -1178, -1668, -2162, -2607, -2940,
@@ -303,6 +319,7 @@ static av_cold int dss_sp_decode_init(AVCodecContext *avctx)

     p->pulse_dec_mode = 1;
     p->avctx          = avctx;
+    p->variable_rate  = avctx->extradata_size >= 1 && avctx->extradata[0] == DSS_ACODEC_DSS_SP_VR;

     return 0;
 }
@@ -324,12 +341,23 @@ static void dss_sp_unpack_coeffs(DssSpContext *p, const uint8_t *src)

     init_get_bits(&gb, p->bits, DSS_SP_FRAME_SIZE * 8);

+    /* Variable rate frames start with a type bit; a 0 marks a short frame
+     * that carries only the filter and four noise gains. */
+    p->noise_frame = p->variable_rate && !get_bits1(&gb);
+
     for (i = 0; i < 2; i++)
         fparam->filter_idx[i] = get_bits(&gb, 5);
     for (; i < 8; i++)
         fparam->filter_idx[i] = get_bits(&gb, 4);
-    for (; i < 14; i++)
+    for (; i < 13; i++)
         fparam->filter_idx[i] = get_bits(&gb, 3);
+    fparam->filter_idx[13] = get_bits(&gb, p->variable_rate ? 2 : 3);
+
+    if (p->noise_frame) {
+        for (subframe_idx = 0; subframe_idx < 4; subframe_idx++)
+            fparam->sf_adaptive_gain[subframe_idx] = get_bits(&gb, 5);
+        return;
+    }

     for (subframe_idx = 0; subframe_idx < 4; subframe_idx++) {
         fparam->sf_adaptive_gain[subframe_idx] = get_bits(&gb, 5);
@@ -437,6 +465,8 @@ static void dss_sp_unpack_filter(DssSpContext *p)

     for (i = 0; i < 14; i++)
         p->lpc_filter[i] = dss_sp_filter_cb[i][p->fparam.filter_idx[i]];
+    if (p->variable_rate)
+        p->lpc_filter[13] = dss_sp_vr_filter_cb13[p->fparam.filter_idx[13]];
 }

 static void dss_sp_convert_coeffs(int32_t *lpc_filter, int32_t *coeffs)
@@ -475,6 +505,16 @@ static void dss_sp_add_pulses(int32_t *vector_buf,
                                          0x4000) >> 15;
 }

+static void dss_sp_gen_noise(DssSpContext *p, int32_t *vector, int gain)
+{
+    int i;
+
+    for (i = 0; i < 72; i++) {
+        p->noise_seed = p->noise_seed * 0x209 + 0x103;
+        vector[i]     = (int16_t)p->noise_seed * gain >> 14;
+    }
+}
+
 static void dss_sp_gen_exc(int32_t *vector, int32_t *prev_exc,
                            int pitch_lag, int gain)
 {
@@ -720,16 +760,21 @@ static int dss_sp_decode_one_frame(DssSpContext *p,
     dss_sp_convert_coeffs(p->lpc_filter, p->filter);

     for (j = 0; j < SUBFRAMES; j++) {
-        dss_sp_gen_exc(p->vector_buf, p->history,
-                       p->fparam.pitch_lag[j],
-                       dss_sp_adaptive_gain[p->fparam.sf_adaptive_gain[j]]);
+        if (p->noise_frame) {
+            dss_sp_gen_noise(p, p->vector_buf,
+                             dss_sp_noise_gain[p->fparam.sf_adaptive_gain[j]]);
+        } else {
+            dss_sp_gen_exc(p->vector_buf, p->history,
+                           p->fparam.pitch_lag[j],
+                           dss_sp_adaptive_gain[p->fparam.sf_adaptive_gain[j]]);

-        dss_sp_add_pulses(p->vector_buf, &p->fparam.sf[j]);
+            dss_sp_add_pulses(p->vector_buf, &p->fparam.sf[j]);

-        dss_sp_update_buf(p->vector_buf, p->history);
+            dss_sp_update_buf(p->vector_buf, p->history);

-        for (i = 0; i < 72; i++)
-            p->vector_buf[i] = p->history[72 - i];
+            for (i = 0; i < 72; i++)
+                p->vector_buf[i] = p->history[72 - i];
+        }

         dss_sp_shift_sq_sub(p->filter,
                             p->err_buf2, p->vector_buf);