Commit 8839da28d2 for ffmpeg

commit 8839da28d2e53f0b286868e15b115d29ca77cb81
Author: Patrick Domack <patrickdk@patrickdk.com>
Date:   Sun Nov 1 04:56:49 2026 -0500

    avformat/dss: give packets after a seek the right timestamps

    The demuxer set no timestamps, so after a seek the packets restarted at
    pts 0. Only variable rate SP found the landing block from the block
    headers. Fixed rate SP assumed every block is full of 41-byte frames. A
    block that holds fewer frames breaks that, and in a long recording the
    seek landed up to two blocks before the one holding the target. LP used
    the size of the last frame read, which is 0 before the first read, so a
    seek made right after opening the file went back to the start. A seek
    past the end failed, after which lavf read through the whole file and
    still could not seek.

    Find the block from the per-block frame counts for every format, keep
    the index of its first frame, and stamp every packet with its frame
    index. Past the end, land on the block that holds the last frame, also
    where the last block is cut short and seeking to the next one fails. The
    old LP estimate was the only user of packet_size, so remove it.

    A byte seek moves the input under the block walk.
    In SP the walk then went on from where it had stopped, and in LP the
    first packet after the seek was not a packet of the file. Set
    AVFMT_NO_BYTE_SEEK, so that a byte seek fails.

    The tests seek to the first frame of every block, and into the middle of
    each, in sp.dss, lp.dss, DS230002.DSS, the Olympus-encoded SP, SP_VR and
    LP clips in DSS and DS2 form, the paused SP vector and two long
    recordings. Every seek lands on the block holding the target, the first
    packet carries the timestamp of that block's first frame, and the
    packets match an unseeked read from that frame. Before this change those
    packets started at pts 0. An LP seek made before any read now lands on
    the right block. A byte seek into sp.dss, lp.dss or olympus-sp.ds2 now
    fails. Before, the packets after one into sp.dss went on from packet 51
    for targets 1556 and 3000 alike. A seek past the end now gives the last
    frames, and ffmpeg -ss past the end finishes without "could not seek",
    also for files cut inside a block. Without a seek, packets, timestamps
    and decoded frames do not change on eleven DSS and DS2 SP and LP files.

    fate-seek-dss-sp seeks in the existing dss/sp.dss through
    libavformat/tests/seek, with a seek range that runs past the end of the
    file, and fails without this commit.

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

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

diff --git a/libavformat/dss.c b/libavformat/dss.c
index 1ecf3cd270..76935a4e3d 100644
--- a/libavformat/dss.c
+++ b/libavformat/dss.c
@@ -58,8 +58,8 @@ typedef struct DSSDemuxContext {
     int64_t block_pos;      /* offset of the current audio block header */
     int frames_left;        /* frames still to come from that block */
     int dss_sp_swap_byte;
+    int64_t frames_read;    /* index of the next frame */

-    int packet_size;
     int dss_header_size;
 } DSSDemuxContext;

@@ -379,6 +379,7 @@ static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
         return ret;

     pkt->duration     = 264;
+    pkt->pts          = ctx->frames_read++ * 264;
     pkt->pos = pos;
     pkt->stream_index = 0;

@@ -438,7 +439,6 @@ static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)

     size = frame_size[byte & 3];

-    ctx->packet_size = size;
     ctx->counter--;

     ret = av_new_packet(pkt, size);
@@ -449,6 +449,7 @@ static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
     pkt->data[0]  = byte;
     offset        = 1;
     pkt->duration = 240;
+    pkt->pts      = ctx->frames_read++ * 240;
     s->bit_rate = 8LL * size-- * st->codecpar->sample_rate * 512 / (506 * pkt->duration);

     pkt->stream_index = 0;
