PR #25904 opened by Jun Zhao (mypopydev)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25904
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25904.patch

These two commits fix a -shortest failure in FFmpeg's sync queue. With a 
fixed-frame-size audio encoder (native AAC:
  1024 samples) fed smaller frames, overflow_heartbeat() used the first queued 
frame's own end time while
  receive_for_stream() used the chunk end; that mismatch stalled audio release 
until the 1 MiB auto-grown FIFO returned
   AVERROR(ENOSPC) — reported as "No space left on device" even though no disk 
is involved. The fix makes the heartbeat
   use the same chunk end, but only when the queued frame is shorter than the 
encoder frame size, so streams with
  larger frames behave exactly as before. The second commit adds a 
deterministic FATE regression test that feeds
  1-sample frames from a single lavfi input (no -re, no wall-clock or CPU 
race); it fails with ENOSPC on the unfixed
  tree and passes with the fix.


From 54f3bd152a624859a92123bac68eea5cac422e95 Mon Sep 17 00:00:00 2001
From: Jun Zhao <[email protected]>
Date: Tue, 10 Nov 2026 09:02:06 +0800
Subject: [PATCH 1/2] fftools/sync_queue: fix -shortest with frames smaller
 than the encoder frame size

When an audio encoder requires fixed-size output frames (the native AAC
encoder uses 1024 samples) but the frames reaching the sync queue are
smaller, receive_for_stream() releases frame_samples-sized chunks and
compares the chunk end against the queue head:

    ts = frame_end(sq, peek, nb_samples);

overflow_heartbeat() instead used the first queued frame's own end:

    tail_ts = frame_end(sq, frame, 0);

The heartbeat therefore lifted the lagging streams to a timestamp a little
short of the chunk end, so the chunk was never released. The audio FIFO then
grew without bound until av_fifo_write() hit the 1 MiB auto-grow limit and
returned AVERROR(ENOSPC), reported as "No space left on device" although no
disk is involved. -shortest is required, and the audio must run ahead of the
video by more than -shortest_buf_duration.

Use the chunk end in the heartbeat too, but only when the queued frame is
shorter than the encoder frame size. frame_end() ignores the frame's own
duration when nb_samples is given, so using it unconditionally would also
change streams whose frames are larger than frame_samples, making -shortest
write more audio than before. Keep frame_end(..., 0) for those, for packet
streams (sq_frame_samples() only applies to SYNC_QUEUE_FRAMES), and for
streams without a fixed frame size.

The mismatch was introduced by f9d3c06533 (2023-03-23), which added
frame_end(..., nb_samples) for receive but used frame_end(..., 0) in the
heartbeat. The stalled queue then grows until av_fifo_write() hits the
1 MiB auto-grow limit in libavutil/fifo.c and returns ENOSPC.

Fixes: https://code.ffmpeg.org/FFmpeg/FFmpeg/issues/25878
Signed-off-by: Jun Zhao <[email protected]>
---
 fftools/sync_queue.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c
