Commit 19b540e5b5 for ffmpeg

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

    avformat/dss: support format 1, variable rate SP

    Olympus encoder mode 1 writes DSS files with codec byte 1. This 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. Long frames
    take 41 bytes, short frames 9, and both keep the swap-byte packing of
    format 0.

    The demuxer now accepts format 1, sizes each frame from its type bit and
    passes the format to the decoder as one byte of extradata. For a short
    frame the decoder fills each subframe with LCG noise scaled by the gain
    and runs the normal synthesis. The noise does not enter the pitch
    history. dss_sp outputs format 1 as it does format 0, at 11000 Hz with
    264 samples a frame. Seeking finds the block from the frame counts in
    the block headers. The 41-byte estimate used for format 0 lands late
    once short frames appear, at 12.62 s for a 12 s seek in one test file.

    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.

    Mean PESQ against the source over ten speech clips, nine of 20 s and one
    of 10 s, encoded by the Olympus encoder in mode 1:

                      WB     NB
      this decoder    2.651  3.668
      R8              2.645  3.564
      dss32.dll       2.607  3.666

    As a parsing check, the format 1 output scored against R8's output gives
    PESQ WB/NB 3.694/4.300, in line with 3.859/4.280 for format 0. Seeks to four
    points in two files land in the block that holds the target, and the
    packets that follow match the run without a seek. Every other DSS and
    DS2 sample decodes as before.

    fate-dss-format1 decodes olympus-format1.dss, the 28th and 29th audio
    blocks of a 10 s excerpt of a public domain LibriVox reading that the
    ODMS R8 encoder wrote in mode 1. It fails without this commit, since the
    demuxer refused format 1 before.

    olympus-format1-noswap-prev-empty60.dss holds the blocks of
    olympus-format1-noswap-prev.dss, a sample of a later commit, with 60
    more empty format 1 blocks after the file header. Its first block with
    frames starts with a swapped frame, and the swap byte is the second to
    last byte of the empty block in front.
    fate-dss-format1-noswap-prev-empty60 decodes it from the file.
    fate-dss-format1-noswap-prev-empty60-pipe reads it through pipe:0 with
    -blocksize 7 and shares the reference. A walk that does not keep the 2
    bytes in front of each block had no swap byte for that frame from the
    pipe. It dropped the frame and gave the other 17 packets, and the pipe
    test fails with it.

    The tests need these new files in the samples suite:

        dss/olympus-format1.dss
        dss/olympus-format1-noswap-prev-empty60.dss

    [Guillain d'Erceville: split from the DS2 work, so this goes in before
    it; figures for DS2 files were measured with the whole series; Changelog
    entry]

    Signed-off-by: Patrick Domack <patrickdk@patrickdk.com>
    Signed-off-by: Guillain d'Erceville <guillain@poulpe.us>

diff --git a/Changelog b/Changelog
index 5603844316..8731d00514 100644
--- a/Changelog
+++ b/Changelog
@@ -8,6 +8,7 @@ version <next>:
 - No longer disabling GCC autovectorization on PowerPC, RISC-V, MIPS and LoongArch
 - PCM PAMF decoder
 - Super Audio CD (SACD) demuxer
+- DSS variable rate SP (format 1) support

 version 9.1:
 - ASTC image encoding and decoding via libastcenc, with .astc and KTX 1.0 muxers and demuxers
diff --git a/libavformat/dss.c b/libavformat/dss.c
index 29cf7df38c..1ecf3cd270 100644
--- a/libavformat/dss.c
+++ b/libavformat/dss.c
@@ -37,6 +37,7 @@

 #define DSS_HEAD_OFFSET_ACODEC        0x2a4
 #define DSS_ACODEC_DSS_SP             0x0    /* SP mode */
+#define DSS_ACODEC_DSS_SP_VR          0x1    /* SP mode, variable rate */
 #define DSS_ACODEC_G723_1             0x2    /* LP mode */

 #define DSS_HEAD_OFFSET_COMMENT       0x31e
@@ -46,6 +47,7 @@
 #define DSS_AUDIO_BLOCK_HEADER_SIZE   6
 #define DSS_BLOCK_KEEP_BEFORE         2
 #define DSS_FRAME_SIZE                42
+#define DSS_SP_SHORT_FRAME_SIZE       9

 static const uint8_t frame_size[4] = { 24, 20, 4, 1 };

@@ -157,8 +159,13 @@ static int dss_read_header(AVFormatContext *s)
     avio_seek(pb, DSS_HEAD_OFFSET_ACODEC, SEEK_SET);
     ctx->audio_codec = avio_r8(pb);

