Commit a633e84947 for ffmpeg

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

    avformat/dss: give DSS files their exact duration

    The DSS demuxer set no duration, so lavf estimated one from a bit rate
    and the file size. SP gave a bit rate that assumes every block is full
    of 41-byte frames, and LP gave the bit rate of the last frame read.
    Neither holds for padding, pauses, the 9-byte frames of format 1 or a
    file cut short. The Olympus-encoded SP clip reported 10.071 s, the
    format 1 clip 9.775 s and the LP clip 10.121 s. They decode to 10.008,
    10.008 and 10.050 s.

    Count the frames by reading them as the demuxer returns them, and set
    the stream's duration and frame count from that. The block headers alone
    do not give the count. A file cut short can end inside a frame. A block
    that states more frames than it holds sends the reader on into the next
    blocks until it has read them or the file ends, and in LP a frame that
    does not parse ends the stream. This reads the whole file once at open.
    Drop the two bit rate estimates. lavf derives the bit rate from the file
    size and the duration.

    The tests cover 52 DSS and DS2 files: the Olympus-encoded clips in both
    containers, the FATE samples including sp-paused.dss and paused-qp.ds2,
    other Olympus-encoded clips and real recordings. On every file the
    duration now equals the decoded sample count over the sample rate, and
    for DSS nb_frames equals the frame count. Before, 32 were wrong: the 31
    files the DSS demuxer reads, and paused-qp.ds2. sp-paused.dss ends
    inside a frame. Its headers state 28 frames, and the new count gives 27,
    as many as decode. Copies of six files cut short at seven points each
    also match. So do 323 damaged copies of sp.dss, sp-paused.dss,
    olympus-format1.dss, lp.dss and the Olympus-encoded DS2 SP, format 1 and
    LP clips, with one block or all of them stating 0 or 255 frames, frame
    offsets out of range, random payload, or a cut at eight points. Summing
    the stated counts would give 113 of them a duration longer than the
    decode: sp.dss with block 20 of 27 stating 255 would come to 567 frames
    where 333 decode, and lp.dss with random payload to 584 frames where 7
    do. DS2 files built to test the count also match: QP7 blocks that state
    1 and 3 frames more than start in them, a QP7 copy of the re-sync block
    in paused-qp.ds2, and a QP frame that runs into a re-sync block. The
    stated counts gave the QP7 copy 409 frames where 402 decode. The 13 MB
    file now has a stream bit rate of 6656256000. Packets, timestamps and
    decoded samples do not change on any of the 52 files, and seeks still
    land as before. Read from a pipe, a DSS file still has no duration, and
    now has no bit rate either. The references of fate-dss-sp, fate-dss-lp,
    fate-ds2-qp and fate-ds2-qp-paused hold no duration and do not change.

    fate-dss-format1-duration probes the stream duration of
    olympus-format1.dss and fails without this commit. Without it the
    duration is 0.592455 s, and with it 0.960 s.

    [Michael Niedermayer: propagate errors other than EOF and invalid data
    from the frame count]

    [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 47a13d227f..a81d42b28a 100644
--- a/libavformat/dss.c
+++ b/libavformat/dss.c
@@ -126,12 +126,50 @@ static int dss_read_metadata_string(AVFormatContext *s, unsigned int offset,
     return av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
 }

+static int dss_read_packet(AVFormatContext *s, AVPacket *pkt);
+
+/* 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
+ * holds, and stops where the file ends or a frame does not parse. So the
+ * 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;
+    AVPacket *pkt;
+
+    if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
+        return 0;
+
+    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++;
+        av_packet_unref(pkt);
+    }
+    av_packet_free(&pkt);
+
+    if (ret != AVERROR_EOF && ret != AVERROR_INVALIDDATA)
+        return ret;
+    return frames;
+}
+
 static int dss_read_header(AVFormatContext *s)
 {
     DSSDemuxContext *ctx = s->priv_data;
     AVIOContext *pb = s->pb;
     AVStream *st;
-    int64_t ret64;
+    int64_t ret64, frames;
     int ret, version;

     st = avformat_new_stream(s, NULL);
@@ -169,8 +207,6 @@ static int dss_read_header(AVFormatContext *s)
          * decimates 11:12, so the output rate is 11000 Hz, not 11025. This
          * also sets the packet time base just below. */
         st->codecpar->sample_rate = 11000;
-        s->bit_rate = 8 * (DSS_FRAME_SIZE - 1) * st->codecpar->sample_rate
-                        * 512 / (506 * 264);
     } else if (ctx->audio_codec == DSS_ACODEC_G723_1) {
         st->codecpar->codec_id    = AV_CODEC_ID_G723_1;
         st->codecpar->sample_rate = 8000;
@@ -186,6 +222,14 @@ static int dss_read_header(AVFormatContext *s)
     avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
     st->start_time = 0;

+    frames = dss_count_frames(s);
+    if (frames < 0)
+        return (int)frames;
+    if (frames > 0) {
+        st->nb_frames = frames;
+        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)
@@ -193,6 +237,7 @@ static int dss_read_header(AVFormatContext *s)

     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;
@@ -412,7 +457,6 @@ static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
 static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     DSSDemuxContext *ctx = s->priv_data;
-    AVStream *st = s->streams[0];
     uint8_t byte;
     int size, ret;
     int64_t pos;
@@ -440,7 +484,6 @@ static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
     pkt->data[0]  = byte;
     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;

diff --git a/tests/fate/audio.mak b/tests/fate/audio.mak
index c725980f96..071fcd7557 100644
--- a/tests/fate/audio.mak
+++ b/tests/fate/audio.mak
@@ -34,6 +34,9 @@ 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_FFPROBE-$(CONFIG_DSS_DEMUXER) += fate-dss-format1-duration
+fate-dss-format1-duration: CMD = probe -show_entries stream=duration,nb_frames $(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

diff --git a/tests/ref/fate/dss-format1-duration b/tests/ref/fate/dss-format1-duration
new file mode 100644
index 0000000000..b659ab84ed
--- /dev/null
+++ b/tests/ref/fate/dss-format1-duration
@@ -0,0 +1,4 @@
+[STREAM]
+duration=0.960000
+nb_frames=40
+[/STREAM]