feat(498): QSV profiles can decode via VA-API (Prefer native decoder) #506

Merged
timothy merged 14 commits from feat/498-qsv-native-decode into main 2026-07-20 22:16:37 +02:00
Owner

fixes #498

What

Adds a per-FFmpeg-profile boolean QsvPreferNativeDecoder (default ON) that mirrors Jellyfin's "Prefer OS native decoders" checkbox: on the QSV acceleration path, decode with the error-tolerant VA-API decoder while still encoding with QSV, instead of the strict QSV decoder that fails on imperfect H.264 (the reported ~50% channel cold-start failures on the Intel i7-10700K).

How

  • QsvHardwareAccelerationOption emits -hwaccel vaapi (no -hwaccel_output_format) so frames download to system memory; the existing VA-API→QSV derived-device chain (-init_hw_device vaapi=va:… / qsv=hw@va) is retained for the encoder.
  • New DecoderVaapiToSoftware (software-output implicit VA-API decoder); QsvPipelineBuilder sets the decoder mode to Vaapi on this path so the decoded software frames flow through the existing format=nv12,hwupload,vpp_qsv/deinterlace_qsv branch to the QSV encoder — the same graph the production software-decode+QSV-encode error-loop already runs. This is a PCIe round-trip, not Jellyfin's zero-copy hwmap (out of scope).
  • Native decode is Linux-only (!OperatingSystem.IsWindows() guard — ffmpeg has no vaapi hwaccel on Windows).
  • Full chain: bool? domain column (dual-provider migration, HasDefaultValue(true) → existing QSV profiles adopt it on upgrade; nullable so an explicit false persists), Application records/mapper/handlers, REST DTOs (bool? = null + ?? true for /api/v1 additive-compat), OpenAPI regen, and a QSV-only "Prefer native decoder" checkbox in the SPA.

Verification

  • Full solution build 0 errors; ErsatzTV.Tests 1753/1753 + FFmpeg.Tests 24/24 (incl. new arg-level tests pinning: -hwaccel vaapi, no -hwaccel_output_format, hwupload precedes every vpp_qsv/deinterlace_qsv, h264_qsv encoder; and false-persists-as-false round-trip tests guarding the EF nullable-bool gotcha).
  • SQLite migration applies to a fresh DB (column nullable, default 1). MySQL apply is covered by the CI migrations job.
  • BOM + dotnet format gate clean (normalized two legacy files touched, whitespace-only).
  • Hardware validation on the Intel QSV host is NOT claimed here — the arg-level tests are the automated net; real iHD decode-tolerance / upload-bandwidth verification happens on the i7-10700K post-deploy (the amd64/macOS dev+CI machines have no QSV).

Reviews

Design + plan pre-reviewed (Fable). Every task independently reviewed. Final whole-branch adversarial review (Fable): MERGEABLE. Its findings dispositioned in commit 650d5bf9: Windows guard, IsIntelVaapiOrQsv Nut-parity, a non-vacuous test assertion, doc corrections — all re-reviewed Approved.

Accepted trade-off / follow-up

With native decode ON, HDR content on a QSV profile tonemaps in software (decoder mode Vaapi skips tonemap_qsv). Correct but higher CPU; the escape hatch (flag OFF) keeps GPU tonemap for HDR-heavy users who don't need the tolerant decoder. Optimizing the native path to tonemap_qsv is tracked in #505. Recorded in docs/decisions.md.

🤖 Generated with Claude Code

