Skip to content

media: apple: avd: allow pre-streaming H.264/HEVC format-negotiation SPS - #3073

Merged
RyanTheTide merged 2 commits into
mainfrom
fix/avd-v4l2-stateless-init-ctrls
Nov 24, 2026
Merged

RyanTheTide merged 2 commits into
mainfrom
fix/avd-v4l2-stateless-init-ctrls

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What: Allow minimal pre-streaming H.264 and HEVC format-negotiation SPS controls (!flags && !pic_width_in_mbs_minus1 && !pic_height_in_map_units_minus1 for H.264; zeroed CTB size fields for HEVC) in apple-avd try_ctrl while keeping full SPS validation on populated interlaced SPS structs, streaming try_ctrl, and every run(), fixing Chromium V4L2VideoDecoder::SetExtCtrlsInit() -EINVAL failures before CAPTURE format negotiation.
Tested: J316s (M1 Pro, 7.3.0-aurora-fleet87) and J616s (M4 Pro, 7.3.0-aurora-fleet87) at exact 7c49bb1eb91d: GStreamer v4l2slh264dec, v4l2slh265dec, and v4l2slvp9dec hardware decode PASS on both Macs (0 SELinux denials, 0 failed units, 0 new kernel errors); Chromium SetExtCtrlsInit() improved from -EINVAL (22) (stock apple_avd) to SetupOutputFormat(): candidate: NV12 for both H.264 and HEVC on both Macs with VP9 40/40 hardware frames PASS; tools/asahi/avd-codec-tail-tests.py PASS; KUnit apple-avd-h* 76/76 PASS; scripts/checkpatch.pl 0 errors / 0 warnings.
Risk: two-way door · all Macs with Apple AVD (M1–M4)
Not yet: Chromium H.264 V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED (-ERANGE in SubmitDecode()) and HEVC bitstream start-code handling are separate follow-up work; awaiting exact-head CI completion (host-tests, pr-text, readiness).

Details

