Skip to content

media/hevc_d: Accept the unset default SPS in try_ctrl_sps - #7796

Closed
YORROY123 wants to merge 2 commits into
raspberrypi:rpi-6.18.yfrom
YORROY123:hevc-d-accept-unset-sps
Closed

YORROY123 wants to merge 2 commits into
raspberrypi:rpi-6.18.yfrom
YORROY123:hevc-d-accept-unset-sps

Conversation

@YORROY123

Copy link
Copy Markdown
Contributor

The SPS control's default value is all zeros, which is also how the driver
marks the SPS as "not yet set" (is_sps_set() checks for a zero width).
try_ctrl_sps() rejects that value (chroma_format_idc 0, 0x0 picture), so
the control's own default cannot be written back. On a Pi 5,
v4l2-compliance -m /dev/media0 reports:

fail: v4l2-test-controls.cpp(939): try_ext_ctrls returned an error (22)
test VIDIOC_G/S/TRY_EXT_CTRLS: FAIL
Grand Total for rpi-hevc-dec device /dev/media0: 57, Succeeded: 56, Failed: 1

and each attempt logs Chroma format (0) unsupported.

The change returns early from try_ctrl_sps() when the SPS is unset. I
did not give the control a non-zero default size instead, because that would
make is_sps_set() true before userspace has set an SPS and would change the
CAPTURE format enumeration. Decoding already refuses to start with "SPS never
set".

Tested on a Pi 5 (6.18.39 with this change built as an out-of-tree module),
comparing against the stock module:

stock with change
v4l2-compliance -m /dev/media0 56/57 57/57
TRY/S_EXT_CTRLS with the default SPS EINVAL OK
SPS with 4:2:2, 12-bit or 8192 width EINVAL EINVAL
CAPTURE ENUM_FMT before an SPS is set 4 formats 4 formats
ffmpeg -hwaccel drm, 720p 8-bit and 10-bit HEVC frame MD5s match software decode frame MD5s match software decode

The change also applies on top of #7780 and runs before its new SPS checks, so
those checks never see the unset default.

The failure was found with v4l2-compliance. The analysis, the fix and this
description were written with help from an AI coding assistant (Claude). The
tests above were run on real hardware.

The default value of the SPS control is all zeros, and a zero
pic_width_in_luma_samples is how the driver marks the SPS as not yet set
(is_sps_set()). try_ctrl_sps() rejects that value though, because
chroma_format_idc is 0 and the picture is 0x0, so the control's own
default cannot be written back. v4l2-compliance reads the control and
passes the value to VIDIOC_TRY_EXT_CTRLS, which fails:

  fail: v4l2-test-controls.cpp(939): try_ext_ctrls returned an error (22)
  test VIDIOC_G/S/TRY_EXT_CTRLS: FAIL

and every attempt also logs "Chroma format (0) unsupported".

