diff --git a/MediaClient/media/video_decoder.cpp b/MediaClient/media/video_decoder.cpp index 394fed0..ec81f8e 100644 --- a/MediaClient/media/video_decoder.cpp +++ b/MediaClient/media/video_decoder.cpp @@ -9,10 +9,62 @@ #include "ANSLicense.h" // ANS_DBG macro (gated by ANSCORE_DEBUGVIEW) +#include +#include +#include + extern "C" { #include "libavutil/imgutils.h" #include "libavutil/buffer.h" #include "libavutil/mem.h" +#include "libavutil/log.h" +} + +// --------------------------------------------------------------------------- +// FFmpeg error-log bridge — corruption detection for hwaccel decode. +// +// On slice loss the HEVC decoder logs "First slice in a frame missing" / +// "Could not find ref with POC n" at AV_LOG_ERROR, but with hwaccel (NVDEC) +// it does NOT set AVFrame::decode_error_flags — verified empirically — so +// frame-level inspection cannot see the corruption. This bridge installs a +// process-wide av_log callback that maps the logging AVCodecContext back to +// its CVideoDecoder and latches a per-decoder corruption flag, which +// readFrame() consumes to re-arm the keyframe gate. +// +// Notes: with hwaccel the decoder runs single-threaded, so the logging ctx +// is exactly our AVCodecContext (SW frame-threading logs from per-thread +// clones and would not match — those paths set decode_error_flags instead). +// The callback chains to av_log_default_callback so console output remains. +// --------------------------------------------------------------------------- +namespace { + std::mutex s_logBridgeMutex; + std::unordered_map*> s_logBridgeMap; // AVCodecContext* → corrupt flag + + void ansAvLogBridge(void* ptr, int level, const char* fmt, va_list vl) + { + if (ptr && level <= AV_LOG_ERROR) { + std::lock_guard lk(s_logBridgeMutex); + auto it = s_logBridgeMap.find(ptr); + if (it != s_logBridgeMap.end()) { + it->second->store(true, std::memory_order_relaxed); + } + } + av_log_default_callback(ptr, level, fmt, vl); + } + + void registerLogBridge(void* codecCtx, std::atomic* corruptFlag) + { + static std::once_flag s_installOnce; + std::call_once(s_installOnce, []() { av_log_set_callback(ansAvLogBridge); }); + std::lock_guard lk(s_logBridgeMutex); + s_logBridgeMap[codecCtx] = corruptFlag; + } + + void unregisterLogBridge(void* codecCtx) + { + std::lock_guard lk(s_logBridgeMutex); + s_logBridgeMap.erase(codecCtx); + } } // --------------------------------------------------------------------------- @@ -428,6 +480,10 @@ void CVideoDecoder::uninit() m_pContext->hw_device_ctx = NULL; } + // Stop the av_log bridge from touching this decoder's flag BEFORE the + // context is destroyed (the map key is the context pointer). + unregisterLogBridge(m_pContext); + // FIXED: Close codec before freeing context avcodec_close(m_pContext); avcodec_free_context(&m_pContext); @@ -602,6 +658,13 @@ BOOL CVideoDecoder::init(enum AVCodecID codec, uint8* extradata, int extradata_s return FALSE; } + // Route this context's FFmpeg error logs to our corruption flag so the + // keyframe gate can react to slice loss on the hwaccel path (which never + // sets decode_error_flags). Unregistered in uninit() before the context + // is freed. + m_bCorruptSeen.store(false, std::memory_order_relaxed); + registerLogBridge(m_pContext, &m_bCorruptSeen); + // Debug: one-shot visibility into which decoder actually got opened. // m_bHardwareDecoderEnabled is set by hwDecoderInit() on success; when // hwMode == HW_DECODING_DISABLE or hwDecoderInit failed, it stays FALSE @@ -1101,16 +1164,27 @@ BOOL CVideoDecoder::readFrame() // // Packet loss on lossy links (Wi-Fi cameras) drops slices mid-GOP: // FFmpeg logs "First slice in a frame missing" / "Could not find ref - // with POC n", conceals the damage, and marks the output frame via - // AVFrame::decode_error_flags (missing refs, concealment active, ...). - // Every subsequent frame in the GOP references the concealed one, so - // rather than display green/blocky garbage until the next IDR, drop - // the frame and re-arm the keyframe gate — the display holds the last - // clean frame and resumes on the next clean keyframe. - if (m_pFrame->decode_error_flags != 0) { + // with POC n" and conceals the damage. Every subsequent frame in the + // GOP references the concealed one, so rather than display green/ + // blocky garbage until the next IDR, drop the frame and re-arm the + // keyframe gate — the display holds the last clean frame and resumes + // on the next clean keyframe. + // + // Two detection signals, both required: + // • decode_error_flags — set by SW decode paths (H.264 always, + // HEVC frame-threaded). + // • m_bCorruptSeen — latched by the av_log bridge; the ONLY signal + // on the hwaccel (NVDEC) path, which logs the error but leaves + // decode_error_flags at 0 (verified empirically). + const bool frameCorrupt = (m_pFrame->decode_error_flags != 0) + || m_bCorruptSeen.exchange(false, std::memory_order_relaxed); + if (frameCorrupt) { if (!m_bWaitKeyframe.load(std::memory_order_relaxed)) { m_bWaitKeyframe.store(true, std::memory_order_relaxed); m_nKeyframeWaitCount = 0; + log_print(HT_LOG_WARN, + "%s, corrupt frame (decode_error_flags=0x%x) -- gating until next keyframe\r\n", + __FUNCTION__, m_pFrame->decode_error_flags); ANS_DBG("MEDIA_Decode", "corrupt frame (decode_error_flags=0x%x) — re-arming keyframe gate", m_pFrame->decode_error_flags); diff --git a/MediaClient/media/video_decoder.h b/MediaClient/media/video_decoder.h index 24d3c14..8495c6f 100644 --- a/MediaClient/media/video_decoder.h +++ b/MediaClient/media/video_decoder.h @@ -173,6 +173,11 @@ private: // keyframe can't blank the display forever. std::atomic m_bWaitKeyframe{ true }; // true until first IDR after (re)start int m_nKeyframeWaitCount{ 0 }; // frames dropped while waiting + // Latched by the av_log bridge (video_decoder.cpp) when FFmpeg logs a decode + // error for this decoder's AVCodecContext. Needed because hwaccel (NVDEC) + // decode does NOT set AVFrame::decode_error_flags on slice loss — the log + // is the only corruption signal on that path. Consumed by readFrame(). + std::atomic m_bCorruptSeen{ false }; int m_hwGpuIndex; // GPU index assigned by HWDecoderPool (-1 = legacy) AVFrame* m_pCudaHWFrame; // Cloned CUDA HW frame (device ptrs) for inference const AVCodec* m_pCodec;