[FFmpeg-devel] [PATCH v2] avformat/mov: fix buffering issue for special HTTP(s) mp4.

Chen, Jinkai chenjinkai at agora.io
Tue Dec 20 11:20:58 EET 2022


Problem:
Using ffplay play on these sources:
https://ali-sprite-video.yyouwang.com/video/works/202211/1667997073624_73.mp4
https://images.voghion.com/productImages/04_01_C_30011_2020220106GiuseppeFanara0012.mp4

Solution:
Add a private option, it will use separated IO context(HTTP connection)
for each AVStream. Preventing from reading audio and video in
long distance(offset), which cause seeking(HTTP request) frequently.

Storing the user options when open input,
and make sure that can be passed to demuxer context.

Signed-off-by: Gamhoi Chan <chenjinkai at agora.io<mailto:chenjinkai at agora.io>>
---
libavformat/avformat.c |  1 +
libavformat/demux.c    |  5 ++++-
libavformat/internal.h |  5 +++++
libavformat/isom.h     |  1 +
libavformat/mov.c      | 14 +++++++++++++-
5 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/libavformat/avformat.c b/libavformat/avformat.c
index 19c7219471..4453727f34 100644
--- a/libavformat/avformat.c
+++ b/libavformat/avformat.c
@@ -129,6 +129,7 @@ void avformat_free_context(AVFormatContext *s)
    av_freep(&s->chapters);
    av_dict_free(&s->metadata);
    av_dict_free(&si->id3v2_meta);
+    av_dict_free(&si->options);
    av_packet_free(&si->pkt);
    av_packet_free(&si->parse_pkt);
    av_freep(&s->streams);
diff --git a/libavformat/demux.c b/libavformat/demux.c
index 2dfd82a63c..2377bfdab0 100644
--- a/libavformat/demux.c
+++ b/libavformat/demux.c
@@ -237,8 +237,11 @@ int avformat_open_input(AVFormatContext **ps, const char *filename,
    if (fmt)
        s->iformat = fmt;

-    if (options)
+    if (options) {
        av_dict_copy(&tmp, *options, 0);
+        si->options = NULL;
+        av_dict_copy(&si->options, *options, 0);
+    }

    if (s->pb) // must be before any goto fail
        s->flags |= AVFMT_FLAG_CUSTOM_IO;
diff --git a/libavformat/internal.h b/libavformat/internal.h
index ce837fefc7..7caae8b93e 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -186,6 +186,11 @@ typedef struct FFFormatContext {
     * Contexts and child contexts do not contain a metadata option
     */
    int metafree;
+
+    /**
+     * options from avformat_open_input
+     */
+    AVDictionary *options;
} FFFormatContext;

static av_always_inline FFFormatContext *ffformatcontext(AVFormatContext *s)
diff --git a/libavformat/isom.h b/libavformat/isom.h
index 64fb7065d5..dad049a2df 100644
--- a/libavformat/isom.h
+++ b/libavformat/isom.h
@@ -326,6 +326,7 @@ typedef struct MOVContext {
        int64_t extent_offset;
    } *avif_info;
    int avif_info_size;
+    int use_stream_pb;
} MOVContext;

int ff_mp4_read_descr_len(AVIOContext *pb);
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 29bd3103e3..0d5818b327 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -4519,6 +4519,18 @@ static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
                   st->index, dref->path, dref->dir, dref->filename,
                   dref->volume, dref->nlvl_from, dref->nlvl_to);
        }
+    } else if (c->use_stream_pb) {
+        FFFormatContext *const si = ffformatcontext(c->fc);
+        AVDictionary *opts = NULL;
+        av_dict_copy(&opts, si->options, 0);
+        ret = c->fc->io_open(c->fc, &sc->pb, c->fc->url, AVIO_FLAG_READ, &opts);
+        av_dict_free(&opts);
+        if (ret < 0) {
+            av_log(c->fc, AV_LOG_ERROR,
+                "use_stream_pb, stream %d, error opening url %s.\n",
+                st->index, c->fc->url);
+            return ret;
+        }
    } else {
        sc->pb = c->fc->pb;
        sc->pb_is_copied = 1;
@@ -9119,7 +9131,7 @@ static const AVOption mov_options[] = {
    { "enable_drefs", "Enable external track support.", OFFSET(enable_drefs), AV_OPT_TYPE_BOOL,
        {.i64 = 0}, 0, 1, FLAGS },
    { "max_stts_delta", "treat offsets above this value as invalid", OFFSET(max_stts_delta), AV_OPT_TYPE_INT, {.i64 = UINT_MAX-48000*10 }, 0, UINT_MAX, .flags = AV_OPT_FLAG_DECODING_PARAM },
-
+    { "use_stream_pb", "Each steam has its own AVIOContext.", OFFSET(use_stream_pb), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
    { NULL },
};

--


More information about the ffmpeg-devel mailing list