@@ -487,45 +488,34 @@ static int dss_read_seek(AVFormatContext *s, int stream_index,
                          int64_t timestamp, int flags)
 {
     DSSDemuxContext *ctx = s->priv_data;
-    int64_t ret, seekto;
+    int samples = ctx->audio_codec == DSS_ACODEC_G723_1 ? 240 : 264;
+    int64_t ret, seekto, target = timestamp / samples, frames = 0;
     uint8_t header[DSS_AUDIO_BLOCK_HEADER_SIZE];
-    int offset;
-
-    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;
+    int count, offset;
+
+    /* Frame sizes vary, and a block can hold fewer frames than would fit, so
+     * find the block from the per-block frame counts, taken as in
+     * dss_sp_next_block(). */
+    for (seekto = ctx->dss_header_size; ; seekto += DSS_BLOCK_SIZE) {
+        ret = avio_seek(s->pb, seekto, SEEK_SET);
+        if (ret >= 0)
             ret = ffio_read_size(s->pb, header, DSS_AUDIO_BLOCK_HEADER_SIZE);
-            if (ret < 0)
+        if (ret < 0) {
+            /* Past the end: scan again for the block with the last frame. */
+            if (!frames)
                 return ret;
-            count = header[2] || header[5] == 0xff ? header[2] : header[5];
-            if (frames + count > target)
-                break;
-            frames += count;
+            target = frames - 1;
+            frames = 0;
+            seekto = ctx->dss_header_size - DSS_BLOCK_SIZE;
+            continue;
         }
-    } else if (ctx->audio_codec != DSS_ACODEC_G723_1)
-        seekto = timestamp / 264 * 41 / 506 * 512;
-    else
-        seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
-
-    if (seekto < 0)
-        seekto = 0;
-
-    seekto += ctx->dss_header_size;
-
-    ret = avio_seek(s->pb, seekto, SEEK_SET);
-    if (ret < 0)
-        return ret;
+        count = dss_block_frames(header);
+        if (frames + count > target)
+            break;
+        frames += count;
+    }

-    ret = ffio_read_size(s->pb, header, DSS_AUDIO_BLOCK_HEADER_SIZE);
-    if (ret < 0)
-        return ret;
+    ctx->frames_read = frames;
     ctx->swap        = !!(header[0] & 0x80);
     ctx->frames_left = 0;
     ctx->block_pos   = seekto - DSS_BLOCK_SIZE;