Stateless V4L2 clients following the V4L2 stateless decoder initialization sequence (including Chromium's V4L2VideoDecoder::SetExtCtrlsInit()) issue VIDIOC_S_EXT_CTRLS before STREAMON with minimal H.264 and HEVC SPS structs carrying chroma_format_idc, bit depth, and (for HEVC) picture dimensions so the driver can narrow CAPTURE pixel formats (NV12 vs P010).

  • Split picture format validation (dimensions, chroma format, bit depth) into avd_h264_validate_sps_fmt() and avd_hevc_validate_sps_fmt().
  • Add avd_h264_validate_sps_ctrl(ctx, sps, streaming) which defers V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY only for non-streaming minimal init SPS structs (!sps->flags && !sps->pic_width_in_mbs_minus1 && !sps->pic_height_in_map_units_minus1). Populated interlaced SPS structs (e.g. from FFmpeg v4l2request or GStreamer v4l2slh264dec) continue to return -EINVAL upfront during pre-streaming try_ctrl and STREAMON, preserving software fallback.
  • avd_hevc_try_ctrl() mirrors the same narrow gate for zeroed CTB size fields (!log2_min_luma_coding_block_size_minus3 && !log2_diff_max_min_luma_coding_block_size) while non-streaming.
  • Streaming try_ctrl() and per-frame avd_h264_run() / avd_hevc_run() continue to enforce full avd_h264_validate_sps() and avd_hevc_validate_sps().
  • Add KUnit coverage in avd-h264-test.c (avd_sps_fmt_init_test, covering pre-streaming vs streaming and populated interlaced SPS rejection) and avd-hevc-test.c (avd_hevc_sps_fmt_init_test), and extract avd_h264_validate_sps_fmt / avd_hevc_validate_sps_fmt in drivers/media/platform/apple/avd/tests/program-test.py.
Evidence
  • Exact head: 7c49bb1eb91d35 (7c49bb1eb91d)
  • scripts/checkpatch.pl: 0 errors, 0 warnings
  • python3 tools/asahi/avd-codec-tail-tests.py: PASS (program-test.py + dispatch-test.py + vp9-test.py + av1-test.py + recovery-test.py)
  • KUnit (./tools/testing/kunit/kunit.py run --kunitconfig drivers/media/platform/apple/avd/tests/.kunitconfig 'apple-avd-h*'): Testing complete. Ran 76 tests: passed: 76
  • J316s (M1 Pro, 7.3.0-aurora-fleet87, apple-avd.ko built from 7c49bb1eb91d):
    • GStreamer hardware decode: v4l2slh264dec PASS, v4l2slh265dec PASS, v4l2slvp9dec PASS (Enforcing, FAILED_UNITS=0, NEW_AVCS=0, 0 AVD kernel errors)
    • Chromium V4L2 before (stock apple_avd): H.264 & HEVC SetExtCtrlsInit() -> VIDIOC_S_EXT_CTRLS: Invalid argument (22)
    • Chromium V4L2 after (7c49bb1eb91d): H.264 & HEVC SetExtCtrlsInit() -> SetupOutputFormat(): Output (CAPTURE queue) candidate: NV12; VP9 NV12, P010 (40/40 frames)
  • J616s (M4 Pro, 7.3.0-aurora-fleet87, apple-avd.ko built from 7c49bb1eb91d):
    • GStreamer hardware decode: v4l2slh264dec PASS, v4l2slh265dec PASS, v4l2slvp9dec PASS (Enforcing, FAILED_UNITS=0, NEW_AVCS=0, 0 AVD kernel errors)
    • Chromium V4L2 after (7c49bb1eb91d): H.264 & HEVC SetExtCtrlsInit() -> SetupOutputFormat(): Output (CAPTURE queue) candidate: NV12; VP9 NV12, P010 (40/40 frames)

Stateless V4L2 decoders such as Chromium's V4L2VideoDecoder::SetExtCtrlsInit()
issue VIDIOC_S_EXT_CTRLS before STREAMON with minimal H.264 and HEVC SPS
controls carrying only chroma_format_idc, bit depth, and (for HEVC) picture
dimensions so the driver can narrow CAPTURE pixel formats (NV12 vs P010).

In that pre-streaming init call, H.264 leaves flags == 0 (without
V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY) and HEVC leaves CTB size fields zeroed
(log2_min_luma_coding_block_size_minus3 == 0,
log2_diff_max_min_luma_coding_block_size == 0). avd_h264_try_ctrl() and
avd_hevc_try_ctrl() previously rejected both with -EINVAL, breaking Chromium
hardware H.264 and HEVC initialization before CAPTURE format negotiation.

Split format validation (dimensions, chroma format, bit depth) into
avd_h264_validate_sps_fmt() and avd_hevc_validate_sps_fmt(). Use the format
check for pre-streaming try_ctrl (and avd_h264_start), while streaming
try_ctrl and per-frame avd_h264_run()/avd_hevc_run() continue to enforce full
SPS validation (rejecting interlaced H.264 and unsupported HEVC CTB sizes).

Add KUnit coverage in avd-h264-test.c and avd-hevc-test.c.

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide
RyanTheTide requested review from a team as code owners November 24, 2026 14:39

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: HOLD at 1b53a0870eab
Blockers:

  1. drivers/media/platform/apple/avd/avd-h264.c:816, drivers/media/platform/apple/avd/avd-h264.c:1279-1290: pre-streaming avd_h264_try_ctrl() and avd_h264_start() unconditionally skip the V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY check, so a real parsed interlaced H.264 SPS (with populated pic_width_in_mbs_minus1/pic_height_in_map_units_minus1 or flags like V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD) is now accepted during pre-streaming negotiation and STREAMON, only failing frame-by-frame at decode time instead of returning -EINVAL upfront so clients such as FFmpeg (v4l2request) and GStreamer (v4l2slh264dec) can fall back to software decode. Fix: mirror avd_hevc_try_ctrl()'s narrow gate by only deferring FRAME_MBS_ONLY when !sps->flags && !sps->pic_width_in_mbs_minus1 && !sps->pic_height_in_map_units_minus1 (keeping full avd_h264_validate_sps() active for populated SPS structs), and cover a populated interlaced SPS in avd-h264-test.c.
  2. §5 evidence: add regression-run numbers at the new head (0 SELinux denials, 0 failed units, 0 new kernel errors) plus an H.264/HEVC decode no-regression check (current J616s row decodes VP9 frames and only exercises SetExtCtrlsInit for H.264/HEVC), add a no-regression hardware row on a second Apple AVD Mac generation (M1/M2/M3) since this code is shared across M1–M4, and wait for exact-head CI (host-tests, pr-text, readiness) to turn green.

Checked: §1–§4 and §6 pass (1b53a0870eab: no model gates, no cmdline/module params, full SPS validation retained in avd_h264_run()/avd_hevc_run(), human author/committer + DCO Signed-off-by only, zero attribution issues, clean naming, MERGEABLE on main); §5 incomplete and CI still running/queued.

Evidence
  • Head: 1b53a0870eaba68b6d2f35081cb39d77798bca66 (OPEN, non-draft, MERGEABLE against main 037f7bca6822).
  • Diff audit:
    • avd-hevc.c: avd_hevc_try_ctrl() narrows pre-streaming relaxation to zeroed CTB fields (!log2_min_luma_coding_block_size_minus3 && !log2_diff_max_min_luma_coding_block_size) while still checking non-zero dimensions <= coded_fmt, chroma format, and bit depth; streaming try_ctrl() and avd_hevc_run() continue to enforce avd_hevc_validate_sps() (including avd_hevc_check_sps_geometry()). Safe against null m2m_ctx and concurrent control updates (controls copied under v4l2_ctrl_lock() before avd_hevc_run() validates).
    • avd-h264.c: avd_h264_validate_sps_fmt() and avd_h264_run() full validation are sound and avd_h264_start_slices() handles zeroed dimensions safely (1 initial slot, grown via avd_h264_realloc_slices()), but avd_h264_try_ctrl() and avd_h264_start() do not gate the FRAME_MBS_ONLY relaxation on a minimal/zeroed init SPS as HEVC does for CTB fields, regressing pre-streaming interlaced H.264 rejection.
    • avd.h / KUnit tests: VISIBLE_IF_KUNIT / EXPORT_SYMBOL_IF_KUNIT declarations are properly guarded by #if IS_ENABLED(CONFIG_KUNIT); non-blocking note: consider adding a KUnit case exercising try_ctrl directly across pre-streaming vs streaming queue states.
  • Commits & PR hygiene: single commit 1b53a0870eab by Ryan Murray <[email protected]> with matching human Signed-off-by; PR body follows What/Tested/Risk/Not yet structure.
  • CI at review time (1b53a0870eab): host-tests IN_PROGRESS, pr-text QUEUED (prior run SUCCESS), readiness PENDING/QUEUED.

Address review feedback and CI host-tests on pre-streaming SPS validation:

- Narrow H.264 pre-streaming FRAME_MBS_ONLY deferral to minimal init SPS
  controls (!flags && !pic_width_in_mbs_minus1 &&
  !pic_height_in_map_units_minus1) via avd_h264_validate_sps_ctrl(), keeping
  populated interlaced SPS structs rejected upfront with -EINVAL in both
  avd_h264_try_ctrl() and avd_h264_start() so FFmpeg/GStreamer fall back to
  software decode cleanly.
- Extend avd_sps_fmt_init_test in avd-h264-test.c to cover pre-streaming vs
  streaming ctrl validation and populated interlaced SPS rejection.
- Extract avd_h264_validate_sps_fmt and avd_hevc_validate_sps_fmt in
  drivers/media/platform/apple/avd/tests/program-test.py so avd-codec-tail-tests
  builds cleanly.

Signed-off-by: Ryan Murray <[email protected]>

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: FINISH at 7c49bb1eb91d
Blockers: none (awaiting exact-head CI completion: host-tests IN_PROGRESS, pr-text / pr-text SUCCESS, readiness / readiness PENDING)
Checked: Interdiff 1b53a0870eab..7c49bb1eb91d resolves all prior code and §5 hardware/regression evidence blockers; §1–§6 pass (no model gates, default cmdline, per-frame validation retained, zero attribution issues, human author/committer + DCO Signed-off-by only, clean naming, MERGEABLE on main); waiting only on exact-head CI (host-tests, readiness / readiness) to finish green before approving.

Evidence
  • Head: 7c49bb1eb91d35bbd916361ffb58692f4a8208d9 (OPEN, non-draft, MERGEABLE against main 037f7bca6822).
  • Interdiff 1b53a0870eab..7c49bb1eb91d:
    • drivers/media/platform/apple/avd/avd-h264.c: avd_h264_sps_is_fmt_init() + avd_h264_validate_sps_ctrl(ctx, sps, streaming) restricts pre-streaming V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY deferral to minimal init SPS structs (!sps->flags && !sps->pic_width_in_mbs_minus1 && !sps->pic_height_in_map_units_minus1) in both avd_h264_try_ctrl() and avd_h264_start(); populated interlaced SPS structs continue to fail upfront with -EINVAL, and streaming try_ctrl() plus avd_h264_run() enforce full avd_h264_validate_sps().
    • drivers/media/platform/apple/avd/avd-h264-test.c: avd_sps_fmt_init_test covers pre-streaming vs streaming avd_h264_validate_sps_ctrl(), populated interlaced SPS rejection, V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD rejection, and unsupported chroma/bit-depth formats.
    • drivers/media/platform/apple/avd/tests/program-test.py: extracts avd_h264_validate_sps_fmt and avd_hevc_validate_sps_fmt for host regression builds.
  • §5 evidence at exact 7c49bb1eb91d: J316s (M1 Pro) and J616s (M4 Pro) on 7.3.0-aurora-fleet87 — GStreamer v4l2slh264dec, v4l2slh265dec, v4l2slvp9dec hardware decode PASS on both Macs (Enforcing, FAILED_UNITS=0, NEW_AVCS=0, 0 new/AVD kernel errors); Chromium SetExtCtrlsInit() fixed from -EINVAL (22) to SetupOutputFormat(): candidate: NV12 for H.264/HEVC with VP9 40/40 frames PASS; KUnit apple-avd-h* 76/76 PASS; tools/asahi/avd-codec-tail-tests.py PASS; scripts/checkpatch.pl 0 errors / 0 warnings.
  • Commits & PR hygiene: both commits (1b53a0870eab, 7c49bb1eb91d) by Ryan Murray <[email protected]> with matching human DCO Signed-off-by only; zero AI attribution across commits, PR title, body, and comments.
  • Exact-head CI (7c49bb1eb91d): host-tests IN_PROGRESS, pr-text / pr-text SUCCESS, readiness / readiness PENDING.

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 7c49bb1eb91d once the exact-head host-tests run is green

Blockers: none

Checked: the narrowed gate holds, and I found nothing past Dj's FINISH above.

  • avd_h264_validate_sps_ctrl() skips the FRAME_MBS_ONLY check only for a pre-streaming SPS with flags, pic_width_in_mbs_minus1 and pic_height_in_map_units_minus1 all zero. A populated interlaced SPS is still refused at try_ctrl and at STREAMON.
  • Every frame still goes through the full avd_h264_validate_sps() / avd_hevc_validate_sps() in run().
  • The zero-dimension init SPS cannot under-size anything at STREAMON. In avd_h264_start(), avd_h264_alloc_bufs() and the scratch rows are sized from ctx->image_fmt (the negotiated format), not from the SPS. avd_h264_start_slices() starts at 1 slot and grows.
  • HEVC: avd_hevc_validate_sps_fmt() keeps a zero-dimension check, the coded-format bound, and the chroma and depth checks. Only the CTB geometry check is deferred, and only while the OUTPUT queue is not streaming.
  • v4l2_m2m_get_vq() is guarded against a NULL m2m_ctx. The unlocked vb2_is_streaming() read in try_ctrl is harmless, because run() validates again.
  • No gates, no parameters.
Evidence
  • Diff at 7c49bb1eb91d (6 files, +214/-11).
  • Read avd_h264_start() and the avd_hevc_try_ctrl() / avd_hevc_run() validate call sites at head.
  • CI when posted: pr-text pass, host-tests pending.

@github-actions github-actions Bot added area:media Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) changes-requested A reviewer requested changes (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Merge readiness at 7c49bb1e: not ready yet

Check Detail
CI at head no failing: pr-text / pr-text
Review ok approved by DjDeveloperr
Requested changes ok none
Hardware row at head ok in the PR body at 7c49bb1e
Conflicts ok none
PR text ok Risk: two-way · all Macs with Apple AVD (M1–M4)

Areas: area:media

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 7c49bb1e

Blockers: none

Checked: All checks at 7c49bb1eb91d are now green (pr-text, host-tests, readiness). Code, KUnit/program-test coverage, §1–§6 policy hygiene, and exact-head J316s + J616s hardware decode/regression rows were verified in the prior pass on this same head.

Evidence
  • Exact head: 7c49bb1eb91d35bbd916361ffb58692f4a8208d9 (MERGEABLE onto main).
  • CI at head: pr-text PASS (15:00:53Z), host-tests PASS (15:08:07Z), readiness PASS (15:10:33Z); earlier cancelled runs were superseded duplicates from PR body edits.
  • Prior review: All code and §5 evidence items were cleared in review #6041756286; promoting from FINISH to MERGE now that CI is green.

@github-actions github-actions Bot removed changes-requested A reviewer requested changes (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 24, 2026
@RyanTheTide
RyanTheTide merged commit a9cd131 into main Nov 24, 2026
4 of 8 checks passed
@RyanTheTide
RyanTheTide deleted the fix/avd-v4l2-stateless-init-ctrls branch November 27, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:media Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants