Commit 17220654ae for ffmpeg

commit 17220654ae8219a63e6f02dd5ff236a888a85755
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date:   Wed Nov 11 13:08:57 2026 -0800

    fftools/sync_queue: cut the audio frame at the end of the limiting stream

    With -frames:v or -shortest, the last audio frame was dropped instead of cut,
    so MXF lost video frames when the audio frames were long, as with loudnorm.

    Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
    Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>

diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c
index 62e9e093aa..20fa822e18 100644
--- a/fftools/sync_queue.c
+++ b/fftools/sync_queue.c
@@ -523,7 +523,7 @@ static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx,
         (st->frame_samples <= st->samples_queued || st->finished)) {
         int nb_samples = st->frame_samples;
         SyncQueueFrame peek;
-        int64_t ts;
+        int64_t ts, end = 0, cut = 0;
         int cmp = 1;

         if (st->finished)
@@ -534,14 +534,28 @@ static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx,

         /* check if this stream's tail timestamp does not overtake
          * the overall queue head */
-        if (ts != AV_NOPTS_VALUE && st_head)
+        if (ts != AV_NOPTS_VALUE && st_head) {
             cmp = av_compare_ts(ts, st->tb, st_head->head_ts, st_head->tb);

+            /* A frame ending after the final queue head is cut there when
+             * the encoder takes any number of samples, so that the stream
+             * does not end up to a frame before the limiting one. */
+            if (cmp > 0 && st_head->finished && !st->frame_samples &&
+                frame_samples(sq, peek)) {
+                AVRational sample_tb = { 1, peek.f->sample_rate };
+
+                end = av_rescale_q(st_head->head_ts, st_head->tb, st->tb);
+                cut = av_rescale_q(st_head->head_ts, st_head->tb, sample_tb) -
+                      av_rescale_q(peek.f->pts, st->tb, sample_tb);
+                cut = FFMIN(cut, peek.f->nb_samples);
+            }
+        }
+
         /* We can release frames that do not end after the queue head.
          * Frames with no timestamps are just passed through with no conditions.
          * Frames are also passed through when there are no limiting streams.
          */
-        if (cmp <= 0 || ts == AV_NOPTS_VALUE || !sq->have_limiting) {
+        if (cmp <= 0 || ts == AV_NOPTS_VALUE || !sq->have_limiting || cut > 0) {
             if (nb_samples &&
                 (nb_samples != peek.f->nb_samples || !frame_is_aligned(sq, peek.f))) {
                 int ret = receive_samples(sq, st, frame.f, nb_samples);
@@ -553,6 +567,11 @@ static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx,

                 av_assert0(st->samples_queued >= frame_samples(sq, frame));
                 st->samples_queued -= frame_samples(sq, frame);
+
+                if (cut > 0) {
+                    frame.f->nb_samples = cut;
+                    frame.f->duration   = end - frame.f->pts;
+                }
             }

             av_log(sq->logctx, AV_LOG_DEBUG,
diff --git a/tests/fate/ffmpeg.mak b/tests/fate/ffmpeg.mak
index b46eff0cf3..a0d99340a4 100644
--- a/tests/fate/ffmpeg.mak
+++ b/tests/fate/ffmpeg.mak
@@ -125,6 +125,21 @@ fate-ffmpeg-shortest-small-audio-frames: CMD = ffmpeg -auto_conversion_filters \
     -f lavfi -i "color=s=16x16:r=1:d=2[out0];anullsrc=r=8000:cl=mono:n=1:d=45[out1]" \
     -map 0:v -map 0:a -c:v mpeg4 -c:a aac -shortest -shortest_buf_duration 0.1 -f null -

+# The audio frame extending past the end of a frame-limited video stream is
+# cut there, it used to be dropped, making the audio up to a frame too short.
+FATE_FFMPEG-$(call ALLYES, LAVFI_INDEV COLOR_FILTER SINE_FILTER \
+                           WRAPPED_AVFRAME_DECODER PCM_S16LE_DECODER \
+                           RAWVIDEO_ENCODER PCM_S16LE_ENCODER FRAMECRC_MUXER \
+                           PIPE_PROTOCOL) \
+                           += fate-ffmpeg-frames-cut-audio fate-ffmpeg-frames-cut-audio-round
+fate-ffmpeg-frames-cut-audio: CMD = framecrc \
+    -f lavfi -i "color=s=16x16:r=30000/1001:d=2[out0];sine=r=48000:samples_per_frame=4800:d=2[out1]" \
+    -map 0:v -map 0:a -frames:v 10
+# the end, 1601.6 samples, rounds to the whole frame
+fate-ffmpeg-frames-cut-audio-round: CMD = framecrc \
+    -f lavfi -i "color=s=16x16:r=30000/1001:d=2[out0];sine=r=48000:samples_per_frame=1602:d=2[out1]" \
+    -map 0:v -map 0:a -frames:v 1
+
 # test interleaving video with a sparse subtitle stream
 FATE_SAMPLES_FFMPEG-$(call FRAMECRC, MATROSKA,, COLOR_FILTER VOBSUB_DEMUXER \
                            RAWVIDEO_ENCODER MATROSKA_MUXER) += fate-shortest-sub
diff --git a/tests/ref/fate/ffmpeg-frames-cut-audio b/tests/ref/fate/ffmpeg-frames-cut-audio
new file mode 100644
index 0000000000..7d99039625
--- /dev/null
+++ b/tests/ref/fate/ffmpeg-frames-cut-audio
@@ -0,0 +1,24 @@
+#tb 0: 1001/30000
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 16x16
+#sar 0: 1/1
+#tb 1: 1/48000
+#media_type 1: audio
+#codec_id 1: pcm_s16le
+#sample_rate 1: 48000
+#channel_layout_name 1: mono
+0,          0,          0,        1,      384, 0x29e05000
+1,          0,          0,     4800,     9600, 0x0b589fc2
+0,          1,          1,        1,      384, 0x29e05000
+0,          2,          2,        1,      384, 0x29e05000
+1,       4800,       4800,     4800,     9600, 0x0b589fc2
+0,          3,          3,        1,      384, 0x29e05000
+0,          4,          4,        1,      384, 0x29e05000
+0,          5,          5,        1,      384, 0x29e05000
+1,       9600,       9600,     4800,     9600, 0x0b589fc2
+0,          6,          6,        1,      384, 0x29e05000
+0,          7,          7,        1,      384, 0x29e05000
+0,          8,          8,        1,      384, 0x29e05000
+1,      14400,      14400,     1616,     3232, 0xce663b60
+0,          9,          9,        1,      384, 0x29e05000
diff --git a/tests/ref/fate/ffmpeg-frames-cut-audio-round b/tests/ref/fate/ffmpeg-frames-cut-audio-round
new file mode 100644
index 0000000000..61def4a0d2
--- /dev/null
+++ b/tests/ref/fate/ffmpeg-frames-cut-audio-round
@@ -0,0 +1,12 @@
+#tb 0: 1001/30000
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 16x16
+#sar 0: 1/1
+#tb 1: 1/48000
+#media_type 1: audio
+#codec_id 1: pcm_s16le
+#sample_rate 1: 48000
+#channel_layout_name 1: mono
+0,          0,          0,        1,      384, 0x29e05000
+1,          0,          0,     1602,     3204, 0x49682915