From: James Almer via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: James Almer <code@ffmpeg.org>
Subject: [FFmpeg-devel] [PATCH] avformat/demux: ensure avformat_find_stream_info updates internal stream contexts (PR #21224)
Date: Tue, 16 Dec 2025 22:23:28 -0000
Message-ID: <176592380880.60.11954002838101028383@2cb04c0e5124> (raw)
PR #21224 opened by James Almer (jamrial)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21224
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21224.patch
read_frame_internal() may result in a stream being modified without also returning a packet from it. Given said function only bothered to update the internal stream context for the returned packet, the result would be a desync between the stream's AVCodecParameters and the internal AVCodecContext.
This change makes sure all streams are updated within the avformat_find_stream_info() loop.
Fixes #YWH-PGM40646-20
This PR supersedes #21215.
>From 27fa209cf5f2b995828926e863ce11f1e531881f Mon Sep 17 00:00:00 2001
From: James Almer <jamrial@gmail.com>
Date: Tue, 16 Dec 2025 19:20:40 -0300
Subject: [PATCH] avformat/demux: ensure avformat_find_stream_info updates
internal stream contexts
read_frame_internal() may result in a stream being modified without also
returning a packet from it. Given said function only bothered to update the
internal stream context for the returned packet, the result would be a desync
between the stream's AVCodecParameters and the internal AVCodecContext.
This change makes sure all streams are updated within the
avformat_find_stream_info() loop.
Fixes #YWH-PGM40646-20
Signed-off-by: James Almer <jamrial@gmail.com>
---
libavformat/demux.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/libavformat/demux.c b/libavformat/demux.c
index 934eb80553..b1918d2110 100644
--- a/libavformat/demux.c
+++ b/libavformat/demux.c
@@ -186,6 +186,8 @@ static int init_input(AVFormatContext *s, const char *filename,
s, 0, s->format_probesize);
}
+static int codec_close(FFStream *sti);
+
static int update_stream_avctx(AVFormatContext *s)
{
int ret;
@@ -196,6 +198,14 @@ static int update_stream_avctx(AVFormatContext *s)
if (!sti->need_context_update)
continue;
+ if (avcodec_is_open(sti->avctx)) {
+ av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
+ ret = codec_close(sti);
+ sti->info->found_decoder = 0;
+ if (ret < 0)
+ return ret;
+ }
+
/* close parser, because it depends on the codec */
if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) {
av_parser_close(sti->parser);
@@ -2682,6 +2692,13 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
break;
}
+ /* read_frame_internal() in a previous iteration of this loop may
+ * have made changes to streams without returning a packet for them.
+ * Handle that here. */
+ ret = update_stream_avctx(ic);
+ if (ret < 0)
+ goto unref_then_goto_end;
+
/* check if one codec still needs to be handled */
for (i = 0; i < ic->nb_streams; i++) {
AVStream *const st = ic->streams[i];
--
2.49.1
_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org
reply other threads:[~2025-12-17 4:25 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=176592380880.60.11954002838101028383@2cb04c0e5124 \
--to=ffmpeg-devel@ffmpeg.org \
--cc=code@ffmpeg.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git