-    if (ctx->audio_codec == DSS_ACODEC_DSS_SP) {
+    if (ctx->audio_codec == DSS_ACODEC_DSS_SP ||
+        ctx->audio_codec == DSS_ACODEC_DSS_SP_VR) {
         st->codecpar->codec_id    = AV_CODEC_ID_DSS_SP;
+        /* The decoder reads the format to tell variable rate SP apart. */
+        if ((ret = ff_alloc_extradata(st->codecpar, 1)) < 0)
+            return ret;
+        st->codecpar->extradata[0] = ctx->audio_codec;
         /* See libavcodec/dss_sp.c: the decoder runs at 12000 Hz and
          * decimates 11:12, so the output rate is 11000 Hz, not 11025. This
          * also sets the packet time base just below. */
@@ -332,30 +339,32 @@ static int64_t dss_frame_pos(AVFormatContext *s)
     return pos;
 }

-static void dss_sp_byte_swap(DSSDemuxContext *ctx, uint8_t *data)
+/* size is the frame length in bytes, 41 or 9; both are odd, so the parity
+ * flips after every frame. */
+static void dss_sp_byte_swap(DSSDemuxContext *ctx, uint8_t *data, int size)
 {
     int i;

     if (ctx->swap) {
-        for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
+        for (i = 0; i < size - 1; i += 2)
             data[i] = data[i + 4];

-        /* Zero the padding. */
-        data[DSS_FRAME_SIZE] = 0;
         data[1] = ctx->dss_sp_swap_byte;
     } else {
-        ctx->dss_sp_swap_byte = data[DSS_FRAME_SIZE - 2];
+        ctx->dss_sp_swap_byte = data[size - 1];
     }

-    /* make sure byte 40 is always 0 */
-    data[DSS_FRAME_SIZE - 2] = 0;
-    ctx->swap             ^= 1;
+    /* Zero the padding, and make sure the byte past the frame is 0. */
+    memset(data + size + 1, 0, DSS_FRAME_SIZE - size);
+    data[size - 1] = 0;
+    ctx->swap     ^= 1;
 }

 static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     DSSDemuxContext *ctx = s->priv_data;
-    int read_size, ret, buff_offset = 0;
+    int size = DSS_FRAME_SIZE - 1;
+    int ret, done = 0;
     int64_t pos;

     if (ctx->frames_left <= 0) {
@@ -365,12 +374,6 @@ static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
     }
     pos = dss_frame_pos(s);

-    if (ctx->swap) {
-        read_size   = DSS_FRAME_SIZE - 2;
-        buff_offset = 3;
-    } else
-        read_size = DSS_FRAME_SIZE;
-
     ret = av_new_packet(pkt, DSS_FRAME_SIZE);
     if (ret < 0)
         return ret;
@@ -379,14 +382,37 @@ static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
     pkt->pos = pos;
     pkt->stream_index = 0;

-    /* This will write one byte into pkt's padding if buff_offset == 3 */
-    ret = dss_sp_read_payload(s, pkt->data + buff_offset, read_size);
+    /* In variable rate SP the top bit of the frame's first byte, in 16-bit
+     * word order, is clear for a short frame. A swapped frame starts with the
+     * byte carried over from the previous frame; otherwise it is the second
+     * byte read. */
+    if (ctx->audio_codec == DSS_ACODEC_DSS_SP_VR) {
+        if (ctx->swap) {
+            if (ctx->dss_sp_swap_byte >= 0 && !(ctx->dss_sp_swap_byte & 0x80))
+                size = DSS_SP_SHORT_FRAME_SIZE;
+        } else {
+            ret = dss_sp_read_payload(s, pkt->data, 2);
+            if (ret < 0)
+                return AVERROR_EOF; /* no whole frame left */
+            done = 2;
+            if (!(pkt->data[1] & 0x80))
+                size = DSS_SP_SHORT_FRAME_SIZE;
+        }
+    }
+
+    /* A swapped frame reads one byte less than its size, starting at offset
+     * 3, which writes one byte into pkt's padding for a long frame; an
+     * unswapped one reads one byte more. */
+    if (ctx->swap)
+        ret = dss_sp_read_payload(s, pkt->data + 3, size - 1);
+    else
+        ret = dss_sp_read_payload(s, pkt->data + done, size + 1 - done);
     if (ret < 0)
         return ret == AVERROR_EOF ? ret : AVERROR_EOF;

     ctx->frames_left--;

-    dss_sp_byte_swap(ctx, pkt->data);
+    dss_sp_byte_swap(ctx, pkt->data, size);

     if (ctx->dss_sp_swap_byte < 0) {
         return FFERROR_REDO;
@@ -451,7 +477,7 @@ static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     DSSDemuxContext *ctx = s->priv_data;

-    if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
+    if (ctx->audio_codec != DSS_ACODEC_G723_1)
         return dss_sp_read_packet(s, pkt);
     else
         return dss_723_1_read_packet(s, pkt);
@@ -465,7 +491,25 @@ static int dss_read_seek(AVFormatContext *s, int stream_index,
     uint8_t header[DSS_AUDIO_BLOCK_HEADER_SIZE];
     int offset;

-    if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
+    if (ctx->audio_codec == DSS_ACODEC_DSS_SP_VR) {
+        /* Frame sizes vary, so find the block from the per-block frame
+         * counts, taken as in dss_sp_next_block(). */
+        int64_t target = timestamp / 264, frames = 0;
+        int count;
+
+        for (seekto = 0; ; seekto += DSS_BLOCK_SIZE) {
+            ret = avio_seek(s->pb, ctx->dss_header_size + seekto, SEEK_SET);
+            if (ret < 0)
+                return ret;
+            ret = ffio_read_size(s->pb, header, DSS_AUDIO_BLOCK_HEADER_SIZE);
+            if (ret < 0)
+                return ret;
+            count = header[2] || header[5] == 0xff ? header[2] : header[5];
+            if (frames + count > target)
+                break;
+            frames += count;
+        }
+    } else if (ctx->audio_codec != DSS_ACODEC_G723_1)
         seekto = timestamp / 264 * 41 / 506 * 512;
     else
         seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
@@ -485,7 +529,7 @@ static int dss_read_seek(AVFormatContext *s, int stream_index,
     ctx->swap        = !!(header[0] & 0x80);
     ctx->frames_left = 0;
     ctx->block_pos   = seekto - DSS_BLOCK_SIZE;
-    if (ctx->audio_codec == DSS_ACODEC_DSS_SP) {
+    if (ctx->audio_codec != DSS_ACODEC_G723_1) {
         ctx->dss_sp_swap_byte = -1;
         return 0;
     }
diff --git a/tests/fate/audio.mak b/tests/fate/audio.mak
index 6238554b13..c725980f96 100644
--- a/tests/fate/audio.mak
+++ b/tests/fate/audio.mak
@@ -31,6 +31,9 @@ fate-dolby-e: CMD = pcm -i $(TARGET_SAMPLES)/dolby_e/16-11
 fate-dolby-e: CMP = oneoff
 fate-dolby-e: REF = $(SAMPLES)/dolby_e/16-11.pcm

+FATE_SAMPLES_AUDIO-$(call FRAMECRC, DSS, DSS_SP) += fate-dss-format1
+fate-dss-format1: CMD = framecrc -i $(TARGET_SAMPLES)/dss/olympus-format1.dss
+
 FATE_SAMPLES_AUDIO-$(call FRAMECRC, DSS, G723_1, ARESAMPLE_FILTER) += fate-dss-lp
 fate-dss-lp: CMD = framecrc -i $(TARGET_SAMPLES)/dss/lp.dss -frames 30 -af aresample

@@ -44,6 +47,13 @@ FATE_SAMPLES_AUDIO-$(call FRAMECRC, DSS, DSS_SP, PIPE_PROTOCOL) += fate-dss-sp-e
 fate-dss-sp-empty56-pipe: CMD = cat $(SAMPLES)/dss/olympus-sp-empty56.dss | framecrc -blocksize 7 -i pipe:0
 fate-dss-sp-empty56-pipe: REF = $(SRC_PATH)/tests/ref/fate/dss-sp-empty56

+FATE_SAMPLES_AUDIO-$(call FRAMECRC, DSS, DSS_SP) += fate-dss-format1-noswap-prev-empty60
+fate-dss-format1-noswap-prev-empty60: CMD = framecrc -i $(TARGET_SAMPLES)/dss/olympus-format1-noswap-prev-empty60.dss
+
+FATE_SAMPLES_AUDIO-$(call FRAMECRC, DSS, DSS_SP, PIPE_PROTOCOL) += fate-dss-format1-noswap-prev-empty60-pipe
+fate-dss-format1-noswap-prev-empty60-pipe: CMD = cat $(SAMPLES)/dss/olympus-format1-noswap-prev-empty60.dss | framecrc -blocksize 7 -i pipe:0
+fate-dss-format1-noswap-prev-empty60-pipe: REF = $(SRC_PATH)/tests/ref/fate/dss-format1-noswap-prev-empty60
+
 FATE_SAMPLES_AUDIO-$(call FRAMECRC, DSS, DSS_SP) += fate-dss-sp-pause-block
 fate-dss-sp-pause-block: CMD = framecrc -i $(TARGET_SAMPLES)/dss/olympus-sp-pause-block.dss

diff --git a/tests/ref/fate/dss-format1 b/tests/ref/fate/dss-format1
new file mode 100644
index 0000000000..6d1100e155
--- /dev/null
+++ b/tests/ref/fate/dss-format1
@@ -0,0 +1,45 @@
+#tb 0: 1/11000
+#media_type 0: audio
+#codec_id 0: pcm_s16le
+#sample_rate 0: 11000
+#channel_layout_name 0: mono
+0,          0,          0,      264,      528, 0x218204fe
+0,        264,        264,      264,      528, 0x77b50546
+0,        528,        528,      264,      528, 0x76b1054f
+0,        792,        792,      264,      528, 0x216edf89
+0,       1056,       1056,      264,      528, 0x12e90254
+0,       1320,       1320,      264,      528, 0x174de48b
+0,       1584,       1584,      264,      528, 0x4039e257
+0,       1848,       1848,      264,      528, 0x1187138c
+0,       2112,       2112,      264,      528, 0x3d06ecc4
+0,       2376,       2376,      264,      528, 0xad57dea6
+0,       2640,       2640,      264,      528, 0x864d129b
+0,       2904,       2904,      264,      528, 0xd892495d
+0,       3168,       3168,      264,      528, 0xf65623dd
+0,       3432,       3432,      264,      528, 0x4754f817
+0,       3696,       3696,      264,      528, 0xd174dffa
+0,       3960,       3960,      264,      528, 0xfc8e0ab4
+0,       4224,       4224,      264,      528, 0x0ebae164
+0,       4488,       4488,      264,      528, 0xfef4b39a
+0,       4752,       4752,      264,      528, 0x69d6a06a
+0,       5016,       5016,      264,      528, 0x94be1bf9
+0,       5280,       5280,      264,      528, 0xa15b9933
+0,       5544,       5544,      264,      528, 0x82d77f6f
+0,       5808,       5808,      264,      528, 0xd8325a7f
+0,       6072,       6072,      264,      528, 0x267074a5
+0,       6336,       6336,      264,      528, 0xf21fe2a9
+0,       6600,       6600,      264,      528, 0x66963961
+0,       6864,       6864,      264,      528, 0x4cf73ccc
+0,       7128,       7128,      264,      528, 0x307a9484
+0,       7392,       7392,      264,      528, 0xcba13c2c
+0,       7656,       7656,      264,      528, 0xdac59702
+0,       7920,       7920,      264,      528, 0xe9ceaa3d
+0,       8184,       8184,      264,      528, 0x8cf651d8
+0,       8448,       8448,      264,      528, 0xfa5047d4
+0,       8712,       8712,      264,      528, 0x134925cc
+0,       8976,       8976,      264,      528, 0x3693a769
+0,       9240,       9240,      264,      528, 0x20769be3
+0,       9504,       9504,      264,      528, 0x20592c70
+0,       9768,       9768,      264,      528, 0xa6920bf0
+0,      10032,      10032,      264,      528, 0x0bc31224
+0,      10296,      10296,      264,      528, 0x8fd91cf2
diff --git a/tests/ref/fate/dss-format1-noswap-prev-empty60 b/tests/ref/fate/dss-format1-noswap-prev-empty60
new file mode 100644
index 0000000000..b7a3ba32a5
--- /dev/null
+++ b/tests/ref/fate/dss-format1-noswap-prev-empty60
@@ -0,0 +1,23 @@
+#tb 0: 1/11000
+#media_type 0: audio
+#codec_id 0: pcm_s16le
+#sample_rate 0: 11000
+#channel_layout_name 0: mono
+0,          0,          0,      264,      528, 0x7f2edd6d
+0,        264,        264,      264,      528, 0xb69de265
+0,        528,        528,      264,      528, 0xc203f3f6
+0,        792,        792,      264,      528, 0xe68bcefc
+0,       1056,       1056,      264,      528, 0xd1d0ee7e
+0,       1320,       1320,      264,      528, 0xbbb4ecee
+0,       1584,       1584,      264,      528, 0x72e2f230
+0,       1848,       1848,      264,      528, 0xd80fdaba
+0,       2112,       2112,      264,      528, 0xe16cf04f
+0,       2376,       2376,      264,      528, 0xd66ff2d0
+0,       2640,       2640,      264,      528, 0xdce2f7f3
+0,       2904,       2904,      264,      528, 0x8349e547
+0,       3168,       3168,      264,      528, 0x5456ff8d
+0,       3432,       3432,      264,      528, 0xc9b2f5bd
+0,       3696,       3696,      264,      528, 0x0f68e129
+0,       3960,       3960,      264,      528, 0x5c210c1f
+0,       4224,       4224,      264,      528, 0x08e2fe3d
+0,       4488,       4488,      264,      528, 0x04a17e58