fix(video): fall back to SDR instead of failing session when HDR isn't supported#5429
Open
matix753 wants to merge 1 commit into
Open
fix(video): fall back to SDR instead of failing session when HDR isn't supported#5429matix753 wants to merge 1 commit into
matix753 wants to merge 1 commit into
Conversation
When a client requests HDR (dynamicRange) but the active capture/encode device combination can't deliver it (e.g. NvFBC's CUDA path on Linux, which only ever produces NV12/YUV444P frames regardless of what the encoder itself supports), make_avcodec_encode_session() failed the whole session instead of falling back to SDR like H.264 already does by design. Since the client doesn't learn that its request was rejected, it just reconnects and asks for the same unsupported HDR config again, forever - visible as an infinite Creating encoder [hevc_nvenc] retry loop, even though each individual attempt actually fails cleanly. This introduces a local, possibly-downgraded dynamicRange flag that falls back to SDR (matching colorspace/bit depth) instead of aborting the session when the requested dynamic range isn't supported for the active chroma sampling mode. The startup capability probe is unaffected, since it calls this same function before the capability flag has been determined.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Problem
When a client requests HDR (
dynamicRange) for HEVC or AV1 but the active capture/encodedevice combination can't actually deliver it,
make_avcodec_encode_session()fails thesession outright (
return nullptr) instead of falling back to SDR the way it already doesfor H.264 (
encoder.h264[DYNAMIC_RANGE]is hardcodedfalse, so H.264 never even attemptsHDR and always negotiates cleanly).
This isn't just a hardware-capability edge case. On Linux, NvFBC capture always uses the
cuda::cuda_tencode-device class, which only ever producesAV_PIX_FMT_NV12/AV_PIX_FMT_YUV444Pframes (seesrc/platform/linux/cuda.cpp,cuda_t::set_frame()) — ithas no P010/10-bit path at all, regardless of what the encoder itself supports. The sibling
class
gl_cuda_vram_t(used by the GL/dmabuf capture path) does supportAV_PIX_FMT_P010LE,so this is a structural gap specific to the NvFBC+CUDA path, not a hardware limitation.
Confirmed on an NVIDIA Quadro T1000 (Turing):
ffmpeg -c:v hevc_nvenc -profile:v main10encodes 10-bit HEVC perfectly outside of Sunshine. Inside Sunshine, with
capture = nvfbc,any client HDR-HEVC request fails with:
Observed symptom on a real client (LG webOS Moonlight app,
mariotaku/moonlight-tv):the client doesn't give up after the failed session — it silently reconnects and requests
the exact same HDR-HEVC config again, forever. Sunshine correctly fails every single attempt
(no infinite loop inside Sunshine), but from the outside it looks like a permanent hang:
Creating encoder [hevc_nvenc]→ fail → repeat, indefinitely, once per reconnect.Fix
make_avcodec_encode_session()now downgrades to SDR in place instead of failing thesession when the requested dynamic range isn't supported for the current chroma sampling
mode — mirroring what H.264 already does by design:
dynamicRangebool (starts asconfig.dynamicRange).return nullptr, logs awarning and sets
dynamicRange = false, also correctingencode_device->colorspace(
colorspace_e::rec709,bit_depth = 8) so the rest of the function (sw_fmtselection,ctx->profile, HDR option sets, mastering-display metadata gating viacolorspace_is_hdr()) all agree on SDR for this session.config.dynamicRangein the function now read the localdynamicRangeinstead, so the downgrade is consistent for profile selection and option handling.
This does not change behavior for the encoder capability probe at startup
(
probe_encoders()/test_yuv420_hdr()), since that code path calls this same functionbefore
encoder.hevc[DYNAMIC_RANGE]has been determined — the probe still runs exactly asbefore and correctly records the capability. The fallback only engages for real sessions
after the probe has already determined dynamic range isn't supported, which is exactly the
case that previously looped forever.
Testing
Environment: server = Debian GNU/Linux 13 (trixie), NVIDIA Quadro T1000 (Turing), driver
580.173.02,
capture = nvfbc,encoder = nvenc. Client = LG 75QNED84A6C TV, webOS TV 10.3.1(build 3001, "ponytail-papikonda", kernel 5.4.268), streaming app
com.limelight.webos(community
mariotaku/moonlight-tvclient, not the official LG Moonlight app).hevc_modeforcedto request Main10, confirming the exact log signature above.
HEVC, logs
hevc_nvenc: dynamic range not supported, falling back to SDR, and streamssuccessfully — no reconnect loop, stable 6-camera grid stream, zero perceived delay.
moonlight-tvwebOS client(confirmed via
/proc/<pid>/status, ~1MB/sVmRSSgrowth) that only manifests when theserver keeps failing/re-negotiating HEVC — see
mariotaku/moonlight-tv#479. With
this fix, HDR can stay enabled globally on the client (e.g. for other, HDR-capable hosts)
without breaking hosts that can't deliver HDR through NvFBC.
Scope
This fixes the hang/loop for any hardware+capture combination where dynamic range isn't
supported, not just NvFBC-on-Turing. The
gl_cuda_vram_tP010 gap on the NvFBC path couldalso be closed directly (giving true 10-bit HEVC on Turing+NvFBC), but that's a separate,
larger change to the CUDA conversion path — this PR only fixes the missing graceful fallback,
matching the existing H.264 behavior.