PR #25989 opened by Baptiste Coudurier (bcoudurier) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25989 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25989.patch
Takes over FFmpeg/FFmpeg#25365 and addresses its review. Since 51f5f60443 the movie timescale is a common denominator of all stream time bases, and the QuickTime chapter track used it. With 22050 Hz audio and a 90 kHz stream that is 4410000 ticks/s, so a ten-minute chapter exceeds the INT_MAX packet duration limit. Long chapters were then dropped one by one (broken chapter list) or the chapter track vanished while the `chap` tref still pointed at it (#25317). 1. **avformat/movenc: exclude cover images from the movie timescale** — the original fix from #25365, unchanged except for moving the test out (ePirat's review). Artwork has no timeline and should not raise the movie timescale. 2. **avformat/movenc: give the QuickTime chapter track its own timescale** — following gvarph's review: the overflow also happens with any timed 90 kHz stream, not only covers. The chapter track now uses the common denominator of the chapter time bases when every chapter duration fits, else 1000 (the pre-51f5f60443 value). Chapter write errors are now propagated instead of silently dropping chapters. `mov-mp4-chapters` / `mov-mp4-frag-chapters-seek` refs return to 1/1000 chapter time bases, as before 51f5f60443. 3. **tests/fate/mov: add tests for long QuickTime chapters** — `mov-long-chapters` (timed 90 kHz MJPEG in MP4, gvarph's case) and `mov-cover-long-chapters` (cover art in M4B). `mov-long-chapters` fails without patch 2. Tested: full FATE on macOS/arm64 passes. Fixes #25317. 🤖 Generated with [Claude Code](https://claude.com/claude-code) From 964e399a8bc14a5d44c486aa2e5ffe28ac2ebb8e Mon Sep 17 00:00:00 2001 From: Michael Niedermayer <[email protected]> Date: Thu, 12 Nov 2026 12:17:02 -0800 Subject: [PATCH 1/3] avformat/movenc: exclude cover images from the movie timescale Cover images are stored as metadata and do not have a presentation timeline. Including their time base in the common movie timescale can raise it unnecessarily: 22050 Hz audio and a 90 kHz cover stream select 4410000 ticks per second. Exclude cover images using the same predicate as the artwork writer, while retaining timed thumbnail streams and explicit movie_timescale settings. Update the existing cover-image checksum for the changed movie timing fields. Fixes: 51f5f60443eec9944c9d0cf24e0766edf403d7ef Assisted-by: Fairy Signed-off-by: Baptiste Coudurier <[email protected]> --- libavformat/movenc.c | 4 +++- tests/ref/fate/mov-cover-image | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/libavformat/movenc.c b/libavformat/movenc.c index f36072470f3..38bbc4c3261 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -8664,7 +8664,9 @@ static int mov_init(AVFormatContext *s) track->par = st->codecpar; } - movie_timescale = av_gcd_q(movie_timescale, st->time_base, INT_MAX, (AVRational){1,0}); + /* Cover images are metadata and have no presentation timeline. */ + if (!is_cover_image(st)) + movie_timescale = av_gcd_q(movie_timescale, st->time_base, INT_MAX, (AVRational){1,0}); } if (!movie_timescale.den) movie_timescale = MOV_TIMESCALE_Q; diff --git a/tests/ref/fate/mov-cover-image b/tests/ref/fate/mov-cover-image index 6c6607c39c1..0811b5594a9 100644 --- a/tests/ref/fate/mov-cover-image +++ b/tests/ref/fate/mov-cover-image @@ -1,4 +1,4 @@ -124d92893a4a6dc8e0b5a6c384536e2e *tests/data/fate/mov-cover-image.mp4 +863c657c1ac377cada881745c3220eb8 *tests/data/fate/mov-cover-image.mp4 1024065 tests/data/fate/mov-cover-image.mp4 #extradata 0: 2, 0x00340022 #tb 0: 1/44100 -- 2.54.0 From 7785c19dd2178b29d83ebd002644d65c8bab17dc Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 12:18:32 -0800 Subject: [PATCH 2/3] avformat/movenc: give the QuickTime chapter track its own timescale Since 51f5f60443 the movie timescale is a common denominator of all stream time bases, so it can be very large: 22050 Hz audio next to a 90 kHz video stream selects 4410000 ticks per second. The chapter track used that timescale, which makes a ten-minute chapter exceed the INT_MAX packet duration limit. Such chapters were rejected while the others were kept, leaving a chapter track with missing samples and broken timing, or no chapter track at all while the chap tref still pointed at it. Use the common denominator of the chapter time bases when every chapter duration fits, and the former 1000 Hz timescale otherwise. Also propagate chapter packet write errors instead of silently dropping chapters. Fixes: 51f5f60443eec9944c9d0cf24e0766edf403d7ef Fixes: https://code.ffmpeg.org/FFmpeg/FFmpeg/issues/25317 Signed-off-by: Baptiste Coudurier <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> --- libavformat/movenc.c | 43 ++++++++++++++++++++--- tests/ref/fate/mov-mp4-chapters | 26 +++++++------- tests/ref/fate/mov-mp4-frag-chapters-seek | 2 +- 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/libavformat/movenc.c b/libavformat/movenc.c index 38bbc4c3261..50176ab4134 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -7929,6 +7929,37 @@ fail: } } +static int chapters_fit_timescale(AVFormatContext *s, int timescale) +{ + AVRational tb = { 1, timescale }; + + for (int i = 0; i < s->nb_chapters; i++) { + const AVChapter *c = s->chapters[i]; + int64_t start = av_rescale_q(c->start, c->time_base, tb); + int64_t end = av_rescale_q(c->end, c->time_base, tb); + + if (end - start < 0 || end - start > INT_MAX) + return 0; + } + return 1; +} + +/* The chapter track does not need to share the movie timescale, which can be + * high enough for long chapter durations to overflow. Use a timescale that + * represents the chapter times exactly when their durations fit, and fall + * back to the historical millisecond timescale otherwise. */ +static int mov_chapter_timescale(AVFormatContext *s) +{ + AVRational tb = { 0, 1 }; + + for (int i = 0; i < s->nb_chapters; i++) + tb = av_gcd_q(tb, s->chapters[i]->time_base, INT_MAX, (AVRational){ 1, 0 }); + + if (tb.den > MOV_TIMESCALE && chapters_fit_timescale(s, tb.den)) + return tb.den; + return MOV_TIMESCALE; +} + // QuickTime chapters involve an additional text track with the chapter names // as samples, and a tref pointing from the other tracks to the chapter one. static int mov_create_chapter_track(AVFormatContext *s, int tracknum) @@ -7962,7 +7993,7 @@ static int mov_create_chapter_track(AVFormatContext *s, int tracknum) track->mode = mov->mode; track->tag = MKTAG('t','e','x','t'); - track->timescale = mov->movie_timescale; + track->timescale = mov_chapter_timescale(s); track->par = avcodec_parameters_alloc(); if (!track->par) return AVERROR(ENOMEM); @@ -7992,8 +8023,8 @@ static int mov_create_chapter_track(AVFormatContext *s, int tracknum) AVChapter *c = s->chapters[i]; AVDictionaryEntry *t; - int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,mov->movie_timescale}); - pkt->pts = pkt->dts = av_rescale_q(c->start, c->time_base, (AVRational){1,mov->movie_timescale}); + int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,track->timescale}); + pkt->pts = pkt->dts = av_rescale_q(c->start, c->time_base, (AVRational){1,track->timescale}); pkt->duration = end - pkt->dts; if ((t = av_dict_get(c->metadata, "title", NULL, 0))) { @@ -8011,8 +8042,12 @@ static int mov_create_chapter_track(AVFormatContext *s, int tracknum) AV_WB16(pkt->data, len); memcpy(pkt->data + 2, t->value, len); memcpy(pkt->data + len + 2, encd, sizeof(encd)); - ff_mov_write_packet(s, pkt); + ret = ff_mov_write_packet(s, pkt); av_freep(&pkt->data); + if (ret < 0) { + av_packet_unref(pkt); + return ret; + } } } diff --git a/tests/ref/fate/mov-mp4-chapters b/tests/ref/fate/mov-mp4-chapters index 02f267a0fb2..41a8b211636 100644 --- a/tests/ref/fate/mov-mp4-chapters +++ b/tests/ref/fate/mov-mp4-chapters @@ -1,4 +1,4 @@ -f0289770c1fb126b195755ba7ccc1f50 *tests/data/fate/mov-mp4-chapters.mp4 +fadf37d00540a8023cada062d853fe42 *tests/data/fate/mov-mp4-chapters.mp4 111288 tests/data/fate/mov-mp4-chapters.mp4 #extradata 0: 3469, 0xc6769ddc #tb 0: 1/44100 @@ -13,37 +13,37 @@ f0289770c1fb126b195755ba7ccc1f50 *tests/data/fate/mov-mp4-chapters.mp4 0, 4352, 4352, 256, 41, 0xbccd1463 [CHAPTER] id=0 -time_base=1/44100 +time_base=1/1000 start=0 start_time=0.000000 -end=220500 +end=5000 end_time=5.000000 TAG:title=start [/CHAPTER] [CHAPTER] id=1 -time_base=1/44100 -start=220500 +time_base=1/1000 +start=5000 start_time=5.000000 -end=463050 +end=10500 end_time=10.500000 TAG:title=Five Seconds [/CHAPTER] [CHAPTER] id=2 -time_base=1/44100 -start=463050 +time_base=1/1000 +start=10500 start_time=10.500000 -end=661500 +end=15000 end_time=15.000000 TAG:title=Ten point 5 seconds [/CHAPTER] [CHAPTER] id=3 -time_base=1/44100 -start=661500 +time_base=1/1000 +start=15000 start_time=15.000000 -end=875341 -end_time=19.849002 +end=19849 +end_time=19.849000 TAG:title=15 sec - over soon [/CHAPTER] diff --git a/tests/ref/fate/mov-mp4-frag-chapters-seek b/tests/ref/fate/mov-mp4-frag-chapters-seek index eae6e079bc6..107d2153844 100644 --- a/tests/ref/fate/mov-mp4-frag-chapters-seek +++ b/tests/ref/fate/mov-mp4-frag-chapters-seek @@ -1,4 +1,4 @@ -76aed9319dda2a96839a9d1fe1ae52cb *tests/data/fate/mov-mp4-frag-chapters-seek.mp4 +5cf663c8d0c5532c11ea06b21a09ba7b *tests/data/fate/mov-mp4-frag-chapters-seek.mp4 1363303 tests/data/fate/mov-mp4-frag-chapters-seek.mp4 #extradata 0: 31, 0x64c405ed #tb 0: 1/12800 -- 2.54.0 From c8e18fbb89269e64e2228a0575ac3854dd0bd4d8 Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 12:18:32 -0800 Subject: [PATCH 3/3] tests/fate/mov: add tests for long QuickTime chapters Write two ten-minute chapters next to 22050 Hz audio and a 90 kHz MJPEG stream, once as timed video in MP4 and once as cover art in M4B. Both select a large common timescale for the media streams, which used to push the chapter durations past INT_MAX. Signed-off-by: Baptiste Coudurier <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> --- tests/fate/mov.mak | 32 ++++++++++++++++++++++++++ tests/mov-long-chapters.ffmeta | 11 +++++++++ tests/ref/fate/mov-cover-long-chapters | 28 ++++++++++++++++++++++ tests/ref/fate/mov-long-chapters | 28 ++++++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 tests/mov-long-chapters.ffmeta create mode 100644 tests/ref/fate/mov-cover-long-chapters create mode 100644 tests/ref/fate/mov-long-chapters diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak index 01b1346cec5..f5858345f86 100644 --- a/tests/fate/mov.mak +++ b/tests/fate/mov.mak @@ -626,6 +626,38 @@ fate-mov-stereo3d-sbs-mp4: CMD = transcode rawvideo $(TARGET_PATH)/tests/data/vs "-c:v mpeg4 -qscale:v 10 -stereo3d:v sbsl -strict unofficial -frames:v 2" \ "-c:v copy" "-show_entries stream_side_data_list" "" "" "-s 352x288 -pix_fmt yuv420p" +# Ten-minute chapters with a 90 kHz video stream next to 22050 Hz audio. The +# common movie timescale of these streams is too large for the chapter +# durations to fit in 32 bits, so the chapter track must use its own timescale. +MOV_LONG_CHAPTERS_DEPS = LAVFI_INDEV SINE_FILTER COLOR_FILTER \ + PCM_S16$(if $(filter yes,$(HAVE_BIGENDIAN)),BE,LE)_DECODER \ + WRAPPED_AVFRAME_DECODER AAC_ENCODER MJPEG_ENCODER \ + ARESAMPLE_FILTER SCALE_FILTER FFMETADATA_DEMUXER \ + MOV_DEMUXER FILE_PROTOCOL + +# $(1): output format, $(2): file extension, $(3): extra output options +mov_long_chapters = run_with_temp \ + "$(FFMPEG) -nostdin -v error \ + -f lavfi -i sine=r=22050:d=0.2 -f lavfi -i color=c=red:s=16x16:d=0.04 \ + -i $(TARGET_PATH)/tests/data/mov-long-chapters.ffmeta \ + -map 0:a -map 1:v -map_chapters 2 -c:a aac -c:v mjpeg \ + -fps_mode:v passthrough -enc_time_base:v 1/90000 $(3) \ + -threads 1 -f $(1) -y" \ + "ffprobe$(PROGSSUF)$(EXESUF) -v error \ + -show_entries stream=codec_name,codec_tag_string,time_base:stream_disposition=attached_pic:chapter=start_time,end_time:chapter_tags=title" $(2) + +FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, $(MOV_LONG_CHAPTERS_DEPS) MP4_MUXER) += fate-mov-long-chapters +fate-mov-long-chapters: CMD = $(call mov_long_chapters,mp4,mp4,) + +# Artwork is not part of the presentation and must not raise the movie timescale. +FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, $(MOV_LONG_CHAPTERS_DEPS) IPOD_MUXER) += fate-mov-cover-long-chapters +fate-mov-cover-long-chapters: CMD = $(call mov_long_chapters,ipod,m4b,-disposition:v attached_pic) + +fate-mov-long-chapters fate-mov-cover-long-chapters: tests/data/mov-long-chapters.ffmeta +tests/data/mov-long-chapters.ffmeta: TAG = COPY +tests/data/mov-long-chapters.ffmeta: $(SRC_PATH)/tests/mov-long-chapters.ffmeta | tests/data + $(M)cp $< $@ + FATE_FFMPEG += $(FATE_MOV_FFMPEG-yes) FATE_FFMPEG_FFPROBE += $(FATE_MOV_FFMPEG_FFPROBE-yes) diff --git a/tests/mov-long-chapters.ffmeta b/tests/mov-long-chapters.ffmeta new file mode 100644 index 00000000000..49e100fdf7e --- /dev/null +++ b/tests/mov-long-chapters.ffmeta @@ -0,0 +1,11 @@ +;FFMETADATA1 +[CHAPTER] +TIMEBASE=1/1000 +START=0 +END=600000 +title=CH 1 +[CHAPTER] +TIMEBASE=1/1000 +START=600000 +END=1200000 +title=CH 2 diff --git a/tests/ref/fate/mov-cover-long-chapters b/tests/ref/fate/mov-cover-long-chapters new file mode 100644 index 00000000000..5175bc9c9f8 --- /dev/null +++ b/tests/ref/fate/mov-cover-long-chapters @@ -0,0 +1,28 @@ +[STREAM] +codec_name=aac +codec_tag_string=mp4a +time_base=1/22050 +DISPOSITION:attached_pic=0 +[/STREAM] +[STREAM] +codec_name=bin_data +codec_tag_string=text +time_base=1/1000 +DISPOSITION:attached_pic=0 +[/STREAM] +[STREAM] +codec_name=mjpeg +codec_tag_string=[0][0][0][0] +time_base=1/90000 +DISPOSITION:attached_pic=1 +[/STREAM] +[CHAPTER] +start_time=0.000000 +end_time=600.000000 +TAG:title=CH 1 +[/CHAPTER] +[CHAPTER] +start_time=600.000000 +end_time=1200.000000 +TAG:title=CH 2 +[/CHAPTER] diff --git a/tests/ref/fate/mov-long-chapters b/tests/ref/fate/mov-long-chapters new file mode 100644 index 00000000000..6e7ad6d0ca3 --- /dev/null +++ b/tests/ref/fate/mov-long-chapters @@ -0,0 +1,28 @@ +[STREAM] +codec_name=aac +codec_tag_string=mp4a +time_base=1/22050 +DISPOSITION:attached_pic=0 +[/STREAM] +[STREAM] +codec_name=mjpeg +codec_tag_string=mp4v +time_base=1/90000 +DISPOSITION:attached_pic=0 +[/STREAM] +[STREAM] +codec_name=bin_data +codec_tag_string=text +time_base=1/1000 +DISPOSITION:attached_pic=0 +[/STREAM] +[CHAPTER] +start_time=0.000000 +end_time=600.000000 +TAG:title=CH 1 +[/CHAPTER] +[CHAPTER] +start_time=600.000000 +end_time=1200.000000 +TAG:title=CH 2 +[/CHAPTER] -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
