* [FFmpeg-devel] [PATCH] avformat/demux: Add allow_codec_changes option to AVFormatContext (v2)
@ 2025-06-17 14:36 Pavel Koshevoy
2025-06-17 18:39 ` Marton Balint
0 siblings, 1 reply; 4+ messages in thread
From: Pavel Koshevoy @ 2025-06-17 14:36 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Pavel Koshevoy
Make runtime AVStream.codecpar codec_id updates optional and disabled
by default, so that avformat API clients can enable this feature explicitly
when they add support for runtime codec changes.
Accordingly, codec_close should not assume that the codec_id cannot change.
This fixes 'ffprobe 1_poc.mp4' segfault introduced with
commit 0021484d05f9b0f032fa319399de6e24eea0c04f.
---
doc/APIchanges | 3 +++
doc/formats.texi | 6 ++++++
libavformat/avformat.h | 10 ++++++++++
libavformat/demux.c | 8 +++++++-
libavformat/mpegts.c | 4 +++-
libavformat/options_table.h | 1 +
libavformat/version.h | 2 +-
tests/fate/demux.mak | 2 +-
8 files changed, 32 insertions(+), 4 deletions(-)
diff --git a/doc/APIchanges b/doc/APIchanges
index 91710bb27d..29b3389094 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -2,6 +2,9 @@ The last version increases of all libraries were on 2025-03-28
API changes, most recent first:
+2025-06-17 - xxxxxxxxxx - lavf 62.2.100 - avformat.h
+ Add AVFormatContext.allow_codec_changes AVOption.
+
2025-05-21 - xxxxxxxxxx - lavu 60.3.100 - avassert.h
Add av_unreachable() and av_assume() macros.
diff --git a/doc/formats.texi b/doc/formats.texi
index 876a9e92b3..d18613f8bd 100644
--- a/doc/formats.texi
+++ b/doc/formats.texi
@@ -228,6 +228,12 @@ would require too many resources due to a large number of streams.
Skip estimation of input duration if it requires an additional probing for PTS at end of file.
At present, applicable for MPEG-PS and MPEG-TS.
+@item allow_codec_changes @var{bool} (@emph{input})
+Allow AVStream.codecpar codec_type and codec_id to change at runtime
+(for example when MPEG-TS PMT ES stream_type changes at runtime).
+This is disabled by default, because most clients of avformat API
+do not support random mid-stream codec changes.
+
@item duration_probesize @var{integer} (@emph{input})
Set probing size, in bytes, for input duration estimation when it actually requires
an additional probing for PTS at end of file (at present: MPEG-PS and MPEG-TS).
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index b6c63e2237..c6956626b5 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -1884,6 +1884,16 @@ typedef struct AVFormatContext {
* @see skip_estimate_duration_from_pts
*/
int64_t duration_probesize;
+
+ /**
+ * Allow AVStream.codecpar codec_type and codec_id to change at runtime
+ * (for example when MPEG-TS PMT ES stream_type changes at runtime).
+ * This is disabled by default, because most clients of avformat API
+ * do not support random mid-stream codec changes.
+ * - muxing: unused
+ * - demuxing: set by user
+ */
+ int allow_codec_changes;
} AVFormatContext;
/**
diff --git a/libavformat/demux.c b/libavformat/demux.c
index ecd4f40da9..3749ab67a3 100644
--- a/libavformat/demux.c
+++ b/libavformat/demux.c
@@ -1292,9 +1292,15 @@ static int codec_close(FFStream *sti)
{
AVCodecContext *avctx_new = NULL;
AVCodecParameters *par_tmp = NULL;
+ const AVCodec *new_codec = NULL;
int ret;
- avctx_new = avcodec_alloc_context3(sti->avctx->codec);
+ new_codec =
+ (sti->avctx->codec_id != sti->pub.codecpar->codec_id) ?
+ avcodec_find_decoder(sti->pub.codecpar->codec_id) :
+ sti->avctx->codec;
+
+ avctx_new = avcodec_alloc_context3(new_codec);
if (!avctx_new) {
ret = AVERROR(ENOMEM);
goto fail;
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index deb69a0548..0c78e870bb 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -2510,7 +2510,9 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
if (!st)
goto out;
- if (pes && pes->stream_type != stream_type)
+ if (pes && (!pes->stream_type ||
+ (pes->stream_type != stream_type &&
+ ts->stream->allow_codec_changes)))
mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
add_pid_to_program(prg, pid);
diff --git a/libavformat/options_table.h b/libavformat/options_table.h
index e2e690fd2a..e949d60c3e 100644
--- a/libavformat/options_table.h
+++ b/libavformat/options_table.h
@@ -106,6 +106,7 @@ static const AVOption avformat_options[] = {
{"skip_estimate_duration_from_pts", "skip duration calculation in estimate_timings_from_pts", OFFSET(skip_estimate_duration_from_pts), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, D},
{"max_probe_packets", "Maximum number of packets to probe a codec", OFFSET(max_probe_packets), AV_OPT_TYPE_INT, { .i64 = 2500 }, 0, INT_MAX, D },
{"duration_probesize", "Maximum number of bytes to probe the durations of the streams in estimate_timings_from_pts", OFFSET(duration_probesize), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, D},
+{"allow_codec_changes", "Allow AVStream.codecpar codec_type and codec_id to change at runtime", OFFSET(allow_codec_changes), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, D},
{NULL},
};
diff --git a/libavformat/version.h b/libavformat/version.h
index a7c80dc564..904e7f06aa 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -31,7 +31,7 @@
#include "version_major.h"
-#define LIBAVFORMAT_VERSION_MINOR 1
+#define LIBAVFORMAT_VERSION_MINOR 2
#define LIBAVFORMAT_VERSION_MICRO 100
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
diff --git a/tests/fate/demux.mak b/tests/fate/demux.mak
index ead5ad4b10..4a39e950b9 100644
--- a/tests/fate/demux.mak
+++ b/tests/fate/demux.mak
@@ -158,7 +158,7 @@ FATE_SAMPLES_DEMUX-$(CONFIG_XWMA_DEMUXER) += fate-xwma-demux
fate-xwma-demux: CMD = crc -i $(TARGET_SAMPLES)/xwma/ergon.xwma -c:a copy
FATE_FFPROBE_DEMUX-$(CONFIG_MPEGTS_DEMUXER) += fate-ts-demux
-fate-ts-demux: CMD = ffprobe_demux $(TARGET_SAMPLES)/ac3/mp3ac325-4864-small.ts
+fate-ts-demux: CMD = ffprobe_demux $(TARGET_SAMPLES)/ac3/mp3ac325-4864-small.ts -allow_codec_changes 1
FATE_FFPROBE_DEMUX-$(CONFIG_MPEGTS_DEMUXER) += fate-ts-timed-id3-demux
fate-ts-timed-id3-demux: CMD = ffprobe_demux $(TARGET_SAMPLES)/mpegts/id3.ts
--
2.43.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [FFmpeg-devel] [PATCH] avformat/demux: Add allow_codec_changes option to AVFormatContext (v2)
2025-06-17 14:36 [FFmpeg-devel] [PATCH] avformat/demux: Add allow_codec_changes option to AVFormatContext (v2) Pavel Koshevoy
@ 2025-06-17 18:39 ` Marton Balint
2025-06-17 19:38 ` Pavel Koshevoy
2025-06-17 20:46 ` Michael Niedermayer
0 siblings, 2 replies; 4+ messages in thread
From: Marton Balint @ 2025-06-17 18:39 UTC (permalink / raw)
To: FFmpeg development discussions and patches
On Tue, 17 Jun 2025, Pavel Koshevoy wrote:
> Make runtime AVStream.codecpar codec_id updates optional and disabled
> by default, so that avformat API clients can enable this feature explicitly
> when they add support for runtime codec changes.
>
> Accordingly, codec_close should not assume that the codec_id cannot change.
> This fixes 'ffprobe 1_poc.mp4' segfault introduced with
> commit 0021484d05f9b0f032fa319399de6e24eea0c04f.
> ---
> doc/APIchanges | 3 +++
> doc/formats.texi | 6 ++++++
> libavformat/avformat.h | 10 ++++++++++
> libavformat/demux.c | 8 +++++++-
> libavformat/mpegts.c | 4 +++-
> libavformat/options_table.h | 1 +
> libavformat/version.h | 2 +-
> tests/fate/demux.mak | 2 +-
> 8 files changed, 32 insertions(+), 4 deletions(-)
>
> diff --git a/doc/APIchanges b/doc/APIchanges
> index 91710bb27d..29b3389094 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -2,6 +2,9 @@ The last version increases of all libraries were on 2025-03-28
>
> API changes, most recent first:
>
> +2025-06-17 - xxxxxxxxxx - lavf 62.2.100 - avformat.h
> + Add AVFormatContext.allow_codec_changes AVOption.
Considering that this is a simple boolean option, maybe you should use
AVFormatContext->flags instead, so no new field need to be added, only a
new flag.
Thanks,
Marton
> +
> 2025-05-21 - xxxxxxxxxx - lavu 60.3.100 - avassert.h
> Add av_unreachable() and av_assume() macros.
>
> diff --git a/doc/formats.texi b/doc/formats.texi
> index 876a9e92b3..d18613f8bd 100644
> --- a/doc/formats.texi
> +++ b/doc/formats.texi
> @@ -228,6 +228,12 @@ would require too many resources due to a large number of streams.
> Skip estimation of input duration if it requires an additional probing for PTS at end of file.
> At present, applicable for MPEG-PS and MPEG-TS.
>
> +@item allow_codec_changes @var{bool} (@emph{input})
> +Allow AVStream.codecpar codec_type and codec_id to change at runtime
> +(for example when MPEG-TS PMT ES stream_type changes at runtime).
> +This is disabled by default, because most clients of avformat API
> +do not support random mid-stream codec changes.
> +
> @item duration_probesize @var{integer} (@emph{input})
> Set probing size, in bytes, for input duration estimation when it actually requires
> an additional probing for PTS at end of file (at present: MPEG-PS and MPEG-TS).
> diff --git a/libavformat/avformat.h b/libavformat/avformat.h
> index b6c63e2237..c6956626b5 100644
> --- a/libavformat/avformat.h
> +++ b/libavformat/avformat.h
> @@ -1884,6 +1884,16 @@ typedef struct AVFormatContext {
> * @see skip_estimate_duration_from_pts
> */
> int64_t duration_probesize;
> +
> + /**
> + * Allow AVStream.codecpar codec_type and codec_id to change at runtime
> + * (for example when MPEG-TS PMT ES stream_type changes at runtime).
> + * This is disabled by default, because most clients of avformat API
> + * do not support random mid-stream codec changes.
> + * - muxing: unused
> + * - demuxing: set by user
> + */
> + int allow_codec_changes;
> } AVFormatContext;
>
> /**
> diff --git a/libavformat/demux.c b/libavformat/demux.c
> index ecd4f40da9..3749ab67a3 100644
> --- a/libavformat/demux.c
> +++ b/libavformat/demux.c
> @@ -1292,9 +1292,15 @@ static int codec_close(FFStream *sti)
> {
> AVCodecContext *avctx_new = NULL;
> AVCodecParameters *par_tmp = NULL;
> + const AVCodec *new_codec = NULL;
> int ret;
>
> - avctx_new = avcodec_alloc_context3(sti->avctx->codec);
> + new_codec =
> + (sti->avctx->codec_id != sti->pub.codecpar->codec_id) ?
> + avcodec_find_decoder(sti->pub.codecpar->codec_id) :
> + sti->avctx->codec;
> +
> + avctx_new = avcodec_alloc_context3(new_codec);
> if (!avctx_new) {
> ret = AVERROR(ENOMEM);
> goto fail;
> diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
> index deb69a0548..0c78e870bb 100644
> --- a/libavformat/mpegts.c
> +++ b/libavformat/mpegts.c
> @@ -2510,7 +2510,9 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
> if (!st)
> goto out;
>
> - if (pes && pes->stream_type != stream_type)
> + if (pes && (!pes->stream_type ||
> + (pes->stream_type != stream_type &&
> + ts->stream->allow_codec_changes)))
> mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
>
> add_pid_to_program(prg, pid);
> diff --git a/libavformat/options_table.h b/libavformat/options_table.h
> index e2e690fd2a..e949d60c3e 100644
> --- a/libavformat/options_table.h
> +++ b/libavformat/options_table.h
> @@ -106,6 +106,7 @@ static const AVOption avformat_options[] = {
> {"skip_estimate_duration_from_pts", "skip duration calculation in estimate_timings_from_pts", OFFSET(skip_estimate_duration_from_pts), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, D},
> {"max_probe_packets", "Maximum number of packets to probe a codec", OFFSET(max_probe_packets), AV_OPT_TYPE_INT, { .i64 = 2500 }, 0, INT_MAX, D },
> {"duration_probesize", "Maximum number of bytes to probe the durations of the streams in estimate_timings_from_pts", OFFSET(duration_probesize), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, D},
> +{"allow_codec_changes", "Allow AVStream.codecpar codec_type and codec_id to change at runtime", OFFSET(allow_codec_changes), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, D},
> {NULL},
> };
>
> diff --git a/libavformat/version.h b/libavformat/version.h
> index a7c80dc564..904e7f06aa 100644
> --- a/libavformat/version.h
> +++ b/libavformat/version.h
> @@ -31,7 +31,7 @@
>
> #include "version_major.h"
>
> -#define LIBAVFORMAT_VERSION_MINOR 1
> +#define LIBAVFORMAT_VERSION_MINOR 2
> #define LIBAVFORMAT_VERSION_MICRO 100
>
> #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
> diff --git a/tests/fate/demux.mak b/tests/fate/demux.mak
> index ead5ad4b10..4a39e950b9 100644
> --- a/tests/fate/demux.mak
> +++ b/tests/fate/demux.mak
> @@ -158,7 +158,7 @@ FATE_SAMPLES_DEMUX-$(CONFIG_XWMA_DEMUXER) += fate-xwma-demux
> fate-xwma-demux: CMD = crc -i $(TARGET_SAMPLES)/xwma/ergon.xwma -c:a copy
>
> FATE_FFPROBE_DEMUX-$(CONFIG_MPEGTS_DEMUXER) += fate-ts-demux
> -fate-ts-demux: CMD = ffprobe_demux $(TARGET_SAMPLES)/ac3/mp3ac325-4864-small.ts
> +fate-ts-demux: CMD = ffprobe_demux $(TARGET_SAMPLES)/ac3/mp3ac325-4864-small.ts -allow_codec_changes 1
>
> FATE_FFPROBE_DEMUX-$(CONFIG_MPEGTS_DEMUXER) += fate-ts-timed-id3-demux
> fate-ts-timed-id3-demux: CMD = ffprobe_demux $(TARGET_SAMPLES)/mpegts/id3.ts
> --
> 2.43.0
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
>
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [FFmpeg-devel] [PATCH] avformat/demux: Add allow_codec_changes option to AVFormatContext (v2)
2025-06-17 18:39 ` Marton Balint
@ 2025-06-17 19:38 ` Pavel Koshevoy
2025-06-17 20:46 ` Michael Niedermayer
1 sibling, 0 replies; 4+ messages in thread
From: Pavel Koshevoy @ 2025-06-17 19:38 UTC (permalink / raw)
To: FFmpeg development discussions and patches
On Tue, Jun 17, 2025 at 12:42 PM Marton Balint <cus@passwd.hu> wrote:
>
>
> On Tue, 17 Jun 2025, Pavel Koshevoy wrote:
>
> > Make runtime AVStream.codecpar codec_id updates optional and disabled
> > by default, so that avformat API clients can enable this feature
> explicitly
> > when they add support for runtime codec changes.
> >
> > Accordingly, codec_close should not assume that the codec_id cannot
> change.
> > This fixes 'ffprobe 1_poc.mp4' segfault introduced with
> > commit 0021484d05f9b0f032fa319399de6e24eea0c04f.
> > ---
> > doc/APIchanges | 3 +++
> > doc/formats.texi | 6 ++++++
> > libavformat/avformat.h | 10 ++++++++++
> > libavformat/demux.c | 8 +++++++-
> > libavformat/mpegts.c | 4 +++-
> > libavformat/options_table.h | 1 +
> > libavformat/version.h | 2 +-
> > tests/fate/demux.mak | 2 +-
> > 8 files changed, 32 insertions(+), 4 deletions(-)
> >
> > diff --git a/doc/APIchanges b/doc/APIchanges
> > index 91710bb27d..29b3389094 100644
> > --- a/doc/APIchanges
> > +++ b/doc/APIchanges
> > @@ -2,6 +2,9 @@ The last version increases of all libraries were on
> 2025-03-28
> >
> > API changes, most recent first:
> >
> > +2025-06-17 - xxxxxxxxxx - lavf 62.2.100 - avformat.h
> > + Add AVFormatContext.allow_codec_changes AVOption.
>
> Considering that this is a simple boolean option, maybe you should use
> AVFormatContext->flags instead, so no new field need to be added, only a
> new flag.
>
Are you referring to AVFMT_FLAG_... flags?
Are they controllable from the command line, as an AVOption?
I referenced AVFormatContext.skip_estimate_duration_from_pts as an example,
and that's not a flag.
I'd like a second opinion on this.
Thank you,
Pavel.
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [FFmpeg-devel] [PATCH] avformat/demux: Add allow_codec_changes option to AVFormatContext (v2)
2025-06-17 18:39 ` Marton Balint
2025-06-17 19:38 ` Pavel Koshevoy
@ 2025-06-17 20:46 ` Michael Niedermayer
1 sibling, 0 replies; 4+ messages in thread
From: Michael Niedermayer @ 2025-06-17 20:46 UTC (permalink / raw)
To: FFmpeg development discussions and patches
[-- Attachment #1.1: Type: text/plain, Size: 2105 bytes --]
On Tue, Jun 17, 2025 at 08:39:05PM +0200, Marton Balint wrote:
>
>
> On Tue, 17 Jun 2025, Pavel Koshevoy wrote:
>
> > Make runtime AVStream.codecpar codec_id updates optional and disabled
> > by default, so that avformat API clients can enable this feature explicitly
> > when they add support for runtime codec changes.
> >
> > Accordingly, codec_close should not assume that the codec_id cannot change.
> > This fixes 'ffprobe 1_poc.mp4' segfault introduced with
> > commit 0021484d05f9b0f032fa319399de6e24eea0c04f.
> > ---
> > doc/APIchanges | 3 +++
> > doc/formats.texi | 6 ++++++
> > libavformat/avformat.h | 10 ++++++++++
> > libavformat/demux.c | 8 +++++++-
> > libavformat/mpegts.c | 4 +++-
> > libavformat/options_table.h | 1 +
> > libavformat/version.h | 2 +-
> > tests/fate/demux.mak | 2 +-
> > 8 files changed, 32 insertions(+), 4 deletions(-)
> >
> > diff --git a/doc/APIchanges b/doc/APIchanges
> > index 91710bb27d..29b3389094 100644
> > --- a/doc/APIchanges
> > +++ b/doc/APIchanges
> > @@ -2,6 +2,9 @@ The last version increases of all libraries were on 2025-03-28
> >
> > API changes, most recent first:
> >
> > +2025-06-17 - xxxxxxxxxx - lavf 62.2.100 - avformat.h
> > + Add AVFormatContext.allow_codec_changes AVOption.
>
> Considering that this is a simple boolean option, maybe you should use
> AVFormatContext->flags instead, so no new field need to be added, only a new
> flag.
as a seperate value it could be extended to somethig like
-allow_codec_changes ALL
-allow_codec_changes NONE
-allow_codec_changes WITHIN_TYPE (no type change like video to audio)
but not sure this makes sense or is even reasonable implementation wise
anyway i have nothing against making this a flag if people prefer
just wanted to provide an example where a flag would be at a disadvantage
thx
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
The worst form of inequality is to try to make unequal things equal.
-- Aristotle
[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]
[-- Attachment #2: Type: text/plain, Size: 251 bytes --]
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-06-17 20:46 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-06-17 14:36 [FFmpeg-devel] [PATCH] avformat/demux: Add allow_codec_changes option to AVFormatContext (v2) Pavel Koshevoy
2025-06-17 18:39 ` Marton Balint
2025-06-17 19:38 ` Pavel Koshevoy
2025-06-17 20:46 ` Michael Niedermayer
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