index fbf288c6d19..62e9e093aad 100644
--- a/fftools/sync_queue.c
+++ b/fftools/sync_queue.c
@@ -298,8 +298,18 @@ static int overflow_heartbeat(SyncQueue *sq, int 
stream_idx)
 
     /* get the chosen stream's tail timestamp */
     for (size_t i = 0; tail_ts == AV_NOPTS_VALUE &&
-                       av_container_fifo_peek(st->fifo, (void**)&frame, i) >= 
0; i++)
-        tail_ts = frame_end(sq, frame, 0);
+                       av_container_fifo_peek(st->fifo, (void**)&frame, i) >= 
0; i++) {
+        int nb_samples = 0;
+
+        /* Use the chunk end from receive_for_stream() only when the queued
+         * frame is shorter than the encoder frame size; otherwise keep the
+         * frame's own end timestamp. */
+        if (sq->type == SYNC_QUEUE_FRAMES &&
+            frame.f->nb_samples < st->frame_samples)
+            nb_samples = st->frame_samples;
+
+        tail_ts = frame_end(sq, frame, nb_samples);
+    }
 
     /* overflow triggers when the tail is over specified duration behind the 
head */
     if (tail_ts == AV_NOPTS_VALUE || tail_ts >= st->head_ts ||
-- 
2.54.0


From 4e8b2c29a38fae9ebc8db6db6c809adfdc4014ef Mon Sep 17 00:00:00 2001
From: Jun Zhao <[email protected]>
Date: Tue, 10 Nov 2026 09:03:05 +0800
Subject: [PATCH 2/2] fate/ffmpeg: add regression test for -shortest with small
 audio frames

Feed 1-sample audio frames to the native AAC encoder (frame size 1024)
while a 1 fps video stream in the same lavfi input holds the queue head
behind. Without the preceding fix the audio sync queue grows until
av_fifo_write() returns ENOSPC and ffmpeg exits with 228 ("No space left
on device").

Putting both sources in one lavfi input makes avfilter_graph_request_oldest()
keep pulling the earlier audio output while the video sits at frame 0, so the
reproduction is deterministic and needs no -re sleep or CPU race. d=45
(360000 frames at 8 kHz, n=1) leaves room for frames drained by the heartbeat
over the 1 MiB / sizeof(void*) FIFO auto-grow limit (262144 entries on
32-bit, 131072 on 64-bit).

Verified on an idle machine and with 8, 16 and 24 CPU-bound processes: the
unfixed tree fails with three ENOSPC diagnostics in about a second and the
fixed tree passes. The test is skipped in reduced builds missing the gating
symbols. All 118 targets in tests/fate/ffmpeg.mak pass with the fix.

See: https://code.ffmpeg.org/FFmpeg/FFmpeg/issues/25878
Signed-off-by: Jun Zhao <[email protected]>
---
 tests/fate/ffmpeg.mak | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/tests/fate/ffmpeg.mak b/tests/fate/ffmpeg.mak
index c2bb00b67c6..1808f9d53f1 100644
--- a/tests/fate/ffmpeg.mak
+++ b/tests/fate/ffmpeg.mak
@@ -107,6 +107,24 @@ FATE_FFMPEG-$(call FILTERDEMDEC, AMIX ARESAMPLE SINE, 
RAWVIDEO, \
 fate-shortest: tests/data/vsynth1.yuv
 fate-shortest: CMD = framecrc -auto_conversion_filters -f lavfi -i 
"sine=3000:d=10" -f lavfi -i "sine=1000:d=1" -sws_flags +accurate_rnd+bitexact 
-fflags +bitexact -flags +bitexact -idct simple -f rawvideo -s 352x288 -pix_fmt 
yuv420p -i $(TARGET_PATH)/tests/data/vsynth1.yuv -filter_complex 
"[0:a:0][1:a:0]amix=inputs=2[audio]" -map 2:v:0 -map "[audio]" -sws_flags 
+accurate_rnd+bitexact -fflags +bitexact -flags +bitexact -idct simple -dct 
fastint -qscale 10 -threads 1 -c:v mpeg4 -c:a ac3_fixed -shortest
 
+# -shortest used to stall when the audio frames reaching the sync queue were
+# smaller than the encoder frame size: the audio FIFO grew until
+# av_fifo_write() returned ENOSPC. Putting both sources in one lavfi input
+# keeps the video stream at frame 0 while the audio runs ahead without -re.
+# d=45 (360000 frames at n=1) leaves room for frames drained by the
+# heartbeat over the 1 MiB / sizeof(void*) FIFO limit (262144 entries on
+# 32-bit, 131072 on 64-bit).
+FATE_FFMPEG-$(call ALLYES, LAVFI_INDEV COLOR_FILTER ANULLSRC_FILTER \
+                           ARESAMPLE_FILTER AFORMAT_FILTER SCALE_FILTER \
+                           FORMAT_FILTER PCM_U8_DECODER \
+                           WRAPPED_AVFRAME_DECODER MPEG4_ENCODER \
+                           AAC_ENCODER NULL_MUXER) \
+                           += fate-ffmpeg-shortest-small-audio-frames
+fate-ffmpeg-shortest-small-audio-frames: CMP = null
+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 -f null -
+
 # 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
-- 
2.54.0

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

Reply via email to