@@ -553,6 +543,7 @@ const FFInputFormat ff_dss_demuxer = {
     .p.name         = "dss",
     .p.long_name    = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
     .p.extensions   = "dss",
+    .p.flags        = AVFMT_NO_BYTE_SEEK,
     .priv_data_size = sizeof(DSSDemuxContext),
     .read_probe     = dss_probe,
     .read_header    = dss_read_header,
diff --git a/tests/fate/seek.mak b/tests/fate/seek.mak
index 0181dd2e5f..d27001c826 100644
--- a/tests/fate/seek.mak
+++ b/tests/fate/seek.mak
@@ -191,6 +191,7 @@ FATE_SEEK_EXTRA-$(call ALLYES, MOV_DEMUXER) += fate-seek-test-iibbibb-mp4
 FATE_SEEK_EXTRA-$(call ALLYES, MOV_DEMUXER) += fate-seek-test-iibbibb-neg-ctts-mp4
 FATE_SEEK_EXTRA-$(CONFIG_WAV_DEMUXER)       += fate-seek-bad-avg-byterate
 FATE_SEEK_EXTRA-$(CONFIG_SACD_DEMUXER)      += fate-seek-sacd
+FATE_SEEK_EXTRA-$(CONFIG_DSS_DEMUXER)       += fate-seek-dss-sp

 fate-seek-extra-mp3:  CMD = run libavformat/tests/seek$(EXESUF) $(TARGET_SAMPLES)/gapless/gapless.mp3 -fastseek 1
 fate-seek-bad-avg-byterate: CMD = run libavformat/tests/seek$(EXESUF) $(TARGET_SAMPLES)/wav/wrong-avg-byterate.wav -seekback 500 -stream_id 0
@@ -202,6 +203,7 @@ fate-seek-cache-pipe: CMD = cat $(SAMPLES)/gapless/gapless.mp3 | run libavformat
 fate-seek-mkv-codec-delay:   CMD = run libavformat/tests/seek$(EXESUF) $(TARGET_SAMPLES)/mkv/codec_delay_opus.mkv
 fate-seek-mkv-best-fit-ts:   CMD = run libavformat/tests/seek$(EXESUF) $(TARGET_SAMPLES)/mkv/test7_cut.mkv -duration 4 -frames 3 -best_fit_ts 1
 fate-seek-sacd: CMD = run libavformat/tests/seek$(EXESUF) $(TARGET_SAMPLES)/sacd/2ch-dsd_5ch-dst.iso -duration 2
+fate-seek-dss-sp:     CMD = run libavformat/tests/seek$(EXESUF) $(TARGET_SAMPLES)/dss/sp.dss -duration 10

 # A master playlist with separate audio and video media playlists with different DTS.
 tests/data/hls-seek.m3u8: ffmpeg$(PROGSSUF)$(EXESUF) | tests/data
diff --git a/tests/ref/seek/dss-sp b/tests/ref/seek/dss-sp
new file mode 100644
index 0000000000..4954475e6b
--- /dev/null
+++ b/tests/ref/seek/dss-sp
@@ -0,0 +1,53 @@
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:   1030 size:    42
+ret: 0         st:-1 flags:0  ts:-1.000000
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:   1030 size:    42
+ret: 0         st:-1 flags:1  ts: 1.894167
+ret: 0         st: 0 flags:1 dts: 1.800000 pts: 1.800000 pos:   4142 size:    42
+ret: 0         st: 0 flags:0  ts: 4.788364
+ret: 0         st: 0 flags:1 dts: 4.752000 pts: 4.752000 pos:   9244 size:    42
+ret: 0         st: 0 flags:1  ts: 7.682545
+ret: 0         st: 0 flags:1 dts: 7.416000 pts: 7.416000 pos:  13850 size:    42
+ret: 0         st:-1 flags:0  ts: 0.576668
+ret: 0         st: 0 flags:1 dts: 0.312000 pts: 0.312000 pos:   1570 size:    42
+ret: 0         st:-1 flags:1  ts: 3.470835
+ret: 0         st: 0 flags:1 dts: 3.264000 pts: 3.264000 pos:   6672 size:    42
+ret: 0         st: 0 flags:0  ts: 6.365000
+ret: 0         st: 0 flags:1 dts: 6.240000 pts: 6.240000 pos:  11816 size:    42
+ret: 0         st: 0 flags:1  ts:-0.740818
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:   1030 size:    42
+ret: 0         st:-1 flags:0  ts: 2.153336
+ret: 0         st: 0 flags:1 dts: 2.088000 pts: 2.088000 pos:   4640 size:    42
+ret: 0         st:-1 flags:1  ts: 5.047503
+ret: 0         st: 0 flags:1 dts: 5.040000 pts: 5.040000 pos:   9742 size:    42
+ret: 0         st: 0 flags:0  ts: 7.941636
+ret: 0         st: 0 flags:1 dts: 7.704000 pts: 7.704000 pos:  14348 size:    42
+ret: 0         st: 0 flags:1  ts: 0.835818
+ret: 0         st: 0 flags:1 dts: 0.600000 pts: 0.600000 pos:   2068 size:    42
+ret: 0         st:-1 flags:0  ts: 3.730004
+ret: 0         st: 0 flags:1 dts: 3.576000 pts: 3.576000 pos:   7212 size:    42
+ret: 0         st:-1 flags:1  ts: 6.624171
+ret: 0         st: 0 flags:1 dts: 6.528000 pts: 6.528000 pos:  12314 size:    42
+ret: 0         st: 0 flags:0  ts:-0.481636
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:   1030 size:    42
+ret: 0         st: 0 flags:1  ts: 2.412545
+ret: 0         st: 0 flags:1 dts: 2.376000 pts: 2.376000 pos:   5138 size:    42
+ret: 0         st:-1 flags:0  ts: 5.306672
+ret: 0         st: 0 flags:1 dts: 5.040000 pts: 5.040000 pos:   9742 size:    42
+ret: 0         st:-1 flags:1  ts: 8.200839
+ret: 0         st: 0 flags:1 dts: 7.704000 pts: 7.704000 pos:  14348 size:    42
+ret: 0         st: 0 flags:0  ts: 1.095000
+ret: 0         st: 0 flags:1 dts: 0.912000 pts: 0.912000 pos:   2606 size:    42
+ret: 0         st: 0 flags:1  ts: 3.989182
+ret: 0         st: 0 flags:1 dts: 3.864000 pts: 3.864000 pos:   7710 size:    42
+ret: 0         st:-1 flags:0  ts: 6.883340
+ret: 0         st: 0 flags:1 dts: 6.816000 pts: 6.816000 pos:  12812 size:    42
+ret: 0         st:-1 flags:1  ts:-0.222493
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:   1030 size:    42
+ret: 0         st: 0 flags:0  ts: 2.671636
+ret: 0         st: 0 flags:1 dts: 2.376000 pts: 2.376000 pos:   5138 size:    42
+ret: 0         st: 0 flags:1  ts: 5.565818
+ret: 0         st: 0 flags:1 dts: 5.352000 pts: 5.352000 pos:  10282 size:    42
+ret: 0         st:-1 flags:0  ts: 8.460008
+ret: 0         st: 0 flags:1 dts: 7.704000 pts: 7.704000 pos:  14348 size:    42
+ret: 0         st:-1 flags:1  ts: 1.354175
+ret: 0         st: 0 flags:1 dts: 1.200000 pts: 1.200000 pos:   3104 size:    42