fixes #498 ## What Adds a per-FFmpeg-profile boolean **`QsvPreferNativeDecoder`** (default **ON**) that mirrors Jellyfin's "Prefer OS native decoders" checkbox: on the QSV acceleration path, decode with the error-tolerant **VA-API** decoder while still encoding with **QSV**, instead of the strict QSV decoder that fails on imperfect H.264 (the reported ~50% channel cold-start failures on the Intel i7-10700K). ## How - `QsvHardwareAccelerationOption` emits `-hwaccel vaapi` (no `-hwaccel_output_format`) so frames download to system memory; the existing VA-API→QSV derived-device chain (`-init_hw_device vaapi=va:… / qsv=hw@va`) is retained for the encoder. - New `DecoderVaapiToSoftware` (software-output implicit VA-API decoder); `QsvPipelineBuilder` sets the decoder mode to `Vaapi` on this path so the decoded software frames flow through the existing `format=nv12,hwupload,vpp_qsv`/`deinterlace_qsv` branch to the QSV encoder — the same graph the production software-decode+QSV-encode error-loop already runs. **This is a PCIe round-trip, not Jellyfin's zero-copy `hwmap` (out of scope).** - Native decode is **Linux-only** (`!OperatingSystem.IsWindows()` guard — ffmpeg has no vaapi hwaccel on Windows). - Full chain: `bool?` domain column (dual-provider migration, `HasDefaultValue(true)` → existing QSV profiles adopt it on upgrade; nullable so an explicit `false` persists), Application records/mapper/handlers, REST DTOs (`bool? = null` + `?? true` for `/api/v1` additive-compat), OpenAPI regen, and a QSV-only "Prefer native decoder" checkbox in the SPA. ## Verification - Full solution build 0 errors; **ErsatzTV.Tests 1753/1753** + **FFmpeg.Tests 24/24** (incl. new arg-level tests pinning: `-hwaccel vaapi`, no `-hwaccel_output_format`, `hwupload` precedes every `vpp_qsv`/`deinterlace_qsv`, `h264_qsv` encoder; and false-persists-as-false round-trip tests guarding the EF nullable-bool gotcha). - SQLite migration applies to a fresh DB (column nullable, default 1). MySQL apply is covered by the CI `migrations` job. - BOM + `dotnet format` gate clean (normalized two legacy files touched, whitespace-only). - **Hardware validation on the Intel QSV host is NOT claimed here** — the arg-level tests are the automated net; real iHD decode-tolerance / upload-bandwidth verification happens on the i7-10700K post-deploy (the amd64/macOS dev+CI machines have no QSV). ## Reviews Design + plan pre-reviewed (Fable). Every task independently reviewed. Final whole-branch adversarial review (Fable): **MERGEABLE**. Its findings dispositioned in commit `650d5bf9`: Windows guard, `IsIntelVaapiOrQsv` Nut-parity, a non-vacuous test assertion, doc corrections — all re-reviewed **Approved**. ## Accepted trade-off / follow-up With native decode ON, HDR content on a QSV profile tonemaps in **software** (decoder mode `Vaapi` skips `tonemap_qsv`). Correct but higher CPU; the escape hatch (flag OFF) keeps GPU tonemap for HDR-heavy users who don't need the tolerant decoder. Optimizing the native path to `tonemap_qsv` is tracked in **#505**. Recorded in `docs/decisions.md`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
timothy added 14 commits 2026-07-20 21:33:28 +02:00
Mirrors Jellyfin's default-on "prefer native decoder" hybrid via a new
QsvPreferNativeDecoder profile boolean. Reverified against code before
designing; records the rejected decode-family-enum alternative (Option C).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- Blocker 1: VA-API decode to SOFTWARE frames (drop -hwaccel_output_format,
  new DecoderVaapiToSoftware) so the proven hwupload/vpp_qsv branch bridges to
  the QSV encoder — the naive hardware-surface path emits a bare vpp_qsv on
  VA-API frames and fails on ~all content.
- Blocker 2: bool? domain property + != false coercion (DeinterlaceVideo
  pattern) so create-with-false actually persists false.
- High 3: REST DTOs bool?=null + ?? true for /api/v1 additive-compat.
- Medium 4: correct Task 4 test scaffolding (DefaultHardwareCapabilities).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Field-reference update skipped: docs/channels.md and docs/domain-model.md
have no per-field FFmpeg-profile catalogue (channels.md's Encoding bullet
is a one-line summary, not a field list; grep for QsvExtraHardwareFrames
or HardwareAcceleration finds no such list in either doc).
Whitespace-only (git diff -w is empty); the #311 format job checks whole
touched files, and these two legacy files carried pre-existing violations
never caught before (no PR had touched them since the gate landed).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
fix(498): address final-review findings (Windows guard, Nut parity, docs, test)
Build ErsatzTV Image / CI image pin matches docker/ci (pull_request) Successful in 6s
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 9s
Build ErsatzTV Image / decisions.md append-only (pull_request) Successful in 28s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 17s
Build ErsatzTV Image / Functional E2E (curl contracts) (pull_request) Successful in 6m12s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 12m23s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 18m30s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 18m37s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
650d5bf972
From the Fable whole-branch review:
- M1: guard native VA-API decode with !OperatingSystem.IsWindows() (no vaapi
  hwaccel on Windows; QSV caps over-reported there) — DRY'd into a preferNativeDecode local.
