PR #26282 opened by angelrenefm
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26282
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26282.patch

See the commit message for the analysis and the test on a Pixel 7. Full 
description below in the next edit.


From 5e6a50cddefdb7025be152d931630717d47effbe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=81ngel=20Ren=C3=A9?=
 <[email protected]>
Date: Sun, 22 Nov 2026 21:19:56 +0100
Subject: [PATCH] avcodec/v4l2_m2m_enc: return EOF when flushed before any
 frame was queued
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

If the encoder is flushed before it has been sent a single frame, for
example because the muxer failed to write its header, v4l2_receive_packet()
sends V4L2_ENC_CMD_STOP while neither queue is streaming, then starts both
queues and waits for the capture buffer marked V4L2_BUF_FLAG_LAST.

The stateful encoder interface says that a stop command issued while a
queue is not streaming does not start the drain sequence
(Documentation/userspace-api/media/v4l/dev-encoder.rst, "Drain"), so no
buffer is ever marked last and the dequeue polls forever with no timeout.
ffmpeg then hangs instead of exiting with the muxer's error.

Nothing was queued, so there is nothing to drain: return AVERROR_EOF.

Reproduced on a Pixel 7 (Google GS201, s5p-mfc with MFC v15 support):

  ffmpeg -f lavfi -i testsrc2=size=1280x720:rate=30 -frames:v 60 \
         -pix_fmt nv12 -c:v h264_v4l2m2m -b:v 4M out.mkv

Matroska needs extradata, which h264_v4l2m2m does not provide, so writing
the header fails. Without this patch ffmpeg hung in poll() on the encoder
device in 3 of 3 runs; with it, it exits at once with the muxer's error,
3 of 3. Encoding the same input to mp4 still gives 60 of 60 frames.

Signed-off-by: Ángel René <[email protected]>
Assisted-by: LLM
---
 libavcodec/v4l2_m2m_enc.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/libavcodec/v4l2_m2m_enc.c b/libavcodec/v4l2_m2m_enc.c
index 93703ccc63f..f10bf3822c9 100644
--- a/libavcodec/v4l2_m2m_enc.c
+++ b/libavcodec/v4l2_m2m_enc.c
@@ -304,6 +304,13 @@ static int v4l2_receive_packet(AVCodecContext *avctx, 
AVPacket *avpkt)
             frame = NULL;
     }
 
+    /* Nothing was ever queued: there is nothing to drain. The stop
+     * command would be sent before either queue streams, which the
+     * stateful encoder interface ignores, and no buffer would ever come
+     * back marked as the last one. */
+    if (!frame && !output->streamon)
+        return AVERROR_EOF;
+
     ret = v4l2_send_frame(avctx, frame);
     if (ret != AVERROR(EAGAIN))
         av_frame_unref(frame);
-- 
2.54.1

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

Reply via email to