Upstream commit 4f4b6a9 ("media: v4l2-ctrls: default HEVC SPS
chroma format to 4:2:0") would not fix this on its own, as the 0x0
picture is still rejected. Giving the control a non-zero default size
instead would make is_sps_set() true before userspace has set an SPS
and change the CAPTURE format enumeration.

Treat a zero width as unset and accept it without further checks.
Decoding already refuses to start with "SPS never set", and the CAPTURE
format code keeps offering all formats until a real SPS is set.

Tested on a Pi 5 with 6.18.39 and this change built as an out-of-tree
module:
- v4l2-compliance -m /dev/media0: 56/57 -> 57/57
- a 4:2:2, 12-bit or 8192 wide SPS is still rejected
- CAPTURE ENUM_FMT before an SPS is set is unchanged (4 formats)
- ffmpeg -hwaccel drm decoding 8-bit and 10-bit 720p HEVC gives frame
  MD5s identical to software decoding, before and after the change

The failure was found by running v4l2-compliance on a Pi 5. The
analysis, the fix and this changelog were written with the help of an
AI coding assistant (Claude), and the testing above was run on real
hardware.

Fixes: 6853a7e ("media: platform: Add Raspberry Pi HEVC decoder driver")
Signed-off-by: Roy <rongh7426@gmail.com>
@6by9

6by9 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

The failure is mentioned in the cover letter for V2 of the patchset upstreaming the driver - https://patchwork.linuxtv.org/project/linux-media/cover/20260304-media-rpi-hevc-dec-v6-0-93868ae6dff8@raspberrypi.com/

  • v4l2-compliance output added to cover letter (Nicholas)
    I believe the "fail: v4l2-test-controls.cpp(939): try_ext_ctrls
    returned an error (22)" is expected as it is validating the SPS.
    Hantro and Cedrus certainly both appear to return errors in the same place

If upstream care (and they wish to fix Hantro and Cedrus as well), then it's worth fixing.
If they don't care, then adding it isn't worth it just to make v4l2-compliance happy. It's not as if setting that value into the control has any real-world use.

The one bit that would be worthwhile would be squashing any log line should they try that, otherwise it's a mechanism to flood the kernel logs.

try_ctrl_sps(), try_ctrl_pps() and try_ctrl_slice_params() print a
v4l2_warn() for every value they reject. Any user who can open the
device can call VIDIOC_TRY_EXT_CTRLS in a loop and fill the kernel log.

Use hevc_d_dbg() at level 1 instead, so the messages are still
available with the debug module parameter.

Tested on a Pi 5 (6.18.39, out-of-tree module), as a normal user in the
video group, with 20000 VIDIOC_TRY_EXT_CTRLS calls using a 4:2:2 SPS,
counting new kernel log records with the /dev/kmsg sequence number:
- before: 20000 new log records
- after: 0 new log records
- after, with debug=1: 20000 new log records
v4l2-compliance and 8-bit and 10-bit ffmpeg -hwaccel drm decoding give
the same results as before. try_ctrl_slice_params() does not exist in
the 6.18.39 driver, so its 3 messages were only build tested.

Suggested by Dave Stevenson in the review of this pull request. The
change and this changelog were written with the help of an AI coding
assistant (Claude).

Signed-off-by: Roy <rongh7426@gmail.com>
@YORROY123

Copy link
Copy Markdown
Contributor Author

Thanks. I added a second commit for the log flooding: the messages in try_ctrl_sps/pps/slice_params now use hevc_d_dbg(1, ...). As a normal user, 20000 VIDIOC_TRY_EXT_CTRLS calls with a bad SPS added 20000 kernel log records before and 0 after (they are still there with debug=1).

On the first commit: upstream took 4f4b6a9 in August for the same compliance failure in rkvdec, so it may be wanted after all, but I'm happy to drop it if you'd rather not carry it. If this conflicts with #7780 I can rebase on top of it.

@6by9

6by9 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

As you can see, #7780 is doing a load of updates to try and clean up the driver before I do the next patchset for upstream.

Would you mind if I just pass this over to @jc-kynesim to add into that as he sees fit? It avoids having to consider additional potential Signed-off-by and similar tags on the patches for upstream.

I think I prefer the approach taken in 4f4b6a9 to always initialise the control to something vaguely sane, rather than accepting an all-zero SPS control. Particularly if that has been accepted for rkvdec. Again I'll defer to @jc-kynesim though.

@YORROY123

Copy link
Copy Markdown
Contributor Author

Sure, fine by me, please pass it on to @jc-kynesim. If any of it gets folded in, a "Reported-by: Roy rongh7426@gmail.com" would be appreciated.

One thing to watch with a non-zero default: is_sps_set() treats pic_width_in_luma_samples == 0 as "SPS not set yet". When I gave the control a default size (1920x1088) it looked set at open, and CAPTURE ENUM_FMT dropped from 4 to 2 formats before userspace had sent an SPS. With only chroma_format_idc = 1 as in 4f4b6a9 the width stays 0, so try_ctrl_sps() still rejects it on the size check.

@6by9

6by9 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Sure, fine by me, please pass it on to @jc-kynesim. If any of it gets folded in, a "Reported-by: Roy rongh7426@gmail.com" would be appreciated.

The issue won't be present in the patchset sent upstream, so there's nothing to submit for a Reported-by. That's what I mean by not having to worry about SoB and other tags on the patchset.

#7708 will get merged to the rpi-6.18.y branch, and can have @jc-kynesim's preferred version of your patches with Reported-by:, but it'll all just get squashed into one patch adding the hopefully bug-free driver for upstream.

@jc-kynesim

Copy link
Copy Markdown
Contributor

Yeah, dropping the SPS checks to dbg is probably worthwhile, but it is definitely worth being having a mechanism that allows you to tell what was rejected!. The whole SPS thing I'm less convinced by - yes v4l2 ctrl get/set should maybe work but in the case of SPS/PPS etc these need to be set by the user, the driver making stuff up has no value whatsoever. If I have to change this to get it accepted upstream then so be it but it is a V4L2 thing that is, for this usage, simply wrong.

jc-kynesim added a commit to jc-kynesim/linux that referenced this pull request Nov 25, 2026
…ration

It is possible to flood the log with warning messages by setting bad
values in SPS, PPS etc. It is valuable to have this info when
debugging so reduce the level to debug.

Reported-by: Roy <rongh7426@gmail.com>
Closes: raspberrypi#7796
Signed-off-by: John Cox <jc@kynesim.co.uk>
@pelwell pelwell closed this in 16ae264 Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants