video_decoder: av_log bridge — corruption detection for hwaccel decode

The corruption re-arm added in 6344062 keyed off AVFrame::decode_error_flags,
but the HEVC decoder on the hwaccel (NVDEC) path never sets it: 120 s of live
Wi-Fi packet loss produced 10+ "First slice in a frame missing" / "Could not
find ref with POC n" log bursts with decode_error_flags == 0 on every frame,
so the keyframe gate never fired.

Bridge: install a process-wide av_log callback (chains to
av_log_default_callback) that maps the logging AVCodecContext back to its
CVideoDecoder and latches a per-decoder m_bCorruptSeen flag on AV_LOG_ERROR
messages. readFrame() consumes the latch alongside decode_error_flags to
re-arm the keyframe gate. With hwaccel the decoder is single-threaded, so
the logging context is exactly our AVCodecContext; SW frame-threaded paths
log from per-thread clones but set decode_error_flags instead, which the
existing check covers.

Registered after avcodec_open2, unregistered in uninit() before the context
is freed. Map lookup only on error-level messages (rare). Also promote the
gate-arm message to log_print(HT_LOG_WARN) for console visibility.

Clean-path verified live: 6105 frames / 5 min NVDEC RTSP with zero errors
and no regressions. Loss-path verification pending a naturally lossy window.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-07 05:01:22 +10:00
co-authored by Claude Fable 5
parent 6344062179
commit 273dd44a6d
2 changed files with 86 additions and 7 deletions
+81 -7
View File
@@ -9,10 +9,62 @@
#include "ANSLicense.h" // ANS_DBG macro (gated by ANSCORE_DEBUGVIEW) #include "ANSLicense.h" // ANS_DBG macro (gated by ANSCORE_DEBUGVIEW)
#include <cstdarg>
#include <mutex>
#include <unordered_map>
extern "C" { extern "C" {
#include "libavutil/imgutils.h" #include "libavutil/imgutils.h"
#include "libavutil/buffer.h" #include "libavutil/buffer.h"
#include "libavutil/mem.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<void*, std::atomic<bool>*> 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<std::mutex> 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<bool>* corruptFlag)
{
static std::once_flag s_installOnce;
std::call_once(s_installOnce, []() { av_log_set_callback(ansAvLogBridge); });
std::lock_guard<std::mutex> lk(s_logBridgeMutex);
s_logBridgeMap[codecCtx] = corruptFlag;
}
void unregisterLogBridge(void* codecCtx)
{
std::lock_guard<std::mutex> lk(s_logBridgeMutex);
s_logBridgeMap.erase(codecCtx);
}
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -428,6 +480,10 @@ void CVideoDecoder::uninit()
m_pContext->hw_device_ctx = NULL; 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 // FIXED: Close codec before freeing context
avcodec_close(m_pContext); avcodec_close(m_pContext);
avcodec_free_context(&m_pContext); avcodec_free_context(&m_pContext);
@@ -602,6 +658,13 @@ BOOL CVideoDecoder::init(enum AVCodecID codec, uint8* extradata, int extradata_s
return FALSE; 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. // Debug: one-shot visibility into which decoder actually got opened.
// m_bHardwareDecoderEnabled is set by hwDecoderInit() on success; when // m_bHardwareDecoderEnabled is set by hwDecoderInit() on success; when
// hwMode == HW_DECODING_DISABLE or hwDecoderInit failed, it stays FALSE // 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: // Packet loss on lossy links (Wi-Fi cameras) drops slices mid-GOP:
// FFmpeg logs "First slice in a frame missing" / "Could not find ref // FFmpeg logs "First slice in a frame missing" / "Could not find ref
// with POC n", conceals the damage, and marks the output frame via // with POC n" and conceals the damage. Every subsequent frame in the
// AVFrame::decode_error_flags (missing refs, concealment active, ...). // GOP references the concealed one, so rather than display green/
// Every subsequent frame in the GOP references the concealed one, so // blocky garbage until the next IDR, drop the frame and re-arm the
// rather than display green/blocky garbage until the next IDR, drop // keyframe gate — the display holds the last clean frame and resumes
// the frame and re-arm the keyframe gate — the display holds the last // on the next clean keyframe.
// clean frame and resumes on the next clean keyframe. //
if (m_pFrame->decode_error_flags != 0) { // 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)) { if (!m_bWaitKeyframe.load(std::memory_order_relaxed)) {
m_bWaitKeyframe.store(true, std::memory_order_relaxed); m_bWaitKeyframe.store(true, std::memory_order_relaxed);
m_nKeyframeWaitCount = 0; 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", ANS_DBG("MEDIA_Decode",
"corrupt frame (decode_error_flags=0x%x) — re-arming keyframe gate", "corrupt frame (decode_error_flags=0x%x) — re-arming keyframe gate",
m_pFrame->decode_error_flags); m_pFrame->decode_error_flags);
+5
View File
@@ -173,6 +173,11 @@ private:
// keyframe can't blank the display forever. // keyframe can't blank the display forever.
std::atomic<bool> m_bWaitKeyframe{ true }; // true until first IDR after (re)start std::atomic<bool> m_bWaitKeyframe{ true }; // true until first IDR after (re)start
int m_nKeyframeWaitCount{ 0 }; // frames dropped while waiting 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<bool> m_bCorruptSeen{ false };
int m_hwGpuIndex; // GPU index assigned by HWDecoderPool (-1 = legacy) int m_hwGpuIndex; // GPU index assigned by HWDecoderPool (-1 = legacy)
AVFrame* m_pCudaHWFrame; // Cloned CUDA HW frame (device ptrs) for inference AVFrame* m_pCudaHWFrame; // Cloned CUDA HW frame (device ptrs) for inference
const AVCodec* m_pCodec; const AVCodec* m_pCodec;