Commit b6ae964963 for ffmpeg

commit b6ae964963a9ad428e4e48fe2be85353dd300cab
Author: Guillain d'Erceville <guillain@poulpe.us>
Date:   Tue Nov 10 00:17:38 2026 +0100

    avformat/dss: seek along the frames of a read from the start

    A seek found the block that holds the target by adding up the frame
    counts that the block headers state. The reader does not follow those
    counts. A block that states more frames than it holds sends it on into
    the next blocks, and in LP a frame that does not parse ends the stream.
    The duration is counted by reading, so in a damaged file the seek and
    the duration did not agree. olympus-sp-byte2-ff.dss reads as 48 frames,
    1.152 s, while its headers state 291. A seek to 1.0 s went back to pts
    3168, and one to 6.9 s returned pts 73656 with data that a read from the
    start never gives.

    Seek by reading from the first frame up to the one that holds the
    target, and return that frame next. Its timestamp and the frames after
    it are then those of a read from the start. Past the end, the last frame
    is returned, then what ended the read: the end of the file, or in LP the
    frame that does not parse. A read error during the seek fails it. The
    seek now lands on the frame that holds the target, not on the first
    frame of its block. Like opening the file, it reads the input up to the
    target. On a 27 minute recording a seek takes 2.5 ms on average, and
    opening the file 4.4 ms.

    The tests seek to every frame and to the middle of every frame, before
    the start and past the end, with AVSEEK_FLAG_BACKWARD, no flag and
    AVSEEK_FLAG_ANY, in the 23 DSS samples of the FATE suite that the
    demuxer opens, 16 real recordings and 552 damaged copies of seven
    samples: a block stating 0, 19, 255 or a random number of frames, every
    block stating 0 or 255, a frame offset out of range, a flipped swap
    bit, a block of random payload, or a cut at eight points. Every seek
    lands on the frame that holds the target, and the next four packets
    match the read from the start in timestamp, position, size and data,
    and after the last frame the read ends. Before, in 127 of the damaged
    copies and in olympus-sp-byte2-ff.dss and
    olympus-format1-header-copy.dss, some seeks gave packets that a read
    from the start does not give, or gives later than the target. With a
    read error injected at each byte offset of six samples, every seek that
    meets the error fails. Without a seek, packets and
    durations do not change.

    The references of fate-seek-dss-sp change: each seek lands on the frame
    that holds the target, and past the end on the last frame, 7.776 s.

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

diff --git a/libavformat/dss.c b/libavformat/dss.c
index 73290ebf90..9e7488869c 100644
--- a/libavformat/dss.c
+++ b/libavformat/dss.c
@@ -57,6 +57,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;
+    AVPacket *seek_pkt;     /* frame a seek stopped at, returned next */
+    int seek_end;           /* then what ended the read past it, if anything */
     int64_t frames_read;    /* index of the next frame */

     int dss_header_size;
