Commit 471478e0d2 for ffmpeg

commit 471478e0d2bf873ebffa9e96d7fba54f8cf23563
Author: Kacper Michajłow <kasper93@gmail.com>
Date:   Thu Dec 3 23:47:57 2026 +0100

    avformat/dashdec: seek within media ranges

    Map positions in the nested demuxer to the buffered initialization
    section and the current media segment. For HTTP inputs opened at a
    nonzero offset, request the remaining range using absolute file offsets
    and keep the previous input if the request fails. A representation with
    a single fragment is now seekable with an initialization section or a
    media range as well.

    Signed-off-by: Kacper Michajłow <kasper93@gmail.com>

diff --git a/libavformat/dashdec.c b/libavformat/dashdec.c
index fce5e7aac9..747b6f1693 100644
--- a/libavformat/dashdec.c
+++ b/libavformat/dashdec.c
@@ -112,7 +112,7 @@ struct representation {

     int64_t cur_seq_no;
     int64_t cur_seg_offset;
-    int64_t cur_seg_size;
+    int input_is_http;
     struct fragment *cur_seg;
     int n_open_failures;            /* consecutive open_input failures since last good read */

@@ -1854,7 +1854,7 @@ static int read_from_url(struct representation *pls, struct fragment *seg,

     /* limit read if the fragment was only a part of a file */
     if (seg->size >= 0) {
-        buf_size = FFMIN(buf_size, pls->cur_seg_size - pls->cur_seg_offset);
+        buf_size = FFMIN(buf_size, seg->size - pls->cur_seg_offset);
         if (buf_size <= 0)
             return AVERROR_EOF;
     }
@@ -1866,11 +1866,12 @@ static int read_from_url(struct representation *pls, struct fragment *seg,
     return ret;
 }

-static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
+static int open_input(DASHContext *c, struct representation *pls, const struct fragment *seg,
+                      AVIOContext **input)
 {
     AVDictionary *opts = NULL;
     char *url = NULL;
-    int ret = 0, is_http = 0;
+    int ret = 0;

     url = av_mallocz(c->max_url_size);
     if (!url) {
@@ -1881,18 +1882,22 @@ static int open_input(DASHContext *c, struct representation *pls, struct fragmen
     if (seg->size >= 0) {
         /* try to restrict the HTTP request to the part we want
          * (if this is in fact a HTTP request) */
-        av_dict_set_int(&opts, "offset", seg->url_offset, 0);
-        av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0);
+        ret = av_dict_set_int(&opts, "offset", seg->url_offset, 0);
+        if (ret < 0)
+            goto cleanup;
+        ret = av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0);
+        if (ret < 0)
+            goto cleanup;
     }

     ff_make_absolute_url(url, c->max_url_size, c->base_url, seg->url);
     av_log(pls->parent, AV_LOG_VERBOSE, "DASH request for url '%s', offset %"PRId64"\n",
            url, seg->url_offset);
-    ret = open_url(pls->parent, &pls->input, url, &c->avio_opts, opts, &is_http);
-    if (ret >= 0 && !is_http && seg->url_offset > 0) {
-        int64_t pos = avio_seek(pls->input, seg->url_offset, SEEK_SET);
+    ret = open_url(pls->parent, input, url, &c->avio_opts, opts, &pls->input_is_http);
+    if (ret >= 0 && !pls->input_is_http && seg->url_offset > 0) {
+        int64_t pos = avio_seek(*input, seg->url_offset, SEEK_SET);
         if (pos < 0) {
-            ff_format_io_close(pls->parent, &pls->input);
+            ff_format_io_close(pls->parent, input);
             ret = pos;
         }
     }
@@ -1900,8 +1905,6 @@ static int open_input(DASHContext *c, struct representation *pls, struct fragmen
 cleanup:
     av_free(url);
     av_dict_free(&opts);
-    pls->cur_seg_offset = 0;
-    pls->cur_seg_size = seg->size;
     return ret;
 }

@@ -1916,7 +1919,7 @@ static int update_init_section(struct representation *pls)
     if (!pls->init_section || pls->init_sec_buf)
         return 0;

-    ret = open_input(c, pls, pls->init_section);
+    ret = open_input(c, pls, pls->init_section, &pls->input);
     if (ret < 0) {
         av_log(pls->parent, AV_LOG_WARNING,
                "Failed to open an initialization section\n");
@@ -1938,6 +1941,7 @@ static int update_init_section(struct representation *pls)

     av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);

+    pls->cur_seg_offset = 0;
     ret = read_from_url(pls, pls->init_section, pls->init_sec_buf,
                         pls->init_sec_buf_size);
     ff_format_io_close(pls->parent, &pls->input);
@@ -1951,22 +1955,49 @@ static int update_init_section(struct representation *pls)
     return 0;
 }

-static int is_whole_file(const struct representation *pls)
-{
-    return pls->cur_seg && !pls->init_sec_data_len &&
-           !pls->cur_seg->url_offset && pls->cur_seg->size < 0;
-}
-
 static int64_t seek_data(void *opaque, int64_t offset, int whence)
 {
     struct representation *v = opaque;
-    /* Avoid seeking for non-whole files, where an "offset" does not correspond
-     * to a position in the input. AVIO sets 0 position at "offset" of the input. */
-    if (is_whole_file(v)) {
+    const struct fragment *seg = v->cur_seg;
+    if (!seg || !v->input)
+        return AVERROR(ENOSYS);
+    if (!v->init_sec_data_len && !seg->url_offset && seg->size < 0)
         return avio_seek(v->input, offset, whence);
+
+    if (whence == AVSEEK_SIZE) {
+        int64_t size = seg->size >= 0 ? seg->size : avio_size(v->input);
+        if (size > INT64_MAX - v->init_sec_data_len)
+            return AVERROR(EOVERFLOW);
+        return size < 0 ? size : size + v->init_sec_data_len;
     }
+    if (whence != SEEK_SET || offset < 0)
+        return AVERROR(EINVAL);

-    return AVERROR(ENOSYS);
+    int64_t media_offset = FFMAX(offset - v->init_sec_data_len, 0);
+    if (seg->size >= 0 && media_offset > seg->size)
+        return AVERROR(EINVAL);
+    if (media_offset != v->cur_seg_offset && media_offset != seg->size) {
+        if (v->input_is_http && seg->url_offset) {
+            /* HTTP's offset option starts AVIO at zero. Request the remaining
+             * range in file coordinates and keep the old input on failure. */
+            struct fragment range = *seg;
+            AVIOContext *input = NULL;
+            range.url_offset += media_offset;
+            range.size -= media_offset;
+            int ret = open_input(v->parent->priv_data, v, &range, &input);
+            if (ret < 0)
+                return ret;
+            ff_format_io_close(v->parent, &v->input);
+            v->input = input;
+        } else {
+            int64_t pos = avio_seek(v->input, seg->url_offset + media_offset, SEEK_SET);
+            if (pos < 0)
+                return pos;
+        }
+    }
+    v->init_sec_buf_read_offset = FFMIN(offset, v->init_sec_data_len);
+    v->cur_seg_offset = media_offset;
+    return offset;
 }

 static int read_data(void *opaque, uint8_t *buf, int buf_size)
@@ -1989,7 +2020,8 @@ restart:
         if (ret)
             goto end;

-        ret = open_input(c, v, v->cur_seg);
+        v->cur_seg_offset = 0;
+        ret = open_input(c, v, v->cur_seg, &v->input);
         if (ret < 0) {
             if (ff_check_interrupt(c->interrupt_callback)) {
                 ret = AVERROR_EXIT;
@@ -2097,8 +2129,8 @@ static int reopen_demux_for_component(AVFormatContext *s, struct representation
         goto fail;
     }

-    if (!c->is_live && pls->n_fragments == 1 && is_whole_file(pls))
-        pls->pb.pub.seekable = pls->input->seekable;
+    if (!c->is_live && pls->n_fragments == 1)
+        pls->pb.pub.seekable = pls->input->seekable & AVIO_SEEKABLE_NORMAL;
     pls->ctx->pb = &pls->pb.pub;
     pls->ctx->io_open  = nested_io_open;

@@ -2586,7 +2618,6 @@ static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t see
     // single fragment mode
     if (pls->n_fragments == 1) {
         pls->cur_timestamp = 0;
-        pls->cur_seg_offset = 0;
         pls->eof = 0;
         if (dry_run)
             return 0;