- L2: IsIntelVaapiOrQsv also matches decoder mode Vaapi, preserving Intel
  audio-dup parity on the (producerless) Nut-output branch.
- L1: replace the vacuous ShouldNotContain(" deinterlace_qsv") with an
  occurrence-count assertion that actually catches a second bare occurrence.
- N1/H1: decisions.md — correct the column to nullable-with-default (not NOT NULL),
  and record the accepted HDR software-tonemap trade-off + Linux-only guard, with
  the tonemap_qsv optimization tracked in #505.

H1 (HDR tonemap reroute) accepted-and-deferred per that decision; #505 filed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Author
Owner

Review-verdict: MERGEABLE @ 650d5bf972

Final whole-branch adversarial review (Fable) returned MERGEABLE; its findings (Windows guard, IsIntelVaapiOrQsv Nut-parity, non-vacuous test assertion, doc corrections) were fixed in commit 650d5bf9 and independently re-reviewed Approved. The one High finding (HDR content tonemaps in software on the native-decode path) was accepted-and-deferred with an escape hatch (flag OFF), documented in docs/decisions.md, and tracked in #505. Local gate green: full build 0 errors, ErsatzTV.Tests 1753/1753 + FFmpeg.Tests 24/24, BOM + format clean, SQLite migration applies to a fresh DB. Hardware validation on the Intel QSV host is deferred to post-deploy (no QSV on the dev/CI machines) — the arg-level tests are the pre-merge net.

Review-verdict: MERGEABLE @ 650d5bf9725a1b1b6c693e74fdb58d6168baa357 Final whole-branch adversarial review (Fable) returned MERGEABLE; its findings (Windows guard, `IsIntelVaapiOrQsv` Nut-parity, non-vacuous test assertion, doc corrections) were fixed in commit `650d5bf9` and independently re-reviewed **Approved**. The one High finding (HDR content tonemaps in software on the native-decode path) was accepted-and-deferred with an escape hatch (flag OFF), documented in `docs/decisions.md`, and tracked in #505. Local gate green: full build 0 errors, ErsatzTV.Tests 1753/1753 + FFmpeg.Tests 24/24, BOM + format clean, SQLite migration applies to a fresh DB. Hardware validation on the Intel QSV host is deferred to post-deploy (no QSV on the dev/CI machines) — the arg-level tests are the pre-merge net.
timothy force-pushed feat/498-qsv-native-decode from 650d5bf972 to 374528250b 2026-07-20 21:58:12 +02:00 Compare
Author
Owner

Review-verdict: MERGEABLE @ 374528250b

Rebased onto current main (picked up #67). The reviewed code is byte-identical (git rebase preserved every blob) — the only change vs the prior verdict (@ 650d5bf9) is the append-only docs/decisions.md merge (both the #67 and #498 entries kept, in order) and the new base. Re-verified locally on top of #67: full build 0 errors, ErsatzTV.Tests 1760/1760 (incl. #67's new tests) + FFmpeg.Tests 24/24, OpenAPI regenerated + check:api clean (both imageSource and qsvPreferNativeDecoder present), BOM clean. Fable whole-branch verdict MERGEABLE + fix-commit re-review Approved still hold for this identical code.

Review-verdict: MERGEABLE @ 374528250b4b6e6336520a9d550b92fd86c4e083 Rebased onto current `main` (picked up #67). The reviewed code is byte-identical (git rebase preserved every blob) — the only change vs the prior verdict (`@ 650d5bf9`) is the append-only `docs/decisions.md` merge (both the #67 and #498 entries kept, in order) and the new base. Re-verified locally on top of #67: full build 0 errors, ErsatzTV.Tests 1760/1760 (incl. #67's new tests) + FFmpeg.Tests 24/24, OpenAPI regenerated + `check:api` clean (both `imageSource` and `qsvPreferNativeDecoder` present), BOM clean. Fable whole-branch verdict MERGEABLE + fix-commit re-review Approved still hold for this identical code.
timothy scheduled this pull request to auto merge when all checks succeed 2026-07-20 21:59:37 +02:00
timothy merged commit 36499a96e0 into main 2026-07-20 22:16:37 +02:00
timothy deleted branch feat/498-qsv-native-decode 2026-07-20 22:16:38 +02:00
Sign in to join this conversation.