@@ -128,6 +130,24 @@ static int dss_read_metadata_string(AVFormatContext *s, unsigned int offset,

 static int dss_read_packet(AVFormatContext *s, AVPacket *pkt);

+/* Go back to the first audio block, where reading starts after the header. */
+static int dss_rewind(AVFormatContext *s)
+{
+    DSSDemuxContext *ctx = s->priv_data;
+    int64_t ret = avio_seek(s->pb, ctx->dss_header_size, SEEK_SET);
+
+    if (ret < 0)
+        return (int)ret;
+
+    ctx->swap        = 0;
+    ctx->frames_left = 0;
+    ctx->frames_read = 0;
+    ctx->dss_sp_swap_byte = -1;
+    /* dss_next_block steps forward first, so start one block short. */
+    ctx->block_pos   = ctx->dss_header_size - DSS_BLOCK_SIZE;
+    return 0;
+}
+
 /* Frames in the file, or 0 if the input cannot seek. Each block header states
  * how many frames start in the block, but the reader takes them from where the
  * first one starts, on into the next blocks when the block states more than it
@@ -135,7 +155,6 @@ static int dss_read_packet(AVFormatContext *s, AVPacket *pkt);
  * frames are counted by reading them all. */
 static int64_t dss_count_frames(AVFormatContext *s)
 {
-    DSSDemuxContext *ctx = s->priv_data;
     AVIOContext *pb = s->pb;
     int64_t frames = 0;
     int ret;
@@ -143,15 +162,13 @@ static int64_t dss_count_frames(AVFormatContext *s)

     if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
         return 0;
+    if ((ret = dss_rewind(s)) < 0)
+        return ret;

     pkt = av_packet_alloc();
     if (!pkt)
         return AVERROR(ENOMEM);

-    /* dss_next_block steps forward first, so start one block short. */
-    ctx->block_pos        = ctx->dss_header_size - DSS_BLOCK_SIZE;
-    ctx->frames_left      = 0;
-    ctx->dss_sp_swap_byte = -1;
     while ((ret = dss_read_packet(s, pkt)) >= 0 || ret == FFERROR_REDO) {
         if (ret >= 0)
             frames++;
@@ -171,7 +188,7 @@ static int dss_read_header(AVFormatContext *s)
     DSSDemuxContext *ctx = s->priv_data;
     AVIOContext *pb = s->pb;
     AVStream *st;
-    int64_t ret64, frames;
+    int64_t frames;
     int ret, version;

     st = avformat_new_stream(s, NULL);
@@ -231,19 +248,7 @@ static int dss_read_header(AVFormatContext *s)
         st->duration  = frames * (ctx->audio_codec == DSS_ACODEC_G723_1 ? 240 : 264);
     }

-    /* Jump over header */
-
-    if ((ret64 = avio_seek(pb, ctx->dss_header_size, SEEK_SET)) < 0)
-        return (int)ret64;
-
-    ctx->swap        = 0;
-    ctx->frames_left = 0;
-    ctx->frames_read = 0;
-    ctx->dss_sp_swap_byte = -1;
-    /* dss_next_block steps forward first, so start one block short. */
-    ctx->block_pos   = ctx->dss_header_size - DSS_BLOCK_SIZE;
-
-    return 0;
+    return dss_rewind(s);
 }

 static int dss_seek_block(AVFormatContext *s, int64_t pos)
@@ -519,47 +524,81 @@ static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
         return dss_723_1_read_packet(s, pkt);
 }

+/* Return the frame a seek stopped at, and if the read ended after it, what
+ * ended it; then go on reading. */
+static int dss_read_next_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    DSSDemuxContext *ctx = s->priv_data;
+
+    if (ctx->seek_pkt && ctx->seek_pkt->size) {
+        av_packet_move_ref(pkt, ctx->seek_pkt);
+        return 0;
+    }
+    if (ctx->seek_end) {
+        int ret = ctx->seek_end;
+
+        ctx->seek_end = 0;
+        return ret;
+    }
+    return dss_read_packet(s, pkt);
+}
+
+/* Read from the first frame up to the one that holds the timestamp, and keep
+ * that one to be returned next, or past the end the last one. Only reading
+ * tells where the frames are, as for the duration: a block that states more
+ * frames than it holds sends the reader on into the next blocks, and a frame
+ * that does not parse ends the stream. So the frame found, its timestamp and
+ * the frames after it are those of a read from the start. Like opening the
+ * file, this reads the input up to the target. */
 static int dss_read_seek(AVFormatContext *s, int stream_index,
                          int64_t timestamp, int flags)
 {
     DSSDemuxContext *ctx = s->priv_data;
-    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 count;
+    AVPacket *pkt;
+    int ret;

-    /* 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_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 (s->pb->error < 0 && s->pb->error != AVERROR_EOF)
-                return s->pb->error;
-            /* Past the end: scan again for the block with the last frame. */
-            if (!frames)
-                return ret;
-            target = frames - 1;
-            frames = 0;
-            seekto = ctx->dss_header_size - DSS_BLOCK_SIZE;
+    if (!ctx->seek_pkt && !(ctx->seek_pkt = av_packet_alloc()))
+        return AVERROR(ENOMEM);
+    av_packet_unref(ctx->seek_pkt);
+    ctx->seek_end = 0;
+    if ((ret = dss_rewind(s)) < 0)
+        return ret;
+
+    pkt = av_packet_alloc();
+    if (!pkt)
+        return AVERROR(ENOMEM);
+
+    while ((ret = dss_read_packet(s, pkt)) >= 0 || ret == FFERROR_REDO) {
+        if (ret < 0)
             continue;
-        }
-        count = dss_block_frames(header);
-        if (frames + count > target)
+        av_packet_unref(ctx->seek_pkt);
+        av_packet_move_ref(ctx->seek_pkt, pkt);
+        if (ctx->seek_pkt->pts + ctx->seek_pkt->duration > timestamp)
             break;
-        frames += count;
     }
+    av_packet_free(&pkt);

-    ctx->frames_read = frames;
-    ctx->swap        = !!(header[0] & 0x80);
-    ctx->frames_left = 0;
-    ctx->block_pos   = seekto - DSS_BLOCK_SIZE;
-    ctx->dss_sp_swap_byte = -1;
-    return 0;
+    if (ret < 0) {
+        if (s->pb->error < 0 && s->pb->error != AVERROR_EOF)
+            ret = s->pb->error;
+        else if ((ret == AVERROR_EOF || ret == AVERROR_INVALIDDATA) &&
+                 ctx->seek_pkt->size) {
+            ctx->seek_end = ret;
+            ret = 0;
+        }
+        if (ret < 0)
+            av_packet_unref(ctx->seek_pkt);
+    }
+    return ret;
 }

+static int dss_read_close(AVFormatContext *s)
+{
+    DSSDemuxContext *ctx = s->priv_data;
+
+    av_packet_free(&ctx->seek_pkt);
+    return 0;
+}

 const FFInputFormat ff_dss_demuxer = {
     .p.name         = "dss",
@@ -569,6 +608,7 @@ const FFInputFormat ff_dss_demuxer = {
     .priv_data_size = sizeof(DSSDemuxContext),
     .read_probe     = dss_probe,
     .read_header    = dss_read_header,
-    .read_packet    = dss_read_packet,
+    .read_packet    = dss_read_next_packet,
     .read_seek      = dss_read_seek,
+    .read_close     = dss_read_close,
 };
diff --git a/tests/ref/seek/dss-sp b/tests/ref/seek/dss-sp
index 4954475e6b..06ea7cfc96 100644
--- a/tests/ref/seek/dss-sp
+++ b/tests/ref/seek/dss-sp
@@ -2,52 +2,52 @@ 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:1 dts: 1.872000 pts: 1.872000 pos:   4264 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 dts: 4.776000 pts: 4.776000 pos:   9286 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: 0 flags:1 dts: 7.680000 pts: 7.680000 pos:  14300 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: 0 flags:1 dts: 0.576000 pts: 0.576000 pos:   2020 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:1 dts: 3.456000 pts: 3.456000 pos:   7000 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 dts: 6.360000 pts: 6.360000 pos:  12022 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: 0 flags:1 dts: 2.136000 pts: 2.136000 pos:   4722 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 dts: 7.776000 pts: 7.776000 pos:  14470 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: 0 flags:1 dts: 0.816000 pts: 0.816000 pos:   2436 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: 0 flags:1 dts: 3.720000 pts: 3.720000 pos:   7458 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:1 dts: 6.624000 pts: 6.624000 pos:  12478 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: 0 flags:1 dts: 2.400000 pts: 2.400000 pos:   5178 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: 0 flags:1 dts: 5.304000 pts: 5.304000 pos:  10194 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:1 dts: 7.776000 pts: 7.776000 pos:  14470 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 dts: 1.080000 pts: 1.080000 pos:   2894 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: 0 flags:1 dts: 3.984000 pts: 3.984000 pos:   7914 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: 0 flags:1 dts: 6.864000 pts: 6.864000 pos:  12894 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 dts: 2.664000 pts: 2.664000 pos:   5630 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: 0 flags:1 dts: 5.544000 pts: 5.544000 pos:  10610 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: 0 flags:1 dts: 7.776000 pts: 7.776000 pos:  14470 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
+ret: 0         st: 0 flags:1 dts: 1.344000 pts: 1.344000 pos:   3350 size:    42