MediaClient: keyframe gating (startup + corruption re-arm), libyuv NV12 path

Restores the previously shelved decoder/player hardening and extends it:

1. Startup keyframe gating (video_decoder): after (re)start the decoder
   joins the stream mid-GOP; frames referencing unseen pictures render as
   green/blocky concealment garbage. Drop everything until the first IDR
   (300-frame safety valve so unmarked streams can't blank forever).

2. NEW - corruption re-arm: on lossy links (Wi-Fi cameras) dropped slices
   surface as "First slice in a frame missing" / "Could not find ref with
   POC n" and FFmpeg marks the output via AVFrame::decode_error_flags.
   Re-arm the keyframe gate and drop the rest of the GOP so the display
   holds the last clean frame instead of showing concealment artifacts;
   corrupt drops count toward the safety valve.

3. libyuv NV12->BGR conversion (video_player): honors NVDEC/D3D11VA padded
   strides (1080->1088 rows) that cv::cvtColorTwoPlane mishandled (green
   cast + striped noise), picks BT.601/709/2020 coefficients from the
   frame's signalled colorspace, and is ~3-5x faster than sws_scale.

Verified live against an RTSP HEVC camera (NVDEC path): steady frame flow,
no regressions on the clean-stream path.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-06 21:49:22 +10:00
co-authored by Claude Fable 5
parent 044fedd344
commit 6344062179
4 changed files with 173 additions and 3 deletions
+7
View File
@@ -428,6 +428,13 @@ protected:
int m_nv12LastWidth = 0, m_nv12LastHeight = 0;
int m_nv12LastColorspace = -1;
int m_nv12LastRange = -1;
// Guards m_nv12SwsCtx create/use/free. The NV12→BGR conversion deliberately
// runs WITHOUT _mutex (see avframeToCvMat, commit "Fix RTSP stall by
// releasing _mutex during BGR conversion"), so this dedicated lock protects
// the context against close()/dtor freeing it mid-sws_scale without
// reintroducing contention with onVideoFrame. Lock order: _mutex → this
// (close()/dtor hold _mutex first); avframeNV12ToCvMat takes only this lock.
std::mutex m_nv12SwsMutex;
void initSwsContext(int width, int height, AVPixelFormat pixFmt, AVPixelFormat outputPixFmt = AV_PIX_FMT_YUVJ420P);
void initNV12SwsContext(const AVFrame* frame);