PR #25952 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25952
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25952.patch

A comment on #20471 reports that `-frames:v 1170`, with the audio going through 
loudnorm into MXF, writes only 1167 frames on every track. It reproduces with 
lavfi sources at 30000/1001 fps: 1169 of 1170 edit units.

**Cause**

In `receive_for_stream()`, once a limiting stream is finished (frame limit or 
`-shortest`), an audio frame ending after the end of that stream is dropped 
whole. loudnorm outputs frames of about 100 ms, so the audio ends up to 100 ms 
short, which is 3 frames at 29.97 fps, and the MXF muxer drops the video edit 
units that have no audio. With ordinary 1024-sample frames the shortfall is 
less than one video frame and went unnoticed. The latency of loudnorm is not 
involved: the frame straddling the end reaches the sync queue and is discarded 
there.

**Fix**

For streams whose encoder takes any number of samples (`frame_samples == 0`, 
e.g. PCM), the frame straddling the end of the limiting stream is read from the 
fifo and its `nb_samples` and `duration` are reduced so that it ends exactly 
there. Encoders with a fixed frame size are unchanged.

**Test**

New test `fate-ffmpeg-frames-cut-audio`: lavfi `color` at 30000/1001 fps plus 
`sine` with `samples_per_frame=4800`, `-frames:v 10`, framecrc. Without the fix 
the last audio packet (1616 samples) is missing.

The audio ends exactly at the video end at 25, 29.97, 23.976 and 59.94 fps, and 
with `-shortest`. A full FATE run changes no existing reference.

This addresses the `-frames:v` + loudnorm report in that comment only; the 
issue itself is about SCC captions.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



From 12c22b8be869d66f4a2ab6d20f741b43cfac69bd Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Wed, 11 Nov 2026 13:08:57 -0800
Subject: [PATCH] fftools/sync_queue: cut the audio frame at the end of the
 limiting stream

An audio frame ending after the end of a finished limiting stream was
dropped, so with -frames:v or -shortest the audio could be shorter than
the video by up to a frame. That is 100ms with the frames output by
loudnorm, and muxers needing audio for every video frame, like MXF,
then dropped the last video frames.

Output the samples up to the end of the limiting stream when the
encoder takes any number of samples.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 fftools/sync_queue.c                   | 30 ++++++++++++++++++++++++++
 tests/fate/ffmpeg.mak                  | 10 +++++++++
 tests/ref/fate/ffmpeg-frames-cut-audio | 24 +++++++++++++++++++++
 3 files changed, 64 insertions(+)
 create mode 100644 tests/ref/fate/ffmpeg-frames-cut-audio

diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c
index 62e9e093aad..0b0465e790e 100644
--- a/fftools/sync_queue.c
+++ b/fftools/sync_queue.c
@@ -563,6 +563,36 @@ static int receive_for_stream(SyncQueue *sq, unsigned int 
stream_idx,
 
             return 0;
         }
+
+        /* The frame ends after the finished stream limiting the queue.
+         * Encoders taking any number of samples get the samples up to that
+         * end, so that the stream is not shorter than the limiting one
+         * by up to a frame. */
+        if (sq->type == SYNC_QUEUE_FRAMES && !st->frame_samples &&
+            peek.f->nb_samples && sq->head_finished_stream >= 0) {
+            const SyncQueueStream *st_end = 
&sq->streams[sq->head_finished_stream];
+            int64_t end = av_rescale_q(st_end->head_ts, st_end->tb, st->tb);
+            int64_t samples = av_rescale_q(end - peek.f->pts, st->tb,
+                                           (AVRational){ 1, 
peek.f->sample_rate });
+
+            if (samples > 0 && samples < peek.f->nb_samples) {
+                int ret = av_container_fifo_read(st->fifo, SQPTR(sq, frame), 
0);
+                av_assert0(ret >= 0);
+
+                av_assert0(st->samples_queued >= frame.f->nb_samples);
+                st->samples_queued -= frame.f->nb_samples;
+
+                frame.f->nb_samples = samples;
+                frame.f->duration   = end - frame.f->pts;
+
+                av_log(sq->logctx, AV_LOG_DEBUG,
+                       "sq: receive %u ts %s cut at the end of %d\n", 
stream_idx,
+                       av_ts2timestr(frame_end(sq, frame, 0), &st->tb),
+                       sq->head_finished_stream);
+
+                return 0;
+            }
+        }
     }
 
     return (sq->finished || (st->finished && 
!av_container_fifo_can_read(st->fifo))) ?
diff --git a/tests/fate/ffmpeg.mak b/tests/fate/ffmpeg.mak
index b46eff0cf3b..58626e0ecd1 100644
--- a/tests/fate/ffmpeg.mak
+++ b/tests/fate/ffmpeg.mak
@@ -125,6 +125,16 @@ 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) \
+                           += fate-ffmpeg-frames-cut-audio
+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
+
 # 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 00000000000..7d99039625d
--- /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
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to