Fix debug

This commit is contained in:
2026-05-07 23:35:18 +10:00
parent 75693cadd9
commit aa323a07a3
4 changed files with 220 additions and 186 deletions
+40 -15
View File
@@ -943,18 +943,20 @@ BOOL CVideoDecoder::decode(AVPacket* pkt)
if (!m_bInited)
{
log_print(HT_LOG_ERR, "%s, Decoder not initialized\r\n", __FUNCTION__);
// Hot-path pre-check; per-packet during early frames if decode() races
// with init.
ANS_DBG("MEDIA_Decode", "decode(pkt) called but decoder not initialized");
return FALSE;
}
if (!m_bRunning)
{
log_print(HT_LOG_WARN, "%s, Decoder not running\r\n", __FUNCTION__);
ANS_DBG("MEDIA_Decode", "decode(pkt) called but decoder not running");
return FALSE;
}
if (!m_pContext) {
log_print(HT_LOG_ERR, "%s, Context is NULL\r\n", __FUNCTION__);
ANS_DBG("MEDIA_Decode", "decode(pkt) called but m_pContext is NULL");
return FALSE;
}
@@ -968,7 +970,12 @@ BOOL CVideoDecoder::decode(AVPacket* pkt)
{
if (!readFrame())
{
log_print(HT_LOG_ERR, "%s, Failed to read frame during retry %d\r\n", __FUNCTION__, retryCount);
// Hot-path error: was log_print() which fflushes per call and can
// spike per-packet during stream stress. ANS_DBG is silent in
// production; the per-frame error count is also visible via
// MEDIA_Pulse drop(err=) so observability is preserved.
ANS_DBG("MEDIA_Decode",
"send_packet retry %d: readFrame failed", retryCount);
return FALSE;
}
Sleep(1); // Reduced sleep time
@@ -980,7 +987,8 @@ BOOL CVideoDecoder::decode(AVPacket* pkt)
{
char error_buf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(ret, error_buf, sizeof(error_buf));
log_print(HT_LOG_ERR, "%s, avcodec_send_packet failed: %s (ret=%d)\r\n", __FUNCTION__, error_buf, ret);
ANS_DBG("MEDIA_Decode",
"avcodec_send_packet failed: %s (ret=%d)", error_buf, ret);
return FALSE;
}
@@ -993,25 +1001,29 @@ BOOL CVideoDecoder::decode(uint8* data, int len, int64_t pts)
if (!m_bInited)
{
log_print(HT_LOG_ERR, "%s, Decoder not initialized\r\n", __FUNCTION__);
// Hot-path: per-packet if decoder isn't initialised. Was log_print
// which fflushes per call.
ANS_DBG("MEDIA_Decode", "decode called but decoder not initialized");
return FALSE;
}
if (!m_bRunning)
{
log_print(HT_LOG_WARN, "%s, Decoder not running\r\n", __FUNCTION__);
// Per-packet during start/stop transitions.
ANS_DBG("MEDIA_Decode", "decode called but decoder not running");
return FALSE;
}
if (!data || len <= 0) {
log_print(HT_LOG_ERR, "%s, Invalid input data\r\n", __FUNCTION__);
ANS_DBG("MEDIA_Decode", "decode called with invalid input (data=%p len=%d)",
(void*)data, len);
return FALSE;
}
// Allocate packet
AVPacket* packet = av_packet_alloc();
if (!packet) {
log_print(HT_LOG_ERR, "%s, Failed to allocate AVPacket\r\n", __FUNCTION__);
ANS_DBG("MEDIA_Decode", "av_packet_alloc failed (OOM?)");
return FALSE;
}
@@ -1020,7 +1032,8 @@ BOOL CVideoDecoder::decode(uint8* data, int len, int64_t pts)
if (ret < 0) {
char error_buf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(ret, error_buf, sizeof(error_buf));
log_print(HT_LOG_ERR, "%s, Failed to allocate packet data: %s\r\n", __FUNCTION__, error_buf);
ANS_DBG("MEDIA_Decode",
"av_new_packet failed (len=%d): %s", len, error_buf);
av_packet_free(&packet);
return FALSE;
}
@@ -1061,13 +1074,21 @@ BOOL CVideoDecoder::readFrame()
else if (ret < 0) {
char error_buf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(ret, error_buf, sizeof(error_buf));
log_print(HT_LOG_ERR, "%s, avcodec_receive_frame failed: %s (ret=%d)\r\n", __FUNCTION__, error_buf, ret);
// Hot-path error during stream stress (UDP loss, corrupt I-frame).
// Was log_print(HT_LOG_ERR, ...) which fflushes per call and could
// fire ~25/sec on a degraded stream — observed as sudden GetImage
// slowness. ANS_DBG is silent in production; pulse counter
// MEDIA_Pulse drop(err=) preserves observability.
ANS_DBG("MEDIA_Decode",
"avcodec_receive_frame failed: %s (ret=%d)", error_buf, ret);
return FALSE;
}
// Check if we got a valid frame
if (!m_pFrame || m_pFrame->width <= 0 || m_pFrame->height <= 0) {
log_print(HT_LOG_WARN, "%s, Received invalid frame\r\n", __FUNCTION__);
ANS_DBG("MEDIA_Decode", "received invalid frame (w=%d h=%d)",
m_pFrame ? m_pFrame->width : -1,
m_pFrame ? m_pFrame->height : -1);
av_frame_unref(m_pFrame);
continue;
}
@@ -1090,8 +1111,10 @@ BOOL CVideoDecoder::readFrame()
{
char error_buf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(ret, error_buf, sizeof(error_buf));
log_print(HT_LOG_ERR, "%s, Error transferring hardware frame to system memory: %s (ret=%d)\r\n",
__FUNCTION__, error_buf, ret);
// Hot-path: HW→SW transfer failure. Per-frame on degraded HW
// decoder. Gated to avoid fflush spike during stress.
ANS_DBG("MEDIA_Decode",
"hwframe_transfer_data failed: %s (ret=%d)", error_buf, ret);
av_frame_unref(m_pFrame);
if (m_pCudaHWFrame) av_frame_free(&m_pCudaHWFrame);
continue;
@@ -1139,7 +1162,9 @@ int CVideoDecoder::render(AVFrame* frame)
m_pCallback(frame, m_pUserdata);
}
catch (...) {
log_print(HT_LOG_ERR, "%s, Exception in callback function\r\n", __FUNCTION__);
// Hot-path: per-frame if onVideoFrame ever throws. Was log_print
// which fflushes per call.
ANS_DBG("MEDIA_Decode", "exception in onVideoFrame callback");
return 0;
}
}
+84 -84
View File
@@ -219,14 +219,14 @@ AVFrame* CVideoPlayer::cropFrame(const AVFrame* srcFrame, cv::Rect bBox, bool cr
// Validate the bounding box dimensions
if (bBox.width <= 10 || bBox.height <= 10) {
std::cerr << "Invalid bounding box dimensions for cropping." << std::endl;
ANS_DBG("MEDIA_Crop", "invalid bounding box dimensions");
return nullptr;
}
// Allocate memory for the cropped frame
AVFrame* croppedFrame = av_frame_alloc();
if (!croppedFrame) {
std::cerr << "Failed to allocate memory for the cropped frame." << std::endl;
ANS_DBG("MEDIA_Crop", "failed to allocate cropped frame");
return nullptr;
}
@@ -247,7 +247,7 @@ AVFrame* CVideoPlayer::cropFrame(const AVFrame* srcFrame, cv::Rect bBox, bool cr
return croppedFrame;
}
catch (const std::exception& e) {
std::cerr << "Exception in CVideoPlayer::cropFrame: " << e.what() << std::endl;
ANS_DBG("MEDIA_Crop", "cropFrame exception: %s", e.what());
return nullptr;
}
}
@@ -289,13 +289,14 @@ bool CVideoPlayer::cropFrameData(const AVFrame* srcFrame, AVFrame* croppedFrame,
break;
default:
std::cerr << "Unsupported pixel format: " << av_get_pix_fmt_name((AVPixelFormat)srcFrame->format) << std::endl;
ANS_DBG("MEDIA_Crop", "unsupported pixel format: %s",
av_get_pix_fmt_name((AVPixelFormat)srcFrame->format));
return false;
}
return true;
}
catch (const std::exception& e) {
std::cerr << "Exception in cropFrameData: " << e.what() << std::endl;
ANS_DBG("MEDIA_Crop", "cropFrameData exception: %s", e.what());
return false;
}
}
@@ -310,7 +311,7 @@ void CVideoPlayer::cropPlane(const AVFrame* srcFrame, AVFrame* croppedFrame, int
croppedFrame->linesize[planeIndex] = srcFrame->linesize[planeIndex];
}
catch (const std::exception& e) {
std::cerr << "Exception in cropPlane: " << e.what() << std::endl;
ANS_DBG("MEDIA_Crop", "cropPlane exception: %s", e.what());
}
}
@@ -323,7 +324,7 @@ AVFrame* CVideoPlayer::convertNV12ToYUVJ420P(const AVFrame* nv12Frame) {
try {
if (!nv12Frame || !nv12Frame->data[0] || nv12Frame->width <= 10 || nv12Frame->height <= 10) {
std::cerr << "Invalid or empty NV12 frame data, or invalid dimensions." << std::endl;
ANS_DBG("MEDIA_NV12", "invalid or empty NV12 frame data");
return nullptr;
}
@@ -333,7 +334,7 @@ AVFrame* CVideoPlayer::convertNV12ToYUVJ420P(const AVFrame* nv12Frame) {
// ✅ Allocate new YUVJ420P frame
yuvjFrame = av_frame_alloc();
if (!yuvjFrame) {
std::cerr << "Failed to allocate YUVJ420P frame" << std::endl;
ANS_DBG("MEDIA_NV12", "failed to allocate YUVJ420P frame");
return nullptr;
}
@@ -343,7 +344,7 @@ AVFrame* CVideoPlayer::convertNV12ToYUVJ420P(const AVFrame* nv12Frame) {
// ✅ Allocate buffer for YUVJ420P frame
if (av_frame_get_buffer(yuvjFrame, 32) < 0) {
std::cerr << "Failed to allocate buffer for YUVJ420P" << std::endl;
ANS_DBG("MEDIA_NV12", "failed to allocate YUVJ420P buffer");
av_frame_free(&yuvjFrame);
return nullptr;
}
@@ -376,7 +377,7 @@ AVFrame* CVideoPlayer::convertNV12ToYUVJ420P(const AVFrame* nv12Frame) {
return yuvjFrame;
}
catch (const std::exception& e) {
std::cerr << "Exception in convertNV12ToYUVJ420P: " << e.what() << std::endl;
ANS_DBG("MEDIA_NV12", "convertNV12ToYUVJ420P exception: %s", e.what());
// ✅ Prevent Memory Leak by Freeing the Allocated Frame
if (yuvjFrame) {
@@ -393,17 +394,17 @@ std::string CVideoPlayer::avframeYUVJ420PToJpegStringUsingFFMpeg(const AVFrame*
return "";
}
if (!pFrame || !pFrame->data[0] || pFrame->width <= 10 || pFrame->height <= 10) {
std::cerr << "Invalid or empty frame data, or invalid dimensions." << std::endl;
ANS_DBG("MEDIA_Convert", "invalid or empty frame data / dimensions");
return "";
}
AVCodec* jpegCodec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
if (!jpegCodec) {
std::cerr << "Failed to find MJPEG encoder." << std::endl;
ANS_DBG("MEDIA_Jpeg", "failed to find MJPEG encoder");
return "";
}
AVCodecContext* jpegContext = avcodec_alloc_context3(jpegCodec);
if (!jpegContext) {
std::cerr << "Failed to allocate codec context." << std::endl;
ANS_DBG("MEDIA_Jpeg", "failed to allocate codec context");
return "";
}
int imageSize = std::max(pFrame->width, pFrame->height);
@@ -480,7 +481,7 @@ std::string CVideoPlayer::avframeYUVJ420PToJpegStringUsingFFMpeg(const AVFrame*
return isSuccess ? jpegData : "";
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeToJpegString: " << e.what() << std::endl;
ANS_DBG("MEDIA_Jpeg", "avframeToJpegString exception: %s", e.what());
return ""; // Return empty string on error
}
}
@@ -551,7 +552,7 @@ std::string CVideoPlayer::avframeYUVJ420PToJpegStringUsingTurboJPEG(const AVFram
return std::string(reinterpret_cast<char*>(jpegDataPtr), jpegSize);
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeToJpegString: " << e.what() << std::endl;
ANS_DBG("MEDIA_Jpeg", "avframeToJpegString exception: %s", e.what());
return ""; // Return empty string on error
}
}
@@ -559,21 +560,21 @@ std::string CVideoPlayer::encodeYUVJ420PToJPEG(AVFrame* frame, int quality) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
if (!frame || frame->format != AV_PIX_FMT_YUVJ420P) {
std::cerr << "Invalid frame format (must be YUVJ420P)" << std::endl;
ANS_DBG("MEDIA_Convert", "invalid frame format (must be YUVJ420P)");
return "";
}
// Find MJPEG encoder
AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
if (!codec) {
std::cerr << "JPEG encoder not found" << std::endl;
ANS_DBG("MEDIA_Jpeg", "JPEG encoder not found");
return "";
}
// Allocate codec context
AVCodecContext* codecCtx = avcodec_alloc_context3(codec);
if (!codecCtx) {
std::cerr << "Failed to allocate codec context" << std::endl;
ANS_DBG("MEDIA_Jpeg", "failed to allocate codec context (yuvj420p)");
return "";
}
@@ -595,7 +596,7 @@ std::string CVideoPlayer::encodeYUVJ420PToJPEG(AVFrame* frame, int quality) {
// Open codec
if (avcodec_open2(codecCtx, codec, &opts) < 0) {
std::cerr << "Failed to open JPEG encoder" << std::endl;
ANS_DBG("MEDIA_Jpeg", "failed to open JPEG encoder");
avcodec_free_context(&codecCtx);
return "";
}
@@ -607,14 +608,14 @@ std::string CVideoPlayer::encodeYUVJ420PToJPEG(AVFrame* frame, int quality) {
// Send frame to encoder
if (avcodec_send_frame(codecCtx, frame) < 0) {
std::cerr << "Failed to send frame for encoding" << std::endl;
ANS_DBG("MEDIA_Jpeg", "failed to send frame for encoding");
avcodec_free_context(&codecCtx);
return "";
}
// Receive encoded packet
if (avcodec_receive_packet(codecCtx, &pkt) < 0) {
std::cerr << "Failed to receive encoded packet" << std::endl;
ANS_DBG("MEDIA_Jpeg", "failed to receive encoded packet");
avcodec_free_context(&codecCtx);
return "";
}
@@ -628,7 +629,7 @@ std::string CVideoPlayer::encodeYUVJ420PToJPEG(AVFrame* frame, int quality) {
return jpegString;
}
catch (const std::exception& e) {
std::cerr << "Exception in encodeYUVJ420PToJPEG: " << e.what() << std::endl;
ANS_DBG("MEDIA_Jpeg", "encodeYUVJ420PToJPEG exception: %s", e.what());
return ""; // Return empty string on error
}
}
@@ -649,7 +650,7 @@ std::string CVideoPlayer::avframeYUVJ420PToJpegString(const AVFrame* spFrame) {
}
if (!spFrame || !spFrame->data[0] || spFrame->width <= 10 || spFrame->height <= 10) {
std::cerr << "Invalid or empty frame data, or invalid dimensions." << std::endl;
ANS_DBG("MEDIA_Convert", "invalid or empty frame data / dimensions");
return "";
}
@@ -735,7 +736,7 @@ std::string CVideoPlayer::avframeYUVJ420PToJpegString(const AVFrame* spFrame) {
return isSuccess ? jpegData : "";
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeYUVJ420PToJpegString: " << e.what() << std::endl;
ANS_DBG("MEDIA_Jpeg", "avframeYUVJ420PToJpegString exception: %s", e.what());
// ✅ Ensure all allocated frames are freed in case of an exception
if (croppedFrame && croppedFrame != pFrame && croppedFrame != convertedFrame) {
@@ -762,14 +763,14 @@ std::string CVideoPlayer::encodeNV12ToJPEG_TurboJPEG(const AVFrame* pFrame, int
}
// Ensure TurboJPEG instance is valid
if (!_tjInstance) {
std::cerr << "TurboJPEG instance is not initialized." << std::endl;
ANS_DBG("MEDIA_Jpeg", "TurboJPEG instance not initialized");
return "";
}
// Ensure the frame format is NV12
if (pFrame->format != AV_PIX_FMT_NV12) {
std::cerr << "Unsupported format! Expected NV12, got: "
<< av_get_pix_fmt_name((AVPixelFormat)pFrame->format) << std::endl;
ANS_DBG("MEDIA_Jpeg", "TurboJPEG: unsupported format (expected NV12, got %s)",
av_get_pix_fmt_name((AVPixelFormat)pFrame->format));
return "";
}
@@ -860,7 +861,7 @@ std::string CVideoPlayer::encodeNV12ToJPEG_TurboJPEG(const AVFrame* pFrame, int
);
if (ret < 0) {
std::cerr << "TurboJPEG compression failed: " << tjGetErrorStr() << std::endl;
ANS_DBG("MEDIA_Jpeg", "TurboJPEG compression failed: %s", tjGetErrorStr());
return "";
}
@@ -875,7 +876,7 @@ std::string CVideoPlayer::encodeNV12ToJPEG_TurboJPEG(const AVFrame* pFrame, int
return std::string(reinterpret_cast<char*>(jpegDataPtr), jpegSize);
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeNV12ToJpegStringUsingTurboJPEG: " << e.what() << std::endl;
ANS_DBG("MEDIA_Jpeg", "avframeNV12ToJpegStringUsingTurboJPEG exception: %s", e.what());
return ""; // Return empty string on error
}
}
@@ -890,7 +891,7 @@ std::string CVideoPlayer::encodeNV12ToJPEG_FFmpeg(const AVFrame* nv12Frame, int
}
if (nv12Frame->format != AV_PIX_FMT_NV12) {
std::cerr << "Invalid frame format! Expected NV12." << std::endl;
ANS_DBG("MEDIA_Convert", "invalid frame format (expected NV12)");
return "";
}
@@ -900,13 +901,13 @@ std::string CVideoPlayer::encodeNV12ToJPEG_FFmpeg(const AVFrame* nv12Frame, int
// ✅ Find and allocate MJPEG encoder
AVCodec* jpegCodec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
if (!jpegCodec) {
std::cerr << "MJPEG encoder not found!" << std::endl;
ANS_DBG("MEDIA_Jpeg", "MJPEG encoder not found");
return "";
}
codecCtx = avcodec_alloc_context3(jpegCodec);
if (!codecCtx) {
std::cerr << "Failed to allocate codec context!" << std::endl;
ANS_DBG("MEDIA_Jpeg", "failed to allocate codec context (NV12)");
return "";
}
@@ -922,7 +923,7 @@ std::string CVideoPlayer::encodeNV12ToJPEG_FFmpeg(const AVFrame* nv12Frame, int
codecCtx->global_quality = quality * FF_QP2LAMBDA;
if (avcodec_open2(codecCtx, jpegCodec, nullptr) < 0) {
std::cerr << "Failed to open MJPEG encoder!" << std::endl;
ANS_DBG("MEDIA_Jpeg", "failed to open MJPEG encoder");
avcodec_free_context(&codecCtx);
return "";
}
@@ -930,7 +931,7 @@ std::string CVideoPlayer::encodeNV12ToJPEG_FFmpeg(const AVFrame* nv12Frame, int
// ✅ Allocate YUVJ420P frame
yuvjFrame = av_frame_alloc();
if (!yuvjFrame) {
std::cerr << "Failed to allocate YUVJ420P frame!" << std::endl;
ANS_DBG("MEDIA_Jpeg", "failed to allocate YUVJ420P frame");
avcodec_free_context(&codecCtx);
return "";
}
@@ -940,7 +941,7 @@ std::string CVideoPlayer::encodeNV12ToJPEG_FFmpeg(const AVFrame* nv12Frame, int
yuvjFrame->height = height;
if (av_frame_get_buffer(yuvjFrame, 32) < 0) {
std::cerr << "Failed to allocate buffer for YUVJ420P frame!" << std::endl;
ANS_DBG("MEDIA_Jpeg", "failed to allocate buffer for YUVJ420P frame");
av_frame_free(&yuvjFrame);
avcodec_free_context(&codecCtx);
return "";
@@ -994,7 +995,7 @@ std::string CVideoPlayer::encodeNV12ToJPEG_FFmpeg(const AVFrame* nv12Frame, int
return isSuccess ? jpegData : "";
}
catch (const std::exception& e) {
std::cerr << "Exception in encodeNV12ToJPEG_FFmpeg: " << e.what() << std::endl;
ANS_DBG("MEDIA_Jpeg", "encodeNV12ToJPEG_FFmpeg exception: %s", e.what());
}
// ✅ Ensure memory cleanup in case of exceptions
@@ -1019,7 +1020,7 @@ std::string CVideoPlayer::avframeToJpegString(const AVFrame* spFrame) {
}
if (!spFrame || !spFrame->data[0] || spFrame->width <= 10 || spFrame->height <= 10) {
std::cerr << "Invalid or empty frame data, or invalid dimensions." << std::endl;
ANS_DBG("MEDIA_Convert", "invalid or empty frame data / dimensions");
return "";
}
@@ -1091,7 +1092,7 @@ std::string CVideoPlayer::avframeToJpegString(const AVFrame* spFrame) {
return isSuccess ? jpegData : "";
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeToJpegString: " << e.what() << std::endl;
ANS_DBG("MEDIA_Jpeg", "avframeToJpegString exception: %s", e.what());
// ✅ Cleanup memory in case of exceptions
if (croppedFrame && croppedFrame != pFrame && croppedFrame != convertedFrame) {
@@ -1159,7 +1160,7 @@ bool CVideoPlayer::areFramesIdentical(AVFrame* frame1, AVFrame* frame2) {
return true; // If all planes match
}
catch (const std::exception& e) {
std::cerr << "Exception in areFramesIdentical: " << e.what() << std::endl;
ANS_DBG("MEDIA_Convert", "areFramesIdentical exception: %s", e.what());
return false;
}
}
@@ -1169,9 +1170,9 @@ void CVideoPlayer::initSwsContext(int width, int height, AVPixelFormat pixFmt, A
try {
// Validate input dimensions and pixel format
if (width <= 0 || height <= 0 || pixFmt == AV_PIX_FMT_NONE) {
std::cerr << "Invalid parameters: width=" << width
<< ", height=" << height
<< ", pixFmt=" << pixFmt << std::endl;
ANS_DBG("MEDIA_Convert",
"initSwsContext invalid params: w=%d h=%d pixFmt=%d",
width, height, (int)pixFmt);
return;
}
@@ -1199,10 +1200,9 @@ void CVideoPlayer::initSwsContext(int width, int height, AVPixelFormat pixFmt, A
// Check for errors in SwsContext creation
if (!swsCtx) {
std::cerr << "Failed to create SwsContext: width=" << width
<< ", height=" << height
<< ", inputPixFmt=" << pixFmt
<< ", outputPixFmt=" << outputPixFmt << std::endl;
ANS_DBG("MEDIA_Convert",
"sws_getContext failed: w=%d h=%d in=%d out=%d",
width, height, (int)pixFmt, (int)outputPixFmt);
return;
}
@@ -1213,17 +1213,17 @@ void CVideoPlayer::initSwsContext(int width, int height, AVPixelFormat pixFmt, A
lastOutPixFmt = outputPixFmt;
}
catch (const std::exception& e) {
std::cerr << "Exception in initSwsContext: " << e.what() << std::endl;
ANS_DBG("MEDIA_Convert", "initSwsContext exception: %s", e.what());
}
catch (...) {
std::cerr << "Unknown exception in initSwsContext." << std::endl;
ANS_DBG("MEDIA_Convert", "initSwsContext unknown exception");
}
}
cv::Mat CVideoPlayer::avframeAnyToCvmat(const AVFrame* frame) {
std::lock_guard<std::recursive_mutex> lock(_mutex); // Protect against concurrent access
try {
if (!frame || !frame->data[0] || frame->width <= 10 || frame->height <= 10) {
std::cerr << "Invalid or empty frame data, or invalid dimensions." << std::endl;
ANS_DBG("MEDIA_Convert", "invalid or empty frame data / dimensions");
return cv::Mat(); // Return an empty matrix if the frame is invalid
}
initSwsContext(frame->width, frame->height, static_cast<AVPixelFormat>(frame->format), AV_PIX_FMT_BGR24);
@@ -1237,14 +1237,14 @@ cv::Mat CVideoPlayer::avframeAnyToCvmat(const AVFrame* frame) {
// Perform the conversion using sws_scale
int result = sws_scale(swsCtx, frame->data, frame->linesize, 0, frame->height, dst, dstStride);
if (result < 0) {
std::cerr << "Failed to scale the frame." << std::endl;
ANS_DBG("MEDIA_Convert", "sws_scale failed");
return cv::Mat(); // Return an empty matrix if scaling fails
}
return image; // Return the successfully converted OpenCV Mat
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeToCvmat: " << e.what() << std::endl;
ANS_DBG("MEDIA_Convert", "avframeToCvmat exception: %s", e.what());
return cv::Mat(); // Return an empty matrix on error
}
}
@@ -1254,7 +1254,7 @@ cv::Mat CVideoPlayer::avframeYUVJ420PToCvmat(const AVFrame* frame) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
if (!frame || !frame->data[0] || frame->width <= 10 || frame->height <= 10) {
std::cerr << "Invalid or empty frame data, or invalid dimensions." << std::endl;
ANS_DBG("MEDIA_Convert", "invalid or empty frame data / dimensions");
return cv::Mat();
}
@@ -1304,7 +1304,7 @@ cv::Mat CVideoPlayer::avframeYUVJ420PToCvmat(const AVFrame* frame) {
return image; // Return the converted OpenCV Mat (BGR)
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeToCvmatYUVJ420P: " << e.what() << std::endl;
ANS_DBG("MEDIA_Convert", "avframeToCvmatYUVJ420P exception: %s", e.what());
return cv::Mat(); // Return an empty matrix on error
}
}
@@ -1359,7 +1359,7 @@ void CVideoPlayer::initNV12SwsContext(const AVFrame* frame) {
nullptr, nullptr, nullptr);
if (!m_nv12SwsCtx) {
std::cerr << "Failed to create NV12 SwsContext" << std::endl;
ANS_DBG("MEDIA_Convert", "failed to create NV12 SwsContext");
return;
}
@@ -1383,7 +1383,7 @@ cv::Mat CVideoPlayer::avframeNV12ToCvMat(const AVFrame* frame)
{
try {
if (!frame || frame->width <= 0 || frame->height <= 0) {
std::cerr << "Invalid frame! Either null, incorrect format, or zero dimensions." << std::endl;
ANS_DBG("MEDIA_Convert", "invalid frame: null/wrong-format/zero-dims");
return cv::Mat();
}
// Software decode handler
@@ -1417,7 +1417,7 @@ cv::Mat CVideoPlayer::avframeNV12ToCvMat(const AVFrame* frame)
return bgrImage;
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeNV12ToCvMat: " << e.what() << std::endl;
ANS_DBG("MEDIA_Convert", "avframeNV12ToCvMat exception: %s", e.what());
return cv::Mat();
}
}
@@ -1496,7 +1496,7 @@ cv::Mat CVideoPlayer::avframeYUV420PToCvMat(const AVFrame* frame) {
bgrImage.data, static_cast<int>(bgrImage.step),
width, height);
if (ret != 0) {
std::cerr << "libyuv::I420/J420ToRGB24 failed with ret=" << ret << std::endl;
ANS_DBG("MEDIA_Convert", "libyuv I420/J420ToRGB24 failed ret=%d %dx%d", ret, width, height);
return cv::Mat();
}
if (m_nImageQuality == 1) {
@@ -1589,7 +1589,7 @@ cv::Mat CVideoPlayer::avframeYUV420PToCvMat(const AVFrame* frame) {
#endif // ANSCORE_HAS_LIBYUV
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeYUV420PToCvMat: " << e.what() << std::endl;
ANS_DBG("MEDIA_Convert", "avframeYUV420PToCvMat exception: %s", e.what());
return cv::Mat();
}
}
@@ -1618,7 +1618,7 @@ cv::Mat CVideoPlayer::avframeYUV422PToCvMat(const AVFrame* frame) {
frame->data[2], frame->linesize[2],
s_argbBuf.data(), argbStride, width, height);
if (r1 != 0) {
std::cerr << "libyuv::J422ToARGB failed ret=" << r1 << std::endl;
ANS_DBG("MEDIA_Convert", "libyuv J422ToARGB failed ret=%d", r1);
return cv::Mat();
}
ret = libyuv::ARGBToRGB24(
@@ -1634,7 +1634,7 @@ cv::Mat CVideoPlayer::avframeYUV422PToCvMat(const AVFrame* frame) {
width, height);
}
if (ret != 0) {
std::cerr << "libyuv::I422/J422ToRGB24 failed ret=" << ret << std::endl;
ANS_DBG("MEDIA_Convert", "libyuv I422/J422ToRGB24 failed ret=%d", ret);
return cv::Mat();
}
if (m_nImageQuality == 1) {
@@ -1646,7 +1646,7 @@ cv::Mat CVideoPlayer::avframeYUV422PToCvMat(const AVFrame* frame) {
#endif
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeYUV422PToCvMat: " << e.what() << std::endl;
ANS_DBG("MEDIA_Convert", "avframeYUV422PToCvMat exception: %s", e.what());
return cv::Mat();
}
}
@@ -1672,7 +1672,7 @@ cv::Mat CVideoPlayer::avframeYUV444PToCvMat(const AVFrame* frame) {
frame->data[2], frame->linesize[2],
s_argbBuf.data(), argbStride, width, height);
if (r1 != 0) {
std::cerr << "libyuv::J444ToARGB failed ret=" << r1 << std::endl;
ANS_DBG("MEDIA_Convert", "libyuv J444ToARGB failed ret=%d", r1);
return cv::Mat();
}
ret = libyuv::ARGBToRGB24(
@@ -1688,7 +1688,7 @@ cv::Mat CVideoPlayer::avframeYUV444PToCvMat(const AVFrame* frame) {
width, height);
}
if (ret != 0) {
std::cerr << "libyuv::I444/J444ToRGB24 failed ret=" << ret << std::endl;
ANS_DBG("MEDIA_Convert", "libyuv I444/J444ToRGB24 failed ret=%d", ret);
return cv::Mat();
}
if (m_nImageQuality == 1) {
@@ -1700,7 +1700,7 @@ cv::Mat CVideoPlayer::avframeYUV444PToCvMat(const AVFrame* frame) {
#endif
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeYUV444PToCvMat: " << e.what() << std::endl;
ANS_DBG("MEDIA_Convert", "avframeYUV444PToCvMat exception: %s", e.what());
return cv::Mat();
}
}
@@ -1728,7 +1728,7 @@ cv::Mat CVideoPlayer::avframeYUV420P10LEToCvMat(const AVFrame* frame) {
reinterpret_cast<const uint16_t*>(frame->data[2]), frame->linesize[2] / 2,
s_argbBuf.data(), argbStride, width, height);
if (r1 != 0) {
std::cerr << "libyuv::I010ToARGB failed ret=" << r1 << std::endl;
ANS_DBG("MEDIA_Convert", "libyuv I010ToARGB failed ret=%d", r1);
return cv::Mat();
}
@@ -1738,7 +1738,7 @@ cv::Mat CVideoPlayer::avframeYUV420P10LEToCvMat(const AVFrame* frame) {
bgrImage.data, static_cast<int>(bgrImage.step),
width, height);
if (r2 != 0) {
std::cerr << "libyuv::ARGBToRGB24 failed ret=" << r2 << std::endl;
ANS_DBG("MEDIA_Convert", "libyuv ARGBToRGB24 failed ret=%d", r2);
return cv::Mat();
}
if (m_nImageQuality == 1) {
@@ -1750,7 +1750,7 @@ cv::Mat CVideoPlayer::avframeYUV420P10LEToCvMat(const AVFrame* frame) {
#endif
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeYUV420P10LEToCvMat: " << e.what() << std::endl;
ANS_DBG("MEDIA_Convert", "avframeYUV420P10LEToCvMat exception: %s", e.what());
return cv::Mat();
}
}
@@ -1774,7 +1774,7 @@ cv::Mat CVideoPlayer::avframeYUV422P10LEToCvMat(const AVFrame* frame) {
reinterpret_cast<const uint16_t*>(frame->data[2]), frame->linesize[2] / 2,
s_argbBuf.data(), argbStride, width, height);
if (r1 != 0) {
std::cerr << "libyuv::I210ToARGB failed ret=" << r1 << std::endl;
ANS_DBG("MEDIA_Convert", "libyuv I210ToARGB failed ret=%d", r1);
return cv::Mat();
}
@@ -1784,7 +1784,7 @@ cv::Mat CVideoPlayer::avframeYUV422P10LEToCvMat(const AVFrame* frame) {
bgrImage.data, static_cast<int>(bgrImage.step),
width, height);
if (r2 != 0) {
std::cerr << "libyuv::ARGBToRGB24 failed ret=" << r2 << std::endl;
ANS_DBG("MEDIA_Convert", "libyuv ARGBToRGB24 failed ret=%d", r2);
return cv::Mat();
}
if (m_nImageQuality == 1) {
@@ -1796,7 +1796,7 @@ cv::Mat CVideoPlayer::avframeYUV422P10LEToCvMat(const AVFrame* frame) {
#endif
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeYUV422P10LEToCvMat: " << e.what() << std::endl;
ANS_DBG("MEDIA_Convert", "avframeYUV422P10LEToCvMat exception: %s", e.what());
return cv::Mat();
}
}
@@ -1824,7 +1824,7 @@ cv::Mat CVideoPlayer::avframeYUV444P10LEToCvMat(const AVFrame* frame) {
s_argbBuf.data(), argbStride,
&libyuv::kYuvI601Constants, width, height);
if (r1 != 0) {
std::cerr << "libyuv::I410ToARGBMatrix failed ret=" << r1 << std::endl;
ANS_DBG("MEDIA_Convert", "libyuv I410ToARGBMatrix failed ret=%d", r1);
return cv::Mat();
}
@@ -1834,7 +1834,7 @@ cv::Mat CVideoPlayer::avframeYUV444P10LEToCvMat(const AVFrame* frame) {
bgrImage.data, static_cast<int>(bgrImage.step),
width, height);
if (r2 != 0) {
std::cerr << "libyuv::ARGBToRGB24 failed ret=" << r2 << std::endl;
ANS_DBG("MEDIA_Convert", "libyuv ARGBToRGB24 failed ret=%d", r2);
return cv::Mat();
}
if (m_nImageQuality == 1) {
@@ -1846,7 +1846,7 @@ cv::Mat CVideoPlayer::avframeYUV444P10LEToCvMat(const AVFrame* frame) {
#endif
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeYUV444P10LEToCvMat: " << e.what() << std::endl;
ANS_DBG("MEDIA_Convert", "avframeYUV444P10LEToCvMat exception: %s", e.what());
return cv::Mat();
}
}
@@ -1873,7 +1873,7 @@ cv::Mat CVideoPlayer::avframeYUV420P12LEToCvMat(const AVFrame* frame) {
s_argbBuf.data(), argbStride,
&libyuv::kYuvI601Constants, width, height);
if (r1 != 0) {
std::cerr << "libyuv::I012ToARGBMatrix failed ret=" << r1 << std::endl;
ANS_DBG("MEDIA_Convert", "libyuv I012ToARGBMatrix failed ret=%d", r1);
return cv::Mat();
}
@@ -1883,7 +1883,7 @@ cv::Mat CVideoPlayer::avframeYUV420P12LEToCvMat(const AVFrame* frame) {
bgrImage.data, static_cast<int>(bgrImage.step),
width, height);
if (r2 != 0) {
std::cerr << "libyuv::ARGBToRGB24 failed ret=" << r2 << std::endl;
ANS_DBG("MEDIA_Convert", "libyuv ARGBToRGB24 failed ret=%d", r2);
return cv::Mat();
}
if (m_nImageQuality == 1) {
@@ -1895,7 +1895,7 @@ cv::Mat CVideoPlayer::avframeYUV420P12LEToCvMat(const AVFrame* frame) {
#endif
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeYUV420P12LEToCvMat: " << e.what() << std::endl;
ANS_DBG("MEDIA_Convert", "avframeYUV420P12LEToCvMat exception: %s", e.what());
return cv::Mat();
}
}
@@ -1930,7 +1930,7 @@ cv::Mat CVideoPlayer::avframeYUV422P12LEToCvMat(const AVFrame* frame) {
v8, uvW,
width, height);
if (r1 != 0) {
std::cerr << "libyuv::I212ToI422 failed ret=" << r1 << std::endl;
ANS_DBG("MEDIA_Convert", "libyuv I212ToI422 failed ret=%d", r1);
return cv::Mat();
}
@@ -1942,7 +1942,7 @@ cv::Mat CVideoPlayer::avframeYUV422P12LEToCvMat(const AVFrame* frame) {
bgrImage.data, static_cast<int>(bgrImage.step),
width, height);
if (r2 != 0) {
std::cerr << "libyuv::I422ToRGB24 failed ret=" << r2 << std::endl;
ANS_DBG("MEDIA_Convert", "libyuv I422ToRGB24 failed ret=%d", r2);
return cv::Mat();
}
if (m_nImageQuality == 1) {
@@ -1954,7 +1954,7 @@ cv::Mat CVideoPlayer::avframeYUV422P12LEToCvMat(const AVFrame* frame) {
#endif
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeYUV422P12LEToCvMat: " << e.what() << std::endl;
ANS_DBG("MEDIA_Convert", "avframeYUV422P12LEToCvMat exception: %s", e.what());
return cv::Mat();
}
}
@@ -1987,7 +1987,7 @@ cv::Mat CVideoPlayer::avframeYUV444P12LEToCvMat(const AVFrame* frame) {
v8, width,
width, height);
if (r1 != 0) {
std::cerr << "libyuv::I412ToI444 failed ret=" << r1 << std::endl;
ANS_DBG("MEDIA_Convert", "libyuv I412ToI444 failed ret=%d", r1);
return cv::Mat();
}
@@ -1999,7 +1999,7 @@ cv::Mat CVideoPlayer::avframeYUV444P12LEToCvMat(const AVFrame* frame) {
bgrImage.data, static_cast<int>(bgrImage.step),
width, height);
if (r2 != 0) {
std::cerr << "libyuv::I444ToRGB24 failed ret=" << r2 << std::endl;
ANS_DBG("MEDIA_Convert", "libyuv I444ToRGB24 failed ret=%d", r2);
return cv::Mat();
}
if (m_nImageQuality == 1) {
@@ -2011,7 +2011,7 @@ cv::Mat CVideoPlayer::avframeYUV444P12LEToCvMat(const AVFrame* frame) {
#endif
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeYUV444P12LEToCvMat: " << e.what() << std::endl;
ANS_DBG("MEDIA_Convert", "avframeYUV444P12LEToCvMat exception: %s", e.what());
return cv::Mat();
}
}
@@ -2024,7 +2024,7 @@ cv::Mat CVideoPlayer::avframeToCVMat(const AVFrame* pFrame) {
try {
// 1. Validate input frame
if (!pFrame || !pFrame->data[0] || pFrame->width <= 10 || pFrame->height <= 10) {
std::cerr << "Invalid or empty frame data, or invalid dimensions." << std::endl;
ANS_DBG("MEDIA_Convert", "invalid or empty frame data / dimensions");
return cv::Mat();
}
@@ -2142,7 +2142,7 @@ cv::Mat CVideoPlayer::avframeToCVMat(const AVFrame* pFrame) {
}
}
catch (const std::exception& e) {
std::cerr << "Exception in avframeToCvMat: " << e.what() << std::endl;
ANS_DBG("MEDIA_Convert", "avframeToCvMat dispatcher exception: %s", e.what());
return cv::Mat(); // Return an empty matrix on error
}
}
+4 -1
View File
@@ -18,6 +18,7 @@
#include <turbojpeg.h>
#include <atomic>
#include <chrono>
#include "ANSLicense.h" // ANS_DBG (used by inline FrameQueue::pushFrame below)
// Leak diagnostics — net counters surfaced in MEDIA_Leak heartbeat.
// Defined in video_player.cpp; also incremented from FrameQueue here.
@@ -71,7 +72,9 @@ public:
// Clone the frame to ensure proper ownership transfer
AVFrame* frameCopy = av_frame_clone(newFrame);
if (!frameCopy) {
std::cerr << "Failed to clone AVFrame!" << std::endl;
// av_frame_clone failure (basically OOM). Gated to avoid spamming
// stderr during memory pressure.
ANS_DBG("MEDIA_Queue", "av_frame_clone failed (OOM?)");
return;
}
g_queueClones.fetch_add(1, std::memory_order_relaxed);