From a4ee3bbb278798858c2228304658cdf99cb75004 Mon Sep 17 00:00:00 2001 From: Xenolphthalein <21215869+Xenolphthalein@users.noreply.github.com> Date: Wed, 18 Nov 2026 12:01:12 +0100 Subject: [PATCH] media: venus: wait for the first source change in G_FMT(CAPTURE) FFmpeg's v4l2m2m decoders queue the stream, start OUTPUT and ask for the CAPTURE format before the source change event. With the event subscribed, Venus answers -EINVAL until the firmware has parsed the headers, about 2 ms later. FFmpeg takes that as fatal for its capture setup and drops the packet it holds, then the next one, until the format is known: in one run 36 of 60 packets never reached the decoder, so every later frame referenced pictures it never got, and when the window lasted to the end of the file nothing came out at all. GStreamer waits for the event and is not affected. While OUTPUT streams with buffers handed to the firmware, wait up to 1 s for the first source change, as iris does at CAPTURE STREAMON, and answer with the parsed format. Without a stream the error stays. On the PICO 4, FFmpeg's hevc_v4l2m2m (with and without B-frames), h264_v4l2m2m and vp8_v4l2m2m decode 60 frames of 1080p60 and vp9_v4l2m2m 60 frames of 720p30, every frame equal to software decoding in five runs each, where before one of fifteen runs did. --- drivers/media/platform/qcom/venus/vdec.c | 29 ++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/drivers/media/platform/qcom/venus/vdec.c b/drivers/media/platform/qcom/venus/vdec.c index 3ef2c7200689..6c830ecc1aef 100644 --- a/drivers/media/platform/qcom/venus/vdec.c +++ b/drivers/media/platform/qcom/venus/vdec.c @@ -248,14 +248,39 @@ static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f) return 0; } +/* + * How long G_FMT(CAPTURE) waits for the firmware's first source change while it holds the + * client's stream: it parses the headers within a few milliseconds. + */ +#define VDEC_FIRST_SRC_CHANGE_MS 1000 + static int vdec_check_src_change(struct venus_inst *inst) { int ret; if (inst->subscriptions & V4L2_EVENT_SOURCE_CHANGE && inst->codec_state == VENUS_DEC_STATE_INIT && - !inst->reconfig) - return -EINVAL; + !inst->reconfig) { + /* + * A client that queued its stream and asks for the CAPTURE format before the + * source change event, as FFmpeg's v4l2m2m decoders do, gets the format once the + * firmware has parsed the headers instead of an error: FFmpeg drops the packet it + * holds on every failed attempt and then decodes from a stream with holes. The + * wait is iris's at CAPTURE STREAMON. Without a stream there is nothing to wait for. + */ + if (!inst->streamon_out || !v4l2_m2m_num_src_bufs_ready(inst->m2m_ctx)) + return -EINVAL; + + ret = wait_event_timeout(inst->reconf_wait, inst->reconfig, + msecs_to_jiffies(VDEC_FIRST_SRC_CHANGE_MS)); + if (!ret) { + dev_dbg(inst->core->dev, VDBGH "no source change within %u ms\n", + VDEC_FIRST_SRC_CHANGE_MS); + return -EINVAL; + } + + return 0; + } if (inst->subscriptions & V4L2_EVENT_SOURCE_CHANGE) return 0;